arc: Fix ARI warning for printf(%p)
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2016 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include <sys/stat.h>
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include <ctype.h>
67 #include "gdb_bfd.h"
68 #include "f-lang.h"
69 #include "source.h"
70 #include "filestuff.h"
71 #include "build-id.h"
72 #include "namespace.h"
73
74 #include <fcntl.h>
75 #include <sys/types.h>
76 #include <algorithm>
77
78 typedef struct symbol *symbolp;
79 DEF_VEC_P (symbolp);
80
81 /* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84 static unsigned int dwarf_read_debug = 0;
85
86 /* When non-zero, dump DIEs after they are read in. */
87 static unsigned int dwarf_die_debug = 0;
88
89 /* When non-zero, dump line number entries as they are read in. */
90 static unsigned int dwarf_line_debug = 0;
91
92 /* When non-zero, cross-check physname against demangler. */
93 static int check_physname = 0;
94
95 /* When non-zero, do not reject deprecated .gdb_index sections. */
96 static int use_deprecated_index_sections = 0;
97
98 static const struct objfile_data *dwarf2_objfile_data_key;
99
100 /* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102 static int dwarf2_locexpr_index;
103 static int dwarf2_loclist_index;
104 static int dwarf2_locexpr_block_index;
105 static int dwarf2_loclist_block_index;
106
107 /* A descriptor for dwarf sections.
108
109 S.ASECTION, SIZE are typically initialized when the objfile is first
110 scanned. BUFFER, READIN are filled in later when the section is read.
111 If the section contained compressed data then SIZE is updated to record
112 the uncompressed size of the section.
113
114 DWP file format V2 introduces a wrinkle that is easiest to handle by
115 creating the concept of virtual sections contained within a real section.
116 In DWP V2 the sections of the input DWO files are concatenated together
117 into one section, but section offsets are kept relative to the original
118 input section.
119 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
120 the real section this "virtual" section is contained in, and BUFFER,SIZE
121 describe the virtual section. */
122
123 struct dwarf2_section_info
124 {
125 union
126 {
127 /* If this is a real section, the bfd section. */
128 asection *section;
129 /* If this is a virtual section, pointer to the containing ("real")
130 section. */
131 struct dwarf2_section_info *containing_section;
132 } s;
133 /* Pointer to section data, only valid if readin. */
134 const gdb_byte *buffer;
135 /* The size of the section, real or virtual. */
136 bfd_size_type size;
137 /* If this is a virtual section, the offset in the real section.
138 Only valid if is_virtual. */
139 bfd_size_type virtual_offset;
140 /* True if we have tried to read this section. */
141 char readin;
142 /* True if this is a virtual section, False otherwise.
143 This specifies which of s.section and s.containing_section to use. */
144 char is_virtual;
145 };
146
147 typedef struct dwarf2_section_info dwarf2_section_info_def;
148 DEF_VEC_O (dwarf2_section_info_def);
149
150 /* All offsets in the index are of this type. It must be
151 architecture-independent. */
152 typedef uint32_t offset_type;
153
154 DEF_VEC_I (offset_type);
155
156 /* Ensure only legit values are used. */
157 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
158 do { \
159 gdb_assert ((unsigned int) (value) <= 1); \
160 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
161 } while (0)
162
163 /* Ensure only legit values are used. */
164 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
165 do { \
166 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
167 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
168 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
169 } while (0)
170
171 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
172 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
173 do { \
174 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
175 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
176 } while (0)
177
178 /* A description of the mapped index. The file format is described in
179 a comment by the code that writes the index. */
180 struct mapped_index
181 {
182 /* Index data format version. */
183 int version;
184
185 /* The total length of the buffer. */
186 off_t total_size;
187
188 /* A pointer to the address table data. */
189 const gdb_byte *address_table;
190
191 /* Size of the address table data in bytes. */
192 offset_type address_table_size;
193
194 /* The symbol table, implemented as a hash table. */
195 const offset_type *symbol_table;
196
197 /* Size in slots, each slot is 2 offset_types. */
198 offset_type symbol_table_slots;
199
200 /* A pointer to the constant pool. */
201 const char *constant_pool;
202 };
203
204 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
205 DEF_VEC_P (dwarf2_per_cu_ptr);
206
207 struct tu_stats
208 {
209 int nr_uniq_abbrev_tables;
210 int nr_symtabs;
211 int nr_symtab_sharers;
212 int nr_stmt_less_type_units;
213 int nr_all_type_units_reallocs;
214 };
215
216 /* Collection of data recorded per objfile.
217 This hangs off of dwarf2_objfile_data_key. */
218
219 struct dwarf2_per_objfile
220 {
221 struct dwarf2_section_info info;
222 struct dwarf2_section_info abbrev;
223 struct dwarf2_section_info line;
224 struct dwarf2_section_info loc;
225 struct dwarf2_section_info macinfo;
226 struct dwarf2_section_info macro;
227 struct dwarf2_section_info str;
228 struct dwarf2_section_info ranges;
229 struct dwarf2_section_info addr;
230 struct dwarf2_section_info frame;
231 struct dwarf2_section_info eh_frame;
232 struct dwarf2_section_info gdb_index;
233
234 VEC (dwarf2_section_info_def) *types;
235
236 /* Back link. */
237 struct objfile *objfile;
238
239 /* Table of all the compilation units. This is used to locate
240 the target compilation unit of a particular reference. */
241 struct dwarf2_per_cu_data **all_comp_units;
242
243 /* The number of compilation units in ALL_COMP_UNITS. */
244 int n_comp_units;
245
246 /* The number of .debug_types-related CUs. */
247 int n_type_units;
248
249 /* The number of elements allocated in all_type_units.
250 If there are skeleton-less TUs, we add them to all_type_units lazily. */
251 int n_allocated_type_units;
252
253 /* The .debug_types-related CUs (TUs).
254 This is stored in malloc space because we may realloc it. */
255 struct signatured_type **all_type_units;
256
257 /* Table of struct type_unit_group objects.
258 The hash key is the DW_AT_stmt_list value. */
259 htab_t type_unit_groups;
260
261 /* A table mapping .debug_types signatures to its signatured_type entry.
262 This is NULL if the .debug_types section hasn't been read in yet. */
263 htab_t signatured_types;
264
265 /* Type unit statistics, to see how well the scaling improvements
266 are doing. */
267 struct tu_stats tu_stats;
268
269 /* A chain of compilation units that are currently read in, so that
270 they can be freed later. */
271 struct dwarf2_per_cu_data *read_in_chain;
272
273 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
274 This is NULL if the table hasn't been allocated yet. */
275 htab_t dwo_files;
276
277 /* Non-zero if we've check for whether there is a DWP file. */
278 int dwp_checked;
279
280 /* The DWP file if there is one, or NULL. */
281 struct dwp_file *dwp_file;
282
283 /* The shared '.dwz' file, if one exists. This is used when the
284 original data was compressed using 'dwz -m'. */
285 struct dwz_file *dwz_file;
286
287 /* A flag indicating wether this objfile has a section loaded at a
288 VMA of 0. */
289 int has_section_at_zero;
290
291 /* True if we are using the mapped index,
292 or we are faking it for OBJF_READNOW's sake. */
293 unsigned char using_index;
294
295 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
296 struct mapped_index *index_table;
297
298 /* When using index_table, this keeps track of all quick_file_names entries.
299 TUs typically share line table entries with a CU, so we maintain a
300 separate table of all line table entries to support the sharing.
301 Note that while there can be way more TUs than CUs, we've already
302 sorted all the TUs into "type unit groups", grouped by their
303 DW_AT_stmt_list value. Therefore the only sharing done here is with a
304 CU and its associated TU group if there is one. */
305 htab_t quick_file_names_table;
306
307 /* Set during partial symbol reading, to prevent queueing of full
308 symbols. */
309 int reading_partial_symbols;
310
311 /* Table mapping type DIEs to their struct type *.
312 This is NULL if not allocated yet.
313 The mapping is done via (CU/TU + DIE offset) -> type. */
314 htab_t die_type_hash;
315
316 /* The CUs we recently read. */
317 VEC (dwarf2_per_cu_ptr) *just_read_cus;
318
319 /* Table containing line_header indexed by offset and offset_in_dwz. */
320 htab_t line_header_hash;
321 };
322
323 static struct dwarf2_per_objfile *dwarf2_per_objfile;
324
325 /* Default names of the debugging sections. */
326
327 /* Note that if the debugging section has been compressed, it might
328 have a name like .zdebug_info. */
329
330 static const struct dwarf2_debug_sections dwarf2_elf_names =
331 {
332 { ".debug_info", ".zdebug_info" },
333 { ".debug_abbrev", ".zdebug_abbrev" },
334 { ".debug_line", ".zdebug_line" },
335 { ".debug_loc", ".zdebug_loc" },
336 { ".debug_macinfo", ".zdebug_macinfo" },
337 { ".debug_macro", ".zdebug_macro" },
338 { ".debug_str", ".zdebug_str" },
339 { ".debug_ranges", ".zdebug_ranges" },
340 { ".debug_types", ".zdebug_types" },
341 { ".debug_addr", ".zdebug_addr" },
342 { ".debug_frame", ".zdebug_frame" },
343 { ".eh_frame", NULL },
344 { ".gdb_index", ".zgdb_index" },
345 23
346 };
347
348 /* List of DWO/DWP sections. */
349
350 static const struct dwop_section_names
351 {
352 struct dwarf2_section_names abbrev_dwo;
353 struct dwarf2_section_names info_dwo;
354 struct dwarf2_section_names line_dwo;
355 struct dwarf2_section_names loc_dwo;
356 struct dwarf2_section_names macinfo_dwo;
357 struct dwarf2_section_names macro_dwo;
358 struct dwarf2_section_names str_dwo;
359 struct dwarf2_section_names str_offsets_dwo;
360 struct dwarf2_section_names types_dwo;
361 struct dwarf2_section_names cu_index;
362 struct dwarf2_section_names tu_index;
363 }
364 dwop_section_names =
365 {
366 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
367 { ".debug_info.dwo", ".zdebug_info.dwo" },
368 { ".debug_line.dwo", ".zdebug_line.dwo" },
369 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
370 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
371 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
372 { ".debug_str.dwo", ".zdebug_str.dwo" },
373 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
374 { ".debug_types.dwo", ".zdebug_types.dwo" },
375 { ".debug_cu_index", ".zdebug_cu_index" },
376 { ".debug_tu_index", ".zdebug_tu_index" },
377 };
378
379 /* local data types */
380
381 /* The data in a compilation unit header, after target2host
382 translation, looks like this. */
383 struct comp_unit_head
384 {
385 unsigned int length;
386 short version;
387 unsigned char addr_size;
388 unsigned char signed_addr_p;
389 sect_offset abbrev_offset;
390
391 /* Size of file offsets; either 4 or 8. */
392 unsigned int offset_size;
393
394 /* Size of the length field; either 4 or 12. */
395 unsigned int initial_length_size;
396
397 /* Offset to the first byte of this compilation unit header in the
398 .debug_info section, for resolving relative reference dies. */
399 sect_offset offset;
400
401 /* Offset to first die in this cu from the start of the cu.
402 This will be the first byte following the compilation unit header. */
403 cu_offset first_die_offset;
404 };
405
406 /* Type used for delaying computation of method physnames.
407 See comments for compute_delayed_physnames. */
408 struct delayed_method_info
409 {
410 /* The type to which the method is attached, i.e., its parent class. */
411 struct type *type;
412
413 /* The index of the method in the type's function fieldlists. */
414 int fnfield_index;
415
416 /* The index of the method in the fieldlist. */
417 int index;
418
419 /* The name of the DIE. */
420 const char *name;
421
422 /* The DIE associated with this method. */
423 struct die_info *die;
424 };
425
426 typedef struct delayed_method_info delayed_method_info;
427 DEF_VEC_O (delayed_method_info);
428
429 /* Internal state when decoding a particular compilation unit. */
430 struct dwarf2_cu
431 {
432 /* The objfile containing this compilation unit. */
433 struct objfile *objfile;
434
435 /* The header of the compilation unit. */
436 struct comp_unit_head header;
437
438 /* Base address of this compilation unit. */
439 CORE_ADDR base_address;
440
441 /* Non-zero if base_address has been set. */
442 int base_known;
443
444 /* The language we are debugging. */
445 enum language language;
446 const struct language_defn *language_defn;
447
448 const char *producer;
449
450 /* The generic symbol table building routines have separate lists for
451 file scope symbols and all all other scopes (local scopes). So
452 we need to select the right one to pass to add_symbol_to_list().
453 We do it by keeping a pointer to the correct list in list_in_scope.
454
455 FIXME: The original dwarf code just treated the file scope as the
456 first local scope, and all other local scopes as nested local
457 scopes, and worked fine. Check to see if we really need to
458 distinguish these in buildsym.c. */
459 struct pending **list_in_scope;
460
461 /* The abbrev table for this CU.
462 Normally this points to the abbrev table in the objfile.
463 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
464 struct abbrev_table *abbrev_table;
465
466 /* Hash table holding all the loaded partial DIEs
467 with partial_die->offset.SECT_OFF as hash. */
468 htab_t partial_dies;
469
470 /* Storage for things with the same lifetime as this read-in compilation
471 unit, including partial DIEs. */
472 struct obstack comp_unit_obstack;
473
474 /* When multiple dwarf2_cu structures are living in memory, this field
475 chains them all together, so that they can be released efficiently.
476 We will probably also want a generation counter so that most-recently-used
477 compilation units are cached... */
478 struct dwarf2_per_cu_data *read_in_chain;
479
480 /* Backlink to our per_cu entry. */
481 struct dwarf2_per_cu_data *per_cu;
482
483 /* How many compilation units ago was this CU last referenced? */
484 int last_used;
485
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
488 htab_t die_hash;
489
490 /* Full DIEs if read in. */
491 struct die_info *dies;
492
493 /* A set of pointers to dwarf2_per_cu_data objects for compilation
494 units referenced by this one. Only set during full symbol processing;
495 partial symbol tables do not have dependencies. */
496 htab_t dependencies;
497
498 /* Header data from the line table, during full symbol processing. */
499 struct line_header *line_header;
500
501 /* A list of methods which need to have physnames computed
502 after all type information has been read. */
503 VEC (delayed_method_info) *method_list;
504
505 /* To be copied to symtab->call_site_htab. */
506 htab_t call_site_htab;
507
508 /* Non-NULL if this CU came from a DWO file.
509 There is an invariant here that is important to remember:
510 Except for attributes copied from the top level DIE in the "main"
511 (or "stub") file in preparation for reading the DWO file
512 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
513 Either there isn't a DWO file (in which case this is NULL and the point
514 is moot), or there is and either we're not going to read it (in which
515 case this is NULL) or there is and we are reading it (in which case this
516 is non-NULL). */
517 struct dwo_unit *dwo_unit;
518
519 /* The DW_AT_addr_base attribute if present, zero otherwise
520 (zero is a valid value though).
521 Note this value comes from the Fission stub CU/TU's DIE. */
522 ULONGEST addr_base;
523
524 /* The DW_AT_ranges_base attribute if present, zero otherwise
525 (zero is a valid value though).
526 Note this value comes from the Fission stub CU/TU's DIE.
527 Also note that the value is zero in the non-DWO case so this value can
528 be used without needing to know whether DWO files are in use or not.
529 N.B. This does not apply to DW_AT_ranges appearing in
530 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
531 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
532 DW_AT_ranges_base *would* have to be applied, and we'd have to care
533 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
534 ULONGEST ranges_base;
535
536 /* Mark used when releasing cached dies. */
537 unsigned int mark : 1;
538
539 /* This CU references .debug_loc. See the symtab->locations_valid field.
540 This test is imperfect as there may exist optimized debug code not using
541 any location list and still facing inlining issues if handled as
542 unoptimized code. For a future better test see GCC PR other/32998. */
543 unsigned int has_loclist : 1;
544
545 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
546 if all the producer_is_* fields are valid. This information is cached
547 because profiling CU expansion showed excessive time spent in
548 producer_is_gxx_lt_4_6. */
549 unsigned int checked_producer : 1;
550 unsigned int producer_is_gxx_lt_4_6 : 1;
551 unsigned int producer_is_gcc_lt_4_3 : 1;
552 unsigned int producer_is_icc : 1;
553
554 /* When set, the file that we're processing is known to have
555 debugging info for C++ namespaces. GCC 3.3.x did not produce
556 this information, but later versions do. */
557
558 unsigned int processing_has_namespace_info : 1;
559 };
560
561 /* Persistent data held for a compilation unit, even when not
562 processing it. We put a pointer to this structure in the
563 read_symtab_private field of the psymtab. */
564
565 struct dwarf2_per_cu_data
566 {
567 /* The start offset and length of this compilation unit.
568 NOTE: Unlike comp_unit_head.length, this length includes
569 initial_length_size.
570 If the DIE refers to a DWO file, this is always of the original die,
571 not the DWO file. */
572 sect_offset offset;
573 unsigned int length;
574
575 /* Flag indicating this compilation unit will be read in before
576 any of the current compilation units are processed. */
577 unsigned int queued : 1;
578
579 /* This flag will be set when reading partial DIEs if we need to load
580 absolutely all DIEs for this compilation unit, instead of just the ones
581 we think are interesting. It gets set if we look for a DIE in the
582 hash table and don't find it. */
583 unsigned int load_all_dies : 1;
584
585 /* Non-zero if this CU is from .debug_types.
586 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
587 this is non-zero. */
588 unsigned int is_debug_types : 1;
589
590 /* Non-zero if this CU is from the .dwz file. */
591 unsigned int is_dwz : 1;
592
593 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
594 This flag is only valid if is_debug_types is true.
595 We can't read a CU directly from a DWO file: There are required
596 attributes in the stub. */
597 unsigned int reading_dwo_directly : 1;
598
599 /* Non-zero if the TU has been read.
600 This is used to assist the "Stay in DWO Optimization" for Fission:
601 When reading a DWO, it's faster to read TUs from the DWO instead of
602 fetching them from random other DWOs (due to comdat folding).
603 If the TU has already been read, the optimization is unnecessary
604 (and unwise - we don't want to change where gdb thinks the TU lives
605 "midflight").
606 This flag is only valid if is_debug_types is true. */
607 unsigned int tu_read : 1;
608
609 /* The section this CU/TU lives in.
610 If the DIE refers to a DWO file, this is always the original die,
611 not the DWO file. */
612 struct dwarf2_section_info *section;
613
614 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
615 of the CU cache it gets reset to NULL again. This is left as NULL for
616 dummy CUs (a CU header, but nothing else). */
617 struct dwarf2_cu *cu;
618
619 /* The corresponding objfile.
620 Normally we can get the objfile from dwarf2_per_objfile.
621 However we can enter this file with just a "per_cu" handle. */
622 struct objfile *objfile;
623
624 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
625 is active. Otherwise, the 'psymtab' field is active. */
626 union
627 {
628 /* The partial symbol table associated with this compilation unit,
629 or NULL for unread partial units. */
630 struct partial_symtab *psymtab;
631
632 /* Data needed by the "quick" functions. */
633 struct dwarf2_per_cu_quick_data *quick;
634 } v;
635
636 /* The CUs we import using DW_TAG_imported_unit. This is filled in
637 while reading psymtabs, used to compute the psymtab dependencies,
638 and then cleared. Then it is filled in again while reading full
639 symbols, and only deleted when the objfile is destroyed.
640
641 This is also used to work around a difference between the way gold
642 generates .gdb_index version <=7 and the way gdb does. Arguably this
643 is a gold bug. For symbols coming from TUs, gold records in the index
644 the CU that includes the TU instead of the TU itself. This breaks
645 dw2_lookup_symbol: It assumes that if the index says symbol X lives
646 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
647 will find X. Alas TUs live in their own symtab, so after expanding CU Y
648 we need to look in TU Z to find X. Fortunately, this is akin to
649 DW_TAG_imported_unit, so we just use the same mechanism: For
650 .gdb_index version <=7 this also records the TUs that the CU referred
651 to. Concurrently with this change gdb was modified to emit version 8
652 indices so we only pay a price for gold generated indices.
653 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
654 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
655 };
656
657 /* Entry in the signatured_types hash table. */
658
659 struct signatured_type
660 {
661 /* The "per_cu" object of this type.
662 This struct is used iff per_cu.is_debug_types.
663 N.B.: This is the first member so that it's easy to convert pointers
664 between them. */
665 struct dwarf2_per_cu_data per_cu;
666
667 /* The type's signature. */
668 ULONGEST signature;
669
670 /* Offset in the TU of the type's DIE, as read from the TU header.
671 If this TU is a DWO stub and the definition lives in a DWO file
672 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
673 cu_offset type_offset_in_tu;
674
675 /* Offset in the section of the type's DIE.
676 If the definition lives in a DWO file, this is the offset in the
677 .debug_types.dwo section.
678 The value is zero until the actual value is known.
679 Zero is otherwise not a valid section offset. */
680 sect_offset type_offset_in_section;
681
682 /* Type units are grouped by their DW_AT_stmt_list entry so that they
683 can share them. This points to the containing symtab. */
684 struct type_unit_group *type_unit_group;
685
686 /* The type.
687 The first time we encounter this type we fully read it in and install it
688 in the symbol tables. Subsequent times we only need the type. */
689 struct type *type;
690
691 /* Containing DWO unit.
692 This field is valid iff per_cu.reading_dwo_directly. */
693 struct dwo_unit *dwo_unit;
694 };
695
696 typedef struct signatured_type *sig_type_ptr;
697 DEF_VEC_P (sig_type_ptr);
698
699 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
700 This includes type_unit_group and quick_file_names. */
701
702 struct stmt_list_hash
703 {
704 /* The DWO unit this table is from or NULL if there is none. */
705 struct dwo_unit *dwo_unit;
706
707 /* Offset in .debug_line or .debug_line.dwo. */
708 sect_offset line_offset;
709 };
710
711 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
712 an object of this type. */
713
714 struct type_unit_group
715 {
716 /* dwarf2read.c's main "handle" on a TU symtab.
717 To simplify things we create an artificial CU that "includes" all the
718 type units using this stmt_list so that the rest of the code still has
719 a "per_cu" handle on the symtab.
720 This PER_CU is recognized by having no section. */
721 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
722 struct dwarf2_per_cu_data per_cu;
723
724 /* The TUs that share this DW_AT_stmt_list entry.
725 This is added to while parsing type units to build partial symtabs,
726 and is deleted afterwards and not used again. */
727 VEC (sig_type_ptr) *tus;
728
729 /* The compunit symtab.
730 Type units in a group needn't all be defined in the same source file,
731 so we create an essentially anonymous symtab as the compunit symtab. */
732 struct compunit_symtab *compunit_symtab;
733
734 /* The data used to construct the hash key. */
735 struct stmt_list_hash hash;
736
737 /* The number of symtabs from the line header.
738 The value here must match line_header.num_file_names. */
739 unsigned int num_symtabs;
740
741 /* The symbol tables for this TU (obtained from the files listed in
742 DW_AT_stmt_list).
743 WARNING: The order of entries here must match the order of entries
744 in the line header. After the first TU using this type_unit_group, the
745 line header for the subsequent TUs is recreated from this. This is done
746 because we need to use the same symtabs for each TU using the same
747 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
748 there's no guarantee the line header doesn't have duplicate entries. */
749 struct symtab **symtabs;
750 };
751
752 /* These sections are what may appear in a (real or virtual) DWO file. */
753
754 struct dwo_sections
755 {
756 struct dwarf2_section_info abbrev;
757 struct dwarf2_section_info line;
758 struct dwarf2_section_info loc;
759 struct dwarf2_section_info macinfo;
760 struct dwarf2_section_info macro;
761 struct dwarf2_section_info str;
762 struct dwarf2_section_info str_offsets;
763 /* In the case of a virtual DWO file, these two are unused. */
764 struct dwarf2_section_info info;
765 VEC (dwarf2_section_info_def) *types;
766 };
767
768 /* CUs/TUs in DWP/DWO files. */
769
770 struct dwo_unit
771 {
772 /* Backlink to the containing struct dwo_file. */
773 struct dwo_file *dwo_file;
774
775 /* The "id" that distinguishes this CU/TU.
776 .debug_info calls this "dwo_id", .debug_types calls this "signature".
777 Since signatures came first, we stick with it for consistency. */
778 ULONGEST signature;
779
780 /* The section this CU/TU lives in, in the DWO file. */
781 struct dwarf2_section_info *section;
782
783 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
784 sect_offset offset;
785 unsigned int length;
786
787 /* For types, offset in the type's DIE of the type defined by this TU. */
788 cu_offset type_offset_in_tu;
789 };
790
791 /* include/dwarf2.h defines the DWP section codes.
792 It defines a max value but it doesn't define a min value, which we
793 use for error checking, so provide one. */
794
795 enum dwp_v2_section_ids
796 {
797 DW_SECT_MIN = 1
798 };
799
800 /* Data for one DWO file.
801
802 This includes virtual DWO files (a virtual DWO file is a DWO file as it
803 appears in a DWP file). DWP files don't really have DWO files per se -
804 comdat folding of types "loses" the DWO file they came from, and from
805 a high level view DWP files appear to contain a mass of random types.
806 However, to maintain consistency with the non-DWP case we pretend DWP
807 files contain virtual DWO files, and we assign each TU with one virtual
808 DWO file (generally based on the line and abbrev section offsets -
809 a heuristic that seems to work in practice). */
810
811 struct dwo_file
812 {
813 /* The DW_AT_GNU_dwo_name attribute.
814 For virtual DWO files the name is constructed from the section offsets
815 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
816 from related CU+TUs. */
817 const char *dwo_name;
818
819 /* The DW_AT_comp_dir attribute. */
820 const char *comp_dir;
821
822 /* The bfd, when the file is open. Otherwise this is NULL.
823 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
824 bfd *dbfd;
825
826 /* The sections that make up this DWO file.
827 Remember that for virtual DWO files in DWP V2, these are virtual
828 sections (for lack of a better name). */
829 struct dwo_sections sections;
830
831 /* The CU in the file.
832 We only support one because having more than one requires hacking the
833 dwo_name of each to match, which is highly unlikely to happen.
834 Doing this means all TUs can share comp_dir: We also assume that
835 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
836 struct dwo_unit *cu;
837
838 /* Table of TUs in the file.
839 Each element is a struct dwo_unit. */
840 htab_t tus;
841 };
842
843 /* These sections are what may appear in a DWP file. */
844
845 struct dwp_sections
846 {
847 /* These are used by both DWP version 1 and 2. */
848 struct dwarf2_section_info str;
849 struct dwarf2_section_info cu_index;
850 struct dwarf2_section_info tu_index;
851
852 /* These are only used by DWP version 2 files.
853 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
854 sections are referenced by section number, and are not recorded here.
855 In DWP version 2 there is at most one copy of all these sections, each
856 section being (effectively) comprised of the concatenation of all of the
857 individual sections that exist in the version 1 format.
858 To keep the code simple we treat each of these concatenated pieces as a
859 section itself (a virtual section?). */
860 struct dwarf2_section_info abbrev;
861 struct dwarf2_section_info info;
862 struct dwarf2_section_info line;
863 struct dwarf2_section_info loc;
864 struct dwarf2_section_info macinfo;
865 struct dwarf2_section_info macro;
866 struct dwarf2_section_info str_offsets;
867 struct dwarf2_section_info types;
868 };
869
870 /* These sections are what may appear in a virtual DWO file in DWP version 1.
871 A virtual DWO file is a DWO file as it appears in a DWP file. */
872
873 struct virtual_v1_dwo_sections
874 {
875 struct dwarf2_section_info abbrev;
876 struct dwarf2_section_info line;
877 struct dwarf2_section_info loc;
878 struct dwarf2_section_info macinfo;
879 struct dwarf2_section_info macro;
880 struct dwarf2_section_info str_offsets;
881 /* Each DWP hash table entry records one CU or one TU.
882 That is recorded here, and copied to dwo_unit.section. */
883 struct dwarf2_section_info info_or_types;
884 };
885
886 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
887 In version 2, the sections of the DWO files are concatenated together
888 and stored in one section of that name. Thus each ELF section contains
889 several "virtual" sections. */
890
891 struct virtual_v2_dwo_sections
892 {
893 bfd_size_type abbrev_offset;
894 bfd_size_type abbrev_size;
895
896 bfd_size_type line_offset;
897 bfd_size_type line_size;
898
899 bfd_size_type loc_offset;
900 bfd_size_type loc_size;
901
902 bfd_size_type macinfo_offset;
903 bfd_size_type macinfo_size;
904
905 bfd_size_type macro_offset;
906 bfd_size_type macro_size;
907
908 bfd_size_type str_offsets_offset;
909 bfd_size_type str_offsets_size;
910
911 /* Each DWP hash table entry records one CU or one TU.
912 That is recorded here, and copied to dwo_unit.section. */
913 bfd_size_type info_or_types_offset;
914 bfd_size_type info_or_types_size;
915 };
916
917 /* Contents of DWP hash tables. */
918
919 struct dwp_hash_table
920 {
921 uint32_t version, nr_columns;
922 uint32_t nr_units, nr_slots;
923 const gdb_byte *hash_table, *unit_table;
924 union
925 {
926 struct
927 {
928 const gdb_byte *indices;
929 } v1;
930 struct
931 {
932 /* This is indexed by column number and gives the id of the section
933 in that column. */
934 #define MAX_NR_V2_DWO_SECTIONS \
935 (1 /* .debug_info or .debug_types */ \
936 + 1 /* .debug_abbrev */ \
937 + 1 /* .debug_line */ \
938 + 1 /* .debug_loc */ \
939 + 1 /* .debug_str_offsets */ \
940 + 1 /* .debug_macro or .debug_macinfo */)
941 int section_ids[MAX_NR_V2_DWO_SECTIONS];
942 const gdb_byte *offsets;
943 const gdb_byte *sizes;
944 } v2;
945 } section_pool;
946 };
947
948 /* Data for one DWP file. */
949
950 struct dwp_file
951 {
952 /* Name of the file. */
953 const char *name;
954
955 /* File format version. */
956 int version;
957
958 /* The bfd. */
959 bfd *dbfd;
960
961 /* Section info for this file. */
962 struct dwp_sections sections;
963
964 /* Table of CUs in the file. */
965 const struct dwp_hash_table *cus;
966
967 /* Table of TUs in the file. */
968 const struct dwp_hash_table *tus;
969
970 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
971 htab_t loaded_cus;
972 htab_t loaded_tus;
973
974 /* Table to map ELF section numbers to their sections.
975 This is only needed for the DWP V1 file format. */
976 unsigned int num_sections;
977 asection **elf_sections;
978 };
979
980 /* This represents a '.dwz' file. */
981
982 struct dwz_file
983 {
984 /* A dwz file can only contain a few sections. */
985 struct dwarf2_section_info abbrev;
986 struct dwarf2_section_info info;
987 struct dwarf2_section_info str;
988 struct dwarf2_section_info line;
989 struct dwarf2_section_info macro;
990 struct dwarf2_section_info gdb_index;
991
992 /* The dwz's BFD. */
993 bfd *dwz_bfd;
994 };
995
996 /* Struct used to pass misc. parameters to read_die_and_children, et
997 al. which are used for both .debug_info and .debug_types dies.
998 All parameters here are unchanging for the life of the call. This
999 struct exists to abstract away the constant parameters of die reading. */
1000
1001 struct die_reader_specs
1002 {
1003 /* The bfd of die_section. */
1004 bfd* abfd;
1005
1006 /* The CU of the DIE we are parsing. */
1007 struct dwarf2_cu *cu;
1008
1009 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1010 struct dwo_file *dwo_file;
1011
1012 /* The section the die comes from.
1013 This is either .debug_info or .debug_types, or the .dwo variants. */
1014 struct dwarf2_section_info *die_section;
1015
1016 /* die_section->buffer. */
1017 const gdb_byte *buffer;
1018
1019 /* The end of the buffer. */
1020 const gdb_byte *buffer_end;
1021
1022 /* The value of the DW_AT_comp_dir attribute. */
1023 const char *comp_dir;
1024 };
1025
1026 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1027 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1028 const gdb_byte *info_ptr,
1029 struct die_info *comp_unit_die,
1030 int has_children,
1031 void *data);
1032
1033 struct file_entry
1034 {
1035 const char *name;
1036 unsigned int dir_index;
1037 unsigned int mod_time;
1038 unsigned int length;
1039 /* Non-zero if referenced by the Line Number Program. */
1040 int included_p;
1041 /* The associated symbol table, if any. */
1042 struct symtab *symtab;
1043 };
1044
1045 /* The line number information for a compilation unit (found in the
1046 .debug_line section) begins with a "statement program header",
1047 which contains the following information. */
1048 struct line_header
1049 {
1050 /* Offset of line number information in .debug_line section. */
1051 sect_offset offset;
1052
1053 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1054 unsigned offset_in_dwz : 1;
1055
1056 unsigned int total_length;
1057 unsigned short version;
1058 unsigned int header_length;
1059 unsigned char minimum_instruction_length;
1060 unsigned char maximum_ops_per_instruction;
1061 unsigned char default_is_stmt;
1062 int line_base;
1063 unsigned char line_range;
1064 unsigned char opcode_base;
1065
1066 /* standard_opcode_lengths[i] is the number of operands for the
1067 standard opcode whose value is i. This means that
1068 standard_opcode_lengths[0] is unused, and the last meaningful
1069 element is standard_opcode_lengths[opcode_base - 1]. */
1070 unsigned char *standard_opcode_lengths;
1071
1072 /* The include_directories table. NOTE! These strings are not
1073 allocated with xmalloc; instead, they are pointers into
1074 debug_line_buffer. If you try to free them, `free' will get
1075 indigestion. */
1076 unsigned int num_include_dirs, include_dirs_size;
1077 const char **include_dirs;
1078
1079 /* The file_names table. NOTE! These strings are not allocated
1080 with xmalloc; instead, they are pointers into debug_line_buffer.
1081 Don't try to free them directly. */
1082 unsigned int num_file_names, file_names_size;
1083 struct file_entry *file_names;
1084
1085 /* The start and end of the statement program following this
1086 header. These point into dwarf2_per_objfile->line_buffer. */
1087 const gdb_byte *statement_program_start, *statement_program_end;
1088 };
1089
1090 /* When we construct a partial symbol table entry we only
1091 need this much information. */
1092 struct partial_die_info
1093 {
1094 /* Offset of this DIE. */
1095 sect_offset offset;
1096
1097 /* DWARF-2 tag for this DIE. */
1098 ENUM_BITFIELD(dwarf_tag) tag : 16;
1099
1100 /* Assorted flags describing the data found in this DIE. */
1101 unsigned int has_children : 1;
1102 unsigned int is_external : 1;
1103 unsigned int is_declaration : 1;
1104 unsigned int has_type : 1;
1105 unsigned int has_specification : 1;
1106 unsigned int has_pc_info : 1;
1107 unsigned int may_be_inlined : 1;
1108
1109 /* Flag set if the SCOPE field of this structure has been
1110 computed. */
1111 unsigned int scope_set : 1;
1112
1113 /* Flag set if the DIE has a byte_size attribute. */
1114 unsigned int has_byte_size : 1;
1115
1116 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1117 unsigned int has_const_value : 1;
1118
1119 /* Flag set if any of the DIE's children are template arguments. */
1120 unsigned int has_template_arguments : 1;
1121
1122 /* Flag set if fixup_partial_die has been called on this die. */
1123 unsigned int fixup_called : 1;
1124
1125 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1126 unsigned int is_dwz : 1;
1127
1128 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1129 unsigned int spec_is_dwz : 1;
1130
1131 /* The name of this DIE. Normally the value of DW_AT_name, but
1132 sometimes a default name for unnamed DIEs. */
1133 const char *name;
1134
1135 /* The linkage name, if present. */
1136 const char *linkage_name;
1137
1138 /* The scope to prepend to our children. This is generally
1139 allocated on the comp_unit_obstack, so will disappear
1140 when this compilation unit leaves the cache. */
1141 const char *scope;
1142
1143 /* Some data associated with the partial DIE. The tag determines
1144 which field is live. */
1145 union
1146 {
1147 /* The location description associated with this DIE, if any. */
1148 struct dwarf_block *locdesc;
1149 /* The offset of an import, for DW_TAG_imported_unit. */
1150 sect_offset offset;
1151 } d;
1152
1153 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1154 CORE_ADDR lowpc;
1155 CORE_ADDR highpc;
1156
1157 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1158 DW_AT_sibling, if any. */
1159 /* NOTE: This member isn't strictly necessary, read_partial_die could
1160 return DW_AT_sibling values to its caller load_partial_dies. */
1161 const gdb_byte *sibling;
1162
1163 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1164 DW_AT_specification (or DW_AT_abstract_origin or
1165 DW_AT_extension). */
1166 sect_offset spec_offset;
1167
1168 /* Pointers to this DIE's parent, first child, and next sibling,
1169 if any. */
1170 struct partial_die_info *die_parent, *die_child, *die_sibling;
1171 };
1172
1173 /* This data structure holds the information of an abbrev. */
1174 struct abbrev_info
1175 {
1176 unsigned int number; /* number identifying abbrev */
1177 enum dwarf_tag tag; /* dwarf tag */
1178 unsigned short has_children; /* boolean */
1179 unsigned short num_attrs; /* number of attributes */
1180 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1181 struct abbrev_info *next; /* next in chain */
1182 };
1183
1184 struct attr_abbrev
1185 {
1186 ENUM_BITFIELD(dwarf_attribute) name : 16;
1187 ENUM_BITFIELD(dwarf_form) form : 16;
1188 };
1189
1190 /* Size of abbrev_table.abbrev_hash_table. */
1191 #define ABBREV_HASH_SIZE 121
1192
1193 /* Top level data structure to contain an abbreviation table. */
1194
1195 struct abbrev_table
1196 {
1197 /* Where the abbrev table came from.
1198 This is used as a sanity check when the table is used. */
1199 sect_offset offset;
1200
1201 /* Storage for the abbrev table. */
1202 struct obstack abbrev_obstack;
1203
1204 /* Hash table of abbrevs.
1205 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1206 It could be statically allocated, but the previous code didn't so we
1207 don't either. */
1208 struct abbrev_info **abbrevs;
1209 };
1210
1211 /* Attributes have a name and a value. */
1212 struct attribute
1213 {
1214 ENUM_BITFIELD(dwarf_attribute) name : 16;
1215 ENUM_BITFIELD(dwarf_form) form : 15;
1216
1217 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1218 field should be in u.str (existing only for DW_STRING) but it is kept
1219 here for better struct attribute alignment. */
1220 unsigned int string_is_canonical : 1;
1221
1222 union
1223 {
1224 const char *str;
1225 struct dwarf_block *blk;
1226 ULONGEST unsnd;
1227 LONGEST snd;
1228 CORE_ADDR addr;
1229 ULONGEST signature;
1230 }
1231 u;
1232 };
1233
1234 /* This data structure holds a complete die structure. */
1235 struct die_info
1236 {
1237 /* DWARF-2 tag for this DIE. */
1238 ENUM_BITFIELD(dwarf_tag) tag : 16;
1239
1240 /* Number of attributes */
1241 unsigned char num_attrs;
1242
1243 /* True if we're presently building the full type name for the
1244 type derived from this DIE. */
1245 unsigned char building_fullname : 1;
1246
1247 /* True if this die is in process. PR 16581. */
1248 unsigned char in_process : 1;
1249
1250 /* Abbrev number */
1251 unsigned int abbrev;
1252
1253 /* Offset in .debug_info or .debug_types section. */
1254 sect_offset offset;
1255
1256 /* The dies in a compilation unit form an n-ary tree. PARENT
1257 points to this die's parent; CHILD points to the first child of
1258 this node; and all the children of a given node are chained
1259 together via their SIBLING fields. */
1260 struct die_info *child; /* Its first child, if any. */
1261 struct die_info *sibling; /* Its next sibling, if any. */
1262 struct die_info *parent; /* Its parent, if any. */
1263
1264 /* An array of attributes, with NUM_ATTRS elements. There may be
1265 zero, but it's not common and zero-sized arrays are not
1266 sufficiently portable C. */
1267 struct attribute attrs[1];
1268 };
1269
1270 /* Get at parts of an attribute structure. */
1271
1272 #define DW_STRING(attr) ((attr)->u.str)
1273 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1274 #define DW_UNSND(attr) ((attr)->u.unsnd)
1275 #define DW_BLOCK(attr) ((attr)->u.blk)
1276 #define DW_SND(attr) ((attr)->u.snd)
1277 #define DW_ADDR(attr) ((attr)->u.addr)
1278 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1279
1280 /* Blocks are a bunch of untyped bytes. */
1281 struct dwarf_block
1282 {
1283 size_t size;
1284
1285 /* Valid only if SIZE is not zero. */
1286 const gdb_byte *data;
1287 };
1288
1289 #ifndef ATTR_ALLOC_CHUNK
1290 #define ATTR_ALLOC_CHUNK 4
1291 #endif
1292
1293 /* Allocate fields for structs, unions and enums in this size. */
1294 #ifndef DW_FIELD_ALLOC_CHUNK
1295 #define DW_FIELD_ALLOC_CHUNK 4
1296 #endif
1297
1298 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1299 but this would require a corresponding change in unpack_field_as_long
1300 and friends. */
1301 static int bits_per_byte = 8;
1302
1303 struct nextfield
1304 {
1305 struct nextfield *next;
1306 int accessibility;
1307 int virtuality;
1308 struct field field;
1309 };
1310
1311 struct nextfnfield
1312 {
1313 struct nextfnfield *next;
1314 struct fn_field fnfield;
1315 };
1316
1317 struct fnfieldlist
1318 {
1319 const char *name;
1320 int length;
1321 struct nextfnfield *head;
1322 };
1323
1324 struct typedef_field_list
1325 {
1326 struct typedef_field field;
1327 struct typedef_field_list *next;
1328 };
1329
1330 /* The routines that read and process dies for a C struct or C++ class
1331 pass lists of data member fields and lists of member function fields
1332 in an instance of a field_info structure, as defined below. */
1333 struct field_info
1334 {
1335 /* List of data member and baseclasses fields. */
1336 struct nextfield *fields, *baseclasses;
1337
1338 /* Number of fields (including baseclasses). */
1339 int nfields;
1340
1341 /* Number of baseclasses. */
1342 int nbaseclasses;
1343
1344 /* Set if the accesibility of one of the fields is not public. */
1345 int non_public_fields;
1346
1347 /* Member function fields array, entries are allocated in the order they
1348 are encountered in the object file. */
1349 struct nextfnfield *fnfields;
1350
1351 /* Member function fieldlist array, contains name of possibly overloaded
1352 member function, number of overloaded member functions and a pointer
1353 to the head of the member function field chain. */
1354 struct fnfieldlist *fnfieldlists;
1355
1356 /* Number of entries in the fnfieldlists array. */
1357 int nfnfields;
1358
1359 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1360 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1361 struct typedef_field_list *typedef_field_list;
1362 unsigned typedef_field_list_count;
1363 };
1364
1365 /* One item on the queue of compilation units to read in full symbols
1366 for. */
1367 struct dwarf2_queue_item
1368 {
1369 struct dwarf2_per_cu_data *per_cu;
1370 enum language pretend_language;
1371 struct dwarf2_queue_item *next;
1372 };
1373
1374 /* The current queue. */
1375 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1376
1377 /* Loaded secondary compilation units are kept in memory until they
1378 have not been referenced for the processing of this many
1379 compilation units. Set this to zero to disable caching. Cache
1380 sizes of up to at least twenty will improve startup time for
1381 typical inter-CU-reference binaries, at an obvious memory cost. */
1382 static int dwarf_max_cache_age = 5;
1383 static void
1384 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1385 struct cmd_list_element *c, const char *value)
1386 {
1387 fprintf_filtered (file, _("The upper bound on the age of cached "
1388 "DWARF compilation units is %s.\n"),
1389 value);
1390 }
1391 \f
1392 /* local function prototypes */
1393
1394 static const char *get_section_name (const struct dwarf2_section_info *);
1395
1396 static const char *get_section_file_name (const struct dwarf2_section_info *);
1397
1398 static void dwarf2_locate_sections (bfd *, asection *, void *);
1399
1400 static void dwarf2_find_base_address (struct die_info *die,
1401 struct dwarf2_cu *cu);
1402
1403 static struct partial_symtab *create_partial_symtab
1404 (struct dwarf2_per_cu_data *per_cu, const char *name);
1405
1406 static void dwarf2_build_psymtabs_hard (struct objfile *);
1407
1408 static void scan_partial_symbols (struct partial_die_info *,
1409 CORE_ADDR *, CORE_ADDR *,
1410 int, struct dwarf2_cu *);
1411
1412 static void add_partial_symbol (struct partial_die_info *,
1413 struct dwarf2_cu *);
1414
1415 static void add_partial_namespace (struct partial_die_info *pdi,
1416 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1417 int set_addrmap, struct dwarf2_cu *cu);
1418
1419 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1420 CORE_ADDR *highpc, int set_addrmap,
1421 struct dwarf2_cu *cu);
1422
1423 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1424 struct dwarf2_cu *cu);
1425
1426 static void add_partial_subprogram (struct partial_die_info *pdi,
1427 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1428 int need_pc, struct dwarf2_cu *cu);
1429
1430 static void dwarf2_read_symtab (struct partial_symtab *,
1431 struct objfile *);
1432
1433 static void psymtab_to_symtab_1 (struct partial_symtab *);
1434
1435 static struct abbrev_info *abbrev_table_lookup_abbrev
1436 (const struct abbrev_table *, unsigned int);
1437
1438 static struct abbrev_table *abbrev_table_read_table
1439 (struct dwarf2_section_info *, sect_offset);
1440
1441 static void abbrev_table_free (struct abbrev_table *);
1442
1443 static void abbrev_table_free_cleanup (void *);
1444
1445 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1446 struct dwarf2_section_info *);
1447
1448 static void dwarf2_free_abbrev_table (void *);
1449
1450 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1451
1452 static struct partial_die_info *load_partial_dies
1453 (const struct die_reader_specs *, const gdb_byte *, int);
1454
1455 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1456 struct partial_die_info *,
1457 struct abbrev_info *,
1458 unsigned int,
1459 const gdb_byte *);
1460
1461 static struct partial_die_info *find_partial_die (sect_offset, int,
1462 struct dwarf2_cu *);
1463
1464 static void fixup_partial_die (struct partial_die_info *,
1465 struct dwarf2_cu *);
1466
1467 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1468 struct attribute *, struct attr_abbrev *,
1469 const gdb_byte *);
1470
1471 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1472
1473 static int read_1_signed_byte (bfd *, const gdb_byte *);
1474
1475 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1476
1477 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1478
1479 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1480
1481 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1482 unsigned int *);
1483
1484 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1485
1486 static LONGEST read_checked_initial_length_and_offset
1487 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1488 unsigned int *, unsigned int *);
1489
1490 static LONGEST read_offset (bfd *, const gdb_byte *,
1491 const struct comp_unit_head *,
1492 unsigned int *);
1493
1494 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1495
1496 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1497 sect_offset);
1498
1499 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1500
1501 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1502
1503 static const char *read_indirect_string (bfd *, const gdb_byte *,
1504 const struct comp_unit_head *,
1505 unsigned int *);
1506
1507 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1508
1509 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
1510
1511 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1512
1513 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1514 const gdb_byte *,
1515 unsigned int *);
1516
1517 static const char *read_str_index (const struct die_reader_specs *reader,
1518 ULONGEST str_index);
1519
1520 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1521
1522 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1523 struct dwarf2_cu *);
1524
1525 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1526 unsigned int);
1527
1528 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1529 struct dwarf2_cu *cu);
1530
1531 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1532 struct dwarf2_cu *cu);
1533
1534 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1535
1536 static struct die_info *die_specification (struct die_info *die,
1537 struct dwarf2_cu **);
1538
1539 static void free_line_header (struct line_header *lh);
1540
1541 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1542 struct dwarf2_cu *cu);
1543
1544 static void dwarf_decode_lines (struct line_header *, const char *,
1545 struct dwarf2_cu *, struct partial_symtab *,
1546 CORE_ADDR, int decode_mapping);
1547
1548 static void dwarf2_start_subfile (const char *, const char *);
1549
1550 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1551 const char *, const char *,
1552 CORE_ADDR);
1553
1554 static struct symbol *new_symbol (struct die_info *, struct type *,
1555 struct dwarf2_cu *);
1556
1557 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1558 struct dwarf2_cu *, struct symbol *);
1559
1560 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1561 struct dwarf2_cu *);
1562
1563 static void dwarf2_const_value_attr (const struct attribute *attr,
1564 struct type *type,
1565 const char *name,
1566 struct obstack *obstack,
1567 struct dwarf2_cu *cu, LONGEST *value,
1568 const gdb_byte **bytes,
1569 struct dwarf2_locexpr_baton **baton);
1570
1571 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1572
1573 static int need_gnat_info (struct dwarf2_cu *);
1574
1575 static struct type *die_descriptive_type (struct die_info *,
1576 struct dwarf2_cu *);
1577
1578 static void set_descriptive_type (struct type *, struct die_info *,
1579 struct dwarf2_cu *);
1580
1581 static struct type *die_containing_type (struct die_info *,
1582 struct dwarf2_cu *);
1583
1584 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1585 struct dwarf2_cu *);
1586
1587 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1588
1589 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1590
1591 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1592
1593 static char *typename_concat (struct obstack *obs, const char *prefix,
1594 const char *suffix, int physname,
1595 struct dwarf2_cu *cu);
1596
1597 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1598
1599 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1600
1601 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1602
1603 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1604
1605 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1606
1607 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1608 struct dwarf2_cu *, struct partial_symtab *);
1609
1610 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1611 values. Keep the items ordered with increasing constraints compliance. */
1612 enum pc_bounds_kind
1613 {
1614 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1615 PC_BOUNDS_NOT_PRESENT,
1616
1617 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1618 were present but they do not form a valid range of PC addresses. */
1619 PC_BOUNDS_INVALID,
1620
1621 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1622 PC_BOUNDS_RANGES,
1623
1624 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1625 PC_BOUNDS_HIGH_LOW,
1626 };
1627
1628 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1629 CORE_ADDR *, CORE_ADDR *,
1630 struct dwarf2_cu *,
1631 struct partial_symtab *);
1632
1633 static void get_scope_pc_bounds (struct die_info *,
1634 CORE_ADDR *, CORE_ADDR *,
1635 struct dwarf2_cu *);
1636
1637 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1638 CORE_ADDR, struct dwarf2_cu *);
1639
1640 static void dwarf2_add_field (struct field_info *, struct die_info *,
1641 struct dwarf2_cu *);
1642
1643 static void dwarf2_attach_fields_to_type (struct field_info *,
1644 struct type *, struct dwarf2_cu *);
1645
1646 static void dwarf2_add_member_fn (struct field_info *,
1647 struct die_info *, struct type *,
1648 struct dwarf2_cu *);
1649
1650 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1651 struct type *,
1652 struct dwarf2_cu *);
1653
1654 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1655
1656 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1657
1658 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1659
1660 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1661
1662 static struct using_direct **using_directives (enum language);
1663
1664 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1665
1666 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1667
1668 static struct type *read_module_type (struct die_info *die,
1669 struct dwarf2_cu *cu);
1670
1671 static const char *namespace_name (struct die_info *die,
1672 int *is_anonymous, struct dwarf2_cu *);
1673
1674 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1675
1676 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1677
1678 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1679 struct dwarf2_cu *);
1680
1681 static struct die_info *read_die_and_siblings_1
1682 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1683 struct die_info *);
1684
1685 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1686 const gdb_byte *info_ptr,
1687 const gdb_byte **new_info_ptr,
1688 struct die_info *parent);
1689
1690 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1691 struct die_info **, const gdb_byte *,
1692 int *, int);
1693
1694 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1695 struct die_info **, const gdb_byte *,
1696 int *);
1697
1698 static void process_die (struct die_info *, struct dwarf2_cu *);
1699
1700 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1701 struct obstack *);
1702
1703 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1704
1705 static const char *dwarf2_full_name (const char *name,
1706 struct die_info *die,
1707 struct dwarf2_cu *cu);
1708
1709 static const char *dwarf2_physname (const char *name, struct die_info *die,
1710 struct dwarf2_cu *cu);
1711
1712 static struct die_info *dwarf2_extension (struct die_info *die,
1713 struct dwarf2_cu **);
1714
1715 static const char *dwarf_tag_name (unsigned int);
1716
1717 static const char *dwarf_attr_name (unsigned int);
1718
1719 static const char *dwarf_form_name (unsigned int);
1720
1721 static char *dwarf_bool_name (unsigned int);
1722
1723 static const char *dwarf_type_encoding_name (unsigned int);
1724
1725 static struct die_info *sibling_die (struct die_info *);
1726
1727 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1728
1729 static void dump_die_for_error (struct die_info *);
1730
1731 static void dump_die_1 (struct ui_file *, int level, int max_level,
1732 struct die_info *);
1733
1734 /*static*/ void dump_die (struct die_info *, int max_level);
1735
1736 static void store_in_ref_table (struct die_info *,
1737 struct dwarf2_cu *);
1738
1739 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1740
1741 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1742
1743 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1744 const struct attribute *,
1745 struct dwarf2_cu **);
1746
1747 static struct die_info *follow_die_ref (struct die_info *,
1748 const struct attribute *,
1749 struct dwarf2_cu **);
1750
1751 static struct die_info *follow_die_sig (struct die_info *,
1752 const struct attribute *,
1753 struct dwarf2_cu **);
1754
1755 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1756 struct dwarf2_cu *);
1757
1758 static struct type *get_DW_AT_signature_type (struct die_info *,
1759 const struct attribute *,
1760 struct dwarf2_cu *);
1761
1762 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1763
1764 static void read_signatured_type (struct signatured_type *);
1765
1766 static int attr_to_dynamic_prop (const struct attribute *attr,
1767 struct die_info *die, struct dwarf2_cu *cu,
1768 struct dynamic_prop *prop);
1769
1770 /* memory allocation interface */
1771
1772 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1773
1774 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1775
1776 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1777
1778 static int attr_form_is_block (const struct attribute *);
1779
1780 static int attr_form_is_section_offset (const struct attribute *);
1781
1782 static int attr_form_is_constant (const struct attribute *);
1783
1784 static int attr_form_is_ref (const struct attribute *);
1785
1786 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1787 struct dwarf2_loclist_baton *baton,
1788 const struct attribute *attr);
1789
1790 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1791 struct symbol *sym,
1792 struct dwarf2_cu *cu,
1793 int is_block);
1794
1795 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1796 const gdb_byte *info_ptr,
1797 struct abbrev_info *abbrev);
1798
1799 static void free_stack_comp_unit (void *);
1800
1801 static hashval_t partial_die_hash (const void *item);
1802
1803 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1804
1805 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1806 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1807
1808 static void init_one_comp_unit (struct dwarf2_cu *cu,
1809 struct dwarf2_per_cu_data *per_cu);
1810
1811 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1812 struct die_info *comp_unit_die,
1813 enum language pretend_language);
1814
1815 static void free_heap_comp_unit (void *);
1816
1817 static void free_cached_comp_units (void *);
1818
1819 static void age_cached_comp_units (void);
1820
1821 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1822
1823 static struct type *set_die_type (struct die_info *, struct type *,
1824 struct dwarf2_cu *);
1825
1826 static void create_all_comp_units (struct objfile *);
1827
1828 static int create_all_type_units (struct objfile *);
1829
1830 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1831 enum language);
1832
1833 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1834 enum language);
1835
1836 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1837 enum language);
1838
1839 static void dwarf2_add_dependence (struct dwarf2_cu *,
1840 struct dwarf2_per_cu_data *);
1841
1842 static void dwarf2_mark (struct dwarf2_cu *);
1843
1844 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1845
1846 static struct type *get_die_type_at_offset (sect_offset,
1847 struct dwarf2_per_cu_data *);
1848
1849 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1850
1851 static void dwarf2_release_queue (void *dummy);
1852
1853 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1854 enum language pretend_language);
1855
1856 static void process_queue (void);
1857
1858 static void find_file_and_directory (struct die_info *die,
1859 struct dwarf2_cu *cu,
1860 const char **name, const char **comp_dir);
1861
1862 static char *file_full_name (int file, struct line_header *lh,
1863 const char *comp_dir);
1864
1865 static const gdb_byte *read_and_check_comp_unit_head
1866 (struct comp_unit_head *header,
1867 struct dwarf2_section_info *section,
1868 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1869 int is_debug_types_section);
1870
1871 static void init_cutu_and_read_dies
1872 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1873 int use_existing_cu, int keep,
1874 die_reader_func_ftype *die_reader_func, void *data);
1875
1876 static void init_cutu_and_read_dies_simple
1877 (struct dwarf2_per_cu_data *this_cu,
1878 die_reader_func_ftype *die_reader_func, void *data);
1879
1880 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1881
1882 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1883
1884 static struct dwo_unit *lookup_dwo_unit_in_dwp
1885 (struct dwp_file *dwp_file, const char *comp_dir,
1886 ULONGEST signature, int is_debug_types);
1887
1888 static struct dwp_file *get_dwp_file (void);
1889
1890 static struct dwo_unit *lookup_dwo_comp_unit
1891 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1892
1893 static struct dwo_unit *lookup_dwo_type_unit
1894 (struct signatured_type *, const char *, const char *);
1895
1896 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1897
1898 static void free_dwo_file_cleanup (void *);
1899
1900 static void process_cu_includes (void);
1901
1902 static void check_producer (struct dwarf2_cu *cu);
1903
1904 static void free_line_header_voidp (void *arg);
1905 \f
1906 /* Various complaints about symbol reading that don't abort the process. */
1907
1908 static void
1909 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1910 {
1911 complaint (&symfile_complaints,
1912 _("statement list doesn't fit in .debug_line section"));
1913 }
1914
1915 static void
1916 dwarf2_debug_line_missing_file_complaint (void)
1917 {
1918 complaint (&symfile_complaints,
1919 _(".debug_line section has line data without a file"));
1920 }
1921
1922 static void
1923 dwarf2_debug_line_missing_end_sequence_complaint (void)
1924 {
1925 complaint (&symfile_complaints,
1926 _(".debug_line section has line "
1927 "program sequence without an end"));
1928 }
1929
1930 static void
1931 dwarf2_complex_location_expr_complaint (void)
1932 {
1933 complaint (&symfile_complaints, _("location expression too complex"));
1934 }
1935
1936 static void
1937 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1938 int arg3)
1939 {
1940 complaint (&symfile_complaints,
1941 _("const value length mismatch for '%s', got %d, expected %d"),
1942 arg1, arg2, arg3);
1943 }
1944
1945 static void
1946 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1947 {
1948 complaint (&symfile_complaints,
1949 _("debug info runs off end of %s section"
1950 " [in module %s]"),
1951 get_section_name (section),
1952 get_section_file_name (section));
1953 }
1954
1955 static void
1956 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1957 {
1958 complaint (&symfile_complaints,
1959 _("macro debug info contains a "
1960 "malformed macro definition:\n`%s'"),
1961 arg1);
1962 }
1963
1964 static void
1965 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1966 {
1967 complaint (&symfile_complaints,
1968 _("invalid attribute class or form for '%s' in '%s'"),
1969 arg1, arg2);
1970 }
1971
1972 /* Hash function for line_header_hash. */
1973
1974 static hashval_t
1975 line_header_hash (const struct line_header *ofs)
1976 {
1977 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
1978 }
1979
1980 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1981
1982 static hashval_t
1983 line_header_hash_voidp (const void *item)
1984 {
1985 const struct line_header *ofs = (const struct line_header *) item;
1986
1987 return line_header_hash (ofs);
1988 }
1989
1990 /* Equality function for line_header_hash. */
1991
1992 static int
1993 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1994 {
1995 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1996 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1997
1998 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
1999 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2000 }
2001
2002 \f
2003 #if WORDS_BIGENDIAN
2004
2005 /* Convert VALUE between big- and little-endian. */
2006 static offset_type
2007 byte_swap (offset_type value)
2008 {
2009 offset_type result;
2010
2011 result = (value & 0xff) << 24;
2012 result |= (value & 0xff00) << 8;
2013 result |= (value & 0xff0000) >> 8;
2014 result |= (value & 0xff000000) >> 24;
2015 return result;
2016 }
2017
2018 #define MAYBE_SWAP(V) byte_swap (V)
2019
2020 #else
2021 #define MAYBE_SWAP(V) (V)
2022 #endif /* WORDS_BIGENDIAN */
2023
2024 /* Read the given attribute value as an address, taking the attribute's
2025 form into account. */
2026
2027 static CORE_ADDR
2028 attr_value_as_address (struct attribute *attr)
2029 {
2030 CORE_ADDR addr;
2031
2032 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2033 {
2034 /* Aside from a few clearly defined exceptions, attributes that
2035 contain an address must always be in DW_FORM_addr form.
2036 Unfortunately, some compilers happen to be violating this
2037 requirement by encoding addresses using other forms, such
2038 as DW_FORM_data4 for example. For those broken compilers,
2039 we try to do our best, without any guarantee of success,
2040 to interpret the address correctly. It would also be nice
2041 to generate a complaint, but that would require us to maintain
2042 a list of legitimate cases where a non-address form is allowed,
2043 as well as update callers to pass in at least the CU's DWARF
2044 version. This is more overhead than what we're willing to
2045 expand for a pretty rare case. */
2046 addr = DW_UNSND (attr);
2047 }
2048 else
2049 addr = DW_ADDR (attr);
2050
2051 return addr;
2052 }
2053
2054 /* The suffix for an index file. */
2055 #define INDEX_SUFFIX ".gdb-index"
2056
2057 /* Try to locate the sections we need for DWARF 2 debugging
2058 information and return true if we have enough to do something.
2059 NAMES points to the dwarf2 section names, or is NULL if the standard
2060 ELF names are used. */
2061
2062 int
2063 dwarf2_has_info (struct objfile *objfile,
2064 const struct dwarf2_debug_sections *names)
2065 {
2066 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2067 objfile_data (objfile, dwarf2_objfile_data_key));
2068 if (!dwarf2_per_objfile)
2069 {
2070 /* Initialize per-objfile state. */
2071 struct dwarf2_per_objfile *data
2072 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2073
2074 memset (data, 0, sizeof (*data));
2075 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2076 dwarf2_per_objfile = data;
2077
2078 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2079 (void *) names);
2080 dwarf2_per_objfile->objfile = objfile;
2081 }
2082 return (!dwarf2_per_objfile->info.is_virtual
2083 && dwarf2_per_objfile->info.s.section != NULL
2084 && !dwarf2_per_objfile->abbrev.is_virtual
2085 && dwarf2_per_objfile->abbrev.s.section != NULL);
2086 }
2087
2088 /* Return the containing section of virtual section SECTION. */
2089
2090 static struct dwarf2_section_info *
2091 get_containing_section (const struct dwarf2_section_info *section)
2092 {
2093 gdb_assert (section->is_virtual);
2094 return section->s.containing_section;
2095 }
2096
2097 /* Return the bfd owner of SECTION. */
2098
2099 static struct bfd *
2100 get_section_bfd_owner (const struct dwarf2_section_info *section)
2101 {
2102 if (section->is_virtual)
2103 {
2104 section = get_containing_section (section);
2105 gdb_assert (!section->is_virtual);
2106 }
2107 return section->s.section->owner;
2108 }
2109
2110 /* Return the bfd section of SECTION.
2111 Returns NULL if the section is not present. */
2112
2113 static asection *
2114 get_section_bfd_section (const struct dwarf2_section_info *section)
2115 {
2116 if (section->is_virtual)
2117 {
2118 section = get_containing_section (section);
2119 gdb_assert (!section->is_virtual);
2120 }
2121 return section->s.section;
2122 }
2123
2124 /* Return the name of SECTION. */
2125
2126 static const char *
2127 get_section_name (const struct dwarf2_section_info *section)
2128 {
2129 asection *sectp = get_section_bfd_section (section);
2130
2131 gdb_assert (sectp != NULL);
2132 return bfd_section_name (get_section_bfd_owner (section), sectp);
2133 }
2134
2135 /* Return the name of the file SECTION is in. */
2136
2137 static const char *
2138 get_section_file_name (const struct dwarf2_section_info *section)
2139 {
2140 bfd *abfd = get_section_bfd_owner (section);
2141
2142 return bfd_get_filename (abfd);
2143 }
2144
2145 /* Return the id of SECTION.
2146 Returns 0 if SECTION doesn't exist. */
2147
2148 static int
2149 get_section_id (const struct dwarf2_section_info *section)
2150 {
2151 asection *sectp = get_section_bfd_section (section);
2152
2153 if (sectp == NULL)
2154 return 0;
2155 return sectp->id;
2156 }
2157
2158 /* Return the flags of SECTION.
2159 SECTION (or containing section if this is a virtual section) must exist. */
2160
2161 static int
2162 get_section_flags (const struct dwarf2_section_info *section)
2163 {
2164 asection *sectp = get_section_bfd_section (section);
2165
2166 gdb_assert (sectp != NULL);
2167 return bfd_get_section_flags (sectp->owner, sectp);
2168 }
2169
2170 /* When loading sections, we look either for uncompressed section or for
2171 compressed section names. */
2172
2173 static int
2174 section_is_p (const char *section_name,
2175 const struct dwarf2_section_names *names)
2176 {
2177 if (names->normal != NULL
2178 && strcmp (section_name, names->normal) == 0)
2179 return 1;
2180 if (names->compressed != NULL
2181 && strcmp (section_name, names->compressed) == 0)
2182 return 1;
2183 return 0;
2184 }
2185
2186 /* This function is mapped across the sections and remembers the
2187 offset and size of each of the debugging sections we are interested
2188 in. */
2189
2190 static void
2191 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
2192 {
2193 const struct dwarf2_debug_sections *names;
2194 flagword aflag = bfd_get_section_flags (abfd, sectp);
2195
2196 if (vnames == NULL)
2197 names = &dwarf2_elf_names;
2198 else
2199 names = (const struct dwarf2_debug_sections *) vnames;
2200
2201 if ((aflag & SEC_HAS_CONTENTS) == 0)
2202 {
2203 }
2204 else if (section_is_p (sectp->name, &names->info))
2205 {
2206 dwarf2_per_objfile->info.s.section = sectp;
2207 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
2208 }
2209 else if (section_is_p (sectp->name, &names->abbrev))
2210 {
2211 dwarf2_per_objfile->abbrev.s.section = sectp;
2212 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
2213 }
2214 else if (section_is_p (sectp->name, &names->line))
2215 {
2216 dwarf2_per_objfile->line.s.section = sectp;
2217 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
2218 }
2219 else if (section_is_p (sectp->name, &names->loc))
2220 {
2221 dwarf2_per_objfile->loc.s.section = sectp;
2222 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
2223 }
2224 else if (section_is_p (sectp->name, &names->macinfo))
2225 {
2226 dwarf2_per_objfile->macinfo.s.section = sectp;
2227 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
2228 }
2229 else if (section_is_p (sectp->name, &names->macro))
2230 {
2231 dwarf2_per_objfile->macro.s.section = sectp;
2232 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2233 }
2234 else if (section_is_p (sectp->name, &names->str))
2235 {
2236 dwarf2_per_objfile->str.s.section = sectp;
2237 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
2238 }
2239 else if (section_is_p (sectp->name, &names->addr))
2240 {
2241 dwarf2_per_objfile->addr.s.section = sectp;
2242 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2243 }
2244 else if (section_is_p (sectp->name, &names->frame))
2245 {
2246 dwarf2_per_objfile->frame.s.section = sectp;
2247 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
2248 }
2249 else if (section_is_p (sectp->name, &names->eh_frame))
2250 {
2251 dwarf2_per_objfile->eh_frame.s.section = sectp;
2252 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
2253 }
2254 else if (section_is_p (sectp->name, &names->ranges))
2255 {
2256 dwarf2_per_objfile->ranges.s.section = sectp;
2257 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
2258 }
2259 else if (section_is_p (sectp->name, &names->types))
2260 {
2261 struct dwarf2_section_info type_section;
2262
2263 memset (&type_section, 0, sizeof (type_section));
2264 type_section.s.section = sectp;
2265 type_section.size = bfd_get_section_size (sectp);
2266
2267 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2268 &type_section);
2269 }
2270 else if (section_is_p (sectp->name, &names->gdb_index))
2271 {
2272 dwarf2_per_objfile->gdb_index.s.section = sectp;
2273 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2274 }
2275
2276 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2277 && bfd_section_vma (abfd, sectp) == 0)
2278 dwarf2_per_objfile->has_section_at_zero = 1;
2279 }
2280
2281 /* A helper function that decides whether a section is empty,
2282 or not present. */
2283
2284 static int
2285 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2286 {
2287 if (section->is_virtual)
2288 return section->size == 0;
2289 return section->s.section == NULL || section->size == 0;
2290 }
2291
2292 /* Read the contents of the section INFO.
2293 OBJFILE is the main object file, but not necessarily the file where
2294 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2295 of the DWO file.
2296 If the section is compressed, uncompress it before returning. */
2297
2298 static void
2299 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2300 {
2301 asection *sectp;
2302 bfd *abfd;
2303 gdb_byte *buf, *retbuf;
2304
2305 if (info->readin)
2306 return;
2307 info->buffer = NULL;
2308 info->readin = 1;
2309
2310 if (dwarf2_section_empty_p (info))
2311 return;
2312
2313 sectp = get_section_bfd_section (info);
2314
2315 /* If this is a virtual section we need to read in the real one first. */
2316 if (info->is_virtual)
2317 {
2318 struct dwarf2_section_info *containing_section =
2319 get_containing_section (info);
2320
2321 gdb_assert (sectp != NULL);
2322 if ((sectp->flags & SEC_RELOC) != 0)
2323 {
2324 error (_("Dwarf Error: DWP format V2 with relocations is not"
2325 " supported in section %s [in module %s]"),
2326 get_section_name (info), get_section_file_name (info));
2327 }
2328 dwarf2_read_section (objfile, containing_section);
2329 /* Other code should have already caught virtual sections that don't
2330 fit. */
2331 gdb_assert (info->virtual_offset + info->size
2332 <= containing_section->size);
2333 /* If the real section is empty or there was a problem reading the
2334 section we shouldn't get here. */
2335 gdb_assert (containing_section->buffer != NULL);
2336 info->buffer = containing_section->buffer + info->virtual_offset;
2337 return;
2338 }
2339
2340 /* If the section has relocations, we must read it ourselves.
2341 Otherwise we attach it to the BFD. */
2342 if ((sectp->flags & SEC_RELOC) == 0)
2343 {
2344 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2345 return;
2346 }
2347
2348 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2349 info->buffer = buf;
2350
2351 /* When debugging .o files, we may need to apply relocations; see
2352 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2353 We never compress sections in .o files, so we only need to
2354 try this when the section is not compressed. */
2355 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2356 if (retbuf != NULL)
2357 {
2358 info->buffer = retbuf;
2359 return;
2360 }
2361
2362 abfd = get_section_bfd_owner (info);
2363 gdb_assert (abfd != NULL);
2364
2365 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2366 || bfd_bread (buf, info->size, abfd) != info->size)
2367 {
2368 error (_("Dwarf Error: Can't read DWARF data"
2369 " in section %s [in module %s]"),
2370 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2371 }
2372 }
2373
2374 /* A helper function that returns the size of a section in a safe way.
2375 If you are positive that the section has been read before using the
2376 size, then it is safe to refer to the dwarf2_section_info object's
2377 "size" field directly. In other cases, you must call this
2378 function, because for compressed sections the size field is not set
2379 correctly until the section has been read. */
2380
2381 static bfd_size_type
2382 dwarf2_section_size (struct objfile *objfile,
2383 struct dwarf2_section_info *info)
2384 {
2385 if (!info->readin)
2386 dwarf2_read_section (objfile, info);
2387 return info->size;
2388 }
2389
2390 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2391 SECTION_NAME. */
2392
2393 void
2394 dwarf2_get_section_info (struct objfile *objfile,
2395 enum dwarf2_section_enum sect,
2396 asection **sectp, const gdb_byte **bufp,
2397 bfd_size_type *sizep)
2398 {
2399 struct dwarf2_per_objfile *data
2400 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2401 dwarf2_objfile_data_key);
2402 struct dwarf2_section_info *info;
2403
2404 /* We may see an objfile without any DWARF, in which case we just
2405 return nothing. */
2406 if (data == NULL)
2407 {
2408 *sectp = NULL;
2409 *bufp = NULL;
2410 *sizep = 0;
2411 return;
2412 }
2413 switch (sect)
2414 {
2415 case DWARF2_DEBUG_FRAME:
2416 info = &data->frame;
2417 break;
2418 case DWARF2_EH_FRAME:
2419 info = &data->eh_frame;
2420 break;
2421 default:
2422 gdb_assert_not_reached ("unexpected section");
2423 }
2424
2425 dwarf2_read_section (objfile, info);
2426
2427 *sectp = get_section_bfd_section (info);
2428 *bufp = info->buffer;
2429 *sizep = info->size;
2430 }
2431
2432 /* A helper function to find the sections for a .dwz file. */
2433
2434 static void
2435 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2436 {
2437 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2438
2439 /* Note that we only support the standard ELF names, because .dwz
2440 is ELF-only (at the time of writing). */
2441 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2442 {
2443 dwz_file->abbrev.s.section = sectp;
2444 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2445 }
2446 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2447 {
2448 dwz_file->info.s.section = sectp;
2449 dwz_file->info.size = bfd_get_section_size (sectp);
2450 }
2451 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2452 {
2453 dwz_file->str.s.section = sectp;
2454 dwz_file->str.size = bfd_get_section_size (sectp);
2455 }
2456 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2457 {
2458 dwz_file->line.s.section = sectp;
2459 dwz_file->line.size = bfd_get_section_size (sectp);
2460 }
2461 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2462 {
2463 dwz_file->macro.s.section = sectp;
2464 dwz_file->macro.size = bfd_get_section_size (sectp);
2465 }
2466 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2467 {
2468 dwz_file->gdb_index.s.section = sectp;
2469 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2470 }
2471 }
2472
2473 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2474 there is no .gnu_debugaltlink section in the file. Error if there
2475 is such a section but the file cannot be found. */
2476
2477 static struct dwz_file *
2478 dwarf2_get_dwz_file (void)
2479 {
2480 bfd *dwz_bfd;
2481 char *data;
2482 struct cleanup *cleanup;
2483 const char *filename;
2484 struct dwz_file *result;
2485 bfd_size_type buildid_len_arg;
2486 size_t buildid_len;
2487 bfd_byte *buildid;
2488
2489 if (dwarf2_per_objfile->dwz_file != NULL)
2490 return dwarf2_per_objfile->dwz_file;
2491
2492 bfd_set_error (bfd_error_no_error);
2493 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2494 &buildid_len_arg, &buildid);
2495 if (data == NULL)
2496 {
2497 if (bfd_get_error () == bfd_error_no_error)
2498 return NULL;
2499 error (_("could not read '.gnu_debugaltlink' section: %s"),
2500 bfd_errmsg (bfd_get_error ()));
2501 }
2502 cleanup = make_cleanup (xfree, data);
2503 make_cleanup (xfree, buildid);
2504
2505 buildid_len = (size_t) buildid_len_arg;
2506
2507 filename = (const char *) data;
2508 if (!IS_ABSOLUTE_PATH (filename))
2509 {
2510 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2511 char *rel;
2512
2513 make_cleanup (xfree, abs);
2514 abs = ldirname (abs);
2515 make_cleanup (xfree, abs);
2516
2517 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2518 make_cleanup (xfree, rel);
2519 filename = rel;
2520 }
2521
2522 /* First try the file name given in the section. If that doesn't
2523 work, try to use the build-id instead. */
2524 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2525 if (dwz_bfd != NULL)
2526 {
2527 if (!build_id_verify (dwz_bfd, buildid_len, buildid))
2528 {
2529 gdb_bfd_unref (dwz_bfd);
2530 dwz_bfd = NULL;
2531 }
2532 }
2533
2534 if (dwz_bfd == NULL)
2535 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2536
2537 if (dwz_bfd == NULL)
2538 error (_("could not find '.gnu_debugaltlink' file for %s"),
2539 objfile_name (dwarf2_per_objfile->objfile));
2540
2541 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2542 struct dwz_file);
2543 result->dwz_bfd = dwz_bfd;
2544
2545 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2546
2547 do_cleanups (cleanup);
2548
2549 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, dwz_bfd);
2550 dwarf2_per_objfile->dwz_file = result;
2551 return result;
2552 }
2553 \f
2554 /* DWARF quick_symbols_functions support. */
2555
2556 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2557 unique line tables, so we maintain a separate table of all .debug_line
2558 derived entries to support the sharing.
2559 All the quick functions need is the list of file names. We discard the
2560 line_header when we're done and don't need to record it here. */
2561 struct quick_file_names
2562 {
2563 /* The data used to construct the hash key. */
2564 struct stmt_list_hash hash;
2565
2566 /* The number of entries in file_names, real_names. */
2567 unsigned int num_file_names;
2568
2569 /* The file names from the line table, after being run through
2570 file_full_name. */
2571 const char **file_names;
2572
2573 /* The file names from the line table after being run through
2574 gdb_realpath. These are computed lazily. */
2575 const char **real_names;
2576 };
2577
2578 /* When using the index (and thus not using psymtabs), each CU has an
2579 object of this type. This is used to hold information needed by
2580 the various "quick" methods. */
2581 struct dwarf2_per_cu_quick_data
2582 {
2583 /* The file table. This can be NULL if there was no file table
2584 or it's currently not read in.
2585 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2586 struct quick_file_names *file_names;
2587
2588 /* The corresponding symbol table. This is NULL if symbols for this
2589 CU have not yet been read. */
2590 struct compunit_symtab *compunit_symtab;
2591
2592 /* A temporary mark bit used when iterating over all CUs in
2593 expand_symtabs_matching. */
2594 unsigned int mark : 1;
2595
2596 /* True if we've tried to read the file table and found there isn't one.
2597 There will be no point in trying to read it again next time. */
2598 unsigned int no_file_data : 1;
2599 };
2600
2601 /* Utility hash function for a stmt_list_hash. */
2602
2603 static hashval_t
2604 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2605 {
2606 hashval_t v = 0;
2607
2608 if (stmt_list_hash->dwo_unit != NULL)
2609 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2610 v += stmt_list_hash->line_offset.sect_off;
2611 return v;
2612 }
2613
2614 /* Utility equality function for a stmt_list_hash. */
2615
2616 static int
2617 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2618 const struct stmt_list_hash *rhs)
2619 {
2620 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2621 return 0;
2622 if (lhs->dwo_unit != NULL
2623 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2624 return 0;
2625
2626 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2627 }
2628
2629 /* Hash function for a quick_file_names. */
2630
2631 static hashval_t
2632 hash_file_name_entry (const void *e)
2633 {
2634 const struct quick_file_names *file_data
2635 = (const struct quick_file_names *) e;
2636
2637 return hash_stmt_list_entry (&file_data->hash);
2638 }
2639
2640 /* Equality function for a quick_file_names. */
2641
2642 static int
2643 eq_file_name_entry (const void *a, const void *b)
2644 {
2645 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2646 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2647
2648 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2649 }
2650
2651 /* Delete function for a quick_file_names. */
2652
2653 static void
2654 delete_file_name_entry (void *e)
2655 {
2656 struct quick_file_names *file_data = (struct quick_file_names *) e;
2657 int i;
2658
2659 for (i = 0; i < file_data->num_file_names; ++i)
2660 {
2661 xfree ((void*) file_data->file_names[i]);
2662 if (file_data->real_names)
2663 xfree ((void*) file_data->real_names[i]);
2664 }
2665
2666 /* The space for the struct itself lives on objfile_obstack,
2667 so we don't free it here. */
2668 }
2669
2670 /* Create a quick_file_names hash table. */
2671
2672 static htab_t
2673 create_quick_file_names_table (unsigned int nr_initial_entries)
2674 {
2675 return htab_create_alloc (nr_initial_entries,
2676 hash_file_name_entry, eq_file_name_entry,
2677 delete_file_name_entry, xcalloc, xfree);
2678 }
2679
2680 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2681 have to be created afterwards. You should call age_cached_comp_units after
2682 processing PER_CU->CU. dw2_setup must have been already called. */
2683
2684 static void
2685 load_cu (struct dwarf2_per_cu_data *per_cu)
2686 {
2687 if (per_cu->is_debug_types)
2688 load_full_type_unit (per_cu);
2689 else
2690 load_full_comp_unit (per_cu, language_minimal);
2691
2692 if (per_cu->cu == NULL)
2693 return; /* Dummy CU. */
2694
2695 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2696 }
2697
2698 /* Read in the symbols for PER_CU. */
2699
2700 static void
2701 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2702 {
2703 struct cleanup *back_to;
2704
2705 /* Skip type_unit_groups, reading the type units they contain
2706 is handled elsewhere. */
2707 if (IS_TYPE_UNIT_GROUP (per_cu))
2708 return;
2709
2710 back_to = make_cleanup (dwarf2_release_queue, NULL);
2711
2712 if (dwarf2_per_objfile->using_index
2713 ? per_cu->v.quick->compunit_symtab == NULL
2714 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2715 {
2716 queue_comp_unit (per_cu, language_minimal);
2717 load_cu (per_cu);
2718
2719 /* If we just loaded a CU from a DWO, and we're working with an index
2720 that may badly handle TUs, load all the TUs in that DWO as well.
2721 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2722 if (!per_cu->is_debug_types
2723 && per_cu->cu != NULL
2724 && per_cu->cu->dwo_unit != NULL
2725 && dwarf2_per_objfile->index_table != NULL
2726 && dwarf2_per_objfile->index_table->version <= 7
2727 /* DWP files aren't supported yet. */
2728 && get_dwp_file () == NULL)
2729 queue_and_load_all_dwo_tus (per_cu);
2730 }
2731
2732 process_queue ();
2733
2734 /* Age the cache, releasing compilation units that have not
2735 been used recently. */
2736 age_cached_comp_units ();
2737
2738 do_cleanups (back_to);
2739 }
2740
2741 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2742 the objfile from which this CU came. Returns the resulting symbol
2743 table. */
2744
2745 static struct compunit_symtab *
2746 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2747 {
2748 gdb_assert (dwarf2_per_objfile->using_index);
2749 if (!per_cu->v.quick->compunit_symtab)
2750 {
2751 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2752 increment_reading_symtab ();
2753 dw2_do_instantiate_symtab (per_cu);
2754 process_cu_includes ();
2755 do_cleanups (back_to);
2756 }
2757
2758 return per_cu->v.quick->compunit_symtab;
2759 }
2760
2761 /* Return the CU/TU given its index.
2762
2763 This is intended for loops like:
2764
2765 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2766 + dwarf2_per_objfile->n_type_units); ++i)
2767 {
2768 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2769
2770 ...;
2771 }
2772 */
2773
2774 static struct dwarf2_per_cu_data *
2775 dw2_get_cutu (int index)
2776 {
2777 if (index >= dwarf2_per_objfile->n_comp_units)
2778 {
2779 index -= dwarf2_per_objfile->n_comp_units;
2780 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2781 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2782 }
2783
2784 return dwarf2_per_objfile->all_comp_units[index];
2785 }
2786
2787 /* Return the CU given its index.
2788 This differs from dw2_get_cutu in that it's for when you know INDEX
2789 refers to a CU. */
2790
2791 static struct dwarf2_per_cu_data *
2792 dw2_get_cu (int index)
2793 {
2794 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2795
2796 return dwarf2_per_objfile->all_comp_units[index];
2797 }
2798
2799 /* A helper for create_cus_from_index that handles a given list of
2800 CUs. */
2801
2802 static void
2803 create_cus_from_index_list (struct objfile *objfile,
2804 const gdb_byte *cu_list, offset_type n_elements,
2805 struct dwarf2_section_info *section,
2806 int is_dwz,
2807 int base_offset)
2808 {
2809 offset_type i;
2810
2811 for (i = 0; i < n_elements; i += 2)
2812 {
2813 struct dwarf2_per_cu_data *the_cu;
2814 ULONGEST offset, length;
2815
2816 gdb_static_assert (sizeof (ULONGEST) >= 8);
2817 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2818 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2819 cu_list += 2 * 8;
2820
2821 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2822 struct dwarf2_per_cu_data);
2823 the_cu->offset.sect_off = offset;
2824 the_cu->length = length;
2825 the_cu->objfile = objfile;
2826 the_cu->section = section;
2827 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2828 struct dwarf2_per_cu_quick_data);
2829 the_cu->is_dwz = is_dwz;
2830 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2831 }
2832 }
2833
2834 /* Read the CU list from the mapped index, and use it to create all
2835 the CU objects for this objfile. */
2836
2837 static void
2838 create_cus_from_index (struct objfile *objfile,
2839 const gdb_byte *cu_list, offset_type cu_list_elements,
2840 const gdb_byte *dwz_list, offset_type dwz_elements)
2841 {
2842 struct dwz_file *dwz;
2843
2844 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2845 dwarf2_per_objfile->all_comp_units =
2846 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2847 dwarf2_per_objfile->n_comp_units);
2848
2849 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2850 &dwarf2_per_objfile->info, 0, 0);
2851
2852 if (dwz_elements == 0)
2853 return;
2854
2855 dwz = dwarf2_get_dwz_file ();
2856 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2857 cu_list_elements / 2);
2858 }
2859
2860 /* Create the signatured type hash table from the index. */
2861
2862 static void
2863 create_signatured_type_table_from_index (struct objfile *objfile,
2864 struct dwarf2_section_info *section,
2865 const gdb_byte *bytes,
2866 offset_type elements)
2867 {
2868 offset_type i;
2869 htab_t sig_types_hash;
2870
2871 dwarf2_per_objfile->n_type_units
2872 = dwarf2_per_objfile->n_allocated_type_units
2873 = elements / 3;
2874 dwarf2_per_objfile->all_type_units =
2875 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
2876
2877 sig_types_hash = allocate_signatured_type_table (objfile);
2878
2879 for (i = 0; i < elements; i += 3)
2880 {
2881 struct signatured_type *sig_type;
2882 ULONGEST offset, type_offset_in_tu, signature;
2883 void **slot;
2884
2885 gdb_static_assert (sizeof (ULONGEST) >= 8);
2886 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2887 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2888 BFD_ENDIAN_LITTLE);
2889 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2890 bytes += 3 * 8;
2891
2892 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2893 struct signatured_type);
2894 sig_type->signature = signature;
2895 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2896 sig_type->per_cu.is_debug_types = 1;
2897 sig_type->per_cu.section = section;
2898 sig_type->per_cu.offset.sect_off = offset;
2899 sig_type->per_cu.objfile = objfile;
2900 sig_type->per_cu.v.quick
2901 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2902 struct dwarf2_per_cu_quick_data);
2903
2904 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2905 *slot = sig_type;
2906
2907 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2908 }
2909
2910 dwarf2_per_objfile->signatured_types = sig_types_hash;
2911 }
2912
2913 /* Read the address map data from the mapped index, and use it to
2914 populate the objfile's psymtabs_addrmap. */
2915
2916 static void
2917 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2918 {
2919 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2920 const gdb_byte *iter, *end;
2921 struct obstack temp_obstack;
2922 struct addrmap *mutable_map;
2923 struct cleanup *cleanup;
2924 CORE_ADDR baseaddr;
2925
2926 obstack_init (&temp_obstack);
2927 cleanup = make_cleanup_obstack_free (&temp_obstack);
2928 mutable_map = addrmap_create_mutable (&temp_obstack);
2929
2930 iter = index->address_table;
2931 end = iter + index->address_table_size;
2932
2933 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2934
2935 while (iter < end)
2936 {
2937 ULONGEST hi, lo, cu_index;
2938 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2939 iter += 8;
2940 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2941 iter += 8;
2942 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2943 iter += 4;
2944
2945 if (lo > hi)
2946 {
2947 complaint (&symfile_complaints,
2948 _(".gdb_index address table has invalid range (%s - %s)"),
2949 hex_string (lo), hex_string (hi));
2950 continue;
2951 }
2952
2953 if (cu_index >= dwarf2_per_objfile->n_comp_units)
2954 {
2955 complaint (&symfile_complaints,
2956 _(".gdb_index address table has invalid CU number %u"),
2957 (unsigned) cu_index);
2958 continue;
2959 }
2960
2961 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
2962 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
2963 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
2964 }
2965
2966 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2967 &objfile->objfile_obstack);
2968 do_cleanups (cleanup);
2969 }
2970
2971 /* The hash function for strings in the mapped index. This is the same as
2972 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2973 implementation. This is necessary because the hash function is tied to the
2974 format of the mapped index file. The hash values do not have to match with
2975 SYMBOL_HASH_NEXT.
2976
2977 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2978
2979 static hashval_t
2980 mapped_index_string_hash (int index_version, const void *p)
2981 {
2982 const unsigned char *str = (const unsigned char *) p;
2983 hashval_t r = 0;
2984 unsigned char c;
2985
2986 while ((c = *str++) != 0)
2987 {
2988 if (index_version >= 5)
2989 c = tolower (c);
2990 r = r * 67 + c - 113;
2991 }
2992
2993 return r;
2994 }
2995
2996 /* Find a slot in the mapped index INDEX for the object named NAME.
2997 If NAME is found, set *VEC_OUT to point to the CU vector in the
2998 constant pool and return 1. If NAME cannot be found, return 0. */
2999
3000 static int
3001 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3002 offset_type **vec_out)
3003 {
3004 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3005 offset_type hash;
3006 offset_type slot, step;
3007 int (*cmp) (const char *, const char *);
3008
3009 if (current_language->la_language == language_cplus
3010 || current_language->la_language == language_java
3011 || current_language->la_language == language_fortran
3012 || current_language->la_language == language_d)
3013 {
3014 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3015 not contain any. */
3016
3017 if (strchr (name, '(') != NULL)
3018 {
3019 char *without_params = cp_remove_params (name);
3020
3021 if (without_params != NULL)
3022 {
3023 make_cleanup (xfree, without_params);
3024 name = without_params;
3025 }
3026 }
3027 }
3028
3029 /* Index version 4 did not support case insensitive searches. But the
3030 indices for case insensitive languages are built in lowercase, therefore
3031 simulate our NAME being searched is also lowercased. */
3032 hash = mapped_index_string_hash ((index->version == 4
3033 && case_sensitivity == case_sensitive_off
3034 ? 5 : index->version),
3035 name);
3036
3037 slot = hash & (index->symbol_table_slots - 1);
3038 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3039 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3040
3041 for (;;)
3042 {
3043 /* Convert a slot number to an offset into the table. */
3044 offset_type i = 2 * slot;
3045 const char *str;
3046 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3047 {
3048 do_cleanups (back_to);
3049 return 0;
3050 }
3051
3052 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3053 if (!cmp (name, str))
3054 {
3055 *vec_out = (offset_type *) (index->constant_pool
3056 + MAYBE_SWAP (index->symbol_table[i + 1]));
3057 do_cleanups (back_to);
3058 return 1;
3059 }
3060
3061 slot = (slot + step) & (index->symbol_table_slots - 1);
3062 }
3063 }
3064
3065 /* A helper function that reads the .gdb_index from SECTION and fills
3066 in MAP. FILENAME is the name of the file containing the section;
3067 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3068 ok to use deprecated sections.
3069
3070 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3071 out parameters that are filled in with information about the CU and
3072 TU lists in the section.
3073
3074 Returns 1 if all went well, 0 otherwise. */
3075
3076 static int
3077 read_index_from_section (struct objfile *objfile,
3078 const char *filename,
3079 int deprecated_ok,
3080 struct dwarf2_section_info *section,
3081 struct mapped_index *map,
3082 const gdb_byte **cu_list,
3083 offset_type *cu_list_elements,
3084 const gdb_byte **types_list,
3085 offset_type *types_list_elements)
3086 {
3087 const gdb_byte *addr;
3088 offset_type version;
3089 offset_type *metadata;
3090 int i;
3091
3092 if (dwarf2_section_empty_p (section))
3093 return 0;
3094
3095 /* Older elfutils strip versions could keep the section in the main
3096 executable while splitting it for the separate debug info file. */
3097 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3098 return 0;
3099
3100 dwarf2_read_section (objfile, section);
3101
3102 addr = section->buffer;
3103 /* Version check. */
3104 version = MAYBE_SWAP (*(offset_type *) addr);
3105 /* Versions earlier than 3 emitted every copy of a psymbol. This
3106 causes the index to behave very poorly for certain requests. Version 3
3107 contained incomplete addrmap. So, it seems better to just ignore such
3108 indices. */
3109 if (version < 4)
3110 {
3111 static int warning_printed = 0;
3112 if (!warning_printed)
3113 {
3114 warning (_("Skipping obsolete .gdb_index section in %s."),
3115 filename);
3116 warning_printed = 1;
3117 }
3118 return 0;
3119 }
3120 /* Index version 4 uses a different hash function than index version
3121 5 and later.
3122
3123 Versions earlier than 6 did not emit psymbols for inlined
3124 functions. Using these files will cause GDB not to be able to
3125 set breakpoints on inlined functions by name, so we ignore these
3126 indices unless the user has done
3127 "set use-deprecated-index-sections on". */
3128 if (version < 6 && !deprecated_ok)
3129 {
3130 static int warning_printed = 0;
3131 if (!warning_printed)
3132 {
3133 warning (_("\
3134 Skipping deprecated .gdb_index section in %s.\n\
3135 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3136 to use the section anyway."),
3137 filename);
3138 warning_printed = 1;
3139 }
3140 return 0;
3141 }
3142 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3143 of the TU (for symbols coming from TUs),
3144 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3145 Plus gold-generated indices can have duplicate entries for global symbols,
3146 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3147 These are just performance bugs, and we can't distinguish gdb-generated
3148 indices from gold-generated ones, so issue no warning here. */
3149
3150 /* Indexes with higher version than the one supported by GDB may be no
3151 longer backward compatible. */
3152 if (version > 8)
3153 return 0;
3154
3155 map->version = version;
3156 map->total_size = section->size;
3157
3158 metadata = (offset_type *) (addr + sizeof (offset_type));
3159
3160 i = 0;
3161 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3162 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3163 / 8);
3164 ++i;
3165
3166 *types_list = addr + MAYBE_SWAP (metadata[i]);
3167 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3168 - MAYBE_SWAP (metadata[i]))
3169 / 8);
3170 ++i;
3171
3172 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3173 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3174 - MAYBE_SWAP (metadata[i]));
3175 ++i;
3176
3177 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3178 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3179 - MAYBE_SWAP (metadata[i]))
3180 / (2 * sizeof (offset_type)));
3181 ++i;
3182
3183 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3184
3185 return 1;
3186 }
3187
3188
3189 /* Read the index file. If everything went ok, initialize the "quick"
3190 elements of all the CUs and return 1. Otherwise, return 0. */
3191
3192 static int
3193 dwarf2_read_index (struct objfile *objfile)
3194 {
3195 struct mapped_index local_map, *map;
3196 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3197 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3198 struct dwz_file *dwz;
3199
3200 if (!read_index_from_section (objfile, objfile_name (objfile),
3201 use_deprecated_index_sections,
3202 &dwarf2_per_objfile->gdb_index, &local_map,
3203 &cu_list, &cu_list_elements,
3204 &types_list, &types_list_elements))
3205 return 0;
3206
3207 /* Don't use the index if it's empty. */
3208 if (local_map.symbol_table_slots == 0)
3209 return 0;
3210
3211 /* If there is a .dwz file, read it so we can get its CU list as
3212 well. */
3213 dwz = dwarf2_get_dwz_file ();
3214 if (dwz != NULL)
3215 {
3216 struct mapped_index dwz_map;
3217 const gdb_byte *dwz_types_ignore;
3218 offset_type dwz_types_elements_ignore;
3219
3220 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3221 1,
3222 &dwz->gdb_index, &dwz_map,
3223 &dwz_list, &dwz_list_elements,
3224 &dwz_types_ignore,
3225 &dwz_types_elements_ignore))
3226 {
3227 warning (_("could not read '.gdb_index' section from %s; skipping"),
3228 bfd_get_filename (dwz->dwz_bfd));
3229 return 0;
3230 }
3231 }
3232
3233 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3234 dwz_list_elements);
3235
3236 if (types_list_elements)
3237 {
3238 struct dwarf2_section_info *section;
3239
3240 /* We can only handle a single .debug_types when we have an
3241 index. */
3242 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3243 return 0;
3244
3245 section = VEC_index (dwarf2_section_info_def,
3246 dwarf2_per_objfile->types, 0);
3247
3248 create_signatured_type_table_from_index (objfile, section, types_list,
3249 types_list_elements);
3250 }
3251
3252 create_addrmap_from_index (objfile, &local_map);
3253
3254 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3255 *map = local_map;
3256
3257 dwarf2_per_objfile->index_table = map;
3258 dwarf2_per_objfile->using_index = 1;
3259 dwarf2_per_objfile->quick_file_names_table =
3260 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3261
3262 return 1;
3263 }
3264
3265 /* A helper for the "quick" functions which sets the global
3266 dwarf2_per_objfile according to OBJFILE. */
3267
3268 static void
3269 dw2_setup (struct objfile *objfile)
3270 {
3271 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3272 objfile_data (objfile, dwarf2_objfile_data_key));
3273 gdb_assert (dwarf2_per_objfile);
3274 }
3275
3276 /* die_reader_func for dw2_get_file_names. */
3277
3278 static void
3279 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3280 const gdb_byte *info_ptr,
3281 struct die_info *comp_unit_die,
3282 int has_children,
3283 void *data)
3284 {
3285 struct dwarf2_cu *cu = reader->cu;
3286 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3287 struct objfile *objfile = dwarf2_per_objfile->objfile;
3288 struct dwarf2_per_cu_data *lh_cu;
3289 struct line_header *lh;
3290 struct attribute *attr;
3291 int i;
3292 const char *name, *comp_dir;
3293 void **slot;
3294 struct quick_file_names *qfn;
3295 unsigned int line_offset;
3296
3297 gdb_assert (! this_cu->is_debug_types);
3298
3299 /* Our callers never want to match partial units -- instead they
3300 will match the enclosing full CU. */
3301 if (comp_unit_die->tag == DW_TAG_partial_unit)
3302 {
3303 this_cu->v.quick->no_file_data = 1;
3304 return;
3305 }
3306
3307 lh_cu = this_cu;
3308 lh = NULL;
3309 slot = NULL;
3310 line_offset = 0;
3311
3312 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3313 if (attr)
3314 {
3315 struct quick_file_names find_entry;
3316
3317 line_offset = DW_UNSND (attr);
3318
3319 /* We may have already read in this line header (TU line header sharing).
3320 If we have we're done. */
3321 find_entry.hash.dwo_unit = cu->dwo_unit;
3322 find_entry.hash.line_offset.sect_off = line_offset;
3323 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3324 &find_entry, INSERT);
3325 if (*slot != NULL)
3326 {
3327 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3328 return;
3329 }
3330
3331 lh = dwarf_decode_line_header (line_offset, cu);
3332 }
3333 if (lh == NULL)
3334 {
3335 lh_cu->v.quick->no_file_data = 1;
3336 return;
3337 }
3338
3339 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3340 qfn->hash.dwo_unit = cu->dwo_unit;
3341 qfn->hash.line_offset.sect_off = line_offset;
3342 gdb_assert (slot != NULL);
3343 *slot = qfn;
3344
3345 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
3346
3347 qfn->num_file_names = lh->num_file_names;
3348 qfn->file_names =
3349 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->num_file_names);
3350 for (i = 0; i < lh->num_file_names; ++i)
3351 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3352 qfn->real_names = NULL;
3353
3354 free_line_header (lh);
3355
3356 lh_cu->v.quick->file_names = qfn;
3357 }
3358
3359 /* A helper for the "quick" functions which attempts to read the line
3360 table for THIS_CU. */
3361
3362 static struct quick_file_names *
3363 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3364 {
3365 /* This should never be called for TUs. */
3366 gdb_assert (! this_cu->is_debug_types);
3367 /* Nor type unit groups. */
3368 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3369
3370 if (this_cu->v.quick->file_names != NULL)
3371 return this_cu->v.quick->file_names;
3372 /* If we know there is no line data, no point in looking again. */
3373 if (this_cu->v.quick->no_file_data)
3374 return NULL;
3375
3376 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3377
3378 if (this_cu->v.quick->no_file_data)
3379 return NULL;
3380 return this_cu->v.quick->file_names;
3381 }
3382
3383 /* A helper for the "quick" functions which computes and caches the
3384 real path for a given file name from the line table. */
3385
3386 static const char *
3387 dw2_get_real_path (struct objfile *objfile,
3388 struct quick_file_names *qfn, int index)
3389 {
3390 if (qfn->real_names == NULL)
3391 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3392 qfn->num_file_names, const char *);
3393
3394 if (qfn->real_names[index] == NULL)
3395 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3396
3397 return qfn->real_names[index];
3398 }
3399
3400 static struct symtab *
3401 dw2_find_last_source_symtab (struct objfile *objfile)
3402 {
3403 struct compunit_symtab *cust;
3404 int index;
3405
3406 dw2_setup (objfile);
3407 index = dwarf2_per_objfile->n_comp_units - 1;
3408 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3409 if (cust == NULL)
3410 return NULL;
3411 return compunit_primary_filetab (cust);
3412 }
3413
3414 /* Traversal function for dw2_forget_cached_source_info. */
3415
3416 static int
3417 dw2_free_cached_file_names (void **slot, void *info)
3418 {
3419 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3420
3421 if (file_data->real_names)
3422 {
3423 int i;
3424
3425 for (i = 0; i < file_data->num_file_names; ++i)
3426 {
3427 xfree ((void*) file_data->real_names[i]);
3428 file_data->real_names[i] = NULL;
3429 }
3430 }
3431
3432 return 1;
3433 }
3434
3435 static void
3436 dw2_forget_cached_source_info (struct objfile *objfile)
3437 {
3438 dw2_setup (objfile);
3439
3440 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3441 dw2_free_cached_file_names, NULL);
3442 }
3443
3444 /* Helper function for dw2_map_symtabs_matching_filename that expands
3445 the symtabs and calls the iterator. */
3446
3447 static int
3448 dw2_map_expand_apply (struct objfile *objfile,
3449 struct dwarf2_per_cu_data *per_cu,
3450 const char *name, const char *real_path,
3451 int (*callback) (struct symtab *, void *),
3452 void *data)
3453 {
3454 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3455
3456 /* Don't visit already-expanded CUs. */
3457 if (per_cu->v.quick->compunit_symtab)
3458 return 0;
3459
3460 /* This may expand more than one symtab, and we want to iterate over
3461 all of them. */
3462 dw2_instantiate_symtab (per_cu);
3463
3464 return iterate_over_some_symtabs (name, real_path, callback, data,
3465 objfile->compunit_symtabs, last_made);
3466 }
3467
3468 /* Implementation of the map_symtabs_matching_filename method. */
3469
3470 static int
3471 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3472 const char *real_path,
3473 int (*callback) (struct symtab *, void *),
3474 void *data)
3475 {
3476 int i;
3477 const char *name_basename = lbasename (name);
3478
3479 dw2_setup (objfile);
3480
3481 /* The rule is CUs specify all the files, including those used by
3482 any TU, so there's no need to scan TUs here. */
3483
3484 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3485 {
3486 int j;
3487 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3488 struct quick_file_names *file_data;
3489
3490 /* We only need to look at symtabs not already expanded. */
3491 if (per_cu->v.quick->compunit_symtab)
3492 continue;
3493
3494 file_data = dw2_get_file_names (per_cu);
3495 if (file_data == NULL)
3496 continue;
3497
3498 for (j = 0; j < file_data->num_file_names; ++j)
3499 {
3500 const char *this_name = file_data->file_names[j];
3501 const char *this_real_name;
3502
3503 if (compare_filenames_for_search (this_name, name))
3504 {
3505 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3506 callback, data))
3507 return 1;
3508 continue;
3509 }
3510
3511 /* Before we invoke realpath, which can get expensive when many
3512 files are involved, do a quick comparison of the basenames. */
3513 if (! basenames_may_differ
3514 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3515 continue;
3516
3517 this_real_name = dw2_get_real_path (objfile, file_data, j);
3518 if (compare_filenames_for_search (this_real_name, name))
3519 {
3520 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3521 callback, data))
3522 return 1;
3523 continue;
3524 }
3525
3526 if (real_path != NULL)
3527 {
3528 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3529 gdb_assert (IS_ABSOLUTE_PATH (name));
3530 if (this_real_name != NULL
3531 && FILENAME_CMP (real_path, this_real_name) == 0)
3532 {
3533 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3534 callback, data))
3535 return 1;
3536 continue;
3537 }
3538 }
3539 }
3540 }
3541
3542 return 0;
3543 }
3544
3545 /* Struct used to manage iterating over all CUs looking for a symbol. */
3546
3547 struct dw2_symtab_iterator
3548 {
3549 /* The internalized form of .gdb_index. */
3550 struct mapped_index *index;
3551 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3552 int want_specific_block;
3553 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3554 Unused if !WANT_SPECIFIC_BLOCK. */
3555 int block_index;
3556 /* The kind of symbol we're looking for. */
3557 domain_enum domain;
3558 /* The list of CUs from the index entry of the symbol,
3559 or NULL if not found. */
3560 offset_type *vec;
3561 /* The next element in VEC to look at. */
3562 int next;
3563 /* The number of elements in VEC, or zero if there is no match. */
3564 int length;
3565 /* Have we seen a global version of the symbol?
3566 If so we can ignore all further global instances.
3567 This is to work around gold/15646, inefficient gold-generated
3568 indices. */
3569 int global_seen;
3570 };
3571
3572 /* Initialize the index symtab iterator ITER.
3573 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3574 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3575
3576 static void
3577 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3578 struct mapped_index *index,
3579 int want_specific_block,
3580 int block_index,
3581 domain_enum domain,
3582 const char *name)
3583 {
3584 iter->index = index;
3585 iter->want_specific_block = want_specific_block;
3586 iter->block_index = block_index;
3587 iter->domain = domain;
3588 iter->next = 0;
3589 iter->global_seen = 0;
3590
3591 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3592 iter->length = MAYBE_SWAP (*iter->vec);
3593 else
3594 {
3595 iter->vec = NULL;
3596 iter->length = 0;
3597 }
3598 }
3599
3600 /* Return the next matching CU or NULL if there are no more. */
3601
3602 static struct dwarf2_per_cu_data *
3603 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3604 {
3605 for ( ; iter->next < iter->length; ++iter->next)
3606 {
3607 offset_type cu_index_and_attrs =
3608 MAYBE_SWAP (iter->vec[iter->next + 1]);
3609 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3610 struct dwarf2_per_cu_data *per_cu;
3611 int want_static = iter->block_index != GLOBAL_BLOCK;
3612 /* This value is only valid for index versions >= 7. */
3613 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3614 gdb_index_symbol_kind symbol_kind =
3615 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3616 /* Only check the symbol attributes if they're present.
3617 Indices prior to version 7 don't record them,
3618 and indices >= 7 may elide them for certain symbols
3619 (gold does this). */
3620 int attrs_valid =
3621 (iter->index->version >= 7
3622 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3623
3624 /* Don't crash on bad data. */
3625 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3626 + dwarf2_per_objfile->n_type_units))
3627 {
3628 complaint (&symfile_complaints,
3629 _(".gdb_index entry has bad CU index"
3630 " [in module %s]"),
3631 objfile_name (dwarf2_per_objfile->objfile));
3632 continue;
3633 }
3634
3635 per_cu = dw2_get_cutu (cu_index);
3636
3637 /* Skip if already read in. */
3638 if (per_cu->v.quick->compunit_symtab)
3639 continue;
3640
3641 /* Check static vs global. */
3642 if (attrs_valid)
3643 {
3644 if (iter->want_specific_block
3645 && want_static != is_static)
3646 continue;
3647 /* Work around gold/15646. */
3648 if (!is_static && iter->global_seen)
3649 continue;
3650 if (!is_static)
3651 iter->global_seen = 1;
3652 }
3653
3654 /* Only check the symbol's kind if it has one. */
3655 if (attrs_valid)
3656 {
3657 switch (iter->domain)
3658 {
3659 case VAR_DOMAIN:
3660 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3661 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3662 /* Some types are also in VAR_DOMAIN. */
3663 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3664 continue;
3665 break;
3666 case STRUCT_DOMAIN:
3667 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3668 continue;
3669 break;
3670 case LABEL_DOMAIN:
3671 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3672 continue;
3673 break;
3674 default:
3675 break;
3676 }
3677 }
3678
3679 ++iter->next;
3680 return per_cu;
3681 }
3682
3683 return NULL;
3684 }
3685
3686 static struct compunit_symtab *
3687 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3688 const char *name, domain_enum domain)
3689 {
3690 struct compunit_symtab *stab_best = NULL;
3691 struct mapped_index *index;
3692
3693 dw2_setup (objfile);
3694
3695 index = dwarf2_per_objfile->index_table;
3696
3697 /* index is NULL if OBJF_READNOW. */
3698 if (index)
3699 {
3700 struct dw2_symtab_iterator iter;
3701 struct dwarf2_per_cu_data *per_cu;
3702
3703 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3704
3705 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3706 {
3707 struct symbol *sym, *with_opaque = NULL;
3708 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3709 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3710 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3711
3712 sym = block_find_symbol (block, name, domain,
3713 block_find_non_opaque_type_preferred,
3714 &with_opaque);
3715
3716 /* Some caution must be observed with overloaded functions
3717 and methods, since the index will not contain any overload
3718 information (but NAME might contain it). */
3719
3720 if (sym != NULL
3721 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3722 return stab;
3723 if (with_opaque != NULL
3724 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3725 stab_best = stab;
3726
3727 /* Keep looking through other CUs. */
3728 }
3729 }
3730
3731 return stab_best;
3732 }
3733
3734 static void
3735 dw2_print_stats (struct objfile *objfile)
3736 {
3737 int i, total, count;
3738
3739 dw2_setup (objfile);
3740 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3741 count = 0;
3742 for (i = 0; i < total; ++i)
3743 {
3744 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3745
3746 if (!per_cu->v.quick->compunit_symtab)
3747 ++count;
3748 }
3749 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3750 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3751 }
3752
3753 /* This dumps minimal information about the index.
3754 It is called via "mt print objfiles".
3755 One use is to verify .gdb_index has been loaded by the
3756 gdb.dwarf2/gdb-index.exp testcase. */
3757
3758 static void
3759 dw2_dump (struct objfile *objfile)
3760 {
3761 dw2_setup (objfile);
3762 gdb_assert (dwarf2_per_objfile->using_index);
3763 printf_filtered (".gdb_index:");
3764 if (dwarf2_per_objfile->index_table != NULL)
3765 {
3766 printf_filtered (" version %d\n",
3767 dwarf2_per_objfile->index_table->version);
3768 }
3769 else
3770 printf_filtered (" faked for \"readnow\"\n");
3771 printf_filtered ("\n");
3772 }
3773
3774 static void
3775 dw2_relocate (struct objfile *objfile,
3776 const struct section_offsets *new_offsets,
3777 const struct section_offsets *delta)
3778 {
3779 /* There's nothing to relocate here. */
3780 }
3781
3782 static void
3783 dw2_expand_symtabs_for_function (struct objfile *objfile,
3784 const char *func_name)
3785 {
3786 struct mapped_index *index;
3787
3788 dw2_setup (objfile);
3789
3790 index = dwarf2_per_objfile->index_table;
3791
3792 /* index is NULL if OBJF_READNOW. */
3793 if (index)
3794 {
3795 struct dw2_symtab_iterator iter;
3796 struct dwarf2_per_cu_data *per_cu;
3797
3798 /* Note: It doesn't matter what we pass for block_index here. */
3799 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3800 func_name);
3801
3802 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3803 dw2_instantiate_symtab (per_cu);
3804 }
3805 }
3806
3807 static void
3808 dw2_expand_all_symtabs (struct objfile *objfile)
3809 {
3810 int i;
3811
3812 dw2_setup (objfile);
3813
3814 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3815 + dwarf2_per_objfile->n_type_units); ++i)
3816 {
3817 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3818
3819 dw2_instantiate_symtab (per_cu);
3820 }
3821 }
3822
3823 static void
3824 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3825 const char *fullname)
3826 {
3827 int i;
3828
3829 dw2_setup (objfile);
3830
3831 /* We don't need to consider type units here.
3832 This is only called for examining code, e.g. expand_line_sal.
3833 There can be an order of magnitude (or more) more type units
3834 than comp units, and we avoid them if we can. */
3835
3836 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3837 {
3838 int j;
3839 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3840 struct quick_file_names *file_data;
3841
3842 /* We only need to look at symtabs not already expanded. */
3843 if (per_cu->v.quick->compunit_symtab)
3844 continue;
3845
3846 file_data = dw2_get_file_names (per_cu);
3847 if (file_data == NULL)
3848 continue;
3849
3850 for (j = 0; j < file_data->num_file_names; ++j)
3851 {
3852 const char *this_fullname = file_data->file_names[j];
3853
3854 if (filename_cmp (this_fullname, fullname) == 0)
3855 {
3856 dw2_instantiate_symtab (per_cu);
3857 break;
3858 }
3859 }
3860 }
3861 }
3862
3863 static void
3864 dw2_map_matching_symbols (struct objfile *objfile,
3865 const char * name, domain_enum domain,
3866 int global,
3867 int (*callback) (struct block *,
3868 struct symbol *, void *),
3869 void *data, symbol_compare_ftype *match,
3870 symbol_compare_ftype *ordered_compare)
3871 {
3872 /* Currently unimplemented; used for Ada. The function can be called if the
3873 current language is Ada for a non-Ada objfile using GNU index. As Ada
3874 does not look for non-Ada symbols this function should just return. */
3875 }
3876
3877 static void
3878 dw2_expand_symtabs_matching
3879 (struct objfile *objfile,
3880 expand_symtabs_file_matcher_ftype *file_matcher,
3881 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
3882 expand_symtabs_exp_notify_ftype *expansion_notify,
3883 enum search_domain kind,
3884 void *data)
3885 {
3886 int i;
3887 offset_type iter;
3888 struct mapped_index *index;
3889
3890 dw2_setup (objfile);
3891
3892 /* index_table is NULL if OBJF_READNOW. */
3893 if (!dwarf2_per_objfile->index_table)
3894 return;
3895 index = dwarf2_per_objfile->index_table;
3896
3897 if (file_matcher != NULL)
3898 {
3899 struct cleanup *cleanup;
3900 htab_t visited_found, visited_not_found;
3901
3902 visited_found = htab_create_alloc (10,
3903 htab_hash_pointer, htab_eq_pointer,
3904 NULL, xcalloc, xfree);
3905 cleanup = make_cleanup_htab_delete (visited_found);
3906 visited_not_found = htab_create_alloc (10,
3907 htab_hash_pointer, htab_eq_pointer,
3908 NULL, xcalloc, xfree);
3909 make_cleanup_htab_delete (visited_not_found);
3910
3911 /* The rule is CUs specify all the files, including those used by
3912 any TU, so there's no need to scan TUs here. */
3913
3914 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3915 {
3916 int j;
3917 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3918 struct quick_file_names *file_data;
3919 void **slot;
3920
3921 QUIT;
3922
3923 per_cu->v.quick->mark = 0;
3924
3925 /* We only need to look at symtabs not already expanded. */
3926 if (per_cu->v.quick->compunit_symtab)
3927 continue;
3928
3929 file_data = dw2_get_file_names (per_cu);
3930 if (file_data == NULL)
3931 continue;
3932
3933 if (htab_find (visited_not_found, file_data) != NULL)
3934 continue;
3935 else if (htab_find (visited_found, file_data) != NULL)
3936 {
3937 per_cu->v.quick->mark = 1;
3938 continue;
3939 }
3940
3941 for (j = 0; j < file_data->num_file_names; ++j)
3942 {
3943 const char *this_real_name;
3944
3945 if (file_matcher (file_data->file_names[j], data, 0))
3946 {
3947 per_cu->v.quick->mark = 1;
3948 break;
3949 }
3950
3951 /* Before we invoke realpath, which can get expensive when many
3952 files are involved, do a quick comparison of the basenames. */
3953 if (!basenames_may_differ
3954 && !file_matcher (lbasename (file_data->file_names[j]),
3955 data, 1))
3956 continue;
3957
3958 this_real_name = dw2_get_real_path (objfile, file_data, j);
3959 if (file_matcher (this_real_name, data, 0))
3960 {
3961 per_cu->v.quick->mark = 1;
3962 break;
3963 }
3964 }
3965
3966 slot = htab_find_slot (per_cu->v.quick->mark
3967 ? visited_found
3968 : visited_not_found,
3969 file_data, INSERT);
3970 *slot = file_data;
3971 }
3972
3973 do_cleanups (cleanup);
3974 }
3975
3976 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3977 {
3978 offset_type idx = 2 * iter;
3979 const char *name;
3980 offset_type *vec, vec_len, vec_idx;
3981 int global_seen = 0;
3982
3983 QUIT;
3984
3985 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3986 continue;
3987
3988 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3989
3990 if (! (*symbol_matcher) (name, data))
3991 continue;
3992
3993 /* The name was matched, now expand corresponding CUs that were
3994 marked. */
3995 vec = (offset_type *) (index->constant_pool
3996 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3997 vec_len = MAYBE_SWAP (vec[0]);
3998 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3999 {
4000 struct dwarf2_per_cu_data *per_cu;
4001 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4002 /* This value is only valid for index versions >= 7. */
4003 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4004 gdb_index_symbol_kind symbol_kind =
4005 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4006 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4007 /* Only check the symbol attributes if they're present.
4008 Indices prior to version 7 don't record them,
4009 and indices >= 7 may elide them for certain symbols
4010 (gold does this). */
4011 int attrs_valid =
4012 (index->version >= 7
4013 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4014
4015 /* Work around gold/15646. */
4016 if (attrs_valid)
4017 {
4018 if (!is_static && global_seen)
4019 continue;
4020 if (!is_static)
4021 global_seen = 1;
4022 }
4023
4024 /* Only check the symbol's kind if it has one. */
4025 if (attrs_valid)
4026 {
4027 switch (kind)
4028 {
4029 case VARIABLES_DOMAIN:
4030 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4031 continue;
4032 break;
4033 case FUNCTIONS_DOMAIN:
4034 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4035 continue;
4036 break;
4037 case TYPES_DOMAIN:
4038 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4039 continue;
4040 break;
4041 default:
4042 break;
4043 }
4044 }
4045
4046 /* Don't crash on bad data. */
4047 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4048 + dwarf2_per_objfile->n_type_units))
4049 {
4050 complaint (&symfile_complaints,
4051 _(".gdb_index entry has bad CU index"
4052 " [in module %s]"), objfile_name (objfile));
4053 continue;
4054 }
4055
4056 per_cu = dw2_get_cutu (cu_index);
4057 if (file_matcher == NULL || per_cu->v.quick->mark)
4058 {
4059 int symtab_was_null =
4060 (per_cu->v.quick->compunit_symtab == NULL);
4061
4062 dw2_instantiate_symtab (per_cu);
4063
4064 if (expansion_notify != NULL
4065 && symtab_was_null
4066 && per_cu->v.quick->compunit_symtab != NULL)
4067 {
4068 expansion_notify (per_cu->v.quick->compunit_symtab,
4069 data);
4070 }
4071 }
4072 }
4073 }
4074 }
4075
4076 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4077 symtab. */
4078
4079 static struct compunit_symtab *
4080 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4081 CORE_ADDR pc)
4082 {
4083 int i;
4084
4085 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4086 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4087 return cust;
4088
4089 if (cust->includes == NULL)
4090 return NULL;
4091
4092 for (i = 0; cust->includes[i]; ++i)
4093 {
4094 struct compunit_symtab *s = cust->includes[i];
4095
4096 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4097 if (s != NULL)
4098 return s;
4099 }
4100
4101 return NULL;
4102 }
4103
4104 static struct compunit_symtab *
4105 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4106 struct bound_minimal_symbol msymbol,
4107 CORE_ADDR pc,
4108 struct obj_section *section,
4109 int warn_if_readin)
4110 {
4111 struct dwarf2_per_cu_data *data;
4112 struct compunit_symtab *result;
4113
4114 dw2_setup (objfile);
4115
4116 if (!objfile->psymtabs_addrmap)
4117 return NULL;
4118
4119 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4120 pc);
4121 if (!data)
4122 return NULL;
4123
4124 if (warn_if_readin && data->v.quick->compunit_symtab)
4125 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4126 paddress (get_objfile_arch (objfile), pc));
4127
4128 result
4129 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4130 pc);
4131 gdb_assert (result != NULL);
4132 return result;
4133 }
4134
4135 static void
4136 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4137 void *data, int need_fullname)
4138 {
4139 int i;
4140 struct cleanup *cleanup;
4141 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4142 NULL, xcalloc, xfree);
4143
4144 cleanup = make_cleanup_htab_delete (visited);
4145 dw2_setup (objfile);
4146
4147 /* The rule is CUs specify all the files, including those used by
4148 any TU, so there's no need to scan TUs here.
4149 We can ignore file names coming from already-expanded CUs. */
4150
4151 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4152 {
4153 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4154
4155 if (per_cu->v.quick->compunit_symtab)
4156 {
4157 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
4158 INSERT);
4159
4160 *slot = per_cu->v.quick->file_names;
4161 }
4162 }
4163
4164 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4165 {
4166 int j;
4167 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4168 struct quick_file_names *file_data;
4169 void **slot;
4170
4171 /* We only need to look at symtabs not already expanded. */
4172 if (per_cu->v.quick->compunit_symtab)
4173 continue;
4174
4175 file_data = dw2_get_file_names (per_cu);
4176 if (file_data == NULL)
4177 continue;
4178
4179 slot = htab_find_slot (visited, file_data, INSERT);
4180 if (*slot)
4181 {
4182 /* Already visited. */
4183 continue;
4184 }
4185 *slot = file_data;
4186
4187 for (j = 0; j < file_data->num_file_names; ++j)
4188 {
4189 const char *this_real_name;
4190
4191 if (need_fullname)
4192 this_real_name = dw2_get_real_path (objfile, file_data, j);
4193 else
4194 this_real_name = NULL;
4195 (*fun) (file_data->file_names[j], this_real_name, data);
4196 }
4197 }
4198
4199 do_cleanups (cleanup);
4200 }
4201
4202 static int
4203 dw2_has_symbols (struct objfile *objfile)
4204 {
4205 return 1;
4206 }
4207
4208 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4209 {
4210 dw2_has_symbols,
4211 dw2_find_last_source_symtab,
4212 dw2_forget_cached_source_info,
4213 dw2_map_symtabs_matching_filename,
4214 dw2_lookup_symbol,
4215 dw2_print_stats,
4216 dw2_dump,
4217 dw2_relocate,
4218 dw2_expand_symtabs_for_function,
4219 dw2_expand_all_symtabs,
4220 dw2_expand_symtabs_with_fullname,
4221 dw2_map_matching_symbols,
4222 dw2_expand_symtabs_matching,
4223 dw2_find_pc_sect_compunit_symtab,
4224 dw2_map_symbol_filenames
4225 };
4226
4227 /* Initialize for reading DWARF for this objfile. Return 0 if this
4228 file will use psymtabs, or 1 if using the GNU index. */
4229
4230 int
4231 dwarf2_initialize_objfile (struct objfile *objfile)
4232 {
4233 /* If we're about to read full symbols, don't bother with the
4234 indices. In this case we also don't care if some other debug
4235 format is making psymtabs, because they are all about to be
4236 expanded anyway. */
4237 if ((objfile->flags & OBJF_READNOW))
4238 {
4239 int i;
4240
4241 dwarf2_per_objfile->using_index = 1;
4242 create_all_comp_units (objfile);
4243 create_all_type_units (objfile);
4244 dwarf2_per_objfile->quick_file_names_table =
4245 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4246
4247 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4248 + dwarf2_per_objfile->n_type_units); ++i)
4249 {
4250 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4251
4252 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4253 struct dwarf2_per_cu_quick_data);
4254 }
4255
4256 /* Return 1 so that gdb sees the "quick" functions. However,
4257 these functions will be no-ops because we will have expanded
4258 all symtabs. */
4259 return 1;
4260 }
4261
4262 if (dwarf2_read_index (objfile))
4263 return 1;
4264
4265 return 0;
4266 }
4267
4268 \f
4269
4270 /* Build a partial symbol table. */
4271
4272 void
4273 dwarf2_build_psymtabs (struct objfile *objfile)
4274 {
4275
4276 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4277 {
4278 init_psymbol_list (objfile, 1024);
4279 }
4280
4281 TRY
4282 {
4283 /* This isn't really ideal: all the data we allocate on the
4284 objfile's obstack is still uselessly kept around. However,
4285 freeing it seems unsafe. */
4286 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
4287
4288 dwarf2_build_psymtabs_hard (objfile);
4289 discard_cleanups (cleanups);
4290 }
4291 CATCH (except, RETURN_MASK_ERROR)
4292 {
4293 exception_print (gdb_stderr, except);
4294 }
4295 END_CATCH
4296 }
4297
4298 /* Return the total length of the CU described by HEADER. */
4299
4300 static unsigned int
4301 get_cu_length (const struct comp_unit_head *header)
4302 {
4303 return header->initial_length_size + header->length;
4304 }
4305
4306 /* Return TRUE if OFFSET is within CU_HEADER. */
4307
4308 static inline int
4309 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
4310 {
4311 sect_offset bottom = { cu_header->offset.sect_off };
4312 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
4313
4314 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
4315 }
4316
4317 /* Find the base address of the compilation unit for range lists and
4318 location lists. It will normally be specified by DW_AT_low_pc.
4319 In DWARF-3 draft 4, the base address could be overridden by
4320 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4321 compilation units with discontinuous ranges. */
4322
4323 static void
4324 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4325 {
4326 struct attribute *attr;
4327
4328 cu->base_known = 0;
4329 cu->base_address = 0;
4330
4331 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4332 if (attr)
4333 {
4334 cu->base_address = attr_value_as_address (attr);
4335 cu->base_known = 1;
4336 }
4337 else
4338 {
4339 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4340 if (attr)
4341 {
4342 cu->base_address = attr_value_as_address (attr);
4343 cu->base_known = 1;
4344 }
4345 }
4346 }
4347
4348 /* Read in the comp unit header information from the debug_info at info_ptr.
4349 NOTE: This leaves members offset, first_die_offset to be filled in
4350 by the caller. */
4351
4352 static const gdb_byte *
4353 read_comp_unit_head (struct comp_unit_head *cu_header,
4354 const gdb_byte *info_ptr, bfd *abfd)
4355 {
4356 int signed_addr;
4357 unsigned int bytes_read;
4358
4359 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4360 cu_header->initial_length_size = bytes_read;
4361 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4362 info_ptr += bytes_read;
4363 cu_header->version = read_2_bytes (abfd, info_ptr);
4364 info_ptr += 2;
4365 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4366 &bytes_read);
4367 info_ptr += bytes_read;
4368 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4369 info_ptr += 1;
4370 signed_addr = bfd_get_sign_extend_vma (abfd);
4371 if (signed_addr < 0)
4372 internal_error (__FILE__, __LINE__,
4373 _("read_comp_unit_head: dwarf from non elf file"));
4374 cu_header->signed_addr_p = signed_addr;
4375
4376 return info_ptr;
4377 }
4378
4379 /* Helper function that returns the proper abbrev section for
4380 THIS_CU. */
4381
4382 static struct dwarf2_section_info *
4383 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4384 {
4385 struct dwarf2_section_info *abbrev;
4386
4387 if (this_cu->is_dwz)
4388 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4389 else
4390 abbrev = &dwarf2_per_objfile->abbrev;
4391
4392 return abbrev;
4393 }
4394
4395 /* Subroutine of read_and_check_comp_unit_head and
4396 read_and_check_type_unit_head to simplify them.
4397 Perform various error checking on the header. */
4398
4399 static void
4400 error_check_comp_unit_head (struct comp_unit_head *header,
4401 struct dwarf2_section_info *section,
4402 struct dwarf2_section_info *abbrev_section)
4403 {
4404 const char *filename = get_section_file_name (section);
4405
4406 if (header->version != 2 && header->version != 3 && header->version != 4)
4407 error (_("Dwarf Error: wrong version in compilation unit header "
4408 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4409 filename);
4410
4411 if (header->abbrev_offset.sect_off
4412 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4413 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4414 "(offset 0x%lx + 6) [in module %s]"),
4415 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
4416 filename);
4417
4418 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4419 avoid potential 32-bit overflow. */
4420 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4421 > section->size)
4422 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4423 "(offset 0x%lx + 0) [in module %s]"),
4424 (long) header->length, (long) header->offset.sect_off,
4425 filename);
4426 }
4427
4428 /* Read in a CU/TU header and perform some basic error checking.
4429 The contents of the header are stored in HEADER.
4430 The result is a pointer to the start of the first DIE. */
4431
4432 static const gdb_byte *
4433 read_and_check_comp_unit_head (struct comp_unit_head *header,
4434 struct dwarf2_section_info *section,
4435 struct dwarf2_section_info *abbrev_section,
4436 const gdb_byte *info_ptr,
4437 int is_debug_types_section)
4438 {
4439 const gdb_byte *beg_of_comp_unit = info_ptr;
4440 bfd *abfd = get_section_bfd_owner (section);
4441
4442 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4443
4444 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4445
4446 /* If we're reading a type unit, skip over the signature and
4447 type_offset fields. */
4448 if (is_debug_types_section)
4449 info_ptr += 8 /*signature*/ + header->offset_size;
4450
4451 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4452
4453 error_check_comp_unit_head (header, section, abbrev_section);
4454
4455 return info_ptr;
4456 }
4457
4458 /* Read in the types comp unit header information from .debug_types entry at
4459 types_ptr. The result is a pointer to one past the end of the header. */
4460
4461 static const gdb_byte *
4462 read_and_check_type_unit_head (struct comp_unit_head *header,
4463 struct dwarf2_section_info *section,
4464 struct dwarf2_section_info *abbrev_section,
4465 const gdb_byte *info_ptr,
4466 ULONGEST *signature,
4467 cu_offset *type_offset_in_tu)
4468 {
4469 const gdb_byte *beg_of_comp_unit = info_ptr;
4470 bfd *abfd = get_section_bfd_owner (section);
4471
4472 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4473
4474 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4475
4476 /* If we're reading a type unit, skip over the signature and
4477 type_offset fields. */
4478 if (signature != NULL)
4479 *signature = read_8_bytes (abfd, info_ptr);
4480 info_ptr += 8;
4481 if (type_offset_in_tu != NULL)
4482 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4483 header->offset_size);
4484 info_ptr += header->offset_size;
4485
4486 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4487
4488 error_check_comp_unit_head (header, section, abbrev_section);
4489
4490 return info_ptr;
4491 }
4492
4493 /* Fetch the abbreviation table offset from a comp or type unit header. */
4494
4495 static sect_offset
4496 read_abbrev_offset (struct dwarf2_section_info *section,
4497 sect_offset offset)
4498 {
4499 bfd *abfd = get_section_bfd_owner (section);
4500 const gdb_byte *info_ptr;
4501 unsigned int initial_length_size, offset_size;
4502 sect_offset abbrev_offset;
4503
4504 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4505 info_ptr = section->buffer + offset.sect_off;
4506 read_initial_length (abfd, info_ptr, &initial_length_size);
4507 offset_size = initial_length_size == 4 ? 4 : 8;
4508 info_ptr += initial_length_size + 2 /*version*/;
4509 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4510 return abbrev_offset;
4511 }
4512
4513 /* Allocate a new partial symtab for file named NAME and mark this new
4514 partial symtab as being an include of PST. */
4515
4516 static void
4517 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4518 struct objfile *objfile)
4519 {
4520 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4521
4522 if (!IS_ABSOLUTE_PATH (subpst->filename))
4523 {
4524 /* It shares objfile->objfile_obstack. */
4525 subpst->dirname = pst->dirname;
4526 }
4527
4528 subpst->textlow = 0;
4529 subpst->texthigh = 0;
4530
4531 subpst->dependencies
4532 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4533 subpst->dependencies[0] = pst;
4534 subpst->number_of_dependencies = 1;
4535
4536 subpst->globals_offset = 0;
4537 subpst->n_global_syms = 0;
4538 subpst->statics_offset = 0;
4539 subpst->n_static_syms = 0;
4540 subpst->compunit_symtab = NULL;
4541 subpst->read_symtab = pst->read_symtab;
4542 subpst->readin = 0;
4543
4544 /* No private part is necessary for include psymtabs. This property
4545 can be used to differentiate between such include psymtabs and
4546 the regular ones. */
4547 subpst->read_symtab_private = NULL;
4548 }
4549
4550 /* Read the Line Number Program data and extract the list of files
4551 included by the source file represented by PST. Build an include
4552 partial symtab for each of these included files. */
4553
4554 static void
4555 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4556 struct die_info *die,
4557 struct partial_symtab *pst)
4558 {
4559 struct line_header *lh = NULL;
4560 struct attribute *attr;
4561
4562 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4563 if (attr)
4564 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4565 if (lh == NULL)
4566 return; /* No linetable, so no includes. */
4567
4568 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4569 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
4570
4571 free_line_header (lh);
4572 }
4573
4574 static hashval_t
4575 hash_signatured_type (const void *item)
4576 {
4577 const struct signatured_type *sig_type
4578 = (const struct signatured_type *) item;
4579
4580 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4581 return sig_type->signature;
4582 }
4583
4584 static int
4585 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4586 {
4587 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4588 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4589
4590 return lhs->signature == rhs->signature;
4591 }
4592
4593 /* Allocate a hash table for signatured types. */
4594
4595 static htab_t
4596 allocate_signatured_type_table (struct objfile *objfile)
4597 {
4598 return htab_create_alloc_ex (41,
4599 hash_signatured_type,
4600 eq_signatured_type,
4601 NULL,
4602 &objfile->objfile_obstack,
4603 hashtab_obstack_allocate,
4604 dummy_obstack_deallocate);
4605 }
4606
4607 /* A helper function to add a signatured type CU to a table. */
4608
4609 static int
4610 add_signatured_type_cu_to_table (void **slot, void *datum)
4611 {
4612 struct signatured_type *sigt = (struct signatured_type *) *slot;
4613 struct signatured_type ***datap = (struct signatured_type ***) datum;
4614
4615 **datap = sigt;
4616 ++*datap;
4617
4618 return 1;
4619 }
4620
4621 /* Create the hash table of all entries in the .debug_types
4622 (or .debug_types.dwo) section(s).
4623 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4624 otherwise it is NULL.
4625
4626 The result is a pointer to the hash table or NULL if there are no types.
4627
4628 Note: This function processes DWO files only, not DWP files. */
4629
4630 static htab_t
4631 create_debug_types_hash_table (struct dwo_file *dwo_file,
4632 VEC (dwarf2_section_info_def) *types)
4633 {
4634 struct objfile *objfile = dwarf2_per_objfile->objfile;
4635 htab_t types_htab = NULL;
4636 int ix;
4637 struct dwarf2_section_info *section;
4638 struct dwarf2_section_info *abbrev_section;
4639
4640 if (VEC_empty (dwarf2_section_info_def, types))
4641 return NULL;
4642
4643 abbrev_section = (dwo_file != NULL
4644 ? &dwo_file->sections.abbrev
4645 : &dwarf2_per_objfile->abbrev);
4646
4647 if (dwarf_read_debug)
4648 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4649 dwo_file ? ".dwo" : "",
4650 get_section_file_name (abbrev_section));
4651
4652 for (ix = 0;
4653 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4654 ++ix)
4655 {
4656 bfd *abfd;
4657 const gdb_byte *info_ptr, *end_ptr;
4658
4659 dwarf2_read_section (objfile, section);
4660 info_ptr = section->buffer;
4661
4662 if (info_ptr == NULL)
4663 continue;
4664
4665 /* We can't set abfd until now because the section may be empty or
4666 not present, in which case the bfd is unknown. */
4667 abfd = get_section_bfd_owner (section);
4668
4669 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4670 because we don't need to read any dies: the signature is in the
4671 header. */
4672
4673 end_ptr = info_ptr + section->size;
4674 while (info_ptr < end_ptr)
4675 {
4676 sect_offset offset;
4677 cu_offset type_offset_in_tu;
4678 ULONGEST signature;
4679 struct signatured_type *sig_type;
4680 struct dwo_unit *dwo_tu;
4681 void **slot;
4682 const gdb_byte *ptr = info_ptr;
4683 struct comp_unit_head header;
4684 unsigned int length;
4685
4686 offset.sect_off = ptr - section->buffer;
4687
4688 /* We need to read the type's signature in order to build the hash
4689 table, but we don't need anything else just yet. */
4690
4691 ptr = read_and_check_type_unit_head (&header, section,
4692 abbrev_section, ptr,
4693 &signature, &type_offset_in_tu);
4694
4695 length = get_cu_length (&header);
4696
4697 /* Skip dummy type units. */
4698 if (ptr >= info_ptr + length
4699 || peek_abbrev_code (abfd, ptr) == 0)
4700 {
4701 info_ptr += length;
4702 continue;
4703 }
4704
4705 if (types_htab == NULL)
4706 {
4707 if (dwo_file)
4708 types_htab = allocate_dwo_unit_table (objfile);
4709 else
4710 types_htab = allocate_signatured_type_table (objfile);
4711 }
4712
4713 if (dwo_file)
4714 {
4715 sig_type = NULL;
4716 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4717 struct dwo_unit);
4718 dwo_tu->dwo_file = dwo_file;
4719 dwo_tu->signature = signature;
4720 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4721 dwo_tu->section = section;
4722 dwo_tu->offset = offset;
4723 dwo_tu->length = length;
4724 }
4725 else
4726 {
4727 /* N.B.: type_offset is not usable if this type uses a DWO file.
4728 The real type_offset is in the DWO file. */
4729 dwo_tu = NULL;
4730 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4731 struct signatured_type);
4732 sig_type->signature = signature;
4733 sig_type->type_offset_in_tu = type_offset_in_tu;
4734 sig_type->per_cu.objfile = objfile;
4735 sig_type->per_cu.is_debug_types = 1;
4736 sig_type->per_cu.section = section;
4737 sig_type->per_cu.offset = offset;
4738 sig_type->per_cu.length = length;
4739 }
4740
4741 slot = htab_find_slot (types_htab,
4742 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4743 INSERT);
4744 gdb_assert (slot != NULL);
4745 if (*slot != NULL)
4746 {
4747 sect_offset dup_offset;
4748
4749 if (dwo_file)
4750 {
4751 const struct dwo_unit *dup_tu
4752 = (const struct dwo_unit *) *slot;
4753
4754 dup_offset = dup_tu->offset;
4755 }
4756 else
4757 {
4758 const struct signatured_type *dup_tu
4759 = (const struct signatured_type *) *slot;
4760
4761 dup_offset = dup_tu->per_cu.offset;
4762 }
4763
4764 complaint (&symfile_complaints,
4765 _("debug type entry at offset 0x%x is duplicate to"
4766 " the entry at offset 0x%x, signature %s"),
4767 offset.sect_off, dup_offset.sect_off,
4768 hex_string (signature));
4769 }
4770 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4771
4772 if (dwarf_read_debug > 1)
4773 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4774 offset.sect_off,
4775 hex_string (signature));
4776
4777 info_ptr += length;
4778 }
4779 }
4780
4781 return types_htab;
4782 }
4783
4784 /* Create the hash table of all entries in the .debug_types section,
4785 and initialize all_type_units.
4786 The result is zero if there is an error (e.g. missing .debug_types section),
4787 otherwise non-zero. */
4788
4789 static int
4790 create_all_type_units (struct objfile *objfile)
4791 {
4792 htab_t types_htab;
4793 struct signatured_type **iter;
4794
4795 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4796 if (types_htab == NULL)
4797 {
4798 dwarf2_per_objfile->signatured_types = NULL;
4799 return 0;
4800 }
4801
4802 dwarf2_per_objfile->signatured_types = types_htab;
4803
4804 dwarf2_per_objfile->n_type_units
4805 = dwarf2_per_objfile->n_allocated_type_units
4806 = htab_elements (types_htab);
4807 dwarf2_per_objfile->all_type_units =
4808 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
4809 iter = &dwarf2_per_objfile->all_type_units[0];
4810 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4811 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4812 == dwarf2_per_objfile->n_type_units);
4813
4814 return 1;
4815 }
4816
4817 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4818 If SLOT is non-NULL, it is the entry to use in the hash table.
4819 Otherwise we find one. */
4820
4821 static struct signatured_type *
4822 add_type_unit (ULONGEST sig, void **slot)
4823 {
4824 struct objfile *objfile = dwarf2_per_objfile->objfile;
4825 int n_type_units = dwarf2_per_objfile->n_type_units;
4826 struct signatured_type *sig_type;
4827
4828 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4829 ++n_type_units;
4830 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4831 {
4832 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4833 dwarf2_per_objfile->n_allocated_type_units = 1;
4834 dwarf2_per_objfile->n_allocated_type_units *= 2;
4835 dwarf2_per_objfile->all_type_units
4836 = XRESIZEVEC (struct signatured_type *,
4837 dwarf2_per_objfile->all_type_units,
4838 dwarf2_per_objfile->n_allocated_type_units);
4839 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4840 }
4841 dwarf2_per_objfile->n_type_units = n_type_units;
4842
4843 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4844 struct signatured_type);
4845 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4846 sig_type->signature = sig;
4847 sig_type->per_cu.is_debug_types = 1;
4848 if (dwarf2_per_objfile->using_index)
4849 {
4850 sig_type->per_cu.v.quick =
4851 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4852 struct dwarf2_per_cu_quick_data);
4853 }
4854
4855 if (slot == NULL)
4856 {
4857 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4858 sig_type, INSERT);
4859 }
4860 gdb_assert (*slot == NULL);
4861 *slot = sig_type;
4862 /* The rest of sig_type must be filled in by the caller. */
4863 return sig_type;
4864 }
4865
4866 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4867 Fill in SIG_ENTRY with DWO_ENTRY. */
4868
4869 static void
4870 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4871 struct signatured_type *sig_entry,
4872 struct dwo_unit *dwo_entry)
4873 {
4874 /* Make sure we're not clobbering something we don't expect to. */
4875 gdb_assert (! sig_entry->per_cu.queued);
4876 gdb_assert (sig_entry->per_cu.cu == NULL);
4877 if (dwarf2_per_objfile->using_index)
4878 {
4879 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4880 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
4881 }
4882 else
4883 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
4884 gdb_assert (sig_entry->signature == dwo_entry->signature);
4885 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4886 gdb_assert (sig_entry->type_unit_group == NULL);
4887 gdb_assert (sig_entry->dwo_unit == NULL);
4888
4889 sig_entry->per_cu.section = dwo_entry->section;
4890 sig_entry->per_cu.offset = dwo_entry->offset;
4891 sig_entry->per_cu.length = dwo_entry->length;
4892 sig_entry->per_cu.reading_dwo_directly = 1;
4893 sig_entry->per_cu.objfile = objfile;
4894 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4895 sig_entry->dwo_unit = dwo_entry;
4896 }
4897
4898 /* Subroutine of lookup_signatured_type.
4899 If we haven't read the TU yet, create the signatured_type data structure
4900 for a TU to be read in directly from a DWO file, bypassing the stub.
4901 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4902 using .gdb_index, then when reading a CU we want to stay in the DWO file
4903 containing that CU. Otherwise we could end up reading several other DWO
4904 files (due to comdat folding) to process the transitive closure of all the
4905 mentioned TUs, and that can be slow. The current DWO file will have every
4906 type signature that it needs.
4907 We only do this for .gdb_index because in the psymtab case we already have
4908 to read all the DWOs to build the type unit groups. */
4909
4910 static struct signatured_type *
4911 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4912 {
4913 struct objfile *objfile = dwarf2_per_objfile->objfile;
4914 struct dwo_file *dwo_file;
4915 struct dwo_unit find_dwo_entry, *dwo_entry;
4916 struct signatured_type find_sig_entry, *sig_entry;
4917 void **slot;
4918
4919 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4920
4921 /* If TU skeletons have been removed then we may not have read in any
4922 TUs yet. */
4923 if (dwarf2_per_objfile->signatured_types == NULL)
4924 {
4925 dwarf2_per_objfile->signatured_types
4926 = allocate_signatured_type_table (objfile);
4927 }
4928
4929 /* We only ever need to read in one copy of a signatured type.
4930 Use the global signatured_types array to do our own comdat-folding
4931 of types. If this is the first time we're reading this TU, and
4932 the TU has an entry in .gdb_index, replace the recorded data from
4933 .gdb_index with this TU. */
4934
4935 find_sig_entry.signature = sig;
4936 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4937 &find_sig_entry, INSERT);
4938 sig_entry = (struct signatured_type *) *slot;
4939
4940 /* We can get here with the TU already read, *or* in the process of being
4941 read. Don't reassign the global entry to point to this DWO if that's
4942 the case. Also note that if the TU is already being read, it may not
4943 have come from a DWO, the program may be a mix of Fission-compiled
4944 code and non-Fission-compiled code. */
4945
4946 /* Have we already tried to read this TU?
4947 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
4948 needn't exist in the global table yet). */
4949 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
4950 return sig_entry;
4951
4952 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4953 dwo_unit of the TU itself. */
4954 dwo_file = cu->dwo_unit->dwo_file;
4955
4956 /* Ok, this is the first time we're reading this TU. */
4957 if (dwo_file->tus == NULL)
4958 return NULL;
4959 find_dwo_entry.signature = sig;
4960 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
4961 if (dwo_entry == NULL)
4962 return NULL;
4963
4964 /* If the global table doesn't have an entry for this TU, add one. */
4965 if (sig_entry == NULL)
4966 sig_entry = add_type_unit (sig, slot);
4967
4968 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4969 sig_entry->per_cu.tu_read = 1;
4970 return sig_entry;
4971 }
4972
4973 /* Subroutine of lookup_signatured_type.
4974 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4975 then try the DWP file. If the TU stub (skeleton) has been removed then
4976 it won't be in .gdb_index. */
4977
4978 static struct signatured_type *
4979 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4980 {
4981 struct objfile *objfile = dwarf2_per_objfile->objfile;
4982 struct dwp_file *dwp_file = get_dwp_file ();
4983 struct dwo_unit *dwo_entry;
4984 struct signatured_type find_sig_entry, *sig_entry;
4985 void **slot;
4986
4987 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4988 gdb_assert (dwp_file != NULL);
4989
4990 /* If TU skeletons have been removed then we may not have read in any
4991 TUs yet. */
4992 if (dwarf2_per_objfile->signatured_types == NULL)
4993 {
4994 dwarf2_per_objfile->signatured_types
4995 = allocate_signatured_type_table (objfile);
4996 }
4997
4998 find_sig_entry.signature = sig;
4999 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5000 &find_sig_entry, INSERT);
5001 sig_entry = (struct signatured_type *) *slot;
5002
5003 /* Have we already tried to read this TU?
5004 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5005 needn't exist in the global table yet). */
5006 if (sig_entry != NULL)
5007 return sig_entry;
5008
5009 if (dwp_file->tus == NULL)
5010 return NULL;
5011 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5012 sig, 1 /* is_debug_types */);
5013 if (dwo_entry == NULL)
5014 return NULL;
5015
5016 sig_entry = add_type_unit (sig, slot);
5017 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5018
5019 return sig_entry;
5020 }
5021
5022 /* Lookup a signature based type for DW_FORM_ref_sig8.
5023 Returns NULL if signature SIG is not present in the table.
5024 It is up to the caller to complain about this. */
5025
5026 static struct signatured_type *
5027 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5028 {
5029 if (cu->dwo_unit
5030 && dwarf2_per_objfile->using_index)
5031 {
5032 /* We're in a DWO/DWP file, and we're using .gdb_index.
5033 These cases require special processing. */
5034 if (get_dwp_file () == NULL)
5035 return lookup_dwo_signatured_type (cu, sig);
5036 else
5037 return lookup_dwp_signatured_type (cu, sig);
5038 }
5039 else
5040 {
5041 struct signatured_type find_entry, *entry;
5042
5043 if (dwarf2_per_objfile->signatured_types == NULL)
5044 return NULL;
5045 find_entry.signature = sig;
5046 entry = ((struct signatured_type *)
5047 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5048 return entry;
5049 }
5050 }
5051 \f
5052 /* Low level DIE reading support. */
5053
5054 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5055
5056 static void
5057 init_cu_die_reader (struct die_reader_specs *reader,
5058 struct dwarf2_cu *cu,
5059 struct dwarf2_section_info *section,
5060 struct dwo_file *dwo_file)
5061 {
5062 gdb_assert (section->readin && section->buffer != NULL);
5063 reader->abfd = get_section_bfd_owner (section);
5064 reader->cu = cu;
5065 reader->dwo_file = dwo_file;
5066 reader->die_section = section;
5067 reader->buffer = section->buffer;
5068 reader->buffer_end = section->buffer + section->size;
5069 reader->comp_dir = NULL;
5070 }
5071
5072 /* Subroutine of init_cutu_and_read_dies to simplify it.
5073 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5074 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5075 already.
5076
5077 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5078 from it to the DIE in the DWO. If NULL we are skipping the stub.
5079 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5080 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5081 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5082 STUB_COMP_DIR may be non-NULL.
5083 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5084 are filled in with the info of the DIE from the DWO file.
5085 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5086 provided an abbrev table to use.
5087 The result is non-zero if a valid (non-dummy) DIE was found. */
5088
5089 static int
5090 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5091 struct dwo_unit *dwo_unit,
5092 int abbrev_table_provided,
5093 struct die_info *stub_comp_unit_die,
5094 const char *stub_comp_dir,
5095 struct die_reader_specs *result_reader,
5096 const gdb_byte **result_info_ptr,
5097 struct die_info **result_comp_unit_die,
5098 int *result_has_children)
5099 {
5100 struct objfile *objfile = dwarf2_per_objfile->objfile;
5101 struct dwarf2_cu *cu = this_cu->cu;
5102 struct dwarf2_section_info *section;
5103 bfd *abfd;
5104 const gdb_byte *begin_info_ptr, *info_ptr;
5105 ULONGEST signature; /* Or dwo_id. */
5106 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5107 int i,num_extra_attrs;
5108 struct dwarf2_section_info *dwo_abbrev_section;
5109 struct attribute *attr;
5110 struct die_info *comp_unit_die;
5111
5112 /* At most one of these may be provided. */
5113 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5114
5115 /* These attributes aren't processed until later:
5116 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5117 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5118 referenced later. However, these attributes are found in the stub
5119 which we won't have later. In order to not impose this complication
5120 on the rest of the code, we read them here and copy them to the
5121 DWO CU/TU die. */
5122
5123 stmt_list = NULL;
5124 low_pc = NULL;
5125 high_pc = NULL;
5126 ranges = NULL;
5127 comp_dir = NULL;
5128
5129 if (stub_comp_unit_die != NULL)
5130 {
5131 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5132 DWO file. */
5133 if (! this_cu->is_debug_types)
5134 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5135 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5136 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5137 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5138 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5139
5140 /* There should be a DW_AT_addr_base attribute here (if needed).
5141 We need the value before we can process DW_FORM_GNU_addr_index. */
5142 cu->addr_base = 0;
5143 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5144 if (attr)
5145 cu->addr_base = DW_UNSND (attr);
5146
5147 /* There should be a DW_AT_ranges_base attribute here (if needed).
5148 We need the value before we can process DW_AT_ranges. */
5149 cu->ranges_base = 0;
5150 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5151 if (attr)
5152 cu->ranges_base = DW_UNSND (attr);
5153 }
5154 else if (stub_comp_dir != NULL)
5155 {
5156 /* Reconstruct the comp_dir attribute to simplify the code below. */
5157 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5158 comp_dir->name = DW_AT_comp_dir;
5159 comp_dir->form = DW_FORM_string;
5160 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5161 DW_STRING (comp_dir) = stub_comp_dir;
5162 }
5163
5164 /* Set up for reading the DWO CU/TU. */
5165 cu->dwo_unit = dwo_unit;
5166 section = dwo_unit->section;
5167 dwarf2_read_section (objfile, section);
5168 abfd = get_section_bfd_owner (section);
5169 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5170 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5171 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5172
5173 if (this_cu->is_debug_types)
5174 {
5175 ULONGEST header_signature;
5176 cu_offset type_offset_in_tu;
5177 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5178
5179 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5180 dwo_abbrev_section,
5181 info_ptr,
5182 &header_signature,
5183 &type_offset_in_tu);
5184 /* This is not an assert because it can be caused by bad debug info. */
5185 if (sig_type->signature != header_signature)
5186 {
5187 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5188 " TU at offset 0x%x [in module %s]"),
5189 hex_string (sig_type->signature),
5190 hex_string (header_signature),
5191 dwo_unit->offset.sect_off,
5192 bfd_get_filename (abfd));
5193 }
5194 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5195 /* For DWOs coming from DWP files, we don't know the CU length
5196 nor the type's offset in the TU until now. */
5197 dwo_unit->length = get_cu_length (&cu->header);
5198 dwo_unit->type_offset_in_tu = type_offset_in_tu;
5199
5200 /* Establish the type offset that can be used to lookup the type.
5201 For DWO files, we don't know it until now. */
5202 sig_type->type_offset_in_section.sect_off =
5203 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5204 }
5205 else
5206 {
5207 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5208 dwo_abbrev_section,
5209 info_ptr, 0);
5210 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5211 /* For DWOs coming from DWP files, we don't know the CU length
5212 until now. */
5213 dwo_unit->length = get_cu_length (&cu->header);
5214 }
5215
5216 /* Replace the CU's original abbrev table with the DWO's.
5217 Reminder: We can't read the abbrev table until we've read the header. */
5218 if (abbrev_table_provided)
5219 {
5220 /* Don't free the provided abbrev table, the caller of
5221 init_cutu_and_read_dies owns it. */
5222 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5223 /* Ensure the DWO abbrev table gets freed. */
5224 make_cleanup (dwarf2_free_abbrev_table, cu);
5225 }
5226 else
5227 {
5228 dwarf2_free_abbrev_table (cu);
5229 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5230 /* Leave any existing abbrev table cleanup as is. */
5231 }
5232
5233 /* Read in the die, but leave space to copy over the attributes
5234 from the stub. This has the benefit of simplifying the rest of
5235 the code - all the work to maintain the illusion of a single
5236 DW_TAG_{compile,type}_unit DIE is done here. */
5237 num_extra_attrs = ((stmt_list != NULL)
5238 + (low_pc != NULL)
5239 + (high_pc != NULL)
5240 + (ranges != NULL)
5241 + (comp_dir != NULL));
5242 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5243 result_has_children, num_extra_attrs);
5244
5245 /* Copy over the attributes from the stub to the DIE we just read in. */
5246 comp_unit_die = *result_comp_unit_die;
5247 i = comp_unit_die->num_attrs;
5248 if (stmt_list != NULL)
5249 comp_unit_die->attrs[i++] = *stmt_list;
5250 if (low_pc != NULL)
5251 comp_unit_die->attrs[i++] = *low_pc;
5252 if (high_pc != NULL)
5253 comp_unit_die->attrs[i++] = *high_pc;
5254 if (ranges != NULL)
5255 comp_unit_die->attrs[i++] = *ranges;
5256 if (comp_dir != NULL)
5257 comp_unit_die->attrs[i++] = *comp_dir;
5258 comp_unit_die->num_attrs += num_extra_attrs;
5259
5260 if (dwarf_die_debug)
5261 {
5262 fprintf_unfiltered (gdb_stdlog,
5263 "Read die from %s@0x%x of %s:\n",
5264 get_section_name (section),
5265 (unsigned) (begin_info_ptr - section->buffer),
5266 bfd_get_filename (abfd));
5267 dump_die (comp_unit_die, dwarf_die_debug);
5268 }
5269
5270 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5271 TUs by skipping the stub and going directly to the entry in the DWO file.
5272 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5273 to get it via circuitous means. Blech. */
5274 if (comp_dir != NULL)
5275 result_reader->comp_dir = DW_STRING (comp_dir);
5276
5277 /* Skip dummy compilation units. */
5278 if (info_ptr >= begin_info_ptr + dwo_unit->length
5279 || peek_abbrev_code (abfd, info_ptr) == 0)
5280 return 0;
5281
5282 *result_info_ptr = info_ptr;
5283 return 1;
5284 }
5285
5286 /* Subroutine of init_cutu_and_read_dies to simplify it.
5287 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5288 Returns NULL if the specified DWO unit cannot be found. */
5289
5290 static struct dwo_unit *
5291 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5292 struct die_info *comp_unit_die)
5293 {
5294 struct dwarf2_cu *cu = this_cu->cu;
5295 struct attribute *attr;
5296 ULONGEST signature;
5297 struct dwo_unit *dwo_unit;
5298 const char *comp_dir, *dwo_name;
5299
5300 gdb_assert (cu != NULL);
5301
5302 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5303 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5304 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5305
5306 if (this_cu->is_debug_types)
5307 {
5308 struct signatured_type *sig_type;
5309
5310 /* Since this_cu is the first member of struct signatured_type,
5311 we can go from a pointer to one to a pointer to the other. */
5312 sig_type = (struct signatured_type *) this_cu;
5313 signature = sig_type->signature;
5314 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5315 }
5316 else
5317 {
5318 struct attribute *attr;
5319
5320 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5321 if (! attr)
5322 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5323 " [in module %s]"),
5324 dwo_name, objfile_name (this_cu->objfile));
5325 signature = DW_UNSND (attr);
5326 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5327 signature);
5328 }
5329
5330 return dwo_unit;
5331 }
5332
5333 /* Subroutine of init_cutu_and_read_dies to simplify it.
5334 See it for a description of the parameters.
5335 Read a TU directly from a DWO file, bypassing the stub.
5336
5337 Note: This function could be a little bit simpler if we shared cleanups
5338 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5339 to do, so we keep this function self-contained. Or we could move this
5340 into our caller, but it's complex enough already. */
5341
5342 static void
5343 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5344 int use_existing_cu, int keep,
5345 die_reader_func_ftype *die_reader_func,
5346 void *data)
5347 {
5348 struct dwarf2_cu *cu;
5349 struct signatured_type *sig_type;
5350 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5351 struct die_reader_specs reader;
5352 const gdb_byte *info_ptr;
5353 struct die_info *comp_unit_die;
5354 int has_children;
5355
5356 /* Verify we can do the following downcast, and that we have the
5357 data we need. */
5358 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5359 sig_type = (struct signatured_type *) this_cu;
5360 gdb_assert (sig_type->dwo_unit != NULL);
5361
5362 cleanups = make_cleanup (null_cleanup, NULL);
5363
5364 if (use_existing_cu && this_cu->cu != NULL)
5365 {
5366 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5367 cu = this_cu->cu;
5368 /* There's no need to do the rereading_dwo_cu handling that
5369 init_cutu_and_read_dies does since we don't read the stub. */
5370 }
5371 else
5372 {
5373 /* If !use_existing_cu, this_cu->cu must be NULL. */
5374 gdb_assert (this_cu->cu == NULL);
5375 cu = XNEW (struct dwarf2_cu);
5376 init_one_comp_unit (cu, this_cu);
5377 /* If an error occurs while loading, release our storage. */
5378 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5379 }
5380
5381 /* A future optimization, if needed, would be to use an existing
5382 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5383 could share abbrev tables. */
5384
5385 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5386 0 /* abbrev_table_provided */,
5387 NULL /* stub_comp_unit_die */,
5388 sig_type->dwo_unit->dwo_file->comp_dir,
5389 &reader, &info_ptr,
5390 &comp_unit_die, &has_children) == 0)
5391 {
5392 /* Dummy die. */
5393 do_cleanups (cleanups);
5394 return;
5395 }
5396
5397 /* All the "real" work is done here. */
5398 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5399
5400 /* This duplicates the code in init_cutu_and_read_dies,
5401 but the alternative is making the latter more complex.
5402 This function is only for the special case of using DWO files directly:
5403 no point in overly complicating the general case just to handle this. */
5404 if (free_cu_cleanup != NULL)
5405 {
5406 if (keep)
5407 {
5408 /* We've successfully allocated this compilation unit. Let our
5409 caller clean it up when finished with it. */
5410 discard_cleanups (free_cu_cleanup);
5411
5412 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5413 So we have to manually free the abbrev table. */
5414 dwarf2_free_abbrev_table (cu);
5415
5416 /* Link this CU into read_in_chain. */
5417 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5418 dwarf2_per_objfile->read_in_chain = this_cu;
5419 }
5420 else
5421 do_cleanups (free_cu_cleanup);
5422 }
5423
5424 do_cleanups (cleanups);
5425 }
5426
5427 /* Initialize a CU (or TU) and read its DIEs.
5428 If the CU defers to a DWO file, read the DWO file as well.
5429
5430 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5431 Otherwise the table specified in the comp unit header is read in and used.
5432 This is an optimization for when we already have the abbrev table.
5433
5434 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5435 Otherwise, a new CU is allocated with xmalloc.
5436
5437 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5438 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5439
5440 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5441 linker) then DIE_READER_FUNC will not get called. */
5442
5443 static void
5444 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5445 struct abbrev_table *abbrev_table,
5446 int use_existing_cu, int keep,
5447 die_reader_func_ftype *die_reader_func,
5448 void *data)
5449 {
5450 struct objfile *objfile = dwarf2_per_objfile->objfile;
5451 struct dwarf2_section_info *section = this_cu->section;
5452 bfd *abfd = get_section_bfd_owner (section);
5453 struct dwarf2_cu *cu;
5454 const gdb_byte *begin_info_ptr, *info_ptr;
5455 struct die_reader_specs reader;
5456 struct die_info *comp_unit_die;
5457 int has_children;
5458 struct attribute *attr;
5459 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5460 struct signatured_type *sig_type = NULL;
5461 struct dwarf2_section_info *abbrev_section;
5462 /* Non-zero if CU currently points to a DWO file and we need to
5463 reread it. When this happens we need to reread the skeleton die
5464 before we can reread the DWO file (this only applies to CUs, not TUs). */
5465 int rereading_dwo_cu = 0;
5466
5467 if (dwarf_die_debug)
5468 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5469 this_cu->is_debug_types ? "type" : "comp",
5470 this_cu->offset.sect_off);
5471
5472 if (use_existing_cu)
5473 gdb_assert (keep);
5474
5475 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5476 file (instead of going through the stub), short-circuit all of this. */
5477 if (this_cu->reading_dwo_directly)
5478 {
5479 /* Narrow down the scope of possibilities to have to understand. */
5480 gdb_assert (this_cu->is_debug_types);
5481 gdb_assert (abbrev_table == NULL);
5482 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5483 die_reader_func, data);
5484 return;
5485 }
5486
5487 cleanups = make_cleanup (null_cleanup, NULL);
5488
5489 /* This is cheap if the section is already read in. */
5490 dwarf2_read_section (objfile, section);
5491
5492 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5493
5494 abbrev_section = get_abbrev_section_for_cu (this_cu);
5495
5496 if (use_existing_cu && this_cu->cu != NULL)
5497 {
5498 cu = this_cu->cu;
5499 /* If this CU is from a DWO file we need to start over, we need to
5500 refetch the attributes from the skeleton CU.
5501 This could be optimized by retrieving those attributes from when we
5502 were here the first time: the previous comp_unit_die was stored in
5503 comp_unit_obstack. But there's no data yet that we need this
5504 optimization. */
5505 if (cu->dwo_unit != NULL)
5506 rereading_dwo_cu = 1;
5507 }
5508 else
5509 {
5510 /* If !use_existing_cu, this_cu->cu must be NULL. */
5511 gdb_assert (this_cu->cu == NULL);
5512 cu = XNEW (struct dwarf2_cu);
5513 init_one_comp_unit (cu, this_cu);
5514 /* If an error occurs while loading, release our storage. */
5515 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5516 }
5517
5518 /* Get the header. */
5519 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5520 {
5521 /* We already have the header, there's no need to read it in again. */
5522 info_ptr += cu->header.first_die_offset.cu_off;
5523 }
5524 else
5525 {
5526 if (this_cu->is_debug_types)
5527 {
5528 ULONGEST signature;
5529 cu_offset type_offset_in_tu;
5530
5531 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5532 abbrev_section, info_ptr,
5533 &signature,
5534 &type_offset_in_tu);
5535
5536 /* Since per_cu is the first member of struct signatured_type,
5537 we can go from a pointer to one to a pointer to the other. */
5538 sig_type = (struct signatured_type *) this_cu;
5539 gdb_assert (sig_type->signature == signature);
5540 gdb_assert (sig_type->type_offset_in_tu.cu_off
5541 == type_offset_in_tu.cu_off);
5542 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5543
5544 /* LENGTH has not been set yet for type units if we're
5545 using .gdb_index. */
5546 this_cu->length = get_cu_length (&cu->header);
5547
5548 /* Establish the type offset that can be used to lookup the type. */
5549 sig_type->type_offset_in_section.sect_off =
5550 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5551 }
5552 else
5553 {
5554 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5555 abbrev_section,
5556 info_ptr, 0);
5557
5558 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5559 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5560 }
5561 }
5562
5563 /* Skip dummy compilation units. */
5564 if (info_ptr >= begin_info_ptr + this_cu->length
5565 || peek_abbrev_code (abfd, info_ptr) == 0)
5566 {
5567 do_cleanups (cleanups);
5568 return;
5569 }
5570
5571 /* If we don't have them yet, read the abbrevs for this compilation unit.
5572 And if we need to read them now, make sure they're freed when we're
5573 done. Note that it's important that if the CU had an abbrev table
5574 on entry we don't free it when we're done: Somewhere up the call stack
5575 it may be in use. */
5576 if (abbrev_table != NULL)
5577 {
5578 gdb_assert (cu->abbrev_table == NULL);
5579 gdb_assert (cu->header.abbrev_offset.sect_off
5580 == abbrev_table->offset.sect_off);
5581 cu->abbrev_table = abbrev_table;
5582 }
5583 else if (cu->abbrev_table == NULL)
5584 {
5585 dwarf2_read_abbrevs (cu, abbrev_section);
5586 make_cleanup (dwarf2_free_abbrev_table, cu);
5587 }
5588 else if (rereading_dwo_cu)
5589 {
5590 dwarf2_free_abbrev_table (cu);
5591 dwarf2_read_abbrevs (cu, abbrev_section);
5592 }
5593
5594 /* Read the top level CU/TU die. */
5595 init_cu_die_reader (&reader, cu, section, NULL);
5596 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5597
5598 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5599 from the DWO file.
5600 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5601 DWO CU, that this test will fail (the attribute will not be present). */
5602 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5603 if (attr)
5604 {
5605 struct dwo_unit *dwo_unit;
5606 struct die_info *dwo_comp_unit_die;
5607
5608 if (has_children)
5609 {
5610 complaint (&symfile_complaints,
5611 _("compilation unit with DW_AT_GNU_dwo_name"
5612 " has children (offset 0x%x) [in module %s]"),
5613 this_cu->offset.sect_off, bfd_get_filename (abfd));
5614 }
5615 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5616 if (dwo_unit != NULL)
5617 {
5618 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5619 abbrev_table != NULL,
5620 comp_unit_die, NULL,
5621 &reader, &info_ptr,
5622 &dwo_comp_unit_die, &has_children) == 0)
5623 {
5624 /* Dummy die. */
5625 do_cleanups (cleanups);
5626 return;
5627 }
5628 comp_unit_die = dwo_comp_unit_die;
5629 }
5630 else
5631 {
5632 /* Yikes, we couldn't find the rest of the DIE, we only have
5633 the stub. A complaint has already been logged. There's
5634 not much more we can do except pass on the stub DIE to
5635 die_reader_func. We don't want to throw an error on bad
5636 debug info. */
5637 }
5638 }
5639
5640 /* All of the above is setup for this call. Yikes. */
5641 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5642
5643 /* Done, clean up. */
5644 if (free_cu_cleanup != NULL)
5645 {
5646 if (keep)
5647 {
5648 /* We've successfully allocated this compilation unit. Let our
5649 caller clean it up when finished with it. */
5650 discard_cleanups (free_cu_cleanup);
5651
5652 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5653 So we have to manually free the abbrev table. */
5654 dwarf2_free_abbrev_table (cu);
5655
5656 /* Link this CU into read_in_chain. */
5657 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5658 dwarf2_per_objfile->read_in_chain = this_cu;
5659 }
5660 else
5661 do_cleanups (free_cu_cleanup);
5662 }
5663
5664 do_cleanups (cleanups);
5665 }
5666
5667 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5668 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5669 to have already done the lookup to find the DWO file).
5670
5671 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5672 THIS_CU->is_debug_types, but nothing else.
5673
5674 We fill in THIS_CU->length.
5675
5676 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5677 linker) then DIE_READER_FUNC will not get called.
5678
5679 THIS_CU->cu is always freed when done.
5680 This is done in order to not leave THIS_CU->cu in a state where we have
5681 to care whether it refers to the "main" CU or the DWO CU. */
5682
5683 static void
5684 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5685 struct dwo_file *dwo_file,
5686 die_reader_func_ftype *die_reader_func,
5687 void *data)
5688 {
5689 struct objfile *objfile = dwarf2_per_objfile->objfile;
5690 struct dwarf2_section_info *section = this_cu->section;
5691 bfd *abfd = get_section_bfd_owner (section);
5692 struct dwarf2_section_info *abbrev_section;
5693 struct dwarf2_cu cu;
5694 const gdb_byte *begin_info_ptr, *info_ptr;
5695 struct die_reader_specs reader;
5696 struct cleanup *cleanups;
5697 struct die_info *comp_unit_die;
5698 int has_children;
5699
5700 if (dwarf_die_debug)
5701 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5702 this_cu->is_debug_types ? "type" : "comp",
5703 this_cu->offset.sect_off);
5704
5705 gdb_assert (this_cu->cu == NULL);
5706
5707 abbrev_section = (dwo_file != NULL
5708 ? &dwo_file->sections.abbrev
5709 : get_abbrev_section_for_cu (this_cu));
5710
5711 /* This is cheap if the section is already read in. */
5712 dwarf2_read_section (objfile, section);
5713
5714 init_one_comp_unit (&cu, this_cu);
5715
5716 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5717
5718 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5719 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5720 abbrev_section, info_ptr,
5721 this_cu->is_debug_types);
5722
5723 this_cu->length = get_cu_length (&cu.header);
5724
5725 /* Skip dummy compilation units. */
5726 if (info_ptr >= begin_info_ptr + this_cu->length
5727 || peek_abbrev_code (abfd, info_ptr) == 0)
5728 {
5729 do_cleanups (cleanups);
5730 return;
5731 }
5732
5733 dwarf2_read_abbrevs (&cu, abbrev_section);
5734 make_cleanup (dwarf2_free_abbrev_table, &cu);
5735
5736 init_cu_die_reader (&reader, &cu, section, dwo_file);
5737 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5738
5739 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5740
5741 do_cleanups (cleanups);
5742 }
5743
5744 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5745 does not lookup the specified DWO file.
5746 This cannot be used to read DWO files.
5747
5748 THIS_CU->cu is always freed when done.
5749 This is done in order to not leave THIS_CU->cu in a state where we have
5750 to care whether it refers to the "main" CU or the DWO CU.
5751 We can revisit this if the data shows there's a performance issue. */
5752
5753 static void
5754 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5755 die_reader_func_ftype *die_reader_func,
5756 void *data)
5757 {
5758 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5759 }
5760 \f
5761 /* Type Unit Groups.
5762
5763 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5764 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5765 so that all types coming from the same compilation (.o file) are grouped
5766 together. A future step could be to put the types in the same symtab as
5767 the CU the types ultimately came from. */
5768
5769 static hashval_t
5770 hash_type_unit_group (const void *item)
5771 {
5772 const struct type_unit_group *tu_group
5773 = (const struct type_unit_group *) item;
5774
5775 return hash_stmt_list_entry (&tu_group->hash);
5776 }
5777
5778 static int
5779 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5780 {
5781 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5782 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
5783
5784 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5785 }
5786
5787 /* Allocate a hash table for type unit groups. */
5788
5789 static htab_t
5790 allocate_type_unit_groups_table (void)
5791 {
5792 return htab_create_alloc_ex (3,
5793 hash_type_unit_group,
5794 eq_type_unit_group,
5795 NULL,
5796 &dwarf2_per_objfile->objfile->objfile_obstack,
5797 hashtab_obstack_allocate,
5798 dummy_obstack_deallocate);
5799 }
5800
5801 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5802 partial symtabs. We combine several TUs per psymtab to not let the size
5803 of any one psymtab grow too big. */
5804 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5805 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5806
5807 /* Helper routine for get_type_unit_group.
5808 Create the type_unit_group object used to hold one or more TUs. */
5809
5810 static struct type_unit_group *
5811 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5812 {
5813 struct objfile *objfile = dwarf2_per_objfile->objfile;
5814 struct dwarf2_per_cu_data *per_cu;
5815 struct type_unit_group *tu_group;
5816
5817 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5818 struct type_unit_group);
5819 per_cu = &tu_group->per_cu;
5820 per_cu->objfile = objfile;
5821
5822 if (dwarf2_per_objfile->using_index)
5823 {
5824 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5825 struct dwarf2_per_cu_quick_data);
5826 }
5827 else
5828 {
5829 unsigned int line_offset = line_offset_struct.sect_off;
5830 struct partial_symtab *pst;
5831 char *name;
5832
5833 /* Give the symtab a useful name for debug purposes. */
5834 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5835 name = xstrprintf ("<type_units_%d>",
5836 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5837 else
5838 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5839
5840 pst = create_partial_symtab (per_cu, name);
5841 pst->anonymous = 1;
5842
5843 xfree (name);
5844 }
5845
5846 tu_group->hash.dwo_unit = cu->dwo_unit;
5847 tu_group->hash.line_offset = line_offset_struct;
5848
5849 return tu_group;
5850 }
5851
5852 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5853 STMT_LIST is a DW_AT_stmt_list attribute. */
5854
5855 static struct type_unit_group *
5856 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5857 {
5858 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5859 struct type_unit_group *tu_group;
5860 void **slot;
5861 unsigned int line_offset;
5862 struct type_unit_group type_unit_group_for_lookup;
5863
5864 if (dwarf2_per_objfile->type_unit_groups == NULL)
5865 {
5866 dwarf2_per_objfile->type_unit_groups =
5867 allocate_type_unit_groups_table ();
5868 }
5869
5870 /* Do we need to create a new group, or can we use an existing one? */
5871
5872 if (stmt_list)
5873 {
5874 line_offset = DW_UNSND (stmt_list);
5875 ++tu_stats->nr_symtab_sharers;
5876 }
5877 else
5878 {
5879 /* Ugh, no stmt_list. Rare, but we have to handle it.
5880 We can do various things here like create one group per TU or
5881 spread them over multiple groups to split up the expansion work.
5882 To avoid worst case scenarios (too many groups or too large groups)
5883 we, umm, group them in bunches. */
5884 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5885 | (tu_stats->nr_stmt_less_type_units
5886 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5887 ++tu_stats->nr_stmt_less_type_units;
5888 }
5889
5890 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5891 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5892 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5893 &type_unit_group_for_lookup, INSERT);
5894 if (*slot != NULL)
5895 {
5896 tu_group = (struct type_unit_group *) *slot;
5897 gdb_assert (tu_group != NULL);
5898 }
5899 else
5900 {
5901 sect_offset line_offset_struct;
5902
5903 line_offset_struct.sect_off = line_offset;
5904 tu_group = create_type_unit_group (cu, line_offset_struct);
5905 *slot = tu_group;
5906 ++tu_stats->nr_symtabs;
5907 }
5908
5909 return tu_group;
5910 }
5911 \f
5912 /* Partial symbol tables. */
5913
5914 /* Create a psymtab named NAME and assign it to PER_CU.
5915
5916 The caller must fill in the following details:
5917 dirname, textlow, texthigh. */
5918
5919 static struct partial_symtab *
5920 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5921 {
5922 struct objfile *objfile = per_cu->objfile;
5923 struct partial_symtab *pst;
5924
5925 pst = start_psymtab_common (objfile, name, 0,
5926 objfile->global_psymbols.next,
5927 objfile->static_psymbols.next);
5928
5929 pst->psymtabs_addrmap_supported = 1;
5930
5931 /* This is the glue that links PST into GDB's symbol API. */
5932 pst->read_symtab_private = per_cu;
5933 pst->read_symtab = dwarf2_read_symtab;
5934 per_cu->v.psymtab = pst;
5935
5936 return pst;
5937 }
5938
5939 /* The DATA object passed to process_psymtab_comp_unit_reader has this
5940 type. */
5941
5942 struct process_psymtab_comp_unit_data
5943 {
5944 /* True if we are reading a DW_TAG_partial_unit. */
5945
5946 int want_partial_unit;
5947
5948 /* The "pretend" language that is used if the CU doesn't declare a
5949 language. */
5950
5951 enum language pretend_language;
5952 };
5953
5954 /* die_reader_func for process_psymtab_comp_unit. */
5955
5956 static void
5957 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5958 const gdb_byte *info_ptr,
5959 struct die_info *comp_unit_die,
5960 int has_children,
5961 void *data)
5962 {
5963 struct dwarf2_cu *cu = reader->cu;
5964 struct objfile *objfile = cu->objfile;
5965 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5966 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5967 CORE_ADDR baseaddr;
5968 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5969 struct partial_symtab *pst;
5970 enum pc_bounds_kind cu_bounds_kind;
5971 const char *filename;
5972 struct process_psymtab_comp_unit_data *info
5973 = (struct process_psymtab_comp_unit_data *) data;
5974
5975 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
5976 return;
5977
5978 gdb_assert (! per_cu->is_debug_types);
5979
5980 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
5981
5982 cu->list_in_scope = &file_symbols;
5983
5984 /* Allocate a new partial symbol table structure. */
5985 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
5986 if (filename == NULL)
5987 filename = "";
5988
5989 pst = create_partial_symtab (per_cu, filename);
5990
5991 /* This must be done before calling dwarf2_build_include_psymtabs. */
5992 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5993
5994 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5995
5996 dwarf2_find_base_address (comp_unit_die, cu);
5997
5998 /* Possibly set the default values of LOWPC and HIGHPC from
5999 `DW_AT_ranges'. */
6000 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6001 &best_highpc, cu, pst);
6002 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6003 /* Store the contiguous range if it is not empty; it can be empty for
6004 CUs with no code. */
6005 addrmap_set_empty (objfile->psymtabs_addrmap,
6006 gdbarch_adjust_dwarf2_addr (gdbarch,
6007 best_lowpc + baseaddr),
6008 gdbarch_adjust_dwarf2_addr (gdbarch,
6009 best_highpc + baseaddr) - 1,
6010 pst);
6011
6012 /* Check if comp unit has_children.
6013 If so, read the rest of the partial symbols from this comp unit.
6014 If not, there's no more debug_info for this comp unit. */
6015 if (has_children)
6016 {
6017 struct partial_die_info *first_die;
6018 CORE_ADDR lowpc, highpc;
6019
6020 lowpc = ((CORE_ADDR) -1);
6021 highpc = ((CORE_ADDR) 0);
6022
6023 first_die = load_partial_dies (reader, info_ptr, 1);
6024
6025 scan_partial_symbols (first_die, &lowpc, &highpc,
6026 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6027
6028 /* If we didn't find a lowpc, set it to highpc to avoid
6029 complaints from `maint check'. */
6030 if (lowpc == ((CORE_ADDR) -1))
6031 lowpc = highpc;
6032
6033 /* If the compilation unit didn't have an explicit address range,
6034 then use the information extracted from its child dies. */
6035 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6036 {
6037 best_lowpc = lowpc;
6038 best_highpc = highpc;
6039 }
6040 }
6041 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6042 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6043
6044 end_psymtab_common (objfile, pst);
6045
6046 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6047 {
6048 int i;
6049 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6050 struct dwarf2_per_cu_data *iter;
6051
6052 /* Fill in 'dependencies' here; we fill in 'users' in a
6053 post-pass. */
6054 pst->number_of_dependencies = len;
6055 pst->dependencies =
6056 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6057 for (i = 0;
6058 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6059 i, iter);
6060 ++i)
6061 pst->dependencies[i] = iter->v.psymtab;
6062
6063 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6064 }
6065
6066 /* Get the list of files included in the current compilation unit,
6067 and build a psymtab for each of them. */
6068 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6069
6070 if (dwarf_read_debug)
6071 {
6072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6073
6074 fprintf_unfiltered (gdb_stdlog,
6075 "Psymtab for %s unit @0x%x: %s - %s"
6076 ", %d global, %d static syms\n",
6077 per_cu->is_debug_types ? "type" : "comp",
6078 per_cu->offset.sect_off,
6079 paddress (gdbarch, pst->textlow),
6080 paddress (gdbarch, pst->texthigh),
6081 pst->n_global_syms, pst->n_static_syms);
6082 }
6083 }
6084
6085 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6086 Process compilation unit THIS_CU for a psymtab. */
6087
6088 static void
6089 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6090 int want_partial_unit,
6091 enum language pretend_language)
6092 {
6093 struct process_psymtab_comp_unit_data info;
6094
6095 /* If this compilation unit was already read in, free the
6096 cached copy in order to read it in again. This is
6097 necessary because we skipped some symbols when we first
6098 read in the compilation unit (see load_partial_dies).
6099 This problem could be avoided, but the benefit is unclear. */
6100 if (this_cu->cu != NULL)
6101 free_one_cached_comp_unit (this_cu);
6102
6103 gdb_assert (! this_cu->is_debug_types);
6104 info.want_partial_unit = want_partial_unit;
6105 info.pretend_language = pretend_language;
6106 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6107 process_psymtab_comp_unit_reader,
6108 &info);
6109
6110 /* Age out any secondary CUs. */
6111 age_cached_comp_units ();
6112 }
6113
6114 /* Reader function for build_type_psymtabs. */
6115
6116 static void
6117 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6118 const gdb_byte *info_ptr,
6119 struct die_info *type_unit_die,
6120 int has_children,
6121 void *data)
6122 {
6123 struct objfile *objfile = dwarf2_per_objfile->objfile;
6124 struct dwarf2_cu *cu = reader->cu;
6125 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6126 struct signatured_type *sig_type;
6127 struct type_unit_group *tu_group;
6128 struct attribute *attr;
6129 struct partial_die_info *first_die;
6130 CORE_ADDR lowpc, highpc;
6131 struct partial_symtab *pst;
6132
6133 gdb_assert (data == NULL);
6134 gdb_assert (per_cu->is_debug_types);
6135 sig_type = (struct signatured_type *) per_cu;
6136
6137 if (! has_children)
6138 return;
6139
6140 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6141 tu_group = get_type_unit_group (cu, attr);
6142
6143 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6144
6145 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6146 cu->list_in_scope = &file_symbols;
6147 pst = create_partial_symtab (per_cu, "");
6148 pst->anonymous = 1;
6149
6150 first_die = load_partial_dies (reader, info_ptr, 1);
6151
6152 lowpc = (CORE_ADDR) -1;
6153 highpc = (CORE_ADDR) 0;
6154 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6155
6156 end_psymtab_common (objfile, pst);
6157 }
6158
6159 /* Struct used to sort TUs by their abbreviation table offset. */
6160
6161 struct tu_abbrev_offset
6162 {
6163 struct signatured_type *sig_type;
6164 sect_offset abbrev_offset;
6165 };
6166
6167 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6168
6169 static int
6170 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6171 {
6172 const struct tu_abbrev_offset * const *a
6173 = (const struct tu_abbrev_offset * const*) ap;
6174 const struct tu_abbrev_offset * const *b
6175 = (const struct tu_abbrev_offset * const*) bp;
6176 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6177 unsigned int boff = (*b)->abbrev_offset.sect_off;
6178
6179 return (aoff > boff) - (aoff < boff);
6180 }
6181
6182 /* Efficiently read all the type units.
6183 This does the bulk of the work for build_type_psymtabs.
6184
6185 The efficiency is because we sort TUs by the abbrev table they use and
6186 only read each abbrev table once. In one program there are 200K TUs
6187 sharing 8K abbrev tables.
6188
6189 The main purpose of this function is to support building the
6190 dwarf2_per_objfile->type_unit_groups table.
6191 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6192 can collapse the search space by grouping them by stmt_list.
6193 The savings can be significant, in the same program from above the 200K TUs
6194 share 8K stmt_list tables.
6195
6196 FUNC is expected to call get_type_unit_group, which will create the
6197 struct type_unit_group if necessary and add it to
6198 dwarf2_per_objfile->type_unit_groups. */
6199
6200 static void
6201 build_type_psymtabs_1 (void)
6202 {
6203 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6204 struct cleanup *cleanups;
6205 struct abbrev_table *abbrev_table;
6206 sect_offset abbrev_offset;
6207 struct tu_abbrev_offset *sorted_by_abbrev;
6208 int i;
6209
6210 /* It's up to the caller to not call us multiple times. */
6211 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6212
6213 if (dwarf2_per_objfile->n_type_units == 0)
6214 return;
6215
6216 /* TUs typically share abbrev tables, and there can be way more TUs than
6217 abbrev tables. Sort by abbrev table to reduce the number of times we
6218 read each abbrev table in.
6219 Alternatives are to punt or to maintain a cache of abbrev tables.
6220 This is simpler and efficient enough for now.
6221
6222 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6223 symtab to use). Typically TUs with the same abbrev offset have the same
6224 stmt_list value too so in practice this should work well.
6225
6226 The basic algorithm here is:
6227
6228 sort TUs by abbrev table
6229 for each TU with same abbrev table:
6230 read abbrev table if first user
6231 read TU top level DIE
6232 [IWBN if DWO skeletons had DW_AT_stmt_list]
6233 call FUNC */
6234
6235 if (dwarf_read_debug)
6236 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6237
6238 /* Sort in a separate table to maintain the order of all_type_units
6239 for .gdb_index: TU indices directly index all_type_units. */
6240 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6241 dwarf2_per_objfile->n_type_units);
6242 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6243 {
6244 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6245
6246 sorted_by_abbrev[i].sig_type = sig_type;
6247 sorted_by_abbrev[i].abbrev_offset =
6248 read_abbrev_offset (sig_type->per_cu.section,
6249 sig_type->per_cu.offset);
6250 }
6251 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6252 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6253 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6254
6255 abbrev_offset.sect_off = ~(unsigned) 0;
6256 abbrev_table = NULL;
6257 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6258
6259 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6260 {
6261 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6262
6263 /* Switch to the next abbrev table if necessary. */
6264 if (abbrev_table == NULL
6265 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6266 {
6267 if (abbrev_table != NULL)
6268 {
6269 abbrev_table_free (abbrev_table);
6270 /* Reset to NULL in case abbrev_table_read_table throws
6271 an error: abbrev_table_free_cleanup will get called. */
6272 abbrev_table = NULL;
6273 }
6274 abbrev_offset = tu->abbrev_offset;
6275 abbrev_table =
6276 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6277 abbrev_offset);
6278 ++tu_stats->nr_uniq_abbrev_tables;
6279 }
6280
6281 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6282 build_type_psymtabs_reader, NULL);
6283 }
6284
6285 do_cleanups (cleanups);
6286 }
6287
6288 /* Print collected type unit statistics. */
6289
6290 static void
6291 print_tu_stats (void)
6292 {
6293 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6294
6295 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6296 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6297 dwarf2_per_objfile->n_type_units);
6298 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6299 tu_stats->nr_uniq_abbrev_tables);
6300 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6301 tu_stats->nr_symtabs);
6302 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6303 tu_stats->nr_symtab_sharers);
6304 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6305 tu_stats->nr_stmt_less_type_units);
6306 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6307 tu_stats->nr_all_type_units_reallocs);
6308 }
6309
6310 /* Traversal function for build_type_psymtabs. */
6311
6312 static int
6313 build_type_psymtab_dependencies (void **slot, void *info)
6314 {
6315 struct objfile *objfile = dwarf2_per_objfile->objfile;
6316 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6317 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6318 struct partial_symtab *pst = per_cu->v.psymtab;
6319 int len = VEC_length (sig_type_ptr, tu_group->tus);
6320 struct signatured_type *iter;
6321 int i;
6322
6323 gdb_assert (len > 0);
6324 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6325
6326 pst->number_of_dependencies = len;
6327 pst->dependencies =
6328 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6329 for (i = 0;
6330 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6331 ++i)
6332 {
6333 gdb_assert (iter->per_cu.is_debug_types);
6334 pst->dependencies[i] = iter->per_cu.v.psymtab;
6335 iter->type_unit_group = tu_group;
6336 }
6337
6338 VEC_free (sig_type_ptr, tu_group->tus);
6339
6340 return 1;
6341 }
6342
6343 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6344 Build partial symbol tables for the .debug_types comp-units. */
6345
6346 static void
6347 build_type_psymtabs (struct objfile *objfile)
6348 {
6349 if (! create_all_type_units (objfile))
6350 return;
6351
6352 build_type_psymtabs_1 ();
6353 }
6354
6355 /* Traversal function for process_skeletonless_type_unit.
6356 Read a TU in a DWO file and build partial symbols for it. */
6357
6358 static int
6359 process_skeletonless_type_unit (void **slot, void *info)
6360 {
6361 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6362 struct objfile *objfile = (struct objfile *) info;
6363 struct signatured_type find_entry, *entry;
6364
6365 /* If this TU doesn't exist in the global table, add it and read it in. */
6366
6367 if (dwarf2_per_objfile->signatured_types == NULL)
6368 {
6369 dwarf2_per_objfile->signatured_types
6370 = allocate_signatured_type_table (objfile);
6371 }
6372
6373 find_entry.signature = dwo_unit->signature;
6374 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6375 INSERT);
6376 /* If we've already seen this type there's nothing to do. What's happening
6377 is we're doing our own version of comdat-folding here. */
6378 if (*slot != NULL)
6379 return 1;
6380
6381 /* This does the job that create_all_type_units would have done for
6382 this TU. */
6383 entry = add_type_unit (dwo_unit->signature, slot);
6384 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6385 *slot = entry;
6386
6387 /* This does the job that build_type_psymtabs_1 would have done. */
6388 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6389 build_type_psymtabs_reader, NULL);
6390
6391 return 1;
6392 }
6393
6394 /* Traversal function for process_skeletonless_type_units. */
6395
6396 static int
6397 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6398 {
6399 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6400
6401 if (dwo_file->tus != NULL)
6402 {
6403 htab_traverse_noresize (dwo_file->tus,
6404 process_skeletonless_type_unit, info);
6405 }
6406
6407 return 1;
6408 }
6409
6410 /* Scan all TUs of DWO files, verifying we've processed them.
6411 This is needed in case a TU was emitted without its skeleton.
6412 Note: This can't be done until we know what all the DWO files are. */
6413
6414 static void
6415 process_skeletonless_type_units (struct objfile *objfile)
6416 {
6417 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6418 if (get_dwp_file () == NULL
6419 && dwarf2_per_objfile->dwo_files != NULL)
6420 {
6421 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6422 process_dwo_file_for_skeletonless_type_units,
6423 objfile);
6424 }
6425 }
6426
6427 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6428
6429 static void
6430 psymtabs_addrmap_cleanup (void *o)
6431 {
6432 struct objfile *objfile = (struct objfile *) o;
6433
6434 objfile->psymtabs_addrmap = NULL;
6435 }
6436
6437 /* Compute the 'user' field for each psymtab in OBJFILE. */
6438
6439 static void
6440 set_partial_user (struct objfile *objfile)
6441 {
6442 int i;
6443
6444 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6445 {
6446 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6447 struct partial_symtab *pst = per_cu->v.psymtab;
6448 int j;
6449
6450 if (pst == NULL)
6451 continue;
6452
6453 for (j = 0; j < pst->number_of_dependencies; ++j)
6454 {
6455 /* Set the 'user' field only if it is not already set. */
6456 if (pst->dependencies[j]->user == NULL)
6457 pst->dependencies[j]->user = pst;
6458 }
6459 }
6460 }
6461
6462 /* Build the partial symbol table by doing a quick pass through the
6463 .debug_info and .debug_abbrev sections. */
6464
6465 static void
6466 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6467 {
6468 struct cleanup *back_to, *addrmap_cleanup;
6469 struct obstack temp_obstack;
6470 int i;
6471
6472 if (dwarf_read_debug)
6473 {
6474 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6475 objfile_name (objfile));
6476 }
6477
6478 dwarf2_per_objfile->reading_partial_symbols = 1;
6479
6480 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6481
6482 /* Any cached compilation units will be linked by the per-objfile
6483 read_in_chain. Make sure to free them when we're done. */
6484 back_to = make_cleanup (free_cached_comp_units, NULL);
6485
6486 build_type_psymtabs (objfile);
6487
6488 create_all_comp_units (objfile);
6489
6490 /* Create a temporary address map on a temporary obstack. We later
6491 copy this to the final obstack. */
6492 obstack_init (&temp_obstack);
6493 make_cleanup_obstack_free (&temp_obstack);
6494 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6495 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6496
6497 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6498 {
6499 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6500
6501 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6502 }
6503
6504 /* This has to wait until we read the CUs, we need the list of DWOs. */
6505 process_skeletonless_type_units (objfile);
6506
6507 /* Now that all TUs have been processed we can fill in the dependencies. */
6508 if (dwarf2_per_objfile->type_unit_groups != NULL)
6509 {
6510 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6511 build_type_psymtab_dependencies, NULL);
6512 }
6513
6514 if (dwarf_read_debug)
6515 print_tu_stats ();
6516
6517 set_partial_user (objfile);
6518
6519 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6520 &objfile->objfile_obstack);
6521 discard_cleanups (addrmap_cleanup);
6522
6523 do_cleanups (back_to);
6524
6525 if (dwarf_read_debug)
6526 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6527 objfile_name (objfile));
6528 }
6529
6530 /* die_reader_func for load_partial_comp_unit. */
6531
6532 static void
6533 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6534 const gdb_byte *info_ptr,
6535 struct die_info *comp_unit_die,
6536 int has_children,
6537 void *data)
6538 {
6539 struct dwarf2_cu *cu = reader->cu;
6540
6541 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6542
6543 /* Check if comp unit has_children.
6544 If so, read the rest of the partial symbols from this comp unit.
6545 If not, there's no more debug_info for this comp unit. */
6546 if (has_children)
6547 load_partial_dies (reader, info_ptr, 0);
6548 }
6549
6550 /* Load the partial DIEs for a secondary CU into memory.
6551 This is also used when rereading a primary CU with load_all_dies. */
6552
6553 static void
6554 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6555 {
6556 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6557 load_partial_comp_unit_reader, NULL);
6558 }
6559
6560 static void
6561 read_comp_units_from_section (struct objfile *objfile,
6562 struct dwarf2_section_info *section,
6563 unsigned int is_dwz,
6564 int *n_allocated,
6565 int *n_comp_units,
6566 struct dwarf2_per_cu_data ***all_comp_units)
6567 {
6568 const gdb_byte *info_ptr;
6569 bfd *abfd = get_section_bfd_owner (section);
6570
6571 if (dwarf_read_debug)
6572 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6573 get_section_name (section),
6574 get_section_file_name (section));
6575
6576 dwarf2_read_section (objfile, section);
6577
6578 info_ptr = section->buffer;
6579
6580 while (info_ptr < section->buffer + section->size)
6581 {
6582 unsigned int length, initial_length_size;
6583 struct dwarf2_per_cu_data *this_cu;
6584 sect_offset offset;
6585
6586 offset.sect_off = info_ptr - section->buffer;
6587
6588 /* Read just enough information to find out where the next
6589 compilation unit is. */
6590 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6591
6592 /* Save the compilation unit for later lookup. */
6593 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
6594 memset (this_cu, 0, sizeof (*this_cu));
6595 this_cu->offset = offset;
6596 this_cu->length = length + initial_length_size;
6597 this_cu->is_dwz = is_dwz;
6598 this_cu->objfile = objfile;
6599 this_cu->section = section;
6600
6601 if (*n_comp_units == *n_allocated)
6602 {
6603 *n_allocated *= 2;
6604 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6605 *all_comp_units, *n_allocated);
6606 }
6607 (*all_comp_units)[*n_comp_units] = this_cu;
6608 ++*n_comp_units;
6609
6610 info_ptr = info_ptr + this_cu->length;
6611 }
6612 }
6613
6614 /* Create a list of all compilation units in OBJFILE.
6615 This is only done for -readnow and building partial symtabs. */
6616
6617 static void
6618 create_all_comp_units (struct objfile *objfile)
6619 {
6620 int n_allocated;
6621 int n_comp_units;
6622 struct dwarf2_per_cu_data **all_comp_units;
6623 struct dwz_file *dwz;
6624
6625 n_comp_units = 0;
6626 n_allocated = 10;
6627 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6628
6629 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6630 &n_allocated, &n_comp_units, &all_comp_units);
6631
6632 dwz = dwarf2_get_dwz_file ();
6633 if (dwz != NULL)
6634 read_comp_units_from_section (objfile, &dwz->info, 1,
6635 &n_allocated, &n_comp_units,
6636 &all_comp_units);
6637
6638 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6639 struct dwarf2_per_cu_data *,
6640 n_comp_units);
6641 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6642 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6643 xfree (all_comp_units);
6644 dwarf2_per_objfile->n_comp_units = n_comp_units;
6645 }
6646
6647 /* Process all loaded DIEs for compilation unit CU, starting at
6648 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6649 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6650 DW_AT_ranges). See the comments of add_partial_subprogram on how
6651 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6652
6653 static void
6654 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6655 CORE_ADDR *highpc, int set_addrmap,
6656 struct dwarf2_cu *cu)
6657 {
6658 struct partial_die_info *pdi;
6659
6660 /* Now, march along the PDI's, descending into ones which have
6661 interesting children but skipping the children of the other ones,
6662 until we reach the end of the compilation unit. */
6663
6664 pdi = first_die;
6665
6666 while (pdi != NULL)
6667 {
6668 fixup_partial_die (pdi, cu);
6669
6670 /* Anonymous namespaces or modules have no name but have interesting
6671 children, so we need to look at them. Ditto for anonymous
6672 enums. */
6673
6674 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6675 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6676 || pdi->tag == DW_TAG_imported_unit)
6677 {
6678 switch (pdi->tag)
6679 {
6680 case DW_TAG_subprogram:
6681 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6682 break;
6683 case DW_TAG_constant:
6684 case DW_TAG_variable:
6685 case DW_TAG_typedef:
6686 case DW_TAG_union_type:
6687 if (!pdi->is_declaration)
6688 {
6689 add_partial_symbol (pdi, cu);
6690 }
6691 break;
6692 case DW_TAG_class_type:
6693 case DW_TAG_interface_type:
6694 case DW_TAG_structure_type:
6695 if (!pdi->is_declaration)
6696 {
6697 add_partial_symbol (pdi, cu);
6698 }
6699 if (cu->language == language_rust && pdi->has_children)
6700 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6701 set_addrmap, cu);
6702 break;
6703 case DW_TAG_enumeration_type:
6704 if (!pdi->is_declaration)
6705 add_partial_enumeration (pdi, cu);
6706 break;
6707 case DW_TAG_base_type:
6708 case DW_TAG_subrange_type:
6709 /* File scope base type definitions are added to the partial
6710 symbol table. */
6711 add_partial_symbol (pdi, cu);
6712 break;
6713 case DW_TAG_namespace:
6714 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6715 break;
6716 case DW_TAG_module:
6717 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6718 break;
6719 case DW_TAG_imported_unit:
6720 {
6721 struct dwarf2_per_cu_data *per_cu;
6722
6723 /* For now we don't handle imported units in type units. */
6724 if (cu->per_cu->is_debug_types)
6725 {
6726 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6727 " supported in type units [in module %s]"),
6728 objfile_name (cu->objfile));
6729 }
6730
6731 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6732 pdi->is_dwz,
6733 cu->objfile);
6734
6735 /* Go read the partial unit, if needed. */
6736 if (per_cu->v.psymtab == NULL)
6737 process_psymtab_comp_unit (per_cu, 1, cu->language);
6738
6739 VEC_safe_push (dwarf2_per_cu_ptr,
6740 cu->per_cu->imported_symtabs, per_cu);
6741 }
6742 break;
6743 case DW_TAG_imported_declaration:
6744 add_partial_symbol (pdi, cu);
6745 break;
6746 default:
6747 break;
6748 }
6749 }
6750
6751 /* If the die has a sibling, skip to the sibling. */
6752
6753 pdi = pdi->die_sibling;
6754 }
6755 }
6756
6757 /* Functions used to compute the fully scoped name of a partial DIE.
6758
6759 Normally, this is simple. For C++, the parent DIE's fully scoped
6760 name is concatenated with "::" and the partial DIE's name. For
6761 Java, the same thing occurs except that "." is used instead of "::".
6762 Enumerators are an exception; they use the scope of their parent
6763 enumeration type, i.e. the name of the enumeration type is not
6764 prepended to the enumerator.
6765
6766 There are two complexities. One is DW_AT_specification; in this
6767 case "parent" means the parent of the target of the specification,
6768 instead of the direct parent of the DIE. The other is compilers
6769 which do not emit DW_TAG_namespace; in this case we try to guess
6770 the fully qualified name of structure types from their members'
6771 linkage names. This must be done using the DIE's children rather
6772 than the children of any DW_AT_specification target. We only need
6773 to do this for structures at the top level, i.e. if the target of
6774 any DW_AT_specification (if any; otherwise the DIE itself) does not
6775 have a parent. */
6776
6777 /* Compute the scope prefix associated with PDI's parent, in
6778 compilation unit CU. The result will be allocated on CU's
6779 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6780 field. NULL is returned if no prefix is necessary. */
6781 static const char *
6782 partial_die_parent_scope (struct partial_die_info *pdi,
6783 struct dwarf2_cu *cu)
6784 {
6785 const char *grandparent_scope;
6786 struct partial_die_info *parent, *real_pdi;
6787
6788 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6789 then this means the parent of the specification DIE. */
6790
6791 real_pdi = pdi;
6792 while (real_pdi->has_specification)
6793 real_pdi = find_partial_die (real_pdi->spec_offset,
6794 real_pdi->spec_is_dwz, cu);
6795
6796 parent = real_pdi->die_parent;
6797 if (parent == NULL)
6798 return NULL;
6799
6800 if (parent->scope_set)
6801 return parent->scope;
6802
6803 fixup_partial_die (parent, cu);
6804
6805 grandparent_scope = partial_die_parent_scope (parent, cu);
6806
6807 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6808 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6809 Work around this problem here. */
6810 if (cu->language == language_cplus
6811 && parent->tag == DW_TAG_namespace
6812 && strcmp (parent->name, "::") == 0
6813 && grandparent_scope == NULL)
6814 {
6815 parent->scope = NULL;
6816 parent->scope_set = 1;
6817 return NULL;
6818 }
6819
6820 if (pdi->tag == DW_TAG_enumerator)
6821 /* Enumerators should not get the name of the enumeration as a prefix. */
6822 parent->scope = grandparent_scope;
6823 else if (parent->tag == DW_TAG_namespace
6824 || parent->tag == DW_TAG_module
6825 || parent->tag == DW_TAG_structure_type
6826 || parent->tag == DW_TAG_class_type
6827 || parent->tag == DW_TAG_interface_type
6828 || parent->tag == DW_TAG_union_type
6829 || parent->tag == DW_TAG_enumeration_type)
6830 {
6831 if (grandparent_scope == NULL)
6832 parent->scope = parent->name;
6833 else
6834 parent->scope = typename_concat (&cu->comp_unit_obstack,
6835 grandparent_scope,
6836 parent->name, 0, cu);
6837 }
6838 else
6839 {
6840 /* FIXME drow/2004-04-01: What should we be doing with
6841 function-local names? For partial symbols, we should probably be
6842 ignoring them. */
6843 complaint (&symfile_complaints,
6844 _("unhandled containing DIE tag %d for DIE at %d"),
6845 parent->tag, pdi->offset.sect_off);
6846 parent->scope = grandparent_scope;
6847 }
6848
6849 parent->scope_set = 1;
6850 return parent->scope;
6851 }
6852
6853 /* Return the fully scoped name associated with PDI, from compilation unit
6854 CU. The result will be allocated with malloc. */
6855
6856 static char *
6857 partial_die_full_name (struct partial_die_info *pdi,
6858 struct dwarf2_cu *cu)
6859 {
6860 const char *parent_scope;
6861
6862 /* If this is a template instantiation, we can not work out the
6863 template arguments from partial DIEs. So, unfortunately, we have
6864 to go through the full DIEs. At least any work we do building
6865 types here will be reused if full symbols are loaded later. */
6866 if (pdi->has_template_arguments)
6867 {
6868 fixup_partial_die (pdi, cu);
6869
6870 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6871 {
6872 struct die_info *die;
6873 struct attribute attr;
6874 struct dwarf2_cu *ref_cu = cu;
6875
6876 /* DW_FORM_ref_addr is using section offset. */
6877 attr.name = (enum dwarf_attribute) 0;
6878 attr.form = DW_FORM_ref_addr;
6879 attr.u.unsnd = pdi->offset.sect_off;
6880 die = follow_die_ref (NULL, &attr, &ref_cu);
6881
6882 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6883 }
6884 }
6885
6886 parent_scope = partial_die_parent_scope (pdi, cu);
6887 if (parent_scope == NULL)
6888 return NULL;
6889 else
6890 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6891 }
6892
6893 static void
6894 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6895 {
6896 struct objfile *objfile = cu->objfile;
6897 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6898 CORE_ADDR addr = 0;
6899 const char *actual_name = NULL;
6900 CORE_ADDR baseaddr;
6901 char *built_actual_name;
6902
6903 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6904
6905 built_actual_name = partial_die_full_name (pdi, cu);
6906 if (built_actual_name != NULL)
6907 actual_name = built_actual_name;
6908
6909 if (actual_name == NULL)
6910 actual_name = pdi->name;
6911
6912 switch (pdi->tag)
6913 {
6914 case DW_TAG_subprogram:
6915 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
6916 if (pdi->is_external || cu->language == language_ada)
6917 {
6918 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6919 of the global scope. But in Ada, we want to be able to access
6920 nested procedures globally. So all Ada subprograms are stored
6921 in the global scope. */
6922 add_psymbol_to_list (actual_name, strlen (actual_name),
6923 built_actual_name != NULL,
6924 VAR_DOMAIN, LOC_BLOCK,
6925 &objfile->global_psymbols,
6926 addr, cu->language, objfile);
6927 }
6928 else
6929 {
6930 add_psymbol_to_list (actual_name, strlen (actual_name),
6931 built_actual_name != NULL,
6932 VAR_DOMAIN, LOC_BLOCK,
6933 &objfile->static_psymbols,
6934 addr, cu->language, objfile);
6935 }
6936 break;
6937 case DW_TAG_constant:
6938 {
6939 struct psymbol_allocation_list *list;
6940
6941 if (pdi->is_external)
6942 list = &objfile->global_psymbols;
6943 else
6944 list = &objfile->static_psymbols;
6945 add_psymbol_to_list (actual_name, strlen (actual_name),
6946 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6947 list, 0, cu->language, objfile);
6948 }
6949 break;
6950 case DW_TAG_variable:
6951 if (pdi->d.locdesc)
6952 addr = decode_locdesc (pdi->d.locdesc, cu);
6953
6954 if (pdi->d.locdesc
6955 && addr == 0
6956 && !dwarf2_per_objfile->has_section_at_zero)
6957 {
6958 /* A global or static variable may also have been stripped
6959 out by the linker if unused, in which case its address
6960 will be nullified; do not add such variables into partial
6961 symbol table then. */
6962 }
6963 else if (pdi->is_external)
6964 {
6965 /* Global Variable.
6966 Don't enter into the minimal symbol tables as there is
6967 a minimal symbol table entry from the ELF symbols already.
6968 Enter into partial symbol table if it has a location
6969 descriptor or a type.
6970 If the location descriptor is missing, new_symbol will create
6971 a LOC_UNRESOLVED symbol, the address of the variable will then
6972 be determined from the minimal symbol table whenever the variable
6973 is referenced.
6974 The address for the partial symbol table entry is not
6975 used by GDB, but it comes in handy for debugging partial symbol
6976 table building. */
6977
6978 if (pdi->d.locdesc || pdi->has_type)
6979 add_psymbol_to_list (actual_name, strlen (actual_name),
6980 built_actual_name != NULL,
6981 VAR_DOMAIN, LOC_STATIC,
6982 &objfile->global_psymbols,
6983 addr + baseaddr,
6984 cu->language, objfile);
6985 }
6986 else
6987 {
6988 int has_loc = pdi->d.locdesc != NULL;
6989
6990 /* Static Variable. Skip symbols whose value we cannot know (those
6991 without location descriptors or constant values). */
6992 if (!has_loc && !pdi->has_const_value)
6993 {
6994 xfree (built_actual_name);
6995 return;
6996 }
6997
6998 add_psymbol_to_list (actual_name, strlen (actual_name),
6999 built_actual_name != NULL,
7000 VAR_DOMAIN, LOC_STATIC,
7001 &objfile->static_psymbols,
7002 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7003 cu->language, objfile);
7004 }
7005 break;
7006 case DW_TAG_typedef:
7007 case DW_TAG_base_type:
7008 case DW_TAG_subrange_type:
7009 add_psymbol_to_list (actual_name, strlen (actual_name),
7010 built_actual_name != NULL,
7011 VAR_DOMAIN, LOC_TYPEDEF,
7012 &objfile->static_psymbols,
7013 0, cu->language, objfile);
7014 break;
7015 case DW_TAG_imported_declaration:
7016 case DW_TAG_namespace:
7017 add_psymbol_to_list (actual_name, strlen (actual_name),
7018 built_actual_name != NULL,
7019 VAR_DOMAIN, LOC_TYPEDEF,
7020 &objfile->global_psymbols,
7021 0, cu->language, objfile);
7022 break;
7023 case DW_TAG_module:
7024 add_psymbol_to_list (actual_name, strlen (actual_name),
7025 built_actual_name != NULL,
7026 MODULE_DOMAIN, LOC_TYPEDEF,
7027 &objfile->global_psymbols,
7028 0, cu->language, objfile);
7029 break;
7030 case DW_TAG_class_type:
7031 case DW_TAG_interface_type:
7032 case DW_TAG_structure_type:
7033 case DW_TAG_union_type:
7034 case DW_TAG_enumeration_type:
7035 /* Skip external references. The DWARF standard says in the section
7036 about "Structure, Union, and Class Type Entries": "An incomplete
7037 structure, union or class type is represented by a structure,
7038 union or class entry that does not have a byte size attribute
7039 and that has a DW_AT_declaration attribute." */
7040 if (!pdi->has_byte_size && pdi->is_declaration)
7041 {
7042 xfree (built_actual_name);
7043 return;
7044 }
7045
7046 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7047 static vs. global. */
7048 add_psymbol_to_list (actual_name, strlen (actual_name),
7049 built_actual_name != NULL,
7050 STRUCT_DOMAIN, LOC_TYPEDEF,
7051 (cu->language == language_cplus
7052 || cu->language == language_java)
7053 ? &objfile->global_psymbols
7054 : &objfile->static_psymbols,
7055 0, cu->language, objfile);
7056
7057 break;
7058 case DW_TAG_enumerator:
7059 add_psymbol_to_list (actual_name, strlen (actual_name),
7060 built_actual_name != NULL,
7061 VAR_DOMAIN, LOC_CONST,
7062 (cu->language == language_cplus
7063 || cu->language == language_java)
7064 ? &objfile->global_psymbols
7065 : &objfile->static_psymbols,
7066 0, cu->language, objfile);
7067 break;
7068 default:
7069 break;
7070 }
7071
7072 xfree (built_actual_name);
7073 }
7074
7075 /* Read a partial die corresponding to a namespace; also, add a symbol
7076 corresponding to that namespace to the symbol table. NAMESPACE is
7077 the name of the enclosing namespace. */
7078
7079 static void
7080 add_partial_namespace (struct partial_die_info *pdi,
7081 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7082 int set_addrmap, struct dwarf2_cu *cu)
7083 {
7084 /* Add a symbol for the namespace. */
7085
7086 add_partial_symbol (pdi, cu);
7087
7088 /* Now scan partial symbols in that namespace. */
7089
7090 if (pdi->has_children)
7091 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7092 }
7093
7094 /* Read a partial die corresponding to a Fortran module. */
7095
7096 static void
7097 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7098 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7099 {
7100 /* Add a symbol for the namespace. */
7101
7102 add_partial_symbol (pdi, cu);
7103
7104 /* Now scan partial symbols in that module. */
7105
7106 if (pdi->has_children)
7107 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7108 }
7109
7110 /* Read a partial die corresponding to a subprogram and create a partial
7111 symbol for that subprogram. When the CU language allows it, this
7112 routine also defines a partial symbol for each nested subprogram
7113 that this subprogram contains. If SET_ADDRMAP is true, record the
7114 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7115 and highest PC values found in PDI.
7116
7117 PDI may also be a lexical block, in which case we simply search
7118 recursively for subprograms defined inside that lexical block.
7119 Again, this is only performed when the CU language allows this
7120 type of definitions. */
7121
7122 static void
7123 add_partial_subprogram (struct partial_die_info *pdi,
7124 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7125 int set_addrmap, struct dwarf2_cu *cu)
7126 {
7127 if (pdi->tag == DW_TAG_subprogram)
7128 {
7129 if (pdi->has_pc_info)
7130 {
7131 if (pdi->lowpc < *lowpc)
7132 *lowpc = pdi->lowpc;
7133 if (pdi->highpc > *highpc)
7134 *highpc = pdi->highpc;
7135 if (set_addrmap)
7136 {
7137 struct objfile *objfile = cu->objfile;
7138 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7139 CORE_ADDR baseaddr;
7140 CORE_ADDR highpc;
7141 CORE_ADDR lowpc;
7142
7143 baseaddr = ANOFFSET (objfile->section_offsets,
7144 SECT_OFF_TEXT (objfile));
7145 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7146 pdi->lowpc + baseaddr);
7147 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7148 pdi->highpc + baseaddr);
7149 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7150 cu->per_cu->v.psymtab);
7151 }
7152 }
7153
7154 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7155 {
7156 if (!pdi->is_declaration)
7157 /* Ignore subprogram DIEs that do not have a name, they are
7158 illegal. Do not emit a complaint at this point, we will
7159 do so when we convert this psymtab into a symtab. */
7160 if (pdi->name)
7161 add_partial_symbol (pdi, cu);
7162 }
7163 }
7164
7165 if (! pdi->has_children)
7166 return;
7167
7168 if (cu->language == language_ada)
7169 {
7170 pdi = pdi->die_child;
7171 while (pdi != NULL)
7172 {
7173 fixup_partial_die (pdi, cu);
7174 if (pdi->tag == DW_TAG_subprogram
7175 || pdi->tag == DW_TAG_lexical_block)
7176 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7177 pdi = pdi->die_sibling;
7178 }
7179 }
7180 }
7181
7182 /* Read a partial die corresponding to an enumeration type. */
7183
7184 static void
7185 add_partial_enumeration (struct partial_die_info *enum_pdi,
7186 struct dwarf2_cu *cu)
7187 {
7188 struct partial_die_info *pdi;
7189
7190 if (enum_pdi->name != NULL)
7191 add_partial_symbol (enum_pdi, cu);
7192
7193 pdi = enum_pdi->die_child;
7194 while (pdi)
7195 {
7196 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7197 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7198 else
7199 add_partial_symbol (pdi, cu);
7200 pdi = pdi->die_sibling;
7201 }
7202 }
7203
7204 /* Return the initial uleb128 in the die at INFO_PTR. */
7205
7206 static unsigned int
7207 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7208 {
7209 unsigned int bytes_read;
7210
7211 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7212 }
7213
7214 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7215 Return the corresponding abbrev, or NULL if the number is zero (indicating
7216 an empty DIE). In either case *BYTES_READ will be set to the length of
7217 the initial number. */
7218
7219 static struct abbrev_info *
7220 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7221 struct dwarf2_cu *cu)
7222 {
7223 bfd *abfd = cu->objfile->obfd;
7224 unsigned int abbrev_number;
7225 struct abbrev_info *abbrev;
7226
7227 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7228
7229 if (abbrev_number == 0)
7230 return NULL;
7231
7232 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7233 if (!abbrev)
7234 {
7235 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7236 " at offset 0x%x [in module %s]"),
7237 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7238 cu->header.offset.sect_off, bfd_get_filename (abfd));
7239 }
7240
7241 return abbrev;
7242 }
7243
7244 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7245 Returns a pointer to the end of a series of DIEs, terminated by an empty
7246 DIE. Any children of the skipped DIEs will also be skipped. */
7247
7248 static const gdb_byte *
7249 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7250 {
7251 struct dwarf2_cu *cu = reader->cu;
7252 struct abbrev_info *abbrev;
7253 unsigned int bytes_read;
7254
7255 while (1)
7256 {
7257 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7258 if (abbrev == NULL)
7259 return info_ptr + bytes_read;
7260 else
7261 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7262 }
7263 }
7264
7265 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7266 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7267 abbrev corresponding to that skipped uleb128 should be passed in
7268 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7269 children. */
7270
7271 static const gdb_byte *
7272 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7273 struct abbrev_info *abbrev)
7274 {
7275 unsigned int bytes_read;
7276 struct attribute attr;
7277 bfd *abfd = reader->abfd;
7278 struct dwarf2_cu *cu = reader->cu;
7279 const gdb_byte *buffer = reader->buffer;
7280 const gdb_byte *buffer_end = reader->buffer_end;
7281 unsigned int form, i;
7282
7283 for (i = 0; i < abbrev->num_attrs; i++)
7284 {
7285 /* The only abbrev we care about is DW_AT_sibling. */
7286 if (abbrev->attrs[i].name == DW_AT_sibling)
7287 {
7288 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7289 if (attr.form == DW_FORM_ref_addr)
7290 complaint (&symfile_complaints,
7291 _("ignoring absolute DW_AT_sibling"));
7292 else
7293 {
7294 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7295 const gdb_byte *sibling_ptr = buffer + off;
7296
7297 if (sibling_ptr < info_ptr)
7298 complaint (&symfile_complaints,
7299 _("DW_AT_sibling points backwards"));
7300 else if (sibling_ptr > reader->buffer_end)
7301 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7302 else
7303 return sibling_ptr;
7304 }
7305 }
7306
7307 /* If it isn't DW_AT_sibling, skip this attribute. */
7308 form = abbrev->attrs[i].form;
7309 skip_attribute:
7310 switch (form)
7311 {
7312 case DW_FORM_ref_addr:
7313 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7314 and later it is offset sized. */
7315 if (cu->header.version == 2)
7316 info_ptr += cu->header.addr_size;
7317 else
7318 info_ptr += cu->header.offset_size;
7319 break;
7320 case DW_FORM_GNU_ref_alt:
7321 info_ptr += cu->header.offset_size;
7322 break;
7323 case DW_FORM_addr:
7324 info_ptr += cu->header.addr_size;
7325 break;
7326 case DW_FORM_data1:
7327 case DW_FORM_ref1:
7328 case DW_FORM_flag:
7329 info_ptr += 1;
7330 break;
7331 case DW_FORM_flag_present:
7332 break;
7333 case DW_FORM_data2:
7334 case DW_FORM_ref2:
7335 info_ptr += 2;
7336 break;
7337 case DW_FORM_data4:
7338 case DW_FORM_ref4:
7339 info_ptr += 4;
7340 break;
7341 case DW_FORM_data8:
7342 case DW_FORM_ref8:
7343 case DW_FORM_ref_sig8:
7344 info_ptr += 8;
7345 break;
7346 case DW_FORM_string:
7347 read_direct_string (abfd, info_ptr, &bytes_read);
7348 info_ptr += bytes_read;
7349 break;
7350 case DW_FORM_sec_offset:
7351 case DW_FORM_strp:
7352 case DW_FORM_GNU_strp_alt:
7353 info_ptr += cu->header.offset_size;
7354 break;
7355 case DW_FORM_exprloc:
7356 case DW_FORM_block:
7357 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7358 info_ptr += bytes_read;
7359 break;
7360 case DW_FORM_block1:
7361 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7362 break;
7363 case DW_FORM_block2:
7364 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7365 break;
7366 case DW_FORM_block4:
7367 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7368 break;
7369 case DW_FORM_sdata:
7370 case DW_FORM_udata:
7371 case DW_FORM_ref_udata:
7372 case DW_FORM_GNU_addr_index:
7373 case DW_FORM_GNU_str_index:
7374 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7375 break;
7376 case DW_FORM_indirect:
7377 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7378 info_ptr += bytes_read;
7379 /* We need to continue parsing from here, so just go back to
7380 the top. */
7381 goto skip_attribute;
7382
7383 default:
7384 error (_("Dwarf Error: Cannot handle %s "
7385 "in DWARF reader [in module %s]"),
7386 dwarf_form_name (form),
7387 bfd_get_filename (abfd));
7388 }
7389 }
7390
7391 if (abbrev->has_children)
7392 return skip_children (reader, info_ptr);
7393 else
7394 return info_ptr;
7395 }
7396
7397 /* Locate ORIG_PDI's sibling.
7398 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7399
7400 static const gdb_byte *
7401 locate_pdi_sibling (const struct die_reader_specs *reader,
7402 struct partial_die_info *orig_pdi,
7403 const gdb_byte *info_ptr)
7404 {
7405 /* Do we know the sibling already? */
7406
7407 if (orig_pdi->sibling)
7408 return orig_pdi->sibling;
7409
7410 /* Are there any children to deal with? */
7411
7412 if (!orig_pdi->has_children)
7413 return info_ptr;
7414
7415 /* Skip the children the long way. */
7416
7417 return skip_children (reader, info_ptr);
7418 }
7419
7420 /* Expand this partial symbol table into a full symbol table. SELF is
7421 not NULL. */
7422
7423 static void
7424 dwarf2_read_symtab (struct partial_symtab *self,
7425 struct objfile *objfile)
7426 {
7427 if (self->readin)
7428 {
7429 warning (_("bug: psymtab for %s is already read in."),
7430 self->filename);
7431 }
7432 else
7433 {
7434 if (info_verbose)
7435 {
7436 printf_filtered (_("Reading in symbols for %s..."),
7437 self->filename);
7438 gdb_flush (gdb_stdout);
7439 }
7440
7441 /* Restore our global data. */
7442 dwarf2_per_objfile
7443 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7444 dwarf2_objfile_data_key);
7445
7446 /* If this psymtab is constructed from a debug-only objfile, the
7447 has_section_at_zero flag will not necessarily be correct. We
7448 can get the correct value for this flag by looking at the data
7449 associated with the (presumably stripped) associated objfile. */
7450 if (objfile->separate_debug_objfile_backlink)
7451 {
7452 struct dwarf2_per_objfile *dpo_backlink
7453 = ((struct dwarf2_per_objfile *)
7454 objfile_data (objfile->separate_debug_objfile_backlink,
7455 dwarf2_objfile_data_key));
7456
7457 dwarf2_per_objfile->has_section_at_zero
7458 = dpo_backlink->has_section_at_zero;
7459 }
7460
7461 dwarf2_per_objfile->reading_partial_symbols = 0;
7462
7463 psymtab_to_symtab_1 (self);
7464
7465 /* Finish up the debug error message. */
7466 if (info_verbose)
7467 printf_filtered (_("done.\n"));
7468 }
7469
7470 process_cu_includes ();
7471 }
7472 \f
7473 /* Reading in full CUs. */
7474
7475 /* Add PER_CU to the queue. */
7476
7477 static void
7478 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7479 enum language pretend_language)
7480 {
7481 struct dwarf2_queue_item *item;
7482
7483 per_cu->queued = 1;
7484 item = XNEW (struct dwarf2_queue_item);
7485 item->per_cu = per_cu;
7486 item->pretend_language = pretend_language;
7487 item->next = NULL;
7488
7489 if (dwarf2_queue == NULL)
7490 dwarf2_queue = item;
7491 else
7492 dwarf2_queue_tail->next = item;
7493
7494 dwarf2_queue_tail = item;
7495 }
7496
7497 /* If PER_CU is not yet queued, add it to the queue.
7498 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7499 dependency.
7500 The result is non-zero if PER_CU was queued, otherwise the result is zero
7501 meaning either PER_CU is already queued or it is already loaded.
7502
7503 N.B. There is an invariant here that if a CU is queued then it is loaded.
7504 The caller is required to load PER_CU if we return non-zero. */
7505
7506 static int
7507 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7508 struct dwarf2_per_cu_data *per_cu,
7509 enum language pretend_language)
7510 {
7511 /* We may arrive here during partial symbol reading, if we need full
7512 DIEs to process an unusual case (e.g. template arguments). Do
7513 not queue PER_CU, just tell our caller to load its DIEs. */
7514 if (dwarf2_per_objfile->reading_partial_symbols)
7515 {
7516 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7517 return 1;
7518 return 0;
7519 }
7520
7521 /* Mark the dependence relation so that we don't flush PER_CU
7522 too early. */
7523 if (dependent_cu != NULL)
7524 dwarf2_add_dependence (dependent_cu, per_cu);
7525
7526 /* If it's already on the queue, we have nothing to do. */
7527 if (per_cu->queued)
7528 return 0;
7529
7530 /* If the compilation unit is already loaded, just mark it as
7531 used. */
7532 if (per_cu->cu != NULL)
7533 {
7534 per_cu->cu->last_used = 0;
7535 return 0;
7536 }
7537
7538 /* Add it to the queue. */
7539 queue_comp_unit (per_cu, pretend_language);
7540
7541 return 1;
7542 }
7543
7544 /* Process the queue. */
7545
7546 static void
7547 process_queue (void)
7548 {
7549 struct dwarf2_queue_item *item, *next_item;
7550
7551 if (dwarf_read_debug)
7552 {
7553 fprintf_unfiltered (gdb_stdlog,
7554 "Expanding one or more symtabs of objfile %s ...\n",
7555 objfile_name (dwarf2_per_objfile->objfile));
7556 }
7557
7558 /* The queue starts out with one item, but following a DIE reference
7559 may load a new CU, adding it to the end of the queue. */
7560 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7561 {
7562 if ((dwarf2_per_objfile->using_index
7563 ? !item->per_cu->v.quick->compunit_symtab
7564 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7565 /* Skip dummy CUs. */
7566 && item->per_cu->cu != NULL)
7567 {
7568 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7569 unsigned int debug_print_threshold;
7570 char buf[100];
7571
7572 if (per_cu->is_debug_types)
7573 {
7574 struct signatured_type *sig_type =
7575 (struct signatured_type *) per_cu;
7576
7577 sprintf (buf, "TU %s at offset 0x%x",
7578 hex_string (sig_type->signature),
7579 per_cu->offset.sect_off);
7580 /* There can be 100s of TUs.
7581 Only print them in verbose mode. */
7582 debug_print_threshold = 2;
7583 }
7584 else
7585 {
7586 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7587 debug_print_threshold = 1;
7588 }
7589
7590 if (dwarf_read_debug >= debug_print_threshold)
7591 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7592
7593 if (per_cu->is_debug_types)
7594 process_full_type_unit (per_cu, item->pretend_language);
7595 else
7596 process_full_comp_unit (per_cu, item->pretend_language);
7597
7598 if (dwarf_read_debug >= debug_print_threshold)
7599 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7600 }
7601
7602 item->per_cu->queued = 0;
7603 next_item = item->next;
7604 xfree (item);
7605 }
7606
7607 dwarf2_queue_tail = NULL;
7608
7609 if (dwarf_read_debug)
7610 {
7611 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7612 objfile_name (dwarf2_per_objfile->objfile));
7613 }
7614 }
7615
7616 /* Free all allocated queue entries. This function only releases anything if
7617 an error was thrown; if the queue was processed then it would have been
7618 freed as we went along. */
7619
7620 static void
7621 dwarf2_release_queue (void *dummy)
7622 {
7623 struct dwarf2_queue_item *item, *last;
7624
7625 item = dwarf2_queue;
7626 while (item)
7627 {
7628 /* Anything still marked queued is likely to be in an
7629 inconsistent state, so discard it. */
7630 if (item->per_cu->queued)
7631 {
7632 if (item->per_cu->cu != NULL)
7633 free_one_cached_comp_unit (item->per_cu);
7634 item->per_cu->queued = 0;
7635 }
7636
7637 last = item;
7638 item = item->next;
7639 xfree (last);
7640 }
7641
7642 dwarf2_queue = dwarf2_queue_tail = NULL;
7643 }
7644
7645 /* Read in full symbols for PST, and anything it depends on. */
7646
7647 static void
7648 psymtab_to_symtab_1 (struct partial_symtab *pst)
7649 {
7650 struct dwarf2_per_cu_data *per_cu;
7651 int i;
7652
7653 if (pst->readin)
7654 return;
7655
7656 for (i = 0; i < pst->number_of_dependencies; i++)
7657 if (!pst->dependencies[i]->readin
7658 && pst->dependencies[i]->user == NULL)
7659 {
7660 /* Inform about additional files that need to be read in. */
7661 if (info_verbose)
7662 {
7663 /* FIXME: i18n: Need to make this a single string. */
7664 fputs_filtered (" ", gdb_stdout);
7665 wrap_here ("");
7666 fputs_filtered ("and ", gdb_stdout);
7667 wrap_here ("");
7668 printf_filtered ("%s...", pst->dependencies[i]->filename);
7669 wrap_here (""); /* Flush output. */
7670 gdb_flush (gdb_stdout);
7671 }
7672 psymtab_to_symtab_1 (pst->dependencies[i]);
7673 }
7674
7675 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7676
7677 if (per_cu == NULL)
7678 {
7679 /* It's an include file, no symbols to read for it.
7680 Everything is in the parent symtab. */
7681 pst->readin = 1;
7682 return;
7683 }
7684
7685 dw2_do_instantiate_symtab (per_cu);
7686 }
7687
7688 /* Trivial hash function for die_info: the hash value of a DIE
7689 is its offset in .debug_info for this objfile. */
7690
7691 static hashval_t
7692 die_hash (const void *item)
7693 {
7694 const struct die_info *die = (const struct die_info *) item;
7695
7696 return die->offset.sect_off;
7697 }
7698
7699 /* Trivial comparison function for die_info structures: two DIEs
7700 are equal if they have the same offset. */
7701
7702 static int
7703 die_eq (const void *item_lhs, const void *item_rhs)
7704 {
7705 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7706 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7707
7708 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7709 }
7710
7711 /* die_reader_func for load_full_comp_unit.
7712 This is identical to read_signatured_type_reader,
7713 but is kept separate for now. */
7714
7715 static void
7716 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7717 const gdb_byte *info_ptr,
7718 struct die_info *comp_unit_die,
7719 int has_children,
7720 void *data)
7721 {
7722 struct dwarf2_cu *cu = reader->cu;
7723 enum language *language_ptr = (enum language *) data;
7724
7725 gdb_assert (cu->die_hash == NULL);
7726 cu->die_hash =
7727 htab_create_alloc_ex (cu->header.length / 12,
7728 die_hash,
7729 die_eq,
7730 NULL,
7731 &cu->comp_unit_obstack,
7732 hashtab_obstack_allocate,
7733 dummy_obstack_deallocate);
7734
7735 if (has_children)
7736 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7737 &info_ptr, comp_unit_die);
7738 cu->dies = comp_unit_die;
7739 /* comp_unit_die is not stored in die_hash, no need. */
7740
7741 /* We try not to read any attributes in this function, because not
7742 all CUs needed for references have been loaded yet, and symbol
7743 table processing isn't initialized. But we have to set the CU language,
7744 or we won't be able to build types correctly.
7745 Similarly, if we do not read the producer, we can not apply
7746 producer-specific interpretation. */
7747 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7748 }
7749
7750 /* Load the DIEs associated with PER_CU into memory. */
7751
7752 static void
7753 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7754 enum language pretend_language)
7755 {
7756 gdb_assert (! this_cu->is_debug_types);
7757
7758 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7759 load_full_comp_unit_reader, &pretend_language);
7760 }
7761
7762 /* Add a DIE to the delayed physname list. */
7763
7764 static void
7765 add_to_method_list (struct type *type, int fnfield_index, int index,
7766 const char *name, struct die_info *die,
7767 struct dwarf2_cu *cu)
7768 {
7769 struct delayed_method_info mi;
7770 mi.type = type;
7771 mi.fnfield_index = fnfield_index;
7772 mi.index = index;
7773 mi.name = name;
7774 mi.die = die;
7775 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7776 }
7777
7778 /* A cleanup for freeing the delayed method list. */
7779
7780 static void
7781 free_delayed_list (void *ptr)
7782 {
7783 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7784 if (cu->method_list != NULL)
7785 {
7786 VEC_free (delayed_method_info, cu->method_list);
7787 cu->method_list = NULL;
7788 }
7789 }
7790
7791 /* Compute the physnames of any methods on the CU's method list.
7792
7793 The computation of method physnames is delayed in order to avoid the
7794 (bad) condition that one of the method's formal parameters is of an as yet
7795 incomplete type. */
7796
7797 static void
7798 compute_delayed_physnames (struct dwarf2_cu *cu)
7799 {
7800 int i;
7801 struct delayed_method_info *mi;
7802 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7803 {
7804 const char *physname;
7805 struct fn_fieldlist *fn_flp
7806 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7807 physname = dwarf2_physname (mi->name, mi->die, cu);
7808 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7809 = physname ? physname : "";
7810 }
7811 }
7812
7813 /* Go objects should be embedded in a DW_TAG_module DIE,
7814 and it's not clear if/how imported objects will appear.
7815 To keep Go support simple until that's worked out,
7816 go back through what we've read and create something usable.
7817 We could do this while processing each DIE, and feels kinda cleaner,
7818 but that way is more invasive.
7819 This is to, for example, allow the user to type "p var" or "b main"
7820 without having to specify the package name, and allow lookups
7821 of module.object to work in contexts that use the expression
7822 parser. */
7823
7824 static void
7825 fixup_go_packaging (struct dwarf2_cu *cu)
7826 {
7827 char *package_name = NULL;
7828 struct pending *list;
7829 int i;
7830
7831 for (list = global_symbols; list != NULL; list = list->next)
7832 {
7833 for (i = 0; i < list->nsyms; ++i)
7834 {
7835 struct symbol *sym = list->symbol[i];
7836
7837 if (SYMBOL_LANGUAGE (sym) == language_go
7838 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7839 {
7840 char *this_package_name = go_symbol_package_name (sym);
7841
7842 if (this_package_name == NULL)
7843 continue;
7844 if (package_name == NULL)
7845 package_name = this_package_name;
7846 else
7847 {
7848 if (strcmp (package_name, this_package_name) != 0)
7849 complaint (&symfile_complaints,
7850 _("Symtab %s has objects from two different Go packages: %s and %s"),
7851 (symbol_symtab (sym) != NULL
7852 ? symtab_to_filename_for_display
7853 (symbol_symtab (sym))
7854 : objfile_name (cu->objfile)),
7855 this_package_name, package_name);
7856 xfree (this_package_name);
7857 }
7858 }
7859 }
7860 }
7861
7862 if (package_name != NULL)
7863 {
7864 struct objfile *objfile = cu->objfile;
7865 const char *saved_package_name
7866 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
7867 package_name,
7868 strlen (package_name));
7869 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7870 saved_package_name);
7871 struct symbol *sym;
7872
7873 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7874
7875 sym = allocate_symbol (objfile);
7876 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7877 SYMBOL_SET_NAMES (sym, saved_package_name,
7878 strlen (saved_package_name), 0, objfile);
7879 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7880 e.g., "main" finds the "main" module and not C's main(). */
7881 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7882 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7883 SYMBOL_TYPE (sym) = type;
7884
7885 add_symbol_to_list (sym, &global_symbols);
7886
7887 xfree (package_name);
7888 }
7889 }
7890
7891 /* Return the symtab for PER_CU. This works properly regardless of
7892 whether we're using the index or psymtabs. */
7893
7894 static struct compunit_symtab *
7895 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
7896 {
7897 return (dwarf2_per_objfile->using_index
7898 ? per_cu->v.quick->compunit_symtab
7899 : per_cu->v.psymtab->compunit_symtab);
7900 }
7901
7902 /* A helper function for computing the list of all symbol tables
7903 included by PER_CU. */
7904
7905 static void
7906 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
7907 htab_t all_children, htab_t all_type_symtabs,
7908 struct dwarf2_per_cu_data *per_cu,
7909 struct compunit_symtab *immediate_parent)
7910 {
7911 void **slot;
7912 int ix;
7913 struct compunit_symtab *cust;
7914 struct dwarf2_per_cu_data *iter;
7915
7916 slot = htab_find_slot (all_children, per_cu, INSERT);
7917 if (*slot != NULL)
7918 {
7919 /* This inclusion and its children have been processed. */
7920 return;
7921 }
7922
7923 *slot = per_cu;
7924 /* Only add a CU if it has a symbol table. */
7925 cust = get_compunit_symtab (per_cu);
7926 if (cust != NULL)
7927 {
7928 /* If this is a type unit only add its symbol table if we haven't
7929 seen it yet (type unit per_cu's can share symtabs). */
7930 if (per_cu->is_debug_types)
7931 {
7932 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
7933 if (*slot == NULL)
7934 {
7935 *slot = cust;
7936 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7937 if (cust->user == NULL)
7938 cust->user = immediate_parent;
7939 }
7940 }
7941 else
7942 {
7943 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7944 if (cust->user == NULL)
7945 cust->user = immediate_parent;
7946 }
7947 }
7948
7949 for (ix = 0;
7950 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7951 ++ix)
7952 {
7953 recursively_compute_inclusions (result, all_children,
7954 all_type_symtabs, iter, cust);
7955 }
7956 }
7957
7958 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
7959 PER_CU. */
7960
7961 static void
7962 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7963 {
7964 gdb_assert (! per_cu->is_debug_types);
7965
7966 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7967 {
7968 int ix, len;
7969 struct dwarf2_per_cu_data *per_cu_iter;
7970 struct compunit_symtab *compunit_symtab_iter;
7971 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
7972 htab_t all_children, all_type_symtabs;
7973 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
7974
7975 /* If we don't have a symtab, we can just skip this case. */
7976 if (cust == NULL)
7977 return;
7978
7979 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7980 NULL, xcalloc, xfree);
7981 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7982 NULL, xcalloc, xfree);
7983
7984 for (ix = 0;
7985 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7986 ix, per_cu_iter);
7987 ++ix)
7988 {
7989 recursively_compute_inclusions (&result_symtabs, all_children,
7990 all_type_symtabs, per_cu_iter,
7991 cust);
7992 }
7993
7994 /* Now we have a transitive closure of all the included symtabs. */
7995 len = VEC_length (compunit_symtab_ptr, result_symtabs);
7996 cust->includes
7997 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
7998 struct compunit_symtab *, len + 1);
7999 for (ix = 0;
8000 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8001 compunit_symtab_iter);
8002 ++ix)
8003 cust->includes[ix] = compunit_symtab_iter;
8004 cust->includes[len] = NULL;
8005
8006 VEC_free (compunit_symtab_ptr, result_symtabs);
8007 htab_delete (all_children);
8008 htab_delete (all_type_symtabs);
8009 }
8010 }
8011
8012 /* Compute the 'includes' field for the symtabs of all the CUs we just
8013 read. */
8014
8015 static void
8016 process_cu_includes (void)
8017 {
8018 int ix;
8019 struct dwarf2_per_cu_data *iter;
8020
8021 for (ix = 0;
8022 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8023 ix, iter);
8024 ++ix)
8025 {
8026 if (! iter->is_debug_types)
8027 compute_compunit_symtab_includes (iter);
8028 }
8029
8030 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8031 }
8032
8033 /* Generate full symbol information for PER_CU, whose DIEs have
8034 already been loaded into memory. */
8035
8036 static void
8037 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8038 enum language pretend_language)
8039 {
8040 struct dwarf2_cu *cu = per_cu->cu;
8041 struct objfile *objfile = per_cu->objfile;
8042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8043 CORE_ADDR lowpc, highpc;
8044 struct compunit_symtab *cust;
8045 struct cleanup *back_to, *delayed_list_cleanup;
8046 CORE_ADDR baseaddr;
8047 struct block *static_block;
8048 CORE_ADDR addr;
8049
8050 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8051
8052 buildsym_init ();
8053 back_to = make_cleanup (really_free_pendings, NULL);
8054 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8055
8056 cu->list_in_scope = &file_symbols;
8057
8058 cu->language = pretend_language;
8059 cu->language_defn = language_def (cu->language);
8060
8061 /* Do line number decoding in read_file_scope () */
8062 process_die (cu->dies, cu);
8063
8064 /* For now fudge the Go package. */
8065 if (cu->language == language_go)
8066 fixup_go_packaging (cu);
8067
8068 /* Now that we have processed all the DIEs in the CU, all the types
8069 should be complete, and it should now be safe to compute all of the
8070 physnames. */
8071 compute_delayed_physnames (cu);
8072 do_cleanups (delayed_list_cleanup);
8073
8074 /* Some compilers don't define a DW_AT_high_pc attribute for the
8075 compilation unit. If the DW_AT_high_pc is missing, synthesize
8076 it, by scanning the DIE's below the compilation unit. */
8077 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8078
8079 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8080 static_block = end_symtab_get_static_block (addr, 0, 1);
8081
8082 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8083 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8084 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8085 addrmap to help ensure it has an accurate map of pc values belonging to
8086 this comp unit. */
8087 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8088
8089 cust = end_symtab_from_static_block (static_block,
8090 SECT_OFF_TEXT (objfile), 0);
8091
8092 if (cust != NULL)
8093 {
8094 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8095
8096 /* Set symtab language to language from DW_AT_language. If the
8097 compilation is from a C file generated by language preprocessors, do
8098 not set the language if it was already deduced by start_subfile. */
8099 if (!(cu->language == language_c
8100 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8101 COMPUNIT_FILETABS (cust)->language = cu->language;
8102
8103 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8104 produce DW_AT_location with location lists but it can be possibly
8105 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8106 there were bugs in prologue debug info, fixed later in GCC-4.5
8107 by "unwind info for epilogues" patch (which is not directly related).
8108
8109 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8110 needed, it would be wrong due to missing DW_AT_producer there.
8111
8112 Still one can confuse GDB by using non-standard GCC compilation
8113 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8114 */
8115 if (cu->has_loclist && gcc_4_minor >= 5)
8116 cust->locations_valid = 1;
8117
8118 if (gcc_4_minor >= 5)
8119 cust->epilogue_unwind_valid = 1;
8120
8121 cust->call_site_htab = cu->call_site_htab;
8122 }
8123
8124 if (dwarf2_per_objfile->using_index)
8125 per_cu->v.quick->compunit_symtab = cust;
8126 else
8127 {
8128 struct partial_symtab *pst = per_cu->v.psymtab;
8129 pst->compunit_symtab = cust;
8130 pst->readin = 1;
8131 }
8132
8133 /* Push it for inclusion processing later. */
8134 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8135
8136 do_cleanups (back_to);
8137 }
8138
8139 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8140 already been loaded into memory. */
8141
8142 static void
8143 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8144 enum language pretend_language)
8145 {
8146 struct dwarf2_cu *cu = per_cu->cu;
8147 struct objfile *objfile = per_cu->objfile;
8148 struct compunit_symtab *cust;
8149 struct cleanup *back_to, *delayed_list_cleanup;
8150 struct signatured_type *sig_type;
8151
8152 gdb_assert (per_cu->is_debug_types);
8153 sig_type = (struct signatured_type *) per_cu;
8154
8155 buildsym_init ();
8156 back_to = make_cleanup (really_free_pendings, NULL);
8157 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8158
8159 cu->list_in_scope = &file_symbols;
8160
8161 cu->language = pretend_language;
8162 cu->language_defn = language_def (cu->language);
8163
8164 /* The symbol tables are set up in read_type_unit_scope. */
8165 process_die (cu->dies, cu);
8166
8167 /* For now fudge the Go package. */
8168 if (cu->language == language_go)
8169 fixup_go_packaging (cu);
8170
8171 /* Now that we have processed all the DIEs in the CU, all the types
8172 should be complete, and it should now be safe to compute all of the
8173 physnames. */
8174 compute_delayed_physnames (cu);
8175 do_cleanups (delayed_list_cleanup);
8176
8177 /* TUs share symbol tables.
8178 If this is the first TU to use this symtab, complete the construction
8179 of it with end_expandable_symtab. Otherwise, complete the addition of
8180 this TU's symbols to the existing symtab. */
8181 if (sig_type->type_unit_group->compunit_symtab == NULL)
8182 {
8183 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8184 sig_type->type_unit_group->compunit_symtab = cust;
8185
8186 if (cust != NULL)
8187 {
8188 /* Set symtab language to language from DW_AT_language. If the
8189 compilation is from a C file generated by language preprocessors,
8190 do not set the language if it was already deduced by
8191 start_subfile. */
8192 if (!(cu->language == language_c
8193 && COMPUNIT_FILETABS (cust)->language != language_c))
8194 COMPUNIT_FILETABS (cust)->language = cu->language;
8195 }
8196 }
8197 else
8198 {
8199 augment_type_symtab ();
8200 cust = sig_type->type_unit_group->compunit_symtab;
8201 }
8202
8203 if (dwarf2_per_objfile->using_index)
8204 per_cu->v.quick->compunit_symtab = cust;
8205 else
8206 {
8207 struct partial_symtab *pst = per_cu->v.psymtab;
8208 pst->compunit_symtab = cust;
8209 pst->readin = 1;
8210 }
8211
8212 do_cleanups (back_to);
8213 }
8214
8215 /* Process an imported unit DIE. */
8216
8217 static void
8218 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8219 {
8220 struct attribute *attr;
8221
8222 /* For now we don't handle imported units in type units. */
8223 if (cu->per_cu->is_debug_types)
8224 {
8225 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8226 " supported in type units [in module %s]"),
8227 objfile_name (cu->objfile));
8228 }
8229
8230 attr = dwarf2_attr (die, DW_AT_import, cu);
8231 if (attr != NULL)
8232 {
8233 struct dwarf2_per_cu_data *per_cu;
8234 sect_offset offset;
8235 int is_dwz;
8236
8237 offset = dwarf2_get_ref_die_offset (attr);
8238 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8239 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
8240
8241 /* If necessary, add it to the queue and load its DIEs. */
8242 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8243 load_full_comp_unit (per_cu, cu->language);
8244
8245 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8246 per_cu);
8247 }
8248 }
8249
8250 /* Reset the in_process bit of a die. */
8251
8252 static void
8253 reset_die_in_process (void *arg)
8254 {
8255 struct die_info *die = (struct die_info *) arg;
8256
8257 die->in_process = 0;
8258 }
8259
8260 /* Process a die and its children. */
8261
8262 static void
8263 process_die (struct die_info *die, struct dwarf2_cu *cu)
8264 {
8265 struct cleanup *in_process;
8266
8267 /* We should only be processing those not already in process. */
8268 gdb_assert (!die->in_process);
8269
8270 die->in_process = 1;
8271 in_process = make_cleanup (reset_die_in_process,die);
8272
8273 switch (die->tag)
8274 {
8275 case DW_TAG_padding:
8276 break;
8277 case DW_TAG_compile_unit:
8278 case DW_TAG_partial_unit:
8279 read_file_scope (die, cu);
8280 break;
8281 case DW_TAG_type_unit:
8282 read_type_unit_scope (die, cu);
8283 break;
8284 case DW_TAG_subprogram:
8285 case DW_TAG_inlined_subroutine:
8286 read_func_scope (die, cu);
8287 break;
8288 case DW_TAG_lexical_block:
8289 case DW_TAG_try_block:
8290 case DW_TAG_catch_block:
8291 read_lexical_block_scope (die, cu);
8292 break;
8293 case DW_TAG_GNU_call_site:
8294 read_call_site_scope (die, cu);
8295 break;
8296 case DW_TAG_class_type:
8297 case DW_TAG_interface_type:
8298 case DW_TAG_structure_type:
8299 case DW_TAG_union_type:
8300 process_structure_scope (die, cu);
8301 break;
8302 case DW_TAG_enumeration_type:
8303 process_enumeration_scope (die, cu);
8304 break;
8305
8306 /* These dies have a type, but processing them does not create
8307 a symbol or recurse to process the children. Therefore we can
8308 read them on-demand through read_type_die. */
8309 case DW_TAG_subroutine_type:
8310 case DW_TAG_set_type:
8311 case DW_TAG_array_type:
8312 case DW_TAG_pointer_type:
8313 case DW_TAG_ptr_to_member_type:
8314 case DW_TAG_reference_type:
8315 case DW_TAG_string_type:
8316 break;
8317
8318 case DW_TAG_base_type:
8319 case DW_TAG_subrange_type:
8320 case DW_TAG_typedef:
8321 /* Add a typedef symbol for the type definition, if it has a
8322 DW_AT_name. */
8323 new_symbol (die, read_type_die (die, cu), cu);
8324 break;
8325 case DW_TAG_common_block:
8326 read_common_block (die, cu);
8327 break;
8328 case DW_TAG_common_inclusion:
8329 break;
8330 case DW_TAG_namespace:
8331 cu->processing_has_namespace_info = 1;
8332 read_namespace (die, cu);
8333 break;
8334 case DW_TAG_module:
8335 cu->processing_has_namespace_info = 1;
8336 read_module (die, cu);
8337 break;
8338 case DW_TAG_imported_declaration:
8339 cu->processing_has_namespace_info = 1;
8340 if (read_namespace_alias (die, cu))
8341 break;
8342 /* The declaration is not a global namespace alias: fall through. */
8343 case DW_TAG_imported_module:
8344 cu->processing_has_namespace_info = 1;
8345 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8346 || cu->language != language_fortran))
8347 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8348 dwarf_tag_name (die->tag));
8349 read_import_statement (die, cu);
8350 break;
8351
8352 case DW_TAG_imported_unit:
8353 process_imported_unit_die (die, cu);
8354 break;
8355
8356 default:
8357 new_symbol (die, NULL, cu);
8358 break;
8359 }
8360
8361 do_cleanups (in_process);
8362 }
8363 \f
8364 /* DWARF name computation. */
8365
8366 /* A helper function for dwarf2_compute_name which determines whether DIE
8367 needs to have the name of the scope prepended to the name listed in the
8368 die. */
8369
8370 static int
8371 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8372 {
8373 struct attribute *attr;
8374
8375 switch (die->tag)
8376 {
8377 case DW_TAG_namespace:
8378 case DW_TAG_typedef:
8379 case DW_TAG_class_type:
8380 case DW_TAG_interface_type:
8381 case DW_TAG_structure_type:
8382 case DW_TAG_union_type:
8383 case DW_TAG_enumeration_type:
8384 case DW_TAG_enumerator:
8385 case DW_TAG_subprogram:
8386 case DW_TAG_inlined_subroutine:
8387 case DW_TAG_member:
8388 case DW_TAG_imported_declaration:
8389 return 1;
8390
8391 case DW_TAG_variable:
8392 case DW_TAG_constant:
8393 /* We only need to prefix "globally" visible variables. These include
8394 any variable marked with DW_AT_external or any variable that
8395 lives in a namespace. [Variables in anonymous namespaces
8396 require prefixing, but they are not DW_AT_external.] */
8397
8398 if (dwarf2_attr (die, DW_AT_specification, cu))
8399 {
8400 struct dwarf2_cu *spec_cu = cu;
8401
8402 return die_needs_namespace (die_specification (die, &spec_cu),
8403 spec_cu);
8404 }
8405
8406 attr = dwarf2_attr (die, DW_AT_external, cu);
8407 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8408 && die->parent->tag != DW_TAG_module)
8409 return 0;
8410 /* A variable in a lexical block of some kind does not need a
8411 namespace, even though in C++ such variables may be external
8412 and have a mangled name. */
8413 if (die->parent->tag == DW_TAG_lexical_block
8414 || die->parent->tag == DW_TAG_try_block
8415 || die->parent->tag == DW_TAG_catch_block
8416 || die->parent->tag == DW_TAG_subprogram)
8417 return 0;
8418 return 1;
8419
8420 default:
8421 return 0;
8422 }
8423 }
8424
8425 /* Retrieve the last character from a mem_file. */
8426
8427 static void
8428 do_ui_file_peek_last (void *object, const char *buffer, long length)
8429 {
8430 char *last_char_p = (char *) object;
8431
8432 if (length > 0)
8433 *last_char_p = buffer[length - 1];
8434 }
8435
8436 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8437 compute the physname for the object, which include a method's:
8438 - formal parameters (C++/Java),
8439 - receiver type (Go),
8440 - return type (Java).
8441
8442 The term "physname" is a bit confusing.
8443 For C++, for example, it is the demangled name.
8444 For Go, for example, it's the mangled name.
8445
8446 For Ada, return the DIE's linkage name rather than the fully qualified
8447 name. PHYSNAME is ignored..
8448
8449 The result is allocated on the objfile_obstack and canonicalized. */
8450
8451 static const char *
8452 dwarf2_compute_name (const char *name,
8453 struct die_info *die, struct dwarf2_cu *cu,
8454 int physname)
8455 {
8456 struct objfile *objfile = cu->objfile;
8457
8458 if (name == NULL)
8459 name = dwarf2_name (die, cu);
8460
8461 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8462 but otherwise compute it by typename_concat inside GDB.
8463 FIXME: Actually this is not really true, or at least not always true.
8464 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8465 Fortran names because there is no mangling standard. So new_symbol_full
8466 will set the demangled name to the result of dwarf2_full_name, and it is
8467 the demangled name that GDB uses if it exists. */
8468 if (cu->language == language_ada
8469 || (cu->language == language_fortran && physname))
8470 {
8471 /* For Ada unit, we prefer the linkage name over the name, as
8472 the former contains the exported name, which the user expects
8473 to be able to reference. Ideally, we want the user to be able
8474 to reference this entity using either natural or linkage name,
8475 but we haven't started looking at this enhancement yet. */
8476 const char *linkage_name;
8477
8478 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8479 if (linkage_name == NULL)
8480 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8481 if (linkage_name != NULL)
8482 return linkage_name;
8483 }
8484
8485 /* These are the only languages we know how to qualify names in. */
8486 if (name != NULL
8487 && (cu->language == language_cplus || cu->language == language_java
8488 || cu->language == language_fortran || cu->language == language_d
8489 || cu->language == language_rust))
8490 {
8491 if (die_needs_namespace (die, cu))
8492 {
8493 long length;
8494 const char *prefix;
8495 struct ui_file *buf;
8496 char *intermediate_name;
8497 const char *canonical_name = NULL;
8498
8499 prefix = determine_prefix (die, cu);
8500 buf = mem_fileopen ();
8501 if (*prefix != '\0')
8502 {
8503 char *prefixed_name = typename_concat (NULL, prefix, name,
8504 physname, cu);
8505
8506 fputs_unfiltered (prefixed_name, buf);
8507 xfree (prefixed_name);
8508 }
8509 else
8510 fputs_unfiltered (name, buf);
8511
8512 /* Template parameters may be specified in the DIE's DW_AT_name, or
8513 as children with DW_TAG_template_type_param or
8514 DW_TAG_value_type_param. If the latter, add them to the name
8515 here. If the name already has template parameters, then
8516 skip this step; some versions of GCC emit both, and
8517 it is more efficient to use the pre-computed name.
8518
8519 Something to keep in mind about this process: it is very
8520 unlikely, or in some cases downright impossible, to produce
8521 something that will match the mangled name of a function.
8522 If the definition of the function has the same debug info,
8523 we should be able to match up with it anyway. But fallbacks
8524 using the minimal symbol, for instance to find a method
8525 implemented in a stripped copy of libstdc++, will not work.
8526 If we do not have debug info for the definition, we will have to
8527 match them up some other way.
8528
8529 When we do name matching there is a related problem with function
8530 templates; two instantiated function templates are allowed to
8531 differ only by their return types, which we do not add here. */
8532
8533 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8534 {
8535 struct attribute *attr;
8536 struct die_info *child;
8537 int first = 1;
8538
8539 die->building_fullname = 1;
8540
8541 for (child = die->child; child != NULL; child = child->sibling)
8542 {
8543 struct type *type;
8544 LONGEST value;
8545 const gdb_byte *bytes;
8546 struct dwarf2_locexpr_baton *baton;
8547 struct value *v;
8548
8549 if (child->tag != DW_TAG_template_type_param
8550 && child->tag != DW_TAG_template_value_param)
8551 continue;
8552
8553 if (first)
8554 {
8555 fputs_unfiltered ("<", buf);
8556 first = 0;
8557 }
8558 else
8559 fputs_unfiltered (", ", buf);
8560
8561 attr = dwarf2_attr (child, DW_AT_type, cu);
8562 if (attr == NULL)
8563 {
8564 complaint (&symfile_complaints,
8565 _("template parameter missing DW_AT_type"));
8566 fputs_unfiltered ("UNKNOWN_TYPE", buf);
8567 continue;
8568 }
8569 type = die_type (child, cu);
8570
8571 if (child->tag == DW_TAG_template_type_param)
8572 {
8573 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
8574 continue;
8575 }
8576
8577 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8578 if (attr == NULL)
8579 {
8580 complaint (&symfile_complaints,
8581 _("template parameter missing "
8582 "DW_AT_const_value"));
8583 fputs_unfiltered ("UNKNOWN_VALUE", buf);
8584 continue;
8585 }
8586
8587 dwarf2_const_value_attr (attr, type, name,
8588 &cu->comp_unit_obstack, cu,
8589 &value, &bytes, &baton);
8590
8591 if (TYPE_NOSIGN (type))
8592 /* GDB prints characters as NUMBER 'CHAR'. If that's
8593 changed, this can use value_print instead. */
8594 c_printchar (value, type, buf);
8595 else
8596 {
8597 struct value_print_options opts;
8598
8599 if (baton != NULL)
8600 v = dwarf2_evaluate_loc_desc (type, NULL,
8601 baton->data,
8602 baton->size,
8603 baton->per_cu);
8604 else if (bytes != NULL)
8605 {
8606 v = allocate_value (type);
8607 memcpy (value_contents_writeable (v), bytes,
8608 TYPE_LENGTH (type));
8609 }
8610 else
8611 v = value_from_longest (type, value);
8612
8613 /* Specify decimal so that we do not depend on
8614 the radix. */
8615 get_formatted_print_options (&opts, 'd');
8616 opts.raw = 1;
8617 value_print (v, buf, &opts);
8618 release_value (v);
8619 value_free (v);
8620 }
8621 }
8622
8623 die->building_fullname = 0;
8624
8625 if (!first)
8626 {
8627 /* Close the argument list, with a space if necessary
8628 (nested templates). */
8629 char last_char = '\0';
8630 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8631 if (last_char == '>')
8632 fputs_unfiltered (" >", buf);
8633 else
8634 fputs_unfiltered (">", buf);
8635 }
8636 }
8637
8638 /* For Java and C++ methods, append formal parameter type
8639 information, if PHYSNAME. */
8640
8641 if (physname && die->tag == DW_TAG_subprogram
8642 && (cu->language == language_cplus
8643 || cu->language == language_java))
8644 {
8645 struct type *type = read_type_die (die, cu);
8646
8647 c_type_print_args (type, buf, 1, cu->language,
8648 &type_print_raw_options);
8649
8650 if (cu->language == language_java)
8651 {
8652 /* For java, we must append the return type to method
8653 names. */
8654 if (die->tag == DW_TAG_subprogram)
8655 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
8656 0, 0, &type_print_raw_options);
8657 }
8658 else if (cu->language == language_cplus)
8659 {
8660 /* Assume that an artificial first parameter is
8661 "this", but do not crash if it is not. RealView
8662 marks unnamed (and thus unused) parameters as
8663 artificial; there is no way to differentiate
8664 the two cases. */
8665 if (TYPE_NFIELDS (type) > 0
8666 && TYPE_FIELD_ARTIFICIAL (type, 0)
8667 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8668 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8669 0))))
8670 fputs_unfiltered (" const", buf);
8671 }
8672 }
8673
8674 intermediate_name = ui_file_xstrdup (buf, &length);
8675 ui_file_delete (buf);
8676
8677 if (cu->language == language_cplus)
8678 canonical_name
8679 = dwarf2_canonicalize_name (intermediate_name, cu,
8680 &objfile->per_bfd->storage_obstack);
8681
8682 /* If we only computed INTERMEDIATE_NAME, or if
8683 INTERMEDIATE_NAME is already canonical, then we need to
8684 copy it to the appropriate obstack. */
8685 if (canonical_name == NULL || canonical_name == intermediate_name)
8686 name = ((const char *)
8687 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8688 intermediate_name,
8689 strlen (intermediate_name)));
8690 else
8691 name = canonical_name;
8692
8693 xfree (intermediate_name);
8694 }
8695 }
8696
8697 return name;
8698 }
8699
8700 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8701 If scope qualifiers are appropriate they will be added. The result
8702 will be allocated on the storage_obstack, or NULL if the DIE does
8703 not have a name. NAME may either be from a previous call to
8704 dwarf2_name or NULL.
8705
8706 The output string will be canonicalized (if C++/Java). */
8707
8708 static const char *
8709 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8710 {
8711 return dwarf2_compute_name (name, die, cu, 0);
8712 }
8713
8714 /* Construct a physname for the given DIE in CU. NAME may either be
8715 from a previous call to dwarf2_name or NULL. The result will be
8716 allocated on the objfile_objstack or NULL if the DIE does not have a
8717 name.
8718
8719 The output string will be canonicalized (if C++/Java). */
8720
8721 static const char *
8722 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8723 {
8724 struct objfile *objfile = cu->objfile;
8725 const char *retval, *mangled = NULL, *canon = NULL;
8726 struct cleanup *back_to;
8727 int need_copy = 1;
8728
8729 /* In this case dwarf2_compute_name is just a shortcut not building anything
8730 on its own. */
8731 if (!die_needs_namespace (die, cu))
8732 return dwarf2_compute_name (name, die, cu, 1);
8733
8734 back_to = make_cleanup (null_cleanup, NULL);
8735
8736 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8737 if (mangled == NULL)
8738 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8739
8740 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8741 See https://github.com/rust-lang/rust/issues/32925. */
8742 if (cu->language == language_rust && mangled != NULL
8743 && strchr (mangled, '{') != NULL)
8744 mangled = NULL;
8745
8746 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8747 has computed. */
8748 if (mangled != NULL)
8749 {
8750 char *demangled;
8751
8752 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8753 type. It is easier for GDB users to search for such functions as
8754 `name(params)' than `long name(params)'. In such case the minimal
8755 symbol names do not match the full symbol names but for template
8756 functions there is never a need to look up their definition from their
8757 declaration so the only disadvantage remains the minimal symbol
8758 variant `long name(params)' does not have the proper inferior type.
8759 */
8760
8761 if (cu->language == language_go)
8762 {
8763 /* This is a lie, but we already lie to the caller new_symbol_full.
8764 new_symbol_full assumes we return the mangled name.
8765 This just undoes that lie until things are cleaned up. */
8766 demangled = NULL;
8767 }
8768 else
8769 {
8770 demangled = gdb_demangle (mangled,
8771 (DMGL_PARAMS | DMGL_ANSI
8772 | (cu->language == language_java
8773 ? DMGL_JAVA | DMGL_RET_POSTFIX
8774 : DMGL_RET_DROP)));
8775 }
8776 if (demangled)
8777 {
8778 make_cleanup (xfree, demangled);
8779 canon = demangled;
8780 }
8781 else
8782 {
8783 canon = mangled;
8784 need_copy = 0;
8785 }
8786 }
8787
8788 if (canon == NULL || check_physname)
8789 {
8790 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8791
8792 if (canon != NULL && strcmp (physname, canon) != 0)
8793 {
8794 /* It may not mean a bug in GDB. The compiler could also
8795 compute DW_AT_linkage_name incorrectly. But in such case
8796 GDB would need to be bug-to-bug compatible. */
8797
8798 complaint (&symfile_complaints,
8799 _("Computed physname <%s> does not match demangled <%s> "
8800 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8801 physname, canon, mangled, die->offset.sect_off,
8802 objfile_name (objfile));
8803
8804 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8805 is available here - over computed PHYSNAME. It is safer
8806 against both buggy GDB and buggy compilers. */
8807
8808 retval = canon;
8809 }
8810 else
8811 {
8812 retval = physname;
8813 need_copy = 0;
8814 }
8815 }
8816 else
8817 retval = canon;
8818
8819 if (need_copy)
8820 retval = ((const char *)
8821 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8822 retval, strlen (retval)));
8823
8824 do_cleanups (back_to);
8825 return retval;
8826 }
8827
8828 /* Inspect DIE in CU for a namespace alias. If one exists, record
8829 a new symbol for it.
8830
8831 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8832
8833 static int
8834 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8835 {
8836 struct attribute *attr;
8837
8838 /* If the die does not have a name, this is not a namespace
8839 alias. */
8840 attr = dwarf2_attr (die, DW_AT_name, cu);
8841 if (attr != NULL)
8842 {
8843 int num;
8844 struct die_info *d = die;
8845 struct dwarf2_cu *imported_cu = cu;
8846
8847 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8848 keep inspecting DIEs until we hit the underlying import. */
8849 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8850 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8851 {
8852 attr = dwarf2_attr (d, DW_AT_import, cu);
8853 if (attr == NULL)
8854 break;
8855
8856 d = follow_die_ref (d, attr, &imported_cu);
8857 if (d->tag != DW_TAG_imported_declaration)
8858 break;
8859 }
8860
8861 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8862 {
8863 complaint (&symfile_complaints,
8864 _("DIE at 0x%x has too many recursively imported "
8865 "declarations"), d->offset.sect_off);
8866 return 0;
8867 }
8868
8869 if (attr != NULL)
8870 {
8871 struct type *type;
8872 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8873
8874 type = get_die_type_at_offset (offset, cu->per_cu);
8875 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8876 {
8877 /* This declaration is a global namespace alias. Add
8878 a symbol for it whose type is the aliased namespace. */
8879 new_symbol (die, type, cu);
8880 return 1;
8881 }
8882 }
8883 }
8884
8885 return 0;
8886 }
8887
8888 /* Return the using directives repository (global or local?) to use in the
8889 current context for LANGUAGE.
8890
8891 For Ada, imported declarations can materialize renamings, which *may* be
8892 global. However it is impossible (for now?) in DWARF to distinguish
8893 "external" imported declarations and "static" ones. As all imported
8894 declarations seem to be static in all other languages, make them all CU-wide
8895 global only in Ada. */
8896
8897 static struct using_direct **
8898 using_directives (enum language language)
8899 {
8900 if (language == language_ada && context_stack_depth == 0)
8901 return &global_using_directives;
8902 else
8903 return &local_using_directives;
8904 }
8905
8906 /* Read the import statement specified by the given die and record it. */
8907
8908 static void
8909 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8910 {
8911 struct objfile *objfile = cu->objfile;
8912 struct attribute *import_attr;
8913 struct die_info *imported_die, *child_die;
8914 struct dwarf2_cu *imported_cu;
8915 const char *imported_name;
8916 const char *imported_name_prefix;
8917 const char *canonical_name;
8918 const char *import_alias;
8919 const char *imported_declaration = NULL;
8920 const char *import_prefix;
8921 VEC (const_char_ptr) *excludes = NULL;
8922 struct cleanup *cleanups;
8923
8924 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8925 if (import_attr == NULL)
8926 {
8927 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8928 dwarf_tag_name (die->tag));
8929 return;
8930 }
8931
8932 imported_cu = cu;
8933 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8934 imported_name = dwarf2_name (imported_die, imported_cu);
8935 if (imported_name == NULL)
8936 {
8937 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8938
8939 The import in the following code:
8940 namespace A
8941 {
8942 typedef int B;
8943 }
8944
8945 int main ()
8946 {
8947 using A::B;
8948 B b;
8949 return b;
8950 }
8951
8952 ...
8953 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8954 <52> DW_AT_decl_file : 1
8955 <53> DW_AT_decl_line : 6
8956 <54> DW_AT_import : <0x75>
8957 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8958 <59> DW_AT_name : B
8959 <5b> DW_AT_decl_file : 1
8960 <5c> DW_AT_decl_line : 2
8961 <5d> DW_AT_type : <0x6e>
8962 ...
8963 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8964 <76> DW_AT_byte_size : 4
8965 <77> DW_AT_encoding : 5 (signed)
8966
8967 imports the wrong die ( 0x75 instead of 0x58 ).
8968 This case will be ignored until the gcc bug is fixed. */
8969 return;
8970 }
8971
8972 /* Figure out the local name after import. */
8973 import_alias = dwarf2_name (die, cu);
8974
8975 /* Figure out where the statement is being imported to. */
8976 import_prefix = determine_prefix (die, cu);
8977
8978 /* Figure out what the scope of the imported die is and prepend it
8979 to the name of the imported die. */
8980 imported_name_prefix = determine_prefix (imported_die, imported_cu);
8981
8982 if (imported_die->tag != DW_TAG_namespace
8983 && imported_die->tag != DW_TAG_module)
8984 {
8985 imported_declaration = imported_name;
8986 canonical_name = imported_name_prefix;
8987 }
8988 else if (strlen (imported_name_prefix) > 0)
8989 canonical_name = obconcat (&objfile->objfile_obstack,
8990 imported_name_prefix,
8991 (cu->language == language_d ? "." : "::"),
8992 imported_name, (char *) NULL);
8993 else
8994 canonical_name = imported_name;
8995
8996 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8997
8998 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8999 for (child_die = die->child; child_die && child_die->tag;
9000 child_die = sibling_die (child_die))
9001 {
9002 /* DWARF-4: A Fortran use statement with a “rename list” may be
9003 represented by an imported module entry with an import attribute
9004 referring to the module and owned entries corresponding to those
9005 entities that are renamed as part of being imported. */
9006
9007 if (child_die->tag != DW_TAG_imported_declaration)
9008 {
9009 complaint (&symfile_complaints,
9010 _("child DW_TAG_imported_declaration expected "
9011 "- DIE at 0x%x [in module %s]"),
9012 child_die->offset.sect_off, objfile_name (objfile));
9013 continue;
9014 }
9015
9016 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9017 if (import_attr == NULL)
9018 {
9019 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9020 dwarf_tag_name (child_die->tag));
9021 continue;
9022 }
9023
9024 imported_cu = cu;
9025 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9026 &imported_cu);
9027 imported_name = dwarf2_name (imported_die, imported_cu);
9028 if (imported_name == NULL)
9029 {
9030 complaint (&symfile_complaints,
9031 _("child DW_TAG_imported_declaration has unknown "
9032 "imported name - DIE at 0x%x [in module %s]"),
9033 child_die->offset.sect_off, objfile_name (objfile));
9034 continue;
9035 }
9036
9037 VEC_safe_push (const_char_ptr, excludes, imported_name);
9038
9039 process_die (child_die, cu);
9040 }
9041
9042 add_using_directive (using_directives (cu->language),
9043 import_prefix,
9044 canonical_name,
9045 import_alias,
9046 imported_declaration,
9047 excludes,
9048 0,
9049 &objfile->objfile_obstack);
9050
9051 do_cleanups (cleanups);
9052 }
9053
9054 /* Cleanup function for handle_DW_AT_stmt_list. */
9055
9056 static void
9057 free_cu_line_header (void *arg)
9058 {
9059 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
9060
9061 free_line_header (cu->line_header);
9062 cu->line_header = NULL;
9063 }
9064
9065 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9066 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9067 this, it was first present in GCC release 4.3.0. */
9068
9069 static int
9070 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9071 {
9072 if (!cu->checked_producer)
9073 check_producer (cu);
9074
9075 return cu->producer_is_gcc_lt_4_3;
9076 }
9077
9078 static void
9079 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
9080 const char **name, const char **comp_dir)
9081 {
9082 /* Find the filename. Do not use dwarf2_name here, since the filename
9083 is not a source language identifier. */
9084 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9085 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9086
9087 if (*comp_dir == NULL
9088 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9089 && IS_ABSOLUTE_PATH (*name))
9090 {
9091 char *d = ldirname (*name);
9092
9093 *comp_dir = d;
9094 if (d != NULL)
9095 make_cleanup (xfree, d);
9096 }
9097 if (*comp_dir != NULL)
9098 {
9099 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9100 directory, get rid of it. */
9101 const char *cp = strchr (*comp_dir, ':');
9102
9103 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9104 *comp_dir = cp + 1;
9105 }
9106
9107 if (*name == NULL)
9108 *name = "<unknown>";
9109 }
9110
9111 /* Handle DW_AT_stmt_list for a compilation unit.
9112 DIE is the DW_TAG_compile_unit die for CU.
9113 COMP_DIR is the compilation directory. LOWPC is passed to
9114 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9115
9116 static void
9117 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9118 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9119 {
9120 struct objfile *objfile = dwarf2_per_objfile->objfile;
9121 struct attribute *attr;
9122 unsigned int line_offset;
9123 struct line_header line_header_local;
9124 hashval_t line_header_local_hash;
9125 unsigned u;
9126 void **slot;
9127 int decode_mapping;
9128
9129 gdb_assert (! cu->per_cu->is_debug_types);
9130
9131 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9132 if (attr == NULL)
9133 return;
9134
9135 line_offset = DW_UNSND (attr);
9136
9137 /* The line header hash table is only created if needed (it exists to
9138 prevent redundant reading of the line table for partial_units).
9139 If we're given a partial_unit, we'll need it. If we're given a
9140 compile_unit, then use the line header hash table if it's already
9141 created, but don't create one just yet. */
9142
9143 if (dwarf2_per_objfile->line_header_hash == NULL
9144 && die->tag == DW_TAG_partial_unit)
9145 {
9146 dwarf2_per_objfile->line_header_hash
9147 = htab_create_alloc_ex (127, line_header_hash_voidp,
9148 line_header_eq_voidp,
9149 free_line_header_voidp,
9150 &objfile->objfile_obstack,
9151 hashtab_obstack_allocate,
9152 dummy_obstack_deallocate);
9153 }
9154
9155 line_header_local.offset.sect_off = line_offset;
9156 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9157 line_header_local_hash = line_header_hash (&line_header_local);
9158 if (dwarf2_per_objfile->line_header_hash != NULL)
9159 {
9160 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9161 &line_header_local,
9162 line_header_local_hash, NO_INSERT);
9163
9164 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9165 is not present in *SLOT (since if there is something in *SLOT then
9166 it will be for a partial_unit). */
9167 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9168 {
9169 gdb_assert (*slot != NULL);
9170 cu->line_header = (struct line_header *) *slot;
9171 return;
9172 }
9173 }
9174
9175 /* dwarf_decode_line_header does not yet provide sufficient information.
9176 We always have to call also dwarf_decode_lines for it. */
9177 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9178 if (cu->line_header == NULL)
9179 return;
9180
9181 if (dwarf2_per_objfile->line_header_hash == NULL)
9182 slot = NULL;
9183 else
9184 {
9185 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9186 &line_header_local,
9187 line_header_local_hash, INSERT);
9188 gdb_assert (slot != NULL);
9189 }
9190 if (slot != NULL && *slot == NULL)
9191 {
9192 /* This newly decoded line number information unit will be owned
9193 by line_header_hash hash table. */
9194 *slot = cu->line_header;
9195 }
9196 else
9197 {
9198 /* We cannot free any current entry in (*slot) as that struct line_header
9199 may be already used by multiple CUs. Create only temporary decoded
9200 line_header for this CU - it may happen at most once for each line
9201 number information unit. And if we're not using line_header_hash
9202 then this is what we want as well. */
9203 gdb_assert (die->tag != DW_TAG_partial_unit);
9204 make_cleanup (free_cu_line_header, cu);
9205 }
9206 decode_mapping = (die->tag != DW_TAG_partial_unit);
9207 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9208 decode_mapping);
9209 }
9210
9211 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9212
9213 static void
9214 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9215 {
9216 struct objfile *objfile = dwarf2_per_objfile->objfile;
9217 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9218 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9219 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9220 CORE_ADDR highpc = ((CORE_ADDR) 0);
9221 struct attribute *attr;
9222 const char *name = NULL;
9223 const char *comp_dir = NULL;
9224 struct die_info *child_die;
9225 CORE_ADDR baseaddr;
9226
9227 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9228
9229 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9230
9231 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9232 from finish_block. */
9233 if (lowpc == ((CORE_ADDR) -1))
9234 lowpc = highpc;
9235 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9236
9237 find_file_and_directory (die, cu, &name, &comp_dir);
9238
9239 prepare_one_comp_unit (cu, die, cu->language);
9240
9241 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9242 standardised yet. As a workaround for the language detection we fall
9243 back to the DW_AT_producer string. */
9244 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9245 cu->language = language_opencl;
9246
9247 /* Similar hack for Go. */
9248 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9249 set_cu_language (DW_LANG_Go, cu);
9250
9251 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
9252
9253 /* Decode line number information if present. We do this before
9254 processing child DIEs, so that the line header table is available
9255 for DW_AT_decl_file. */
9256 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
9257
9258 /* Process all dies in compilation unit. */
9259 if (die->child != NULL)
9260 {
9261 child_die = die->child;
9262 while (child_die && child_die->tag)
9263 {
9264 process_die (child_die, cu);
9265 child_die = sibling_die (child_die);
9266 }
9267 }
9268
9269 /* Decode macro information, if present. Dwarf 2 macro information
9270 refers to information in the line number info statement program
9271 header, so we can only read it if we've read the header
9272 successfully. */
9273 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9274 if (attr && cu->line_header)
9275 {
9276 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9277 complaint (&symfile_complaints,
9278 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
9279
9280 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9281 }
9282 else
9283 {
9284 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9285 if (attr && cu->line_header)
9286 {
9287 unsigned int macro_offset = DW_UNSND (attr);
9288
9289 dwarf_decode_macros (cu, macro_offset, 0);
9290 }
9291 }
9292
9293 do_cleanups (back_to);
9294 }
9295
9296 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9297 Create the set of symtabs used by this TU, or if this TU is sharing
9298 symtabs with another TU and the symtabs have already been created
9299 then restore those symtabs in the line header.
9300 We don't need the pc/line-number mapping for type units. */
9301
9302 static void
9303 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9304 {
9305 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9306 struct type_unit_group *tu_group;
9307 int first_time;
9308 struct line_header *lh;
9309 struct attribute *attr;
9310 unsigned int i, line_offset;
9311 struct signatured_type *sig_type;
9312
9313 gdb_assert (per_cu->is_debug_types);
9314 sig_type = (struct signatured_type *) per_cu;
9315
9316 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9317
9318 /* If we're using .gdb_index (includes -readnow) then
9319 per_cu->type_unit_group may not have been set up yet. */
9320 if (sig_type->type_unit_group == NULL)
9321 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9322 tu_group = sig_type->type_unit_group;
9323
9324 /* If we've already processed this stmt_list there's no real need to
9325 do it again, we could fake it and just recreate the part we need
9326 (file name,index -> symtab mapping). If data shows this optimization
9327 is useful we can do it then. */
9328 first_time = tu_group->compunit_symtab == NULL;
9329
9330 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9331 debug info. */
9332 lh = NULL;
9333 if (attr != NULL)
9334 {
9335 line_offset = DW_UNSND (attr);
9336 lh = dwarf_decode_line_header (line_offset, cu);
9337 }
9338 if (lh == NULL)
9339 {
9340 if (first_time)
9341 dwarf2_start_symtab (cu, "", NULL, 0);
9342 else
9343 {
9344 gdb_assert (tu_group->symtabs == NULL);
9345 restart_symtab (tu_group->compunit_symtab, "", 0);
9346 }
9347 return;
9348 }
9349
9350 cu->line_header = lh;
9351 make_cleanup (free_cu_line_header, cu);
9352
9353 if (first_time)
9354 {
9355 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9356
9357 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9358 still initializing it, and our caller (a few levels up)
9359 process_full_type_unit still needs to know if this is the first
9360 time. */
9361
9362 tu_group->num_symtabs = lh->num_file_names;
9363 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
9364
9365 for (i = 0; i < lh->num_file_names; ++i)
9366 {
9367 const char *dir = NULL;
9368 struct file_entry *fe = &lh->file_names[i];
9369
9370 if (fe->dir_index && lh->include_dirs != NULL)
9371 dir = lh->include_dirs[fe->dir_index - 1];
9372 dwarf2_start_subfile (fe->name, dir);
9373
9374 if (current_subfile->symtab == NULL)
9375 {
9376 /* NOTE: start_subfile will recognize when it's been passed
9377 a file it has already seen. So we can't assume there's a
9378 simple mapping from lh->file_names to subfiles, plus
9379 lh->file_names may contain dups. */
9380 current_subfile->symtab
9381 = allocate_symtab (cust, current_subfile->name);
9382 }
9383
9384 fe->symtab = current_subfile->symtab;
9385 tu_group->symtabs[i] = fe->symtab;
9386 }
9387 }
9388 else
9389 {
9390 restart_symtab (tu_group->compunit_symtab, "", 0);
9391
9392 for (i = 0; i < lh->num_file_names; ++i)
9393 {
9394 struct file_entry *fe = &lh->file_names[i];
9395
9396 fe->symtab = tu_group->symtabs[i];
9397 }
9398 }
9399
9400 /* The main symtab is allocated last. Type units don't have DW_AT_name
9401 so they don't have a "real" (so to speak) symtab anyway.
9402 There is later code that will assign the main symtab to all symbols
9403 that don't have one. We need to handle the case of a symbol with a
9404 missing symtab (DW_AT_decl_file) anyway. */
9405 }
9406
9407 /* Process DW_TAG_type_unit.
9408 For TUs we want to skip the first top level sibling if it's not the
9409 actual type being defined by this TU. In this case the first top
9410 level sibling is there to provide context only. */
9411
9412 static void
9413 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9414 {
9415 struct die_info *child_die;
9416
9417 prepare_one_comp_unit (cu, die, language_minimal);
9418
9419 /* Initialize (or reinitialize) the machinery for building symtabs.
9420 We do this before processing child DIEs, so that the line header table
9421 is available for DW_AT_decl_file. */
9422 setup_type_unit_groups (die, cu);
9423
9424 if (die->child != NULL)
9425 {
9426 child_die = die->child;
9427 while (child_die && child_die->tag)
9428 {
9429 process_die (child_die, cu);
9430 child_die = sibling_die (child_die);
9431 }
9432 }
9433 }
9434 \f
9435 /* DWO/DWP files.
9436
9437 http://gcc.gnu.org/wiki/DebugFission
9438 http://gcc.gnu.org/wiki/DebugFissionDWP
9439
9440 To simplify handling of both DWO files ("object" files with the DWARF info)
9441 and DWP files (a file with the DWOs packaged up into one file), we treat
9442 DWP files as having a collection of virtual DWO files. */
9443
9444 static hashval_t
9445 hash_dwo_file (const void *item)
9446 {
9447 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9448 hashval_t hash;
9449
9450 hash = htab_hash_string (dwo_file->dwo_name);
9451 if (dwo_file->comp_dir != NULL)
9452 hash += htab_hash_string (dwo_file->comp_dir);
9453 return hash;
9454 }
9455
9456 static int
9457 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9458 {
9459 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9460 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9461
9462 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9463 return 0;
9464 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9465 return lhs->comp_dir == rhs->comp_dir;
9466 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9467 }
9468
9469 /* Allocate a hash table for DWO files. */
9470
9471 static htab_t
9472 allocate_dwo_file_hash_table (void)
9473 {
9474 struct objfile *objfile = dwarf2_per_objfile->objfile;
9475
9476 return htab_create_alloc_ex (41,
9477 hash_dwo_file,
9478 eq_dwo_file,
9479 NULL,
9480 &objfile->objfile_obstack,
9481 hashtab_obstack_allocate,
9482 dummy_obstack_deallocate);
9483 }
9484
9485 /* Lookup DWO file DWO_NAME. */
9486
9487 static void **
9488 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9489 {
9490 struct dwo_file find_entry;
9491 void **slot;
9492
9493 if (dwarf2_per_objfile->dwo_files == NULL)
9494 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9495
9496 memset (&find_entry, 0, sizeof (find_entry));
9497 find_entry.dwo_name = dwo_name;
9498 find_entry.comp_dir = comp_dir;
9499 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9500
9501 return slot;
9502 }
9503
9504 static hashval_t
9505 hash_dwo_unit (const void *item)
9506 {
9507 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9508
9509 /* This drops the top 32 bits of the id, but is ok for a hash. */
9510 return dwo_unit->signature;
9511 }
9512
9513 static int
9514 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9515 {
9516 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9517 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9518
9519 /* The signature is assumed to be unique within the DWO file.
9520 So while object file CU dwo_id's always have the value zero,
9521 that's OK, assuming each object file DWO file has only one CU,
9522 and that's the rule for now. */
9523 return lhs->signature == rhs->signature;
9524 }
9525
9526 /* Allocate a hash table for DWO CUs,TUs.
9527 There is one of these tables for each of CUs,TUs for each DWO file. */
9528
9529 static htab_t
9530 allocate_dwo_unit_table (struct objfile *objfile)
9531 {
9532 /* Start out with a pretty small number.
9533 Generally DWO files contain only one CU and maybe some TUs. */
9534 return htab_create_alloc_ex (3,
9535 hash_dwo_unit,
9536 eq_dwo_unit,
9537 NULL,
9538 &objfile->objfile_obstack,
9539 hashtab_obstack_allocate,
9540 dummy_obstack_deallocate);
9541 }
9542
9543 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9544
9545 struct create_dwo_cu_data
9546 {
9547 struct dwo_file *dwo_file;
9548 struct dwo_unit dwo_unit;
9549 };
9550
9551 /* die_reader_func for create_dwo_cu. */
9552
9553 static void
9554 create_dwo_cu_reader (const struct die_reader_specs *reader,
9555 const gdb_byte *info_ptr,
9556 struct die_info *comp_unit_die,
9557 int has_children,
9558 void *datap)
9559 {
9560 struct dwarf2_cu *cu = reader->cu;
9561 sect_offset offset = cu->per_cu->offset;
9562 struct dwarf2_section_info *section = cu->per_cu->section;
9563 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9564 struct dwo_file *dwo_file = data->dwo_file;
9565 struct dwo_unit *dwo_unit = &data->dwo_unit;
9566 struct attribute *attr;
9567
9568 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9569 if (attr == NULL)
9570 {
9571 complaint (&symfile_complaints,
9572 _("Dwarf Error: debug entry at offset 0x%x is missing"
9573 " its dwo_id [in module %s]"),
9574 offset.sect_off, dwo_file->dwo_name);
9575 return;
9576 }
9577
9578 dwo_unit->dwo_file = dwo_file;
9579 dwo_unit->signature = DW_UNSND (attr);
9580 dwo_unit->section = section;
9581 dwo_unit->offset = offset;
9582 dwo_unit->length = cu->per_cu->length;
9583
9584 if (dwarf_read_debug)
9585 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9586 offset.sect_off, hex_string (dwo_unit->signature));
9587 }
9588
9589 /* Create the dwo_unit for the lone CU in DWO_FILE.
9590 Note: This function processes DWO files only, not DWP files. */
9591
9592 static struct dwo_unit *
9593 create_dwo_cu (struct dwo_file *dwo_file)
9594 {
9595 struct objfile *objfile = dwarf2_per_objfile->objfile;
9596 struct dwarf2_section_info *section = &dwo_file->sections.info;
9597 const gdb_byte *info_ptr, *end_ptr;
9598 struct create_dwo_cu_data create_dwo_cu_data;
9599 struct dwo_unit *dwo_unit;
9600
9601 dwarf2_read_section (objfile, section);
9602 info_ptr = section->buffer;
9603
9604 if (info_ptr == NULL)
9605 return NULL;
9606
9607 if (dwarf_read_debug)
9608 {
9609 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9610 get_section_name (section),
9611 get_section_file_name (section));
9612 }
9613
9614 create_dwo_cu_data.dwo_file = dwo_file;
9615 dwo_unit = NULL;
9616
9617 end_ptr = info_ptr + section->size;
9618 while (info_ptr < end_ptr)
9619 {
9620 struct dwarf2_per_cu_data per_cu;
9621
9622 memset (&create_dwo_cu_data.dwo_unit, 0,
9623 sizeof (create_dwo_cu_data.dwo_unit));
9624 memset (&per_cu, 0, sizeof (per_cu));
9625 per_cu.objfile = objfile;
9626 per_cu.is_debug_types = 0;
9627 per_cu.offset.sect_off = info_ptr - section->buffer;
9628 per_cu.section = section;
9629
9630 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
9631 create_dwo_cu_reader,
9632 &create_dwo_cu_data);
9633
9634 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9635 {
9636 /* If we've already found one, complain. We only support one
9637 because having more than one requires hacking the dwo_name of
9638 each to match, which is highly unlikely to happen. */
9639 if (dwo_unit != NULL)
9640 {
9641 complaint (&symfile_complaints,
9642 _("Multiple CUs in DWO file %s [in module %s]"),
9643 dwo_file->dwo_name, objfile_name (objfile));
9644 break;
9645 }
9646
9647 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9648 *dwo_unit = create_dwo_cu_data.dwo_unit;
9649 }
9650
9651 info_ptr += per_cu.length;
9652 }
9653
9654 return dwo_unit;
9655 }
9656
9657 /* DWP file .debug_{cu,tu}_index section format:
9658 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9659
9660 DWP Version 1:
9661
9662 Both index sections have the same format, and serve to map a 64-bit
9663 signature to a set of section numbers. Each section begins with a header,
9664 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9665 indexes, and a pool of 32-bit section numbers. The index sections will be
9666 aligned at 8-byte boundaries in the file.
9667
9668 The index section header consists of:
9669
9670 V, 32 bit version number
9671 -, 32 bits unused
9672 N, 32 bit number of compilation units or type units in the index
9673 M, 32 bit number of slots in the hash table
9674
9675 Numbers are recorded using the byte order of the application binary.
9676
9677 The hash table begins at offset 16 in the section, and consists of an array
9678 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9679 order of the application binary). Unused slots in the hash table are 0.
9680 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9681
9682 The parallel table begins immediately after the hash table
9683 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9684 array of 32-bit indexes (using the byte order of the application binary),
9685 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9686 table contains a 32-bit index into the pool of section numbers. For unused
9687 hash table slots, the corresponding entry in the parallel table will be 0.
9688
9689 The pool of section numbers begins immediately following the hash table
9690 (at offset 16 + 12 * M from the beginning of the section). The pool of
9691 section numbers consists of an array of 32-bit words (using the byte order
9692 of the application binary). Each item in the array is indexed starting
9693 from 0. The hash table entry provides the index of the first section
9694 number in the set. Additional section numbers in the set follow, and the
9695 set is terminated by a 0 entry (section number 0 is not used in ELF).
9696
9697 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9698 section must be the first entry in the set, and the .debug_abbrev.dwo must
9699 be the second entry. Other members of the set may follow in any order.
9700
9701 ---
9702
9703 DWP Version 2:
9704
9705 DWP Version 2 combines all the .debug_info, etc. sections into one,
9706 and the entries in the index tables are now offsets into these sections.
9707 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9708 section.
9709
9710 Index Section Contents:
9711 Header
9712 Hash Table of Signatures dwp_hash_table.hash_table
9713 Parallel Table of Indices dwp_hash_table.unit_table
9714 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9715 Table of Section Sizes dwp_hash_table.v2.sizes
9716
9717 The index section header consists of:
9718
9719 V, 32 bit version number
9720 L, 32 bit number of columns in the table of section offsets
9721 N, 32 bit number of compilation units or type units in the index
9722 M, 32 bit number of slots in the hash table
9723
9724 Numbers are recorded using the byte order of the application binary.
9725
9726 The hash table has the same format as version 1.
9727 The parallel table of indices has the same format as version 1,
9728 except that the entries are origin-1 indices into the table of sections
9729 offsets and the table of section sizes.
9730
9731 The table of offsets begins immediately following the parallel table
9732 (at offset 16 + 12 * M from the beginning of the section). The table is
9733 a two-dimensional array of 32-bit words (using the byte order of the
9734 application binary), with L columns and N+1 rows, in row-major order.
9735 Each row in the array is indexed starting from 0. The first row provides
9736 a key to the remaining rows: each column in this row provides an identifier
9737 for a debug section, and the offsets in the same column of subsequent rows
9738 refer to that section. The section identifiers are:
9739
9740 DW_SECT_INFO 1 .debug_info.dwo
9741 DW_SECT_TYPES 2 .debug_types.dwo
9742 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9743 DW_SECT_LINE 4 .debug_line.dwo
9744 DW_SECT_LOC 5 .debug_loc.dwo
9745 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9746 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9747 DW_SECT_MACRO 8 .debug_macro.dwo
9748
9749 The offsets provided by the CU and TU index sections are the base offsets
9750 for the contributions made by each CU or TU to the corresponding section
9751 in the package file. Each CU and TU header contains an abbrev_offset
9752 field, used to find the abbreviations table for that CU or TU within the
9753 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9754 be interpreted as relative to the base offset given in the index section.
9755 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9756 should be interpreted as relative to the base offset for .debug_line.dwo,
9757 and offsets into other debug sections obtained from DWARF attributes should
9758 also be interpreted as relative to the corresponding base offset.
9759
9760 The table of sizes begins immediately following the table of offsets.
9761 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9762 with L columns and N rows, in row-major order. Each row in the array is
9763 indexed starting from 1 (row 0 is shared by the two tables).
9764
9765 ---
9766
9767 Hash table lookup is handled the same in version 1 and 2:
9768
9769 We assume that N and M will not exceed 2^32 - 1.
9770 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9771
9772 Given a 64-bit compilation unit signature or a type signature S, an entry
9773 in the hash table is located as follows:
9774
9775 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9776 the low-order k bits all set to 1.
9777
9778 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9779
9780 3) If the hash table entry at index H matches the signature, use that
9781 entry. If the hash table entry at index H is unused (all zeroes),
9782 terminate the search: the signature is not present in the table.
9783
9784 4) Let H = (H + H') modulo M. Repeat at Step 3.
9785
9786 Because M > N and H' and M are relatively prime, the search is guaranteed
9787 to stop at an unused slot or find the match. */
9788
9789 /* Create a hash table to map DWO IDs to their CU/TU entry in
9790 .debug_{info,types}.dwo in DWP_FILE.
9791 Returns NULL if there isn't one.
9792 Note: This function processes DWP files only, not DWO files. */
9793
9794 static struct dwp_hash_table *
9795 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9796 {
9797 struct objfile *objfile = dwarf2_per_objfile->objfile;
9798 bfd *dbfd = dwp_file->dbfd;
9799 const gdb_byte *index_ptr, *index_end;
9800 struct dwarf2_section_info *index;
9801 uint32_t version, nr_columns, nr_units, nr_slots;
9802 struct dwp_hash_table *htab;
9803
9804 if (is_debug_types)
9805 index = &dwp_file->sections.tu_index;
9806 else
9807 index = &dwp_file->sections.cu_index;
9808
9809 if (dwarf2_section_empty_p (index))
9810 return NULL;
9811 dwarf2_read_section (objfile, index);
9812
9813 index_ptr = index->buffer;
9814 index_end = index_ptr + index->size;
9815
9816 version = read_4_bytes (dbfd, index_ptr);
9817 index_ptr += 4;
9818 if (version == 2)
9819 nr_columns = read_4_bytes (dbfd, index_ptr);
9820 else
9821 nr_columns = 0;
9822 index_ptr += 4;
9823 nr_units = read_4_bytes (dbfd, index_ptr);
9824 index_ptr += 4;
9825 nr_slots = read_4_bytes (dbfd, index_ptr);
9826 index_ptr += 4;
9827
9828 if (version != 1 && version != 2)
9829 {
9830 error (_("Dwarf Error: unsupported DWP file version (%s)"
9831 " [in module %s]"),
9832 pulongest (version), dwp_file->name);
9833 }
9834 if (nr_slots != (nr_slots & -nr_slots))
9835 {
9836 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9837 " is not power of 2 [in module %s]"),
9838 pulongest (nr_slots), dwp_file->name);
9839 }
9840
9841 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9842 htab->version = version;
9843 htab->nr_columns = nr_columns;
9844 htab->nr_units = nr_units;
9845 htab->nr_slots = nr_slots;
9846 htab->hash_table = index_ptr;
9847 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9848
9849 /* Exit early if the table is empty. */
9850 if (nr_slots == 0 || nr_units == 0
9851 || (version == 2 && nr_columns == 0))
9852 {
9853 /* All must be zero. */
9854 if (nr_slots != 0 || nr_units != 0
9855 || (version == 2 && nr_columns != 0))
9856 {
9857 complaint (&symfile_complaints,
9858 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9859 " all zero [in modules %s]"),
9860 dwp_file->name);
9861 }
9862 return htab;
9863 }
9864
9865 if (version == 1)
9866 {
9867 htab->section_pool.v1.indices =
9868 htab->unit_table + sizeof (uint32_t) * nr_slots;
9869 /* It's harder to decide whether the section is too small in v1.
9870 V1 is deprecated anyway so we punt. */
9871 }
9872 else
9873 {
9874 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9875 int *ids = htab->section_pool.v2.section_ids;
9876 /* Reverse map for error checking. */
9877 int ids_seen[DW_SECT_MAX + 1];
9878 int i;
9879
9880 if (nr_columns < 2)
9881 {
9882 error (_("Dwarf Error: bad DWP hash table, too few columns"
9883 " in section table [in module %s]"),
9884 dwp_file->name);
9885 }
9886 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9887 {
9888 error (_("Dwarf Error: bad DWP hash table, too many columns"
9889 " in section table [in module %s]"),
9890 dwp_file->name);
9891 }
9892 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9893 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9894 for (i = 0; i < nr_columns; ++i)
9895 {
9896 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9897
9898 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9899 {
9900 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9901 " in section table [in module %s]"),
9902 id, dwp_file->name);
9903 }
9904 if (ids_seen[id] != -1)
9905 {
9906 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9907 " id %d in section table [in module %s]"),
9908 id, dwp_file->name);
9909 }
9910 ids_seen[id] = i;
9911 ids[i] = id;
9912 }
9913 /* Must have exactly one info or types section. */
9914 if (((ids_seen[DW_SECT_INFO] != -1)
9915 + (ids_seen[DW_SECT_TYPES] != -1))
9916 != 1)
9917 {
9918 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9919 " DWO info/types section [in module %s]"),
9920 dwp_file->name);
9921 }
9922 /* Must have an abbrev section. */
9923 if (ids_seen[DW_SECT_ABBREV] == -1)
9924 {
9925 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9926 " section [in module %s]"),
9927 dwp_file->name);
9928 }
9929 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9930 htab->section_pool.v2.sizes =
9931 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9932 * nr_units * nr_columns);
9933 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9934 * nr_units * nr_columns))
9935 > index_end)
9936 {
9937 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9938 " [in module %s]"),
9939 dwp_file->name);
9940 }
9941 }
9942
9943 return htab;
9944 }
9945
9946 /* Update SECTIONS with the data from SECTP.
9947
9948 This function is like the other "locate" section routines that are
9949 passed to bfd_map_over_sections, but in this context the sections to
9950 read comes from the DWP V1 hash table, not the full ELF section table.
9951
9952 The result is non-zero for success, or zero if an error was found. */
9953
9954 static int
9955 locate_v1_virtual_dwo_sections (asection *sectp,
9956 struct virtual_v1_dwo_sections *sections)
9957 {
9958 const struct dwop_section_names *names = &dwop_section_names;
9959
9960 if (section_is_p (sectp->name, &names->abbrev_dwo))
9961 {
9962 /* There can be only one. */
9963 if (sections->abbrev.s.section != NULL)
9964 return 0;
9965 sections->abbrev.s.section = sectp;
9966 sections->abbrev.size = bfd_get_section_size (sectp);
9967 }
9968 else if (section_is_p (sectp->name, &names->info_dwo)
9969 || section_is_p (sectp->name, &names->types_dwo))
9970 {
9971 /* There can be only one. */
9972 if (sections->info_or_types.s.section != NULL)
9973 return 0;
9974 sections->info_or_types.s.section = sectp;
9975 sections->info_or_types.size = bfd_get_section_size (sectp);
9976 }
9977 else if (section_is_p (sectp->name, &names->line_dwo))
9978 {
9979 /* There can be only one. */
9980 if (sections->line.s.section != NULL)
9981 return 0;
9982 sections->line.s.section = sectp;
9983 sections->line.size = bfd_get_section_size (sectp);
9984 }
9985 else if (section_is_p (sectp->name, &names->loc_dwo))
9986 {
9987 /* There can be only one. */
9988 if (sections->loc.s.section != NULL)
9989 return 0;
9990 sections->loc.s.section = sectp;
9991 sections->loc.size = bfd_get_section_size (sectp);
9992 }
9993 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9994 {
9995 /* There can be only one. */
9996 if (sections->macinfo.s.section != NULL)
9997 return 0;
9998 sections->macinfo.s.section = sectp;
9999 sections->macinfo.size = bfd_get_section_size (sectp);
10000 }
10001 else if (section_is_p (sectp->name, &names->macro_dwo))
10002 {
10003 /* There can be only one. */
10004 if (sections->macro.s.section != NULL)
10005 return 0;
10006 sections->macro.s.section = sectp;
10007 sections->macro.size = bfd_get_section_size (sectp);
10008 }
10009 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10010 {
10011 /* There can be only one. */
10012 if (sections->str_offsets.s.section != NULL)
10013 return 0;
10014 sections->str_offsets.s.section = sectp;
10015 sections->str_offsets.size = bfd_get_section_size (sectp);
10016 }
10017 else
10018 {
10019 /* No other kind of section is valid. */
10020 return 0;
10021 }
10022
10023 return 1;
10024 }
10025
10026 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10027 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10028 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10029 This is for DWP version 1 files. */
10030
10031 static struct dwo_unit *
10032 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10033 uint32_t unit_index,
10034 const char *comp_dir,
10035 ULONGEST signature, int is_debug_types)
10036 {
10037 struct objfile *objfile = dwarf2_per_objfile->objfile;
10038 const struct dwp_hash_table *dwp_htab =
10039 is_debug_types ? dwp_file->tus : dwp_file->cus;
10040 bfd *dbfd = dwp_file->dbfd;
10041 const char *kind = is_debug_types ? "TU" : "CU";
10042 struct dwo_file *dwo_file;
10043 struct dwo_unit *dwo_unit;
10044 struct virtual_v1_dwo_sections sections;
10045 void **dwo_file_slot;
10046 char *virtual_dwo_name;
10047 struct cleanup *cleanups;
10048 int i;
10049
10050 gdb_assert (dwp_file->version == 1);
10051
10052 if (dwarf_read_debug)
10053 {
10054 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10055 kind,
10056 pulongest (unit_index), hex_string (signature),
10057 dwp_file->name);
10058 }
10059
10060 /* Fetch the sections of this DWO unit.
10061 Put a limit on the number of sections we look for so that bad data
10062 doesn't cause us to loop forever. */
10063
10064 #define MAX_NR_V1_DWO_SECTIONS \
10065 (1 /* .debug_info or .debug_types */ \
10066 + 1 /* .debug_abbrev */ \
10067 + 1 /* .debug_line */ \
10068 + 1 /* .debug_loc */ \
10069 + 1 /* .debug_str_offsets */ \
10070 + 1 /* .debug_macro or .debug_macinfo */ \
10071 + 1 /* trailing zero */)
10072
10073 memset (&sections, 0, sizeof (sections));
10074 cleanups = make_cleanup (null_cleanup, 0);
10075
10076 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10077 {
10078 asection *sectp;
10079 uint32_t section_nr =
10080 read_4_bytes (dbfd,
10081 dwp_htab->section_pool.v1.indices
10082 + (unit_index + i) * sizeof (uint32_t));
10083
10084 if (section_nr == 0)
10085 break;
10086 if (section_nr >= dwp_file->num_sections)
10087 {
10088 error (_("Dwarf Error: bad DWP hash table, section number too large"
10089 " [in module %s]"),
10090 dwp_file->name);
10091 }
10092
10093 sectp = dwp_file->elf_sections[section_nr];
10094 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10095 {
10096 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10097 " [in module %s]"),
10098 dwp_file->name);
10099 }
10100 }
10101
10102 if (i < 2
10103 || dwarf2_section_empty_p (&sections.info_or_types)
10104 || dwarf2_section_empty_p (&sections.abbrev))
10105 {
10106 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10107 " [in module %s]"),
10108 dwp_file->name);
10109 }
10110 if (i == MAX_NR_V1_DWO_SECTIONS)
10111 {
10112 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10113 " [in module %s]"),
10114 dwp_file->name);
10115 }
10116
10117 /* It's easier for the rest of the code if we fake a struct dwo_file and
10118 have dwo_unit "live" in that. At least for now.
10119
10120 The DWP file can be made up of a random collection of CUs and TUs.
10121 However, for each CU + set of TUs that came from the same original DWO
10122 file, we can combine them back into a virtual DWO file to save space
10123 (fewer struct dwo_file objects to allocate). Remember that for really
10124 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10125
10126 virtual_dwo_name =
10127 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10128 get_section_id (&sections.abbrev),
10129 get_section_id (&sections.line),
10130 get_section_id (&sections.loc),
10131 get_section_id (&sections.str_offsets));
10132 make_cleanup (xfree, virtual_dwo_name);
10133 /* Can we use an existing virtual DWO file? */
10134 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10135 /* Create one if necessary. */
10136 if (*dwo_file_slot == NULL)
10137 {
10138 if (dwarf_read_debug)
10139 {
10140 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10141 virtual_dwo_name);
10142 }
10143 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10144 dwo_file->dwo_name
10145 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10146 virtual_dwo_name,
10147 strlen (virtual_dwo_name));
10148 dwo_file->comp_dir = comp_dir;
10149 dwo_file->sections.abbrev = sections.abbrev;
10150 dwo_file->sections.line = sections.line;
10151 dwo_file->sections.loc = sections.loc;
10152 dwo_file->sections.macinfo = sections.macinfo;
10153 dwo_file->sections.macro = sections.macro;
10154 dwo_file->sections.str_offsets = sections.str_offsets;
10155 /* The "str" section is global to the entire DWP file. */
10156 dwo_file->sections.str = dwp_file->sections.str;
10157 /* The info or types section is assigned below to dwo_unit,
10158 there's no need to record it in dwo_file.
10159 Also, we can't simply record type sections in dwo_file because
10160 we record a pointer into the vector in dwo_unit. As we collect more
10161 types we'll grow the vector and eventually have to reallocate space
10162 for it, invalidating all copies of pointers into the previous
10163 contents. */
10164 *dwo_file_slot = dwo_file;
10165 }
10166 else
10167 {
10168 if (dwarf_read_debug)
10169 {
10170 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10171 virtual_dwo_name);
10172 }
10173 dwo_file = (struct dwo_file *) *dwo_file_slot;
10174 }
10175 do_cleanups (cleanups);
10176
10177 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10178 dwo_unit->dwo_file = dwo_file;
10179 dwo_unit->signature = signature;
10180 dwo_unit->section =
10181 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10182 *dwo_unit->section = sections.info_or_types;
10183 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10184
10185 return dwo_unit;
10186 }
10187
10188 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10189 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10190 piece within that section used by a TU/CU, return a virtual section
10191 of just that piece. */
10192
10193 static struct dwarf2_section_info
10194 create_dwp_v2_section (struct dwarf2_section_info *section,
10195 bfd_size_type offset, bfd_size_type size)
10196 {
10197 struct dwarf2_section_info result;
10198 asection *sectp;
10199
10200 gdb_assert (section != NULL);
10201 gdb_assert (!section->is_virtual);
10202
10203 memset (&result, 0, sizeof (result));
10204 result.s.containing_section = section;
10205 result.is_virtual = 1;
10206
10207 if (size == 0)
10208 return result;
10209
10210 sectp = get_section_bfd_section (section);
10211
10212 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10213 bounds of the real section. This is a pretty-rare event, so just
10214 flag an error (easier) instead of a warning and trying to cope. */
10215 if (sectp == NULL
10216 || offset + size > bfd_get_section_size (sectp))
10217 {
10218 bfd *abfd = sectp->owner;
10219
10220 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10221 " in section %s [in module %s]"),
10222 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10223 objfile_name (dwarf2_per_objfile->objfile));
10224 }
10225
10226 result.virtual_offset = offset;
10227 result.size = size;
10228 return result;
10229 }
10230
10231 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10232 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10233 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10234 This is for DWP version 2 files. */
10235
10236 static struct dwo_unit *
10237 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10238 uint32_t unit_index,
10239 const char *comp_dir,
10240 ULONGEST signature, int is_debug_types)
10241 {
10242 struct objfile *objfile = dwarf2_per_objfile->objfile;
10243 const struct dwp_hash_table *dwp_htab =
10244 is_debug_types ? dwp_file->tus : dwp_file->cus;
10245 bfd *dbfd = dwp_file->dbfd;
10246 const char *kind = is_debug_types ? "TU" : "CU";
10247 struct dwo_file *dwo_file;
10248 struct dwo_unit *dwo_unit;
10249 struct virtual_v2_dwo_sections sections;
10250 void **dwo_file_slot;
10251 char *virtual_dwo_name;
10252 struct cleanup *cleanups;
10253 int i;
10254
10255 gdb_assert (dwp_file->version == 2);
10256
10257 if (dwarf_read_debug)
10258 {
10259 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10260 kind,
10261 pulongest (unit_index), hex_string (signature),
10262 dwp_file->name);
10263 }
10264
10265 /* Fetch the section offsets of this DWO unit. */
10266
10267 memset (&sections, 0, sizeof (sections));
10268 cleanups = make_cleanup (null_cleanup, 0);
10269
10270 for (i = 0; i < dwp_htab->nr_columns; ++i)
10271 {
10272 uint32_t offset = read_4_bytes (dbfd,
10273 dwp_htab->section_pool.v2.offsets
10274 + (((unit_index - 1) * dwp_htab->nr_columns
10275 + i)
10276 * sizeof (uint32_t)));
10277 uint32_t size = read_4_bytes (dbfd,
10278 dwp_htab->section_pool.v2.sizes
10279 + (((unit_index - 1) * dwp_htab->nr_columns
10280 + i)
10281 * sizeof (uint32_t)));
10282
10283 switch (dwp_htab->section_pool.v2.section_ids[i])
10284 {
10285 case DW_SECT_INFO:
10286 case DW_SECT_TYPES:
10287 sections.info_or_types_offset = offset;
10288 sections.info_or_types_size = size;
10289 break;
10290 case DW_SECT_ABBREV:
10291 sections.abbrev_offset = offset;
10292 sections.abbrev_size = size;
10293 break;
10294 case DW_SECT_LINE:
10295 sections.line_offset = offset;
10296 sections.line_size = size;
10297 break;
10298 case DW_SECT_LOC:
10299 sections.loc_offset = offset;
10300 sections.loc_size = size;
10301 break;
10302 case DW_SECT_STR_OFFSETS:
10303 sections.str_offsets_offset = offset;
10304 sections.str_offsets_size = size;
10305 break;
10306 case DW_SECT_MACINFO:
10307 sections.macinfo_offset = offset;
10308 sections.macinfo_size = size;
10309 break;
10310 case DW_SECT_MACRO:
10311 sections.macro_offset = offset;
10312 sections.macro_size = size;
10313 break;
10314 }
10315 }
10316
10317 /* It's easier for the rest of the code if we fake a struct dwo_file and
10318 have dwo_unit "live" in that. At least for now.
10319
10320 The DWP file can be made up of a random collection of CUs and TUs.
10321 However, for each CU + set of TUs that came from the same original DWO
10322 file, we can combine them back into a virtual DWO file to save space
10323 (fewer struct dwo_file objects to allocate). Remember that for really
10324 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10325
10326 virtual_dwo_name =
10327 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10328 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10329 (long) (sections.line_size ? sections.line_offset : 0),
10330 (long) (sections.loc_size ? sections.loc_offset : 0),
10331 (long) (sections.str_offsets_size
10332 ? sections.str_offsets_offset : 0));
10333 make_cleanup (xfree, virtual_dwo_name);
10334 /* Can we use an existing virtual DWO file? */
10335 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10336 /* Create one if necessary. */
10337 if (*dwo_file_slot == NULL)
10338 {
10339 if (dwarf_read_debug)
10340 {
10341 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10342 virtual_dwo_name);
10343 }
10344 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10345 dwo_file->dwo_name
10346 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10347 virtual_dwo_name,
10348 strlen (virtual_dwo_name));
10349 dwo_file->comp_dir = comp_dir;
10350 dwo_file->sections.abbrev =
10351 create_dwp_v2_section (&dwp_file->sections.abbrev,
10352 sections.abbrev_offset, sections.abbrev_size);
10353 dwo_file->sections.line =
10354 create_dwp_v2_section (&dwp_file->sections.line,
10355 sections.line_offset, sections.line_size);
10356 dwo_file->sections.loc =
10357 create_dwp_v2_section (&dwp_file->sections.loc,
10358 sections.loc_offset, sections.loc_size);
10359 dwo_file->sections.macinfo =
10360 create_dwp_v2_section (&dwp_file->sections.macinfo,
10361 sections.macinfo_offset, sections.macinfo_size);
10362 dwo_file->sections.macro =
10363 create_dwp_v2_section (&dwp_file->sections.macro,
10364 sections.macro_offset, sections.macro_size);
10365 dwo_file->sections.str_offsets =
10366 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10367 sections.str_offsets_offset,
10368 sections.str_offsets_size);
10369 /* The "str" section is global to the entire DWP file. */
10370 dwo_file->sections.str = dwp_file->sections.str;
10371 /* The info or types section is assigned below to dwo_unit,
10372 there's no need to record it in dwo_file.
10373 Also, we can't simply record type sections in dwo_file because
10374 we record a pointer into the vector in dwo_unit. As we collect more
10375 types we'll grow the vector and eventually have to reallocate space
10376 for it, invalidating all copies of pointers into the previous
10377 contents. */
10378 *dwo_file_slot = dwo_file;
10379 }
10380 else
10381 {
10382 if (dwarf_read_debug)
10383 {
10384 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10385 virtual_dwo_name);
10386 }
10387 dwo_file = (struct dwo_file *) *dwo_file_slot;
10388 }
10389 do_cleanups (cleanups);
10390
10391 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10392 dwo_unit->dwo_file = dwo_file;
10393 dwo_unit->signature = signature;
10394 dwo_unit->section =
10395 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10396 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10397 ? &dwp_file->sections.types
10398 : &dwp_file->sections.info,
10399 sections.info_or_types_offset,
10400 sections.info_or_types_size);
10401 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10402
10403 return dwo_unit;
10404 }
10405
10406 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10407 Returns NULL if the signature isn't found. */
10408
10409 static struct dwo_unit *
10410 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10411 ULONGEST signature, int is_debug_types)
10412 {
10413 const struct dwp_hash_table *dwp_htab =
10414 is_debug_types ? dwp_file->tus : dwp_file->cus;
10415 bfd *dbfd = dwp_file->dbfd;
10416 uint32_t mask = dwp_htab->nr_slots - 1;
10417 uint32_t hash = signature & mask;
10418 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10419 unsigned int i;
10420 void **slot;
10421 struct dwo_unit find_dwo_cu;
10422
10423 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10424 find_dwo_cu.signature = signature;
10425 slot = htab_find_slot (is_debug_types
10426 ? dwp_file->loaded_tus
10427 : dwp_file->loaded_cus,
10428 &find_dwo_cu, INSERT);
10429
10430 if (*slot != NULL)
10431 return (struct dwo_unit *) *slot;
10432
10433 /* Use a for loop so that we don't loop forever on bad debug info. */
10434 for (i = 0; i < dwp_htab->nr_slots; ++i)
10435 {
10436 ULONGEST signature_in_table;
10437
10438 signature_in_table =
10439 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10440 if (signature_in_table == signature)
10441 {
10442 uint32_t unit_index =
10443 read_4_bytes (dbfd,
10444 dwp_htab->unit_table + hash * sizeof (uint32_t));
10445
10446 if (dwp_file->version == 1)
10447 {
10448 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10449 comp_dir, signature,
10450 is_debug_types);
10451 }
10452 else
10453 {
10454 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10455 comp_dir, signature,
10456 is_debug_types);
10457 }
10458 return (struct dwo_unit *) *slot;
10459 }
10460 if (signature_in_table == 0)
10461 return NULL;
10462 hash = (hash + hash2) & mask;
10463 }
10464
10465 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10466 " [in module %s]"),
10467 dwp_file->name);
10468 }
10469
10470 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10471 Open the file specified by FILE_NAME and hand it off to BFD for
10472 preliminary analysis. Return a newly initialized bfd *, which
10473 includes a canonicalized copy of FILE_NAME.
10474 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10475 SEARCH_CWD is true if the current directory is to be searched.
10476 It will be searched before debug-file-directory.
10477 If successful, the file is added to the bfd include table of the
10478 objfile's bfd (see gdb_bfd_record_inclusion).
10479 If unable to find/open the file, return NULL.
10480 NOTE: This function is derived from symfile_bfd_open. */
10481
10482 static bfd *
10483 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10484 {
10485 bfd *sym_bfd;
10486 int desc, flags;
10487 char *absolute_name;
10488 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10489 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10490 to debug_file_directory. */
10491 char *search_path;
10492 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10493
10494 if (search_cwd)
10495 {
10496 if (*debug_file_directory != '\0')
10497 search_path = concat (".", dirname_separator_string,
10498 debug_file_directory, (char *) NULL);
10499 else
10500 search_path = xstrdup (".");
10501 }
10502 else
10503 search_path = xstrdup (debug_file_directory);
10504
10505 flags = OPF_RETURN_REALPATH;
10506 if (is_dwp)
10507 flags |= OPF_SEARCH_IN_PATH;
10508 desc = openp (search_path, flags, file_name,
10509 O_RDONLY | O_BINARY, &absolute_name);
10510 xfree (search_path);
10511 if (desc < 0)
10512 return NULL;
10513
10514 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
10515 xfree (absolute_name);
10516 if (sym_bfd == NULL)
10517 return NULL;
10518 bfd_set_cacheable (sym_bfd, 1);
10519
10520 if (!bfd_check_format (sym_bfd, bfd_object))
10521 {
10522 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
10523 return NULL;
10524 }
10525
10526 /* Success. Record the bfd as having been included by the objfile's bfd.
10527 This is important because things like demangled_names_hash lives in the
10528 objfile's per_bfd space and may have references to things like symbol
10529 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10530 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd);
10531
10532 return sym_bfd;
10533 }
10534
10535 /* Try to open DWO file FILE_NAME.
10536 COMP_DIR is the DW_AT_comp_dir attribute.
10537 The result is the bfd handle of the file.
10538 If there is a problem finding or opening the file, return NULL.
10539 Upon success, the canonicalized path of the file is stored in the bfd,
10540 same as symfile_bfd_open. */
10541
10542 static bfd *
10543 open_dwo_file (const char *file_name, const char *comp_dir)
10544 {
10545 bfd *abfd;
10546
10547 if (IS_ABSOLUTE_PATH (file_name))
10548 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10549
10550 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10551
10552 if (comp_dir != NULL)
10553 {
10554 char *path_to_try = concat (comp_dir, SLASH_STRING,
10555 file_name, (char *) NULL);
10556
10557 /* NOTE: If comp_dir is a relative path, this will also try the
10558 search path, which seems useful. */
10559 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
10560 xfree (path_to_try);
10561 if (abfd != NULL)
10562 return abfd;
10563 }
10564
10565 /* That didn't work, try debug-file-directory, which, despite its name,
10566 is a list of paths. */
10567
10568 if (*debug_file_directory == '\0')
10569 return NULL;
10570
10571 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10572 }
10573
10574 /* This function is mapped across the sections and remembers the offset and
10575 size of each of the DWO debugging sections we are interested in. */
10576
10577 static void
10578 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10579 {
10580 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10581 const struct dwop_section_names *names = &dwop_section_names;
10582
10583 if (section_is_p (sectp->name, &names->abbrev_dwo))
10584 {
10585 dwo_sections->abbrev.s.section = sectp;
10586 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10587 }
10588 else if (section_is_p (sectp->name, &names->info_dwo))
10589 {
10590 dwo_sections->info.s.section = sectp;
10591 dwo_sections->info.size = bfd_get_section_size (sectp);
10592 }
10593 else if (section_is_p (sectp->name, &names->line_dwo))
10594 {
10595 dwo_sections->line.s.section = sectp;
10596 dwo_sections->line.size = bfd_get_section_size (sectp);
10597 }
10598 else if (section_is_p (sectp->name, &names->loc_dwo))
10599 {
10600 dwo_sections->loc.s.section = sectp;
10601 dwo_sections->loc.size = bfd_get_section_size (sectp);
10602 }
10603 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10604 {
10605 dwo_sections->macinfo.s.section = sectp;
10606 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10607 }
10608 else if (section_is_p (sectp->name, &names->macro_dwo))
10609 {
10610 dwo_sections->macro.s.section = sectp;
10611 dwo_sections->macro.size = bfd_get_section_size (sectp);
10612 }
10613 else if (section_is_p (sectp->name, &names->str_dwo))
10614 {
10615 dwo_sections->str.s.section = sectp;
10616 dwo_sections->str.size = bfd_get_section_size (sectp);
10617 }
10618 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10619 {
10620 dwo_sections->str_offsets.s.section = sectp;
10621 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10622 }
10623 else if (section_is_p (sectp->name, &names->types_dwo))
10624 {
10625 struct dwarf2_section_info type_section;
10626
10627 memset (&type_section, 0, sizeof (type_section));
10628 type_section.s.section = sectp;
10629 type_section.size = bfd_get_section_size (sectp);
10630 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10631 &type_section);
10632 }
10633 }
10634
10635 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10636 by PER_CU. This is for the non-DWP case.
10637 The result is NULL if DWO_NAME can't be found. */
10638
10639 static struct dwo_file *
10640 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10641 const char *dwo_name, const char *comp_dir)
10642 {
10643 struct objfile *objfile = dwarf2_per_objfile->objfile;
10644 struct dwo_file *dwo_file;
10645 bfd *dbfd;
10646 struct cleanup *cleanups;
10647
10648 dbfd = open_dwo_file (dwo_name, comp_dir);
10649 if (dbfd == NULL)
10650 {
10651 if (dwarf_read_debug)
10652 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10653 return NULL;
10654 }
10655 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10656 dwo_file->dwo_name = dwo_name;
10657 dwo_file->comp_dir = comp_dir;
10658 dwo_file->dbfd = dbfd;
10659
10660 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10661
10662 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
10663
10664 dwo_file->cu = create_dwo_cu (dwo_file);
10665
10666 dwo_file->tus = create_debug_types_hash_table (dwo_file,
10667 dwo_file->sections.types);
10668
10669 discard_cleanups (cleanups);
10670
10671 if (dwarf_read_debug)
10672 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10673
10674 return dwo_file;
10675 }
10676
10677 /* This function is mapped across the sections and remembers the offset and
10678 size of each of the DWP debugging sections common to version 1 and 2 that
10679 we are interested in. */
10680
10681 static void
10682 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10683 void *dwp_file_ptr)
10684 {
10685 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10686 const struct dwop_section_names *names = &dwop_section_names;
10687 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10688
10689 /* Record the ELF section number for later lookup: this is what the
10690 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10691 gdb_assert (elf_section_nr < dwp_file->num_sections);
10692 dwp_file->elf_sections[elf_section_nr] = sectp;
10693
10694 /* Look for specific sections that we need. */
10695 if (section_is_p (sectp->name, &names->str_dwo))
10696 {
10697 dwp_file->sections.str.s.section = sectp;
10698 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10699 }
10700 else if (section_is_p (sectp->name, &names->cu_index))
10701 {
10702 dwp_file->sections.cu_index.s.section = sectp;
10703 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10704 }
10705 else if (section_is_p (sectp->name, &names->tu_index))
10706 {
10707 dwp_file->sections.tu_index.s.section = sectp;
10708 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10709 }
10710 }
10711
10712 /* This function is mapped across the sections and remembers the offset and
10713 size of each of the DWP version 2 debugging sections that we are interested
10714 in. This is split into a separate function because we don't know if we
10715 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10716
10717 static void
10718 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10719 {
10720 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10721 const struct dwop_section_names *names = &dwop_section_names;
10722 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10723
10724 /* Record the ELF section number for later lookup: this is what the
10725 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10726 gdb_assert (elf_section_nr < dwp_file->num_sections);
10727 dwp_file->elf_sections[elf_section_nr] = sectp;
10728
10729 /* Look for specific sections that we need. */
10730 if (section_is_p (sectp->name, &names->abbrev_dwo))
10731 {
10732 dwp_file->sections.abbrev.s.section = sectp;
10733 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10734 }
10735 else if (section_is_p (sectp->name, &names->info_dwo))
10736 {
10737 dwp_file->sections.info.s.section = sectp;
10738 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10739 }
10740 else if (section_is_p (sectp->name, &names->line_dwo))
10741 {
10742 dwp_file->sections.line.s.section = sectp;
10743 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10744 }
10745 else if (section_is_p (sectp->name, &names->loc_dwo))
10746 {
10747 dwp_file->sections.loc.s.section = sectp;
10748 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10749 }
10750 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10751 {
10752 dwp_file->sections.macinfo.s.section = sectp;
10753 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10754 }
10755 else if (section_is_p (sectp->name, &names->macro_dwo))
10756 {
10757 dwp_file->sections.macro.s.section = sectp;
10758 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10759 }
10760 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10761 {
10762 dwp_file->sections.str_offsets.s.section = sectp;
10763 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10764 }
10765 else if (section_is_p (sectp->name, &names->types_dwo))
10766 {
10767 dwp_file->sections.types.s.section = sectp;
10768 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10769 }
10770 }
10771
10772 /* Hash function for dwp_file loaded CUs/TUs. */
10773
10774 static hashval_t
10775 hash_dwp_loaded_cutus (const void *item)
10776 {
10777 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10778
10779 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10780 return dwo_unit->signature;
10781 }
10782
10783 /* Equality function for dwp_file loaded CUs/TUs. */
10784
10785 static int
10786 eq_dwp_loaded_cutus (const void *a, const void *b)
10787 {
10788 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10789 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10790
10791 return dua->signature == dub->signature;
10792 }
10793
10794 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10795
10796 static htab_t
10797 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10798 {
10799 return htab_create_alloc_ex (3,
10800 hash_dwp_loaded_cutus,
10801 eq_dwp_loaded_cutus,
10802 NULL,
10803 &objfile->objfile_obstack,
10804 hashtab_obstack_allocate,
10805 dummy_obstack_deallocate);
10806 }
10807
10808 /* Try to open DWP file FILE_NAME.
10809 The result is the bfd handle of the file.
10810 If there is a problem finding or opening the file, return NULL.
10811 Upon success, the canonicalized path of the file is stored in the bfd,
10812 same as symfile_bfd_open. */
10813
10814 static bfd *
10815 open_dwp_file (const char *file_name)
10816 {
10817 bfd *abfd;
10818
10819 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
10820 if (abfd != NULL)
10821 return abfd;
10822
10823 /* Work around upstream bug 15652.
10824 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10825 [Whether that's a "bug" is debatable, but it is getting in our way.]
10826 We have no real idea where the dwp file is, because gdb's realpath-ing
10827 of the executable's path may have discarded the needed info.
10828 [IWBN if the dwp file name was recorded in the executable, akin to
10829 .gnu_debuglink, but that doesn't exist yet.]
10830 Strip the directory from FILE_NAME and search again. */
10831 if (*debug_file_directory != '\0')
10832 {
10833 /* Don't implicitly search the current directory here.
10834 If the user wants to search "." to handle this case,
10835 it must be added to debug-file-directory. */
10836 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10837 0 /*search_cwd*/);
10838 }
10839
10840 return NULL;
10841 }
10842
10843 /* Initialize the use of the DWP file for the current objfile.
10844 By convention the name of the DWP file is ${objfile}.dwp.
10845 The result is NULL if it can't be found. */
10846
10847 static struct dwp_file *
10848 open_and_init_dwp_file (void)
10849 {
10850 struct objfile *objfile = dwarf2_per_objfile->objfile;
10851 struct dwp_file *dwp_file;
10852 char *dwp_name;
10853 bfd *dbfd;
10854 struct cleanup *cleanups = make_cleanup (null_cleanup, 0);
10855
10856 /* Try to find first .dwp for the binary file before any symbolic links
10857 resolving. */
10858
10859 /* If the objfile is a debug file, find the name of the real binary
10860 file and get the name of dwp file from there. */
10861 if (objfile->separate_debug_objfile_backlink != NULL)
10862 {
10863 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10864 const char *backlink_basename = lbasename (backlink->original_name);
10865 char *debug_dirname = ldirname (objfile->original_name);
10866
10867 make_cleanup (xfree, debug_dirname);
10868 dwp_name = xstrprintf ("%s%s%s.dwp", debug_dirname,
10869 SLASH_STRING, backlink_basename);
10870 }
10871 else
10872 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10873 make_cleanup (xfree, dwp_name);
10874
10875 dbfd = open_dwp_file (dwp_name);
10876 if (dbfd == NULL
10877 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10878 {
10879 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10880 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10881 make_cleanup (xfree, dwp_name);
10882 dbfd = open_dwp_file (dwp_name);
10883 }
10884
10885 if (dbfd == NULL)
10886 {
10887 if (dwarf_read_debug)
10888 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10889 do_cleanups (cleanups);
10890 return NULL;
10891 }
10892 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10893 dwp_file->name = bfd_get_filename (dbfd);
10894 dwp_file->dbfd = dbfd;
10895 do_cleanups (cleanups);
10896
10897 /* +1: section 0 is unused */
10898 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
10899 dwp_file->elf_sections =
10900 OBSTACK_CALLOC (&objfile->objfile_obstack,
10901 dwp_file->num_sections, asection *);
10902
10903 bfd_map_over_sections (dbfd, dwarf2_locate_common_dwp_sections, dwp_file);
10904
10905 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10906
10907 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10908
10909 /* The DWP file version is stored in the hash table. Oh well. */
10910 if (dwp_file->cus->version != dwp_file->tus->version)
10911 {
10912 /* Technically speaking, we should try to limp along, but this is
10913 pretty bizarre. We use pulongest here because that's the established
10914 portability solution (e.g, we cannot use %u for uint32_t). */
10915 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10916 " TU version %s [in DWP file %s]"),
10917 pulongest (dwp_file->cus->version),
10918 pulongest (dwp_file->tus->version), dwp_name);
10919 }
10920 dwp_file->version = dwp_file->cus->version;
10921
10922 if (dwp_file->version == 2)
10923 bfd_map_over_sections (dbfd, dwarf2_locate_v2_dwp_sections, dwp_file);
10924
10925 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10926 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
10927
10928 if (dwarf_read_debug)
10929 {
10930 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10931 fprintf_unfiltered (gdb_stdlog,
10932 " %s CUs, %s TUs\n",
10933 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10934 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
10935 }
10936
10937 return dwp_file;
10938 }
10939
10940 /* Wrapper around open_and_init_dwp_file, only open it once. */
10941
10942 static struct dwp_file *
10943 get_dwp_file (void)
10944 {
10945 if (! dwarf2_per_objfile->dwp_checked)
10946 {
10947 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10948 dwarf2_per_objfile->dwp_checked = 1;
10949 }
10950 return dwarf2_per_objfile->dwp_file;
10951 }
10952
10953 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10954 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10955 or in the DWP file for the objfile, referenced by THIS_UNIT.
10956 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
10957 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10958
10959 This is called, for example, when wanting to read a variable with a
10960 complex location. Therefore we don't want to do file i/o for every call.
10961 Therefore we don't want to look for a DWO file on every call.
10962 Therefore we first see if we've already seen SIGNATURE in a DWP file,
10963 then we check if we've already seen DWO_NAME, and only THEN do we check
10964 for a DWO file.
10965
10966 The result is a pointer to the dwo_unit object or NULL if we didn't find it
10967 (dwo_id mismatch or couldn't find the DWO/DWP file). */
10968
10969 static struct dwo_unit *
10970 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
10971 const char *dwo_name, const char *comp_dir,
10972 ULONGEST signature, int is_debug_types)
10973 {
10974 struct objfile *objfile = dwarf2_per_objfile->objfile;
10975 const char *kind = is_debug_types ? "TU" : "CU";
10976 void **dwo_file_slot;
10977 struct dwo_file *dwo_file;
10978 struct dwp_file *dwp_file;
10979
10980 /* First see if there's a DWP file.
10981 If we have a DWP file but didn't find the DWO inside it, don't
10982 look for the original DWO file. It makes gdb behave differently
10983 depending on whether one is debugging in the build tree. */
10984
10985 dwp_file = get_dwp_file ();
10986 if (dwp_file != NULL)
10987 {
10988 const struct dwp_hash_table *dwp_htab =
10989 is_debug_types ? dwp_file->tus : dwp_file->cus;
10990
10991 if (dwp_htab != NULL)
10992 {
10993 struct dwo_unit *dwo_cutu =
10994 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
10995 signature, is_debug_types);
10996
10997 if (dwo_cutu != NULL)
10998 {
10999 if (dwarf_read_debug)
11000 {
11001 fprintf_unfiltered (gdb_stdlog,
11002 "Virtual DWO %s %s found: @%s\n",
11003 kind, hex_string (signature),
11004 host_address_to_string (dwo_cutu));
11005 }
11006 return dwo_cutu;
11007 }
11008 }
11009 }
11010 else
11011 {
11012 /* No DWP file, look for the DWO file. */
11013
11014 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11015 if (*dwo_file_slot == NULL)
11016 {
11017 /* Read in the file and build a table of the CUs/TUs it contains. */
11018 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11019 }
11020 /* NOTE: This will be NULL if unable to open the file. */
11021 dwo_file = (struct dwo_file *) *dwo_file_slot;
11022
11023 if (dwo_file != NULL)
11024 {
11025 struct dwo_unit *dwo_cutu = NULL;
11026
11027 if (is_debug_types && dwo_file->tus)
11028 {
11029 struct dwo_unit find_dwo_cutu;
11030
11031 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11032 find_dwo_cutu.signature = signature;
11033 dwo_cutu
11034 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11035 }
11036 else if (!is_debug_types && dwo_file->cu)
11037 {
11038 if (signature == dwo_file->cu->signature)
11039 dwo_cutu = dwo_file->cu;
11040 }
11041
11042 if (dwo_cutu != NULL)
11043 {
11044 if (dwarf_read_debug)
11045 {
11046 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11047 kind, dwo_name, hex_string (signature),
11048 host_address_to_string (dwo_cutu));
11049 }
11050 return dwo_cutu;
11051 }
11052 }
11053 }
11054
11055 /* We didn't find it. This could mean a dwo_id mismatch, or
11056 someone deleted the DWO/DWP file, or the search path isn't set up
11057 correctly to find the file. */
11058
11059 if (dwarf_read_debug)
11060 {
11061 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11062 kind, dwo_name, hex_string (signature));
11063 }
11064
11065 /* This is a warning and not a complaint because it can be caused by
11066 pilot error (e.g., user accidentally deleting the DWO). */
11067 {
11068 /* Print the name of the DWP file if we looked there, helps the user
11069 better diagnose the problem. */
11070 char *dwp_text = NULL;
11071 struct cleanup *cleanups;
11072
11073 if (dwp_file != NULL)
11074 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11075 cleanups = make_cleanup (xfree, dwp_text);
11076
11077 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11078 " [in module %s]"),
11079 kind, dwo_name, hex_string (signature),
11080 dwp_text != NULL ? dwp_text : "",
11081 this_unit->is_debug_types ? "TU" : "CU",
11082 this_unit->offset.sect_off, objfile_name (objfile));
11083
11084 do_cleanups (cleanups);
11085 }
11086 return NULL;
11087 }
11088
11089 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11090 See lookup_dwo_cutu_unit for details. */
11091
11092 static struct dwo_unit *
11093 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11094 const char *dwo_name, const char *comp_dir,
11095 ULONGEST signature)
11096 {
11097 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11098 }
11099
11100 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11101 See lookup_dwo_cutu_unit for details. */
11102
11103 static struct dwo_unit *
11104 lookup_dwo_type_unit (struct signatured_type *this_tu,
11105 const char *dwo_name, const char *comp_dir)
11106 {
11107 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11108 }
11109
11110 /* Traversal function for queue_and_load_all_dwo_tus. */
11111
11112 static int
11113 queue_and_load_dwo_tu (void **slot, void *info)
11114 {
11115 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11116 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11117 ULONGEST signature = dwo_unit->signature;
11118 struct signatured_type *sig_type =
11119 lookup_dwo_signatured_type (per_cu->cu, signature);
11120
11121 if (sig_type != NULL)
11122 {
11123 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11124
11125 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11126 a real dependency of PER_CU on SIG_TYPE. That is detected later
11127 while processing PER_CU. */
11128 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11129 load_full_type_unit (sig_cu);
11130 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11131 }
11132
11133 return 1;
11134 }
11135
11136 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11137 The DWO may have the only definition of the type, though it may not be
11138 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11139 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11140
11141 static void
11142 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11143 {
11144 struct dwo_unit *dwo_unit;
11145 struct dwo_file *dwo_file;
11146
11147 gdb_assert (!per_cu->is_debug_types);
11148 gdb_assert (get_dwp_file () == NULL);
11149 gdb_assert (per_cu->cu != NULL);
11150
11151 dwo_unit = per_cu->cu->dwo_unit;
11152 gdb_assert (dwo_unit != NULL);
11153
11154 dwo_file = dwo_unit->dwo_file;
11155 if (dwo_file->tus != NULL)
11156 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11157 }
11158
11159 /* Free all resources associated with DWO_FILE.
11160 Close the DWO file and munmap the sections.
11161 All memory should be on the objfile obstack. */
11162
11163 static void
11164 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11165 {
11166
11167 /* Note: dbfd is NULL for virtual DWO files. */
11168 gdb_bfd_unref (dwo_file->dbfd);
11169
11170 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11171 }
11172
11173 /* Wrapper for free_dwo_file for use in cleanups. */
11174
11175 static void
11176 free_dwo_file_cleanup (void *arg)
11177 {
11178 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11179 struct objfile *objfile = dwarf2_per_objfile->objfile;
11180
11181 free_dwo_file (dwo_file, objfile);
11182 }
11183
11184 /* Traversal function for free_dwo_files. */
11185
11186 static int
11187 free_dwo_file_from_slot (void **slot, void *info)
11188 {
11189 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11190 struct objfile *objfile = (struct objfile *) info;
11191
11192 free_dwo_file (dwo_file, objfile);
11193
11194 return 1;
11195 }
11196
11197 /* Free all resources associated with DWO_FILES. */
11198
11199 static void
11200 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11201 {
11202 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11203 }
11204 \f
11205 /* Read in various DIEs. */
11206
11207 /* qsort helper for inherit_abstract_dies. */
11208
11209 static int
11210 unsigned_int_compar (const void *ap, const void *bp)
11211 {
11212 unsigned int a = *(unsigned int *) ap;
11213 unsigned int b = *(unsigned int *) bp;
11214
11215 return (a > b) - (b > a);
11216 }
11217
11218 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11219 Inherit only the children of the DW_AT_abstract_origin DIE not being
11220 already referenced by DW_AT_abstract_origin from the children of the
11221 current DIE. */
11222
11223 static void
11224 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11225 {
11226 struct die_info *child_die;
11227 unsigned die_children_count;
11228 /* CU offsets which were referenced by children of the current DIE. */
11229 sect_offset *offsets;
11230 sect_offset *offsets_end, *offsetp;
11231 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11232 struct die_info *origin_die;
11233 /* Iterator of the ORIGIN_DIE children. */
11234 struct die_info *origin_child_die;
11235 struct cleanup *cleanups;
11236 struct attribute *attr;
11237 struct dwarf2_cu *origin_cu;
11238 struct pending **origin_previous_list_in_scope;
11239
11240 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11241 if (!attr)
11242 return;
11243
11244 /* Note that following die references may follow to a die in a
11245 different cu. */
11246
11247 origin_cu = cu;
11248 origin_die = follow_die_ref (die, attr, &origin_cu);
11249
11250 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11251 symbols in. */
11252 origin_previous_list_in_scope = origin_cu->list_in_scope;
11253 origin_cu->list_in_scope = cu->list_in_scope;
11254
11255 if (die->tag != origin_die->tag
11256 && !(die->tag == DW_TAG_inlined_subroutine
11257 && origin_die->tag == DW_TAG_subprogram))
11258 complaint (&symfile_complaints,
11259 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11260 die->offset.sect_off, origin_die->offset.sect_off);
11261
11262 child_die = die->child;
11263 die_children_count = 0;
11264 while (child_die && child_die->tag)
11265 {
11266 child_die = sibling_die (child_die);
11267 die_children_count++;
11268 }
11269 offsets = XNEWVEC (sect_offset, die_children_count);
11270 cleanups = make_cleanup (xfree, offsets);
11271
11272 offsets_end = offsets;
11273 for (child_die = die->child;
11274 child_die && child_die->tag;
11275 child_die = sibling_die (child_die))
11276 {
11277 struct die_info *child_origin_die;
11278 struct dwarf2_cu *child_origin_cu;
11279
11280 /* We are trying to process concrete instance entries:
11281 DW_TAG_GNU_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11282 it's not relevant to our analysis here. i.e. detecting DIEs that are
11283 present in the abstract instance but not referenced in the concrete
11284 one. */
11285 if (child_die->tag == DW_TAG_GNU_call_site)
11286 continue;
11287
11288 /* For each CHILD_DIE, find the corresponding child of
11289 ORIGIN_DIE. If there is more than one layer of
11290 DW_AT_abstract_origin, follow them all; there shouldn't be,
11291 but GCC versions at least through 4.4 generate this (GCC PR
11292 40573). */
11293 child_origin_die = child_die;
11294 child_origin_cu = cu;
11295 while (1)
11296 {
11297 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11298 child_origin_cu);
11299 if (attr == NULL)
11300 break;
11301 child_origin_die = follow_die_ref (child_origin_die, attr,
11302 &child_origin_cu);
11303 }
11304
11305 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11306 counterpart may exist. */
11307 if (child_origin_die != child_die)
11308 {
11309 if (child_die->tag != child_origin_die->tag
11310 && !(child_die->tag == DW_TAG_inlined_subroutine
11311 && child_origin_die->tag == DW_TAG_subprogram))
11312 complaint (&symfile_complaints,
11313 _("Child DIE 0x%x and its abstract origin 0x%x have "
11314 "different tags"), child_die->offset.sect_off,
11315 child_origin_die->offset.sect_off);
11316 if (child_origin_die->parent != origin_die)
11317 complaint (&symfile_complaints,
11318 _("Child DIE 0x%x and its abstract origin 0x%x have "
11319 "different parents"), child_die->offset.sect_off,
11320 child_origin_die->offset.sect_off);
11321 else
11322 *offsets_end++ = child_origin_die->offset;
11323 }
11324 }
11325 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11326 unsigned_int_compar);
11327 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11328 if (offsetp[-1].sect_off == offsetp->sect_off)
11329 complaint (&symfile_complaints,
11330 _("Multiple children of DIE 0x%x refer "
11331 "to DIE 0x%x as their abstract origin"),
11332 die->offset.sect_off, offsetp->sect_off);
11333
11334 offsetp = offsets;
11335 origin_child_die = origin_die->child;
11336 while (origin_child_die && origin_child_die->tag)
11337 {
11338 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11339 while (offsetp < offsets_end
11340 && offsetp->sect_off < origin_child_die->offset.sect_off)
11341 offsetp++;
11342 if (offsetp >= offsets_end
11343 || offsetp->sect_off > origin_child_die->offset.sect_off)
11344 {
11345 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11346 Check whether we're already processing ORIGIN_CHILD_DIE.
11347 This can happen with mutually referenced abstract_origins.
11348 PR 16581. */
11349 if (!origin_child_die->in_process)
11350 process_die (origin_child_die, origin_cu);
11351 }
11352 origin_child_die = sibling_die (origin_child_die);
11353 }
11354 origin_cu->list_in_scope = origin_previous_list_in_scope;
11355
11356 do_cleanups (cleanups);
11357 }
11358
11359 static void
11360 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11361 {
11362 struct objfile *objfile = cu->objfile;
11363 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11364 struct context_stack *newobj;
11365 CORE_ADDR lowpc;
11366 CORE_ADDR highpc;
11367 struct die_info *child_die;
11368 struct attribute *attr, *call_line, *call_file;
11369 const char *name;
11370 CORE_ADDR baseaddr;
11371 struct block *block;
11372 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11373 VEC (symbolp) *template_args = NULL;
11374 struct template_symbol *templ_func = NULL;
11375
11376 if (inlined_func)
11377 {
11378 /* If we do not have call site information, we can't show the
11379 caller of this inlined function. That's too confusing, so
11380 only use the scope for local variables. */
11381 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11382 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11383 if (call_line == NULL || call_file == NULL)
11384 {
11385 read_lexical_block_scope (die, cu);
11386 return;
11387 }
11388 }
11389
11390 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11391
11392 name = dwarf2_name (die, cu);
11393
11394 /* Ignore functions with missing or empty names. These are actually
11395 illegal according to the DWARF standard. */
11396 if (name == NULL)
11397 {
11398 complaint (&symfile_complaints,
11399 _("missing name for subprogram DIE at %d"),
11400 die->offset.sect_off);
11401 return;
11402 }
11403
11404 /* Ignore functions with missing or invalid low and high pc attributes. */
11405 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11406 <= PC_BOUNDS_INVALID)
11407 {
11408 attr = dwarf2_attr (die, DW_AT_external, cu);
11409 if (!attr || !DW_UNSND (attr))
11410 complaint (&symfile_complaints,
11411 _("cannot get low and high bounds "
11412 "for subprogram DIE at %d"),
11413 die->offset.sect_off);
11414 return;
11415 }
11416
11417 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11418 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11419
11420 /* If we have any template arguments, then we must allocate a
11421 different sort of symbol. */
11422 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11423 {
11424 if (child_die->tag == DW_TAG_template_type_param
11425 || child_die->tag == DW_TAG_template_value_param)
11426 {
11427 templ_func = allocate_template_symbol (objfile);
11428 templ_func->base.is_cplus_template_function = 1;
11429 break;
11430 }
11431 }
11432
11433 newobj = push_context (0, lowpc);
11434 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11435 (struct symbol *) templ_func);
11436
11437 /* If there is a location expression for DW_AT_frame_base, record
11438 it. */
11439 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11440 if (attr)
11441 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11442
11443 /* If there is a location for the static link, record it. */
11444 newobj->static_link = NULL;
11445 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11446 if (attr)
11447 {
11448 newobj->static_link
11449 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11450 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11451 }
11452
11453 cu->list_in_scope = &local_symbols;
11454
11455 if (die->child != NULL)
11456 {
11457 child_die = die->child;
11458 while (child_die && child_die->tag)
11459 {
11460 if (child_die->tag == DW_TAG_template_type_param
11461 || child_die->tag == DW_TAG_template_value_param)
11462 {
11463 struct symbol *arg = new_symbol (child_die, NULL, cu);
11464
11465 if (arg != NULL)
11466 VEC_safe_push (symbolp, template_args, arg);
11467 }
11468 else
11469 process_die (child_die, cu);
11470 child_die = sibling_die (child_die);
11471 }
11472 }
11473
11474 inherit_abstract_dies (die, cu);
11475
11476 /* If we have a DW_AT_specification, we might need to import using
11477 directives from the context of the specification DIE. See the
11478 comment in determine_prefix. */
11479 if (cu->language == language_cplus
11480 && dwarf2_attr (die, DW_AT_specification, cu))
11481 {
11482 struct dwarf2_cu *spec_cu = cu;
11483 struct die_info *spec_die = die_specification (die, &spec_cu);
11484
11485 while (spec_die)
11486 {
11487 child_die = spec_die->child;
11488 while (child_die && child_die->tag)
11489 {
11490 if (child_die->tag == DW_TAG_imported_module)
11491 process_die (child_die, spec_cu);
11492 child_die = sibling_die (child_die);
11493 }
11494
11495 /* In some cases, GCC generates specification DIEs that
11496 themselves contain DW_AT_specification attributes. */
11497 spec_die = die_specification (spec_die, &spec_cu);
11498 }
11499 }
11500
11501 newobj = pop_context ();
11502 /* Make a block for the local symbols within. */
11503 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11504 newobj->static_link, lowpc, highpc);
11505
11506 /* For C++, set the block's scope. */
11507 if ((cu->language == language_cplus
11508 || cu->language == language_fortran
11509 || cu->language == language_d
11510 || cu->language == language_rust)
11511 && cu->processing_has_namespace_info)
11512 block_set_scope (block, determine_prefix (die, cu),
11513 &objfile->objfile_obstack);
11514
11515 /* If we have address ranges, record them. */
11516 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11517
11518 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11519
11520 /* Attach template arguments to function. */
11521 if (! VEC_empty (symbolp, template_args))
11522 {
11523 gdb_assert (templ_func != NULL);
11524
11525 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11526 templ_func->template_arguments
11527 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11528 templ_func->n_template_arguments);
11529 memcpy (templ_func->template_arguments,
11530 VEC_address (symbolp, template_args),
11531 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11532 VEC_free (symbolp, template_args);
11533 }
11534
11535 /* In C++, we can have functions nested inside functions (e.g., when
11536 a function declares a class that has methods). This means that
11537 when we finish processing a function scope, we may need to go
11538 back to building a containing block's symbol lists. */
11539 local_symbols = newobj->locals;
11540 local_using_directives = newobj->local_using_directives;
11541
11542 /* If we've finished processing a top-level function, subsequent
11543 symbols go in the file symbol list. */
11544 if (outermost_context_p ())
11545 cu->list_in_scope = &file_symbols;
11546 }
11547
11548 /* Process all the DIES contained within a lexical block scope. Start
11549 a new scope, process the dies, and then close the scope. */
11550
11551 static void
11552 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11553 {
11554 struct objfile *objfile = cu->objfile;
11555 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11556 struct context_stack *newobj;
11557 CORE_ADDR lowpc, highpc;
11558 struct die_info *child_die;
11559 CORE_ADDR baseaddr;
11560
11561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11562
11563 /* Ignore blocks with missing or invalid low and high pc attributes. */
11564 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11565 as multiple lexical blocks? Handling children in a sane way would
11566 be nasty. Might be easier to properly extend generic blocks to
11567 describe ranges. */
11568 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11569 {
11570 case PC_BOUNDS_NOT_PRESENT:
11571 /* DW_TAG_lexical_block has no attributes, process its children as if
11572 there was no wrapping by that DW_TAG_lexical_block.
11573 GCC does no longer produces such DWARF since GCC r224161. */
11574 for (child_die = die->child;
11575 child_die != NULL && child_die->tag;
11576 child_die = sibling_die (child_die))
11577 process_die (child_die, cu);
11578 return;
11579 case PC_BOUNDS_INVALID:
11580 return;
11581 }
11582 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11583 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11584
11585 push_context (0, lowpc);
11586 if (die->child != NULL)
11587 {
11588 child_die = die->child;
11589 while (child_die && child_die->tag)
11590 {
11591 process_die (child_die, cu);
11592 child_die = sibling_die (child_die);
11593 }
11594 }
11595 inherit_abstract_dies (die, cu);
11596 newobj = pop_context ();
11597
11598 if (local_symbols != NULL || local_using_directives != NULL)
11599 {
11600 struct block *block
11601 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11602 newobj->start_addr, highpc);
11603
11604 /* Note that recording ranges after traversing children, as we
11605 do here, means that recording a parent's ranges entails
11606 walking across all its children's ranges as they appear in
11607 the address map, which is quadratic behavior.
11608
11609 It would be nicer to record the parent's ranges before
11610 traversing its children, simply overriding whatever you find
11611 there. But since we don't even decide whether to create a
11612 block until after we've traversed its children, that's hard
11613 to do. */
11614 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11615 }
11616 local_symbols = newobj->locals;
11617 local_using_directives = newobj->local_using_directives;
11618 }
11619
11620 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
11621
11622 static void
11623 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11624 {
11625 struct objfile *objfile = cu->objfile;
11626 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11627 CORE_ADDR pc, baseaddr;
11628 struct attribute *attr;
11629 struct call_site *call_site, call_site_local;
11630 void **slot;
11631 int nparams;
11632 struct die_info *child_die;
11633
11634 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11635
11636 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11637 if (!attr)
11638 {
11639 complaint (&symfile_complaints,
11640 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
11641 "DIE 0x%x [in module %s]"),
11642 die->offset.sect_off, objfile_name (objfile));
11643 return;
11644 }
11645 pc = attr_value_as_address (attr) + baseaddr;
11646 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11647
11648 if (cu->call_site_htab == NULL)
11649 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11650 NULL, &objfile->objfile_obstack,
11651 hashtab_obstack_allocate, NULL);
11652 call_site_local.pc = pc;
11653 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11654 if (*slot != NULL)
11655 {
11656 complaint (&symfile_complaints,
11657 _("Duplicate PC %s for DW_TAG_GNU_call_site "
11658 "DIE 0x%x [in module %s]"),
11659 paddress (gdbarch, pc), die->offset.sect_off,
11660 objfile_name (objfile));
11661 return;
11662 }
11663
11664 /* Count parameters at the caller. */
11665
11666 nparams = 0;
11667 for (child_die = die->child; child_die && child_die->tag;
11668 child_die = sibling_die (child_die))
11669 {
11670 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11671 {
11672 complaint (&symfile_complaints,
11673 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
11674 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11675 child_die->tag, child_die->offset.sect_off,
11676 objfile_name (objfile));
11677 continue;
11678 }
11679
11680 nparams++;
11681 }
11682
11683 call_site
11684 = ((struct call_site *)
11685 obstack_alloc (&objfile->objfile_obstack,
11686 sizeof (*call_site)
11687 + (sizeof (*call_site->parameter) * (nparams - 1))));
11688 *slot = call_site;
11689 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11690 call_site->pc = pc;
11691
11692 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11693 {
11694 struct die_info *func_die;
11695
11696 /* Skip also over DW_TAG_inlined_subroutine. */
11697 for (func_die = die->parent;
11698 func_die && func_die->tag != DW_TAG_subprogram
11699 && func_die->tag != DW_TAG_subroutine_type;
11700 func_die = func_die->parent);
11701
11702 /* DW_AT_GNU_all_call_sites is a superset
11703 of DW_AT_GNU_all_tail_call_sites. */
11704 if (func_die
11705 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11706 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11707 {
11708 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11709 not complete. But keep CALL_SITE for look ups via call_site_htab,
11710 both the initial caller containing the real return address PC and
11711 the final callee containing the current PC of a chain of tail
11712 calls do not need to have the tail call list complete. But any
11713 function candidate for a virtual tail call frame searched via
11714 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11715 determined unambiguously. */
11716 }
11717 else
11718 {
11719 struct type *func_type = NULL;
11720
11721 if (func_die)
11722 func_type = get_die_type (func_die, cu);
11723 if (func_type != NULL)
11724 {
11725 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11726
11727 /* Enlist this call site to the function. */
11728 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11729 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11730 }
11731 else
11732 complaint (&symfile_complaints,
11733 _("Cannot find function owning DW_TAG_GNU_call_site "
11734 "DIE 0x%x [in module %s]"),
11735 die->offset.sect_off, objfile_name (objfile));
11736 }
11737 }
11738
11739 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11740 if (attr == NULL)
11741 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11742 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11743 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11744 /* Keep NULL DWARF_BLOCK. */;
11745 else if (attr_form_is_block (attr))
11746 {
11747 struct dwarf2_locexpr_baton *dlbaton;
11748
11749 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11750 dlbaton->data = DW_BLOCK (attr)->data;
11751 dlbaton->size = DW_BLOCK (attr)->size;
11752 dlbaton->per_cu = cu->per_cu;
11753
11754 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11755 }
11756 else if (attr_form_is_ref (attr))
11757 {
11758 struct dwarf2_cu *target_cu = cu;
11759 struct die_info *target_die;
11760
11761 target_die = follow_die_ref (die, attr, &target_cu);
11762 gdb_assert (target_cu->objfile == objfile);
11763 if (die_is_declaration (target_die, target_cu))
11764 {
11765 const char *target_physname;
11766
11767 /* Prefer the mangled name; otherwise compute the demangled one. */
11768 target_physname = dwarf2_string_attr (target_die,
11769 DW_AT_linkage_name,
11770 target_cu);
11771 if (target_physname == NULL)
11772 target_physname = dwarf2_string_attr (target_die,
11773 DW_AT_MIPS_linkage_name,
11774 target_cu);
11775 if (target_physname == NULL)
11776 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11777 if (target_physname == NULL)
11778 complaint (&symfile_complaints,
11779 _("DW_AT_GNU_call_site_target target DIE has invalid "
11780 "physname, for referencing DIE 0x%x [in module %s]"),
11781 die->offset.sect_off, objfile_name (objfile));
11782 else
11783 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11784 }
11785 else
11786 {
11787 CORE_ADDR lowpc;
11788
11789 /* DW_AT_entry_pc should be preferred. */
11790 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11791 <= PC_BOUNDS_INVALID)
11792 complaint (&symfile_complaints,
11793 _("DW_AT_GNU_call_site_target target DIE has invalid "
11794 "low pc, for referencing DIE 0x%x [in module %s]"),
11795 die->offset.sect_off, objfile_name (objfile));
11796 else
11797 {
11798 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11799 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11800 }
11801 }
11802 }
11803 else
11804 complaint (&symfile_complaints,
11805 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
11806 "block nor reference, for DIE 0x%x [in module %s]"),
11807 die->offset.sect_off, objfile_name (objfile));
11808
11809 call_site->per_cu = cu->per_cu;
11810
11811 for (child_die = die->child;
11812 child_die && child_die->tag;
11813 child_die = sibling_die (child_die))
11814 {
11815 struct call_site_parameter *parameter;
11816 struct attribute *loc, *origin;
11817
11818 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11819 {
11820 /* Already printed the complaint above. */
11821 continue;
11822 }
11823
11824 gdb_assert (call_site->parameter_count < nparams);
11825 parameter = &call_site->parameter[call_site->parameter_count];
11826
11827 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11828 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11829 register is contained in DW_AT_GNU_call_site_value. */
11830
11831 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11832 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11833 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11834 {
11835 sect_offset offset;
11836
11837 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11838 offset = dwarf2_get_ref_die_offset (origin);
11839 if (!offset_in_cu_p (&cu->header, offset))
11840 {
11841 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11842 binding can be done only inside one CU. Such referenced DIE
11843 therefore cannot be even moved to DW_TAG_partial_unit. */
11844 complaint (&symfile_complaints,
11845 _("DW_AT_abstract_origin offset is not in CU for "
11846 "DW_TAG_GNU_call_site child DIE 0x%x "
11847 "[in module %s]"),
11848 child_die->offset.sect_off, objfile_name (objfile));
11849 continue;
11850 }
11851 parameter->u.param_offset.cu_off = (offset.sect_off
11852 - cu->header.offset.sect_off);
11853 }
11854 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11855 {
11856 complaint (&symfile_complaints,
11857 _("No DW_FORM_block* DW_AT_location for "
11858 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11859 child_die->offset.sect_off, objfile_name (objfile));
11860 continue;
11861 }
11862 else
11863 {
11864 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11865 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11866 if (parameter->u.dwarf_reg != -1)
11867 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11868 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11869 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11870 &parameter->u.fb_offset))
11871 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11872 else
11873 {
11874 complaint (&symfile_complaints,
11875 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11876 "for DW_FORM_block* DW_AT_location is supported for "
11877 "DW_TAG_GNU_call_site child DIE 0x%x "
11878 "[in module %s]"),
11879 child_die->offset.sect_off, objfile_name (objfile));
11880 continue;
11881 }
11882 }
11883
11884 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11885 if (!attr_form_is_block (attr))
11886 {
11887 complaint (&symfile_complaints,
11888 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
11889 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11890 child_die->offset.sect_off, objfile_name (objfile));
11891 continue;
11892 }
11893 parameter->value = DW_BLOCK (attr)->data;
11894 parameter->value_size = DW_BLOCK (attr)->size;
11895
11896 /* Parameters are not pre-cleared by memset above. */
11897 parameter->data_value = NULL;
11898 parameter->data_value_size = 0;
11899 call_site->parameter_count++;
11900
11901 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11902 if (attr)
11903 {
11904 if (!attr_form_is_block (attr))
11905 complaint (&symfile_complaints,
11906 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
11907 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11908 child_die->offset.sect_off, objfile_name (objfile));
11909 else
11910 {
11911 parameter->data_value = DW_BLOCK (attr)->data;
11912 parameter->data_value_size = DW_BLOCK (attr)->size;
11913 }
11914 }
11915 }
11916 }
11917
11918 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
11919 Return 1 if the attributes are present and valid, otherwise, return 0.
11920 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
11921
11922 static int
11923 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
11924 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11925 struct partial_symtab *ranges_pst)
11926 {
11927 struct objfile *objfile = cu->objfile;
11928 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11929 struct comp_unit_head *cu_header = &cu->header;
11930 bfd *obfd = objfile->obfd;
11931 unsigned int addr_size = cu_header->addr_size;
11932 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11933 /* Base address selection entry. */
11934 CORE_ADDR base;
11935 int found_base;
11936 unsigned int dummy;
11937 const gdb_byte *buffer;
11938 int low_set;
11939 CORE_ADDR low = 0;
11940 CORE_ADDR high = 0;
11941 CORE_ADDR baseaddr;
11942
11943 found_base = cu->base_known;
11944 base = cu->base_address;
11945
11946 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
11947 if (offset >= dwarf2_per_objfile->ranges.size)
11948 {
11949 complaint (&symfile_complaints,
11950 _("Offset %d out of bounds for DW_AT_ranges attribute"),
11951 offset);
11952 return 0;
11953 }
11954 buffer = dwarf2_per_objfile->ranges.buffer + offset;
11955
11956 low_set = 0;
11957
11958 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11959
11960 while (1)
11961 {
11962 CORE_ADDR range_beginning, range_end;
11963
11964 range_beginning = read_address (obfd, buffer, cu, &dummy);
11965 buffer += addr_size;
11966 range_end = read_address (obfd, buffer, cu, &dummy);
11967 buffer += addr_size;
11968 offset += 2 * addr_size;
11969
11970 /* An end of list marker is a pair of zero addresses. */
11971 if (range_beginning == 0 && range_end == 0)
11972 /* Found the end of list entry. */
11973 break;
11974
11975 /* Each base address selection entry is a pair of 2 values.
11976 The first is the largest possible address, the second is
11977 the base address. Check for a base address here. */
11978 if ((range_beginning & mask) == mask)
11979 {
11980 /* If we found the largest possible address, then we already
11981 have the base address in range_end. */
11982 base = range_end;
11983 found_base = 1;
11984 continue;
11985 }
11986
11987 if (!found_base)
11988 {
11989 /* We have no valid base address for the ranges
11990 data. */
11991 complaint (&symfile_complaints,
11992 _("Invalid .debug_ranges data (no base address)"));
11993 return 0;
11994 }
11995
11996 if (range_beginning > range_end)
11997 {
11998 /* Inverted range entries are invalid. */
11999 complaint (&symfile_complaints,
12000 _("Invalid .debug_ranges data (inverted range)"));
12001 return 0;
12002 }
12003
12004 /* Empty range entries have no effect. */
12005 if (range_beginning == range_end)
12006 continue;
12007
12008 range_beginning += base;
12009 range_end += base;
12010
12011 /* A not-uncommon case of bad debug info.
12012 Don't pollute the addrmap with bad data. */
12013 if (range_beginning + baseaddr == 0
12014 && !dwarf2_per_objfile->has_section_at_zero)
12015 {
12016 complaint (&symfile_complaints,
12017 _(".debug_ranges entry has start address of zero"
12018 " [in module %s]"), objfile_name (objfile));
12019 continue;
12020 }
12021
12022 if (ranges_pst != NULL)
12023 {
12024 CORE_ADDR lowpc;
12025 CORE_ADDR highpc;
12026
12027 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12028 range_beginning + baseaddr);
12029 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12030 range_end + baseaddr);
12031 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12032 ranges_pst);
12033 }
12034
12035 /* FIXME: This is recording everything as a low-high
12036 segment of consecutive addresses. We should have a
12037 data structure for discontiguous block ranges
12038 instead. */
12039 if (! low_set)
12040 {
12041 low = range_beginning;
12042 high = range_end;
12043 low_set = 1;
12044 }
12045 else
12046 {
12047 if (range_beginning < low)
12048 low = range_beginning;
12049 if (range_end > high)
12050 high = range_end;
12051 }
12052 }
12053
12054 if (! low_set)
12055 /* If the first entry is an end-of-list marker, the range
12056 describes an empty scope, i.e. no instructions. */
12057 return 0;
12058
12059 if (low_return)
12060 *low_return = low;
12061 if (high_return)
12062 *high_return = high;
12063 return 1;
12064 }
12065
12066 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12067 definition for the return value. *LOWPC and *HIGHPC are set iff
12068 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12069
12070 static enum pc_bounds_kind
12071 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12072 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12073 struct partial_symtab *pst)
12074 {
12075 struct attribute *attr;
12076 struct attribute *attr_high;
12077 CORE_ADDR low = 0;
12078 CORE_ADDR high = 0;
12079 enum pc_bounds_kind ret;
12080
12081 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12082 if (attr_high)
12083 {
12084 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12085 if (attr)
12086 {
12087 low = attr_value_as_address (attr);
12088 high = attr_value_as_address (attr_high);
12089 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12090 high += low;
12091 }
12092 else
12093 /* Found high w/o low attribute. */
12094 return PC_BOUNDS_INVALID;
12095
12096 /* Found consecutive range of addresses. */
12097 ret = PC_BOUNDS_HIGH_LOW;
12098 }
12099 else
12100 {
12101 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12102 if (attr != NULL)
12103 {
12104 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12105 We take advantage of the fact that DW_AT_ranges does not appear
12106 in DW_TAG_compile_unit of DWO files. */
12107 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12108 unsigned int ranges_offset = (DW_UNSND (attr)
12109 + (need_ranges_base
12110 ? cu->ranges_base
12111 : 0));
12112
12113 /* Value of the DW_AT_ranges attribute is the offset in the
12114 .debug_ranges section. */
12115 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12116 return PC_BOUNDS_INVALID;
12117 /* Found discontinuous range of addresses. */
12118 ret = PC_BOUNDS_RANGES;
12119 }
12120 else
12121 return PC_BOUNDS_NOT_PRESENT;
12122 }
12123
12124 /* read_partial_die has also the strict LOW < HIGH requirement. */
12125 if (high <= low)
12126 return PC_BOUNDS_INVALID;
12127
12128 /* When using the GNU linker, .gnu.linkonce. sections are used to
12129 eliminate duplicate copies of functions and vtables and such.
12130 The linker will arbitrarily choose one and discard the others.
12131 The AT_*_pc values for such functions refer to local labels in
12132 these sections. If the section from that file was discarded, the
12133 labels are not in the output, so the relocs get a value of 0.
12134 If this is a discarded function, mark the pc bounds as invalid,
12135 so that GDB will ignore it. */
12136 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12137 return PC_BOUNDS_INVALID;
12138
12139 *lowpc = low;
12140 if (highpc)
12141 *highpc = high;
12142 return ret;
12143 }
12144
12145 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12146 its low and high PC addresses. Do nothing if these addresses could not
12147 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12148 and HIGHPC to the high address if greater than HIGHPC. */
12149
12150 static void
12151 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12152 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12153 struct dwarf2_cu *cu)
12154 {
12155 CORE_ADDR low, high;
12156 struct die_info *child = die->child;
12157
12158 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12159 {
12160 *lowpc = std::min (*lowpc, low);
12161 *highpc = std::max (*highpc, high);
12162 }
12163
12164 /* If the language does not allow nested subprograms (either inside
12165 subprograms or lexical blocks), we're done. */
12166 if (cu->language != language_ada)
12167 return;
12168
12169 /* Check all the children of the given DIE. If it contains nested
12170 subprograms, then check their pc bounds. Likewise, we need to
12171 check lexical blocks as well, as they may also contain subprogram
12172 definitions. */
12173 while (child && child->tag)
12174 {
12175 if (child->tag == DW_TAG_subprogram
12176 || child->tag == DW_TAG_lexical_block)
12177 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12178 child = sibling_die (child);
12179 }
12180 }
12181
12182 /* Get the low and high pc's represented by the scope DIE, and store
12183 them in *LOWPC and *HIGHPC. If the correct values can't be
12184 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12185
12186 static void
12187 get_scope_pc_bounds (struct die_info *die,
12188 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12189 struct dwarf2_cu *cu)
12190 {
12191 CORE_ADDR best_low = (CORE_ADDR) -1;
12192 CORE_ADDR best_high = (CORE_ADDR) 0;
12193 CORE_ADDR current_low, current_high;
12194
12195 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
12196 >= PC_BOUNDS_RANGES)
12197 {
12198 best_low = current_low;
12199 best_high = current_high;
12200 }
12201 else
12202 {
12203 struct die_info *child = die->child;
12204
12205 while (child && child->tag)
12206 {
12207 switch (child->tag) {
12208 case DW_TAG_subprogram:
12209 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12210 break;
12211 case DW_TAG_namespace:
12212 case DW_TAG_module:
12213 /* FIXME: carlton/2004-01-16: Should we do this for
12214 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12215 that current GCC's always emit the DIEs corresponding
12216 to definitions of methods of classes as children of a
12217 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12218 the DIEs giving the declarations, which could be
12219 anywhere). But I don't see any reason why the
12220 standards says that they have to be there. */
12221 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12222
12223 if (current_low != ((CORE_ADDR) -1))
12224 {
12225 best_low = std::min (best_low, current_low);
12226 best_high = std::max (best_high, current_high);
12227 }
12228 break;
12229 default:
12230 /* Ignore. */
12231 break;
12232 }
12233
12234 child = sibling_die (child);
12235 }
12236 }
12237
12238 *lowpc = best_low;
12239 *highpc = best_high;
12240 }
12241
12242 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12243 in DIE. */
12244
12245 static void
12246 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12247 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12248 {
12249 struct objfile *objfile = cu->objfile;
12250 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12251 struct attribute *attr;
12252 struct attribute *attr_high;
12253
12254 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12255 if (attr_high)
12256 {
12257 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12258 if (attr)
12259 {
12260 CORE_ADDR low = attr_value_as_address (attr);
12261 CORE_ADDR high = attr_value_as_address (attr_high);
12262
12263 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12264 high += low;
12265
12266 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12267 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12268 record_block_range (block, low, high - 1);
12269 }
12270 }
12271
12272 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12273 if (attr)
12274 {
12275 bfd *obfd = objfile->obfd;
12276 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12277 We take advantage of the fact that DW_AT_ranges does not appear
12278 in DW_TAG_compile_unit of DWO files. */
12279 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12280
12281 /* The value of the DW_AT_ranges attribute is the offset of the
12282 address range list in the .debug_ranges section. */
12283 unsigned long offset = (DW_UNSND (attr)
12284 + (need_ranges_base ? cu->ranges_base : 0));
12285 const gdb_byte *buffer;
12286
12287 /* For some target architectures, but not others, the
12288 read_address function sign-extends the addresses it returns.
12289 To recognize base address selection entries, we need a
12290 mask. */
12291 unsigned int addr_size = cu->header.addr_size;
12292 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12293
12294 /* The base address, to which the next pair is relative. Note
12295 that this 'base' is a DWARF concept: most entries in a range
12296 list are relative, to reduce the number of relocs against the
12297 debugging information. This is separate from this function's
12298 'baseaddr' argument, which GDB uses to relocate debugging
12299 information from a shared library based on the address at
12300 which the library was loaded. */
12301 CORE_ADDR base = cu->base_address;
12302 int base_known = cu->base_known;
12303
12304 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12305 if (offset >= dwarf2_per_objfile->ranges.size)
12306 {
12307 complaint (&symfile_complaints,
12308 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
12309 offset);
12310 return;
12311 }
12312 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12313
12314 for (;;)
12315 {
12316 unsigned int bytes_read;
12317 CORE_ADDR start, end;
12318
12319 start = read_address (obfd, buffer, cu, &bytes_read);
12320 buffer += bytes_read;
12321 end = read_address (obfd, buffer, cu, &bytes_read);
12322 buffer += bytes_read;
12323
12324 /* Did we find the end of the range list? */
12325 if (start == 0 && end == 0)
12326 break;
12327
12328 /* Did we find a base address selection entry? */
12329 else if ((start & base_select_mask) == base_select_mask)
12330 {
12331 base = end;
12332 base_known = 1;
12333 }
12334
12335 /* We found an ordinary address range. */
12336 else
12337 {
12338 if (!base_known)
12339 {
12340 complaint (&symfile_complaints,
12341 _("Invalid .debug_ranges data "
12342 "(no base address)"));
12343 return;
12344 }
12345
12346 if (start > end)
12347 {
12348 /* Inverted range entries are invalid. */
12349 complaint (&symfile_complaints,
12350 _("Invalid .debug_ranges data "
12351 "(inverted range)"));
12352 return;
12353 }
12354
12355 /* Empty range entries have no effect. */
12356 if (start == end)
12357 continue;
12358
12359 start += base + baseaddr;
12360 end += base + baseaddr;
12361
12362 /* A not-uncommon case of bad debug info.
12363 Don't pollute the addrmap with bad data. */
12364 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
12365 {
12366 complaint (&symfile_complaints,
12367 _(".debug_ranges entry has start address of zero"
12368 " [in module %s]"), objfile_name (objfile));
12369 continue;
12370 }
12371
12372 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12373 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12374 record_block_range (block, start, end - 1);
12375 }
12376 }
12377 }
12378 }
12379
12380 /* Check whether the producer field indicates either of GCC < 4.6, or the
12381 Intel C/C++ compiler, and cache the result in CU. */
12382
12383 static void
12384 check_producer (struct dwarf2_cu *cu)
12385 {
12386 int major, minor;
12387
12388 if (cu->producer == NULL)
12389 {
12390 /* For unknown compilers expect their behavior is DWARF version
12391 compliant.
12392
12393 GCC started to support .debug_types sections by -gdwarf-4 since
12394 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12395 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12396 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12397 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12398 }
12399 else if (producer_is_gcc (cu->producer, &major, &minor))
12400 {
12401 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12402 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12403 }
12404 else if (startswith (cu->producer, "Intel(R) C"))
12405 cu->producer_is_icc = 1;
12406 else
12407 {
12408 /* For other non-GCC compilers, expect their behavior is DWARF version
12409 compliant. */
12410 }
12411
12412 cu->checked_producer = 1;
12413 }
12414
12415 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12416 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12417 during 4.6.0 experimental. */
12418
12419 static int
12420 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12421 {
12422 if (!cu->checked_producer)
12423 check_producer (cu);
12424
12425 return cu->producer_is_gxx_lt_4_6;
12426 }
12427
12428 /* Return the default accessibility type if it is not overriden by
12429 DW_AT_accessibility. */
12430
12431 static enum dwarf_access_attribute
12432 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12433 {
12434 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12435 {
12436 /* The default DWARF 2 accessibility for members is public, the default
12437 accessibility for inheritance is private. */
12438
12439 if (die->tag != DW_TAG_inheritance)
12440 return DW_ACCESS_public;
12441 else
12442 return DW_ACCESS_private;
12443 }
12444 else
12445 {
12446 /* DWARF 3+ defines the default accessibility a different way. The same
12447 rules apply now for DW_TAG_inheritance as for the members and it only
12448 depends on the container kind. */
12449
12450 if (die->parent->tag == DW_TAG_class_type)
12451 return DW_ACCESS_private;
12452 else
12453 return DW_ACCESS_public;
12454 }
12455 }
12456
12457 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12458 offset. If the attribute was not found return 0, otherwise return
12459 1. If it was found but could not properly be handled, set *OFFSET
12460 to 0. */
12461
12462 static int
12463 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12464 LONGEST *offset)
12465 {
12466 struct attribute *attr;
12467
12468 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12469 if (attr != NULL)
12470 {
12471 *offset = 0;
12472
12473 /* Note that we do not check for a section offset first here.
12474 This is because DW_AT_data_member_location is new in DWARF 4,
12475 so if we see it, we can assume that a constant form is really
12476 a constant and not a section offset. */
12477 if (attr_form_is_constant (attr))
12478 *offset = dwarf2_get_attr_constant_value (attr, 0);
12479 else if (attr_form_is_section_offset (attr))
12480 dwarf2_complex_location_expr_complaint ();
12481 else if (attr_form_is_block (attr))
12482 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12483 else
12484 dwarf2_complex_location_expr_complaint ();
12485
12486 return 1;
12487 }
12488
12489 return 0;
12490 }
12491
12492 /* Add an aggregate field to the field list. */
12493
12494 static void
12495 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12496 struct dwarf2_cu *cu)
12497 {
12498 struct objfile *objfile = cu->objfile;
12499 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12500 struct nextfield *new_field;
12501 struct attribute *attr;
12502 struct field *fp;
12503 const char *fieldname = "";
12504
12505 /* Allocate a new field list entry and link it in. */
12506 new_field = XNEW (struct nextfield);
12507 make_cleanup (xfree, new_field);
12508 memset (new_field, 0, sizeof (struct nextfield));
12509
12510 if (die->tag == DW_TAG_inheritance)
12511 {
12512 new_field->next = fip->baseclasses;
12513 fip->baseclasses = new_field;
12514 }
12515 else
12516 {
12517 new_field->next = fip->fields;
12518 fip->fields = new_field;
12519 }
12520 fip->nfields++;
12521
12522 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12523 if (attr)
12524 new_field->accessibility = DW_UNSND (attr);
12525 else
12526 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12527 if (new_field->accessibility != DW_ACCESS_public)
12528 fip->non_public_fields = 1;
12529
12530 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12531 if (attr)
12532 new_field->virtuality = DW_UNSND (attr);
12533 else
12534 new_field->virtuality = DW_VIRTUALITY_none;
12535
12536 fp = &new_field->field;
12537
12538 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12539 {
12540 LONGEST offset;
12541
12542 /* Data member other than a C++ static data member. */
12543
12544 /* Get type of field. */
12545 fp->type = die_type (die, cu);
12546
12547 SET_FIELD_BITPOS (*fp, 0);
12548
12549 /* Get bit size of field (zero if none). */
12550 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12551 if (attr)
12552 {
12553 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12554 }
12555 else
12556 {
12557 FIELD_BITSIZE (*fp) = 0;
12558 }
12559
12560 /* Get bit offset of field. */
12561 if (handle_data_member_location (die, cu, &offset))
12562 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12563 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12564 if (attr)
12565 {
12566 if (gdbarch_bits_big_endian (gdbarch))
12567 {
12568 /* For big endian bits, the DW_AT_bit_offset gives the
12569 additional bit offset from the MSB of the containing
12570 anonymous object to the MSB of the field. We don't
12571 have to do anything special since we don't need to
12572 know the size of the anonymous object. */
12573 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12574 }
12575 else
12576 {
12577 /* For little endian bits, compute the bit offset to the
12578 MSB of the anonymous object, subtract off the number of
12579 bits from the MSB of the field to the MSB of the
12580 object, and then subtract off the number of bits of
12581 the field itself. The result is the bit offset of
12582 the LSB of the field. */
12583 int anonymous_size;
12584 int bit_offset = DW_UNSND (attr);
12585
12586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12587 if (attr)
12588 {
12589 /* The size of the anonymous object containing
12590 the bit field is explicit, so use the
12591 indicated size (in bytes). */
12592 anonymous_size = DW_UNSND (attr);
12593 }
12594 else
12595 {
12596 /* The size of the anonymous object containing
12597 the bit field must be inferred from the type
12598 attribute of the data member containing the
12599 bit field. */
12600 anonymous_size = TYPE_LENGTH (fp->type);
12601 }
12602 SET_FIELD_BITPOS (*fp,
12603 (FIELD_BITPOS (*fp)
12604 + anonymous_size * bits_per_byte
12605 - bit_offset - FIELD_BITSIZE (*fp)));
12606 }
12607 }
12608
12609 /* Get name of field. */
12610 fieldname = dwarf2_name (die, cu);
12611 if (fieldname == NULL)
12612 fieldname = "";
12613
12614 /* The name is already allocated along with this objfile, so we don't
12615 need to duplicate it for the type. */
12616 fp->name = fieldname;
12617
12618 /* Change accessibility for artificial fields (e.g. virtual table
12619 pointer or virtual base class pointer) to private. */
12620 if (dwarf2_attr (die, DW_AT_artificial, cu))
12621 {
12622 FIELD_ARTIFICIAL (*fp) = 1;
12623 new_field->accessibility = DW_ACCESS_private;
12624 fip->non_public_fields = 1;
12625 }
12626 }
12627 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12628 {
12629 /* C++ static member. */
12630
12631 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12632 is a declaration, but all versions of G++ as of this writing
12633 (so through at least 3.2.1) incorrectly generate
12634 DW_TAG_variable tags. */
12635
12636 const char *physname;
12637
12638 /* Get name of field. */
12639 fieldname = dwarf2_name (die, cu);
12640 if (fieldname == NULL)
12641 return;
12642
12643 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12644 if (attr
12645 /* Only create a symbol if this is an external value.
12646 new_symbol checks this and puts the value in the global symbol
12647 table, which we want. If it is not external, new_symbol
12648 will try to put the value in cu->list_in_scope which is wrong. */
12649 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12650 {
12651 /* A static const member, not much different than an enum as far as
12652 we're concerned, except that we can support more types. */
12653 new_symbol (die, NULL, cu);
12654 }
12655
12656 /* Get physical name. */
12657 physname = dwarf2_physname (fieldname, die, cu);
12658
12659 /* The name is already allocated along with this objfile, so we don't
12660 need to duplicate it for the type. */
12661 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12662 FIELD_TYPE (*fp) = die_type (die, cu);
12663 FIELD_NAME (*fp) = fieldname;
12664 }
12665 else if (die->tag == DW_TAG_inheritance)
12666 {
12667 LONGEST offset;
12668
12669 /* C++ base class field. */
12670 if (handle_data_member_location (die, cu, &offset))
12671 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12672 FIELD_BITSIZE (*fp) = 0;
12673 FIELD_TYPE (*fp) = die_type (die, cu);
12674 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12675 fip->nbaseclasses++;
12676 }
12677 }
12678
12679 /* Add a typedef defined in the scope of the FIP's class. */
12680
12681 static void
12682 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12683 struct dwarf2_cu *cu)
12684 {
12685 struct typedef_field_list *new_field;
12686 struct typedef_field *fp;
12687
12688 /* Allocate a new field list entry and link it in. */
12689 new_field = XCNEW (struct typedef_field_list);
12690 make_cleanup (xfree, new_field);
12691
12692 gdb_assert (die->tag == DW_TAG_typedef);
12693
12694 fp = &new_field->field;
12695
12696 /* Get name of field. */
12697 fp->name = dwarf2_name (die, cu);
12698 if (fp->name == NULL)
12699 return;
12700
12701 fp->type = read_type_die (die, cu);
12702
12703 new_field->next = fip->typedef_field_list;
12704 fip->typedef_field_list = new_field;
12705 fip->typedef_field_list_count++;
12706 }
12707
12708 /* Create the vector of fields, and attach it to the type. */
12709
12710 static void
12711 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12712 struct dwarf2_cu *cu)
12713 {
12714 int nfields = fip->nfields;
12715
12716 /* Record the field count, allocate space for the array of fields,
12717 and create blank accessibility bitfields if necessary. */
12718 TYPE_NFIELDS (type) = nfields;
12719 TYPE_FIELDS (type) = (struct field *)
12720 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12721 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12722
12723 if (fip->non_public_fields && cu->language != language_ada)
12724 {
12725 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12726
12727 TYPE_FIELD_PRIVATE_BITS (type) =
12728 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12729 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12730
12731 TYPE_FIELD_PROTECTED_BITS (type) =
12732 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12733 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12734
12735 TYPE_FIELD_IGNORE_BITS (type) =
12736 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12737 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12738 }
12739
12740 /* If the type has baseclasses, allocate and clear a bit vector for
12741 TYPE_FIELD_VIRTUAL_BITS. */
12742 if (fip->nbaseclasses && cu->language != language_ada)
12743 {
12744 int num_bytes = B_BYTES (fip->nbaseclasses);
12745 unsigned char *pointer;
12746
12747 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12748 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
12749 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12750 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12751 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12752 }
12753
12754 /* Copy the saved-up fields into the field vector. Start from the head of
12755 the list, adding to the tail of the field array, so that they end up in
12756 the same order in the array in which they were added to the list. */
12757 while (nfields-- > 0)
12758 {
12759 struct nextfield *fieldp;
12760
12761 if (fip->fields)
12762 {
12763 fieldp = fip->fields;
12764 fip->fields = fieldp->next;
12765 }
12766 else
12767 {
12768 fieldp = fip->baseclasses;
12769 fip->baseclasses = fieldp->next;
12770 }
12771
12772 TYPE_FIELD (type, nfields) = fieldp->field;
12773 switch (fieldp->accessibility)
12774 {
12775 case DW_ACCESS_private:
12776 if (cu->language != language_ada)
12777 SET_TYPE_FIELD_PRIVATE (type, nfields);
12778 break;
12779
12780 case DW_ACCESS_protected:
12781 if (cu->language != language_ada)
12782 SET_TYPE_FIELD_PROTECTED (type, nfields);
12783 break;
12784
12785 case DW_ACCESS_public:
12786 break;
12787
12788 default:
12789 /* Unknown accessibility. Complain and treat it as public. */
12790 {
12791 complaint (&symfile_complaints, _("unsupported accessibility %d"),
12792 fieldp->accessibility);
12793 }
12794 break;
12795 }
12796 if (nfields < fip->nbaseclasses)
12797 {
12798 switch (fieldp->virtuality)
12799 {
12800 case DW_VIRTUALITY_virtual:
12801 case DW_VIRTUALITY_pure_virtual:
12802 if (cu->language == language_ada)
12803 error (_("unexpected virtuality in component of Ada type"));
12804 SET_TYPE_FIELD_VIRTUAL (type, nfields);
12805 break;
12806 }
12807 }
12808 }
12809 }
12810
12811 /* Return true if this member function is a constructor, false
12812 otherwise. */
12813
12814 static int
12815 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12816 {
12817 const char *fieldname;
12818 const char *type_name;
12819 int len;
12820
12821 if (die->parent == NULL)
12822 return 0;
12823
12824 if (die->parent->tag != DW_TAG_structure_type
12825 && die->parent->tag != DW_TAG_union_type
12826 && die->parent->tag != DW_TAG_class_type)
12827 return 0;
12828
12829 fieldname = dwarf2_name (die, cu);
12830 type_name = dwarf2_name (die->parent, cu);
12831 if (fieldname == NULL || type_name == NULL)
12832 return 0;
12833
12834 len = strlen (fieldname);
12835 return (strncmp (fieldname, type_name, len) == 0
12836 && (type_name[len] == '\0' || type_name[len] == '<'));
12837 }
12838
12839 /* Add a member function to the proper fieldlist. */
12840
12841 static void
12842 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
12843 struct type *type, struct dwarf2_cu *cu)
12844 {
12845 struct objfile *objfile = cu->objfile;
12846 struct attribute *attr;
12847 struct fnfieldlist *flp;
12848 int i;
12849 struct fn_field *fnp;
12850 const char *fieldname;
12851 struct nextfnfield *new_fnfield;
12852 struct type *this_type;
12853 enum dwarf_access_attribute accessibility;
12854
12855 if (cu->language == language_ada)
12856 error (_("unexpected member function in Ada type"));
12857
12858 /* Get name of member function. */
12859 fieldname = dwarf2_name (die, cu);
12860 if (fieldname == NULL)
12861 return;
12862
12863 /* Look up member function name in fieldlist. */
12864 for (i = 0; i < fip->nfnfields; i++)
12865 {
12866 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
12867 break;
12868 }
12869
12870 /* Create new list element if necessary. */
12871 if (i < fip->nfnfields)
12872 flp = &fip->fnfieldlists[i];
12873 else
12874 {
12875 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
12876 {
12877 fip->fnfieldlists = (struct fnfieldlist *)
12878 xrealloc (fip->fnfieldlists,
12879 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
12880 * sizeof (struct fnfieldlist));
12881 if (fip->nfnfields == 0)
12882 make_cleanup (free_current_contents, &fip->fnfieldlists);
12883 }
12884 flp = &fip->fnfieldlists[fip->nfnfields];
12885 flp->name = fieldname;
12886 flp->length = 0;
12887 flp->head = NULL;
12888 i = fip->nfnfields++;
12889 }
12890
12891 /* Create a new member function field and chain it to the field list
12892 entry. */
12893 new_fnfield = XNEW (struct nextfnfield);
12894 make_cleanup (xfree, new_fnfield);
12895 memset (new_fnfield, 0, sizeof (struct nextfnfield));
12896 new_fnfield->next = flp->head;
12897 flp->head = new_fnfield;
12898 flp->length++;
12899
12900 /* Fill in the member function field info. */
12901 fnp = &new_fnfield->fnfield;
12902
12903 /* Delay processing of the physname until later. */
12904 if (cu->language == language_cplus || cu->language == language_java)
12905 {
12906 add_to_method_list (type, i, flp->length - 1, fieldname,
12907 die, cu);
12908 }
12909 else
12910 {
12911 const char *physname = dwarf2_physname (fieldname, die, cu);
12912 fnp->physname = physname ? physname : "";
12913 }
12914
12915 fnp->type = alloc_type (objfile);
12916 this_type = read_type_die (die, cu);
12917 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
12918 {
12919 int nparams = TYPE_NFIELDS (this_type);
12920
12921 /* TYPE is the domain of this method, and THIS_TYPE is the type
12922 of the method itself (TYPE_CODE_METHOD). */
12923 smash_to_method_type (fnp->type, type,
12924 TYPE_TARGET_TYPE (this_type),
12925 TYPE_FIELDS (this_type),
12926 TYPE_NFIELDS (this_type),
12927 TYPE_VARARGS (this_type));
12928
12929 /* Handle static member functions.
12930 Dwarf2 has no clean way to discern C++ static and non-static
12931 member functions. G++ helps GDB by marking the first
12932 parameter for non-static member functions (which is the this
12933 pointer) as artificial. We obtain this information from
12934 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12935 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
12936 fnp->voffset = VOFFSET_STATIC;
12937 }
12938 else
12939 complaint (&symfile_complaints, _("member function type missing for '%s'"),
12940 dwarf2_full_name (fieldname, die, cu));
12941
12942 /* Get fcontext from DW_AT_containing_type if present. */
12943 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12944 fnp->fcontext = die_containing_type (die, cu);
12945
12946 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12947 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12948
12949 /* Get accessibility. */
12950 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12951 if (attr)
12952 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
12953 else
12954 accessibility = dwarf2_default_access_attribute (die, cu);
12955 switch (accessibility)
12956 {
12957 case DW_ACCESS_private:
12958 fnp->is_private = 1;
12959 break;
12960 case DW_ACCESS_protected:
12961 fnp->is_protected = 1;
12962 break;
12963 }
12964
12965 /* Check for artificial methods. */
12966 attr = dwarf2_attr (die, DW_AT_artificial, cu);
12967 if (attr && DW_UNSND (attr) != 0)
12968 fnp->is_artificial = 1;
12969
12970 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12971
12972 /* Get index in virtual function table if it is a virtual member
12973 function. For older versions of GCC, this is an offset in the
12974 appropriate virtual table, as specified by DW_AT_containing_type.
12975 For everyone else, it is an expression to be evaluated relative
12976 to the object address. */
12977
12978 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
12979 if (attr)
12980 {
12981 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
12982 {
12983 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
12984 {
12985 /* Old-style GCC. */
12986 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
12987 }
12988 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
12989 || (DW_BLOCK (attr)->size > 1
12990 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
12991 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
12992 {
12993 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
12994 if ((fnp->voffset % cu->header.addr_size) != 0)
12995 dwarf2_complex_location_expr_complaint ();
12996 else
12997 fnp->voffset /= cu->header.addr_size;
12998 fnp->voffset += 2;
12999 }
13000 else
13001 dwarf2_complex_location_expr_complaint ();
13002
13003 if (!fnp->fcontext)
13004 {
13005 /* If there is no `this' field and no DW_AT_containing_type,
13006 we cannot actually find a base class context for the
13007 vtable! */
13008 if (TYPE_NFIELDS (this_type) == 0
13009 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13010 {
13011 complaint (&symfile_complaints,
13012 _("cannot determine context for virtual member "
13013 "function \"%s\" (offset %d)"),
13014 fieldname, die->offset.sect_off);
13015 }
13016 else
13017 {
13018 fnp->fcontext
13019 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13020 }
13021 }
13022 }
13023 else if (attr_form_is_section_offset (attr))
13024 {
13025 dwarf2_complex_location_expr_complaint ();
13026 }
13027 else
13028 {
13029 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13030 fieldname);
13031 }
13032 }
13033 else
13034 {
13035 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13036 if (attr && DW_UNSND (attr))
13037 {
13038 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13039 complaint (&symfile_complaints,
13040 _("Member function \"%s\" (offset %d) is virtual "
13041 "but the vtable offset is not specified"),
13042 fieldname, die->offset.sect_off);
13043 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13044 TYPE_CPLUS_DYNAMIC (type) = 1;
13045 }
13046 }
13047 }
13048
13049 /* Create the vector of member function fields, and attach it to the type. */
13050
13051 static void
13052 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13053 struct dwarf2_cu *cu)
13054 {
13055 struct fnfieldlist *flp;
13056 int i;
13057
13058 if (cu->language == language_ada)
13059 error (_("unexpected member functions in Ada type"));
13060
13061 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13062 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13063 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13064
13065 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13066 {
13067 struct nextfnfield *nfp = flp->head;
13068 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13069 int k;
13070
13071 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13072 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13073 fn_flp->fn_fields = (struct fn_field *)
13074 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13075 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13076 fn_flp->fn_fields[k] = nfp->fnfield;
13077 }
13078
13079 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13080 }
13081
13082 /* Returns non-zero if NAME is the name of a vtable member in CU's
13083 language, zero otherwise. */
13084 static int
13085 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13086 {
13087 static const char vptr[] = "_vptr";
13088 static const char vtable[] = "vtable";
13089
13090 /* Look for the C++ and Java forms of the vtable. */
13091 if ((cu->language == language_java
13092 && startswith (name, vtable))
13093 || (startswith (name, vptr)
13094 && is_cplus_marker (name[sizeof (vptr) - 1])))
13095 return 1;
13096
13097 return 0;
13098 }
13099
13100 /* GCC outputs unnamed structures that are really pointers to member
13101 functions, with the ABI-specified layout. If TYPE describes
13102 such a structure, smash it into a member function type.
13103
13104 GCC shouldn't do this; it should just output pointer to member DIEs.
13105 This is GCC PR debug/28767. */
13106
13107 static void
13108 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13109 {
13110 struct type *pfn_type, *self_type, *new_type;
13111
13112 /* Check for a structure with no name and two children. */
13113 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13114 return;
13115
13116 /* Check for __pfn and __delta members. */
13117 if (TYPE_FIELD_NAME (type, 0) == NULL
13118 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13119 || TYPE_FIELD_NAME (type, 1) == NULL
13120 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13121 return;
13122
13123 /* Find the type of the method. */
13124 pfn_type = TYPE_FIELD_TYPE (type, 0);
13125 if (pfn_type == NULL
13126 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13127 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13128 return;
13129
13130 /* Look for the "this" argument. */
13131 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13132 if (TYPE_NFIELDS (pfn_type) == 0
13133 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13134 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13135 return;
13136
13137 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13138 new_type = alloc_type (objfile);
13139 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13140 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13141 TYPE_VARARGS (pfn_type));
13142 smash_to_methodptr_type (type, new_type);
13143 }
13144
13145 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13146 (icc). */
13147
13148 static int
13149 producer_is_icc (struct dwarf2_cu *cu)
13150 {
13151 if (!cu->checked_producer)
13152 check_producer (cu);
13153
13154 return cu->producer_is_icc;
13155 }
13156
13157 /* Called when we find the DIE that starts a structure or union scope
13158 (definition) to create a type for the structure or union. Fill in
13159 the type's name and general properties; the members will not be
13160 processed until process_structure_scope. A symbol table entry for
13161 the type will also not be done until process_structure_scope (assuming
13162 the type has a name).
13163
13164 NOTE: we need to call these functions regardless of whether or not the
13165 DIE has a DW_AT_name attribute, since it might be an anonymous
13166 structure or union. This gets the type entered into our set of
13167 user defined types. */
13168
13169 static struct type *
13170 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13171 {
13172 struct objfile *objfile = cu->objfile;
13173 struct type *type;
13174 struct attribute *attr;
13175 const char *name;
13176
13177 /* If the definition of this type lives in .debug_types, read that type.
13178 Don't follow DW_AT_specification though, that will take us back up
13179 the chain and we want to go down. */
13180 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13181 if (attr)
13182 {
13183 type = get_DW_AT_signature_type (die, attr, cu);
13184
13185 /* The type's CU may not be the same as CU.
13186 Ensure TYPE is recorded with CU in die_type_hash. */
13187 return set_die_type (die, type, cu);
13188 }
13189
13190 type = alloc_type (objfile);
13191 INIT_CPLUS_SPECIFIC (type);
13192
13193 name = dwarf2_name (die, cu);
13194 if (name != NULL)
13195 {
13196 if (cu->language == language_cplus
13197 || cu->language == language_java
13198 || cu->language == language_d
13199 || cu->language == language_rust)
13200 {
13201 const char *full_name = dwarf2_full_name (name, die, cu);
13202
13203 /* dwarf2_full_name might have already finished building the DIE's
13204 type. If so, there is no need to continue. */
13205 if (get_die_type (die, cu) != NULL)
13206 return get_die_type (die, cu);
13207
13208 TYPE_TAG_NAME (type) = full_name;
13209 if (die->tag == DW_TAG_structure_type
13210 || die->tag == DW_TAG_class_type)
13211 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13212 }
13213 else
13214 {
13215 /* The name is already allocated along with this objfile, so
13216 we don't need to duplicate it for the type. */
13217 TYPE_TAG_NAME (type) = name;
13218 if (die->tag == DW_TAG_class_type)
13219 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13220 }
13221 }
13222
13223 if (die->tag == DW_TAG_structure_type)
13224 {
13225 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13226 }
13227 else if (die->tag == DW_TAG_union_type)
13228 {
13229 TYPE_CODE (type) = TYPE_CODE_UNION;
13230 }
13231 else
13232 {
13233 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13234 }
13235
13236 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13237 TYPE_DECLARED_CLASS (type) = 1;
13238
13239 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13240 if (attr)
13241 {
13242 if (attr_form_is_constant (attr))
13243 TYPE_LENGTH (type) = DW_UNSND (attr);
13244 else
13245 {
13246 /* For the moment, dynamic type sizes are not supported
13247 by GDB's struct type. The actual size is determined
13248 on-demand when resolving the type of a given object,
13249 so set the type's length to zero for now. Otherwise,
13250 we record an expression as the length, and that expression
13251 could lead to a very large value, which could eventually
13252 lead to us trying to allocate that much memory when creating
13253 a value of that type. */
13254 TYPE_LENGTH (type) = 0;
13255 }
13256 }
13257 else
13258 {
13259 TYPE_LENGTH (type) = 0;
13260 }
13261
13262 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13263 {
13264 /* ICC does not output the required DW_AT_declaration
13265 on incomplete types, but gives them a size of zero. */
13266 TYPE_STUB (type) = 1;
13267 }
13268 else
13269 TYPE_STUB_SUPPORTED (type) = 1;
13270
13271 if (die_is_declaration (die, cu))
13272 TYPE_STUB (type) = 1;
13273 else if (attr == NULL && die->child == NULL
13274 && producer_is_realview (cu->producer))
13275 /* RealView does not output the required DW_AT_declaration
13276 on incomplete types. */
13277 TYPE_STUB (type) = 1;
13278
13279 /* We need to add the type field to the die immediately so we don't
13280 infinitely recurse when dealing with pointers to the structure
13281 type within the structure itself. */
13282 set_die_type (die, type, cu);
13283
13284 /* set_die_type should be already done. */
13285 set_descriptive_type (type, die, cu);
13286
13287 return type;
13288 }
13289
13290 /* Finish creating a structure or union type, including filling in
13291 its members and creating a symbol for it. */
13292
13293 static void
13294 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13295 {
13296 struct objfile *objfile = cu->objfile;
13297 struct die_info *child_die;
13298 struct type *type;
13299
13300 type = get_die_type (die, cu);
13301 if (type == NULL)
13302 type = read_structure_type (die, cu);
13303
13304 if (die->child != NULL && ! die_is_declaration (die, cu))
13305 {
13306 struct field_info fi;
13307 VEC (symbolp) *template_args = NULL;
13308 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13309
13310 memset (&fi, 0, sizeof (struct field_info));
13311
13312 child_die = die->child;
13313
13314 while (child_die && child_die->tag)
13315 {
13316 if (child_die->tag == DW_TAG_member
13317 || child_die->tag == DW_TAG_variable)
13318 {
13319 /* NOTE: carlton/2002-11-05: A C++ static data member
13320 should be a DW_TAG_member that is a declaration, but
13321 all versions of G++ as of this writing (so through at
13322 least 3.2.1) incorrectly generate DW_TAG_variable
13323 tags for them instead. */
13324 dwarf2_add_field (&fi, child_die, cu);
13325 }
13326 else if (child_die->tag == DW_TAG_subprogram)
13327 {
13328 /* Rust doesn't have member functions in the C++ sense.
13329 However, it does emit ordinary functions as children
13330 of a struct DIE. */
13331 if (cu->language == language_rust)
13332 read_func_scope (child_die, cu);
13333 else
13334 {
13335 /* C++ member function. */
13336 dwarf2_add_member_fn (&fi, child_die, type, cu);
13337 }
13338 }
13339 else if (child_die->tag == DW_TAG_inheritance)
13340 {
13341 /* C++ base class field. */
13342 dwarf2_add_field (&fi, child_die, cu);
13343 }
13344 else if (child_die->tag == DW_TAG_typedef)
13345 dwarf2_add_typedef (&fi, child_die, cu);
13346 else if (child_die->tag == DW_TAG_template_type_param
13347 || child_die->tag == DW_TAG_template_value_param)
13348 {
13349 struct symbol *arg = new_symbol (child_die, NULL, cu);
13350
13351 if (arg != NULL)
13352 VEC_safe_push (symbolp, template_args, arg);
13353 }
13354
13355 child_die = sibling_die (child_die);
13356 }
13357
13358 /* Attach template arguments to type. */
13359 if (! VEC_empty (symbolp, template_args))
13360 {
13361 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13362 TYPE_N_TEMPLATE_ARGUMENTS (type)
13363 = VEC_length (symbolp, template_args);
13364 TYPE_TEMPLATE_ARGUMENTS (type)
13365 = XOBNEWVEC (&objfile->objfile_obstack,
13366 struct symbol *,
13367 TYPE_N_TEMPLATE_ARGUMENTS (type));
13368 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13369 VEC_address (symbolp, template_args),
13370 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13371 * sizeof (struct symbol *)));
13372 VEC_free (symbolp, template_args);
13373 }
13374
13375 /* Attach fields and member functions to the type. */
13376 if (fi.nfields)
13377 dwarf2_attach_fields_to_type (&fi, type, cu);
13378 if (fi.nfnfields)
13379 {
13380 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13381
13382 /* Get the type which refers to the base class (possibly this
13383 class itself) which contains the vtable pointer for the current
13384 class from the DW_AT_containing_type attribute. This use of
13385 DW_AT_containing_type is a GNU extension. */
13386
13387 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13388 {
13389 struct type *t = die_containing_type (die, cu);
13390
13391 set_type_vptr_basetype (type, t);
13392 if (type == t)
13393 {
13394 int i;
13395
13396 /* Our own class provides vtbl ptr. */
13397 for (i = TYPE_NFIELDS (t) - 1;
13398 i >= TYPE_N_BASECLASSES (t);
13399 --i)
13400 {
13401 const char *fieldname = TYPE_FIELD_NAME (t, i);
13402
13403 if (is_vtable_name (fieldname, cu))
13404 {
13405 set_type_vptr_fieldno (type, i);
13406 break;
13407 }
13408 }
13409
13410 /* Complain if virtual function table field not found. */
13411 if (i < TYPE_N_BASECLASSES (t))
13412 complaint (&symfile_complaints,
13413 _("virtual function table pointer "
13414 "not found when defining class '%s'"),
13415 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13416 "");
13417 }
13418 else
13419 {
13420 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13421 }
13422 }
13423 else if (cu->producer
13424 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13425 {
13426 /* The IBM XLC compiler does not provide direct indication
13427 of the containing type, but the vtable pointer is
13428 always named __vfp. */
13429
13430 int i;
13431
13432 for (i = TYPE_NFIELDS (type) - 1;
13433 i >= TYPE_N_BASECLASSES (type);
13434 --i)
13435 {
13436 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13437 {
13438 set_type_vptr_fieldno (type, i);
13439 set_type_vptr_basetype (type, type);
13440 break;
13441 }
13442 }
13443 }
13444 }
13445
13446 /* Copy fi.typedef_field_list linked list elements content into the
13447 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13448 if (fi.typedef_field_list)
13449 {
13450 int i = fi.typedef_field_list_count;
13451
13452 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13453 TYPE_TYPEDEF_FIELD_ARRAY (type)
13454 = ((struct typedef_field *)
13455 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13456 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13457
13458 /* Reverse the list order to keep the debug info elements order. */
13459 while (--i >= 0)
13460 {
13461 struct typedef_field *dest, *src;
13462
13463 dest = &TYPE_TYPEDEF_FIELD (type, i);
13464 src = &fi.typedef_field_list->field;
13465 fi.typedef_field_list = fi.typedef_field_list->next;
13466 *dest = *src;
13467 }
13468 }
13469
13470 do_cleanups (back_to);
13471
13472 if (HAVE_CPLUS_STRUCT (type))
13473 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
13474 }
13475
13476 quirk_gcc_member_function_pointer (type, objfile);
13477
13478 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13479 snapshots) has been known to create a die giving a declaration
13480 for a class that has, as a child, a die giving a definition for a
13481 nested class. So we have to process our children even if the
13482 current die is a declaration. Normally, of course, a declaration
13483 won't have any children at all. */
13484
13485 child_die = die->child;
13486
13487 while (child_die != NULL && child_die->tag)
13488 {
13489 if (child_die->tag == DW_TAG_member
13490 || child_die->tag == DW_TAG_variable
13491 || child_die->tag == DW_TAG_inheritance
13492 || child_die->tag == DW_TAG_template_value_param
13493 || child_die->tag == DW_TAG_template_type_param)
13494 {
13495 /* Do nothing. */
13496 }
13497 else
13498 process_die (child_die, cu);
13499
13500 child_die = sibling_die (child_die);
13501 }
13502
13503 /* Do not consider external references. According to the DWARF standard,
13504 these DIEs are identified by the fact that they have no byte_size
13505 attribute, and a declaration attribute. */
13506 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13507 || !die_is_declaration (die, cu))
13508 new_symbol (die, type, cu);
13509 }
13510
13511 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13512 update TYPE using some information only available in DIE's children. */
13513
13514 static void
13515 update_enumeration_type_from_children (struct die_info *die,
13516 struct type *type,
13517 struct dwarf2_cu *cu)
13518 {
13519 struct obstack obstack;
13520 struct die_info *child_die;
13521 int unsigned_enum = 1;
13522 int flag_enum = 1;
13523 ULONGEST mask = 0;
13524 struct cleanup *old_chain;
13525
13526 obstack_init (&obstack);
13527 old_chain = make_cleanup_obstack_free (&obstack);
13528
13529 for (child_die = die->child;
13530 child_die != NULL && child_die->tag;
13531 child_die = sibling_die (child_die))
13532 {
13533 struct attribute *attr;
13534 LONGEST value;
13535 const gdb_byte *bytes;
13536 struct dwarf2_locexpr_baton *baton;
13537 const char *name;
13538
13539 if (child_die->tag != DW_TAG_enumerator)
13540 continue;
13541
13542 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13543 if (attr == NULL)
13544 continue;
13545
13546 name = dwarf2_name (child_die, cu);
13547 if (name == NULL)
13548 name = "<anonymous enumerator>";
13549
13550 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13551 &value, &bytes, &baton);
13552 if (value < 0)
13553 {
13554 unsigned_enum = 0;
13555 flag_enum = 0;
13556 }
13557 else if ((mask & value) != 0)
13558 flag_enum = 0;
13559 else
13560 mask |= value;
13561
13562 /* If we already know that the enum type is neither unsigned, nor
13563 a flag type, no need to look at the rest of the enumerates. */
13564 if (!unsigned_enum && !flag_enum)
13565 break;
13566 }
13567
13568 if (unsigned_enum)
13569 TYPE_UNSIGNED (type) = 1;
13570 if (flag_enum)
13571 TYPE_FLAG_ENUM (type) = 1;
13572
13573 do_cleanups (old_chain);
13574 }
13575
13576 /* Given a DW_AT_enumeration_type die, set its type. We do not
13577 complete the type's fields yet, or create any symbols. */
13578
13579 static struct type *
13580 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13581 {
13582 struct objfile *objfile = cu->objfile;
13583 struct type *type;
13584 struct attribute *attr;
13585 const char *name;
13586
13587 /* If the definition of this type lives in .debug_types, read that type.
13588 Don't follow DW_AT_specification though, that will take us back up
13589 the chain and we want to go down. */
13590 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13591 if (attr)
13592 {
13593 type = get_DW_AT_signature_type (die, attr, cu);
13594
13595 /* The type's CU may not be the same as CU.
13596 Ensure TYPE is recorded with CU in die_type_hash. */
13597 return set_die_type (die, type, cu);
13598 }
13599
13600 type = alloc_type (objfile);
13601
13602 TYPE_CODE (type) = TYPE_CODE_ENUM;
13603 name = dwarf2_full_name (NULL, die, cu);
13604 if (name != NULL)
13605 TYPE_TAG_NAME (type) = name;
13606
13607 attr = dwarf2_attr (die, DW_AT_type, cu);
13608 if (attr != NULL)
13609 {
13610 struct type *underlying_type = die_type (die, cu);
13611
13612 TYPE_TARGET_TYPE (type) = underlying_type;
13613 }
13614
13615 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13616 if (attr)
13617 {
13618 TYPE_LENGTH (type) = DW_UNSND (attr);
13619 }
13620 else
13621 {
13622 TYPE_LENGTH (type) = 0;
13623 }
13624
13625 /* The enumeration DIE can be incomplete. In Ada, any type can be
13626 declared as private in the package spec, and then defined only
13627 inside the package body. Such types are known as Taft Amendment
13628 Types. When another package uses such a type, an incomplete DIE
13629 may be generated by the compiler. */
13630 if (die_is_declaration (die, cu))
13631 TYPE_STUB (type) = 1;
13632
13633 /* Finish the creation of this type by using the enum's children.
13634 We must call this even when the underlying type has been provided
13635 so that we can determine if we're looking at a "flag" enum. */
13636 update_enumeration_type_from_children (die, type, cu);
13637
13638 /* If this type has an underlying type that is not a stub, then we
13639 may use its attributes. We always use the "unsigned" attribute
13640 in this situation, because ordinarily we guess whether the type
13641 is unsigned -- but the guess can be wrong and the underlying type
13642 can tell us the reality. However, we defer to a local size
13643 attribute if one exists, because this lets the compiler override
13644 the underlying type if needed. */
13645 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13646 {
13647 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13648 if (TYPE_LENGTH (type) == 0)
13649 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13650 }
13651
13652 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13653
13654 return set_die_type (die, type, cu);
13655 }
13656
13657 /* Given a pointer to a die which begins an enumeration, process all
13658 the dies that define the members of the enumeration, and create the
13659 symbol for the enumeration type.
13660
13661 NOTE: We reverse the order of the element list. */
13662
13663 static void
13664 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13665 {
13666 struct type *this_type;
13667
13668 this_type = get_die_type (die, cu);
13669 if (this_type == NULL)
13670 this_type = read_enumeration_type (die, cu);
13671
13672 if (die->child != NULL)
13673 {
13674 struct die_info *child_die;
13675 struct symbol *sym;
13676 struct field *fields = NULL;
13677 int num_fields = 0;
13678 const char *name;
13679
13680 child_die = die->child;
13681 while (child_die && child_die->tag)
13682 {
13683 if (child_die->tag != DW_TAG_enumerator)
13684 {
13685 process_die (child_die, cu);
13686 }
13687 else
13688 {
13689 name = dwarf2_name (child_die, cu);
13690 if (name)
13691 {
13692 sym = new_symbol (child_die, this_type, cu);
13693
13694 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13695 {
13696 fields = (struct field *)
13697 xrealloc (fields,
13698 (num_fields + DW_FIELD_ALLOC_CHUNK)
13699 * sizeof (struct field));
13700 }
13701
13702 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13703 FIELD_TYPE (fields[num_fields]) = NULL;
13704 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13705 FIELD_BITSIZE (fields[num_fields]) = 0;
13706
13707 num_fields++;
13708 }
13709 }
13710
13711 child_die = sibling_die (child_die);
13712 }
13713
13714 if (num_fields)
13715 {
13716 TYPE_NFIELDS (this_type) = num_fields;
13717 TYPE_FIELDS (this_type) = (struct field *)
13718 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13719 memcpy (TYPE_FIELDS (this_type), fields,
13720 sizeof (struct field) * num_fields);
13721 xfree (fields);
13722 }
13723 }
13724
13725 /* If we are reading an enum from a .debug_types unit, and the enum
13726 is a declaration, and the enum is not the signatured type in the
13727 unit, then we do not want to add a symbol for it. Adding a
13728 symbol would in some cases obscure the true definition of the
13729 enum, giving users an incomplete type when the definition is
13730 actually available. Note that we do not want to do this for all
13731 enums which are just declarations, because C++0x allows forward
13732 enum declarations. */
13733 if (cu->per_cu->is_debug_types
13734 && die_is_declaration (die, cu))
13735 {
13736 struct signatured_type *sig_type;
13737
13738 sig_type = (struct signatured_type *) cu->per_cu;
13739 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13740 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
13741 return;
13742 }
13743
13744 new_symbol (die, this_type, cu);
13745 }
13746
13747 /* Extract all information from a DW_TAG_array_type DIE and put it in
13748 the DIE's type field. For now, this only handles one dimensional
13749 arrays. */
13750
13751 static struct type *
13752 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13753 {
13754 struct objfile *objfile = cu->objfile;
13755 struct die_info *child_die;
13756 struct type *type;
13757 struct type *element_type, *range_type, *index_type;
13758 struct type **range_types = NULL;
13759 struct attribute *attr;
13760 int ndim = 0;
13761 struct cleanup *back_to;
13762 const char *name;
13763 unsigned int bit_stride = 0;
13764
13765 element_type = die_type (die, cu);
13766
13767 /* The die_type call above may have already set the type for this DIE. */
13768 type = get_die_type (die, cu);
13769 if (type)
13770 return type;
13771
13772 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13773 if (attr != NULL)
13774 bit_stride = DW_UNSND (attr) * 8;
13775
13776 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13777 if (attr != NULL)
13778 bit_stride = DW_UNSND (attr);
13779
13780 /* Irix 6.2 native cc creates array types without children for
13781 arrays with unspecified length. */
13782 if (die->child == NULL)
13783 {
13784 index_type = objfile_type (objfile)->builtin_int;
13785 range_type = create_static_range_type (NULL, index_type, 0, -1);
13786 type = create_array_type_with_stride (NULL, element_type, range_type,
13787 bit_stride);
13788 return set_die_type (die, type, cu);
13789 }
13790
13791 back_to = make_cleanup (null_cleanup, NULL);
13792 child_die = die->child;
13793 while (child_die && child_die->tag)
13794 {
13795 if (child_die->tag == DW_TAG_subrange_type)
13796 {
13797 struct type *child_type = read_type_die (child_die, cu);
13798
13799 if (child_type != NULL)
13800 {
13801 /* The range type was succesfully read. Save it for the
13802 array type creation. */
13803 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
13804 {
13805 range_types = (struct type **)
13806 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
13807 * sizeof (struct type *));
13808 if (ndim == 0)
13809 make_cleanup (free_current_contents, &range_types);
13810 }
13811 range_types[ndim++] = child_type;
13812 }
13813 }
13814 child_die = sibling_die (child_die);
13815 }
13816
13817 /* Dwarf2 dimensions are output from left to right, create the
13818 necessary array types in backwards order. */
13819
13820 type = element_type;
13821
13822 if (read_array_order (die, cu) == DW_ORD_col_major)
13823 {
13824 int i = 0;
13825
13826 while (i < ndim)
13827 type = create_array_type_with_stride (NULL, type, range_types[i++],
13828 bit_stride);
13829 }
13830 else
13831 {
13832 while (ndim-- > 0)
13833 type = create_array_type_with_stride (NULL, type, range_types[ndim],
13834 bit_stride);
13835 }
13836
13837 /* Understand Dwarf2 support for vector types (like they occur on
13838 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13839 array type. This is not part of the Dwarf2/3 standard yet, but a
13840 custom vendor extension. The main difference between a regular
13841 array and the vector variant is that vectors are passed by value
13842 to functions. */
13843 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13844 if (attr)
13845 make_vector_type (type);
13846
13847 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13848 implementation may choose to implement triple vectors using this
13849 attribute. */
13850 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13851 if (attr)
13852 {
13853 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
13854 TYPE_LENGTH (type) = DW_UNSND (attr);
13855 else
13856 complaint (&symfile_complaints,
13857 _("DW_AT_byte_size for array type smaller "
13858 "than the total size of elements"));
13859 }
13860
13861 name = dwarf2_name (die, cu);
13862 if (name)
13863 TYPE_NAME (type) = name;
13864
13865 /* Install the type in the die. */
13866 set_die_type (die, type, cu);
13867
13868 /* set_die_type should be already done. */
13869 set_descriptive_type (type, die, cu);
13870
13871 do_cleanups (back_to);
13872
13873 return type;
13874 }
13875
13876 static enum dwarf_array_dim_ordering
13877 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13878 {
13879 struct attribute *attr;
13880
13881 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13882
13883 if (attr)
13884 return (enum dwarf_array_dim_ordering) DW_SND (attr);
13885
13886 /* GNU F77 is a special case, as at 08/2004 array type info is the
13887 opposite order to the dwarf2 specification, but data is still
13888 laid out as per normal fortran.
13889
13890 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13891 version checking. */
13892
13893 if (cu->language == language_fortran
13894 && cu->producer && strstr (cu->producer, "GNU F77"))
13895 {
13896 return DW_ORD_row_major;
13897 }
13898
13899 switch (cu->language_defn->la_array_ordering)
13900 {
13901 case array_column_major:
13902 return DW_ORD_col_major;
13903 case array_row_major:
13904 default:
13905 return DW_ORD_row_major;
13906 };
13907 }
13908
13909 /* Extract all information from a DW_TAG_set_type DIE and put it in
13910 the DIE's type field. */
13911
13912 static struct type *
13913 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13914 {
13915 struct type *domain_type, *set_type;
13916 struct attribute *attr;
13917
13918 domain_type = die_type (die, cu);
13919
13920 /* The die_type call above may have already set the type for this DIE. */
13921 set_type = get_die_type (die, cu);
13922 if (set_type)
13923 return set_type;
13924
13925 set_type = create_set_type (NULL, domain_type);
13926
13927 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13928 if (attr)
13929 TYPE_LENGTH (set_type) = DW_UNSND (attr);
13930
13931 return set_die_type (die, set_type, cu);
13932 }
13933
13934 /* A helper for read_common_block that creates a locexpr baton.
13935 SYM is the symbol which we are marking as computed.
13936 COMMON_DIE is the DIE for the common block.
13937 COMMON_LOC is the location expression attribute for the common
13938 block itself.
13939 MEMBER_LOC is the location expression attribute for the particular
13940 member of the common block that we are processing.
13941 CU is the CU from which the above come. */
13942
13943 static void
13944 mark_common_block_symbol_computed (struct symbol *sym,
13945 struct die_info *common_die,
13946 struct attribute *common_loc,
13947 struct attribute *member_loc,
13948 struct dwarf2_cu *cu)
13949 {
13950 struct objfile *objfile = dwarf2_per_objfile->objfile;
13951 struct dwarf2_locexpr_baton *baton;
13952 gdb_byte *ptr;
13953 unsigned int cu_off;
13954 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
13955 LONGEST offset = 0;
13956
13957 gdb_assert (common_loc && member_loc);
13958 gdb_assert (attr_form_is_block (common_loc));
13959 gdb_assert (attr_form_is_block (member_loc)
13960 || attr_form_is_constant (member_loc));
13961
13962 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13963 baton->per_cu = cu->per_cu;
13964 gdb_assert (baton->per_cu);
13965
13966 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13967
13968 if (attr_form_is_constant (member_loc))
13969 {
13970 offset = dwarf2_get_attr_constant_value (member_loc, 0);
13971 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13972 }
13973 else
13974 baton->size += DW_BLOCK (member_loc)->size;
13975
13976 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
13977 baton->data = ptr;
13978
13979 *ptr++ = DW_OP_call4;
13980 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
13981 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13982 ptr += 4;
13983
13984 if (attr_form_is_constant (member_loc))
13985 {
13986 *ptr++ = DW_OP_addr;
13987 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13988 ptr += cu->header.addr_size;
13989 }
13990 else
13991 {
13992 /* We have to copy the data here, because DW_OP_call4 will only
13993 use a DW_AT_location attribute. */
13994 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
13995 ptr += DW_BLOCK (member_loc)->size;
13996 }
13997
13998 *ptr++ = DW_OP_plus;
13999 gdb_assert (ptr - baton->data == baton->size);
14000
14001 SYMBOL_LOCATION_BATON (sym) = baton;
14002 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14003 }
14004
14005 /* Create appropriate locally-scoped variables for all the
14006 DW_TAG_common_block entries. Also create a struct common_block
14007 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14008 is used to sepate the common blocks name namespace from regular
14009 variable names. */
14010
14011 static void
14012 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14013 {
14014 struct attribute *attr;
14015
14016 attr = dwarf2_attr (die, DW_AT_location, cu);
14017 if (attr)
14018 {
14019 /* Support the .debug_loc offsets. */
14020 if (attr_form_is_block (attr))
14021 {
14022 /* Ok. */
14023 }
14024 else if (attr_form_is_section_offset (attr))
14025 {
14026 dwarf2_complex_location_expr_complaint ();
14027 attr = NULL;
14028 }
14029 else
14030 {
14031 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14032 "common block member");
14033 attr = NULL;
14034 }
14035 }
14036
14037 if (die->child != NULL)
14038 {
14039 struct objfile *objfile = cu->objfile;
14040 struct die_info *child_die;
14041 size_t n_entries = 0, size;
14042 struct common_block *common_block;
14043 struct symbol *sym;
14044
14045 for (child_die = die->child;
14046 child_die && child_die->tag;
14047 child_die = sibling_die (child_die))
14048 ++n_entries;
14049
14050 size = (sizeof (struct common_block)
14051 + (n_entries - 1) * sizeof (struct symbol *));
14052 common_block
14053 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14054 size);
14055 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14056 common_block->n_entries = 0;
14057
14058 for (child_die = die->child;
14059 child_die && child_die->tag;
14060 child_die = sibling_die (child_die))
14061 {
14062 /* Create the symbol in the DW_TAG_common_block block in the current
14063 symbol scope. */
14064 sym = new_symbol (child_die, NULL, cu);
14065 if (sym != NULL)
14066 {
14067 struct attribute *member_loc;
14068
14069 common_block->contents[common_block->n_entries++] = sym;
14070
14071 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14072 cu);
14073 if (member_loc)
14074 {
14075 /* GDB has handled this for a long time, but it is
14076 not specified by DWARF. It seems to have been
14077 emitted by gfortran at least as recently as:
14078 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14079 complaint (&symfile_complaints,
14080 _("Variable in common block has "
14081 "DW_AT_data_member_location "
14082 "- DIE at 0x%x [in module %s]"),
14083 child_die->offset.sect_off,
14084 objfile_name (cu->objfile));
14085
14086 if (attr_form_is_section_offset (member_loc))
14087 dwarf2_complex_location_expr_complaint ();
14088 else if (attr_form_is_constant (member_loc)
14089 || attr_form_is_block (member_loc))
14090 {
14091 if (attr)
14092 mark_common_block_symbol_computed (sym, die, attr,
14093 member_loc, cu);
14094 }
14095 else
14096 dwarf2_complex_location_expr_complaint ();
14097 }
14098 }
14099 }
14100
14101 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14102 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14103 }
14104 }
14105
14106 /* Create a type for a C++ namespace. */
14107
14108 static struct type *
14109 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14110 {
14111 struct objfile *objfile = cu->objfile;
14112 const char *previous_prefix, *name;
14113 int is_anonymous;
14114 struct type *type;
14115
14116 /* For extensions, reuse the type of the original namespace. */
14117 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14118 {
14119 struct die_info *ext_die;
14120 struct dwarf2_cu *ext_cu = cu;
14121
14122 ext_die = dwarf2_extension (die, &ext_cu);
14123 type = read_type_die (ext_die, ext_cu);
14124
14125 /* EXT_CU may not be the same as CU.
14126 Ensure TYPE is recorded with CU in die_type_hash. */
14127 return set_die_type (die, type, cu);
14128 }
14129
14130 name = namespace_name (die, &is_anonymous, cu);
14131
14132 /* Now build the name of the current namespace. */
14133
14134 previous_prefix = determine_prefix (die, cu);
14135 if (previous_prefix[0] != '\0')
14136 name = typename_concat (&objfile->objfile_obstack,
14137 previous_prefix, name, 0, cu);
14138
14139 /* Create the type. */
14140 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14141 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14142
14143 return set_die_type (die, type, cu);
14144 }
14145
14146 /* Read a namespace scope. */
14147
14148 static void
14149 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14150 {
14151 struct objfile *objfile = cu->objfile;
14152 int is_anonymous;
14153
14154 /* Add a symbol associated to this if we haven't seen the namespace
14155 before. Also, add a using directive if it's an anonymous
14156 namespace. */
14157
14158 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14159 {
14160 struct type *type;
14161
14162 type = read_type_die (die, cu);
14163 new_symbol (die, type, cu);
14164
14165 namespace_name (die, &is_anonymous, cu);
14166 if (is_anonymous)
14167 {
14168 const char *previous_prefix = determine_prefix (die, cu);
14169
14170 add_using_directive (using_directives (cu->language),
14171 previous_prefix, TYPE_NAME (type), NULL,
14172 NULL, NULL, 0, &objfile->objfile_obstack);
14173 }
14174 }
14175
14176 if (die->child != NULL)
14177 {
14178 struct die_info *child_die = die->child;
14179
14180 while (child_die && child_die->tag)
14181 {
14182 process_die (child_die, cu);
14183 child_die = sibling_die (child_die);
14184 }
14185 }
14186 }
14187
14188 /* Read a Fortran module as type. This DIE can be only a declaration used for
14189 imported module. Still we need that type as local Fortran "use ... only"
14190 declaration imports depend on the created type in determine_prefix. */
14191
14192 static struct type *
14193 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14194 {
14195 struct objfile *objfile = cu->objfile;
14196 const char *module_name;
14197 struct type *type;
14198
14199 module_name = dwarf2_name (die, cu);
14200 if (!module_name)
14201 complaint (&symfile_complaints,
14202 _("DW_TAG_module has no name, offset 0x%x"),
14203 die->offset.sect_off);
14204 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14205
14206 /* determine_prefix uses TYPE_TAG_NAME. */
14207 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14208
14209 return set_die_type (die, type, cu);
14210 }
14211
14212 /* Read a Fortran module. */
14213
14214 static void
14215 read_module (struct die_info *die, struct dwarf2_cu *cu)
14216 {
14217 struct die_info *child_die = die->child;
14218 struct type *type;
14219
14220 type = read_type_die (die, cu);
14221 new_symbol (die, type, cu);
14222
14223 while (child_die && child_die->tag)
14224 {
14225 process_die (child_die, cu);
14226 child_die = sibling_die (child_die);
14227 }
14228 }
14229
14230 /* Return the name of the namespace represented by DIE. Set
14231 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14232 namespace. */
14233
14234 static const char *
14235 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14236 {
14237 struct die_info *current_die;
14238 const char *name = NULL;
14239
14240 /* Loop through the extensions until we find a name. */
14241
14242 for (current_die = die;
14243 current_die != NULL;
14244 current_die = dwarf2_extension (die, &cu))
14245 {
14246 /* We don't use dwarf2_name here so that we can detect the absence
14247 of a name -> anonymous namespace. */
14248 name = dwarf2_string_attr (die, DW_AT_name, cu);
14249
14250 if (name != NULL)
14251 break;
14252 }
14253
14254 /* Is it an anonymous namespace? */
14255
14256 *is_anonymous = (name == NULL);
14257 if (*is_anonymous)
14258 name = CP_ANONYMOUS_NAMESPACE_STR;
14259
14260 return name;
14261 }
14262
14263 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14264 the user defined type vector. */
14265
14266 static struct type *
14267 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14268 {
14269 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14270 struct comp_unit_head *cu_header = &cu->header;
14271 struct type *type;
14272 struct attribute *attr_byte_size;
14273 struct attribute *attr_address_class;
14274 int byte_size, addr_class;
14275 struct type *target_type;
14276
14277 target_type = die_type (die, cu);
14278
14279 /* The die_type call above may have already set the type for this DIE. */
14280 type = get_die_type (die, cu);
14281 if (type)
14282 return type;
14283
14284 type = lookup_pointer_type (target_type);
14285
14286 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14287 if (attr_byte_size)
14288 byte_size = DW_UNSND (attr_byte_size);
14289 else
14290 byte_size = cu_header->addr_size;
14291
14292 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14293 if (attr_address_class)
14294 addr_class = DW_UNSND (attr_address_class);
14295 else
14296 addr_class = DW_ADDR_none;
14297
14298 /* If the pointer size or address class is different than the
14299 default, create a type variant marked as such and set the
14300 length accordingly. */
14301 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14302 {
14303 if (gdbarch_address_class_type_flags_p (gdbarch))
14304 {
14305 int type_flags;
14306
14307 type_flags = gdbarch_address_class_type_flags
14308 (gdbarch, byte_size, addr_class);
14309 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14310 == 0);
14311 type = make_type_with_address_space (type, type_flags);
14312 }
14313 else if (TYPE_LENGTH (type) != byte_size)
14314 {
14315 complaint (&symfile_complaints,
14316 _("invalid pointer size %d"), byte_size);
14317 }
14318 else
14319 {
14320 /* Should we also complain about unhandled address classes? */
14321 }
14322 }
14323
14324 TYPE_LENGTH (type) = byte_size;
14325 return set_die_type (die, type, cu);
14326 }
14327
14328 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14329 the user defined type vector. */
14330
14331 static struct type *
14332 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14333 {
14334 struct type *type;
14335 struct type *to_type;
14336 struct type *domain;
14337
14338 to_type = die_type (die, cu);
14339 domain = die_containing_type (die, cu);
14340
14341 /* The calls above may have already set the type for this DIE. */
14342 type = get_die_type (die, cu);
14343 if (type)
14344 return type;
14345
14346 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14347 type = lookup_methodptr_type (to_type);
14348 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14349 {
14350 struct type *new_type = alloc_type (cu->objfile);
14351
14352 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14353 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14354 TYPE_VARARGS (to_type));
14355 type = lookup_methodptr_type (new_type);
14356 }
14357 else
14358 type = lookup_memberptr_type (to_type, domain);
14359
14360 return set_die_type (die, type, cu);
14361 }
14362
14363 /* Extract all information from a DW_TAG_reference_type DIE and add to
14364 the user defined type vector. */
14365
14366 static struct type *
14367 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
14368 {
14369 struct comp_unit_head *cu_header = &cu->header;
14370 struct type *type, *target_type;
14371 struct attribute *attr;
14372
14373 target_type = die_type (die, cu);
14374
14375 /* The die_type call above may have already set the type for this DIE. */
14376 type = get_die_type (die, cu);
14377 if (type)
14378 return type;
14379
14380 type = lookup_reference_type (target_type);
14381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14382 if (attr)
14383 {
14384 TYPE_LENGTH (type) = DW_UNSND (attr);
14385 }
14386 else
14387 {
14388 TYPE_LENGTH (type) = cu_header->addr_size;
14389 }
14390 return set_die_type (die, type, cu);
14391 }
14392
14393 /* Add the given cv-qualifiers to the element type of the array. GCC
14394 outputs DWARF type qualifiers that apply to an array, not the
14395 element type. But GDB relies on the array element type to carry
14396 the cv-qualifiers. This mimics section 6.7.3 of the C99
14397 specification. */
14398
14399 static struct type *
14400 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14401 struct type *base_type, int cnst, int voltl)
14402 {
14403 struct type *el_type, *inner_array;
14404
14405 base_type = copy_type (base_type);
14406 inner_array = base_type;
14407
14408 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14409 {
14410 TYPE_TARGET_TYPE (inner_array) =
14411 copy_type (TYPE_TARGET_TYPE (inner_array));
14412 inner_array = TYPE_TARGET_TYPE (inner_array);
14413 }
14414
14415 el_type = TYPE_TARGET_TYPE (inner_array);
14416 cnst |= TYPE_CONST (el_type);
14417 voltl |= TYPE_VOLATILE (el_type);
14418 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14419
14420 return set_die_type (die, base_type, cu);
14421 }
14422
14423 static struct type *
14424 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14425 {
14426 struct type *base_type, *cv_type;
14427
14428 base_type = die_type (die, cu);
14429
14430 /* The die_type call above may have already set the type for this DIE. */
14431 cv_type = get_die_type (die, cu);
14432 if (cv_type)
14433 return cv_type;
14434
14435 /* In case the const qualifier is applied to an array type, the element type
14436 is so qualified, not the array type (section 6.7.3 of C99). */
14437 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14438 return add_array_cv_type (die, cu, base_type, 1, 0);
14439
14440 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14441 return set_die_type (die, cv_type, cu);
14442 }
14443
14444 static struct type *
14445 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14446 {
14447 struct type *base_type, *cv_type;
14448
14449 base_type = die_type (die, cu);
14450
14451 /* The die_type call above may have already set the type for this DIE. */
14452 cv_type = get_die_type (die, cu);
14453 if (cv_type)
14454 return cv_type;
14455
14456 /* In case the volatile qualifier is applied to an array type, the
14457 element type is so qualified, not the array type (section 6.7.3
14458 of C99). */
14459 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14460 return add_array_cv_type (die, cu, base_type, 0, 1);
14461
14462 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14463 return set_die_type (die, cv_type, cu);
14464 }
14465
14466 /* Handle DW_TAG_restrict_type. */
14467
14468 static struct type *
14469 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14470 {
14471 struct type *base_type, *cv_type;
14472
14473 base_type = die_type (die, cu);
14474
14475 /* The die_type call above may have already set the type for this DIE. */
14476 cv_type = get_die_type (die, cu);
14477 if (cv_type)
14478 return cv_type;
14479
14480 cv_type = make_restrict_type (base_type);
14481 return set_die_type (die, cv_type, cu);
14482 }
14483
14484 /* Handle DW_TAG_atomic_type. */
14485
14486 static struct type *
14487 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14488 {
14489 struct type *base_type, *cv_type;
14490
14491 base_type = die_type (die, cu);
14492
14493 /* The die_type call above may have already set the type for this DIE. */
14494 cv_type = get_die_type (die, cu);
14495 if (cv_type)
14496 return cv_type;
14497
14498 cv_type = make_atomic_type (base_type);
14499 return set_die_type (die, cv_type, cu);
14500 }
14501
14502 /* Extract all information from a DW_TAG_string_type DIE and add to
14503 the user defined type vector. It isn't really a user defined type,
14504 but it behaves like one, with other DIE's using an AT_user_def_type
14505 attribute to reference it. */
14506
14507 static struct type *
14508 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14509 {
14510 struct objfile *objfile = cu->objfile;
14511 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14512 struct type *type, *range_type, *index_type, *char_type;
14513 struct attribute *attr;
14514 unsigned int length;
14515
14516 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14517 if (attr)
14518 {
14519 length = DW_UNSND (attr);
14520 }
14521 else
14522 {
14523 /* Check for the DW_AT_byte_size attribute. */
14524 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14525 if (attr)
14526 {
14527 length = DW_UNSND (attr);
14528 }
14529 else
14530 {
14531 length = 1;
14532 }
14533 }
14534
14535 index_type = objfile_type (objfile)->builtin_int;
14536 range_type = create_static_range_type (NULL, index_type, 1, length);
14537 char_type = language_string_char_type (cu->language_defn, gdbarch);
14538 type = create_string_type (NULL, char_type, range_type);
14539
14540 return set_die_type (die, type, cu);
14541 }
14542
14543 /* Assuming that DIE corresponds to a function, returns nonzero
14544 if the function is prototyped. */
14545
14546 static int
14547 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14548 {
14549 struct attribute *attr;
14550
14551 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14552 if (attr && (DW_UNSND (attr) != 0))
14553 return 1;
14554
14555 /* The DWARF standard implies that the DW_AT_prototyped attribute
14556 is only meaninful for C, but the concept also extends to other
14557 languages that allow unprototyped functions (Eg: Objective C).
14558 For all other languages, assume that functions are always
14559 prototyped. */
14560 if (cu->language != language_c
14561 && cu->language != language_objc
14562 && cu->language != language_opencl)
14563 return 1;
14564
14565 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14566 prototyped and unprototyped functions; default to prototyped,
14567 since that is more common in modern code (and RealView warns
14568 about unprototyped functions). */
14569 if (producer_is_realview (cu->producer))
14570 return 1;
14571
14572 return 0;
14573 }
14574
14575 /* Handle DIES due to C code like:
14576
14577 struct foo
14578 {
14579 int (*funcp)(int a, long l);
14580 int b;
14581 };
14582
14583 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14584
14585 static struct type *
14586 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14587 {
14588 struct objfile *objfile = cu->objfile;
14589 struct type *type; /* Type that this function returns. */
14590 struct type *ftype; /* Function that returns above type. */
14591 struct attribute *attr;
14592
14593 type = die_type (die, cu);
14594
14595 /* The die_type call above may have already set the type for this DIE. */
14596 ftype = get_die_type (die, cu);
14597 if (ftype)
14598 return ftype;
14599
14600 ftype = lookup_function_type (type);
14601
14602 if (prototyped_function_p (die, cu))
14603 TYPE_PROTOTYPED (ftype) = 1;
14604
14605 /* Store the calling convention in the type if it's available in
14606 the subroutine die. Otherwise set the calling convention to
14607 the default value DW_CC_normal. */
14608 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14609 if (attr)
14610 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14611 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14612 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14613 else
14614 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14615
14616 /* Record whether the function returns normally to its caller or not
14617 if the DWARF producer set that information. */
14618 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14619 if (attr && (DW_UNSND (attr) != 0))
14620 TYPE_NO_RETURN (ftype) = 1;
14621
14622 /* We need to add the subroutine type to the die immediately so
14623 we don't infinitely recurse when dealing with parameters
14624 declared as the same subroutine type. */
14625 set_die_type (die, ftype, cu);
14626
14627 if (die->child != NULL)
14628 {
14629 struct type *void_type = objfile_type (objfile)->builtin_void;
14630 struct die_info *child_die;
14631 int nparams, iparams;
14632
14633 /* Count the number of parameters.
14634 FIXME: GDB currently ignores vararg functions, but knows about
14635 vararg member functions. */
14636 nparams = 0;
14637 child_die = die->child;
14638 while (child_die && child_die->tag)
14639 {
14640 if (child_die->tag == DW_TAG_formal_parameter)
14641 nparams++;
14642 else if (child_die->tag == DW_TAG_unspecified_parameters)
14643 TYPE_VARARGS (ftype) = 1;
14644 child_die = sibling_die (child_die);
14645 }
14646
14647 /* Allocate storage for parameters and fill them in. */
14648 TYPE_NFIELDS (ftype) = nparams;
14649 TYPE_FIELDS (ftype) = (struct field *)
14650 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14651
14652 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14653 even if we error out during the parameters reading below. */
14654 for (iparams = 0; iparams < nparams; iparams++)
14655 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14656
14657 iparams = 0;
14658 child_die = die->child;
14659 while (child_die && child_die->tag)
14660 {
14661 if (child_die->tag == DW_TAG_formal_parameter)
14662 {
14663 struct type *arg_type;
14664
14665 /* DWARF version 2 has no clean way to discern C++
14666 static and non-static member functions. G++ helps
14667 GDB by marking the first parameter for non-static
14668 member functions (which is the this pointer) as
14669 artificial. We pass this information to
14670 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14671
14672 DWARF version 3 added DW_AT_object_pointer, which GCC
14673 4.5 does not yet generate. */
14674 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14675 if (attr)
14676 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14677 else
14678 {
14679 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14680
14681 /* GCC/43521: In java, the formal parameter
14682 "this" is sometimes not marked with DW_AT_artificial. */
14683 if (cu->language == language_java)
14684 {
14685 const char *name = dwarf2_name (child_die, cu);
14686
14687 if (name && !strcmp (name, "this"))
14688 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
14689 }
14690 }
14691 arg_type = die_type (child_die, cu);
14692
14693 /* RealView does not mark THIS as const, which the testsuite
14694 expects. GCC marks THIS as const in method definitions,
14695 but not in the class specifications (GCC PR 43053). */
14696 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14697 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14698 {
14699 int is_this = 0;
14700 struct dwarf2_cu *arg_cu = cu;
14701 const char *name = dwarf2_name (child_die, cu);
14702
14703 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14704 if (attr)
14705 {
14706 /* If the compiler emits this, use it. */
14707 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14708 is_this = 1;
14709 }
14710 else if (name && strcmp (name, "this") == 0)
14711 /* Function definitions will have the argument names. */
14712 is_this = 1;
14713 else if (name == NULL && iparams == 0)
14714 /* Declarations may not have the names, so like
14715 elsewhere in GDB, assume an artificial first
14716 argument is "this". */
14717 is_this = 1;
14718
14719 if (is_this)
14720 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14721 arg_type, 0);
14722 }
14723
14724 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14725 iparams++;
14726 }
14727 child_die = sibling_die (child_die);
14728 }
14729 }
14730
14731 return ftype;
14732 }
14733
14734 static struct type *
14735 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14736 {
14737 struct objfile *objfile = cu->objfile;
14738 const char *name = NULL;
14739 struct type *this_type, *target_type;
14740
14741 name = dwarf2_full_name (NULL, die, cu);
14742 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14743 TYPE_TARGET_STUB (this_type) = 1;
14744 set_die_type (die, this_type, cu);
14745 target_type = die_type (die, cu);
14746 if (target_type != this_type)
14747 TYPE_TARGET_TYPE (this_type) = target_type;
14748 else
14749 {
14750 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14751 spec and cause infinite loops in GDB. */
14752 complaint (&symfile_complaints,
14753 _("Self-referential DW_TAG_typedef "
14754 "- DIE at 0x%x [in module %s]"),
14755 die->offset.sect_off, objfile_name (objfile));
14756 TYPE_TARGET_TYPE (this_type) = NULL;
14757 }
14758 return this_type;
14759 }
14760
14761 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14762 (which may be different from NAME) to the architecture back-end to allow
14763 it to guess the correct format if necessary. */
14764
14765 static struct type *
14766 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
14767 const char *name_hint)
14768 {
14769 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14770 const struct floatformat **format;
14771 struct type *type;
14772
14773 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14774 if (format)
14775 type = init_float_type (objfile, bits, name, format);
14776 else
14777 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
14778
14779 return type;
14780 }
14781
14782 /* Find a representation of a given base type and install
14783 it in the TYPE field of the die. */
14784
14785 static struct type *
14786 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
14787 {
14788 struct objfile *objfile = cu->objfile;
14789 struct type *type;
14790 struct attribute *attr;
14791 int encoding = 0, bits = 0;
14792 const char *name;
14793
14794 attr = dwarf2_attr (die, DW_AT_encoding, cu);
14795 if (attr)
14796 {
14797 encoding = DW_UNSND (attr);
14798 }
14799 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14800 if (attr)
14801 {
14802 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
14803 }
14804 name = dwarf2_name (die, cu);
14805 if (!name)
14806 {
14807 complaint (&symfile_complaints,
14808 _("DW_AT_name missing from DW_TAG_base_type"));
14809 }
14810
14811 switch (encoding)
14812 {
14813 case DW_ATE_address:
14814 /* Turn DW_ATE_address into a void * pointer. */
14815 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
14816 type = init_pointer_type (objfile, bits, name, type);
14817 break;
14818 case DW_ATE_boolean:
14819 type = init_boolean_type (objfile, bits, 1, name);
14820 break;
14821 case DW_ATE_complex_float:
14822 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
14823 type = init_complex_type (objfile, name, type);
14824 break;
14825 case DW_ATE_decimal_float:
14826 type = init_decfloat_type (objfile, bits, name);
14827 break;
14828 case DW_ATE_float:
14829 type = dwarf2_init_float_type (objfile, bits, name, name);
14830 break;
14831 case DW_ATE_signed:
14832 type = init_integer_type (objfile, bits, 0, name);
14833 break;
14834 case DW_ATE_unsigned:
14835 if (cu->language == language_fortran
14836 && name
14837 && startswith (name, "character("))
14838 type = init_character_type (objfile, bits, 1, name);
14839 else
14840 type = init_integer_type (objfile, bits, 1, name);
14841 break;
14842 case DW_ATE_signed_char:
14843 if (cu->language == language_ada || cu->language == language_m2
14844 || cu->language == language_pascal
14845 || cu->language == language_fortran)
14846 type = init_character_type (objfile, bits, 0, name);
14847 else
14848 type = init_integer_type (objfile, bits, 0, name);
14849 break;
14850 case DW_ATE_unsigned_char:
14851 if (cu->language == language_ada || cu->language == language_m2
14852 || cu->language == language_pascal
14853 || cu->language == language_fortran
14854 || cu->language == language_rust)
14855 type = init_character_type (objfile, bits, 1, name);
14856 else
14857 type = init_integer_type (objfile, bits, 1, name);
14858 break;
14859 case DW_ATE_UTF:
14860 /* We just treat this as an integer and then recognize the
14861 type by name elsewhere. */
14862 type = init_integer_type (objfile, bits, 0, name);
14863 break;
14864
14865 default:
14866 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
14867 dwarf_type_encoding_name (encoding));
14868 type = init_type (objfile, TYPE_CODE_ERROR,
14869 bits / TARGET_CHAR_BIT, name);
14870 break;
14871 }
14872
14873 if (name && strcmp (name, "char") == 0)
14874 TYPE_NOSIGN (type) = 1;
14875
14876 return set_die_type (die, type, cu);
14877 }
14878
14879 /* Parse dwarf attribute if it's a block, reference or constant and put the
14880 resulting value of the attribute into struct bound_prop.
14881 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
14882
14883 static int
14884 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
14885 struct dwarf2_cu *cu, struct dynamic_prop *prop)
14886 {
14887 struct dwarf2_property_baton *baton;
14888 struct obstack *obstack = &cu->objfile->objfile_obstack;
14889
14890 if (attr == NULL || prop == NULL)
14891 return 0;
14892
14893 if (attr_form_is_block (attr))
14894 {
14895 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14896 baton->referenced_type = NULL;
14897 baton->locexpr.per_cu = cu->per_cu;
14898 baton->locexpr.size = DW_BLOCK (attr)->size;
14899 baton->locexpr.data = DW_BLOCK (attr)->data;
14900 prop->data.baton = baton;
14901 prop->kind = PROP_LOCEXPR;
14902 gdb_assert (prop->data.baton != NULL);
14903 }
14904 else if (attr_form_is_ref (attr))
14905 {
14906 struct dwarf2_cu *target_cu = cu;
14907 struct die_info *target_die;
14908 struct attribute *target_attr;
14909
14910 target_die = follow_die_ref (die, attr, &target_cu);
14911 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
14912 if (target_attr == NULL)
14913 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
14914 target_cu);
14915 if (target_attr == NULL)
14916 return 0;
14917
14918 switch (target_attr->name)
14919 {
14920 case DW_AT_location:
14921 if (attr_form_is_section_offset (target_attr))
14922 {
14923 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14924 baton->referenced_type = die_type (target_die, target_cu);
14925 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
14926 prop->data.baton = baton;
14927 prop->kind = PROP_LOCLIST;
14928 gdb_assert (prop->data.baton != NULL);
14929 }
14930 else if (attr_form_is_block (target_attr))
14931 {
14932 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14933 baton->referenced_type = die_type (target_die, target_cu);
14934 baton->locexpr.per_cu = cu->per_cu;
14935 baton->locexpr.size = DW_BLOCK (target_attr)->size;
14936 baton->locexpr.data = DW_BLOCK (target_attr)->data;
14937 prop->data.baton = baton;
14938 prop->kind = PROP_LOCEXPR;
14939 gdb_assert (prop->data.baton != NULL);
14940 }
14941 else
14942 {
14943 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14944 "dynamic property");
14945 return 0;
14946 }
14947 break;
14948 case DW_AT_data_member_location:
14949 {
14950 LONGEST offset;
14951
14952 if (!handle_data_member_location (target_die, target_cu,
14953 &offset))
14954 return 0;
14955
14956 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14957 baton->referenced_type = read_type_die (target_die->parent,
14958 target_cu);
14959 baton->offset_info.offset = offset;
14960 baton->offset_info.type = die_type (target_die, target_cu);
14961 prop->data.baton = baton;
14962 prop->kind = PROP_ADDR_OFFSET;
14963 break;
14964 }
14965 }
14966 }
14967 else if (attr_form_is_constant (attr))
14968 {
14969 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
14970 prop->kind = PROP_CONST;
14971 }
14972 else
14973 {
14974 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
14975 dwarf2_name (die, cu));
14976 return 0;
14977 }
14978
14979 return 1;
14980 }
14981
14982 /* Read the given DW_AT_subrange DIE. */
14983
14984 static struct type *
14985 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
14986 {
14987 struct type *base_type, *orig_base_type;
14988 struct type *range_type;
14989 struct attribute *attr;
14990 struct dynamic_prop low, high;
14991 int low_default_is_valid;
14992 int high_bound_is_count = 0;
14993 const char *name;
14994 LONGEST negative_mask;
14995
14996 orig_base_type = die_type (die, cu);
14997 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
14998 whereas the real type might be. So, we use ORIG_BASE_TYPE when
14999 creating the range type, but we use the result of check_typedef
15000 when examining properties of the type. */
15001 base_type = check_typedef (orig_base_type);
15002
15003 /* The die_type call above may have already set the type for this DIE. */
15004 range_type = get_die_type (die, cu);
15005 if (range_type)
15006 return range_type;
15007
15008 low.kind = PROP_CONST;
15009 high.kind = PROP_CONST;
15010 high.data.const_val = 0;
15011
15012 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15013 omitting DW_AT_lower_bound. */
15014 switch (cu->language)
15015 {
15016 case language_c:
15017 case language_cplus:
15018 low.data.const_val = 0;
15019 low_default_is_valid = 1;
15020 break;
15021 case language_fortran:
15022 low.data.const_val = 1;
15023 low_default_is_valid = 1;
15024 break;
15025 case language_d:
15026 case language_java:
15027 case language_objc:
15028 case language_rust:
15029 low.data.const_val = 0;
15030 low_default_is_valid = (cu->header.version >= 4);
15031 break;
15032 case language_ada:
15033 case language_m2:
15034 case language_pascal:
15035 low.data.const_val = 1;
15036 low_default_is_valid = (cu->header.version >= 4);
15037 break;
15038 default:
15039 low.data.const_val = 0;
15040 low_default_is_valid = 0;
15041 break;
15042 }
15043
15044 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15045 if (attr)
15046 attr_to_dynamic_prop (attr, die, cu, &low);
15047 else if (!low_default_is_valid)
15048 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15049 "- DIE at 0x%x [in module %s]"),
15050 die->offset.sect_off, objfile_name (cu->objfile));
15051
15052 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15053 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15054 {
15055 attr = dwarf2_attr (die, DW_AT_count, cu);
15056 if (attr_to_dynamic_prop (attr, die, cu, &high))
15057 {
15058 /* If bounds are constant do the final calculation here. */
15059 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15060 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15061 else
15062 high_bound_is_count = 1;
15063 }
15064 }
15065
15066 /* Dwarf-2 specifications explicitly allows to create subrange types
15067 without specifying a base type.
15068 In that case, the base type must be set to the type of
15069 the lower bound, upper bound or count, in that order, if any of these
15070 three attributes references an object that has a type.
15071 If no base type is found, the Dwarf-2 specifications say that
15072 a signed integer type of size equal to the size of an address should
15073 be used.
15074 For the following C code: `extern char gdb_int [];'
15075 GCC produces an empty range DIE.
15076 FIXME: muller/2010-05-28: Possible references to object for low bound,
15077 high bound or count are not yet handled by this code. */
15078 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15079 {
15080 struct objfile *objfile = cu->objfile;
15081 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15082 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15083 struct type *int_type = objfile_type (objfile)->builtin_int;
15084
15085 /* Test "int", "long int", and "long long int" objfile types,
15086 and select the first one having a size above or equal to the
15087 architecture address size. */
15088 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15089 base_type = int_type;
15090 else
15091 {
15092 int_type = objfile_type (objfile)->builtin_long;
15093 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15094 base_type = int_type;
15095 else
15096 {
15097 int_type = objfile_type (objfile)->builtin_long_long;
15098 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15099 base_type = int_type;
15100 }
15101 }
15102 }
15103
15104 /* Normally, the DWARF producers are expected to use a signed
15105 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15106 But this is unfortunately not always the case, as witnessed
15107 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15108 is used instead. To work around that ambiguity, we treat
15109 the bounds as signed, and thus sign-extend their values, when
15110 the base type is signed. */
15111 negative_mask =
15112 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15113 if (low.kind == PROP_CONST
15114 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15115 low.data.const_val |= negative_mask;
15116 if (high.kind == PROP_CONST
15117 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15118 high.data.const_val |= negative_mask;
15119
15120 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15121
15122 if (high_bound_is_count)
15123 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15124
15125 /* Ada expects an empty array on no boundary attributes. */
15126 if (attr == NULL && cu->language != language_ada)
15127 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15128
15129 name = dwarf2_name (die, cu);
15130 if (name)
15131 TYPE_NAME (range_type) = name;
15132
15133 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15134 if (attr)
15135 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15136
15137 set_die_type (die, range_type, cu);
15138
15139 /* set_die_type should be already done. */
15140 set_descriptive_type (range_type, die, cu);
15141
15142 return range_type;
15143 }
15144
15145 static struct type *
15146 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15147 {
15148 struct type *type;
15149
15150 /* For now, we only support the C meaning of an unspecified type: void. */
15151
15152 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15153 TYPE_NAME (type) = dwarf2_name (die, cu);
15154
15155 return set_die_type (die, type, cu);
15156 }
15157
15158 /* Read a single die and all its descendents. Set the die's sibling
15159 field to NULL; set other fields in the die correctly, and set all
15160 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15161 location of the info_ptr after reading all of those dies. PARENT
15162 is the parent of the die in question. */
15163
15164 static struct die_info *
15165 read_die_and_children (const struct die_reader_specs *reader,
15166 const gdb_byte *info_ptr,
15167 const gdb_byte **new_info_ptr,
15168 struct die_info *parent)
15169 {
15170 struct die_info *die;
15171 const gdb_byte *cur_ptr;
15172 int has_children;
15173
15174 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15175 if (die == NULL)
15176 {
15177 *new_info_ptr = cur_ptr;
15178 return NULL;
15179 }
15180 store_in_ref_table (die, reader->cu);
15181
15182 if (has_children)
15183 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15184 else
15185 {
15186 die->child = NULL;
15187 *new_info_ptr = cur_ptr;
15188 }
15189
15190 die->sibling = NULL;
15191 die->parent = parent;
15192 return die;
15193 }
15194
15195 /* Read a die, all of its descendents, and all of its siblings; set
15196 all of the fields of all of the dies correctly. Arguments are as
15197 in read_die_and_children. */
15198
15199 static struct die_info *
15200 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15201 const gdb_byte *info_ptr,
15202 const gdb_byte **new_info_ptr,
15203 struct die_info *parent)
15204 {
15205 struct die_info *first_die, *last_sibling;
15206 const gdb_byte *cur_ptr;
15207
15208 cur_ptr = info_ptr;
15209 first_die = last_sibling = NULL;
15210
15211 while (1)
15212 {
15213 struct die_info *die
15214 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15215
15216 if (die == NULL)
15217 {
15218 *new_info_ptr = cur_ptr;
15219 return first_die;
15220 }
15221
15222 if (!first_die)
15223 first_die = die;
15224 else
15225 last_sibling->sibling = die;
15226
15227 last_sibling = die;
15228 }
15229 }
15230
15231 /* Read a die, all of its descendents, and all of its siblings; set
15232 all of the fields of all of the dies correctly. Arguments are as
15233 in read_die_and_children.
15234 This the main entry point for reading a DIE and all its children. */
15235
15236 static struct die_info *
15237 read_die_and_siblings (const struct die_reader_specs *reader,
15238 const gdb_byte *info_ptr,
15239 const gdb_byte **new_info_ptr,
15240 struct die_info *parent)
15241 {
15242 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15243 new_info_ptr, parent);
15244
15245 if (dwarf_die_debug)
15246 {
15247 fprintf_unfiltered (gdb_stdlog,
15248 "Read die from %s@0x%x of %s:\n",
15249 get_section_name (reader->die_section),
15250 (unsigned) (info_ptr - reader->die_section->buffer),
15251 bfd_get_filename (reader->abfd));
15252 dump_die (die, dwarf_die_debug);
15253 }
15254
15255 return die;
15256 }
15257
15258 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15259 attributes.
15260 The caller is responsible for filling in the extra attributes
15261 and updating (*DIEP)->num_attrs.
15262 Set DIEP to point to a newly allocated die with its information,
15263 except for its child, sibling, and parent fields.
15264 Set HAS_CHILDREN to tell whether the die has children or not. */
15265
15266 static const gdb_byte *
15267 read_full_die_1 (const struct die_reader_specs *reader,
15268 struct die_info **diep, const gdb_byte *info_ptr,
15269 int *has_children, int num_extra_attrs)
15270 {
15271 unsigned int abbrev_number, bytes_read, i;
15272 sect_offset offset;
15273 struct abbrev_info *abbrev;
15274 struct die_info *die;
15275 struct dwarf2_cu *cu = reader->cu;
15276 bfd *abfd = reader->abfd;
15277
15278 offset.sect_off = info_ptr - reader->buffer;
15279 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15280 info_ptr += bytes_read;
15281 if (!abbrev_number)
15282 {
15283 *diep = NULL;
15284 *has_children = 0;
15285 return info_ptr;
15286 }
15287
15288 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15289 if (!abbrev)
15290 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15291 abbrev_number,
15292 bfd_get_filename (abfd));
15293
15294 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15295 die->offset = offset;
15296 die->tag = abbrev->tag;
15297 die->abbrev = abbrev_number;
15298
15299 /* Make the result usable.
15300 The caller needs to update num_attrs after adding the extra
15301 attributes. */
15302 die->num_attrs = abbrev->num_attrs;
15303
15304 for (i = 0; i < abbrev->num_attrs; ++i)
15305 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15306 info_ptr);
15307
15308 *diep = die;
15309 *has_children = abbrev->has_children;
15310 return info_ptr;
15311 }
15312
15313 /* Read a die and all its attributes.
15314 Set DIEP to point to a newly allocated die with its information,
15315 except for its child, sibling, and parent fields.
15316 Set HAS_CHILDREN to tell whether the die has children or not. */
15317
15318 static const gdb_byte *
15319 read_full_die (const struct die_reader_specs *reader,
15320 struct die_info **diep, const gdb_byte *info_ptr,
15321 int *has_children)
15322 {
15323 const gdb_byte *result;
15324
15325 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15326
15327 if (dwarf_die_debug)
15328 {
15329 fprintf_unfiltered (gdb_stdlog,
15330 "Read die from %s@0x%x of %s:\n",
15331 get_section_name (reader->die_section),
15332 (unsigned) (info_ptr - reader->die_section->buffer),
15333 bfd_get_filename (reader->abfd));
15334 dump_die (*diep, dwarf_die_debug);
15335 }
15336
15337 return result;
15338 }
15339 \f
15340 /* Abbreviation tables.
15341
15342 In DWARF version 2, the description of the debugging information is
15343 stored in a separate .debug_abbrev section. Before we read any
15344 dies from a section we read in all abbreviations and install them
15345 in a hash table. */
15346
15347 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15348
15349 static struct abbrev_info *
15350 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15351 {
15352 struct abbrev_info *abbrev;
15353
15354 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15355 memset (abbrev, 0, sizeof (struct abbrev_info));
15356
15357 return abbrev;
15358 }
15359
15360 /* Add an abbreviation to the table. */
15361
15362 static void
15363 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15364 unsigned int abbrev_number,
15365 struct abbrev_info *abbrev)
15366 {
15367 unsigned int hash_number;
15368
15369 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15370 abbrev->next = abbrev_table->abbrevs[hash_number];
15371 abbrev_table->abbrevs[hash_number] = abbrev;
15372 }
15373
15374 /* Look up an abbrev in the table.
15375 Returns NULL if the abbrev is not found. */
15376
15377 static struct abbrev_info *
15378 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15379 unsigned int abbrev_number)
15380 {
15381 unsigned int hash_number;
15382 struct abbrev_info *abbrev;
15383
15384 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15385 abbrev = abbrev_table->abbrevs[hash_number];
15386
15387 while (abbrev)
15388 {
15389 if (abbrev->number == abbrev_number)
15390 return abbrev;
15391 abbrev = abbrev->next;
15392 }
15393 return NULL;
15394 }
15395
15396 /* Read in an abbrev table. */
15397
15398 static struct abbrev_table *
15399 abbrev_table_read_table (struct dwarf2_section_info *section,
15400 sect_offset offset)
15401 {
15402 struct objfile *objfile = dwarf2_per_objfile->objfile;
15403 bfd *abfd = get_section_bfd_owner (section);
15404 struct abbrev_table *abbrev_table;
15405 const gdb_byte *abbrev_ptr;
15406 struct abbrev_info *cur_abbrev;
15407 unsigned int abbrev_number, bytes_read, abbrev_name;
15408 unsigned int abbrev_form;
15409 struct attr_abbrev *cur_attrs;
15410 unsigned int allocated_attrs;
15411
15412 abbrev_table = XNEW (struct abbrev_table);
15413 abbrev_table->offset = offset;
15414 obstack_init (&abbrev_table->abbrev_obstack);
15415 abbrev_table->abbrevs =
15416 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15417 ABBREV_HASH_SIZE);
15418 memset (abbrev_table->abbrevs, 0,
15419 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15420
15421 dwarf2_read_section (objfile, section);
15422 abbrev_ptr = section->buffer + offset.sect_off;
15423 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15424 abbrev_ptr += bytes_read;
15425
15426 allocated_attrs = ATTR_ALLOC_CHUNK;
15427 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15428
15429 /* Loop until we reach an abbrev number of 0. */
15430 while (abbrev_number)
15431 {
15432 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15433
15434 /* read in abbrev header */
15435 cur_abbrev->number = abbrev_number;
15436 cur_abbrev->tag
15437 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15438 abbrev_ptr += bytes_read;
15439 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15440 abbrev_ptr += 1;
15441
15442 /* now read in declarations */
15443 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15444 abbrev_ptr += bytes_read;
15445 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15446 abbrev_ptr += bytes_read;
15447 while (abbrev_name)
15448 {
15449 if (cur_abbrev->num_attrs == allocated_attrs)
15450 {
15451 allocated_attrs += ATTR_ALLOC_CHUNK;
15452 cur_attrs
15453 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15454 }
15455
15456 cur_attrs[cur_abbrev->num_attrs].name
15457 = (enum dwarf_attribute) abbrev_name;
15458 cur_attrs[cur_abbrev->num_attrs++].form
15459 = (enum dwarf_form) abbrev_form;
15460 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15461 abbrev_ptr += bytes_read;
15462 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15463 abbrev_ptr += bytes_read;
15464 }
15465
15466 cur_abbrev->attrs =
15467 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15468 cur_abbrev->num_attrs);
15469 memcpy (cur_abbrev->attrs, cur_attrs,
15470 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15471
15472 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15473
15474 /* Get next abbreviation.
15475 Under Irix6 the abbreviations for a compilation unit are not
15476 always properly terminated with an abbrev number of 0.
15477 Exit loop if we encounter an abbreviation which we have
15478 already read (which means we are about to read the abbreviations
15479 for the next compile unit) or if the end of the abbreviation
15480 table is reached. */
15481 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15482 break;
15483 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15484 abbrev_ptr += bytes_read;
15485 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15486 break;
15487 }
15488
15489 xfree (cur_attrs);
15490 return abbrev_table;
15491 }
15492
15493 /* Free the resources held by ABBREV_TABLE. */
15494
15495 static void
15496 abbrev_table_free (struct abbrev_table *abbrev_table)
15497 {
15498 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15499 xfree (abbrev_table);
15500 }
15501
15502 /* Same as abbrev_table_free but as a cleanup.
15503 We pass in a pointer to the pointer to the table so that we can
15504 set the pointer to NULL when we're done. It also simplifies
15505 build_type_psymtabs_1. */
15506
15507 static void
15508 abbrev_table_free_cleanup (void *table_ptr)
15509 {
15510 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15511
15512 if (*abbrev_table_ptr != NULL)
15513 abbrev_table_free (*abbrev_table_ptr);
15514 *abbrev_table_ptr = NULL;
15515 }
15516
15517 /* Read the abbrev table for CU from ABBREV_SECTION. */
15518
15519 static void
15520 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15521 struct dwarf2_section_info *abbrev_section)
15522 {
15523 cu->abbrev_table =
15524 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15525 }
15526
15527 /* Release the memory used by the abbrev table for a compilation unit. */
15528
15529 static void
15530 dwarf2_free_abbrev_table (void *ptr_to_cu)
15531 {
15532 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15533
15534 if (cu->abbrev_table != NULL)
15535 abbrev_table_free (cu->abbrev_table);
15536 /* Set this to NULL so that we SEGV if we try to read it later,
15537 and also because free_comp_unit verifies this is NULL. */
15538 cu->abbrev_table = NULL;
15539 }
15540 \f
15541 /* Returns nonzero if TAG represents a type that we might generate a partial
15542 symbol for. */
15543
15544 static int
15545 is_type_tag_for_partial (int tag)
15546 {
15547 switch (tag)
15548 {
15549 #if 0
15550 /* Some types that would be reasonable to generate partial symbols for,
15551 that we don't at present. */
15552 case DW_TAG_array_type:
15553 case DW_TAG_file_type:
15554 case DW_TAG_ptr_to_member_type:
15555 case DW_TAG_set_type:
15556 case DW_TAG_string_type:
15557 case DW_TAG_subroutine_type:
15558 #endif
15559 case DW_TAG_base_type:
15560 case DW_TAG_class_type:
15561 case DW_TAG_interface_type:
15562 case DW_TAG_enumeration_type:
15563 case DW_TAG_structure_type:
15564 case DW_TAG_subrange_type:
15565 case DW_TAG_typedef:
15566 case DW_TAG_union_type:
15567 return 1;
15568 default:
15569 return 0;
15570 }
15571 }
15572
15573 /* Load all DIEs that are interesting for partial symbols into memory. */
15574
15575 static struct partial_die_info *
15576 load_partial_dies (const struct die_reader_specs *reader,
15577 const gdb_byte *info_ptr, int building_psymtab)
15578 {
15579 struct dwarf2_cu *cu = reader->cu;
15580 struct objfile *objfile = cu->objfile;
15581 struct partial_die_info *part_die;
15582 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15583 struct abbrev_info *abbrev;
15584 unsigned int bytes_read;
15585 unsigned int load_all = 0;
15586 int nesting_level = 1;
15587
15588 parent_die = NULL;
15589 last_die = NULL;
15590
15591 gdb_assert (cu->per_cu != NULL);
15592 if (cu->per_cu->load_all_dies)
15593 load_all = 1;
15594
15595 cu->partial_dies
15596 = htab_create_alloc_ex (cu->header.length / 12,
15597 partial_die_hash,
15598 partial_die_eq,
15599 NULL,
15600 &cu->comp_unit_obstack,
15601 hashtab_obstack_allocate,
15602 dummy_obstack_deallocate);
15603
15604 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15605
15606 while (1)
15607 {
15608 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15609
15610 /* A NULL abbrev means the end of a series of children. */
15611 if (abbrev == NULL)
15612 {
15613 if (--nesting_level == 0)
15614 {
15615 /* PART_DIE was probably the last thing allocated on the
15616 comp_unit_obstack, so we could call obstack_free
15617 here. We don't do that because the waste is small,
15618 and will be cleaned up when we're done with this
15619 compilation unit. This way, we're also more robust
15620 against other users of the comp_unit_obstack. */
15621 return first_die;
15622 }
15623 info_ptr += bytes_read;
15624 last_die = parent_die;
15625 parent_die = parent_die->die_parent;
15626 continue;
15627 }
15628
15629 /* Check for template arguments. We never save these; if
15630 they're seen, we just mark the parent, and go on our way. */
15631 if (parent_die != NULL
15632 && cu->language == language_cplus
15633 && (abbrev->tag == DW_TAG_template_type_param
15634 || abbrev->tag == DW_TAG_template_value_param))
15635 {
15636 parent_die->has_template_arguments = 1;
15637
15638 if (!load_all)
15639 {
15640 /* We don't need a partial DIE for the template argument. */
15641 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15642 continue;
15643 }
15644 }
15645
15646 /* We only recurse into c++ subprograms looking for template arguments.
15647 Skip their other children. */
15648 if (!load_all
15649 && cu->language == language_cplus
15650 && parent_die != NULL
15651 && parent_die->tag == DW_TAG_subprogram)
15652 {
15653 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15654 continue;
15655 }
15656
15657 /* Check whether this DIE is interesting enough to save. Normally
15658 we would not be interested in members here, but there may be
15659 later variables referencing them via DW_AT_specification (for
15660 static members). */
15661 if (!load_all
15662 && !is_type_tag_for_partial (abbrev->tag)
15663 && abbrev->tag != DW_TAG_constant
15664 && abbrev->tag != DW_TAG_enumerator
15665 && abbrev->tag != DW_TAG_subprogram
15666 && abbrev->tag != DW_TAG_lexical_block
15667 && abbrev->tag != DW_TAG_variable
15668 && abbrev->tag != DW_TAG_namespace
15669 && abbrev->tag != DW_TAG_module
15670 && abbrev->tag != DW_TAG_member
15671 && abbrev->tag != DW_TAG_imported_unit
15672 && abbrev->tag != DW_TAG_imported_declaration)
15673 {
15674 /* Otherwise we skip to the next sibling, if any. */
15675 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15676 continue;
15677 }
15678
15679 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15680 info_ptr);
15681
15682 /* This two-pass algorithm for processing partial symbols has a
15683 high cost in cache pressure. Thus, handle some simple cases
15684 here which cover the majority of C partial symbols. DIEs
15685 which neither have specification tags in them, nor could have
15686 specification tags elsewhere pointing at them, can simply be
15687 processed and discarded.
15688
15689 This segment is also optional; scan_partial_symbols and
15690 add_partial_symbol will handle these DIEs if we chain
15691 them in normally. When compilers which do not emit large
15692 quantities of duplicate debug information are more common,
15693 this code can probably be removed. */
15694
15695 /* Any complete simple types at the top level (pretty much all
15696 of them, for a language without namespaces), can be processed
15697 directly. */
15698 if (parent_die == NULL
15699 && part_die->has_specification == 0
15700 && part_die->is_declaration == 0
15701 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15702 || part_die->tag == DW_TAG_base_type
15703 || part_die->tag == DW_TAG_subrange_type))
15704 {
15705 if (building_psymtab && part_die->name != NULL)
15706 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15707 VAR_DOMAIN, LOC_TYPEDEF,
15708 &objfile->static_psymbols,
15709 0, cu->language, objfile);
15710 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15711 continue;
15712 }
15713
15714 /* The exception for DW_TAG_typedef with has_children above is
15715 a workaround of GCC PR debug/47510. In the case of this complaint
15716 type_name_no_tag_or_error will error on such types later.
15717
15718 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15719 it could not find the child DIEs referenced later, this is checked
15720 above. In correct DWARF DW_TAG_typedef should have no children. */
15721
15722 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15723 complaint (&symfile_complaints,
15724 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15725 "- DIE at 0x%x [in module %s]"),
15726 part_die->offset.sect_off, objfile_name (objfile));
15727
15728 /* If we're at the second level, and we're an enumerator, and
15729 our parent has no specification (meaning possibly lives in a
15730 namespace elsewhere), then we can add the partial symbol now
15731 instead of queueing it. */
15732 if (part_die->tag == DW_TAG_enumerator
15733 && parent_die != NULL
15734 && parent_die->die_parent == NULL
15735 && parent_die->tag == DW_TAG_enumeration_type
15736 && parent_die->has_specification == 0)
15737 {
15738 if (part_die->name == NULL)
15739 complaint (&symfile_complaints,
15740 _("malformed enumerator DIE ignored"));
15741 else if (building_psymtab)
15742 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15743 VAR_DOMAIN, LOC_CONST,
15744 (cu->language == language_cplus
15745 || cu->language == language_java)
15746 ? &objfile->global_psymbols
15747 : &objfile->static_psymbols,
15748 0, cu->language, objfile);
15749
15750 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15751 continue;
15752 }
15753
15754 /* We'll save this DIE so link it in. */
15755 part_die->die_parent = parent_die;
15756 part_die->die_sibling = NULL;
15757 part_die->die_child = NULL;
15758
15759 if (last_die && last_die == parent_die)
15760 last_die->die_child = part_die;
15761 else if (last_die)
15762 last_die->die_sibling = part_die;
15763
15764 last_die = part_die;
15765
15766 if (first_die == NULL)
15767 first_die = part_die;
15768
15769 /* Maybe add the DIE to the hash table. Not all DIEs that we
15770 find interesting need to be in the hash table, because we
15771 also have the parent/sibling/child chains; only those that we
15772 might refer to by offset later during partial symbol reading.
15773
15774 For now this means things that might have be the target of a
15775 DW_AT_specification, DW_AT_abstract_origin, or
15776 DW_AT_extension. DW_AT_extension will refer only to
15777 namespaces; DW_AT_abstract_origin refers to functions (and
15778 many things under the function DIE, but we do not recurse
15779 into function DIEs during partial symbol reading) and
15780 possibly variables as well; DW_AT_specification refers to
15781 declarations. Declarations ought to have the DW_AT_declaration
15782 flag. It happens that GCC forgets to put it in sometimes, but
15783 only for functions, not for types.
15784
15785 Adding more things than necessary to the hash table is harmless
15786 except for the performance cost. Adding too few will result in
15787 wasted time in find_partial_die, when we reread the compilation
15788 unit with load_all_dies set. */
15789
15790 if (load_all
15791 || abbrev->tag == DW_TAG_constant
15792 || abbrev->tag == DW_TAG_subprogram
15793 || abbrev->tag == DW_TAG_variable
15794 || abbrev->tag == DW_TAG_namespace
15795 || part_die->is_declaration)
15796 {
15797 void **slot;
15798
15799 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
15800 part_die->offset.sect_off, INSERT);
15801 *slot = part_die;
15802 }
15803
15804 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15805
15806 /* For some DIEs we want to follow their children (if any). For C
15807 we have no reason to follow the children of structures; for other
15808 languages we have to, so that we can get at method physnames
15809 to infer fully qualified class names, for DW_AT_specification,
15810 and for C++ template arguments. For C++, we also look one level
15811 inside functions to find template arguments (if the name of the
15812 function does not already contain the template arguments).
15813
15814 For Ada, we need to scan the children of subprograms and lexical
15815 blocks as well because Ada allows the definition of nested
15816 entities that could be interesting for the debugger, such as
15817 nested subprograms for instance. */
15818 if (last_die->has_children
15819 && (load_all
15820 || last_die->tag == DW_TAG_namespace
15821 || last_die->tag == DW_TAG_module
15822 || last_die->tag == DW_TAG_enumeration_type
15823 || (cu->language == language_cplus
15824 && last_die->tag == DW_TAG_subprogram
15825 && (last_die->name == NULL
15826 || strchr (last_die->name, '<') == NULL))
15827 || (cu->language != language_c
15828 && (last_die->tag == DW_TAG_class_type
15829 || last_die->tag == DW_TAG_interface_type
15830 || last_die->tag == DW_TAG_structure_type
15831 || last_die->tag == DW_TAG_union_type))
15832 || (cu->language == language_ada
15833 && (last_die->tag == DW_TAG_subprogram
15834 || last_die->tag == DW_TAG_lexical_block))))
15835 {
15836 nesting_level++;
15837 parent_die = last_die;
15838 continue;
15839 }
15840
15841 /* Otherwise we skip to the next sibling, if any. */
15842 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
15843
15844 /* Back to the top, do it again. */
15845 }
15846 }
15847
15848 /* Read a minimal amount of information into the minimal die structure. */
15849
15850 static const gdb_byte *
15851 read_partial_die (const struct die_reader_specs *reader,
15852 struct partial_die_info *part_die,
15853 struct abbrev_info *abbrev, unsigned int abbrev_len,
15854 const gdb_byte *info_ptr)
15855 {
15856 struct dwarf2_cu *cu = reader->cu;
15857 struct objfile *objfile = cu->objfile;
15858 const gdb_byte *buffer = reader->buffer;
15859 unsigned int i;
15860 struct attribute attr;
15861 int has_low_pc_attr = 0;
15862 int has_high_pc_attr = 0;
15863 int high_pc_relative = 0;
15864
15865 memset (part_die, 0, sizeof (struct partial_die_info));
15866
15867 part_die->offset.sect_off = info_ptr - buffer;
15868
15869 info_ptr += abbrev_len;
15870
15871 if (abbrev == NULL)
15872 return info_ptr;
15873
15874 part_die->tag = abbrev->tag;
15875 part_die->has_children = abbrev->has_children;
15876
15877 for (i = 0; i < abbrev->num_attrs; ++i)
15878 {
15879 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
15880
15881 /* Store the data if it is of an attribute we want to keep in a
15882 partial symbol table. */
15883 switch (attr.name)
15884 {
15885 case DW_AT_name:
15886 switch (part_die->tag)
15887 {
15888 case DW_TAG_compile_unit:
15889 case DW_TAG_partial_unit:
15890 case DW_TAG_type_unit:
15891 /* Compilation units have a DW_AT_name that is a filename, not
15892 a source language identifier. */
15893 case DW_TAG_enumeration_type:
15894 case DW_TAG_enumerator:
15895 /* These tags always have simple identifiers already; no need
15896 to canonicalize them. */
15897 part_die->name = DW_STRING (&attr);
15898 break;
15899 default:
15900 part_die->name
15901 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
15902 &objfile->per_bfd->storage_obstack);
15903 break;
15904 }
15905 break;
15906 case DW_AT_linkage_name:
15907 case DW_AT_MIPS_linkage_name:
15908 /* Note that both forms of linkage name might appear. We
15909 assume they will be the same, and we only store the last
15910 one we see. */
15911 if (cu->language == language_ada)
15912 part_die->name = DW_STRING (&attr);
15913 part_die->linkage_name = DW_STRING (&attr);
15914 break;
15915 case DW_AT_low_pc:
15916 has_low_pc_attr = 1;
15917 part_die->lowpc = attr_value_as_address (&attr);
15918 break;
15919 case DW_AT_high_pc:
15920 has_high_pc_attr = 1;
15921 part_die->highpc = attr_value_as_address (&attr);
15922 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
15923 high_pc_relative = 1;
15924 break;
15925 case DW_AT_location:
15926 /* Support the .debug_loc offsets. */
15927 if (attr_form_is_block (&attr))
15928 {
15929 part_die->d.locdesc = DW_BLOCK (&attr);
15930 }
15931 else if (attr_form_is_section_offset (&attr))
15932 {
15933 dwarf2_complex_location_expr_complaint ();
15934 }
15935 else
15936 {
15937 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15938 "partial symbol information");
15939 }
15940 break;
15941 case DW_AT_external:
15942 part_die->is_external = DW_UNSND (&attr);
15943 break;
15944 case DW_AT_declaration:
15945 part_die->is_declaration = DW_UNSND (&attr);
15946 break;
15947 case DW_AT_type:
15948 part_die->has_type = 1;
15949 break;
15950 case DW_AT_abstract_origin:
15951 case DW_AT_specification:
15952 case DW_AT_extension:
15953 part_die->has_specification = 1;
15954 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
15955 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15956 || cu->per_cu->is_dwz);
15957 break;
15958 case DW_AT_sibling:
15959 /* Ignore absolute siblings, they might point outside of
15960 the current compile unit. */
15961 if (attr.form == DW_FORM_ref_addr)
15962 complaint (&symfile_complaints,
15963 _("ignoring absolute DW_AT_sibling"));
15964 else
15965 {
15966 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
15967 const gdb_byte *sibling_ptr = buffer + off;
15968
15969 if (sibling_ptr < info_ptr)
15970 complaint (&symfile_complaints,
15971 _("DW_AT_sibling points backwards"));
15972 else if (sibling_ptr > reader->buffer_end)
15973 dwarf2_section_buffer_overflow_complaint (reader->die_section);
15974 else
15975 part_die->sibling = sibling_ptr;
15976 }
15977 break;
15978 case DW_AT_byte_size:
15979 part_die->has_byte_size = 1;
15980 break;
15981 case DW_AT_const_value:
15982 part_die->has_const_value = 1;
15983 break;
15984 case DW_AT_calling_convention:
15985 /* DWARF doesn't provide a way to identify a program's source-level
15986 entry point. DW_AT_calling_convention attributes are only meant
15987 to describe functions' calling conventions.
15988
15989 However, because it's a necessary piece of information in
15990 Fortran, and because DW_CC_program is the only piece of debugging
15991 information whose definition refers to a 'main program' at all,
15992 several compilers have begun marking Fortran main programs with
15993 DW_CC_program --- even when those functions use the standard
15994 calling conventions.
15995
15996 So until DWARF specifies a way to provide this information and
15997 compilers pick up the new representation, we'll support this
15998 practice. */
15999 if (DW_UNSND (&attr) == DW_CC_program
16000 && cu->language == language_fortran
16001 && part_die->name != NULL)
16002 set_objfile_main_name (objfile, part_die->name, language_fortran);
16003 break;
16004 case DW_AT_inline:
16005 if (DW_UNSND (&attr) == DW_INL_inlined
16006 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16007 part_die->may_be_inlined = 1;
16008 break;
16009
16010 case DW_AT_import:
16011 if (part_die->tag == DW_TAG_imported_unit)
16012 {
16013 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
16014 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16015 || cu->per_cu->is_dwz);
16016 }
16017 break;
16018
16019 default:
16020 break;
16021 }
16022 }
16023
16024 if (high_pc_relative)
16025 part_die->highpc += part_die->lowpc;
16026
16027 if (has_low_pc_attr && has_high_pc_attr)
16028 {
16029 /* When using the GNU linker, .gnu.linkonce. sections are used to
16030 eliminate duplicate copies of functions and vtables and such.
16031 The linker will arbitrarily choose one and discard the others.
16032 The AT_*_pc values for such functions refer to local labels in
16033 these sections. If the section from that file was discarded, the
16034 labels are not in the output, so the relocs get a value of 0.
16035 If this is a discarded function, mark the pc bounds as invalid,
16036 so that GDB will ignore it. */
16037 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16038 {
16039 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16040
16041 complaint (&symfile_complaints,
16042 _("DW_AT_low_pc %s is zero "
16043 "for DIE at 0x%x [in module %s]"),
16044 paddress (gdbarch, part_die->lowpc),
16045 part_die->offset.sect_off, objfile_name (objfile));
16046 }
16047 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16048 else if (part_die->lowpc >= part_die->highpc)
16049 {
16050 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16051
16052 complaint (&symfile_complaints,
16053 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16054 "for DIE at 0x%x [in module %s]"),
16055 paddress (gdbarch, part_die->lowpc),
16056 paddress (gdbarch, part_die->highpc),
16057 part_die->offset.sect_off, objfile_name (objfile));
16058 }
16059 else
16060 part_die->has_pc_info = 1;
16061 }
16062
16063 return info_ptr;
16064 }
16065
16066 /* Find a cached partial DIE at OFFSET in CU. */
16067
16068 static struct partial_die_info *
16069 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
16070 {
16071 struct partial_die_info *lookup_die = NULL;
16072 struct partial_die_info part_die;
16073
16074 part_die.offset = offset;
16075 lookup_die = ((struct partial_die_info *)
16076 htab_find_with_hash (cu->partial_dies, &part_die,
16077 offset.sect_off));
16078
16079 return lookup_die;
16080 }
16081
16082 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16083 except in the case of .debug_types DIEs which do not reference
16084 outside their CU (they do however referencing other types via
16085 DW_FORM_ref_sig8). */
16086
16087 static struct partial_die_info *
16088 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
16089 {
16090 struct objfile *objfile = cu->objfile;
16091 struct dwarf2_per_cu_data *per_cu = NULL;
16092 struct partial_die_info *pd = NULL;
16093
16094 if (offset_in_dwz == cu->per_cu->is_dwz
16095 && offset_in_cu_p (&cu->header, offset))
16096 {
16097 pd = find_partial_die_in_comp_unit (offset, cu);
16098 if (pd != NULL)
16099 return pd;
16100 /* We missed recording what we needed.
16101 Load all dies and try again. */
16102 per_cu = cu->per_cu;
16103 }
16104 else
16105 {
16106 /* TUs don't reference other CUs/TUs (except via type signatures). */
16107 if (cu->per_cu->is_debug_types)
16108 {
16109 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16110 " external reference to offset 0x%lx [in module %s].\n"),
16111 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16112 bfd_get_filename (objfile->obfd));
16113 }
16114 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16115 objfile);
16116
16117 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16118 load_partial_comp_unit (per_cu);
16119
16120 per_cu->cu->last_used = 0;
16121 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16122 }
16123
16124 /* If we didn't find it, and not all dies have been loaded,
16125 load them all and try again. */
16126
16127 if (pd == NULL && per_cu->load_all_dies == 0)
16128 {
16129 per_cu->load_all_dies = 1;
16130
16131 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16132 THIS_CU->cu may already be in use. So we can't just free it and
16133 replace its DIEs with the ones we read in. Instead, we leave those
16134 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16135 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16136 set. */
16137 load_partial_comp_unit (per_cu);
16138
16139 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16140 }
16141
16142 if (pd == NULL)
16143 internal_error (__FILE__, __LINE__,
16144 _("could not find partial DIE 0x%x "
16145 "in cache [from module %s]\n"),
16146 offset.sect_off, bfd_get_filename (objfile->obfd));
16147 return pd;
16148 }
16149
16150 /* See if we can figure out if the class lives in a namespace. We do
16151 this by looking for a member function; its demangled name will
16152 contain namespace info, if there is any. */
16153
16154 static void
16155 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16156 struct dwarf2_cu *cu)
16157 {
16158 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16159 what template types look like, because the demangler
16160 frequently doesn't give the same name as the debug info. We
16161 could fix this by only using the demangled name to get the
16162 prefix (but see comment in read_structure_type). */
16163
16164 struct partial_die_info *real_pdi;
16165 struct partial_die_info *child_pdi;
16166
16167 /* If this DIE (this DIE's specification, if any) has a parent, then
16168 we should not do this. We'll prepend the parent's fully qualified
16169 name when we create the partial symbol. */
16170
16171 real_pdi = struct_pdi;
16172 while (real_pdi->has_specification)
16173 real_pdi = find_partial_die (real_pdi->spec_offset,
16174 real_pdi->spec_is_dwz, cu);
16175
16176 if (real_pdi->die_parent != NULL)
16177 return;
16178
16179 for (child_pdi = struct_pdi->die_child;
16180 child_pdi != NULL;
16181 child_pdi = child_pdi->die_sibling)
16182 {
16183 if (child_pdi->tag == DW_TAG_subprogram
16184 && child_pdi->linkage_name != NULL)
16185 {
16186 char *actual_class_name
16187 = language_class_name_from_physname (cu->language_defn,
16188 child_pdi->linkage_name);
16189 if (actual_class_name != NULL)
16190 {
16191 struct_pdi->name
16192 = ((const char *)
16193 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16194 actual_class_name,
16195 strlen (actual_class_name)));
16196 xfree (actual_class_name);
16197 }
16198 break;
16199 }
16200 }
16201 }
16202
16203 /* Adjust PART_DIE before generating a symbol for it. This function
16204 may set the is_external flag or change the DIE's name. */
16205
16206 static void
16207 fixup_partial_die (struct partial_die_info *part_die,
16208 struct dwarf2_cu *cu)
16209 {
16210 /* Once we've fixed up a die, there's no point in doing so again.
16211 This also avoids a memory leak if we were to call
16212 guess_partial_die_structure_name multiple times. */
16213 if (part_die->fixup_called)
16214 return;
16215
16216 /* If we found a reference attribute and the DIE has no name, try
16217 to find a name in the referred to DIE. */
16218
16219 if (part_die->name == NULL && part_die->has_specification)
16220 {
16221 struct partial_die_info *spec_die;
16222
16223 spec_die = find_partial_die (part_die->spec_offset,
16224 part_die->spec_is_dwz, cu);
16225
16226 fixup_partial_die (spec_die, cu);
16227
16228 if (spec_die->name)
16229 {
16230 part_die->name = spec_die->name;
16231
16232 /* Copy DW_AT_external attribute if it is set. */
16233 if (spec_die->is_external)
16234 part_die->is_external = spec_die->is_external;
16235 }
16236 }
16237
16238 /* Set default names for some unnamed DIEs. */
16239
16240 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16241 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16242
16243 /* If there is no parent die to provide a namespace, and there are
16244 children, see if we can determine the namespace from their linkage
16245 name. */
16246 if (cu->language == language_cplus
16247 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16248 && part_die->die_parent == NULL
16249 && part_die->has_children
16250 && (part_die->tag == DW_TAG_class_type
16251 || part_die->tag == DW_TAG_structure_type
16252 || part_die->tag == DW_TAG_union_type))
16253 guess_partial_die_structure_name (part_die, cu);
16254
16255 /* GCC might emit a nameless struct or union that has a linkage
16256 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16257 if (part_die->name == NULL
16258 && (part_die->tag == DW_TAG_class_type
16259 || part_die->tag == DW_TAG_interface_type
16260 || part_die->tag == DW_TAG_structure_type
16261 || part_die->tag == DW_TAG_union_type)
16262 && part_die->linkage_name != NULL)
16263 {
16264 char *demangled;
16265
16266 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16267 if (demangled)
16268 {
16269 const char *base;
16270
16271 /* Strip any leading namespaces/classes, keep only the base name.
16272 DW_AT_name for named DIEs does not contain the prefixes. */
16273 base = strrchr (demangled, ':');
16274 if (base && base > demangled && base[-1] == ':')
16275 base++;
16276 else
16277 base = demangled;
16278
16279 part_die->name
16280 = ((const char *)
16281 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16282 base, strlen (base)));
16283 xfree (demangled);
16284 }
16285 }
16286
16287 part_die->fixup_called = 1;
16288 }
16289
16290 /* Read an attribute value described by an attribute form. */
16291
16292 static const gdb_byte *
16293 read_attribute_value (const struct die_reader_specs *reader,
16294 struct attribute *attr, unsigned form,
16295 const gdb_byte *info_ptr)
16296 {
16297 struct dwarf2_cu *cu = reader->cu;
16298 struct objfile *objfile = cu->objfile;
16299 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16300 bfd *abfd = reader->abfd;
16301 struct comp_unit_head *cu_header = &cu->header;
16302 unsigned int bytes_read;
16303 struct dwarf_block *blk;
16304
16305 attr->form = (enum dwarf_form) form;
16306 switch (form)
16307 {
16308 case DW_FORM_ref_addr:
16309 if (cu->header.version == 2)
16310 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16311 else
16312 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16313 &cu->header, &bytes_read);
16314 info_ptr += bytes_read;
16315 break;
16316 case DW_FORM_GNU_ref_alt:
16317 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16318 info_ptr += bytes_read;
16319 break;
16320 case DW_FORM_addr:
16321 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16322 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16323 info_ptr += bytes_read;
16324 break;
16325 case DW_FORM_block2:
16326 blk = dwarf_alloc_block (cu);
16327 blk->size = read_2_bytes (abfd, info_ptr);
16328 info_ptr += 2;
16329 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16330 info_ptr += blk->size;
16331 DW_BLOCK (attr) = blk;
16332 break;
16333 case DW_FORM_block4:
16334 blk = dwarf_alloc_block (cu);
16335 blk->size = read_4_bytes (abfd, info_ptr);
16336 info_ptr += 4;
16337 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16338 info_ptr += blk->size;
16339 DW_BLOCK (attr) = blk;
16340 break;
16341 case DW_FORM_data2:
16342 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16343 info_ptr += 2;
16344 break;
16345 case DW_FORM_data4:
16346 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16347 info_ptr += 4;
16348 break;
16349 case DW_FORM_data8:
16350 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16351 info_ptr += 8;
16352 break;
16353 case DW_FORM_sec_offset:
16354 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16355 info_ptr += bytes_read;
16356 break;
16357 case DW_FORM_string:
16358 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16359 DW_STRING_IS_CANONICAL (attr) = 0;
16360 info_ptr += bytes_read;
16361 break;
16362 case DW_FORM_strp:
16363 if (!cu->per_cu->is_dwz)
16364 {
16365 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16366 &bytes_read);
16367 DW_STRING_IS_CANONICAL (attr) = 0;
16368 info_ptr += bytes_read;
16369 break;
16370 }
16371 /* FALLTHROUGH */
16372 case DW_FORM_GNU_strp_alt:
16373 {
16374 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16375 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16376 &bytes_read);
16377
16378 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16379 DW_STRING_IS_CANONICAL (attr) = 0;
16380 info_ptr += bytes_read;
16381 }
16382 break;
16383 case DW_FORM_exprloc:
16384 case DW_FORM_block:
16385 blk = dwarf_alloc_block (cu);
16386 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16387 info_ptr += bytes_read;
16388 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16389 info_ptr += blk->size;
16390 DW_BLOCK (attr) = blk;
16391 break;
16392 case DW_FORM_block1:
16393 blk = dwarf_alloc_block (cu);
16394 blk->size = read_1_byte (abfd, info_ptr);
16395 info_ptr += 1;
16396 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16397 info_ptr += blk->size;
16398 DW_BLOCK (attr) = blk;
16399 break;
16400 case DW_FORM_data1:
16401 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16402 info_ptr += 1;
16403 break;
16404 case DW_FORM_flag:
16405 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16406 info_ptr += 1;
16407 break;
16408 case DW_FORM_flag_present:
16409 DW_UNSND (attr) = 1;
16410 break;
16411 case DW_FORM_sdata:
16412 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16413 info_ptr += bytes_read;
16414 break;
16415 case DW_FORM_udata:
16416 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16417 info_ptr += bytes_read;
16418 break;
16419 case DW_FORM_ref1:
16420 DW_UNSND (attr) = (cu->header.offset.sect_off
16421 + read_1_byte (abfd, info_ptr));
16422 info_ptr += 1;
16423 break;
16424 case DW_FORM_ref2:
16425 DW_UNSND (attr) = (cu->header.offset.sect_off
16426 + read_2_bytes (abfd, info_ptr));
16427 info_ptr += 2;
16428 break;
16429 case DW_FORM_ref4:
16430 DW_UNSND (attr) = (cu->header.offset.sect_off
16431 + read_4_bytes (abfd, info_ptr));
16432 info_ptr += 4;
16433 break;
16434 case DW_FORM_ref8:
16435 DW_UNSND (attr) = (cu->header.offset.sect_off
16436 + read_8_bytes (abfd, info_ptr));
16437 info_ptr += 8;
16438 break;
16439 case DW_FORM_ref_sig8:
16440 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16441 info_ptr += 8;
16442 break;
16443 case DW_FORM_ref_udata:
16444 DW_UNSND (attr) = (cu->header.offset.sect_off
16445 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16446 info_ptr += bytes_read;
16447 break;
16448 case DW_FORM_indirect:
16449 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16450 info_ptr += bytes_read;
16451 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
16452 break;
16453 case DW_FORM_GNU_addr_index:
16454 if (reader->dwo_file == NULL)
16455 {
16456 /* For now flag a hard error.
16457 Later we can turn this into a complaint. */
16458 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16459 dwarf_form_name (form),
16460 bfd_get_filename (abfd));
16461 }
16462 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16463 info_ptr += bytes_read;
16464 break;
16465 case DW_FORM_GNU_str_index:
16466 if (reader->dwo_file == NULL)
16467 {
16468 /* For now flag a hard error.
16469 Later we can turn this into a complaint if warranted. */
16470 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16471 dwarf_form_name (form),
16472 bfd_get_filename (abfd));
16473 }
16474 {
16475 ULONGEST str_index =
16476 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16477
16478 DW_STRING (attr) = read_str_index (reader, str_index);
16479 DW_STRING_IS_CANONICAL (attr) = 0;
16480 info_ptr += bytes_read;
16481 }
16482 break;
16483 default:
16484 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16485 dwarf_form_name (form),
16486 bfd_get_filename (abfd));
16487 }
16488
16489 /* Super hack. */
16490 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16491 attr->form = DW_FORM_GNU_ref_alt;
16492
16493 /* We have seen instances where the compiler tried to emit a byte
16494 size attribute of -1 which ended up being encoded as an unsigned
16495 0xffffffff. Although 0xffffffff is technically a valid size value,
16496 an object of this size seems pretty unlikely so we can relatively
16497 safely treat these cases as if the size attribute was invalid and
16498 treat them as zero by default. */
16499 if (attr->name == DW_AT_byte_size
16500 && form == DW_FORM_data4
16501 && DW_UNSND (attr) >= 0xffffffff)
16502 {
16503 complaint
16504 (&symfile_complaints,
16505 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16506 hex_string (DW_UNSND (attr)));
16507 DW_UNSND (attr) = 0;
16508 }
16509
16510 return info_ptr;
16511 }
16512
16513 /* Read an attribute described by an abbreviated attribute. */
16514
16515 static const gdb_byte *
16516 read_attribute (const struct die_reader_specs *reader,
16517 struct attribute *attr, struct attr_abbrev *abbrev,
16518 const gdb_byte *info_ptr)
16519 {
16520 attr->name = abbrev->name;
16521 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
16522 }
16523
16524 /* Read dwarf information from a buffer. */
16525
16526 static unsigned int
16527 read_1_byte (bfd *abfd, const gdb_byte *buf)
16528 {
16529 return bfd_get_8 (abfd, buf);
16530 }
16531
16532 static int
16533 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16534 {
16535 return bfd_get_signed_8 (abfd, buf);
16536 }
16537
16538 static unsigned int
16539 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16540 {
16541 return bfd_get_16 (abfd, buf);
16542 }
16543
16544 static int
16545 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16546 {
16547 return bfd_get_signed_16 (abfd, buf);
16548 }
16549
16550 static unsigned int
16551 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16552 {
16553 return bfd_get_32 (abfd, buf);
16554 }
16555
16556 static int
16557 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16558 {
16559 return bfd_get_signed_32 (abfd, buf);
16560 }
16561
16562 static ULONGEST
16563 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16564 {
16565 return bfd_get_64 (abfd, buf);
16566 }
16567
16568 static CORE_ADDR
16569 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16570 unsigned int *bytes_read)
16571 {
16572 struct comp_unit_head *cu_header = &cu->header;
16573 CORE_ADDR retval = 0;
16574
16575 if (cu_header->signed_addr_p)
16576 {
16577 switch (cu_header->addr_size)
16578 {
16579 case 2:
16580 retval = bfd_get_signed_16 (abfd, buf);
16581 break;
16582 case 4:
16583 retval = bfd_get_signed_32 (abfd, buf);
16584 break;
16585 case 8:
16586 retval = bfd_get_signed_64 (abfd, buf);
16587 break;
16588 default:
16589 internal_error (__FILE__, __LINE__,
16590 _("read_address: bad switch, signed [in module %s]"),
16591 bfd_get_filename (abfd));
16592 }
16593 }
16594 else
16595 {
16596 switch (cu_header->addr_size)
16597 {
16598 case 2:
16599 retval = bfd_get_16 (abfd, buf);
16600 break;
16601 case 4:
16602 retval = bfd_get_32 (abfd, buf);
16603 break;
16604 case 8:
16605 retval = bfd_get_64 (abfd, buf);
16606 break;
16607 default:
16608 internal_error (__FILE__, __LINE__,
16609 _("read_address: bad switch, "
16610 "unsigned [in module %s]"),
16611 bfd_get_filename (abfd));
16612 }
16613 }
16614
16615 *bytes_read = cu_header->addr_size;
16616 return retval;
16617 }
16618
16619 /* Read the initial length from a section. The (draft) DWARF 3
16620 specification allows the initial length to take up either 4 bytes
16621 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16622 bytes describe the length and all offsets will be 8 bytes in length
16623 instead of 4.
16624
16625 An older, non-standard 64-bit format is also handled by this
16626 function. The older format in question stores the initial length
16627 as an 8-byte quantity without an escape value. Lengths greater
16628 than 2^32 aren't very common which means that the initial 4 bytes
16629 is almost always zero. Since a length value of zero doesn't make
16630 sense for the 32-bit format, this initial zero can be considered to
16631 be an escape value which indicates the presence of the older 64-bit
16632 format. As written, the code can't detect (old format) lengths
16633 greater than 4GB. If it becomes necessary to handle lengths
16634 somewhat larger than 4GB, we could allow other small values (such
16635 as the non-sensical values of 1, 2, and 3) to also be used as
16636 escape values indicating the presence of the old format.
16637
16638 The value returned via bytes_read should be used to increment the
16639 relevant pointer after calling read_initial_length().
16640
16641 [ Note: read_initial_length() and read_offset() are based on the
16642 document entitled "DWARF Debugging Information Format", revision
16643 3, draft 8, dated November 19, 2001. This document was obtained
16644 from:
16645
16646 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16647
16648 This document is only a draft and is subject to change. (So beware.)
16649
16650 Details regarding the older, non-standard 64-bit format were
16651 determined empirically by examining 64-bit ELF files produced by
16652 the SGI toolchain on an IRIX 6.5 machine.
16653
16654 - Kevin, July 16, 2002
16655 ] */
16656
16657 static LONGEST
16658 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16659 {
16660 LONGEST length = bfd_get_32 (abfd, buf);
16661
16662 if (length == 0xffffffff)
16663 {
16664 length = bfd_get_64 (abfd, buf + 4);
16665 *bytes_read = 12;
16666 }
16667 else if (length == 0)
16668 {
16669 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16670 length = bfd_get_64 (abfd, buf);
16671 *bytes_read = 8;
16672 }
16673 else
16674 {
16675 *bytes_read = 4;
16676 }
16677
16678 return length;
16679 }
16680
16681 /* Cover function for read_initial_length.
16682 Returns the length of the object at BUF, and stores the size of the
16683 initial length in *BYTES_READ and stores the size that offsets will be in
16684 *OFFSET_SIZE.
16685 If the initial length size is not equivalent to that specified in
16686 CU_HEADER then issue a complaint.
16687 This is useful when reading non-comp-unit headers. */
16688
16689 static LONGEST
16690 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
16691 const struct comp_unit_head *cu_header,
16692 unsigned int *bytes_read,
16693 unsigned int *offset_size)
16694 {
16695 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16696
16697 gdb_assert (cu_header->initial_length_size == 4
16698 || cu_header->initial_length_size == 8
16699 || cu_header->initial_length_size == 12);
16700
16701 if (cu_header->initial_length_size != *bytes_read)
16702 complaint (&symfile_complaints,
16703 _("intermixed 32-bit and 64-bit DWARF sections"));
16704
16705 *offset_size = (*bytes_read == 4) ? 4 : 8;
16706 return length;
16707 }
16708
16709 /* Read an offset from the data stream. The size of the offset is
16710 given by cu_header->offset_size. */
16711
16712 static LONGEST
16713 read_offset (bfd *abfd, const gdb_byte *buf,
16714 const struct comp_unit_head *cu_header,
16715 unsigned int *bytes_read)
16716 {
16717 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
16718
16719 *bytes_read = cu_header->offset_size;
16720 return offset;
16721 }
16722
16723 /* Read an offset from the data stream. */
16724
16725 static LONGEST
16726 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
16727 {
16728 LONGEST retval = 0;
16729
16730 switch (offset_size)
16731 {
16732 case 4:
16733 retval = bfd_get_32 (abfd, buf);
16734 break;
16735 case 8:
16736 retval = bfd_get_64 (abfd, buf);
16737 break;
16738 default:
16739 internal_error (__FILE__, __LINE__,
16740 _("read_offset_1: bad switch [in module %s]"),
16741 bfd_get_filename (abfd));
16742 }
16743
16744 return retval;
16745 }
16746
16747 static const gdb_byte *
16748 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
16749 {
16750 /* If the size of a host char is 8 bits, we can return a pointer
16751 to the buffer, otherwise we have to copy the data to a buffer
16752 allocated on the temporary obstack. */
16753 gdb_assert (HOST_CHAR_BIT == 8);
16754 return buf;
16755 }
16756
16757 static const char *
16758 read_direct_string (bfd *abfd, const gdb_byte *buf,
16759 unsigned int *bytes_read_ptr)
16760 {
16761 /* If the size of a host char is 8 bits, we can return a pointer
16762 to the string, otherwise we have to copy the string to a buffer
16763 allocated on the temporary obstack. */
16764 gdb_assert (HOST_CHAR_BIT == 8);
16765 if (*buf == '\0')
16766 {
16767 *bytes_read_ptr = 1;
16768 return NULL;
16769 }
16770 *bytes_read_ptr = strlen ((const char *) buf) + 1;
16771 return (const char *) buf;
16772 }
16773
16774 static const char *
16775 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
16776 {
16777 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
16778 if (dwarf2_per_objfile->str.buffer == NULL)
16779 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
16780 bfd_get_filename (abfd));
16781 if (str_offset >= dwarf2_per_objfile->str.size)
16782 error (_("DW_FORM_strp pointing outside of "
16783 ".debug_str section [in module %s]"),
16784 bfd_get_filename (abfd));
16785 gdb_assert (HOST_CHAR_BIT == 8);
16786 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
16787 return NULL;
16788 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
16789 }
16790
16791 /* Read a string at offset STR_OFFSET in the .debug_str section from
16792 the .dwz file DWZ. Throw an error if the offset is too large. If
16793 the string consists of a single NUL byte, return NULL; otherwise
16794 return a pointer to the string. */
16795
16796 static const char *
16797 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
16798 {
16799 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
16800
16801 if (dwz->str.buffer == NULL)
16802 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
16803 "section [in module %s]"),
16804 bfd_get_filename (dwz->dwz_bfd));
16805 if (str_offset >= dwz->str.size)
16806 error (_("DW_FORM_GNU_strp_alt pointing outside of "
16807 ".debug_str section [in module %s]"),
16808 bfd_get_filename (dwz->dwz_bfd));
16809 gdb_assert (HOST_CHAR_BIT == 8);
16810 if (dwz->str.buffer[str_offset] == '\0')
16811 return NULL;
16812 return (const char *) (dwz->str.buffer + str_offset);
16813 }
16814
16815 static const char *
16816 read_indirect_string (bfd *abfd, const gdb_byte *buf,
16817 const struct comp_unit_head *cu_header,
16818 unsigned int *bytes_read_ptr)
16819 {
16820 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
16821
16822 return read_indirect_string_at_offset (abfd, str_offset);
16823 }
16824
16825 static ULONGEST
16826 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
16827 unsigned int *bytes_read_ptr)
16828 {
16829 ULONGEST result;
16830 unsigned int num_read;
16831 int shift;
16832 unsigned char byte;
16833
16834 result = 0;
16835 shift = 0;
16836 num_read = 0;
16837 while (1)
16838 {
16839 byte = bfd_get_8 (abfd, buf);
16840 buf++;
16841 num_read++;
16842 result |= ((ULONGEST) (byte & 127) << shift);
16843 if ((byte & 128) == 0)
16844 {
16845 break;
16846 }
16847 shift += 7;
16848 }
16849 *bytes_read_ptr = num_read;
16850 return result;
16851 }
16852
16853 static LONGEST
16854 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
16855 unsigned int *bytes_read_ptr)
16856 {
16857 LONGEST result;
16858 int shift, num_read;
16859 unsigned char byte;
16860
16861 result = 0;
16862 shift = 0;
16863 num_read = 0;
16864 while (1)
16865 {
16866 byte = bfd_get_8 (abfd, buf);
16867 buf++;
16868 num_read++;
16869 result |= ((LONGEST) (byte & 127) << shift);
16870 shift += 7;
16871 if ((byte & 128) == 0)
16872 {
16873 break;
16874 }
16875 }
16876 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
16877 result |= -(((LONGEST) 1) << shift);
16878 *bytes_read_ptr = num_read;
16879 return result;
16880 }
16881
16882 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
16883 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
16884 ADDR_SIZE is the size of addresses from the CU header. */
16885
16886 static CORE_ADDR
16887 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
16888 {
16889 struct objfile *objfile = dwarf2_per_objfile->objfile;
16890 bfd *abfd = objfile->obfd;
16891 const gdb_byte *info_ptr;
16892
16893 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
16894 if (dwarf2_per_objfile->addr.buffer == NULL)
16895 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
16896 objfile_name (objfile));
16897 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
16898 error (_("DW_FORM_addr_index pointing outside of "
16899 ".debug_addr section [in module %s]"),
16900 objfile_name (objfile));
16901 info_ptr = (dwarf2_per_objfile->addr.buffer
16902 + addr_base + addr_index * addr_size);
16903 if (addr_size == 4)
16904 return bfd_get_32 (abfd, info_ptr);
16905 else
16906 return bfd_get_64 (abfd, info_ptr);
16907 }
16908
16909 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
16910
16911 static CORE_ADDR
16912 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
16913 {
16914 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
16915 }
16916
16917 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
16918
16919 static CORE_ADDR
16920 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
16921 unsigned int *bytes_read)
16922 {
16923 bfd *abfd = cu->objfile->obfd;
16924 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
16925
16926 return read_addr_index (cu, addr_index);
16927 }
16928
16929 /* Data structure to pass results from dwarf2_read_addr_index_reader
16930 back to dwarf2_read_addr_index. */
16931
16932 struct dwarf2_read_addr_index_data
16933 {
16934 ULONGEST addr_base;
16935 int addr_size;
16936 };
16937
16938 /* die_reader_func for dwarf2_read_addr_index. */
16939
16940 static void
16941 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
16942 const gdb_byte *info_ptr,
16943 struct die_info *comp_unit_die,
16944 int has_children,
16945 void *data)
16946 {
16947 struct dwarf2_cu *cu = reader->cu;
16948 struct dwarf2_read_addr_index_data *aidata =
16949 (struct dwarf2_read_addr_index_data *) data;
16950
16951 aidata->addr_base = cu->addr_base;
16952 aidata->addr_size = cu->header.addr_size;
16953 }
16954
16955 /* Given an index in .debug_addr, fetch the value.
16956 NOTE: This can be called during dwarf expression evaluation,
16957 long after the debug information has been read, and thus per_cu->cu
16958 may no longer exist. */
16959
16960 CORE_ADDR
16961 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
16962 unsigned int addr_index)
16963 {
16964 struct objfile *objfile = per_cu->objfile;
16965 struct dwarf2_cu *cu = per_cu->cu;
16966 ULONGEST addr_base;
16967 int addr_size;
16968
16969 /* This is intended to be called from outside this file. */
16970 dw2_setup (objfile);
16971
16972 /* We need addr_base and addr_size.
16973 If we don't have PER_CU->cu, we have to get it.
16974 Nasty, but the alternative is storing the needed info in PER_CU,
16975 which at this point doesn't seem justified: it's not clear how frequently
16976 it would get used and it would increase the size of every PER_CU.
16977 Entry points like dwarf2_per_cu_addr_size do a similar thing
16978 so we're not in uncharted territory here.
16979 Alas we need to be a bit more complicated as addr_base is contained
16980 in the DIE.
16981
16982 We don't need to read the entire CU(/TU).
16983 We just need the header and top level die.
16984
16985 IWBN to use the aging mechanism to let us lazily later discard the CU.
16986 For now we skip this optimization. */
16987
16988 if (cu != NULL)
16989 {
16990 addr_base = cu->addr_base;
16991 addr_size = cu->header.addr_size;
16992 }
16993 else
16994 {
16995 struct dwarf2_read_addr_index_data aidata;
16996
16997 /* Note: We can't use init_cutu_and_read_dies_simple here,
16998 we need addr_base. */
16999 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17000 dwarf2_read_addr_index_reader, &aidata);
17001 addr_base = aidata.addr_base;
17002 addr_size = aidata.addr_size;
17003 }
17004
17005 return read_addr_index_1 (addr_index, addr_base, addr_size);
17006 }
17007
17008 /* Given a DW_FORM_GNU_str_index, fetch the string.
17009 This is only used by the Fission support. */
17010
17011 static const char *
17012 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17013 {
17014 struct objfile *objfile = dwarf2_per_objfile->objfile;
17015 const char *objf_name = objfile_name (objfile);
17016 bfd *abfd = objfile->obfd;
17017 struct dwarf2_cu *cu = reader->cu;
17018 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17019 struct dwarf2_section_info *str_offsets_section =
17020 &reader->dwo_file->sections.str_offsets;
17021 const gdb_byte *info_ptr;
17022 ULONGEST str_offset;
17023 static const char form_name[] = "DW_FORM_GNU_str_index";
17024
17025 dwarf2_read_section (objfile, str_section);
17026 dwarf2_read_section (objfile, str_offsets_section);
17027 if (str_section->buffer == NULL)
17028 error (_("%s used without .debug_str.dwo section"
17029 " in CU at offset 0x%lx [in module %s]"),
17030 form_name, (long) cu->header.offset.sect_off, objf_name);
17031 if (str_offsets_section->buffer == NULL)
17032 error (_("%s used without .debug_str_offsets.dwo section"
17033 " in CU at offset 0x%lx [in module %s]"),
17034 form_name, (long) cu->header.offset.sect_off, objf_name);
17035 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17036 error (_("%s pointing outside of .debug_str_offsets.dwo"
17037 " section in CU at offset 0x%lx [in module %s]"),
17038 form_name, (long) cu->header.offset.sect_off, objf_name);
17039 info_ptr = (str_offsets_section->buffer
17040 + str_index * cu->header.offset_size);
17041 if (cu->header.offset_size == 4)
17042 str_offset = bfd_get_32 (abfd, info_ptr);
17043 else
17044 str_offset = bfd_get_64 (abfd, info_ptr);
17045 if (str_offset >= str_section->size)
17046 error (_("Offset from %s pointing outside of"
17047 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
17048 form_name, (long) cu->header.offset.sect_off, objf_name);
17049 return (const char *) (str_section->buffer + str_offset);
17050 }
17051
17052 /* Return the length of an LEB128 number in BUF. */
17053
17054 static int
17055 leb128_size (const gdb_byte *buf)
17056 {
17057 const gdb_byte *begin = buf;
17058 gdb_byte byte;
17059
17060 while (1)
17061 {
17062 byte = *buf++;
17063 if ((byte & 128) == 0)
17064 return buf - begin;
17065 }
17066 }
17067
17068 static void
17069 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17070 {
17071 switch (lang)
17072 {
17073 case DW_LANG_C89:
17074 case DW_LANG_C99:
17075 case DW_LANG_C11:
17076 case DW_LANG_C:
17077 case DW_LANG_UPC:
17078 cu->language = language_c;
17079 break;
17080 case DW_LANG_C_plus_plus:
17081 case DW_LANG_C_plus_plus_11:
17082 case DW_LANG_C_plus_plus_14:
17083 cu->language = language_cplus;
17084 break;
17085 case DW_LANG_D:
17086 cu->language = language_d;
17087 break;
17088 case DW_LANG_Fortran77:
17089 case DW_LANG_Fortran90:
17090 case DW_LANG_Fortran95:
17091 case DW_LANG_Fortran03:
17092 case DW_LANG_Fortran08:
17093 cu->language = language_fortran;
17094 break;
17095 case DW_LANG_Go:
17096 cu->language = language_go;
17097 break;
17098 case DW_LANG_Mips_Assembler:
17099 cu->language = language_asm;
17100 break;
17101 case DW_LANG_Java:
17102 cu->language = language_java;
17103 break;
17104 case DW_LANG_Ada83:
17105 case DW_LANG_Ada95:
17106 cu->language = language_ada;
17107 break;
17108 case DW_LANG_Modula2:
17109 cu->language = language_m2;
17110 break;
17111 case DW_LANG_Pascal83:
17112 cu->language = language_pascal;
17113 break;
17114 case DW_LANG_ObjC:
17115 cu->language = language_objc;
17116 break;
17117 case DW_LANG_Rust:
17118 case DW_LANG_Rust_old:
17119 cu->language = language_rust;
17120 break;
17121 case DW_LANG_Cobol74:
17122 case DW_LANG_Cobol85:
17123 default:
17124 cu->language = language_minimal;
17125 break;
17126 }
17127 cu->language_defn = language_def (cu->language);
17128 }
17129
17130 /* Return the named attribute or NULL if not there. */
17131
17132 static struct attribute *
17133 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17134 {
17135 for (;;)
17136 {
17137 unsigned int i;
17138 struct attribute *spec = NULL;
17139
17140 for (i = 0; i < die->num_attrs; ++i)
17141 {
17142 if (die->attrs[i].name == name)
17143 return &die->attrs[i];
17144 if (die->attrs[i].name == DW_AT_specification
17145 || die->attrs[i].name == DW_AT_abstract_origin)
17146 spec = &die->attrs[i];
17147 }
17148
17149 if (!spec)
17150 break;
17151
17152 die = follow_die_ref (die, spec, &cu);
17153 }
17154
17155 return NULL;
17156 }
17157
17158 /* Return the named attribute or NULL if not there,
17159 but do not follow DW_AT_specification, etc.
17160 This is for use in contexts where we're reading .debug_types dies.
17161 Following DW_AT_specification, DW_AT_abstract_origin will take us
17162 back up the chain, and we want to go down. */
17163
17164 static struct attribute *
17165 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17166 {
17167 unsigned int i;
17168
17169 for (i = 0; i < die->num_attrs; ++i)
17170 if (die->attrs[i].name == name)
17171 return &die->attrs[i];
17172
17173 return NULL;
17174 }
17175
17176 /* Return the string associated with a string-typed attribute, or NULL if it
17177 is either not found or is of an incorrect type. */
17178
17179 static const char *
17180 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17181 {
17182 struct attribute *attr;
17183 const char *str = NULL;
17184
17185 attr = dwarf2_attr (die, name, cu);
17186
17187 if (attr != NULL)
17188 {
17189 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_string
17190 || attr->form == DW_FORM_GNU_strp_alt)
17191 str = DW_STRING (attr);
17192 else
17193 complaint (&symfile_complaints,
17194 _("string type expected for attribute %s for "
17195 "DIE at 0x%x in module %s"),
17196 dwarf_attr_name (name), die->offset.sect_off,
17197 objfile_name (cu->objfile));
17198 }
17199
17200 return str;
17201 }
17202
17203 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17204 and holds a non-zero value. This function should only be used for
17205 DW_FORM_flag or DW_FORM_flag_present attributes. */
17206
17207 static int
17208 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17209 {
17210 struct attribute *attr = dwarf2_attr (die, name, cu);
17211
17212 return (attr && DW_UNSND (attr));
17213 }
17214
17215 static int
17216 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17217 {
17218 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17219 which value is non-zero. However, we have to be careful with
17220 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17221 (via dwarf2_flag_true_p) follows this attribute. So we may
17222 end up accidently finding a declaration attribute that belongs
17223 to a different DIE referenced by the specification attribute,
17224 even though the given DIE does not have a declaration attribute. */
17225 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17226 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17227 }
17228
17229 /* Return the die giving the specification for DIE, if there is
17230 one. *SPEC_CU is the CU containing DIE on input, and the CU
17231 containing the return value on output. If there is no
17232 specification, but there is an abstract origin, that is
17233 returned. */
17234
17235 static struct die_info *
17236 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17237 {
17238 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17239 *spec_cu);
17240
17241 if (spec_attr == NULL)
17242 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17243
17244 if (spec_attr == NULL)
17245 return NULL;
17246 else
17247 return follow_die_ref (die, spec_attr, spec_cu);
17248 }
17249
17250 /* Free the line_header structure *LH, and any arrays and strings it
17251 refers to.
17252 NOTE: This is also used as a "cleanup" function. */
17253
17254 static void
17255 free_line_header (struct line_header *lh)
17256 {
17257 if (lh->standard_opcode_lengths)
17258 xfree (lh->standard_opcode_lengths);
17259
17260 /* Remember that all the lh->file_names[i].name pointers are
17261 pointers into debug_line_buffer, and don't need to be freed. */
17262 if (lh->file_names)
17263 xfree (lh->file_names);
17264
17265 /* Similarly for the include directory names. */
17266 if (lh->include_dirs)
17267 xfree (lh->include_dirs);
17268
17269 xfree (lh);
17270 }
17271
17272 /* Stub for free_line_header to match void * callback types. */
17273
17274 static void
17275 free_line_header_voidp (void *arg)
17276 {
17277 struct line_header *lh = (struct line_header *) arg;
17278
17279 free_line_header (lh);
17280 }
17281
17282 /* Add an entry to LH's include directory table. */
17283
17284 static void
17285 add_include_dir (struct line_header *lh, const char *include_dir)
17286 {
17287 if (dwarf_line_debug >= 2)
17288 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17289 lh->num_include_dirs + 1, include_dir);
17290
17291 /* Grow the array if necessary. */
17292 if (lh->include_dirs_size == 0)
17293 {
17294 lh->include_dirs_size = 1; /* for testing */
17295 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
17296 }
17297 else if (lh->num_include_dirs >= lh->include_dirs_size)
17298 {
17299 lh->include_dirs_size *= 2;
17300 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17301 lh->include_dirs_size);
17302 }
17303
17304 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17305 }
17306
17307 /* Add an entry to LH's file name table. */
17308
17309 static void
17310 add_file_name (struct line_header *lh,
17311 const char *name,
17312 unsigned int dir_index,
17313 unsigned int mod_time,
17314 unsigned int length)
17315 {
17316 struct file_entry *fe;
17317
17318 if (dwarf_line_debug >= 2)
17319 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17320 lh->num_file_names + 1, name);
17321
17322 /* Grow the array if necessary. */
17323 if (lh->file_names_size == 0)
17324 {
17325 lh->file_names_size = 1; /* for testing */
17326 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
17327 }
17328 else if (lh->num_file_names >= lh->file_names_size)
17329 {
17330 lh->file_names_size *= 2;
17331 lh->file_names
17332 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
17333 }
17334
17335 fe = &lh->file_names[lh->num_file_names++];
17336 fe->name = name;
17337 fe->dir_index = dir_index;
17338 fe->mod_time = mod_time;
17339 fe->length = length;
17340 fe->included_p = 0;
17341 fe->symtab = NULL;
17342 }
17343
17344 /* A convenience function to find the proper .debug_line section for a CU. */
17345
17346 static struct dwarf2_section_info *
17347 get_debug_line_section (struct dwarf2_cu *cu)
17348 {
17349 struct dwarf2_section_info *section;
17350
17351 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17352 DWO file. */
17353 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17354 section = &cu->dwo_unit->dwo_file->sections.line;
17355 else if (cu->per_cu->is_dwz)
17356 {
17357 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17358
17359 section = &dwz->line;
17360 }
17361 else
17362 section = &dwarf2_per_objfile->line;
17363
17364 return section;
17365 }
17366
17367 /* Read the statement program header starting at OFFSET in
17368 .debug_line, or .debug_line.dwo. Return a pointer
17369 to a struct line_header, allocated using xmalloc.
17370 Returns NULL if there is a problem reading the header, e.g., if it
17371 has a version we don't understand.
17372
17373 NOTE: the strings in the include directory and file name tables of
17374 the returned object point into the dwarf line section buffer,
17375 and must not be freed. */
17376
17377 static struct line_header *
17378 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
17379 {
17380 struct cleanup *back_to;
17381 struct line_header *lh;
17382 const gdb_byte *line_ptr;
17383 unsigned int bytes_read, offset_size;
17384 int i;
17385 const char *cur_dir, *cur_file;
17386 struct dwarf2_section_info *section;
17387 bfd *abfd;
17388
17389 section = get_debug_line_section (cu);
17390 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17391 if (section->buffer == NULL)
17392 {
17393 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17394 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17395 else
17396 complaint (&symfile_complaints, _("missing .debug_line section"));
17397 return 0;
17398 }
17399
17400 /* We can't do this until we know the section is non-empty.
17401 Only then do we know we have such a section. */
17402 abfd = get_section_bfd_owner (section);
17403
17404 /* Make sure that at least there's room for the total_length field.
17405 That could be 12 bytes long, but we're just going to fudge that. */
17406 if (offset + 4 >= section->size)
17407 {
17408 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17409 return 0;
17410 }
17411
17412 lh = XNEW (struct line_header);
17413 memset (lh, 0, sizeof (*lh));
17414 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17415 (void *) lh);
17416
17417 lh->offset.sect_off = offset;
17418 lh->offset_in_dwz = cu->per_cu->is_dwz;
17419
17420 line_ptr = section->buffer + offset;
17421
17422 /* Read in the header. */
17423 lh->total_length =
17424 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17425 &bytes_read, &offset_size);
17426 line_ptr += bytes_read;
17427 if (line_ptr + lh->total_length > (section->buffer + section->size))
17428 {
17429 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17430 do_cleanups (back_to);
17431 return 0;
17432 }
17433 lh->statement_program_end = line_ptr + lh->total_length;
17434 lh->version = read_2_bytes (abfd, line_ptr);
17435 line_ptr += 2;
17436 if (lh->version > 4)
17437 {
17438 /* This is a version we don't understand. The format could have
17439 changed in ways we don't handle properly so just punt. */
17440 complaint (&symfile_complaints,
17441 _("unsupported version in .debug_line section"));
17442 return NULL;
17443 }
17444 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17445 line_ptr += offset_size;
17446 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17447 line_ptr += 1;
17448 if (lh->version >= 4)
17449 {
17450 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17451 line_ptr += 1;
17452 }
17453 else
17454 lh->maximum_ops_per_instruction = 1;
17455
17456 if (lh->maximum_ops_per_instruction == 0)
17457 {
17458 lh->maximum_ops_per_instruction = 1;
17459 complaint (&symfile_complaints,
17460 _("invalid maximum_ops_per_instruction "
17461 "in `.debug_line' section"));
17462 }
17463
17464 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17465 line_ptr += 1;
17466 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17467 line_ptr += 1;
17468 lh->line_range = read_1_byte (abfd, line_ptr);
17469 line_ptr += 1;
17470 lh->opcode_base = read_1_byte (abfd, line_ptr);
17471 line_ptr += 1;
17472 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
17473
17474 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17475 for (i = 1; i < lh->opcode_base; ++i)
17476 {
17477 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17478 line_ptr += 1;
17479 }
17480
17481 /* Read directory table. */
17482 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17483 {
17484 line_ptr += bytes_read;
17485 add_include_dir (lh, cur_dir);
17486 }
17487 line_ptr += bytes_read;
17488
17489 /* Read file name table. */
17490 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17491 {
17492 unsigned int dir_index, mod_time, length;
17493
17494 line_ptr += bytes_read;
17495 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17496 line_ptr += bytes_read;
17497 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17498 line_ptr += bytes_read;
17499 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17500 line_ptr += bytes_read;
17501
17502 add_file_name (lh, cur_file, dir_index, mod_time, length);
17503 }
17504 line_ptr += bytes_read;
17505 lh->statement_program_start = line_ptr;
17506
17507 if (line_ptr > (section->buffer + section->size))
17508 complaint (&symfile_complaints,
17509 _("line number info header doesn't "
17510 "fit in `.debug_line' section"));
17511
17512 discard_cleanups (back_to);
17513 return lh;
17514 }
17515
17516 /* Subroutine of dwarf_decode_lines to simplify it.
17517 Return the file name of the psymtab for included file FILE_INDEX
17518 in line header LH of PST.
17519 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17520 If space for the result is malloc'd, it will be freed by a cleanup.
17521 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17522
17523 The function creates dangling cleanup registration. */
17524
17525 static const char *
17526 psymtab_include_file_name (const struct line_header *lh, int file_index,
17527 const struct partial_symtab *pst,
17528 const char *comp_dir)
17529 {
17530 const struct file_entry fe = lh->file_names [file_index];
17531 const char *include_name = fe.name;
17532 const char *include_name_to_compare = include_name;
17533 const char *dir_name = NULL;
17534 const char *pst_filename;
17535 char *copied_name = NULL;
17536 int file_is_pst;
17537
17538 if (fe.dir_index && lh->include_dirs != NULL)
17539 dir_name = lh->include_dirs[fe.dir_index - 1];
17540
17541 if (!IS_ABSOLUTE_PATH (include_name)
17542 && (dir_name != NULL || comp_dir != NULL))
17543 {
17544 /* Avoid creating a duplicate psymtab for PST.
17545 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17546 Before we do the comparison, however, we need to account
17547 for DIR_NAME and COMP_DIR.
17548 First prepend dir_name (if non-NULL). If we still don't
17549 have an absolute path prepend comp_dir (if non-NULL).
17550 However, the directory we record in the include-file's
17551 psymtab does not contain COMP_DIR (to match the
17552 corresponding symtab(s)).
17553
17554 Example:
17555
17556 bash$ cd /tmp
17557 bash$ gcc -g ./hello.c
17558 include_name = "hello.c"
17559 dir_name = "."
17560 DW_AT_comp_dir = comp_dir = "/tmp"
17561 DW_AT_name = "./hello.c"
17562
17563 */
17564
17565 if (dir_name != NULL)
17566 {
17567 char *tem = concat (dir_name, SLASH_STRING,
17568 include_name, (char *)NULL);
17569
17570 make_cleanup (xfree, tem);
17571 include_name = tem;
17572 include_name_to_compare = include_name;
17573 }
17574 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
17575 {
17576 char *tem = concat (comp_dir, SLASH_STRING,
17577 include_name, (char *)NULL);
17578
17579 make_cleanup (xfree, tem);
17580 include_name_to_compare = tem;
17581 }
17582 }
17583
17584 pst_filename = pst->filename;
17585 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
17586 {
17587 copied_name = concat (pst->dirname, SLASH_STRING,
17588 pst_filename, (char *)NULL);
17589 pst_filename = copied_name;
17590 }
17591
17592 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
17593
17594 if (copied_name != NULL)
17595 xfree (copied_name);
17596
17597 if (file_is_pst)
17598 return NULL;
17599 return include_name;
17600 }
17601
17602 /* State machine to track the state of the line number program. */
17603
17604 typedef struct
17605 {
17606 /* These are part of the standard DWARF line number state machine. */
17607
17608 unsigned char op_index;
17609 unsigned int file;
17610 unsigned int line;
17611 CORE_ADDR address;
17612 int is_stmt;
17613 unsigned int discriminator;
17614
17615 /* Additional bits of state we need to track. */
17616
17617 /* The last file that we called dwarf2_start_subfile for.
17618 This is only used for TLLs. */
17619 unsigned int last_file;
17620 /* The last file a line number was recorded for. */
17621 struct subfile *last_subfile;
17622
17623 /* The function to call to record a line. */
17624 record_line_ftype *record_line;
17625
17626 /* The last line number that was recorded, used to coalesce
17627 consecutive entries for the same line. This can happen, for
17628 example, when discriminators are present. PR 17276. */
17629 unsigned int last_line;
17630 int line_has_non_zero_discriminator;
17631 } lnp_state_machine;
17632
17633 /* There's a lot of static state to pass to dwarf_record_line.
17634 This keeps it all together. */
17635
17636 typedef struct
17637 {
17638 /* The gdbarch. */
17639 struct gdbarch *gdbarch;
17640
17641 /* The line number header. */
17642 struct line_header *line_header;
17643
17644 /* Non-zero if we're recording lines.
17645 Otherwise we're building partial symtabs and are just interested in
17646 finding include files mentioned by the line number program. */
17647 int record_lines_p;
17648 } lnp_reader_state;
17649
17650 /* Ignore this record_line request. */
17651
17652 static void
17653 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
17654 {
17655 return;
17656 }
17657
17658 /* Return non-zero if we should add LINE to the line number table.
17659 LINE is the line to add, LAST_LINE is the last line that was added,
17660 LAST_SUBFILE is the subfile for LAST_LINE.
17661 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
17662 had a non-zero discriminator.
17663
17664 We have to be careful in the presence of discriminators.
17665 E.g., for this line:
17666
17667 for (i = 0; i < 100000; i++);
17668
17669 clang can emit four line number entries for that one line,
17670 each with a different discriminator.
17671 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
17672
17673 However, we want gdb to coalesce all four entries into one.
17674 Otherwise the user could stepi into the middle of the line and
17675 gdb would get confused about whether the pc really was in the
17676 middle of the line.
17677
17678 Things are further complicated by the fact that two consecutive
17679 line number entries for the same line is a heuristic used by gcc
17680 to denote the end of the prologue. So we can't just discard duplicate
17681 entries, we have to be selective about it. The heuristic we use is
17682 that we only collapse consecutive entries for the same line if at least
17683 one of those entries has a non-zero discriminator. PR 17276.
17684
17685 Note: Addresses in the line number state machine can never go backwards
17686 within one sequence, thus this coalescing is ok. */
17687
17688 static int
17689 dwarf_record_line_p (unsigned int line, unsigned int last_line,
17690 int line_has_non_zero_discriminator,
17691 struct subfile *last_subfile)
17692 {
17693 if (current_subfile != last_subfile)
17694 return 1;
17695 if (line != last_line)
17696 return 1;
17697 /* Same line for the same file that we've seen already.
17698 As a last check, for pr 17276, only record the line if the line
17699 has never had a non-zero discriminator. */
17700 if (!line_has_non_zero_discriminator)
17701 return 1;
17702 return 0;
17703 }
17704
17705 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
17706 in the line table of subfile SUBFILE. */
17707
17708 static void
17709 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
17710 unsigned int line, CORE_ADDR address,
17711 record_line_ftype p_record_line)
17712 {
17713 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
17714
17715 if (dwarf_line_debug)
17716 {
17717 fprintf_unfiltered (gdb_stdlog,
17718 "Recording line %u, file %s, address %s\n",
17719 line, lbasename (subfile->name),
17720 paddress (gdbarch, address));
17721 }
17722
17723 (*p_record_line) (subfile, line, addr);
17724 }
17725
17726 /* Subroutine of dwarf_decode_lines_1 to simplify it.
17727 Mark the end of a set of line number records.
17728 The arguments are the same as for dwarf_record_line_1.
17729 If SUBFILE is NULL the request is ignored. */
17730
17731 static void
17732 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
17733 CORE_ADDR address, record_line_ftype p_record_line)
17734 {
17735 if (subfile == NULL)
17736 return;
17737
17738 if (dwarf_line_debug)
17739 {
17740 fprintf_unfiltered (gdb_stdlog,
17741 "Finishing current line, file %s, address %s\n",
17742 lbasename (subfile->name),
17743 paddress (gdbarch, address));
17744 }
17745
17746 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
17747 }
17748
17749 /* Record the line in STATE.
17750 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
17751
17752 static void
17753 dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
17754 int end_sequence)
17755 {
17756 const struct line_header *lh = reader->line_header;
17757 unsigned int file, line, discriminator;
17758 int is_stmt;
17759
17760 file = state->file;
17761 line = state->line;
17762 is_stmt = state->is_stmt;
17763 discriminator = state->discriminator;
17764
17765 if (dwarf_line_debug)
17766 {
17767 fprintf_unfiltered (gdb_stdlog,
17768 "Processing actual line %u: file %u,"
17769 " address %s, is_stmt %u, discrim %u\n",
17770 line, file,
17771 paddress (reader->gdbarch, state->address),
17772 is_stmt, discriminator);
17773 }
17774
17775 if (file == 0 || file - 1 >= lh->num_file_names)
17776 dwarf2_debug_line_missing_file_complaint ();
17777 /* For now we ignore lines not starting on an instruction boundary.
17778 But not when processing end_sequence for compatibility with the
17779 previous version of the code. */
17780 else if (state->op_index == 0 || end_sequence)
17781 {
17782 lh->file_names[file - 1].included_p = 1;
17783 if (reader->record_lines_p && is_stmt)
17784 {
17785 if (state->last_subfile != current_subfile || end_sequence)
17786 {
17787 dwarf_finish_line (reader->gdbarch, state->last_subfile,
17788 state->address, state->record_line);
17789 }
17790
17791 if (!end_sequence)
17792 {
17793 if (dwarf_record_line_p (line, state->last_line,
17794 state->line_has_non_zero_discriminator,
17795 state->last_subfile))
17796 {
17797 dwarf_record_line_1 (reader->gdbarch, current_subfile,
17798 line, state->address,
17799 state->record_line);
17800 }
17801 state->last_subfile = current_subfile;
17802 state->last_line = line;
17803 }
17804 }
17805 }
17806 }
17807
17808 /* Initialize STATE for the start of a line number program. */
17809
17810 static void
17811 init_lnp_state_machine (lnp_state_machine *state,
17812 const lnp_reader_state *reader)
17813 {
17814 memset (state, 0, sizeof (*state));
17815
17816 /* Just starting, there is no "last file". */
17817 state->last_file = 0;
17818 state->last_subfile = NULL;
17819
17820 state->record_line = record_line;
17821
17822 state->last_line = 0;
17823 state->line_has_non_zero_discriminator = 0;
17824
17825 /* Initialize these according to the DWARF spec. */
17826 state->op_index = 0;
17827 state->file = 1;
17828 state->line = 1;
17829 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
17830 was a line entry for it so that the backend has a chance to adjust it
17831 and also record it in case it needs it. This is currently used by MIPS
17832 code, cf. `mips_adjust_dwarf2_line'. */
17833 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
17834 state->is_stmt = reader->line_header->default_is_stmt;
17835 state->discriminator = 0;
17836 }
17837
17838 /* Check address and if invalid nop-out the rest of the lines in this
17839 sequence. */
17840
17841 static void
17842 check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
17843 const gdb_byte *line_ptr,
17844 CORE_ADDR lowpc, CORE_ADDR address)
17845 {
17846 /* If address < lowpc then it's not a usable value, it's outside the
17847 pc range of the CU. However, we restrict the test to only address
17848 values of zero to preserve GDB's previous behaviour which is to
17849 handle the specific case of a function being GC'd by the linker. */
17850
17851 if (address == 0 && address < lowpc)
17852 {
17853 /* This line table is for a function which has been
17854 GCd by the linker. Ignore it. PR gdb/12528 */
17855
17856 struct objfile *objfile = cu->objfile;
17857 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
17858
17859 complaint (&symfile_complaints,
17860 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
17861 line_offset, objfile_name (objfile));
17862 state->record_line = noop_record_line;
17863 /* Note: sm.record_line is left as noop_record_line
17864 until we see DW_LNE_end_sequence. */
17865 }
17866 }
17867
17868 /* Subroutine of dwarf_decode_lines to simplify it.
17869 Process the line number information in LH.
17870 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
17871 program in order to set included_p for every referenced header. */
17872
17873 static void
17874 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
17875 const int decode_for_pst_p, CORE_ADDR lowpc)
17876 {
17877 const gdb_byte *line_ptr, *extended_end;
17878 const gdb_byte *line_end;
17879 unsigned int bytes_read, extended_len;
17880 unsigned char op_code, extended_op;
17881 CORE_ADDR baseaddr;
17882 struct objfile *objfile = cu->objfile;
17883 bfd *abfd = objfile->obfd;
17884 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17885 /* Non-zero if we're recording line info (as opposed to building partial
17886 symtabs). */
17887 int record_lines_p = !decode_for_pst_p;
17888 /* A collection of things we need to pass to dwarf_record_line. */
17889 lnp_reader_state reader_state;
17890
17891 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17892
17893 line_ptr = lh->statement_program_start;
17894 line_end = lh->statement_program_end;
17895
17896 reader_state.gdbarch = gdbarch;
17897 reader_state.line_header = lh;
17898 reader_state.record_lines_p = record_lines_p;
17899
17900 /* Read the statement sequences until there's nothing left. */
17901 while (line_ptr < line_end)
17902 {
17903 /* The DWARF line number program state machine. */
17904 lnp_state_machine state_machine;
17905 int end_sequence = 0;
17906
17907 /* Reset the state machine at the start of each sequence. */
17908 init_lnp_state_machine (&state_machine, &reader_state);
17909
17910 if (record_lines_p && lh->num_file_names >= state_machine.file)
17911 {
17912 /* Start a subfile for the current file of the state machine. */
17913 /* lh->include_dirs and lh->file_names are 0-based, but the
17914 directory and file name numbers in the statement program
17915 are 1-based. */
17916 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
17917 const char *dir = NULL;
17918
17919 if (fe->dir_index && lh->include_dirs != NULL)
17920 dir = lh->include_dirs[fe->dir_index - 1];
17921
17922 dwarf2_start_subfile (fe->name, dir);
17923 }
17924
17925 /* Decode the table. */
17926 while (line_ptr < line_end && !end_sequence)
17927 {
17928 op_code = read_1_byte (abfd, line_ptr);
17929 line_ptr += 1;
17930
17931 if (op_code >= lh->opcode_base)
17932 {
17933 /* Special opcode. */
17934 unsigned char adj_opcode;
17935 CORE_ADDR addr_adj;
17936 int line_delta;
17937
17938 adj_opcode = op_code - lh->opcode_base;
17939 addr_adj = (((state_machine.op_index
17940 + (adj_opcode / lh->line_range))
17941 / lh->maximum_ops_per_instruction)
17942 * lh->minimum_instruction_length);
17943 state_machine.address
17944 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
17945 state_machine.op_index = ((state_machine.op_index
17946 + (adj_opcode / lh->line_range))
17947 % lh->maximum_ops_per_instruction);
17948 line_delta = lh->line_base + (adj_opcode % lh->line_range);
17949 state_machine.line += line_delta;
17950 if (line_delta != 0)
17951 state_machine.line_has_non_zero_discriminator
17952 = state_machine.discriminator != 0;
17953
17954 dwarf_record_line (&reader_state, &state_machine, 0);
17955 state_machine.discriminator = 0;
17956 }
17957 else switch (op_code)
17958 {
17959 case DW_LNS_extended_op:
17960 extended_len = read_unsigned_leb128 (abfd, line_ptr,
17961 &bytes_read);
17962 line_ptr += bytes_read;
17963 extended_end = line_ptr + extended_len;
17964 extended_op = read_1_byte (abfd, line_ptr);
17965 line_ptr += 1;
17966 switch (extended_op)
17967 {
17968 case DW_LNE_end_sequence:
17969 state_machine.record_line = record_line;
17970 end_sequence = 1;
17971 break;
17972 case DW_LNE_set_address:
17973 {
17974 CORE_ADDR address
17975 = read_address (abfd, line_ptr, cu, &bytes_read);
17976
17977 line_ptr += bytes_read;
17978 check_line_address (cu, &state_machine, line_ptr,
17979 lowpc, address);
17980 state_machine.op_index = 0;
17981 address += baseaddr;
17982 state_machine.address
17983 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
17984 }
17985 break;
17986 case DW_LNE_define_file:
17987 {
17988 const char *cur_file;
17989 unsigned int dir_index, mod_time, length;
17990
17991 cur_file = read_direct_string (abfd, line_ptr,
17992 &bytes_read);
17993 line_ptr += bytes_read;
17994 dir_index =
17995 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17996 line_ptr += bytes_read;
17997 mod_time =
17998 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17999 line_ptr += bytes_read;
18000 length =
18001 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18002 line_ptr += bytes_read;
18003 add_file_name (lh, cur_file, dir_index, mod_time, length);
18004 }
18005 break;
18006 case DW_LNE_set_discriminator:
18007 /* The discriminator is not interesting to the debugger;
18008 just ignore it. We still need to check its value though:
18009 if there are consecutive entries for the same
18010 (non-prologue) line we want to coalesce them.
18011 PR 17276. */
18012 state_machine.discriminator
18013 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18014 state_machine.line_has_non_zero_discriminator
18015 |= state_machine.discriminator != 0;
18016 line_ptr += bytes_read;
18017 break;
18018 default:
18019 complaint (&symfile_complaints,
18020 _("mangled .debug_line section"));
18021 return;
18022 }
18023 /* Make sure that we parsed the extended op correctly. If e.g.
18024 we expected a different address size than the producer used,
18025 we may have read the wrong number of bytes. */
18026 if (line_ptr != extended_end)
18027 {
18028 complaint (&symfile_complaints,
18029 _("mangled .debug_line section"));
18030 return;
18031 }
18032 break;
18033 case DW_LNS_copy:
18034 dwarf_record_line (&reader_state, &state_machine, 0);
18035 state_machine.discriminator = 0;
18036 break;
18037 case DW_LNS_advance_pc:
18038 {
18039 CORE_ADDR adjust
18040 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18041 CORE_ADDR addr_adj;
18042
18043 addr_adj = (((state_machine.op_index + adjust)
18044 / lh->maximum_ops_per_instruction)
18045 * lh->minimum_instruction_length);
18046 state_machine.address
18047 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18048 state_machine.op_index = ((state_machine.op_index + adjust)
18049 % lh->maximum_ops_per_instruction);
18050 line_ptr += bytes_read;
18051 }
18052 break;
18053 case DW_LNS_advance_line:
18054 {
18055 int line_delta
18056 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18057
18058 state_machine.line += line_delta;
18059 if (line_delta != 0)
18060 state_machine.line_has_non_zero_discriminator
18061 = state_machine.discriminator != 0;
18062 line_ptr += bytes_read;
18063 }
18064 break;
18065 case DW_LNS_set_file:
18066 {
18067 /* The arrays lh->include_dirs and lh->file_names are
18068 0-based, but the directory and file name numbers in
18069 the statement program are 1-based. */
18070 struct file_entry *fe;
18071 const char *dir = NULL;
18072
18073 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
18074 &bytes_read);
18075 line_ptr += bytes_read;
18076 if (state_machine.file == 0
18077 || state_machine.file - 1 >= lh->num_file_names)
18078 dwarf2_debug_line_missing_file_complaint ();
18079 else
18080 {
18081 fe = &lh->file_names[state_machine.file - 1];
18082 if (fe->dir_index && lh->include_dirs != NULL)
18083 dir = lh->include_dirs[fe->dir_index - 1];
18084 if (record_lines_p)
18085 {
18086 state_machine.last_subfile = current_subfile;
18087 state_machine.line_has_non_zero_discriminator
18088 = state_machine.discriminator != 0;
18089 dwarf2_start_subfile (fe->name, dir);
18090 }
18091 }
18092 }
18093 break;
18094 case DW_LNS_set_column:
18095 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18096 line_ptr += bytes_read;
18097 break;
18098 case DW_LNS_negate_stmt:
18099 state_machine.is_stmt = (!state_machine.is_stmt);
18100 break;
18101 case DW_LNS_set_basic_block:
18102 break;
18103 /* Add to the address register of the state machine the
18104 address increment value corresponding to special opcode
18105 255. I.e., this value is scaled by the minimum
18106 instruction length since special opcode 255 would have
18107 scaled the increment. */
18108 case DW_LNS_const_add_pc:
18109 {
18110 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
18111 CORE_ADDR addr_adj;
18112
18113 addr_adj = (((state_machine.op_index + adjust)
18114 / lh->maximum_ops_per_instruction)
18115 * lh->minimum_instruction_length);
18116 state_machine.address
18117 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18118 state_machine.op_index = ((state_machine.op_index + adjust)
18119 % lh->maximum_ops_per_instruction);
18120 }
18121 break;
18122 case DW_LNS_fixed_advance_pc:
18123 {
18124 CORE_ADDR addr_adj;
18125
18126 addr_adj = read_2_bytes (abfd, line_ptr);
18127 state_machine.address
18128 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18129 state_machine.op_index = 0;
18130 line_ptr += 2;
18131 }
18132 break;
18133 default:
18134 {
18135 /* Unknown standard opcode, ignore it. */
18136 int i;
18137
18138 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18139 {
18140 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18141 line_ptr += bytes_read;
18142 }
18143 }
18144 }
18145 }
18146
18147 if (!end_sequence)
18148 dwarf2_debug_line_missing_end_sequence_complaint ();
18149
18150 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18151 in which case we still finish recording the last line). */
18152 dwarf_record_line (&reader_state, &state_machine, 1);
18153 }
18154 }
18155
18156 /* Decode the Line Number Program (LNP) for the given line_header
18157 structure and CU. The actual information extracted and the type
18158 of structures created from the LNP depends on the value of PST.
18159
18160 1. If PST is NULL, then this procedure uses the data from the program
18161 to create all necessary symbol tables, and their linetables.
18162
18163 2. If PST is not NULL, this procedure reads the program to determine
18164 the list of files included by the unit represented by PST, and
18165 builds all the associated partial symbol tables.
18166
18167 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18168 It is used for relative paths in the line table.
18169 NOTE: When processing partial symtabs (pst != NULL),
18170 comp_dir == pst->dirname.
18171
18172 NOTE: It is important that psymtabs have the same file name (via strcmp)
18173 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18174 symtab we don't use it in the name of the psymtabs we create.
18175 E.g. expand_line_sal requires this when finding psymtabs to expand.
18176 A good testcase for this is mb-inline.exp.
18177
18178 LOWPC is the lowest address in CU (or 0 if not known).
18179
18180 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18181 for its PC<->lines mapping information. Otherwise only the filename
18182 table is read in. */
18183
18184 static void
18185 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18186 struct dwarf2_cu *cu, struct partial_symtab *pst,
18187 CORE_ADDR lowpc, int decode_mapping)
18188 {
18189 struct objfile *objfile = cu->objfile;
18190 const int decode_for_pst_p = (pst != NULL);
18191
18192 if (decode_mapping)
18193 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18194
18195 if (decode_for_pst_p)
18196 {
18197 int file_index;
18198
18199 /* Now that we're done scanning the Line Header Program, we can
18200 create the psymtab of each included file. */
18201 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18202 if (lh->file_names[file_index].included_p == 1)
18203 {
18204 const char *include_name =
18205 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18206 if (include_name != NULL)
18207 dwarf2_create_include_psymtab (include_name, pst, objfile);
18208 }
18209 }
18210 else
18211 {
18212 /* Make sure a symtab is created for every file, even files
18213 which contain only variables (i.e. no code with associated
18214 line numbers). */
18215 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18216 int i;
18217
18218 for (i = 0; i < lh->num_file_names; i++)
18219 {
18220 const char *dir = NULL;
18221 struct file_entry *fe;
18222
18223 fe = &lh->file_names[i];
18224 if (fe->dir_index && lh->include_dirs != NULL)
18225 dir = lh->include_dirs[fe->dir_index - 1];
18226 dwarf2_start_subfile (fe->name, dir);
18227
18228 if (current_subfile->symtab == NULL)
18229 {
18230 current_subfile->symtab
18231 = allocate_symtab (cust, current_subfile->name);
18232 }
18233 fe->symtab = current_subfile->symtab;
18234 }
18235 }
18236 }
18237
18238 /* Start a subfile for DWARF. FILENAME is the name of the file and
18239 DIRNAME the name of the source directory which contains FILENAME
18240 or NULL if not known.
18241 This routine tries to keep line numbers from identical absolute and
18242 relative file names in a common subfile.
18243
18244 Using the `list' example from the GDB testsuite, which resides in
18245 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18246 of /srcdir/list0.c yields the following debugging information for list0.c:
18247
18248 DW_AT_name: /srcdir/list0.c
18249 DW_AT_comp_dir: /compdir
18250 files.files[0].name: list0.h
18251 files.files[0].dir: /srcdir
18252 files.files[1].name: list0.c
18253 files.files[1].dir: /srcdir
18254
18255 The line number information for list0.c has to end up in a single
18256 subfile, so that `break /srcdir/list0.c:1' works as expected.
18257 start_subfile will ensure that this happens provided that we pass the
18258 concatenation of files.files[1].dir and files.files[1].name as the
18259 subfile's name. */
18260
18261 static void
18262 dwarf2_start_subfile (const char *filename, const char *dirname)
18263 {
18264 char *copy = NULL;
18265
18266 /* In order not to lose the line information directory,
18267 we concatenate it to the filename when it makes sense.
18268 Note that the Dwarf3 standard says (speaking of filenames in line
18269 information): ``The directory index is ignored for file names
18270 that represent full path names''. Thus ignoring dirname in the
18271 `else' branch below isn't an issue. */
18272
18273 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18274 {
18275 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18276 filename = copy;
18277 }
18278
18279 start_subfile (filename);
18280
18281 if (copy != NULL)
18282 xfree (copy);
18283 }
18284
18285 /* Start a symtab for DWARF.
18286 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18287
18288 static struct compunit_symtab *
18289 dwarf2_start_symtab (struct dwarf2_cu *cu,
18290 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18291 {
18292 struct compunit_symtab *cust
18293 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18294
18295 record_debugformat ("DWARF 2");
18296 record_producer (cu->producer);
18297
18298 /* We assume that we're processing GCC output. */
18299 processing_gcc_compilation = 2;
18300
18301 cu->processing_has_namespace_info = 0;
18302
18303 return cust;
18304 }
18305
18306 static void
18307 var_decode_location (struct attribute *attr, struct symbol *sym,
18308 struct dwarf2_cu *cu)
18309 {
18310 struct objfile *objfile = cu->objfile;
18311 struct comp_unit_head *cu_header = &cu->header;
18312
18313 /* NOTE drow/2003-01-30: There used to be a comment and some special
18314 code here to turn a symbol with DW_AT_external and a
18315 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18316 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18317 with some versions of binutils) where shared libraries could have
18318 relocations against symbols in their debug information - the
18319 minimal symbol would have the right address, but the debug info
18320 would not. It's no longer necessary, because we will explicitly
18321 apply relocations when we read in the debug information now. */
18322
18323 /* A DW_AT_location attribute with no contents indicates that a
18324 variable has been optimized away. */
18325 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18326 {
18327 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18328 return;
18329 }
18330
18331 /* Handle one degenerate form of location expression specially, to
18332 preserve GDB's previous behavior when section offsets are
18333 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18334 then mark this symbol as LOC_STATIC. */
18335
18336 if (attr_form_is_block (attr)
18337 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18338 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18339 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18340 && (DW_BLOCK (attr)->size
18341 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18342 {
18343 unsigned int dummy;
18344
18345 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18346 SYMBOL_VALUE_ADDRESS (sym) =
18347 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18348 else
18349 SYMBOL_VALUE_ADDRESS (sym) =
18350 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18351 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18352 fixup_symbol_section (sym, objfile);
18353 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18354 SYMBOL_SECTION (sym));
18355 return;
18356 }
18357
18358 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18359 expression evaluator, and use LOC_COMPUTED only when necessary
18360 (i.e. when the value of a register or memory location is
18361 referenced, or a thread-local block, etc.). Then again, it might
18362 not be worthwhile. I'm assuming that it isn't unless performance
18363 or memory numbers show me otherwise. */
18364
18365 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18366
18367 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18368 cu->has_loclist = 1;
18369 }
18370
18371 /* Given a pointer to a DWARF information entry, figure out if we need
18372 to make a symbol table entry for it, and if so, create a new entry
18373 and return a pointer to it.
18374 If TYPE is NULL, determine symbol type from the die, otherwise
18375 used the passed type.
18376 If SPACE is not NULL, use it to hold the new symbol. If it is
18377 NULL, allocate a new symbol on the objfile's obstack. */
18378
18379 static struct symbol *
18380 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18381 struct symbol *space)
18382 {
18383 struct objfile *objfile = cu->objfile;
18384 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18385 struct symbol *sym = NULL;
18386 const char *name;
18387 struct attribute *attr = NULL;
18388 struct attribute *attr2 = NULL;
18389 CORE_ADDR baseaddr;
18390 struct pending **list_to_add = NULL;
18391
18392 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18393
18394 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18395
18396 name = dwarf2_name (die, cu);
18397 if (name)
18398 {
18399 const char *linkagename;
18400 int suppress_add = 0;
18401
18402 if (space)
18403 sym = space;
18404 else
18405 sym = allocate_symbol (objfile);
18406 OBJSTAT (objfile, n_syms++);
18407
18408 /* Cache this symbol's name and the name's demangled form (if any). */
18409 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18410 linkagename = dwarf2_physname (name, die, cu);
18411 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18412
18413 /* Fortran does not have mangling standard and the mangling does differ
18414 between gfortran, iFort etc. */
18415 if (cu->language == language_fortran
18416 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18417 symbol_set_demangled_name (&(sym->ginfo),
18418 dwarf2_full_name (name, die, cu),
18419 NULL);
18420
18421 /* Default assumptions.
18422 Use the passed type or decode it from the die. */
18423 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18424 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18425 if (type != NULL)
18426 SYMBOL_TYPE (sym) = type;
18427 else
18428 SYMBOL_TYPE (sym) = die_type (die, cu);
18429 attr = dwarf2_attr (die,
18430 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18431 cu);
18432 if (attr)
18433 {
18434 SYMBOL_LINE (sym) = DW_UNSND (attr);
18435 }
18436
18437 attr = dwarf2_attr (die,
18438 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18439 cu);
18440 if (attr)
18441 {
18442 int file_index = DW_UNSND (attr);
18443
18444 if (cu->line_header == NULL
18445 || file_index > cu->line_header->num_file_names)
18446 complaint (&symfile_complaints,
18447 _("file index out of range"));
18448 else if (file_index > 0)
18449 {
18450 struct file_entry *fe;
18451
18452 fe = &cu->line_header->file_names[file_index - 1];
18453 symbol_set_symtab (sym, fe->symtab);
18454 }
18455 }
18456
18457 switch (die->tag)
18458 {
18459 case DW_TAG_label:
18460 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18461 if (attr)
18462 {
18463 CORE_ADDR addr;
18464
18465 addr = attr_value_as_address (attr);
18466 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18467 SYMBOL_VALUE_ADDRESS (sym) = addr;
18468 }
18469 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18470 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18471 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18472 add_symbol_to_list (sym, cu->list_in_scope);
18473 break;
18474 case DW_TAG_subprogram:
18475 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18476 finish_block. */
18477 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18478 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18479 if ((attr2 && (DW_UNSND (attr2) != 0))
18480 || cu->language == language_ada)
18481 {
18482 /* Subprograms marked external are stored as a global symbol.
18483 Ada subprograms, whether marked external or not, are always
18484 stored as a global symbol, because we want to be able to
18485 access them globally. For instance, we want to be able
18486 to break on a nested subprogram without having to
18487 specify the context. */
18488 list_to_add = &global_symbols;
18489 }
18490 else
18491 {
18492 list_to_add = cu->list_in_scope;
18493 }
18494 break;
18495 case DW_TAG_inlined_subroutine:
18496 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18497 finish_block. */
18498 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18499 SYMBOL_INLINED (sym) = 1;
18500 list_to_add = cu->list_in_scope;
18501 break;
18502 case DW_TAG_template_value_param:
18503 suppress_add = 1;
18504 /* Fall through. */
18505 case DW_TAG_constant:
18506 case DW_TAG_variable:
18507 case DW_TAG_member:
18508 /* Compilation with minimal debug info may result in
18509 variables with missing type entries. Change the
18510 misleading `void' type to something sensible. */
18511 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
18512 SYMBOL_TYPE (sym)
18513 = objfile_type (objfile)->nodebug_data_symbol;
18514
18515 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18516 /* In the case of DW_TAG_member, we should only be called for
18517 static const members. */
18518 if (die->tag == DW_TAG_member)
18519 {
18520 /* dwarf2_add_field uses die_is_declaration,
18521 so we do the same. */
18522 gdb_assert (die_is_declaration (die, cu));
18523 gdb_assert (attr);
18524 }
18525 if (attr)
18526 {
18527 dwarf2_const_value (attr, sym, cu);
18528 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18529 if (!suppress_add)
18530 {
18531 if (attr2 && (DW_UNSND (attr2) != 0))
18532 list_to_add = &global_symbols;
18533 else
18534 list_to_add = cu->list_in_scope;
18535 }
18536 break;
18537 }
18538 attr = dwarf2_attr (die, DW_AT_location, cu);
18539 if (attr)
18540 {
18541 var_decode_location (attr, sym, cu);
18542 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18543
18544 /* Fortran explicitly imports any global symbols to the local
18545 scope by DW_TAG_common_block. */
18546 if (cu->language == language_fortran && die->parent
18547 && die->parent->tag == DW_TAG_common_block)
18548 attr2 = NULL;
18549
18550 if (SYMBOL_CLASS (sym) == LOC_STATIC
18551 && SYMBOL_VALUE_ADDRESS (sym) == 0
18552 && !dwarf2_per_objfile->has_section_at_zero)
18553 {
18554 /* When a static variable is eliminated by the linker,
18555 the corresponding debug information is not stripped
18556 out, but the variable address is set to null;
18557 do not add such variables into symbol table. */
18558 }
18559 else if (attr2 && (DW_UNSND (attr2) != 0))
18560 {
18561 /* Workaround gfortran PR debug/40040 - it uses
18562 DW_AT_location for variables in -fPIC libraries which may
18563 get overriden by other libraries/executable and get
18564 a different address. Resolve it by the minimal symbol
18565 which may come from inferior's executable using copy
18566 relocation. Make this workaround only for gfortran as for
18567 other compilers GDB cannot guess the minimal symbol
18568 Fortran mangling kind. */
18569 if (cu->language == language_fortran && die->parent
18570 && die->parent->tag == DW_TAG_module
18571 && cu->producer
18572 && startswith (cu->producer, "GNU Fortran"))
18573 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
18574
18575 /* A variable with DW_AT_external is never static,
18576 but it may be block-scoped. */
18577 list_to_add = (cu->list_in_scope == &file_symbols
18578 ? &global_symbols : cu->list_in_scope);
18579 }
18580 else
18581 list_to_add = cu->list_in_scope;
18582 }
18583 else
18584 {
18585 /* We do not know the address of this symbol.
18586 If it is an external symbol and we have type information
18587 for it, enter the symbol as a LOC_UNRESOLVED symbol.
18588 The address of the variable will then be determined from
18589 the minimal symbol table whenever the variable is
18590 referenced. */
18591 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18592
18593 /* Fortran explicitly imports any global symbols to the local
18594 scope by DW_TAG_common_block. */
18595 if (cu->language == language_fortran && die->parent
18596 && die->parent->tag == DW_TAG_common_block)
18597 {
18598 /* SYMBOL_CLASS doesn't matter here because
18599 read_common_block is going to reset it. */
18600 if (!suppress_add)
18601 list_to_add = cu->list_in_scope;
18602 }
18603 else if (attr2 && (DW_UNSND (attr2) != 0)
18604 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
18605 {
18606 /* A variable with DW_AT_external is never static, but it
18607 may be block-scoped. */
18608 list_to_add = (cu->list_in_scope == &file_symbols
18609 ? &global_symbols : cu->list_in_scope);
18610
18611 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
18612 }
18613 else if (!die_is_declaration (die, cu))
18614 {
18615 /* Use the default LOC_OPTIMIZED_OUT class. */
18616 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
18617 if (!suppress_add)
18618 list_to_add = cu->list_in_scope;
18619 }
18620 }
18621 break;
18622 case DW_TAG_formal_parameter:
18623 /* If we are inside a function, mark this as an argument. If
18624 not, we might be looking at an argument to an inlined function
18625 when we do not have enough information to show inlined frames;
18626 pretend it's a local variable in that case so that the user can
18627 still see it. */
18628 if (context_stack_depth > 0
18629 && context_stack[context_stack_depth - 1].name != NULL)
18630 SYMBOL_IS_ARGUMENT (sym) = 1;
18631 attr = dwarf2_attr (die, DW_AT_location, cu);
18632 if (attr)
18633 {
18634 var_decode_location (attr, sym, cu);
18635 }
18636 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18637 if (attr)
18638 {
18639 dwarf2_const_value (attr, sym, cu);
18640 }
18641
18642 list_to_add = cu->list_in_scope;
18643 break;
18644 case DW_TAG_unspecified_parameters:
18645 /* From varargs functions; gdb doesn't seem to have any
18646 interest in this information, so just ignore it for now.
18647 (FIXME?) */
18648 break;
18649 case DW_TAG_template_type_param:
18650 suppress_add = 1;
18651 /* Fall through. */
18652 case DW_TAG_class_type:
18653 case DW_TAG_interface_type:
18654 case DW_TAG_structure_type:
18655 case DW_TAG_union_type:
18656 case DW_TAG_set_type:
18657 case DW_TAG_enumeration_type:
18658 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18659 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
18660
18661 {
18662 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
18663 really ever be static objects: otherwise, if you try
18664 to, say, break of a class's method and you're in a file
18665 which doesn't mention that class, it won't work unless
18666 the check for all static symbols in lookup_symbol_aux
18667 saves you. See the OtherFileClass tests in
18668 gdb.c++/namespace.exp. */
18669
18670 if (!suppress_add)
18671 {
18672 list_to_add = (cu->list_in_scope == &file_symbols
18673 && (cu->language == language_cplus
18674 || cu->language == language_java)
18675 ? &global_symbols : cu->list_in_scope);
18676
18677 /* The semantics of C++ state that "struct foo {
18678 ... }" also defines a typedef for "foo". A Java
18679 class declaration also defines a typedef for the
18680 class. */
18681 if (cu->language == language_cplus
18682 || cu->language == language_java
18683 || cu->language == language_ada
18684 || cu->language == language_d
18685 || cu->language == language_rust)
18686 {
18687 /* The symbol's name is already allocated along
18688 with this objfile, so we don't need to
18689 duplicate it for the type. */
18690 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
18691 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
18692 }
18693 }
18694 }
18695 break;
18696 case DW_TAG_typedef:
18697 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18698 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18699 list_to_add = cu->list_in_scope;
18700 break;
18701 case DW_TAG_base_type:
18702 case DW_TAG_subrange_type:
18703 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18704 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18705 list_to_add = cu->list_in_scope;
18706 break;
18707 case DW_TAG_enumerator:
18708 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18709 if (attr)
18710 {
18711 dwarf2_const_value (attr, sym, cu);
18712 }
18713 {
18714 /* NOTE: carlton/2003-11-10: See comment above in the
18715 DW_TAG_class_type, etc. block. */
18716
18717 list_to_add = (cu->list_in_scope == &file_symbols
18718 && (cu->language == language_cplus
18719 || cu->language == language_java)
18720 ? &global_symbols : cu->list_in_scope);
18721 }
18722 break;
18723 case DW_TAG_imported_declaration:
18724 case DW_TAG_namespace:
18725 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18726 list_to_add = &global_symbols;
18727 break;
18728 case DW_TAG_module:
18729 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18730 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
18731 list_to_add = &global_symbols;
18732 break;
18733 case DW_TAG_common_block:
18734 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
18735 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
18736 add_symbol_to_list (sym, cu->list_in_scope);
18737 break;
18738 default:
18739 /* Not a tag we recognize. Hopefully we aren't processing
18740 trash data, but since we must specifically ignore things
18741 we don't recognize, there is nothing else we should do at
18742 this point. */
18743 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
18744 dwarf_tag_name (die->tag));
18745 break;
18746 }
18747
18748 if (suppress_add)
18749 {
18750 sym->hash_next = objfile->template_symbols;
18751 objfile->template_symbols = sym;
18752 list_to_add = NULL;
18753 }
18754
18755 if (list_to_add != NULL)
18756 add_symbol_to_list (sym, list_to_add);
18757
18758 /* For the benefit of old versions of GCC, check for anonymous
18759 namespaces based on the demangled name. */
18760 if (!cu->processing_has_namespace_info
18761 && cu->language == language_cplus)
18762 cp_scan_for_anonymous_namespaces (sym, objfile);
18763 }
18764 return (sym);
18765 }
18766
18767 /* A wrapper for new_symbol_full that always allocates a new symbol. */
18768
18769 static struct symbol *
18770 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18771 {
18772 return new_symbol_full (die, type, cu, NULL);
18773 }
18774
18775 /* Given an attr with a DW_FORM_dataN value in host byte order,
18776 zero-extend it as appropriate for the symbol's type. The DWARF
18777 standard (v4) is not entirely clear about the meaning of using
18778 DW_FORM_dataN for a constant with a signed type, where the type is
18779 wider than the data. The conclusion of a discussion on the DWARF
18780 list was that this is unspecified. We choose to always zero-extend
18781 because that is the interpretation long in use by GCC. */
18782
18783 static gdb_byte *
18784 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
18785 struct dwarf2_cu *cu, LONGEST *value, int bits)
18786 {
18787 struct objfile *objfile = cu->objfile;
18788 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
18789 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
18790 LONGEST l = DW_UNSND (attr);
18791
18792 if (bits < sizeof (*value) * 8)
18793 {
18794 l &= ((LONGEST) 1 << bits) - 1;
18795 *value = l;
18796 }
18797 else if (bits == sizeof (*value) * 8)
18798 *value = l;
18799 else
18800 {
18801 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
18802 store_unsigned_integer (bytes, bits / 8, byte_order, l);
18803 return bytes;
18804 }
18805
18806 return NULL;
18807 }
18808
18809 /* Read a constant value from an attribute. Either set *VALUE, or if
18810 the value does not fit in *VALUE, set *BYTES - either already
18811 allocated on the objfile obstack, or newly allocated on OBSTACK,
18812 or, set *BATON, if we translated the constant to a location
18813 expression. */
18814
18815 static void
18816 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
18817 const char *name, struct obstack *obstack,
18818 struct dwarf2_cu *cu,
18819 LONGEST *value, const gdb_byte **bytes,
18820 struct dwarf2_locexpr_baton **baton)
18821 {
18822 struct objfile *objfile = cu->objfile;
18823 struct comp_unit_head *cu_header = &cu->header;
18824 struct dwarf_block *blk;
18825 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
18826 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18827
18828 *value = 0;
18829 *bytes = NULL;
18830 *baton = NULL;
18831
18832 switch (attr->form)
18833 {
18834 case DW_FORM_addr:
18835 case DW_FORM_GNU_addr_index:
18836 {
18837 gdb_byte *data;
18838
18839 if (TYPE_LENGTH (type) != cu_header->addr_size)
18840 dwarf2_const_value_length_mismatch_complaint (name,
18841 cu_header->addr_size,
18842 TYPE_LENGTH (type));
18843 /* Symbols of this form are reasonably rare, so we just
18844 piggyback on the existing location code rather than writing
18845 a new implementation of symbol_computed_ops. */
18846 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
18847 (*baton)->per_cu = cu->per_cu;
18848 gdb_assert ((*baton)->per_cu);
18849
18850 (*baton)->size = 2 + cu_header->addr_size;
18851 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
18852 (*baton)->data = data;
18853
18854 data[0] = DW_OP_addr;
18855 store_unsigned_integer (&data[1], cu_header->addr_size,
18856 byte_order, DW_ADDR (attr));
18857 data[cu_header->addr_size + 1] = DW_OP_stack_value;
18858 }
18859 break;
18860 case DW_FORM_string:
18861 case DW_FORM_strp:
18862 case DW_FORM_GNU_str_index:
18863 case DW_FORM_GNU_strp_alt:
18864 /* DW_STRING is already allocated on the objfile obstack, point
18865 directly to it. */
18866 *bytes = (const gdb_byte *) DW_STRING (attr);
18867 break;
18868 case DW_FORM_block1:
18869 case DW_FORM_block2:
18870 case DW_FORM_block4:
18871 case DW_FORM_block:
18872 case DW_FORM_exprloc:
18873 blk = DW_BLOCK (attr);
18874 if (TYPE_LENGTH (type) != blk->size)
18875 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
18876 TYPE_LENGTH (type));
18877 *bytes = blk->data;
18878 break;
18879
18880 /* The DW_AT_const_value attributes are supposed to carry the
18881 symbol's value "represented as it would be on the target
18882 architecture." By the time we get here, it's already been
18883 converted to host endianness, so we just need to sign- or
18884 zero-extend it as appropriate. */
18885 case DW_FORM_data1:
18886 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
18887 break;
18888 case DW_FORM_data2:
18889 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
18890 break;
18891 case DW_FORM_data4:
18892 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
18893 break;
18894 case DW_FORM_data8:
18895 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
18896 break;
18897
18898 case DW_FORM_sdata:
18899 *value = DW_SND (attr);
18900 break;
18901
18902 case DW_FORM_udata:
18903 *value = DW_UNSND (attr);
18904 break;
18905
18906 default:
18907 complaint (&symfile_complaints,
18908 _("unsupported const value attribute form: '%s'"),
18909 dwarf_form_name (attr->form));
18910 *value = 0;
18911 break;
18912 }
18913 }
18914
18915
18916 /* Copy constant value from an attribute to a symbol. */
18917
18918 static void
18919 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
18920 struct dwarf2_cu *cu)
18921 {
18922 struct objfile *objfile = cu->objfile;
18923 LONGEST value;
18924 const gdb_byte *bytes;
18925 struct dwarf2_locexpr_baton *baton;
18926
18927 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
18928 SYMBOL_PRINT_NAME (sym),
18929 &objfile->objfile_obstack, cu,
18930 &value, &bytes, &baton);
18931
18932 if (baton != NULL)
18933 {
18934 SYMBOL_LOCATION_BATON (sym) = baton;
18935 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
18936 }
18937 else if (bytes != NULL)
18938 {
18939 SYMBOL_VALUE_BYTES (sym) = bytes;
18940 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
18941 }
18942 else
18943 {
18944 SYMBOL_VALUE (sym) = value;
18945 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
18946 }
18947 }
18948
18949 /* Return the type of the die in question using its DW_AT_type attribute. */
18950
18951 static struct type *
18952 die_type (struct die_info *die, struct dwarf2_cu *cu)
18953 {
18954 struct attribute *type_attr;
18955
18956 type_attr = dwarf2_attr (die, DW_AT_type, cu);
18957 if (!type_attr)
18958 {
18959 /* A missing DW_AT_type represents a void type. */
18960 return objfile_type (cu->objfile)->builtin_void;
18961 }
18962
18963 return lookup_die_type (die, type_attr, cu);
18964 }
18965
18966 /* True iff CU's producer generates GNAT Ada auxiliary information
18967 that allows to find parallel types through that information instead
18968 of having to do expensive parallel lookups by type name. */
18969
18970 static int
18971 need_gnat_info (struct dwarf2_cu *cu)
18972 {
18973 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
18974 of GNAT produces this auxiliary information, without any indication
18975 that it is produced. Part of enhancing the FSF version of GNAT
18976 to produce that information will be to put in place an indicator
18977 that we can use in order to determine whether the descriptive type
18978 info is available or not. One suggestion that has been made is
18979 to use a new attribute, attached to the CU die. For now, assume
18980 that the descriptive type info is not available. */
18981 return 0;
18982 }
18983
18984 /* Return the auxiliary type of the die in question using its
18985 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
18986 attribute is not present. */
18987
18988 static struct type *
18989 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
18990 {
18991 struct attribute *type_attr;
18992
18993 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
18994 if (!type_attr)
18995 return NULL;
18996
18997 return lookup_die_type (die, type_attr, cu);
18998 }
18999
19000 /* If DIE has a descriptive_type attribute, then set the TYPE's
19001 descriptive type accordingly. */
19002
19003 static void
19004 set_descriptive_type (struct type *type, struct die_info *die,
19005 struct dwarf2_cu *cu)
19006 {
19007 struct type *descriptive_type = die_descriptive_type (die, cu);
19008
19009 if (descriptive_type)
19010 {
19011 ALLOCATE_GNAT_AUX_TYPE (type);
19012 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19013 }
19014 }
19015
19016 /* Return the containing type of the die in question using its
19017 DW_AT_containing_type attribute. */
19018
19019 static struct type *
19020 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19021 {
19022 struct attribute *type_attr;
19023
19024 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19025 if (!type_attr)
19026 error (_("Dwarf Error: Problem turning containing type into gdb type "
19027 "[in module %s]"), objfile_name (cu->objfile));
19028
19029 return lookup_die_type (die, type_attr, cu);
19030 }
19031
19032 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19033
19034 static struct type *
19035 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19036 {
19037 struct objfile *objfile = dwarf2_per_objfile->objfile;
19038 char *message, *saved;
19039
19040 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19041 objfile_name (objfile),
19042 cu->header.offset.sect_off,
19043 die->offset.sect_off);
19044 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19045 message, strlen (message));
19046 xfree (message);
19047
19048 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19049 }
19050
19051 /* Look up the type of DIE in CU using its type attribute ATTR.
19052 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19053 DW_AT_containing_type.
19054 If there is no type substitute an error marker. */
19055
19056 static struct type *
19057 lookup_die_type (struct die_info *die, const struct attribute *attr,
19058 struct dwarf2_cu *cu)
19059 {
19060 struct objfile *objfile = cu->objfile;
19061 struct type *this_type;
19062
19063 gdb_assert (attr->name == DW_AT_type
19064 || attr->name == DW_AT_GNAT_descriptive_type
19065 || attr->name == DW_AT_containing_type);
19066
19067 /* First see if we have it cached. */
19068
19069 if (attr->form == DW_FORM_GNU_ref_alt)
19070 {
19071 struct dwarf2_per_cu_data *per_cu;
19072 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19073
19074 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19075 this_type = get_die_type_at_offset (offset, per_cu);
19076 }
19077 else if (attr_form_is_ref (attr))
19078 {
19079 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19080
19081 this_type = get_die_type_at_offset (offset, cu->per_cu);
19082 }
19083 else if (attr->form == DW_FORM_ref_sig8)
19084 {
19085 ULONGEST signature = DW_SIGNATURE (attr);
19086
19087 return get_signatured_type (die, signature, cu);
19088 }
19089 else
19090 {
19091 complaint (&symfile_complaints,
19092 _("Dwarf Error: Bad type attribute %s in DIE"
19093 " at 0x%x [in module %s]"),
19094 dwarf_attr_name (attr->name), die->offset.sect_off,
19095 objfile_name (objfile));
19096 return build_error_marker_type (cu, die);
19097 }
19098
19099 /* If not cached we need to read it in. */
19100
19101 if (this_type == NULL)
19102 {
19103 struct die_info *type_die = NULL;
19104 struct dwarf2_cu *type_cu = cu;
19105
19106 if (attr_form_is_ref (attr))
19107 type_die = follow_die_ref (die, attr, &type_cu);
19108 if (type_die == NULL)
19109 return build_error_marker_type (cu, die);
19110 /* If we find the type now, it's probably because the type came
19111 from an inter-CU reference and the type's CU got expanded before
19112 ours. */
19113 this_type = read_type_die (type_die, type_cu);
19114 }
19115
19116 /* If we still don't have a type use an error marker. */
19117
19118 if (this_type == NULL)
19119 return build_error_marker_type (cu, die);
19120
19121 return this_type;
19122 }
19123
19124 /* Return the type in DIE, CU.
19125 Returns NULL for invalid types.
19126
19127 This first does a lookup in die_type_hash,
19128 and only reads the die in if necessary.
19129
19130 NOTE: This can be called when reading in partial or full symbols. */
19131
19132 static struct type *
19133 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19134 {
19135 struct type *this_type;
19136
19137 this_type = get_die_type (die, cu);
19138 if (this_type)
19139 return this_type;
19140
19141 return read_type_die_1 (die, cu);
19142 }
19143
19144 /* Read the type in DIE, CU.
19145 Returns NULL for invalid types. */
19146
19147 static struct type *
19148 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19149 {
19150 struct type *this_type = NULL;
19151
19152 switch (die->tag)
19153 {
19154 case DW_TAG_class_type:
19155 case DW_TAG_interface_type:
19156 case DW_TAG_structure_type:
19157 case DW_TAG_union_type:
19158 this_type = read_structure_type (die, cu);
19159 break;
19160 case DW_TAG_enumeration_type:
19161 this_type = read_enumeration_type (die, cu);
19162 break;
19163 case DW_TAG_subprogram:
19164 case DW_TAG_subroutine_type:
19165 case DW_TAG_inlined_subroutine:
19166 this_type = read_subroutine_type (die, cu);
19167 break;
19168 case DW_TAG_array_type:
19169 this_type = read_array_type (die, cu);
19170 break;
19171 case DW_TAG_set_type:
19172 this_type = read_set_type (die, cu);
19173 break;
19174 case DW_TAG_pointer_type:
19175 this_type = read_tag_pointer_type (die, cu);
19176 break;
19177 case DW_TAG_ptr_to_member_type:
19178 this_type = read_tag_ptr_to_member_type (die, cu);
19179 break;
19180 case DW_TAG_reference_type:
19181 this_type = read_tag_reference_type (die, cu);
19182 break;
19183 case DW_TAG_const_type:
19184 this_type = read_tag_const_type (die, cu);
19185 break;
19186 case DW_TAG_volatile_type:
19187 this_type = read_tag_volatile_type (die, cu);
19188 break;
19189 case DW_TAG_restrict_type:
19190 this_type = read_tag_restrict_type (die, cu);
19191 break;
19192 case DW_TAG_string_type:
19193 this_type = read_tag_string_type (die, cu);
19194 break;
19195 case DW_TAG_typedef:
19196 this_type = read_typedef (die, cu);
19197 break;
19198 case DW_TAG_subrange_type:
19199 this_type = read_subrange_type (die, cu);
19200 break;
19201 case DW_TAG_base_type:
19202 this_type = read_base_type (die, cu);
19203 break;
19204 case DW_TAG_unspecified_type:
19205 this_type = read_unspecified_type (die, cu);
19206 break;
19207 case DW_TAG_namespace:
19208 this_type = read_namespace_type (die, cu);
19209 break;
19210 case DW_TAG_module:
19211 this_type = read_module_type (die, cu);
19212 break;
19213 case DW_TAG_atomic_type:
19214 this_type = read_tag_atomic_type (die, cu);
19215 break;
19216 default:
19217 complaint (&symfile_complaints,
19218 _("unexpected tag in read_type_die: '%s'"),
19219 dwarf_tag_name (die->tag));
19220 break;
19221 }
19222
19223 return this_type;
19224 }
19225
19226 /* See if we can figure out if the class lives in a namespace. We do
19227 this by looking for a member function; its demangled name will
19228 contain namespace info, if there is any.
19229 Return the computed name or NULL.
19230 Space for the result is allocated on the objfile's obstack.
19231 This is the full-die version of guess_partial_die_structure_name.
19232 In this case we know DIE has no useful parent. */
19233
19234 static char *
19235 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19236 {
19237 struct die_info *spec_die;
19238 struct dwarf2_cu *spec_cu;
19239 struct die_info *child;
19240
19241 spec_cu = cu;
19242 spec_die = die_specification (die, &spec_cu);
19243 if (spec_die != NULL)
19244 {
19245 die = spec_die;
19246 cu = spec_cu;
19247 }
19248
19249 for (child = die->child;
19250 child != NULL;
19251 child = child->sibling)
19252 {
19253 if (child->tag == DW_TAG_subprogram)
19254 {
19255 const char *linkage_name;
19256
19257 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19258 if (linkage_name == NULL)
19259 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19260 cu);
19261 if (linkage_name != NULL)
19262 {
19263 char *actual_name
19264 = language_class_name_from_physname (cu->language_defn,
19265 linkage_name);
19266 char *name = NULL;
19267
19268 if (actual_name != NULL)
19269 {
19270 const char *die_name = dwarf2_name (die, cu);
19271
19272 if (die_name != NULL
19273 && strcmp (die_name, actual_name) != 0)
19274 {
19275 /* Strip off the class name from the full name.
19276 We want the prefix. */
19277 int die_name_len = strlen (die_name);
19278 int actual_name_len = strlen (actual_name);
19279
19280 /* Test for '::' as a sanity check. */
19281 if (actual_name_len > die_name_len + 2
19282 && actual_name[actual_name_len
19283 - die_name_len - 1] == ':')
19284 name = (char *) obstack_copy0 (
19285 &cu->objfile->per_bfd->storage_obstack,
19286 actual_name, actual_name_len - die_name_len - 2);
19287 }
19288 }
19289 xfree (actual_name);
19290 return name;
19291 }
19292 }
19293 }
19294
19295 return NULL;
19296 }
19297
19298 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19299 prefix part in such case. See
19300 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19301
19302 static char *
19303 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19304 {
19305 struct attribute *attr;
19306 const char *base;
19307
19308 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19309 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19310 return NULL;
19311
19312 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19313 return NULL;
19314
19315 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19316 if (attr == NULL)
19317 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19318 if (attr == NULL || DW_STRING (attr) == NULL)
19319 return NULL;
19320
19321 /* dwarf2_name had to be already called. */
19322 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19323
19324 /* Strip the base name, keep any leading namespaces/classes. */
19325 base = strrchr (DW_STRING (attr), ':');
19326 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19327 return "";
19328
19329 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19330 DW_STRING (attr),
19331 &base[-1] - DW_STRING (attr));
19332 }
19333
19334 /* Return the name of the namespace/class that DIE is defined within,
19335 or "" if we can't tell. The caller should not xfree the result.
19336
19337 For example, if we're within the method foo() in the following
19338 code:
19339
19340 namespace N {
19341 class C {
19342 void foo () {
19343 }
19344 };
19345 }
19346
19347 then determine_prefix on foo's die will return "N::C". */
19348
19349 static const char *
19350 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19351 {
19352 struct die_info *parent, *spec_die;
19353 struct dwarf2_cu *spec_cu;
19354 struct type *parent_type;
19355 char *retval;
19356
19357 if (cu->language != language_cplus && cu->language != language_java
19358 && cu->language != language_fortran && cu->language != language_d
19359 && cu->language != language_rust)
19360 return "";
19361
19362 retval = anonymous_struct_prefix (die, cu);
19363 if (retval)
19364 return retval;
19365
19366 /* We have to be careful in the presence of DW_AT_specification.
19367 For example, with GCC 3.4, given the code
19368
19369 namespace N {
19370 void foo() {
19371 // Definition of N::foo.
19372 }
19373 }
19374
19375 then we'll have a tree of DIEs like this:
19376
19377 1: DW_TAG_compile_unit
19378 2: DW_TAG_namespace // N
19379 3: DW_TAG_subprogram // declaration of N::foo
19380 4: DW_TAG_subprogram // definition of N::foo
19381 DW_AT_specification // refers to die #3
19382
19383 Thus, when processing die #4, we have to pretend that we're in
19384 the context of its DW_AT_specification, namely the contex of die
19385 #3. */
19386 spec_cu = cu;
19387 spec_die = die_specification (die, &spec_cu);
19388 if (spec_die == NULL)
19389 parent = die->parent;
19390 else
19391 {
19392 parent = spec_die->parent;
19393 cu = spec_cu;
19394 }
19395
19396 if (parent == NULL)
19397 return "";
19398 else if (parent->building_fullname)
19399 {
19400 const char *name;
19401 const char *parent_name;
19402
19403 /* It has been seen on RealView 2.2 built binaries,
19404 DW_TAG_template_type_param types actually _defined_ as
19405 children of the parent class:
19406
19407 enum E {};
19408 template class <class Enum> Class{};
19409 Class<enum E> class_e;
19410
19411 1: DW_TAG_class_type (Class)
19412 2: DW_TAG_enumeration_type (E)
19413 3: DW_TAG_enumerator (enum1:0)
19414 3: DW_TAG_enumerator (enum2:1)
19415 ...
19416 2: DW_TAG_template_type_param
19417 DW_AT_type DW_FORM_ref_udata (E)
19418
19419 Besides being broken debug info, it can put GDB into an
19420 infinite loop. Consider:
19421
19422 When we're building the full name for Class<E>, we'll start
19423 at Class, and go look over its template type parameters,
19424 finding E. We'll then try to build the full name of E, and
19425 reach here. We're now trying to build the full name of E,
19426 and look over the parent DIE for containing scope. In the
19427 broken case, if we followed the parent DIE of E, we'd again
19428 find Class, and once again go look at its template type
19429 arguments, etc., etc. Simply don't consider such parent die
19430 as source-level parent of this die (it can't be, the language
19431 doesn't allow it), and break the loop here. */
19432 name = dwarf2_name (die, cu);
19433 parent_name = dwarf2_name (parent, cu);
19434 complaint (&symfile_complaints,
19435 _("template param type '%s' defined within parent '%s'"),
19436 name ? name : "<unknown>",
19437 parent_name ? parent_name : "<unknown>");
19438 return "";
19439 }
19440 else
19441 switch (parent->tag)
19442 {
19443 case DW_TAG_namespace:
19444 parent_type = read_type_die (parent, cu);
19445 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19446 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19447 Work around this problem here. */
19448 if (cu->language == language_cplus
19449 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19450 return "";
19451 /* We give a name to even anonymous namespaces. */
19452 return TYPE_TAG_NAME (parent_type);
19453 case DW_TAG_class_type:
19454 case DW_TAG_interface_type:
19455 case DW_TAG_structure_type:
19456 case DW_TAG_union_type:
19457 case DW_TAG_module:
19458 parent_type = read_type_die (parent, cu);
19459 if (TYPE_TAG_NAME (parent_type) != NULL)
19460 return TYPE_TAG_NAME (parent_type);
19461 else
19462 /* An anonymous structure is only allowed non-static data
19463 members; no typedefs, no member functions, et cetera.
19464 So it does not need a prefix. */
19465 return "";
19466 case DW_TAG_compile_unit:
19467 case DW_TAG_partial_unit:
19468 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19469 if (cu->language == language_cplus
19470 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19471 && die->child != NULL
19472 && (die->tag == DW_TAG_class_type
19473 || die->tag == DW_TAG_structure_type
19474 || die->tag == DW_TAG_union_type))
19475 {
19476 char *name = guess_full_die_structure_name (die, cu);
19477 if (name != NULL)
19478 return name;
19479 }
19480 return "";
19481 case DW_TAG_enumeration_type:
19482 parent_type = read_type_die (parent, cu);
19483 if (TYPE_DECLARED_CLASS (parent_type))
19484 {
19485 if (TYPE_TAG_NAME (parent_type) != NULL)
19486 return TYPE_TAG_NAME (parent_type);
19487 return "";
19488 }
19489 /* Fall through. */
19490 default:
19491 return determine_prefix (parent, cu);
19492 }
19493 }
19494
19495 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19496 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19497 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19498 an obconcat, otherwise allocate storage for the result. The CU argument is
19499 used to determine the language and hence, the appropriate separator. */
19500
19501 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19502
19503 static char *
19504 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19505 int physname, struct dwarf2_cu *cu)
19506 {
19507 const char *lead = "";
19508 const char *sep;
19509
19510 if (suffix == NULL || suffix[0] == '\0'
19511 || prefix == NULL || prefix[0] == '\0')
19512 sep = "";
19513 else if (cu->language == language_java)
19514 sep = ".";
19515 else if (cu->language == language_d)
19516 {
19517 /* For D, the 'main' function could be defined in any module, but it
19518 should never be prefixed. */
19519 if (strcmp (suffix, "D main") == 0)
19520 {
19521 prefix = "";
19522 sep = "";
19523 }
19524 else
19525 sep = ".";
19526 }
19527 else if (cu->language == language_fortran && physname)
19528 {
19529 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19530 DW_AT_MIPS_linkage_name is preferred and used instead. */
19531
19532 lead = "__";
19533 sep = "_MOD_";
19534 }
19535 else
19536 sep = "::";
19537
19538 if (prefix == NULL)
19539 prefix = "";
19540 if (suffix == NULL)
19541 suffix = "";
19542
19543 if (obs == NULL)
19544 {
19545 char *retval
19546 = ((char *)
19547 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
19548
19549 strcpy (retval, lead);
19550 strcat (retval, prefix);
19551 strcat (retval, sep);
19552 strcat (retval, suffix);
19553 return retval;
19554 }
19555 else
19556 {
19557 /* We have an obstack. */
19558 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
19559 }
19560 }
19561
19562 /* Return sibling of die, NULL if no sibling. */
19563
19564 static struct die_info *
19565 sibling_die (struct die_info *die)
19566 {
19567 return die->sibling;
19568 }
19569
19570 /* Get name of a die, return NULL if not found. */
19571
19572 static const char *
19573 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
19574 struct obstack *obstack)
19575 {
19576 if (name && cu->language == language_cplus)
19577 {
19578 char *canon_name = cp_canonicalize_string (name);
19579
19580 if (canon_name != NULL)
19581 {
19582 if (strcmp (canon_name, name) != 0)
19583 name = (const char *) obstack_copy0 (obstack, canon_name,
19584 strlen (canon_name));
19585 xfree (canon_name);
19586 }
19587 }
19588
19589 return name;
19590 }
19591
19592 /* Get name of a die, return NULL if not found.
19593 Anonymous namespaces are converted to their magic string. */
19594
19595 static const char *
19596 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
19597 {
19598 struct attribute *attr;
19599
19600 attr = dwarf2_attr (die, DW_AT_name, cu);
19601 if ((!attr || !DW_STRING (attr))
19602 && die->tag != DW_TAG_namespace
19603 && die->tag != DW_TAG_class_type
19604 && die->tag != DW_TAG_interface_type
19605 && die->tag != DW_TAG_structure_type
19606 && die->tag != DW_TAG_union_type)
19607 return NULL;
19608
19609 switch (die->tag)
19610 {
19611 case DW_TAG_compile_unit:
19612 case DW_TAG_partial_unit:
19613 /* Compilation units have a DW_AT_name that is a filename, not
19614 a source language identifier. */
19615 case DW_TAG_enumeration_type:
19616 case DW_TAG_enumerator:
19617 /* These tags always have simple identifiers already; no need
19618 to canonicalize them. */
19619 return DW_STRING (attr);
19620
19621 case DW_TAG_namespace:
19622 if (attr != NULL && DW_STRING (attr) != NULL)
19623 return DW_STRING (attr);
19624 return CP_ANONYMOUS_NAMESPACE_STR;
19625
19626 case DW_TAG_subprogram:
19627 /* Java constructors will all be named "<init>", so return
19628 the class name when we see this special case. */
19629 if (cu->language == language_java
19630 && DW_STRING (attr) != NULL
19631 && strcmp (DW_STRING (attr), "<init>") == 0)
19632 {
19633 struct dwarf2_cu *spec_cu = cu;
19634 struct die_info *spec_die;
19635
19636 /* GCJ will output '<init>' for Java constructor names.
19637 For this special case, return the name of the parent class. */
19638
19639 /* GCJ may output subprogram DIEs with AT_specification set.
19640 If so, use the name of the specified DIE. */
19641 spec_die = die_specification (die, &spec_cu);
19642 if (spec_die != NULL)
19643 return dwarf2_name (spec_die, spec_cu);
19644
19645 do
19646 {
19647 die = die->parent;
19648 if (die->tag == DW_TAG_class_type)
19649 return dwarf2_name (die, cu);
19650 }
19651 while (die->tag != DW_TAG_compile_unit
19652 && die->tag != DW_TAG_partial_unit);
19653 }
19654 break;
19655
19656 case DW_TAG_class_type:
19657 case DW_TAG_interface_type:
19658 case DW_TAG_structure_type:
19659 case DW_TAG_union_type:
19660 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
19661 structures or unions. These were of the form "._%d" in GCC 4.1,
19662 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
19663 and GCC 4.4. We work around this problem by ignoring these. */
19664 if (attr && DW_STRING (attr)
19665 && (startswith (DW_STRING (attr), "._")
19666 || startswith (DW_STRING (attr), "<anonymous")))
19667 return NULL;
19668
19669 /* GCC might emit a nameless typedef that has a linkage name. See
19670 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19671 if (!attr || DW_STRING (attr) == NULL)
19672 {
19673 char *demangled = NULL;
19674
19675 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19676 if (attr == NULL)
19677 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19678
19679 if (attr == NULL || DW_STRING (attr) == NULL)
19680 return NULL;
19681
19682 /* Avoid demangling DW_STRING (attr) the second time on a second
19683 call for the same DIE. */
19684 if (!DW_STRING_IS_CANONICAL (attr))
19685 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
19686
19687 if (demangled)
19688 {
19689 const char *base;
19690
19691 /* FIXME: we already did this for the partial symbol... */
19692 DW_STRING (attr)
19693 = ((const char *)
19694 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19695 demangled, strlen (demangled)));
19696 DW_STRING_IS_CANONICAL (attr) = 1;
19697 xfree (demangled);
19698
19699 /* Strip any leading namespaces/classes, keep only the base name.
19700 DW_AT_name for named DIEs does not contain the prefixes. */
19701 base = strrchr (DW_STRING (attr), ':');
19702 if (base && base > DW_STRING (attr) && base[-1] == ':')
19703 return &base[1];
19704 else
19705 return DW_STRING (attr);
19706 }
19707 }
19708 break;
19709
19710 default:
19711 break;
19712 }
19713
19714 if (!DW_STRING_IS_CANONICAL (attr))
19715 {
19716 DW_STRING (attr)
19717 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
19718 &cu->objfile->per_bfd->storage_obstack);
19719 DW_STRING_IS_CANONICAL (attr) = 1;
19720 }
19721 return DW_STRING (attr);
19722 }
19723
19724 /* Return the die that this die in an extension of, or NULL if there
19725 is none. *EXT_CU is the CU containing DIE on input, and the CU
19726 containing the return value on output. */
19727
19728 static struct die_info *
19729 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
19730 {
19731 struct attribute *attr;
19732
19733 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
19734 if (attr == NULL)
19735 return NULL;
19736
19737 return follow_die_ref (die, attr, ext_cu);
19738 }
19739
19740 /* Convert a DIE tag into its string name. */
19741
19742 static const char *
19743 dwarf_tag_name (unsigned tag)
19744 {
19745 const char *name = get_DW_TAG_name (tag);
19746
19747 if (name == NULL)
19748 return "DW_TAG_<unknown>";
19749
19750 return name;
19751 }
19752
19753 /* Convert a DWARF attribute code into its string name. */
19754
19755 static const char *
19756 dwarf_attr_name (unsigned attr)
19757 {
19758 const char *name;
19759
19760 #ifdef MIPS /* collides with DW_AT_HP_block_index */
19761 if (attr == DW_AT_MIPS_fde)
19762 return "DW_AT_MIPS_fde";
19763 #else
19764 if (attr == DW_AT_HP_block_index)
19765 return "DW_AT_HP_block_index";
19766 #endif
19767
19768 name = get_DW_AT_name (attr);
19769
19770 if (name == NULL)
19771 return "DW_AT_<unknown>";
19772
19773 return name;
19774 }
19775
19776 /* Convert a DWARF value form code into its string name. */
19777
19778 static const char *
19779 dwarf_form_name (unsigned form)
19780 {
19781 const char *name = get_DW_FORM_name (form);
19782
19783 if (name == NULL)
19784 return "DW_FORM_<unknown>";
19785
19786 return name;
19787 }
19788
19789 static char *
19790 dwarf_bool_name (unsigned mybool)
19791 {
19792 if (mybool)
19793 return "TRUE";
19794 else
19795 return "FALSE";
19796 }
19797
19798 /* Convert a DWARF type code into its string name. */
19799
19800 static const char *
19801 dwarf_type_encoding_name (unsigned enc)
19802 {
19803 const char *name = get_DW_ATE_name (enc);
19804
19805 if (name == NULL)
19806 return "DW_ATE_<unknown>";
19807
19808 return name;
19809 }
19810
19811 static void
19812 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
19813 {
19814 unsigned int i;
19815
19816 print_spaces (indent, f);
19817 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
19818 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
19819
19820 if (die->parent != NULL)
19821 {
19822 print_spaces (indent, f);
19823 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
19824 die->parent->offset.sect_off);
19825 }
19826
19827 print_spaces (indent, f);
19828 fprintf_unfiltered (f, " has children: %s\n",
19829 dwarf_bool_name (die->child != NULL));
19830
19831 print_spaces (indent, f);
19832 fprintf_unfiltered (f, " attributes:\n");
19833
19834 for (i = 0; i < die->num_attrs; ++i)
19835 {
19836 print_spaces (indent, f);
19837 fprintf_unfiltered (f, " %s (%s) ",
19838 dwarf_attr_name (die->attrs[i].name),
19839 dwarf_form_name (die->attrs[i].form));
19840
19841 switch (die->attrs[i].form)
19842 {
19843 case DW_FORM_addr:
19844 case DW_FORM_GNU_addr_index:
19845 fprintf_unfiltered (f, "address: ");
19846 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
19847 break;
19848 case DW_FORM_block2:
19849 case DW_FORM_block4:
19850 case DW_FORM_block:
19851 case DW_FORM_block1:
19852 fprintf_unfiltered (f, "block: size %s",
19853 pulongest (DW_BLOCK (&die->attrs[i])->size));
19854 break;
19855 case DW_FORM_exprloc:
19856 fprintf_unfiltered (f, "expression: size %s",
19857 pulongest (DW_BLOCK (&die->attrs[i])->size));
19858 break;
19859 case DW_FORM_ref_addr:
19860 fprintf_unfiltered (f, "ref address: ");
19861 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
19862 break;
19863 case DW_FORM_GNU_ref_alt:
19864 fprintf_unfiltered (f, "alt ref address: ");
19865 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
19866 break;
19867 case DW_FORM_ref1:
19868 case DW_FORM_ref2:
19869 case DW_FORM_ref4:
19870 case DW_FORM_ref8:
19871 case DW_FORM_ref_udata:
19872 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
19873 (long) (DW_UNSND (&die->attrs[i])));
19874 break;
19875 case DW_FORM_data1:
19876 case DW_FORM_data2:
19877 case DW_FORM_data4:
19878 case DW_FORM_data8:
19879 case DW_FORM_udata:
19880 case DW_FORM_sdata:
19881 fprintf_unfiltered (f, "constant: %s",
19882 pulongest (DW_UNSND (&die->attrs[i])));
19883 break;
19884 case DW_FORM_sec_offset:
19885 fprintf_unfiltered (f, "section offset: %s",
19886 pulongest (DW_UNSND (&die->attrs[i])));
19887 break;
19888 case DW_FORM_ref_sig8:
19889 fprintf_unfiltered (f, "signature: %s",
19890 hex_string (DW_SIGNATURE (&die->attrs[i])));
19891 break;
19892 case DW_FORM_string:
19893 case DW_FORM_strp:
19894 case DW_FORM_GNU_str_index:
19895 case DW_FORM_GNU_strp_alt:
19896 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
19897 DW_STRING (&die->attrs[i])
19898 ? DW_STRING (&die->attrs[i]) : "",
19899 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
19900 break;
19901 case DW_FORM_flag:
19902 if (DW_UNSND (&die->attrs[i]))
19903 fprintf_unfiltered (f, "flag: TRUE");
19904 else
19905 fprintf_unfiltered (f, "flag: FALSE");
19906 break;
19907 case DW_FORM_flag_present:
19908 fprintf_unfiltered (f, "flag: TRUE");
19909 break;
19910 case DW_FORM_indirect:
19911 /* The reader will have reduced the indirect form to
19912 the "base form" so this form should not occur. */
19913 fprintf_unfiltered (f,
19914 "unexpected attribute form: DW_FORM_indirect");
19915 break;
19916 default:
19917 fprintf_unfiltered (f, "unsupported attribute form: %d.",
19918 die->attrs[i].form);
19919 break;
19920 }
19921 fprintf_unfiltered (f, "\n");
19922 }
19923 }
19924
19925 static void
19926 dump_die_for_error (struct die_info *die)
19927 {
19928 dump_die_shallow (gdb_stderr, 0, die);
19929 }
19930
19931 static void
19932 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
19933 {
19934 int indent = level * 4;
19935
19936 gdb_assert (die != NULL);
19937
19938 if (level >= max_level)
19939 return;
19940
19941 dump_die_shallow (f, indent, die);
19942
19943 if (die->child != NULL)
19944 {
19945 print_spaces (indent, f);
19946 fprintf_unfiltered (f, " Children:");
19947 if (level + 1 < max_level)
19948 {
19949 fprintf_unfiltered (f, "\n");
19950 dump_die_1 (f, level + 1, max_level, die->child);
19951 }
19952 else
19953 {
19954 fprintf_unfiltered (f,
19955 " [not printed, max nesting level reached]\n");
19956 }
19957 }
19958
19959 if (die->sibling != NULL && level > 0)
19960 {
19961 dump_die_1 (f, level, max_level, die->sibling);
19962 }
19963 }
19964
19965 /* This is called from the pdie macro in gdbinit.in.
19966 It's not static so gcc will keep a copy callable from gdb. */
19967
19968 void
19969 dump_die (struct die_info *die, int max_level)
19970 {
19971 dump_die_1 (gdb_stdlog, 0, max_level, die);
19972 }
19973
19974 static void
19975 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
19976 {
19977 void **slot;
19978
19979 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
19980 INSERT);
19981
19982 *slot = die;
19983 }
19984
19985 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
19986 required kind. */
19987
19988 static sect_offset
19989 dwarf2_get_ref_die_offset (const struct attribute *attr)
19990 {
19991 sect_offset retval = { DW_UNSND (attr) };
19992
19993 if (attr_form_is_ref (attr))
19994 return retval;
19995
19996 retval.sect_off = 0;
19997 complaint (&symfile_complaints,
19998 _("unsupported die ref attribute form: '%s'"),
19999 dwarf_form_name (attr->form));
20000 return retval;
20001 }
20002
20003 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20004 * the value held by the attribute is not constant. */
20005
20006 static LONGEST
20007 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20008 {
20009 if (attr->form == DW_FORM_sdata)
20010 return DW_SND (attr);
20011 else if (attr->form == DW_FORM_udata
20012 || attr->form == DW_FORM_data1
20013 || attr->form == DW_FORM_data2
20014 || attr->form == DW_FORM_data4
20015 || attr->form == DW_FORM_data8)
20016 return DW_UNSND (attr);
20017 else
20018 {
20019 complaint (&symfile_complaints,
20020 _("Attribute value is not a constant (%s)"),
20021 dwarf_form_name (attr->form));
20022 return default_value;
20023 }
20024 }
20025
20026 /* Follow reference or signature attribute ATTR of SRC_DIE.
20027 On entry *REF_CU is the CU of SRC_DIE.
20028 On exit *REF_CU is the CU of the result. */
20029
20030 static struct die_info *
20031 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20032 struct dwarf2_cu **ref_cu)
20033 {
20034 struct die_info *die;
20035
20036 if (attr_form_is_ref (attr))
20037 die = follow_die_ref (src_die, attr, ref_cu);
20038 else if (attr->form == DW_FORM_ref_sig8)
20039 die = follow_die_sig (src_die, attr, ref_cu);
20040 else
20041 {
20042 dump_die_for_error (src_die);
20043 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20044 objfile_name ((*ref_cu)->objfile));
20045 }
20046
20047 return die;
20048 }
20049
20050 /* Follow reference OFFSET.
20051 On entry *REF_CU is the CU of the source die referencing OFFSET.
20052 On exit *REF_CU is the CU of the result.
20053 Returns NULL if OFFSET is invalid. */
20054
20055 static struct die_info *
20056 follow_die_offset (sect_offset offset, int offset_in_dwz,
20057 struct dwarf2_cu **ref_cu)
20058 {
20059 struct die_info temp_die;
20060 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20061
20062 gdb_assert (cu->per_cu != NULL);
20063
20064 target_cu = cu;
20065
20066 if (cu->per_cu->is_debug_types)
20067 {
20068 /* .debug_types CUs cannot reference anything outside their CU.
20069 If they need to, they have to reference a signatured type via
20070 DW_FORM_ref_sig8. */
20071 if (! offset_in_cu_p (&cu->header, offset))
20072 return NULL;
20073 }
20074 else if (offset_in_dwz != cu->per_cu->is_dwz
20075 || ! offset_in_cu_p (&cu->header, offset))
20076 {
20077 struct dwarf2_per_cu_data *per_cu;
20078
20079 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
20080 cu->objfile);
20081
20082 /* If necessary, add it to the queue and load its DIEs. */
20083 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20084 load_full_comp_unit (per_cu, cu->language);
20085
20086 target_cu = per_cu->cu;
20087 }
20088 else if (cu->dies == NULL)
20089 {
20090 /* We're loading full DIEs during partial symbol reading. */
20091 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20092 load_full_comp_unit (cu->per_cu, language_minimal);
20093 }
20094
20095 *ref_cu = target_cu;
20096 temp_die.offset = offset;
20097 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20098 &temp_die, offset.sect_off);
20099 }
20100
20101 /* Follow reference attribute ATTR of SRC_DIE.
20102 On entry *REF_CU is the CU of SRC_DIE.
20103 On exit *REF_CU is the CU of the result. */
20104
20105 static struct die_info *
20106 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20107 struct dwarf2_cu **ref_cu)
20108 {
20109 sect_offset offset = dwarf2_get_ref_die_offset (attr);
20110 struct dwarf2_cu *cu = *ref_cu;
20111 struct die_info *die;
20112
20113 die = follow_die_offset (offset,
20114 (attr->form == DW_FORM_GNU_ref_alt
20115 || cu->per_cu->is_dwz),
20116 ref_cu);
20117 if (!die)
20118 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20119 "at 0x%x [in module %s]"),
20120 offset.sect_off, src_die->offset.sect_off,
20121 objfile_name (cu->objfile));
20122
20123 return die;
20124 }
20125
20126 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20127 Returned value is intended for DW_OP_call*. Returned
20128 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20129
20130 struct dwarf2_locexpr_baton
20131 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20132 struct dwarf2_per_cu_data *per_cu,
20133 CORE_ADDR (*get_frame_pc) (void *baton),
20134 void *baton)
20135 {
20136 struct dwarf2_cu *cu;
20137 struct die_info *die;
20138 struct attribute *attr;
20139 struct dwarf2_locexpr_baton retval;
20140
20141 dw2_setup (per_cu->objfile);
20142
20143 if (per_cu->cu == NULL)
20144 load_cu (per_cu);
20145 cu = per_cu->cu;
20146 if (cu == NULL)
20147 {
20148 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20149 Instead just throw an error, not much else we can do. */
20150 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20151 offset.sect_off, objfile_name (per_cu->objfile));
20152 }
20153
20154 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20155 if (!die)
20156 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20157 offset.sect_off, objfile_name (per_cu->objfile));
20158
20159 attr = dwarf2_attr (die, DW_AT_location, cu);
20160 if (!attr)
20161 {
20162 /* DWARF: "If there is no such attribute, then there is no effect.".
20163 DATA is ignored if SIZE is 0. */
20164
20165 retval.data = NULL;
20166 retval.size = 0;
20167 }
20168 else if (attr_form_is_section_offset (attr))
20169 {
20170 struct dwarf2_loclist_baton loclist_baton;
20171 CORE_ADDR pc = (*get_frame_pc) (baton);
20172 size_t size;
20173
20174 fill_in_loclist_baton (cu, &loclist_baton, attr);
20175
20176 retval.data = dwarf2_find_location_expression (&loclist_baton,
20177 &size, pc);
20178 retval.size = size;
20179 }
20180 else
20181 {
20182 if (!attr_form_is_block (attr))
20183 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20184 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20185 offset.sect_off, objfile_name (per_cu->objfile));
20186
20187 retval.data = DW_BLOCK (attr)->data;
20188 retval.size = DW_BLOCK (attr)->size;
20189 }
20190 retval.per_cu = cu->per_cu;
20191
20192 age_cached_comp_units ();
20193
20194 return retval;
20195 }
20196
20197 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20198 offset. */
20199
20200 struct dwarf2_locexpr_baton
20201 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20202 struct dwarf2_per_cu_data *per_cu,
20203 CORE_ADDR (*get_frame_pc) (void *baton),
20204 void *baton)
20205 {
20206 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20207
20208 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20209 }
20210
20211 /* Write a constant of a given type as target-ordered bytes into
20212 OBSTACK. */
20213
20214 static const gdb_byte *
20215 write_constant_as_bytes (struct obstack *obstack,
20216 enum bfd_endian byte_order,
20217 struct type *type,
20218 ULONGEST value,
20219 LONGEST *len)
20220 {
20221 gdb_byte *result;
20222
20223 *len = TYPE_LENGTH (type);
20224 result = (gdb_byte *) obstack_alloc (obstack, *len);
20225 store_unsigned_integer (result, *len, byte_order, value);
20226
20227 return result;
20228 }
20229
20230 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20231 pointer to the constant bytes and set LEN to the length of the
20232 data. If memory is needed, allocate it on OBSTACK. If the DIE
20233 does not have a DW_AT_const_value, return NULL. */
20234
20235 const gdb_byte *
20236 dwarf2_fetch_constant_bytes (sect_offset offset,
20237 struct dwarf2_per_cu_data *per_cu,
20238 struct obstack *obstack,
20239 LONGEST *len)
20240 {
20241 struct dwarf2_cu *cu;
20242 struct die_info *die;
20243 struct attribute *attr;
20244 const gdb_byte *result = NULL;
20245 struct type *type;
20246 LONGEST value;
20247 enum bfd_endian byte_order;
20248
20249 dw2_setup (per_cu->objfile);
20250
20251 if (per_cu->cu == NULL)
20252 load_cu (per_cu);
20253 cu = per_cu->cu;
20254 if (cu == NULL)
20255 {
20256 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20257 Instead just throw an error, not much else we can do. */
20258 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20259 offset.sect_off, objfile_name (per_cu->objfile));
20260 }
20261
20262 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20263 if (!die)
20264 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20265 offset.sect_off, objfile_name (per_cu->objfile));
20266
20267
20268 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20269 if (attr == NULL)
20270 return NULL;
20271
20272 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20273 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20274
20275 switch (attr->form)
20276 {
20277 case DW_FORM_addr:
20278 case DW_FORM_GNU_addr_index:
20279 {
20280 gdb_byte *tem;
20281
20282 *len = cu->header.addr_size;
20283 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20284 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20285 result = tem;
20286 }
20287 break;
20288 case DW_FORM_string:
20289 case DW_FORM_strp:
20290 case DW_FORM_GNU_str_index:
20291 case DW_FORM_GNU_strp_alt:
20292 /* DW_STRING is already allocated on the objfile obstack, point
20293 directly to it. */
20294 result = (const gdb_byte *) DW_STRING (attr);
20295 *len = strlen (DW_STRING (attr));
20296 break;
20297 case DW_FORM_block1:
20298 case DW_FORM_block2:
20299 case DW_FORM_block4:
20300 case DW_FORM_block:
20301 case DW_FORM_exprloc:
20302 result = DW_BLOCK (attr)->data;
20303 *len = DW_BLOCK (attr)->size;
20304 break;
20305
20306 /* The DW_AT_const_value attributes are supposed to carry the
20307 symbol's value "represented as it would be on the target
20308 architecture." By the time we get here, it's already been
20309 converted to host endianness, so we just need to sign- or
20310 zero-extend it as appropriate. */
20311 case DW_FORM_data1:
20312 type = die_type (die, cu);
20313 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20314 if (result == NULL)
20315 result = write_constant_as_bytes (obstack, byte_order,
20316 type, value, len);
20317 break;
20318 case DW_FORM_data2:
20319 type = die_type (die, cu);
20320 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20321 if (result == NULL)
20322 result = write_constant_as_bytes (obstack, byte_order,
20323 type, value, len);
20324 break;
20325 case DW_FORM_data4:
20326 type = die_type (die, cu);
20327 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20328 if (result == NULL)
20329 result = write_constant_as_bytes (obstack, byte_order,
20330 type, value, len);
20331 break;
20332 case DW_FORM_data8:
20333 type = die_type (die, cu);
20334 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20335 if (result == NULL)
20336 result = write_constant_as_bytes (obstack, byte_order,
20337 type, value, len);
20338 break;
20339
20340 case DW_FORM_sdata:
20341 type = die_type (die, cu);
20342 result = write_constant_as_bytes (obstack, byte_order,
20343 type, DW_SND (attr), len);
20344 break;
20345
20346 case DW_FORM_udata:
20347 type = die_type (die, cu);
20348 result = write_constant_as_bytes (obstack, byte_order,
20349 type, DW_UNSND (attr), len);
20350 break;
20351
20352 default:
20353 complaint (&symfile_complaints,
20354 _("unsupported const value attribute form: '%s'"),
20355 dwarf_form_name (attr->form));
20356 break;
20357 }
20358
20359 return result;
20360 }
20361
20362 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20363 PER_CU. */
20364
20365 struct type *
20366 dwarf2_get_die_type (cu_offset die_offset,
20367 struct dwarf2_per_cu_data *per_cu)
20368 {
20369 sect_offset die_offset_sect;
20370
20371 dw2_setup (per_cu->objfile);
20372
20373 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20374 return get_die_type_at_offset (die_offset_sect, per_cu);
20375 }
20376
20377 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20378 On entry *REF_CU is the CU of SRC_DIE.
20379 On exit *REF_CU is the CU of the result.
20380 Returns NULL if the referenced DIE isn't found. */
20381
20382 static struct die_info *
20383 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20384 struct dwarf2_cu **ref_cu)
20385 {
20386 struct die_info temp_die;
20387 struct dwarf2_cu *sig_cu;
20388 struct die_info *die;
20389
20390 /* While it might be nice to assert sig_type->type == NULL here,
20391 we can get here for DW_AT_imported_declaration where we need
20392 the DIE not the type. */
20393
20394 /* If necessary, add it to the queue and load its DIEs. */
20395
20396 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20397 read_signatured_type (sig_type);
20398
20399 sig_cu = sig_type->per_cu.cu;
20400 gdb_assert (sig_cu != NULL);
20401 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20402 temp_die.offset = sig_type->type_offset_in_section;
20403 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20404 temp_die.offset.sect_off);
20405 if (die)
20406 {
20407 /* For .gdb_index version 7 keep track of included TUs.
20408 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20409 if (dwarf2_per_objfile->index_table != NULL
20410 && dwarf2_per_objfile->index_table->version <= 7)
20411 {
20412 VEC_safe_push (dwarf2_per_cu_ptr,
20413 (*ref_cu)->per_cu->imported_symtabs,
20414 sig_cu->per_cu);
20415 }
20416
20417 *ref_cu = sig_cu;
20418 return die;
20419 }
20420
20421 return NULL;
20422 }
20423
20424 /* Follow signatured type referenced by ATTR in SRC_DIE.
20425 On entry *REF_CU is the CU of SRC_DIE.
20426 On exit *REF_CU is the CU of the result.
20427 The result is the DIE of the type.
20428 If the referenced type cannot be found an error is thrown. */
20429
20430 static struct die_info *
20431 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20432 struct dwarf2_cu **ref_cu)
20433 {
20434 ULONGEST signature = DW_SIGNATURE (attr);
20435 struct signatured_type *sig_type;
20436 struct die_info *die;
20437
20438 gdb_assert (attr->form == DW_FORM_ref_sig8);
20439
20440 sig_type = lookup_signatured_type (*ref_cu, signature);
20441 /* sig_type will be NULL if the signatured type is missing from
20442 the debug info. */
20443 if (sig_type == NULL)
20444 {
20445 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20446 " from DIE at 0x%x [in module %s]"),
20447 hex_string (signature), src_die->offset.sect_off,
20448 objfile_name ((*ref_cu)->objfile));
20449 }
20450
20451 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20452 if (die == NULL)
20453 {
20454 dump_die_for_error (src_die);
20455 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20456 " from DIE at 0x%x [in module %s]"),
20457 hex_string (signature), src_die->offset.sect_off,
20458 objfile_name ((*ref_cu)->objfile));
20459 }
20460
20461 return die;
20462 }
20463
20464 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20465 reading in and processing the type unit if necessary. */
20466
20467 static struct type *
20468 get_signatured_type (struct die_info *die, ULONGEST signature,
20469 struct dwarf2_cu *cu)
20470 {
20471 struct signatured_type *sig_type;
20472 struct dwarf2_cu *type_cu;
20473 struct die_info *type_die;
20474 struct type *type;
20475
20476 sig_type = lookup_signatured_type (cu, signature);
20477 /* sig_type will be NULL if the signatured type is missing from
20478 the debug info. */
20479 if (sig_type == NULL)
20480 {
20481 complaint (&symfile_complaints,
20482 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20483 " from DIE at 0x%x [in module %s]"),
20484 hex_string (signature), die->offset.sect_off,
20485 objfile_name (dwarf2_per_objfile->objfile));
20486 return build_error_marker_type (cu, die);
20487 }
20488
20489 /* If we already know the type we're done. */
20490 if (sig_type->type != NULL)
20491 return sig_type->type;
20492
20493 type_cu = cu;
20494 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20495 if (type_die != NULL)
20496 {
20497 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20498 is created. This is important, for example, because for c++ classes
20499 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20500 type = read_type_die (type_die, type_cu);
20501 if (type == NULL)
20502 {
20503 complaint (&symfile_complaints,
20504 _("Dwarf Error: Cannot build signatured type %s"
20505 " referenced from DIE at 0x%x [in module %s]"),
20506 hex_string (signature), die->offset.sect_off,
20507 objfile_name (dwarf2_per_objfile->objfile));
20508 type = build_error_marker_type (cu, die);
20509 }
20510 }
20511 else
20512 {
20513 complaint (&symfile_complaints,
20514 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20515 " from DIE at 0x%x [in module %s]"),
20516 hex_string (signature), die->offset.sect_off,
20517 objfile_name (dwarf2_per_objfile->objfile));
20518 type = build_error_marker_type (cu, die);
20519 }
20520 sig_type->type = type;
20521
20522 return type;
20523 }
20524
20525 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20526 reading in and processing the type unit if necessary. */
20527
20528 static struct type *
20529 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20530 struct dwarf2_cu *cu) /* ARI: editCase function */
20531 {
20532 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20533 if (attr_form_is_ref (attr))
20534 {
20535 struct dwarf2_cu *type_cu = cu;
20536 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20537
20538 return read_type_die (type_die, type_cu);
20539 }
20540 else if (attr->form == DW_FORM_ref_sig8)
20541 {
20542 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20543 }
20544 else
20545 {
20546 complaint (&symfile_complaints,
20547 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20548 " at 0x%x [in module %s]"),
20549 dwarf_form_name (attr->form), die->offset.sect_off,
20550 objfile_name (dwarf2_per_objfile->objfile));
20551 return build_error_marker_type (cu, die);
20552 }
20553 }
20554
20555 /* Load the DIEs associated with type unit PER_CU into memory. */
20556
20557 static void
20558 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
20559 {
20560 struct signatured_type *sig_type;
20561
20562 /* Caller is responsible for ensuring type_unit_groups don't get here. */
20563 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
20564
20565 /* We have the per_cu, but we need the signatured_type.
20566 Fortunately this is an easy translation. */
20567 gdb_assert (per_cu->is_debug_types);
20568 sig_type = (struct signatured_type *) per_cu;
20569
20570 gdb_assert (per_cu->cu == NULL);
20571
20572 read_signatured_type (sig_type);
20573
20574 gdb_assert (per_cu->cu != NULL);
20575 }
20576
20577 /* die_reader_func for read_signatured_type.
20578 This is identical to load_full_comp_unit_reader,
20579 but is kept separate for now. */
20580
20581 static void
20582 read_signatured_type_reader (const struct die_reader_specs *reader,
20583 const gdb_byte *info_ptr,
20584 struct die_info *comp_unit_die,
20585 int has_children,
20586 void *data)
20587 {
20588 struct dwarf2_cu *cu = reader->cu;
20589
20590 gdb_assert (cu->die_hash == NULL);
20591 cu->die_hash =
20592 htab_create_alloc_ex (cu->header.length / 12,
20593 die_hash,
20594 die_eq,
20595 NULL,
20596 &cu->comp_unit_obstack,
20597 hashtab_obstack_allocate,
20598 dummy_obstack_deallocate);
20599
20600 if (has_children)
20601 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
20602 &info_ptr, comp_unit_die);
20603 cu->dies = comp_unit_die;
20604 /* comp_unit_die is not stored in die_hash, no need. */
20605
20606 /* We try not to read any attributes in this function, because not
20607 all CUs needed for references have been loaded yet, and symbol
20608 table processing isn't initialized. But we have to set the CU language,
20609 or we won't be able to build types correctly.
20610 Similarly, if we do not read the producer, we can not apply
20611 producer-specific interpretation. */
20612 prepare_one_comp_unit (cu, cu->dies, language_minimal);
20613 }
20614
20615 /* Read in a signatured type and build its CU and DIEs.
20616 If the type is a stub for the real type in a DWO file,
20617 read in the real type from the DWO file as well. */
20618
20619 static void
20620 read_signatured_type (struct signatured_type *sig_type)
20621 {
20622 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
20623
20624 gdb_assert (per_cu->is_debug_types);
20625 gdb_assert (per_cu->cu == NULL);
20626
20627 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
20628 read_signatured_type_reader, NULL);
20629 sig_type->per_cu.tu_read = 1;
20630 }
20631
20632 /* Decode simple location descriptions.
20633 Given a pointer to a dwarf block that defines a location, compute
20634 the location and return the value.
20635
20636 NOTE drow/2003-11-18: This function is called in two situations
20637 now: for the address of static or global variables (partial symbols
20638 only) and for offsets into structures which are expected to be
20639 (more or less) constant. The partial symbol case should go away,
20640 and only the constant case should remain. That will let this
20641 function complain more accurately. A few special modes are allowed
20642 without complaint for global variables (for instance, global
20643 register values and thread-local values).
20644
20645 A location description containing no operations indicates that the
20646 object is optimized out. The return value is 0 for that case.
20647 FIXME drow/2003-11-16: No callers check for this case any more; soon all
20648 callers will only want a very basic result and this can become a
20649 complaint.
20650
20651 Note that stack[0] is unused except as a default error return. */
20652
20653 static CORE_ADDR
20654 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
20655 {
20656 struct objfile *objfile = cu->objfile;
20657 size_t i;
20658 size_t size = blk->size;
20659 const gdb_byte *data = blk->data;
20660 CORE_ADDR stack[64];
20661 int stacki;
20662 unsigned int bytes_read, unsnd;
20663 gdb_byte op;
20664
20665 i = 0;
20666 stacki = 0;
20667 stack[stacki] = 0;
20668 stack[++stacki] = 0;
20669
20670 while (i < size)
20671 {
20672 op = data[i++];
20673 switch (op)
20674 {
20675 case DW_OP_lit0:
20676 case DW_OP_lit1:
20677 case DW_OP_lit2:
20678 case DW_OP_lit3:
20679 case DW_OP_lit4:
20680 case DW_OP_lit5:
20681 case DW_OP_lit6:
20682 case DW_OP_lit7:
20683 case DW_OP_lit8:
20684 case DW_OP_lit9:
20685 case DW_OP_lit10:
20686 case DW_OP_lit11:
20687 case DW_OP_lit12:
20688 case DW_OP_lit13:
20689 case DW_OP_lit14:
20690 case DW_OP_lit15:
20691 case DW_OP_lit16:
20692 case DW_OP_lit17:
20693 case DW_OP_lit18:
20694 case DW_OP_lit19:
20695 case DW_OP_lit20:
20696 case DW_OP_lit21:
20697 case DW_OP_lit22:
20698 case DW_OP_lit23:
20699 case DW_OP_lit24:
20700 case DW_OP_lit25:
20701 case DW_OP_lit26:
20702 case DW_OP_lit27:
20703 case DW_OP_lit28:
20704 case DW_OP_lit29:
20705 case DW_OP_lit30:
20706 case DW_OP_lit31:
20707 stack[++stacki] = op - DW_OP_lit0;
20708 break;
20709
20710 case DW_OP_reg0:
20711 case DW_OP_reg1:
20712 case DW_OP_reg2:
20713 case DW_OP_reg3:
20714 case DW_OP_reg4:
20715 case DW_OP_reg5:
20716 case DW_OP_reg6:
20717 case DW_OP_reg7:
20718 case DW_OP_reg8:
20719 case DW_OP_reg9:
20720 case DW_OP_reg10:
20721 case DW_OP_reg11:
20722 case DW_OP_reg12:
20723 case DW_OP_reg13:
20724 case DW_OP_reg14:
20725 case DW_OP_reg15:
20726 case DW_OP_reg16:
20727 case DW_OP_reg17:
20728 case DW_OP_reg18:
20729 case DW_OP_reg19:
20730 case DW_OP_reg20:
20731 case DW_OP_reg21:
20732 case DW_OP_reg22:
20733 case DW_OP_reg23:
20734 case DW_OP_reg24:
20735 case DW_OP_reg25:
20736 case DW_OP_reg26:
20737 case DW_OP_reg27:
20738 case DW_OP_reg28:
20739 case DW_OP_reg29:
20740 case DW_OP_reg30:
20741 case DW_OP_reg31:
20742 stack[++stacki] = op - DW_OP_reg0;
20743 if (i < size)
20744 dwarf2_complex_location_expr_complaint ();
20745 break;
20746
20747 case DW_OP_regx:
20748 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
20749 i += bytes_read;
20750 stack[++stacki] = unsnd;
20751 if (i < size)
20752 dwarf2_complex_location_expr_complaint ();
20753 break;
20754
20755 case DW_OP_addr:
20756 stack[++stacki] = read_address (objfile->obfd, &data[i],
20757 cu, &bytes_read);
20758 i += bytes_read;
20759 break;
20760
20761 case DW_OP_const1u:
20762 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
20763 i += 1;
20764 break;
20765
20766 case DW_OP_const1s:
20767 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
20768 i += 1;
20769 break;
20770
20771 case DW_OP_const2u:
20772 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
20773 i += 2;
20774 break;
20775
20776 case DW_OP_const2s:
20777 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
20778 i += 2;
20779 break;
20780
20781 case DW_OP_const4u:
20782 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
20783 i += 4;
20784 break;
20785
20786 case DW_OP_const4s:
20787 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
20788 i += 4;
20789 break;
20790
20791 case DW_OP_const8u:
20792 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
20793 i += 8;
20794 break;
20795
20796 case DW_OP_constu:
20797 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
20798 &bytes_read);
20799 i += bytes_read;
20800 break;
20801
20802 case DW_OP_consts:
20803 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
20804 i += bytes_read;
20805 break;
20806
20807 case DW_OP_dup:
20808 stack[stacki + 1] = stack[stacki];
20809 stacki++;
20810 break;
20811
20812 case DW_OP_plus:
20813 stack[stacki - 1] += stack[stacki];
20814 stacki--;
20815 break;
20816
20817 case DW_OP_plus_uconst:
20818 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
20819 &bytes_read);
20820 i += bytes_read;
20821 break;
20822
20823 case DW_OP_minus:
20824 stack[stacki - 1] -= stack[stacki];
20825 stacki--;
20826 break;
20827
20828 case DW_OP_deref:
20829 /* If we're not the last op, then we definitely can't encode
20830 this using GDB's address_class enum. This is valid for partial
20831 global symbols, although the variable's address will be bogus
20832 in the psymtab. */
20833 if (i < size)
20834 dwarf2_complex_location_expr_complaint ();
20835 break;
20836
20837 case DW_OP_GNU_push_tls_address:
20838 case DW_OP_form_tls_address:
20839 /* The top of the stack has the offset from the beginning
20840 of the thread control block at which the variable is located. */
20841 /* Nothing should follow this operator, so the top of stack would
20842 be returned. */
20843 /* This is valid for partial global symbols, but the variable's
20844 address will be bogus in the psymtab. Make it always at least
20845 non-zero to not look as a variable garbage collected by linker
20846 which have DW_OP_addr 0. */
20847 if (i < size)
20848 dwarf2_complex_location_expr_complaint ();
20849 stack[stacki]++;
20850 break;
20851
20852 case DW_OP_GNU_uninit:
20853 break;
20854
20855 case DW_OP_GNU_addr_index:
20856 case DW_OP_GNU_const_index:
20857 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
20858 &bytes_read);
20859 i += bytes_read;
20860 break;
20861
20862 default:
20863 {
20864 const char *name = get_DW_OP_name (op);
20865
20866 if (name)
20867 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
20868 name);
20869 else
20870 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
20871 op);
20872 }
20873
20874 return (stack[stacki]);
20875 }
20876
20877 /* Enforce maximum stack depth of SIZE-1 to avoid writing
20878 outside of the allocated space. Also enforce minimum>0. */
20879 if (stacki >= ARRAY_SIZE (stack) - 1)
20880 {
20881 complaint (&symfile_complaints,
20882 _("location description stack overflow"));
20883 return 0;
20884 }
20885
20886 if (stacki <= 0)
20887 {
20888 complaint (&symfile_complaints,
20889 _("location description stack underflow"));
20890 return 0;
20891 }
20892 }
20893 return (stack[stacki]);
20894 }
20895
20896 /* memory allocation interface */
20897
20898 static struct dwarf_block *
20899 dwarf_alloc_block (struct dwarf2_cu *cu)
20900 {
20901 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
20902 }
20903
20904 static struct die_info *
20905 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
20906 {
20907 struct die_info *die;
20908 size_t size = sizeof (struct die_info);
20909
20910 if (num_attrs > 1)
20911 size += (num_attrs - 1) * sizeof (struct attribute);
20912
20913 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
20914 memset (die, 0, sizeof (struct die_info));
20915 return (die);
20916 }
20917
20918 \f
20919 /* Macro support. */
20920
20921 /* Return file name relative to the compilation directory of file number I in
20922 *LH's file name table. The result is allocated using xmalloc; the caller is
20923 responsible for freeing it. */
20924
20925 static char *
20926 file_file_name (int file, struct line_header *lh)
20927 {
20928 /* Is the file number a valid index into the line header's file name
20929 table? Remember that file numbers start with one, not zero. */
20930 if (1 <= file && file <= lh->num_file_names)
20931 {
20932 struct file_entry *fe = &lh->file_names[file - 1];
20933
20934 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
20935 || lh->include_dirs == NULL)
20936 return xstrdup (fe->name);
20937 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
20938 fe->name, (char *) NULL);
20939 }
20940 else
20941 {
20942 /* The compiler produced a bogus file number. We can at least
20943 record the macro definitions made in the file, even if we
20944 won't be able to find the file by name. */
20945 char fake_name[80];
20946
20947 xsnprintf (fake_name, sizeof (fake_name),
20948 "<bad macro file number %d>", file);
20949
20950 complaint (&symfile_complaints,
20951 _("bad file number in macro information (%d)"),
20952 file);
20953
20954 return xstrdup (fake_name);
20955 }
20956 }
20957
20958 /* Return the full name of file number I in *LH's file name table.
20959 Use COMP_DIR as the name of the current directory of the
20960 compilation. The result is allocated using xmalloc; the caller is
20961 responsible for freeing it. */
20962 static char *
20963 file_full_name (int file, struct line_header *lh, const char *comp_dir)
20964 {
20965 /* Is the file number a valid index into the line header's file name
20966 table? Remember that file numbers start with one, not zero. */
20967 if (1 <= file && file <= lh->num_file_names)
20968 {
20969 char *relative = file_file_name (file, lh);
20970
20971 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
20972 return relative;
20973 return reconcat (relative, comp_dir, SLASH_STRING,
20974 relative, (char *) NULL);
20975 }
20976 else
20977 return file_file_name (file, lh);
20978 }
20979
20980
20981 static struct macro_source_file *
20982 macro_start_file (int file, int line,
20983 struct macro_source_file *current_file,
20984 struct line_header *lh)
20985 {
20986 /* File name relative to the compilation directory of this source file. */
20987 char *file_name = file_file_name (file, lh);
20988
20989 if (! current_file)
20990 {
20991 /* Note: We don't create a macro table for this compilation unit
20992 at all until we actually get a filename. */
20993 struct macro_table *macro_table = get_macro_table ();
20994
20995 /* If we have no current file, then this must be the start_file
20996 directive for the compilation unit's main source file. */
20997 current_file = macro_set_main (macro_table, file_name);
20998 macro_define_special (macro_table);
20999 }
21000 else
21001 current_file = macro_include (current_file, line, file_name);
21002
21003 xfree (file_name);
21004
21005 return current_file;
21006 }
21007
21008
21009 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21010 followed by a null byte. */
21011 static char *
21012 copy_string (const char *buf, int len)
21013 {
21014 char *s = (char *) xmalloc (len + 1);
21015
21016 memcpy (s, buf, len);
21017 s[len] = '\0';
21018 return s;
21019 }
21020
21021
21022 static const char *
21023 consume_improper_spaces (const char *p, const char *body)
21024 {
21025 if (*p == ' ')
21026 {
21027 complaint (&symfile_complaints,
21028 _("macro definition contains spaces "
21029 "in formal argument list:\n`%s'"),
21030 body);
21031
21032 while (*p == ' ')
21033 p++;
21034 }
21035
21036 return p;
21037 }
21038
21039
21040 static void
21041 parse_macro_definition (struct macro_source_file *file, int line,
21042 const char *body)
21043 {
21044 const char *p;
21045
21046 /* The body string takes one of two forms. For object-like macro
21047 definitions, it should be:
21048
21049 <macro name> " " <definition>
21050
21051 For function-like macro definitions, it should be:
21052
21053 <macro name> "() " <definition>
21054 or
21055 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21056
21057 Spaces may appear only where explicitly indicated, and in the
21058 <definition>.
21059
21060 The Dwarf 2 spec says that an object-like macro's name is always
21061 followed by a space, but versions of GCC around March 2002 omit
21062 the space when the macro's definition is the empty string.
21063
21064 The Dwarf 2 spec says that there should be no spaces between the
21065 formal arguments in a function-like macro's formal argument list,
21066 but versions of GCC around March 2002 include spaces after the
21067 commas. */
21068
21069
21070 /* Find the extent of the macro name. The macro name is terminated
21071 by either a space or null character (for an object-like macro) or
21072 an opening paren (for a function-like macro). */
21073 for (p = body; *p; p++)
21074 if (*p == ' ' || *p == '(')
21075 break;
21076
21077 if (*p == ' ' || *p == '\0')
21078 {
21079 /* It's an object-like macro. */
21080 int name_len = p - body;
21081 char *name = copy_string (body, name_len);
21082 const char *replacement;
21083
21084 if (*p == ' ')
21085 replacement = body + name_len + 1;
21086 else
21087 {
21088 dwarf2_macro_malformed_definition_complaint (body);
21089 replacement = body + name_len;
21090 }
21091
21092 macro_define_object (file, line, name, replacement);
21093
21094 xfree (name);
21095 }
21096 else if (*p == '(')
21097 {
21098 /* It's a function-like macro. */
21099 char *name = copy_string (body, p - body);
21100 int argc = 0;
21101 int argv_size = 1;
21102 char **argv = XNEWVEC (char *, argv_size);
21103
21104 p++;
21105
21106 p = consume_improper_spaces (p, body);
21107
21108 /* Parse the formal argument list. */
21109 while (*p && *p != ')')
21110 {
21111 /* Find the extent of the current argument name. */
21112 const char *arg_start = p;
21113
21114 while (*p && *p != ',' && *p != ')' && *p != ' ')
21115 p++;
21116
21117 if (! *p || p == arg_start)
21118 dwarf2_macro_malformed_definition_complaint (body);
21119 else
21120 {
21121 /* Make sure argv has room for the new argument. */
21122 if (argc >= argv_size)
21123 {
21124 argv_size *= 2;
21125 argv = XRESIZEVEC (char *, argv, argv_size);
21126 }
21127
21128 argv[argc++] = copy_string (arg_start, p - arg_start);
21129 }
21130
21131 p = consume_improper_spaces (p, body);
21132
21133 /* Consume the comma, if present. */
21134 if (*p == ',')
21135 {
21136 p++;
21137
21138 p = consume_improper_spaces (p, body);
21139 }
21140 }
21141
21142 if (*p == ')')
21143 {
21144 p++;
21145
21146 if (*p == ' ')
21147 /* Perfectly formed definition, no complaints. */
21148 macro_define_function (file, line, name,
21149 argc, (const char **) argv,
21150 p + 1);
21151 else if (*p == '\0')
21152 {
21153 /* Complain, but do define it. */
21154 dwarf2_macro_malformed_definition_complaint (body);
21155 macro_define_function (file, line, name,
21156 argc, (const char **) argv,
21157 p);
21158 }
21159 else
21160 /* Just complain. */
21161 dwarf2_macro_malformed_definition_complaint (body);
21162 }
21163 else
21164 /* Just complain. */
21165 dwarf2_macro_malformed_definition_complaint (body);
21166
21167 xfree (name);
21168 {
21169 int i;
21170
21171 for (i = 0; i < argc; i++)
21172 xfree (argv[i]);
21173 }
21174 xfree (argv);
21175 }
21176 else
21177 dwarf2_macro_malformed_definition_complaint (body);
21178 }
21179
21180 /* Skip some bytes from BYTES according to the form given in FORM.
21181 Returns the new pointer. */
21182
21183 static const gdb_byte *
21184 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21185 enum dwarf_form form,
21186 unsigned int offset_size,
21187 struct dwarf2_section_info *section)
21188 {
21189 unsigned int bytes_read;
21190
21191 switch (form)
21192 {
21193 case DW_FORM_data1:
21194 case DW_FORM_flag:
21195 ++bytes;
21196 break;
21197
21198 case DW_FORM_data2:
21199 bytes += 2;
21200 break;
21201
21202 case DW_FORM_data4:
21203 bytes += 4;
21204 break;
21205
21206 case DW_FORM_data8:
21207 bytes += 8;
21208 break;
21209
21210 case DW_FORM_string:
21211 read_direct_string (abfd, bytes, &bytes_read);
21212 bytes += bytes_read;
21213 break;
21214
21215 case DW_FORM_sec_offset:
21216 case DW_FORM_strp:
21217 case DW_FORM_GNU_strp_alt:
21218 bytes += offset_size;
21219 break;
21220
21221 case DW_FORM_block:
21222 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21223 bytes += bytes_read;
21224 break;
21225
21226 case DW_FORM_block1:
21227 bytes += 1 + read_1_byte (abfd, bytes);
21228 break;
21229 case DW_FORM_block2:
21230 bytes += 2 + read_2_bytes (abfd, bytes);
21231 break;
21232 case DW_FORM_block4:
21233 bytes += 4 + read_4_bytes (abfd, bytes);
21234 break;
21235
21236 case DW_FORM_sdata:
21237 case DW_FORM_udata:
21238 case DW_FORM_GNU_addr_index:
21239 case DW_FORM_GNU_str_index:
21240 bytes = gdb_skip_leb128 (bytes, buffer_end);
21241 if (bytes == NULL)
21242 {
21243 dwarf2_section_buffer_overflow_complaint (section);
21244 return NULL;
21245 }
21246 break;
21247
21248 default:
21249 {
21250 complain:
21251 complaint (&symfile_complaints,
21252 _("invalid form 0x%x in `%s'"),
21253 form, get_section_name (section));
21254 return NULL;
21255 }
21256 }
21257
21258 return bytes;
21259 }
21260
21261 /* A helper for dwarf_decode_macros that handles skipping an unknown
21262 opcode. Returns an updated pointer to the macro data buffer; or,
21263 on error, issues a complaint and returns NULL. */
21264
21265 static const gdb_byte *
21266 skip_unknown_opcode (unsigned int opcode,
21267 const gdb_byte **opcode_definitions,
21268 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21269 bfd *abfd,
21270 unsigned int offset_size,
21271 struct dwarf2_section_info *section)
21272 {
21273 unsigned int bytes_read, i;
21274 unsigned long arg;
21275 const gdb_byte *defn;
21276
21277 if (opcode_definitions[opcode] == NULL)
21278 {
21279 complaint (&symfile_complaints,
21280 _("unrecognized DW_MACFINO opcode 0x%x"),
21281 opcode);
21282 return NULL;
21283 }
21284
21285 defn = opcode_definitions[opcode];
21286 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21287 defn += bytes_read;
21288
21289 for (i = 0; i < arg; ++i)
21290 {
21291 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21292 (enum dwarf_form) defn[i], offset_size,
21293 section);
21294 if (mac_ptr == NULL)
21295 {
21296 /* skip_form_bytes already issued the complaint. */
21297 return NULL;
21298 }
21299 }
21300
21301 return mac_ptr;
21302 }
21303
21304 /* A helper function which parses the header of a macro section.
21305 If the macro section is the extended (for now called "GNU") type,
21306 then this updates *OFFSET_SIZE. Returns a pointer to just after
21307 the header, or issues a complaint and returns NULL on error. */
21308
21309 static const gdb_byte *
21310 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21311 bfd *abfd,
21312 const gdb_byte *mac_ptr,
21313 unsigned int *offset_size,
21314 int section_is_gnu)
21315 {
21316 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21317
21318 if (section_is_gnu)
21319 {
21320 unsigned int version, flags;
21321
21322 version = read_2_bytes (abfd, mac_ptr);
21323 if (version != 4)
21324 {
21325 complaint (&symfile_complaints,
21326 _("unrecognized version `%d' in .debug_macro section"),
21327 version);
21328 return NULL;
21329 }
21330 mac_ptr += 2;
21331
21332 flags = read_1_byte (abfd, mac_ptr);
21333 ++mac_ptr;
21334 *offset_size = (flags & 1) ? 8 : 4;
21335
21336 if ((flags & 2) != 0)
21337 /* We don't need the line table offset. */
21338 mac_ptr += *offset_size;
21339
21340 /* Vendor opcode descriptions. */
21341 if ((flags & 4) != 0)
21342 {
21343 unsigned int i, count;
21344
21345 count = read_1_byte (abfd, mac_ptr);
21346 ++mac_ptr;
21347 for (i = 0; i < count; ++i)
21348 {
21349 unsigned int opcode, bytes_read;
21350 unsigned long arg;
21351
21352 opcode = read_1_byte (abfd, mac_ptr);
21353 ++mac_ptr;
21354 opcode_definitions[opcode] = mac_ptr;
21355 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21356 mac_ptr += bytes_read;
21357 mac_ptr += arg;
21358 }
21359 }
21360 }
21361
21362 return mac_ptr;
21363 }
21364
21365 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21366 including DW_MACRO_GNU_transparent_include. */
21367
21368 static void
21369 dwarf_decode_macro_bytes (bfd *abfd,
21370 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21371 struct macro_source_file *current_file,
21372 struct line_header *lh,
21373 struct dwarf2_section_info *section,
21374 int section_is_gnu, int section_is_dwz,
21375 unsigned int offset_size,
21376 htab_t include_hash)
21377 {
21378 struct objfile *objfile = dwarf2_per_objfile->objfile;
21379 enum dwarf_macro_record_type macinfo_type;
21380 int at_commandline;
21381 const gdb_byte *opcode_definitions[256];
21382
21383 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21384 &offset_size, section_is_gnu);
21385 if (mac_ptr == NULL)
21386 {
21387 /* We already issued a complaint. */
21388 return;
21389 }
21390
21391 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21392 GDB is still reading the definitions from command line. First
21393 DW_MACINFO_start_file will need to be ignored as it was already executed
21394 to create CURRENT_FILE for the main source holding also the command line
21395 definitions. On first met DW_MACINFO_start_file this flag is reset to
21396 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21397
21398 at_commandline = 1;
21399
21400 do
21401 {
21402 /* Do we at least have room for a macinfo type byte? */
21403 if (mac_ptr >= mac_end)
21404 {
21405 dwarf2_section_buffer_overflow_complaint (section);
21406 break;
21407 }
21408
21409 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21410 mac_ptr++;
21411
21412 /* Note that we rely on the fact that the corresponding GNU and
21413 DWARF constants are the same. */
21414 switch (macinfo_type)
21415 {
21416 /* A zero macinfo type indicates the end of the macro
21417 information. */
21418 case 0:
21419 break;
21420
21421 case DW_MACRO_GNU_define:
21422 case DW_MACRO_GNU_undef:
21423 case DW_MACRO_GNU_define_indirect:
21424 case DW_MACRO_GNU_undef_indirect:
21425 case DW_MACRO_GNU_define_indirect_alt:
21426 case DW_MACRO_GNU_undef_indirect_alt:
21427 {
21428 unsigned int bytes_read;
21429 int line;
21430 const char *body;
21431 int is_define;
21432
21433 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21434 mac_ptr += bytes_read;
21435
21436 if (macinfo_type == DW_MACRO_GNU_define
21437 || macinfo_type == DW_MACRO_GNU_undef)
21438 {
21439 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21440 mac_ptr += bytes_read;
21441 }
21442 else
21443 {
21444 LONGEST str_offset;
21445
21446 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21447 mac_ptr += offset_size;
21448
21449 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
21450 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
21451 || section_is_dwz)
21452 {
21453 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21454
21455 body = read_indirect_string_from_dwz (dwz, str_offset);
21456 }
21457 else
21458 body = read_indirect_string_at_offset (abfd, str_offset);
21459 }
21460
21461 is_define = (macinfo_type == DW_MACRO_GNU_define
21462 || macinfo_type == DW_MACRO_GNU_define_indirect
21463 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
21464 if (! current_file)
21465 {
21466 /* DWARF violation as no main source is present. */
21467 complaint (&symfile_complaints,
21468 _("debug info with no main source gives macro %s "
21469 "on line %d: %s"),
21470 is_define ? _("definition") : _("undefinition"),
21471 line, body);
21472 break;
21473 }
21474 if ((line == 0 && !at_commandline)
21475 || (line != 0 && at_commandline))
21476 complaint (&symfile_complaints,
21477 _("debug info gives %s macro %s with %s line %d: %s"),
21478 at_commandline ? _("command-line") : _("in-file"),
21479 is_define ? _("definition") : _("undefinition"),
21480 line == 0 ? _("zero") : _("non-zero"), line, body);
21481
21482 if (is_define)
21483 parse_macro_definition (current_file, line, body);
21484 else
21485 {
21486 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
21487 || macinfo_type == DW_MACRO_GNU_undef_indirect
21488 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
21489 macro_undef (current_file, line, body);
21490 }
21491 }
21492 break;
21493
21494 case DW_MACRO_GNU_start_file:
21495 {
21496 unsigned int bytes_read;
21497 int line, file;
21498
21499 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21500 mac_ptr += bytes_read;
21501 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21502 mac_ptr += bytes_read;
21503
21504 if ((line == 0 && !at_commandline)
21505 || (line != 0 && at_commandline))
21506 complaint (&symfile_complaints,
21507 _("debug info gives source %d included "
21508 "from %s at %s line %d"),
21509 file, at_commandline ? _("command-line") : _("file"),
21510 line == 0 ? _("zero") : _("non-zero"), line);
21511
21512 if (at_commandline)
21513 {
21514 /* This DW_MACRO_GNU_start_file was executed in the
21515 pass one. */
21516 at_commandline = 0;
21517 }
21518 else
21519 current_file = macro_start_file (file, line, current_file, lh);
21520 }
21521 break;
21522
21523 case DW_MACRO_GNU_end_file:
21524 if (! current_file)
21525 complaint (&symfile_complaints,
21526 _("macro debug info has an unmatched "
21527 "`close_file' directive"));
21528 else
21529 {
21530 current_file = current_file->included_by;
21531 if (! current_file)
21532 {
21533 enum dwarf_macro_record_type next_type;
21534
21535 /* GCC circa March 2002 doesn't produce the zero
21536 type byte marking the end of the compilation
21537 unit. Complain if it's not there, but exit no
21538 matter what. */
21539
21540 /* Do we at least have room for a macinfo type byte? */
21541 if (mac_ptr >= mac_end)
21542 {
21543 dwarf2_section_buffer_overflow_complaint (section);
21544 return;
21545 }
21546
21547 /* We don't increment mac_ptr here, so this is just
21548 a look-ahead. */
21549 next_type
21550 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21551 mac_ptr);
21552 if (next_type != 0)
21553 complaint (&symfile_complaints,
21554 _("no terminating 0-type entry for "
21555 "macros in `.debug_macinfo' section"));
21556
21557 return;
21558 }
21559 }
21560 break;
21561
21562 case DW_MACRO_GNU_transparent_include:
21563 case DW_MACRO_GNU_transparent_include_alt:
21564 {
21565 LONGEST offset;
21566 void **slot;
21567 bfd *include_bfd = abfd;
21568 struct dwarf2_section_info *include_section = section;
21569 const gdb_byte *include_mac_end = mac_end;
21570 int is_dwz = section_is_dwz;
21571 const gdb_byte *new_mac_ptr;
21572
21573 offset = read_offset_1 (abfd, mac_ptr, offset_size);
21574 mac_ptr += offset_size;
21575
21576 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
21577 {
21578 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21579
21580 dwarf2_read_section (objfile, &dwz->macro);
21581
21582 include_section = &dwz->macro;
21583 include_bfd = get_section_bfd_owner (include_section);
21584 include_mac_end = dwz->macro.buffer + dwz->macro.size;
21585 is_dwz = 1;
21586 }
21587
21588 new_mac_ptr = include_section->buffer + offset;
21589 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
21590
21591 if (*slot != NULL)
21592 {
21593 /* This has actually happened; see
21594 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
21595 complaint (&symfile_complaints,
21596 _("recursive DW_MACRO_GNU_transparent_include in "
21597 ".debug_macro section"));
21598 }
21599 else
21600 {
21601 *slot = (void *) new_mac_ptr;
21602
21603 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
21604 include_mac_end, current_file, lh,
21605 section, section_is_gnu, is_dwz,
21606 offset_size, include_hash);
21607
21608 htab_remove_elt (include_hash, (void *) new_mac_ptr);
21609 }
21610 }
21611 break;
21612
21613 case DW_MACINFO_vendor_ext:
21614 if (!section_is_gnu)
21615 {
21616 unsigned int bytes_read;
21617
21618 /* This reads the constant, but since we don't recognize
21619 any vendor extensions, we ignore it. */
21620 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21621 mac_ptr += bytes_read;
21622 read_direct_string (abfd, mac_ptr, &bytes_read);
21623 mac_ptr += bytes_read;
21624
21625 /* We don't recognize any vendor extensions. */
21626 break;
21627 }
21628 /* FALLTHROUGH */
21629
21630 default:
21631 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
21632 mac_ptr, mac_end, abfd, offset_size,
21633 section);
21634 if (mac_ptr == NULL)
21635 return;
21636 break;
21637 }
21638 } while (macinfo_type != 0);
21639 }
21640
21641 static void
21642 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
21643 int section_is_gnu)
21644 {
21645 struct objfile *objfile = dwarf2_per_objfile->objfile;
21646 struct line_header *lh = cu->line_header;
21647 bfd *abfd;
21648 const gdb_byte *mac_ptr, *mac_end;
21649 struct macro_source_file *current_file = 0;
21650 enum dwarf_macro_record_type macinfo_type;
21651 unsigned int offset_size = cu->header.offset_size;
21652 const gdb_byte *opcode_definitions[256];
21653 struct cleanup *cleanup;
21654 htab_t include_hash;
21655 void **slot;
21656 struct dwarf2_section_info *section;
21657 const char *section_name;
21658
21659 if (cu->dwo_unit != NULL)
21660 {
21661 if (section_is_gnu)
21662 {
21663 section = &cu->dwo_unit->dwo_file->sections.macro;
21664 section_name = ".debug_macro.dwo";
21665 }
21666 else
21667 {
21668 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21669 section_name = ".debug_macinfo.dwo";
21670 }
21671 }
21672 else
21673 {
21674 if (section_is_gnu)
21675 {
21676 section = &dwarf2_per_objfile->macro;
21677 section_name = ".debug_macro";
21678 }
21679 else
21680 {
21681 section = &dwarf2_per_objfile->macinfo;
21682 section_name = ".debug_macinfo";
21683 }
21684 }
21685
21686 dwarf2_read_section (objfile, section);
21687 if (section->buffer == NULL)
21688 {
21689 complaint (&symfile_complaints, _("missing %s section"), section_name);
21690 return;
21691 }
21692 abfd = get_section_bfd_owner (section);
21693
21694 /* First pass: Find the name of the base filename.
21695 This filename is needed in order to process all macros whose definition
21696 (or undefinition) comes from the command line. These macros are defined
21697 before the first DW_MACINFO_start_file entry, and yet still need to be
21698 associated to the base file.
21699
21700 To determine the base file name, we scan the macro definitions until we
21701 reach the first DW_MACINFO_start_file entry. We then initialize
21702 CURRENT_FILE accordingly so that any macro definition found before the
21703 first DW_MACINFO_start_file can still be associated to the base file. */
21704
21705 mac_ptr = section->buffer + offset;
21706 mac_end = section->buffer + section->size;
21707
21708 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21709 &offset_size, section_is_gnu);
21710 if (mac_ptr == NULL)
21711 {
21712 /* We already issued a complaint. */
21713 return;
21714 }
21715
21716 do
21717 {
21718 /* Do we at least have room for a macinfo type byte? */
21719 if (mac_ptr >= mac_end)
21720 {
21721 /* Complaint is printed during the second pass as GDB will probably
21722 stop the first pass earlier upon finding
21723 DW_MACINFO_start_file. */
21724 break;
21725 }
21726
21727 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21728 mac_ptr++;
21729
21730 /* Note that we rely on the fact that the corresponding GNU and
21731 DWARF constants are the same. */
21732 switch (macinfo_type)
21733 {
21734 /* A zero macinfo type indicates the end of the macro
21735 information. */
21736 case 0:
21737 break;
21738
21739 case DW_MACRO_GNU_define:
21740 case DW_MACRO_GNU_undef:
21741 /* Only skip the data by MAC_PTR. */
21742 {
21743 unsigned int bytes_read;
21744
21745 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21746 mac_ptr += bytes_read;
21747 read_direct_string (abfd, mac_ptr, &bytes_read);
21748 mac_ptr += bytes_read;
21749 }
21750 break;
21751
21752 case DW_MACRO_GNU_start_file:
21753 {
21754 unsigned int bytes_read;
21755 int line, file;
21756
21757 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21758 mac_ptr += bytes_read;
21759 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21760 mac_ptr += bytes_read;
21761
21762 current_file = macro_start_file (file, line, current_file, lh);
21763 }
21764 break;
21765
21766 case DW_MACRO_GNU_end_file:
21767 /* No data to skip by MAC_PTR. */
21768 break;
21769
21770 case DW_MACRO_GNU_define_indirect:
21771 case DW_MACRO_GNU_undef_indirect:
21772 case DW_MACRO_GNU_define_indirect_alt:
21773 case DW_MACRO_GNU_undef_indirect_alt:
21774 {
21775 unsigned int bytes_read;
21776
21777 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21778 mac_ptr += bytes_read;
21779 mac_ptr += offset_size;
21780 }
21781 break;
21782
21783 case DW_MACRO_GNU_transparent_include:
21784 case DW_MACRO_GNU_transparent_include_alt:
21785 /* Note that, according to the spec, a transparent include
21786 chain cannot call DW_MACRO_GNU_start_file. So, we can just
21787 skip this opcode. */
21788 mac_ptr += offset_size;
21789 break;
21790
21791 case DW_MACINFO_vendor_ext:
21792 /* Only skip the data by MAC_PTR. */
21793 if (!section_is_gnu)
21794 {
21795 unsigned int bytes_read;
21796
21797 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21798 mac_ptr += bytes_read;
21799 read_direct_string (abfd, mac_ptr, &bytes_read);
21800 mac_ptr += bytes_read;
21801 }
21802 /* FALLTHROUGH */
21803
21804 default:
21805 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
21806 mac_ptr, mac_end, abfd, offset_size,
21807 section);
21808 if (mac_ptr == NULL)
21809 return;
21810 break;
21811 }
21812 } while (macinfo_type != 0 && current_file == NULL);
21813
21814 /* Second pass: Process all entries.
21815
21816 Use the AT_COMMAND_LINE flag to determine whether we are still processing
21817 command-line macro definitions/undefinitions. This flag is unset when we
21818 reach the first DW_MACINFO_start_file entry. */
21819
21820 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
21821 NULL, xcalloc, xfree);
21822 cleanup = make_cleanup_htab_delete (include_hash);
21823 mac_ptr = section->buffer + offset;
21824 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
21825 *slot = (void *) mac_ptr;
21826 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
21827 current_file, lh, section,
21828 section_is_gnu, 0, offset_size, include_hash);
21829 do_cleanups (cleanup);
21830 }
21831
21832 /* Check if the attribute's form is a DW_FORM_block*
21833 if so return true else false. */
21834
21835 static int
21836 attr_form_is_block (const struct attribute *attr)
21837 {
21838 return (attr == NULL ? 0 :
21839 attr->form == DW_FORM_block1
21840 || attr->form == DW_FORM_block2
21841 || attr->form == DW_FORM_block4
21842 || attr->form == DW_FORM_block
21843 || attr->form == DW_FORM_exprloc);
21844 }
21845
21846 /* Return non-zero if ATTR's value is a section offset --- classes
21847 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
21848 You may use DW_UNSND (attr) to retrieve such offsets.
21849
21850 Section 7.5.4, "Attribute Encodings", explains that no attribute
21851 may have a value that belongs to more than one of these classes; it
21852 would be ambiguous if we did, because we use the same forms for all
21853 of them. */
21854
21855 static int
21856 attr_form_is_section_offset (const struct attribute *attr)
21857 {
21858 return (attr->form == DW_FORM_data4
21859 || attr->form == DW_FORM_data8
21860 || attr->form == DW_FORM_sec_offset);
21861 }
21862
21863 /* Return non-zero if ATTR's value falls in the 'constant' class, or
21864 zero otherwise. When this function returns true, you can apply
21865 dwarf2_get_attr_constant_value to it.
21866
21867 However, note that for some attributes you must check
21868 attr_form_is_section_offset before using this test. DW_FORM_data4
21869 and DW_FORM_data8 are members of both the constant class, and of
21870 the classes that contain offsets into other debug sections
21871 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
21872 that, if an attribute's can be either a constant or one of the
21873 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
21874 taken as section offsets, not constants. */
21875
21876 static int
21877 attr_form_is_constant (const struct attribute *attr)
21878 {
21879 switch (attr->form)
21880 {
21881 case DW_FORM_sdata:
21882 case DW_FORM_udata:
21883 case DW_FORM_data1:
21884 case DW_FORM_data2:
21885 case DW_FORM_data4:
21886 case DW_FORM_data8:
21887 return 1;
21888 default:
21889 return 0;
21890 }
21891 }
21892
21893
21894 /* DW_ADDR is always stored already as sect_offset; despite for the forms
21895 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
21896
21897 static int
21898 attr_form_is_ref (const struct attribute *attr)
21899 {
21900 switch (attr->form)
21901 {
21902 case DW_FORM_ref_addr:
21903 case DW_FORM_ref1:
21904 case DW_FORM_ref2:
21905 case DW_FORM_ref4:
21906 case DW_FORM_ref8:
21907 case DW_FORM_ref_udata:
21908 case DW_FORM_GNU_ref_alt:
21909 return 1;
21910 default:
21911 return 0;
21912 }
21913 }
21914
21915 /* Return the .debug_loc section to use for CU.
21916 For DWO files use .debug_loc.dwo. */
21917
21918 static struct dwarf2_section_info *
21919 cu_debug_loc_section (struct dwarf2_cu *cu)
21920 {
21921 if (cu->dwo_unit)
21922 return &cu->dwo_unit->dwo_file->sections.loc;
21923 return &dwarf2_per_objfile->loc;
21924 }
21925
21926 /* A helper function that fills in a dwarf2_loclist_baton. */
21927
21928 static void
21929 fill_in_loclist_baton (struct dwarf2_cu *cu,
21930 struct dwarf2_loclist_baton *baton,
21931 const struct attribute *attr)
21932 {
21933 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21934
21935 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
21936
21937 baton->per_cu = cu->per_cu;
21938 gdb_assert (baton->per_cu);
21939 /* We don't know how long the location list is, but make sure we
21940 don't run off the edge of the section. */
21941 baton->size = section->size - DW_UNSND (attr);
21942 baton->data = section->buffer + DW_UNSND (attr);
21943 baton->base_address = cu->base_address;
21944 baton->from_dwo = cu->dwo_unit != NULL;
21945 }
21946
21947 static void
21948 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
21949 struct dwarf2_cu *cu, int is_block)
21950 {
21951 struct objfile *objfile = dwarf2_per_objfile->objfile;
21952 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21953
21954 if (attr_form_is_section_offset (attr)
21955 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21956 the section. If so, fall through to the complaint in the
21957 other branch. */
21958 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
21959 {
21960 struct dwarf2_loclist_baton *baton;
21961
21962 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
21963
21964 fill_in_loclist_baton (cu, baton, attr);
21965
21966 if (cu->base_known == 0)
21967 complaint (&symfile_complaints,
21968 _("Location list used without "
21969 "specifying the CU base address."));
21970
21971 SYMBOL_ACLASS_INDEX (sym) = (is_block
21972 ? dwarf2_loclist_block_index
21973 : dwarf2_loclist_index);
21974 SYMBOL_LOCATION_BATON (sym) = baton;
21975 }
21976 else
21977 {
21978 struct dwarf2_locexpr_baton *baton;
21979
21980 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
21981 baton->per_cu = cu->per_cu;
21982 gdb_assert (baton->per_cu);
21983
21984 if (attr_form_is_block (attr))
21985 {
21986 /* Note that we're just copying the block's data pointer
21987 here, not the actual data. We're still pointing into the
21988 info_buffer for SYM's objfile; right now we never release
21989 that buffer, but when we do clean up properly this may
21990 need to change. */
21991 baton->size = DW_BLOCK (attr)->size;
21992 baton->data = DW_BLOCK (attr)->data;
21993 }
21994 else
21995 {
21996 dwarf2_invalid_attrib_class_complaint ("location description",
21997 SYMBOL_NATURAL_NAME (sym));
21998 baton->size = 0;
21999 }
22000
22001 SYMBOL_ACLASS_INDEX (sym) = (is_block
22002 ? dwarf2_locexpr_block_index
22003 : dwarf2_locexpr_index);
22004 SYMBOL_LOCATION_BATON (sym) = baton;
22005 }
22006 }
22007
22008 /* Return the OBJFILE associated with the compilation unit CU. If CU
22009 came from a separate debuginfo file, then the master objfile is
22010 returned. */
22011
22012 struct objfile *
22013 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22014 {
22015 struct objfile *objfile = per_cu->objfile;
22016
22017 /* Return the master objfile, so that we can report and look up the
22018 correct file containing this variable. */
22019 if (objfile->separate_debug_objfile_backlink)
22020 objfile = objfile->separate_debug_objfile_backlink;
22021
22022 return objfile;
22023 }
22024
22025 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22026 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22027 CU_HEADERP first. */
22028
22029 static const struct comp_unit_head *
22030 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22031 struct dwarf2_per_cu_data *per_cu)
22032 {
22033 const gdb_byte *info_ptr;
22034
22035 if (per_cu->cu)
22036 return &per_cu->cu->header;
22037
22038 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
22039
22040 memset (cu_headerp, 0, sizeof (*cu_headerp));
22041 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
22042
22043 return cu_headerp;
22044 }
22045
22046 /* Return the address size given in the compilation unit header for CU. */
22047
22048 int
22049 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22050 {
22051 struct comp_unit_head cu_header_local;
22052 const struct comp_unit_head *cu_headerp;
22053
22054 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22055
22056 return cu_headerp->addr_size;
22057 }
22058
22059 /* Return the offset size given in the compilation unit header for CU. */
22060
22061 int
22062 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22063 {
22064 struct comp_unit_head cu_header_local;
22065 const struct comp_unit_head *cu_headerp;
22066
22067 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22068
22069 return cu_headerp->offset_size;
22070 }
22071
22072 /* See its dwarf2loc.h declaration. */
22073
22074 int
22075 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22076 {
22077 struct comp_unit_head cu_header_local;
22078 const struct comp_unit_head *cu_headerp;
22079
22080 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22081
22082 if (cu_headerp->version == 2)
22083 return cu_headerp->addr_size;
22084 else
22085 return cu_headerp->offset_size;
22086 }
22087
22088 /* Return the text offset of the CU. The returned offset comes from
22089 this CU's objfile. If this objfile came from a separate debuginfo
22090 file, then the offset may be different from the corresponding
22091 offset in the parent objfile. */
22092
22093 CORE_ADDR
22094 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22095 {
22096 struct objfile *objfile = per_cu->objfile;
22097
22098 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22099 }
22100
22101 /* Locate the .debug_info compilation unit from CU's objfile which contains
22102 the DIE at OFFSET. Raises an error on failure. */
22103
22104 static struct dwarf2_per_cu_data *
22105 dwarf2_find_containing_comp_unit (sect_offset offset,
22106 unsigned int offset_in_dwz,
22107 struct objfile *objfile)
22108 {
22109 struct dwarf2_per_cu_data *this_cu;
22110 int low, high;
22111 const sect_offset *cu_off;
22112
22113 low = 0;
22114 high = dwarf2_per_objfile->n_comp_units - 1;
22115 while (high > low)
22116 {
22117 struct dwarf2_per_cu_data *mid_cu;
22118 int mid = low + (high - low) / 2;
22119
22120 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22121 cu_off = &mid_cu->offset;
22122 if (mid_cu->is_dwz > offset_in_dwz
22123 || (mid_cu->is_dwz == offset_in_dwz
22124 && cu_off->sect_off >= offset.sect_off))
22125 high = mid;
22126 else
22127 low = mid + 1;
22128 }
22129 gdb_assert (low == high);
22130 this_cu = dwarf2_per_objfile->all_comp_units[low];
22131 cu_off = &this_cu->offset;
22132 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
22133 {
22134 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22135 error (_("Dwarf Error: could not find partial DIE containing "
22136 "offset 0x%lx [in module %s]"),
22137 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
22138
22139 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22140 <= offset.sect_off);
22141 return dwarf2_per_objfile->all_comp_units[low-1];
22142 }
22143 else
22144 {
22145 this_cu = dwarf2_per_objfile->all_comp_units[low];
22146 if (low == dwarf2_per_objfile->n_comp_units - 1
22147 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22148 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22149 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
22150 return this_cu;
22151 }
22152 }
22153
22154 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22155
22156 static void
22157 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22158 {
22159 memset (cu, 0, sizeof (*cu));
22160 per_cu->cu = cu;
22161 cu->per_cu = per_cu;
22162 cu->objfile = per_cu->objfile;
22163 obstack_init (&cu->comp_unit_obstack);
22164 }
22165
22166 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22167
22168 static void
22169 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22170 enum language pretend_language)
22171 {
22172 struct attribute *attr;
22173
22174 /* Set the language we're debugging. */
22175 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22176 if (attr)
22177 set_cu_language (DW_UNSND (attr), cu);
22178 else
22179 {
22180 cu->language = pretend_language;
22181 cu->language_defn = language_def (cu->language);
22182 }
22183
22184 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22185 }
22186
22187 /* Release one cached compilation unit, CU. We unlink it from the tree
22188 of compilation units, but we don't remove it from the read_in_chain;
22189 the caller is responsible for that.
22190 NOTE: DATA is a void * because this function is also used as a
22191 cleanup routine. */
22192
22193 static void
22194 free_heap_comp_unit (void *data)
22195 {
22196 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22197
22198 gdb_assert (cu->per_cu != NULL);
22199 cu->per_cu->cu = NULL;
22200 cu->per_cu = NULL;
22201
22202 obstack_free (&cu->comp_unit_obstack, NULL);
22203
22204 xfree (cu);
22205 }
22206
22207 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22208 when we're finished with it. We can't free the pointer itself, but be
22209 sure to unlink it from the cache. Also release any associated storage. */
22210
22211 static void
22212 free_stack_comp_unit (void *data)
22213 {
22214 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22215
22216 gdb_assert (cu->per_cu != NULL);
22217 cu->per_cu->cu = NULL;
22218 cu->per_cu = NULL;
22219
22220 obstack_free (&cu->comp_unit_obstack, NULL);
22221 cu->partial_dies = NULL;
22222 }
22223
22224 /* Free all cached compilation units. */
22225
22226 static void
22227 free_cached_comp_units (void *data)
22228 {
22229 struct dwarf2_per_cu_data *per_cu, **last_chain;
22230
22231 per_cu = dwarf2_per_objfile->read_in_chain;
22232 last_chain = &dwarf2_per_objfile->read_in_chain;
22233 while (per_cu != NULL)
22234 {
22235 struct dwarf2_per_cu_data *next_cu;
22236
22237 next_cu = per_cu->cu->read_in_chain;
22238
22239 free_heap_comp_unit (per_cu->cu);
22240 *last_chain = next_cu;
22241
22242 per_cu = next_cu;
22243 }
22244 }
22245
22246 /* Increase the age counter on each cached compilation unit, and free
22247 any that are too old. */
22248
22249 static void
22250 age_cached_comp_units (void)
22251 {
22252 struct dwarf2_per_cu_data *per_cu, **last_chain;
22253
22254 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22255 per_cu = dwarf2_per_objfile->read_in_chain;
22256 while (per_cu != NULL)
22257 {
22258 per_cu->cu->last_used ++;
22259 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22260 dwarf2_mark (per_cu->cu);
22261 per_cu = per_cu->cu->read_in_chain;
22262 }
22263
22264 per_cu = dwarf2_per_objfile->read_in_chain;
22265 last_chain = &dwarf2_per_objfile->read_in_chain;
22266 while (per_cu != NULL)
22267 {
22268 struct dwarf2_per_cu_data *next_cu;
22269
22270 next_cu = per_cu->cu->read_in_chain;
22271
22272 if (!per_cu->cu->mark)
22273 {
22274 free_heap_comp_unit (per_cu->cu);
22275 *last_chain = next_cu;
22276 }
22277 else
22278 last_chain = &per_cu->cu->read_in_chain;
22279
22280 per_cu = next_cu;
22281 }
22282 }
22283
22284 /* Remove a single compilation unit from the cache. */
22285
22286 static void
22287 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22288 {
22289 struct dwarf2_per_cu_data *per_cu, **last_chain;
22290
22291 per_cu = dwarf2_per_objfile->read_in_chain;
22292 last_chain = &dwarf2_per_objfile->read_in_chain;
22293 while (per_cu != NULL)
22294 {
22295 struct dwarf2_per_cu_data *next_cu;
22296
22297 next_cu = per_cu->cu->read_in_chain;
22298
22299 if (per_cu == target_per_cu)
22300 {
22301 free_heap_comp_unit (per_cu->cu);
22302 per_cu->cu = NULL;
22303 *last_chain = next_cu;
22304 break;
22305 }
22306 else
22307 last_chain = &per_cu->cu->read_in_chain;
22308
22309 per_cu = next_cu;
22310 }
22311 }
22312
22313 /* Release all extra memory associated with OBJFILE. */
22314
22315 void
22316 dwarf2_free_objfile (struct objfile *objfile)
22317 {
22318 dwarf2_per_objfile
22319 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22320 dwarf2_objfile_data_key);
22321
22322 if (dwarf2_per_objfile == NULL)
22323 return;
22324
22325 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22326 free_cached_comp_units (NULL);
22327
22328 if (dwarf2_per_objfile->quick_file_names_table)
22329 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22330
22331 if (dwarf2_per_objfile->line_header_hash)
22332 htab_delete (dwarf2_per_objfile->line_header_hash);
22333
22334 /* Everything else should be on the objfile obstack. */
22335 }
22336
22337 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22338 We store these in a hash table separate from the DIEs, and preserve them
22339 when the DIEs are flushed out of cache.
22340
22341 The CU "per_cu" pointer is needed because offset alone is not enough to
22342 uniquely identify the type. A file may have multiple .debug_types sections,
22343 or the type may come from a DWO file. Furthermore, while it's more logical
22344 to use per_cu->section+offset, with Fission the section with the data is in
22345 the DWO file but we don't know that section at the point we need it.
22346 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22347 because we can enter the lookup routine, get_die_type_at_offset, from
22348 outside this file, and thus won't necessarily have PER_CU->cu.
22349 Fortunately, PER_CU is stable for the life of the objfile. */
22350
22351 struct dwarf2_per_cu_offset_and_type
22352 {
22353 const struct dwarf2_per_cu_data *per_cu;
22354 sect_offset offset;
22355 struct type *type;
22356 };
22357
22358 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22359
22360 static hashval_t
22361 per_cu_offset_and_type_hash (const void *item)
22362 {
22363 const struct dwarf2_per_cu_offset_and_type *ofs
22364 = (const struct dwarf2_per_cu_offset_and_type *) item;
22365
22366 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
22367 }
22368
22369 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22370
22371 static int
22372 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22373 {
22374 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22375 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22376 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22377 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22378
22379 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22380 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
22381 }
22382
22383 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22384 table if necessary. For convenience, return TYPE.
22385
22386 The DIEs reading must have careful ordering to:
22387 * Not cause infite loops trying to read in DIEs as a prerequisite for
22388 reading current DIE.
22389 * Not trying to dereference contents of still incompletely read in types
22390 while reading in other DIEs.
22391 * Enable referencing still incompletely read in types just by a pointer to
22392 the type without accessing its fields.
22393
22394 Therefore caller should follow these rules:
22395 * Try to fetch any prerequisite types we may need to build this DIE type
22396 before building the type and calling set_die_type.
22397 * After building type call set_die_type for current DIE as soon as
22398 possible before fetching more types to complete the current type.
22399 * Make the type as complete as possible before fetching more types. */
22400
22401 static struct type *
22402 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22403 {
22404 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22405 struct objfile *objfile = cu->objfile;
22406 struct attribute *attr;
22407 struct dynamic_prop prop;
22408
22409 /* For Ada types, make sure that the gnat-specific data is always
22410 initialized (if not already set). There are a few types where
22411 we should not be doing so, because the type-specific area is
22412 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22413 where the type-specific area is used to store the floatformat).
22414 But this is not a problem, because the gnat-specific information
22415 is actually not needed for these types. */
22416 if (need_gnat_info (cu)
22417 && TYPE_CODE (type) != TYPE_CODE_FUNC
22418 && TYPE_CODE (type) != TYPE_CODE_FLT
22419 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22420 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22421 && TYPE_CODE (type) != TYPE_CODE_METHOD
22422 && !HAVE_GNAT_AUX_INFO (type))
22423 INIT_GNAT_SPECIFIC (type);
22424
22425 /* Read DW_AT_allocated and set in type. */
22426 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22427 if (attr_form_is_block (attr))
22428 {
22429 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22430 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22431 }
22432 else if (attr != NULL)
22433 {
22434 complaint (&symfile_complaints,
22435 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22436 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22437 die->offset.sect_off);
22438 }
22439
22440 /* Read DW_AT_associated and set in type. */
22441 attr = dwarf2_attr (die, DW_AT_associated, cu);
22442 if (attr_form_is_block (attr))
22443 {
22444 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22445 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22446 }
22447 else if (attr != NULL)
22448 {
22449 complaint (&symfile_complaints,
22450 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22451 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22452 die->offset.sect_off);
22453 }
22454
22455 /* Read DW_AT_data_location and set in type. */
22456 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22457 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22458 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22459
22460 if (dwarf2_per_objfile->die_type_hash == NULL)
22461 {
22462 dwarf2_per_objfile->die_type_hash =
22463 htab_create_alloc_ex (127,
22464 per_cu_offset_and_type_hash,
22465 per_cu_offset_and_type_eq,
22466 NULL,
22467 &objfile->objfile_obstack,
22468 hashtab_obstack_allocate,
22469 dummy_obstack_deallocate);
22470 }
22471
22472 ofs.per_cu = cu->per_cu;
22473 ofs.offset = die->offset;
22474 ofs.type = type;
22475 slot = (struct dwarf2_per_cu_offset_and_type **)
22476 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
22477 if (*slot)
22478 complaint (&symfile_complaints,
22479 _("A problem internal to GDB: DIE 0x%x has type already set"),
22480 die->offset.sect_off);
22481 *slot = XOBNEW (&objfile->objfile_obstack,
22482 struct dwarf2_per_cu_offset_and_type);
22483 **slot = ofs;
22484 return type;
22485 }
22486
22487 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22488 or return NULL if the die does not have a saved type. */
22489
22490 static struct type *
22491 get_die_type_at_offset (sect_offset offset,
22492 struct dwarf2_per_cu_data *per_cu)
22493 {
22494 struct dwarf2_per_cu_offset_and_type *slot, ofs;
22495
22496 if (dwarf2_per_objfile->die_type_hash == NULL)
22497 return NULL;
22498
22499 ofs.per_cu = per_cu;
22500 ofs.offset = offset;
22501 slot = ((struct dwarf2_per_cu_offset_and_type *)
22502 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
22503 if (slot)
22504 return slot->type;
22505 else
22506 return NULL;
22507 }
22508
22509 /* Look up the type for DIE in CU in die_type_hash,
22510 or return NULL if DIE does not have a saved type. */
22511
22512 static struct type *
22513 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22514 {
22515 return get_die_type_at_offset (die->offset, cu->per_cu);
22516 }
22517
22518 /* Add a dependence relationship from CU to REF_PER_CU. */
22519
22520 static void
22521 dwarf2_add_dependence (struct dwarf2_cu *cu,
22522 struct dwarf2_per_cu_data *ref_per_cu)
22523 {
22524 void **slot;
22525
22526 if (cu->dependencies == NULL)
22527 cu->dependencies
22528 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
22529 NULL, &cu->comp_unit_obstack,
22530 hashtab_obstack_allocate,
22531 dummy_obstack_deallocate);
22532
22533 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
22534 if (*slot == NULL)
22535 *slot = ref_per_cu;
22536 }
22537
22538 /* Subroutine of dwarf2_mark to pass to htab_traverse.
22539 Set the mark field in every compilation unit in the
22540 cache that we must keep because we are keeping CU. */
22541
22542 static int
22543 dwarf2_mark_helper (void **slot, void *data)
22544 {
22545 struct dwarf2_per_cu_data *per_cu;
22546
22547 per_cu = (struct dwarf2_per_cu_data *) *slot;
22548
22549 /* cu->dependencies references may not yet have been ever read if QUIT aborts
22550 reading of the chain. As such dependencies remain valid it is not much
22551 useful to track and undo them during QUIT cleanups. */
22552 if (per_cu->cu == NULL)
22553 return 1;
22554
22555 if (per_cu->cu->mark)
22556 return 1;
22557 per_cu->cu->mark = 1;
22558
22559 if (per_cu->cu->dependencies != NULL)
22560 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
22561
22562 return 1;
22563 }
22564
22565 /* Set the mark field in CU and in every other compilation unit in the
22566 cache that we must keep because we are keeping CU. */
22567
22568 static void
22569 dwarf2_mark (struct dwarf2_cu *cu)
22570 {
22571 if (cu->mark)
22572 return;
22573 cu->mark = 1;
22574 if (cu->dependencies != NULL)
22575 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
22576 }
22577
22578 static void
22579 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
22580 {
22581 while (per_cu)
22582 {
22583 per_cu->cu->mark = 0;
22584 per_cu = per_cu->cu->read_in_chain;
22585 }
22586 }
22587
22588 /* Trivial hash function for partial_die_info: the hash value of a DIE
22589 is its offset in .debug_info for this objfile. */
22590
22591 static hashval_t
22592 partial_die_hash (const void *item)
22593 {
22594 const struct partial_die_info *part_die
22595 = (const struct partial_die_info *) item;
22596
22597 return part_die->offset.sect_off;
22598 }
22599
22600 /* Trivial comparison function for partial_die_info structures: two DIEs
22601 are equal if they have the same offset. */
22602
22603 static int
22604 partial_die_eq (const void *item_lhs, const void *item_rhs)
22605 {
22606 const struct partial_die_info *part_die_lhs
22607 = (const struct partial_die_info *) item_lhs;
22608 const struct partial_die_info *part_die_rhs
22609 = (const struct partial_die_info *) item_rhs;
22610
22611 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
22612 }
22613
22614 static struct cmd_list_element *set_dwarf_cmdlist;
22615 static struct cmd_list_element *show_dwarf_cmdlist;
22616
22617 static void
22618 set_dwarf_cmd (char *args, int from_tty)
22619 {
22620 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
22621 gdb_stdout);
22622 }
22623
22624 static void
22625 show_dwarf_cmd (char *args, int from_tty)
22626 {
22627 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
22628 }
22629
22630 /* Free data associated with OBJFILE, if necessary. */
22631
22632 static void
22633 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
22634 {
22635 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
22636 int ix;
22637
22638 /* Make sure we don't accidentally use dwarf2_per_objfile while
22639 cleaning up. */
22640 dwarf2_per_objfile = NULL;
22641
22642 for (ix = 0; ix < data->n_comp_units; ++ix)
22643 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
22644
22645 for (ix = 0; ix < data->n_type_units; ++ix)
22646 VEC_free (dwarf2_per_cu_ptr,
22647 data->all_type_units[ix]->per_cu.imported_symtabs);
22648 xfree (data->all_type_units);
22649
22650 VEC_free (dwarf2_section_info_def, data->types);
22651
22652 if (data->dwo_files)
22653 free_dwo_files (data->dwo_files, objfile);
22654 if (data->dwp_file)
22655 gdb_bfd_unref (data->dwp_file->dbfd);
22656
22657 if (data->dwz_file && data->dwz_file->dwz_bfd)
22658 gdb_bfd_unref (data->dwz_file->dwz_bfd);
22659 }
22660
22661 \f
22662 /* The "save gdb-index" command. */
22663
22664 /* The contents of the hash table we create when building the string
22665 table. */
22666 struct strtab_entry
22667 {
22668 offset_type offset;
22669 const char *str;
22670 };
22671
22672 /* Hash function for a strtab_entry.
22673
22674 Function is used only during write_hash_table so no index format backward
22675 compatibility is needed. */
22676
22677 static hashval_t
22678 hash_strtab_entry (const void *e)
22679 {
22680 const struct strtab_entry *entry = (const struct strtab_entry *) e;
22681 return mapped_index_string_hash (INT_MAX, entry->str);
22682 }
22683
22684 /* Equality function for a strtab_entry. */
22685
22686 static int
22687 eq_strtab_entry (const void *a, const void *b)
22688 {
22689 const struct strtab_entry *ea = (const struct strtab_entry *) a;
22690 const struct strtab_entry *eb = (const struct strtab_entry *) b;
22691 return !strcmp (ea->str, eb->str);
22692 }
22693
22694 /* Create a strtab_entry hash table. */
22695
22696 static htab_t
22697 create_strtab (void)
22698 {
22699 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
22700 xfree, xcalloc, xfree);
22701 }
22702
22703 /* Add a string to the constant pool. Return the string's offset in
22704 host order. */
22705
22706 static offset_type
22707 add_string (htab_t table, struct obstack *cpool, const char *str)
22708 {
22709 void **slot;
22710 struct strtab_entry entry;
22711 struct strtab_entry *result;
22712
22713 entry.str = str;
22714 slot = htab_find_slot (table, &entry, INSERT);
22715 if (*slot)
22716 result = (struct strtab_entry *) *slot;
22717 else
22718 {
22719 result = XNEW (struct strtab_entry);
22720 result->offset = obstack_object_size (cpool);
22721 result->str = str;
22722 obstack_grow_str0 (cpool, str);
22723 *slot = result;
22724 }
22725 return result->offset;
22726 }
22727
22728 /* An entry in the symbol table. */
22729 struct symtab_index_entry
22730 {
22731 /* The name of the symbol. */
22732 const char *name;
22733 /* The offset of the name in the constant pool. */
22734 offset_type index_offset;
22735 /* A sorted vector of the indices of all the CUs that hold an object
22736 of this name. */
22737 VEC (offset_type) *cu_indices;
22738 };
22739
22740 /* The symbol table. This is a power-of-2-sized hash table. */
22741 struct mapped_symtab
22742 {
22743 offset_type n_elements;
22744 offset_type size;
22745 struct symtab_index_entry **data;
22746 };
22747
22748 /* Hash function for a symtab_index_entry. */
22749
22750 static hashval_t
22751 hash_symtab_entry (const void *e)
22752 {
22753 const struct symtab_index_entry *entry
22754 = (const struct symtab_index_entry *) e;
22755 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
22756 sizeof (offset_type) * VEC_length (offset_type,
22757 entry->cu_indices),
22758 0);
22759 }
22760
22761 /* Equality function for a symtab_index_entry. */
22762
22763 static int
22764 eq_symtab_entry (const void *a, const void *b)
22765 {
22766 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
22767 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
22768 int len = VEC_length (offset_type, ea->cu_indices);
22769 if (len != VEC_length (offset_type, eb->cu_indices))
22770 return 0;
22771 return !memcmp (VEC_address (offset_type, ea->cu_indices),
22772 VEC_address (offset_type, eb->cu_indices),
22773 sizeof (offset_type) * len);
22774 }
22775
22776 /* Destroy a symtab_index_entry. */
22777
22778 static void
22779 delete_symtab_entry (void *p)
22780 {
22781 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
22782 VEC_free (offset_type, entry->cu_indices);
22783 xfree (entry);
22784 }
22785
22786 /* Create a hash table holding symtab_index_entry objects. */
22787
22788 static htab_t
22789 create_symbol_hash_table (void)
22790 {
22791 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
22792 delete_symtab_entry, xcalloc, xfree);
22793 }
22794
22795 /* Create a new mapped symtab object. */
22796
22797 static struct mapped_symtab *
22798 create_mapped_symtab (void)
22799 {
22800 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
22801 symtab->n_elements = 0;
22802 symtab->size = 1024;
22803 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
22804 return symtab;
22805 }
22806
22807 /* Destroy a mapped_symtab. */
22808
22809 static void
22810 cleanup_mapped_symtab (void *p)
22811 {
22812 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
22813 /* The contents of the array are freed when the other hash table is
22814 destroyed. */
22815 xfree (symtab->data);
22816 xfree (symtab);
22817 }
22818
22819 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
22820 the slot.
22821
22822 Function is used only during write_hash_table so no index format backward
22823 compatibility is needed. */
22824
22825 static struct symtab_index_entry **
22826 find_slot (struct mapped_symtab *symtab, const char *name)
22827 {
22828 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
22829
22830 index = hash & (symtab->size - 1);
22831 step = ((hash * 17) & (symtab->size - 1)) | 1;
22832
22833 for (;;)
22834 {
22835 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
22836 return &symtab->data[index];
22837 index = (index + step) & (symtab->size - 1);
22838 }
22839 }
22840
22841 /* Expand SYMTAB's hash table. */
22842
22843 static void
22844 hash_expand (struct mapped_symtab *symtab)
22845 {
22846 offset_type old_size = symtab->size;
22847 offset_type i;
22848 struct symtab_index_entry **old_entries = symtab->data;
22849
22850 symtab->size *= 2;
22851 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
22852
22853 for (i = 0; i < old_size; ++i)
22854 {
22855 if (old_entries[i])
22856 {
22857 struct symtab_index_entry **slot = find_slot (symtab,
22858 old_entries[i]->name);
22859 *slot = old_entries[i];
22860 }
22861 }
22862
22863 xfree (old_entries);
22864 }
22865
22866 /* Add an entry to SYMTAB. NAME is the name of the symbol.
22867 CU_INDEX is the index of the CU in which the symbol appears.
22868 IS_STATIC is one if the symbol is static, otherwise zero (global). */
22869
22870 static void
22871 add_index_entry (struct mapped_symtab *symtab, const char *name,
22872 int is_static, gdb_index_symbol_kind kind,
22873 offset_type cu_index)
22874 {
22875 struct symtab_index_entry **slot;
22876 offset_type cu_index_and_attrs;
22877
22878 ++symtab->n_elements;
22879 if (4 * symtab->n_elements / 3 >= symtab->size)
22880 hash_expand (symtab);
22881
22882 slot = find_slot (symtab, name);
22883 if (!*slot)
22884 {
22885 *slot = XNEW (struct symtab_index_entry);
22886 (*slot)->name = name;
22887 /* index_offset is set later. */
22888 (*slot)->cu_indices = NULL;
22889 }
22890
22891 cu_index_and_attrs = 0;
22892 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
22893 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
22894 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
22895
22896 /* We don't want to record an index value twice as we want to avoid the
22897 duplication.
22898 We process all global symbols and then all static symbols
22899 (which would allow us to avoid the duplication by only having to check
22900 the last entry pushed), but a symbol could have multiple kinds in one CU.
22901 To keep things simple we don't worry about the duplication here and
22902 sort and uniqufy the list after we've processed all symbols. */
22903 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
22904 }
22905
22906 /* qsort helper routine for uniquify_cu_indices. */
22907
22908 static int
22909 offset_type_compare (const void *ap, const void *bp)
22910 {
22911 offset_type a = *(offset_type *) ap;
22912 offset_type b = *(offset_type *) bp;
22913
22914 return (a > b) - (b > a);
22915 }
22916
22917 /* Sort and remove duplicates of all symbols' cu_indices lists. */
22918
22919 static void
22920 uniquify_cu_indices (struct mapped_symtab *symtab)
22921 {
22922 int i;
22923
22924 for (i = 0; i < symtab->size; ++i)
22925 {
22926 struct symtab_index_entry *entry = symtab->data[i];
22927
22928 if (entry
22929 && entry->cu_indices != NULL)
22930 {
22931 unsigned int next_to_insert, next_to_check;
22932 offset_type last_value;
22933
22934 qsort (VEC_address (offset_type, entry->cu_indices),
22935 VEC_length (offset_type, entry->cu_indices),
22936 sizeof (offset_type), offset_type_compare);
22937
22938 last_value = VEC_index (offset_type, entry->cu_indices, 0);
22939 next_to_insert = 1;
22940 for (next_to_check = 1;
22941 next_to_check < VEC_length (offset_type, entry->cu_indices);
22942 ++next_to_check)
22943 {
22944 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
22945 != last_value)
22946 {
22947 last_value = VEC_index (offset_type, entry->cu_indices,
22948 next_to_check);
22949 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
22950 last_value);
22951 ++next_to_insert;
22952 }
22953 }
22954 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
22955 }
22956 }
22957 }
22958
22959 /* Add a vector of indices to the constant pool. */
22960
22961 static offset_type
22962 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
22963 struct symtab_index_entry *entry)
22964 {
22965 void **slot;
22966
22967 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
22968 if (!*slot)
22969 {
22970 offset_type len = VEC_length (offset_type, entry->cu_indices);
22971 offset_type val = MAYBE_SWAP (len);
22972 offset_type iter;
22973 int i;
22974
22975 *slot = entry;
22976 entry->index_offset = obstack_object_size (cpool);
22977
22978 obstack_grow (cpool, &val, sizeof (val));
22979 for (i = 0;
22980 VEC_iterate (offset_type, entry->cu_indices, i, iter);
22981 ++i)
22982 {
22983 val = MAYBE_SWAP (iter);
22984 obstack_grow (cpool, &val, sizeof (val));
22985 }
22986 }
22987 else
22988 {
22989 struct symtab_index_entry *old_entry
22990 = (struct symtab_index_entry *) *slot;
22991 entry->index_offset = old_entry->index_offset;
22992 entry = old_entry;
22993 }
22994 return entry->index_offset;
22995 }
22996
22997 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
22998 constant pool entries going into the obstack CPOOL. */
22999
23000 static void
23001 write_hash_table (struct mapped_symtab *symtab,
23002 struct obstack *output, struct obstack *cpool)
23003 {
23004 offset_type i;
23005 htab_t symbol_hash_table;
23006 htab_t str_table;
23007
23008 symbol_hash_table = create_symbol_hash_table ();
23009 str_table = create_strtab ();
23010
23011 /* We add all the index vectors to the constant pool first, to
23012 ensure alignment is ok. */
23013 for (i = 0; i < symtab->size; ++i)
23014 {
23015 if (symtab->data[i])
23016 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
23017 }
23018
23019 /* Now write out the hash table. */
23020 for (i = 0; i < symtab->size; ++i)
23021 {
23022 offset_type str_off, vec_off;
23023
23024 if (symtab->data[i])
23025 {
23026 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23027 vec_off = symtab->data[i]->index_offset;
23028 }
23029 else
23030 {
23031 /* While 0 is a valid constant pool index, it is not valid
23032 to have 0 for both offsets. */
23033 str_off = 0;
23034 vec_off = 0;
23035 }
23036
23037 str_off = MAYBE_SWAP (str_off);
23038 vec_off = MAYBE_SWAP (vec_off);
23039
23040 obstack_grow (output, &str_off, sizeof (str_off));
23041 obstack_grow (output, &vec_off, sizeof (vec_off));
23042 }
23043
23044 htab_delete (str_table);
23045 htab_delete (symbol_hash_table);
23046 }
23047
23048 /* Struct to map psymtab to CU index in the index file. */
23049 struct psymtab_cu_index_map
23050 {
23051 struct partial_symtab *psymtab;
23052 unsigned int cu_index;
23053 };
23054
23055 static hashval_t
23056 hash_psymtab_cu_index (const void *item)
23057 {
23058 const struct psymtab_cu_index_map *map
23059 = (const struct psymtab_cu_index_map *) item;
23060
23061 return htab_hash_pointer (map->psymtab);
23062 }
23063
23064 static int
23065 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23066 {
23067 const struct psymtab_cu_index_map *lhs
23068 = (const struct psymtab_cu_index_map *) item_lhs;
23069 const struct psymtab_cu_index_map *rhs
23070 = (const struct psymtab_cu_index_map *) item_rhs;
23071
23072 return lhs->psymtab == rhs->psymtab;
23073 }
23074
23075 /* Helper struct for building the address table. */
23076 struct addrmap_index_data
23077 {
23078 struct objfile *objfile;
23079 struct obstack *addr_obstack;
23080 htab_t cu_index_htab;
23081
23082 /* Non-zero if the previous_* fields are valid.
23083 We can't write an entry until we see the next entry (since it is only then
23084 that we know the end of the entry). */
23085 int previous_valid;
23086 /* Index of the CU in the table of all CUs in the index file. */
23087 unsigned int previous_cu_index;
23088 /* Start address of the CU. */
23089 CORE_ADDR previous_cu_start;
23090 };
23091
23092 /* Write an address entry to OBSTACK. */
23093
23094 static void
23095 add_address_entry (struct objfile *objfile, struct obstack *obstack,
23096 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23097 {
23098 offset_type cu_index_to_write;
23099 gdb_byte addr[8];
23100 CORE_ADDR baseaddr;
23101
23102 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23103
23104 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23105 obstack_grow (obstack, addr, 8);
23106 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23107 obstack_grow (obstack, addr, 8);
23108 cu_index_to_write = MAYBE_SWAP (cu_index);
23109 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23110 }
23111
23112 /* Worker function for traversing an addrmap to build the address table. */
23113
23114 static int
23115 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23116 {
23117 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23118 struct partial_symtab *pst = (struct partial_symtab *) obj;
23119
23120 if (data->previous_valid)
23121 add_address_entry (data->objfile, data->addr_obstack,
23122 data->previous_cu_start, start_addr,
23123 data->previous_cu_index);
23124
23125 data->previous_cu_start = start_addr;
23126 if (pst != NULL)
23127 {
23128 struct psymtab_cu_index_map find_map, *map;
23129 find_map.psymtab = pst;
23130 map = ((struct psymtab_cu_index_map *)
23131 htab_find (data->cu_index_htab, &find_map));
23132 gdb_assert (map != NULL);
23133 data->previous_cu_index = map->cu_index;
23134 data->previous_valid = 1;
23135 }
23136 else
23137 data->previous_valid = 0;
23138
23139 return 0;
23140 }
23141
23142 /* Write OBJFILE's address map to OBSTACK.
23143 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23144 in the index file. */
23145
23146 static void
23147 write_address_map (struct objfile *objfile, struct obstack *obstack,
23148 htab_t cu_index_htab)
23149 {
23150 struct addrmap_index_data addrmap_index_data;
23151
23152 /* When writing the address table, we have to cope with the fact that
23153 the addrmap iterator only provides the start of a region; we have to
23154 wait until the next invocation to get the start of the next region. */
23155
23156 addrmap_index_data.objfile = objfile;
23157 addrmap_index_data.addr_obstack = obstack;
23158 addrmap_index_data.cu_index_htab = cu_index_htab;
23159 addrmap_index_data.previous_valid = 0;
23160
23161 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23162 &addrmap_index_data);
23163
23164 /* It's highly unlikely the last entry (end address = 0xff...ff)
23165 is valid, but we should still handle it.
23166 The end address is recorded as the start of the next region, but that
23167 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23168 anyway. */
23169 if (addrmap_index_data.previous_valid)
23170 add_address_entry (objfile, obstack,
23171 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23172 addrmap_index_data.previous_cu_index);
23173 }
23174
23175 /* Return the symbol kind of PSYM. */
23176
23177 static gdb_index_symbol_kind
23178 symbol_kind (struct partial_symbol *psym)
23179 {
23180 domain_enum domain = PSYMBOL_DOMAIN (psym);
23181 enum address_class aclass = PSYMBOL_CLASS (psym);
23182
23183 switch (domain)
23184 {
23185 case VAR_DOMAIN:
23186 switch (aclass)
23187 {
23188 case LOC_BLOCK:
23189 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23190 case LOC_TYPEDEF:
23191 return GDB_INDEX_SYMBOL_KIND_TYPE;
23192 case LOC_COMPUTED:
23193 case LOC_CONST_BYTES:
23194 case LOC_OPTIMIZED_OUT:
23195 case LOC_STATIC:
23196 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23197 case LOC_CONST:
23198 /* Note: It's currently impossible to recognize psyms as enum values
23199 short of reading the type info. For now punt. */
23200 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23201 default:
23202 /* There are other LOC_FOO values that one might want to classify
23203 as variables, but dwarf2read.c doesn't currently use them. */
23204 return GDB_INDEX_SYMBOL_KIND_OTHER;
23205 }
23206 case STRUCT_DOMAIN:
23207 return GDB_INDEX_SYMBOL_KIND_TYPE;
23208 default:
23209 return GDB_INDEX_SYMBOL_KIND_OTHER;
23210 }
23211 }
23212
23213 /* Add a list of partial symbols to SYMTAB. */
23214
23215 static void
23216 write_psymbols (struct mapped_symtab *symtab,
23217 htab_t psyms_seen,
23218 struct partial_symbol **psymp,
23219 int count,
23220 offset_type cu_index,
23221 int is_static)
23222 {
23223 for (; count-- > 0; ++psymp)
23224 {
23225 struct partial_symbol *psym = *psymp;
23226 void **slot;
23227
23228 if (SYMBOL_LANGUAGE (psym) == language_ada)
23229 error (_("Ada is not currently supported by the index"));
23230
23231 /* Only add a given psymbol once. */
23232 slot = htab_find_slot (psyms_seen, psym, INSERT);
23233 if (!*slot)
23234 {
23235 gdb_index_symbol_kind kind = symbol_kind (psym);
23236
23237 *slot = psym;
23238 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23239 is_static, kind, cu_index);
23240 }
23241 }
23242 }
23243
23244 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
23245 exception if there is an error. */
23246
23247 static void
23248 write_obstack (FILE *file, struct obstack *obstack)
23249 {
23250 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23251 file)
23252 != obstack_object_size (obstack))
23253 error (_("couldn't data write to file"));
23254 }
23255
23256 /* Unlink a file if the argument is not NULL. */
23257
23258 static void
23259 unlink_if_set (void *p)
23260 {
23261 char **filename = (char **) p;
23262 if (*filename)
23263 unlink (*filename);
23264 }
23265
23266 /* A helper struct used when iterating over debug_types. */
23267 struct signatured_type_index_data
23268 {
23269 struct objfile *objfile;
23270 struct mapped_symtab *symtab;
23271 struct obstack *types_list;
23272 htab_t psyms_seen;
23273 int cu_index;
23274 };
23275
23276 /* A helper function that writes a single signatured_type to an
23277 obstack. */
23278
23279 static int
23280 write_one_signatured_type (void **slot, void *d)
23281 {
23282 struct signatured_type_index_data *info
23283 = (struct signatured_type_index_data *) d;
23284 struct signatured_type *entry = (struct signatured_type *) *slot;
23285 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23286 gdb_byte val[8];
23287
23288 write_psymbols (info->symtab,
23289 info->psyms_seen,
23290 info->objfile->global_psymbols.list
23291 + psymtab->globals_offset,
23292 psymtab->n_global_syms, info->cu_index,
23293 0);
23294 write_psymbols (info->symtab,
23295 info->psyms_seen,
23296 info->objfile->static_psymbols.list
23297 + psymtab->statics_offset,
23298 psymtab->n_static_syms, info->cu_index,
23299 1);
23300
23301 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23302 entry->per_cu.offset.sect_off);
23303 obstack_grow (info->types_list, val, 8);
23304 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23305 entry->type_offset_in_tu.cu_off);
23306 obstack_grow (info->types_list, val, 8);
23307 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23308 obstack_grow (info->types_list, val, 8);
23309
23310 ++info->cu_index;
23311
23312 return 1;
23313 }
23314
23315 /* Recurse into all "included" dependencies and write their symbols as
23316 if they appeared in this psymtab. */
23317
23318 static void
23319 recursively_write_psymbols (struct objfile *objfile,
23320 struct partial_symtab *psymtab,
23321 struct mapped_symtab *symtab,
23322 htab_t psyms_seen,
23323 offset_type cu_index)
23324 {
23325 int i;
23326
23327 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23328 if (psymtab->dependencies[i]->user != NULL)
23329 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23330 symtab, psyms_seen, cu_index);
23331
23332 write_psymbols (symtab,
23333 psyms_seen,
23334 objfile->global_psymbols.list + psymtab->globals_offset,
23335 psymtab->n_global_syms, cu_index,
23336 0);
23337 write_psymbols (symtab,
23338 psyms_seen,
23339 objfile->static_psymbols.list + psymtab->statics_offset,
23340 psymtab->n_static_syms, cu_index,
23341 1);
23342 }
23343
23344 /* Create an index file for OBJFILE in the directory DIR. */
23345
23346 static void
23347 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23348 {
23349 struct cleanup *cleanup;
23350 char *filename, *cleanup_filename;
23351 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23352 struct obstack cu_list, types_cu_list;
23353 int i;
23354 FILE *out_file;
23355 struct mapped_symtab *symtab;
23356 offset_type val, size_of_contents, total_len;
23357 struct stat st;
23358 htab_t psyms_seen;
23359 htab_t cu_index_htab;
23360 struct psymtab_cu_index_map *psymtab_cu_index_map;
23361
23362 if (dwarf2_per_objfile->using_index)
23363 error (_("Cannot use an index to create the index"));
23364
23365 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23366 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23367
23368 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23369 return;
23370
23371 if (stat (objfile_name (objfile), &st) < 0)
23372 perror_with_name (objfile_name (objfile));
23373
23374 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
23375 INDEX_SUFFIX, (char *) NULL);
23376 cleanup = make_cleanup (xfree, filename);
23377
23378 out_file = gdb_fopen_cloexec (filename, "wb");
23379 if (!out_file)
23380 error (_("Can't open `%s' for writing"), filename);
23381
23382 cleanup_filename = filename;
23383 make_cleanup (unlink_if_set, &cleanup_filename);
23384
23385 symtab = create_mapped_symtab ();
23386 make_cleanup (cleanup_mapped_symtab, symtab);
23387
23388 obstack_init (&addr_obstack);
23389 make_cleanup_obstack_free (&addr_obstack);
23390
23391 obstack_init (&cu_list);
23392 make_cleanup_obstack_free (&cu_list);
23393
23394 obstack_init (&types_cu_list);
23395 make_cleanup_obstack_free (&types_cu_list);
23396
23397 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
23398 NULL, xcalloc, xfree);
23399 make_cleanup_htab_delete (psyms_seen);
23400
23401 /* While we're scanning CU's create a table that maps a psymtab pointer
23402 (which is what addrmap records) to its index (which is what is recorded
23403 in the index file). This will later be needed to write the address
23404 table. */
23405 cu_index_htab = htab_create_alloc (100,
23406 hash_psymtab_cu_index,
23407 eq_psymtab_cu_index,
23408 NULL, xcalloc, xfree);
23409 make_cleanup_htab_delete (cu_index_htab);
23410 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23411 dwarf2_per_objfile->n_comp_units);
23412 make_cleanup (xfree, psymtab_cu_index_map);
23413
23414 /* The CU list is already sorted, so we don't need to do additional
23415 work here. Also, the debug_types entries do not appear in
23416 all_comp_units, but only in their own hash table. */
23417 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23418 {
23419 struct dwarf2_per_cu_data *per_cu
23420 = dwarf2_per_objfile->all_comp_units[i];
23421 struct partial_symtab *psymtab = per_cu->v.psymtab;
23422 gdb_byte val[8];
23423 struct psymtab_cu_index_map *map;
23424 void **slot;
23425
23426 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23427 It may be referenced from a local scope but in such case it does not
23428 need to be present in .gdb_index. */
23429 if (psymtab == NULL)
23430 continue;
23431
23432 if (psymtab->user == NULL)
23433 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
23434
23435 map = &psymtab_cu_index_map[i];
23436 map->psymtab = psymtab;
23437 map->cu_index = i;
23438 slot = htab_find_slot (cu_index_htab, map, INSERT);
23439 gdb_assert (slot != NULL);
23440 gdb_assert (*slot == NULL);
23441 *slot = map;
23442
23443 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23444 per_cu->offset.sect_off);
23445 obstack_grow (&cu_list, val, 8);
23446 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
23447 obstack_grow (&cu_list, val, 8);
23448 }
23449
23450 /* Dump the address map. */
23451 write_address_map (objfile, &addr_obstack, cu_index_htab);
23452
23453 /* Write out the .debug_type entries, if any. */
23454 if (dwarf2_per_objfile->signatured_types)
23455 {
23456 struct signatured_type_index_data sig_data;
23457
23458 sig_data.objfile = objfile;
23459 sig_data.symtab = symtab;
23460 sig_data.types_list = &types_cu_list;
23461 sig_data.psyms_seen = psyms_seen;
23462 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23463 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23464 write_one_signatured_type, &sig_data);
23465 }
23466
23467 /* Now that we've processed all symbols we can shrink their cu_indices
23468 lists. */
23469 uniquify_cu_indices (symtab);
23470
23471 obstack_init (&constant_pool);
23472 make_cleanup_obstack_free (&constant_pool);
23473 obstack_init (&symtab_obstack);
23474 make_cleanup_obstack_free (&symtab_obstack);
23475 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23476
23477 obstack_init (&contents);
23478 make_cleanup_obstack_free (&contents);
23479 size_of_contents = 6 * sizeof (offset_type);
23480 total_len = size_of_contents;
23481
23482 /* The version number. */
23483 val = MAYBE_SWAP (8);
23484 obstack_grow (&contents, &val, sizeof (val));
23485
23486 /* The offset of the CU list from the start of the file. */
23487 val = MAYBE_SWAP (total_len);
23488 obstack_grow (&contents, &val, sizeof (val));
23489 total_len += obstack_object_size (&cu_list);
23490
23491 /* The offset of the types CU list from the start of the file. */
23492 val = MAYBE_SWAP (total_len);
23493 obstack_grow (&contents, &val, sizeof (val));
23494 total_len += obstack_object_size (&types_cu_list);
23495
23496 /* The offset of the address table from the start of the file. */
23497 val = MAYBE_SWAP (total_len);
23498 obstack_grow (&contents, &val, sizeof (val));
23499 total_len += obstack_object_size (&addr_obstack);
23500
23501 /* The offset of the symbol table from the start of the file. */
23502 val = MAYBE_SWAP (total_len);
23503 obstack_grow (&contents, &val, sizeof (val));
23504 total_len += obstack_object_size (&symtab_obstack);
23505
23506 /* The offset of the constant pool from the start of the file. */
23507 val = MAYBE_SWAP (total_len);
23508 obstack_grow (&contents, &val, sizeof (val));
23509 total_len += obstack_object_size (&constant_pool);
23510
23511 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23512
23513 write_obstack (out_file, &contents);
23514 write_obstack (out_file, &cu_list);
23515 write_obstack (out_file, &types_cu_list);
23516 write_obstack (out_file, &addr_obstack);
23517 write_obstack (out_file, &symtab_obstack);
23518 write_obstack (out_file, &constant_pool);
23519
23520 fclose (out_file);
23521
23522 /* We want to keep the file, so we set cleanup_filename to NULL
23523 here. See unlink_if_set. */
23524 cleanup_filename = NULL;
23525
23526 do_cleanups (cleanup);
23527 }
23528
23529 /* Implementation of the `save gdb-index' command.
23530
23531 Note that the file format used by this command is documented in the
23532 GDB manual. Any changes here must be documented there. */
23533
23534 static void
23535 save_gdb_index_command (char *arg, int from_tty)
23536 {
23537 struct objfile *objfile;
23538
23539 if (!arg || !*arg)
23540 error (_("usage: save gdb-index DIRECTORY"));
23541
23542 ALL_OBJFILES (objfile)
23543 {
23544 struct stat st;
23545
23546 /* If the objfile does not correspond to an actual file, skip it. */
23547 if (stat (objfile_name (objfile), &st) < 0)
23548 continue;
23549
23550 dwarf2_per_objfile
23551 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23552 dwarf2_objfile_data_key);
23553 if (dwarf2_per_objfile)
23554 {
23555
23556 TRY
23557 {
23558 write_psymtabs_to_index (objfile, arg);
23559 }
23560 CATCH (except, RETURN_MASK_ERROR)
23561 {
23562 exception_fprintf (gdb_stderr, except,
23563 _("Error while writing index for `%s': "),
23564 objfile_name (objfile));
23565 }
23566 END_CATCH
23567 }
23568 }
23569 }
23570
23571 \f
23572
23573 int dwarf_always_disassemble;
23574
23575 static void
23576 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23577 struct cmd_list_element *c, const char *value)
23578 {
23579 fprintf_filtered (file,
23580 _("Whether to always disassemble "
23581 "DWARF expressions is %s.\n"),
23582 value);
23583 }
23584
23585 static void
23586 show_check_physname (struct ui_file *file, int from_tty,
23587 struct cmd_list_element *c, const char *value)
23588 {
23589 fprintf_filtered (file,
23590 _("Whether to check \"physname\" is %s.\n"),
23591 value);
23592 }
23593
23594 void _initialize_dwarf2_read (void);
23595
23596 void
23597 _initialize_dwarf2_read (void)
23598 {
23599 struct cmd_list_element *c;
23600
23601 dwarf2_objfile_data_key
23602 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
23603
23604 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23605 Set DWARF specific variables.\n\
23606 Configure DWARF variables such as the cache size"),
23607 &set_dwarf_cmdlist, "maintenance set dwarf ",
23608 0/*allow-unknown*/, &maintenance_set_cmdlist);
23609
23610 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23611 Show DWARF specific variables\n\
23612 Show DWARF variables such as the cache size"),
23613 &show_dwarf_cmdlist, "maintenance show dwarf ",
23614 0/*allow-unknown*/, &maintenance_show_cmdlist);
23615
23616 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23617 &dwarf_max_cache_age, _("\
23618 Set the upper bound on the age of cached DWARF compilation units."), _("\
23619 Show the upper bound on the age of cached DWARF compilation units."), _("\
23620 A higher limit means that cached compilation units will be stored\n\
23621 in memory longer, and more total memory will be used. Zero disables\n\
23622 caching, which can slow down startup."),
23623 NULL,
23624 show_dwarf_max_cache_age,
23625 &set_dwarf_cmdlist,
23626 &show_dwarf_cmdlist);
23627
23628 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
23629 &dwarf_always_disassemble, _("\
23630 Set whether `info address' always disassembles DWARF expressions."), _("\
23631 Show whether `info address' always disassembles DWARF expressions."), _("\
23632 When enabled, DWARF expressions are always printed in an assembly-like\n\
23633 syntax. When disabled, expressions will be printed in a more\n\
23634 conversational style, when possible."),
23635 NULL,
23636 show_dwarf_always_disassemble,
23637 &set_dwarf_cmdlist,
23638 &show_dwarf_cmdlist);
23639
23640 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23641 Set debugging of the DWARF reader."), _("\
23642 Show debugging of the DWARF reader."), _("\
23643 When enabled (non-zero), debugging messages are printed during DWARF\n\
23644 reading and symtab expansion. A value of 1 (one) provides basic\n\
23645 information. A value greater than 1 provides more verbose information."),
23646 NULL,
23647 NULL,
23648 &setdebuglist, &showdebuglist);
23649
23650 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23651 Set debugging of the DWARF DIE reader."), _("\
23652 Show debugging of the DWARF DIE reader."), _("\
23653 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23654 The value is the maximum depth to print."),
23655 NULL,
23656 NULL,
23657 &setdebuglist, &showdebuglist);
23658
23659 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23660 Set debugging of the dwarf line reader."), _("\
23661 Show debugging of the dwarf line reader."), _("\
23662 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23663 A value of 1 (one) provides basic information.\n\
23664 A value greater than 1 provides more verbose information."),
23665 NULL,
23666 NULL,
23667 &setdebuglist, &showdebuglist);
23668
23669 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23670 Set cross-checking of \"physname\" code against demangler."), _("\
23671 Show cross-checking of \"physname\" code against demangler."), _("\
23672 When enabled, GDB's internal \"physname\" code is checked against\n\
23673 the demangler."),
23674 NULL, show_check_physname,
23675 &setdebuglist, &showdebuglist);
23676
23677 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23678 no_class, &use_deprecated_index_sections, _("\
23679 Set whether to use deprecated gdb_index sections."), _("\
23680 Show whether to use deprecated gdb_index sections."), _("\
23681 When enabled, deprecated .gdb_index sections are used anyway.\n\
23682 Normally they are ignored either because of a missing feature or\n\
23683 performance issue.\n\
23684 Warning: This option must be enabled before gdb reads the file."),
23685 NULL,
23686 NULL,
23687 &setlist, &showlist);
23688
23689 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
23690 _("\
23691 Save a gdb-index file.\n\
23692 Usage: save gdb-index DIRECTORY"),
23693 &save_cmdlist);
23694 set_cmd_completer (c, filename_completer);
23695
23696 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23697 &dwarf2_locexpr_funcs);
23698 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23699 &dwarf2_loclist_funcs);
23700
23701 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23702 &dwarf2_block_frame_base_locexpr_funcs);
23703 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23704 &dwarf2_block_frame_base_loclist_funcs);
23705 }
This page took 0.554143 seconds and 4 git commands to generate.