gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71
72 #include <fcntl.h>
73 #include "gdb_string.h"
74 #include "gdb_assert.h"
75 #include <sys/types.h>
76
77 typedef struct symbol *symbolp;
78 DEF_VEC_P (symbolp);
79
80 /* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82 static int dwarf2_read_debug = 0;
83
84 /* When non-zero, dump DIEs after they are read in. */
85 static unsigned int dwarf2_die_debug = 0;
86
87 /* When non-zero, cross-check physname against demangler. */
88 static int check_physname = 0;
89
90 /* When non-zero, do not reject deprecated .gdb_index sections. */
91 static int use_deprecated_index_sections = 0;
92
93 static const struct objfile_data *dwarf2_objfile_data_key;
94
95 struct dwarf2_section_info
96 {
97 asection *asection;
98 gdb_byte *buffer;
99 bfd_size_type size;
100 /* True if we have tried to read this section. */
101 int readin;
102 };
103
104 typedef struct dwarf2_section_info dwarf2_section_info_def;
105 DEF_VEC_O (dwarf2_section_info_def);
106
107 /* All offsets in the index are of this type. It must be
108 architecture-independent. */
109 typedef uint32_t offset_type;
110
111 DEF_VEC_I (offset_type);
112
113 /* Ensure only legit values are used. */
114 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
115 do { \
116 gdb_assert ((unsigned int) (value) <= 1); \
117 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
118 } while (0)
119
120 /* Ensure only legit values are used. */
121 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
122 do { \
123 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
124 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
125 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
129 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
132 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
133 } while (0)
134
135 /* A description of the mapped index. The file format is described in
136 a comment by the code that writes the index. */
137 struct mapped_index
138 {
139 /* Index data format version. */
140 int version;
141
142 /* The total length of the buffer. */
143 off_t total_size;
144
145 /* A pointer to the address table data. */
146 const gdb_byte *address_table;
147
148 /* Size of the address table data in bytes. */
149 offset_type address_table_size;
150
151 /* The symbol table, implemented as a hash table. */
152 const offset_type *symbol_table;
153
154 /* Size in slots, each slot is 2 offset_types. */
155 offset_type symbol_table_slots;
156
157 /* A pointer to the constant pool. */
158 const char *constant_pool;
159 };
160
161 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
162 DEF_VEC_P (dwarf2_per_cu_ptr);
163
164 /* Collection of data recorded per objfile.
165 This hangs off of dwarf2_objfile_data_key. */
166
167 struct dwarf2_per_objfile
168 {
169 struct dwarf2_section_info info;
170 struct dwarf2_section_info abbrev;
171 struct dwarf2_section_info line;
172 struct dwarf2_section_info loc;
173 struct dwarf2_section_info macinfo;
174 struct dwarf2_section_info macro;
175 struct dwarf2_section_info str;
176 struct dwarf2_section_info ranges;
177 struct dwarf2_section_info addr;
178 struct dwarf2_section_info frame;
179 struct dwarf2_section_info eh_frame;
180 struct dwarf2_section_info gdb_index;
181
182 VEC (dwarf2_section_info_def) *types;
183
184 /* Back link. */
185 struct objfile *objfile;
186
187 /* Table of all the compilation units. This is used to locate
188 the target compilation unit of a particular reference. */
189 struct dwarf2_per_cu_data **all_comp_units;
190
191 /* The number of compilation units in ALL_COMP_UNITS. */
192 int n_comp_units;
193
194 /* The number of .debug_types-related CUs. */
195 int n_type_units;
196
197 /* The .debug_types-related CUs (TUs). */
198 struct signatured_type **all_type_units;
199
200 /* The number of entries in all_type_unit_groups. */
201 int n_type_unit_groups;
202
203 /* Table of type unit groups.
204 This exists to make it easy to iterate over all CUs and TU groups. */
205 struct type_unit_group **all_type_unit_groups;
206
207 /* Table of struct type_unit_group objects.
208 The hash key is the DW_AT_stmt_list value. */
209 htab_t type_unit_groups;
210
211 /* A table mapping .debug_types signatures to its signatured_type entry.
212 This is NULL if the .debug_types section hasn't been read in yet. */
213 htab_t signatured_types;
214
215 /* Type unit statistics, to see how well the scaling improvements
216 are doing. */
217 struct tu_stats
218 {
219 int nr_uniq_abbrev_tables;
220 int nr_symtabs;
221 int nr_symtab_sharers;
222 int nr_stmt_less_type_units;
223 } tu_stats;
224
225 /* A chain of compilation units that are currently read in, so that
226 they can be freed later. */
227 struct dwarf2_per_cu_data *read_in_chain;
228
229 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
230 This is NULL if the table hasn't been allocated yet. */
231 htab_t dwo_files;
232
233 /* Non-zero if we've check for whether there is a DWP file. */
234 int dwp_checked;
235
236 /* The DWP file if there is one, or NULL. */
237 struct dwp_file *dwp_file;
238
239 /* The shared '.dwz' file, if one exists. This is used when the
240 original data was compressed using 'dwz -m'. */
241 struct dwz_file *dwz_file;
242
243 /* A flag indicating wether this objfile has a section loaded at a
244 VMA of 0. */
245 int has_section_at_zero;
246
247 /* True if we are using the mapped index,
248 or we are faking it for OBJF_READNOW's sake. */
249 unsigned char using_index;
250
251 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
252 struct mapped_index *index_table;
253
254 /* When using index_table, this keeps track of all quick_file_names entries.
255 TUs typically share line table entries with a CU, so we maintain a
256 separate table of all line table entries to support the sharing.
257 Note that while there can be way more TUs than CUs, we've already
258 sorted all the TUs into "type unit groups", grouped by their
259 DW_AT_stmt_list value. Therefore the only sharing done here is with a
260 CU and its associated TU group if there is one. */
261 htab_t quick_file_names_table;
262
263 /* Set during partial symbol reading, to prevent queueing of full
264 symbols. */
265 int reading_partial_symbols;
266
267 /* Table mapping type DIEs to their struct type *.
268 This is NULL if not allocated yet.
269 The mapping is done via (CU/TU signature + DIE offset) -> type. */
270 htab_t die_type_hash;
271
272 /* The CUs we recently read. */
273 VEC (dwarf2_per_cu_ptr) *just_read_cus;
274 };
275
276 static struct dwarf2_per_objfile *dwarf2_per_objfile;
277
278 /* Default names of the debugging sections. */
279
280 /* Note that if the debugging section has been compressed, it might
281 have a name like .zdebug_info. */
282
283 static const struct dwarf2_debug_sections dwarf2_elf_names =
284 {
285 { ".debug_info", ".zdebug_info" },
286 { ".debug_abbrev", ".zdebug_abbrev" },
287 { ".debug_line", ".zdebug_line" },
288 { ".debug_loc", ".zdebug_loc" },
289 { ".debug_macinfo", ".zdebug_macinfo" },
290 { ".debug_macro", ".zdebug_macro" },
291 { ".debug_str", ".zdebug_str" },
292 { ".debug_ranges", ".zdebug_ranges" },
293 { ".debug_types", ".zdebug_types" },
294 { ".debug_addr", ".zdebug_addr" },
295 { ".debug_frame", ".zdebug_frame" },
296 { ".eh_frame", NULL },
297 { ".gdb_index", ".zgdb_index" },
298 23
299 };
300
301 /* List of DWO/DWP sections. */
302
303 static const struct dwop_section_names
304 {
305 struct dwarf2_section_names abbrev_dwo;
306 struct dwarf2_section_names info_dwo;
307 struct dwarf2_section_names line_dwo;
308 struct dwarf2_section_names loc_dwo;
309 struct dwarf2_section_names macinfo_dwo;
310 struct dwarf2_section_names macro_dwo;
311 struct dwarf2_section_names str_dwo;
312 struct dwarf2_section_names str_offsets_dwo;
313 struct dwarf2_section_names types_dwo;
314 struct dwarf2_section_names cu_index;
315 struct dwarf2_section_names tu_index;
316 }
317 dwop_section_names =
318 {
319 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
320 { ".debug_info.dwo", ".zdebug_info.dwo" },
321 { ".debug_line.dwo", ".zdebug_line.dwo" },
322 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
323 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
324 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
325 { ".debug_str.dwo", ".zdebug_str.dwo" },
326 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
327 { ".debug_types.dwo", ".zdebug_types.dwo" },
328 { ".debug_cu_index", ".zdebug_cu_index" },
329 { ".debug_tu_index", ".zdebug_tu_index" },
330 };
331
332 /* local data types */
333
334 /* The data in a compilation unit header, after target2host
335 translation, looks like this. */
336 struct comp_unit_head
337 {
338 unsigned int length;
339 short version;
340 unsigned char addr_size;
341 unsigned char signed_addr_p;
342 sect_offset abbrev_offset;
343
344 /* Size of file offsets; either 4 or 8. */
345 unsigned int offset_size;
346
347 /* Size of the length field; either 4 or 12. */
348 unsigned int initial_length_size;
349
350 /* Offset to the first byte of this compilation unit header in the
351 .debug_info section, for resolving relative reference dies. */
352 sect_offset offset;
353
354 /* Offset to first die in this cu from the start of the cu.
355 This will be the first byte following the compilation unit header. */
356 cu_offset first_die_offset;
357 };
358
359 /* Type used for delaying computation of method physnames.
360 See comments for compute_delayed_physnames. */
361 struct delayed_method_info
362 {
363 /* The type to which the method is attached, i.e., its parent class. */
364 struct type *type;
365
366 /* The index of the method in the type's function fieldlists. */
367 int fnfield_index;
368
369 /* The index of the method in the fieldlist. */
370 int index;
371
372 /* The name of the DIE. */
373 const char *name;
374
375 /* The DIE associated with this method. */
376 struct die_info *die;
377 };
378
379 typedef struct delayed_method_info delayed_method_info;
380 DEF_VEC_O (delayed_method_info);
381
382 /* Internal state when decoding a particular compilation unit. */
383 struct dwarf2_cu
384 {
385 /* The objfile containing this compilation unit. */
386 struct objfile *objfile;
387
388 /* The header of the compilation unit. */
389 struct comp_unit_head header;
390
391 /* Base address of this compilation unit. */
392 CORE_ADDR base_address;
393
394 /* Non-zero if base_address has been set. */
395 int base_known;
396
397 /* The language we are debugging. */
398 enum language language;
399 const struct language_defn *language_defn;
400
401 const char *producer;
402
403 /* The generic symbol table building routines have separate lists for
404 file scope symbols and all all other scopes (local scopes). So
405 we need to select the right one to pass to add_symbol_to_list().
406 We do it by keeping a pointer to the correct list in list_in_scope.
407
408 FIXME: The original dwarf code just treated the file scope as the
409 first local scope, and all other local scopes as nested local
410 scopes, and worked fine. Check to see if we really need to
411 distinguish these in buildsym.c. */
412 struct pending **list_in_scope;
413
414 /* The abbrev table for this CU.
415 Normally this points to the abbrev table in the objfile.
416 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
417 struct abbrev_table *abbrev_table;
418
419 /* Hash table holding all the loaded partial DIEs
420 with partial_die->offset.SECT_OFF as hash. */
421 htab_t partial_dies;
422
423 /* Storage for things with the same lifetime as this read-in compilation
424 unit, including partial DIEs. */
425 struct obstack comp_unit_obstack;
426
427 /* When multiple dwarf2_cu structures are living in memory, this field
428 chains them all together, so that they can be released efficiently.
429 We will probably also want a generation counter so that most-recently-used
430 compilation units are cached... */
431 struct dwarf2_per_cu_data *read_in_chain;
432
433 /* Backchain to our per_cu entry if the tree has been built. */
434 struct dwarf2_per_cu_data *per_cu;
435
436 /* How many compilation units ago was this CU last referenced? */
437 int last_used;
438
439 /* A hash table of DIE cu_offset for following references with
440 die_info->offset.sect_off as hash. */
441 htab_t die_hash;
442
443 /* Full DIEs if read in. */
444 struct die_info *dies;
445
446 /* A set of pointers to dwarf2_per_cu_data objects for compilation
447 units referenced by this one. Only set during full symbol processing;
448 partial symbol tables do not have dependencies. */
449 htab_t dependencies;
450
451 /* Header data from the line table, during full symbol processing. */
452 struct line_header *line_header;
453
454 /* A list of methods which need to have physnames computed
455 after all type information has been read. */
456 VEC (delayed_method_info) *method_list;
457
458 /* To be copied to symtab->call_site_htab. */
459 htab_t call_site_htab;
460
461 /* Non-NULL if this CU came from a DWO file.
462 There is an invariant here that is important to remember:
463 Except for attributes copied from the top level DIE in the "main"
464 (or "stub") file in preparation for reading the DWO file
465 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
466 Either there isn't a DWO file (in which case this is NULL and the point
467 is moot), or there is and either we're not going to read it (in which
468 case this is NULL) or there is and we are reading it (in which case this
469 is non-NULL). */
470 struct dwo_unit *dwo_unit;
471
472 /* The DW_AT_addr_base attribute if present, zero otherwise
473 (zero is a valid value though).
474 Note this value comes from the stub CU/TU's DIE. */
475 ULONGEST addr_base;
476
477 /* The DW_AT_ranges_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 Also note that the value is zero in the non-DWO case so this value can
481 be used without needing to know whether DWO files are in use or not.
482 N.B. This does not apply to DW_AT_ranges appearing in
483 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
484 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
485 DW_AT_ranges_base *would* have to be applied, and we'd have to care
486 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
487 ULONGEST ranges_base;
488
489 /* Mark used when releasing cached dies. */
490 unsigned int mark : 1;
491
492 /* This CU references .debug_loc. See the symtab->locations_valid field.
493 This test is imperfect as there may exist optimized debug code not using
494 any location list and still facing inlining issues if handled as
495 unoptimized code. For a future better test see GCC PR other/32998. */
496 unsigned int has_loclist : 1;
497
498 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
499 if all the producer_is_* fields are valid. This information is cached
500 because profiling CU expansion showed excessive time spent in
501 producer_is_gxx_lt_4_6. */
502 unsigned int checked_producer : 1;
503 unsigned int producer_is_gxx_lt_4_6 : 1;
504 unsigned int producer_is_gcc_lt_4_3 : 1;
505 unsigned int producer_is_icc : 1;
506
507 /* When set, the file that we're processing is known to have
508 debugging info for C++ namespaces. GCC 3.3.x did not produce
509 this information, but later versions do. */
510
511 unsigned int processing_has_namespace_info : 1;
512 };
513
514 /* Persistent data held for a compilation unit, even when not
515 processing it. We put a pointer to this structure in the
516 read_symtab_private field of the psymtab. */
517
518 struct dwarf2_per_cu_data
519 {
520 /* The start offset and length of this compilation unit.
521 NOTE: Unlike comp_unit_head.length, this length includes
522 initial_length_size.
523 If the DIE refers to a DWO file, this is always of the original die,
524 not the DWO file. */
525 sect_offset offset;
526 unsigned int length;
527
528 /* Flag indicating this compilation unit will be read in before
529 any of the current compilation units are processed. */
530 unsigned int queued : 1;
531
532 /* This flag will be set when reading partial DIEs if we need to load
533 absolutely all DIEs for this compilation unit, instead of just the ones
534 we think are interesting. It gets set if we look for a DIE in the
535 hash table and don't find it. */
536 unsigned int load_all_dies : 1;
537
538 /* Non-zero if this CU is from .debug_types. */
539 unsigned int is_debug_types : 1;
540
541 /* Non-zero if this CU is from the .dwz file. */
542 unsigned int is_dwz : 1;
543
544 /* The section this CU/TU lives in.
545 If the DIE refers to a DWO file, this is always the original die,
546 not the DWO file. */
547 struct dwarf2_section_info *info_or_types_section;
548
549 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
550 of the CU cache it gets reset to NULL again. */
551 struct dwarf2_cu *cu;
552
553 /* The corresponding objfile.
554 Normally we can get the objfile from dwarf2_per_objfile.
555 However we can enter this file with just a "per_cu" handle. */
556 struct objfile *objfile;
557
558 /* When using partial symbol tables, the 'psymtab' field is active.
559 Otherwise the 'quick' field is active. */
560 union
561 {
562 /* The partial symbol table associated with this compilation unit,
563 or NULL for unread partial units. */
564 struct partial_symtab *psymtab;
565
566 /* Data needed by the "quick" functions. */
567 struct dwarf2_per_cu_quick_data *quick;
568 } v;
569
570 /* The CUs we import using DW_TAG_imported_unit. This is filled in
571 while reading psymtabs, used to compute the psymtab dependencies,
572 and then cleared. Then it is filled in again while reading full
573 symbols, and only deleted when the objfile is destroyed.
574
575 This is also used to work around a difference between the way gold
576 generates .gdb_index version <=7 and the way gdb does. Arguably this
577 is a gold bug. For symbols coming from TUs, gold records in the index
578 the CU that includes the TU instead of the TU itself. This breaks
579 dw2_lookup_symbol: It assumes that if the index says symbol X lives
580 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
581 will find X. Alas TUs live in their own symtab, so after expanding CU Y
582 we need to look in TU Z to find X. Fortunately, this is akin to
583 DW_TAG_imported_unit, so we just use the same mechanism: For
584 .gdb_index version <=7 this also records the TUs that the CU referred
585 to. Concurrently with this change gdb was modified to emit version 8
586 indices so we only pay a price for gold generated indices. */
587 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
588
589 /* Type units are grouped by their DW_AT_stmt_list entry so that they
590 can share them. If this is a TU, this points to the containing
591 symtab. */
592 struct type_unit_group *type_unit_group;
593 };
594
595 /* Entry in the signatured_types hash table. */
596
597 struct signatured_type
598 {
599 /* The "per_cu" object of this type.
600 N.B.: This is the first member so that it's easy to convert pointers
601 between them. */
602 struct dwarf2_per_cu_data per_cu;
603
604 /* The type's signature. */
605 ULONGEST signature;
606
607 /* Offset in the TU of the type's DIE, as read from the TU header.
608 If the definition lives in a DWO file, this value is unusable. */
609 cu_offset type_offset_in_tu;
610
611 /* Offset in the section of the type's DIE.
612 If the definition lives in a DWO file, this is the offset in the
613 .debug_types.dwo section.
614 The value is zero until the actual value is known.
615 Zero is otherwise not a valid section offset. */
616 sect_offset type_offset_in_section;
617 };
618
619 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
620 This includes type_unit_group and quick_file_names. */
621
622 struct stmt_list_hash
623 {
624 /* The DWO unit this table is from or NULL if there is none. */
625 struct dwo_unit *dwo_unit;
626
627 /* Offset in .debug_line or .debug_line.dwo. */
628 sect_offset line_offset;
629 };
630
631 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
632 an object of this type. */
633
634 struct type_unit_group
635 {
636 /* dwarf2read.c's main "handle" on the symtab.
637 To simplify things we create an artificial CU that "includes" all the
638 type units using this stmt_list so that the rest of the code still has
639 a "per_cu" handle on the symtab.
640 This PER_CU is recognized by having no section. */
641 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
642 struct dwarf2_per_cu_data per_cu;
643
644 union
645 {
646 /* The TUs that share this DW_AT_stmt_list entry.
647 This is added to while parsing type units to build partial symtabs,
648 and is deleted afterwards and not used again. */
649 VEC (dwarf2_per_cu_ptr) *tus;
650
651 /* When reading the line table in "quick" functions, we need a real TU.
652 Any will do, we know they all share the same DW_AT_stmt_list entry.
653 For simplicity's sake, we pick the first one. */
654 struct dwarf2_per_cu_data *first_tu;
655 } t;
656
657 /* The primary symtab.
658 Type units in a group needn't all be defined in the same source file,
659 so we create an essentially anonymous symtab as the primary symtab. */
660 struct symtab *primary_symtab;
661
662 /* The data used to construct the hash key. */
663 struct stmt_list_hash hash;
664
665 /* The number of symtabs from the line header.
666 The value here must match line_header.num_file_names. */
667 unsigned int num_symtabs;
668
669 /* The symbol tables for this TU (obtained from the files listed in
670 DW_AT_stmt_list).
671 WARNING: The order of entries here must match the order of entries
672 in the line header. After the first TU using this type_unit_group, the
673 line header for the subsequent TUs is recreated from this. This is done
674 because we need to use the same symtabs for each TU using the same
675 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
676 there's no guarantee the line header doesn't have duplicate entries. */
677 struct symtab **symtabs;
678 };
679
680 /* These sections are what may appear in a DWO file. */
681
682 struct dwo_sections
683 {
684 struct dwarf2_section_info abbrev;
685 struct dwarf2_section_info line;
686 struct dwarf2_section_info loc;
687 struct dwarf2_section_info macinfo;
688 struct dwarf2_section_info macro;
689 struct dwarf2_section_info str;
690 struct dwarf2_section_info str_offsets;
691 /* In the case of a virtual DWO file, these two are unused. */
692 struct dwarf2_section_info info;
693 VEC (dwarf2_section_info_def) *types;
694 };
695
696 /* Common bits of DWO CUs/TUs. */
697
698 struct dwo_unit
699 {
700 /* Backlink to the containing struct dwo_file. */
701 struct dwo_file *dwo_file;
702
703 /* The "id" that distinguishes this CU/TU.
704 .debug_info calls this "dwo_id", .debug_types calls this "signature".
705 Since signatures came first, we stick with it for consistency. */
706 ULONGEST signature;
707
708 /* The section this CU/TU lives in, in the DWO file. */
709 struct dwarf2_section_info *info_or_types_section;
710
711 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
712 sect_offset offset;
713 unsigned int length;
714
715 /* For types, offset in the type's DIE of the type defined by this TU. */
716 cu_offset type_offset_in_tu;
717 };
718
719 /* Data for one DWO file.
720 This includes virtual DWO files that have been packaged into a
721 DWP file. */
722
723 struct dwo_file
724 {
725 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
726 For virtual DWO files the name is constructed from the section offsets
727 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
728 from related CU+TUs. */
729 const char *name;
730
731 /* The bfd, when the file is open. Otherwise this is NULL.
732 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
733 bfd *dbfd;
734
735 /* Section info for this file. */
736 struct dwo_sections sections;
737
738 /* Table of CUs in the file.
739 Each element is a struct dwo_unit. */
740 htab_t cus;
741
742 /* Table of TUs in the file.
743 Each element is a struct dwo_unit. */
744 htab_t tus;
745 };
746
747 /* These sections are what may appear in a DWP file. */
748
749 struct dwp_sections
750 {
751 struct dwarf2_section_info str;
752 struct dwarf2_section_info cu_index;
753 struct dwarf2_section_info tu_index;
754 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
755 by section number. We don't need to record them here. */
756 };
757
758 /* These sections are what may appear in a virtual DWO file. */
759
760 struct virtual_dwo_sections
761 {
762 struct dwarf2_section_info abbrev;
763 struct dwarf2_section_info line;
764 struct dwarf2_section_info loc;
765 struct dwarf2_section_info macinfo;
766 struct dwarf2_section_info macro;
767 struct dwarf2_section_info str_offsets;
768 /* Each DWP hash table entry records one CU or one TU.
769 That is recorded here, and copied to dwo_unit.info_or_types_section. */
770 struct dwarf2_section_info info_or_types;
771 };
772
773 /* Contents of DWP hash tables. */
774
775 struct dwp_hash_table
776 {
777 uint32_t nr_units, nr_slots;
778 const gdb_byte *hash_table, *unit_table, *section_pool;
779 };
780
781 /* Data for one DWP file. */
782
783 struct dwp_file
784 {
785 /* Name of the file. */
786 const char *name;
787
788 /* The bfd, when the file is open. Otherwise this is NULL. */
789 bfd *dbfd;
790
791 /* Section info for this file. */
792 struct dwp_sections sections;
793
794 /* Table of CUs in the file. */
795 const struct dwp_hash_table *cus;
796
797 /* Table of TUs in the file. */
798 const struct dwp_hash_table *tus;
799
800 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
801 htab_t loaded_cutus;
802
803 /* Table to map ELF section numbers to their sections. */
804 unsigned int num_sections;
805 asection **elf_sections;
806 };
807
808 /* This represents a '.dwz' file. */
809
810 struct dwz_file
811 {
812 /* A dwz file can only contain a few sections. */
813 struct dwarf2_section_info abbrev;
814 struct dwarf2_section_info info;
815 struct dwarf2_section_info str;
816 struct dwarf2_section_info line;
817 struct dwarf2_section_info macro;
818 struct dwarf2_section_info gdb_index;
819
820 /* The dwz's BFD. */
821 bfd *dwz_bfd;
822 };
823
824 /* Struct used to pass misc. parameters to read_die_and_children, et
825 al. which are used for both .debug_info and .debug_types dies.
826 All parameters here are unchanging for the life of the call. This
827 struct exists to abstract away the constant parameters of die reading. */
828
829 struct die_reader_specs
830 {
831 /* die_section->asection->owner. */
832 bfd* abfd;
833
834 /* The CU of the DIE we are parsing. */
835 struct dwarf2_cu *cu;
836
837 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
838 struct dwo_file *dwo_file;
839
840 /* The section the die comes from.
841 This is either .debug_info or .debug_types, or the .dwo variants. */
842 struct dwarf2_section_info *die_section;
843
844 /* die_section->buffer. */
845 gdb_byte *buffer;
846
847 /* The end of the buffer. */
848 const gdb_byte *buffer_end;
849 };
850
851 /* Type of function passed to init_cutu_and_read_dies, et.al. */
852 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
853 gdb_byte *info_ptr,
854 struct die_info *comp_unit_die,
855 int has_children,
856 void *data);
857
858 /* The line number information for a compilation unit (found in the
859 .debug_line section) begins with a "statement program header",
860 which contains the following information. */
861 struct line_header
862 {
863 unsigned int total_length;
864 unsigned short version;
865 unsigned int header_length;
866 unsigned char minimum_instruction_length;
867 unsigned char maximum_ops_per_instruction;
868 unsigned char default_is_stmt;
869 int line_base;
870 unsigned char line_range;
871 unsigned char opcode_base;
872
873 /* standard_opcode_lengths[i] is the number of operands for the
874 standard opcode whose value is i. This means that
875 standard_opcode_lengths[0] is unused, and the last meaningful
876 element is standard_opcode_lengths[opcode_base - 1]. */
877 unsigned char *standard_opcode_lengths;
878
879 /* The include_directories table. NOTE! These strings are not
880 allocated with xmalloc; instead, they are pointers into
881 debug_line_buffer. If you try to free them, `free' will get
882 indigestion. */
883 unsigned int num_include_dirs, include_dirs_size;
884 char **include_dirs;
885
886 /* The file_names table. NOTE! These strings are not allocated
887 with xmalloc; instead, they are pointers into debug_line_buffer.
888 Don't try to free them directly. */
889 unsigned int num_file_names, file_names_size;
890 struct file_entry
891 {
892 char *name;
893 unsigned int dir_index;
894 unsigned int mod_time;
895 unsigned int length;
896 int included_p; /* Non-zero if referenced by the Line Number Program. */
897 struct symtab *symtab; /* The associated symbol table, if any. */
898 } *file_names;
899
900 /* The start and end of the statement program following this
901 header. These point into dwarf2_per_objfile->line_buffer. */
902 gdb_byte *statement_program_start, *statement_program_end;
903 };
904
905 /* When we construct a partial symbol table entry we only
906 need this much information. */
907 struct partial_die_info
908 {
909 /* Offset of this DIE. */
910 sect_offset offset;
911
912 /* DWARF-2 tag for this DIE. */
913 ENUM_BITFIELD(dwarf_tag) tag : 16;
914
915 /* Assorted flags describing the data found in this DIE. */
916 unsigned int has_children : 1;
917 unsigned int is_external : 1;
918 unsigned int is_declaration : 1;
919 unsigned int has_type : 1;
920 unsigned int has_specification : 1;
921 unsigned int has_pc_info : 1;
922 unsigned int may_be_inlined : 1;
923
924 /* Flag set if the SCOPE field of this structure has been
925 computed. */
926 unsigned int scope_set : 1;
927
928 /* Flag set if the DIE has a byte_size attribute. */
929 unsigned int has_byte_size : 1;
930
931 /* Flag set if any of the DIE's children are template arguments. */
932 unsigned int has_template_arguments : 1;
933
934 /* Flag set if fixup_partial_die has been called on this die. */
935 unsigned int fixup_called : 1;
936
937 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
938 unsigned int is_dwz : 1;
939
940 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
941 unsigned int spec_is_dwz : 1;
942
943 /* The name of this DIE. Normally the value of DW_AT_name, but
944 sometimes a default name for unnamed DIEs. */
945 const char *name;
946
947 /* The linkage name, if present. */
948 const char *linkage_name;
949
950 /* The scope to prepend to our children. This is generally
951 allocated on the comp_unit_obstack, so will disappear
952 when this compilation unit leaves the cache. */
953 const char *scope;
954
955 /* Some data associated with the partial DIE. The tag determines
956 which field is live. */
957 union
958 {
959 /* The location description associated with this DIE, if any. */
960 struct dwarf_block *locdesc;
961 /* The offset of an import, for DW_TAG_imported_unit. */
962 sect_offset offset;
963 } d;
964
965 /* If HAS_PC_INFO, the PC range associated with this DIE. */
966 CORE_ADDR lowpc;
967 CORE_ADDR highpc;
968
969 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
970 DW_AT_sibling, if any. */
971 /* NOTE: This member isn't strictly necessary, read_partial_die could
972 return DW_AT_sibling values to its caller load_partial_dies. */
973 gdb_byte *sibling;
974
975 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
976 DW_AT_specification (or DW_AT_abstract_origin or
977 DW_AT_extension). */
978 sect_offset spec_offset;
979
980 /* Pointers to this DIE's parent, first child, and next sibling,
981 if any. */
982 struct partial_die_info *die_parent, *die_child, *die_sibling;
983 };
984
985 /* This data structure holds the information of an abbrev. */
986 struct abbrev_info
987 {
988 unsigned int number; /* number identifying abbrev */
989 enum dwarf_tag tag; /* dwarf tag */
990 unsigned short has_children; /* boolean */
991 unsigned short num_attrs; /* number of attributes */
992 struct attr_abbrev *attrs; /* an array of attribute descriptions */
993 struct abbrev_info *next; /* next in chain */
994 };
995
996 struct attr_abbrev
997 {
998 ENUM_BITFIELD(dwarf_attribute) name : 16;
999 ENUM_BITFIELD(dwarf_form) form : 16;
1000 };
1001
1002 /* Size of abbrev_table.abbrev_hash_table. */
1003 #define ABBREV_HASH_SIZE 121
1004
1005 /* Top level data structure to contain an abbreviation table. */
1006
1007 struct abbrev_table
1008 {
1009 /* Where the abbrev table came from.
1010 This is used as a sanity check when the table is used. */
1011 sect_offset offset;
1012
1013 /* Storage for the abbrev table. */
1014 struct obstack abbrev_obstack;
1015
1016 /* Hash table of abbrevs.
1017 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1018 It could be statically allocated, but the previous code didn't so we
1019 don't either. */
1020 struct abbrev_info **abbrevs;
1021 };
1022
1023 /* Attributes have a name and a value. */
1024 struct attribute
1025 {
1026 ENUM_BITFIELD(dwarf_attribute) name : 16;
1027 ENUM_BITFIELD(dwarf_form) form : 15;
1028
1029 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1030 field should be in u.str (existing only for DW_STRING) but it is kept
1031 here for better struct attribute alignment. */
1032 unsigned int string_is_canonical : 1;
1033
1034 union
1035 {
1036 const char *str;
1037 struct dwarf_block *blk;
1038 ULONGEST unsnd;
1039 LONGEST snd;
1040 CORE_ADDR addr;
1041 struct signatured_type *signatured_type;
1042 }
1043 u;
1044 };
1045
1046 /* This data structure holds a complete die structure. */
1047 struct die_info
1048 {
1049 /* DWARF-2 tag for this DIE. */
1050 ENUM_BITFIELD(dwarf_tag) tag : 16;
1051
1052 /* Number of attributes */
1053 unsigned char num_attrs;
1054
1055 /* True if we're presently building the full type name for the
1056 type derived from this DIE. */
1057 unsigned char building_fullname : 1;
1058
1059 /* Abbrev number */
1060 unsigned int abbrev;
1061
1062 /* Offset in .debug_info or .debug_types section. */
1063 sect_offset offset;
1064
1065 /* The dies in a compilation unit form an n-ary tree. PARENT
1066 points to this die's parent; CHILD points to the first child of
1067 this node; and all the children of a given node are chained
1068 together via their SIBLING fields. */
1069 struct die_info *child; /* Its first child, if any. */
1070 struct die_info *sibling; /* Its next sibling, if any. */
1071 struct die_info *parent; /* Its parent, if any. */
1072
1073 /* An array of attributes, with NUM_ATTRS elements. There may be
1074 zero, but it's not common and zero-sized arrays are not
1075 sufficiently portable C. */
1076 struct attribute attrs[1];
1077 };
1078
1079 /* Get at parts of an attribute structure. */
1080
1081 #define DW_STRING(attr) ((attr)->u.str)
1082 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1083 #define DW_UNSND(attr) ((attr)->u.unsnd)
1084 #define DW_BLOCK(attr) ((attr)->u.blk)
1085 #define DW_SND(attr) ((attr)->u.snd)
1086 #define DW_ADDR(attr) ((attr)->u.addr)
1087 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1088
1089 /* Blocks are a bunch of untyped bytes. */
1090 struct dwarf_block
1091 {
1092 size_t size;
1093
1094 /* Valid only if SIZE is not zero. */
1095 gdb_byte *data;
1096 };
1097
1098 #ifndef ATTR_ALLOC_CHUNK
1099 #define ATTR_ALLOC_CHUNK 4
1100 #endif
1101
1102 /* Allocate fields for structs, unions and enums in this size. */
1103 #ifndef DW_FIELD_ALLOC_CHUNK
1104 #define DW_FIELD_ALLOC_CHUNK 4
1105 #endif
1106
1107 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1108 but this would require a corresponding change in unpack_field_as_long
1109 and friends. */
1110 static int bits_per_byte = 8;
1111
1112 /* The routines that read and process dies for a C struct or C++ class
1113 pass lists of data member fields and lists of member function fields
1114 in an instance of a field_info structure, as defined below. */
1115 struct field_info
1116 {
1117 /* List of data member and baseclasses fields. */
1118 struct nextfield
1119 {
1120 struct nextfield *next;
1121 int accessibility;
1122 int virtuality;
1123 struct field field;
1124 }
1125 *fields, *baseclasses;
1126
1127 /* Number of fields (including baseclasses). */
1128 int nfields;
1129
1130 /* Number of baseclasses. */
1131 int nbaseclasses;
1132
1133 /* Set if the accesibility of one of the fields is not public. */
1134 int non_public_fields;
1135
1136 /* Member function fields array, entries are allocated in the order they
1137 are encountered in the object file. */
1138 struct nextfnfield
1139 {
1140 struct nextfnfield *next;
1141 struct fn_field fnfield;
1142 }
1143 *fnfields;
1144
1145 /* Member function fieldlist array, contains name of possibly overloaded
1146 member function, number of overloaded member functions and a pointer
1147 to the head of the member function field chain. */
1148 struct fnfieldlist
1149 {
1150 const char *name;
1151 int length;
1152 struct nextfnfield *head;
1153 }
1154 *fnfieldlists;
1155
1156 /* Number of entries in the fnfieldlists array. */
1157 int nfnfields;
1158
1159 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1160 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1161 struct typedef_field_list
1162 {
1163 struct typedef_field field;
1164 struct typedef_field_list *next;
1165 }
1166 *typedef_field_list;
1167 unsigned typedef_field_list_count;
1168 };
1169
1170 /* One item on the queue of compilation units to read in full symbols
1171 for. */
1172 struct dwarf2_queue_item
1173 {
1174 struct dwarf2_per_cu_data *per_cu;
1175 enum language pretend_language;
1176 struct dwarf2_queue_item *next;
1177 };
1178
1179 /* The current queue. */
1180 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1181
1182 /* Loaded secondary compilation units are kept in memory until they
1183 have not been referenced for the processing of this many
1184 compilation units. Set this to zero to disable caching. Cache
1185 sizes of up to at least twenty will improve startup time for
1186 typical inter-CU-reference binaries, at an obvious memory cost. */
1187 static int dwarf2_max_cache_age = 5;
1188 static void
1189 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1190 struct cmd_list_element *c, const char *value)
1191 {
1192 fprintf_filtered (file, _("The upper bound on the age of cached "
1193 "dwarf2 compilation units is %s.\n"),
1194 value);
1195 }
1196
1197
1198 /* Various complaints about symbol reading that don't abort the process. */
1199
1200 static void
1201 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1202 {
1203 complaint (&symfile_complaints,
1204 _("statement list doesn't fit in .debug_line section"));
1205 }
1206
1207 static void
1208 dwarf2_debug_line_missing_file_complaint (void)
1209 {
1210 complaint (&symfile_complaints,
1211 _(".debug_line section has line data without a file"));
1212 }
1213
1214 static void
1215 dwarf2_debug_line_missing_end_sequence_complaint (void)
1216 {
1217 complaint (&symfile_complaints,
1218 _(".debug_line section has line "
1219 "program sequence without an end"));
1220 }
1221
1222 static void
1223 dwarf2_complex_location_expr_complaint (void)
1224 {
1225 complaint (&symfile_complaints, _("location expression too complex"));
1226 }
1227
1228 static void
1229 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1230 int arg3)
1231 {
1232 complaint (&symfile_complaints,
1233 _("const value length mismatch for '%s', got %d, expected %d"),
1234 arg1, arg2, arg3);
1235 }
1236
1237 static void
1238 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1239 {
1240 complaint (&symfile_complaints,
1241 _("debug info runs off end of %s section"
1242 " [in module %s]"),
1243 section->asection->name,
1244 bfd_get_filename (section->asection->owner));
1245 }
1246
1247 static void
1248 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1249 {
1250 complaint (&symfile_complaints,
1251 _("macro debug info contains a "
1252 "malformed macro definition:\n`%s'"),
1253 arg1);
1254 }
1255
1256 static void
1257 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1258 {
1259 complaint (&symfile_complaints,
1260 _("invalid attribute class or form for '%s' in '%s'"),
1261 arg1, arg2);
1262 }
1263
1264 /* local function prototypes */
1265
1266 static void dwarf2_locate_sections (bfd *, asection *, void *);
1267
1268 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1269 struct objfile *);
1270
1271 static void dwarf2_find_base_address (struct die_info *die,
1272 struct dwarf2_cu *cu);
1273
1274 static void dwarf2_build_psymtabs_hard (struct objfile *);
1275
1276 static void scan_partial_symbols (struct partial_die_info *,
1277 CORE_ADDR *, CORE_ADDR *,
1278 int, struct dwarf2_cu *);
1279
1280 static void add_partial_symbol (struct partial_die_info *,
1281 struct dwarf2_cu *);
1282
1283 static void add_partial_namespace (struct partial_die_info *pdi,
1284 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1285 int need_pc, struct dwarf2_cu *cu);
1286
1287 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1288 CORE_ADDR *highpc, int need_pc,
1289 struct dwarf2_cu *cu);
1290
1291 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1292 struct dwarf2_cu *cu);
1293
1294 static void add_partial_subprogram (struct partial_die_info *pdi,
1295 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1296 int need_pc, struct dwarf2_cu *cu);
1297
1298 static void dwarf2_read_symtab (struct partial_symtab *,
1299 struct objfile *);
1300
1301 static void psymtab_to_symtab_1 (struct partial_symtab *);
1302
1303 static struct abbrev_info *abbrev_table_lookup_abbrev
1304 (const struct abbrev_table *, unsigned int);
1305
1306 static struct abbrev_table *abbrev_table_read_table
1307 (struct dwarf2_section_info *, sect_offset);
1308
1309 static void abbrev_table_free (struct abbrev_table *);
1310
1311 static void abbrev_table_free_cleanup (void *);
1312
1313 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1314 struct dwarf2_section_info *);
1315
1316 static void dwarf2_free_abbrev_table (void *);
1317
1318 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1319
1320 static struct partial_die_info *load_partial_dies
1321 (const struct die_reader_specs *, gdb_byte *, int);
1322
1323 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1324 struct partial_die_info *,
1325 struct abbrev_info *,
1326 unsigned int,
1327 gdb_byte *);
1328
1329 static struct partial_die_info *find_partial_die (sect_offset, int,
1330 struct dwarf2_cu *);
1331
1332 static void fixup_partial_die (struct partial_die_info *,
1333 struct dwarf2_cu *);
1334
1335 static gdb_byte *read_attribute (const struct die_reader_specs *,
1336 struct attribute *, struct attr_abbrev *,
1337 gdb_byte *);
1338
1339 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1340
1341 static int read_1_signed_byte (bfd *, const gdb_byte *);
1342
1343 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1344
1345 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1346
1347 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1348
1349 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1350 unsigned int *);
1351
1352 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1353
1354 static LONGEST read_checked_initial_length_and_offset
1355 (bfd *, gdb_byte *, const struct comp_unit_head *,
1356 unsigned int *, unsigned int *);
1357
1358 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1359 unsigned int *);
1360
1361 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1362
1363 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1364 sect_offset);
1365
1366 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1367
1368 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1369
1370 static char *read_indirect_string (bfd *, gdb_byte *,
1371 const struct comp_unit_head *,
1372 unsigned int *);
1373
1374 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1375
1376 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1377
1378 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1379
1380 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1381 unsigned int *);
1382
1383 static char *read_str_index (const struct die_reader_specs *reader,
1384 struct dwarf2_cu *cu, ULONGEST str_index);
1385
1386 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1387
1388 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1389 struct dwarf2_cu *);
1390
1391 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1392 unsigned int);
1393
1394 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1395 struct dwarf2_cu *cu);
1396
1397 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1398
1399 static struct die_info *die_specification (struct die_info *die,
1400 struct dwarf2_cu **);
1401
1402 static void free_line_header (struct line_header *lh);
1403
1404 static void add_file_name (struct line_header *, char *, unsigned int,
1405 unsigned int, unsigned int);
1406
1407 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1408 struct dwarf2_cu *cu);
1409
1410 static void dwarf_decode_lines (struct line_header *, const char *,
1411 struct dwarf2_cu *, struct partial_symtab *,
1412 int);
1413
1414 static void dwarf2_start_subfile (char *, const char *, const char *);
1415
1416 static void dwarf2_start_symtab (struct dwarf2_cu *,
1417 const char *, const char *, CORE_ADDR);
1418
1419 static struct symbol *new_symbol (struct die_info *, struct type *,
1420 struct dwarf2_cu *);
1421
1422 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1423 struct dwarf2_cu *, struct symbol *);
1424
1425 static void dwarf2_const_value (struct attribute *, struct symbol *,
1426 struct dwarf2_cu *);
1427
1428 static void dwarf2_const_value_attr (struct attribute *attr,
1429 struct type *type,
1430 const char *name,
1431 struct obstack *obstack,
1432 struct dwarf2_cu *cu, LONGEST *value,
1433 gdb_byte **bytes,
1434 struct dwarf2_locexpr_baton **baton);
1435
1436 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1437
1438 static int need_gnat_info (struct dwarf2_cu *);
1439
1440 static struct type *die_descriptive_type (struct die_info *,
1441 struct dwarf2_cu *);
1442
1443 static void set_descriptive_type (struct type *, struct die_info *,
1444 struct dwarf2_cu *);
1445
1446 static struct type *die_containing_type (struct die_info *,
1447 struct dwarf2_cu *);
1448
1449 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1450 struct dwarf2_cu *);
1451
1452 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1453
1454 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1455
1456 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1457
1458 static char *typename_concat (struct obstack *obs, const char *prefix,
1459 const char *suffix, int physname,
1460 struct dwarf2_cu *cu);
1461
1462 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1463
1464 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1465
1466 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1467
1468 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1469
1470 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1471
1472 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1473 struct dwarf2_cu *, struct partial_symtab *);
1474
1475 static int dwarf2_get_pc_bounds (struct die_info *,
1476 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1477 struct partial_symtab *);
1478
1479 static void get_scope_pc_bounds (struct die_info *,
1480 CORE_ADDR *, CORE_ADDR *,
1481 struct dwarf2_cu *);
1482
1483 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1484 CORE_ADDR, struct dwarf2_cu *);
1485
1486 static void dwarf2_add_field (struct field_info *, struct die_info *,
1487 struct dwarf2_cu *);
1488
1489 static void dwarf2_attach_fields_to_type (struct field_info *,
1490 struct type *, struct dwarf2_cu *);
1491
1492 static void dwarf2_add_member_fn (struct field_info *,
1493 struct die_info *, struct type *,
1494 struct dwarf2_cu *);
1495
1496 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1497 struct type *,
1498 struct dwarf2_cu *);
1499
1500 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1501
1502 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1503
1504 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1505
1506 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1507
1508 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1509
1510 static struct type *read_module_type (struct die_info *die,
1511 struct dwarf2_cu *cu);
1512
1513 static const char *namespace_name (struct die_info *die,
1514 int *is_anonymous, struct dwarf2_cu *);
1515
1516 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1517
1518 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1519
1520 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1521 struct dwarf2_cu *);
1522
1523 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1524 gdb_byte *info_ptr,
1525 gdb_byte **new_info_ptr,
1526 struct die_info *parent);
1527
1528 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1529 gdb_byte *info_ptr,
1530 gdb_byte **new_info_ptr,
1531 struct die_info *parent);
1532
1533 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1534 struct die_info **, gdb_byte *, int *, int);
1535
1536 static gdb_byte *read_full_die (const struct die_reader_specs *,
1537 struct die_info **, gdb_byte *, int *);
1538
1539 static void process_die (struct die_info *, struct dwarf2_cu *);
1540
1541 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1542 struct obstack *);
1543
1544 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1545
1546 static const char *dwarf2_full_name (const char *name,
1547 struct die_info *die,
1548 struct dwarf2_cu *cu);
1549
1550 static struct die_info *dwarf2_extension (struct die_info *die,
1551 struct dwarf2_cu **);
1552
1553 static const char *dwarf_tag_name (unsigned int);
1554
1555 static const char *dwarf_attr_name (unsigned int);
1556
1557 static const char *dwarf_form_name (unsigned int);
1558
1559 static char *dwarf_bool_name (unsigned int);
1560
1561 static const char *dwarf_type_encoding_name (unsigned int);
1562
1563 static struct die_info *sibling_die (struct die_info *);
1564
1565 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1566
1567 static void dump_die_for_error (struct die_info *);
1568
1569 static void dump_die_1 (struct ui_file *, int level, int max_level,
1570 struct die_info *);
1571
1572 /*static*/ void dump_die (struct die_info *, int max_level);
1573
1574 static void store_in_ref_table (struct die_info *,
1575 struct dwarf2_cu *);
1576
1577 static int is_ref_attr (struct attribute *);
1578
1579 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1580
1581 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1582
1583 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1584 struct attribute *,
1585 struct dwarf2_cu **);
1586
1587 static struct die_info *follow_die_ref (struct die_info *,
1588 struct attribute *,
1589 struct dwarf2_cu **);
1590
1591 static struct die_info *follow_die_sig (struct die_info *,
1592 struct attribute *,
1593 struct dwarf2_cu **);
1594
1595 static struct signatured_type *lookup_signatured_type_at_offset
1596 (struct objfile *objfile,
1597 struct dwarf2_section_info *section, sect_offset offset);
1598
1599 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1600
1601 static void read_signatured_type (struct signatured_type *);
1602
1603 static struct type_unit_group *get_type_unit_group
1604 (struct dwarf2_cu *, struct attribute *);
1605
1606 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1607
1608 /* memory allocation interface */
1609
1610 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1611
1612 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1613
1614 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1615 const char *, int);
1616
1617 static int attr_form_is_block (struct attribute *);
1618
1619 static int attr_form_is_section_offset (struct attribute *);
1620
1621 static int attr_form_is_constant (struct attribute *);
1622
1623 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1624 struct dwarf2_loclist_baton *baton,
1625 struct attribute *attr);
1626
1627 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1628 struct symbol *sym,
1629 struct dwarf2_cu *cu);
1630
1631 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1632 gdb_byte *info_ptr,
1633 struct abbrev_info *abbrev);
1634
1635 static void free_stack_comp_unit (void *);
1636
1637 static hashval_t partial_die_hash (const void *item);
1638
1639 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1640
1641 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1642 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1643
1644 static void init_one_comp_unit (struct dwarf2_cu *cu,
1645 struct dwarf2_per_cu_data *per_cu);
1646
1647 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1648 struct die_info *comp_unit_die,
1649 enum language pretend_language);
1650
1651 static void free_heap_comp_unit (void *);
1652
1653 static void free_cached_comp_units (void *);
1654
1655 static void age_cached_comp_units (void);
1656
1657 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1658
1659 static struct type *set_die_type (struct die_info *, struct type *,
1660 struct dwarf2_cu *);
1661
1662 static void create_all_comp_units (struct objfile *);
1663
1664 static int create_all_type_units (struct objfile *);
1665
1666 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1667 enum language);
1668
1669 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1670 enum language);
1671
1672 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1673 enum language);
1674
1675 static void dwarf2_add_dependence (struct dwarf2_cu *,
1676 struct dwarf2_per_cu_data *);
1677
1678 static void dwarf2_mark (struct dwarf2_cu *);
1679
1680 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1681
1682 static struct type *get_die_type_at_offset (sect_offset,
1683 struct dwarf2_per_cu_data *per_cu);
1684
1685 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1686
1687 static void dwarf2_release_queue (void *dummy);
1688
1689 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1690 enum language pretend_language);
1691
1692 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1693 struct dwarf2_per_cu_data *per_cu,
1694 enum language pretend_language);
1695
1696 static void process_queue (void);
1697
1698 static void find_file_and_directory (struct die_info *die,
1699 struct dwarf2_cu *cu,
1700 const char **name, const char **comp_dir);
1701
1702 static char *file_full_name (int file, struct line_header *lh,
1703 const char *comp_dir);
1704
1705 static gdb_byte *read_and_check_comp_unit_head
1706 (struct comp_unit_head *header,
1707 struct dwarf2_section_info *section,
1708 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1709 int is_debug_types_section);
1710
1711 static void init_cutu_and_read_dies
1712 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1713 int use_existing_cu, int keep,
1714 die_reader_func_ftype *die_reader_func, void *data);
1715
1716 static void init_cutu_and_read_dies_simple
1717 (struct dwarf2_per_cu_data *this_cu,
1718 die_reader_func_ftype *die_reader_func, void *data);
1719
1720 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1721
1722 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1723
1724 static struct dwo_unit *lookup_dwo_comp_unit
1725 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1726
1727 static struct dwo_unit *lookup_dwo_type_unit
1728 (struct signatured_type *, const char *, const char *);
1729
1730 static void free_dwo_file_cleanup (void *);
1731
1732 static void process_cu_includes (void);
1733
1734 static void check_producer (struct dwarf2_cu *cu);
1735
1736 #if WORDS_BIGENDIAN
1737
1738 /* Convert VALUE between big- and little-endian. */
1739 static offset_type
1740 byte_swap (offset_type value)
1741 {
1742 offset_type result;
1743
1744 result = (value & 0xff) << 24;
1745 result |= (value & 0xff00) << 8;
1746 result |= (value & 0xff0000) >> 8;
1747 result |= (value & 0xff000000) >> 24;
1748 return result;
1749 }
1750
1751 #define MAYBE_SWAP(V) byte_swap (V)
1752
1753 #else
1754 #define MAYBE_SWAP(V) (V)
1755 #endif /* WORDS_BIGENDIAN */
1756
1757 /* The suffix for an index file. */
1758 #define INDEX_SUFFIX ".gdb-index"
1759
1760 static const char *dwarf2_physname (const char *name, struct die_info *die,
1761 struct dwarf2_cu *cu);
1762
1763 /* Try to locate the sections we need for DWARF 2 debugging
1764 information and return true if we have enough to do something.
1765 NAMES points to the dwarf2 section names, or is NULL if the standard
1766 ELF names are used. */
1767
1768 int
1769 dwarf2_has_info (struct objfile *objfile,
1770 const struct dwarf2_debug_sections *names)
1771 {
1772 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1773 if (!dwarf2_per_objfile)
1774 {
1775 /* Initialize per-objfile state. */
1776 struct dwarf2_per_objfile *data
1777 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1778
1779 memset (data, 0, sizeof (*data));
1780 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1781 dwarf2_per_objfile = data;
1782
1783 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1784 (void *) names);
1785 dwarf2_per_objfile->objfile = objfile;
1786 }
1787 return (dwarf2_per_objfile->info.asection != NULL
1788 && dwarf2_per_objfile->abbrev.asection != NULL);
1789 }
1790
1791 /* When loading sections, we look either for uncompressed section or for
1792 compressed section names. */
1793
1794 static int
1795 section_is_p (const char *section_name,
1796 const struct dwarf2_section_names *names)
1797 {
1798 if (names->normal != NULL
1799 && strcmp (section_name, names->normal) == 0)
1800 return 1;
1801 if (names->compressed != NULL
1802 && strcmp (section_name, names->compressed) == 0)
1803 return 1;
1804 return 0;
1805 }
1806
1807 /* This function is mapped across the sections and remembers the
1808 offset and size of each of the debugging sections we are interested
1809 in. */
1810
1811 static void
1812 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1813 {
1814 const struct dwarf2_debug_sections *names;
1815 flagword aflag = bfd_get_section_flags (abfd, sectp);
1816
1817 if (vnames == NULL)
1818 names = &dwarf2_elf_names;
1819 else
1820 names = (const struct dwarf2_debug_sections *) vnames;
1821
1822 if ((aflag & SEC_HAS_CONTENTS) == 0)
1823 {
1824 }
1825 else if (section_is_p (sectp->name, &names->info))
1826 {
1827 dwarf2_per_objfile->info.asection = sectp;
1828 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1829 }
1830 else if (section_is_p (sectp->name, &names->abbrev))
1831 {
1832 dwarf2_per_objfile->abbrev.asection = sectp;
1833 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1834 }
1835 else if (section_is_p (sectp->name, &names->line))
1836 {
1837 dwarf2_per_objfile->line.asection = sectp;
1838 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1839 }
1840 else if (section_is_p (sectp->name, &names->loc))
1841 {
1842 dwarf2_per_objfile->loc.asection = sectp;
1843 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1844 }
1845 else if (section_is_p (sectp->name, &names->macinfo))
1846 {
1847 dwarf2_per_objfile->macinfo.asection = sectp;
1848 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1849 }
1850 else if (section_is_p (sectp->name, &names->macro))
1851 {
1852 dwarf2_per_objfile->macro.asection = sectp;
1853 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1854 }
1855 else if (section_is_p (sectp->name, &names->str))
1856 {
1857 dwarf2_per_objfile->str.asection = sectp;
1858 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1859 }
1860 else if (section_is_p (sectp->name, &names->addr))
1861 {
1862 dwarf2_per_objfile->addr.asection = sectp;
1863 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1864 }
1865 else if (section_is_p (sectp->name, &names->frame))
1866 {
1867 dwarf2_per_objfile->frame.asection = sectp;
1868 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1869 }
1870 else if (section_is_p (sectp->name, &names->eh_frame))
1871 {
1872 dwarf2_per_objfile->eh_frame.asection = sectp;
1873 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1874 }
1875 else if (section_is_p (sectp->name, &names->ranges))
1876 {
1877 dwarf2_per_objfile->ranges.asection = sectp;
1878 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1879 }
1880 else if (section_is_p (sectp->name, &names->types))
1881 {
1882 struct dwarf2_section_info type_section;
1883
1884 memset (&type_section, 0, sizeof (type_section));
1885 type_section.asection = sectp;
1886 type_section.size = bfd_get_section_size (sectp);
1887
1888 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1889 &type_section);
1890 }
1891 else if (section_is_p (sectp->name, &names->gdb_index))
1892 {
1893 dwarf2_per_objfile->gdb_index.asection = sectp;
1894 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1895 }
1896
1897 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1898 && bfd_section_vma (abfd, sectp) == 0)
1899 dwarf2_per_objfile->has_section_at_zero = 1;
1900 }
1901
1902 /* A helper function that decides whether a section is empty,
1903 or not present. */
1904
1905 static int
1906 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1907 {
1908 return info->asection == NULL || info->size == 0;
1909 }
1910
1911 /* Read the contents of the section INFO.
1912 OBJFILE is the main object file, but not necessarily the file where
1913 the section comes from. E.g., for DWO files INFO->asection->owner
1914 is the bfd of the DWO file.
1915 If the section is compressed, uncompress it before returning. */
1916
1917 static void
1918 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1919 {
1920 asection *sectp = info->asection;
1921 bfd *abfd;
1922 gdb_byte *buf, *retbuf;
1923 unsigned char header[4];
1924
1925 if (info->readin)
1926 return;
1927 info->buffer = NULL;
1928 info->readin = 1;
1929
1930 if (dwarf2_section_empty_p (info))
1931 return;
1932
1933 abfd = sectp->owner;
1934
1935 /* If the section has relocations, we must read it ourselves.
1936 Otherwise we attach it to the BFD. */
1937 if ((sectp->flags & SEC_RELOC) == 0)
1938 {
1939 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1940
1941 /* We have to cast away const here for historical reasons.
1942 Fixing dwarf2read to be const-correct would be quite nice. */
1943 info->buffer = (gdb_byte *) bytes;
1944 return;
1945 }
1946
1947 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1948 info->buffer = buf;
1949
1950 /* When debugging .o files, we may need to apply relocations; see
1951 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1952 We never compress sections in .o files, so we only need to
1953 try this when the section is not compressed. */
1954 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1955 if (retbuf != NULL)
1956 {
1957 info->buffer = retbuf;
1958 return;
1959 }
1960
1961 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1962 || bfd_bread (buf, info->size, abfd) != info->size)
1963 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1964 bfd_get_filename (abfd));
1965 }
1966
1967 /* A helper function that returns the size of a section in a safe way.
1968 If you are positive that the section has been read before using the
1969 size, then it is safe to refer to the dwarf2_section_info object's
1970 "size" field directly. In other cases, you must call this
1971 function, because for compressed sections the size field is not set
1972 correctly until the section has been read. */
1973
1974 static bfd_size_type
1975 dwarf2_section_size (struct objfile *objfile,
1976 struct dwarf2_section_info *info)
1977 {
1978 if (!info->readin)
1979 dwarf2_read_section (objfile, info);
1980 return info->size;
1981 }
1982
1983 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1984 SECTION_NAME. */
1985
1986 void
1987 dwarf2_get_section_info (struct objfile *objfile,
1988 enum dwarf2_section_enum sect,
1989 asection **sectp, gdb_byte **bufp,
1990 bfd_size_type *sizep)
1991 {
1992 struct dwarf2_per_objfile *data
1993 = objfile_data (objfile, dwarf2_objfile_data_key);
1994 struct dwarf2_section_info *info;
1995
1996 /* We may see an objfile without any DWARF, in which case we just
1997 return nothing. */
1998 if (data == NULL)
1999 {
2000 *sectp = NULL;
2001 *bufp = NULL;
2002 *sizep = 0;
2003 return;
2004 }
2005 switch (sect)
2006 {
2007 case DWARF2_DEBUG_FRAME:
2008 info = &data->frame;
2009 break;
2010 case DWARF2_EH_FRAME:
2011 info = &data->eh_frame;
2012 break;
2013 default:
2014 gdb_assert_not_reached ("unexpected section");
2015 }
2016
2017 dwarf2_read_section (objfile, info);
2018
2019 *sectp = info->asection;
2020 *bufp = info->buffer;
2021 *sizep = info->size;
2022 }
2023
2024 /* A helper function to find the sections for a .dwz file. */
2025
2026 static void
2027 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2028 {
2029 struct dwz_file *dwz_file = arg;
2030
2031 /* Note that we only support the standard ELF names, because .dwz
2032 is ELF-only (at the time of writing). */
2033 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2034 {
2035 dwz_file->abbrev.asection = sectp;
2036 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2037 }
2038 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2039 {
2040 dwz_file->info.asection = sectp;
2041 dwz_file->info.size = bfd_get_section_size (sectp);
2042 }
2043 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2044 {
2045 dwz_file->str.asection = sectp;
2046 dwz_file->str.size = bfd_get_section_size (sectp);
2047 }
2048 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2049 {
2050 dwz_file->line.asection = sectp;
2051 dwz_file->line.size = bfd_get_section_size (sectp);
2052 }
2053 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2054 {
2055 dwz_file->macro.asection = sectp;
2056 dwz_file->macro.size = bfd_get_section_size (sectp);
2057 }
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2059 {
2060 dwz_file->gdb_index.asection = sectp;
2061 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2062 }
2063 }
2064
2065 /* Open the separate '.dwz' debug file, if needed. Error if the file
2066 cannot be found. */
2067
2068 static struct dwz_file *
2069 dwarf2_get_dwz_file (void)
2070 {
2071 bfd *abfd, *dwz_bfd;
2072 asection *section;
2073 gdb_byte *data;
2074 struct cleanup *cleanup;
2075 const char *filename;
2076 struct dwz_file *result;
2077
2078 if (dwarf2_per_objfile->dwz_file != NULL)
2079 return dwarf2_per_objfile->dwz_file;
2080
2081 abfd = dwarf2_per_objfile->objfile->obfd;
2082 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2083 if (section == NULL)
2084 error (_("could not find '.gnu_debugaltlink' section"));
2085 if (!bfd_malloc_and_get_section (abfd, section, &data))
2086 error (_("could not read '.gnu_debugaltlink' section: %s"),
2087 bfd_errmsg (bfd_get_error ()));
2088 cleanup = make_cleanup (xfree, data);
2089
2090 filename = data;
2091 if (!IS_ABSOLUTE_PATH (filename))
2092 {
2093 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2094 char *rel;
2095
2096 make_cleanup (xfree, abs);
2097 abs = ldirname (abs);
2098 make_cleanup (xfree, abs);
2099
2100 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2101 make_cleanup (xfree, rel);
2102 filename = rel;
2103 }
2104
2105 /* The format is just a NUL-terminated file name, followed by the
2106 build-id. For now, though, we ignore the build-id. */
2107 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2108 if (dwz_bfd == NULL)
2109 error (_("could not read '%s': %s"), filename,
2110 bfd_errmsg (bfd_get_error ()));
2111
2112 if (!bfd_check_format (dwz_bfd, bfd_object))
2113 {
2114 gdb_bfd_unref (dwz_bfd);
2115 error (_("file '%s' was not usable: %s"), filename,
2116 bfd_errmsg (bfd_get_error ()));
2117 }
2118
2119 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2120 struct dwz_file);
2121 result->dwz_bfd = dwz_bfd;
2122
2123 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2124
2125 do_cleanups (cleanup);
2126
2127 dwarf2_per_objfile->dwz_file = result;
2128 return result;
2129 }
2130 \f
2131 /* DWARF quick_symbols_functions support. */
2132
2133 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2134 unique line tables, so we maintain a separate table of all .debug_line
2135 derived entries to support the sharing.
2136 All the quick functions need is the list of file names. We discard the
2137 line_header when we're done and don't need to record it here. */
2138 struct quick_file_names
2139 {
2140 /* The data used to construct the hash key. */
2141 struct stmt_list_hash hash;
2142
2143 /* The number of entries in file_names, real_names. */
2144 unsigned int num_file_names;
2145
2146 /* The file names from the line table, after being run through
2147 file_full_name. */
2148 const char **file_names;
2149
2150 /* The file names from the line table after being run through
2151 gdb_realpath. These are computed lazily. */
2152 const char **real_names;
2153 };
2154
2155 /* When using the index (and thus not using psymtabs), each CU has an
2156 object of this type. This is used to hold information needed by
2157 the various "quick" methods. */
2158 struct dwarf2_per_cu_quick_data
2159 {
2160 /* The file table. This can be NULL if there was no file table
2161 or it's currently not read in.
2162 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2163 struct quick_file_names *file_names;
2164
2165 /* The corresponding symbol table. This is NULL if symbols for this
2166 CU have not yet been read. */
2167 struct symtab *symtab;
2168
2169 /* A temporary mark bit used when iterating over all CUs in
2170 expand_symtabs_matching. */
2171 unsigned int mark : 1;
2172
2173 /* True if we've tried to read the file table and found there isn't one.
2174 There will be no point in trying to read it again next time. */
2175 unsigned int no_file_data : 1;
2176 };
2177
2178 /* Utility hash function for a stmt_list_hash. */
2179
2180 static hashval_t
2181 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2182 {
2183 hashval_t v = 0;
2184
2185 if (stmt_list_hash->dwo_unit != NULL)
2186 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2187 v += stmt_list_hash->line_offset.sect_off;
2188 return v;
2189 }
2190
2191 /* Utility equality function for a stmt_list_hash. */
2192
2193 static int
2194 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2195 const struct stmt_list_hash *rhs)
2196 {
2197 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2198 return 0;
2199 if (lhs->dwo_unit != NULL
2200 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2201 return 0;
2202
2203 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2204 }
2205
2206 /* Hash function for a quick_file_names. */
2207
2208 static hashval_t
2209 hash_file_name_entry (const void *e)
2210 {
2211 const struct quick_file_names *file_data = e;
2212
2213 return hash_stmt_list_entry (&file_data->hash);
2214 }
2215
2216 /* Equality function for a quick_file_names. */
2217
2218 static int
2219 eq_file_name_entry (const void *a, const void *b)
2220 {
2221 const struct quick_file_names *ea = a;
2222 const struct quick_file_names *eb = b;
2223
2224 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2225 }
2226
2227 /* Delete function for a quick_file_names. */
2228
2229 static void
2230 delete_file_name_entry (void *e)
2231 {
2232 struct quick_file_names *file_data = e;
2233 int i;
2234
2235 for (i = 0; i < file_data->num_file_names; ++i)
2236 {
2237 xfree ((void*) file_data->file_names[i]);
2238 if (file_data->real_names)
2239 xfree ((void*) file_data->real_names[i]);
2240 }
2241
2242 /* The space for the struct itself lives on objfile_obstack,
2243 so we don't free it here. */
2244 }
2245
2246 /* Create a quick_file_names hash table. */
2247
2248 static htab_t
2249 create_quick_file_names_table (unsigned int nr_initial_entries)
2250 {
2251 return htab_create_alloc (nr_initial_entries,
2252 hash_file_name_entry, eq_file_name_entry,
2253 delete_file_name_entry, xcalloc, xfree);
2254 }
2255
2256 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2257 have to be created afterwards. You should call age_cached_comp_units after
2258 processing PER_CU->CU. dw2_setup must have been already called. */
2259
2260 static void
2261 load_cu (struct dwarf2_per_cu_data *per_cu)
2262 {
2263 if (per_cu->is_debug_types)
2264 load_full_type_unit (per_cu);
2265 else
2266 load_full_comp_unit (per_cu, language_minimal);
2267
2268 gdb_assert (per_cu->cu != NULL);
2269
2270 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2271 }
2272
2273 /* Read in the symbols for PER_CU. */
2274
2275 static void
2276 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2277 {
2278 struct cleanup *back_to;
2279
2280 /* Skip type_unit_groups, reading the type units they contain
2281 is handled elsewhere. */
2282 if (IS_TYPE_UNIT_GROUP (per_cu))
2283 return;
2284
2285 back_to = make_cleanup (dwarf2_release_queue, NULL);
2286
2287 if (dwarf2_per_objfile->using_index
2288 ? per_cu->v.quick->symtab == NULL
2289 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2290 {
2291 queue_comp_unit (per_cu, language_minimal);
2292 load_cu (per_cu);
2293 }
2294
2295 process_queue ();
2296
2297 /* Age the cache, releasing compilation units that have not
2298 been used recently. */
2299 age_cached_comp_units ();
2300
2301 do_cleanups (back_to);
2302 }
2303
2304 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2305 the objfile from which this CU came. Returns the resulting symbol
2306 table. */
2307
2308 static struct symtab *
2309 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2310 {
2311 gdb_assert (dwarf2_per_objfile->using_index);
2312 if (!per_cu->v.quick->symtab)
2313 {
2314 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2315 increment_reading_symtab ();
2316 dw2_do_instantiate_symtab (per_cu);
2317 process_cu_includes ();
2318 do_cleanups (back_to);
2319 }
2320 return per_cu->v.quick->symtab;
2321 }
2322
2323 /* Return the CU given its index.
2324
2325 This is intended for loops like:
2326
2327 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2328 + dwarf2_per_objfile->n_type_units); ++i)
2329 {
2330 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2331
2332 ...;
2333 }
2334 */
2335
2336 static struct dwarf2_per_cu_data *
2337 dw2_get_cu (int index)
2338 {
2339 if (index >= dwarf2_per_objfile->n_comp_units)
2340 {
2341 index -= dwarf2_per_objfile->n_comp_units;
2342 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2343 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2344 }
2345
2346 return dwarf2_per_objfile->all_comp_units[index];
2347 }
2348
2349 /* Return the primary CU given its index.
2350 The difference between this function and dw2_get_cu is in the handling
2351 of type units (TUs). Here we return the type_unit_group object.
2352
2353 This is intended for loops like:
2354
2355 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2356 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2357 {
2358 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2359
2360 ...;
2361 }
2362 */
2363
2364 static struct dwarf2_per_cu_data *
2365 dw2_get_primary_cu (int index)
2366 {
2367 if (index >= dwarf2_per_objfile->n_comp_units)
2368 {
2369 index -= dwarf2_per_objfile->n_comp_units;
2370 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2371 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2372 }
2373
2374 return dwarf2_per_objfile->all_comp_units[index];
2375 }
2376
2377 /* A helper for create_cus_from_index that handles a given list of
2378 CUs. */
2379
2380 static void
2381 create_cus_from_index_list (struct objfile *objfile,
2382 const gdb_byte *cu_list, offset_type n_elements,
2383 struct dwarf2_section_info *section,
2384 int is_dwz,
2385 int base_offset)
2386 {
2387 offset_type i;
2388
2389 for (i = 0; i < n_elements; i += 2)
2390 {
2391 struct dwarf2_per_cu_data *the_cu;
2392 ULONGEST offset, length;
2393
2394 gdb_static_assert (sizeof (ULONGEST) >= 8);
2395 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2396 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2397 cu_list += 2 * 8;
2398
2399 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2400 struct dwarf2_per_cu_data);
2401 the_cu->offset.sect_off = offset;
2402 the_cu->length = length;
2403 the_cu->objfile = objfile;
2404 the_cu->info_or_types_section = section;
2405 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2406 struct dwarf2_per_cu_quick_data);
2407 the_cu->is_dwz = is_dwz;
2408 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2409 }
2410 }
2411
2412 /* Read the CU list from the mapped index, and use it to create all
2413 the CU objects for this objfile. */
2414
2415 static void
2416 create_cus_from_index (struct objfile *objfile,
2417 const gdb_byte *cu_list, offset_type cu_list_elements,
2418 const gdb_byte *dwz_list, offset_type dwz_elements)
2419 {
2420 struct dwz_file *dwz;
2421
2422 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2423 dwarf2_per_objfile->all_comp_units
2424 = obstack_alloc (&objfile->objfile_obstack,
2425 dwarf2_per_objfile->n_comp_units
2426 * sizeof (struct dwarf2_per_cu_data *));
2427
2428 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2429 &dwarf2_per_objfile->info, 0, 0);
2430
2431 if (dwz_elements == 0)
2432 return;
2433
2434 dwz = dwarf2_get_dwz_file ();
2435 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2436 cu_list_elements / 2);
2437 }
2438
2439 /* Create the signatured type hash table from the index. */
2440
2441 static void
2442 create_signatured_type_table_from_index (struct objfile *objfile,
2443 struct dwarf2_section_info *section,
2444 const gdb_byte *bytes,
2445 offset_type elements)
2446 {
2447 offset_type i;
2448 htab_t sig_types_hash;
2449
2450 dwarf2_per_objfile->n_type_units = elements / 3;
2451 dwarf2_per_objfile->all_type_units
2452 = obstack_alloc (&objfile->objfile_obstack,
2453 dwarf2_per_objfile->n_type_units
2454 * sizeof (struct signatured_type *));
2455
2456 sig_types_hash = allocate_signatured_type_table (objfile);
2457
2458 for (i = 0; i < elements; i += 3)
2459 {
2460 struct signatured_type *sig_type;
2461 ULONGEST offset, type_offset_in_tu, signature;
2462 void **slot;
2463
2464 gdb_static_assert (sizeof (ULONGEST) >= 8);
2465 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2466 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2467 BFD_ENDIAN_LITTLE);
2468 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2469 bytes += 3 * 8;
2470
2471 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2472 struct signatured_type);
2473 sig_type->signature = signature;
2474 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2475 sig_type->per_cu.is_debug_types = 1;
2476 sig_type->per_cu.info_or_types_section = section;
2477 sig_type->per_cu.offset.sect_off = offset;
2478 sig_type->per_cu.objfile = objfile;
2479 sig_type->per_cu.v.quick
2480 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2481 struct dwarf2_per_cu_quick_data);
2482
2483 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2484 *slot = sig_type;
2485
2486 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2487 }
2488
2489 dwarf2_per_objfile->signatured_types = sig_types_hash;
2490 }
2491
2492 /* Read the address map data from the mapped index, and use it to
2493 populate the objfile's psymtabs_addrmap. */
2494
2495 static void
2496 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2497 {
2498 const gdb_byte *iter, *end;
2499 struct obstack temp_obstack;
2500 struct addrmap *mutable_map;
2501 struct cleanup *cleanup;
2502 CORE_ADDR baseaddr;
2503
2504 obstack_init (&temp_obstack);
2505 cleanup = make_cleanup_obstack_free (&temp_obstack);
2506 mutable_map = addrmap_create_mutable (&temp_obstack);
2507
2508 iter = index->address_table;
2509 end = iter + index->address_table_size;
2510
2511 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2512
2513 while (iter < end)
2514 {
2515 ULONGEST hi, lo, cu_index;
2516 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2517 iter += 8;
2518 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2519 iter += 8;
2520 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2521 iter += 4;
2522
2523 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2524 dw2_get_cu (cu_index));
2525 }
2526
2527 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2528 &objfile->objfile_obstack);
2529 do_cleanups (cleanup);
2530 }
2531
2532 /* The hash function for strings in the mapped index. This is the same as
2533 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2534 implementation. This is necessary because the hash function is tied to the
2535 format of the mapped index file. The hash values do not have to match with
2536 SYMBOL_HASH_NEXT.
2537
2538 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2539
2540 static hashval_t
2541 mapped_index_string_hash (int index_version, const void *p)
2542 {
2543 const unsigned char *str = (const unsigned char *) p;
2544 hashval_t r = 0;
2545 unsigned char c;
2546
2547 while ((c = *str++) != 0)
2548 {
2549 if (index_version >= 5)
2550 c = tolower (c);
2551 r = r * 67 + c - 113;
2552 }
2553
2554 return r;
2555 }
2556
2557 /* Find a slot in the mapped index INDEX for the object named NAME.
2558 If NAME is found, set *VEC_OUT to point to the CU vector in the
2559 constant pool and return 1. If NAME cannot be found, return 0. */
2560
2561 static int
2562 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2563 offset_type **vec_out)
2564 {
2565 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2566 offset_type hash;
2567 offset_type slot, step;
2568 int (*cmp) (const char *, const char *);
2569
2570 if (current_language->la_language == language_cplus
2571 || current_language->la_language == language_java
2572 || current_language->la_language == language_fortran)
2573 {
2574 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2575 not contain any. */
2576 const char *paren = strchr (name, '(');
2577
2578 if (paren)
2579 {
2580 char *dup;
2581
2582 dup = xmalloc (paren - name + 1);
2583 memcpy (dup, name, paren - name);
2584 dup[paren - name] = 0;
2585
2586 make_cleanup (xfree, dup);
2587 name = dup;
2588 }
2589 }
2590
2591 /* Index version 4 did not support case insensitive searches. But the
2592 indices for case insensitive languages are built in lowercase, therefore
2593 simulate our NAME being searched is also lowercased. */
2594 hash = mapped_index_string_hash ((index->version == 4
2595 && case_sensitivity == case_sensitive_off
2596 ? 5 : index->version),
2597 name);
2598
2599 slot = hash & (index->symbol_table_slots - 1);
2600 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2601 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2602
2603 for (;;)
2604 {
2605 /* Convert a slot number to an offset into the table. */
2606 offset_type i = 2 * slot;
2607 const char *str;
2608 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2609 {
2610 do_cleanups (back_to);
2611 return 0;
2612 }
2613
2614 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2615 if (!cmp (name, str))
2616 {
2617 *vec_out = (offset_type *) (index->constant_pool
2618 + MAYBE_SWAP (index->symbol_table[i + 1]));
2619 do_cleanups (back_to);
2620 return 1;
2621 }
2622
2623 slot = (slot + step) & (index->symbol_table_slots - 1);
2624 }
2625 }
2626
2627 /* A helper function that reads the .gdb_index from SECTION and fills
2628 in MAP. FILENAME is the name of the file containing the section;
2629 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2630 ok to use deprecated sections.
2631
2632 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2633 out parameters that are filled in with information about the CU and
2634 TU lists in the section.
2635
2636 Returns 1 if all went well, 0 otherwise. */
2637
2638 static int
2639 read_index_from_section (struct objfile *objfile,
2640 const char *filename,
2641 int deprecated_ok,
2642 struct dwarf2_section_info *section,
2643 struct mapped_index *map,
2644 const gdb_byte **cu_list,
2645 offset_type *cu_list_elements,
2646 const gdb_byte **types_list,
2647 offset_type *types_list_elements)
2648 {
2649 char *addr;
2650 offset_type version;
2651 offset_type *metadata;
2652 int i;
2653
2654 if (dwarf2_section_empty_p (section))
2655 return 0;
2656
2657 /* Older elfutils strip versions could keep the section in the main
2658 executable while splitting it for the separate debug info file. */
2659 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2660 return 0;
2661
2662 dwarf2_read_section (objfile, section);
2663
2664 addr = section->buffer;
2665 /* Version check. */
2666 version = MAYBE_SWAP (*(offset_type *) addr);
2667 /* Versions earlier than 3 emitted every copy of a psymbol. This
2668 causes the index to behave very poorly for certain requests. Version 3
2669 contained incomplete addrmap. So, it seems better to just ignore such
2670 indices. */
2671 if (version < 4)
2672 {
2673 static int warning_printed = 0;
2674 if (!warning_printed)
2675 {
2676 warning (_("Skipping obsolete .gdb_index section in %s."),
2677 filename);
2678 warning_printed = 1;
2679 }
2680 return 0;
2681 }
2682 /* Index version 4 uses a different hash function than index version
2683 5 and later.
2684
2685 Versions earlier than 6 did not emit psymbols for inlined
2686 functions. Using these files will cause GDB not to be able to
2687 set breakpoints on inlined functions by name, so we ignore these
2688 indices unless the user has done
2689 "set use-deprecated-index-sections on". */
2690 if (version < 6 && !deprecated_ok)
2691 {
2692 static int warning_printed = 0;
2693 if (!warning_printed)
2694 {
2695 warning (_("\
2696 Skipping deprecated .gdb_index section in %s.\n\
2697 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2698 to use the section anyway."),
2699 filename);
2700 warning_printed = 1;
2701 }
2702 return 0;
2703 }
2704 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2705 of the TU (for symbols coming from TUs). It's just a performance bug, and
2706 we can't distinguish gdb-generated indices from gold-generated ones, so
2707 nothing to do here. */
2708
2709 /* Indexes with higher version than the one supported by GDB may be no
2710 longer backward compatible. */
2711 if (version > 8)
2712 return 0;
2713
2714 map->version = version;
2715 map->total_size = section->size;
2716
2717 metadata = (offset_type *) (addr + sizeof (offset_type));
2718
2719 i = 0;
2720 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2721 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2722 / 8);
2723 ++i;
2724
2725 *types_list = addr + MAYBE_SWAP (metadata[i]);
2726 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2727 - MAYBE_SWAP (metadata[i]))
2728 / 8);
2729 ++i;
2730
2731 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2732 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2733 - MAYBE_SWAP (metadata[i]));
2734 ++i;
2735
2736 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2737 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2738 - MAYBE_SWAP (metadata[i]))
2739 / (2 * sizeof (offset_type)));
2740 ++i;
2741
2742 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2743
2744 return 1;
2745 }
2746
2747
2748 /* Read the index file. If everything went ok, initialize the "quick"
2749 elements of all the CUs and return 1. Otherwise, return 0. */
2750
2751 static int
2752 dwarf2_read_index (struct objfile *objfile)
2753 {
2754 struct mapped_index local_map, *map;
2755 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2756 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2757
2758 if (!read_index_from_section (objfile, objfile->name,
2759 use_deprecated_index_sections,
2760 &dwarf2_per_objfile->gdb_index, &local_map,
2761 &cu_list, &cu_list_elements,
2762 &types_list, &types_list_elements))
2763 return 0;
2764
2765 /* Don't use the index if it's empty. */
2766 if (local_map.symbol_table_slots == 0)
2767 return 0;
2768
2769 /* If there is a .dwz file, read it so we can get its CU list as
2770 well. */
2771 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2772 {
2773 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2774 struct mapped_index dwz_map;
2775 const gdb_byte *dwz_types_ignore;
2776 offset_type dwz_types_elements_ignore;
2777
2778 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2779 1,
2780 &dwz->gdb_index, &dwz_map,
2781 &dwz_list, &dwz_list_elements,
2782 &dwz_types_ignore,
2783 &dwz_types_elements_ignore))
2784 {
2785 warning (_("could not read '.gdb_index' section from %s; skipping"),
2786 bfd_get_filename (dwz->dwz_bfd));
2787 return 0;
2788 }
2789 }
2790
2791 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2792 dwz_list_elements);
2793
2794 if (types_list_elements)
2795 {
2796 struct dwarf2_section_info *section;
2797
2798 /* We can only handle a single .debug_types when we have an
2799 index. */
2800 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2801 return 0;
2802
2803 section = VEC_index (dwarf2_section_info_def,
2804 dwarf2_per_objfile->types, 0);
2805
2806 create_signatured_type_table_from_index (objfile, section, types_list,
2807 types_list_elements);
2808 }
2809
2810 create_addrmap_from_index (objfile, &local_map);
2811
2812 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2813 *map = local_map;
2814
2815 dwarf2_per_objfile->index_table = map;
2816 dwarf2_per_objfile->using_index = 1;
2817 dwarf2_per_objfile->quick_file_names_table =
2818 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2819
2820 return 1;
2821 }
2822
2823 /* A helper for the "quick" functions which sets the global
2824 dwarf2_per_objfile according to OBJFILE. */
2825
2826 static void
2827 dw2_setup (struct objfile *objfile)
2828 {
2829 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2830 gdb_assert (dwarf2_per_objfile);
2831 }
2832
2833 /* die_reader_func for dw2_get_file_names. */
2834
2835 static void
2836 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2837 gdb_byte *info_ptr,
2838 struct die_info *comp_unit_die,
2839 int has_children,
2840 void *data)
2841 {
2842 struct dwarf2_cu *cu = reader->cu;
2843 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2844 struct objfile *objfile = dwarf2_per_objfile->objfile;
2845 struct dwarf2_per_cu_data *lh_cu;
2846 struct line_header *lh;
2847 struct attribute *attr;
2848 int i;
2849 const char *name, *comp_dir;
2850 void **slot;
2851 struct quick_file_names *qfn;
2852 unsigned int line_offset;
2853
2854 /* Our callers never want to match partial units -- instead they
2855 will match the enclosing full CU. */
2856 if (comp_unit_die->tag == DW_TAG_partial_unit)
2857 {
2858 this_cu->v.quick->no_file_data = 1;
2859 return;
2860 }
2861
2862 /* If we're reading the line header for TUs, store it in the "per_cu"
2863 for tu_group. */
2864 if (this_cu->is_debug_types)
2865 {
2866 struct type_unit_group *tu_group = data;
2867
2868 gdb_assert (tu_group != NULL);
2869 lh_cu = &tu_group->per_cu;
2870 }
2871 else
2872 lh_cu = this_cu;
2873
2874 lh = NULL;
2875 slot = NULL;
2876 line_offset = 0;
2877
2878 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2879 if (attr)
2880 {
2881 struct quick_file_names find_entry;
2882
2883 line_offset = DW_UNSND (attr);
2884
2885 /* We may have already read in this line header (TU line header sharing).
2886 If we have we're done. */
2887 find_entry.hash.dwo_unit = cu->dwo_unit;
2888 find_entry.hash.line_offset.sect_off = line_offset;
2889 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2890 &find_entry, INSERT);
2891 if (*slot != NULL)
2892 {
2893 lh_cu->v.quick->file_names = *slot;
2894 return;
2895 }
2896
2897 lh = dwarf_decode_line_header (line_offset, cu);
2898 }
2899 if (lh == NULL)
2900 {
2901 lh_cu->v.quick->no_file_data = 1;
2902 return;
2903 }
2904
2905 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2906 qfn->hash.dwo_unit = cu->dwo_unit;
2907 qfn->hash.line_offset.sect_off = line_offset;
2908 gdb_assert (slot != NULL);
2909 *slot = qfn;
2910
2911 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2912
2913 qfn->num_file_names = lh->num_file_names;
2914 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2915 lh->num_file_names * sizeof (char *));
2916 for (i = 0; i < lh->num_file_names; ++i)
2917 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2918 qfn->real_names = NULL;
2919
2920 free_line_header (lh);
2921
2922 lh_cu->v.quick->file_names = qfn;
2923 }
2924
2925 /* A helper for the "quick" functions which attempts to read the line
2926 table for THIS_CU. */
2927
2928 static struct quick_file_names *
2929 dw2_get_file_names (struct objfile *objfile,
2930 struct dwarf2_per_cu_data *this_cu)
2931 {
2932 /* For TUs this should only be called on the parent group. */
2933 if (this_cu->is_debug_types)
2934 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2935
2936 if (this_cu->v.quick->file_names != NULL)
2937 return this_cu->v.quick->file_names;
2938 /* If we know there is no line data, no point in looking again. */
2939 if (this_cu->v.quick->no_file_data)
2940 return NULL;
2941
2942 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2943 in the stub for CUs, there's is no need to lookup the DWO file.
2944 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2945 DWO file. */
2946 if (this_cu->is_debug_types)
2947 {
2948 struct type_unit_group *tu_group = this_cu->type_unit_group;
2949
2950 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2951 dw2_get_file_names_reader, tu_group);
2952 }
2953 else
2954 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2955
2956 if (this_cu->v.quick->no_file_data)
2957 return NULL;
2958 return this_cu->v.quick->file_names;
2959 }
2960
2961 /* A helper for the "quick" functions which computes and caches the
2962 real path for a given file name from the line table. */
2963
2964 static const char *
2965 dw2_get_real_path (struct objfile *objfile,
2966 struct quick_file_names *qfn, int index)
2967 {
2968 if (qfn->real_names == NULL)
2969 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2970 qfn->num_file_names, sizeof (char *));
2971
2972 if (qfn->real_names[index] == NULL)
2973 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2974
2975 return qfn->real_names[index];
2976 }
2977
2978 static struct symtab *
2979 dw2_find_last_source_symtab (struct objfile *objfile)
2980 {
2981 int index;
2982
2983 dw2_setup (objfile);
2984 index = dwarf2_per_objfile->n_comp_units - 1;
2985 return dw2_instantiate_symtab (dw2_get_cu (index));
2986 }
2987
2988 /* Traversal function for dw2_forget_cached_source_info. */
2989
2990 static int
2991 dw2_free_cached_file_names (void **slot, void *info)
2992 {
2993 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2994
2995 if (file_data->real_names)
2996 {
2997 int i;
2998
2999 for (i = 0; i < file_data->num_file_names; ++i)
3000 {
3001 xfree ((void*) file_data->real_names[i]);
3002 file_data->real_names[i] = NULL;
3003 }
3004 }
3005
3006 return 1;
3007 }
3008
3009 static void
3010 dw2_forget_cached_source_info (struct objfile *objfile)
3011 {
3012 dw2_setup (objfile);
3013
3014 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3015 dw2_free_cached_file_names, NULL);
3016 }
3017
3018 /* Helper function for dw2_map_symtabs_matching_filename that expands
3019 the symtabs and calls the iterator. */
3020
3021 static int
3022 dw2_map_expand_apply (struct objfile *objfile,
3023 struct dwarf2_per_cu_data *per_cu,
3024 const char *name, const char *real_path,
3025 int (*callback) (struct symtab *, void *),
3026 void *data)
3027 {
3028 struct symtab *last_made = objfile->symtabs;
3029
3030 /* Don't visit already-expanded CUs. */
3031 if (per_cu->v.quick->symtab)
3032 return 0;
3033
3034 /* This may expand more than one symtab, and we want to iterate over
3035 all of them. */
3036 dw2_instantiate_symtab (per_cu);
3037
3038 return iterate_over_some_symtabs (name, real_path, callback, data,
3039 objfile->symtabs, last_made);
3040 }
3041
3042 /* Implementation of the map_symtabs_matching_filename method. */
3043
3044 static int
3045 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3046 const char *real_path,
3047 int (*callback) (struct symtab *, void *),
3048 void *data)
3049 {
3050 int i;
3051 const char *name_basename = lbasename (name);
3052
3053 dw2_setup (objfile);
3054
3055 /* The rule is CUs specify all the files, including those used by
3056 any TU, so there's no need to scan TUs here. */
3057
3058 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3059 {
3060 int j;
3061 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3062 struct quick_file_names *file_data;
3063
3064 /* We only need to look at symtabs not already expanded. */
3065 if (per_cu->v.quick->symtab)
3066 continue;
3067
3068 file_data = dw2_get_file_names (objfile, per_cu);
3069 if (file_data == NULL)
3070 continue;
3071
3072 for (j = 0; j < file_data->num_file_names; ++j)
3073 {
3074 const char *this_name = file_data->file_names[j];
3075
3076 if (compare_filenames_for_search (this_name, name))
3077 {
3078 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3079 callback, data))
3080 return 1;
3081 }
3082
3083 /* Before we invoke realpath, which can get expensive when many
3084 files are involved, do a quick comparison of the basenames. */
3085 if (! basenames_may_differ
3086 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3087 continue;
3088
3089 if (real_path != NULL)
3090 {
3091 const char *this_real_name = dw2_get_real_path (objfile,
3092 file_data, j);
3093
3094 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3095 gdb_assert (IS_ABSOLUTE_PATH (name));
3096 if (this_real_name != NULL
3097 && FILENAME_CMP (real_path, this_real_name) == 0)
3098 {
3099 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3100 callback, data))
3101 return 1;
3102 }
3103 }
3104 }
3105 }
3106
3107 return 0;
3108 }
3109
3110 /* Struct used to manage iterating over all CUs looking for a symbol. */
3111
3112 struct dw2_symtab_iterator
3113 {
3114 /* The internalized form of .gdb_index. */
3115 struct mapped_index *index;
3116 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3117 int want_specific_block;
3118 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3119 Unused if !WANT_SPECIFIC_BLOCK. */
3120 int block_index;
3121 /* The kind of symbol we're looking for. */
3122 domain_enum domain;
3123 /* The list of CUs from the index entry of the symbol,
3124 or NULL if not found. */
3125 offset_type *vec;
3126 /* The next element in VEC to look at. */
3127 int next;
3128 /* The number of elements in VEC, or zero if there is no match. */
3129 int length;
3130 };
3131
3132 /* Initialize the index symtab iterator ITER.
3133 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3134 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3135
3136 static void
3137 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3138 struct mapped_index *index,
3139 int want_specific_block,
3140 int block_index,
3141 domain_enum domain,
3142 const char *name)
3143 {
3144 iter->index = index;
3145 iter->want_specific_block = want_specific_block;
3146 iter->block_index = block_index;
3147 iter->domain = domain;
3148 iter->next = 0;
3149
3150 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3151 iter->length = MAYBE_SWAP (*iter->vec);
3152 else
3153 {
3154 iter->vec = NULL;
3155 iter->length = 0;
3156 }
3157 }
3158
3159 /* Return the next matching CU or NULL if there are no more. */
3160
3161 static struct dwarf2_per_cu_data *
3162 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3163 {
3164 for ( ; iter->next < iter->length; ++iter->next)
3165 {
3166 offset_type cu_index_and_attrs =
3167 MAYBE_SWAP (iter->vec[iter->next + 1]);
3168 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3169 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3170 int want_static = iter->block_index != GLOBAL_BLOCK;
3171 /* This value is only valid for index versions >= 7. */
3172 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3173 gdb_index_symbol_kind symbol_kind =
3174 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3175 /* Only check the symbol attributes if they're present.
3176 Indices prior to version 7 don't record them,
3177 and indices >= 7 may elide them for certain symbols
3178 (gold does this). */
3179 int attrs_valid =
3180 (iter->index->version >= 7
3181 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3182
3183 /* Skip if already read in. */
3184 if (per_cu->v.quick->symtab)
3185 continue;
3186
3187 if (attrs_valid
3188 && iter->want_specific_block
3189 && want_static != is_static)
3190 continue;
3191
3192 /* Only check the symbol's kind if it has one. */
3193 if (attrs_valid)
3194 {
3195 switch (iter->domain)
3196 {
3197 case VAR_DOMAIN:
3198 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3199 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3200 /* Some types are also in VAR_DOMAIN. */
3201 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3202 continue;
3203 break;
3204 case STRUCT_DOMAIN:
3205 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3206 continue;
3207 break;
3208 case LABEL_DOMAIN:
3209 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3210 continue;
3211 break;
3212 default:
3213 break;
3214 }
3215 }
3216
3217 ++iter->next;
3218 return per_cu;
3219 }
3220
3221 return NULL;
3222 }
3223
3224 static struct symtab *
3225 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3226 const char *name, domain_enum domain)
3227 {
3228 struct symtab *stab_best = NULL;
3229 struct mapped_index *index;
3230
3231 dw2_setup (objfile);
3232
3233 index = dwarf2_per_objfile->index_table;
3234
3235 /* index is NULL if OBJF_READNOW. */
3236 if (index)
3237 {
3238 struct dw2_symtab_iterator iter;
3239 struct dwarf2_per_cu_data *per_cu;
3240
3241 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3242
3243 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3244 {
3245 struct symbol *sym = NULL;
3246 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3247
3248 /* Some caution must be observed with overloaded functions
3249 and methods, since the index will not contain any overload
3250 information (but NAME might contain it). */
3251 if (stab->primary)
3252 {
3253 struct blockvector *bv = BLOCKVECTOR (stab);
3254 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3255
3256 sym = lookup_block_symbol (block, name, domain);
3257 }
3258
3259 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3260 {
3261 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3262 return stab;
3263
3264 stab_best = stab;
3265 }
3266
3267 /* Keep looking through other CUs. */
3268 }
3269 }
3270
3271 return stab_best;
3272 }
3273
3274 static void
3275 dw2_print_stats (struct objfile *objfile)
3276 {
3277 int i, count;
3278
3279 dw2_setup (objfile);
3280 count = 0;
3281 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3282 + dwarf2_per_objfile->n_type_units); ++i)
3283 {
3284 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3285
3286 if (!per_cu->v.quick->symtab)
3287 ++count;
3288 }
3289 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3290 }
3291
3292 static void
3293 dw2_dump (struct objfile *objfile)
3294 {
3295 /* Nothing worth printing. */
3296 }
3297
3298 static void
3299 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3300 struct section_offsets *delta)
3301 {
3302 /* There's nothing to relocate here. */
3303 }
3304
3305 static void
3306 dw2_expand_symtabs_for_function (struct objfile *objfile,
3307 const char *func_name)
3308 {
3309 struct mapped_index *index;
3310
3311 dw2_setup (objfile);
3312
3313 index = dwarf2_per_objfile->index_table;
3314
3315 /* index is NULL if OBJF_READNOW. */
3316 if (index)
3317 {
3318 struct dw2_symtab_iterator iter;
3319 struct dwarf2_per_cu_data *per_cu;
3320
3321 /* Note: It doesn't matter what we pass for block_index here. */
3322 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3323 func_name);
3324
3325 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3326 dw2_instantiate_symtab (per_cu);
3327 }
3328 }
3329
3330 static void
3331 dw2_expand_all_symtabs (struct objfile *objfile)
3332 {
3333 int i;
3334
3335 dw2_setup (objfile);
3336
3337 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3338 + dwarf2_per_objfile->n_type_units); ++i)
3339 {
3340 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3341
3342 dw2_instantiate_symtab (per_cu);
3343 }
3344 }
3345
3346 static void
3347 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3348 const char *fullname)
3349 {
3350 int i;
3351
3352 dw2_setup (objfile);
3353
3354 /* We don't need to consider type units here.
3355 This is only called for examining code, e.g. expand_line_sal.
3356 There can be an order of magnitude (or more) more type units
3357 than comp units, and we avoid them if we can. */
3358
3359 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3360 {
3361 int j;
3362 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3363 struct quick_file_names *file_data;
3364
3365 /* We only need to look at symtabs not already expanded. */
3366 if (per_cu->v.quick->symtab)
3367 continue;
3368
3369 file_data = dw2_get_file_names (objfile, per_cu);
3370 if (file_data == NULL)
3371 continue;
3372
3373 for (j = 0; j < file_data->num_file_names; ++j)
3374 {
3375 const char *this_fullname = file_data->file_names[j];
3376
3377 if (filename_cmp (this_fullname, fullname) == 0)
3378 {
3379 dw2_instantiate_symtab (per_cu);
3380 break;
3381 }
3382 }
3383 }
3384 }
3385
3386 /* A helper function for dw2_find_symbol_file that finds the primary
3387 file name for a given CU. This is a die_reader_func. */
3388
3389 static void
3390 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3391 gdb_byte *info_ptr,
3392 struct die_info *comp_unit_die,
3393 int has_children,
3394 void *data)
3395 {
3396 const char **result_ptr = data;
3397 struct dwarf2_cu *cu = reader->cu;
3398 struct attribute *attr;
3399
3400 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3401 if (attr == NULL)
3402 *result_ptr = NULL;
3403 else
3404 *result_ptr = DW_STRING (attr);
3405 }
3406
3407 static const char *
3408 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3409 {
3410 struct dwarf2_per_cu_data *per_cu;
3411 offset_type *vec;
3412 const char *filename;
3413
3414 dw2_setup (objfile);
3415
3416 /* index_table is NULL if OBJF_READNOW. */
3417 if (!dwarf2_per_objfile->index_table)
3418 {
3419 struct symtab *s;
3420
3421 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3422 {
3423 struct blockvector *bv = BLOCKVECTOR (s);
3424 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3425 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3426
3427 if (sym)
3428 {
3429 /* Only file extension of returned filename is recognized. */
3430 return SYMBOL_SYMTAB (sym)->filename;
3431 }
3432 }
3433 return NULL;
3434 }
3435
3436 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3437 name, &vec))
3438 return NULL;
3439
3440 /* Note that this just looks at the very first one named NAME -- but
3441 actually we are looking for a function. find_main_filename
3442 should be rewritten so that it doesn't require a custom hook. It
3443 could just use the ordinary symbol tables. */
3444 /* vec[0] is the length, which must always be >0. */
3445 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3446
3447 if (per_cu->v.quick->symtab != NULL)
3448 {
3449 /* Only file extension of returned filename is recognized. */
3450 return per_cu->v.quick->symtab->filename;
3451 }
3452
3453 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3454 dw2_get_primary_filename_reader, &filename);
3455
3456 /* Only file extension of returned filename is recognized. */
3457 return filename;
3458 }
3459
3460 static void
3461 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3462 struct objfile *objfile, int global,
3463 int (*callback) (struct block *,
3464 struct symbol *, void *),
3465 void *data, symbol_compare_ftype *match,
3466 symbol_compare_ftype *ordered_compare)
3467 {
3468 /* Currently unimplemented; used for Ada. The function can be called if the
3469 current language is Ada for a non-Ada objfile using GNU index. As Ada
3470 does not look for non-Ada symbols this function should just return. */
3471 }
3472
3473 static void
3474 dw2_expand_symtabs_matching
3475 (struct objfile *objfile,
3476 int (*file_matcher) (const char *, void *, int basenames),
3477 int (*name_matcher) (const char *, void *),
3478 enum search_domain kind,
3479 void *data)
3480 {
3481 int i;
3482 offset_type iter;
3483 struct mapped_index *index;
3484
3485 dw2_setup (objfile);
3486
3487 /* index_table is NULL if OBJF_READNOW. */
3488 if (!dwarf2_per_objfile->index_table)
3489 return;
3490 index = dwarf2_per_objfile->index_table;
3491
3492 if (file_matcher != NULL)
3493 {
3494 struct cleanup *cleanup;
3495 htab_t visited_found, visited_not_found;
3496
3497 visited_found = htab_create_alloc (10,
3498 htab_hash_pointer, htab_eq_pointer,
3499 NULL, xcalloc, xfree);
3500 cleanup = make_cleanup_htab_delete (visited_found);
3501 visited_not_found = htab_create_alloc (10,
3502 htab_hash_pointer, htab_eq_pointer,
3503 NULL, xcalloc, xfree);
3504 make_cleanup_htab_delete (visited_not_found);
3505
3506 /* The rule is CUs specify all the files, including those used by
3507 any TU, so there's no need to scan TUs here. */
3508
3509 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3510 {
3511 int j;
3512 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3513 struct quick_file_names *file_data;
3514 void **slot;
3515
3516 per_cu->v.quick->mark = 0;
3517
3518 /* We only need to look at symtabs not already expanded. */
3519 if (per_cu->v.quick->symtab)
3520 continue;
3521
3522 file_data = dw2_get_file_names (objfile, per_cu);
3523 if (file_data == NULL)
3524 continue;
3525
3526 if (htab_find (visited_not_found, file_data) != NULL)
3527 continue;
3528 else if (htab_find (visited_found, file_data) != NULL)
3529 {
3530 per_cu->v.quick->mark = 1;
3531 continue;
3532 }
3533
3534 for (j = 0; j < file_data->num_file_names; ++j)
3535 {
3536 if (file_matcher (file_data->file_names[j], data, 0))
3537 {
3538 per_cu->v.quick->mark = 1;
3539 break;
3540 }
3541 }
3542
3543 slot = htab_find_slot (per_cu->v.quick->mark
3544 ? visited_found
3545 : visited_not_found,
3546 file_data, INSERT);
3547 *slot = file_data;
3548 }
3549
3550 do_cleanups (cleanup);
3551 }
3552
3553 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3554 {
3555 offset_type idx = 2 * iter;
3556 const char *name;
3557 offset_type *vec, vec_len, vec_idx;
3558
3559 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3560 continue;
3561
3562 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3563
3564 if (! (*name_matcher) (name, data))
3565 continue;
3566
3567 /* The name was matched, now expand corresponding CUs that were
3568 marked. */
3569 vec = (offset_type *) (index->constant_pool
3570 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3571 vec_len = MAYBE_SWAP (vec[0]);
3572 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3573 {
3574 struct dwarf2_per_cu_data *per_cu;
3575 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3576 gdb_index_symbol_kind symbol_kind =
3577 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3578 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3579
3580 /* Don't crash on bad data. */
3581 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3582 + dwarf2_per_objfile->n_type_units))
3583 continue;
3584
3585 /* Only check the symbol's kind if it has one.
3586 Indices prior to version 7 don't record it. */
3587 if (index->version >= 7)
3588 {
3589 switch (kind)
3590 {
3591 case VARIABLES_DOMAIN:
3592 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3593 continue;
3594 break;
3595 case FUNCTIONS_DOMAIN:
3596 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3597 continue;
3598 break;
3599 case TYPES_DOMAIN:
3600 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3601 continue;
3602 break;
3603 default:
3604 break;
3605 }
3606 }
3607
3608 per_cu = dw2_get_cu (cu_index);
3609 if (file_matcher == NULL || per_cu->v.quick->mark)
3610 dw2_instantiate_symtab (per_cu);
3611 }
3612 }
3613 }
3614
3615 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3616 symtab. */
3617
3618 static struct symtab *
3619 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3620 {
3621 int i;
3622
3623 if (BLOCKVECTOR (symtab) != NULL
3624 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3625 return symtab;
3626
3627 if (symtab->includes == NULL)
3628 return NULL;
3629
3630 for (i = 0; symtab->includes[i]; ++i)
3631 {
3632 struct symtab *s = symtab->includes[i];
3633
3634 s = recursively_find_pc_sect_symtab (s, pc);
3635 if (s != NULL)
3636 return s;
3637 }
3638
3639 return NULL;
3640 }
3641
3642 static struct symtab *
3643 dw2_find_pc_sect_symtab (struct objfile *objfile,
3644 struct minimal_symbol *msymbol,
3645 CORE_ADDR pc,
3646 struct obj_section *section,
3647 int warn_if_readin)
3648 {
3649 struct dwarf2_per_cu_data *data;
3650 struct symtab *result;
3651
3652 dw2_setup (objfile);
3653
3654 if (!objfile->psymtabs_addrmap)
3655 return NULL;
3656
3657 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3658 if (!data)
3659 return NULL;
3660
3661 if (warn_if_readin && data->v.quick->symtab)
3662 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3663 paddress (get_objfile_arch (objfile), pc));
3664
3665 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3666 gdb_assert (result != NULL);
3667 return result;
3668 }
3669
3670 static void
3671 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3672 void *data, int need_fullname)
3673 {
3674 int i;
3675 struct cleanup *cleanup;
3676 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3677 NULL, xcalloc, xfree);
3678
3679 cleanup = make_cleanup_htab_delete (visited);
3680 dw2_setup (objfile);
3681
3682 /* The rule is CUs specify all the files, including those used by
3683 any TU, so there's no need to scan TUs here.
3684 We can ignore file names coming from already-expanded CUs. */
3685
3686 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3687 {
3688 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3689
3690 if (per_cu->v.quick->symtab)
3691 {
3692 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3693 INSERT);
3694
3695 *slot = per_cu->v.quick->file_names;
3696 }
3697 }
3698
3699 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3700 {
3701 int j;
3702 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3703 struct quick_file_names *file_data;
3704 void **slot;
3705
3706 /* We only need to look at symtabs not already expanded. */
3707 if (per_cu->v.quick->symtab)
3708 continue;
3709
3710 file_data = dw2_get_file_names (objfile, per_cu);
3711 if (file_data == NULL)
3712 continue;
3713
3714 slot = htab_find_slot (visited, file_data, INSERT);
3715 if (*slot)
3716 {
3717 /* Already visited. */
3718 continue;
3719 }
3720 *slot = file_data;
3721
3722 for (j = 0; j < file_data->num_file_names; ++j)
3723 {
3724 const char *this_real_name;
3725
3726 if (need_fullname)
3727 this_real_name = dw2_get_real_path (objfile, file_data, j);
3728 else
3729 this_real_name = NULL;
3730 (*fun) (file_data->file_names[j], this_real_name, data);
3731 }
3732 }
3733
3734 do_cleanups (cleanup);
3735 }
3736
3737 static int
3738 dw2_has_symbols (struct objfile *objfile)
3739 {
3740 return 1;
3741 }
3742
3743 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3744 {
3745 dw2_has_symbols,
3746 dw2_find_last_source_symtab,
3747 dw2_forget_cached_source_info,
3748 dw2_map_symtabs_matching_filename,
3749 dw2_lookup_symbol,
3750 dw2_print_stats,
3751 dw2_dump,
3752 dw2_relocate,
3753 dw2_expand_symtabs_for_function,
3754 dw2_expand_all_symtabs,
3755 dw2_expand_symtabs_with_fullname,
3756 dw2_find_symbol_file,
3757 dw2_map_matching_symbols,
3758 dw2_expand_symtabs_matching,
3759 dw2_find_pc_sect_symtab,
3760 dw2_map_symbol_filenames
3761 };
3762
3763 /* Initialize for reading DWARF for this objfile. Return 0 if this
3764 file will use psymtabs, or 1 if using the GNU index. */
3765
3766 int
3767 dwarf2_initialize_objfile (struct objfile *objfile)
3768 {
3769 /* If we're about to read full symbols, don't bother with the
3770 indices. In this case we also don't care if some other debug
3771 format is making psymtabs, because they are all about to be
3772 expanded anyway. */
3773 if ((objfile->flags & OBJF_READNOW))
3774 {
3775 int i;
3776
3777 dwarf2_per_objfile->using_index = 1;
3778 create_all_comp_units (objfile);
3779 create_all_type_units (objfile);
3780 dwarf2_per_objfile->quick_file_names_table =
3781 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3782
3783 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3784 + dwarf2_per_objfile->n_type_units); ++i)
3785 {
3786 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3787
3788 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3789 struct dwarf2_per_cu_quick_data);
3790 }
3791
3792 /* Return 1 so that gdb sees the "quick" functions. However,
3793 these functions will be no-ops because we will have expanded
3794 all symtabs. */
3795 return 1;
3796 }
3797
3798 if (dwarf2_read_index (objfile))
3799 return 1;
3800
3801 return 0;
3802 }
3803
3804 \f
3805
3806 /* Build a partial symbol table. */
3807
3808 void
3809 dwarf2_build_psymtabs (struct objfile *objfile)
3810 {
3811 volatile struct gdb_exception except;
3812
3813 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3814 {
3815 init_psymbol_list (objfile, 1024);
3816 }
3817
3818 TRY_CATCH (except, RETURN_MASK_ERROR)
3819 {
3820 /* This isn't really ideal: all the data we allocate on the
3821 objfile's obstack is still uselessly kept around. However,
3822 freeing it seems unsafe. */
3823 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3824
3825 dwarf2_build_psymtabs_hard (objfile);
3826 discard_cleanups (cleanups);
3827 }
3828 if (except.reason < 0)
3829 exception_print (gdb_stderr, except);
3830 }
3831
3832 /* Return the total length of the CU described by HEADER. */
3833
3834 static unsigned int
3835 get_cu_length (const struct comp_unit_head *header)
3836 {
3837 return header->initial_length_size + header->length;
3838 }
3839
3840 /* Return TRUE if OFFSET is within CU_HEADER. */
3841
3842 static inline int
3843 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3844 {
3845 sect_offset bottom = { cu_header->offset.sect_off };
3846 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3847
3848 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3849 }
3850
3851 /* Find the base address of the compilation unit for range lists and
3852 location lists. It will normally be specified by DW_AT_low_pc.
3853 In DWARF-3 draft 4, the base address could be overridden by
3854 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3855 compilation units with discontinuous ranges. */
3856
3857 static void
3858 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3859 {
3860 struct attribute *attr;
3861
3862 cu->base_known = 0;
3863 cu->base_address = 0;
3864
3865 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3866 if (attr)
3867 {
3868 cu->base_address = DW_ADDR (attr);
3869 cu->base_known = 1;
3870 }
3871 else
3872 {
3873 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3874 if (attr)
3875 {
3876 cu->base_address = DW_ADDR (attr);
3877 cu->base_known = 1;
3878 }
3879 }
3880 }
3881
3882 /* Read in the comp unit header information from the debug_info at info_ptr.
3883 NOTE: This leaves members offset, first_die_offset to be filled in
3884 by the caller. */
3885
3886 static gdb_byte *
3887 read_comp_unit_head (struct comp_unit_head *cu_header,
3888 gdb_byte *info_ptr, bfd *abfd)
3889 {
3890 int signed_addr;
3891 unsigned int bytes_read;
3892
3893 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3894 cu_header->initial_length_size = bytes_read;
3895 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3896 info_ptr += bytes_read;
3897 cu_header->version = read_2_bytes (abfd, info_ptr);
3898 info_ptr += 2;
3899 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3900 &bytes_read);
3901 info_ptr += bytes_read;
3902 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3903 info_ptr += 1;
3904 signed_addr = bfd_get_sign_extend_vma (abfd);
3905 if (signed_addr < 0)
3906 internal_error (__FILE__, __LINE__,
3907 _("read_comp_unit_head: dwarf from non elf file"));
3908 cu_header->signed_addr_p = signed_addr;
3909
3910 return info_ptr;
3911 }
3912
3913 /* Helper function that returns the proper abbrev section for
3914 THIS_CU. */
3915
3916 static struct dwarf2_section_info *
3917 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3918 {
3919 struct dwarf2_section_info *abbrev;
3920
3921 if (this_cu->is_dwz)
3922 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3923 else
3924 abbrev = &dwarf2_per_objfile->abbrev;
3925
3926 return abbrev;
3927 }
3928
3929 /* Subroutine of read_and_check_comp_unit_head and
3930 read_and_check_type_unit_head to simplify them.
3931 Perform various error checking on the header. */
3932
3933 static void
3934 error_check_comp_unit_head (struct comp_unit_head *header,
3935 struct dwarf2_section_info *section,
3936 struct dwarf2_section_info *abbrev_section)
3937 {
3938 bfd *abfd = section->asection->owner;
3939 const char *filename = bfd_get_filename (abfd);
3940
3941 if (header->version != 2 && header->version != 3 && header->version != 4)
3942 error (_("Dwarf Error: wrong version in compilation unit header "
3943 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3944 filename);
3945
3946 if (header->abbrev_offset.sect_off
3947 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3948 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3949 "(offset 0x%lx + 6) [in module %s]"),
3950 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3951 filename);
3952
3953 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3954 avoid potential 32-bit overflow. */
3955 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3956 > section->size)
3957 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3958 "(offset 0x%lx + 0) [in module %s]"),
3959 (long) header->length, (long) header->offset.sect_off,
3960 filename);
3961 }
3962
3963 /* Read in a CU/TU header and perform some basic error checking.
3964 The contents of the header are stored in HEADER.
3965 The result is a pointer to the start of the first DIE. */
3966
3967 static gdb_byte *
3968 read_and_check_comp_unit_head (struct comp_unit_head *header,
3969 struct dwarf2_section_info *section,
3970 struct dwarf2_section_info *abbrev_section,
3971 gdb_byte *info_ptr,
3972 int is_debug_types_section)
3973 {
3974 gdb_byte *beg_of_comp_unit = info_ptr;
3975 bfd *abfd = section->asection->owner;
3976
3977 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3978
3979 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3980
3981 /* If we're reading a type unit, skip over the signature and
3982 type_offset fields. */
3983 if (is_debug_types_section)
3984 info_ptr += 8 /*signature*/ + header->offset_size;
3985
3986 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3987
3988 error_check_comp_unit_head (header, section, abbrev_section);
3989
3990 return info_ptr;
3991 }
3992
3993 /* Read in the types comp unit header information from .debug_types entry at
3994 types_ptr. The result is a pointer to one past the end of the header. */
3995
3996 static gdb_byte *
3997 read_and_check_type_unit_head (struct comp_unit_head *header,
3998 struct dwarf2_section_info *section,
3999 struct dwarf2_section_info *abbrev_section,
4000 gdb_byte *info_ptr,
4001 ULONGEST *signature,
4002 cu_offset *type_offset_in_tu)
4003 {
4004 gdb_byte *beg_of_comp_unit = info_ptr;
4005 bfd *abfd = section->asection->owner;
4006
4007 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4008
4009 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4010
4011 /* If we're reading a type unit, skip over the signature and
4012 type_offset fields. */
4013 if (signature != NULL)
4014 *signature = read_8_bytes (abfd, info_ptr);
4015 info_ptr += 8;
4016 if (type_offset_in_tu != NULL)
4017 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4018 header->offset_size);
4019 info_ptr += header->offset_size;
4020
4021 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4022
4023 error_check_comp_unit_head (header, section, abbrev_section);
4024
4025 return info_ptr;
4026 }
4027
4028 /* Fetch the abbreviation table offset from a comp or type unit header. */
4029
4030 static sect_offset
4031 read_abbrev_offset (struct dwarf2_section_info *section,
4032 sect_offset offset)
4033 {
4034 bfd *abfd = section->asection->owner;
4035 gdb_byte *info_ptr;
4036 unsigned int length, initial_length_size, offset_size;
4037 sect_offset abbrev_offset;
4038
4039 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4040 info_ptr = section->buffer + offset.sect_off;
4041 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4042 offset_size = initial_length_size == 4 ? 4 : 8;
4043 info_ptr += initial_length_size + 2 /*version*/;
4044 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4045 return abbrev_offset;
4046 }
4047
4048 /* Allocate a new partial symtab for file named NAME and mark this new
4049 partial symtab as being an include of PST. */
4050
4051 static void
4052 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4053 struct objfile *objfile)
4054 {
4055 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4056
4057 if (!IS_ABSOLUTE_PATH (subpst->filename))
4058 {
4059 /* It shares objfile->objfile_obstack. */
4060 subpst->dirname = pst->dirname;
4061 }
4062
4063 subpst->section_offsets = pst->section_offsets;
4064 subpst->textlow = 0;
4065 subpst->texthigh = 0;
4066
4067 subpst->dependencies = (struct partial_symtab **)
4068 obstack_alloc (&objfile->objfile_obstack,
4069 sizeof (struct partial_symtab *));
4070 subpst->dependencies[0] = pst;
4071 subpst->number_of_dependencies = 1;
4072
4073 subpst->globals_offset = 0;
4074 subpst->n_global_syms = 0;
4075 subpst->statics_offset = 0;
4076 subpst->n_static_syms = 0;
4077 subpst->symtab = NULL;
4078 subpst->read_symtab = pst->read_symtab;
4079 subpst->readin = 0;
4080
4081 /* No private part is necessary for include psymtabs. This property
4082 can be used to differentiate between such include psymtabs and
4083 the regular ones. */
4084 subpst->read_symtab_private = NULL;
4085 }
4086
4087 /* Read the Line Number Program data and extract the list of files
4088 included by the source file represented by PST. Build an include
4089 partial symtab for each of these included files. */
4090
4091 static void
4092 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4093 struct die_info *die,
4094 struct partial_symtab *pst)
4095 {
4096 struct line_header *lh = NULL;
4097 struct attribute *attr;
4098
4099 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4100 if (attr)
4101 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4102 if (lh == NULL)
4103 return; /* No linetable, so no includes. */
4104
4105 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4106 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4107
4108 free_line_header (lh);
4109 }
4110
4111 static hashval_t
4112 hash_signatured_type (const void *item)
4113 {
4114 const struct signatured_type *sig_type = item;
4115
4116 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4117 return sig_type->signature;
4118 }
4119
4120 static int
4121 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4122 {
4123 const struct signatured_type *lhs = item_lhs;
4124 const struct signatured_type *rhs = item_rhs;
4125
4126 return lhs->signature == rhs->signature;
4127 }
4128
4129 /* Allocate a hash table for signatured types. */
4130
4131 static htab_t
4132 allocate_signatured_type_table (struct objfile *objfile)
4133 {
4134 return htab_create_alloc_ex (41,
4135 hash_signatured_type,
4136 eq_signatured_type,
4137 NULL,
4138 &objfile->objfile_obstack,
4139 hashtab_obstack_allocate,
4140 dummy_obstack_deallocate);
4141 }
4142
4143 /* A helper function to add a signatured type CU to a table. */
4144
4145 static int
4146 add_signatured_type_cu_to_table (void **slot, void *datum)
4147 {
4148 struct signatured_type *sigt = *slot;
4149 struct signatured_type ***datap = datum;
4150
4151 **datap = sigt;
4152 ++*datap;
4153
4154 return 1;
4155 }
4156
4157 /* Create the hash table of all entries in the .debug_types section.
4158 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4159 NULL otherwise.
4160 Note: This function processes DWO files only, not DWP files.
4161 The result is a pointer to the hash table or NULL if there are
4162 no types. */
4163
4164 static htab_t
4165 create_debug_types_hash_table (struct dwo_file *dwo_file,
4166 VEC (dwarf2_section_info_def) *types)
4167 {
4168 struct objfile *objfile = dwarf2_per_objfile->objfile;
4169 htab_t types_htab = NULL;
4170 int ix;
4171 struct dwarf2_section_info *section;
4172 struct dwarf2_section_info *abbrev_section;
4173
4174 if (VEC_empty (dwarf2_section_info_def, types))
4175 return NULL;
4176
4177 abbrev_section = (dwo_file != NULL
4178 ? &dwo_file->sections.abbrev
4179 : &dwarf2_per_objfile->abbrev);
4180
4181 if (dwarf2_read_debug)
4182 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4183 dwo_file ? ".dwo" : "",
4184 bfd_get_filename (abbrev_section->asection->owner));
4185
4186 for (ix = 0;
4187 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4188 ++ix)
4189 {
4190 bfd *abfd;
4191 gdb_byte *info_ptr, *end_ptr;
4192 struct dwarf2_section_info *abbrev_section;
4193
4194 dwarf2_read_section (objfile, section);
4195 info_ptr = section->buffer;
4196
4197 if (info_ptr == NULL)
4198 continue;
4199
4200 /* We can't set abfd until now because the section may be empty or
4201 not present, in which case section->asection will be NULL. */
4202 abfd = section->asection->owner;
4203
4204 if (dwo_file)
4205 abbrev_section = &dwo_file->sections.abbrev;
4206 else
4207 abbrev_section = &dwarf2_per_objfile->abbrev;
4208
4209 if (types_htab == NULL)
4210 {
4211 if (dwo_file)
4212 types_htab = allocate_dwo_unit_table (objfile);
4213 else
4214 types_htab = allocate_signatured_type_table (objfile);
4215 }
4216
4217 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4218 because we don't need to read any dies: the signature is in the
4219 header. */
4220
4221 end_ptr = info_ptr + section->size;
4222 while (info_ptr < end_ptr)
4223 {
4224 sect_offset offset;
4225 cu_offset type_offset_in_tu;
4226 ULONGEST signature;
4227 struct signatured_type *sig_type;
4228 struct dwo_unit *dwo_tu;
4229 void **slot;
4230 gdb_byte *ptr = info_ptr;
4231 struct comp_unit_head header;
4232 unsigned int length;
4233
4234 offset.sect_off = ptr - section->buffer;
4235
4236 /* We need to read the type's signature in order to build the hash
4237 table, but we don't need anything else just yet. */
4238
4239 ptr = read_and_check_type_unit_head (&header, section,
4240 abbrev_section, ptr,
4241 &signature, &type_offset_in_tu);
4242
4243 length = get_cu_length (&header);
4244
4245 /* Skip dummy type units. */
4246 if (ptr >= info_ptr + length
4247 || peek_abbrev_code (abfd, ptr) == 0)
4248 {
4249 info_ptr += length;
4250 continue;
4251 }
4252
4253 if (dwo_file)
4254 {
4255 sig_type = NULL;
4256 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4257 struct dwo_unit);
4258 dwo_tu->dwo_file = dwo_file;
4259 dwo_tu->signature = signature;
4260 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4261 dwo_tu->info_or_types_section = section;
4262 dwo_tu->offset = offset;
4263 dwo_tu->length = length;
4264 }
4265 else
4266 {
4267 /* N.B.: type_offset is not usable if this type uses a DWO file.
4268 The real type_offset is in the DWO file. */
4269 dwo_tu = NULL;
4270 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4271 struct signatured_type);
4272 sig_type->signature = signature;
4273 sig_type->type_offset_in_tu = type_offset_in_tu;
4274 sig_type->per_cu.objfile = objfile;
4275 sig_type->per_cu.is_debug_types = 1;
4276 sig_type->per_cu.info_or_types_section = section;
4277 sig_type->per_cu.offset = offset;
4278 sig_type->per_cu.length = length;
4279 }
4280
4281 slot = htab_find_slot (types_htab,
4282 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4283 INSERT);
4284 gdb_assert (slot != NULL);
4285 if (*slot != NULL)
4286 {
4287 sect_offset dup_offset;
4288
4289 if (dwo_file)
4290 {
4291 const struct dwo_unit *dup_tu = *slot;
4292
4293 dup_offset = dup_tu->offset;
4294 }
4295 else
4296 {
4297 const struct signatured_type *dup_tu = *slot;
4298
4299 dup_offset = dup_tu->per_cu.offset;
4300 }
4301
4302 complaint (&symfile_complaints,
4303 _("debug type entry at offset 0x%x is duplicate to the "
4304 "entry at offset 0x%x, signature 0x%s"),
4305 offset.sect_off, dup_offset.sect_off,
4306 phex (signature, sizeof (signature)));
4307 }
4308 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4309
4310 if (dwarf2_read_debug)
4311 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4312 offset.sect_off,
4313 phex (signature, sizeof (signature)));
4314
4315 info_ptr += length;
4316 }
4317 }
4318
4319 return types_htab;
4320 }
4321
4322 /* Create the hash table of all entries in the .debug_types section,
4323 and initialize all_type_units.
4324 The result is zero if there is an error (e.g. missing .debug_types section),
4325 otherwise non-zero. */
4326
4327 static int
4328 create_all_type_units (struct objfile *objfile)
4329 {
4330 htab_t types_htab;
4331 struct signatured_type **iter;
4332
4333 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4334 if (types_htab == NULL)
4335 {
4336 dwarf2_per_objfile->signatured_types = NULL;
4337 return 0;
4338 }
4339
4340 dwarf2_per_objfile->signatured_types = types_htab;
4341
4342 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4343 dwarf2_per_objfile->all_type_units
4344 = obstack_alloc (&objfile->objfile_obstack,
4345 dwarf2_per_objfile->n_type_units
4346 * sizeof (struct signatured_type *));
4347 iter = &dwarf2_per_objfile->all_type_units[0];
4348 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4349 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4350 == dwarf2_per_objfile->n_type_units);
4351
4352 return 1;
4353 }
4354
4355 /* Lookup a signature based type for DW_FORM_ref_sig8.
4356 Returns NULL if signature SIG is not present in the table. */
4357
4358 static struct signatured_type *
4359 lookup_signatured_type (ULONGEST sig)
4360 {
4361 struct signatured_type find_entry, *entry;
4362
4363 if (dwarf2_per_objfile->signatured_types == NULL)
4364 {
4365 complaint (&symfile_complaints,
4366 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4367 return NULL;
4368 }
4369
4370 find_entry.signature = sig;
4371 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4372 return entry;
4373 }
4374 \f
4375 /* Low level DIE reading support. */
4376
4377 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4378
4379 static void
4380 init_cu_die_reader (struct die_reader_specs *reader,
4381 struct dwarf2_cu *cu,
4382 struct dwarf2_section_info *section,
4383 struct dwo_file *dwo_file)
4384 {
4385 gdb_assert (section->readin && section->buffer != NULL);
4386 reader->abfd = section->asection->owner;
4387 reader->cu = cu;
4388 reader->dwo_file = dwo_file;
4389 reader->die_section = section;
4390 reader->buffer = section->buffer;
4391 reader->buffer_end = section->buffer + section->size;
4392 }
4393
4394 /* Initialize a CU (or TU) and read its DIEs.
4395 If the CU defers to a DWO file, read the DWO file as well.
4396
4397 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4398 Otherwise the table specified in the comp unit header is read in and used.
4399 This is an optimization for when we already have the abbrev table.
4400
4401 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4402 Otherwise, a new CU is allocated with xmalloc.
4403
4404 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4405 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4406
4407 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4408 linker) then DIE_READER_FUNC will not get called. */
4409
4410 static void
4411 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4412 struct abbrev_table *abbrev_table,
4413 int use_existing_cu, int keep,
4414 die_reader_func_ftype *die_reader_func,
4415 void *data)
4416 {
4417 struct objfile *objfile = dwarf2_per_objfile->objfile;
4418 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4419 bfd *abfd = section->asection->owner;
4420 struct dwarf2_cu *cu;
4421 gdb_byte *begin_info_ptr, *info_ptr;
4422 struct die_reader_specs reader;
4423 struct die_info *comp_unit_die;
4424 int has_children;
4425 struct attribute *attr;
4426 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4427 struct signatured_type *sig_type = NULL;
4428 struct dwarf2_section_info *abbrev_section;
4429 /* Non-zero if CU currently points to a DWO file and we need to
4430 reread it. When this happens we need to reread the skeleton die
4431 before we can reread the DWO file. */
4432 int rereading_dwo_cu = 0;
4433
4434 if (dwarf2_die_debug)
4435 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4436 this_cu->is_debug_types ? "type" : "comp",
4437 this_cu->offset.sect_off);
4438
4439 if (use_existing_cu)
4440 gdb_assert (keep);
4441
4442 cleanups = make_cleanup (null_cleanup, NULL);
4443
4444 /* This is cheap if the section is already read in. */
4445 dwarf2_read_section (objfile, section);
4446
4447 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4448
4449 abbrev_section = get_abbrev_section_for_cu (this_cu);
4450
4451 if (use_existing_cu && this_cu->cu != NULL)
4452 {
4453 cu = this_cu->cu;
4454
4455 /* If this CU is from a DWO file we need to start over, we need to
4456 refetch the attributes from the skeleton CU.
4457 This could be optimized by retrieving those attributes from when we
4458 were here the first time: the previous comp_unit_die was stored in
4459 comp_unit_obstack. But there's no data yet that we need this
4460 optimization. */
4461 if (cu->dwo_unit != NULL)
4462 rereading_dwo_cu = 1;
4463 }
4464 else
4465 {
4466 /* If !use_existing_cu, this_cu->cu must be NULL. */
4467 gdb_assert (this_cu->cu == NULL);
4468
4469 cu = xmalloc (sizeof (*cu));
4470 init_one_comp_unit (cu, this_cu);
4471
4472 /* If an error occurs while loading, release our storage. */
4473 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4474 }
4475
4476 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4477 {
4478 /* We already have the header, there's no need to read it in again. */
4479 info_ptr += cu->header.first_die_offset.cu_off;
4480 }
4481 else
4482 {
4483 if (this_cu->is_debug_types)
4484 {
4485 ULONGEST signature;
4486 cu_offset type_offset_in_tu;
4487
4488 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4489 abbrev_section, info_ptr,
4490 &signature,
4491 &type_offset_in_tu);
4492
4493 /* Since per_cu is the first member of struct signatured_type,
4494 we can go from a pointer to one to a pointer to the other. */
4495 sig_type = (struct signatured_type *) this_cu;
4496 gdb_assert (sig_type->signature == signature);
4497 gdb_assert (sig_type->type_offset_in_tu.cu_off
4498 == type_offset_in_tu.cu_off);
4499 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4500
4501 /* LENGTH has not been set yet for type units if we're
4502 using .gdb_index. */
4503 this_cu->length = get_cu_length (&cu->header);
4504
4505 /* Establish the type offset that can be used to lookup the type. */
4506 sig_type->type_offset_in_section.sect_off =
4507 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4508 }
4509 else
4510 {
4511 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4512 abbrev_section,
4513 info_ptr, 0);
4514
4515 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4516 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4517 }
4518 }
4519
4520 /* Skip dummy compilation units. */
4521 if (info_ptr >= begin_info_ptr + this_cu->length
4522 || peek_abbrev_code (abfd, info_ptr) == 0)
4523 {
4524 do_cleanups (cleanups);
4525 return;
4526 }
4527
4528 /* If we don't have them yet, read the abbrevs for this compilation unit.
4529 And if we need to read them now, make sure they're freed when we're
4530 done. Note that it's important that if the CU had an abbrev table
4531 on entry we don't free it when we're done: Somewhere up the call stack
4532 it may be in use. */
4533 if (abbrev_table != NULL)
4534 {
4535 gdb_assert (cu->abbrev_table == NULL);
4536 gdb_assert (cu->header.abbrev_offset.sect_off
4537 == abbrev_table->offset.sect_off);
4538 cu->abbrev_table = abbrev_table;
4539 }
4540 else if (cu->abbrev_table == NULL)
4541 {
4542 dwarf2_read_abbrevs (cu, abbrev_section);
4543 make_cleanup (dwarf2_free_abbrev_table, cu);
4544 }
4545 else if (rereading_dwo_cu)
4546 {
4547 dwarf2_free_abbrev_table (cu);
4548 dwarf2_read_abbrevs (cu, abbrev_section);
4549 }
4550
4551 /* Read the top level CU/TU die. */
4552 init_cu_die_reader (&reader, cu, section, NULL);
4553 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4554
4555 /* If we have a DWO stub, process it and then read in the DWO file.
4556 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4557 a DWO CU, that this test will fail. */
4558 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4559 if (attr)
4560 {
4561 const char *dwo_name = DW_STRING (attr);
4562 const char *comp_dir_string;
4563 struct dwo_unit *dwo_unit;
4564 ULONGEST signature; /* Or dwo_id. */
4565 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4566 int i,num_extra_attrs;
4567 struct dwarf2_section_info *dwo_abbrev_section;
4568
4569 if (has_children)
4570 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4571 " has children (offset 0x%x) [in module %s]"),
4572 this_cu->offset.sect_off, bfd_get_filename (abfd));
4573
4574 /* These attributes aren't processed until later:
4575 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4576 However, the attribute is found in the stub which we won't have later.
4577 In order to not impose this complication on the rest of the code,
4578 we read them here and copy them to the DWO CU/TU die. */
4579
4580 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4581 DWO file. */
4582 stmt_list = NULL;
4583 if (! this_cu->is_debug_types)
4584 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4585 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4586 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4587 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4588 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4589
4590 /* There should be a DW_AT_addr_base attribute here (if needed).
4591 We need the value before we can process DW_FORM_GNU_addr_index. */
4592 cu->addr_base = 0;
4593 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4594 if (attr)
4595 cu->addr_base = DW_UNSND (attr);
4596
4597 /* There should be a DW_AT_ranges_base attribute here (if needed).
4598 We need the value before we can process DW_AT_ranges. */
4599 cu->ranges_base = 0;
4600 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4601 if (attr)
4602 cu->ranges_base = DW_UNSND (attr);
4603
4604 if (this_cu->is_debug_types)
4605 {
4606 gdb_assert (sig_type != NULL);
4607 signature = sig_type->signature;
4608 }
4609 else
4610 {
4611 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4612 if (! attr)
4613 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4614 dwo_name);
4615 signature = DW_UNSND (attr);
4616 }
4617
4618 /* We may need the comp_dir in order to find the DWO file. */
4619 comp_dir_string = NULL;
4620 if (comp_dir)
4621 comp_dir_string = DW_STRING (comp_dir);
4622
4623 if (this_cu->is_debug_types)
4624 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4625 else
4626 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4627 signature);
4628
4629 if (dwo_unit == NULL)
4630 {
4631 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4632 " with ID %s [in module %s]"),
4633 this_cu->offset.sect_off,
4634 phex (signature, sizeof (signature)),
4635 objfile->name);
4636 }
4637
4638 /* Set up for reading the DWO CU/TU. */
4639 cu->dwo_unit = dwo_unit;
4640 section = dwo_unit->info_or_types_section;
4641 dwarf2_read_section (objfile, section);
4642 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4643 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4644 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4645
4646 if (this_cu->is_debug_types)
4647 {
4648 ULONGEST signature;
4649 cu_offset type_offset_in_tu;
4650
4651 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4652 dwo_abbrev_section,
4653 info_ptr,
4654 &signature,
4655 &type_offset_in_tu);
4656 gdb_assert (sig_type->signature == signature);
4657 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4658 /* For DWOs coming from DWP files, we don't know the CU length
4659 nor the type's offset in the TU until now. */
4660 dwo_unit->length = get_cu_length (&cu->header);
4661 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4662
4663 /* Establish the type offset that can be used to lookup the type.
4664 For DWO files, we don't know it until now. */
4665 sig_type->type_offset_in_section.sect_off =
4666 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4667 }
4668 else
4669 {
4670 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4671 dwo_abbrev_section,
4672 info_ptr, 0);
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 until now. */
4676 dwo_unit->length = get_cu_length (&cu->header);
4677 }
4678
4679 /* Discard the original CU's abbrev table, and read the DWO's. */
4680 if (abbrev_table == NULL)
4681 {
4682 dwarf2_free_abbrev_table (cu);
4683 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4684 }
4685 else
4686 {
4687 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4688 make_cleanup (dwarf2_free_abbrev_table, cu);
4689 }
4690
4691 /* Read in the die, but leave space to copy over the attributes
4692 from the stub. This has the benefit of simplifying the rest of
4693 the code - all the real work is done here. */
4694 num_extra_attrs = ((stmt_list != NULL)
4695 + (low_pc != NULL)
4696 + (high_pc != NULL)
4697 + (ranges != NULL)
4698 + (comp_dir != NULL));
4699 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4700 &has_children, num_extra_attrs);
4701
4702 /* Copy over the attributes from the stub to the DWO die. */
4703 i = comp_unit_die->num_attrs;
4704 if (stmt_list != NULL)
4705 comp_unit_die->attrs[i++] = *stmt_list;
4706 if (low_pc != NULL)
4707 comp_unit_die->attrs[i++] = *low_pc;
4708 if (high_pc != NULL)
4709 comp_unit_die->attrs[i++] = *high_pc;
4710 if (ranges != NULL)
4711 comp_unit_die->attrs[i++] = *ranges;
4712 if (comp_dir != NULL)
4713 comp_unit_die->attrs[i++] = *comp_dir;
4714 comp_unit_die->num_attrs += num_extra_attrs;
4715
4716 /* Skip dummy compilation units. */
4717 if (info_ptr >= begin_info_ptr + dwo_unit->length
4718 || peek_abbrev_code (abfd, info_ptr) == 0)
4719 {
4720 do_cleanups (cleanups);
4721 return;
4722 }
4723 }
4724
4725 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4726
4727 if (free_cu_cleanup != NULL)
4728 {
4729 if (keep)
4730 {
4731 /* We've successfully allocated this compilation unit. Let our
4732 caller clean it up when finished with it. */
4733 discard_cleanups (free_cu_cleanup);
4734
4735 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4736 So we have to manually free the abbrev table. */
4737 dwarf2_free_abbrev_table (cu);
4738
4739 /* Link this CU into read_in_chain. */
4740 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4741 dwarf2_per_objfile->read_in_chain = this_cu;
4742 }
4743 else
4744 do_cleanups (free_cu_cleanup);
4745 }
4746
4747 do_cleanups (cleanups);
4748 }
4749
4750 /* Read CU/TU THIS_CU in section SECTION,
4751 but do not follow DW_AT_GNU_dwo_name if present.
4752 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4753 to have already done the lookup to find the DWO/DWP file).
4754
4755 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4756 THIS_CU->is_debug_types, but nothing else.
4757
4758 We fill in THIS_CU->length.
4759
4760 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4761 linker) then DIE_READER_FUNC will not get called.
4762
4763 THIS_CU->cu is always freed when done.
4764 This is done in order to not leave THIS_CU->cu in a state where we have
4765 to care whether it refers to the "main" CU or the DWO CU. */
4766
4767 static void
4768 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4769 struct dwarf2_section_info *abbrev_section,
4770 struct dwo_file *dwo_file,
4771 die_reader_func_ftype *die_reader_func,
4772 void *data)
4773 {
4774 struct objfile *objfile = dwarf2_per_objfile->objfile;
4775 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4776 bfd *abfd = section->asection->owner;
4777 struct dwarf2_cu cu;
4778 gdb_byte *begin_info_ptr, *info_ptr;
4779 struct die_reader_specs reader;
4780 struct cleanup *cleanups;
4781 struct die_info *comp_unit_die;
4782 int has_children;
4783
4784 if (dwarf2_die_debug)
4785 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4786 this_cu->is_debug_types ? "type" : "comp",
4787 this_cu->offset.sect_off);
4788
4789 gdb_assert (this_cu->cu == NULL);
4790
4791 /* This is cheap if the section is already read in. */
4792 dwarf2_read_section (objfile, section);
4793
4794 init_one_comp_unit (&cu, this_cu);
4795
4796 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4797
4798 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4799 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4800 abbrev_section, info_ptr,
4801 this_cu->is_debug_types);
4802
4803 this_cu->length = get_cu_length (&cu.header);
4804
4805 /* Skip dummy compilation units. */
4806 if (info_ptr >= begin_info_ptr + this_cu->length
4807 || peek_abbrev_code (abfd, info_ptr) == 0)
4808 {
4809 do_cleanups (cleanups);
4810 return;
4811 }
4812
4813 dwarf2_read_abbrevs (&cu, abbrev_section);
4814 make_cleanup (dwarf2_free_abbrev_table, &cu);
4815
4816 init_cu_die_reader (&reader, &cu, section, dwo_file);
4817 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4818
4819 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4820
4821 do_cleanups (cleanups);
4822 }
4823
4824 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4825 does not lookup the specified DWO file.
4826 This cannot be used to read DWO files.
4827
4828 THIS_CU->cu is always freed when done.
4829 This is done in order to not leave THIS_CU->cu in a state where we have
4830 to care whether it refers to the "main" CU or the DWO CU.
4831 We can revisit this if the data shows there's a performance issue. */
4832
4833 static void
4834 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4835 die_reader_func_ftype *die_reader_func,
4836 void *data)
4837 {
4838 init_cutu_and_read_dies_no_follow (this_cu,
4839 get_abbrev_section_for_cu (this_cu),
4840 NULL,
4841 die_reader_func, data);
4842 }
4843
4844 /* Create a psymtab named NAME and assign it to PER_CU.
4845
4846 The caller must fill in the following details:
4847 dirname, textlow, texthigh. */
4848
4849 static struct partial_symtab *
4850 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4851 {
4852 struct objfile *objfile = per_cu->objfile;
4853 struct partial_symtab *pst;
4854
4855 pst = start_psymtab_common (objfile, objfile->section_offsets,
4856 name, 0,
4857 objfile->global_psymbols.next,
4858 objfile->static_psymbols.next);
4859
4860 pst->psymtabs_addrmap_supported = 1;
4861
4862 /* This is the glue that links PST into GDB's symbol API. */
4863 pst->read_symtab_private = per_cu;
4864 pst->read_symtab = dwarf2_read_symtab;
4865 per_cu->v.psymtab = pst;
4866
4867 return pst;
4868 }
4869
4870 /* die_reader_func for process_psymtab_comp_unit. */
4871
4872 static void
4873 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4874 gdb_byte *info_ptr,
4875 struct die_info *comp_unit_die,
4876 int has_children,
4877 void *data)
4878 {
4879 struct dwarf2_cu *cu = reader->cu;
4880 struct objfile *objfile = cu->objfile;
4881 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4882 struct attribute *attr;
4883 CORE_ADDR baseaddr;
4884 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4885 struct partial_symtab *pst;
4886 int has_pc_info;
4887 const char *filename;
4888 int *want_partial_unit_ptr = data;
4889
4890 if (comp_unit_die->tag == DW_TAG_partial_unit
4891 && (want_partial_unit_ptr == NULL
4892 || !*want_partial_unit_ptr))
4893 return;
4894
4895 gdb_assert (! per_cu->is_debug_types);
4896
4897 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4898
4899 cu->list_in_scope = &file_symbols;
4900
4901 /* Allocate a new partial symbol table structure. */
4902 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4903 if (attr == NULL || !DW_STRING (attr))
4904 filename = "";
4905 else
4906 filename = DW_STRING (attr);
4907
4908 pst = create_partial_symtab (per_cu, filename);
4909
4910 /* This must be done before calling dwarf2_build_include_psymtabs. */
4911 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4912 if (attr != NULL)
4913 pst->dirname = DW_STRING (attr);
4914
4915 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4916
4917 dwarf2_find_base_address (comp_unit_die, cu);
4918
4919 /* Possibly set the default values of LOWPC and HIGHPC from
4920 `DW_AT_ranges'. */
4921 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4922 &best_highpc, cu, pst);
4923 if (has_pc_info == 1 && best_lowpc < best_highpc)
4924 /* Store the contiguous range if it is not empty; it can be empty for
4925 CUs with no code. */
4926 addrmap_set_empty (objfile->psymtabs_addrmap,
4927 best_lowpc + baseaddr,
4928 best_highpc + baseaddr - 1, pst);
4929
4930 /* Check if comp unit has_children.
4931 If so, read the rest of the partial symbols from this comp unit.
4932 If not, there's no more debug_info for this comp unit. */
4933 if (has_children)
4934 {
4935 struct partial_die_info *first_die;
4936 CORE_ADDR lowpc, highpc;
4937
4938 lowpc = ((CORE_ADDR) -1);
4939 highpc = ((CORE_ADDR) 0);
4940
4941 first_die = load_partial_dies (reader, info_ptr, 1);
4942
4943 scan_partial_symbols (first_die, &lowpc, &highpc,
4944 ! has_pc_info, cu);
4945
4946 /* If we didn't find a lowpc, set it to highpc to avoid
4947 complaints from `maint check'. */
4948 if (lowpc == ((CORE_ADDR) -1))
4949 lowpc = highpc;
4950
4951 /* If the compilation unit didn't have an explicit address range,
4952 then use the information extracted from its child dies. */
4953 if (! has_pc_info)
4954 {
4955 best_lowpc = lowpc;
4956 best_highpc = highpc;
4957 }
4958 }
4959 pst->textlow = best_lowpc + baseaddr;
4960 pst->texthigh = best_highpc + baseaddr;
4961
4962 pst->n_global_syms = objfile->global_psymbols.next -
4963 (objfile->global_psymbols.list + pst->globals_offset);
4964 pst->n_static_syms = objfile->static_psymbols.next -
4965 (objfile->static_psymbols.list + pst->statics_offset);
4966 sort_pst_symbols (objfile, pst);
4967
4968 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4969 {
4970 int i;
4971 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4972 struct dwarf2_per_cu_data *iter;
4973
4974 /* Fill in 'dependencies' here; we fill in 'users' in a
4975 post-pass. */
4976 pst->number_of_dependencies = len;
4977 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4978 len * sizeof (struct symtab *));
4979 for (i = 0;
4980 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4981 i, iter);
4982 ++i)
4983 pst->dependencies[i] = iter->v.psymtab;
4984
4985 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4986 }
4987
4988 /* Get the list of files included in the current compilation unit,
4989 and build a psymtab for each of them. */
4990 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4991
4992 if (dwarf2_read_debug)
4993 {
4994 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4995
4996 fprintf_unfiltered (gdb_stdlog,
4997 "Psymtab for %s unit @0x%x: %s - %s"
4998 ", %d global, %d static syms\n",
4999 per_cu->is_debug_types ? "type" : "comp",
5000 per_cu->offset.sect_off,
5001 paddress (gdbarch, pst->textlow),
5002 paddress (gdbarch, pst->texthigh),
5003 pst->n_global_syms, pst->n_static_syms);
5004 }
5005 }
5006
5007 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5008 Process compilation unit THIS_CU for a psymtab. */
5009
5010 static void
5011 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5012 int want_partial_unit)
5013 {
5014 /* If this compilation unit was already read in, free the
5015 cached copy in order to read it in again. This is
5016 necessary because we skipped some symbols when we first
5017 read in the compilation unit (see load_partial_dies).
5018 This problem could be avoided, but the benefit is unclear. */
5019 if (this_cu->cu != NULL)
5020 free_one_cached_comp_unit (this_cu);
5021
5022 gdb_assert (! this_cu->is_debug_types);
5023 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5024 process_psymtab_comp_unit_reader,
5025 &want_partial_unit);
5026
5027 /* Age out any secondary CUs. */
5028 age_cached_comp_units ();
5029 }
5030
5031 static hashval_t
5032 hash_type_unit_group (const void *item)
5033 {
5034 const struct type_unit_group *tu_group = item;
5035
5036 return hash_stmt_list_entry (&tu_group->hash);
5037 }
5038
5039 static int
5040 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5041 {
5042 const struct type_unit_group *lhs = item_lhs;
5043 const struct type_unit_group *rhs = item_rhs;
5044
5045 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5046 }
5047
5048 /* Allocate a hash table for type unit groups. */
5049
5050 static htab_t
5051 allocate_type_unit_groups_table (void)
5052 {
5053 return htab_create_alloc_ex (3,
5054 hash_type_unit_group,
5055 eq_type_unit_group,
5056 NULL,
5057 &dwarf2_per_objfile->objfile->objfile_obstack,
5058 hashtab_obstack_allocate,
5059 dummy_obstack_deallocate);
5060 }
5061
5062 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5063 partial symtabs. We combine several TUs per psymtab to not let the size
5064 of any one psymtab grow too big. */
5065 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5066 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5067
5068 /* Helper routine for get_type_unit_group.
5069 Create the type_unit_group object used to hold one or more TUs. */
5070
5071 static struct type_unit_group *
5072 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5073 {
5074 struct objfile *objfile = dwarf2_per_objfile->objfile;
5075 struct dwarf2_per_cu_data *per_cu;
5076 struct type_unit_group *tu_group;
5077
5078 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5079 struct type_unit_group);
5080 per_cu = &tu_group->per_cu;
5081 per_cu->objfile = objfile;
5082 per_cu->is_debug_types = 1;
5083 per_cu->type_unit_group = tu_group;
5084
5085 if (dwarf2_per_objfile->using_index)
5086 {
5087 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5088 struct dwarf2_per_cu_quick_data);
5089 tu_group->t.first_tu = cu->per_cu;
5090 }
5091 else
5092 {
5093 unsigned int line_offset = line_offset_struct.sect_off;
5094 struct partial_symtab *pst;
5095 char *name;
5096
5097 /* Give the symtab a useful name for debug purposes. */
5098 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5099 name = xstrprintf ("<type_units_%d>",
5100 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5101 else
5102 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5103
5104 pst = create_partial_symtab (per_cu, name);
5105 pst->anonymous = 1;
5106
5107 xfree (name);
5108 }
5109
5110 tu_group->hash.dwo_unit = cu->dwo_unit;
5111 tu_group->hash.line_offset = line_offset_struct;
5112
5113 return tu_group;
5114 }
5115
5116 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5117 STMT_LIST is a DW_AT_stmt_list attribute. */
5118
5119 static struct type_unit_group *
5120 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5121 {
5122 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5123 struct type_unit_group *tu_group;
5124 void **slot;
5125 unsigned int line_offset;
5126 struct type_unit_group type_unit_group_for_lookup;
5127
5128 if (dwarf2_per_objfile->type_unit_groups == NULL)
5129 {
5130 dwarf2_per_objfile->type_unit_groups =
5131 allocate_type_unit_groups_table ();
5132 }
5133
5134 /* Do we need to create a new group, or can we use an existing one? */
5135
5136 if (stmt_list)
5137 {
5138 line_offset = DW_UNSND (stmt_list);
5139 ++tu_stats->nr_symtab_sharers;
5140 }
5141 else
5142 {
5143 /* Ugh, no stmt_list. Rare, but we have to handle it.
5144 We can do various things here like create one group per TU or
5145 spread them over multiple groups to split up the expansion work.
5146 To avoid worst case scenarios (too many groups or too large groups)
5147 we, umm, group them in bunches. */
5148 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5149 | (tu_stats->nr_stmt_less_type_units
5150 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5151 ++tu_stats->nr_stmt_less_type_units;
5152 }
5153
5154 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5155 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5156 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5157 &type_unit_group_for_lookup, INSERT);
5158 if (*slot != NULL)
5159 {
5160 tu_group = *slot;
5161 gdb_assert (tu_group != NULL);
5162 }
5163 else
5164 {
5165 sect_offset line_offset_struct;
5166
5167 line_offset_struct.sect_off = line_offset;
5168 tu_group = create_type_unit_group (cu, line_offset_struct);
5169 *slot = tu_group;
5170 ++tu_stats->nr_symtabs;
5171 }
5172
5173 return tu_group;
5174 }
5175
5176 /* Struct used to sort TUs by their abbreviation table offset. */
5177
5178 struct tu_abbrev_offset
5179 {
5180 struct signatured_type *sig_type;
5181 sect_offset abbrev_offset;
5182 };
5183
5184 /* Helper routine for build_type_unit_groups, passed to qsort. */
5185
5186 static int
5187 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5188 {
5189 const struct tu_abbrev_offset * const *a = ap;
5190 const struct tu_abbrev_offset * const *b = bp;
5191 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5192 unsigned int boff = (*b)->abbrev_offset.sect_off;
5193
5194 return (aoff > boff) - (aoff < boff);
5195 }
5196
5197 /* A helper function to add a type_unit_group to a table. */
5198
5199 static int
5200 add_type_unit_group_to_table (void **slot, void *datum)
5201 {
5202 struct type_unit_group *tu_group = *slot;
5203 struct type_unit_group ***datap = datum;
5204
5205 **datap = tu_group;
5206 ++*datap;
5207
5208 return 1;
5209 }
5210
5211 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5212 each one passing FUNC,DATA.
5213
5214 The efficiency is because we sort TUs by the abbrev table they use and
5215 only read each abbrev table once. In one program there are 200K TUs
5216 sharing 8K abbrev tables.
5217
5218 The main purpose of this function is to support building the
5219 dwarf2_per_objfile->type_unit_groups table.
5220 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5221 can collapse the search space by grouping them by stmt_list.
5222 The savings can be significant, in the same program from above the 200K TUs
5223 share 8K stmt_list tables.
5224
5225 FUNC is expected to call get_type_unit_group, which will create the
5226 struct type_unit_group if necessary and add it to
5227 dwarf2_per_objfile->type_unit_groups. */
5228
5229 static void
5230 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5231 {
5232 struct objfile *objfile = dwarf2_per_objfile->objfile;
5233 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5234 struct cleanup *cleanups;
5235 struct abbrev_table *abbrev_table;
5236 sect_offset abbrev_offset;
5237 struct tu_abbrev_offset *sorted_by_abbrev;
5238 struct type_unit_group **iter;
5239 int i;
5240
5241 /* It's up to the caller to not call us multiple times. */
5242 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5243
5244 if (dwarf2_per_objfile->n_type_units == 0)
5245 return;
5246
5247 /* TUs typically share abbrev tables, and there can be way more TUs than
5248 abbrev tables. Sort by abbrev table to reduce the number of times we
5249 read each abbrev table in.
5250 Alternatives are to punt or to maintain a cache of abbrev tables.
5251 This is simpler and efficient enough for now.
5252
5253 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5254 symtab to use). Typically TUs with the same abbrev offset have the same
5255 stmt_list value too so in practice this should work well.
5256
5257 The basic algorithm here is:
5258
5259 sort TUs by abbrev table
5260 for each TU with same abbrev table:
5261 read abbrev table if first user
5262 read TU top level DIE
5263 [IWBN if DWO skeletons had DW_AT_stmt_list]
5264 call FUNC */
5265
5266 if (dwarf2_read_debug)
5267 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5268
5269 /* Sort in a separate table to maintain the order of all_type_units
5270 for .gdb_index: TU indices directly index all_type_units. */
5271 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5272 dwarf2_per_objfile->n_type_units);
5273 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5274 {
5275 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5276
5277 sorted_by_abbrev[i].sig_type = sig_type;
5278 sorted_by_abbrev[i].abbrev_offset =
5279 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5280 sig_type->per_cu.offset);
5281 }
5282 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5283 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5284 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5285
5286 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5287 called any number of times, so we don't reset tu_stats here. */
5288
5289 abbrev_offset.sect_off = ~(unsigned) 0;
5290 abbrev_table = NULL;
5291 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5292
5293 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5294 {
5295 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5296
5297 /* Switch to the next abbrev table if necessary. */
5298 if (abbrev_table == NULL
5299 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5300 {
5301 if (abbrev_table != NULL)
5302 {
5303 abbrev_table_free (abbrev_table);
5304 /* Reset to NULL in case abbrev_table_read_table throws
5305 an error: abbrev_table_free_cleanup will get called. */
5306 abbrev_table = NULL;
5307 }
5308 abbrev_offset = tu->abbrev_offset;
5309 abbrev_table =
5310 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5311 abbrev_offset);
5312 ++tu_stats->nr_uniq_abbrev_tables;
5313 }
5314
5315 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5316 func, data);
5317 }
5318
5319 /* Create a vector of pointers to primary type units to make it easy to
5320 iterate over them and CUs. See dw2_get_primary_cu. */
5321 dwarf2_per_objfile->n_type_unit_groups =
5322 htab_elements (dwarf2_per_objfile->type_unit_groups);
5323 dwarf2_per_objfile->all_type_unit_groups =
5324 obstack_alloc (&objfile->objfile_obstack,
5325 dwarf2_per_objfile->n_type_unit_groups
5326 * sizeof (struct type_unit_group *));
5327 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5328 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5329 add_type_unit_group_to_table, &iter);
5330 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5331 == dwarf2_per_objfile->n_type_unit_groups);
5332
5333 do_cleanups (cleanups);
5334
5335 if (dwarf2_read_debug)
5336 {
5337 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5338 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5339 dwarf2_per_objfile->n_type_units);
5340 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5341 tu_stats->nr_uniq_abbrev_tables);
5342 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5343 tu_stats->nr_symtabs);
5344 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5345 tu_stats->nr_symtab_sharers);
5346 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5347 tu_stats->nr_stmt_less_type_units);
5348 }
5349 }
5350
5351 /* Reader function for build_type_psymtabs. */
5352
5353 static void
5354 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5355 gdb_byte *info_ptr,
5356 struct die_info *type_unit_die,
5357 int has_children,
5358 void *data)
5359 {
5360 struct objfile *objfile = dwarf2_per_objfile->objfile;
5361 struct dwarf2_cu *cu = reader->cu;
5362 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5363 struct type_unit_group *tu_group;
5364 struct attribute *attr;
5365 struct partial_die_info *first_die;
5366 CORE_ADDR lowpc, highpc;
5367 struct partial_symtab *pst;
5368
5369 gdb_assert (data == NULL);
5370
5371 if (! has_children)
5372 return;
5373
5374 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5375 tu_group = get_type_unit_group (cu, attr);
5376
5377 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5378
5379 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5380 cu->list_in_scope = &file_symbols;
5381 pst = create_partial_symtab (per_cu, "");
5382 pst->anonymous = 1;
5383
5384 first_die = load_partial_dies (reader, info_ptr, 1);
5385
5386 lowpc = (CORE_ADDR) -1;
5387 highpc = (CORE_ADDR) 0;
5388 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5389
5390 pst->n_global_syms = objfile->global_psymbols.next -
5391 (objfile->global_psymbols.list + pst->globals_offset);
5392 pst->n_static_syms = objfile->static_psymbols.next -
5393 (objfile->static_psymbols.list + pst->statics_offset);
5394 sort_pst_symbols (objfile, pst);
5395 }
5396
5397 /* Traversal function for build_type_psymtabs. */
5398
5399 static int
5400 build_type_psymtab_dependencies (void **slot, void *info)
5401 {
5402 struct objfile *objfile = dwarf2_per_objfile->objfile;
5403 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5404 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5405 struct partial_symtab *pst = per_cu->v.psymtab;
5406 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5407 struct dwarf2_per_cu_data *iter;
5408 int i;
5409
5410 gdb_assert (len > 0);
5411
5412 pst->number_of_dependencies = len;
5413 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5414 len * sizeof (struct psymtab *));
5415 for (i = 0;
5416 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5417 ++i)
5418 {
5419 pst->dependencies[i] = iter->v.psymtab;
5420 iter->type_unit_group = tu_group;
5421 }
5422
5423 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5424
5425 return 1;
5426 }
5427
5428 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5429 Build partial symbol tables for the .debug_types comp-units. */
5430
5431 static void
5432 build_type_psymtabs (struct objfile *objfile)
5433 {
5434 if (! create_all_type_units (objfile))
5435 return;
5436
5437 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5438
5439 /* Now that all TUs have been processed we can fill in the dependencies. */
5440 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5441 build_type_psymtab_dependencies, NULL);
5442 }
5443
5444 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5445
5446 static void
5447 psymtabs_addrmap_cleanup (void *o)
5448 {
5449 struct objfile *objfile = o;
5450
5451 objfile->psymtabs_addrmap = NULL;
5452 }
5453
5454 /* Compute the 'user' field for each psymtab in OBJFILE. */
5455
5456 static void
5457 set_partial_user (struct objfile *objfile)
5458 {
5459 int i;
5460
5461 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5462 {
5463 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5464 struct partial_symtab *pst = per_cu->v.psymtab;
5465 int j;
5466
5467 if (pst == NULL)
5468 continue;
5469
5470 for (j = 0; j < pst->number_of_dependencies; ++j)
5471 {
5472 /* Set the 'user' field only if it is not already set. */
5473 if (pst->dependencies[j]->user == NULL)
5474 pst->dependencies[j]->user = pst;
5475 }
5476 }
5477 }
5478
5479 /* Build the partial symbol table by doing a quick pass through the
5480 .debug_info and .debug_abbrev sections. */
5481
5482 static void
5483 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5484 {
5485 struct cleanup *back_to, *addrmap_cleanup;
5486 struct obstack temp_obstack;
5487 int i;
5488
5489 if (dwarf2_read_debug)
5490 {
5491 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5492 objfile->name);
5493 }
5494
5495 dwarf2_per_objfile->reading_partial_symbols = 1;
5496
5497 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5498
5499 /* Any cached compilation units will be linked by the per-objfile
5500 read_in_chain. Make sure to free them when we're done. */
5501 back_to = make_cleanup (free_cached_comp_units, NULL);
5502
5503 build_type_psymtabs (objfile);
5504
5505 create_all_comp_units (objfile);
5506
5507 /* Create a temporary address map on a temporary obstack. We later
5508 copy this to the final obstack. */
5509 obstack_init (&temp_obstack);
5510 make_cleanup_obstack_free (&temp_obstack);
5511 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5512 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5513
5514 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5515 {
5516 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5517
5518 process_psymtab_comp_unit (per_cu, 0);
5519 }
5520
5521 set_partial_user (objfile);
5522
5523 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5524 &objfile->objfile_obstack);
5525 discard_cleanups (addrmap_cleanup);
5526
5527 do_cleanups (back_to);
5528
5529 if (dwarf2_read_debug)
5530 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5531 objfile->name);
5532 }
5533
5534 /* die_reader_func for load_partial_comp_unit. */
5535
5536 static void
5537 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5538 gdb_byte *info_ptr,
5539 struct die_info *comp_unit_die,
5540 int has_children,
5541 void *data)
5542 {
5543 struct dwarf2_cu *cu = reader->cu;
5544
5545 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5546
5547 /* Check if comp unit has_children.
5548 If so, read the rest of the partial symbols from this comp unit.
5549 If not, there's no more debug_info for this comp unit. */
5550 if (has_children)
5551 load_partial_dies (reader, info_ptr, 0);
5552 }
5553
5554 /* Load the partial DIEs for a secondary CU into memory.
5555 This is also used when rereading a primary CU with load_all_dies. */
5556
5557 static void
5558 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5559 {
5560 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5561 load_partial_comp_unit_reader, NULL);
5562 }
5563
5564 static void
5565 read_comp_units_from_section (struct objfile *objfile,
5566 struct dwarf2_section_info *section,
5567 unsigned int is_dwz,
5568 int *n_allocated,
5569 int *n_comp_units,
5570 struct dwarf2_per_cu_data ***all_comp_units)
5571 {
5572 gdb_byte *info_ptr;
5573 bfd *abfd = section->asection->owner;
5574
5575 dwarf2_read_section (objfile, section);
5576
5577 info_ptr = section->buffer;
5578
5579 while (info_ptr < section->buffer + section->size)
5580 {
5581 unsigned int length, initial_length_size;
5582 struct dwarf2_per_cu_data *this_cu;
5583 sect_offset offset;
5584
5585 offset.sect_off = info_ptr - section->buffer;
5586
5587 /* Read just enough information to find out where the next
5588 compilation unit is. */
5589 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5590
5591 /* Save the compilation unit for later lookup. */
5592 this_cu = obstack_alloc (&objfile->objfile_obstack,
5593 sizeof (struct dwarf2_per_cu_data));
5594 memset (this_cu, 0, sizeof (*this_cu));
5595 this_cu->offset = offset;
5596 this_cu->length = length + initial_length_size;
5597 this_cu->is_dwz = is_dwz;
5598 this_cu->objfile = objfile;
5599 this_cu->info_or_types_section = section;
5600
5601 if (*n_comp_units == *n_allocated)
5602 {
5603 *n_allocated *= 2;
5604 *all_comp_units = xrealloc (*all_comp_units,
5605 *n_allocated
5606 * sizeof (struct dwarf2_per_cu_data *));
5607 }
5608 (*all_comp_units)[*n_comp_units] = this_cu;
5609 ++*n_comp_units;
5610
5611 info_ptr = info_ptr + this_cu->length;
5612 }
5613 }
5614
5615 /* Create a list of all compilation units in OBJFILE.
5616 This is only done for -readnow and building partial symtabs. */
5617
5618 static void
5619 create_all_comp_units (struct objfile *objfile)
5620 {
5621 int n_allocated;
5622 int n_comp_units;
5623 struct dwarf2_per_cu_data **all_comp_units;
5624
5625 n_comp_units = 0;
5626 n_allocated = 10;
5627 all_comp_units = xmalloc (n_allocated
5628 * sizeof (struct dwarf2_per_cu_data *));
5629
5630 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5631 &n_allocated, &n_comp_units, &all_comp_units);
5632
5633 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5634 {
5635 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5636
5637 read_comp_units_from_section (objfile, &dwz->info, 1,
5638 &n_allocated, &n_comp_units,
5639 &all_comp_units);
5640 }
5641
5642 dwarf2_per_objfile->all_comp_units
5643 = obstack_alloc (&objfile->objfile_obstack,
5644 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5645 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5646 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5647 xfree (all_comp_units);
5648 dwarf2_per_objfile->n_comp_units = n_comp_units;
5649 }
5650
5651 /* Process all loaded DIEs for compilation unit CU, starting at
5652 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5653 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5654 DW_AT_ranges). If NEED_PC is set, then this function will set
5655 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5656 and record the covered ranges in the addrmap. */
5657
5658 static void
5659 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5660 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5661 {
5662 struct partial_die_info *pdi;
5663
5664 /* Now, march along the PDI's, descending into ones which have
5665 interesting children but skipping the children of the other ones,
5666 until we reach the end of the compilation unit. */
5667
5668 pdi = first_die;
5669
5670 while (pdi != NULL)
5671 {
5672 fixup_partial_die (pdi, cu);
5673
5674 /* Anonymous namespaces or modules have no name but have interesting
5675 children, so we need to look at them. Ditto for anonymous
5676 enums. */
5677
5678 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5679 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5680 || pdi->tag == DW_TAG_imported_unit)
5681 {
5682 switch (pdi->tag)
5683 {
5684 case DW_TAG_subprogram:
5685 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5686 break;
5687 case DW_TAG_constant:
5688 case DW_TAG_variable:
5689 case DW_TAG_typedef:
5690 case DW_TAG_union_type:
5691 if (!pdi->is_declaration)
5692 {
5693 add_partial_symbol (pdi, cu);
5694 }
5695 break;
5696 case DW_TAG_class_type:
5697 case DW_TAG_interface_type:
5698 case DW_TAG_structure_type:
5699 if (!pdi->is_declaration)
5700 {
5701 add_partial_symbol (pdi, cu);
5702 }
5703 break;
5704 case DW_TAG_enumeration_type:
5705 if (!pdi->is_declaration)
5706 add_partial_enumeration (pdi, cu);
5707 break;
5708 case DW_TAG_base_type:
5709 case DW_TAG_subrange_type:
5710 /* File scope base type definitions are added to the partial
5711 symbol table. */
5712 add_partial_symbol (pdi, cu);
5713 break;
5714 case DW_TAG_namespace:
5715 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5716 break;
5717 case DW_TAG_module:
5718 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5719 break;
5720 case DW_TAG_imported_unit:
5721 {
5722 struct dwarf2_per_cu_data *per_cu;
5723
5724 /* For now we don't handle imported units in type units. */
5725 if (cu->per_cu->is_debug_types)
5726 {
5727 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5728 " supported in type units [in module %s]"),
5729 cu->objfile->name);
5730 }
5731
5732 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5733 pdi->is_dwz,
5734 cu->objfile);
5735
5736 /* Go read the partial unit, if needed. */
5737 if (per_cu->v.psymtab == NULL)
5738 process_psymtab_comp_unit (per_cu, 1);
5739
5740 VEC_safe_push (dwarf2_per_cu_ptr,
5741 cu->per_cu->imported_symtabs, per_cu);
5742 }
5743 break;
5744 default:
5745 break;
5746 }
5747 }
5748
5749 /* If the die has a sibling, skip to the sibling. */
5750
5751 pdi = pdi->die_sibling;
5752 }
5753 }
5754
5755 /* Functions used to compute the fully scoped name of a partial DIE.
5756
5757 Normally, this is simple. For C++, the parent DIE's fully scoped
5758 name is concatenated with "::" and the partial DIE's name. For
5759 Java, the same thing occurs except that "." is used instead of "::".
5760 Enumerators are an exception; they use the scope of their parent
5761 enumeration type, i.e. the name of the enumeration type is not
5762 prepended to the enumerator.
5763
5764 There are two complexities. One is DW_AT_specification; in this
5765 case "parent" means the parent of the target of the specification,
5766 instead of the direct parent of the DIE. The other is compilers
5767 which do not emit DW_TAG_namespace; in this case we try to guess
5768 the fully qualified name of structure types from their members'
5769 linkage names. This must be done using the DIE's children rather
5770 than the children of any DW_AT_specification target. We only need
5771 to do this for structures at the top level, i.e. if the target of
5772 any DW_AT_specification (if any; otherwise the DIE itself) does not
5773 have a parent. */
5774
5775 /* Compute the scope prefix associated with PDI's parent, in
5776 compilation unit CU. The result will be allocated on CU's
5777 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5778 field. NULL is returned if no prefix is necessary. */
5779 static const char *
5780 partial_die_parent_scope (struct partial_die_info *pdi,
5781 struct dwarf2_cu *cu)
5782 {
5783 const char *grandparent_scope;
5784 struct partial_die_info *parent, *real_pdi;
5785
5786 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5787 then this means the parent of the specification DIE. */
5788
5789 real_pdi = pdi;
5790 while (real_pdi->has_specification)
5791 real_pdi = find_partial_die (real_pdi->spec_offset,
5792 real_pdi->spec_is_dwz, cu);
5793
5794 parent = real_pdi->die_parent;
5795 if (parent == NULL)
5796 return NULL;
5797
5798 if (parent->scope_set)
5799 return parent->scope;
5800
5801 fixup_partial_die (parent, cu);
5802
5803 grandparent_scope = partial_die_parent_scope (parent, cu);
5804
5805 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5806 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5807 Work around this problem here. */
5808 if (cu->language == language_cplus
5809 && parent->tag == DW_TAG_namespace
5810 && strcmp (parent->name, "::") == 0
5811 && grandparent_scope == NULL)
5812 {
5813 parent->scope = NULL;
5814 parent->scope_set = 1;
5815 return NULL;
5816 }
5817
5818 if (pdi->tag == DW_TAG_enumerator)
5819 /* Enumerators should not get the name of the enumeration as a prefix. */
5820 parent->scope = grandparent_scope;
5821 else if (parent->tag == DW_TAG_namespace
5822 || parent->tag == DW_TAG_module
5823 || parent->tag == DW_TAG_structure_type
5824 || parent->tag == DW_TAG_class_type
5825 || parent->tag == DW_TAG_interface_type
5826 || parent->tag == DW_TAG_union_type
5827 || parent->tag == DW_TAG_enumeration_type)
5828 {
5829 if (grandparent_scope == NULL)
5830 parent->scope = parent->name;
5831 else
5832 parent->scope = typename_concat (&cu->comp_unit_obstack,
5833 grandparent_scope,
5834 parent->name, 0, cu);
5835 }
5836 else
5837 {
5838 /* FIXME drow/2004-04-01: What should we be doing with
5839 function-local names? For partial symbols, we should probably be
5840 ignoring them. */
5841 complaint (&symfile_complaints,
5842 _("unhandled containing DIE tag %d for DIE at %d"),
5843 parent->tag, pdi->offset.sect_off);
5844 parent->scope = grandparent_scope;
5845 }
5846
5847 parent->scope_set = 1;
5848 return parent->scope;
5849 }
5850
5851 /* Return the fully scoped name associated with PDI, from compilation unit
5852 CU. The result will be allocated with malloc. */
5853
5854 static char *
5855 partial_die_full_name (struct partial_die_info *pdi,
5856 struct dwarf2_cu *cu)
5857 {
5858 const char *parent_scope;
5859
5860 /* If this is a template instantiation, we can not work out the
5861 template arguments from partial DIEs. So, unfortunately, we have
5862 to go through the full DIEs. At least any work we do building
5863 types here will be reused if full symbols are loaded later. */
5864 if (pdi->has_template_arguments)
5865 {
5866 fixup_partial_die (pdi, cu);
5867
5868 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5869 {
5870 struct die_info *die;
5871 struct attribute attr;
5872 struct dwarf2_cu *ref_cu = cu;
5873
5874 /* DW_FORM_ref_addr is using section offset. */
5875 attr.name = 0;
5876 attr.form = DW_FORM_ref_addr;
5877 attr.u.unsnd = pdi->offset.sect_off;
5878 die = follow_die_ref (NULL, &attr, &ref_cu);
5879
5880 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5881 }
5882 }
5883
5884 parent_scope = partial_die_parent_scope (pdi, cu);
5885 if (parent_scope == NULL)
5886 return NULL;
5887 else
5888 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5889 }
5890
5891 static void
5892 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5893 {
5894 struct objfile *objfile = cu->objfile;
5895 CORE_ADDR addr = 0;
5896 const char *actual_name = NULL;
5897 CORE_ADDR baseaddr;
5898 char *built_actual_name;
5899
5900 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5901
5902 built_actual_name = partial_die_full_name (pdi, cu);
5903 if (built_actual_name != NULL)
5904 actual_name = built_actual_name;
5905
5906 if (actual_name == NULL)
5907 actual_name = pdi->name;
5908
5909 switch (pdi->tag)
5910 {
5911 case DW_TAG_subprogram:
5912 if (pdi->is_external || cu->language == language_ada)
5913 {
5914 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5915 of the global scope. But in Ada, we want to be able to access
5916 nested procedures globally. So all Ada subprograms are stored
5917 in the global scope. */
5918 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5919 mst_text, objfile); */
5920 add_psymbol_to_list (actual_name, strlen (actual_name),
5921 built_actual_name != NULL,
5922 VAR_DOMAIN, LOC_BLOCK,
5923 &objfile->global_psymbols,
5924 0, pdi->lowpc + baseaddr,
5925 cu->language, objfile);
5926 }
5927 else
5928 {
5929 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5930 mst_file_text, objfile); */
5931 add_psymbol_to_list (actual_name, strlen (actual_name),
5932 built_actual_name != NULL,
5933 VAR_DOMAIN, LOC_BLOCK,
5934 &objfile->static_psymbols,
5935 0, pdi->lowpc + baseaddr,
5936 cu->language, objfile);
5937 }
5938 break;
5939 case DW_TAG_constant:
5940 {
5941 struct psymbol_allocation_list *list;
5942
5943 if (pdi->is_external)
5944 list = &objfile->global_psymbols;
5945 else
5946 list = &objfile->static_psymbols;
5947 add_psymbol_to_list (actual_name, strlen (actual_name),
5948 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
5949 list, 0, 0, cu->language, objfile);
5950 }
5951 break;
5952 case DW_TAG_variable:
5953 if (pdi->d.locdesc)
5954 addr = decode_locdesc (pdi->d.locdesc, cu);
5955
5956 if (pdi->d.locdesc
5957 && addr == 0
5958 && !dwarf2_per_objfile->has_section_at_zero)
5959 {
5960 /* A global or static variable may also have been stripped
5961 out by the linker if unused, in which case its address
5962 will be nullified; do not add such variables into partial
5963 symbol table then. */
5964 }
5965 else if (pdi->is_external)
5966 {
5967 /* Global Variable.
5968 Don't enter into the minimal symbol tables as there is
5969 a minimal symbol table entry from the ELF symbols already.
5970 Enter into partial symbol table if it has a location
5971 descriptor or a type.
5972 If the location descriptor is missing, new_symbol will create
5973 a LOC_UNRESOLVED symbol, the address of the variable will then
5974 be determined from the minimal symbol table whenever the variable
5975 is referenced.
5976 The address for the partial symbol table entry is not
5977 used by GDB, but it comes in handy for debugging partial symbol
5978 table building. */
5979
5980 if (pdi->d.locdesc || pdi->has_type)
5981 add_psymbol_to_list (actual_name, strlen (actual_name),
5982 built_actual_name != NULL,
5983 VAR_DOMAIN, LOC_STATIC,
5984 &objfile->global_psymbols,
5985 0, addr + baseaddr,
5986 cu->language, objfile);
5987 }
5988 else
5989 {
5990 /* Static Variable. Skip symbols without location descriptors. */
5991 if (pdi->d.locdesc == NULL)
5992 {
5993 xfree (built_actual_name);
5994 return;
5995 }
5996 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5997 mst_file_data, objfile); */
5998 add_psymbol_to_list (actual_name, strlen (actual_name),
5999 built_actual_name != NULL,
6000 VAR_DOMAIN, LOC_STATIC,
6001 &objfile->static_psymbols,
6002 0, addr + baseaddr,
6003 cu->language, objfile);
6004 }
6005 break;
6006 case DW_TAG_typedef:
6007 case DW_TAG_base_type:
6008 case DW_TAG_subrange_type:
6009 add_psymbol_to_list (actual_name, strlen (actual_name),
6010 built_actual_name != NULL,
6011 VAR_DOMAIN, LOC_TYPEDEF,
6012 &objfile->static_psymbols,
6013 0, (CORE_ADDR) 0, cu->language, objfile);
6014 break;
6015 case DW_TAG_namespace:
6016 add_psymbol_to_list (actual_name, strlen (actual_name),
6017 built_actual_name != NULL,
6018 VAR_DOMAIN, LOC_TYPEDEF,
6019 &objfile->global_psymbols,
6020 0, (CORE_ADDR) 0, cu->language, objfile);
6021 break;
6022 case DW_TAG_class_type:
6023 case DW_TAG_interface_type:
6024 case DW_TAG_structure_type:
6025 case DW_TAG_union_type:
6026 case DW_TAG_enumeration_type:
6027 /* Skip external references. The DWARF standard says in the section
6028 about "Structure, Union, and Class Type Entries": "An incomplete
6029 structure, union or class type is represented by a structure,
6030 union or class entry that does not have a byte size attribute
6031 and that has a DW_AT_declaration attribute." */
6032 if (!pdi->has_byte_size && pdi->is_declaration)
6033 {
6034 xfree (built_actual_name);
6035 return;
6036 }
6037
6038 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6039 static vs. global. */
6040 add_psymbol_to_list (actual_name, strlen (actual_name),
6041 built_actual_name != NULL,
6042 STRUCT_DOMAIN, LOC_TYPEDEF,
6043 (cu->language == language_cplus
6044 || cu->language == language_java)
6045 ? &objfile->global_psymbols
6046 : &objfile->static_psymbols,
6047 0, (CORE_ADDR) 0, cu->language, objfile);
6048
6049 break;
6050 case DW_TAG_enumerator:
6051 add_psymbol_to_list (actual_name, strlen (actual_name),
6052 built_actual_name != NULL,
6053 VAR_DOMAIN, LOC_CONST,
6054 (cu->language == language_cplus
6055 || cu->language == language_java)
6056 ? &objfile->global_psymbols
6057 : &objfile->static_psymbols,
6058 0, (CORE_ADDR) 0, cu->language, objfile);
6059 break;
6060 default:
6061 break;
6062 }
6063
6064 xfree (built_actual_name);
6065 }
6066
6067 /* Read a partial die corresponding to a namespace; also, add a symbol
6068 corresponding to that namespace to the symbol table. NAMESPACE is
6069 the name of the enclosing namespace. */
6070
6071 static void
6072 add_partial_namespace (struct partial_die_info *pdi,
6073 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6074 int need_pc, struct dwarf2_cu *cu)
6075 {
6076 /* Add a symbol for the namespace. */
6077
6078 add_partial_symbol (pdi, cu);
6079
6080 /* Now scan partial symbols in that namespace. */
6081
6082 if (pdi->has_children)
6083 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6084 }
6085
6086 /* Read a partial die corresponding to a Fortran module. */
6087
6088 static void
6089 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6090 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6091 {
6092 /* Now scan partial symbols in that module. */
6093
6094 if (pdi->has_children)
6095 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6096 }
6097
6098 /* Read a partial die corresponding to a subprogram and create a partial
6099 symbol for that subprogram. When the CU language allows it, this
6100 routine also defines a partial symbol for each nested subprogram
6101 that this subprogram contains.
6102
6103 DIE my also be a lexical block, in which case we simply search
6104 recursively for suprograms defined inside that lexical block.
6105 Again, this is only performed when the CU language allows this
6106 type of definitions. */
6107
6108 static void
6109 add_partial_subprogram (struct partial_die_info *pdi,
6110 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6111 int need_pc, struct dwarf2_cu *cu)
6112 {
6113 if (pdi->tag == DW_TAG_subprogram)
6114 {
6115 if (pdi->has_pc_info)
6116 {
6117 if (pdi->lowpc < *lowpc)
6118 *lowpc = pdi->lowpc;
6119 if (pdi->highpc > *highpc)
6120 *highpc = pdi->highpc;
6121 if (need_pc)
6122 {
6123 CORE_ADDR baseaddr;
6124 struct objfile *objfile = cu->objfile;
6125
6126 baseaddr = ANOFFSET (objfile->section_offsets,
6127 SECT_OFF_TEXT (objfile));
6128 addrmap_set_empty (objfile->psymtabs_addrmap,
6129 pdi->lowpc + baseaddr,
6130 pdi->highpc - 1 + baseaddr,
6131 cu->per_cu->v.psymtab);
6132 }
6133 }
6134
6135 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6136 {
6137 if (!pdi->is_declaration)
6138 /* Ignore subprogram DIEs that do not have a name, they are
6139 illegal. Do not emit a complaint at this point, we will
6140 do so when we convert this psymtab into a symtab. */
6141 if (pdi->name)
6142 add_partial_symbol (pdi, cu);
6143 }
6144 }
6145
6146 if (! pdi->has_children)
6147 return;
6148
6149 if (cu->language == language_ada)
6150 {
6151 pdi = pdi->die_child;
6152 while (pdi != NULL)
6153 {
6154 fixup_partial_die (pdi, cu);
6155 if (pdi->tag == DW_TAG_subprogram
6156 || pdi->tag == DW_TAG_lexical_block)
6157 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6158 pdi = pdi->die_sibling;
6159 }
6160 }
6161 }
6162
6163 /* Read a partial die corresponding to an enumeration type. */
6164
6165 static void
6166 add_partial_enumeration (struct partial_die_info *enum_pdi,
6167 struct dwarf2_cu *cu)
6168 {
6169 struct partial_die_info *pdi;
6170
6171 if (enum_pdi->name != NULL)
6172 add_partial_symbol (enum_pdi, cu);
6173
6174 pdi = enum_pdi->die_child;
6175 while (pdi)
6176 {
6177 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6178 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6179 else
6180 add_partial_symbol (pdi, cu);
6181 pdi = pdi->die_sibling;
6182 }
6183 }
6184
6185 /* Return the initial uleb128 in the die at INFO_PTR. */
6186
6187 static unsigned int
6188 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6189 {
6190 unsigned int bytes_read;
6191
6192 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6193 }
6194
6195 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6196 Return the corresponding abbrev, or NULL if the number is zero (indicating
6197 an empty DIE). In either case *BYTES_READ will be set to the length of
6198 the initial number. */
6199
6200 static struct abbrev_info *
6201 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6202 struct dwarf2_cu *cu)
6203 {
6204 bfd *abfd = cu->objfile->obfd;
6205 unsigned int abbrev_number;
6206 struct abbrev_info *abbrev;
6207
6208 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6209
6210 if (abbrev_number == 0)
6211 return NULL;
6212
6213 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6214 if (!abbrev)
6215 {
6216 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6217 abbrev_number, bfd_get_filename (abfd));
6218 }
6219
6220 return abbrev;
6221 }
6222
6223 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6224 Returns a pointer to the end of a series of DIEs, terminated by an empty
6225 DIE. Any children of the skipped DIEs will also be skipped. */
6226
6227 static gdb_byte *
6228 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6229 {
6230 struct dwarf2_cu *cu = reader->cu;
6231 struct abbrev_info *abbrev;
6232 unsigned int bytes_read;
6233
6234 while (1)
6235 {
6236 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6237 if (abbrev == NULL)
6238 return info_ptr + bytes_read;
6239 else
6240 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6241 }
6242 }
6243
6244 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6245 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6246 abbrev corresponding to that skipped uleb128 should be passed in
6247 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6248 children. */
6249
6250 static gdb_byte *
6251 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6252 struct abbrev_info *abbrev)
6253 {
6254 unsigned int bytes_read;
6255 struct attribute attr;
6256 bfd *abfd = reader->abfd;
6257 struct dwarf2_cu *cu = reader->cu;
6258 gdb_byte *buffer = reader->buffer;
6259 const gdb_byte *buffer_end = reader->buffer_end;
6260 gdb_byte *start_info_ptr = info_ptr;
6261 unsigned int form, i;
6262
6263 for (i = 0; i < abbrev->num_attrs; i++)
6264 {
6265 /* The only abbrev we care about is DW_AT_sibling. */
6266 if (abbrev->attrs[i].name == DW_AT_sibling)
6267 {
6268 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6269 if (attr.form == DW_FORM_ref_addr)
6270 complaint (&symfile_complaints,
6271 _("ignoring absolute DW_AT_sibling"));
6272 else
6273 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6274 }
6275
6276 /* If it isn't DW_AT_sibling, skip this attribute. */
6277 form = abbrev->attrs[i].form;
6278 skip_attribute:
6279 switch (form)
6280 {
6281 case DW_FORM_ref_addr:
6282 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6283 and later it is offset sized. */
6284 if (cu->header.version == 2)
6285 info_ptr += cu->header.addr_size;
6286 else
6287 info_ptr += cu->header.offset_size;
6288 break;
6289 case DW_FORM_GNU_ref_alt:
6290 info_ptr += cu->header.offset_size;
6291 break;
6292 case DW_FORM_addr:
6293 info_ptr += cu->header.addr_size;
6294 break;
6295 case DW_FORM_data1:
6296 case DW_FORM_ref1:
6297 case DW_FORM_flag:
6298 info_ptr += 1;
6299 break;
6300 case DW_FORM_flag_present:
6301 break;
6302 case DW_FORM_data2:
6303 case DW_FORM_ref2:
6304 info_ptr += 2;
6305 break;
6306 case DW_FORM_data4:
6307 case DW_FORM_ref4:
6308 info_ptr += 4;
6309 break;
6310 case DW_FORM_data8:
6311 case DW_FORM_ref8:
6312 case DW_FORM_ref_sig8:
6313 info_ptr += 8;
6314 break;
6315 case DW_FORM_string:
6316 read_direct_string (abfd, info_ptr, &bytes_read);
6317 info_ptr += bytes_read;
6318 break;
6319 case DW_FORM_sec_offset:
6320 case DW_FORM_strp:
6321 case DW_FORM_GNU_strp_alt:
6322 info_ptr += cu->header.offset_size;
6323 break;
6324 case DW_FORM_exprloc:
6325 case DW_FORM_block:
6326 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6327 info_ptr += bytes_read;
6328 break;
6329 case DW_FORM_block1:
6330 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6331 break;
6332 case DW_FORM_block2:
6333 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6334 break;
6335 case DW_FORM_block4:
6336 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6337 break;
6338 case DW_FORM_sdata:
6339 case DW_FORM_udata:
6340 case DW_FORM_ref_udata:
6341 case DW_FORM_GNU_addr_index:
6342 case DW_FORM_GNU_str_index:
6343 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6344 break;
6345 case DW_FORM_indirect:
6346 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6347 info_ptr += bytes_read;
6348 /* We need to continue parsing from here, so just go back to
6349 the top. */
6350 goto skip_attribute;
6351
6352 default:
6353 error (_("Dwarf Error: Cannot handle %s "
6354 "in DWARF reader [in module %s]"),
6355 dwarf_form_name (form),
6356 bfd_get_filename (abfd));
6357 }
6358 }
6359
6360 if (abbrev->has_children)
6361 return skip_children (reader, info_ptr);
6362 else
6363 return info_ptr;
6364 }
6365
6366 /* Locate ORIG_PDI's sibling.
6367 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6368
6369 static gdb_byte *
6370 locate_pdi_sibling (const struct die_reader_specs *reader,
6371 struct partial_die_info *orig_pdi,
6372 gdb_byte *info_ptr)
6373 {
6374 /* Do we know the sibling already? */
6375
6376 if (orig_pdi->sibling)
6377 return orig_pdi->sibling;
6378
6379 /* Are there any children to deal with? */
6380
6381 if (!orig_pdi->has_children)
6382 return info_ptr;
6383
6384 /* Skip the children the long way. */
6385
6386 return skip_children (reader, info_ptr);
6387 }
6388
6389 /* Expand this partial symbol table into a full symbol table. SELF is
6390 not NULL. */
6391
6392 static void
6393 dwarf2_read_symtab (struct partial_symtab *self,
6394 struct objfile *objfile)
6395 {
6396 if (self->readin)
6397 {
6398 warning (_("bug: psymtab for %s is already read in."),
6399 self->filename);
6400 }
6401 else
6402 {
6403 if (info_verbose)
6404 {
6405 printf_filtered (_("Reading in symbols for %s..."),
6406 self->filename);
6407 gdb_flush (gdb_stdout);
6408 }
6409
6410 /* Restore our global data. */
6411 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6412
6413 /* If this psymtab is constructed from a debug-only objfile, the
6414 has_section_at_zero flag will not necessarily be correct. We
6415 can get the correct value for this flag by looking at the data
6416 associated with the (presumably stripped) associated objfile. */
6417 if (objfile->separate_debug_objfile_backlink)
6418 {
6419 struct dwarf2_per_objfile *dpo_backlink
6420 = objfile_data (objfile->separate_debug_objfile_backlink,
6421 dwarf2_objfile_data_key);
6422
6423 dwarf2_per_objfile->has_section_at_zero
6424 = dpo_backlink->has_section_at_zero;
6425 }
6426
6427 dwarf2_per_objfile->reading_partial_symbols = 0;
6428
6429 psymtab_to_symtab_1 (self);
6430
6431 /* Finish up the debug error message. */
6432 if (info_verbose)
6433 printf_filtered (_("done.\n"));
6434 }
6435
6436 process_cu_includes ();
6437 }
6438 \f
6439 /* Reading in full CUs. */
6440
6441 /* Add PER_CU to the queue. */
6442
6443 static void
6444 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6445 enum language pretend_language)
6446 {
6447 struct dwarf2_queue_item *item;
6448
6449 per_cu->queued = 1;
6450 item = xmalloc (sizeof (*item));
6451 item->per_cu = per_cu;
6452 item->pretend_language = pretend_language;
6453 item->next = NULL;
6454
6455 if (dwarf2_queue == NULL)
6456 dwarf2_queue = item;
6457 else
6458 dwarf2_queue_tail->next = item;
6459
6460 dwarf2_queue_tail = item;
6461 }
6462
6463 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6464 unit and add it to our queue.
6465 The result is non-zero if PER_CU was queued, otherwise the result is zero
6466 meaning either PER_CU is already queued or it is already loaded. */
6467
6468 static int
6469 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6470 struct dwarf2_per_cu_data *per_cu,
6471 enum language pretend_language)
6472 {
6473 /* We may arrive here during partial symbol reading, if we need full
6474 DIEs to process an unusual case (e.g. template arguments). Do
6475 not queue PER_CU, just tell our caller to load its DIEs. */
6476 if (dwarf2_per_objfile->reading_partial_symbols)
6477 {
6478 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6479 return 1;
6480 return 0;
6481 }
6482
6483 /* Mark the dependence relation so that we don't flush PER_CU
6484 too early. */
6485 dwarf2_add_dependence (this_cu, per_cu);
6486
6487 /* If it's already on the queue, we have nothing to do. */
6488 if (per_cu->queued)
6489 return 0;
6490
6491 /* If the compilation unit is already loaded, just mark it as
6492 used. */
6493 if (per_cu->cu != NULL)
6494 {
6495 per_cu->cu->last_used = 0;
6496 return 0;
6497 }
6498
6499 /* Add it to the queue. */
6500 queue_comp_unit (per_cu, pretend_language);
6501
6502 return 1;
6503 }
6504
6505 /* Process the queue. */
6506
6507 static void
6508 process_queue (void)
6509 {
6510 struct dwarf2_queue_item *item, *next_item;
6511
6512 if (dwarf2_read_debug)
6513 {
6514 fprintf_unfiltered (gdb_stdlog,
6515 "Expanding one or more symtabs of objfile %s ...\n",
6516 dwarf2_per_objfile->objfile->name);
6517 }
6518
6519 /* The queue starts out with one item, but following a DIE reference
6520 may load a new CU, adding it to the end of the queue. */
6521 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6522 {
6523 if (dwarf2_per_objfile->using_index
6524 ? !item->per_cu->v.quick->symtab
6525 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6526 {
6527 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6528
6529 if (dwarf2_read_debug)
6530 {
6531 fprintf_unfiltered (gdb_stdlog,
6532 "Expanding symtab of %s at offset 0x%x\n",
6533 per_cu->is_debug_types ? "TU" : "CU",
6534 per_cu->offset.sect_off);
6535 }
6536
6537 if (per_cu->is_debug_types)
6538 process_full_type_unit (per_cu, item->pretend_language);
6539 else
6540 process_full_comp_unit (per_cu, item->pretend_language);
6541
6542 if (dwarf2_read_debug)
6543 {
6544 fprintf_unfiltered (gdb_stdlog,
6545 "Done expanding %s at offset 0x%x\n",
6546 per_cu->is_debug_types ? "TU" : "CU",
6547 per_cu->offset.sect_off);
6548 }
6549 }
6550
6551 item->per_cu->queued = 0;
6552 next_item = item->next;
6553 xfree (item);
6554 }
6555
6556 dwarf2_queue_tail = NULL;
6557
6558 if (dwarf2_read_debug)
6559 {
6560 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6561 dwarf2_per_objfile->objfile->name);
6562 }
6563 }
6564
6565 /* Free all allocated queue entries. This function only releases anything if
6566 an error was thrown; if the queue was processed then it would have been
6567 freed as we went along. */
6568
6569 static void
6570 dwarf2_release_queue (void *dummy)
6571 {
6572 struct dwarf2_queue_item *item, *last;
6573
6574 item = dwarf2_queue;
6575 while (item)
6576 {
6577 /* Anything still marked queued is likely to be in an
6578 inconsistent state, so discard it. */
6579 if (item->per_cu->queued)
6580 {
6581 if (item->per_cu->cu != NULL)
6582 free_one_cached_comp_unit (item->per_cu);
6583 item->per_cu->queued = 0;
6584 }
6585
6586 last = item;
6587 item = item->next;
6588 xfree (last);
6589 }
6590
6591 dwarf2_queue = dwarf2_queue_tail = NULL;
6592 }
6593
6594 /* Read in full symbols for PST, and anything it depends on. */
6595
6596 static void
6597 psymtab_to_symtab_1 (struct partial_symtab *pst)
6598 {
6599 struct dwarf2_per_cu_data *per_cu;
6600 int i;
6601
6602 if (pst->readin)
6603 return;
6604
6605 for (i = 0; i < pst->number_of_dependencies; i++)
6606 if (!pst->dependencies[i]->readin
6607 && pst->dependencies[i]->user == NULL)
6608 {
6609 /* Inform about additional files that need to be read in. */
6610 if (info_verbose)
6611 {
6612 /* FIXME: i18n: Need to make this a single string. */
6613 fputs_filtered (" ", gdb_stdout);
6614 wrap_here ("");
6615 fputs_filtered ("and ", gdb_stdout);
6616 wrap_here ("");
6617 printf_filtered ("%s...", pst->dependencies[i]->filename);
6618 wrap_here (""); /* Flush output. */
6619 gdb_flush (gdb_stdout);
6620 }
6621 psymtab_to_symtab_1 (pst->dependencies[i]);
6622 }
6623
6624 per_cu = pst->read_symtab_private;
6625
6626 if (per_cu == NULL)
6627 {
6628 /* It's an include file, no symbols to read for it.
6629 Everything is in the parent symtab. */
6630 pst->readin = 1;
6631 return;
6632 }
6633
6634 dw2_do_instantiate_symtab (per_cu);
6635 }
6636
6637 /* Trivial hash function for die_info: the hash value of a DIE
6638 is its offset in .debug_info for this objfile. */
6639
6640 static hashval_t
6641 die_hash (const void *item)
6642 {
6643 const struct die_info *die = item;
6644
6645 return die->offset.sect_off;
6646 }
6647
6648 /* Trivial comparison function for die_info structures: two DIEs
6649 are equal if they have the same offset. */
6650
6651 static int
6652 die_eq (const void *item_lhs, const void *item_rhs)
6653 {
6654 const struct die_info *die_lhs = item_lhs;
6655 const struct die_info *die_rhs = item_rhs;
6656
6657 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6658 }
6659
6660 /* die_reader_func for load_full_comp_unit.
6661 This is identical to read_signatured_type_reader,
6662 but is kept separate for now. */
6663
6664 static void
6665 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6666 gdb_byte *info_ptr,
6667 struct die_info *comp_unit_die,
6668 int has_children,
6669 void *data)
6670 {
6671 struct dwarf2_cu *cu = reader->cu;
6672 enum language *language_ptr = data;
6673
6674 gdb_assert (cu->die_hash == NULL);
6675 cu->die_hash =
6676 htab_create_alloc_ex (cu->header.length / 12,
6677 die_hash,
6678 die_eq,
6679 NULL,
6680 &cu->comp_unit_obstack,
6681 hashtab_obstack_allocate,
6682 dummy_obstack_deallocate);
6683
6684 if (has_children)
6685 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6686 &info_ptr, comp_unit_die);
6687 cu->dies = comp_unit_die;
6688 /* comp_unit_die is not stored in die_hash, no need. */
6689
6690 /* We try not to read any attributes in this function, because not
6691 all CUs needed for references have been loaded yet, and symbol
6692 table processing isn't initialized. But we have to set the CU language,
6693 or we won't be able to build types correctly.
6694 Similarly, if we do not read the producer, we can not apply
6695 producer-specific interpretation. */
6696 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6697 }
6698
6699 /* Load the DIEs associated with PER_CU into memory. */
6700
6701 static void
6702 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6703 enum language pretend_language)
6704 {
6705 gdb_assert (! this_cu->is_debug_types);
6706
6707 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6708 load_full_comp_unit_reader, &pretend_language);
6709 }
6710
6711 /* Add a DIE to the delayed physname list. */
6712
6713 static void
6714 add_to_method_list (struct type *type, int fnfield_index, int index,
6715 const char *name, struct die_info *die,
6716 struct dwarf2_cu *cu)
6717 {
6718 struct delayed_method_info mi;
6719 mi.type = type;
6720 mi.fnfield_index = fnfield_index;
6721 mi.index = index;
6722 mi.name = name;
6723 mi.die = die;
6724 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6725 }
6726
6727 /* A cleanup for freeing the delayed method list. */
6728
6729 static void
6730 free_delayed_list (void *ptr)
6731 {
6732 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6733 if (cu->method_list != NULL)
6734 {
6735 VEC_free (delayed_method_info, cu->method_list);
6736 cu->method_list = NULL;
6737 }
6738 }
6739
6740 /* Compute the physnames of any methods on the CU's method list.
6741
6742 The computation of method physnames is delayed in order to avoid the
6743 (bad) condition that one of the method's formal parameters is of an as yet
6744 incomplete type. */
6745
6746 static void
6747 compute_delayed_physnames (struct dwarf2_cu *cu)
6748 {
6749 int i;
6750 struct delayed_method_info *mi;
6751 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6752 {
6753 const char *physname;
6754 struct fn_fieldlist *fn_flp
6755 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6756 physname = dwarf2_physname (mi->name, mi->die, cu);
6757 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6758 }
6759 }
6760
6761 /* Go objects should be embedded in a DW_TAG_module DIE,
6762 and it's not clear if/how imported objects will appear.
6763 To keep Go support simple until that's worked out,
6764 go back through what we've read and create something usable.
6765 We could do this while processing each DIE, and feels kinda cleaner,
6766 but that way is more invasive.
6767 This is to, for example, allow the user to type "p var" or "b main"
6768 without having to specify the package name, and allow lookups
6769 of module.object to work in contexts that use the expression
6770 parser. */
6771
6772 static void
6773 fixup_go_packaging (struct dwarf2_cu *cu)
6774 {
6775 char *package_name = NULL;
6776 struct pending *list;
6777 int i;
6778
6779 for (list = global_symbols; list != NULL; list = list->next)
6780 {
6781 for (i = 0; i < list->nsyms; ++i)
6782 {
6783 struct symbol *sym = list->symbol[i];
6784
6785 if (SYMBOL_LANGUAGE (sym) == language_go
6786 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6787 {
6788 char *this_package_name = go_symbol_package_name (sym);
6789
6790 if (this_package_name == NULL)
6791 continue;
6792 if (package_name == NULL)
6793 package_name = this_package_name;
6794 else
6795 {
6796 if (strcmp (package_name, this_package_name) != 0)
6797 complaint (&symfile_complaints,
6798 _("Symtab %s has objects from two different Go packages: %s and %s"),
6799 (SYMBOL_SYMTAB (sym)
6800 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6801 : cu->objfile->name),
6802 this_package_name, package_name);
6803 xfree (this_package_name);
6804 }
6805 }
6806 }
6807 }
6808
6809 if (package_name != NULL)
6810 {
6811 struct objfile *objfile = cu->objfile;
6812 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6813 package_name,
6814 strlen (package_name));
6815 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6816 saved_package_name, objfile);
6817 struct symbol *sym;
6818
6819 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6820
6821 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6822 SYMBOL_SET_LANGUAGE (sym, language_go);
6823 SYMBOL_SET_NAMES (sym, saved_package_name,
6824 strlen (saved_package_name), 0, objfile);
6825 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6826 e.g., "main" finds the "main" module and not C's main(). */
6827 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6828 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6829 SYMBOL_TYPE (sym) = type;
6830
6831 add_symbol_to_list (sym, &global_symbols);
6832
6833 xfree (package_name);
6834 }
6835 }
6836
6837 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6838
6839 /* Return the symtab for PER_CU. This works properly regardless of
6840 whether we're using the index or psymtabs. */
6841
6842 static struct symtab *
6843 get_symtab (struct dwarf2_per_cu_data *per_cu)
6844 {
6845 return (dwarf2_per_objfile->using_index
6846 ? per_cu->v.quick->symtab
6847 : per_cu->v.psymtab->symtab);
6848 }
6849
6850 /* A helper function for computing the list of all symbol tables
6851 included by PER_CU. */
6852
6853 static void
6854 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6855 htab_t all_children,
6856 struct dwarf2_per_cu_data *per_cu)
6857 {
6858 void **slot;
6859 int ix;
6860 struct dwarf2_per_cu_data *iter;
6861
6862 slot = htab_find_slot (all_children, per_cu, INSERT);
6863 if (*slot != NULL)
6864 {
6865 /* This inclusion and its children have been processed. */
6866 return;
6867 }
6868
6869 *slot = per_cu;
6870 /* Only add a CU if it has a symbol table. */
6871 if (get_symtab (per_cu) != NULL)
6872 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6873
6874 for (ix = 0;
6875 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
6876 ++ix)
6877 recursively_compute_inclusions (result, all_children, iter);
6878 }
6879
6880 /* Compute the symtab 'includes' fields for the symtab related to
6881 PER_CU. */
6882
6883 static void
6884 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6885 {
6886 gdb_assert (! per_cu->is_debug_types);
6887
6888 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
6889 {
6890 int ix, len;
6891 struct dwarf2_per_cu_data *iter;
6892 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6893 htab_t all_children;
6894 struct symtab *symtab = get_symtab (per_cu);
6895
6896 /* If we don't have a symtab, we can just skip this case. */
6897 if (symtab == NULL)
6898 return;
6899
6900 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6901 NULL, xcalloc, xfree);
6902
6903 for (ix = 0;
6904 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
6905 ix, iter);
6906 ++ix)
6907 recursively_compute_inclusions (&result_children, all_children, iter);
6908
6909 /* Now we have a transitive closure of all the included CUs, and
6910 for .gdb_index version 7 the included TUs, so we can convert it
6911 to a list of symtabs. */
6912 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6913 symtab->includes
6914 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6915 (len + 1) * sizeof (struct symtab *));
6916 for (ix = 0;
6917 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6918 ++ix)
6919 symtab->includes[ix] = get_symtab (iter);
6920 symtab->includes[len] = NULL;
6921
6922 VEC_free (dwarf2_per_cu_ptr, result_children);
6923 htab_delete (all_children);
6924 }
6925 }
6926
6927 /* Compute the 'includes' field for the symtabs of all the CUs we just
6928 read. */
6929
6930 static void
6931 process_cu_includes (void)
6932 {
6933 int ix;
6934 struct dwarf2_per_cu_data *iter;
6935
6936 for (ix = 0;
6937 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6938 ix, iter);
6939 ++ix)
6940 {
6941 if (! iter->is_debug_types)
6942 compute_symtab_includes (iter);
6943 }
6944
6945 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6946 }
6947
6948 /* Generate full symbol information for PER_CU, whose DIEs have
6949 already been loaded into memory. */
6950
6951 static void
6952 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6953 enum language pretend_language)
6954 {
6955 struct dwarf2_cu *cu = per_cu->cu;
6956 struct objfile *objfile = per_cu->objfile;
6957 CORE_ADDR lowpc, highpc;
6958 struct symtab *symtab;
6959 struct cleanup *back_to, *delayed_list_cleanup;
6960 CORE_ADDR baseaddr;
6961 struct block *static_block;
6962
6963 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6964
6965 buildsym_init ();
6966 back_to = make_cleanup (really_free_pendings, NULL);
6967 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6968
6969 cu->list_in_scope = &file_symbols;
6970
6971 cu->language = pretend_language;
6972 cu->language_defn = language_def (cu->language);
6973
6974 /* Do line number decoding in read_file_scope () */
6975 process_die (cu->dies, cu);
6976
6977 /* For now fudge the Go package. */
6978 if (cu->language == language_go)
6979 fixup_go_packaging (cu);
6980
6981 /* Now that we have processed all the DIEs in the CU, all the types
6982 should be complete, and it should now be safe to compute all of the
6983 physnames. */
6984 compute_delayed_physnames (cu);
6985 do_cleanups (delayed_list_cleanup);
6986
6987 /* Some compilers don't define a DW_AT_high_pc attribute for the
6988 compilation unit. If the DW_AT_high_pc is missing, synthesize
6989 it, by scanning the DIE's below the compilation unit. */
6990 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6991
6992 static_block
6993 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6994 per_cu->imported_symtabs != NULL);
6995
6996 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6997 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6998 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6999 addrmap to help ensure it has an accurate map of pc values belonging to
7000 this comp unit. */
7001 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7002
7003 symtab = end_symtab_from_static_block (static_block, objfile,
7004 SECT_OFF_TEXT (objfile), 0);
7005
7006 if (symtab != NULL)
7007 {
7008 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7009
7010 /* Set symtab language to language from DW_AT_language. If the
7011 compilation is from a C file generated by language preprocessors, do
7012 not set the language if it was already deduced by start_subfile. */
7013 if (!(cu->language == language_c && symtab->language != language_c))
7014 symtab->language = cu->language;
7015
7016 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7017 produce DW_AT_location with location lists but it can be possibly
7018 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7019 there were bugs in prologue debug info, fixed later in GCC-4.5
7020 by "unwind info for epilogues" patch (which is not directly related).
7021
7022 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7023 needed, it would be wrong due to missing DW_AT_producer there.
7024
7025 Still one can confuse GDB by using non-standard GCC compilation
7026 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7027 */
7028 if (cu->has_loclist && gcc_4_minor >= 5)
7029 symtab->locations_valid = 1;
7030
7031 if (gcc_4_minor >= 5)
7032 symtab->epilogue_unwind_valid = 1;
7033
7034 symtab->call_site_htab = cu->call_site_htab;
7035 }
7036
7037 if (dwarf2_per_objfile->using_index)
7038 per_cu->v.quick->symtab = symtab;
7039 else
7040 {
7041 struct partial_symtab *pst = per_cu->v.psymtab;
7042 pst->symtab = symtab;
7043 pst->readin = 1;
7044 }
7045
7046 /* Push it for inclusion processing later. */
7047 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7048
7049 do_cleanups (back_to);
7050 }
7051
7052 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7053 already been loaded into memory. */
7054
7055 static void
7056 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7057 enum language pretend_language)
7058 {
7059 struct dwarf2_cu *cu = per_cu->cu;
7060 struct objfile *objfile = per_cu->objfile;
7061 struct symtab *symtab;
7062 struct cleanup *back_to, *delayed_list_cleanup;
7063
7064 buildsym_init ();
7065 back_to = make_cleanup (really_free_pendings, NULL);
7066 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7067
7068 cu->list_in_scope = &file_symbols;
7069
7070 cu->language = pretend_language;
7071 cu->language_defn = language_def (cu->language);
7072
7073 /* The symbol tables are set up in read_type_unit_scope. */
7074 process_die (cu->dies, cu);
7075
7076 /* For now fudge the Go package. */
7077 if (cu->language == language_go)
7078 fixup_go_packaging (cu);
7079
7080 /* Now that we have processed all the DIEs in the CU, all the types
7081 should be complete, and it should now be safe to compute all of the
7082 physnames. */
7083 compute_delayed_physnames (cu);
7084 do_cleanups (delayed_list_cleanup);
7085
7086 /* TUs share symbol tables.
7087 If this is the first TU to use this symtab, complete the construction
7088 of it with end_expandable_symtab. Otherwise, complete the addition of
7089 this TU's symbols to the existing symtab. */
7090 if (per_cu->type_unit_group->primary_symtab == NULL)
7091 {
7092 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7093 per_cu->type_unit_group->primary_symtab = symtab;
7094
7095 if (symtab != NULL)
7096 {
7097 /* Set symtab language to language from DW_AT_language. If the
7098 compilation is from a C file generated by language preprocessors,
7099 do not set the language if it was already deduced by
7100 start_subfile. */
7101 if (!(cu->language == language_c && symtab->language != language_c))
7102 symtab->language = cu->language;
7103 }
7104 }
7105 else
7106 {
7107 augment_type_symtab (objfile,
7108 per_cu->type_unit_group->primary_symtab);
7109 symtab = per_cu->type_unit_group->primary_symtab;
7110 }
7111
7112 if (dwarf2_per_objfile->using_index)
7113 per_cu->v.quick->symtab = symtab;
7114 else
7115 {
7116 struct partial_symtab *pst = per_cu->v.psymtab;
7117 pst->symtab = symtab;
7118 pst->readin = 1;
7119 }
7120
7121 do_cleanups (back_to);
7122 }
7123
7124 /* Process an imported unit DIE. */
7125
7126 static void
7127 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7128 {
7129 struct attribute *attr;
7130
7131 /* For now we don't handle imported units in type units. */
7132 if (cu->per_cu->is_debug_types)
7133 {
7134 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7135 " supported in type units [in module %s]"),
7136 cu->objfile->name);
7137 }
7138
7139 attr = dwarf2_attr (die, DW_AT_import, cu);
7140 if (attr != NULL)
7141 {
7142 struct dwarf2_per_cu_data *per_cu;
7143 struct symtab *imported_symtab;
7144 sect_offset offset;
7145 int is_dwz;
7146
7147 offset = dwarf2_get_ref_die_offset (attr);
7148 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7149 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7150
7151 /* Queue the unit, if needed. */
7152 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7153 load_full_comp_unit (per_cu, cu->language);
7154
7155 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7156 per_cu);
7157 }
7158 }
7159
7160 /* Process a die and its children. */
7161
7162 static void
7163 process_die (struct die_info *die, struct dwarf2_cu *cu)
7164 {
7165 switch (die->tag)
7166 {
7167 case DW_TAG_padding:
7168 break;
7169 case DW_TAG_compile_unit:
7170 case DW_TAG_partial_unit:
7171 read_file_scope (die, cu);
7172 break;
7173 case DW_TAG_type_unit:
7174 read_type_unit_scope (die, cu);
7175 break;
7176 case DW_TAG_subprogram:
7177 case DW_TAG_inlined_subroutine:
7178 read_func_scope (die, cu);
7179 break;
7180 case DW_TAG_lexical_block:
7181 case DW_TAG_try_block:
7182 case DW_TAG_catch_block:
7183 read_lexical_block_scope (die, cu);
7184 break;
7185 case DW_TAG_GNU_call_site:
7186 read_call_site_scope (die, cu);
7187 break;
7188 case DW_TAG_class_type:
7189 case DW_TAG_interface_type:
7190 case DW_TAG_structure_type:
7191 case DW_TAG_union_type:
7192 process_structure_scope (die, cu);
7193 break;
7194 case DW_TAG_enumeration_type:
7195 process_enumeration_scope (die, cu);
7196 break;
7197
7198 /* These dies have a type, but processing them does not create
7199 a symbol or recurse to process the children. Therefore we can
7200 read them on-demand through read_type_die. */
7201 case DW_TAG_subroutine_type:
7202 case DW_TAG_set_type:
7203 case DW_TAG_array_type:
7204 case DW_TAG_pointer_type:
7205 case DW_TAG_ptr_to_member_type:
7206 case DW_TAG_reference_type:
7207 case DW_TAG_string_type:
7208 break;
7209
7210 case DW_TAG_base_type:
7211 case DW_TAG_subrange_type:
7212 case DW_TAG_typedef:
7213 /* Add a typedef symbol for the type definition, if it has a
7214 DW_AT_name. */
7215 new_symbol (die, read_type_die (die, cu), cu);
7216 break;
7217 case DW_TAG_common_block:
7218 read_common_block (die, cu);
7219 break;
7220 case DW_TAG_common_inclusion:
7221 break;
7222 case DW_TAG_namespace:
7223 cu->processing_has_namespace_info = 1;
7224 read_namespace (die, cu);
7225 break;
7226 case DW_TAG_module:
7227 cu->processing_has_namespace_info = 1;
7228 read_module (die, cu);
7229 break;
7230 case DW_TAG_imported_declaration:
7231 case DW_TAG_imported_module:
7232 cu->processing_has_namespace_info = 1;
7233 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7234 || cu->language != language_fortran))
7235 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7236 dwarf_tag_name (die->tag));
7237 read_import_statement (die, cu);
7238 break;
7239
7240 case DW_TAG_imported_unit:
7241 process_imported_unit_die (die, cu);
7242 break;
7243
7244 default:
7245 new_symbol (die, NULL, cu);
7246 break;
7247 }
7248 }
7249
7250 /* A helper function for dwarf2_compute_name which determines whether DIE
7251 needs to have the name of the scope prepended to the name listed in the
7252 die. */
7253
7254 static int
7255 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7256 {
7257 struct attribute *attr;
7258
7259 switch (die->tag)
7260 {
7261 case DW_TAG_namespace:
7262 case DW_TAG_typedef:
7263 case DW_TAG_class_type:
7264 case DW_TAG_interface_type:
7265 case DW_TAG_structure_type:
7266 case DW_TAG_union_type:
7267 case DW_TAG_enumeration_type:
7268 case DW_TAG_enumerator:
7269 case DW_TAG_subprogram:
7270 case DW_TAG_member:
7271 return 1;
7272
7273 case DW_TAG_variable:
7274 case DW_TAG_constant:
7275 /* We only need to prefix "globally" visible variables. These include
7276 any variable marked with DW_AT_external or any variable that
7277 lives in a namespace. [Variables in anonymous namespaces
7278 require prefixing, but they are not DW_AT_external.] */
7279
7280 if (dwarf2_attr (die, DW_AT_specification, cu))
7281 {
7282 struct dwarf2_cu *spec_cu = cu;
7283
7284 return die_needs_namespace (die_specification (die, &spec_cu),
7285 spec_cu);
7286 }
7287
7288 attr = dwarf2_attr (die, DW_AT_external, cu);
7289 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7290 && die->parent->tag != DW_TAG_module)
7291 return 0;
7292 /* A variable in a lexical block of some kind does not need a
7293 namespace, even though in C++ such variables may be external
7294 and have a mangled name. */
7295 if (die->parent->tag == DW_TAG_lexical_block
7296 || die->parent->tag == DW_TAG_try_block
7297 || die->parent->tag == DW_TAG_catch_block
7298 || die->parent->tag == DW_TAG_subprogram)
7299 return 0;
7300 return 1;
7301
7302 default:
7303 return 0;
7304 }
7305 }
7306
7307 /* Retrieve the last character from a mem_file. */
7308
7309 static void
7310 do_ui_file_peek_last (void *object, const char *buffer, long length)
7311 {
7312 char *last_char_p = (char *) object;
7313
7314 if (length > 0)
7315 *last_char_p = buffer[length - 1];
7316 }
7317
7318 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7319 compute the physname for the object, which include a method's:
7320 - formal parameters (C++/Java),
7321 - receiver type (Go),
7322 - return type (Java).
7323
7324 The term "physname" is a bit confusing.
7325 For C++, for example, it is the demangled name.
7326 For Go, for example, it's the mangled name.
7327
7328 For Ada, return the DIE's linkage name rather than the fully qualified
7329 name. PHYSNAME is ignored..
7330
7331 The result is allocated on the objfile_obstack and canonicalized. */
7332
7333 static const char *
7334 dwarf2_compute_name (const char *name,
7335 struct die_info *die, struct dwarf2_cu *cu,
7336 int physname)
7337 {
7338 struct objfile *objfile = cu->objfile;
7339
7340 if (name == NULL)
7341 name = dwarf2_name (die, cu);
7342
7343 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7344 compute it by typename_concat inside GDB. */
7345 if (cu->language == language_ada
7346 || (cu->language == language_fortran && physname))
7347 {
7348 /* For Ada unit, we prefer the linkage name over the name, as
7349 the former contains the exported name, which the user expects
7350 to be able to reference. Ideally, we want the user to be able
7351 to reference this entity using either natural or linkage name,
7352 but we haven't started looking at this enhancement yet. */
7353 struct attribute *attr;
7354
7355 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7356 if (attr == NULL)
7357 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7358 if (attr && DW_STRING (attr))
7359 return DW_STRING (attr);
7360 }
7361
7362 /* These are the only languages we know how to qualify names in. */
7363 if (name != NULL
7364 && (cu->language == language_cplus || cu->language == language_java
7365 || cu->language == language_fortran))
7366 {
7367 if (die_needs_namespace (die, cu))
7368 {
7369 long length;
7370 const char *prefix;
7371 struct ui_file *buf;
7372
7373 prefix = determine_prefix (die, cu);
7374 buf = mem_fileopen ();
7375 if (*prefix != '\0')
7376 {
7377 char *prefixed_name = typename_concat (NULL, prefix, name,
7378 physname, cu);
7379
7380 fputs_unfiltered (prefixed_name, buf);
7381 xfree (prefixed_name);
7382 }
7383 else
7384 fputs_unfiltered (name, buf);
7385
7386 /* Template parameters may be specified in the DIE's DW_AT_name, or
7387 as children with DW_TAG_template_type_param or
7388 DW_TAG_value_type_param. If the latter, add them to the name
7389 here. If the name already has template parameters, then
7390 skip this step; some versions of GCC emit both, and
7391 it is more efficient to use the pre-computed name.
7392
7393 Something to keep in mind about this process: it is very
7394 unlikely, or in some cases downright impossible, to produce
7395 something that will match the mangled name of a function.
7396 If the definition of the function has the same debug info,
7397 we should be able to match up with it anyway. But fallbacks
7398 using the minimal symbol, for instance to find a method
7399 implemented in a stripped copy of libstdc++, will not work.
7400 If we do not have debug info for the definition, we will have to
7401 match them up some other way.
7402
7403 When we do name matching there is a related problem with function
7404 templates; two instantiated function templates are allowed to
7405 differ only by their return types, which we do not add here. */
7406
7407 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7408 {
7409 struct attribute *attr;
7410 struct die_info *child;
7411 int first = 1;
7412
7413 die->building_fullname = 1;
7414
7415 for (child = die->child; child != NULL; child = child->sibling)
7416 {
7417 struct type *type;
7418 LONGEST value;
7419 gdb_byte *bytes;
7420 struct dwarf2_locexpr_baton *baton;
7421 struct value *v;
7422
7423 if (child->tag != DW_TAG_template_type_param
7424 && child->tag != DW_TAG_template_value_param)
7425 continue;
7426
7427 if (first)
7428 {
7429 fputs_unfiltered ("<", buf);
7430 first = 0;
7431 }
7432 else
7433 fputs_unfiltered (", ", buf);
7434
7435 attr = dwarf2_attr (child, DW_AT_type, cu);
7436 if (attr == NULL)
7437 {
7438 complaint (&symfile_complaints,
7439 _("template parameter missing DW_AT_type"));
7440 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7441 continue;
7442 }
7443 type = die_type (child, cu);
7444
7445 if (child->tag == DW_TAG_template_type_param)
7446 {
7447 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7448 continue;
7449 }
7450
7451 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7452 if (attr == NULL)
7453 {
7454 complaint (&symfile_complaints,
7455 _("template parameter missing "
7456 "DW_AT_const_value"));
7457 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7458 continue;
7459 }
7460
7461 dwarf2_const_value_attr (attr, type, name,
7462 &cu->comp_unit_obstack, cu,
7463 &value, &bytes, &baton);
7464
7465 if (TYPE_NOSIGN (type))
7466 /* GDB prints characters as NUMBER 'CHAR'. If that's
7467 changed, this can use value_print instead. */
7468 c_printchar (value, type, buf);
7469 else
7470 {
7471 struct value_print_options opts;
7472
7473 if (baton != NULL)
7474 v = dwarf2_evaluate_loc_desc (type, NULL,
7475 baton->data,
7476 baton->size,
7477 baton->per_cu);
7478 else if (bytes != NULL)
7479 {
7480 v = allocate_value (type);
7481 memcpy (value_contents_writeable (v), bytes,
7482 TYPE_LENGTH (type));
7483 }
7484 else
7485 v = value_from_longest (type, value);
7486
7487 /* Specify decimal so that we do not depend on
7488 the radix. */
7489 get_formatted_print_options (&opts, 'd');
7490 opts.raw = 1;
7491 value_print (v, buf, &opts);
7492 release_value (v);
7493 value_free (v);
7494 }
7495 }
7496
7497 die->building_fullname = 0;
7498
7499 if (!first)
7500 {
7501 /* Close the argument list, with a space if necessary
7502 (nested templates). */
7503 char last_char = '\0';
7504 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7505 if (last_char == '>')
7506 fputs_unfiltered (" >", buf);
7507 else
7508 fputs_unfiltered (">", buf);
7509 }
7510 }
7511
7512 /* For Java and C++ methods, append formal parameter type
7513 information, if PHYSNAME. */
7514
7515 if (physname && die->tag == DW_TAG_subprogram
7516 && (cu->language == language_cplus
7517 || cu->language == language_java))
7518 {
7519 struct type *type = read_type_die (die, cu);
7520
7521 c_type_print_args (type, buf, 1, cu->language,
7522 &type_print_raw_options);
7523
7524 if (cu->language == language_java)
7525 {
7526 /* For java, we must append the return type to method
7527 names. */
7528 if (die->tag == DW_TAG_subprogram)
7529 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7530 0, 0, &type_print_raw_options);
7531 }
7532 else if (cu->language == language_cplus)
7533 {
7534 /* Assume that an artificial first parameter is
7535 "this", but do not crash if it is not. RealView
7536 marks unnamed (and thus unused) parameters as
7537 artificial; there is no way to differentiate
7538 the two cases. */
7539 if (TYPE_NFIELDS (type) > 0
7540 && TYPE_FIELD_ARTIFICIAL (type, 0)
7541 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7542 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7543 0))))
7544 fputs_unfiltered (" const", buf);
7545 }
7546 }
7547
7548 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7549 &length);
7550 ui_file_delete (buf);
7551
7552 if (cu->language == language_cplus)
7553 {
7554 const char *cname
7555 = dwarf2_canonicalize_name (name, cu,
7556 &objfile->objfile_obstack);
7557
7558 if (cname != NULL)
7559 name = cname;
7560 }
7561 }
7562 }
7563
7564 return name;
7565 }
7566
7567 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7568 If scope qualifiers are appropriate they will be added. The result
7569 will be allocated on the objfile_obstack, or NULL if the DIE does
7570 not have a name. NAME may either be from a previous call to
7571 dwarf2_name or NULL.
7572
7573 The output string will be canonicalized (if C++/Java). */
7574
7575 static const char *
7576 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7577 {
7578 return dwarf2_compute_name (name, die, cu, 0);
7579 }
7580
7581 /* Construct a physname for the given DIE in CU. NAME may either be
7582 from a previous call to dwarf2_name or NULL. The result will be
7583 allocated on the objfile_objstack or NULL if the DIE does not have a
7584 name.
7585
7586 The output string will be canonicalized (if C++/Java). */
7587
7588 static const char *
7589 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7590 {
7591 struct objfile *objfile = cu->objfile;
7592 struct attribute *attr;
7593 const char *retval, *mangled = NULL, *canon = NULL;
7594 struct cleanup *back_to;
7595 int need_copy = 1;
7596
7597 /* In this case dwarf2_compute_name is just a shortcut not building anything
7598 on its own. */
7599 if (!die_needs_namespace (die, cu))
7600 return dwarf2_compute_name (name, die, cu, 1);
7601
7602 back_to = make_cleanup (null_cleanup, NULL);
7603
7604 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7605 if (!attr)
7606 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7607
7608 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7609 has computed. */
7610 if (attr && DW_STRING (attr))
7611 {
7612 char *demangled;
7613
7614 mangled = DW_STRING (attr);
7615
7616 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7617 type. It is easier for GDB users to search for such functions as
7618 `name(params)' than `long name(params)'. In such case the minimal
7619 symbol names do not match the full symbol names but for template
7620 functions there is never a need to look up their definition from their
7621 declaration so the only disadvantage remains the minimal symbol
7622 variant `long name(params)' does not have the proper inferior type.
7623 */
7624
7625 if (cu->language == language_go)
7626 {
7627 /* This is a lie, but we already lie to the caller new_symbol_full.
7628 new_symbol_full assumes we return the mangled name.
7629 This just undoes that lie until things are cleaned up. */
7630 demangled = NULL;
7631 }
7632 else
7633 {
7634 demangled = cplus_demangle (mangled,
7635 (DMGL_PARAMS | DMGL_ANSI
7636 | (cu->language == language_java
7637 ? DMGL_JAVA | DMGL_RET_POSTFIX
7638 : DMGL_RET_DROP)));
7639 }
7640 if (demangled)
7641 {
7642 make_cleanup (xfree, demangled);
7643 canon = demangled;
7644 }
7645 else
7646 {
7647 canon = mangled;
7648 need_copy = 0;
7649 }
7650 }
7651
7652 if (canon == NULL || check_physname)
7653 {
7654 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7655
7656 if (canon != NULL && strcmp (physname, canon) != 0)
7657 {
7658 /* It may not mean a bug in GDB. The compiler could also
7659 compute DW_AT_linkage_name incorrectly. But in such case
7660 GDB would need to be bug-to-bug compatible. */
7661
7662 complaint (&symfile_complaints,
7663 _("Computed physname <%s> does not match demangled <%s> "
7664 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7665 physname, canon, mangled, die->offset.sect_off, objfile->name);
7666
7667 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7668 is available here - over computed PHYSNAME. It is safer
7669 against both buggy GDB and buggy compilers. */
7670
7671 retval = canon;
7672 }
7673 else
7674 {
7675 retval = physname;
7676 need_copy = 0;
7677 }
7678 }
7679 else
7680 retval = canon;
7681
7682 if (need_copy)
7683 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7684
7685 do_cleanups (back_to);
7686 return retval;
7687 }
7688
7689 /* Read the import statement specified by the given die and record it. */
7690
7691 static void
7692 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7693 {
7694 struct objfile *objfile = cu->objfile;
7695 struct attribute *import_attr;
7696 struct die_info *imported_die, *child_die;
7697 struct dwarf2_cu *imported_cu;
7698 const char *imported_name;
7699 const char *imported_name_prefix;
7700 const char *canonical_name;
7701 const char *import_alias;
7702 const char *imported_declaration = NULL;
7703 const char *import_prefix;
7704 VEC (const_char_ptr) *excludes = NULL;
7705 struct cleanup *cleanups;
7706
7707 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7708 if (import_attr == NULL)
7709 {
7710 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7711 dwarf_tag_name (die->tag));
7712 return;
7713 }
7714
7715 imported_cu = cu;
7716 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7717 imported_name = dwarf2_name (imported_die, imported_cu);
7718 if (imported_name == NULL)
7719 {
7720 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7721
7722 The import in the following code:
7723 namespace A
7724 {
7725 typedef int B;
7726 }
7727
7728 int main ()
7729 {
7730 using A::B;
7731 B b;
7732 return b;
7733 }
7734
7735 ...
7736 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7737 <52> DW_AT_decl_file : 1
7738 <53> DW_AT_decl_line : 6
7739 <54> DW_AT_import : <0x75>
7740 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7741 <59> DW_AT_name : B
7742 <5b> DW_AT_decl_file : 1
7743 <5c> DW_AT_decl_line : 2
7744 <5d> DW_AT_type : <0x6e>
7745 ...
7746 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7747 <76> DW_AT_byte_size : 4
7748 <77> DW_AT_encoding : 5 (signed)
7749
7750 imports the wrong die ( 0x75 instead of 0x58 ).
7751 This case will be ignored until the gcc bug is fixed. */
7752 return;
7753 }
7754
7755 /* Figure out the local name after import. */
7756 import_alias = dwarf2_name (die, cu);
7757
7758 /* Figure out where the statement is being imported to. */
7759 import_prefix = determine_prefix (die, cu);
7760
7761 /* Figure out what the scope of the imported die is and prepend it
7762 to the name of the imported die. */
7763 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7764
7765 if (imported_die->tag != DW_TAG_namespace
7766 && imported_die->tag != DW_TAG_module)
7767 {
7768 imported_declaration = imported_name;
7769 canonical_name = imported_name_prefix;
7770 }
7771 else if (strlen (imported_name_prefix) > 0)
7772 canonical_name = obconcat (&objfile->objfile_obstack,
7773 imported_name_prefix, "::", imported_name,
7774 (char *) NULL);
7775 else
7776 canonical_name = imported_name;
7777
7778 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7779
7780 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7781 for (child_die = die->child; child_die && child_die->tag;
7782 child_die = sibling_die (child_die))
7783 {
7784 /* DWARF-4: A Fortran use statement with a “rename list” may be
7785 represented by an imported module entry with an import attribute
7786 referring to the module and owned entries corresponding to those
7787 entities that are renamed as part of being imported. */
7788
7789 if (child_die->tag != DW_TAG_imported_declaration)
7790 {
7791 complaint (&symfile_complaints,
7792 _("child DW_TAG_imported_declaration expected "
7793 "- DIE at 0x%x [in module %s]"),
7794 child_die->offset.sect_off, objfile->name);
7795 continue;
7796 }
7797
7798 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7799 if (import_attr == NULL)
7800 {
7801 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7802 dwarf_tag_name (child_die->tag));
7803 continue;
7804 }
7805
7806 imported_cu = cu;
7807 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7808 &imported_cu);
7809 imported_name = dwarf2_name (imported_die, imported_cu);
7810 if (imported_name == NULL)
7811 {
7812 complaint (&symfile_complaints,
7813 _("child DW_TAG_imported_declaration has unknown "
7814 "imported name - DIE at 0x%x [in module %s]"),
7815 child_die->offset.sect_off, objfile->name);
7816 continue;
7817 }
7818
7819 VEC_safe_push (const_char_ptr, excludes, imported_name);
7820
7821 process_die (child_die, cu);
7822 }
7823
7824 cp_add_using_directive (import_prefix,
7825 canonical_name,
7826 import_alias,
7827 imported_declaration,
7828 excludes,
7829 0,
7830 &objfile->objfile_obstack);
7831
7832 do_cleanups (cleanups);
7833 }
7834
7835 /* Cleanup function for handle_DW_AT_stmt_list. */
7836
7837 static void
7838 free_cu_line_header (void *arg)
7839 {
7840 struct dwarf2_cu *cu = arg;
7841
7842 free_line_header (cu->line_header);
7843 cu->line_header = NULL;
7844 }
7845
7846 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7847 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7848 this, it was first present in GCC release 4.3.0. */
7849
7850 static int
7851 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7852 {
7853 if (!cu->checked_producer)
7854 check_producer (cu);
7855
7856 return cu->producer_is_gcc_lt_4_3;
7857 }
7858
7859 static void
7860 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7861 const char **name, const char **comp_dir)
7862 {
7863 struct attribute *attr;
7864
7865 *name = NULL;
7866 *comp_dir = NULL;
7867
7868 /* Find the filename. Do not use dwarf2_name here, since the filename
7869 is not a source language identifier. */
7870 attr = dwarf2_attr (die, DW_AT_name, cu);
7871 if (attr)
7872 {
7873 *name = DW_STRING (attr);
7874 }
7875
7876 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7877 if (attr)
7878 *comp_dir = DW_STRING (attr);
7879 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7880 && IS_ABSOLUTE_PATH (*name))
7881 {
7882 char *d = ldirname (*name);
7883
7884 *comp_dir = d;
7885 if (d != NULL)
7886 make_cleanup (xfree, d);
7887 }
7888 if (*comp_dir != NULL)
7889 {
7890 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7891 directory, get rid of it. */
7892 char *cp = strchr (*comp_dir, ':');
7893
7894 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7895 *comp_dir = cp + 1;
7896 }
7897
7898 if (*name == NULL)
7899 *name = "<unknown>";
7900 }
7901
7902 /* Handle DW_AT_stmt_list for a compilation unit.
7903 DIE is the DW_TAG_compile_unit die for CU.
7904 COMP_DIR is the compilation directory.
7905 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7906
7907 static void
7908 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7909 const char *comp_dir)
7910 {
7911 struct attribute *attr;
7912
7913 gdb_assert (! cu->per_cu->is_debug_types);
7914
7915 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7916 if (attr)
7917 {
7918 unsigned int line_offset = DW_UNSND (attr);
7919 struct line_header *line_header
7920 = dwarf_decode_line_header (line_offset, cu);
7921
7922 if (line_header)
7923 {
7924 cu->line_header = line_header;
7925 make_cleanup (free_cu_line_header, cu);
7926 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7927 }
7928 }
7929 }
7930
7931 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7932
7933 static void
7934 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7935 {
7936 struct objfile *objfile = dwarf2_per_objfile->objfile;
7937 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7938 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7939 CORE_ADDR highpc = ((CORE_ADDR) 0);
7940 struct attribute *attr;
7941 const char *name = NULL;
7942 const char *comp_dir = NULL;
7943 struct die_info *child_die;
7944 bfd *abfd = objfile->obfd;
7945 CORE_ADDR baseaddr;
7946
7947 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7948
7949 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7950
7951 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7952 from finish_block. */
7953 if (lowpc == ((CORE_ADDR) -1))
7954 lowpc = highpc;
7955 lowpc += baseaddr;
7956 highpc += baseaddr;
7957
7958 find_file_and_directory (die, cu, &name, &comp_dir);
7959
7960 prepare_one_comp_unit (cu, die, cu->language);
7961
7962 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7963 standardised yet. As a workaround for the language detection we fall
7964 back to the DW_AT_producer string. */
7965 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7966 cu->language = language_opencl;
7967
7968 /* Similar hack for Go. */
7969 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7970 set_cu_language (DW_LANG_Go, cu);
7971
7972 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7973
7974 /* Decode line number information if present. We do this before
7975 processing child DIEs, so that the line header table is available
7976 for DW_AT_decl_file. */
7977 handle_DW_AT_stmt_list (die, cu, comp_dir);
7978
7979 /* Process all dies in compilation unit. */
7980 if (die->child != NULL)
7981 {
7982 child_die = die->child;
7983 while (child_die && child_die->tag)
7984 {
7985 process_die (child_die, cu);
7986 child_die = sibling_die (child_die);
7987 }
7988 }
7989
7990 /* Decode macro information, if present. Dwarf 2 macro information
7991 refers to information in the line number info statement program
7992 header, so we can only read it if we've read the header
7993 successfully. */
7994 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7995 if (attr && cu->line_header)
7996 {
7997 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7998 complaint (&symfile_complaints,
7999 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8000
8001 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8002 }
8003 else
8004 {
8005 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8006 if (attr && cu->line_header)
8007 {
8008 unsigned int macro_offset = DW_UNSND (attr);
8009
8010 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8011 }
8012 }
8013
8014 do_cleanups (back_to);
8015 }
8016
8017 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8018 Create the set of symtabs used by this TU, or if this TU is sharing
8019 symtabs with another TU and the symtabs have already been created
8020 then restore those symtabs in the line header.
8021 We don't need the pc/line-number mapping for type units. */
8022
8023 static void
8024 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8025 {
8026 struct objfile *objfile = dwarf2_per_objfile->objfile;
8027 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8028 struct type_unit_group *tu_group;
8029 int first_time;
8030 struct line_header *lh;
8031 struct attribute *attr;
8032 unsigned int i, line_offset;
8033
8034 gdb_assert (per_cu->is_debug_types);
8035
8036 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8037
8038 /* If we're using .gdb_index (includes -readnow) then
8039 per_cu->s.type_unit_group may not have been set up yet. */
8040 if (per_cu->type_unit_group == NULL)
8041 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8042 tu_group = per_cu->type_unit_group;
8043
8044 /* If we've already processed this stmt_list there's no real need to
8045 do it again, we could fake it and just recreate the part we need
8046 (file name,index -> symtab mapping). If data shows this optimization
8047 is useful we can do it then. */
8048 first_time = tu_group->primary_symtab == NULL;
8049
8050 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8051 debug info. */
8052 lh = NULL;
8053 if (attr != NULL)
8054 {
8055 line_offset = DW_UNSND (attr);
8056 lh = dwarf_decode_line_header (line_offset, cu);
8057 }
8058 if (lh == NULL)
8059 {
8060 if (first_time)
8061 dwarf2_start_symtab (cu, "", NULL, 0);
8062 else
8063 {
8064 gdb_assert (tu_group->symtabs == NULL);
8065 restart_symtab (0);
8066 }
8067 /* Note: The primary symtab will get allocated at the end. */
8068 return;
8069 }
8070
8071 cu->line_header = lh;
8072 make_cleanup (free_cu_line_header, cu);
8073
8074 if (first_time)
8075 {
8076 dwarf2_start_symtab (cu, "", NULL, 0);
8077
8078 tu_group->num_symtabs = lh->num_file_names;
8079 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8080
8081 for (i = 0; i < lh->num_file_names; ++i)
8082 {
8083 char *dir = NULL;
8084 struct file_entry *fe = &lh->file_names[i];
8085
8086 if (fe->dir_index)
8087 dir = lh->include_dirs[fe->dir_index - 1];
8088 dwarf2_start_subfile (fe->name, dir, NULL);
8089
8090 /* Note: We don't have to watch for the main subfile here, type units
8091 don't have DW_AT_name. */
8092
8093 if (current_subfile->symtab == NULL)
8094 {
8095 /* NOTE: start_subfile will recognize when it's been passed
8096 a file it has already seen. So we can't assume there's a
8097 simple mapping from lh->file_names to subfiles,
8098 lh->file_names may contain dups. */
8099 current_subfile->symtab = allocate_symtab (current_subfile->name,
8100 objfile);
8101 }
8102
8103 fe->symtab = current_subfile->symtab;
8104 tu_group->symtabs[i] = fe->symtab;
8105 }
8106 }
8107 else
8108 {
8109 restart_symtab (0);
8110
8111 for (i = 0; i < lh->num_file_names; ++i)
8112 {
8113 struct file_entry *fe = &lh->file_names[i];
8114
8115 fe->symtab = tu_group->symtabs[i];
8116 }
8117 }
8118
8119 /* The main symtab is allocated last. Type units don't have DW_AT_name
8120 so they don't have a "real" (so to speak) symtab anyway.
8121 There is later code that will assign the main symtab to all symbols
8122 that don't have one. We need to handle the case of a symbol with a
8123 missing symtab (DW_AT_decl_file) anyway. */
8124 }
8125
8126 /* Process DW_TAG_type_unit.
8127 For TUs we want to skip the first top level sibling if it's not the
8128 actual type being defined by this TU. In this case the first top
8129 level sibling is there to provide context only. */
8130
8131 static void
8132 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8133 {
8134 struct die_info *child_die;
8135
8136 prepare_one_comp_unit (cu, die, language_minimal);
8137
8138 /* Initialize (or reinitialize) the machinery for building symtabs.
8139 We do this before processing child DIEs, so that the line header table
8140 is available for DW_AT_decl_file. */
8141 setup_type_unit_groups (die, cu);
8142
8143 if (die->child != NULL)
8144 {
8145 child_die = die->child;
8146 while (child_die && child_die->tag)
8147 {
8148 process_die (child_die, cu);
8149 child_die = sibling_die (child_die);
8150 }
8151 }
8152 }
8153 \f
8154 /* DWO/DWP files.
8155
8156 http://gcc.gnu.org/wiki/DebugFission
8157 http://gcc.gnu.org/wiki/DebugFissionDWP
8158
8159 To simplify handling of both DWO files ("object" files with the DWARF info)
8160 and DWP files (a file with the DWOs packaged up into one file), we treat
8161 DWP files as having a collection of virtual DWO files. */
8162
8163 static hashval_t
8164 hash_dwo_file (const void *item)
8165 {
8166 const struct dwo_file *dwo_file = item;
8167
8168 return htab_hash_string (dwo_file->name);
8169 }
8170
8171 static int
8172 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8173 {
8174 const struct dwo_file *lhs = item_lhs;
8175 const struct dwo_file *rhs = item_rhs;
8176
8177 return strcmp (lhs->name, rhs->name) == 0;
8178 }
8179
8180 /* Allocate a hash table for DWO files. */
8181
8182 static htab_t
8183 allocate_dwo_file_hash_table (void)
8184 {
8185 struct objfile *objfile = dwarf2_per_objfile->objfile;
8186
8187 return htab_create_alloc_ex (41,
8188 hash_dwo_file,
8189 eq_dwo_file,
8190 NULL,
8191 &objfile->objfile_obstack,
8192 hashtab_obstack_allocate,
8193 dummy_obstack_deallocate);
8194 }
8195
8196 /* Lookup DWO file DWO_NAME. */
8197
8198 static void **
8199 lookup_dwo_file_slot (const char *dwo_name)
8200 {
8201 struct dwo_file find_entry;
8202 void **slot;
8203
8204 if (dwarf2_per_objfile->dwo_files == NULL)
8205 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8206
8207 memset (&find_entry, 0, sizeof (find_entry));
8208 find_entry.name = dwo_name;
8209 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8210
8211 return slot;
8212 }
8213
8214 static hashval_t
8215 hash_dwo_unit (const void *item)
8216 {
8217 const struct dwo_unit *dwo_unit = item;
8218
8219 /* This drops the top 32 bits of the id, but is ok for a hash. */
8220 return dwo_unit->signature;
8221 }
8222
8223 static int
8224 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8225 {
8226 const struct dwo_unit *lhs = item_lhs;
8227 const struct dwo_unit *rhs = item_rhs;
8228
8229 /* The signature is assumed to be unique within the DWO file.
8230 So while object file CU dwo_id's always have the value zero,
8231 that's OK, assuming each object file DWO file has only one CU,
8232 and that's the rule for now. */
8233 return lhs->signature == rhs->signature;
8234 }
8235
8236 /* Allocate a hash table for DWO CUs,TUs.
8237 There is one of these tables for each of CUs,TUs for each DWO file. */
8238
8239 static htab_t
8240 allocate_dwo_unit_table (struct objfile *objfile)
8241 {
8242 /* Start out with a pretty small number.
8243 Generally DWO files contain only one CU and maybe some TUs. */
8244 return htab_create_alloc_ex (3,
8245 hash_dwo_unit,
8246 eq_dwo_unit,
8247 NULL,
8248 &objfile->objfile_obstack,
8249 hashtab_obstack_allocate,
8250 dummy_obstack_deallocate);
8251 }
8252
8253 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8254
8255 struct create_dwo_info_table_data
8256 {
8257 struct dwo_file *dwo_file;
8258 htab_t cu_htab;
8259 };
8260
8261 /* die_reader_func for create_dwo_debug_info_hash_table. */
8262
8263 static void
8264 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8265 gdb_byte *info_ptr,
8266 struct die_info *comp_unit_die,
8267 int has_children,
8268 void *datap)
8269 {
8270 struct dwarf2_cu *cu = reader->cu;
8271 struct objfile *objfile = dwarf2_per_objfile->objfile;
8272 sect_offset offset = cu->per_cu->offset;
8273 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8274 struct create_dwo_info_table_data *data = datap;
8275 struct dwo_file *dwo_file = data->dwo_file;
8276 htab_t cu_htab = data->cu_htab;
8277 void **slot;
8278 struct attribute *attr;
8279 struct dwo_unit *dwo_unit;
8280
8281 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8282 if (attr == NULL)
8283 {
8284 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8285 " its dwo_id [in module %s]"),
8286 offset.sect_off, dwo_file->name);
8287 return;
8288 }
8289
8290 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8291 dwo_unit->dwo_file = dwo_file;
8292 dwo_unit->signature = DW_UNSND (attr);
8293 dwo_unit->info_or_types_section = section;
8294 dwo_unit->offset = offset;
8295 dwo_unit->length = cu->per_cu->length;
8296
8297 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8298 gdb_assert (slot != NULL);
8299 if (*slot != NULL)
8300 {
8301 const struct dwo_unit *dup_dwo_unit = *slot;
8302
8303 complaint (&symfile_complaints,
8304 _("debug entry at offset 0x%x is duplicate to the entry at"
8305 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8306 offset.sect_off, dup_dwo_unit->offset.sect_off,
8307 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8308 dwo_file->name);
8309 }
8310 else
8311 *slot = dwo_unit;
8312
8313 if (dwarf2_read_debug)
8314 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8315 offset.sect_off,
8316 phex (dwo_unit->signature,
8317 sizeof (dwo_unit->signature)));
8318 }
8319
8320 /* Create a hash table to map DWO IDs to their CU entry in
8321 .debug_info.dwo in DWO_FILE.
8322 Note: This function processes DWO files only, not DWP files. */
8323
8324 static htab_t
8325 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8326 {
8327 struct objfile *objfile = dwarf2_per_objfile->objfile;
8328 struct dwarf2_section_info *section = &dwo_file->sections.info;
8329 bfd *abfd;
8330 htab_t cu_htab;
8331 gdb_byte *info_ptr, *end_ptr;
8332 struct create_dwo_info_table_data create_dwo_info_table_data;
8333
8334 dwarf2_read_section (objfile, section);
8335 info_ptr = section->buffer;
8336
8337 if (info_ptr == NULL)
8338 return NULL;
8339
8340 /* We can't set abfd until now because the section may be empty or
8341 not present, in which case section->asection will be NULL. */
8342 abfd = section->asection->owner;
8343
8344 if (dwarf2_read_debug)
8345 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8346 bfd_get_filename (abfd));
8347
8348 cu_htab = allocate_dwo_unit_table (objfile);
8349
8350 create_dwo_info_table_data.dwo_file = dwo_file;
8351 create_dwo_info_table_data.cu_htab = cu_htab;
8352
8353 end_ptr = info_ptr + section->size;
8354 while (info_ptr < end_ptr)
8355 {
8356 struct dwarf2_per_cu_data per_cu;
8357
8358 memset (&per_cu, 0, sizeof (per_cu));
8359 per_cu.objfile = objfile;
8360 per_cu.is_debug_types = 0;
8361 per_cu.offset.sect_off = info_ptr - section->buffer;
8362 per_cu.info_or_types_section = section;
8363
8364 init_cutu_and_read_dies_no_follow (&per_cu,
8365 &dwo_file->sections.abbrev,
8366 dwo_file,
8367 create_dwo_debug_info_hash_table_reader,
8368 &create_dwo_info_table_data);
8369
8370 info_ptr += per_cu.length;
8371 }
8372
8373 return cu_htab;
8374 }
8375
8376 /* DWP file .debug_{cu,tu}_index section format:
8377 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8378
8379 Both index sections have the same format, and serve to map a 64-bit
8380 signature to a set of section numbers. Each section begins with a header,
8381 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8382 indexes, and a pool of 32-bit section numbers. The index sections will be
8383 aligned at 8-byte boundaries in the file.
8384
8385 The index section header contains two unsigned 32-bit values (using the
8386 byte order of the application binary):
8387
8388 N, the number of compilation units or type units in the index
8389 M, the number of slots in the hash table
8390
8391 (We assume that N and M will not exceed 2^32 - 1.)
8392
8393 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8394
8395 The hash table begins at offset 8 in the section, and consists of an array
8396 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8397 order of the application binary). Unused slots in the hash table are 0.
8398 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8399
8400 The parallel table begins immediately after the hash table
8401 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8402 array of 32-bit indexes (using the byte order of the application binary),
8403 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8404 table contains a 32-bit index into the pool of section numbers. For unused
8405 hash table slots, the corresponding entry in the parallel table will be 0.
8406
8407 Given a 64-bit compilation unit signature or a type signature S, an entry
8408 in the hash table is located as follows:
8409
8410 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8411 the low-order k bits all set to 1.
8412
8413 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8414
8415 3) If the hash table entry at index H matches the signature, use that
8416 entry. If the hash table entry at index H is unused (all zeroes),
8417 terminate the search: the signature is not present in the table.
8418
8419 4) Let H = (H + H') modulo M. Repeat at Step 3.
8420
8421 Because M > N and H' and M are relatively prime, the search is guaranteed
8422 to stop at an unused slot or find the match.
8423
8424 The pool of section numbers begins immediately following the hash table
8425 (at offset 8 + 12 * M from the beginning of the section). The pool of
8426 section numbers consists of an array of 32-bit words (using the byte order
8427 of the application binary). Each item in the array is indexed starting
8428 from 0. The hash table entry provides the index of the first section
8429 number in the set. Additional section numbers in the set follow, and the
8430 set is terminated by a 0 entry (section number 0 is not used in ELF).
8431
8432 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8433 section must be the first entry in the set, and the .debug_abbrev.dwo must
8434 be the second entry. Other members of the set may follow in any order. */
8435
8436 /* Create a hash table to map DWO IDs to their CU/TU entry in
8437 .debug_{info,types}.dwo in DWP_FILE.
8438 Returns NULL if there isn't one.
8439 Note: This function processes DWP files only, not DWO files. */
8440
8441 static struct dwp_hash_table *
8442 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8443 {
8444 struct objfile *objfile = dwarf2_per_objfile->objfile;
8445 bfd *dbfd = dwp_file->dbfd;
8446 char *index_ptr, *index_end;
8447 struct dwarf2_section_info *index;
8448 uint32_t version, nr_units, nr_slots;
8449 struct dwp_hash_table *htab;
8450
8451 if (is_debug_types)
8452 index = &dwp_file->sections.tu_index;
8453 else
8454 index = &dwp_file->sections.cu_index;
8455
8456 if (dwarf2_section_empty_p (index))
8457 return NULL;
8458 dwarf2_read_section (objfile, index);
8459
8460 index_ptr = index->buffer;
8461 index_end = index_ptr + index->size;
8462
8463 version = read_4_bytes (dbfd, index_ptr);
8464 index_ptr += 8; /* Skip the unused word. */
8465 nr_units = read_4_bytes (dbfd, index_ptr);
8466 index_ptr += 4;
8467 nr_slots = read_4_bytes (dbfd, index_ptr);
8468 index_ptr += 4;
8469
8470 if (version != 1)
8471 {
8472 error (_("Dwarf Error: unsupported DWP file version (%u)"
8473 " [in module %s]"),
8474 version, dwp_file->name);
8475 }
8476 if (nr_slots != (nr_slots & -nr_slots))
8477 {
8478 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8479 " is not power of 2 [in module %s]"),
8480 nr_slots, dwp_file->name);
8481 }
8482
8483 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8484 htab->nr_units = nr_units;
8485 htab->nr_slots = nr_slots;
8486 htab->hash_table = index_ptr;
8487 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8488 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8489
8490 return htab;
8491 }
8492
8493 /* Update SECTIONS with the data from SECTP.
8494
8495 This function is like the other "locate" section routines that are
8496 passed to bfd_map_over_sections, but in this context the sections to
8497 read comes from the DWP hash table, not the full ELF section table.
8498
8499 The result is non-zero for success, or zero if an error was found. */
8500
8501 static int
8502 locate_virtual_dwo_sections (asection *sectp,
8503 struct virtual_dwo_sections *sections)
8504 {
8505 const struct dwop_section_names *names = &dwop_section_names;
8506
8507 if (section_is_p (sectp->name, &names->abbrev_dwo))
8508 {
8509 /* There can be only one. */
8510 if (sections->abbrev.asection != NULL)
8511 return 0;
8512 sections->abbrev.asection = sectp;
8513 sections->abbrev.size = bfd_get_section_size (sectp);
8514 }
8515 else if (section_is_p (sectp->name, &names->info_dwo)
8516 || section_is_p (sectp->name, &names->types_dwo))
8517 {
8518 /* There can be only one. */
8519 if (sections->info_or_types.asection != NULL)
8520 return 0;
8521 sections->info_or_types.asection = sectp;
8522 sections->info_or_types.size = bfd_get_section_size (sectp);
8523 }
8524 else if (section_is_p (sectp->name, &names->line_dwo))
8525 {
8526 /* There can be only one. */
8527 if (sections->line.asection != NULL)
8528 return 0;
8529 sections->line.asection = sectp;
8530 sections->line.size = bfd_get_section_size (sectp);
8531 }
8532 else if (section_is_p (sectp->name, &names->loc_dwo))
8533 {
8534 /* There can be only one. */
8535 if (sections->loc.asection != NULL)
8536 return 0;
8537 sections->loc.asection = sectp;
8538 sections->loc.size = bfd_get_section_size (sectp);
8539 }
8540 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8541 {
8542 /* There can be only one. */
8543 if (sections->macinfo.asection != NULL)
8544 return 0;
8545 sections->macinfo.asection = sectp;
8546 sections->macinfo.size = bfd_get_section_size (sectp);
8547 }
8548 else if (section_is_p (sectp->name, &names->macro_dwo))
8549 {
8550 /* There can be only one. */
8551 if (sections->macro.asection != NULL)
8552 return 0;
8553 sections->macro.asection = sectp;
8554 sections->macro.size = bfd_get_section_size (sectp);
8555 }
8556 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8557 {
8558 /* There can be only one. */
8559 if (sections->str_offsets.asection != NULL)
8560 return 0;
8561 sections->str_offsets.asection = sectp;
8562 sections->str_offsets.size = bfd_get_section_size (sectp);
8563 }
8564 else
8565 {
8566 /* No other kind of section is valid. */
8567 return 0;
8568 }
8569
8570 return 1;
8571 }
8572
8573 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8574 HTAB is the hash table from the DWP file.
8575 SECTION_INDEX is the index of the DWO in HTAB. */
8576
8577 static struct dwo_unit *
8578 create_dwo_in_dwp (struct dwp_file *dwp_file,
8579 const struct dwp_hash_table *htab,
8580 uint32_t section_index,
8581 ULONGEST signature, int is_debug_types)
8582 {
8583 struct objfile *objfile = dwarf2_per_objfile->objfile;
8584 bfd *dbfd = dwp_file->dbfd;
8585 const char *kind = is_debug_types ? "TU" : "CU";
8586 struct dwo_file *dwo_file;
8587 struct dwo_unit *dwo_unit;
8588 struct virtual_dwo_sections sections;
8589 void **dwo_file_slot;
8590 char *virtual_dwo_name;
8591 struct dwarf2_section_info *cutu;
8592 struct cleanup *cleanups;
8593 int i;
8594
8595 if (dwarf2_read_debug)
8596 {
8597 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8598 kind,
8599 section_index, phex (signature, sizeof (signature)),
8600 dwp_file->name);
8601 }
8602
8603 /* Fetch the sections of this DWO.
8604 Put a limit on the number of sections we look for so that bad data
8605 doesn't cause us to loop forever. */
8606
8607 #define MAX_NR_DWO_SECTIONS \
8608 (1 /* .debug_info or .debug_types */ \
8609 + 1 /* .debug_abbrev */ \
8610 + 1 /* .debug_line */ \
8611 + 1 /* .debug_loc */ \
8612 + 1 /* .debug_str_offsets */ \
8613 + 1 /* .debug_macro */ \
8614 + 1 /* .debug_macinfo */ \
8615 + 1 /* trailing zero */)
8616
8617 memset (&sections, 0, sizeof (sections));
8618 cleanups = make_cleanup (null_cleanup, 0);
8619
8620 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8621 {
8622 asection *sectp;
8623 uint32_t section_nr =
8624 read_4_bytes (dbfd,
8625 htab->section_pool
8626 + (section_index + i) * sizeof (uint32_t));
8627
8628 if (section_nr == 0)
8629 break;
8630 if (section_nr >= dwp_file->num_sections)
8631 {
8632 error (_("Dwarf Error: bad DWP hash table, section number too large"
8633 " [in module %s]"),
8634 dwp_file->name);
8635 }
8636
8637 sectp = dwp_file->elf_sections[section_nr];
8638 if (! locate_virtual_dwo_sections (sectp, &sections))
8639 {
8640 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8641 " [in module %s]"),
8642 dwp_file->name);
8643 }
8644 }
8645
8646 if (i < 2
8647 || sections.info_or_types.asection == NULL
8648 || sections.abbrev.asection == NULL)
8649 {
8650 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8651 " [in module %s]"),
8652 dwp_file->name);
8653 }
8654 if (i == MAX_NR_DWO_SECTIONS)
8655 {
8656 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8657 " [in module %s]"),
8658 dwp_file->name);
8659 }
8660
8661 /* It's easier for the rest of the code if we fake a struct dwo_file and
8662 have dwo_unit "live" in that. At least for now.
8663
8664 The DWP file can be made up of a random collection of CUs and TUs.
8665 However, for each CU + set of TUs that came from the same original DWO
8666 file, we want to combine them back into a virtual DWO file to save space
8667 (fewer struct dwo_file objects to allocated). Remember that for really
8668 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8669
8670 virtual_dwo_name =
8671 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8672 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8673 sections.line.asection ? sections.line.asection->id : 0,
8674 sections.loc.asection ? sections.loc.asection->id : 0,
8675 (sections.str_offsets.asection
8676 ? sections.str_offsets.asection->id
8677 : 0));
8678 make_cleanup (xfree, virtual_dwo_name);
8679 /* Can we use an existing virtual DWO file? */
8680 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8681 /* Create one if necessary. */
8682 if (*dwo_file_slot == NULL)
8683 {
8684 if (dwarf2_read_debug)
8685 {
8686 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8687 virtual_dwo_name);
8688 }
8689 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8690 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8691 virtual_dwo_name,
8692 strlen (virtual_dwo_name));
8693 dwo_file->sections.abbrev = sections.abbrev;
8694 dwo_file->sections.line = sections.line;
8695 dwo_file->sections.loc = sections.loc;
8696 dwo_file->sections.macinfo = sections.macinfo;
8697 dwo_file->sections.macro = sections.macro;
8698 dwo_file->sections.str_offsets = sections.str_offsets;
8699 /* The "str" section is global to the entire DWP file. */
8700 dwo_file->sections.str = dwp_file->sections.str;
8701 /* The info or types section is assigned later to dwo_unit,
8702 there's no need to record it in dwo_file.
8703 Also, we can't simply record type sections in dwo_file because
8704 we record a pointer into the vector in dwo_unit. As we collect more
8705 types we'll grow the vector and eventually have to reallocate space
8706 for it, invalidating all the pointers into the current copy. */
8707 *dwo_file_slot = dwo_file;
8708 }
8709 else
8710 {
8711 if (dwarf2_read_debug)
8712 {
8713 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8714 virtual_dwo_name);
8715 }
8716 dwo_file = *dwo_file_slot;
8717 }
8718 do_cleanups (cleanups);
8719
8720 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8721 dwo_unit->dwo_file = dwo_file;
8722 dwo_unit->signature = signature;
8723 dwo_unit->info_or_types_section =
8724 obstack_alloc (&objfile->objfile_obstack,
8725 sizeof (struct dwarf2_section_info));
8726 *dwo_unit->info_or_types_section = sections.info_or_types;
8727 /* offset, length, type_offset_in_tu are set later. */
8728
8729 return dwo_unit;
8730 }
8731
8732 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8733
8734 static struct dwo_unit *
8735 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8736 const struct dwp_hash_table *htab,
8737 ULONGEST signature, int is_debug_types)
8738 {
8739 bfd *dbfd = dwp_file->dbfd;
8740 uint32_t mask = htab->nr_slots - 1;
8741 uint32_t hash = signature & mask;
8742 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8743 unsigned int i;
8744 void **slot;
8745 struct dwo_unit find_dwo_cu, *dwo_cu;
8746
8747 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8748 find_dwo_cu.signature = signature;
8749 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8750
8751 if (*slot != NULL)
8752 return *slot;
8753
8754 /* Use a for loop so that we don't loop forever on bad debug info. */
8755 for (i = 0; i < htab->nr_slots; ++i)
8756 {
8757 ULONGEST signature_in_table;
8758
8759 signature_in_table =
8760 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8761 if (signature_in_table == signature)
8762 {
8763 uint32_t section_index =
8764 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8765
8766 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8767 signature, is_debug_types);
8768 return *slot;
8769 }
8770 if (signature_in_table == 0)
8771 return NULL;
8772 hash = (hash + hash2) & mask;
8773 }
8774
8775 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8776 " [in module %s]"),
8777 dwp_file->name);
8778 }
8779
8780 /* Subroutine of open_dwop_file to simplify it.
8781 Open the file specified by FILE_NAME and hand it off to BFD for
8782 preliminary analysis. Return a newly initialized bfd *, which
8783 includes a canonicalized copy of FILE_NAME.
8784 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8785 In case of trouble, return NULL.
8786 NOTE: This function is derived from symfile_bfd_open. */
8787
8788 static bfd *
8789 try_open_dwop_file (const char *file_name, int is_dwp)
8790 {
8791 bfd *sym_bfd;
8792 int desc, flags;
8793 char *absolute_name;
8794
8795 flags = OPF_TRY_CWD_FIRST;
8796 if (is_dwp)
8797 flags |= OPF_SEARCH_IN_PATH;
8798 desc = openp (debug_file_directory, flags, file_name,
8799 O_RDONLY | O_BINARY, &absolute_name);
8800 if (desc < 0)
8801 return NULL;
8802
8803 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8804 if (!sym_bfd)
8805 {
8806 xfree (absolute_name);
8807 return NULL;
8808 }
8809 xfree (absolute_name);
8810 bfd_set_cacheable (sym_bfd, 1);
8811
8812 if (!bfd_check_format (sym_bfd, bfd_object))
8813 {
8814 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8815 return NULL;
8816 }
8817
8818 return sym_bfd;
8819 }
8820
8821 /* Try to open DWO/DWP file FILE_NAME.
8822 COMP_DIR is the DW_AT_comp_dir attribute.
8823 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8824 The result is the bfd handle of the file.
8825 If there is a problem finding or opening the file, return NULL.
8826 Upon success, the canonicalized path of the file is stored in the bfd,
8827 same as symfile_bfd_open. */
8828
8829 static bfd *
8830 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8831 {
8832 bfd *abfd;
8833
8834 if (IS_ABSOLUTE_PATH (file_name))
8835 return try_open_dwop_file (file_name, is_dwp);
8836
8837 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8838
8839 if (comp_dir != NULL)
8840 {
8841 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8842
8843 /* NOTE: If comp_dir is a relative path, this will also try the
8844 search path, which seems useful. */
8845 abfd = try_open_dwop_file (path_to_try, is_dwp);
8846 xfree (path_to_try);
8847 if (abfd != NULL)
8848 return abfd;
8849 }
8850
8851 /* That didn't work, try debug-file-directory, which, despite its name,
8852 is a list of paths. */
8853
8854 if (*debug_file_directory == '\0')
8855 return NULL;
8856
8857 return try_open_dwop_file (file_name, is_dwp);
8858 }
8859
8860 /* This function is mapped across the sections and remembers the offset and
8861 size of each of the DWO debugging sections we are interested in. */
8862
8863 static void
8864 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8865 {
8866 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8867 const struct dwop_section_names *names = &dwop_section_names;
8868
8869 if (section_is_p (sectp->name, &names->abbrev_dwo))
8870 {
8871 dwo_sections->abbrev.asection = sectp;
8872 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8873 }
8874 else if (section_is_p (sectp->name, &names->info_dwo))
8875 {
8876 dwo_sections->info.asection = sectp;
8877 dwo_sections->info.size = bfd_get_section_size (sectp);
8878 }
8879 else if (section_is_p (sectp->name, &names->line_dwo))
8880 {
8881 dwo_sections->line.asection = sectp;
8882 dwo_sections->line.size = bfd_get_section_size (sectp);
8883 }
8884 else if (section_is_p (sectp->name, &names->loc_dwo))
8885 {
8886 dwo_sections->loc.asection = sectp;
8887 dwo_sections->loc.size = bfd_get_section_size (sectp);
8888 }
8889 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8890 {
8891 dwo_sections->macinfo.asection = sectp;
8892 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8893 }
8894 else if (section_is_p (sectp->name, &names->macro_dwo))
8895 {
8896 dwo_sections->macro.asection = sectp;
8897 dwo_sections->macro.size = bfd_get_section_size (sectp);
8898 }
8899 else if (section_is_p (sectp->name, &names->str_dwo))
8900 {
8901 dwo_sections->str.asection = sectp;
8902 dwo_sections->str.size = bfd_get_section_size (sectp);
8903 }
8904 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8905 {
8906 dwo_sections->str_offsets.asection = sectp;
8907 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8908 }
8909 else if (section_is_p (sectp->name, &names->types_dwo))
8910 {
8911 struct dwarf2_section_info type_section;
8912
8913 memset (&type_section, 0, sizeof (type_section));
8914 type_section.asection = sectp;
8915 type_section.size = bfd_get_section_size (sectp);
8916 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8917 &type_section);
8918 }
8919 }
8920
8921 /* Initialize the use of the DWO file specified by DWO_NAME.
8922 The result is NULL if DWO_NAME can't be found. */
8923
8924 static struct dwo_file *
8925 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8926 {
8927 struct objfile *objfile = dwarf2_per_objfile->objfile;
8928 struct dwo_file *dwo_file;
8929 bfd *dbfd;
8930 struct cleanup *cleanups;
8931
8932 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8933 if (dbfd == NULL)
8934 {
8935 if (dwarf2_read_debug)
8936 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8937 return NULL;
8938 }
8939 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8940 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8941 dwo_name, strlen (dwo_name));
8942 dwo_file->dbfd = dbfd;
8943
8944 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8945
8946 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8947
8948 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8949
8950 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8951 dwo_file->sections.types);
8952
8953 discard_cleanups (cleanups);
8954
8955 if (dwarf2_read_debug)
8956 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8957
8958 return dwo_file;
8959 }
8960
8961 /* This function is mapped across the sections and remembers the offset and
8962 size of each of the DWP debugging sections we are interested in. */
8963
8964 static void
8965 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
8966 {
8967 struct dwp_file *dwp_file = dwp_file_ptr;
8968 const struct dwop_section_names *names = &dwop_section_names;
8969 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8970
8971 /* Record the ELF section number for later lookup: this is what the
8972 .debug_cu_index,.debug_tu_index tables use. */
8973 gdb_assert (elf_section_nr < dwp_file->num_sections);
8974 dwp_file->elf_sections[elf_section_nr] = sectp;
8975
8976 /* Look for specific sections that we need. */
8977 if (section_is_p (sectp->name, &names->str_dwo))
8978 {
8979 dwp_file->sections.str.asection = sectp;
8980 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8981 }
8982 else if (section_is_p (sectp->name, &names->cu_index))
8983 {
8984 dwp_file->sections.cu_index.asection = sectp;
8985 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8986 }
8987 else if (section_is_p (sectp->name, &names->tu_index))
8988 {
8989 dwp_file->sections.tu_index.asection = sectp;
8990 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8991 }
8992 }
8993
8994 /* Hash function for dwp_file loaded CUs/TUs. */
8995
8996 static hashval_t
8997 hash_dwp_loaded_cutus (const void *item)
8998 {
8999 const struct dwo_unit *dwo_unit = item;
9000
9001 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9002 return dwo_unit->signature;
9003 }
9004
9005 /* Equality function for dwp_file loaded CUs/TUs. */
9006
9007 static int
9008 eq_dwp_loaded_cutus (const void *a, const void *b)
9009 {
9010 const struct dwo_unit *dua = a;
9011 const struct dwo_unit *dub = b;
9012
9013 return dua->signature == dub->signature;
9014 }
9015
9016 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9017
9018 static htab_t
9019 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9020 {
9021 return htab_create_alloc_ex (3,
9022 hash_dwp_loaded_cutus,
9023 eq_dwp_loaded_cutus,
9024 NULL,
9025 &objfile->objfile_obstack,
9026 hashtab_obstack_allocate,
9027 dummy_obstack_deallocate);
9028 }
9029
9030 /* Initialize the use of the DWP file for the current objfile.
9031 By convention the name of the DWP file is ${objfile}.dwp.
9032 The result is NULL if it can't be found. */
9033
9034 static struct dwp_file *
9035 open_and_init_dwp_file (const char *comp_dir)
9036 {
9037 struct objfile *objfile = dwarf2_per_objfile->objfile;
9038 struct dwp_file *dwp_file;
9039 char *dwp_name;
9040 bfd *dbfd;
9041 struct cleanup *cleanups;
9042
9043 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9044 cleanups = make_cleanup (xfree, dwp_name);
9045
9046 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9047 if (dbfd == NULL)
9048 {
9049 if (dwarf2_read_debug)
9050 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9051 do_cleanups (cleanups);
9052 return NULL;
9053 }
9054 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9055 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9056 dwp_name, strlen (dwp_name));
9057 dwp_file->dbfd = dbfd;
9058 do_cleanups (cleanups);
9059
9060 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9061
9062 /* +1: section 0 is unused */
9063 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9064 dwp_file->elf_sections =
9065 OBSTACK_CALLOC (&objfile->objfile_obstack,
9066 dwp_file->num_sections, asection *);
9067
9068 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9069
9070 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9071
9072 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9073
9074 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9075
9076 discard_cleanups (cleanups);
9077
9078 if (dwarf2_read_debug)
9079 {
9080 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9081 fprintf_unfiltered (gdb_stdlog,
9082 " %u CUs, %u TUs\n",
9083 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9084 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9085 }
9086
9087 return dwp_file;
9088 }
9089
9090 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9091 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9092 or in the DWP file for the objfile, referenced by THIS_UNIT.
9093 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9094 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9095
9096 This is called, for example, when wanting to read a variable with a
9097 complex location. Therefore we don't want to do file i/o for every call.
9098 Therefore we don't want to look for a DWO file on every call.
9099 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9100 then we check if we've already seen DWO_NAME, and only THEN do we check
9101 for a DWO file.
9102
9103 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9104 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9105
9106 static struct dwo_unit *
9107 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9108 const char *dwo_name, const char *comp_dir,
9109 ULONGEST signature, int is_debug_types)
9110 {
9111 struct objfile *objfile = dwarf2_per_objfile->objfile;
9112 const char *kind = is_debug_types ? "TU" : "CU";
9113 void **dwo_file_slot;
9114 struct dwo_file *dwo_file;
9115 struct dwp_file *dwp_file;
9116
9117 /* Have we already read SIGNATURE from a DWP file? */
9118
9119 if (! dwarf2_per_objfile->dwp_checked)
9120 {
9121 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9122 dwarf2_per_objfile->dwp_checked = 1;
9123 }
9124 dwp_file = dwarf2_per_objfile->dwp_file;
9125
9126 if (dwp_file != NULL)
9127 {
9128 const struct dwp_hash_table *dwp_htab =
9129 is_debug_types ? dwp_file->tus : dwp_file->cus;
9130
9131 if (dwp_htab != NULL)
9132 {
9133 struct dwo_unit *dwo_cutu =
9134 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9135
9136 if (dwo_cutu != NULL)
9137 {
9138 if (dwarf2_read_debug)
9139 {
9140 fprintf_unfiltered (gdb_stdlog,
9141 "Virtual DWO %s %s found: @%s\n",
9142 kind, hex_string (signature),
9143 host_address_to_string (dwo_cutu));
9144 }
9145 return dwo_cutu;
9146 }
9147 }
9148 }
9149
9150 /* Have we already seen DWO_NAME? */
9151
9152 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9153 if (*dwo_file_slot == NULL)
9154 {
9155 /* Read in the file and build a table of the DWOs it contains. */
9156 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9157 }
9158 /* NOTE: This will be NULL if unable to open the file. */
9159 dwo_file = *dwo_file_slot;
9160
9161 if (dwo_file != NULL)
9162 {
9163 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9164
9165 if (htab != NULL)
9166 {
9167 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9168
9169 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9170 find_dwo_cutu.signature = signature;
9171 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9172
9173 if (dwo_cutu != NULL)
9174 {
9175 if (dwarf2_read_debug)
9176 {
9177 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9178 kind, dwo_name, hex_string (signature),
9179 host_address_to_string (dwo_cutu));
9180 }
9181 return dwo_cutu;
9182 }
9183 }
9184 }
9185
9186 /* We didn't find it. This could mean a dwo_id mismatch, or
9187 someone deleted the DWO/DWP file, or the search path isn't set up
9188 correctly to find the file. */
9189
9190 if (dwarf2_read_debug)
9191 {
9192 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9193 kind, dwo_name, hex_string (signature));
9194 }
9195
9196 complaint (&symfile_complaints,
9197 _("Could not find DWO CU referenced by CU at offset 0x%x"
9198 " [in module %s]"),
9199 this_unit->offset.sect_off, objfile->name);
9200 return NULL;
9201 }
9202
9203 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9204 See lookup_dwo_cutu_unit for details. */
9205
9206 static struct dwo_unit *
9207 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9208 const char *dwo_name, const char *comp_dir,
9209 ULONGEST signature)
9210 {
9211 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9212 }
9213
9214 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9215 See lookup_dwo_cutu_unit for details. */
9216
9217 static struct dwo_unit *
9218 lookup_dwo_type_unit (struct signatured_type *this_tu,
9219 const char *dwo_name, const char *comp_dir)
9220 {
9221 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9222 }
9223
9224 /* Free all resources associated with DWO_FILE.
9225 Close the DWO file and munmap the sections.
9226 All memory should be on the objfile obstack. */
9227
9228 static void
9229 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9230 {
9231 int ix;
9232 struct dwarf2_section_info *section;
9233
9234 gdb_bfd_unref (dwo_file->dbfd);
9235
9236 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9237 }
9238
9239 /* Wrapper for free_dwo_file for use in cleanups. */
9240
9241 static void
9242 free_dwo_file_cleanup (void *arg)
9243 {
9244 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9245 struct objfile *objfile = dwarf2_per_objfile->objfile;
9246
9247 free_dwo_file (dwo_file, objfile);
9248 }
9249
9250 /* Traversal function for free_dwo_files. */
9251
9252 static int
9253 free_dwo_file_from_slot (void **slot, void *info)
9254 {
9255 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9256 struct objfile *objfile = (struct objfile *) info;
9257
9258 free_dwo_file (dwo_file, objfile);
9259
9260 return 1;
9261 }
9262
9263 /* Free all resources associated with DWO_FILES. */
9264
9265 static void
9266 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9267 {
9268 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9269 }
9270 \f
9271 /* Read in various DIEs. */
9272
9273 /* qsort helper for inherit_abstract_dies. */
9274
9275 static int
9276 unsigned_int_compar (const void *ap, const void *bp)
9277 {
9278 unsigned int a = *(unsigned int *) ap;
9279 unsigned int b = *(unsigned int *) bp;
9280
9281 return (a > b) - (b > a);
9282 }
9283
9284 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9285 Inherit only the children of the DW_AT_abstract_origin DIE not being
9286 already referenced by DW_AT_abstract_origin from the children of the
9287 current DIE. */
9288
9289 static void
9290 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9291 {
9292 struct die_info *child_die;
9293 unsigned die_children_count;
9294 /* CU offsets which were referenced by children of the current DIE. */
9295 sect_offset *offsets;
9296 sect_offset *offsets_end, *offsetp;
9297 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9298 struct die_info *origin_die;
9299 /* Iterator of the ORIGIN_DIE children. */
9300 struct die_info *origin_child_die;
9301 struct cleanup *cleanups;
9302 struct attribute *attr;
9303 struct dwarf2_cu *origin_cu;
9304 struct pending **origin_previous_list_in_scope;
9305
9306 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9307 if (!attr)
9308 return;
9309
9310 /* Note that following die references may follow to a die in a
9311 different cu. */
9312
9313 origin_cu = cu;
9314 origin_die = follow_die_ref (die, attr, &origin_cu);
9315
9316 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9317 symbols in. */
9318 origin_previous_list_in_scope = origin_cu->list_in_scope;
9319 origin_cu->list_in_scope = cu->list_in_scope;
9320
9321 if (die->tag != origin_die->tag
9322 && !(die->tag == DW_TAG_inlined_subroutine
9323 && origin_die->tag == DW_TAG_subprogram))
9324 complaint (&symfile_complaints,
9325 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9326 die->offset.sect_off, origin_die->offset.sect_off);
9327
9328 child_die = die->child;
9329 die_children_count = 0;
9330 while (child_die && child_die->tag)
9331 {
9332 child_die = sibling_die (child_die);
9333 die_children_count++;
9334 }
9335 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9336 cleanups = make_cleanup (xfree, offsets);
9337
9338 offsets_end = offsets;
9339 child_die = die->child;
9340 while (child_die && child_die->tag)
9341 {
9342 /* For each CHILD_DIE, find the corresponding child of
9343 ORIGIN_DIE. If there is more than one layer of
9344 DW_AT_abstract_origin, follow them all; there shouldn't be,
9345 but GCC versions at least through 4.4 generate this (GCC PR
9346 40573). */
9347 struct die_info *child_origin_die = child_die;
9348 struct dwarf2_cu *child_origin_cu = cu;
9349
9350 while (1)
9351 {
9352 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9353 child_origin_cu);
9354 if (attr == NULL)
9355 break;
9356 child_origin_die = follow_die_ref (child_origin_die, attr,
9357 &child_origin_cu);
9358 }
9359
9360 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9361 counterpart may exist. */
9362 if (child_origin_die != child_die)
9363 {
9364 if (child_die->tag != child_origin_die->tag
9365 && !(child_die->tag == DW_TAG_inlined_subroutine
9366 && child_origin_die->tag == DW_TAG_subprogram))
9367 complaint (&symfile_complaints,
9368 _("Child DIE 0x%x and its abstract origin 0x%x have "
9369 "different tags"), child_die->offset.sect_off,
9370 child_origin_die->offset.sect_off);
9371 if (child_origin_die->parent != origin_die)
9372 complaint (&symfile_complaints,
9373 _("Child DIE 0x%x and its abstract origin 0x%x have "
9374 "different parents"), child_die->offset.sect_off,
9375 child_origin_die->offset.sect_off);
9376 else
9377 *offsets_end++ = child_origin_die->offset;
9378 }
9379 child_die = sibling_die (child_die);
9380 }
9381 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9382 unsigned_int_compar);
9383 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9384 if (offsetp[-1].sect_off == offsetp->sect_off)
9385 complaint (&symfile_complaints,
9386 _("Multiple children of DIE 0x%x refer "
9387 "to DIE 0x%x as their abstract origin"),
9388 die->offset.sect_off, offsetp->sect_off);
9389
9390 offsetp = offsets;
9391 origin_child_die = origin_die->child;
9392 while (origin_child_die && origin_child_die->tag)
9393 {
9394 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9395 while (offsetp < offsets_end
9396 && offsetp->sect_off < origin_child_die->offset.sect_off)
9397 offsetp++;
9398 if (offsetp >= offsets_end
9399 || offsetp->sect_off > origin_child_die->offset.sect_off)
9400 {
9401 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9402 process_die (origin_child_die, origin_cu);
9403 }
9404 origin_child_die = sibling_die (origin_child_die);
9405 }
9406 origin_cu->list_in_scope = origin_previous_list_in_scope;
9407
9408 do_cleanups (cleanups);
9409 }
9410
9411 static void
9412 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9413 {
9414 struct objfile *objfile = cu->objfile;
9415 struct context_stack *new;
9416 CORE_ADDR lowpc;
9417 CORE_ADDR highpc;
9418 struct die_info *child_die;
9419 struct attribute *attr, *call_line, *call_file;
9420 const char *name;
9421 CORE_ADDR baseaddr;
9422 struct block *block;
9423 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9424 VEC (symbolp) *template_args = NULL;
9425 struct template_symbol *templ_func = NULL;
9426
9427 if (inlined_func)
9428 {
9429 /* If we do not have call site information, we can't show the
9430 caller of this inlined function. That's too confusing, so
9431 only use the scope for local variables. */
9432 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9433 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9434 if (call_line == NULL || call_file == NULL)
9435 {
9436 read_lexical_block_scope (die, cu);
9437 return;
9438 }
9439 }
9440
9441 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9442
9443 name = dwarf2_name (die, cu);
9444
9445 /* Ignore functions with missing or empty names. These are actually
9446 illegal according to the DWARF standard. */
9447 if (name == NULL)
9448 {
9449 complaint (&symfile_complaints,
9450 _("missing name for subprogram DIE at %d"),
9451 die->offset.sect_off);
9452 return;
9453 }
9454
9455 /* Ignore functions with missing or invalid low and high pc attributes. */
9456 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9457 {
9458 attr = dwarf2_attr (die, DW_AT_external, cu);
9459 if (!attr || !DW_UNSND (attr))
9460 complaint (&symfile_complaints,
9461 _("cannot get low and high bounds "
9462 "for subprogram DIE at %d"),
9463 die->offset.sect_off);
9464 return;
9465 }
9466
9467 lowpc += baseaddr;
9468 highpc += baseaddr;
9469
9470 /* If we have any template arguments, then we must allocate a
9471 different sort of symbol. */
9472 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9473 {
9474 if (child_die->tag == DW_TAG_template_type_param
9475 || child_die->tag == DW_TAG_template_value_param)
9476 {
9477 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9478 struct template_symbol);
9479 templ_func->base.is_cplus_template_function = 1;
9480 break;
9481 }
9482 }
9483
9484 new = push_context (0, lowpc);
9485 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9486 (struct symbol *) templ_func);
9487
9488 /* If there is a location expression for DW_AT_frame_base, record
9489 it. */
9490 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9491 if (attr)
9492 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9493 expression is being recorded directly in the function's symbol
9494 and not in a separate frame-base object. I guess this hack is
9495 to avoid adding some sort of frame-base adjunct/annex to the
9496 function's symbol :-(. The problem with doing this is that it
9497 results in a function symbol with a location expression that
9498 has nothing to do with the location of the function, ouch! The
9499 relationship should be: a function's symbol has-a frame base; a
9500 frame-base has-a location expression. */
9501 dwarf2_symbol_mark_computed (attr, new->name, cu);
9502
9503 cu->list_in_scope = &local_symbols;
9504
9505 if (die->child != NULL)
9506 {
9507 child_die = die->child;
9508 while (child_die && child_die->tag)
9509 {
9510 if (child_die->tag == DW_TAG_template_type_param
9511 || child_die->tag == DW_TAG_template_value_param)
9512 {
9513 struct symbol *arg = new_symbol (child_die, NULL, cu);
9514
9515 if (arg != NULL)
9516 VEC_safe_push (symbolp, template_args, arg);
9517 }
9518 else
9519 process_die (child_die, cu);
9520 child_die = sibling_die (child_die);
9521 }
9522 }
9523
9524 inherit_abstract_dies (die, cu);
9525
9526 /* If we have a DW_AT_specification, we might need to import using
9527 directives from the context of the specification DIE. See the
9528 comment in determine_prefix. */
9529 if (cu->language == language_cplus
9530 && dwarf2_attr (die, DW_AT_specification, cu))
9531 {
9532 struct dwarf2_cu *spec_cu = cu;
9533 struct die_info *spec_die = die_specification (die, &spec_cu);
9534
9535 while (spec_die)
9536 {
9537 child_die = spec_die->child;
9538 while (child_die && child_die->tag)
9539 {
9540 if (child_die->tag == DW_TAG_imported_module)
9541 process_die (child_die, spec_cu);
9542 child_die = sibling_die (child_die);
9543 }
9544
9545 /* In some cases, GCC generates specification DIEs that
9546 themselves contain DW_AT_specification attributes. */
9547 spec_die = die_specification (spec_die, &spec_cu);
9548 }
9549 }
9550
9551 new = pop_context ();
9552 /* Make a block for the local symbols within. */
9553 block = finish_block (new->name, &local_symbols, new->old_blocks,
9554 lowpc, highpc, objfile);
9555
9556 /* For C++, set the block's scope. */
9557 if ((cu->language == language_cplus || cu->language == language_fortran)
9558 && cu->processing_has_namespace_info)
9559 block_set_scope (block, determine_prefix (die, cu),
9560 &objfile->objfile_obstack);
9561
9562 /* If we have address ranges, record them. */
9563 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9564
9565 /* Attach template arguments to function. */
9566 if (! VEC_empty (symbolp, template_args))
9567 {
9568 gdb_assert (templ_func != NULL);
9569
9570 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9571 templ_func->template_arguments
9572 = obstack_alloc (&objfile->objfile_obstack,
9573 (templ_func->n_template_arguments
9574 * sizeof (struct symbol *)));
9575 memcpy (templ_func->template_arguments,
9576 VEC_address (symbolp, template_args),
9577 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9578 VEC_free (symbolp, template_args);
9579 }
9580
9581 /* In C++, we can have functions nested inside functions (e.g., when
9582 a function declares a class that has methods). This means that
9583 when we finish processing a function scope, we may need to go
9584 back to building a containing block's symbol lists. */
9585 local_symbols = new->locals;
9586 using_directives = new->using_directives;
9587
9588 /* If we've finished processing a top-level function, subsequent
9589 symbols go in the file symbol list. */
9590 if (outermost_context_p ())
9591 cu->list_in_scope = &file_symbols;
9592 }
9593
9594 /* Process all the DIES contained within a lexical block scope. Start
9595 a new scope, process the dies, and then close the scope. */
9596
9597 static void
9598 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9599 {
9600 struct objfile *objfile = cu->objfile;
9601 struct context_stack *new;
9602 CORE_ADDR lowpc, highpc;
9603 struct die_info *child_die;
9604 CORE_ADDR baseaddr;
9605
9606 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9607
9608 /* Ignore blocks with missing or invalid low and high pc attributes. */
9609 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9610 as multiple lexical blocks? Handling children in a sane way would
9611 be nasty. Might be easier to properly extend generic blocks to
9612 describe ranges. */
9613 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9614 return;
9615 lowpc += baseaddr;
9616 highpc += baseaddr;
9617
9618 push_context (0, lowpc);
9619 if (die->child != NULL)
9620 {
9621 child_die = die->child;
9622 while (child_die && child_die->tag)
9623 {
9624 process_die (child_die, cu);
9625 child_die = sibling_die (child_die);
9626 }
9627 }
9628 new = pop_context ();
9629
9630 if (local_symbols != NULL || using_directives != NULL)
9631 {
9632 struct block *block
9633 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9634 highpc, objfile);
9635
9636 /* Note that recording ranges after traversing children, as we
9637 do here, means that recording a parent's ranges entails
9638 walking across all its children's ranges as they appear in
9639 the address map, which is quadratic behavior.
9640
9641 It would be nicer to record the parent's ranges before
9642 traversing its children, simply overriding whatever you find
9643 there. But since we don't even decide whether to create a
9644 block until after we've traversed its children, that's hard
9645 to do. */
9646 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9647 }
9648 local_symbols = new->locals;
9649 using_directives = new->using_directives;
9650 }
9651
9652 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9653
9654 static void
9655 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9656 {
9657 struct objfile *objfile = cu->objfile;
9658 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9659 CORE_ADDR pc, baseaddr;
9660 struct attribute *attr;
9661 struct call_site *call_site, call_site_local;
9662 void **slot;
9663 int nparams;
9664 struct die_info *child_die;
9665
9666 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9667
9668 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9669 if (!attr)
9670 {
9671 complaint (&symfile_complaints,
9672 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9673 "DIE 0x%x [in module %s]"),
9674 die->offset.sect_off, objfile->name);
9675 return;
9676 }
9677 pc = DW_ADDR (attr) + baseaddr;
9678
9679 if (cu->call_site_htab == NULL)
9680 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9681 NULL, &objfile->objfile_obstack,
9682 hashtab_obstack_allocate, NULL);
9683 call_site_local.pc = pc;
9684 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9685 if (*slot != NULL)
9686 {
9687 complaint (&symfile_complaints,
9688 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9689 "DIE 0x%x [in module %s]"),
9690 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9691 return;
9692 }
9693
9694 /* Count parameters at the caller. */
9695
9696 nparams = 0;
9697 for (child_die = die->child; child_die && child_die->tag;
9698 child_die = sibling_die (child_die))
9699 {
9700 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9701 {
9702 complaint (&symfile_complaints,
9703 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9704 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9705 child_die->tag, child_die->offset.sect_off, objfile->name);
9706 continue;
9707 }
9708
9709 nparams++;
9710 }
9711
9712 call_site = obstack_alloc (&objfile->objfile_obstack,
9713 (sizeof (*call_site)
9714 + (sizeof (*call_site->parameter)
9715 * (nparams - 1))));
9716 *slot = call_site;
9717 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9718 call_site->pc = pc;
9719
9720 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9721 {
9722 struct die_info *func_die;
9723
9724 /* Skip also over DW_TAG_inlined_subroutine. */
9725 for (func_die = die->parent;
9726 func_die && func_die->tag != DW_TAG_subprogram
9727 && func_die->tag != DW_TAG_subroutine_type;
9728 func_die = func_die->parent);
9729
9730 /* DW_AT_GNU_all_call_sites is a superset
9731 of DW_AT_GNU_all_tail_call_sites. */
9732 if (func_die
9733 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9734 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9735 {
9736 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9737 not complete. But keep CALL_SITE for look ups via call_site_htab,
9738 both the initial caller containing the real return address PC and
9739 the final callee containing the current PC of a chain of tail
9740 calls do not need to have the tail call list complete. But any
9741 function candidate for a virtual tail call frame searched via
9742 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9743 determined unambiguously. */
9744 }
9745 else
9746 {
9747 struct type *func_type = NULL;
9748
9749 if (func_die)
9750 func_type = get_die_type (func_die, cu);
9751 if (func_type != NULL)
9752 {
9753 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9754
9755 /* Enlist this call site to the function. */
9756 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9757 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9758 }
9759 else
9760 complaint (&symfile_complaints,
9761 _("Cannot find function owning DW_TAG_GNU_call_site "
9762 "DIE 0x%x [in module %s]"),
9763 die->offset.sect_off, objfile->name);
9764 }
9765 }
9766
9767 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9768 if (attr == NULL)
9769 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9770 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9771 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9772 /* Keep NULL DWARF_BLOCK. */;
9773 else if (attr_form_is_block (attr))
9774 {
9775 struct dwarf2_locexpr_baton *dlbaton;
9776
9777 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9778 dlbaton->data = DW_BLOCK (attr)->data;
9779 dlbaton->size = DW_BLOCK (attr)->size;
9780 dlbaton->per_cu = cu->per_cu;
9781
9782 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9783 }
9784 else if (is_ref_attr (attr))
9785 {
9786 struct dwarf2_cu *target_cu = cu;
9787 struct die_info *target_die;
9788
9789 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9790 gdb_assert (target_cu->objfile == objfile);
9791 if (die_is_declaration (target_die, target_cu))
9792 {
9793 const char *target_physname;
9794
9795 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9796 if (target_physname == NULL)
9797 complaint (&symfile_complaints,
9798 _("DW_AT_GNU_call_site_target target DIE has invalid "
9799 "physname, for referencing DIE 0x%x [in module %s]"),
9800 die->offset.sect_off, objfile->name);
9801 else
9802 SET_FIELD_PHYSNAME (call_site->target, target_physname);
9803 }
9804 else
9805 {
9806 CORE_ADDR lowpc;
9807
9808 /* DW_AT_entry_pc should be preferred. */
9809 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9810 complaint (&symfile_complaints,
9811 _("DW_AT_GNU_call_site_target target DIE has invalid "
9812 "low pc, for referencing DIE 0x%x [in module %s]"),
9813 die->offset.sect_off, objfile->name);
9814 else
9815 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9816 }
9817 }
9818 else
9819 complaint (&symfile_complaints,
9820 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9821 "block nor reference, for DIE 0x%x [in module %s]"),
9822 die->offset.sect_off, objfile->name);
9823
9824 call_site->per_cu = cu->per_cu;
9825
9826 for (child_die = die->child;
9827 child_die && child_die->tag;
9828 child_die = sibling_die (child_die))
9829 {
9830 struct call_site_parameter *parameter;
9831 struct attribute *loc, *origin;
9832
9833 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9834 {
9835 /* Already printed the complaint above. */
9836 continue;
9837 }
9838
9839 gdb_assert (call_site->parameter_count < nparams);
9840 parameter = &call_site->parameter[call_site->parameter_count];
9841
9842 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9843 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9844 register is contained in DW_AT_GNU_call_site_value. */
9845
9846 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9847 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9848 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9849 {
9850 sect_offset offset;
9851
9852 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9853 offset = dwarf2_get_ref_die_offset (origin);
9854 if (!offset_in_cu_p (&cu->header, offset))
9855 {
9856 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9857 binding can be done only inside one CU. Such referenced DIE
9858 therefore cannot be even moved to DW_TAG_partial_unit. */
9859 complaint (&symfile_complaints,
9860 _("DW_AT_abstract_origin offset is not in CU for "
9861 "DW_TAG_GNU_call_site child DIE 0x%x "
9862 "[in module %s]"),
9863 child_die->offset.sect_off, objfile->name);
9864 continue;
9865 }
9866 parameter->u.param_offset.cu_off = (offset.sect_off
9867 - cu->header.offset.sect_off);
9868 }
9869 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9870 {
9871 complaint (&symfile_complaints,
9872 _("No DW_FORM_block* DW_AT_location for "
9873 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9874 child_die->offset.sect_off, objfile->name);
9875 continue;
9876 }
9877 else
9878 {
9879 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9880 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9881 if (parameter->u.dwarf_reg != -1)
9882 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9883 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9884 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9885 &parameter->u.fb_offset))
9886 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9887 else
9888 {
9889 complaint (&symfile_complaints,
9890 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9891 "for DW_FORM_block* DW_AT_location is supported for "
9892 "DW_TAG_GNU_call_site child DIE 0x%x "
9893 "[in module %s]"),
9894 child_die->offset.sect_off, objfile->name);
9895 continue;
9896 }
9897 }
9898
9899 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9900 if (!attr_form_is_block (attr))
9901 {
9902 complaint (&symfile_complaints,
9903 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9904 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9905 child_die->offset.sect_off, objfile->name);
9906 continue;
9907 }
9908 parameter->value = DW_BLOCK (attr)->data;
9909 parameter->value_size = DW_BLOCK (attr)->size;
9910
9911 /* Parameters are not pre-cleared by memset above. */
9912 parameter->data_value = NULL;
9913 parameter->data_value_size = 0;
9914 call_site->parameter_count++;
9915
9916 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9917 if (attr)
9918 {
9919 if (!attr_form_is_block (attr))
9920 complaint (&symfile_complaints,
9921 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9922 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9923 child_die->offset.sect_off, objfile->name);
9924 else
9925 {
9926 parameter->data_value = DW_BLOCK (attr)->data;
9927 parameter->data_value_size = DW_BLOCK (attr)->size;
9928 }
9929 }
9930 }
9931 }
9932
9933 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9934 Return 1 if the attributes are present and valid, otherwise, return 0.
9935 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9936
9937 static int
9938 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9939 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9940 struct partial_symtab *ranges_pst)
9941 {
9942 struct objfile *objfile = cu->objfile;
9943 struct comp_unit_head *cu_header = &cu->header;
9944 bfd *obfd = objfile->obfd;
9945 unsigned int addr_size = cu_header->addr_size;
9946 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9947 /* Base address selection entry. */
9948 CORE_ADDR base;
9949 int found_base;
9950 unsigned int dummy;
9951 gdb_byte *buffer;
9952 CORE_ADDR marker;
9953 int low_set;
9954 CORE_ADDR low = 0;
9955 CORE_ADDR high = 0;
9956 CORE_ADDR baseaddr;
9957
9958 found_base = cu->base_known;
9959 base = cu->base_address;
9960
9961 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9962 if (offset >= dwarf2_per_objfile->ranges.size)
9963 {
9964 complaint (&symfile_complaints,
9965 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9966 offset);
9967 return 0;
9968 }
9969 buffer = dwarf2_per_objfile->ranges.buffer + offset;
9970
9971 /* Read in the largest possible address. */
9972 marker = read_address (obfd, buffer, cu, &dummy);
9973 if ((marker & mask) == mask)
9974 {
9975 /* If we found the largest possible address, then
9976 read the base address. */
9977 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9978 buffer += 2 * addr_size;
9979 offset += 2 * addr_size;
9980 found_base = 1;
9981 }
9982
9983 low_set = 0;
9984
9985 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9986
9987 while (1)
9988 {
9989 CORE_ADDR range_beginning, range_end;
9990
9991 range_beginning = read_address (obfd, buffer, cu, &dummy);
9992 buffer += addr_size;
9993 range_end = read_address (obfd, buffer, cu, &dummy);
9994 buffer += addr_size;
9995 offset += 2 * addr_size;
9996
9997 /* An end of list marker is a pair of zero addresses. */
9998 if (range_beginning == 0 && range_end == 0)
9999 /* Found the end of list entry. */
10000 break;
10001
10002 /* Each base address selection entry is a pair of 2 values.
10003 The first is the largest possible address, the second is
10004 the base address. Check for a base address here. */
10005 if ((range_beginning & mask) == mask)
10006 {
10007 /* If we found the largest possible address, then
10008 read the base address. */
10009 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10010 found_base = 1;
10011 continue;
10012 }
10013
10014 if (!found_base)
10015 {
10016 /* We have no valid base address for the ranges
10017 data. */
10018 complaint (&symfile_complaints,
10019 _("Invalid .debug_ranges data (no base address)"));
10020 return 0;
10021 }
10022
10023 if (range_beginning > range_end)
10024 {
10025 /* Inverted range entries are invalid. */
10026 complaint (&symfile_complaints,
10027 _("Invalid .debug_ranges data (inverted range)"));
10028 return 0;
10029 }
10030
10031 /* Empty range entries have no effect. */
10032 if (range_beginning == range_end)
10033 continue;
10034
10035 range_beginning += base;
10036 range_end += base;
10037
10038 /* A not-uncommon case of bad debug info.
10039 Don't pollute the addrmap with bad data. */
10040 if (range_beginning + baseaddr == 0
10041 && !dwarf2_per_objfile->has_section_at_zero)
10042 {
10043 complaint (&symfile_complaints,
10044 _(".debug_ranges entry has start address of zero"
10045 " [in module %s]"), objfile->name);
10046 continue;
10047 }
10048
10049 if (ranges_pst != NULL)
10050 addrmap_set_empty (objfile->psymtabs_addrmap,
10051 range_beginning + baseaddr,
10052 range_end - 1 + baseaddr,
10053 ranges_pst);
10054
10055 /* FIXME: This is recording everything as a low-high
10056 segment of consecutive addresses. We should have a
10057 data structure for discontiguous block ranges
10058 instead. */
10059 if (! low_set)
10060 {
10061 low = range_beginning;
10062 high = range_end;
10063 low_set = 1;
10064 }
10065 else
10066 {
10067 if (range_beginning < low)
10068 low = range_beginning;
10069 if (range_end > high)
10070 high = range_end;
10071 }
10072 }
10073
10074 if (! low_set)
10075 /* If the first entry is an end-of-list marker, the range
10076 describes an empty scope, i.e. no instructions. */
10077 return 0;
10078
10079 if (low_return)
10080 *low_return = low;
10081 if (high_return)
10082 *high_return = high;
10083 return 1;
10084 }
10085
10086 /* Get low and high pc attributes from a die. Return 1 if the attributes
10087 are present and valid, otherwise, return 0. Return -1 if the range is
10088 discontinuous, i.e. derived from DW_AT_ranges information. */
10089
10090 static int
10091 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10092 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10093 struct partial_symtab *pst)
10094 {
10095 struct attribute *attr;
10096 struct attribute *attr_high;
10097 CORE_ADDR low = 0;
10098 CORE_ADDR high = 0;
10099 int ret = 0;
10100
10101 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10102 if (attr_high)
10103 {
10104 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10105 if (attr)
10106 {
10107 low = DW_ADDR (attr);
10108 if (attr_high->form == DW_FORM_addr
10109 || attr_high->form == DW_FORM_GNU_addr_index)
10110 high = DW_ADDR (attr_high);
10111 else
10112 high = low + DW_UNSND (attr_high);
10113 }
10114 else
10115 /* Found high w/o low attribute. */
10116 return 0;
10117
10118 /* Found consecutive range of addresses. */
10119 ret = 1;
10120 }
10121 else
10122 {
10123 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10124 if (attr != NULL)
10125 {
10126 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10127 We take advantage of the fact that DW_AT_ranges does not appear
10128 in DW_TAG_compile_unit of DWO files. */
10129 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10130 unsigned int ranges_offset = (DW_UNSND (attr)
10131 + (need_ranges_base
10132 ? cu->ranges_base
10133 : 0));
10134
10135 /* Value of the DW_AT_ranges attribute is the offset in the
10136 .debug_ranges section. */
10137 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10138 return 0;
10139 /* Found discontinuous range of addresses. */
10140 ret = -1;
10141 }
10142 }
10143
10144 /* read_partial_die has also the strict LOW < HIGH requirement. */
10145 if (high <= low)
10146 return 0;
10147
10148 /* When using the GNU linker, .gnu.linkonce. sections are used to
10149 eliminate duplicate copies of functions and vtables and such.
10150 The linker will arbitrarily choose one and discard the others.
10151 The AT_*_pc values for such functions refer to local labels in
10152 these sections. If the section from that file was discarded, the
10153 labels are not in the output, so the relocs get a value of 0.
10154 If this is a discarded function, mark the pc bounds as invalid,
10155 so that GDB will ignore it. */
10156 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10157 return 0;
10158
10159 *lowpc = low;
10160 if (highpc)
10161 *highpc = high;
10162 return ret;
10163 }
10164
10165 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10166 its low and high PC addresses. Do nothing if these addresses could not
10167 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10168 and HIGHPC to the high address if greater than HIGHPC. */
10169
10170 static void
10171 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10172 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10173 struct dwarf2_cu *cu)
10174 {
10175 CORE_ADDR low, high;
10176 struct die_info *child = die->child;
10177
10178 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10179 {
10180 *lowpc = min (*lowpc, low);
10181 *highpc = max (*highpc, high);
10182 }
10183
10184 /* If the language does not allow nested subprograms (either inside
10185 subprograms or lexical blocks), we're done. */
10186 if (cu->language != language_ada)
10187 return;
10188
10189 /* Check all the children of the given DIE. If it contains nested
10190 subprograms, then check their pc bounds. Likewise, we need to
10191 check lexical blocks as well, as they may also contain subprogram
10192 definitions. */
10193 while (child && child->tag)
10194 {
10195 if (child->tag == DW_TAG_subprogram
10196 || child->tag == DW_TAG_lexical_block)
10197 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10198 child = sibling_die (child);
10199 }
10200 }
10201
10202 /* Get the low and high pc's represented by the scope DIE, and store
10203 them in *LOWPC and *HIGHPC. If the correct values can't be
10204 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10205
10206 static void
10207 get_scope_pc_bounds (struct die_info *die,
10208 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10209 struct dwarf2_cu *cu)
10210 {
10211 CORE_ADDR best_low = (CORE_ADDR) -1;
10212 CORE_ADDR best_high = (CORE_ADDR) 0;
10213 CORE_ADDR current_low, current_high;
10214
10215 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10216 {
10217 best_low = current_low;
10218 best_high = current_high;
10219 }
10220 else
10221 {
10222 struct die_info *child = die->child;
10223
10224 while (child && child->tag)
10225 {
10226 switch (child->tag) {
10227 case DW_TAG_subprogram:
10228 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10229 break;
10230 case DW_TAG_namespace:
10231 case DW_TAG_module:
10232 /* FIXME: carlton/2004-01-16: Should we do this for
10233 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10234 that current GCC's always emit the DIEs corresponding
10235 to definitions of methods of classes as children of a
10236 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10237 the DIEs giving the declarations, which could be
10238 anywhere). But I don't see any reason why the
10239 standards says that they have to be there. */
10240 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10241
10242 if (current_low != ((CORE_ADDR) -1))
10243 {
10244 best_low = min (best_low, current_low);
10245 best_high = max (best_high, current_high);
10246 }
10247 break;
10248 default:
10249 /* Ignore. */
10250 break;
10251 }
10252
10253 child = sibling_die (child);
10254 }
10255 }
10256
10257 *lowpc = best_low;
10258 *highpc = best_high;
10259 }
10260
10261 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10262 in DIE. */
10263
10264 static void
10265 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10266 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10267 {
10268 struct objfile *objfile = cu->objfile;
10269 struct attribute *attr;
10270 struct attribute *attr_high;
10271
10272 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10273 if (attr_high)
10274 {
10275 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10276 if (attr)
10277 {
10278 CORE_ADDR low = DW_ADDR (attr);
10279 CORE_ADDR high;
10280 if (attr_high->form == DW_FORM_addr
10281 || attr_high->form == DW_FORM_GNU_addr_index)
10282 high = DW_ADDR (attr_high);
10283 else
10284 high = low + DW_UNSND (attr_high);
10285
10286 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10287 }
10288 }
10289
10290 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10291 if (attr)
10292 {
10293 bfd *obfd = objfile->obfd;
10294 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10295 We take advantage of the fact that DW_AT_ranges does not appear
10296 in DW_TAG_compile_unit of DWO files. */
10297 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10298
10299 /* The value of the DW_AT_ranges attribute is the offset of the
10300 address range list in the .debug_ranges section. */
10301 unsigned long offset = (DW_UNSND (attr)
10302 + (need_ranges_base ? cu->ranges_base : 0));
10303 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10304
10305 /* For some target architectures, but not others, the
10306 read_address function sign-extends the addresses it returns.
10307 To recognize base address selection entries, we need a
10308 mask. */
10309 unsigned int addr_size = cu->header.addr_size;
10310 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10311
10312 /* The base address, to which the next pair is relative. Note
10313 that this 'base' is a DWARF concept: most entries in a range
10314 list are relative, to reduce the number of relocs against the
10315 debugging information. This is separate from this function's
10316 'baseaddr' argument, which GDB uses to relocate debugging
10317 information from a shared library based on the address at
10318 which the library was loaded. */
10319 CORE_ADDR base = cu->base_address;
10320 int base_known = cu->base_known;
10321
10322 gdb_assert (dwarf2_per_objfile->ranges.readin);
10323 if (offset >= dwarf2_per_objfile->ranges.size)
10324 {
10325 complaint (&symfile_complaints,
10326 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10327 offset);
10328 return;
10329 }
10330
10331 for (;;)
10332 {
10333 unsigned int bytes_read;
10334 CORE_ADDR start, end;
10335
10336 start = read_address (obfd, buffer, cu, &bytes_read);
10337 buffer += bytes_read;
10338 end = read_address (obfd, buffer, cu, &bytes_read);
10339 buffer += bytes_read;
10340
10341 /* Did we find the end of the range list? */
10342 if (start == 0 && end == 0)
10343 break;
10344
10345 /* Did we find a base address selection entry? */
10346 else if ((start & base_select_mask) == base_select_mask)
10347 {
10348 base = end;
10349 base_known = 1;
10350 }
10351
10352 /* We found an ordinary address range. */
10353 else
10354 {
10355 if (!base_known)
10356 {
10357 complaint (&symfile_complaints,
10358 _("Invalid .debug_ranges data "
10359 "(no base address)"));
10360 return;
10361 }
10362
10363 if (start > end)
10364 {
10365 /* Inverted range entries are invalid. */
10366 complaint (&symfile_complaints,
10367 _("Invalid .debug_ranges data "
10368 "(inverted range)"));
10369 return;
10370 }
10371
10372 /* Empty range entries have no effect. */
10373 if (start == end)
10374 continue;
10375
10376 start += base + baseaddr;
10377 end += base + baseaddr;
10378
10379 /* A not-uncommon case of bad debug info.
10380 Don't pollute the addrmap with bad data. */
10381 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10382 {
10383 complaint (&symfile_complaints,
10384 _(".debug_ranges entry has start address of zero"
10385 " [in module %s]"), objfile->name);
10386 continue;
10387 }
10388
10389 record_block_range (block, start, end - 1);
10390 }
10391 }
10392 }
10393 }
10394
10395 /* Check whether the producer field indicates either of GCC < 4.6, or the
10396 Intel C/C++ compiler, and cache the result in CU. */
10397
10398 static void
10399 check_producer (struct dwarf2_cu *cu)
10400 {
10401 const char *cs;
10402 int major, minor, release;
10403
10404 if (cu->producer == NULL)
10405 {
10406 /* For unknown compilers expect their behavior is DWARF version
10407 compliant.
10408
10409 GCC started to support .debug_types sections by -gdwarf-4 since
10410 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10411 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10412 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10413 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10414 }
10415 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10416 {
10417 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10418
10419 cs = &cu->producer[strlen ("GNU ")];
10420 while (*cs && !isdigit (*cs))
10421 cs++;
10422 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10423 {
10424 /* Not recognized as GCC. */
10425 }
10426 else
10427 {
10428 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10429 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10430 }
10431 }
10432 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10433 cu->producer_is_icc = 1;
10434 else
10435 {
10436 /* For other non-GCC compilers, expect their behavior is DWARF version
10437 compliant. */
10438 }
10439
10440 cu->checked_producer = 1;
10441 }
10442
10443 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10444 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10445 during 4.6.0 experimental. */
10446
10447 static int
10448 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10449 {
10450 if (!cu->checked_producer)
10451 check_producer (cu);
10452
10453 return cu->producer_is_gxx_lt_4_6;
10454 }
10455
10456 /* Return the default accessibility type if it is not overriden by
10457 DW_AT_accessibility. */
10458
10459 static enum dwarf_access_attribute
10460 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10461 {
10462 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10463 {
10464 /* The default DWARF 2 accessibility for members is public, the default
10465 accessibility for inheritance is private. */
10466
10467 if (die->tag != DW_TAG_inheritance)
10468 return DW_ACCESS_public;
10469 else
10470 return DW_ACCESS_private;
10471 }
10472 else
10473 {
10474 /* DWARF 3+ defines the default accessibility a different way. The same
10475 rules apply now for DW_TAG_inheritance as for the members and it only
10476 depends on the container kind. */
10477
10478 if (die->parent->tag == DW_TAG_class_type)
10479 return DW_ACCESS_private;
10480 else
10481 return DW_ACCESS_public;
10482 }
10483 }
10484
10485 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10486 offset. If the attribute was not found return 0, otherwise return
10487 1. If it was found but could not properly be handled, set *OFFSET
10488 to 0. */
10489
10490 static int
10491 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10492 LONGEST *offset)
10493 {
10494 struct attribute *attr;
10495
10496 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10497 if (attr != NULL)
10498 {
10499 *offset = 0;
10500
10501 /* Note that we do not check for a section offset first here.
10502 This is because DW_AT_data_member_location is new in DWARF 4,
10503 so if we see it, we can assume that a constant form is really
10504 a constant and not a section offset. */
10505 if (attr_form_is_constant (attr))
10506 *offset = dwarf2_get_attr_constant_value (attr, 0);
10507 else if (attr_form_is_section_offset (attr))
10508 dwarf2_complex_location_expr_complaint ();
10509 else if (attr_form_is_block (attr))
10510 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10511 else
10512 dwarf2_complex_location_expr_complaint ();
10513
10514 return 1;
10515 }
10516
10517 return 0;
10518 }
10519
10520 /* Add an aggregate field to the field list. */
10521
10522 static void
10523 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10524 struct dwarf2_cu *cu)
10525 {
10526 struct objfile *objfile = cu->objfile;
10527 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10528 struct nextfield *new_field;
10529 struct attribute *attr;
10530 struct field *fp;
10531 const char *fieldname = "";
10532
10533 /* Allocate a new field list entry and link it in. */
10534 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10535 make_cleanup (xfree, new_field);
10536 memset (new_field, 0, sizeof (struct nextfield));
10537
10538 if (die->tag == DW_TAG_inheritance)
10539 {
10540 new_field->next = fip->baseclasses;
10541 fip->baseclasses = new_field;
10542 }
10543 else
10544 {
10545 new_field->next = fip->fields;
10546 fip->fields = new_field;
10547 }
10548 fip->nfields++;
10549
10550 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10551 if (attr)
10552 new_field->accessibility = DW_UNSND (attr);
10553 else
10554 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10555 if (new_field->accessibility != DW_ACCESS_public)
10556 fip->non_public_fields = 1;
10557
10558 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10559 if (attr)
10560 new_field->virtuality = DW_UNSND (attr);
10561 else
10562 new_field->virtuality = DW_VIRTUALITY_none;
10563
10564 fp = &new_field->field;
10565
10566 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10567 {
10568 LONGEST offset;
10569
10570 /* Data member other than a C++ static data member. */
10571
10572 /* Get type of field. */
10573 fp->type = die_type (die, cu);
10574
10575 SET_FIELD_BITPOS (*fp, 0);
10576
10577 /* Get bit size of field (zero if none). */
10578 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10579 if (attr)
10580 {
10581 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10582 }
10583 else
10584 {
10585 FIELD_BITSIZE (*fp) = 0;
10586 }
10587
10588 /* Get bit offset of field. */
10589 if (handle_data_member_location (die, cu, &offset))
10590 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10591 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10592 if (attr)
10593 {
10594 if (gdbarch_bits_big_endian (gdbarch))
10595 {
10596 /* For big endian bits, the DW_AT_bit_offset gives the
10597 additional bit offset from the MSB of the containing
10598 anonymous object to the MSB of the field. We don't
10599 have to do anything special since we don't need to
10600 know the size of the anonymous object. */
10601 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10602 }
10603 else
10604 {
10605 /* For little endian bits, compute the bit offset to the
10606 MSB of the anonymous object, subtract off the number of
10607 bits from the MSB of the field to the MSB of the
10608 object, and then subtract off the number of bits of
10609 the field itself. The result is the bit offset of
10610 the LSB of the field. */
10611 int anonymous_size;
10612 int bit_offset = DW_UNSND (attr);
10613
10614 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10615 if (attr)
10616 {
10617 /* The size of the anonymous object containing
10618 the bit field is explicit, so use the
10619 indicated size (in bytes). */
10620 anonymous_size = DW_UNSND (attr);
10621 }
10622 else
10623 {
10624 /* The size of the anonymous object containing
10625 the bit field must be inferred from the type
10626 attribute of the data member containing the
10627 bit field. */
10628 anonymous_size = TYPE_LENGTH (fp->type);
10629 }
10630 SET_FIELD_BITPOS (*fp,
10631 (FIELD_BITPOS (*fp)
10632 + anonymous_size * bits_per_byte
10633 - bit_offset - FIELD_BITSIZE (*fp)));
10634 }
10635 }
10636
10637 /* Get name of field. */
10638 fieldname = dwarf2_name (die, cu);
10639 if (fieldname == NULL)
10640 fieldname = "";
10641
10642 /* The name is already allocated along with this objfile, so we don't
10643 need to duplicate it for the type. */
10644 fp->name = fieldname;
10645
10646 /* Change accessibility for artificial fields (e.g. virtual table
10647 pointer or virtual base class pointer) to private. */
10648 if (dwarf2_attr (die, DW_AT_artificial, cu))
10649 {
10650 FIELD_ARTIFICIAL (*fp) = 1;
10651 new_field->accessibility = DW_ACCESS_private;
10652 fip->non_public_fields = 1;
10653 }
10654 }
10655 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10656 {
10657 /* C++ static member. */
10658
10659 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10660 is a declaration, but all versions of G++ as of this writing
10661 (so through at least 3.2.1) incorrectly generate
10662 DW_TAG_variable tags. */
10663
10664 const char *physname;
10665
10666 /* Get name of field. */
10667 fieldname = dwarf2_name (die, cu);
10668 if (fieldname == NULL)
10669 return;
10670
10671 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10672 if (attr
10673 /* Only create a symbol if this is an external value.
10674 new_symbol checks this and puts the value in the global symbol
10675 table, which we want. If it is not external, new_symbol
10676 will try to put the value in cu->list_in_scope which is wrong. */
10677 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10678 {
10679 /* A static const member, not much different than an enum as far as
10680 we're concerned, except that we can support more types. */
10681 new_symbol (die, NULL, cu);
10682 }
10683
10684 /* Get physical name. */
10685 physname = dwarf2_physname (fieldname, die, cu);
10686
10687 /* The name is already allocated along with this objfile, so we don't
10688 need to duplicate it for the type. */
10689 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10690 FIELD_TYPE (*fp) = die_type (die, cu);
10691 FIELD_NAME (*fp) = fieldname;
10692 }
10693 else if (die->tag == DW_TAG_inheritance)
10694 {
10695 LONGEST offset;
10696
10697 /* C++ base class field. */
10698 if (handle_data_member_location (die, cu, &offset))
10699 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10700 FIELD_BITSIZE (*fp) = 0;
10701 FIELD_TYPE (*fp) = die_type (die, cu);
10702 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10703 fip->nbaseclasses++;
10704 }
10705 }
10706
10707 /* Add a typedef defined in the scope of the FIP's class. */
10708
10709 static void
10710 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10711 struct dwarf2_cu *cu)
10712 {
10713 struct objfile *objfile = cu->objfile;
10714 struct typedef_field_list *new_field;
10715 struct attribute *attr;
10716 struct typedef_field *fp;
10717 char *fieldname = "";
10718
10719 /* Allocate a new field list entry and link it in. */
10720 new_field = xzalloc (sizeof (*new_field));
10721 make_cleanup (xfree, new_field);
10722
10723 gdb_assert (die->tag == DW_TAG_typedef);
10724
10725 fp = &new_field->field;
10726
10727 /* Get name of field. */
10728 fp->name = dwarf2_name (die, cu);
10729 if (fp->name == NULL)
10730 return;
10731
10732 fp->type = read_type_die (die, cu);
10733
10734 new_field->next = fip->typedef_field_list;
10735 fip->typedef_field_list = new_field;
10736 fip->typedef_field_list_count++;
10737 }
10738
10739 /* Create the vector of fields, and attach it to the type. */
10740
10741 static void
10742 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10743 struct dwarf2_cu *cu)
10744 {
10745 int nfields = fip->nfields;
10746
10747 /* Record the field count, allocate space for the array of fields,
10748 and create blank accessibility bitfields if necessary. */
10749 TYPE_NFIELDS (type) = nfields;
10750 TYPE_FIELDS (type) = (struct field *)
10751 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10752 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10753
10754 if (fip->non_public_fields && cu->language != language_ada)
10755 {
10756 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10757
10758 TYPE_FIELD_PRIVATE_BITS (type) =
10759 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10760 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10761
10762 TYPE_FIELD_PROTECTED_BITS (type) =
10763 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10764 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10765
10766 TYPE_FIELD_IGNORE_BITS (type) =
10767 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10768 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10769 }
10770
10771 /* If the type has baseclasses, allocate and clear a bit vector for
10772 TYPE_FIELD_VIRTUAL_BITS. */
10773 if (fip->nbaseclasses && cu->language != language_ada)
10774 {
10775 int num_bytes = B_BYTES (fip->nbaseclasses);
10776 unsigned char *pointer;
10777
10778 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10779 pointer = TYPE_ALLOC (type, num_bytes);
10780 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10781 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10782 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10783 }
10784
10785 /* Copy the saved-up fields into the field vector. Start from the head of
10786 the list, adding to the tail of the field array, so that they end up in
10787 the same order in the array in which they were added to the list. */
10788 while (nfields-- > 0)
10789 {
10790 struct nextfield *fieldp;
10791
10792 if (fip->fields)
10793 {
10794 fieldp = fip->fields;
10795 fip->fields = fieldp->next;
10796 }
10797 else
10798 {
10799 fieldp = fip->baseclasses;
10800 fip->baseclasses = fieldp->next;
10801 }
10802
10803 TYPE_FIELD (type, nfields) = fieldp->field;
10804 switch (fieldp->accessibility)
10805 {
10806 case DW_ACCESS_private:
10807 if (cu->language != language_ada)
10808 SET_TYPE_FIELD_PRIVATE (type, nfields);
10809 break;
10810
10811 case DW_ACCESS_protected:
10812 if (cu->language != language_ada)
10813 SET_TYPE_FIELD_PROTECTED (type, nfields);
10814 break;
10815
10816 case DW_ACCESS_public:
10817 break;
10818
10819 default:
10820 /* Unknown accessibility. Complain and treat it as public. */
10821 {
10822 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10823 fieldp->accessibility);
10824 }
10825 break;
10826 }
10827 if (nfields < fip->nbaseclasses)
10828 {
10829 switch (fieldp->virtuality)
10830 {
10831 case DW_VIRTUALITY_virtual:
10832 case DW_VIRTUALITY_pure_virtual:
10833 if (cu->language == language_ada)
10834 error (_("unexpected virtuality in component of Ada type"));
10835 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10836 break;
10837 }
10838 }
10839 }
10840 }
10841
10842 /* Return true if this member function is a constructor, false
10843 otherwise. */
10844
10845 static int
10846 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10847 {
10848 const char *fieldname;
10849 const char *typename;
10850 int len;
10851
10852 if (die->parent == NULL)
10853 return 0;
10854
10855 if (die->parent->tag != DW_TAG_structure_type
10856 && die->parent->tag != DW_TAG_union_type
10857 && die->parent->tag != DW_TAG_class_type)
10858 return 0;
10859
10860 fieldname = dwarf2_name (die, cu);
10861 typename = dwarf2_name (die->parent, cu);
10862 if (fieldname == NULL || typename == NULL)
10863 return 0;
10864
10865 len = strlen (fieldname);
10866 return (strncmp (fieldname, typename, len) == 0
10867 && (typename[len] == '\0' || typename[len] == '<'));
10868 }
10869
10870 /* Add a member function to the proper fieldlist. */
10871
10872 static void
10873 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10874 struct type *type, struct dwarf2_cu *cu)
10875 {
10876 struct objfile *objfile = cu->objfile;
10877 struct attribute *attr;
10878 struct fnfieldlist *flp;
10879 int i;
10880 struct fn_field *fnp;
10881 const char *fieldname;
10882 struct nextfnfield *new_fnfield;
10883 struct type *this_type;
10884 enum dwarf_access_attribute accessibility;
10885
10886 if (cu->language == language_ada)
10887 error (_("unexpected member function in Ada type"));
10888
10889 /* Get name of member function. */
10890 fieldname = dwarf2_name (die, cu);
10891 if (fieldname == NULL)
10892 return;
10893
10894 /* Look up member function name in fieldlist. */
10895 for (i = 0; i < fip->nfnfields; i++)
10896 {
10897 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10898 break;
10899 }
10900
10901 /* Create new list element if necessary. */
10902 if (i < fip->nfnfields)
10903 flp = &fip->fnfieldlists[i];
10904 else
10905 {
10906 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10907 {
10908 fip->fnfieldlists = (struct fnfieldlist *)
10909 xrealloc (fip->fnfieldlists,
10910 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10911 * sizeof (struct fnfieldlist));
10912 if (fip->nfnfields == 0)
10913 make_cleanup (free_current_contents, &fip->fnfieldlists);
10914 }
10915 flp = &fip->fnfieldlists[fip->nfnfields];
10916 flp->name = fieldname;
10917 flp->length = 0;
10918 flp->head = NULL;
10919 i = fip->nfnfields++;
10920 }
10921
10922 /* Create a new member function field and chain it to the field list
10923 entry. */
10924 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10925 make_cleanup (xfree, new_fnfield);
10926 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10927 new_fnfield->next = flp->head;
10928 flp->head = new_fnfield;
10929 flp->length++;
10930
10931 /* Fill in the member function field info. */
10932 fnp = &new_fnfield->fnfield;
10933
10934 /* Delay processing of the physname until later. */
10935 if (cu->language == language_cplus || cu->language == language_java)
10936 {
10937 add_to_method_list (type, i, flp->length - 1, fieldname,
10938 die, cu);
10939 }
10940 else
10941 {
10942 const char *physname = dwarf2_physname (fieldname, die, cu);
10943 fnp->physname = physname ? physname : "";
10944 }
10945
10946 fnp->type = alloc_type (objfile);
10947 this_type = read_type_die (die, cu);
10948 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10949 {
10950 int nparams = TYPE_NFIELDS (this_type);
10951
10952 /* TYPE is the domain of this method, and THIS_TYPE is the type
10953 of the method itself (TYPE_CODE_METHOD). */
10954 smash_to_method_type (fnp->type, type,
10955 TYPE_TARGET_TYPE (this_type),
10956 TYPE_FIELDS (this_type),
10957 TYPE_NFIELDS (this_type),
10958 TYPE_VARARGS (this_type));
10959
10960 /* Handle static member functions.
10961 Dwarf2 has no clean way to discern C++ static and non-static
10962 member functions. G++ helps GDB by marking the first
10963 parameter for non-static member functions (which is the this
10964 pointer) as artificial. We obtain this information from
10965 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
10966 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10967 fnp->voffset = VOFFSET_STATIC;
10968 }
10969 else
10970 complaint (&symfile_complaints, _("member function type missing for '%s'"),
10971 dwarf2_full_name (fieldname, die, cu));
10972
10973 /* Get fcontext from DW_AT_containing_type if present. */
10974 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10975 fnp->fcontext = die_containing_type (die, cu);
10976
10977 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10978 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
10979
10980 /* Get accessibility. */
10981 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10982 if (attr)
10983 accessibility = DW_UNSND (attr);
10984 else
10985 accessibility = dwarf2_default_access_attribute (die, cu);
10986 switch (accessibility)
10987 {
10988 case DW_ACCESS_private:
10989 fnp->is_private = 1;
10990 break;
10991 case DW_ACCESS_protected:
10992 fnp->is_protected = 1;
10993 break;
10994 }
10995
10996 /* Check for artificial methods. */
10997 attr = dwarf2_attr (die, DW_AT_artificial, cu);
10998 if (attr && DW_UNSND (attr) != 0)
10999 fnp->is_artificial = 1;
11000
11001 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11002
11003 /* Get index in virtual function table if it is a virtual member
11004 function. For older versions of GCC, this is an offset in the
11005 appropriate virtual table, as specified by DW_AT_containing_type.
11006 For everyone else, it is an expression to be evaluated relative
11007 to the object address. */
11008
11009 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11010 if (attr)
11011 {
11012 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11013 {
11014 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11015 {
11016 /* Old-style GCC. */
11017 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11018 }
11019 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11020 || (DW_BLOCK (attr)->size > 1
11021 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11022 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11023 {
11024 struct dwarf_block blk;
11025 int offset;
11026
11027 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11028 ? 1 : 2);
11029 blk.size = DW_BLOCK (attr)->size - offset;
11030 blk.data = DW_BLOCK (attr)->data + offset;
11031 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11032 if ((fnp->voffset % cu->header.addr_size) != 0)
11033 dwarf2_complex_location_expr_complaint ();
11034 else
11035 fnp->voffset /= cu->header.addr_size;
11036 fnp->voffset += 2;
11037 }
11038 else
11039 dwarf2_complex_location_expr_complaint ();
11040
11041 if (!fnp->fcontext)
11042 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11043 }
11044 else if (attr_form_is_section_offset (attr))
11045 {
11046 dwarf2_complex_location_expr_complaint ();
11047 }
11048 else
11049 {
11050 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11051 fieldname);
11052 }
11053 }
11054 else
11055 {
11056 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11057 if (attr && DW_UNSND (attr))
11058 {
11059 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11060 complaint (&symfile_complaints,
11061 _("Member function \"%s\" (offset %d) is virtual "
11062 "but the vtable offset is not specified"),
11063 fieldname, die->offset.sect_off);
11064 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11065 TYPE_CPLUS_DYNAMIC (type) = 1;
11066 }
11067 }
11068 }
11069
11070 /* Create the vector of member function fields, and attach it to the type. */
11071
11072 static void
11073 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11074 struct dwarf2_cu *cu)
11075 {
11076 struct fnfieldlist *flp;
11077 int i;
11078
11079 if (cu->language == language_ada)
11080 error (_("unexpected member functions in Ada type"));
11081
11082 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11083 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11084 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11085
11086 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11087 {
11088 struct nextfnfield *nfp = flp->head;
11089 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11090 int k;
11091
11092 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11093 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11094 fn_flp->fn_fields = (struct fn_field *)
11095 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11096 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11097 fn_flp->fn_fields[k] = nfp->fnfield;
11098 }
11099
11100 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11101 }
11102
11103 /* Returns non-zero if NAME is the name of a vtable member in CU's
11104 language, zero otherwise. */
11105 static int
11106 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11107 {
11108 static const char vptr[] = "_vptr";
11109 static const char vtable[] = "vtable";
11110
11111 /* Look for the C++ and Java forms of the vtable. */
11112 if ((cu->language == language_java
11113 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11114 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11115 && is_cplus_marker (name[sizeof (vptr) - 1])))
11116 return 1;
11117
11118 return 0;
11119 }
11120
11121 /* GCC outputs unnamed structures that are really pointers to member
11122 functions, with the ABI-specified layout. If TYPE describes
11123 such a structure, smash it into a member function type.
11124
11125 GCC shouldn't do this; it should just output pointer to member DIEs.
11126 This is GCC PR debug/28767. */
11127
11128 static void
11129 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11130 {
11131 struct type *pfn_type, *domain_type, *new_type;
11132
11133 /* Check for a structure with no name and two children. */
11134 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11135 return;
11136
11137 /* Check for __pfn and __delta members. */
11138 if (TYPE_FIELD_NAME (type, 0) == NULL
11139 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11140 || TYPE_FIELD_NAME (type, 1) == NULL
11141 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11142 return;
11143
11144 /* Find the type of the method. */
11145 pfn_type = TYPE_FIELD_TYPE (type, 0);
11146 if (pfn_type == NULL
11147 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11148 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11149 return;
11150
11151 /* Look for the "this" argument. */
11152 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11153 if (TYPE_NFIELDS (pfn_type) == 0
11154 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11155 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11156 return;
11157
11158 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11159 new_type = alloc_type (objfile);
11160 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11161 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11162 TYPE_VARARGS (pfn_type));
11163 smash_to_methodptr_type (type, new_type);
11164 }
11165
11166 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11167 (icc). */
11168
11169 static int
11170 producer_is_icc (struct dwarf2_cu *cu)
11171 {
11172 if (!cu->checked_producer)
11173 check_producer (cu);
11174
11175 return cu->producer_is_icc;
11176 }
11177
11178 /* Called when we find the DIE that starts a structure or union scope
11179 (definition) to create a type for the structure or union. Fill in
11180 the type's name and general properties; the members will not be
11181 processed until process_structure_type.
11182
11183 NOTE: we need to call these functions regardless of whether or not the
11184 DIE has a DW_AT_name attribute, since it might be an anonymous
11185 structure or union. This gets the type entered into our set of
11186 user defined types.
11187
11188 However, if the structure is incomplete (an opaque struct/union)
11189 then suppress creating a symbol table entry for it since gdb only
11190 wants to find the one with the complete definition. Note that if
11191 it is complete, we just call new_symbol, which does it's own
11192 checking about whether the struct/union is anonymous or not (and
11193 suppresses creating a symbol table entry itself). */
11194
11195 static struct type *
11196 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11197 {
11198 struct objfile *objfile = cu->objfile;
11199 struct type *type;
11200 struct attribute *attr;
11201 const char *name;
11202
11203 /* If the definition of this type lives in .debug_types, read that type.
11204 Don't follow DW_AT_specification though, that will take us back up
11205 the chain and we want to go down. */
11206 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11207 if (attr)
11208 {
11209 struct dwarf2_cu *type_cu = cu;
11210 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11211
11212 /* We could just recurse on read_structure_type, but we need to call
11213 get_die_type to ensure only one type for this DIE is created.
11214 This is important, for example, because for c++ classes we need
11215 TYPE_NAME set which is only done by new_symbol. Blech. */
11216 type = read_type_die (type_die, type_cu);
11217
11218 /* TYPE_CU may not be the same as CU.
11219 Ensure TYPE is recorded in CU's type_hash table. */
11220 return set_die_type (die, type, cu);
11221 }
11222
11223 type = alloc_type (objfile);
11224 INIT_CPLUS_SPECIFIC (type);
11225
11226 name = dwarf2_name (die, cu);
11227 if (name != NULL)
11228 {
11229 if (cu->language == language_cplus
11230 || cu->language == language_java)
11231 {
11232 const char *full_name = dwarf2_full_name (name, die, cu);
11233
11234 /* dwarf2_full_name might have already finished building the DIE's
11235 type. If so, there is no need to continue. */
11236 if (get_die_type (die, cu) != NULL)
11237 return get_die_type (die, cu);
11238
11239 TYPE_TAG_NAME (type) = full_name;
11240 if (die->tag == DW_TAG_structure_type
11241 || die->tag == DW_TAG_class_type)
11242 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11243 }
11244 else
11245 {
11246 /* The name is already allocated along with this objfile, so
11247 we don't need to duplicate it for the type. */
11248 TYPE_TAG_NAME (type) = name;
11249 if (die->tag == DW_TAG_class_type)
11250 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11251 }
11252 }
11253
11254 if (die->tag == DW_TAG_structure_type)
11255 {
11256 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11257 }
11258 else if (die->tag == DW_TAG_union_type)
11259 {
11260 TYPE_CODE (type) = TYPE_CODE_UNION;
11261 }
11262 else
11263 {
11264 TYPE_CODE (type) = TYPE_CODE_CLASS;
11265 }
11266
11267 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11268 TYPE_DECLARED_CLASS (type) = 1;
11269
11270 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11271 if (attr)
11272 {
11273 TYPE_LENGTH (type) = DW_UNSND (attr);
11274 }
11275 else
11276 {
11277 TYPE_LENGTH (type) = 0;
11278 }
11279
11280 if (producer_is_icc (cu))
11281 {
11282 /* ICC does not output the required DW_AT_declaration
11283 on incomplete types, but gives them a size of zero. */
11284 }
11285 else
11286 TYPE_STUB_SUPPORTED (type) = 1;
11287
11288 if (die_is_declaration (die, cu))
11289 TYPE_STUB (type) = 1;
11290 else if (attr == NULL && die->child == NULL
11291 && producer_is_realview (cu->producer))
11292 /* RealView does not output the required DW_AT_declaration
11293 on incomplete types. */
11294 TYPE_STUB (type) = 1;
11295
11296 /* We need to add the type field to the die immediately so we don't
11297 infinitely recurse when dealing with pointers to the structure
11298 type within the structure itself. */
11299 set_die_type (die, type, cu);
11300
11301 /* set_die_type should be already done. */
11302 set_descriptive_type (type, die, cu);
11303
11304 return type;
11305 }
11306
11307 /* Finish creating a structure or union type, including filling in
11308 its members and creating a symbol for it. */
11309
11310 static void
11311 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11312 {
11313 struct objfile *objfile = cu->objfile;
11314 struct die_info *child_die = die->child;
11315 struct type *type;
11316
11317 type = get_die_type (die, cu);
11318 if (type == NULL)
11319 type = read_structure_type (die, cu);
11320
11321 if (die->child != NULL && ! die_is_declaration (die, cu))
11322 {
11323 struct field_info fi;
11324 struct die_info *child_die;
11325 VEC (symbolp) *template_args = NULL;
11326 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11327
11328 memset (&fi, 0, sizeof (struct field_info));
11329
11330 child_die = die->child;
11331
11332 while (child_die && child_die->tag)
11333 {
11334 if (child_die->tag == DW_TAG_member
11335 || child_die->tag == DW_TAG_variable)
11336 {
11337 /* NOTE: carlton/2002-11-05: A C++ static data member
11338 should be a DW_TAG_member that is a declaration, but
11339 all versions of G++ as of this writing (so through at
11340 least 3.2.1) incorrectly generate DW_TAG_variable
11341 tags for them instead. */
11342 dwarf2_add_field (&fi, child_die, cu);
11343 }
11344 else if (child_die->tag == DW_TAG_subprogram)
11345 {
11346 /* C++ member function. */
11347 dwarf2_add_member_fn (&fi, child_die, type, cu);
11348 }
11349 else if (child_die->tag == DW_TAG_inheritance)
11350 {
11351 /* C++ base class field. */
11352 dwarf2_add_field (&fi, child_die, cu);
11353 }
11354 else if (child_die->tag == DW_TAG_typedef)
11355 dwarf2_add_typedef (&fi, child_die, cu);
11356 else if (child_die->tag == DW_TAG_template_type_param
11357 || child_die->tag == DW_TAG_template_value_param)
11358 {
11359 struct symbol *arg = new_symbol (child_die, NULL, cu);
11360
11361 if (arg != NULL)
11362 VEC_safe_push (symbolp, template_args, arg);
11363 }
11364
11365 child_die = sibling_die (child_die);
11366 }
11367
11368 /* Attach template arguments to type. */
11369 if (! VEC_empty (symbolp, template_args))
11370 {
11371 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11372 TYPE_N_TEMPLATE_ARGUMENTS (type)
11373 = VEC_length (symbolp, template_args);
11374 TYPE_TEMPLATE_ARGUMENTS (type)
11375 = obstack_alloc (&objfile->objfile_obstack,
11376 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11377 * sizeof (struct symbol *)));
11378 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11379 VEC_address (symbolp, template_args),
11380 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11381 * sizeof (struct symbol *)));
11382 VEC_free (symbolp, template_args);
11383 }
11384
11385 /* Attach fields and member functions to the type. */
11386 if (fi.nfields)
11387 dwarf2_attach_fields_to_type (&fi, type, cu);
11388 if (fi.nfnfields)
11389 {
11390 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11391
11392 /* Get the type which refers to the base class (possibly this
11393 class itself) which contains the vtable pointer for the current
11394 class from the DW_AT_containing_type attribute. This use of
11395 DW_AT_containing_type is a GNU extension. */
11396
11397 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11398 {
11399 struct type *t = die_containing_type (die, cu);
11400
11401 TYPE_VPTR_BASETYPE (type) = t;
11402 if (type == t)
11403 {
11404 int i;
11405
11406 /* Our own class provides vtbl ptr. */
11407 for (i = TYPE_NFIELDS (t) - 1;
11408 i >= TYPE_N_BASECLASSES (t);
11409 --i)
11410 {
11411 const char *fieldname = TYPE_FIELD_NAME (t, i);
11412
11413 if (is_vtable_name (fieldname, cu))
11414 {
11415 TYPE_VPTR_FIELDNO (type) = i;
11416 break;
11417 }
11418 }
11419
11420 /* Complain if virtual function table field not found. */
11421 if (i < TYPE_N_BASECLASSES (t))
11422 complaint (&symfile_complaints,
11423 _("virtual function table pointer "
11424 "not found when defining class '%s'"),
11425 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11426 "");
11427 }
11428 else
11429 {
11430 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11431 }
11432 }
11433 else if (cu->producer
11434 && strncmp (cu->producer,
11435 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11436 {
11437 /* The IBM XLC compiler does not provide direct indication
11438 of the containing type, but the vtable pointer is
11439 always named __vfp. */
11440
11441 int i;
11442
11443 for (i = TYPE_NFIELDS (type) - 1;
11444 i >= TYPE_N_BASECLASSES (type);
11445 --i)
11446 {
11447 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11448 {
11449 TYPE_VPTR_FIELDNO (type) = i;
11450 TYPE_VPTR_BASETYPE (type) = type;
11451 break;
11452 }
11453 }
11454 }
11455 }
11456
11457 /* Copy fi.typedef_field_list linked list elements content into the
11458 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11459 if (fi.typedef_field_list)
11460 {
11461 int i = fi.typedef_field_list_count;
11462
11463 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11464 TYPE_TYPEDEF_FIELD_ARRAY (type)
11465 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11466 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11467
11468 /* Reverse the list order to keep the debug info elements order. */
11469 while (--i >= 0)
11470 {
11471 struct typedef_field *dest, *src;
11472
11473 dest = &TYPE_TYPEDEF_FIELD (type, i);
11474 src = &fi.typedef_field_list->field;
11475 fi.typedef_field_list = fi.typedef_field_list->next;
11476 *dest = *src;
11477 }
11478 }
11479
11480 do_cleanups (back_to);
11481
11482 if (HAVE_CPLUS_STRUCT (type))
11483 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11484 }
11485
11486 quirk_gcc_member_function_pointer (type, objfile);
11487
11488 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11489 snapshots) has been known to create a die giving a declaration
11490 for a class that has, as a child, a die giving a definition for a
11491 nested class. So we have to process our children even if the
11492 current die is a declaration. Normally, of course, a declaration
11493 won't have any children at all. */
11494
11495 while (child_die != NULL && child_die->tag)
11496 {
11497 if (child_die->tag == DW_TAG_member
11498 || child_die->tag == DW_TAG_variable
11499 || child_die->tag == DW_TAG_inheritance
11500 || child_die->tag == DW_TAG_template_value_param
11501 || child_die->tag == DW_TAG_template_type_param)
11502 {
11503 /* Do nothing. */
11504 }
11505 else
11506 process_die (child_die, cu);
11507
11508 child_die = sibling_die (child_die);
11509 }
11510
11511 /* Do not consider external references. According to the DWARF standard,
11512 these DIEs are identified by the fact that they have no byte_size
11513 attribute, and a declaration attribute. */
11514 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11515 || !die_is_declaration (die, cu))
11516 new_symbol (die, type, cu);
11517 }
11518
11519 /* Given a DW_AT_enumeration_type die, set its type. We do not
11520 complete the type's fields yet, or create any symbols. */
11521
11522 static struct type *
11523 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11524 {
11525 struct objfile *objfile = cu->objfile;
11526 struct type *type;
11527 struct attribute *attr;
11528 const char *name;
11529
11530 /* If the definition of this type lives in .debug_types, read that type.
11531 Don't follow DW_AT_specification though, that will take us back up
11532 the chain and we want to go down. */
11533 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11534 if (attr)
11535 {
11536 struct dwarf2_cu *type_cu = cu;
11537 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11538
11539 type = read_type_die (type_die, type_cu);
11540
11541 /* TYPE_CU may not be the same as CU.
11542 Ensure TYPE is recorded in CU's type_hash table. */
11543 return set_die_type (die, type, cu);
11544 }
11545
11546 type = alloc_type (objfile);
11547
11548 TYPE_CODE (type) = TYPE_CODE_ENUM;
11549 name = dwarf2_full_name (NULL, die, cu);
11550 if (name != NULL)
11551 TYPE_TAG_NAME (type) = name;
11552
11553 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11554 if (attr)
11555 {
11556 TYPE_LENGTH (type) = DW_UNSND (attr);
11557 }
11558 else
11559 {
11560 TYPE_LENGTH (type) = 0;
11561 }
11562
11563 /* The enumeration DIE can be incomplete. In Ada, any type can be
11564 declared as private in the package spec, and then defined only
11565 inside the package body. Such types are known as Taft Amendment
11566 Types. When another package uses such a type, an incomplete DIE
11567 may be generated by the compiler. */
11568 if (die_is_declaration (die, cu))
11569 TYPE_STUB (type) = 1;
11570
11571 return set_die_type (die, type, cu);
11572 }
11573
11574 /* Given a pointer to a die which begins an enumeration, process all
11575 the dies that define the members of the enumeration, and create the
11576 symbol for the enumeration type.
11577
11578 NOTE: We reverse the order of the element list. */
11579
11580 static void
11581 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11582 {
11583 struct type *this_type;
11584
11585 this_type = get_die_type (die, cu);
11586 if (this_type == NULL)
11587 this_type = read_enumeration_type (die, cu);
11588
11589 if (die->child != NULL)
11590 {
11591 struct die_info *child_die;
11592 struct symbol *sym;
11593 struct field *fields = NULL;
11594 int num_fields = 0;
11595 int unsigned_enum = 1;
11596 const char *name;
11597 int flag_enum = 1;
11598 ULONGEST mask = 0;
11599
11600 child_die = die->child;
11601 while (child_die && child_die->tag)
11602 {
11603 if (child_die->tag != DW_TAG_enumerator)
11604 {
11605 process_die (child_die, cu);
11606 }
11607 else
11608 {
11609 name = dwarf2_name (child_die, cu);
11610 if (name)
11611 {
11612 sym = new_symbol (child_die, this_type, cu);
11613 if (SYMBOL_VALUE (sym) < 0)
11614 {
11615 unsigned_enum = 0;
11616 flag_enum = 0;
11617 }
11618 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11619 flag_enum = 0;
11620 else
11621 mask |= SYMBOL_VALUE (sym);
11622
11623 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11624 {
11625 fields = (struct field *)
11626 xrealloc (fields,
11627 (num_fields + DW_FIELD_ALLOC_CHUNK)
11628 * sizeof (struct field));
11629 }
11630
11631 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11632 FIELD_TYPE (fields[num_fields]) = NULL;
11633 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11634 FIELD_BITSIZE (fields[num_fields]) = 0;
11635
11636 num_fields++;
11637 }
11638 }
11639
11640 child_die = sibling_die (child_die);
11641 }
11642
11643 if (num_fields)
11644 {
11645 TYPE_NFIELDS (this_type) = num_fields;
11646 TYPE_FIELDS (this_type) = (struct field *)
11647 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11648 memcpy (TYPE_FIELDS (this_type), fields,
11649 sizeof (struct field) * num_fields);
11650 xfree (fields);
11651 }
11652 if (unsigned_enum)
11653 TYPE_UNSIGNED (this_type) = 1;
11654 if (flag_enum)
11655 TYPE_FLAG_ENUM (this_type) = 1;
11656 }
11657
11658 /* If we are reading an enum from a .debug_types unit, and the enum
11659 is a declaration, and the enum is not the signatured type in the
11660 unit, then we do not want to add a symbol for it. Adding a
11661 symbol would in some cases obscure the true definition of the
11662 enum, giving users an incomplete type when the definition is
11663 actually available. Note that we do not want to do this for all
11664 enums which are just declarations, because C++0x allows forward
11665 enum declarations. */
11666 if (cu->per_cu->is_debug_types
11667 && die_is_declaration (die, cu))
11668 {
11669 struct signatured_type *sig_type;
11670
11671 sig_type
11672 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11673 cu->per_cu->info_or_types_section,
11674 cu->per_cu->offset);
11675 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11676 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11677 return;
11678 }
11679
11680 new_symbol (die, this_type, cu);
11681 }
11682
11683 /* Extract all information from a DW_TAG_array_type DIE and put it in
11684 the DIE's type field. For now, this only handles one dimensional
11685 arrays. */
11686
11687 static struct type *
11688 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11689 {
11690 struct objfile *objfile = cu->objfile;
11691 struct die_info *child_die;
11692 struct type *type;
11693 struct type *element_type, *range_type, *index_type;
11694 struct type **range_types = NULL;
11695 struct attribute *attr;
11696 int ndim = 0;
11697 struct cleanup *back_to;
11698 const char *name;
11699
11700 element_type = die_type (die, cu);
11701
11702 /* The die_type call above may have already set the type for this DIE. */
11703 type = get_die_type (die, cu);
11704 if (type)
11705 return type;
11706
11707 /* Irix 6.2 native cc creates array types without children for
11708 arrays with unspecified length. */
11709 if (die->child == NULL)
11710 {
11711 index_type = objfile_type (objfile)->builtin_int;
11712 range_type = create_range_type (NULL, index_type, 0, -1);
11713 type = create_array_type (NULL, element_type, range_type);
11714 return set_die_type (die, type, cu);
11715 }
11716
11717 back_to = make_cleanup (null_cleanup, NULL);
11718 child_die = die->child;
11719 while (child_die && child_die->tag)
11720 {
11721 if (child_die->tag == DW_TAG_subrange_type)
11722 {
11723 struct type *child_type = read_type_die (child_die, cu);
11724
11725 if (child_type != NULL)
11726 {
11727 /* The range type was succesfully read. Save it for the
11728 array type creation. */
11729 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11730 {
11731 range_types = (struct type **)
11732 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11733 * sizeof (struct type *));
11734 if (ndim == 0)
11735 make_cleanup (free_current_contents, &range_types);
11736 }
11737 range_types[ndim++] = child_type;
11738 }
11739 }
11740 child_die = sibling_die (child_die);
11741 }
11742
11743 /* Dwarf2 dimensions are output from left to right, create the
11744 necessary array types in backwards order. */
11745
11746 type = element_type;
11747
11748 if (read_array_order (die, cu) == DW_ORD_col_major)
11749 {
11750 int i = 0;
11751
11752 while (i < ndim)
11753 type = create_array_type (NULL, type, range_types[i++]);
11754 }
11755 else
11756 {
11757 while (ndim-- > 0)
11758 type = create_array_type (NULL, type, range_types[ndim]);
11759 }
11760
11761 /* Understand Dwarf2 support for vector types (like they occur on
11762 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11763 array type. This is not part of the Dwarf2/3 standard yet, but a
11764 custom vendor extension. The main difference between a regular
11765 array and the vector variant is that vectors are passed by value
11766 to functions. */
11767 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11768 if (attr)
11769 make_vector_type (type);
11770
11771 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11772 implementation may choose to implement triple vectors using this
11773 attribute. */
11774 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11775 if (attr)
11776 {
11777 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11778 TYPE_LENGTH (type) = DW_UNSND (attr);
11779 else
11780 complaint (&symfile_complaints,
11781 _("DW_AT_byte_size for array type smaller "
11782 "than the total size of elements"));
11783 }
11784
11785 name = dwarf2_name (die, cu);
11786 if (name)
11787 TYPE_NAME (type) = name;
11788
11789 /* Install the type in the die. */
11790 set_die_type (die, type, cu);
11791
11792 /* set_die_type should be already done. */
11793 set_descriptive_type (type, die, cu);
11794
11795 do_cleanups (back_to);
11796
11797 return type;
11798 }
11799
11800 static enum dwarf_array_dim_ordering
11801 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11802 {
11803 struct attribute *attr;
11804
11805 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11806
11807 if (attr) return DW_SND (attr);
11808
11809 /* GNU F77 is a special case, as at 08/2004 array type info is the
11810 opposite order to the dwarf2 specification, but data is still
11811 laid out as per normal fortran.
11812
11813 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11814 version checking. */
11815
11816 if (cu->language == language_fortran
11817 && cu->producer && strstr (cu->producer, "GNU F77"))
11818 {
11819 return DW_ORD_row_major;
11820 }
11821
11822 switch (cu->language_defn->la_array_ordering)
11823 {
11824 case array_column_major:
11825 return DW_ORD_col_major;
11826 case array_row_major:
11827 default:
11828 return DW_ORD_row_major;
11829 };
11830 }
11831
11832 /* Extract all information from a DW_TAG_set_type DIE and put it in
11833 the DIE's type field. */
11834
11835 static struct type *
11836 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11837 {
11838 struct type *domain_type, *set_type;
11839 struct attribute *attr;
11840
11841 domain_type = die_type (die, cu);
11842
11843 /* The die_type call above may have already set the type for this DIE. */
11844 set_type = get_die_type (die, cu);
11845 if (set_type)
11846 return set_type;
11847
11848 set_type = create_set_type (NULL, domain_type);
11849
11850 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11851 if (attr)
11852 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11853
11854 return set_die_type (die, set_type, cu);
11855 }
11856
11857 /* A helper for read_common_block that creates a locexpr baton.
11858 SYM is the symbol which we are marking as computed.
11859 COMMON_DIE is the DIE for the common block.
11860 COMMON_LOC is the location expression attribute for the common
11861 block itself.
11862 MEMBER_LOC is the location expression attribute for the particular
11863 member of the common block that we are processing.
11864 CU is the CU from which the above come. */
11865
11866 static void
11867 mark_common_block_symbol_computed (struct symbol *sym,
11868 struct die_info *common_die,
11869 struct attribute *common_loc,
11870 struct attribute *member_loc,
11871 struct dwarf2_cu *cu)
11872 {
11873 struct objfile *objfile = dwarf2_per_objfile->objfile;
11874 struct dwarf2_locexpr_baton *baton;
11875 gdb_byte *ptr;
11876 unsigned int cu_off;
11877 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11878 LONGEST offset = 0;
11879
11880 gdb_assert (common_loc && member_loc);
11881 gdb_assert (attr_form_is_block (common_loc));
11882 gdb_assert (attr_form_is_block (member_loc)
11883 || attr_form_is_constant (member_loc));
11884
11885 baton = obstack_alloc (&objfile->objfile_obstack,
11886 sizeof (struct dwarf2_locexpr_baton));
11887 baton->per_cu = cu->per_cu;
11888 gdb_assert (baton->per_cu);
11889
11890 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11891
11892 if (attr_form_is_constant (member_loc))
11893 {
11894 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11895 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11896 }
11897 else
11898 baton->size += DW_BLOCK (member_loc)->size;
11899
11900 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11901 baton->data = ptr;
11902
11903 *ptr++ = DW_OP_call4;
11904 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11905 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11906 ptr += 4;
11907
11908 if (attr_form_is_constant (member_loc))
11909 {
11910 *ptr++ = DW_OP_addr;
11911 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11912 ptr += cu->header.addr_size;
11913 }
11914 else
11915 {
11916 /* We have to copy the data here, because DW_OP_call4 will only
11917 use a DW_AT_location attribute. */
11918 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11919 ptr += DW_BLOCK (member_loc)->size;
11920 }
11921
11922 *ptr++ = DW_OP_plus;
11923 gdb_assert (ptr - baton->data == baton->size);
11924
11925 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11926 SYMBOL_LOCATION_BATON (sym) = baton;
11927 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11928 }
11929
11930 /* Create appropriate locally-scoped variables for all the
11931 DW_TAG_common_block entries. Also create a struct common_block
11932 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11933 is used to sepate the common blocks name namespace from regular
11934 variable names. */
11935
11936 static void
11937 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11938 {
11939 struct attribute *attr;
11940
11941 attr = dwarf2_attr (die, DW_AT_location, cu);
11942 if (attr)
11943 {
11944 /* Support the .debug_loc offsets. */
11945 if (attr_form_is_block (attr))
11946 {
11947 /* Ok. */
11948 }
11949 else if (attr_form_is_section_offset (attr))
11950 {
11951 dwarf2_complex_location_expr_complaint ();
11952 attr = NULL;
11953 }
11954 else
11955 {
11956 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11957 "common block member");
11958 attr = NULL;
11959 }
11960 }
11961
11962 if (die->child != NULL)
11963 {
11964 struct objfile *objfile = cu->objfile;
11965 struct die_info *child_die;
11966 size_t n_entries = 0, size;
11967 struct common_block *common_block;
11968 struct symbol *sym;
11969
11970 for (child_die = die->child;
11971 child_die && child_die->tag;
11972 child_die = sibling_die (child_die))
11973 ++n_entries;
11974
11975 size = (sizeof (struct common_block)
11976 + (n_entries - 1) * sizeof (struct symbol *));
11977 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11978 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11979 common_block->n_entries = 0;
11980
11981 for (child_die = die->child;
11982 child_die && child_die->tag;
11983 child_die = sibling_die (child_die))
11984 {
11985 /* Create the symbol in the DW_TAG_common_block block in the current
11986 symbol scope. */
11987 sym = new_symbol (child_die, NULL, cu);
11988 if (sym != NULL)
11989 {
11990 struct attribute *member_loc;
11991
11992 common_block->contents[common_block->n_entries++] = sym;
11993
11994 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11995 cu);
11996 if (member_loc)
11997 {
11998 /* GDB has handled this for a long time, but it is
11999 not specified by DWARF. It seems to have been
12000 emitted by gfortran at least as recently as:
12001 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12002 complaint (&symfile_complaints,
12003 _("Variable in common block has "
12004 "DW_AT_data_member_location "
12005 "- DIE at 0x%x [in module %s]"),
12006 child_die->offset.sect_off, cu->objfile->name);
12007
12008 if (attr_form_is_section_offset (member_loc))
12009 dwarf2_complex_location_expr_complaint ();
12010 else if (attr_form_is_constant (member_loc)
12011 || attr_form_is_block (member_loc))
12012 {
12013 if (attr)
12014 mark_common_block_symbol_computed (sym, die, attr,
12015 member_loc, cu);
12016 }
12017 else
12018 dwarf2_complex_location_expr_complaint ();
12019 }
12020 }
12021 }
12022
12023 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12024 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12025 }
12026 }
12027
12028 /* Create a type for a C++ namespace. */
12029
12030 static struct type *
12031 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12032 {
12033 struct objfile *objfile = cu->objfile;
12034 const char *previous_prefix, *name;
12035 int is_anonymous;
12036 struct type *type;
12037
12038 /* For extensions, reuse the type of the original namespace. */
12039 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12040 {
12041 struct die_info *ext_die;
12042 struct dwarf2_cu *ext_cu = cu;
12043
12044 ext_die = dwarf2_extension (die, &ext_cu);
12045 type = read_type_die (ext_die, ext_cu);
12046
12047 /* EXT_CU may not be the same as CU.
12048 Ensure TYPE is recorded in CU's type_hash table. */
12049 return set_die_type (die, type, cu);
12050 }
12051
12052 name = namespace_name (die, &is_anonymous, cu);
12053
12054 /* Now build the name of the current namespace. */
12055
12056 previous_prefix = determine_prefix (die, cu);
12057 if (previous_prefix[0] != '\0')
12058 name = typename_concat (&objfile->objfile_obstack,
12059 previous_prefix, name, 0, cu);
12060
12061 /* Create the type. */
12062 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12063 objfile);
12064 TYPE_NAME (type) = name;
12065 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12066
12067 return set_die_type (die, type, cu);
12068 }
12069
12070 /* Read a C++ namespace. */
12071
12072 static void
12073 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12074 {
12075 struct objfile *objfile = cu->objfile;
12076 int is_anonymous;
12077
12078 /* Add a symbol associated to this if we haven't seen the namespace
12079 before. Also, add a using directive if it's an anonymous
12080 namespace. */
12081
12082 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12083 {
12084 struct type *type;
12085
12086 type = read_type_die (die, cu);
12087 new_symbol (die, type, cu);
12088
12089 namespace_name (die, &is_anonymous, cu);
12090 if (is_anonymous)
12091 {
12092 const char *previous_prefix = determine_prefix (die, cu);
12093
12094 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12095 NULL, NULL, 0, &objfile->objfile_obstack);
12096 }
12097 }
12098
12099 if (die->child != NULL)
12100 {
12101 struct die_info *child_die = die->child;
12102
12103 while (child_die && child_die->tag)
12104 {
12105 process_die (child_die, cu);
12106 child_die = sibling_die (child_die);
12107 }
12108 }
12109 }
12110
12111 /* Read a Fortran module as type. This DIE can be only a declaration used for
12112 imported module. Still we need that type as local Fortran "use ... only"
12113 declaration imports depend on the created type in determine_prefix. */
12114
12115 static struct type *
12116 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12117 {
12118 struct objfile *objfile = cu->objfile;
12119 const char *module_name;
12120 struct type *type;
12121
12122 module_name = dwarf2_name (die, cu);
12123 if (!module_name)
12124 complaint (&symfile_complaints,
12125 _("DW_TAG_module has no name, offset 0x%x"),
12126 die->offset.sect_off);
12127 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12128
12129 /* determine_prefix uses TYPE_TAG_NAME. */
12130 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12131
12132 return set_die_type (die, type, cu);
12133 }
12134
12135 /* Read a Fortran module. */
12136
12137 static void
12138 read_module (struct die_info *die, struct dwarf2_cu *cu)
12139 {
12140 struct die_info *child_die = die->child;
12141
12142 while (child_die && child_die->tag)
12143 {
12144 process_die (child_die, cu);
12145 child_die = sibling_die (child_die);
12146 }
12147 }
12148
12149 /* Return the name of the namespace represented by DIE. Set
12150 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12151 namespace. */
12152
12153 static const char *
12154 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12155 {
12156 struct die_info *current_die;
12157 const char *name = NULL;
12158
12159 /* Loop through the extensions until we find a name. */
12160
12161 for (current_die = die;
12162 current_die != NULL;
12163 current_die = dwarf2_extension (die, &cu))
12164 {
12165 name = dwarf2_name (current_die, cu);
12166 if (name != NULL)
12167 break;
12168 }
12169
12170 /* Is it an anonymous namespace? */
12171
12172 *is_anonymous = (name == NULL);
12173 if (*is_anonymous)
12174 name = CP_ANONYMOUS_NAMESPACE_STR;
12175
12176 return name;
12177 }
12178
12179 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12180 the user defined type vector. */
12181
12182 static struct type *
12183 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12184 {
12185 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12186 struct comp_unit_head *cu_header = &cu->header;
12187 struct type *type;
12188 struct attribute *attr_byte_size;
12189 struct attribute *attr_address_class;
12190 int byte_size, addr_class;
12191 struct type *target_type;
12192
12193 target_type = die_type (die, cu);
12194
12195 /* The die_type call above may have already set the type for this DIE. */
12196 type = get_die_type (die, cu);
12197 if (type)
12198 return type;
12199
12200 type = lookup_pointer_type (target_type);
12201
12202 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12203 if (attr_byte_size)
12204 byte_size = DW_UNSND (attr_byte_size);
12205 else
12206 byte_size = cu_header->addr_size;
12207
12208 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12209 if (attr_address_class)
12210 addr_class = DW_UNSND (attr_address_class);
12211 else
12212 addr_class = DW_ADDR_none;
12213
12214 /* If the pointer size or address class is different than the
12215 default, create a type variant marked as such and set the
12216 length accordingly. */
12217 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12218 {
12219 if (gdbarch_address_class_type_flags_p (gdbarch))
12220 {
12221 int type_flags;
12222
12223 type_flags = gdbarch_address_class_type_flags
12224 (gdbarch, byte_size, addr_class);
12225 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12226 == 0);
12227 type = make_type_with_address_space (type, type_flags);
12228 }
12229 else if (TYPE_LENGTH (type) != byte_size)
12230 {
12231 complaint (&symfile_complaints,
12232 _("invalid pointer size %d"), byte_size);
12233 }
12234 else
12235 {
12236 /* Should we also complain about unhandled address classes? */
12237 }
12238 }
12239
12240 TYPE_LENGTH (type) = byte_size;
12241 return set_die_type (die, type, cu);
12242 }
12243
12244 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12245 the user defined type vector. */
12246
12247 static struct type *
12248 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12249 {
12250 struct type *type;
12251 struct type *to_type;
12252 struct type *domain;
12253
12254 to_type = die_type (die, cu);
12255 domain = die_containing_type (die, cu);
12256
12257 /* The calls above may have already set the type for this DIE. */
12258 type = get_die_type (die, cu);
12259 if (type)
12260 return type;
12261
12262 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12263 type = lookup_methodptr_type (to_type);
12264 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12265 {
12266 struct type *new_type = alloc_type (cu->objfile);
12267
12268 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12269 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12270 TYPE_VARARGS (to_type));
12271 type = lookup_methodptr_type (new_type);
12272 }
12273 else
12274 type = lookup_memberptr_type (to_type, domain);
12275
12276 return set_die_type (die, type, cu);
12277 }
12278
12279 /* Extract all information from a DW_TAG_reference_type DIE and add to
12280 the user defined type vector. */
12281
12282 static struct type *
12283 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12284 {
12285 struct comp_unit_head *cu_header = &cu->header;
12286 struct type *type, *target_type;
12287 struct attribute *attr;
12288
12289 target_type = die_type (die, cu);
12290
12291 /* The die_type call above may have already set the type for this DIE. */
12292 type = get_die_type (die, cu);
12293 if (type)
12294 return type;
12295
12296 type = lookup_reference_type (target_type);
12297 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12298 if (attr)
12299 {
12300 TYPE_LENGTH (type) = DW_UNSND (attr);
12301 }
12302 else
12303 {
12304 TYPE_LENGTH (type) = cu_header->addr_size;
12305 }
12306 return set_die_type (die, type, cu);
12307 }
12308
12309 static struct type *
12310 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12311 {
12312 struct type *base_type, *cv_type;
12313
12314 base_type = die_type (die, cu);
12315
12316 /* The die_type call above may have already set the type for this DIE. */
12317 cv_type = get_die_type (die, cu);
12318 if (cv_type)
12319 return cv_type;
12320
12321 /* In case the const qualifier is applied to an array type, the element type
12322 is so qualified, not the array type (section 6.7.3 of C99). */
12323 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12324 {
12325 struct type *el_type, *inner_array;
12326
12327 base_type = copy_type (base_type);
12328 inner_array = base_type;
12329
12330 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12331 {
12332 TYPE_TARGET_TYPE (inner_array) =
12333 copy_type (TYPE_TARGET_TYPE (inner_array));
12334 inner_array = TYPE_TARGET_TYPE (inner_array);
12335 }
12336
12337 el_type = TYPE_TARGET_TYPE (inner_array);
12338 TYPE_TARGET_TYPE (inner_array) =
12339 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12340
12341 return set_die_type (die, base_type, cu);
12342 }
12343
12344 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12345 return set_die_type (die, cv_type, cu);
12346 }
12347
12348 static struct type *
12349 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12350 {
12351 struct type *base_type, *cv_type;
12352
12353 base_type = die_type (die, cu);
12354
12355 /* The die_type call above may have already set the type for this DIE. */
12356 cv_type = get_die_type (die, cu);
12357 if (cv_type)
12358 return cv_type;
12359
12360 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12361 return set_die_type (die, cv_type, cu);
12362 }
12363
12364 /* Handle DW_TAG_restrict_type. */
12365
12366 static struct type *
12367 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12368 {
12369 struct type *base_type, *cv_type;
12370
12371 base_type = die_type (die, cu);
12372
12373 /* The die_type call above may have already set the type for this DIE. */
12374 cv_type = get_die_type (die, cu);
12375 if (cv_type)
12376 return cv_type;
12377
12378 cv_type = make_restrict_type (base_type);
12379 return set_die_type (die, cv_type, cu);
12380 }
12381
12382 /* Extract all information from a DW_TAG_string_type DIE and add to
12383 the user defined type vector. It isn't really a user defined type,
12384 but it behaves like one, with other DIE's using an AT_user_def_type
12385 attribute to reference it. */
12386
12387 static struct type *
12388 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12389 {
12390 struct objfile *objfile = cu->objfile;
12391 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12392 struct type *type, *range_type, *index_type, *char_type;
12393 struct attribute *attr;
12394 unsigned int length;
12395
12396 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12397 if (attr)
12398 {
12399 length = DW_UNSND (attr);
12400 }
12401 else
12402 {
12403 /* Check for the DW_AT_byte_size attribute. */
12404 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12405 if (attr)
12406 {
12407 length = DW_UNSND (attr);
12408 }
12409 else
12410 {
12411 length = 1;
12412 }
12413 }
12414
12415 index_type = objfile_type (objfile)->builtin_int;
12416 range_type = create_range_type (NULL, index_type, 1, length);
12417 char_type = language_string_char_type (cu->language_defn, gdbarch);
12418 type = create_string_type (NULL, char_type, range_type);
12419
12420 return set_die_type (die, type, cu);
12421 }
12422
12423 /* Handle DIES due to C code like:
12424
12425 struct foo
12426 {
12427 int (*funcp)(int a, long l);
12428 int b;
12429 };
12430
12431 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12432
12433 static struct type *
12434 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12435 {
12436 struct objfile *objfile = cu->objfile;
12437 struct type *type; /* Type that this function returns. */
12438 struct type *ftype; /* Function that returns above type. */
12439 struct attribute *attr;
12440
12441 type = die_type (die, cu);
12442
12443 /* The die_type call above may have already set the type for this DIE. */
12444 ftype = get_die_type (die, cu);
12445 if (ftype)
12446 return ftype;
12447
12448 ftype = lookup_function_type (type);
12449
12450 /* All functions in C++, Pascal and Java have prototypes. */
12451 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12452 if ((attr && (DW_UNSND (attr) != 0))
12453 || cu->language == language_cplus
12454 || cu->language == language_java
12455 || cu->language == language_pascal)
12456 TYPE_PROTOTYPED (ftype) = 1;
12457 else if (producer_is_realview (cu->producer))
12458 /* RealView does not emit DW_AT_prototyped. We can not
12459 distinguish prototyped and unprototyped functions; default to
12460 prototyped, since that is more common in modern code (and
12461 RealView warns about unprototyped functions). */
12462 TYPE_PROTOTYPED (ftype) = 1;
12463
12464 /* Store the calling convention in the type if it's available in
12465 the subroutine die. Otherwise set the calling convention to
12466 the default value DW_CC_normal. */
12467 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12468 if (attr)
12469 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12470 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12471 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12472 else
12473 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12474
12475 /* We need to add the subroutine type to the die immediately so
12476 we don't infinitely recurse when dealing with parameters
12477 declared as the same subroutine type. */
12478 set_die_type (die, ftype, cu);
12479
12480 if (die->child != NULL)
12481 {
12482 struct type *void_type = objfile_type (objfile)->builtin_void;
12483 struct die_info *child_die;
12484 int nparams, iparams;
12485
12486 /* Count the number of parameters.
12487 FIXME: GDB currently ignores vararg functions, but knows about
12488 vararg member functions. */
12489 nparams = 0;
12490 child_die = die->child;
12491 while (child_die && child_die->tag)
12492 {
12493 if (child_die->tag == DW_TAG_formal_parameter)
12494 nparams++;
12495 else if (child_die->tag == DW_TAG_unspecified_parameters)
12496 TYPE_VARARGS (ftype) = 1;
12497 child_die = sibling_die (child_die);
12498 }
12499
12500 /* Allocate storage for parameters and fill them in. */
12501 TYPE_NFIELDS (ftype) = nparams;
12502 TYPE_FIELDS (ftype) = (struct field *)
12503 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12504
12505 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12506 even if we error out during the parameters reading below. */
12507 for (iparams = 0; iparams < nparams; iparams++)
12508 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12509
12510 iparams = 0;
12511 child_die = die->child;
12512 while (child_die && child_die->tag)
12513 {
12514 if (child_die->tag == DW_TAG_formal_parameter)
12515 {
12516 struct type *arg_type;
12517
12518 /* DWARF version 2 has no clean way to discern C++
12519 static and non-static member functions. G++ helps
12520 GDB by marking the first parameter for non-static
12521 member functions (which is the this pointer) as
12522 artificial. We pass this information to
12523 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12524
12525 DWARF version 3 added DW_AT_object_pointer, which GCC
12526 4.5 does not yet generate. */
12527 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12528 if (attr)
12529 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12530 else
12531 {
12532 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12533
12534 /* GCC/43521: In java, the formal parameter
12535 "this" is sometimes not marked with DW_AT_artificial. */
12536 if (cu->language == language_java)
12537 {
12538 const char *name = dwarf2_name (child_die, cu);
12539
12540 if (name && !strcmp (name, "this"))
12541 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12542 }
12543 }
12544 arg_type = die_type (child_die, cu);
12545
12546 /* RealView does not mark THIS as const, which the testsuite
12547 expects. GCC marks THIS as const in method definitions,
12548 but not in the class specifications (GCC PR 43053). */
12549 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12550 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12551 {
12552 int is_this = 0;
12553 struct dwarf2_cu *arg_cu = cu;
12554 const char *name = dwarf2_name (child_die, cu);
12555
12556 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12557 if (attr)
12558 {
12559 /* If the compiler emits this, use it. */
12560 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12561 is_this = 1;
12562 }
12563 else if (name && strcmp (name, "this") == 0)
12564 /* Function definitions will have the argument names. */
12565 is_this = 1;
12566 else if (name == NULL && iparams == 0)
12567 /* Declarations may not have the names, so like
12568 elsewhere in GDB, assume an artificial first
12569 argument is "this". */
12570 is_this = 1;
12571
12572 if (is_this)
12573 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12574 arg_type, 0);
12575 }
12576
12577 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12578 iparams++;
12579 }
12580 child_die = sibling_die (child_die);
12581 }
12582 }
12583
12584 return ftype;
12585 }
12586
12587 static struct type *
12588 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12589 {
12590 struct objfile *objfile = cu->objfile;
12591 const char *name = NULL;
12592 struct type *this_type, *target_type;
12593
12594 name = dwarf2_full_name (NULL, die, cu);
12595 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12596 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12597 TYPE_NAME (this_type) = name;
12598 set_die_type (die, this_type, cu);
12599 target_type = die_type (die, cu);
12600 if (target_type != this_type)
12601 TYPE_TARGET_TYPE (this_type) = target_type;
12602 else
12603 {
12604 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12605 spec and cause infinite loops in GDB. */
12606 complaint (&symfile_complaints,
12607 _("Self-referential DW_TAG_typedef "
12608 "- DIE at 0x%x [in module %s]"),
12609 die->offset.sect_off, objfile->name);
12610 TYPE_TARGET_TYPE (this_type) = NULL;
12611 }
12612 return this_type;
12613 }
12614
12615 /* Find a representation of a given base type and install
12616 it in the TYPE field of the die. */
12617
12618 static struct type *
12619 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12620 {
12621 struct objfile *objfile = cu->objfile;
12622 struct type *type;
12623 struct attribute *attr;
12624 int encoding = 0, size = 0;
12625 const char *name;
12626 enum type_code code = TYPE_CODE_INT;
12627 int type_flags = 0;
12628 struct type *target_type = NULL;
12629
12630 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12631 if (attr)
12632 {
12633 encoding = DW_UNSND (attr);
12634 }
12635 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12636 if (attr)
12637 {
12638 size = DW_UNSND (attr);
12639 }
12640 name = dwarf2_name (die, cu);
12641 if (!name)
12642 {
12643 complaint (&symfile_complaints,
12644 _("DW_AT_name missing from DW_TAG_base_type"));
12645 }
12646
12647 switch (encoding)
12648 {
12649 case DW_ATE_address:
12650 /* Turn DW_ATE_address into a void * pointer. */
12651 code = TYPE_CODE_PTR;
12652 type_flags |= TYPE_FLAG_UNSIGNED;
12653 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12654 break;
12655 case DW_ATE_boolean:
12656 code = TYPE_CODE_BOOL;
12657 type_flags |= TYPE_FLAG_UNSIGNED;
12658 break;
12659 case DW_ATE_complex_float:
12660 code = TYPE_CODE_COMPLEX;
12661 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12662 break;
12663 case DW_ATE_decimal_float:
12664 code = TYPE_CODE_DECFLOAT;
12665 break;
12666 case DW_ATE_float:
12667 code = TYPE_CODE_FLT;
12668 break;
12669 case DW_ATE_signed:
12670 break;
12671 case DW_ATE_unsigned:
12672 type_flags |= TYPE_FLAG_UNSIGNED;
12673 if (cu->language == language_fortran
12674 && name
12675 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12676 code = TYPE_CODE_CHAR;
12677 break;
12678 case DW_ATE_signed_char:
12679 if (cu->language == language_ada || cu->language == language_m2
12680 || cu->language == language_pascal
12681 || cu->language == language_fortran)
12682 code = TYPE_CODE_CHAR;
12683 break;
12684 case DW_ATE_unsigned_char:
12685 if (cu->language == language_ada || cu->language == language_m2
12686 || cu->language == language_pascal
12687 || cu->language == language_fortran)
12688 code = TYPE_CODE_CHAR;
12689 type_flags |= TYPE_FLAG_UNSIGNED;
12690 break;
12691 case DW_ATE_UTF:
12692 /* We just treat this as an integer and then recognize the
12693 type by name elsewhere. */
12694 break;
12695
12696 default:
12697 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12698 dwarf_type_encoding_name (encoding));
12699 break;
12700 }
12701
12702 type = init_type (code, size, type_flags, NULL, objfile);
12703 TYPE_NAME (type) = name;
12704 TYPE_TARGET_TYPE (type) = target_type;
12705
12706 if (name && strcmp (name, "char") == 0)
12707 TYPE_NOSIGN (type) = 1;
12708
12709 return set_die_type (die, type, cu);
12710 }
12711
12712 /* Read the given DW_AT_subrange DIE. */
12713
12714 static struct type *
12715 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12716 {
12717 struct type *base_type;
12718 struct type *range_type;
12719 struct attribute *attr;
12720 LONGEST low, high;
12721 int low_default_is_valid;
12722 const char *name;
12723 LONGEST negative_mask;
12724
12725 base_type = die_type (die, cu);
12726 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12727 check_typedef (base_type);
12728
12729 /* The die_type call above may have already set the type for this DIE. */
12730 range_type = get_die_type (die, cu);
12731 if (range_type)
12732 return range_type;
12733
12734 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12735 omitting DW_AT_lower_bound. */
12736 switch (cu->language)
12737 {
12738 case language_c:
12739 case language_cplus:
12740 low = 0;
12741 low_default_is_valid = 1;
12742 break;
12743 case language_fortran:
12744 low = 1;
12745 low_default_is_valid = 1;
12746 break;
12747 case language_d:
12748 case language_java:
12749 case language_objc:
12750 low = 0;
12751 low_default_is_valid = (cu->header.version >= 4);
12752 break;
12753 case language_ada:
12754 case language_m2:
12755 case language_pascal:
12756 low = 1;
12757 low_default_is_valid = (cu->header.version >= 4);
12758 break;
12759 default:
12760 low = 0;
12761 low_default_is_valid = 0;
12762 break;
12763 }
12764
12765 /* FIXME: For variable sized arrays either of these could be
12766 a variable rather than a constant value. We'll allow it,
12767 but we don't know how to handle it. */
12768 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12769 if (attr)
12770 low = dwarf2_get_attr_constant_value (attr, low);
12771 else if (!low_default_is_valid)
12772 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12773 "- DIE at 0x%x [in module %s]"),
12774 die->offset.sect_off, cu->objfile->name);
12775
12776 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12777 if (attr)
12778 {
12779 if (attr_form_is_block (attr) || is_ref_attr (attr))
12780 {
12781 /* GCC encodes arrays with unspecified or dynamic length
12782 with a DW_FORM_block1 attribute or a reference attribute.
12783 FIXME: GDB does not yet know how to handle dynamic
12784 arrays properly, treat them as arrays with unspecified
12785 length for now.
12786
12787 FIXME: jimb/2003-09-22: GDB does not really know
12788 how to handle arrays of unspecified length
12789 either; we just represent them as zero-length
12790 arrays. Choose an appropriate upper bound given
12791 the lower bound we've computed above. */
12792 high = low - 1;
12793 }
12794 else
12795 high = dwarf2_get_attr_constant_value (attr, 1);
12796 }
12797 else
12798 {
12799 attr = dwarf2_attr (die, DW_AT_count, cu);
12800 if (attr)
12801 {
12802 int count = dwarf2_get_attr_constant_value (attr, 1);
12803 high = low + count - 1;
12804 }
12805 else
12806 {
12807 /* Unspecified array length. */
12808 high = low - 1;
12809 }
12810 }
12811
12812 /* Dwarf-2 specifications explicitly allows to create subrange types
12813 without specifying a base type.
12814 In that case, the base type must be set to the type of
12815 the lower bound, upper bound or count, in that order, if any of these
12816 three attributes references an object that has a type.
12817 If no base type is found, the Dwarf-2 specifications say that
12818 a signed integer type of size equal to the size of an address should
12819 be used.
12820 For the following C code: `extern char gdb_int [];'
12821 GCC produces an empty range DIE.
12822 FIXME: muller/2010-05-28: Possible references to object for low bound,
12823 high bound or count are not yet handled by this code. */
12824 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12825 {
12826 struct objfile *objfile = cu->objfile;
12827 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12828 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12829 struct type *int_type = objfile_type (objfile)->builtin_int;
12830
12831 /* Test "int", "long int", and "long long int" objfile types,
12832 and select the first one having a size above or equal to the
12833 architecture address size. */
12834 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12835 base_type = int_type;
12836 else
12837 {
12838 int_type = objfile_type (objfile)->builtin_long;
12839 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12840 base_type = int_type;
12841 else
12842 {
12843 int_type = objfile_type (objfile)->builtin_long_long;
12844 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12845 base_type = int_type;
12846 }
12847 }
12848 }
12849
12850 negative_mask =
12851 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12852 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12853 low |= negative_mask;
12854 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12855 high |= negative_mask;
12856
12857 range_type = create_range_type (NULL, base_type, low, high);
12858
12859 /* Mark arrays with dynamic length at least as an array of unspecified
12860 length. GDB could check the boundary but before it gets implemented at
12861 least allow accessing the array elements. */
12862 if (attr && attr_form_is_block (attr))
12863 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12864
12865 /* Ada expects an empty array on no boundary attributes. */
12866 if (attr == NULL && cu->language != language_ada)
12867 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12868
12869 name = dwarf2_name (die, cu);
12870 if (name)
12871 TYPE_NAME (range_type) = name;
12872
12873 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12874 if (attr)
12875 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12876
12877 set_die_type (die, range_type, cu);
12878
12879 /* set_die_type should be already done. */
12880 set_descriptive_type (range_type, die, cu);
12881
12882 return range_type;
12883 }
12884
12885 static struct type *
12886 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12887 {
12888 struct type *type;
12889
12890 /* For now, we only support the C meaning of an unspecified type: void. */
12891
12892 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12893 TYPE_NAME (type) = dwarf2_name (die, cu);
12894
12895 return set_die_type (die, type, cu);
12896 }
12897
12898 /* Read a single die and all its descendents. Set the die's sibling
12899 field to NULL; set other fields in the die correctly, and set all
12900 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12901 location of the info_ptr after reading all of those dies. PARENT
12902 is the parent of the die in question. */
12903
12904 static struct die_info *
12905 read_die_and_children (const struct die_reader_specs *reader,
12906 gdb_byte *info_ptr,
12907 gdb_byte **new_info_ptr,
12908 struct die_info *parent)
12909 {
12910 struct die_info *die;
12911 gdb_byte *cur_ptr;
12912 int has_children;
12913
12914 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12915 if (die == NULL)
12916 {
12917 *new_info_ptr = cur_ptr;
12918 return NULL;
12919 }
12920 store_in_ref_table (die, reader->cu);
12921
12922 if (has_children)
12923 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12924 else
12925 {
12926 die->child = NULL;
12927 *new_info_ptr = cur_ptr;
12928 }
12929
12930 die->sibling = NULL;
12931 die->parent = parent;
12932 return die;
12933 }
12934
12935 /* Read a die, all of its descendents, and all of its siblings; set
12936 all of the fields of all of the dies correctly. Arguments are as
12937 in read_die_and_children. */
12938
12939 static struct die_info *
12940 read_die_and_siblings (const struct die_reader_specs *reader,
12941 gdb_byte *info_ptr,
12942 gdb_byte **new_info_ptr,
12943 struct die_info *parent)
12944 {
12945 struct die_info *first_die, *last_sibling;
12946 gdb_byte *cur_ptr;
12947
12948 cur_ptr = info_ptr;
12949 first_die = last_sibling = NULL;
12950
12951 while (1)
12952 {
12953 struct die_info *die
12954 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12955
12956 if (die == NULL)
12957 {
12958 *new_info_ptr = cur_ptr;
12959 return first_die;
12960 }
12961
12962 if (!first_die)
12963 first_die = die;
12964 else
12965 last_sibling->sibling = die;
12966
12967 last_sibling = die;
12968 }
12969 }
12970
12971 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12972 attributes.
12973 The caller is responsible for filling in the extra attributes
12974 and updating (*DIEP)->num_attrs.
12975 Set DIEP to point to a newly allocated die with its information,
12976 except for its child, sibling, and parent fields.
12977 Set HAS_CHILDREN to tell whether the die has children or not. */
12978
12979 static gdb_byte *
12980 read_full_die_1 (const struct die_reader_specs *reader,
12981 struct die_info **diep, gdb_byte *info_ptr,
12982 int *has_children, int num_extra_attrs)
12983 {
12984 unsigned int abbrev_number, bytes_read, i;
12985 sect_offset offset;
12986 struct abbrev_info *abbrev;
12987 struct die_info *die;
12988 struct dwarf2_cu *cu = reader->cu;
12989 bfd *abfd = reader->abfd;
12990
12991 offset.sect_off = info_ptr - reader->buffer;
12992 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12993 info_ptr += bytes_read;
12994 if (!abbrev_number)
12995 {
12996 *diep = NULL;
12997 *has_children = 0;
12998 return info_ptr;
12999 }
13000
13001 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13002 if (!abbrev)
13003 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13004 abbrev_number,
13005 bfd_get_filename (abfd));
13006
13007 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13008 die->offset = offset;
13009 die->tag = abbrev->tag;
13010 die->abbrev = abbrev_number;
13011
13012 /* Make the result usable.
13013 The caller needs to update num_attrs after adding the extra
13014 attributes. */
13015 die->num_attrs = abbrev->num_attrs;
13016
13017 for (i = 0; i < abbrev->num_attrs; ++i)
13018 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13019 info_ptr);
13020
13021 *diep = die;
13022 *has_children = abbrev->has_children;
13023 return info_ptr;
13024 }
13025
13026 /* Read a die and all its attributes.
13027 Set DIEP to point to a newly allocated die with its information,
13028 except for its child, sibling, and parent fields.
13029 Set HAS_CHILDREN to tell whether the die has children or not. */
13030
13031 static gdb_byte *
13032 read_full_die (const struct die_reader_specs *reader,
13033 struct die_info **diep, gdb_byte *info_ptr,
13034 int *has_children)
13035 {
13036 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13037 }
13038 \f
13039 /* Abbreviation tables.
13040
13041 In DWARF version 2, the description of the debugging information is
13042 stored in a separate .debug_abbrev section. Before we read any
13043 dies from a section we read in all abbreviations and install them
13044 in a hash table. */
13045
13046 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13047
13048 static struct abbrev_info *
13049 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13050 {
13051 struct abbrev_info *abbrev;
13052
13053 abbrev = (struct abbrev_info *)
13054 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13055 memset (abbrev, 0, sizeof (struct abbrev_info));
13056 return abbrev;
13057 }
13058
13059 /* Add an abbreviation to the table. */
13060
13061 static void
13062 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13063 unsigned int abbrev_number,
13064 struct abbrev_info *abbrev)
13065 {
13066 unsigned int hash_number;
13067
13068 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13069 abbrev->next = abbrev_table->abbrevs[hash_number];
13070 abbrev_table->abbrevs[hash_number] = abbrev;
13071 }
13072
13073 /* Look up an abbrev in the table.
13074 Returns NULL if the abbrev is not found. */
13075
13076 static struct abbrev_info *
13077 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13078 unsigned int abbrev_number)
13079 {
13080 unsigned int hash_number;
13081 struct abbrev_info *abbrev;
13082
13083 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13084 abbrev = abbrev_table->abbrevs[hash_number];
13085
13086 while (abbrev)
13087 {
13088 if (abbrev->number == abbrev_number)
13089 return abbrev;
13090 abbrev = abbrev->next;
13091 }
13092 return NULL;
13093 }
13094
13095 /* Read in an abbrev table. */
13096
13097 static struct abbrev_table *
13098 abbrev_table_read_table (struct dwarf2_section_info *section,
13099 sect_offset offset)
13100 {
13101 struct objfile *objfile = dwarf2_per_objfile->objfile;
13102 bfd *abfd = section->asection->owner;
13103 struct abbrev_table *abbrev_table;
13104 gdb_byte *abbrev_ptr;
13105 struct abbrev_info *cur_abbrev;
13106 unsigned int abbrev_number, bytes_read, abbrev_name;
13107 unsigned int abbrev_form;
13108 struct attr_abbrev *cur_attrs;
13109 unsigned int allocated_attrs;
13110
13111 abbrev_table = XMALLOC (struct abbrev_table);
13112 abbrev_table->offset = offset;
13113 obstack_init (&abbrev_table->abbrev_obstack);
13114 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13115 (ABBREV_HASH_SIZE
13116 * sizeof (struct abbrev_info *)));
13117 memset (abbrev_table->abbrevs, 0,
13118 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13119
13120 dwarf2_read_section (objfile, section);
13121 abbrev_ptr = section->buffer + offset.sect_off;
13122 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13123 abbrev_ptr += bytes_read;
13124
13125 allocated_attrs = ATTR_ALLOC_CHUNK;
13126 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13127
13128 /* Loop until we reach an abbrev number of 0. */
13129 while (abbrev_number)
13130 {
13131 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13132
13133 /* read in abbrev header */
13134 cur_abbrev->number = abbrev_number;
13135 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13136 abbrev_ptr += bytes_read;
13137 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13138 abbrev_ptr += 1;
13139
13140 /* now read in declarations */
13141 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13142 abbrev_ptr += bytes_read;
13143 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13144 abbrev_ptr += bytes_read;
13145 while (abbrev_name)
13146 {
13147 if (cur_abbrev->num_attrs == allocated_attrs)
13148 {
13149 allocated_attrs += ATTR_ALLOC_CHUNK;
13150 cur_attrs
13151 = xrealloc (cur_attrs, (allocated_attrs
13152 * sizeof (struct attr_abbrev)));
13153 }
13154
13155 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13156 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13157 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13158 abbrev_ptr += bytes_read;
13159 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13160 abbrev_ptr += bytes_read;
13161 }
13162
13163 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13164 (cur_abbrev->num_attrs
13165 * sizeof (struct attr_abbrev)));
13166 memcpy (cur_abbrev->attrs, cur_attrs,
13167 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13168
13169 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13170
13171 /* Get next abbreviation.
13172 Under Irix6 the abbreviations for a compilation unit are not
13173 always properly terminated with an abbrev number of 0.
13174 Exit loop if we encounter an abbreviation which we have
13175 already read (which means we are about to read the abbreviations
13176 for the next compile unit) or if the end of the abbreviation
13177 table is reached. */
13178 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13179 break;
13180 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13181 abbrev_ptr += bytes_read;
13182 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13183 break;
13184 }
13185
13186 xfree (cur_attrs);
13187 return abbrev_table;
13188 }
13189
13190 /* Free the resources held by ABBREV_TABLE. */
13191
13192 static void
13193 abbrev_table_free (struct abbrev_table *abbrev_table)
13194 {
13195 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13196 xfree (abbrev_table);
13197 }
13198
13199 /* Same as abbrev_table_free but as a cleanup.
13200 We pass in a pointer to the pointer to the table so that we can
13201 set the pointer to NULL when we're done. It also simplifies
13202 build_type_unit_groups. */
13203
13204 static void
13205 abbrev_table_free_cleanup (void *table_ptr)
13206 {
13207 struct abbrev_table **abbrev_table_ptr = table_ptr;
13208
13209 if (*abbrev_table_ptr != NULL)
13210 abbrev_table_free (*abbrev_table_ptr);
13211 *abbrev_table_ptr = NULL;
13212 }
13213
13214 /* Read the abbrev table for CU from ABBREV_SECTION. */
13215
13216 static void
13217 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13218 struct dwarf2_section_info *abbrev_section)
13219 {
13220 cu->abbrev_table =
13221 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13222 }
13223
13224 /* Release the memory used by the abbrev table for a compilation unit. */
13225
13226 static void
13227 dwarf2_free_abbrev_table (void *ptr_to_cu)
13228 {
13229 struct dwarf2_cu *cu = ptr_to_cu;
13230
13231 abbrev_table_free (cu->abbrev_table);
13232 /* Set this to NULL so that we SEGV if we try to read it later,
13233 and also because free_comp_unit verifies this is NULL. */
13234 cu->abbrev_table = NULL;
13235 }
13236 \f
13237 /* Returns nonzero if TAG represents a type that we might generate a partial
13238 symbol for. */
13239
13240 static int
13241 is_type_tag_for_partial (int tag)
13242 {
13243 switch (tag)
13244 {
13245 #if 0
13246 /* Some types that would be reasonable to generate partial symbols for,
13247 that we don't at present. */
13248 case DW_TAG_array_type:
13249 case DW_TAG_file_type:
13250 case DW_TAG_ptr_to_member_type:
13251 case DW_TAG_set_type:
13252 case DW_TAG_string_type:
13253 case DW_TAG_subroutine_type:
13254 #endif
13255 case DW_TAG_base_type:
13256 case DW_TAG_class_type:
13257 case DW_TAG_interface_type:
13258 case DW_TAG_enumeration_type:
13259 case DW_TAG_structure_type:
13260 case DW_TAG_subrange_type:
13261 case DW_TAG_typedef:
13262 case DW_TAG_union_type:
13263 return 1;
13264 default:
13265 return 0;
13266 }
13267 }
13268
13269 /* Load all DIEs that are interesting for partial symbols into memory. */
13270
13271 static struct partial_die_info *
13272 load_partial_dies (const struct die_reader_specs *reader,
13273 gdb_byte *info_ptr, int building_psymtab)
13274 {
13275 struct dwarf2_cu *cu = reader->cu;
13276 struct objfile *objfile = cu->objfile;
13277 struct partial_die_info *part_die;
13278 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13279 struct abbrev_info *abbrev;
13280 unsigned int bytes_read;
13281 unsigned int load_all = 0;
13282 int nesting_level = 1;
13283
13284 parent_die = NULL;
13285 last_die = NULL;
13286
13287 gdb_assert (cu->per_cu != NULL);
13288 if (cu->per_cu->load_all_dies)
13289 load_all = 1;
13290
13291 cu->partial_dies
13292 = htab_create_alloc_ex (cu->header.length / 12,
13293 partial_die_hash,
13294 partial_die_eq,
13295 NULL,
13296 &cu->comp_unit_obstack,
13297 hashtab_obstack_allocate,
13298 dummy_obstack_deallocate);
13299
13300 part_die = obstack_alloc (&cu->comp_unit_obstack,
13301 sizeof (struct partial_die_info));
13302
13303 while (1)
13304 {
13305 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13306
13307 /* A NULL abbrev means the end of a series of children. */
13308 if (abbrev == NULL)
13309 {
13310 if (--nesting_level == 0)
13311 {
13312 /* PART_DIE was probably the last thing allocated on the
13313 comp_unit_obstack, so we could call obstack_free
13314 here. We don't do that because the waste is small,
13315 and will be cleaned up when we're done with this
13316 compilation unit. This way, we're also more robust
13317 against other users of the comp_unit_obstack. */
13318 return first_die;
13319 }
13320 info_ptr += bytes_read;
13321 last_die = parent_die;
13322 parent_die = parent_die->die_parent;
13323 continue;
13324 }
13325
13326 /* Check for template arguments. We never save these; if
13327 they're seen, we just mark the parent, and go on our way. */
13328 if (parent_die != NULL
13329 && cu->language == language_cplus
13330 && (abbrev->tag == DW_TAG_template_type_param
13331 || abbrev->tag == DW_TAG_template_value_param))
13332 {
13333 parent_die->has_template_arguments = 1;
13334
13335 if (!load_all)
13336 {
13337 /* We don't need a partial DIE for the template argument. */
13338 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13339 continue;
13340 }
13341 }
13342
13343 /* We only recurse into c++ subprograms looking for template arguments.
13344 Skip their other children. */
13345 if (!load_all
13346 && cu->language == language_cplus
13347 && parent_die != NULL
13348 && parent_die->tag == DW_TAG_subprogram)
13349 {
13350 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13351 continue;
13352 }
13353
13354 /* Check whether this DIE is interesting enough to save. Normally
13355 we would not be interested in members here, but there may be
13356 later variables referencing them via DW_AT_specification (for
13357 static members). */
13358 if (!load_all
13359 && !is_type_tag_for_partial (abbrev->tag)
13360 && abbrev->tag != DW_TAG_constant
13361 && abbrev->tag != DW_TAG_enumerator
13362 && abbrev->tag != DW_TAG_subprogram
13363 && abbrev->tag != DW_TAG_lexical_block
13364 && abbrev->tag != DW_TAG_variable
13365 && abbrev->tag != DW_TAG_namespace
13366 && abbrev->tag != DW_TAG_module
13367 && abbrev->tag != DW_TAG_member
13368 && abbrev->tag != DW_TAG_imported_unit)
13369 {
13370 /* Otherwise we skip to the next sibling, if any. */
13371 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13372 continue;
13373 }
13374
13375 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13376 info_ptr);
13377
13378 /* This two-pass algorithm for processing partial symbols has a
13379 high cost in cache pressure. Thus, handle some simple cases
13380 here which cover the majority of C partial symbols. DIEs
13381 which neither have specification tags in them, nor could have
13382 specification tags elsewhere pointing at them, can simply be
13383 processed and discarded.
13384
13385 This segment is also optional; scan_partial_symbols and
13386 add_partial_symbol will handle these DIEs if we chain
13387 them in normally. When compilers which do not emit large
13388 quantities of duplicate debug information are more common,
13389 this code can probably be removed. */
13390
13391 /* Any complete simple types at the top level (pretty much all
13392 of them, for a language without namespaces), can be processed
13393 directly. */
13394 if (parent_die == NULL
13395 && part_die->has_specification == 0
13396 && part_die->is_declaration == 0
13397 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13398 || part_die->tag == DW_TAG_base_type
13399 || part_die->tag == DW_TAG_subrange_type))
13400 {
13401 if (building_psymtab && part_die->name != NULL)
13402 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13403 VAR_DOMAIN, LOC_TYPEDEF,
13404 &objfile->static_psymbols,
13405 0, (CORE_ADDR) 0, cu->language, objfile);
13406 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13407 continue;
13408 }
13409
13410 /* The exception for DW_TAG_typedef with has_children above is
13411 a workaround of GCC PR debug/47510. In the case of this complaint
13412 type_name_no_tag_or_error will error on such types later.
13413
13414 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13415 it could not find the child DIEs referenced later, this is checked
13416 above. In correct DWARF DW_TAG_typedef should have no children. */
13417
13418 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13419 complaint (&symfile_complaints,
13420 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13421 "- DIE at 0x%x [in module %s]"),
13422 part_die->offset.sect_off, objfile->name);
13423
13424 /* If we're at the second level, and we're an enumerator, and
13425 our parent has no specification (meaning possibly lives in a
13426 namespace elsewhere), then we can add the partial symbol now
13427 instead of queueing it. */
13428 if (part_die->tag == DW_TAG_enumerator
13429 && parent_die != NULL
13430 && parent_die->die_parent == NULL
13431 && parent_die->tag == DW_TAG_enumeration_type
13432 && parent_die->has_specification == 0)
13433 {
13434 if (part_die->name == NULL)
13435 complaint (&symfile_complaints,
13436 _("malformed enumerator DIE ignored"));
13437 else if (building_psymtab)
13438 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13439 VAR_DOMAIN, LOC_CONST,
13440 (cu->language == language_cplus
13441 || cu->language == language_java)
13442 ? &objfile->global_psymbols
13443 : &objfile->static_psymbols,
13444 0, (CORE_ADDR) 0, cu->language, objfile);
13445
13446 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13447 continue;
13448 }
13449
13450 /* We'll save this DIE so link it in. */
13451 part_die->die_parent = parent_die;
13452 part_die->die_sibling = NULL;
13453 part_die->die_child = NULL;
13454
13455 if (last_die && last_die == parent_die)
13456 last_die->die_child = part_die;
13457 else if (last_die)
13458 last_die->die_sibling = part_die;
13459
13460 last_die = part_die;
13461
13462 if (first_die == NULL)
13463 first_die = part_die;
13464
13465 /* Maybe add the DIE to the hash table. Not all DIEs that we
13466 find interesting need to be in the hash table, because we
13467 also have the parent/sibling/child chains; only those that we
13468 might refer to by offset later during partial symbol reading.
13469
13470 For now this means things that might have be the target of a
13471 DW_AT_specification, DW_AT_abstract_origin, or
13472 DW_AT_extension. DW_AT_extension will refer only to
13473 namespaces; DW_AT_abstract_origin refers to functions (and
13474 many things under the function DIE, but we do not recurse
13475 into function DIEs during partial symbol reading) and
13476 possibly variables as well; DW_AT_specification refers to
13477 declarations. Declarations ought to have the DW_AT_declaration
13478 flag. It happens that GCC forgets to put it in sometimes, but
13479 only for functions, not for types.
13480
13481 Adding more things than necessary to the hash table is harmless
13482 except for the performance cost. Adding too few will result in
13483 wasted time in find_partial_die, when we reread the compilation
13484 unit with load_all_dies set. */
13485
13486 if (load_all
13487 || abbrev->tag == DW_TAG_constant
13488 || abbrev->tag == DW_TAG_subprogram
13489 || abbrev->tag == DW_TAG_variable
13490 || abbrev->tag == DW_TAG_namespace
13491 || part_die->is_declaration)
13492 {
13493 void **slot;
13494
13495 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13496 part_die->offset.sect_off, INSERT);
13497 *slot = part_die;
13498 }
13499
13500 part_die = obstack_alloc (&cu->comp_unit_obstack,
13501 sizeof (struct partial_die_info));
13502
13503 /* For some DIEs we want to follow their children (if any). For C
13504 we have no reason to follow the children of structures; for other
13505 languages we have to, so that we can get at method physnames
13506 to infer fully qualified class names, for DW_AT_specification,
13507 and for C++ template arguments. For C++, we also look one level
13508 inside functions to find template arguments (if the name of the
13509 function does not already contain the template arguments).
13510
13511 For Ada, we need to scan the children of subprograms and lexical
13512 blocks as well because Ada allows the definition of nested
13513 entities that could be interesting for the debugger, such as
13514 nested subprograms for instance. */
13515 if (last_die->has_children
13516 && (load_all
13517 || last_die->tag == DW_TAG_namespace
13518 || last_die->tag == DW_TAG_module
13519 || last_die->tag == DW_TAG_enumeration_type
13520 || (cu->language == language_cplus
13521 && last_die->tag == DW_TAG_subprogram
13522 && (last_die->name == NULL
13523 || strchr (last_die->name, '<') == NULL))
13524 || (cu->language != language_c
13525 && (last_die->tag == DW_TAG_class_type
13526 || last_die->tag == DW_TAG_interface_type
13527 || last_die->tag == DW_TAG_structure_type
13528 || last_die->tag == DW_TAG_union_type))
13529 || (cu->language == language_ada
13530 && (last_die->tag == DW_TAG_subprogram
13531 || last_die->tag == DW_TAG_lexical_block))))
13532 {
13533 nesting_level++;
13534 parent_die = last_die;
13535 continue;
13536 }
13537
13538 /* Otherwise we skip to the next sibling, if any. */
13539 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13540
13541 /* Back to the top, do it again. */
13542 }
13543 }
13544
13545 /* Read a minimal amount of information into the minimal die structure. */
13546
13547 static gdb_byte *
13548 read_partial_die (const struct die_reader_specs *reader,
13549 struct partial_die_info *part_die,
13550 struct abbrev_info *abbrev, unsigned int abbrev_len,
13551 gdb_byte *info_ptr)
13552 {
13553 struct dwarf2_cu *cu = reader->cu;
13554 struct objfile *objfile = cu->objfile;
13555 gdb_byte *buffer = reader->buffer;
13556 unsigned int i;
13557 struct attribute attr;
13558 int has_low_pc_attr = 0;
13559 int has_high_pc_attr = 0;
13560 int high_pc_relative = 0;
13561
13562 memset (part_die, 0, sizeof (struct partial_die_info));
13563
13564 part_die->offset.sect_off = info_ptr - buffer;
13565
13566 info_ptr += abbrev_len;
13567
13568 if (abbrev == NULL)
13569 return info_ptr;
13570
13571 part_die->tag = abbrev->tag;
13572 part_die->has_children = abbrev->has_children;
13573
13574 for (i = 0; i < abbrev->num_attrs; ++i)
13575 {
13576 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13577
13578 /* Store the data if it is of an attribute we want to keep in a
13579 partial symbol table. */
13580 switch (attr.name)
13581 {
13582 case DW_AT_name:
13583 switch (part_die->tag)
13584 {
13585 case DW_TAG_compile_unit:
13586 case DW_TAG_partial_unit:
13587 case DW_TAG_type_unit:
13588 /* Compilation units have a DW_AT_name that is a filename, not
13589 a source language identifier. */
13590 case DW_TAG_enumeration_type:
13591 case DW_TAG_enumerator:
13592 /* These tags always have simple identifiers already; no need
13593 to canonicalize them. */
13594 part_die->name = DW_STRING (&attr);
13595 break;
13596 default:
13597 part_die->name
13598 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13599 &objfile->objfile_obstack);
13600 break;
13601 }
13602 break;
13603 case DW_AT_linkage_name:
13604 case DW_AT_MIPS_linkage_name:
13605 /* Note that both forms of linkage name might appear. We
13606 assume they will be the same, and we only store the last
13607 one we see. */
13608 if (cu->language == language_ada)
13609 part_die->name = DW_STRING (&attr);
13610 part_die->linkage_name = DW_STRING (&attr);
13611 break;
13612 case DW_AT_low_pc:
13613 has_low_pc_attr = 1;
13614 part_die->lowpc = DW_ADDR (&attr);
13615 break;
13616 case DW_AT_high_pc:
13617 has_high_pc_attr = 1;
13618 if (attr.form == DW_FORM_addr
13619 || attr.form == DW_FORM_GNU_addr_index)
13620 part_die->highpc = DW_ADDR (&attr);
13621 else
13622 {
13623 high_pc_relative = 1;
13624 part_die->highpc = DW_UNSND (&attr);
13625 }
13626 break;
13627 case DW_AT_location:
13628 /* Support the .debug_loc offsets. */
13629 if (attr_form_is_block (&attr))
13630 {
13631 part_die->d.locdesc = DW_BLOCK (&attr);
13632 }
13633 else if (attr_form_is_section_offset (&attr))
13634 {
13635 dwarf2_complex_location_expr_complaint ();
13636 }
13637 else
13638 {
13639 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13640 "partial symbol information");
13641 }
13642 break;
13643 case DW_AT_external:
13644 part_die->is_external = DW_UNSND (&attr);
13645 break;
13646 case DW_AT_declaration:
13647 part_die->is_declaration = DW_UNSND (&attr);
13648 break;
13649 case DW_AT_type:
13650 part_die->has_type = 1;
13651 break;
13652 case DW_AT_abstract_origin:
13653 case DW_AT_specification:
13654 case DW_AT_extension:
13655 part_die->has_specification = 1;
13656 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13657 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13658 || cu->per_cu->is_dwz);
13659 break;
13660 case DW_AT_sibling:
13661 /* Ignore absolute siblings, they might point outside of
13662 the current compile unit. */
13663 if (attr.form == DW_FORM_ref_addr)
13664 complaint (&symfile_complaints,
13665 _("ignoring absolute DW_AT_sibling"));
13666 else
13667 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13668 break;
13669 case DW_AT_byte_size:
13670 part_die->has_byte_size = 1;
13671 break;
13672 case DW_AT_calling_convention:
13673 /* DWARF doesn't provide a way to identify a program's source-level
13674 entry point. DW_AT_calling_convention attributes are only meant
13675 to describe functions' calling conventions.
13676
13677 However, because it's a necessary piece of information in
13678 Fortran, and because DW_CC_program is the only piece of debugging
13679 information whose definition refers to a 'main program' at all,
13680 several compilers have begun marking Fortran main programs with
13681 DW_CC_program --- even when those functions use the standard
13682 calling conventions.
13683
13684 So until DWARF specifies a way to provide this information and
13685 compilers pick up the new representation, we'll support this
13686 practice. */
13687 if (DW_UNSND (&attr) == DW_CC_program
13688 && cu->language == language_fortran)
13689 {
13690 set_main_name (part_die->name);
13691
13692 /* As this DIE has a static linkage the name would be difficult
13693 to look up later. */
13694 language_of_main = language_fortran;
13695 }
13696 break;
13697 case DW_AT_inline:
13698 if (DW_UNSND (&attr) == DW_INL_inlined
13699 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13700 part_die->may_be_inlined = 1;
13701 break;
13702
13703 case DW_AT_import:
13704 if (part_die->tag == DW_TAG_imported_unit)
13705 {
13706 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13707 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13708 || cu->per_cu->is_dwz);
13709 }
13710 break;
13711
13712 default:
13713 break;
13714 }
13715 }
13716
13717 if (high_pc_relative)
13718 part_die->highpc += part_die->lowpc;
13719
13720 if (has_low_pc_attr && has_high_pc_attr)
13721 {
13722 /* When using the GNU linker, .gnu.linkonce. sections are used to
13723 eliminate duplicate copies of functions and vtables and such.
13724 The linker will arbitrarily choose one and discard the others.
13725 The AT_*_pc values for such functions refer to local labels in
13726 these sections. If the section from that file was discarded, the
13727 labels are not in the output, so the relocs get a value of 0.
13728 If this is a discarded function, mark the pc bounds as invalid,
13729 so that GDB will ignore it. */
13730 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13731 {
13732 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13733
13734 complaint (&symfile_complaints,
13735 _("DW_AT_low_pc %s is zero "
13736 "for DIE at 0x%x [in module %s]"),
13737 paddress (gdbarch, part_die->lowpc),
13738 part_die->offset.sect_off, objfile->name);
13739 }
13740 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13741 else if (part_die->lowpc >= part_die->highpc)
13742 {
13743 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13744
13745 complaint (&symfile_complaints,
13746 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13747 "for DIE at 0x%x [in module %s]"),
13748 paddress (gdbarch, part_die->lowpc),
13749 paddress (gdbarch, part_die->highpc),
13750 part_die->offset.sect_off, objfile->name);
13751 }
13752 else
13753 part_die->has_pc_info = 1;
13754 }
13755
13756 return info_ptr;
13757 }
13758
13759 /* Find a cached partial DIE at OFFSET in CU. */
13760
13761 static struct partial_die_info *
13762 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13763 {
13764 struct partial_die_info *lookup_die = NULL;
13765 struct partial_die_info part_die;
13766
13767 part_die.offset = offset;
13768 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13769 offset.sect_off);
13770
13771 return lookup_die;
13772 }
13773
13774 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13775 except in the case of .debug_types DIEs which do not reference
13776 outside their CU (they do however referencing other types via
13777 DW_FORM_ref_sig8). */
13778
13779 static struct partial_die_info *
13780 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13781 {
13782 struct objfile *objfile = cu->objfile;
13783 struct dwarf2_per_cu_data *per_cu = NULL;
13784 struct partial_die_info *pd = NULL;
13785
13786 if (offset_in_dwz == cu->per_cu->is_dwz
13787 && offset_in_cu_p (&cu->header, offset))
13788 {
13789 pd = find_partial_die_in_comp_unit (offset, cu);
13790 if (pd != NULL)
13791 return pd;
13792 /* We missed recording what we needed.
13793 Load all dies and try again. */
13794 per_cu = cu->per_cu;
13795 }
13796 else
13797 {
13798 /* TUs don't reference other CUs/TUs (except via type signatures). */
13799 if (cu->per_cu->is_debug_types)
13800 {
13801 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13802 " external reference to offset 0x%lx [in module %s].\n"),
13803 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13804 bfd_get_filename (objfile->obfd));
13805 }
13806 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13807 objfile);
13808
13809 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13810 load_partial_comp_unit (per_cu);
13811
13812 per_cu->cu->last_used = 0;
13813 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13814 }
13815
13816 /* If we didn't find it, and not all dies have been loaded,
13817 load them all and try again. */
13818
13819 if (pd == NULL && per_cu->load_all_dies == 0)
13820 {
13821 per_cu->load_all_dies = 1;
13822
13823 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13824 THIS_CU->cu may already be in use. So we can't just free it and
13825 replace its DIEs with the ones we read in. Instead, we leave those
13826 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13827 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13828 set. */
13829 load_partial_comp_unit (per_cu);
13830
13831 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13832 }
13833
13834 if (pd == NULL)
13835 internal_error (__FILE__, __LINE__,
13836 _("could not find partial DIE 0x%x "
13837 "in cache [from module %s]\n"),
13838 offset.sect_off, bfd_get_filename (objfile->obfd));
13839 return pd;
13840 }
13841
13842 /* See if we can figure out if the class lives in a namespace. We do
13843 this by looking for a member function; its demangled name will
13844 contain namespace info, if there is any. */
13845
13846 static void
13847 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13848 struct dwarf2_cu *cu)
13849 {
13850 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13851 what template types look like, because the demangler
13852 frequently doesn't give the same name as the debug info. We
13853 could fix this by only using the demangled name to get the
13854 prefix (but see comment in read_structure_type). */
13855
13856 struct partial_die_info *real_pdi;
13857 struct partial_die_info *child_pdi;
13858
13859 /* If this DIE (this DIE's specification, if any) has a parent, then
13860 we should not do this. We'll prepend the parent's fully qualified
13861 name when we create the partial symbol. */
13862
13863 real_pdi = struct_pdi;
13864 while (real_pdi->has_specification)
13865 real_pdi = find_partial_die (real_pdi->spec_offset,
13866 real_pdi->spec_is_dwz, cu);
13867
13868 if (real_pdi->die_parent != NULL)
13869 return;
13870
13871 for (child_pdi = struct_pdi->die_child;
13872 child_pdi != NULL;
13873 child_pdi = child_pdi->die_sibling)
13874 {
13875 if (child_pdi->tag == DW_TAG_subprogram
13876 && child_pdi->linkage_name != NULL)
13877 {
13878 char *actual_class_name
13879 = language_class_name_from_physname (cu->language_defn,
13880 child_pdi->linkage_name);
13881 if (actual_class_name != NULL)
13882 {
13883 struct_pdi->name
13884 = obstack_copy0 (&cu->objfile->objfile_obstack,
13885 actual_class_name,
13886 strlen (actual_class_name));
13887 xfree (actual_class_name);
13888 }
13889 break;
13890 }
13891 }
13892 }
13893
13894 /* Adjust PART_DIE before generating a symbol for it. This function
13895 may set the is_external flag or change the DIE's name. */
13896
13897 static void
13898 fixup_partial_die (struct partial_die_info *part_die,
13899 struct dwarf2_cu *cu)
13900 {
13901 /* Once we've fixed up a die, there's no point in doing so again.
13902 This also avoids a memory leak if we were to call
13903 guess_partial_die_structure_name multiple times. */
13904 if (part_die->fixup_called)
13905 return;
13906
13907 /* If we found a reference attribute and the DIE has no name, try
13908 to find a name in the referred to DIE. */
13909
13910 if (part_die->name == NULL && part_die->has_specification)
13911 {
13912 struct partial_die_info *spec_die;
13913
13914 spec_die = find_partial_die (part_die->spec_offset,
13915 part_die->spec_is_dwz, cu);
13916
13917 fixup_partial_die (spec_die, cu);
13918
13919 if (spec_die->name)
13920 {
13921 part_die->name = spec_die->name;
13922
13923 /* Copy DW_AT_external attribute if it is set. */
13924 if (spec_die->is_external)
13925 part_die->is_external = spec_die->is_external;
13926 }
13927 }
13928
13929 /* Set default names for some unnamed DIEs. */
13930
13931 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13932 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13933
13934 /* If there is no parent die to provide a namespace, and there are
13935 children, see if we can determine the namespace from their linkage
13936 name. */
13937 if (cu->language == language_cplus
13938 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13939 && part_die->die_parent == NULL
13940 && part_die->has_children
13941 && (part_die->tag == DW_TAG_class_type
13942 || part_die->tag == DW_TAG_structure_type
13943 || part_die->tag == DW_TAG_union_type))
13944 guess_partial_die_structure_name (part_die, cu);
13945
13946 /* GCC might emit a nameless struct or union that has a linkage
13947 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13948 if (part_die->name == NULL
13949 && (part_die->tag == DW_TAG_class_type
13950 || part_die->tag == DW_TAG_interface_type
13951 || part_die->tag == DW_TAG_structure_type
13952 || part_die->tag == DW_TAG_union_type)
13953 && part_die->linkage_name != NULL)
13954 {
13955 char *demangled;
13956
13957 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13958 if (demangled)
13959 {
13960 const char *base;
13961
13962 /* Strip any leading namespaces/classes, keep only the base name.
13963 DW_AT_name for named DIEs does not contain the prefixes. */
13964 base = strrchr (demangled, ':');
13965 if (base && base > demangled && base[-1] == ':')
13966 base++;
13967 else
13968 base = demangled;
13969
13970 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
13971 base, strlen (base));
13972 xfree (demangled);
13973 }
13974 }
13975
13976 part_die->fixup_called = 1;
13977 }
13978
13979 /* Read an attribute value described by an attribute form. */
13980
13981 static gdb_byte *
13982 read_attribute_value (const struct die_reader_specs *reader,
13983 struct attribute *attr, unsigned form,
13984 gdb_byte *info_ptr)
13985 {
13986 struct dwarf2_cu *cu = reader->cu;
13987 bfd *abfd = reader->abfd;
13988 struct comp_unit_head *cu_header = &cu->header;
13989 unsigned int bytes_read;
13990 struct dwarf_block *blk;
13991
13992 attr->form = form;
13993 switch (form)
13994 {
13995 case DW_FORM_ref_addr:
13996 if (cu->header.version == 2)
13997 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13998 else
13999 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14000 &cu->header, &bytes_read);
14001 info_ptr += bytes_read;
14002 break;
14003 case DW_FORM_GNU_ref_alt:
14004 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14005 info_ptr += bytes_read;
14006 break;
14007 case DW_FORM_addr:
14008 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14009 info_ptr += bytes_read;
14010 break;
14011 case DW_FORM_block2:
14012 blk = dwarf_alloc_block (cu);
14013 blk->size = read_2_bytes (abfd, info_ptr);
14014 info_ptr += 2;
14015 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14016 info_ptr += blk->size;
14017 DW_BLOCK (attr) = blk;
14018 break;
14019 case DW_FORM_block4:
14020 blk = dwarf_alloc_block (cu);
14021 blk->size = read_4_bytes (abfd, info_ptr);
14022 info_ptr += 4;
14023 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14024 info_ptr += blk->size;
14025 DW_BLOCK (attr) = blk;
14026 break;
14027 case DW_FORM_data2:
14028 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14029 info_ptr += 2;
14030 break;
14031 case DW_FORM_data4:
14032 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14033 info_ptr += 4;
14034 break;
14035 case DW_FORM_data8:
14036 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14037 info_ptr += 8;
14038 break;
14039 case DW_FORM_sec_offset:
14040 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14041 info_ptr += bytes_read;
14042 break;
14043 case DW_FORM_string:
14044 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14045 DW_STRING_IS_CANONICAL (attr) = 0;
14046 info_ptr += bytes_read;
14047 break;
14048 case DW_FORM_strp:
14049 if (!cu->per_cu->is_dwz)
14050 {
14051 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14052 &bytes_read);
14053 DW_STRING_IS_CANONICAL (attr) = 0;
14054 info_ptr += bytes_read;
14055 break;
14056 }
14057 /* FALLTHROUGH */
14058 case DW_FORM_GNU_strp_alt:
14059 {
14060 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14061 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14062 &bytes_read);
14063
14064 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14065 DW_STRING_IS_CANONICAL (attr) = 0;
14066 info_ptr += bytes_read;
14067 }
14068 break;
14069 case DW_FORM_exprloc:
14070 case DW_FORM_block:
14071 blk = dwarf_alloc_block (cu);
14072 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14073 info_ptr += bytes_read;
14074 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14075 info_ptr += blk->size;
14076 DW_BLOCK (attr) = blk;
14077 break;
14078 case DW_FORM_block1:
14079 blk = dwarf_alloc_block (cu);
14080 blk->size = read_1_byte (abfd, info_ptr);
14081 info_ptr += 1;
14082 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14083 info_ptr += blk->size;
14084 DW_BLOCK (attr) = blk;
14085 break;
14086 case DW_FORM_data1:
14087 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14088 info_ptr += 1;
14089 break;
14090 case DW_FORM_flag:
14091 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14092 info_ptr += 1;
14093 break;
14094 case DW_FORM_flag_present:
14095 DW_UNSND (attr) = 1;
14096 break;
14097 case DW_FORM_sdata:
14098 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14099 info_ptr += bytes_read;
14100 break;
14101 case DW_FORM_udata:
14102 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14103 info_ptr += bytes_read;
14104 break;
14105 case DW_FORM_ref1:
14106 DW_UNSND (attr) = (cu->header.offset.sect_off
14107 + read_1_byte (abfd, info_ptr));
14108 info_ptr += 1;
14109 break;
14110 case DW_FORM_ref2:
14111 DW_UNSND (attr) = (cu->header.offset.sect_off
14112 + read_2_bytes (abfd, info_ptr));
14113 info_ptr += 2;
14114 break;
14115 case DW_FORM_ref4:
14116 DW_UNSND (attr) = (cu->header.offset.sect_off
14117 + read_4_bytes (abfd, info_ptr));
14118 info_ptr += 4;
14119 break;
14120 case DW_FORM_ref8:
14121 DW_UNSND (attr) = (cu->header.offset.sect_off
14122 + read_8_bytes (abfd, info_ptr));
14123 info_ptr += 8;
14124 break;
14125 case DW_FORM_ref_sig8:
14126 /* Convert the signature to something we can record in DW_UNSND
14127 for later lookup.
14128 NOTE: This is NULL if the type wasn't found. */
14129 DW_SIGNATURED_TYPE (attr) =
14130 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14131 info_ptr += 8;
14132 break;
14133 case DW_FORM_ref_udata:
14134 DW_UNSND (attr) = (cu->header.offset.sect_off
14135 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14136 info_ptr += bytes_read;
14137 break;
14138 case DW_FORM_indirect:
14139 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14140 info_ptr += bytes_read;
14141 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14142 break;
14143 case DW_FORM_GNU_addr_index:
14144 if (reader->dwo_file == NULL)
14145 {
14146 /* For now flag a hard error.
14147 Later we can turn this into a complaint. */
14148 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14149 dwarf_form_name (form),
14150 bfd_get_filename (abfd));
14151 }
14152 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14153 info_ptr += bytes_read;
14154 break;
14155 case DW_FORM_GNU_str_index:
14156 if (reader->dwo_file == NULL)
14157 {
14158 /* For now flag a hard error.
14159 Later we can turn this into a complaint if warranted. */
14160 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14161 dwarf_form_name (form),
14162 bfd_get_filename (abfd));
14163 }
14164 {
14165 ULONGEST str_index =
14166 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14167
14168 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14169 DW_STRING_IS_CANONICAL (attr) = 0;
14170 info_ptr += bytes_read;
14171 }
14172 break;
14173 default:
14174 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14175 dwarf_form_name (form),
14176 bfd_get_filename (abfd));
14177 }
14178
14179 /* Super hack. */
14180 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14181 attr->form = DW_FORM_GNU_ref_alt;
14182
14183 /* We have seen instances where the compiler tried to emit a byte
14184 size attribute of -1 which ended up being encoded as an unsigned
14185 0xffffffff. Although 0xffffffff is technically a valid size value,
14186 an object of this size seems pretty unlikely so we can relatively
14187 safely treat these cases as if the size attribute was invalid and
14188 treat them as zero by default. */
14189 if (attr->name == DW_AT_byte_size
14190 && form == DW_FORM_data4
14191 && DW_UNSND (attr) >= 0xffffffff)
14192 {
14193 complaint
14194 (&symfile_complaints,
14195 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14196 hex_string (DW_UNSND (attr)));
14197 DW_UNSND (attr) = 0;
14198 }
14199
14200 return info_ptr;
14201 }
14202
14203 /* Read an attribute described by an abbreviated attribute. */
14204
14205 static gdb_byte *
14206 read_attribute (const struct die_reader_specs *reader,
14207 struct attribute *attr, struct attr_abbrev *abbrev,
14208 gdb_byte *info_ptr)
14209 {
14210 attr->name = abbrev->name;
14211 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14212 }
14213
14214 /* Read dwarf information from a buffer. */
14215
14216 static unsigned int
14217 read_1_byte (bfd *abfd, const gdb_byte *buf)
14218 {
14219 return bfd_get_8 (abfd, buf);
14220 }
14221
14222 static int
14223 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14224 {
14225 return bfd_get_signed_8 (abfd, buf);
14226 }
14227
14228 static unsigned int
14229 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14230 {
14231 return bfd_get_16 (abfd, buf);
14232 }
14233
14234 static int
14235 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14236 {
14237 return bfd_get_signed_16 (abfd, buf);
14238 }
14239
14240 static unsigned int
14241 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14242 {
14243 return bfd_get_32 (abfd, buf);
14244 }
14245
14246 static int
14247 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14248 {
14249 return bfd_get_signed_32 (abfd, buf);
14250 }
14251
14252 static ULONGEST
14253 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14254 {
14255 return bfd_get_64 (abfd, buf);
14256 }
14257
14258 static CORE_ADDR
14259 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14260 unsigned int *bytes_read)
14261 {
14262 struct comp_unit_head *cu_header = &cu->header;
14263 CORE_ADDR retval = 0;
14264
14265 if (cu_header->signed_addr_p)
14266 {
14267 switch (cu_header->addr_size)
14268 {
14269 case 2:
14270 retval = bfd_get_signed_16 (abfd, buf);
14271 break;
14272 case 4:
14273 retval = bfd_get_signed_32 (abfd, buf);
14274 break;
14275 case 8:
14276 retval = bfd_get_signed_64 (abfd, buf);
14277 break;
14278 default:
14279 internal_error (__FILE__, __LINE__,
14280 _("read_address: bad switch, signed [in module %s]"),
14281 bfd_get_filename (abfd));
14282 }
14283 }
14284 else
14285 {
14286 switch (cu_header->addr_size)
14287 {
14288 case 2:
14289 retval = bfd_get_16 (abfd, buf);
14290 break;
14291 case 4:
14292 retval = bfd_get_32 (abfd, buf);
14293 break;
14294 case 8:
14295 retval = bfd_get_64 (abfd, buf);
14296 break;
14297 default:
14298 internal_error (__FILE__, __LINE__,
14299 _("read_address: bad switch, "
14300 "unsigned [in module %s]"),
14301 bfd_get_filename (abfd));
14302 }
14303 }
14304
14305 *bytes_read = cu_header->addr_size;
14306 return retval;
14307 }
14308
14309 /* Read the initial length from a section. The (draft) DWARF 3
14310 specification allows the initial length to take up either 4 bytes
14311 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14312 bytes describe the length and all offsets will be 8 bytes in length
14313 instead of 4.
14314
14315 An older, non-standard 64-bit format is also handled by this
14316 function. The older format in question stores the initial length
14317 as an 8-byte quantity without an escape value. Lengths greater
14318 than 2^32 aren't very common which means that the initial 4 bytes
14319 is almost always zero. Since a length value of zero doesn't make
14320 sense for the 32-bit format, this initial zero can be considered to
14321 be an escape value which indicates the presence of the older 64-bit
14322 format. As written, the code can't detect (old format) lengths
14323 greater than 4GB. If it becomes necessary to handle lengths
14324 somewhat larger than 4GB, we could allow other small values (such
14325 as the non-sensical values of 1, 2, and 3) to also be used as
14326 escape values indicating the presence of the old format.
14327
14328 The value returned via bytes_read should be used to increment the
14329 relevant pointer after calling read_initial_length().
14330
14331 [ Note: read_initial_length() and read_offset() are based on the
14332 document entitled "DWARF Debugging Information Format", revision
14333 3, draft 8, dated November 19, 2001. This document was obtained
14334 from:
14335
14336 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14337
14338 This document is only a draft and is subject to change. (So beware.)
14339
14340 Details regarding the older, non-standard 64-bit format were
14341 determined empirically by examining 64-bit ELF files produced by
14342 the SGI toolchain on an IRIX 6.5 machine.
14343
14344 - Kevin, July 16, 2002
14345 ] */
14346
14347 static LONGEST
14348 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14349 {
14350 LONGEST length = bfd_get_32 (abfd, buf);
14351
14352 if (length == 0xffffffff)
14353 {
14354 length = bfd_get_64 (abfd, buf + 4);
14355 *bytes_read = 12;
14356 }
14357 else if (length == 0)
14358 {
14359 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14360 length = bfd_get_64 (abfd, buf);
14361 *bytes_read = 8;
14362 }
14363 else
14364 {
14365 *bytes_read = 4;
14366 }
14367
14368 return length;
14369 }
14370
14371 /* Cover function for read_initial_length.
14372 Returns the length of the object at BUF, and stores the size of the
14373 initial length in *BYTES_READ and stores the size that offsets will be in
14374 *OFFSET_SIZE.
14375 If the initial length size is not equivalent to that specified in
14376 CU_HEADER then issue a complaint.
14377 This is useful when reading non-comp-unit headers. */
14378
14379 static LONGEST
14380 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14381 const struct comp_unit_head *cu_header,
14382 unsigned int *bytes_read,
14383 unsigned int *offset_size)
14384 {
14385 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14386
14387 gdb_assert (cu_header->initial_length_size == 4
14388 || cu_header->initial_length_size == 8
14389 || cu_header->initial_length_size == 12);
14390
14391 if (cu_header->initial_length_size != *bytes_read)
14392 complaint (&symfile_complaints,
14393 _("intermixed 32-bit and 64-bit DWARF sections"));
14394
14395 *offset_size = (*bytes_read == 4) ? 4 : 8;
14396 return length;
14397 }
14398
14399 /* Read an offset from the data stream. The size of the offset is
14400 given by cu_header->offset_size. */
14401
14402 static LONGEST
14403 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14404 unsigned int *bytes_read)
14405 {
14406 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14407
14408 *bytes_read = cu_header->offset_size;
14409 return offset;
14410 }
14411
14412 /* Read an offset from the data stream. */
14413
14414 static LONGEST
14415 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14416 {
14417 LONGEST retval = 0;
14418
14419 switch (offset_size)
14420 {
14421 case 4:
14422 retval = bfd_get_32 (abfd, buf);
14423 break;
14424 case 8:
14425 retval = bfd_get_64 (abfd, buf);
14426 break;
14427 default:
14428 internal_error (__FILE__, __LINE__,
14429 _("read_offset_1: bad switch [in module %s]"),
14430 bfd_get_filename (abfd));
14431 }
14432
14433 return retval;
14434 }
14435
14436 static gdb_byte *
14437 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14438 {
14439 /* If the size of a host char is 8 bits, we can return a pointer
14440 to the buffer, otherwise we have to copy the data to a buffer
14441 allocated on the temporary obstack. */
14442 gdb_assert (HOST_CHAR_BIT == 8);
14443 return buf;
14444 }
14445
14446 static char *
14447 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14448 {
14449 /* If the size of a host char is 8 bits, we can return a pointer
14450 to the string, otherwise we have to copy the string to a buffer
14451 allocated on the temporary obstack. */
14452 gdb_assert (HOST_CHAR_BIT == 8);
14453 if (*buf == '\0')
14454 {
14455 *bytes_read_ptr = 1;
14456 return NULL;
14457 }
14458 *bytes_read_ptr = strlen ((char *) buf) + 1;
14459 return (char *) buf;
14460 }
14461
14462 static char *
14463 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14464 {
14465 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14466 if (dwarf2_per_objfile->str.buffer == NULL)
14467 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14468 bfd_get_filename (abfd));
14469 if (str_offset >= dwarf2_per_objfile->str.size)
14470 error (_("DW_FORM_strp pointing outside of "
14471 ".debug_str section [in module %s]"),
14472 bfd_get_filename (abfd));
14473 gdb_assert (HOST_CHAR_BIT == 8);
14474 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14475 return NULL;
14476 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14477 }
14478
14479 /* Read a string at offset STR_OFFSET in the .debug_str section from
14480 the .dwz file DWZ. Throw an error if the offset is too large. If
14481 the string consists of a single NUL byte, return NULL; otherwise
14482 return a pointer to the string. */
14483
14484 static char *
14485 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14486 {
14487 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14488
14489 if (dwz->str.buffer == NULL)
14490 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14491 "section [in module %s]"),
14492 bfd_get_filename (dwz->dwz_bfd));
14493 if (str_offset >= dwz->str.size)
14494 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14495 ".debug_str section [in module %s]"),
14496 bfd_get_filename (dwz->dwz_bfd));
14497 gdb_assert (HOST_CHAR_BIT == 8);
14498 if (dwz->str.buffer[str_offset] == '\0')
14499 return NULL;
14500 return (char *) (dwz->str.buffer + str_offset);
14501 }
14502
14503 static char *
14504 read_indirect_string (bfd *abfd, gdb_byte *buf,
14505 const struct comp_unit_head *cu_header,
14506 unsigned int *bytes_read_ptr)
14507 {
14508 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14509
14510 return read_indirect_string_at_offset (abfd, str_offset);
14511 }
14512
14513 static ULONGEST
14514 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14515 {
14516 ULONGEST result;
14517 unsigned int num_read;
14518 int i, shift;
14519 unsigned char byte;
14520
14521 result = 0;
14522 shift = 0;
14523 num_read = 0;
14524 i = 0;
14525 while (1)
14526 {
14527 byte = bfd_get_8 (abfd, buf);
14528 buf++;
14529 num_read++;
14530 result |= ((ULONGEST) (byte & 127) << shift);
14531 if ((byte & 128) == 0)
14532 {
14533 break;
14534 }
14535 shift += 7;
14536 }
14537 *bytes_read_ptr = num_read;
14538 return result;
14539 }
14540
14541 static LONGEST
14542 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14543 {
14544 LONGEST result;
14545 int i, shift, num_read;
14546 unsigned char byte;
14547
14548 result = 0;
14549 shift = 0;
14550 num_read = 0;
14551 i = 0;
14552 while (1)
14553 {
14554 byte = bfd_get_8 (abfd, buf);
14555 buf++;
14556 num_read++;
14557 result |= ((LONGEST) (byte & 127) << shift);
14558 shift += 7;
14559 if ((byte & 128) == 0)
14560 {
14561 break;
14562 }
14563 }
14564 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14565 result |= -(((LONGEST) 1) << shift);
14566 *bytes_read_ptr = num_read;
14567 return result;
14568 }
14569
14570 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14571 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14572 ADDR_SIZE is the size of addresses from the CU header. */
14573
14574 static CORE_ADDR
14575 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14576 {
14577 struct objfile *objfile = dwarf2_per_objfile->objfile;
14578 bfd *abfd = objfile->obfd;
14579 const gdb_byte *info_ptr;
14580
14581 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14582 if (dwarf2_per_objfile->addr.buffer == NULL)
14583 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14584 objfile->name);
14585 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14586 error (_("DW_FORM_addr_index pointing outside of "
14587 ".debug_addr section [in module %s]"),
14588 objfile->name);
14589 info_ptr = (dwarf2_per_objfile->addr.buffer
14590 + addr_base + addr_index * addr_size);
14591 if (addr_size == 4)
14592 return bfd_get_32 (abfd, info_ptr);
14593 else
14594 return bfd_get_64 (abfd, info_ptr);
14595 }
14596
14597 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14598
14599 static CORE_ADDR
14600 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14601 {
14602 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14603 }
14604
14605 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14606
14607 static CORE_ADDR
14608 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14609 unsigned int *bytes_read)
14610 {
14611 bfd *abfd = cu->objfile->obfd;
14612 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14613
14614 return read_addr_index (cu, addr_index);
14615 }
14616
14617 /* Data structure to pass results from dwarf2_read_addr_index_reader
14618 back to dwarf2_read_addr_index. */
14619
14620 struct dwarf2_read_addr_index_data
14621 {
14622 ULONGEST addr_base;
14623 int addr_size;
14624 };
14625
14626 /* die_reader_func for dwarf2_read_addr_index. */
14627
14628 static void
14629 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14630 gdb_byte *info_ptr,
14631 struct die_info *comp_unit_die,
14632 int has_children,
14633 void *data)
14634 {
14635 struct dwarf2_cu *cu = reader->cu;
14636 struct dwarf2_read_addr_index_data *aidata =
14637 (struct dwarf2_read_addr_index_data *) data;
14638
14639 aidata->addr_base = cu->addr_base;
14640 aidata->addr_size = cu->header.addr_size;
14641 }
14642
14643 /* Given an index in .debug_addr, fetch the value.
14644 NOTE: This can be called during dwarf expression evaluation,
14645 long after the debug information has been read, and thus per_cu->cu
14646 may no longer exist. */
14647
14648 CORE_ADDR
14649 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14650 unsigned int addr_index)
14651 {
14652 struct objfile *objfile = per_cu->objfile;
14653 struct dwarf2_cu *cu = per_cu->cu;
14654 ULONGEST addr_base;
14655 int addr_size;
14656
14657 /* This is intended to be called from outside this file. */
14658 dw2_setup (objfile);
14659
14660 /* We need addr_base and addr_size.
14661 If we don't have PER_CU->cu, we have to get it.
14662 Nasty, but the alternative is storing the needed info in PER_CU,
14663 which at this point doesn't seem justified: it's not clear how frequently
14664 it would get used and it would increase the size of every PER_CU.
14665 Entry points like dwarf2_per_cu_addr_size do a similar thing
14666 so we're not in uncharted territory here.
14667 Alas we need to be a bit more complicated as addr_base is contained
14668 in the DIE.
14669
14670 We don't need to read the entire CU(/TU).
14671 We just need the header and top level die.
14672
14673 IWBN to use the aging mechanism to let us lazily later discard the CU.
14674 For now we skip this optimization. */
14675
14676 if (cu != NULL)
14677 {
14678 addr_base = cu->addr_base;
14679 addr_size = cu->header.addr_size;
14680 }
14681 else
14682 {
14683 struct dwarf2_read_addr_index_data aidata;
14684
14685 /* Note: We can't use init_cutu_and_read_dies_simple here,
14686 we need addr_base. */
14687 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14688 dwarf2_read_addr_index_reader, &aidata);
14689 addr_base = aidata.addr_base;
14690 addr_size = aidata.addr_size;
14691 }
14692
14693 return read_addr_index_1 (addr_index, addr_base, addr_size);
14694 }
14695
14696 /* Given a DW_AT_str_index, fetch the string. */
14697
14698 static char *
14699 read_str_index (const struct die_reader_specs *reader,
14700 struct dwarf2_cu *cu, ULONGEST str_index)
14701 {
14702 struct objfile *objfile = dwarf2_per_objfile->objfile;
14703 const char *dwo_name = objfile->name;
14704 bfd *abfd = objfile->obfd;
14705 struct dwo_sections *sections = &reader->dwo_file->sections;
14706 gdb_byte *info_ptr;
14707 ULONGEST str_offset;
14708
14709 dwarf2_read_section (objfile, &sections->str);
14710 dwarf2_read_section (objfile, &sections->str_offsets);
14711 if (sections->str.buffer == NULL)
14712 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14713 " in CU at offset 0x%lx [in module %s]"),
14714 (long) cu->header.offset.sect_off, dwo_name);
14715 if (sections->str_offsets.buffer == NULL)
14716 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14717 " in CU at offset 0x%lx [in module %s]"),
14718 (long) cu->header.offset.sect_off, dwo_name);
14719 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14720 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14721 " section in CU at offset 0x%lx [in module %s]"),
14722 (long) cu->header.offset.sect_off, dwo_name);
14723 info_ptr = (sections->str_offsets.buffer
14724 + str_index * cu->header.offset_size);
14725 if (cu->header.offset_size == 4)
14726 str_offset = bfd_get_32 (abfd, info_ptr);
14727 else
14728 str_offset = bfd_get_64 (abfd, info_ptr);
14729 if (str_offset >= sections->str.size)
14730 error (_("Offset from DW_FORM_str_index pointing outside of"
14731 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14732 (long) cu->header.offset.sect_off, dwo_name);
14733 return (char *) (sections->str.buffer + str_offset);
14734 }
14735
14736 /* Return the length of an LEB128 number in BUF. */
14737
14738 static int
14739 leb128_size (const gdb_byte *buf)
14740 {
14741 const gdb_byte *begin = buf;
14742 gdb_byte byte;
14743
14744 while (1)
14745 {
14746 byte = *buf++;
14747 if ((byte & 128) == 0)
14748 return buf - begin;
14749 }
14750 }
14751
14752 static void
14753 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14754 {
14755 switch (lang)
14756 {
14757 case DW_LANG_C89:
14758 case DW_LANG_C99:
14759 case DW_LANG_C:
14760 cu->language = language_c;
14761 break;
14762 case DW_LANG_C_plus_plus:
14763 cu->language = language_cplus;
14764 break;
14765 case DW_LANG_D:
14766 cu->language = language_d;
14767 break;
14768 case DW_LANG_Fortran77:
14769 case DW_LANG_Fortran90:
14770 case DW_LANG_Fortran95:
14771 cu->language = language_fortran;
14772 break;
14773 case DW_LANG_Go:
14774 cu->language = language_go;
14775 break;
14776 case DW_LANG_Mips_Assembler:
14777 cu->language = language_asm;
14778 break;
14779 case DW_LANG_Java:
14780 cu->language = language_java;
14781 break;
14782 case DW_LANG_Ada83:
14783 case DW_LANG_Ada95:
14784 cu->language = language_ada;
14785 break;
14786 case DW_LANG_Modula2:
14787 cu->language = language_m2;
14788 break;
14789 case DW_LANG_Pascal83:
14790 cu->language = language_pascal;
14791 break;
14792 case DW_LANG_ObjC:
14793 cu->language = language_objc;
14794 break;
14795 case DW_LANG_Cobol74:
14796 case DW_LANG_Cobol85:
14797 default:
14798 cu->language = language_minimal;
14799 break;
14800 }
14801 cu->language_defn = language_def (cu->language);
14802 }
14803
14804 /* Return the named attribute or NULL if not there. */
14805
14806 static struct attribute *
14807 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14808 {
14809 for (;;)
14810 {
14811 unsigned int i;
14812 struct attribute *spec = NULL;
14813
14814 for (i = 0; i < die->num_attrs; ++i)
14815 {
14816 if (die->attrs[i].name == name)
14817 return &die->attrs[i];
14818 if (die->attrs[i].name == DW_AT_specification
14819 || die->attrs[i].name == DW_AT_abstract_origin)
14820 spec = &die->attrs[i];
14821 }
14822
14823 if (!spec)
14824 break;
14825
14826 die = follow_die_ref (die, spec, &cu);
14827 }
14828
14829 return NULL;
14830 }
14831
14832 /* Return the named attribute or NULL if not there,
14833 but do not follow DW_AT_specification, etc.
14834 This is for use in contexts where we're reading .debug_types dies.
14835 Following DW_AT_specification, DW_AT_abstract_origin will take us
14836 back up the chain, and we want to go down. */
14837
14838 static struct attribute *
14839 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14840 {
14841 unsigned int i;
14842
14843 for (i = 0; i < die->num_attrs; ++i)
14844 if (die->attrs[i].name == name)
14845 return &die->attrs[i];
14846
14847 return NULL;
14848 }
14849
14850 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14851 and holds a non-zero value. This function should only be used for
14852 DW_FORM_flag or DW_FORM_flag_present attributes. */
14853
14854 static int
14855 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14856 {
14857 struct attribute *attr = dwarf2_attr (die, name, cu);
14858
14859 return (attr && DW_UNSND (attr));
14860 }
14861
14862 static int
14863 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14864 {
14865 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14866 which value is non-zero. However, we have to be careful with
14867 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14868 (via dwarf2_flag_true_p) follows this attribute. So we may
14869 end up accidently finding a declaration attribute that belongs
14870 to a different DIE referenced by the specification attribute,
14871 even though the given DIE does not have a declaration attribute. */
14872 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14873 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14874 }
14875
14876 /* Return the die giving the specification for DIE, if there is
14877 one. *SPEC_CU is the CU containing DIE on input, and the CU
14878 containing the return value on output. If there is no
14879 specification, but there is an abstract origin, that is
14880 returned. */
14881
14882 static struct die_info *
14883 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14884 {
14885 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14886 *spec_cu);
14887
14888 if (spec_attr == NULL)
14889 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14890
14891 if (spec_attr == NULL)
14892 return NULL;
14893 else
14894 return follow_die_ref (die, spec_attr, spec_cu);
14895 }
14896
14897 /* Free the line_header structure *LH, and any arrays and strings it
14898 refers to.
14899 NOTE: This is also used as a "cleanup" function. */
14900
14901 static void
14902 free_line_header (struct line_header *lh)
14903 {
14904 if (lh->standard_opcode_lengths)
14905 xfree (lh->standard_opcode_lengths);
14906
14907 /* Remember that all the lh->file_names[i].name pointers are
14908 pointers into debug_line_buffer, and don't need to be freed. */
14909 if (lh->file_names)
14910 xfree (lh->file_names);
14911
14912 /* Similarly for the include directory names. */
14913 if (lh->include_dirs)
14914 xfree (lh->include_dirs);
14915
14916 xfree (lh);
14917 }
14918
14919 /* Add an entry to LH's include directory table. */
14920
14921 static void
14922 add_include_dir (struct line_header *lh, char *include_dir)
14923 {
14924 /* Grow the array if necessary. */
14925 if (lh->include_dirs_size == 0)
14926 {
14927 lh->include_dirs_size = 1; /* for testing */
14928 lh->include_dirs = xmalloc (lh->include_dirs_size
14929 * sizeof (*lh->include_dirs));
14930 }
14931 else if (lh->num_include_dirs >= lh->include_dirs_size)
14932 {
14933 lh->include_dirs_size *= 2;
14934 lh->include_dirs = xrealloc (lh->include_dirs,
14935 (lh->include_dirs_size
14936 * sizeof (*lh->include_dirs)));
14937 }
14938
14939 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14940 }
14941
14942 /* Add an entry to LH's file name table. */
14943
14944 static void
14945 add_file_name (struct line_header *lh,
14946 char *name,
14947 unsigned int dir_index,
14948 unsigned int mod_time,
14949 unsigned int length)
14950 {
14951 struct file_entry *fe;
14952
14953 /* Grow the array if necessary. */
14954 if (lh->file_names_size == 0)
14955 {
14956 lh->file_names_size = 1; /* for testing */
14957 lh->file_names = xmalloc (lh->file_names_size
14958 * sizeof (*lh->file_names));
14959 }
14960 else if (lh->num_file_names >= lh->file_names_size)
14961 {
14962 lh->file_names_size *= 2;
14963 lh->file_names = xrealloc (lh->file_names,
14964 (lh->file_names_size
14965 * sizeof (*lh->file_names)));
14966 }
14967
14968 fe = &lh->file_names[lh->num_file_names++];
14969 fe->name = name;
14970 fe->dir_index = dir_index;
14971 fe->mod_time = mod_time;
14972 fe->length = length;
14973 fe->included_p = 0;
14974 fe->symtab = NULL;
14975 }
14976
14977 /* A convenience function to find the proper .debug_line section for a
14978 CU. */
14979
14980 static struct dwarf2_section_info *
14981 get_debug_line_section (struct dwarf2_cu *cu)
14982 {
14983 struct dwarf2_section_info *section;
14984
14985 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14986 DWO file. */
14987 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14988 section = &cu->dwo_unit->dwo_file->sections.line;
14989 else if (cu->per_cu->is_dwz)
14990 {
14991 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14992
14993 section = &dwz->line;
14994 }
14995 else
14996 section = &dwarf2_per_objfile->line;
14997
14998 return section;
14999 }
15000
15001 /* Read the statement program header starting at OFFSET in
15002 .debug_line, or .debug_line.dwo. Return a pointer
15003 to a struct line_header, allocated using xmalloc.
15004
15005 NOTE: the strings in the include directory and file name tables of
15006 the returned object point into the dwarf line section buffer,
15007 and must not be freed. */
15008
15009 static struct line_header *
15010 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15011 {
15012 struct cleanup *back_to;
15013 struct line_header *lh;
15014 gdb_byte *line_ptr;
15015 unsigned int bytes_read, offset_size;
15016 int i;
15017 char *cur_dir, *cur_file;
15018 struct dwarf2_section_info *section;
15019 bfd *abfd;
15020
15021 section = get_debug_line_section (cu);
15022 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15023 if (section->buffer == NULL)
15024 {
15025 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15026 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15027 else
15028 complaint (&symfile_complaints, _("missing .debug_line section"));
15029 return 0;
15030 }
15031
15032 /* We can't do this until we know the section is non-empty.
15033 Only then do we know we have such a section. */
15034 abfd = section->asection->owner;
15035
15036 /* Make sure that at least there's room for the total_length field.
15037 That could be 12 bytes long, but we're just going to fudge that. */
15038 if (offset + 4 >= section->size)
15039 {
15040 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15041 return 0;
15042 }
15043
15044 lh = xmalloc (sizeof (*lh));
15045 memset (lh, 0, sizeof (*lh));
15046 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15047 (void *) lh);
15048
15049 line_ptr = section->buffer + offset;
15050
15051 /* Read in the header. */
15052 lh->total_length =
15053 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15054 &bytes_read, &offset_size);
15055 line_ptr += bytes_read;
15056 if (line_ptr + lh->total_length > (section->buffer + section->size))
15057 {
15058 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15059 return 0;
15060 }
15061 lh->statement_program_end = line_ptr + lh->total_length;
15062 lh->version = read_2_bytes (abfd, line_ptr);
15063 line_ptr += 2;
15064 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15065 line_ptr += offset_size;
15066 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15067 line_ptr += 1;
15068 if (lh->version >= 4)
15069 {
15070 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15071 line_ptr += 1;
15072 }
15073 else
15074 lh->maximum_ops_per_instruction = 1;
15075
15076 if (lh->maximum_ops_per_instruction == 0)
15077 {
15078 lh->maximum_ops_per_instruction = 1;
15079 complaint (&symfile_complaints,
15080 _("invalid maximum_ops_per_instruction "
15081 "in `.debug_line' section"));
15082 }
15083
15084 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15085 line_ptr += 1;
15086 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15087 line_ptr += 1;
15088 lh->line_range = read_1_byte (abfd, line_ptr);
15089 line_ptr += 1;
15090 lh->opcode_base = read_1_byte (abfd, line_ptr);
15091 line_ptr += 1;
15092 lh->standard_opcode_lengths
15093 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15094
15095 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15096 for (i = 1; i < lh->opcode_base; ++i)
15097 {
15098 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15099 line_ptr += 1;
15100 }
15101
15102 /* Read directory table. */
15103 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15104 {
15105 line_ptr += bytes_read;
15106 add_include_dir (lh, cur_dir);
15107 }
15108 line_ptr += bytes_read;
15109
15110 /* Read file name table. */
15111 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15112 {
15113 unsigned int dir_index, mod_time, length;
15114
15115 line_ptr += bytes_read;
15116 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15117 line_ptr += bytes_read;
15118 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15119 line_ptr += bytes_read;
15120 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15121 line_ptr += bytes_read;
15122
15123 add_file_name (lh, cur_file, dir_index, mod_time, length);
15124 }
15125 line_ptr += bytes_read;
15126 lh->statement_program_start = line_ptr;
15127
15128 if (line_ptr > (section->buffer + section->size))
15129 complaint (&symfile_complaints,
15130 _("line number info header doesn't "
15131 "fit in `.debug_line' section"));
15132
15133 discard_cleanups (back_to);
15134 return lh;
15135 }
15136
15137 /* Subroutine of dwarf_decode_lines to simplify it.
15138 Return the file name of the psymtab for included file FILE_INDEX
15139 in line header LH of PST.
15140 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15141 If space for the result is malloc'd, it will be freed by a cleanup.
15142 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15143
15144 The function creates dangling cleanup registration. */
15145
15146 static char *
15147 psymtab_include_file_name (const struct line_header *lh, int file_index,
15148 const struct partial_symtab *pst,
15149 const char *comp_dir)
15150 {
15151 const struct file_entry fe = lh->file_names [file_index];
15152 char *include_name = fe.name;
15153 char *include_name_to_compare = include_name;
15154 char *dir_name = NULL;
15155 const char *pst_filename;
15156 char *copied_name = NULL;
15157 int file_is_pst;
15158
15159 if (fe.dir_index)
15160 dir_name = lh->include_dirs[fe.dir_index - 1];
15161
15162 if (!IS_ABSOLUTE_PATH (include_name)
15163 && (dir_name != NULL || comp_dir != NULL))
15164 {
15165 /* Avoid creating a duplicate psymtab for PST.
15166 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15167 Before we do the comparison, however, we need to account
15168 for DIR_NAME and COMP_DIR.
15169 First prepend dir_name (if non-NULL). If we still don't
15170 have an absolute path prepend comp_dir (if non-NULL).
15171 However, the directory we record in the include-file's
15172 psymtab does not contain COMP_DIR (to match the
15173 corresponding symtab(s)).
15174
15175 Example:
15176
15177 bash$ cd /tmp
15178 bash$ gcc -g ./hello.c
15179 include_name = "hello.c"
15180 dir_name = "."
15181 DW_AT_comp_dir = comp_dir = "/tmp"
15182 DW_AT_name = "./hello.c" */
15183
15184 if (dir_name != NULL)
15185 {
15186 include_name = concat (dir_name, SLASH_STRING,
15187 include_name, (char *)NULL);
15188 include_name_to_compare = include_name;
15189 make_cleanup (xfree, include_name);
15190 }
15191 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15192 {
15193 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15194 include_name, (char *)NULL);
15195 }
15196 }
15197
15198 pst_filename = pst->filename;
15199 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15200 {
15201 copied_name = concat (pst->dirname, SLASH_STRING,
15202 pst_filename, (char *)NULL);
15203 pst_filename = copied_name;
15204 }
15205
15206 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15207
15208 if (include_name_to_compare != include_name)
15209 xfree (include_name_to_compare);
15210 if (copied_name != NULL)
15211 xfree (copied_name);
15212
15213 if (file_is_pst)
15214 return NULL;
15215 return include_name;
15216 }
15217
15218 /* Ignore this record_line request. */
15219
15220 static void
15221 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15222 {
15223 return;
15224 }
15225
15226 /* Subroutine of dwarf_decode_lines to simplify it.
15227 Process the line number information in LH. */
15228
15229 static void
15230 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15231 struct dwarf2_cu *cu, struct partial_symtab *pst)
15232 {
15233 gdb_byte *line_ptr, *extended_end;
15234 gdb_byte *line_end;
15235 unsigned int bytes_read, extended_len;
15236 unsigned char op_code, extended_op, adj_opcode;
15237 CORE_ADDR baseaddr;
15238 struct objfile *objfile = cu->objfile;
15239 bfd *abfd = objfile->obfd;
15240 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15241 const int decode_for_pst_p = (pst != NULL);
15242 struct subfile *last_subfile = NULL;
15243 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15244 = record_line;
15245
15246 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15247
15248 line_ptr = lh->statement_program_start;
15249 line_end = lh->statement_program_end;
15250
15251 /* Read the statement sequences until there's nothing left. */
15252 while (line_ptr < line_end)
15253 {
15254 /* state machine registers */
15255 CORE_ADDR address = 0;
15256 unsigned int file = 1;
15257 unsigned int line = 1;
15258 unsigned int column = 0;
15259 int is_stmt = lh->default_is_stmt;
15260 int basic_block = 0;
15261 int end_sequence = 0;
15262 CORE_ADDR addr;
15263 unsigned char op_index = 0;
15264
15265 if (!decode_for_pst_p && lh->num_file_names >= file)
15266 {
15267 /* Start a subfile for the current file of the state machine. */
15268 /* lh->include_dirs and lh->file_names are 0-based, but the
15269 directory and file name numbers in the statement program
15270 are 1-based. */
15271 struct file_entry *fe = &lh->file_names[file - 1];
15272 char *dir = NULL;
15273
15274 if (fe->dir_index)
15275 dir = lh->include_dirs[fe->dir_index - 1];
15276
15277 dwarf2_start_subfile (fe->name, dir, comp_dir);
15278 }
15279
15280 /* Decode the table. */
15281 while (!end_sequence)
15282 {
15283 op_code = read_1_byte (abfd, line_ptr);
15284 line_ptr += 1;
15285 if (line_ptr > line_end)
15286 {
15287 dwarf2_debug_line_missing_end_sequence_complaint ();
15288 break;
15289 }
15290
15291 if (op_code >= lh->opcode_base)
15292 {
15293 /* Special operand. */
15294 adj_opcode = op_code - lh->opcode_base;
15295 address += (((op_index + (adj_opcode / lh->line_range))
15296 / lh->maximum_ops_per_instruction)
15297 * lh->minimum_instruction_length);
15298 op_index = ((op_index + (adj_opcode / lh->line_range))
15299 % lh->maximum_ops_per_instruction);
15300 line += lh->line_base + (adj_opcode % lh->line_range);
15301 if (lh->num_file_names < file || file == 0)
15302 dwarf2_debug_line_missing_file_complaint ();
15303 /* For now we ignore lines not starting on an
15304 instruction boundary. */
15305 else if (op_index == 0)
15306 {
15307 lh->file_names[file - 1].included_p = 1;
15308 if (!decode_for_pst_p && is_stmt)
15309 {
15310 if (last_subfile != current_subfile)
15311 {
15312 addr = gdbarch_addr_bits_remove (gdbarch, address);
15313 if (last_subfile)
15314 (*p_record_line) (last_subfile, 0, addr);
15315 last_subfile = current_subfile;
15316 }
15317 /* Append row to matrix using current values. */
15318 addr = gdbarch_addr_bits_remove (gdbarch, address);
15319 (*p_record_line) (current_subfile, line, addr);
15320 }
15321 }
15322 basic_block = 0;
15323 }
15324 else switch (op_code)
15325 {
15326 case DW_LNS_extended_op:
15327 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15328 &bytes_read);
15329 line_ptr += bytes_read;
15330 extended_end = line_ptr + extended_len;
15331 extended_op = read_1_byte (abfd, line_ptr);
15332 line_ptr += 1;
15333 switch (extended_op)
15334 {
15335 case DW_LNE_end_sequence:
15336 p_record_line = record_line;
15337 end_sequence = 1;
15338 break;
15339 case DW_LNE_set_address:
15340 address = read_address (abfd, line_ptr, cu, &bytes_read);
15341
15342 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15343 {
15344 /* This line table is for a function which has been
15345 GCd by the linker. Ignore it. PR gdb/12528 */
15346
15347 long line_offset
15348 = line_ptr - get_debug_line_section (cu)->buffer;
15349
15350 complaint (&symfile_complaints,
15351 _(".debug_line address at offset 0x%lx is 0 "
15352 "[in module %s]"),
15353 line_offset, objfile->name);
15354 p_record_line = noop_record_line;
15355 }
15356
15357 op_index = 0;
15358 line_ptr += bytes_read;
15359 address += baseaddr;
15360 break;
15361 case DW_LNE_define_file:
15362 {
15363 char *cur_file;
15364 unsigned int dir_index, mod_time, length;
15365
15366 cur_file = read_direct_string (abfd, line_ptr,
15367 &bytes_read);
15368 line_ptr += bytes_read;
15369 dir_index =
15370 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15371 line_ptr += bytes_read;
15372 mod_time =
15373 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15374 line_ptr += bytes_read;
15375 length =
15376 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15377 line_ptr += bytes_read;
15378 add_file_name (lh, cur_file, dir_index, mod_time, length);
15379 }
15380 break;
15381 case DW_LNE_set_discriminator:
15382 /* The discriminator is not interesting to the debugger;
15383 just ignore it. */
15384 line_ptr = extended_end;
15385 break;
15386 default:
15387 complaint (&symfile_complaints,
15388 _("mangled .debug_line section"));
15389 return;
15390 }
15391 /* Make sure that we parsed the extended op correctly. If e.g.
15392 we expected a different address size than the producer used,
15393 we may have read the wrong number of bytes. */
15394 if (line_ptr != extended_end)
15395 {
15396 complaint (&symfile_complaints,
15397 _("mangled .debug_line section"));
15398 return;
15399 }
15400 break;
15401 case DW_LNS_copy:
15402 if (lh->num_file_names < file || file == 0)
15403 dwarf2_debug_line_missing_file_complaint ();
15404 else
15405 {
15406 lh->file_names[file - 1].included_p = 1;
15407 if (!decode_for_pst_p && is_stmt)
15408 {
15409 if (last_subfile != current_subfile)
15410 {
15411 addr = gdbarch_addr_bits_remove (gdbarch, address);
15412 if (last_subfile)
15413 (*p_record_line) (last_subfile, 0, addr);
15414 last_subfile = current_subfile;
15415 }
15416 addr = gdbarch_addr_bits_remove (gdbarch, address);
15417 (*p_record_line) (current_subfile, line, addr);
15418 }
15419 }
15420 basic_block = 0;
15421 break;
15422 case DW_LNS_advance_pc:
15423 {
15424 CORE_ADDR adjust
15425 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15426
15427 address += (((op_index + adjust)
15428 / lh->maximum_ops_per_instruction)
15429 * lh->minimum_instruction_length);
15430 op_index = ((op_index + adjust)
15431 % lh->maximum_ops_per_instruction);
15432 line_ptr += bytes_read;
15433 }
15434 break;
15435 case DW_LNS_advance_line:
15436 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15437 line_ptr += bytes_read;
15438 break;
15439 case DW_LNS_set_file:
15440 {
15441 /* The arrays lh->include_dirs and lh->file_names are
15442 0-based, but the directory and file name numbers in
15443 the statement program are 1-based. */
15444 struct file_entry *fe;
15445 char *dir = NULL;
15446
15447 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15448 line_ptr += bytes_read;
15449 if (lh->num_file_names < file || file == 0)
15450 dwarf2_debug_line_missing_file_complaint ();
15451 else
15452 {
15453 fe = &lh->file_names[file - 1];
15454 if (fe->dir_index)
15455 dir = lh->include_dirs[fe->dir_index - 1];
15456 if (!decode_for_pst_p)
15457 {
15458 last_subfile = current_subfile;
15459 dwarf2_start_subfile (fe->name, dir, comp_dir);
15460 }
15461 }
15462 }
15463 break;
15464 case DW_LNS_set_column:
15465 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15466 line_ptr += bytes_read;
15467 break;
15468 case DW_LNS_negate_stmt:
15469 is_stmt = (!is_stmt);
15470 break;
15471 case DW_LNS_set_basic_block:
15472 basic_block = 1;
15473 break;
15474 /* Add to the address register of the state machine the
15475 address increment value corresponding to special opcode
15476 255. I.e., this value is scaled by the minimum
15477 instruction length since special opcode 255 would have
15478 scaled the increment. */
15479 case DW_LNS_const_add_pc:
15480 {
15481 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15482
15483 address += (((op_index + adjust)
15484 / lh->maximum_ops_per_instruction)
15485 * lh->minimum_instruction_length);
15486 op_index = ((op_index + adjust)
15487 % lh->maximum_ops_per_instruction);
15488 }
15489 break;
15490 case DW_LNS_fixed_advance_pc:
15491 address += read_2_bytes (abfd, line_ptr);
15492 op_index = 0;
15493 line_ptr += 2;
15494 break;
15495 default:
15496 {
15497 /* Unknown standard opcode, ignore it. */
15498 int i;
15499
15500 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15501 {
15502 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15503 line_ptr += bytes_read;
15504 }
15505 }
15506 }
15507 }
15508 if (lh->num_file_names < file || file == 0)
15509 dwarf2_debug_line_missing_file_complaint ();
15510 else
15511 {
15512 lh->file_names[file - 1].included_p = 1;
15513 if (!decode_for_pst_p)
15514 {
15515 addr = gdbarch_addr_bits_remove (gdbarch, address);
15516 (*p_record_line) (current_subfile, 0, addr);
15517 }
15518 }
15519 }
15520 }
15521
15522 /* Decode the Line Number Program (LNP) for the given line_header
15523 structure and CU. The actual information extracted and the type
15524 of structures created from the LNP depends on the value of PST.
15525
15526 1. If PST is NULL, then this procedure uses the data from the program
15527 to create all necessary symbol tables, and their linetables.
15528
15529 2. If PST is not NULL, this procedure reads the program to determine
15530 the list of files included by the unit represented by PST, and
15531 builds all the associated partial symbol tables.
15532
15533 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15534 It is used for relative paths in the line table.
15535 NOTE: When processing partial symtabs (pst != NULL),
15536 comp_dir == pst->dirname.
15537
15538 NOTE: It is important that psymtabs have the same file name (via strcmp)
15539 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15540 symtab we don't use it in the name of the psymtabs we create.
15541 E.g. expand_line_sal requires this when finding psymtabs to expand.
15542 A good testcase for this is mb-inline.exp. */
15543
15544 static void
15545 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15546 struct dwarf2_cu *cu, struct partial_symtab *pst,
15547 int want_line_info)
15548 {
15549 struct objfile *objfile = cu->objfile;
15550 const int decode_for_pst_p = (pst != NULL);
15551 struct subfile *first_subfile = current_subfile;
15552
15553 if (want_line_info)
15554 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15555
15556 if (decode_for_pst_p)
15557 {
15558 int file_index;
15559
15560 /* Now that we're done scanning the Line Header Program, we can
15561 create the psymtab of each included file. */
15562 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15563 if (lh->file_names[file_index].included_p == 1)
15564 {
15565 char *include_name =
15566 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15567 if (include_name != NULL)
15568 dwarf2_create_include_psymtab (include_name, pst, objfile);
15569 }
15570 }
15571 else
15572 {
15573 /* Make sure a symtab is created for every file, even files
15574 which contain only variables (i.e. no code with associated
15575 line numbers). */
15576 int i;
15577
15578 for (i = 0; i < lh->num_file_names; i++)
15579 {
15580 char *dir = NULL;
15581 struct file_entry *fe;
15582
15583 fe = &lh->file_names[i];
15584 if (fe->dir_index)
15585 dir = lh->include_dirs[fe->dir_index - 1];
15586 dwarf2_start_subfile (fe->name, dir, comp_dir);
15587
15588 /* Skip the main file; we don't need it, and it must be
15589 allocated last, so that it will show up before the
15590 non-primary symtabs in the objfile's symtab list. */
15591 if (current_subfile == first_subfile)
15592 continue;
15593
15594 if (current_subfile->symtab == NULL)
15595 current_subfile->symtab = allocate_symtab (current_subfile->name,
15596 objfile);
15597 fe->symtab = current_subfile->symtab;
15598 }
15599 }
15600 }
15601
15602 /* Start a subfile for DWARF. FILENAME is the name of the file and
15603 DIRNAME the name of the source directory which contains FILENAME
15604 or NULL if not known. COMP_DIR is the compilation directory for the
15605 linetable's compilation unit or NULL if not known.
15606 This routine tries to keep line numbers from identical absolute and
15607 relative file names in a common subfile.
15608
15609 Using the `list' example from the GDB testsuite, which resides in
15610 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15611 of /srcdir/list0.c yields the following debugging information for list0.c:
15612
15613 DW_AT_name: /srcdir/list0.c
15614 DW_AT_comp_dir: /compdir
15615 files.files[0].name: list0.h
15616 files.files[0].dir: /srcdir
15617 files.files[1].name: list0.c
15618 files.files[1].dir: /srcdir
15619
15620 The line number information for list0.c has to end up in a single
15621 subfile, so that `break /srcdir/list0.c:1' works as expected.
15622 start_subfile will ensure that this happens provided that we pass the
15623 concatenation of files.files[1].dir and files.files[1].name as the
15624 subfile's name. */
15625
15626 static void
15627 dwarf2_start_subfile (char *filename, const char *dirname,
15628 const char *comp_dir)
15629 {
15630 char *fullname;
15631
15632 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15633 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15634 second argument to start_subfile. To be consistent, we do the
15635 same here. In order not to lose the line information directory,
15636 we concatenate it to the filename when it makes sense.
15637 Note that the Dwarf3 standard says (speaking of filenames in line
15638 information): ``The directory index is ignored for file names
15639 that represent full path names''. Thus ignoring dirname in the
15640 `else' branch below isn't an issue. */
15641
15642 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15643 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15644 else
15645 fullname = filename;
15646
15647 start_subfile (fullname, comp_dir);
15648
15649 if (fullname != filename)
15650 xfree (fullname);
15651 }
15652
15653 /* Start a symtab for DWARF.
15654 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15655
15656 static void
15657 dwarf2_start_symtab (struct dwarf2_cu *cu,
15658 const char *name, const char *comp_dir, CORE_ADDR low_pc)
15659 {
15660 start_symtab (name, comp_dir, low_pc);
15661 record_debugformat ("DWARF 2");
15662 record_producer (cu->producer);
15663
15664 /* We assume that we're processing GCC output. */
15665 processing_gcc_compilation = 2;
15666
15667 cu->processing_has_namespace_info = 0;
15668 }
15669
15670 static void
15671 var_decode_location (struct attribute *attr, struct symbol *sym,
15672 struct dwarf2_cu *cu)
15673 {
15674 struct objfile *objfile = cu->objfile;
15675 struct comp_unit_head *cu_header = &cu->header;
15676
15677 /* NOTE drow/2003-01-30: There used to be a comment and some special
15678 code here to turn a symbol with DW_AT_external and a
15679 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15680 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15681 with some versions of binutils) where shared libraries could have
15682 relocations against symbols in their debug information - the
15683 minimal symbol would have the right address, but the debug info
15684 would not. It's no longer necessary, because we will explicitly
15685 apply relocations when we read in the debug information now. */
15686
15687 /* A DW_AT_location attribute with no contents indicates that a
15688 variable has been optimized away. */
15689 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15690 {
15691 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15692 return;
15693 }
15694
15695 /* Handle one degenerate form of location expression specially, to
15696 preserve GDB's previous behavior when section offsets are
15697 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15698 then mark this symbol as LOC_STATIC. */
15699
15700 if (attr_form_is_block (attr)
15701 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15702 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15703 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15704 && (DW_BLOCK (attr)->size
15705 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15706 {
15707 unsigned int dummy;
15708
15709 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15710 SYMBOL_VALUE_ADDRESS (sym) =
15711 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15712 else
15713 SYMBOL_VALUE_ADDRESS (sym) =
15714 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15715 SYMBOL_CLASS (sym) = LOC_STATIC;
15716 fixup_symbol_section (sym, objfile);
15717 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15718 SYMBOL_SECTION (sym));
15719 return;
15720 }
15721
15722 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15723 expression evaluator, and use LOC_COMPUTED only when necessary
15724 (i.e. when the value of a register or memory location is
15725 referenced, or a thread-local block, etc.). Then again, it might
15726 not be worthwhile. I'm assuming that it isn't unless performance
15727 or memory numbers show me otherwise. */
15728
15729 dwarf2_symbol_mark_computed (attr, sym, cu);
15730 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15731
15732 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15733 cu->has_loclist = 1;
15734 }
15735
15736 /* Given a pointer to a DWARF information entry, figure out if we need
15737 to make a symbol table entry for it, and if so, create a new entry
15738 and return a pointer to it.
15739 If TYPE is NULL, determine symbol type from the die, otherwise
15740 used the passed type.
15741 If SPACE is not NULL, use it to hold the new symbol. If it is
15742 NULL, allocate a new symbol on the objfile's obstack. */
15743
15744 static struct symbol *
15745 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15746 struct symbol *space)
15747 {
15748 struct objfile *objfile = cu->objfile;
15749 struct symbol *sym = NULL;
15750 const char *name;
15751 struct attribute *attr = NULL;
15752 struct attribute *attr2 = NULL;
15753 CORE_ADDR baseaddr;
15754 struct pending **list_to_add = NULL;
15755
15756 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15757
15758 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15759
15760 name = dwarf2_name (die, cu);
15761 if (name)
15762 {
15763 const char *linkagename;
15764 int suppress_add = 0;
15765
15766 if (space)
15767 sym = space;
15768 else
15769 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15770 OBJSTAT (objfile, n_syms++);
15771
15772 /* Cache this symbol's name and the name's demangled form (if any). */
15773 SYMBOL_SET_LANGUAGE (sym, cu->language);
15774 linkagename = dwarf2_physname (name, die, cu);
15775 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15776
15777 /* Fortran does not have mangling standard and the mangling does differ
15778 between gfortran, iFort etc. */
15779 if (cu->language == language_fortran
15780 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15781 symbol_set_demangled_name (&(sym->ginfo),
15782 dwarf2_full_name (name, die, cu),
15783 NULL);
15784
15785 /* Default assumptions.
15786 Use the passed type or decode it from the die. */
15787 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15788 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15789 if (type != NULL)
15790 SYMBOL_TYPE (sym) = type;
15791 else
15792 SYMBOL_TYPE (sym) = die_type (die, cu);
15793 attr = dwarf2_attr (die,
15794 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15795 cu);
15796 if (attr)
15797 {
15798 SYMBOL_LINE (sym) = DW_UNSND (attr);
15799 }
15800
15801 attr = dwarf2_attr (die,
15802 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15803 cu);
15804 if (attr)
15805 {
15806 int file_index = DW_UNSND (attr);
15807
15808 if (cu->line_header == NULL
15809 || file_index > cu->line_header->num_file_names)
15810 complaint (&symfile_complaints,
15811 _("file index out of range"));
15812 else if (file_index > 0)
15813 {
15814 struct file_entry *fe;
15815
15816 fe = &cu->line_header->file_names[file_index - 1];
15817 SYMBOL_SYMTAB (sym) = fe->symtab;
15818 }
15819 }
15820
15821 switch (die->tag)
15822 {
15823 case DW_TAG_label:
15824 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15825 if (attr)
15826 {
15827 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15828 }
15829 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15830 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15831 SYMBOL_CLASS (sym) = LOC_LABEL;
15832 add_symbol_to_list (sym, cu->list_in_scope);
15833 break;
15834 case DW_TAG_subprogram:
15835 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15836 finish_block. */
15837 SYMBOL_CLASS (sym) = LOC_BLOCK;
15838 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15839 if ((attr2 && (DW_UNSND (attr2) != 0))
15840 || cu->language == language_ada)
15841 {
15842 /* Subprograms marked external are stored as a global symbol.
15843 Ada subprograms, whether marked external or not, are always
15844 stored as a global symbol, because we want to be able to
15845 access them globally. For instance, we want to be able
15846 to break on a nested subprogram without having to
15847 specify the context. */
15848 list_to_add = &global_symbols;
15849 }
15850 else
15851 {
15852 list_to_add = cu->list_in_scope;
15853 }
15854 break;
15855 case DW_TAG_inlined_subroutine:
15856 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15857 finish_block. */
15858 SYMBOL_CLASS (sym) = LOC_BLOCK;
15859 SYMBOL_INLINED (sym) = 1;
15860 list_to_add = cu->list_in_scope;
15861 break;
15862 case DW_TAG_template_value_param:
15863 suppress_add = 1;
15864 /* Fall through. */
15865 case DW_TAG_constant:
15866 case DW_TAG_variable:
15867 case DW_TAG_member:
15868 /* Compilation with minimal debug info may result in
15869 variables with missing type entries. Change the
15870 misleading `void' type to something sensible. */
15871 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15872 SYMBOL_TYPE (sym)
15873 = objfile_type (objfile)->nodebug_data_symbol;
15874
15875 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15876 /* In the case of DW_TAG_member, we should only be called for
15877 static const members. */
15878 if (die->tag == DW_TAG_member)
15879 {
15880 /* dwarf2_add_field uses die_is_declaration,
15881 so we do the same. */
15882 gdb_assert (die_is_declaration (die, cu));
15883 gdb_assert (attr);
15884 }
15885 if (attr)
15886 {
15887 dwarf2_const_value (attr, sym, cu);
15888 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15889 if (!suppress_add)
15890 {
15891 if (attr2 && (DW_UNSND (attr2) != 0))
15892 list_to_add = &global_symbols;
15893 else
15894 list_to_add = cu->list_in_scope;
15895 }
15896 break;
15897 }
15898 attr = dwarf2_attr (die, DW_AT_location, cu);
15899 if (attr)
15900 {
15901 var_decode_location (attr, sym, cu);
15902 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15903
15904 /* Fortran explicitly imports any global symbols to the local
15905 scope by DW_TAG_common_block. */
15906 if (cu->language == language_fortran && die->parent
15907 && die->parent->tag == DW_TAG_common_block)
15908 attr2 = NULL;
15909
15910 if (SYMBOL_CLASS (sym) == LOC_STATIC
15911 && SYMBOL_VALUE_ADDRESS (sym) == 0
15912 && !dwarf2_per_objfile->has_section_at_zero)
15913 {
15914 /* When a static variable is eliminated by the linker,
15915 the corresponding debug information is not stripped
15916 out, but the variable address is set to null;
15917 do not add such variables into symbol table. */
15918 }
15919 else if (attr2 && (DW_UNSND (attr2) != 0))
15920 {
15921 /* Workaround gfortran PR debug/40040 - it uses
15922 DW_AT_location for variables in -fPIC libraries which may
15923 get overriden by other libraries/executable and get
15924 a different address. Resolve it by the minimal symbol
15925 which may come from inferior's executable using copy
15926 relocation. Make this workaround only for gfortran as for
15927 other compilers GDB cannot guess the minimal symbol
15928 Fortran mangling kind. */
15929 if (cu->language == language_fortran && die->parent
15930 && die->parent->tag == DW_TAG_module
15931 && cu->producer
15932 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15933 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15934
15935 /* A variable with DW_AT_external is never static,
15936 but it may be block-scoped. */
15937 list_to_add = (cu->list_in_scope == &file_symbols
15938 ? &global_symbols : cu->list_in_scope);
15939 }
15940 else
15941 list_to_add = cu->list_in_scope;
15942 }
15943 else
15944 {
15945 /* We do not know the address of this symbol.
15946 If it is an external symbol and we have type information
15947 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15948 The address of the variable will then be determined from
15949 the minimal symbol table whenever the variable is
15950 referenced. */
15951 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15952
15953 /* Fortran explicitly imports any global symbols to the local
15954 scope by DW_TAG_common_block. */
15955 if (cu->language == language_fortran && die->parent
15956 && die->parent->tag == DW_TAG_common_block)
15957 {
15958 /* SYMBOL_CLASS doesn't matter here because
15959 read_common_block is going to reset it. */
15960 if (!suppress_add)
15961 list_to_add = cu->list_in_scope;
15962 }
15963 else if (attr2 && (DW_UNSND (attr2) != 0)
15964 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
15965 {
15966 /* A variable with DW_AT_external is never static, but it
15967 may be block-scoped. */
15968 list_to_add = (cu->list_in_scope == &file_symbols
15969 ? &global_symbols : cu->list_in_scope);
15970
15971 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15972 }
15973 else if (!die_is_declaration (die, cu))
15974 {
15975 /* Use the default LOC_OPTIMIZED_OUT class. */
15976 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15977 if (!suppress_add)
15978 list_to_add = cu->list_in_scope;
15979 }
15980 }
15981 break;
15982 case DW_TAG_formal_parameter:
15983 /* If we are inside a function, mark this as an argument. If
15984 not, we might be looking at an argument to an inlined function
15985 when we do not have enough information to show inlined frames;
15986 pretend it's a local variable in that case so that the user can
15987 still see it. */
15988 if (context_stack_depth > 0
15989 && context_stack[context_stack_depth - 1].name != NULL)
15990 SYMBOL_IS_ARGUMENT (sym) = 1;
15991 attr = dwarf2_attr (die, DW_AT_location, cu);
15992 if (attr)
15993 {
15994 var_decode_location (attr, sym, cu);
15995 }
15996 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15997 if (attr)
15998 {
15999 dwarf2_const_value (attr, sym, cu);
16000 }
16001
16002 list_to_add = cu->list_in_scope;
16003 break;
16004 case DW_TAG_unspecified_parameters:
16005 /* From varargs functions; gdb doesn't seem to have any
16006 interest in this information, so just ignore it for now.
16007 (FIXME?) */
16008 break;
16009 case DW_TAG_template_type_param:
16010 suppress_add = 1;
16011 /* Fall through. */
16012 case DW_TAG_class_type:
16013 case DW_TAG_interface_type:
16014 case DW_TAG_structure_type:
16015 case DW_TAG_union_type:
16016 case DW_TAG_set_type:
16017 case DW_TAG_enumeration_type:
16018 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16019 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16020
16021 {
16022 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16023 really ever be static objects: otherwise, if you try
16024 to, say, break of a class's method and you're in a file
16025 which doesn't mention that class, it won't work unless
16026 the check for all static symbols in lookup_symbol_aux
16027 saves you. See the OtherFileClass tests in
16028 gdb.c++/namespace.exp. */
16029
16030 if (!suppress_add)
16031 {
16032 list_to_add = (cu->list_in_scope == &file_symbols
16033 && (cu->language == language_cplus
16034 || cu->language == language_java)
16035 ? &global_symbols : cu->list_in_scope);
16036
16037 /* The semantics of C++ state that "struct foo {
16038 ... }" also defines a typedef for "foo". A Java
16039 class declaration also defines a typedef for the
16040 class. */
16041 if (cu->language == language_cplus
16042 || cu->language == language_java
16043 || cu->language == language_ada)
16044 {
16045 /* The symbol's name is already allocated along
16046 with this objfile, so we don't need to
16047 duplicate it for the type. */
16048 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16049 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16050 }
16051 }
16052 }
16053 break;
16054 case DW_TAG_typedef:
16055 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16056 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16057 list_to_add = cu->list_in_scope;
16058 break;
16059 case DW_TAG_base_type:
16060 case DW_TAG_subrange_type:
16061 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16062 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16063 list_to_add = cu->list_in_scope;
16064 break;
16065 case DW_TAG_enumerator:
16066 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16067 if (attr)
16068 {
16069 dwarf2_const_value (attr, sym, cu);
16070 }
16071 {
16072 /* NOTE: carlton/2003-11-10: See comment above in the
16073 DW_TAG_class_type, etc. block. */
16074
16075 list_to_add = (cu->list_in_scope == &file_symbols
16076 && (cu->language == language_cplus
16077 || cu->language == language_java)
16078 ? &global_symbols : cu->list_in_scope);
16079 }
16080 break;
16081 case DW_TAG_namespace:
16082 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16083 list_to_add = &global_symbols;
16084 break;
16085 case DW_TAG_common_block:
16086 SYMBOL_CLASS (sym) = LOC_COMMON_BLOCK;
16087 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16088 add_symbol_to_list (sym, cu->list_in_scope);
16089 break;
16090 default:
16091 /* Not a tag we recognize. Hopefully we aren't processing
16092 trash data, but since we must specifically ignore things
16093 we don't recognize, there is nothing else we should do at
16094 this point. */
16095 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16096 dwarf_tag_name (die->tag));
16097 break;
16098 }
16099
16100 if (suppress_add)
16101 {
16102 sym->hash_next = objfile->template_symbols;
16103 objfile->template_symbols = sym;
16104 list_to_add = NULL;
16105 }
16106
16107 if (list_to_add != NULL)
16108 add_symbol_to_list (sym, list_to_add);
16109
16110 /* For the benefit of old versions of GCC, check for anonymous
16111 namespaces based on the demangled name. */
16112 if (!cu->processing_has_namespace_info
16113 && cu->language == language_cplus)
16114 cp_scan_for_anonymous_namespaces (sym, objfile);
16115 }
16116 return (sym);
16117 }
16118
16119 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16120
16121 static struct symbol *
16122 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16123 {
16124 return new_symbol_full (die, type, cu, NULL);
16125 }
16126
16127 /* Given an attr with a DW_FORM_dataN value in host byte order,
16128 zero-extend it as appropriate for the symbol's type. The DWARF
16129 standard (v4) is not entirely clear about the meaning of using
16130 DW_FORM_dataN for a constant with a signed type, where the type is
16131 wider than the data. The conclusion of a discussion on the DWARF
16132 list was that this is unspecified. We choose to always zero-extend
16133 because that is the interpretation long in use by GCC. */
16134
16135 static gdb_byte *
16136 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16137 const char *name, struct obstack *obstack,
16138 struct dwarf2_cu *cu, LONGEST *value, int bits)
16139 {
16140 struct objfile *objfile = cu->objfile;
16141 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16142 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16143 LONGEST l = DW_UNSND (attr);
16144
16145 if (bits < sizeof (*value) * 8)
16146 {
16147 l &= ((LONGEST) 1 << bits) - 1;
16148 *value = l;
16149 }
16150 else if (bits == sizeof (*value) * 8)
16151 *value = l;
16152 else
16153 {
16154 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16155 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16156 return bytes;
16157 }
16158
16159 return NULL;
16160 }
16161
16162 /* Read a constant value from an attribute. Either set *VALUE, or if
16163 the value does not fit in *VALUE, set *BYTES - either already
16164 allocated on the objfile obstack, or newly allocated on OBSTACK,
16165 or, set *BATON, if we translated the constant to a location
16166 expression. */
16167
16168 static void
16169 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16170 const char *name, struct obstack *obstack,
16171 struct dwarf2_cu *cu,
16172 LONGEST *value, gdb_byte **bytes,
16173 struct dwarf2_locexpr_baton **baton)
16174 {
16175 struct objfile *objfile = cu->objfile;
16176 struct comp_unit_head *cu_header = &cu->header;
16177 struct dwarf_block *blk;
16178 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16179 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16180
16181 *value = 0;
16182 *bytes = NULL;
16183 *baton = NULL;
16184
16185 switch (attr->form)
16186 {
16187 case DW_FORM_addr:
16188 case DW_FORM_GNU_addr_index:
16189 {
16190 gdb_byte *data;
16191
16192 if (TYPE_LENGTH (type) != cu_header->addr_size)
16193 dwarf2_const_value_length_mismatch_complaint (name,
16194 cu_header->addr_size,
16195 TYPE_LENGTH (type));
16196 /* Symbols of this form are reasonably rare, so we just
16197 piggyback on the existing location code rather than writing
16198 a new implementation of symbol_computed_ops. */
16199 *baton = obstack_alloc (&objfile->objfile_obstack,
16200 sizeof (struct dwarf2_locexpr_baton));
16201 (*baton)->per_cu = cu->per_cu;
16202 gdb_assert ((*baton)->per_cu);
16203
16204 (*baton)->size = 2 + cu_header->addr_size;
16205 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16206 (*baton)->data = data;
16207
16208 data[0] = DW_OP_addr;
16209 store_unsigned_integer (&data[1], cu_header->addr_size,
16210 byte_order, DW_ADDR (attr));
16211 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16212 }
16213 break;
16214 case DW_FORM_string:
16215 case DW_FORM_strp:
16216 case DW_FORM_GNU_str_index:
16217 case DW_FORM_GNU_strp_alt:
16218 /* DW_STRING is already allocated on the objfile obstack, point
16219 directly to it. */
16220 *bytes = (gdb_byte *) DW_STRING (attr);
16221 break;
16222 case DW_FORM_block1:
16223 case DW_FORM_block2:
16224 case DW_FORM_block4:
16225 case DW_FORM_block:
16226 case DW_FORM_exprloc:
16227 blk = DW_BLOCK (attr);
16228 if (TYPE_LENGTH (type) != blk->size)
16229 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16230 TYPE_LENGTH (type));
16231 *bytes = blk->data;
16232 break;
16233
16234 /* The DW_AT_const_value attributes are supposed to carry the
16235 symbol's value "represented as it would be on the target
16236 architecture." By the time we get here, it's already been
16237 converted to host endianness, so we just need to sign- or
16238 zero-extend it as appropriate. */
16239 case DW_FORM_data1:
16240 *bytes = dwarf2_const_value_data (attr, type, name,
16241 obstack, cu, value, 8);
16242 break;
16243 case DW_FORM_data2:
16244 *bytes = dwarf2_const_value_data (attr, type, name,
16245 obstack, cu, value, 16);
16246 break;
16247 case DW_FORM_data4:
16248 *bytes = dwarf2_const_value_data (attr, type, name,
16249 obstack, cu, value, 32);
16250 break;
16251 case DW_FORM_data8:
16252 *bytes = dwarf2_const_value_data (attr, type, name,
16253 obstack, cu, value, 64);
16254 break;
16255
16256 case DW_FORM_sdata:
16257 *value = DW_SND (attr);
16258 break;
16259
16260 case DW_FORM_udata:
16261 *value = DW_UNSND (attr);
16262 break;
16263
16264 default:
16265 complaint (&symfile_complaints,
16266 _("unsupported const value attribute form: '%s'"),
16267 dwarf_form_name (attr->form));
16268 *value = 0;
16269 break;
16270 }
16271 }
16272
16273
16274 /* Copy constant value from an attribute to a symbol. */
16275
16276 static void
16277 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16278 struct dwarf2_cu *cu)
16279 {
16280 struct objfile *objfile = cu->objfile;
16281 struct comp_unit_head *cu_header = &cu->header;
16282 LONGEST value;
16283 gdb_byte *bytes;
16284 struct dwarf2_locexpr_baton *baton;
16285
16286 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16287 SYMBOL_PRINT_NAME (sym),
16288 &objfile->objfile_obstack, cu,
16289 &value, &bytes, &baton);
16290
16291 if (baton != NULL)
16292 {
16293 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16294 SYMBOL_LOCATION_BATON (sym) = baton;
16295 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16296 }
16297 else if (bytes != NULL)
16298 {
16299 SYMBOL_VALUE_BYTES (sym) = bytes;
16300 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16301 }
16302 else
16303 {
16304 SYMBOL_VALUE (sym) = value;
16305 SYMBOL_CLASS (sym) = LOC_CONST;
16306 }
16307 }
16308
16309 /* Return the type of the die in question using its DW_AT_type attribute. */
16310
16311 static struct type *
16312 die_type (struct die_info *die, struct dwarf2_cu *cu)
16313 {
16314 struct attribute *type_attr;
16315
16316 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16317 if (!type_attr)
16318 {
16319 /* A missing DW_AT_type represents a void type. */
16320 return objfile_type (cu->objfile)->builtin_void;
16321 }
16322
16323 return lookup_die_type (die, type_attr, cu);
16324 }
16325
16326 /* True iff CU's producer generates GNAT Ada auxiliary information
16327 that allows to find parallel types through that information instead
16328 of having to do expensive parallel lookups by type name. */
16329
16330 static int
16331 need_gnat_info (struct dwarf2_cu *cu)
16332 {
16333 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16334 of GNAT produces this auxiliary information, without any indication
16335 that it is produced. Part of enhancing the FSF version of GNAT
16336 to produce that information will be to put in place an indicator
16337 that we can use in order to determine whether the descriptive type
16338 info is available or not. One suggestion that has been made is
16339 to use a new attribute, attached to the CU die. For now, assume
16340 that the descriptive type info is not available. */
16341 return 0;
16342 }
16343
16344 /* Return the auxiliary type of the die in question using its
16345 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16346 attribute is not present. */
16347
16348 static struct type *
16349 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16350 {
16351 struct attribute *type_attr;
16352
16353 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16354 if (!type_attr)
16355 return NULL;
16356
16357 return lookup_die_type (die, type_attr, cu);
16358 }
16359
16360 /* If DIE has a descriptive_type attribute, then set the TYPE's
16361 descriptive type accordingly. */
16362
16363 static void
16364 set_descriptive_type (struct type *type, struct die_info *die,
16365 struct dwarf2_cu *cu)
16366 {
16367 struct type *descriptive_type = die_descriptive_type (die, cu);
16368
16369 if (descriptive_type)
16370 {
16371 ALLOCATE_GNAT_AUX_TYPE (type);
16372 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16373 }
16374 }
16375
16376 /* Return the containing type of the die in question using its
16377 DW_AT_containing_type attribute. */
16378
16379 static struct type *
16380 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16381 {
16382 struct attribute *type_attr;
16383
16384 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16385 if (!type_attr)
16386 error (_("Dwarf Error: Problem turning containing type into gdb type "
16387 "[in module %s]"), cu->objfile->name);
16388
16389 return lookup_die_type (die, type_attr, cu);
16390 }
16391
16392 /* Look up the type of DIE in CU using its type attribute ATTR.
16393 If there is no type substitute an error marker. */
16394
16395 static struct type *
16396 lookup_die_type (struct die_info *die, struct attribute *attr,
16397 struct dwarf2_cu *cu)
16398 {
16399 struct objfile *objfile = cu->objfile;
16400 struct type *this_type;
16401
16402 /* First see if we have it cached. */
16403
16404 if (attr->form == DW_FORM_GNU_ref_alt)
16405 {
16406 struct dwarf2_per_cu_data *per_cu;
16407 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16408
16409 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16410 this_type = get_die_type_at_offset (offset, per_cu);
16411 }
16412 else if (is_ref_attr (attr))
16413 {
16414 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16415
16416 this_type = get_die_type_at_offset (offset, cu->per_cu);
16417 }
16418 else if (attr->form == DW_FORM_ref_sig8)
16419 {
16420 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16421
16422 /* sig_type will be NULL if the signatured type is missing from
16423 the debug info. */
16424 if (sig_type == NULL)
16425 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16426 "at 0x%x [in module %s]"),
16427 die->offset.sect_off, objfile->name);
16428
16429 gdb_assert (sig_type->per_cu.is_debug_types);
16430 /* If we haven't filled in type_offset_in_section yet, then we
16431 haven't read the type in yet. */
16432 this_type = NULL;
16433 if (sig_type->type_offset_in_section.sect_off != 0)
16434 {
16435 this_type =
16436 get_die_type_at_offset (sig_type->type_offset_in_section,
16437 &sig_type->per_cu);
16438 }
16439 }
16440 else
16441 {
16442 dump_die_for_error (die);
16443 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16444 dwarf_attr_name (attr->name), objfile->name);
16445 }
16446
16447 /* If not cached we need to read it in. */
16448
16449 if (this_type == NULL)
16450 {
16451 struct die_info *type_die;
16452 struct dwarf2_cu *type_cu = cu;
16453
16454 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16455 /* If we found the type now, it's probably because the type came
16456 from an inter-CU reference and the type's CU got expanded before
16457 ours. */
16458 this_type = get_die_type (type_die, type_cu);
16459 if (this_type == NULL)
16460 this_type = read_type_die_1 (type_die, type_cu);
16461 }
16462
16463 /* If we still don't have a type use an error marker. */
16464
16465 if (this_type == NULL)
16466 {
16467 char *message, *saved;
16468
16469 /* read_type_die already issued a complaint. */
16470 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16471 objfile->name,
16472 cu->header.offset.sect_off,
16473 die->offset.sect_off);
16474 saved = obstack_copy0 (&objfile->objfile_obstack,
16475 message, strlen (message));
16476 xfree (message);
16477
16478 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16479 }
16480
16481 return this_type;
16482 }
16483
16484 /* Return the type in DIE, CU.
16485 Returns NULL for invalid types.
16486
16487 This first does a lookup in the appropriate type_hash table,
16488 and only reads the die in if necessary.
16489
16490 NOTE: This can be called when reading in partial or full symbols. */
16491
16492 static struct type *
16493 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16494 {
16495 struct type *this_type;
16496
16497 this_type = get_die_type (die, cu);
16498 if (this_type)
16499 return this_type;
16500
16501 return read_type_die_1 (die, cu);
16502 }
16503
16504 /* Read the type in DIE, CU.
16505 Returns NULL for invalid types. */
16506
16507 static struct type *
16508 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16509 {
16510 struct type *this_type = NULL;
16511
16512 switch (die->tag)
16513 {
16514 case DW_TAG_class_type:
16515 case DW_TAG_interface_type:
16516 case DW_TAG_structure_type:
16517 case DW_TAG_union_type:
16518 this_type = read_structure_type (die, cu);
16519 break;
16520 case DW_TAG_enumeration_type:
16521 this_type = read_enumeration_type (die, cu);
16522 break;
16523 case DW_TAG_subprogram:
16524 case DW_TAG_subroutine_type:
16525 case DW_TAG_inlined_subroutine:
16526 this_type = read_subroutine_type (die, cu);
16527 break;
16528 case DW_TAG_array_type:
16529 this_type = read_array_type (die, cu);
16530 break;
16531 case DW_TAG_set_type:
16532 this_type = read_set_type (die, cu);
16533 break;
16534 case DW_TAG_pointer_type:
16535 this_type = read_tag_pointer_type (die, cu);
16536 break;
16537 case DW_TAG_ptr_to_member_type:
16538 this_type = read_tag_ptr_to_member_type (die, cu);
16539 break;
16540 case DW_TAG_reference_type:
16541 this_type = read_tag_reference_type (die, cu);
16542 break;
16543 case DW_TAG_const_type:
16544 this_type = read_tag_const_type (die, cu);
16545 break;
16546 case DW_TAG_volatile_type:
16547 this_type = read_tag_volatile_type (die, cu);
16548 break;
16549 case DW_TAG_restrict_type:
16550 this_type = read_tag_restrict_type (die, cu);
16551 break;
16552 case DW_TAG_string_type:
16553 this_type = read_tag_string_type (die, cu);
16554 break;
16555 case DW_TAG_typedef:
16556 this_type = read_typedef (die, cu);
16557 break;
16558 case DW_TAG_subrange_type:
16559 this_type = read_subrange_type (die, cu);
16560 break;
16561 case DW_TAG_base_type:
16562 this_type = read_base_type (die, cu);
16563 break;
16564 case DW_TAG_unspecified_type:
16565 this_type = read_unspecified_type (die, cu);
16566 break;
16567 case DW_TAG_namespace:
16568 this_type = read_namespace_type (die, cu);
16569 break;
16570 case DW_TAG_module:
16571 this_type = read_module_type (die, cu);
16572 break;
16573 default:
16574 complaint (&symfile_complaints,
16575 _("unexpected tag in read_type_die: '%s'"),
16576 dwarf_tag_name (die->tag));
16577 break;
16578 }
16579
16580 return this_type;
16581 }
16582
16583 /* See if we can figure out if the class lives in a namespace. We do
16584 this by looking for a member function; its demangled name will
16585 contain namespace info, if there is any.
16586 Return the computed name or NULL.
16587 Space for the result is allocated on the objfile's obstack.
16588 This is the full-die version of guess_partial_die_structure_name.
16589 In this case we know DIE has no useful parent. */
16590
16591 static char *
16592 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16593 {
16594 struct die_info *spec_die;
16595 struct dwarf2_cu *spec_cu;
16596 struct die_info *child;
16597
16598 spec_cu = cu;
16599 spec_die = die_specification (die, &spec_cu);
16600 if (spec_die != NULL)
16601 {
16602 die = spec_die;
16603 cu = spec_cu;
16604 }
16605
16606 for (child = die->child;
16607 child != NULL;
16608 child = child->sibling)
16609 {
16610 if (child->tag == DW_TAG_subprogram)
16611 {
16612 struct attribute *attr;
16613
16614 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16615 if (attr == NULL)
16616 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16617 if (attr != NULL)
16618 {
16619 char *actual_name
16620 = language_class_name_from_physname (cu->language_defn,
16621 DW_STRING (attr));
16622 char *name = NULL;
16623
16624 if (actual_name != NULL)
16625 {
16626 const char *die_name = dwarf2_name (die, cu);
16627
16628 if (die_name != NULL
16629 && strcmp (die_name, actual_name) != 0)
16630 {
16631 /* Strip off the class name from the full name.
16632 We want the prefix. */
16633 int die_name_len = strlen (die_name);
16634 int actual_name_len = strlen (actual_name);
16635
16636 /* Test for '::' as a sanity check. */
16637 if (actual_name_len > die_name_len + 2
16638 && actual_name[actual_name_len
16639 - die_name_len - 1] == ':')
16640 name =
16641 obstack_copy0 (&cu->objfile->objfile_obstack,
16642 actual_name,
16643 actual_name_len - die_name_len - 2);
16644 }
16645 }
16646 xfree (actual_name);
16647 return name;
16648 }
16649 }
16650 }
16651
16652 return NULL;
16653 }
16654
16655 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16656 prefix part in such case. See
16657 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16658
16659 static char *
16660 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16661 {
16662 struct attribute *attr;
16663 char *base;
16664
16665 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16666 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16667 return NULL;
16668
16669 attr = dwarf2_attr (die, DW_AT_name, cu);
16670 if (attr != NULL && DW_STRING (attr) != NULL)
16671 return NULL;
16672
16673 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16674 if (attr == NULL)
16675 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16676 if (attr == NULL || DW_STRING (attr) == NULL)
16677 return NULL;
16678
16679 /* dwarf2_name had to be already called. */
16680 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16681
16682 /* Strip the base name, keep any leading namespaces/classes. */
16683 base = strrchr (DW_STRING (attr), ':');
16684 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16685 return "";
16686
16687 return obstack_copy0 (&cu->objfile->objfile_obstack,
16688 DW_STRING (attr), &base[-1] - DW_STRING (attr));
16689 }
16690
16691 /* Return the name of the namespace/class that DIE is defined within,
16692 or "" if we can't tell. The caller should not xfree the result.
16693
16694 For example, if we're within the method foo() in the following
16695 code:
16696
16697 namespace N {
16698 class C {
16699 void foo () {
16700 }
16701 };
16702 }
16703
16704 then determine_prefix on foo's die will return "N::C". */
16705
16706 static const char *
16707 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16708 {
16709 struct die_info *parent, *spec_die;
16710 struct dwarf2_cu *spec_cu;
16711 struct type *parent_type;
16712 char *retval;
16713
16714 if (cu->language != language_cplus && cu->language != language_java
16715 && cu->language != language_fortran)
16716 return "";
16717
16718 retval = anonymous_struct_prefix (die, cu);
16719 if (retval)
16720 return retval;
16721
16722 /* We have to be careful in the presence of DW_AT_specification.
16723 For example, with GCC 3.4, given the code
16724
16725 namespace N {
16726 void foo() {
16727 // Definition of N::foo.
16728 }
16729 }
16730
16731 then we'll have a tree of DIEs like this:
16732
16733 1: DW_TAG_compile_unit
16734 2: DW_TAG_namespace // N
16735 3: DW_TAG_subprogram // declaration of N::foo
16736 4: DW_TAG_subprogram // definition of N::foo
16737 DW_AT_specification // refers to die #3
16738
16739 Thus, when processing die #4, we have to pretend that we're in
16740 the context of its DW_AT_specification, namely the contex of die
16741 #3. */
16742 spec_cu = cu;
16743 spec_die = die_specification (die, &spec_cu);
16744 if (spec_die == NULL)
16745 parent = die->parent;
16746 else
16747 {
16748 parent = spec_die->parent;
16749 cu = spec_cu;
16750 }
16751
16752 if (parent == NULL)
16753 return "";
16754 else if (parent->building_fullname)
16755 {
16756 const char *name;
16757 const char *parent_name;
16758
16759 /* It has been seen on RealView 2.2 built binaries,
16760 DW_TAG_template_type_param types actually _defined_ as
16761 children of the parent class:
16762
16763 enum E {};
16764 template class <class Enum> Class{};
16765 Class<enum E> class_e;
16766
16767 1: DW_TAG_class_type (Class)
16768 2: DW_TAG_enumeration_type (E)
16769 3: DW_TAG_enumerator (enum1:0)
16770 3: DW_TAG_enumerator (enum2:1)
16771 ...
16772 2: DW_TAG_template_type_param
16773 DW_AT_type DW_FORM_ref_udata (E)
16774
16775 Besides being broken debug info, it can put GDB into an
16776 infinite loop. Consider:
16777
16778 When we're building the full name for Class<E>, we'll start
16779 at Class, and go look over its template type parameters,
16780 finding E. We'll then try to build the full name of E, and
16781 reach here. We're now trying to build the full name of E,
16782 and look over the parent DIE for containing scope. In the
16783 broken case, if we followed the parent DIE of E, we'd again
16784 find Class, and once again go look at its template type
16785 arguments, etc., etc. Simply don't consider such parent die
16786 as source-level parent of this die (it can't be, the language
16787 doesn't allow it), and break the loop here. */
16788 name = dwarf2_name (die, cu);
16789 parent_name = dwarf2_name (parent, cu);
16790 complaint (&symfile_complaints,
16791 _("template param type '%s' defined within parent '%s'"),
16792 name ? name : "<unknown>",
16793 parent_name ? parent_name : "<unknown>");
16794 return "";
16795 }
16796 else
16797 switch (parent->tag)
16798 {
16799 case DW_TAG_namespace:
16800 parent_type = read_type_die (parent, cu);
16801 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16802 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16803 Work around this problem here. */
16804 if (cu->language == language_cplus
16805 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16806 return "";
16807 /* We give a name to even anonymous namespaces. */
16808 return TYPE_TAG_NAME (parent_type);
16809 case DW_TAG_class_type:
16810 case DW_TAG_interface_type:
16811 case DW_TAG_structure_type:
16812 case DW_TAG_union_type:
16813 case DW_TAG_module:
16814 parent_type = read_type_die (parent, cu);
16815 if (TYPE_TAG_NAME (parent_type) != NULL)
16816 return TYPE_TAG_NAME (parent_type);
16817 else
16818 /* An anonymous structure is only allowed non-static data
16819 members; no typedefs, no member functions, et cetera.
16820 So it does not need a prefix. */
16821 return "";
16822 case DW_TAG_compile_unit:
16823 case DW_TAG_partial_unit:
16824 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16825 if (cu->language == language_cplus
16826 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16827 && die->child != NULL
16828 && (die->tag == DW_TAG_class_type
16829 || die->tag == DW_TAG_structure_type
16830 || die->tag == DW_TAG_union_type))
16831 {
16832 char *name = guess_full_die_structure_name (die, cu);
16833 if (name != NULL)
16834 return name;
16835 }
16836 return "";
16837 default:
16838 return determine_prefix (parent, cu);
16839 }
16840 }
16841
16842 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16843 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16844 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16845 an obconcat, otherwise allocate storage for the result. The CU argument is
16846 used to determine the language and hence, the appropriate separator. */
16847
16848 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16849
16850 static char *
16851 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16852 int physname, struct dwarf2_cu *cu)
16853 {
16854 const char *lead = "";
16855 const char *sep;
16856
16857 if (suffix == NULL || suffix[0] == '\0'
16858 || prefix == NULL || prefix[0] == '\0')
16859 sep = "";
16860 else if (cu->language == language_java)
16861 sep = ".";
16862 else if (cu->language == language_fortran && physname)
16863 {
16864 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16865 DW_AT_MIPS_linkage_name is preferred and used instead. */
16866
16867 lead = "__";
16868 sep = "_MOD_";
16869 }
16870 else
16871 sep = "::";
16872
16873 if (prefix == NULL)
16874 prefix = "";
16875 if (suffix == NULL)
16876 suffix = "";
16877
16878 if (obs == NULL)
16879 {
16880 char *retval
16881 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16882
16883 strcpy (retval, lead);
16884 strcat (retval, prefix);
16885 strcat (retval, sep);
16886 strcat (retval, suffix);
16887 return retval;
16888 }
16889 else
16890 {
16891 /* We have an obstack. */
16892 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16893 }
16894 }
16895
16896 /* Return sibling of die, NULL if no sibling. */
16897
16898 static struct die_info *
16899 sibling_die (struct die_info *die)
16900 {
16901 return die->sibling;
16902 }
16903
16904 /* Get name of a die, return NULL if not found. */
16905
16906 static const char *
16907 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
16908 struct obstack *obstack)
16909 {
16910 if (name && cu->language == language_cplus)
16911 {
16912 char *canon_name = cp_canonicalize_string (name);
16913
16914 if (canon_name != NULL)
16915 {
16916 if (strcmp (canon_name, name) != 0)
16917 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
16918 xfree (canon_name);
16919 }
16920 }
16921
16922 return name;
16923 }
16924
16925 /* Get name of a die, return NULL if not found. */
16926
16927 static const char *
16928 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16929 {
16930 struct attribute *attr;
16931
16932 attr = dwarf2_attr (die, DW_AT_name, cu);
16933 if ((!attr || !DW_STRING (attr))
16934 && die->tag != DW_TAG_class_type
16935 && die->tag != DW_TAG_interface_type
16936 && die->tag != DW_TAG_structure_type
16937 && die->tag != DW_TAG_union_type)
16938 return NULL;
16939
16940 switch (die->tag)
16941 {
16942 case DW_TAG_compile_unit:
16943 case DW_TAG_partial_unit:
16944 /* Compilation units have a DW_AT_name that is a filename, not
16945 a source language identifier. */
16946 case DW_TAG_enumeration_type:
16947 case DW_TAG_enumerator:
16948 /* These tags always have simple identifiers already; no need
16949 to canonicalize them. */
16950 return DW_STRING (attr);
16951
16952 case DW_TAG_subprogram:
16953 /* Java constructors will all be named "<init>", so return
16954 the class name when we see this special case. */
16955 if (cu->language == language_java
16956 && DW_STRING (attr) != NULL
16957 && strcmp (DW_STRING (attr), "<init>") == 0)
16958 {
16959 struct dwarf2_cu *spec_cu = cu;
16960 struct die_info *spec_die;
16961
16962 /* GCJ will output '<init>' for Java constructor names.
16963 For this special case, return the name of the parent class. */
16964
16965 /* GCJ may output suprogram DIEs with AT_specification set.
16966 If so, use the name of the specified DIE. */
16967 spec_die = die_specification (die, &spec_cu);
16968 if (spec_die != NULL)
16969 return dwarf2_name (spec_die, spec_cu);
16970
16971 do
16972 {
16973 die = die->parent;
16974 if (die->tag == DW_TAG_class_type)
16975 return dwarf2_name (die, cu);
16976 }
16977 while (die->tag != DW_TAG_compile_unit
16978 && die->tag != DW_TAG_partial_unit);
16979 }
16980 break;
16981
16982 case DW_TAG_class_type:
16983 case DW_TAG_interface_type:
16984 case DW_TAG_structure_type:
16985 case DW_TAG_union_type:
16986 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16987 structures or unions. These were of the form "._%d" in GCC 4.1,
16988 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16989 and GCC 4.4. We work around this problem by ignoring these. */
16990 if (attr && DW_STRING (attr)
16991 && (strncmp (DW_STRING (attr), "._", 2) == 0
16992 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16993 return NULL;
16994
16995 /* GCC might emit a nameless typedef that has a linkage name. See
16996 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16997 if (!attr || DW_STRING (attr) == NULL)
16998 {
16999 char *demangled = NULL;
17000
17001 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17002 if (attr == NULL)
17003 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17004
17005 if (attr == NULL || DW_STRING (attr) == NULL)
17006 return NULL;
17007
17008 /* Avoid demangling DW_STRING (attr) the second time on a second
17009 call for the same DIE. */
17010 if (!DW_STRING_IS_CANONICAL (attr))
17011 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17012
17013 if (demangled)
17014 {
17015 char *base;
17016
17017 /* FIXME: we already did this for the partial symbol... */
17018 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17019 demangled, strlen (demangled));
17020 DW_STRING_IS_CANONICAL (attr) = 1;
17021 xfree (demangled);
17022
17023 /* Strip any leading namespaces/classes, keep only the base name.
17024 DW_AT_name for named DIEs does not contain the prefixes. */
17025 base = strrchr (DW_STRING (attr), ':');
17026 if (base && base > DW_STRING (attr) && base[-1] == ':')
17027 return &base[1];
17028 else
17029 return DW_STRING (attr);
17030 }
17031 }
17032 break;
17033
17034 default:
17035 break;
17036 }
17037
17038 if (!DW_STRING_IS_CANONICAL (attr))
17039 {
17040 DW_STRING (attr)
17041 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17042 &cu->objfile->objfile_obstack);
17043 DW_STRING_IS_CANONICAL (attr) = 1;
17044 }
17045 return DW_STRING (attr);
17046 }
17047
17048 /* Return the die that this die in an extension of, or NULL if there
17049 is none. *EXT_CU is the CU containing DIE on input, and the CU
17050 containing the return value on output. */
17051
17052 static struct die_info *
17053 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17054 {
17055 struct attribute *attr;
17056
17057 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17058 if (attr == NULL)
17059 return NULL;
17060
17061 return follow_die_ref (die, attr, ext_cu);
17062 }
17063
17064 /* Convert a DIE tag into its string name. */
17065
17066 static const char *
17067 dwarf_tag_name (unsigned tag)
17068 {
17069 const char *name = get_DW_TAG_name (tag);
17070
17071 if (name == NULL)
17072 return "DW_TAG_<unknown>";
17073
17074 return name;
17075 }
17076
17077 /* Convert a DWARF attribute code into its string name. */
17078
17079 static const char *
17080 dwarf_attr_name (unsigned attr)
17081 {
17082 const char *name;
17083
17084 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17085 if (attr == DW_AT_MIPS_fde)
17086 return "DW_AT_MIPS_fde";
17087 #else
17088 if (attr == DW_AT_HP_block_index)
17089 return "DW_AT_HP_block_index";
17090 #endif
17091
17092 name = get_DW_AT_name (attr);
17093
17094 if (name == NULL)
17095 return "DW_AT_<unknown>";
17096
17097 return name;
17098 }
17099
17100 /* Convert a DWARF value form code into its string name. */
17101
17102 static const char *
17103 dwarf_form_name (unsigned form)
17104 {
17105 const char *name = get_DW_FORM_name (form);
17106
17107 if (name == NULL)
17108 return "DW_FORM_<unknown>";
17109
17110 return name;
17111 }
17112
17113 static char *
17114 dwarf_bool_name (unsigned mybool)
17115 {
17116 if (mybool)
17117 return "TRUE";
17118 else
17119 return "FALSE";
17120 }
17121
17122 /* Convert a DWARF type code into its string name. */
17123
17124 static const char *
17125 dwarf_type_encoding_name (unsigned enc)
17126 {
17127 const char *name = get_DW_ATE_name (enc);
17128
17129 if (name == NULL)
17130 return "DW_ATE_<unknown>";
17131
17132 return name;
17133 }
17134
17135 static void
17136 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17137 {
17138 unsigned int i;
17139
17140 print_spaces (indent, f);
17141 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17142 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17143
17144 if (die->parent != NULL)
17145 {
17146 print_spaces (indent, f);
17147 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17148 die->parent->offset.sect_off);
17149 }
17150
17151 print_spaces (indent, f);
17152 fprintf_unfiltered (f, " has children: %s\n",
17153 dwarf_bool_name (die->child != NULL));
17154
17155 print_spaces (indent, f);
17156 fprintf_unfiltered (f, " attributes:\n");
17157
17158 for (i = 0; i < die->num_attrs; ++i)
17159 {
17160 print_spaces (indent, f);
17161 fprintf_unfiltered (f, " %s (%s) ",
17162 dwarf_attr_name (die->attrs[i].name),
17163 dwarf_form_name (die->attrs[i].form));
17164
17165 switch (die->attrs[i].form)
17166 {
17167 case DW_FORM_addr:
17168 case DW_FORM_GNU_addr_index:
17169 fprintf_unfiltered (f, "address: ");
17170 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17171 break;
17172 case DW_FORM_block2:
17173 case DW_FORM_block4:
17174 case DW_FORM_block:
17175 case DW_FORM_block1:
17176 fprintf_unfiltered (f, "block: size %s",
17177 pulongest (DW_BLOCK (&die->attrs[i])->size));
17178 break;
17179 case DW_FORM_exprloc:
17180 fprintf_unfiltered (f, "expression: size %s",
17181 pulongest (DW_BLOCK (&die->attrs[i])->size));
17182 break;
17183 case DW_FORM_ref_addr:
17184 fprintf_unfiltered (f, "ref address: ");
17185 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17186 break;
17187 case DW_FORM_GNU_ref_alt:
17188 fprintf_unfiltered (f, "alt ref address: ");
17189 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17190 break;
17191 case DW_FORM_ref1:
17192 case DW_FORM_ref2:
17193 case DW_FORM_ref4:
17194 case DW_FORM_ref8:
17195 case DW_FORM_ref_udata:
17196 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17197 (long) (DW_UNSND (&die->attrs[i])));
17198 break;
17199 case DW_FORM_data1:
17200 case DW_FORM_data2:
17201 case DW_FORM_data4:
17202 case DW_FORM_data8:
17203 case DW_FORM_udata:
17204 case DW_FORM_sdata:
17205 fprintf_unfiltered (f, "constant: %s",
17206 pulongest (DW_UNSND (&die->attrs[i])));
17207 break;
17208 case DW_FORM_sec_offset:
17209 fprintf_unfiltered (f, "section offset: %s",
17210 pulongest (DW_UNSND (&die->attrs[i])));
17211 break;
17212 case DW_FORM_ref_sig8:
17213 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17214 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
17215 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
17216 else
17217 fprintf_unfiltered (f, "signatured type, offset: unknown");
17218 break;
17219 case DW_FORM_string:
17220 case DW_FORM_strp:
17221 case DW_FORM_GNU_str_index:
17222 case DW_FORM_GNU_strp_alt:
17223 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17224 DW_STRING (&die->attrs[i])
17225 ? DW_STRING (&die->attrs[i]) : "",
17226 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17227 break;
17228 case DW_FORM_flag:
17229 if (DW_UNSND (&die->attrs[i]))
17230 fprintf_unfiltered (f, "flag: TRUE");
17231 else
17232 fprintf_unfiltered (f, "flag: FALSE");
17233 break;
17234 case DW_FORM_flag_present:
17235 fprintf_unfiltered (f, "flag: TRUE");
17236 break;
17237 case DW_FORM_indirect:
17238 /* The reader will have reduced the indirect form to
17239 the "base form" so this form should not occur. */
17240 fprintf_unfiltered (f,
17241 "unexpected attribute form: DW_FORM_indirect");
17242 break;
17243 default:
17244 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17245 die->attrs[i].form);
17246 break;
17247 }
17248 fprintf_unfiltered (f, "\n");
17249 }
17250 }
17251
17252 static void
17253 dump_die_for_error (struct die_info *die)
17254 {
17255 dump_die_shallow (gdb_stderr, 0, die);
17256 }
17257
17258 static void
17259 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17260 {
17261 int indent = level * 4;
17262
17263 gdb_assert (die != NULL);
17264
17265 if (level >= max_level)
17266 return;
17267
17268 dump_die_shallow (f, indent, die);
17269
17270 if (die->child != NULL)
17271 {
17272 print_spaces (indent, f);
17273 fprintf_unfiltered (f, " Children:");
17274 if (level + 1 < max_level)
17275 {
17276 fprintf_unfiltered (f, "\n");
17277 dump_die_1 (f, level + 1, max_level, die->child);
17278 }
17279 else
17280 {
17281 fprintf_unfiltered (f,
17282 " [not printed, max nesting level reached]\n");
17283 }
17284 }
17285
17286 if (die->sibling != NULL && level > 0)
17287 {
17288 dump_die_1 (f, level, max_level, die->sibling);
17289 }
17290 }
17291
17292 /* This is called from the pdie macro in gdbinit.in.
17293 It's not static so gcc will keep a copy callable from gdb. */
17294
17295 void
17296 dump_die (struct die_info *die, int max_level)
17297 {
17298 dump_die_1 (gdb_stdlog, 0, max_level, die);
17299 }
17300
17301 static void
17302 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17303 {
17304 void **slot;
17305
17306 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17307 INSERT);
17308
17309 *slot = die;
17310 }
17311
17312 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17313 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17314
17315 static int
17316 is_ref_attr (struct attribute *attr)
17317 {
17318 switch (attr->form)
17319 {
17320 case DW_FORM_ref_addr:
17321 case DW_FORM_ref1:
17322 case DW_FORM_ref2:
17323 case DW_FORM_ref4:
17324 case DW_FORM_ref8:
17325 case DW_FORM_ref_udata:
17326 case DW_FORM_GNU_ref_alt:
17327 return 1;
17328 default:
17329 return 0;
17330 }
17331 }
17332
17333 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17334 required kind. */
17335
17336 static sect_offset
17337 dwarf2_get_ref_die_offset (struct attribute *attr)
17338 {
17339 sect_offset retval = { DW_UNSND (attr) };
17340
17341 if (is_ref_attr (attr))
17342 return retval;
17343
17344 retval.sect_off = 0;
17345 complaint (&symfile_complaints,
17346 _("unsupported die ref attribute form: '%s'"),
17347 dwarf_form_name (attr->form));
17348 return retval;
17349 }
17350
17351 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17352 * the value held by the attribute is not constant. */
17353
17354 static LONGEST
17355 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17356 {
17357 if (attr->form == DW_FORM_sdata)
17358 return DW_SND (attr);
17359 else if (attr->form == DW_FORM_udata
17360 || attr->form == DW_FORM_data1
17361 || attr->form == DW_FORM_data2
17362 || attr->form == DW_FORM_data4
17363 || attr->form == DW_FORM_data8)
17364 return DW_UNSND (attr);
17365 else
17366 {
17367 complaint (&symfile_complaints,
17368 _("Attribute value is not a constant (%s)"),
17369 dwarf_form_name (attr->form));
17370 return default_value;
17371 }
17372 }
17373
17374 /* Follow reference or signature attribute ATTR of SRC_DIE.
17375 On entry *REF_CU is the CU of SRC_DIE.
17376 On exit *REF_CU is the CU of the result. */
17377
17378 static struct die_info *
17379 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17380 struct dwarf2_cu **ref_cu)
17381 {
17382 struct die_info *die;
17383
17384 if (is_ref_attr (attr))
17385 die = follow_die_ref (src_die, attr, ref_cu);
17386 else if (attr->form == DW_FORM_ref_sig8)
17387 die = follow_die_sig (src_die, attr, ref_cu);
17388 else
17389 {
17390 dump_die_for_error (src_die);
17391 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17392 (*ref_cu)->objfile->name);
17393 }
17394
17395 return die;
17396 }
17397
17398 /* Follow reference OFFSET.
17399 On entry *REF_CU is the CU of the source die referencing OFFSET.
17400 On exit *REF_CU is the CU of the result.
17401 Returns NULL if OFFSET is invalid. */
17402
17403 static struct die_info *
17404 follow_die_offset (sect_offset offset, int offset_in_dwz,
17405 struct dwarf2_cu **ref_cu)
17406 {
17407 struct die_info temp_die;
17408 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17409
17410 gdb_assert (cu->per_cu != NULL);
17411
17412 target_cu = cu;
17413
17414 if (cu->per_cu->is_debug_types)
17415 {
17416 /* .debug_types CUs cannot reference anything outside their CU.
17417 If they need to, they have to reference a signatured type via
17418 DW_FORM_ref_sig8. */
17419 if (! offset_in_cu_p (&cu->header, offset))
17420 return NULL;
17421 }
17422 else if (offset_in_dwz != cu->per_cu->is_dwz
17423 || ! offset_in_cu_p (&cu->header, offset))
17424 {
17425 struct dwarf2_per_cu_data *per_cu;
17426
17427 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17428 cu->objfile);
17429
17430 /* If necessary, add it to the queue and load its DIEs. */
17431 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17432 load_full_comp_unit (per_cu, cu->language);
17433
17434 target_cu = per_cu->cu;
17435 }
17436 else if (cu->dies == NULL)
17437 {
17438 /* We're loading full DIEs during partial symbol reading. */
17439 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17440 load_full_comp_unit (cu->per_cu, language_minimal);
17441 }
17442
17443 *ref_cu = target_cu;
17444 temp_die.offset = offset;
17445 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17446 }
17447
17448 /* Follow reference attribute ATTR of SRC_DIE.
17449 On entry *REF_CU is the CU of SRC_DIE.
17450 On exit *REF_CU is the CU of the result. */
17451
17452 static struct die_info *
17453 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17454 struct dwarf2_cu **ref_cu)
17455 {
17456 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17457 struct dwarf2_cu *cu = *ref_cu;
17458 struct die_info *die;
17459
17460 die = follow_die_offset (offset,
17461 (attr->form == DW_FORM_GNU_ref_alt
17462 || cu->per_cu->is_dwz),
17463 ref_cu);
17464 if (!die)
17465 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17466 "at 0x%x [in module %s]"),
17467 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17468
17469 return die;
17470 }
17471
17472 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17473 Returned value is intended for DW_OP_call*. Returned
17474 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17475
17476 struct dwarf2_locexpr_baton
17477 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17478 struct dwarf2_per_cu_data *per_cu,
17479 CORE_ADDR (*get_frame_pc) (void *baton),
17480 void *baton)
17481 {
17482 struct dwarf2_cu *cu;
17483 struct die_info *die;
17484 struct attribute *attr;
17485 struct dwarf2_locexpr_baton retval;
17486
17487 dw2_setup (per_cu->objfile);
17488
17489 if (per_cu->cu == NULL)
17490 load_cu (per_cu);
17491 cu = per_cu->cu;
17492
17493 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17494 if (!die)
17495 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17496 offset.sect_off, per_cu->objfile->name);
17497
17498 attr = dwarf2_attr (die, DW_AT_location, cu);
17499 if (!attr)
17500 {
17501 /* DWARF: "If there is no such attribute, then there is no effect.".
17502 DATA is ignored if SIZE is 0. */
17503
17504 retval.data = NULL;
17505 retval.size = 0;
17506 }
17507 else if (attr_form_is_section_offset (attr))
17508 {
17509 struct dwarf2_loclist_baton loclist_baton;
17510 CORE_ADDR pc = (*get_frame_pc) (baton);
17511 size_t size;
17512
17513 fill_in_loclist_baton (cu, &loclist_baton, attr);
17514
17515 retval.data = dwarf2_find_location_expression (&loclist_baton,
17516 &size, pc);
17517 retval.size = size;
17518 }
17519 else
17520 {
17521 if (!attr_form_is_block (attr))
17522 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17523 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17524 offset.sect_off, per_cu->objfile->name);
17525
17526 retval.data = DW_BLOCK (attr)->data;
17527 retval.size = DW_BLOCK (attr)->size;
17528 }
17529 retval.per_cu = cu->per_cu;
17530
17531 age_cached_comp_units ();
17532
17533 return retval;
17534 }
17535
17536 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17537 offset. */
17538
17539 struct dwarf2_locexpr_baton
17540 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17541 struct dwarf2_per_cu_data *per_cu,
17542 CORE_ADDR (*get_frame_pc) (void *baton),
17543 void *baton)
17544 {
17545 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17546
17547 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17548 }
17549
17550 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17551 PER_CU. */
17552
17553 struct type *
17554 dwarf2_get_die_type (cu_offset die_offset,
17555 struct dwarf2_per_cu_data *per_cu)
17556 {
17557 sect_offset die_offset_sect;
17558
17559 dw2_setup (per_cu->objfile);
17560
17561 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17562 return get_die_type_at_offset (die_offset_sect, per_cu);
17563 }
17564
17565 /* Follow the signature attribute ATTR in SRC_DIE.
17566 On entry *REF_CU is the CU of SRC_DIE.
17567 On exit *REF_CU is the CU of the result. */
17568
17569 static struct die_info *
17570 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17571 struct dwarf2_cu **ref_cu)
17572 {
17573 struct objfile *objfile = (*ref_cu)->objfile;
17574 struct die_info temp_die;
17575 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17576 struct dwarf2_cu *sig_cu;
17577 struct die_info *die;
17578
17579 /* sig_type will be NULL if the signatured type is missing from
17580 the debug info. */
17581 if (sig_type == NULL)
17582 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17583 "at 0x%x [in module %s]"),
17584 src_die->offset.sect_off, objfile->name);
17585
17586 /* If necessary, add it to the queue and load its DIEs. */
17587
17588 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17589 read_signatured_type (sig_type);
17590
17591 gdb_assert (sig_type->per_cu.cu != NULL);
17592
17593 sig_cu = sig_type->per_cu.cu;
17594 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17595 temp_die.offset = sig_type->type_offset_in_section;
17596 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17597 temp_die.offset.sect_off);
17598 if (die)
17599 {
17600 /* For .gdb_index version 7 keep track of included TUs.
17601 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17602 if (dwarf2_per_objfile->index_table != NULL
17603 && dwarf2_per_objfile->index_table->version <= 7)
17604 {
17605 VEC_safe_push (dwarf2_per_cu_ptr,
17606 (*ref_cu)->per_cu->imported_symtabs,
17607 sig_cu->per_cu);
17608 }
17609
17610 *ref_cu = sig_cu;
17611 return die;
17612 }
17613
17614 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17615 "from DIE at 0x%x [in module %s]"),
17616 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17617 }
17618
17619 /* Given an offset of a signatured type, return its signatured_type. */
17620
17621 static struct signatured_type *
17622 lookup_signatured_type_at_offset (struct objfile *objfile,
17623 struct dwarf2_section_info *section,
17624 sect_offset offset)
17625 {
17626 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17627 unsigned int length, initial_length_size;
17628 unsigned int sig_offset;
17629 struct signatured_type find_entry, *sig_type;
17630
17631 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17632 sig_offset = (initial_length_size
17633 + 2 /*version*/
17634 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17635 + 1 /*address_size*/);
17636 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17637 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17638
17639 /* This is only used to lookup previously recorded types.
17640 If we didn't find it, it's our bug. */
17641 gdb_assert (sig_type != NULL);
17642 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17643
17644 return sig_type;
17645 }
17646
17647 /* Load the DIEs associated with type unit PER_CU into memory. */
17648
17649 static void
17650 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17651 {
17652 struct signatured_type *sig_type;
17653
17654 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17655 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17656
17657 /* We have the per_cu, but we need the signatured_type.
17658 Fortunately this is an easy translation. */
17659 gdb_assert (per_cu->is_debug_types);
17660 sig_type = (struct signatured_type *) per_cu;
17661
17662 gdb_assert (per_cu->cu == NULL);
17663
17664 read_signatured_type (sig_type);
17665
17666 gdb_assert (per_cu->cu != NULL);
17667 }
17668
17669 /* die_reader_func for read_signatured_type.
17670 This is identical to load_full_comp_unit_reader,
17671 but is kept separate for now. */
17672
17673 static void
17674 read_signatured_type_reader (const struct die_reader_specs *reader,
17675 gdb_byte *info_ptr,
17676 struct die_info *comp_unit_die,
17677 int has_children,
17678 void *data)
17679 {
17680 struct dwarf2_cu *cu = reader->cu;
17681
17682 gdb_assert (cu->die_hash == NULL);
17683 cu->die_hash =
17684 htab_create_alloc_ex (cu->header.length / 12,
17685 die_hash,
17686 die_eq,
17687 NULL,
17688 &cu->comp_unit_obstack,
17689 hashtab_obstack_allocate,
17690 dummy_obstack_deallocate);
17691
17692 if (has_children)
17693 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17694 &info_ptr, comp_unit_die);
17695 cu->dies = comp_unit_die;
17696 /* comp_unit_die is not stored in die_hash, no need. */
17697
17698 /* We try not to read any attributes in this function, because not
17699 all CUs needed for references have been loaded yet, and symbol
17700 table processing isn't initialized. But we have to set the CU language,
17701 or we won't be able to build types correctly.
17702 Similarly, if we do not read the producer, we can not apply
17703 producer-specific interpretation. */
17704 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17705 }
17706
17707 /* Read in a signatured type and build its CU and DIEs.
17708 If the type is a stub for the real type in a DWO file,
17709 read in the real type from the DWO file as well. */
17710
17711 static void
17712 read_signatured_type (struct signatured_type *sig_type)
17713 {
17714 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17715
17716 gdb_assert (per_cu->is_debug_types);
17717 gdb_assert (per_cu->cu == NULL);
17718
17719 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17720 read_signatured_type_reader, NULL);
17721 }
17722
17723 /* Decode simple location descriptions.
17724 Given a pointer to a dwarf block that defines a location, compute
17725 the location and return the value.
17726
17727 NOTE drow/2003-11-18: This function is called in two situations
17728 now: for the address of static or global variables (partial symbols
17729 only) and for offsets into structures which are expected to be
17730 (more or less) constant. The partial symbol case should go away,
17731 and only the constant case should remain. That will let this
17732 function complain more accurately. A few special modes are allowed
17733 without complaint for global variables (for instance, global
17734 register values and thread-local values).
17735
17736 A location description containing no operations indicates that the
17737 object is optimized out. The return value is 0 for that case.
17738 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17739 callers will only want a very basic result and this can become a
17740 complaint.
17741
17742 Note that stack[0] is unused except as a default error return. */
17743
17744 static CORE_ADDR
17745 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17746 {
17747 struct objfile *objfile = cu->objfile;
17748 size_t i;
17749 size_t size = blk->size;
17750 gdb_byte *data = blk->data;
17751 CORE_ADDR stack[64];
17752 int stacki;
17753 unsigned int bytes_read, unsnd;
17754 gdb_byte op;
17755
17756 i = 0;
17757 stacki = 0;
17758 stack[stacki] = 0;
17759 stack[++stacki] = 0;
17760
17761 while (i < size)
17762 {
17763 op = data[i++];
17764 switch (op)
17765 {
17766 case DW_OP_lit0:
17767 case DW_OP_lit1:
17768 case DW_OP_lit2:
17769 case DW_OP_lit3:
17770 case DW_OP_lit4:
17771 case DW_OP_lit5:
17772 case DW_OP_lit6:
17773 case DW_OP_lit7:
17774 case DW_OP_lit8:
17775 case DW_OP_lit9:
17776 case DW_OP_lit10:
17777 case DW_OP_lit11:
17778 case DW_OP_lit12:
17779 case DW_OP_lit13:
17780 case DW_OP_lit14:
17781 case DW_OP_lit15:
17782 case DW_OP_lit16:
17783 case DW_OP_lit17:
17784 case DW_OP_lit18:
17785 case DW_OP_lit19:
17786 case DW_OP_lit20:
17787 case DW_OP_lit21:
17788 case DW_OP_lit22:
17789 case DW_OP_lit23:
17790 case DW_OP_lit24:
17791 case DW_OP_lit25:
17792 case DW_OP_lit26:
17793 case DW_OP_lit27:
17794 case DW_OP_lit28:
17795 case DW_OP_lit29:
17796 case DW_OP_lit30:
17797 case DW_OP_lit31:
17798 stack[++stacki] = op - DW_OP_lit0;
17799 break;
17800
17801 case DW_OP_reg0:
17802 case DW_OP_reg1:
17803 case DW_OP_reg2:
17804 case DW_OP_reg3:
17805 case DW_OP_reg4:
17806 case DW_OP_reg5:
17807 case DW_OP_reg6:
17808 case DW_OP_reg7:
17809 case DW_OP_reg8:
17810 case DW_OP_reg9:
17811 case DW_OP_reg10:
17812 case DW_OP_reg11:
17813 case DW_OP_reg12:
17814 case DW_OP_reg13:
17815 case DW_OP_reg14:
17816 case DW_OP_reg15:
17817 case DW_OP_reg16:
17818 case DW_OP_reg17:
17819 case DW_OP_reg18:
17820 case DW_OP_reg19:
17821 case DW_OP_reg20:
17822 case DW_OP_reg21:
17823 case DW_OP_reg22:
17824 case DW_OP_reg23:
17825 case DW_OP_reg24:
17826 case DW_OP_reg25:
17827 case DW_OP_reg26:
17828 case DW_OP_reg27:
17829 case DW_OP_reg28:
17830 case DW_OP_reg29:
17831 case DW_OP_reg30:
17832 case DW_OP_reg31:
17833 stack[++stacki] = op - DW_OP_reg0;
17834 if (i < size)
17835 dwarf2_complex_location_expr_complaint ();
17836 break;
17837
17838 case DW_OP_regx:
17839 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17840 i += bytes_read;
17841 stack[++stacki] = unsnd;
17842 if (i < size)
17843 dwarf2_complex_location_expr_complaint ();
17844 break;
17845
17846 case DW_OP_addr:
17847 stack[++stacki] = read_address (objfile->obfd, &data[i],
17848 cu, &bytes_read);
17849 i += bytes_read;
17850 break;
17851
17852 case DW_OP_const1u:
17853 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17854 i += 1;
17855 break;
17856
17857 case DW_OP_const1s:
17858 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17859 i += 1;
17860 break;
17861
17862 case DW_OP_const2u:
17863 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17864 i += 2;
17865 break;
17866
17867 case DW_OP_const2s:
17868 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17869 i += 2;
17870 break;
17871
17872 case DW_OP_const4u:
17873 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17874 i += 4;
17875 break;
17876
17877 case DW_OP_const4s:
17878 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17879 i += 4;
17880 break;
17881
17882 case DW_OP_const8u:
17883 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17884 i += 8;
17885 break;
17886
17887 case DW_OP_constu:
17888 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17889 &bytes_read);
17890 i += bytes_read;
17891 break;
17892
17893 case DW_OP_consts:
17894 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17895 i += bytes_read;
17896 break;
17897
17898 case DW_OP_dup:
17899 stack[stacki + 1] = stack[stacki];
17900 stacki++;
17901 break;
17902
17903 case DW_OP_plus:
17904 stack[stacki - 1] += stack[stacki];
17905 stacki--;
17906 break;
17907
17908 case DW_OP_plus_uconst:
17909 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17910 &bytes_read);
17911 i += bytes_read;
17912 break;
17913
17914 case DW_OP_minus:
17915 stack[stacki - 1] -= stack[stacki];
17916 stacki--;
17917 break;
17918
17919 case DW_OP_deref:
17920 /* If we're not the last op, then we definitely can't encode
17921 this using GDB's address_class enum. This is valid for partial
17922 global symbols, although the variable's address will be bogus
17923 in the psymtab. */
17924 if (i < size)
17925 dwarf2_complex_location_expr_complaint ();
17926 break;
17927
17928 case DW_OP_GNU_push_tls_address:
17929 /* The top of the stack has the offset from the beginning
17930 of the thread control block at which the variable is located. */
17931 /* Nothing should follow this operator, so the top of stack would
17932 be returned. */
17933 /* This is valid for partial global symbols, but the variable's
17934 address will be bogus in the psymtab. Make it always at least
17935 non-zero to not look as a variable garbage collected by linker
17936 which have DW_OP_addr 0. */
17937 if (i < size)
17938 dwarf2_complex_location_expr_complaint ();
17939 stack[stacki]++;
17940 break;
17941
17942 case DW_OP_GNU_uninit:
17943 break;
17944
17945 case DW_OP_GNU_addr_index:
17946 case DW_OP_GNU_const_index:
17947 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17948 &bytes_read);
17949 i += bytes_read;
17950 break;
17951
17952 default:
17953 {
17954 const char *name = get_DW_OP_name (op);
17955
17956 if (name)
17957 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17958 name);
17959 else
17960 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17961 op);
17962 }
17963
17964 return (stack[stacki]);
17965 }
17966
17967 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17968 outside of the allocated space. Also enforce minimum>0. */
17969 if (stacki >= ARRAY_SIZE (stack) - 1)
17970 {
17971 complaint (&symfile_complaints,
17972 _("location description stack overflow"));
17973 return 0;
17974 }
17975
17976 if (stacki <= 0)
17977 {
17978 complaint (&symfile_complaints,
17979 _("location description stack underflow"));
17980 return 0;
17981 }
17982 }
17983 return (stack[stacki]);
17984 }
17985
17986 /* memory allocation interface */
17987
17988 static struct dwarf_block *
17989 dwarf_alloc_block (struct dwarf2_cu *cu)
17990 {
17991 struct dwarf_block *blk;
17992
17993 blk = (struct dwarf_block *)
17994 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
17995 return (blk);
17996 }
17997
17998 static struct die_info *
17999 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18000 {
18001 struct die_info *die;
18002 size_t size = sizeof (struct die_info);
18003
18004 if (num_attrs > 1)
18005 size += (num_attrs - 1) * sizeof (struct attribute);
18006
18007 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18008 memset (die, 0, sizeof (struct die_info));
18009 return (die);
18010 }
18011
18012 \f
18013 /* Macro support. */
18014
18015 /* Return the full name of file number I in *LH's file name table.
18016 Use COMP_DIR as the name of the current directory of the
18017 compilation. The result is allocated using xmalloc; the caller is
18018 responsible for freeing it. */
18019 static char *
18020 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18021 {
18022 /* Is the file number a valid index into the line header's file name
18023 table? Remember that file numbers start with one, not zero. */
18024 if (1 <= file && file <= lh->num_file_names)
18025 {
18026 struct file_entry *fe = &lh->file_names[file - 1];
18027
18028 if (IS_ABSOLUTE_PATH (fe->name))
18029 return xstrdup (fe->name);
18030 else
18031 {
18032 const char *dir;
18033 int dir_len;
18034 char *full_name;
18035
18036 if (fe->dir_index)
18037 dir = lh->include_dirs[fe->dir_index - 1];
18038 else
18039 dir = comp_dir;
18040
18041 if (dir)
18042 {
18043 dir_len = strlen (dir);
18044 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
18045 strcpy (full_name, dir);
18046 full_name[dir_len] = '/';
18047 strcpy (full_name + dir_len + 1, fe->name);
18048 return full_name;
18049 }
18050 else
18051 return xstrdup (fe->name);
18052 }
18053 }
18054 else
18055 {
18056 /* The compiler produced a bogus file number. We can at least
18057 record the macro definitions made in the file, even if we
18058 won't be able to find the file by name. */
18059 char fake_name[80];
18060
18061 xsnprintf (fake_name, sizeof (fake_name),
18062 "<bad macro file number %d>", file);
18063
18064 complaint (&symfile_complaints,
18065 _("bad file number in macro information (%d)"),
18066 file);
18067
18068 return xstrdup (fake_name);
18069 }
18070 }
18071
18072
18073 static struct macro_source_file *
18074 macro_start_file (int file, int line,
18075 struct macro_source_file *current_file,
18076 const char *comp_dir,
18077 struct line_header *lh, struct objfile *objfile)
18078 {
18079 /* The full name of this source file. */
18080 char *full_name = file_full_name (file, lh, comp_dir);
18081
18082 /* We don't create a macro table for this compilation unit
18083 at all until we actually get a filename. */
18084 if (! pending_macros)
18085 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18086 objfile->per_bfd->macro_cache);
18087
18088 if (! current_file)
18089 {
18090 /* If we have no current file, then this must be the start_file
18091 directive for the compilation unit's main source file. */
18092 current_file = macro_set_main (pending_macros, full_name);
18093 macro_define_special (pending_macros);
18094 }
18095 else
18096 current_file = macro_include (current_file, line, full_name);
18097
18098 xfree (full_name);
18099
18100 return current_file;
18101 }
18102
18103
18104 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18105 followed by a null byte. */
18106 static char *
18107 copy_string (const char *buf, int len)
18108 {
18109 char *s = xmalloc (len + 1);
18110
18111 memcpy (s, buf, len);
18112 s[len] = '\0';
18113 return s;
18114 }
18115
18116
18117 static const char *
18118 consume_improper_spaces (const char *p, const char *body)
18119 {
18120 if (*p == ' ')
18121 {
18122 complaint (&symfile_complaints,
18123 _("macro definition contains spaces "
18124 "in formal argument list:\n`%s'"),
18125 body);
18126
18127 while (*p == ' ')
18128 p++;
18129 }
18130
18131 return p;
18132 }
18133
18134
18135 static void
18136 parse_macro_definition (struct macro_source_file *file, int line,
18137 const char *body)
18138 {
18139 const char *p;
18140
18141 /* The body string takes one of two forms. For object-like macro
18142 definitions, it should be:
18143
18144 <macro name> " " <definition>
18145
18146 For function-like macro definitions, it should be:
18147
18148 <macro name> "() " <definition>
18149 or
18150 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18151
18152 Spaces may appear only where explicitly indicated, and in the
18153 <definition>.
18154
18155 The Dwarf 2 spec says that an object-like macro's name is always
18156 followed by a space, but versions of GCC around March 2002 omit
18157 the space when the macro's definition is the empty string.
18158
18159 The Dwarf 2 spec says that there should be no spaces between the
18160 formal arguments in a function-like macro's formal argument list,
18161 but versions of GCC around March 2002 include spaces after the
18162 commas. */
18163
18164
18165 /* Find the extent of the macro name. The macro name is terminated
18166 by either a space or null character (for an object-like macro) or
18167 an opening paren (for a function-like macro). */
18168 for (p = body; *p; p++)
18169 if (*p == ' ' || *p == '(')
18170 break;
18171
18172 if (*p == ' ' || *p == '\0')
18173 {
18174 /* It's an object-like macro. */
18175 int name_len = p - body;
18176 char *name = copy_string (body, name_len);
18177 const char *replacement;
18178
18179 if (*p == ' ')
18180 replacement = body + name_len + 1;
18181 else
18182 {
18183 dwarf2_macro_malformed_definition_complaint (body);
18184 replacement = body + name_len;
18185 }
18186
18187 macro_define_object (file, line, name, replacement);
18188
18189 xfree (name);
18190 }
18191 else if (*p == '(')
18192 {
18193 /* It's a function-like macro. */
18194 char *name = copy_string (body, p - body);
18195 int argc = 0;
18196 int argv_size = 1;
18197 char **argv = xmalloc (argv_size * sizeof (*argv));
18198
18199 p++;
18200
18201 p = consume_improper_spaces (p, body);
18202
18203 /* Parse the formal argument list. */
18204 while (*p && *p != ')')
18205 {
18206 /* Find the extent of the current argument name. */
18207 const char *arg_start = p;
18208
18209 while (*p && *p != ',' && *p != ')' && *p != ' ')
18210 p++;
18211
18212 if (! *p || p == arg_start)
18213 dwarf2_macro_malformed_definition_complaint (body);
18214 else
18215 {
18216 /* Make sure argv has room for the new argument. */
18217 if (argc >= argv_size)
18218 {
18219 argv_size *= 2;
18220 argv = xrealloc (argv, argv_size * sizeof (*argv));
18221 }
18222
18223 argv[argc++] = copy_string (arg_start, p - arg_start);
18224 }
18225
18226 p = consume_improper_spaces (p, body);
18227
18228 /* Consume the comma, if present. */
18229 if (*p == ',')
18230 {
18231 p++;
18232
18233 p = consume_improper_spaces (p, body);
18234 }
18235 }
18236
18237 if (*p == ')')
18238 {
18239 p++;
18240
18241 if (*p == ' ')
18242 /* Perfectly formed definition, no complaints. */
18243 macro_define_function (file, line, name,
18244 argc, (const char **) argv,
18245 p + 1);
18246 else if (*p == '\0')
18247 {
18248 /* Complain, but do define it. */
18249 dwarf2_macro_malformed_definition_complaint (body);
18250 macro_define_function (file, line, name,
18251 argc, (const char **) argv,
18252 p);
18253 }
18254 else
18255 /* Just complain. */
18256 dwarf2_macro_malformed_definition_complaint (body);
18257 }
18258 else
18259 /* Just complain. */
18260 dwarf2_macro_malformed_definition_complaint (body);
18261
18262 xfree (name);
18263 {
18264 int i;
18265
18266 for (i = 0; i < argc; i++)
18267 xfree (argv[i]);
18268 }
18269 xfree (argv);
18270 }
18271 else
18272 dwarf2_macro_malformed_definition_complaint (body);
18273 }
18274
18275 /* Skip some bytes from BYTES according to the form given in FORM.
18276 Returns the new pointer. */
18277
18278 static gdb_byte *
18279 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18280 enum dwarf_form form,
18281 unsigned int offset_size,
18282 struct dwarf2_section_info *section)
18283 {
18284 unsigned int bytes_read;
18285
18286 switch (form)
18287 {
18288 case DW_FORM_data1:
18289 case DW_FORM_flag:
18290 ++bytes;
18291 break;
18292
18293 case DW_FORM_data2:
18294 bytes += 2;
18295 break;
18296
18297 case DW_FORM_data4:
18298 bytes += 4;
18299 break;
18300
18301 case DW_FORM_data8:
18302 bytes += 8;
18303 break;
18304
18305 case DW_FORM_string:
18306 read_direct_string (abfd, bytes, &bytes_read);
18307 bytes += bytes_read;
18308 break;
18309
18310 case DW_FORM_sec_offset:
18311 case DW_FORM_strp:
18312 case DW_FORM_GNU_strp_alt:
18313 bytes += offset_size;
18314 break;
18315
18316 case DW_FORM_block:
18317 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18318 bytes += bytes_read;
18319 break;
18320
18321 case DW_FORM_block1:
18322 bytes += 1 + read_1_byte (abfd, bytes);
18323 break;
18324 case DW_FORM_block2:
18325 bytes += 2 + read_2_bytes (abfd, bytes);
18326 break;
18327 case DW_FORM_block4:
18328 bytes += 4 + read_4_bytes (abfd, bytes);
18329 break;
18330
18331 case DW_FORM_sdata:
18332 case DW_FORM_udata:
18333 case DW_FORM_GNU_addr_index:
18334 case DW_FORM_GNU_str_index:
18335 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18336 if (bytes == NULL)
18337 {
18338 dwarf2_section_buffer_overflow_complaint (section);
18339 return NULL;
18340 }
18341 break;
18342
18343 default:
18344 {
18345 complain:
18346 complaint (&symfile_complaints,
18347 _("invalid form 0x%x in `%s'"),
18348 form,
18349 section->asection->name);
18350 return NULL;
18351 }
18352 }
18353
18354 return bytes;
18355 }
18356
18357 /* A helper for dwarf_decode_macros that handles skipping an unknown
18358 opcode. Returns an updated pointer to the macro data buffer; or,
18359 on error, issues a complaint and returns NULL. */
18360
18361 static gdb_byte *
18362 skip_unknown_opcode (unsigned int opcode,
18363 gdb_byte **opcode_definitions,
18364 gdb_byte *mac_ptr, gdb_byte *mac_end,
18365 bfd *abfd,
18366 unsigned int offset_size,
18367 struct dwarf2_section_info *section)
18368 {
18369 unsigned int bytes_read, i;
18370 unsigned long arg;
18371 gdb_byte *defn;
18372
18373 if (opcode_definitions[opcode] == NULL)
18374 {
18375 complaint (&symfile_complaints,
18376 _("unrecognized DW_MACFINO opcode 0x%x"),
18377 opcode);
18378 return NULL;
18379 }
18380
18381 defn = opcode_definitions[opcode];
18382 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18383 defn += bytes_read;
18384
18385 for (i = 0; i < arg; ++i)
18386 {
18387 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18388 section);
18389 if (mac_ptr == NULL)
18390 {
18391 /* skip_form_bytes already issued the complaint. */
18392 return NULL;
18393 }
18394 }
18395
18396 return mac_ptr;
18397 }
18398
18399 /* A helper function which parses the header of a macro section.
18400 If the macro section is the extended (for now called "GNU") type,
18401 then this updates *OFFSET_SIZE. Returns a pointer to just after
18402 the header, or issues a complaint and returns NULL on error. */
18403
18404 static gdb_byte *
18405 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18406 bfd *abfd,
18407 gdb_byte *mac_ptr,
18408 unsigned int *offset_size,
18409 int section_is_gnu)
18410 {
18411 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18412
18413 if (section_is_gnu)
18414 {
18415 unsigned int version, flags;
18416
18417 version = read_2_bytes (abfd, mac_ptr);
18418 if (version != 4)
18419 {
18420 complaint (&symfile_complaints,
18421 _("unrecognized version `%d' in .debug_macro section"),
18422 version);
18423 return NULL;
18424 }
18425 mac_ptr += 2;
18426
18427 flags = read_1_byte (abfd, mac_ptr);
18428 ++mac_ptr;
18429 *offset_size = (flags & 1) ? 8 : 4;
18430
18431 if ((flags & 2) != 0)
18432 /* We don't need the line table offset. */
18433 mac_ptr += *offset_size;
18434
18435 /* Vendor opcode descriptions. */
18436 if ((flags & 4) != 0)
18437 {
18438 unsigned int i, count;
18439
18440 count = read_1_byte (abfd, mac_ptr);
18441 ++mac_ptr;
18442 for (i = 0; i < count; ++i)
18443 {
18444 unsigned int opcode, bytes_read;
18445 unsigned long arg;
18446
18447 opcode = read_1_byte (abfd, mac_ptr);
18448 ++mac_ptr;
18449 opcode_definitions[opcode] = mac_ptr;
18450 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18451 mac_ptr += bytes_read;
18452 mac_ptr += arg;
18453 }
18454 }
18455 }
18456
18457 return mac_ptr;
18458 }
18459
18460 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18461 including DW_MACRO_GNU_transparent_include. */
18462
18463 static void
18464 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18465 struct macro_source_file *current_file,
18466 struct line_header *lh, const char *comp_dir,
18467 struct dwarf2_section_info *section,
18468 int section_is_gnu, int section_is_dwz,
18469 unsigned int offset_size,
18470 struct objfile *objfile,
18471 htab_t include_hash)
18472 {
18473 enum dwarf_macro_record_type macinfo_type;
18474 int at_commandline;
18475 gdb_byte *opcode_definitions[256];
18476
18477 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18478 &offset_size, section_is_gnu);
18479 if (mac_ptr == NULL)
18480 {
18481 /* We already issued a complaint. */
18482 return;
18483 }
18484
18485 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18486 GDB is still reading the definitions from command line. First
18487 DW_MACINFO_start_file will need to be ignored as it was already executed
18488 to create CURRENT_FILE for the main source holding also the command line
18489 definitions. On first met DW_MACINFO_start_file this flag is reset to
18490 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18491
18492 at_commandline = 1;
18493
18494 do
18495 {
18496 /* Do we at least have room for a macinfo type byte? */
18497 if (mac_ptr >= mac_end)
18498 {
18499 dwarf2_section_buffer_overflow_complaint (section);
18500 break;
18501 }
18502
18503 macinfo_type = read_1_byte (abfd, mac_ptr);
18504 mac_ptr++;
18505
18506 /* Note that we rely on the fact that the corresponding GNU and
18507 DWARF constants are the same. */
18508 switch (macinfo_type)
18509 {
18510 /* A zero macinfo type indicates the end of the macro
18511 information. */
18512 case 0:
18513 break;
18514
18515 case DW_MACRO_GNU_define:
18516 case DW_MACRO_GNU_undef:
18517 case DW_MACRO_GNU_define_indirect:
18518 case DW_MACRO_GNU_undef_indirect:
18519 case DW_MACRO_GNU_define_indirect_alt:
18520 case DW_MACRO_GNU_undef_indirect_alt:
18521 {
18522 unsigned int bytes_read;
18523 int line;
18524 char *body;
18525 int is_define;
18526
18527 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18528 mac_ptr += bytes_read;
18529
18530 if (macinfo_type == DW_MACRO_GNU_define
18531 || macinfo_type == DW_MACRO_GNU_undef)
18532 {
18533 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18534 mac_ptr += bytes_read;
18535 }
18536 else
18537 {
18538 LONGEST str_offset;
18539
18540 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18541 mac_ptr += offset_size;
18542
18543 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18544 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18545 || section_is_dwz)
18546 {
18547 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18548
18549 body = read_indirect_string_from_dwz (dwz, str_offset);
18550 }
18551 else
18552 body = read_indirect_string_at_offset (abfd, str_offset);
18553 }
18554
18555 is_define = (macinfo_type == DW_MACRO_GNU_define
18556 || macinfo_type == DW_MACRO_GNU_define_indirect
18557 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18558 if (! current_file)
18559 {
18560 /* DWARF violation as no main source is present. */
18561 complaint (&symfile_complaints,
18562 _("debug info with no main source gives macro %s "
18563 "on line %d: %s"),
18564 is_define ? _("definition") : _("undefinition"),
18565 line, body);
18566 break;
18567 }
18568 if ((line == 0 && !at_commandline)
18569 || (line != 0 && at_commandline))
18570 complaint (&symfile_complaints,
18571 _("debug info gives %s macro %s with %s line %d: %s"),
18572 at_commandline ? _("command-line") : _("in-file"),
18573 is_define ? _("definition") : _("undefinition"),
18574 line == 0 ? _("zero") : _("non-zero"), line, body);
18575
18576 if (is_define)
18577 parse_macro_definition (current_file, line, body);
18578 else
18579 {
18580 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18581 || macinfo_type == DW_MACRO_GNU_undef_indirect
18582 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18583 macro_undef (current_file, line, body);
18584 }
18585 }
18586 break;
18587
18588 case DW_MACRO_GNU_start_file:
18589 {
18590 unsigned int bytes_read;
18591 int line, file;
18592
18593 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18594 mac_ptr += bytes_read;
18595 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18596 mac_ptr += bytes_read;
18597
18598 if ((line == 0 && !at_commandline)
18599 || (line != 0 && at_commandline))
18600 complaint (&symfile_complaints,
18601 _("debug info gives source %d included "
18602 "from %s at %s line %d"),
18603 file, at_commandline ? _("command-line") : _("file"),
18604 line == 0 ? _("zero") : _("non-zero"), line);
18605
18606 if (at_commandline)
18607 {
18608 /* This DW_MACRO_GNU_start_file was executed in the
18609 pass one. */
18610 at_commandline = 0;
18611 }
18612 else
18613 current_file = macro_start_file (file, line,
18614 current_file, comp_dir,
18615 lh, objfile);
18616 }
18617 break;
18618
18619 case DW_MACRO_GNU_end_file:
18620 if (! current_file)
18621 complaint (&symfile_complaints,
18622 _("macro debug info has an unmatched "
18623 "`close_file' directive"));
18624 else
18625 {
18626 current_file = current_file->included_by;
18627 if (! current_file)
18628 {
18629 enum dwarf_macro_record_type next_type;
18630
18631 /* GCC circa March 2002 doesn't produce the zero
18632 type byte marking the end of the compilation
18633 unit. Complain if it's not there, but exit no
18634 matter what. */
18635
18636 /* Do we at least have room for a macinfo type byte? */
18637 if (mac_ptr >= mac_end)
18638 {
18639 dwarf2_section_buffer_overflow_complaint (section);
18640 return;
18641 }
18642
18643 /* We don't increment mac_ptr here, so this is just
18644 a look-ahead. */
18645 next_type = read_1_byte (abfd, mac_ptr);
18646 if (next_type != 0)
18647 complaint (&symfile_complaints,
18648 _("no terminating 0-type entry for "
18649 "macros in `.debug_macinfo' section"));
18650
18651 return;
18652 }
18653 }
18654 break;
18655
18656 case DW_MACRO_GNU_transparent_include:
18657 case DW_MACRO_GNU_transparent_include_alt:
18658 {
18659 LONGEST offset;
18660 void **slot;
18661 bfd *include_bfd = abfd;
18662 struct dwarf2_section_info *include_section = section;
18663 struct dwarf2_section_info alt_section;
18664 gdb_byte *include_mac_end = mac_end;
18665 int is_dwz = section_is_dwz;
18666 gdb_byte *new_mac_ptr;
18667
18668 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18669 mac_ptr += offset_size;
18670
18671 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18672 {
18673 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18674
18675 dwarf2_read_section (dwarf2_per_objfile->objfile,
18676 &dwz->macro);
18677
18678 include_bfd = dwz->macro.asection->owner;
18679 include_section = &dwz->macro;
18680 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18681 is_dwz = 1;
18682 }
18683
18684 new_mac_ptr = include_section->buffer + offset;
18685 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18686
18687 if (*slot != NULL)
18688 {
18689 /* This has actually happened; see
18690 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18691 complaint (&symfile_complaints,
18692 _("recursive DW_MACRO_GNU_transparent_include in "
18693 ".debug_macro section"));
18694 }
18695 else
18696 {
18697 *slot = new_mac_ptr;
18698
18699 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18700 include_mac_end, current_file,
18701 lh, comp_dir,
18702 section, section_is_gnu, is_dwz,
18703 offset_size, objfile, include_hash);
18704
18705 htab_remove_elt (include_hash, new_mac_ptr);
18706 }
18707 }
18708 break;
18709
18710 case DW_MACINFO_vendor_ext:
18711 if (!section_is_gnu)
18712 {
18713 unsigned int bytes_read;
18714 int constant;
18715
18716 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18717 mac_ptr += bytes_read;
18718 read_direct_string (abfd, mac_ptr, &bytes_read);
18719 mac_ptr += bytes_read;
18720
18721 /* We don't recognize any vendor extensions. */
18722 break;
18723 }
18724 /* FALLTHROUGH */
18725
18726 default:
18727 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18728 mac_ptr, mac_end, abfd, offset_size,
18729 section);
18730 if (mac_ptr == NULL)
18731 return;
18732 break;
18733 }
18734 } while (macinfo_type != 0);
18735 }
18736
18737 static void
18738 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18739 const char *comp_dir, int section_is_gnu)
18740 {
18741 struct objfile *objfile = dwarf2_per_objfile->objfile;
18742 struct line_header *lh = cu->line_header;
18743 bfd *abfd;
18744 gdb_byte *mac_ptr, *mac_end;
18745 struct macro_source_file *current_file = 0;
18746 enum dwarf_macro_record_type macinfo_type;
18747 unsigned int offset_size = cu->header.offset_size;
18748 gdb_byte *opcode_definitions[256];
18749 struct cleanup *cleanup;
18750 htab_t include_hash;
18751 void **slot;
18752 struct dwarf2_section_info *section;
18753 const char *section_name;
18754
18755 if (cu->dwo_unit != NULL)
18756 {
18757 if (section_is_gnu)
18758 {
18759 section = &cu->dwo_unit->dwo_file->sections.macro;
18760 section_name = ".debug_macro.dwo";
18761 }
18762 else
18763 {
18764 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18765 section_name = ".debug_macinfo.dwo";
18766 }
18767 }
18768 else
18769 {
18770 if (section_is_gnu)
18771 {
18772 section = &dwarf2_per_objfile->macro;
18773 section_name = ".debug_macro";
18774 }
18775 else
18776 {
18777 section = &dwarf2_per_objfile->macinfo;
18778 section_name = ".debug_macinfo";
18779 }
18780 }
18781
18782 dwarf2_read_section (objfile, section);
18783 if (section->buffer == NULL)
18784 {
18785 complaint (&symfile_complaints, _("missing %s section"), section_name);
18786 return;
18787 }
18788 abfd = section->asection->owner;
18789
18790 /* First pass: Find the name of the base filename.
18791 This filename is needed in order to process all macros whose definition
18792 (or undefinition) comes from the command line. These macros are defined
18793 before the first DW_MACINFO_start_file entry, and yet still need to be
18794 associated to the base file.
18795
18796 To determine the base file name, we scan the macro definitions until we
18797 reach the first DW_MACINFO_start_file entry. We then initialize
18798 CURRENT_FILE accordingly so that any macro definition found before the
18799 first DW_MACINFO_start_file can still be associated to the base file. */
18800
18801 mac_ptr = section->buffer + offset;
18802 mac_end = section->buffer + section->size;
18803
18804 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18805 &offset_size, section_is_gnu);
18806 if (mac_ptr == NULL)
18807 {
18808 /* We already issued a complaint. */
18809 return;
18810 }
18811
18812 do
18813 {
18814 /* Do we at least have room for a macinfo type byte? */
18815 if (mac_ptr >= mac_end)
18816 {
18817 /* Complaint is printed during the second pass as GDB will probably
18818 stop the first pass earlier upon finding
18819 DW_MACINFO_start_file. */
18820 break;
18821 }
18822
18823 macinfo_type = read_1_byte (abfd, mac_ptr);
18824 mac_ptr++;
18825
18826 /* Note that we rely on the fact that the corresponding GNU and
18827 DWARF constants are the same. */
18828 switch (macinfo_type)
18829 {
18830 /* A zero macinfo type indicates the end of the macro
18831 information. */
18832 case 0:
18833 break;
18834
18835 case DW_MACRO_GNU_define:
18836 case DW_MACRO_GNU_undef:
18837 /* Only skip the data by MAC_PTR. */
18838 {
18839 unsigned int bytes_read;
18840
18841 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18842 mac_ptr += bytes_read;
18843 read_direct_string (abfd, mac_ptr, &bytes_read);
18844 mac_ptr += bytes_read;
18845 }
18846 break;
18847
18848 case DW_MACRO_GNU_start_file:
18849 {
18850 unsigned int bytes_read;
18851 int line, file;
18852
18853 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18854 mac_ptr += bytes_read;
18855 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18856 mac_ptr += bytes_read;
18857
18858 current_file = macro_start_file (file, line, current_file,
18859 comp_dir, lh, objfile);
18860 }
18861 break;
18862
18863 case DW_MACRO_GNU_end_file:
18864 /* No data to skip by MAC_PTR. */
18865 break;
18866
18867 case DW_MACRO_GNU_define_indirect:
18868 case DW_MACRO_GNU_undef_indirect:
18869 case DW_MACRO_GNU_define_indirect_alt:
18870 case DW_MACRO_GNU_undef_indirect_alt:
18871 {
18872 unsigned int bytes_read;
18873
18874 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18875 mac_ptr += bytes_read;
18876 mac_ptr += offset_size;
18877 }
18878 break;
18879
18880 case DW_MACRO_GNU_transparent_include:
18881 case DW_MACRO_GNU_transparent_include_alt:
18882 /* Note that, according to the spec, a transparent include
18883 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18884 skip this opcode. */
18885 mac_ptr += offset_size;
18886 break;
18887
18888 case DW_MACINFO_vendor_ext:
18889 /* Only skip the data by MAC_PTR. */
18890 if (!section_is_gnu)
18891 {
18892 unsigned int bytes_read;
18893
18894 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18895 mac_ptr += bytes_read;
18896 read_direct_string (abfd, mac_ptr, &bytes_read);
18897 mac_ptr += bytes_read;
18898 }
18899 /* FALLTHROUGH */
18900
18901 default:
18902 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18903 mac_ptr, mac_end, abfd, offset_size,
18904 section);
18905 if (mac_ptr == NULL)
18906 return;
18907 break;
18908 }
18909 } while (macinfo_type != 0 && current_file == NULL);
18910
18911 /* Second pass: Process all entries.
18912
18913 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18914 command-line macro definitions/undefinitions. This flag is unset when we
18915 reach the first DW_MACINFO_start_file entry. */
18916
18917 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18918 NULL, xcalloc, xfree);
18919 cleanup = make_cleanup_htab_delete (include_hash);
18920 mac_ptr = section->buffer + offset;
18921 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18922 *slot = mac_ptr;
18923 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18924 current_file, lh, comp_dir, section,
18925 section_is_gnu, 0,
18926 offset_size, objfile, include_hash);
18927 do_cleanups (cleanup);
18928 }
18929
18930 /* Check if the attribute's form is a DW_FORM_block*
18931 if so return true else false. */
18932
18933 static int
18934 attr_form_is_block (struct attribute *attr)
18935 {
18936 return (attr == NULL ? 0 :
18937 attr->form == DW_FORM_block1
18938 || attr->form == DW_FORM_block2
18939 || attr->form == DW_FORM_block4
18940 || attr->form == DW_FORM_block
18941 || attr->form == DW_FORM_exprloc);
18942 }
18943
18944 /* Return non-zero if ATTR's value is a section offset --- classes
18945 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18946 You may use DW_UNSND (attr) to retrieve such offsets.
18947
18948 Section 7.5.4, "Attribute Encodings", explains that no attribute
18949 may have a value that belongs to more than one of these classes; it
18950 would be ambiguous if we did, because we use the same forms for all
18951 of them. */
18952
18953 static int
18954 attr_form_is_section_offset (struct attribute *attr)
18955 {
18956 return (attr->form == DW_FORM_data4
18957 || attr->form == DW_FORM_data8
18958 || attr->form == DW_FORM_sec_offset);
18959 }
18960
18961 /* Return non-zero if ATTR's value falls in the 'constant' class, or
18962 zero otherwise. When this function returns true, you can apply
18963 dwarf2_get_attr_constant_value to it.
18964
18965 However, note that for some attributes you must check
18966 attr_form_is_section_offset before using this test. DW_FORM_data4
18967 and DW_FORM_data8 are members of both the constant class, and of
18968 the classes that contain offsets into other debug sections
18969 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18970 that, if an attribute's can be either a constant or one of the
18971 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18972 taken as section offsets, not constants. */
18973
18974 static int
18975 attr_form_is_constant (struct attribute *attr)
18976 {
18977 switch (attr->form)
18978 {
18979 case DW_FORM_sdata:
18980 case DW_FORM_udata:
18981 case DW_FORM_data1:
18982 case DW_FORM_data2:
18983 case DW_FORM_data4:
18984 case DW_FORM_data8:
18985 return 1;
18986 default:
18987 return 0;
18988 }
18989 }
18990
18991 /* Return the .debug_loc section to use for CU.
18992 For DWO files use .debug_loc.dwo. */
18993
18994 static struct dwarf2_section_info *
18995 cu_debug_loc_section (struct dwarf2_cu *cu)
18996 {
18997 if (cu->dwo_unit)
18998 return &cu->dwo_unit->dwo_file->sections.loc;
18999 return &dwarf2_per_objfile->loc;
19000 }
19001
19002 /* A helper function that fills in a dwarf2_loclist_baton. */
19003
19004 static void
19005 fill_in_loclist_baton (struct dwarf2_cu *cu,
19006 struct dwarf2_loclist_baton *baton,
19007 struct attribute *attr)
19008 {
19009 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19010
19011 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19012
19013 baton->per_cu = cu->per_cu;
19014 gdb_assert (baton->per_cu);
19015 /* We don't know how long the location list is, but make sure we
19016 don't run off the edge of the section. */
19017 baton->size = section->size - DW_UNSND (attr);
19018 baton->data = section->buffer + DW_UNSND (attr);
19019 baton->base_address = cu->base_address;
19020 baton->from_dwo = cu->dwo_unit != NULL;
19021 }
19022
19023 static void
19024 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19025 struct dwarf2_cu *cu)
19026 {
19027 struct objfile *objfile = dwarf2_per_objfile->objfile;
19028 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19029
19030 if (attr_form_is_section_offset (attr)
19031 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19032 the section. If so, fall through to the complaint in the
19033 other branch. */
19034 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19035 {
19036 struct dwarf2_loclist_baton *baton;
19037
19038 baton = obstack_alloc (&objfile->objfile_obstack,
19039 sizeof (struct dwarf2_loclist_baton));
19040
19041 fill_in_loclist_baton (cu, baton, attr);
19042
19043 if (cu->base_known == 0)
19044 complaint (&symfile_complaints,
19045 _("Location list used without "
19046 "specifying the CU base address."));
19047
19048 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
19049 SYMBOL_LOCATION_BATON (sym) = baton;
19050 }
19051 else
19052 {
19053 struct dwarf2_locexpr_baton *baton;
19054
19055 baton = obstack_alloc (&objfile->objfile_obstack,
19056 sizeof (struct dwarf2_locexpr_baton));
19057 baton->per_cu = cu->per_cu;
19058 gdb_assert (baton->per_cu);
19059
19060 if (attr_form_is_block (attr))
19061 {
19062 /* Note that we're just copying the block's data pointer
19063 here, not the actual data. We're still pointing into the
19064 info_buffer for SYM's objfile; right now we never release
19065 that buffer, but when we do clean up properly this may
19066 need to change. */
19067 baton->size = DW_BLOCK (attr)->size;
19068 baton->data = DW_BLOCK (attr)->data;
19069 }
19070 else
19071 {
19072 dwarf2_invalid_attrib_class_complaint ("location description",
19073 SYMBOL_NATURAL_NAME (sym));
19074 baton->size = 0;
19075 }
19076
19077 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
19078 SYMBOL_LOCATION_BATON (sym) = baton;
19079 }
19080 }
19081
19082 /* Return the OBJFILE associated with the compilation unit CU. If CU
19083 came from a separate debuginfo file, then the master objfile is
19084 returned. */
19085
19086 struct objfile *
19087 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19088 {
19089 struct objfile *objfile = per_cu->objfile;
19090
19091 /* Return the master objfile, so that we can report and look up the
19092 correct file containing this variable. */
19093 if (objfile->separate_debug_objfile_backlink)
19094 objfile = objfile->separate_debug_objfile_backlink;
19095
19096 return objfile;
19097 }
19098
19099 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19100 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19101 CU_HEADERP first. */
19102
19103 static const struct comp_unit_head *
19104 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19105 struct dwarf2_per_cu_data *per_cu)
19106 {
19107 gdb_byte *info_ptr;
19108
19109 if (per_cu->cu)
19110 return &per_cu->cu->header;
19111
19112 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
19113
19114 memset (cu_headerp, 0, sizeof (*cu_headerp));
19115 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19116
19117 return cu_headerp;
19118 }
19119
19120 /* Return the address size given in the compilation unit header for CU. */
19121
19122 int
19123 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19124 {
19125 struct comp_unit_head cu_header_local;
19126 const struct comp_unit_head *cu_headerp;
19127
19128 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19129
19130 return cu_headerp->addr_size;
19131 }
19132
19133 /* Return the offset size given in the compilation unit header for CU. */
19134
19135 int
19136 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19137 {
19138 struct comp_unit_head cu_header_local;
19139 const struct comp_unit_head *cu_headerp;
19140
19141 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19142
19143 return cu_headerp->offset_size;
19144 }
19145
19146 /* See its dwarf2loc.h declaration. */
19147
19148 int
19149 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19150 {
19151 struct comp_unit_head cu_header_local;
19152 const struct comp_unit_head *cu_headerp;
19153
19154 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19155
19156 if (cu_headerp->version == 2)
19157 return cu_headerp->addr_size;
19158 else
19159 return cu_headerp->offset_size;
19160 }
19161
19162 /* Return the text offset of the CU. The returned offset comes from
19163 this CU's objfile. If this objfile came from a separate debuginfo
19164 file, then the offset may be different from the corresponding
19165 offset in the parent objfile. */
19166
19167 CORE_ADDR
19168 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19169 {
19170 struct objfile *objfile = per_cu->objfile;
19171
19172 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19173 }
19174
19175 /* Locate the .debug_info compilation unit from CU's objfile which contains
19176 the DIE at OFFSET. Raises an error on failure. */
19177
19178 static struct dwarf2_per_cu_data *
19179 dwarf2_find_containing_comp_unit (sect_offset offset,
19180 unsigned int offset_in_dwz,
19181 struct objfile *objfile)
19182 {
19183 struct dwarf2_per_cu_data *this_cu;
19184 int low, high;
19185 const sect_offset *cu_off;
19186
19187 low = 0;
19188 high = dwarf2_per_objfile->n_comp_units - 1;
19189 while (high > low)
19190 {
19191 struct dwarf2_per_cu_data *mid_cu;
19192 int mid = low + (high - low) / 2;
19193
19194 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19195 cu_off = &mid_cu->offset;
19196 if (mid_cu->is_dwz > offset_in_dwz
19197 || (mid_cu->is_dwz == offset_in_dwz
19198 && cu_off->sect_off >= offset.sect_off))
19199 high = mid;
19200 else
19201 low = mid + 1;
19202 }
19203 gdb_assert (low == high);
19204 this_cu = dwarf2_per_objfile->all_comp_units[low];
19205 cu_off = &this_cu->offset;
19206 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19207 {
19208 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19209 error (_("Dwarf Error: could not find partial DIE containing "
19210 "offset 0x%lx [in module %s]"),
19211 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19212
19213 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19214 <= offset.sect_off);
19215 return dwarf2_per_objfile->all_comp_units[low-1];
19216 }
19217 else
19218 {
19219 this_cu = dwarf2_per_objfile->all_comp_units[low];
19220 if (low == dwarf2_per_objfile->n_comp_units - 1
19221 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19222 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19223 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19224 return this_cu;
19225 }
19226 }
19227
19228 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19229
19230 static void
19231 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19232 {
19233 memset (cu, 0, sizeof (*cu));
19234 per_cu->cu = cu;
19235 cu->per_cu = per_cu;
19236 cu->objfile = per_cu->objfile;
19237 obstack_init (&cu->comp_unit_obstack);
19238 }
19239
19240 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19241
19242 static void
19243 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19244 enum language pretend_language)
19245 {
19246 struct attribute *attr;
19247
19248 /* Set the language we're debugging. */
19249 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19250 if (attr)
19251 set_cu_language (DW_UNSND (attr), cu);
19252 else
19253 {
19254 cu->language = pretend_language;
19255 cu->language_defn = language_def (cu->language);
19256 }
19257
19258 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19259 if (attr)
19260 cu->producer = DW_STRING (attr);
19261 }
19262
19263 /* Release one cached compilation unit, CU. We unlink it from the tree
19264 of compilation units, but we don't remove it from the read_in_chain;
19265 the caller is responsible for that.
19266 NOTE: DATA is a void * because this function is also used as a
19267 cleanup routine. */
19268
19269 static void
19270 free_heap_comp_unit (void *data)
19271 {
19272 struct dwarf2_cu *cu = data;
19273
19274 gdb_assert (cu->per_cu != NULL);
19275 cu->per_cu->cu = NULL;
19276 cu->per_cu = NULL;
19277
19278 obstack_free (&cu->comp_unit_obstack, NULL);
19279
19280 xfree (cu);
19281 }
19282
19283 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19284 when we're finished with it. We can't free the pointer itself, but be
19285 sure to unlink it from the cache. Also release any associated storage. */
19286
19287 static void
19288 free_stack_comp_unit (void *data)
19289 {
19290 struct dwarf2_cu *cu = data;
19291
19292 gdb_assert (cu->per_cu != NULL);
19293 cu->per_cu->cu = NULL;
19294 cu->per_cu = NULL;
19295
19296 obstack_free (&cu->comp_unit_obstack, NULL);
19297 cu->partial_dies = NULL;
19298 }
19299
19300 /* Free all cached compilation units. */
19301
19302 static void
19303 free_cached_comp_units (void *data)
19304 {
19305 struct dwarf2_per_cu_data *per_cu, **last_chain;
19306
19307 per_cu = dwarf2_per_objfile->read_in_chain;
19308 last_chain = &dwarf2_per_objfile->read_in_chain;
19309 while (per_cu != NULL)
19310 {
19311 struct dwarf2_per_cu_data *next_cu;
19312
19313 next_cu = per_cu->cu->read_in_chain;
19314
19315 free_heap_comp_unit (per_cu->cu);
19316 *last_chain = next_cu;
19317
19318 per_cu = next_cu;
19319 }
19320 }
19321
19322 /* Increase the age counter on each cached compilation unit, and free
19323 any that are too old. */
19324
19325 static void
19326 age_cached_comp_units (void)
19327 {
19328 struct dwarf2_per_cu_data *per_cu, **last_chain;
19329
19330 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19331 per_cu = dwarf2_per_objfile->read_in_chain;
19332 while (per_cu != NULL)
19333 {
19334 per_cu->cu->last_used ++;
19335 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19336 dwarf2_mark (per_cu->cu);
19337 per_cu = per_cu->cu->read_in_chain;
19338 }
19339
19340 per_cu = dwarf2_per_objfile->read_in_chain;
19341 last_chain = &dwarf2_per_objfile->read_in_chain;
19342 while (per_cu != NULL)
19343 {
19344 struct dwarf2_per_cu_data *next_cu;
19345
19346 next_cu = per_cu->cu->read_in_chain;
19347
19348 if (!per_cu->cu->mark)
19349 {
19350 free_heap_comp_unit (per_cu->cu);
19351 *last_chain = next_cu;
19352 }
19353 else
19354 last_chain = &per_cu->cu->read_in_chain;
19355
19356 per_cu = next_cu;
19357 }
19358 }
19359
19360 /* Remove a single compilation unit from the cache. */
19361
19362 static void
19363 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19364 {
19365 struct dwarf2_per_cu_data *per_cu, **last_chain;
19366
19367 per_cu = dwarf2_per_objfile->read_in_chain;
19368 last_chain = &dwarf2_per_objfile->read_in_chain;
19369 while (per_cu != NULL)
19370 {
19371 struct dwarf2_per_cu_data *next_cu;
19372
19373 next_cu = per_cu->cu->read_in_chain;
19374
19375 if (per_cu == target_per_cu)
19376 {
19377 free_heap_comp_unit (per_cu->cu);
19378 per_cu->cu = NULL;
19379 *last_chain = next_cu;
19380 break;
19381 }
19382 else
19383 last_chain = &per_cu->cu->read_in_chain;
19384
19385 per_cu = next_cu;
19386 }
19387 }
19388
19389 /* Release all extra memory associated with OBJFILE. */
19390
19391 void
19392 dwarf2_free_objfile (struct objfile *objfile)
19393 {
19394 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19395
19396 if (dwarf2_per_objfile == NULL)
19397 return;
19398
19399 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19400 free_cached_comp_units (NULL);
19401
19402 if (dwarf2_per_objfile->quick_file_names_table)
19403 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19404
19405 /* Everything else should be on the objfile obstack. */
19406 }
19407
19408 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19409 We store these in a hash table separate from the DIEs, and preserve them
19410 when the DIEs are flushed out of cache.
19411
19412 The CU "per_cu" pointer is needed because offset alone is not enough to
19413 uniquely identify the type. A file may have multiple .debug_types sections,
19414 or the type may come from a DWO file. We have to use something in
19415 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19416 routine, get_die_type_at_offset, from outside this file, and thus won't
19417 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19418 of the objfile. */
19419
19420 struct dwarf2_per_cu_offset_and_type
19421 {
19422 const struct dwarf2_per_cu_data *per_cu;
19423 sect_offset offset;
19424 struct type *type;
19425 };
19426
19427 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19428
19429 static hashval_t
19430 per_cu_offset_and_type_hash (const void *item)
19431 {
19432 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19433
19434 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19435 }
19436
19437 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19438
19439 static int
19440 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19441 {
19442 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19443 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19444
19445 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19446 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19447 }
19448
19449 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19450 table if necessary. For convenience, return TYPE.
19451
19452 The DIEs reading must have careful ordering to:
19453 * Not cause infite loops trying to read in DIEs as a prerequisite for
19454 reading current DIE.
19455 * Not trying to dereference contents of still incompletely read in types
19456 while reading in other DIEs.
19457 * Enable referencing still incompletely read in types just by a pointer to
19458 the type without accessing its fields.
19459
19460 Therefore caller should follow these rules:
19461 * Try to fetch any prerequisite types we may need to build this DIE type
19462 before building the type and calling set_die_type.
19463 * After building type call set_die_type for current DIE as soon as
19464 possible before fetching more types to complete the current type.
19465 * Make the type as complete as possible before fetching more types. */
19466
19467 static struct type *
19468 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19469 {
19470 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19471 struct objfile *objfile = cu->objfile;
19472
19473 /* For Ada types, make sure that the gnat-specific data is always
19474 initialized (if not already set). There are a few types where
19475 we should not be doing so, because the type-specific area is
19476 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19477 where the type-specific area is used to store the floatformat).
19478 But this is not a problem, because the gnat-specific information
19479 is actually not needed for these types. */
19480 if (need_gnat_info (cu)
19481 && TYPE_CODE (type) != TYPE_CODE_FUNC
19482 && TYPE_CODE (type) != TYPE_CODE_FLT
19483 && !HAVE_GNAT_AUX_INFO (type))
19484 INIT_GNAT_SPECIFIC (type);
19485
19486 if (dwarf2_per_objfile->die_type_hash == NULL)
19487 {
19488 dwarf2_per_objfile->die_type_hash =
19489 htab_create_alloc_ex (127,
19490 per_cu_offset_and_type_hash,
19491 per_cu_offset_and_type_eq,
19492 NULL,
19493 &objfile->objfile_obstack,
19494 hashtab_obstack_allocate,
19495 dummy_obstack_deallocate);
19496 }
19497
19498 ofs.per_cu = cu->per_cu;
19499 ofs.offset = die->offset;
19500 ofs.type = type;
19501 slot = (struct dwarf2_per_cu_offset_and_type **)
19502 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19503 if (*slot)
19504 complaint (&symfile_complaints,
19505 _("A problem internal to GDB: DIE 0x%x has type already set"),
19506 die->offset.sect_off);
19507 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19508 **slot = ofs;
19509 return type;
19510 }
19511
19512 /* Look up the type for the die at OFFSET in the appropriate type_hash
19513 table, or return NULL if the die does not have a saved type. */
19514
19515 static struct type *
19516 get_die_type_at_offset (sect_offset offset,
19517 struct dwarf2_per_cu_data *per_cu)
19518 {
19519 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19520
19521 if (dwarf2_per_objfile->die_type_hash == NULL)
19522 return NULL;
19523
19524 ofs.per_cu = per_cu;
19525 ofs.offset = offset;
19526 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19527 if (slot)
19528 return slot->type;
19529 else
19530 return NULL;
19531 }
19532
19533 /* Look up the type for DIE in the appropriate type_hash table,
19534 or return NULL if DIE does not have a saved type. */
19535
19536 static struct type *
19537 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19538 {
19539 return get_die_type_at_offset (die->offset, cu->per_cu);
19540 }
19541
19542 /* Add a dependence relationship from CU to REF_PER_CU. */
19543
19544 static void
19545 dwarf2_add_dependence (struct dwarf2_cu *cu,
19546 struct dwarf2_per_cu_data *ref_per_cu)
19547 {
19548 void **slot;
19549
19550 if (cu->dependencies == NULL)
19551 cu->dependencies
19552 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19553 NULL, &cu->comp_unit_obstack,
19554 hashtab_obstack_allocate,
19555 dummy_obstack_deallocate);
19556
19557 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19558 if (*slot == NULL)
19559 *slot = ref_per_cu;
19560 }
19561
19562 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19563 Set the mark field in every compilation unit in the
19564 cache that we must keep because we are keeping CU. */
19565
19566 static int
19567 dwarf2_mark_helper (void **slot, void *data)
19568 {
19569 struct dwarf2_per_cu_data *per_cu;
19570
19571 per_cu = (struct dwarf2_per_cu_data *) *slot;
19572
19573 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19574 reading of the chain. As such dependencies remain valid it is not much
19575 useful to track and undo them during QUIT cleanups. */
19576 if (per_cu->cu == NULL)
19577 return 1;
19578
19579 if (per_cu->cu->mark)
19580 return 1;
19581 per_cu->cu->mark = 1;
19582
19583 if (per_cu->cu->dependencies != NULL)
19584 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19585
19586 return 1;
19587 }
19588
19589 /* Set the mark field in CU and in every other compilation unit in the
19590 cache that we must keep because we are keeping CU. */
19591
19592 static void
19593 dwarf2_mark (struct dwarf2_cu *cu)
19594 {
19595 if (cu->mark)
19596 return;
19597 cu->mark = 1;
19598 if (cu->dependencies != NULL)
19599 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19600 }
19601
19602 static void
19603 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19604 {
19605 while (per_cu)
19606 {
19607 per_cu->cu->mark = 0;
19608 per_cu = per_cu->cu->read_in_chain;
19609 }
19610 }
19611
19612 /* Trivial hash function for partial_die_info: the hash value of a DIE
19613 is its offset in .debug_info for this objfile. */
19614
19615 static hashval_t
19616 partial_die_hash (const void *item)
19617 {
19618 const struct partial_die_info *part_die = item;
19619
19620 return part_die->offset.sect_off;
19621 }
19622
19623 /* Trivial comparison function for partial_die_info structures: two DIEs
19624 are equal if they have the same offset. */
19625
19626 static int
19627 partial_die_eq (const void *item_lhs, const void *item_rhs)
19628 {
19629 const struct partial_die_info *part_die_lhs = item_lhs;
19630 const struct partial_die_info *part_die_rhs = item_rhs;
19631
19632 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19633 }
19634
19635 static struct cmd_list_element *set_dwarf2_cmdlist;
19636 static struct cmd_list_element *show_dwarf2_cmdlist;
19637
19638 static void
19639 set_dwarf2_cmd (char *args, int from_tty)
19640 {
19641 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19642 }
19643
19644 static void
19645 show_dwarf2_cmd (char *args, int from_tty)
19646 {
19647 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19648 }
19649
19650 /* Free data associated with OBJFILE, if necessary. */
19651
19652 static void
19653 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19654 {
19655 struct dwarf2_per_objfile *data = d;
19656 int ix;
19657
19658 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19659 VEC_free (dwarf2_per_cu_ptr,
19660 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19661
19662 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19663 VEC_free (dwarf2_per_cu_ptr,
19664 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19665
19666 VEC_free (dwarf2_section_info_def, data->types);
19667
19668 if (data->dwo_files)
19669 free_dwo_files (data->dwo_files, objfile);
19670
19671 if (data->dwz_file && data->dwz_file->dwz_bfd)
19672 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19673 }
19674
19675 \f
19676 /* The "save gdb-index" command. */
19677
19678 /* The contents of the hash table we create when building the string
19679 table. */
19680 struct strtab_entry
19681 {
19682 offset_type offset;
19683 const char *str;
19684 };
19685
19686 /* Hash function for a strtab_entry.
19687
19688 Function is used only during write_hash_table so no index format backward
19689 compatibility is needed. */
19690
19691 static hashval_t
19692 hash_strtab_entry (const void *e)
19693 {
19694 const struct strtab_entry *entry = e;
19695 return mapped_index_string_hash (INT_MAX, entry->str);
19696 }
19697
19698 /* Equality function for a strtab_entry. */
19699
19700 static int
19701 eq_strtab_entry (const void *a, const void *b)
19702 {
19703 const struct strtab_entry *ea = a;
19704 const struct strtab_entry *eb = b;
19705 return !strcmp (ea->str, eb->str);
19706 }
19707
19708 /* Create a strtab_entry hash table. */
19709
19710 static htab_t
19711 create_strtab (void)
19712 {
19713 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19714 xfree, xcalloc, xfree);
19715 }
19716
19717 /* Add a string to the constant pool. Return the string's offset in
19718 host order. */
19719
19720 static offset_type
19721 add_string (htab_t table, struct obstack *cpool, const char *str)
19722 {
19723 void **slot;
19724 struct strtab_entry entry;
19725 struct strtab_entry *result;
19726
19727 entry.str = str;
19728 slot = htab_find_slot (table, &entry, INSERT);
19729 if (*slot)
19730 result = *slot;
19731 else
19732 {
19733 result = XNEW (struct strtab_entry);
19734 result->offset = obstack_object_size (cpool);
19735 result->str = str;
19736 obstack_grow_str0 (cpool, str);
19737 *slot = result;
19738 }
19739 return result->offset;
19740 }
19741
19742 /* An entry in the symbol table. */
19743 struct symtab_index_entry
19744 {
19745 /* The name of the symbol. */
19746 const char *name;
19747 /* The offset of the name in the constant pool. */
19748 offset_type index_offset;
19749 /* A sorted vector of the indices of all the CUs that hold an object
19750 of this name. */
19751 VEC (offset_type) *cu_indices;
19752 };
19753
19754 /* The symbol table. This is a power-of-2-sized hash table. */
19755 struct mapped_symtab
19756 {
19757 offset_type n_elements;
19758 offset_type size;
19759 struct symtab_index_entry **data;
19760 };
19761
19762 /* Hash function for a symtab_index_entry. */
19763
19764 static hashval_t
19765 hash_symtab_entry (const void *e)
19766 {
19767 const struct symtab_index_entry *entry = e;
19768 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19769 sizeof (offset_type) * VEC_length (offset_type,
19770 entry->cu_indices),
19771 0);
19772 }
19773
19774 /* Equality function for a symtab_index_entry. */
19775
19776 static int
19777 eq_symtab_entry (const void *a, const void *b)
19778 {
19779 const struct symtab_index_entry *ea = a;
19780 const struct symtab_index_entry *eb = b;
19781 int len = VEC_length (offset_type, ea->cu_indices);
19782 if (len != VEC_length (offset_type, eb->cu_indices))
19783 return 0;
19784 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19785 VEC_address (offset_type, eb->cu_indices),
19786 sizeof (offset_type) * len);
19787 }
19788
19789 /* Destroy a symtab_index_entry. */
19790
19791 static void
19792 delete_symtab_entry (void *p)
19793 {
19794 struct symtab_index_entry *entry = p;
19795 VEC_free (offset_type, entry->cu_indices);
19796 xfree (entry);
19797 }
19798
19799 /* Create a hash table holding symtab_index_entry objects. */
19800
19801 static htab_t
19802 create_symbol_hash_table (void)
19803 {
19804 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19805 delete_symtab_entry, xcalloc, xfree);
19806 }
19807
19808 /* Create a new mapped symtab object. */
19809
19810 static struct mapped_symtab *
19811 create_mapped_symtab (void)
19812 {
19813 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19814 symtab->n_elements = 0;
19815 symtab->size = 1024;
19816 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19817 return symtab;
19818 }
19819
19820 /* Destroy a mapped_symtab. */
19821
19822 static void
19823 cleanup_mapped_symtab (void *p)
19824 {
19825 struct mapped_symtab *symtab = p;
19826 /* The contents of the array are freed when the other hash table is
19827 destroyed. */
19828 xfree (symtab->data);
19829 xfree (symtab);
19830 }
19831
19832 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19833 the slot.
19834
19835 Function is used only during write_hash_table so no index format backward
19836 compatibility is needed. */
19837
19838 static struct symtab_index_entry **
19839 find_slot (struct mapped_symtab *symtab, const char *name)
19840 {
19841 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19842
19843 index = hash & (symtab->size - 1);
19844 step = ((hash * 17) & (symtab->size - 1)) | 1;
19845
19846 for (;;)
19847 {
19848 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19849 return &symtab->data[index];
19850 index = (index + step) & (symtab->size - 1);
19851 }
19852 }
19853
19854 /* Expand SYMTAB's hash table. */
19855
19856 static void
19857 hash_expand (struct mapped_symtab *symtab)
19858 {
19859 offset_type old_size = symtab->size;
19860 offset_type i;
19861 struct symtab_index_entry **old_entries = symtab->data;
19862
19863 symtab->size *= 2;
19864 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19865
19866 for (i = 0; i < old_size; ++i)
19867 {
19868 if (old_entries[i])
19869 {
19870 struct symtab_index_entry **slot = find_slot (symtab,
19871 old_entries[i]->name);
19872 *slot = old_entries[i];
19873 }
19874 }
19875
19876 xfree (old_entries);
19877 }
19878
19879 /* Add an entry to SYMTAB. NAME is the name of the symbol.
19880 CU_INDEX is the index of the CU in which the symbol appears.
19881 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19882
19883 static void
19884 add_index_entry (struct mapped_symtab *symtab, const char *name,
19885 int is_static, gdb_index_symbol_kind kind,
19886 offset_type cu_index)
19887 {
19888 struct symtab_index_entry **slot;
19889 offset_type cu_index_and_attrs;
19890
19891 ++symtab->n_elements;
19892 if (4 * symtab->n_elements / 3 >= symtab->size)
19893 hash_expand (symtab);
19894
19895 slot = find_slot (symtab, name);
19896 if (!*slot)
19897 {
19898 *slot = XNEW (struct symtab_index_entry);
19899 (*slot)->name = name;
19900 /* index_offset is set later. */
19901 (*slot)->cu_indices = NULL;
19902 }
19903
19904 cu_index_and_attrs = 0;
19905 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19906 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19907 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19908
19909 /* We don't want to record an index value twice as we want to avoid the
19910 duplication.
19911 We process all global symbols and then all static symbols
19912 (which would allow us to avoid the duplication by only having to check
19913 the last entry pushed), but a symbol could have multiple kinds in one CU.
19914 To keep things simple we don't worry about the duplication here and
19915 sort and uniqufy the list after we've processed all symbols. */
19916 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19917 }
19918
19919 /* qsort helper routine for uniquify_cu_indices. */
19920
19921 static int
19922 offset_type_compare (const void *ap, const void *bp)
19923 {
19924 offset_type a = *(offset_type *) ap;
19925 offset_type b = *(offset_type *) bp;
19926
19927 return (a > b) - (b > a);
19928 }
19929
19930 /* Sort and remove duplicates of all symbols' cu_indices lists. */
19931
19932 static void
19933 uniquify_cu_indices (struct mapped_symtab *symtab)
19934 {
19935 int i;
19936
19937 for (i = 0; i < symtab->size; ++i)
19938 {
19939 struct symtab_index_entry *entry = symtab->data[i];
19940
19941 if (entry
19942 && entry->cu_indices != NULL)
19943 {
19944 unsigned int next_to_insert, next_to_check;
19945 offset_type last_value;
19946
19947 qsort (VEC_address (offset_type, entry->cu_indices),
19948 VEC_length (offset_type, entry->cu_indices),
19949 sizeof (offset_type), offset_type_compare);
19950
19951 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19952 next_to_insert = 1;
19953 for (next_to_check = 1;
19954 next_to_check < VEC_length (offset_type, entry->cu_indices);
19955 ++next_to_check)
19956 {
19957 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19958 != last_value)
19959 {
19960 last_value = VEC_index (offset_type, entry->cu_indices,
19961 next_to_check);
19962 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19963 last_value);
19964 ++next_to_insert;
19965 }
19966 }
19967 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19968 }
19969 }
19970 }
19971
19972 /* Add a vector of indices to the constant pool. */
19973
19974 static offset_type
19975 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
19976 struct symtab_index_entry *entry)
19977 {
19978 void **slot;
19979
19980 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
19981 if (!*slot)
19982 {
19983 offset_type len = VEC_length (offset_type, entry->cu_indices);
19984 offset_type val = MAYBE_SWAP (len);
19985 offset_type iter;
19986 int i;
19987
19988 *slot = entry;
19989 entry->index_offset = obstack_object_size (cpool);
19990
19991 obstack_grow (cpool, &val, sizeof (val));
19992 for (i = 0;
19993 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19994 ++i)
19995 {
19996 val = MAYBE_SWAP (iter);
19997 obstack_grow (cpool, &val, sizeof (val));
19998 }
19999 }
20000 else
20001 {
20002 struct symtab_index_entry *old_entry = *slot;
20003 entry->index_offset = old_entry->index_offset;
20004 entry = old_entry;
20005 }
20006 return entry->index_offset;
20007 }
20008
20009 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20010 constant pool entries going into the obstack CPOOL. */
20011
20012 static void
20013 write_hash_table (struct mapped_symtab *symtab,
20014 struct obstack *output, struct obstack *cpool)
20015 {
20016 offset_type i;
20017 htab_t symbol_hash_table;
20018 htab_t str_table;
20019
20020 symbol_hash_table = create_symbol_hash_table ();
20021 str_table = create_strtab ();
20022
20023 /* We add all the index vectors to the constant pool first, to
20024 ensure alignment is ok. */
20025 for (i = 0; i < symtab->size; ++i)
20026 {
20027 if (symtab->data[i])
20028 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20029 }
20030
20031 /* Now write out the hash table. */
20032 for (i = 0; i < symtab->size; ++i)
20033 {
20034 offset_type str_off, vec_off;
20035
20036 if (symtab->data[i])
20037 {
20038 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20039 vec_off = symtab->data[i]->index_offset;
20040 }
20041 else
20042 {
20043 /* While 0 is a valid constant pool index, it is not valid
20044 to have 0 for both offsets. */
20045 str_off = 0;
20046 vec_off = 0;
20047 }
20048
20049 str_off = MAYBE_SWAP (str_off);
20050 vec_off = MAYBE_SWAP (vec_off);
20051
20052 obstack_grow (output, &str_off, sizeof (str_off));
20053 obstack_grow (output, &vec_off, sizeof (vec_off));
20054 }
20055
20056 htab_delete (str_table);
20057 htab_delete (symbol_hash_table);
20058 }
20059
20060 /* Struct to map psymtab to CU index in the index file. */
20061 struct psymtab_cu_index_map
20062 {
20063 struct partial_symtab *psymtab;
20064 unsigned int cu_index;
20065 };
20066
20067 static hashval_t
20068 hash_psymtab_cu_index (const void *item)
20069 {
20070 const struct psymtab_cu_index_map *map = item;
20071
20072 return htab_hash_pointer (map->psymtab);
20073 }
20074
20075 static int
20076 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20077 {
20078 const struct psymtab_cu_index_map *lhs = item_lhs;
20079 const struct psymtab_cu_index_map *rhs = item_rhs;
20080
20081 return lhs->psymtab == rhs->psymtab;
20082 }
20083
20084 /* Helper struct for building the address table. */
20085 struct addrmap_index_data
20086 {
20087 struct objfile *objfile;
20088 struct obstack *addr_obstack;
20089 htab_t cu_index_htab;
20090
20091 /* Non-zero if the previous_* fields are valid.
20092 We can't write an entry until we see the next entry (since it is only then
20093 that we know the end of the entry). */
20094 int previous_valid;
20095 /* Index of the CU in the table of all CUs in the index file. */
20096 unsigned int previous_cu_index;
20097 /* Start address of the CU. */
20098 CORE_ADDR previous_cu_start;
20099 };
20100
20101 /* Write an address entry to OBSTACK. */
20102
20103 static void
20104 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20105 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20106 {
20107 offset_type cu_index_to_write;
20108 char addr[8];
20109 CORE_ADDR baseaddr;
20110
20111 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20112
20113 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20114 obstack_grow (obstack, addr, 8);
20115 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20116 obstack_grow (obstack, addr, 8);
20117 cu_index_to_write = MAYBE_SWAP (cu_index);
20118 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20119 }
20120
20121 /* Worker function for traversing an addrmap to build the address table. */
20122
20123 static int
20124 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20125 {
20126 struct addrmap_index_data *data = datap;
20127 struct partial_symtab *pst = obj;
20128
20129 if (data->previous_valid)
20130 add_address_entry (data->objfile, data->addr_obstack,
20131 data->previous_cu_start, start_addr,
20132 data->previous_cu_index);
20133
20134 data->previous_cu_start = start_addr;
20135 if (pst != NULL)
20136 {
20137 struct psymtab_cu_index_map find_map, *map;
20138 find_map.psymtab = pst;
20139 map = htab_find (data->cu_index_htab, &find_map);
20140 gdb_assert (map != NULL);
20141 data->previous_cu_index = map->cu_index;
20142 data->previous_valid = 1;
20143 }
20144 else
20145 data->previous_valid = 0;
20146
20147 return 0;
20148 }
20149
20150 /* Write OBJFILE's address map to OBSTACK.
20151 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20152 in the index file. */
20153
20154 static void
20155 write_address_map (struct objfile *objfile, struct obstack *obstack,
20156 htab_t cu_index_htab)
20157 {
20158 struct addrmap_index_data addrmap_index_data;
20159
20160 /* When writing the address table, we have to cope with the fact that
20161 the addrmap iterator only provides the start of a region; we have to
20162 wait until the next invocation to get the start of the next region. */
20163
20164 addrmap_index_data.objfile = objfile;
20165 addrmap_index_data.addr_obstack = obstack;
20166 addrmap_index_data.cu_index_htab = cu_index_htab;
20167 addrmap_index_data.previous_valid = 0;
20168
20169 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20170 &addrmap_index_data);
20171
20172 /* It's highly unlikely the last entry (end address = 0xff...ff)
20173 is valid, but we should still handle it.
20174 The end address is recorded as the start of the next region, but that
20175 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20176 anyway. */
20177 if (addrmap_index_data.previous_valid)
20178 add_address_entry (objfile, obstack,
20179 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20180 addrmap_index_data.previous_cu_index);
20181 }
20182
20183 /* Return the symbol kind of PSYM. */
20184
20185 static gdb_index_symbol_kind
20186 symbol_kind (struct partial_symbol *psym)
20187 {
20188 domain_enum domain = PSYMBOL_DOMAIN (psym);
20189 enum address_class aclass = PSYMBOL_CLASS (psym);
20190
20191 switch (domain)
20192 {
20193 case VAR_DOMAIN:
20194 switch (aclass)
20195 {
20196 case LOC_BLOCK:
20197 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20198 case LOC_TYPEDEF:
20199 return GDB_INDEX_SYMBOL_KIND_TYPE;
20200 case LOC_COMPUTED:
20201 case LOC_CONST_BYTES:
20202 case LOC_OPTIMIZED_OUT:
20203 case LOC_STATIC:
20204 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20205 case LOC_CONST:
20206 /* Note: It's currently impossible to recognize psyms as enum values
20207 short of reading the type info. For now punt. */
20208 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20209 default:
20210 /* There are other LOC_FOO values that one might want to classify
20211 as variables, but dwarf2read.c doesn't currently use them. */
20212 return GDB_INDEX_SYMBOL_KIND_OTHER;
20213 }
20214 case STRUCT_DOMAIN:
20215 return GDB_INDEX_SYMBOL_KIND_TYPE;
20216 default:
20217 return GDB_INDEX_SYMBOL_KIND_OTHER;
20218 }
20219 }
20220
20221 /* Add a list of partial symbols to SYMTAB. */
20222
20223 static void
20224 write_psymbols (struct mapped_symtab *symtab,
20225 htab_t psyms_seen,
20226 struct partial_symbol **psymp,
20227 int count,
20228 offset_type cu_index,
20229 int is_static)
20230 {
20231 for (; count-- > 0; ++psymp)
20232 {
20233 struct partial_symbol *psym = *psymp;
20234 void **slot;
20235
20236 if (SYMBOL_LANGUAGE (psym) == language_ada)
20237 error (_("Ada is not currently supported by the index"));
20238
20239 /* Only add a given psymbol once. */
20240 slot = htab_find_slot (psyms_seen, psym, INSERT);
20241 if (!*slot)
20242 {
20243 gdb_index_symbol_kind kind = symbol_kind (psym);
20244
20245 *slot = psym;
20246 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20247 is_static, kind, cu_index);
20248 }
20249 }
20250 }
20251
20252 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20253 exception if there is an error. */
20254
20255 static void
20256 write_obstack (FILE *file, struct obstack *obstack)
20257 {
20258 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20259 file)
20260 != obstack_object_size (obstack))
20261 error (_("couldn't data write to file"));
20262 }
20263
20264 /* Unlink a file if the argument is not NULL. */
20265
20266 static void
20267 unlink_if_set (void *p)
20268 {
20269 char **filename = p;
20270 if (*filename)
20271 unlink (*filename);
20272 }
20273
20274 /* A helper struct used when iterating over debug_types. */
20275 struct signatured_type_index_data
20276 {
20277 struct objfile *objfile;
20278 struct mapped_symtab *symtab;
20279 struct obstack *types_list;
20280 htab_t psyms_seen;
20281 int cu_index;
20282 };
20283
20284 /* A helper function that writes a single signatured_type to an
20285 obstack. */
20286
20287 static int
20288 write_one_signatured_type (void **slot, void *d)
20289 {
20290 struct signatured_type_index_data *info = d;
20291 struct signatured_type *entry = (struct signatured_type *) *slot;
20292 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20293 struct partial_symtab *psymtab = per_cu->v.psymtab;
20294 gdb_byte val[8];
20295
20296 write_psymbols (info->symtab,
20297 info->psyms_seen,
20298 info->objfile->global_psymbols.list
20299 + psymtab->globals_offset,
20300 psymtab->n_global_syms, info->cu_index,
20301 0);
20302 write_psymbols (info->symtab,
20303 info->psyms_seen,
20304 info->objfile->static_psymbols.list
20305 + psymtab->statics_offset,
20306 psymtab->n_static_syms, info->cu_index,
20307 1);
20308
20309 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20310 entry->per_cu.offset.sect_off);
20311 obstack_grow (info->types_list, val, 8);
20312 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20313 entry->type_offset_in_tu.cu_off);
20314 obstack_grow (info->types_list, val, 8);
20315 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20316 obstack_grow (info->types_list, val, 8);
20317
20318 ++info->cu_index;
20319
20320 return 1;
20321 }
20322
20323 /* Recurse into all "included" dependencies and write their symbols as
20324 if they appeared in this psymtab. */
20325
20326 static void
20327 recursively_write_psymbols (struct objfile *objfile,
20328 struct partial_symtab *psymtab,
20329 struct mapped_symtab *symtab,
20330 htab_t psyms_seen,
20331 offset_type cu_index)
20332 {
20333 int i;
20334
20335 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20336 if (psymtab->dependencies[i]->user != NULL)
20337 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20338 symtab, psyms_seen, cu_index);
20339
20340 write_psymbols (symtab,
20341 psyms_seen,
20342 objfile->global_psymbols.list + psymtab->globals_offset,
20343 psymtab->n_global_syms, cu_index,
20344 0);
20345 write_psymbols (symtab,
20346 psyms_seen,
20347 objfile->static_psymbols.list + psymtab->statics_offset,
20348 psymtab->n_static_syms, cu_index,
20349 1);
20350 }
20351
20352 /* Create an index file for OBJFILE in the directory DIR. */
20353
20354 static void
20355 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20356 {
20357 struct cleanup *cleanup;
20358 char *filename, *cleanup_filename;
20359 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20360 struct obstack cu_list, types_cu_list;
20361 int i;
20362 FILE *out_file;
20363 struct mapped_symtab *symtab;
20364 offset_type val, size_of_contents, total_len;
20365 struct stat st;
20366 htab_t psyms_seen;
20367 htab_t cu_index_htab;
20368 struct psymtab_cu_index_map *psymtab_cu_index_map;
20369
20370 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20371 return;
20372
20373 if (dwarf2_per_objfile->using_index)
20374 error (_("Cannot use an index to create the index"));
20375
20376 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20377 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20378
20379 if (stat (objfile->name, &st) < 0)
20380 perror_with_name (objfile->name);
20381
20382 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20383 INDEX_SUFFIX, (char *) NULL);
20384 cleanup = make_cleanup (xfree, filename);
20385
20386 out_file = fopen (filename, "wb");
20387 if (!out_file)
20388 error (_("Can't open `%s' for writing"), filename);
20389
20390 cleanup_filename = filename;
20391 make_cleanup (unlink_if_set, &cleanup_filename);
20392
20393 symtab = create_mapped_symtab ();
20394 make_cleanup (cleanup_mapped_symtab, symtab);
20395
20396 obstack_init (&addr_obstack);
20397 make_cleanup_obstack_free (&addr_obstack);
20398
20399 obstack_init (&cu_list);
20400 make_cleanup_obstack_free (&cu_list);
20401
20402 obstack_init (&types_cu_list);
20403 make_cleanup_obstack_free (&types_cu_list);
20404
20405 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20406 NULL, xcalloc, xfree);
20407 make_cleanup_htab_delete (psyms_seen);
20408
20409 /* While we're scanning CU's create a table that maps a psymtab pointer
20410 (which is what addrmap records) to its index (which is what is recorded
20411 in the index file). This will later be needed to write the address
20412 table. */
20413 cu_index_htab = htab_create_alloc (100,
20414 hash_psymtab_cu_index,
20415 eq_psymtab_cu_index,
20416 NULL, xcalloc, xfree);
20417 make_cleanup_htab_delete (cu_index_htab);
20418 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20419 xmalloc (sizeof (struct psymtab_cu_index_map)
20420 * dwarf2_per_objfile->n_comp_units);
20421 make_cleanup (xfree, psymtab_cu_index_map);
20422
20423 /* The CU list is already sorted, so we don't need to do additional
20424 work here. Also, the debug_types entries do not appear in
20425 all_comp_units, but only in their own hash table. */
20426 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20427 {
20428 struct dwarf2_per_cu_data *per_cu
20429 = dwarf2_per_objfile->all_comp_units[i];
20430 struct partial_symtab *psymtab = per_cu->v.psymtab;
20431 gdb_byte val[8];
20432 struct psymtab_cu_index_map *map;
20433 void **slot;
20434
20435 if (psymtab->user == NULL)
20436 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20437
20438 map = &psymtab_cu_index_map[i];
20439 map->psymtab = psymtab;
20440 map->cu_index = i;
20441 slot = htab_find_slot (cu_index_htab, map, INSERT);
20442 gdb_assert (slot != NULL);
20443 gdb_assert (*slot == NULL);
20444 *slot = map;
20445
20446 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20447 per_cu->offset.sect_off);
20448 obstack_grow (&cu_list, val, 8);
20449 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20450 obstack_grow (&cu_list, val, 8);
20451 }
20452
20453 /* Dump the address map. */
20454 write_address_map (objfile, &addr_obstack, cu_index_htab);
20455
20456 /* Write out the .debug_type entries, if any. */
20457 if (dwarf2_per_objfile->signatured_types)
20458 {
20459 struct signatured_type_index_data sig_data;
20460
20461 sig_data.objfile = objfile;
20462 sig_data.symtab = symtab;
20463 sig_data.types_list = &types_cu_list;
20464 sig_data.psyms_seen = psyms_seen;
20465 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20466 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20467 write_one_signatured_type, &sig_data);
20468 }
20469
20470 /* Now that we've processed all symbols we can shrink their cu_indices
20471 lists. */
20472 uniquify_cu_indices (symtab);
20473
20474 obstack_init (&constant_pool);
20475 make_cleanup_obstack_free (&constant_pool);
20476 obstack_init (&symtab_obstack);
20477 make_cleanup_obstack_free (&symtab_obstack);
20478 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20479
20480 obstack_init (&contents);
20481 make_cleanup_obstack_free (&contents);
20482 size_of_contents = 6 * sizeof (offset_type);
20483 total_len = size_of_contents;
20484
20485 /* The version number. */
20486 val = MAYBE_SWAP (8);
20487 obstack_grow (&contents, &val, sizeof (val));
20488
20489 /* The offset of the CU list from the start of the file. */
20490 val = MAYBE_SWAP (total_len);
20491 obstack_grow (&contents, &val, sizeof (val));
20492 total_len += obstack_object_size (&cu_list);
20493
20494 /* The offset of the types CU list from the start of the file. */
20495 val = MAYBE_SWAP (total_len);
20496 obstack_grow (&contents, &val, sizeof (val));
20497 total_len += obstack_object_size (&types_cu_list);
20498
20499 /* The offset of the address table from the start of the file. */
20500 val = MAYBE_SWAP (total_len);
20501 obstack_grow (&contents, &val, sizeof (val));
20502 total_len += obstack_object_size (&addr_obstack);
20503
20504 /* The offset of the symbol table from the start of the file. */
20505 val = MAYBE_SWAP (total_len);
20506 obstack_grow (&contents, &val, sizeof (val));
20507 total_len += obstack_object_size (&symtab_obstack);
20508
20509 /* The offset of the constant pool from the start of the file. */
20510 val = MAYBE_SWAP (total_len);
20511 obstack_grow (&contents, &val, sizeof (val));
20512 total_len += obstack_object_size (&constant_pool);
20513
20514 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20515
20516 write_obstack (out_file, &contents);
20517 write_obstack (out_file, &cu_list);
20518 write_obstack (out_file, &types_cu_list);
20519 write_obstack (out_file, &addr_obstack);
20520 write_obstack (out_file, &symtab_obstack);
20521 write_obstack (out_file, &constant_pool);
20522
20523 fclose (out_file);
20524
20525 /* We want to keep the file, so we set cleanup_filename to NULL
20526 here. See unlink_if_set. */
20527 cleanup_filename = NULL;
20528
20529 do_cleanups (cleanup);
20530 }
20531
20532 /* Implementation of the `save gdb-index' command.
20533
20534 Note that the file format used by this command is documented in the
20535 GDB manual. Any changes here must be documented there. */
20536
20537 static void
20538 save_gdb_index_command (char *arg, int from_tty)
20539 {
20540 struct objfile *objfile;
20541
20542 if (!arg || !*arg)
20543 error (_("usage: save gdb-index DIRECTORY"));
20544
20545 ALL_OBJFILES (objfile)
20546 {
20547 struct stat st;
20548
20549 /* If the objfile does not correspond to an actual file, skip it. */
20550 if (stat (objfile->name, &st) < 0)
20551 continue;
20552
20553 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20554 if (dwarf2_per_objfile)
20555 {
20556 volatile struct gdb_exception except;
20557
20558 TRY_CATCH (except, RETURN_MASK_ERROR)
20559 {
20560 write_psymtabs_to_index (objfile, arg);
20561 }
20562 if (except.reason < 0)
20563 exception_fprintf (gdb_stderr, except,
20564 _("Error while writing index for `%s': "),
20565 objfile->name);
20566 }
20567 }
20568 }
20569
20570 \f
20571
20572 int dwarf2_always_disassemble;
20573
20574 static void
20575 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20576 struct cmd_list_element *c, const char *value)
20577 {
20578 fprintf_filtered (file,
20579 _("Whether to always disassemble "
20580 "DWARF expressions is %s.\n"),
20581 value);
20582 }
20583
20584 static void
20585 show_check_physname (struct ui_file *file, int from_tty,
20586 struct cmd_list_element *c, const char *value)
20587 {
20588 fprintf_filtered (file,
20589 _("Whether to check \"physname\" is %s.\n"),
20590 value);
20591 }
20592
20593 void _initialize_dwarf2_read (void);
20594
20595 void
20596 _initialize_dwarf2_read (void)
20597 {
20598 struct cmd_list_element *c;
20599
20600 dwarf2_objfile_data_key
20601 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20602
20603 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20604 Set DWARF 2 specific variables.\n\
20605 Configure DWARF 2 variables such as the cache size"),
20606 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20607 0/*allow-unknown*/, &maintenance_set_cmdlist);
20608
20609 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20610 Show DWARF 2 specific variables\n\
20611 Show DWARF 2 variables such as the cache size"),
20612 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20613 0/*allow-unknown*/, &maintenance_show_cmdlist);
20614
20615 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20616 &dwarf2_max_cache_age, _("\
20617 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20618 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20619 A higher limit means that cached compilation units will be stored\n\
20620 in memory longer, and more total memory will be used. Zero disables\n\
20621 caching, which can slow down startup."),
20622 NULL,
20623 show_dwarf2_max_cache_age,
20624 &set_dwarf2_cmdlist,
20625 &show_dwarf2_cmdlist);
20626
20627 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20628 &dwarf2_always_disassemble, _("\
20629 Set whether `info address' always disassembles DWARF expressions."), _("\
20630 Show whether `info address' always disassembles DWARF expressions."), _("\
20631 When enabled, DWARF expressions are always printed in an assembly-like\n\
20632 syntax. When disabled, expressions will be printed in a more\n\
20633 conversational style, when possible."),
20634 NULL,
20635 show_dwarf2_always_disassemble,
20636 &set_dwarf2_cmdlist,
20637 &show_dwarf2_cmdlist);
20638
20639 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20640 Set debugging of the dwarf2 reader."), _("\
20641 Show debugging of the dwarf2 reader."), _("\
20642 When enabled, debugging messages are printed during dwarf2 reading\n\
20643 and symtab expansion."),
20644 NULL,
20645 NULL,
20646 &setdebuglist, &showdebuglist);
20647
20648 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20649 Set debugging of the dwarf2 DIE reader."), _("\
20650 Show debugging of the dwarf2 DIE reader."), _("\
20651 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20652 The value is the maximum depth to print."),
20653 NULL,
20654 NULL,
20655 &setdebuglist, &showdebuglist);
20656
20657 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20658 Set cross-checking of \"physname\" code against demangler."), _("\
20659 Show cross-checking of \"physname\" code against demangler."), _("\
20660 When enabled, GDB's internal \"physname\" code is checked against\n\
20661 the demangler."),
20662 NULL, show_check_physname,
20663 &setdebuglist, &showdebuglist);
20664
20665 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20666 no_class, &use_deprecated_index_sections, _("\
20667 Set whether to use deprecated gdb_index sections."), _("\
20668 Show whether to use deprecated gdb_index sections."), _("\
20669 When enabled, deprecated .gdb_index sections are used anyway.\n\
20670 Normally they are ignored either because of a missing feature or\n\
20671 performance issue.\n\
20672 Warning: This option must be enabled before gdb reads the file."),
20673 NULL,
20674 NULL,
20675 &setlist, &showlist);
20676
20677 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20678 _("\
20679 Save a gdb-index file.\n\
20680 Usage: save gdb-index DIRECTORY"),
20681 &save_cmdlist);
20682 set_cmd_completer (c, filename_completer);
20683 }
This page took 0.464144 seconds and 5 git commands to generate.