Eliminate make_cleanup_ui_file_delete / make ui_file a class hierarchy
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2017 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 "psympriv.h"
57 #include <sys/stat.h>
58 #include "completer.h"
59 #include "vec.h"
60 #include "c-lang.h"
61 #include "go-lang.h"
62 #include "valprint.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
65 #include <ctype.h>
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "filestuff.h"
70 #include "build-id.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.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 /* This DIE has been marked DW_AT_main_subprogram. */
1110 unsigned int main_subprogram : 1;
1111
1112 /* Flag set if the SCOPE field of this structure has been
1113 computed. */
1114 unsigned int scope_set : 1;
1115
1116 /* Flag set if the DIE has a byte_size attribute. */
1117 unsigned int has_byte_size : 1;
1118
1119 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1120 unsigned int has_const_value : 1;
1121
1122 /* Flag set if any of the DIE's children are template arguments. */
1123 unsigned int has_template_arguments : 1;
1124
1125 /* Flag set if fixup_partial_die has been called on this die. */
1126 unsigned int fixup_called : 1;
1127
1128 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1129 unsigned int is_dwz : 1;
1130
1131 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1132 unsigned int spec_is_dwz : 1;
1133
1134 /* The name of this DIE. Normally the value of DW_AT_name, but
1135 sometimes a default name for unnamed DIEs. */
1136 const char *name;
1137
1138 /* The linkage name, if present. */
1139 const char *linkage_name;
1140
1141 /* The scope to prepend to our children. This is generally
1142 allocated on the comp_unit_obstack, so will disappear
1143 when this compilation unit leaves the cache. */
1144 const char *scope;
1145
1146 /* Some data associated with the partial DIE. The tag determines
1147 which field is live. */
1148 union
1149 {
1150 /* The location description associated with this DIE, if any. */
1151 struct dwarf_block *locdesc;
1152 /* The offset of an import, for DW_TAG_imported_unit. */
1153 sect_offset offset;
1154 } d;
1155
1156 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1157 CORE_ADDR lowpc;
1158 CORE_ADDR highpc;
1159
1160 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1161 DW_AT_sibling, if any. */
1162 /* NOTE: This member isn't strictly necessary, read_partial_die could
1163 return DW_AT_sibling values to its caller load_partial_dies. */
1164 const gdb_byte *sibling;
1165
1166 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1167 DW_AT_specification (or DW_AT_abstract_origin or
1168 DW_AT_extension). */
1169 sect_offset spec_offset;
1170
1171 /* Pointers to this DIE's parent, first child, and next sibling,
1172 if any. */
1173 struct partial_die_info *die_parent, *die_child, *die_sibling;
1174 };
1175
1176 /* This data structure holds the information of an abbrev. */
1177 struct abbrev_info
1178 {
1179 unsigned int number; /* number identifying abbrev */
1180 enum dwarf_tag tag; /* dwarf tag */
1181 unsigned short has_children; /* boolean */
1182 unsigned short num_attrs; /* number of attributes */
1183 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1184 struct abbrev_info *next; /* next in chain */
1185 };
1186
1187 struct attr_abbrev
1188 {
1189 ENUM_BITFIELD(dwarf_attribute) name : 16;
1190 ENUM_BITFIELD(dwarf_form) form : 16;
1191 };
1192
1193 /* Size of abbrev_table.abbrev_hash_table. */
1194 #define ABBREV_HASH_SIZE 121
1195
1196 /* Top level data structure to contain an abbreviation table. */
1197
1198 struct abbrev_table
1199 {
1200 /* Where the abbrev table came from.
1201 This is used as a sanity check when the table is used. */
1202 sect_offset offset;
1203
1204 /* Storage for the abbrev table. */
1205 struct obstack abbrev_obstack;
1206
1207 /* Hash table of abbrevs.
1208 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1209 It could be statically allocated, but the previous code didn't so we
1210 don't either. */
1211 struct abbrev_info **abbrevs;
1212 };
1213
1214 /* Attributes have a name and a value. */
1215 struct attribute
1216 {
1217 ENUM_BITFIELD(dwarf_attribute) name : 16;
1218 ENUM_BITFIELD(dwarf_form) form : 15;
1219
1220 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1221 field should be in u.str (existing only for DW_STRING) but it is kept
1222 here for better struct attribute alignment. */
1223 unsigned int string_is_canonical : 1;
1224
1225 union
1226 {
1227 const char *str;
1228 struct dwarf_block *blk;
1229 ULONGEST unsnd;
1230 LONGEST snd;
1231 CORE_ADDR addr;
1232 ULONGEST signature;
1233 }
1234 u;
1235 };
1236
1237 /* This data structure holds a complete die structure. */
1238 struct die_info
1239 {
1240 /* DWARF-2 tag for this DIE. */
1241 ENUM_BITFIELD(dwarf_tag) tag : 16;
1242
1243 /* Number of attributes */
1244 unsigned char num_attrs;
1245
1246 /* True if we're presently building the full type name for the
1247 type derived from this DIE. */
1248 unsigned char building_fullname : 1;
1249
1250 /* True if this die is in process. PR 16581. */
1251 unsigned char in_process : 1;
1252
1253 /* Abbrev number */
1254 unsigned int abbrev;
1255
1256 /* Offset in .debug_info or .debug_types section. */
1257 sect_offset offset;
1258
1259 /* The dies in a compilation unit form an n-ary tree. PARENT
1260 points to this die's parent; CHILD points to the first child of
1261 this node; and all the children of a given node are chained
1262 together via their SIBLING fields. */
1263 struct die_info *child; /* Its first child, if any. */
1264 struct die_info *sibling; /* Its next sibling, if any. */
1265 struct die_info *parent; /* Its parent, if any. */
1266
1267 /* An array of attributes, with NUM_ATTRS elements. There may be
1268 zero, but it's not common and zero-sized arrays are not
1269 sufficiently portable C. */
1270 struct attribute attrs[1];
1271 };
1272
1273 /* Get at parts of an attribute structure. */
1274
1275 #define DW_STRING(attr) ((attr)->u.str)
1276 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1277 #define DW_UNSND(attr) ((attr)->u.unsnd)
1278 #define DW_BLOCK(attr) ((attr)->u.blk)
1279 #define DW_SND(attr) ((attr)->u.snd)
1280 #define DW_ADDR(attr) ((attr)->u.addr)
1281 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1282
1283 /* Blocks are a bunch of untyped bytes. */
1284 struct dwarf_block
1285 {
1286 size_t size;
1287
1288 /* Valid only if SIZE is not zero. */
1289 const gdb_byte *data;
1290 };
1291
1292 #ifndef ATTR_ALLOC_CHUNK
1293 #define ATTR_ALLOC_CHUNK 4
1294 #endif
1295
1296 /* Allocate fields for structs, unions and enums in this size. */
1297 #ifndef DW_FIELD_ALLOC_CHUNK
1298 #define DW_FIELD_ALLOC_CHUNK 4
1299 #endif
1300
1301 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1302 but this would require a corresponding change in unpack_field_as_long
1303 and friends. */
1304 static int bits_per_byte = 8;
1305
1306 struct nextfield
1307 {
1308 struct nextfield *next;
1309 int accessibility;
1310 int virtuality;
1311 struct field field;
1312 };
1313
1314 struct nextfnfield
1315 {
1316 struct nextfnfield *next;
1317 struct fn_field fnfield;
1318 };
1319
1320 struct fnfieldlist
1321 {
1322 const char *name;
1323 int length;
1324 struct nextfnfield *head;
1325 };
1326
1327 struct typedef_field_list
1328 {
1329 struct typedef_field field;
1330 struct typedef_field_list *next;
1331 };
1332
1333 /* The routines that read and process dies for a C struct or C++ class
1334 pass lists of data member fields and lists of member function fields
1335 in an instance of a field_info structure, as defined below. */
1336 struct field_info
1337 {
1338 /* List of data member and baseclasses fields. */
1339 struct nextfield *fields, *baseclasses;
1340
1341 /* Number of fields (including baseclasses). */
1342 int nfields;
1343
1344 /* Number of baseclasses. */
1345 int nbaseclasses;
1346
1347 /* Set if the accesibility of one of the fields is not public. */
1348 int non_public_fields;
1349
1350 /* Member function fields array, entries are allocated in the order they
1351 are encountered in the object file. */
1352 struct nextfnfield *fnfields;
1353
1354 /* Member function fieldlist array, contains name of possibly overloaded
1355 member function, number of overloaded member functions and a pointer
1356 to the head of the member function field chain. */
1357 struct fnfieldlist *fnfieldlists;
1358
1359 /* Number of entries in the fnfieldlists array. */
1360 int nfnfields;
1361
1362 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1363 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1364 struct typedef_field_list *typedef_field_list;
1365 unsigned typedef_field_list_count;
1366 };
1367
1368 /* One item on the queue of compilation units to read in full symbols
1369 for. */
1370 struct dwarf2_queue_item
1371 {
1372 struct dwarf2_per_cu_data *per_cu;
1373 enum language pretend_language;
1374 struct dwarf2_queue_item *next;
1375 };
1376
1377 /* The current queue. */
1378 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1379
1380 /* Loaded secondary compilation units are kept in memory until they
1381 have not been referenced for the processing of this many
1382 compilation units. Set this to zero to disable caching. Cache
1383 sizes of up to at least twenty will improve startup time for
1384 typical inter-CU-reference binaries, at an obvious memory cost. */
1385 static int dwarf_max_cache_age = 5;
1386 static void
1387 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1388 struct cmd_list_element *c, const char *value)
1389 {
1390 fprintf_filtered (file, _("The upper bound on the age of cached "
1391 "DWARF compilation units is %s.\n"),
1392 value);
1393 }
1394 \f
1395 /* local function prototypes */
1396
1397 static const char *get_section_name (const struct dwarf2_section_info *);
1398
1399 static const char *get_section_file_name (const struct dwarf2_section_info *);
1400
1401 static void dwarf2_locate_sections (bfd *, asection *, void *);
1402
1403 static void dwarf2_find_base_address (struct die_info *die,
1404 struct dwarf2_cu *cu);
1405
1406 static struct partial_symtab *create_partial_symtab
1407 (struct dwarf2_per_cu_data *per_cu, const char *name);
1408
1409 static void dwarf2_build_psymtabs_hard (struct objfile *);
1410
1411 static void scan_partial_symbols (struct partial_die_info *,
1412 CORE_ADDR *, CORE_ADDR *,
1413 int, struct dwarf2_cu *);
1414
1415 static void add_partial_symbol (struct partial_die_info *,
1416 struct dwarf2_cu *);
1417
1418 static void add_partial_namespace (struct partial_die_info *pdi,
1419 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1420 int set_addrmap, struct dwarf2_cu *cu);
1421
1422 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1423 CORE_ADDR *highpc, int set_addrmap,
1424 struct dwarf2_cu *cu);
1425
1426 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1427 struct dwarf2_cu *cu);
1428
1429 static void add_partial_subprogram (struct partial_die_info *pdi,
1430 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1431 int need_pc, struct dwarf2_cu *cu);
1432
1433 static void dwarf2_read_symtab (struct partial_symtab *,
1434 struct objfile *);
1435
1436 static void psymtab_to_symtab_1 (struct partial_symtab *);
1437
1438 static struct abbrev_info *abbrev_table_lookup_abbrev
1439 (const struct abbrev_table *, unsigned int);
1440
1441 static struct abbrev_table *abbrev_table_read_table
1442 (struct dwarf2_section_info *, sect_offset);
1443
1444 static void abbrev_table_free (struct abbrev_table *);
1445
1446 static void abbrev_table_free_cleanup (void *);
1447
1448 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1449 struct dwarf2_section_info *);
1450
1451 static void dwarf2_free_abbrev_table (void *);
1452
1453 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1454
1455 static struct partial_die_info *load_partial_dies
1456 (const struct die_reader_specs *, const gdb_byte *, int);
1457
1458 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1459 struct partial_die_info *,
1460 struct abbrev_info *,
1461 unsigned int,
1462 const gdb_byte *);
1463
1464 static struct partial_die_info *find_partial_die (sect_offset, int,
1465 struct dwarf2_cu *);
1466
1467 static void fixup_partial_die (struct partial_die_info *,
1468 struct dwarf2_cu *);
1469
1470 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1471 struct attribute *, struct attr_abbrev *,
1472 const gdb_byte *);
1473
1474 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1475
1476 static int read_1_signed_byte (bfd *, const gdb_byte *);
1477
1478 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1479
1480 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1481
1482 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1483
1484 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1485 unsigned int *);
1486
1487 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1488
1489 static LONGEST read_checked_initial_length_and_offset
1490 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1491 unsigned int *, unsigned int *);
1492
1493 static LONGEST read_offset (bfd *, const gdb_byte *,
1494 const struct comp_unit_head *,
1495 unsigned int *);
1496
1497 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1498
1499 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1500 sect_offset);
1501
1502 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1503
1504 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1505
1506 static const char *read_indirect_string (bfd *, const gdb_byte *,
1507 const struct comp_unit_head *,
1508 unsigned int *);
1509
1510 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1511
1512 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
1513
1514 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1515
1516 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1517 const gdb_byte *,
1518 unsigned int *);
1519
1520 static const char *read_str_index (const struct die_reader_specs *reader,
1521 ULONGEST str_index);
1522
1523 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1524
1525 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1526 struct dwarf2_cu *);
1527
1528 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1529 unsigned int);
1530
1531 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1532 struct dwarf2_cu *cu);
1533
1534 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1535 struct dwarf2_cu *cu);
1536
1537 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1538
1539 static struct die_info *die_specification (struct die_info *die,
1540 struct dwarf2_cu **);
1541
1542 static void free_line_header (struct line_header *lh);
1543
1544 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1545 struct dwarf2_cu *cu);
1546
1547 static void dwarf_decode_lines (struct line_header *, const char *,
1548 struct dwarf2_cu *, struct partial_symtab *,
1549 CORE_ADDR, int decode_mapping);
1550
1551 static void dwarf2_start_subfile (const char *, const char *);
1552
1553 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1554 const char *, const char *,
1555 CORE_ADDR);
1556
1557 static struct symbol *new_symbol (struct die_info *, struct type *,
1558 struct dwarf2_cu *);
1559
1560 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1561 struct dwarf2_cu *, struct symbol *);
1562
1563 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1564 struct dwarf2_cu *);
1565
1566 static void dwarf2_const_value_attr (const struct attribute *attr,
1567 struct type *type,
1568 const char *name,
1569 struct obstack *obstack,
1570 struct dwarf2_cu *cu, LONGEST *value,
1571 const gdb_byte **bytes,
1572 struct dwarf2_locexpr_baton **baton);
1573
1574 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1575
1576 static int need_gnat_info (struct dwarf2_cu *);
1577
1578 static struct type *die_descriptive_type (struct die_info *,
1579 struct dwarf2_cu *);
1580
1581 static void set_descriptive_type (struct type *, struct die_info *,
1582 struct dwarf2_cu *);
1583
1584 static struct type *die_containing_type (struct die_info *,
1585 struct dwarf2_cu *);
1586
1587 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1588 struct dwarf2_cu *);
1589
1590 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1591
1592 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1593
1594 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1595
1596 static char *typename_concat (struct obstack *obs, const char *prefix,
1597 const char *suffix, int physname,
1598 struct dwarf2_cu *cu);
1599
1600 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1601
1602 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1603
1604 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1605
1606 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1607
1608 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1609
1610 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1611 struct dwarf2_cu *, struct partial_symtab *);
1612
1613 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1614 values. Keep the items ordered with increasing constraints compliance. */
1615 enum pc_bounds_kind
1616 {
1617 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1618 PC_BOUNDS_NOT_PRESENT,
1619
1620 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1621 were present but they do not form a valid range of PC addresses. */
1622 PC_BOUNDS_INVALID,
1623
1624 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1625 PC_BOUNDS_RANGES,
1626
1627 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1628 PC_BOUNDS_HIGH_LOW,
1629 };
1630
1631 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1632 CORE_ADDR *, CORE_ADDR *,
1633 struct dwarf2_cu *,
1634 struct partial_symtab *);
1635
1636 static void get_scope_pc_bounds (struct die_info *,
1637 CORE_ADDR *, CORE_ADDR *,
1638 struct dwarf2_cu *);
1639
1640 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1641 CORE_ADDR, struct dwarf2_cu *);
1642
1643 static void dwarf2_add_field (struct field_info *, struct die_info *,
1644 struct dwarf2_cu *);
1645
1646 static void dwarf2_attach_fields_to_type (struct field_info *,
1647 struct type *, struct dwarf2_cu *);
1648
1649 static void dwarf2_add_member_fn (struct field_info *,
1650 struct die_info *, struct type *,
1651 struct dwarf2_cu *);
1652
1653 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1654 struct type *,
1655 struct dwarf2_cu *);
1656
1657 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1658
1659 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1660
1661 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1662
1663 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1664
1665 static struct using_direct **using_directives (enum language);
1666
1667 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1668
1669 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1670
1671 static struct type *read_module_type (struct die_info *die,
1672 struct dwarf2_cu *cu);
1673
1674 static const char *namespace_name (struct die_info *die,
1675 int *is_anonymous, struct dwarf2_cu *);
1676
1677 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1678
1679 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1680
1681 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1682 struct dwarf2_cu *);
1683
1684 static struct die_info *read_die_and_siblings_1
1685 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1686 struct die_info *);
1687
1688 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1689 const gdb_byte *info_ptr,
1690 const gdb_byte **new_info_ptr,
1691 struct die_info *parent);
1692
1693 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1694 struct die_info **, const gdb_byte *,
1695 int *, int);
1696
1697 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1698 struct die_info **, const gdb_byte *,
1699 int *);
1700
1701 static void process_die (struct die_info *, struct dwarf2_cu *);
1702
1703 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1704 struct obstack *);
1705
1706 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1707
1708 static const char *dwarf2_full_name (const char *name,
1709 struct die_info *die,
1710 struct dwarf2_cu *cu);
1711
1712 static const char *dwarf2_physname (const char *name, struct die_info *die,
1713 struct dwarf2_cu *cu);
1714
1715 static struct die_info *dwarf2_extension (struct die_info *die,
1716 struct dwarf2_cu **);
1717
1718 static const char *dwarf_tag_name (unsigned int);
1719
1720 static const char *dwarf_attr_name (unsigned int);
1721
1722 static const char *dwarf_form_name (unsigned int);
1723
1724 static char *dwarf_bool_name (unsigned int);
1725
1726 static const char *dwarf_type_encoding_name (unsigned int);
1727
1728 static struct die_info *sibling_die (struct die_info *);
1729
1730 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1731
1732 static void dump_die_for_error (struct die_info *);
1733
1734 static void dump_die_1 (struct ui_file *, int level, int max_level,
1735 struct die_info *);
1736
1737 /*static*/ void dump_die (struct die_info *, int max_level);
1738
1739 static void store_in_ref_table (struct die_info *,
1740 struct dwarf2_cu *);
1741
1742 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1743
1744 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1745
1746 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1747 const struct attribute *,
1748 struct dwarf2_cu **);
1749
1750 static struct die_info *follow_die_ref (struct die_info *,
1751 const struct attribute *,
1752 struct dwarf2_cu **);
1753
1754 static struct die_info *follow_die_sig (struct die_info *,
1755 const struct attribute *,
1756 struct dwarf2_cu **);
1757
1758 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1759 struct dwarf2_cu *);
1760
1761 static struct type *get_DW_AT_signature_type (struct die_info *,
1762 const struct attribute *,
1763 struct dwarf2_cu *);
1764
1765 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1766
1767 static void read_signatured_type (struct signatured_type *);
1768
1769 static int attr_to_dynamic_prop (const struct attribute *attr,
1770 struct die_info *die, struct dwarf2_cu *cu,
1771 struct dynamic_prop *prop);
1772
1773 /* memory allocation interface */
1774
1775 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1776
1777 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1778
1779 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1780
1781 static int attr_form_is_block (const struct attribute *);
1782
1783 static int attr_form_is_section_offset (const struct attribute *);
1784
1785 static int attr_form_is_constant (const struct attribute *);
1786
1787 static int attr_form_is_ref (const struct attribute *);
1788
1789 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1790 struct dwarf2_loclist_baton *baton,
1791 const struct attribute *attr);
1792
1793 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1794 struct symbol *sym,
1795 struct dwarf2_cu *cu,
1796 int is_block);
1797
1798 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1799 const gdb_byte *info_ptr,
1800 struct abbrev_info *abbrev);
1801
1802 static void free_stack_comp_unit (void *);
1803
1804 static hashval_t partial_die_hash (const void *item);
1805
1806 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1807
1808 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1809 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1810
1811 static void init_one_comp_unit (struct dwarf2_cu *cu,
1812 struct dwarf2_per_cu_data *per_cu);
1813
1814 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1815 struct die_info *comp_unit_die,
1816 enum language pretend_language);
1817
1818 static void free_heap_comp_unit (void *);
1819
1820 static void free_cached_comp_units (void *);
1821
1822 static void age_cached_comp_units (void);
1823
1824 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1825
1826 static struct type *set_die_type (struct die_info *, struct type *,
1827 struct dwarf2_cu *);
1828
1829 static void create_all_comp_units (struct objfile *);
1830
1831 static int create_all_type_units (struct objfile *);
1832
1833 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1834 enum language);
1835
1836 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1837 enum language);
1838
1839 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1840 enum language);
1841
1842 static void dwarf2_add_dependence (struct dwarf2_cu *,
1843 struct dwarf2_per_cu_data *);
1844
1845 static void dwarf2_mark (struct dwarf2_cu *);
1846
1847 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1848
1849 static struct type *get_die_type_at_offset (sect_offset,
1850 struct dwarf2_per_cu_data *);
1851
1852 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1853
1854 static void dwarf2_release_queue (void *dummy);
1855
1856 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1857 enum language pretend_language);
1858
1859 static void process_queue (void);
1860
1861 static void find_file_and_directory (struct die_info *die,
1862 struct dwarf2_cu *cu,
1863 const char **name, const char **comp_dir);
1864
1865 static char *file_full_name (int file, struct line_header *lh,
1866 const char *comp_dir);
1867
1868 static const gdb_byte *read_and_check_comp_unit_head
1869 (struct comp_unit_head *header,
1870 struct dwarf2_section_info *section,
1871 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1872 int is_debug_types_section);
1873
1874 static void init_cutu_and_read_dies
1875 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1876 int use_existing_cu, int keep,
1877 die_reader_func_ftype *die_reader_func, void *data);
1878
1879 static void init_cutu_and_read_dies_simple
1880 (struct dwarf2_per_cu_data *this_cu,
1881 die_reader_func_ftype *die_reader_func, void *data);
1882
1883 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1884
1885 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1886
1887 static struct dwo_unit *lookup_dwo_unit_in_dwp
1888 (struct dwp_file *dwp_file, const char *comp_dir,
1889 ULONGEST signature, int is_debug_types);
1890
1891 static struct dwp_file *get_dwp_file (void);
1892
1893 static struct dwo_unit *lookup_dwo_comp_unit
1894 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1895
1896 static struct dwo_unit *lookup_dwo_type_unit
1897 (struct signatured_type *, const char *, const char *);
1898
1899 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1900
1901 static void free_dwo_file_cleanup (void *);
1902
1903 static void process_cu_includes (void);
1904
1905 static void check_producer (struct dwarf2_cu *cu);
1906
1907 static void free_line_header_voidp (void *arg);
1908 \f
1909 /* Various complaints about symbol reading that don't abort the process. */
1910
1911 static void
1912 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1913 {
1914 complaint (&symfile_complaints,
1915 _("statement list doesn't fit in .debug_line section"));
1916 }
1917
1918 static void
1919 dwarf2_debug_line_missing_file_complaint (void)
1920 {
1921 complaint (&symfile_complaints,
1922 _(".debug_line section has line data without a file"));
1923 }
1924
1925 static void
1926 dwarf2_debug_line_missing_end_sequence_complaint (void)
1927 {
1928 complaint (&symfile_complaints,
1929 _(".debug_line section has line "
1930 "program sequence without an end"));
1931 }
1932
1933 static void
1934 dwarf2_complex_location_expr_complaint (void)
1935 {
1936 complaint (&symfile_complaints, _("location expression too complex"));
1937 }
1938
1939 static void
1940 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1941 int arg3)
1942 {
1943 complaint (&symfile_complaints,
1944 _("const value length mismatch for '%s', got %d, expected %d"),
1945 arg1, arg2, arg3);
1946 }
1947
1948 static void
1949 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1950 {
1951 complaint (&symfile_complaints,
1952 _("debug info runs off end of %s section"
1953 " [in module %s]"),
1954 get_section_name (section),
1955 get_section_file_name (section));
1956 }
1957
1958 static void
1959 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1960 {
1961 complaint (&symfile_complaints,
1962 _("macro debug info contains a "
1963 "malformed macro definition:\n`%s'"),
1964 arg1);
1965 }
1966
1967 static void
1968 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1969 {
1970 complaint (&symfile_complaints,
1971 _("invalid attribute class or form for '%s' in '%s'"),
1972 arg1, arg2);
1973 }
1974
1975 /* Hash function for line_header_hash. */
1976
1977 static hashval_t
1978 line_header_hash (const struct line_header *ofs)
1979 {
1980 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
1981 }
1982
1983 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1984
1985 static hashval_t
1986 line_header_hash_voidp (const void *item)
1987 {
1988 const struct line_header *ofs = (const struct line_header *) item;
1989
1990 return line_header_hash (ofs);
1991 }
1992
1993 /* Equality function for line_header_hash. */
1994
1995 static int
1996 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1997 {
1998 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1999 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2000
2001 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
2002 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2003 }
2004
2005 \f
2006 #if WORDS_BIGENDIAN
2007
2008 /* Convert VALUE between big- and little-endian. */
2009 static offset_type
2010 byte_swap (offset_type value)
2011 {
2012 offset_type result;
2013
2014 result = (value & 0xff) << 24;
2015 result |= (value & 0xff00) << 8;
2016 result |= (value & 0xff0000) >> 8;
2017 result |= (value & 0xff000000) >> 24;
2018 return result;
2019 }
2020
2021 #define MAYBE_SWAP(V) byte_swap (V)
2022
2023 #else
2024 #define MAYBE_SWAP(V) (V)
2025 #endif /* WORDS_BIGENDIAN */
2026
2027 /* Read the given attribute value as an address, taking the attribute's
2028 form into account. */
2029
2030 static CORE_ADDR
2031 attr_value_as_address (struct attribute *attr)
2032 {
2033 CORE_ADDR addr;
2034
2035 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2036 {
2037 /* Aside from a few clearly defined exceptions, attributes that
2038 contain an address must always be in DW_FORM_addr form.
2039 Unfortunately, some compilers happen to be violating this
2040 requirement by encoding addresses using other forms, such
2041 as DW_FORM_data4 for example. For those broken compilers,
2042 we try to do our best, without any guarantee of success,
2043 to interpret the address correctly. It would also be nice
2044 to generate a complaint, but that would require us to maintain
2045 a list of legitimate cases where a non-address form is allowed,
2046 as well as update callers to pass in at least the CU's DWARF
2047 version. This is more overhead than what we're willing to
2048 expand for a pretty rare case. */
2049 addr = DW_UNSND (attr);
2050 }
2051 else
2052 addr = DW_ADDR (attr);
2053
2054 return addr;
2055 }
2056
2057 /* The suffix for an index file. */
2058 #define INDEX_SUFFIX ".gdb-index"
2059
2060 /* Try to locate the sections we need for DWARF 2 debugging
2061 information and return true if we have enough to do something.
2062 NAMES points to the dwarf2 section names, or is NULL if the standard
2063 ELF names are used. */
2064
2065 int
2066 dwarf2_has_info (struct objfile *objfile,
2067 const struct dwarf2_debug_sections *names)
2068 {
2069 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2070 objfile_data (objfile, dwarf2_objfile_data_key));
2071 if (!dwarf2_per_objfile)
2072 {
2073 /* Initialize per-objfile state. */
2074 struct dwarf2_per_objfile *data
2075 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2076
2077 memset (data, 0, sizeof (*data));
2078 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2079 dwarf2_per_objfile = data;
2080
2081 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2082 (void *) names);
2083 dwarf2_per_objfile->objfile = objfile;
2084 }
2085 return (!dwarf2_per_objfile->info.is_virtual
2086 && dwarf2_per_objfile->info.s.section != NULL
2087 && !dwarf2_per_objfile->abbrev.is_virtual
2088 && dwarf2_per_objfile->abbrev.s.section != NULL);
2089 }
2090
2091 /* Return the containing section of virtual section SECTION. */
2092
2093 static struct dwarf2_section_info *
2094 get_containing_section (const struct dwarf2_section_info *section)
2095 {
2096 gdb_assert (section->is_virtual);
2097 return section->s.containing_section;
2098 }
2099
2100 /* Return the bfd owner of SECTION. */
2101
2102 static struct bfd *
2103 get_section_bfd_owner (const struct dwarf2_section_info *section)
2104 {
2105 if (section->is_virtual)
2106 {
2107 section = get_containing_section (section);
2108 gdb_assert (!section->is_virtual);
2109 }
2110 return section->s.section->owner;
2111 }
2112
2113 /* Return the bfd section of SECTION.
2114 Returns NULL if the section is not present. */
2115
2116 static asection *
2117 get_section_bfd_section (const struct dwarf2_section_info *section)
2118 {
2119 if (section->is_virtual)
2120 {
2121 section = get_containing_section (section);
2122 gdb_assert (!section->is_virtual);
2123 }
2124 return section->s.section;
2125 }
2126
2127 /* Return the name of SECTION. */
2128
2129 static const char *
2130 get_section_name (const struct dwarf2_section_info *section)
2131 {
2132 asection *sectp = get_section_bfd_section (section);
2133
2134 gdb_assert (sectp != NULL);
2135 return bfd_section_name (get_section_bfd_owner (section), sectp);
2136 }
2137
2138 /* Return the name of the file SECTION is in. */
2139
2140 static const char *
2141 get_section_file_name (const struct dwarf2_section_info *section)
2142 {
2143 bfd *abfd = get_section_bfd_owner (section);
2144
2145 return bfd_get_filename (abfd);
2146 }
2147
2148 /* Return the id of SECTION.
2149 Returns 0 if SECTION doesn't exist. */
2150
2151 static int
2152 get_section_id (const struct dwarf2_section_info *section)
2153 {
2154 asection *sectp = get_section_bfd_section (section);
2155
2156 if (sectp == NULL)
2157 return 0;
2158 return sectp->id;
2159 }
2160
2161 /* Return the flags of SECTION.
2162 SECTION (or containing section if this is a virtual section) must exist. */
2163
2164 static int
2165 get_section_flags (const struct dwarf2_section_info *section)
2166 {
2167 asection *sectp = get_section_bfd_section (section);
2168
2169 gdb_assert (sectp != NULL);
2170 return bfd_get_section_flags (sectp->owner, sectp);
2171 }
2172
2173 /* When loading sections, we look either for uncompressed section or for
2174 compressed section names. */
2175
2176 static int
2177 section_is_p (const char *section_name,
2178 const struct dwarf2_section_names *names)
2179 {
2180 if (names->normal != NULL
2181 && strcmp (section_name, names->normal) == 0)
2182 return 1;
2183 if (names->compressed != NULL
2184 && strcmp (section_name, names->compressed) == 0)
2185 return 1;
2186 return 0;
2187 }
2188
2189 /* This function is mapped across the sections and remembers the
2190 offset and size of each of the debugging sections we are interested
2191 in. */
2192
2193 static void
2194 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
2195 {
2196 const struct dwarf2_debug_sections *names;
2197 flagword aflag = bfd_get_section_flags (abfd, sectp);
2198
2199 if (vnames == NULL)
2200 names = &dwarf2_elf_names;
2201 else
2202 names = (const struct dwarf2_debug_sections *) vnames;
2203
2204 if ((aflag & SEC_HAS_CONTENTS) == 0)
2205 {
2206 }
2207 else if (section_is_p (sectp->name, &names->info))
2208 {
2209 dwarf2_per_objfile->info.s.section = sectp;
2210 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
2211 }
2212 else if (section_is_p (sectp->name, &names->abbrev))
2213 {
2214 dwarf2_per_objfile->abbrev.s.section = sectp;
2215 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
2216 }
2217 else if (section_is_p (sectp->name, &names->line))
2218 {
2219 dwarf2_per_objfile->line.s.section = sectp;
2220 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
2221 }
2222 else if (section_is_p (sectp->name, &names->loc))
2223 {
2224 dwarf2_per_objfile->loc.s.section = sectp;
2225 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
2226 }
2227 else if (section_is_p (sectp->name, &names->macinfo))
2228 {
2229 dwarf2_per_objfile->macinfo.s.section = sectp;
2230 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
2231 }
2232 else if (section_is_p (sectp->name, &names->macro))
2233 {
2234 dwarf2_per_objfile->macro.s.section = sectp;
2235 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2236 }
2237 else if (section_is_p (sectp->name, &names->str))
2238 {
2239 dwarf2_per_objfile->str.s.section = sectp;
2240 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
2241 }
2242 else if (section_is_p (sectp->name, &names->addr))
2243 {
2244 dwarf2_per_objfile->addr.s.section = sectp;
2245 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2246 }
2247 else if (section_is_p (sectp->name, &names->frame))
2248 {
2249 dwarf2_per_objfile->frame.s.section = sectp;
2250 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
2251 }
2252 else if (section_is_p (sectp->name, &names->eh_frame))
2253 {
2254 dwarf2_per_objfile->eh_frame.s.section = sectp;
2255 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
2256 }
2257 else if (section_is_p (sectp->name, &names->ranges))
2258 {
2259 dwarf2_per_objfile->ranges.s.section = sectp;
2260 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
2261 }
2262 else if (section_is_p (sectp->name, &names->types))
2263 {
2264 struct dwarf2_section_info type_section;
2265
2266 memset (&type_section, 0, sizeof (type_section));
2267 type_section.s.section = sectp;
2268 type_section.size = bfd_get_section_size (sectp);
2269
2270 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2271 &type_section);
2272 }
2273 else if (section_is_p (sectp->name, &names->gdb_index))
2274 {
2275 dwarf2_per_objfile->gdb_index.s.section = sectp;
2276 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2277 }
2278
2279 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2280 && bfd_section_vma (abfd, sectp) == 0)
2281 dwarf2_per_objfile->has_section_at_zero = 1;
2282 }
2283
2284 /* A helper function that decides whether a section is empty,
2285 or not present. */
2286
2287 static int
2288 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2289 {
2290 if (section->is_virtual)
2291 return section->size == 0;
2292 return section->s.section == NULL || section->size == 0;
2293 }
2294
2295 /* Read the contents of the section INFO.
2296 OBJFILE is the main object file, but not necessarily the file where
2297 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2298 of the DWO file.
2299 If the section is compressed, uncompress it before returning. */
2300
2301 static void
2302 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2303 {
2304 asection *sectp;
2305 bfd *abfd;
2306 gdb_byte *buf, *retbuf;
2307
2308 if (info->readin)
2309 return;
2310 info->buffer = NULL;
2311 info->readin = 1;
2312
2313 if (dwarf2_section_empty_p (info))
2314 return;
2315
2316 sectp = get_section_bfd_section (info);
2317
2318 /* If this is a virtual section we need to read in the real one first. */
2319 if (info->is_virtual)
2320 {
2321 struct dwarf2_section_info *containing_section =
2322 get_containing_section (info);
2323
2324 gdb_assert (sectp != NULL);
2325 if ((sectp->flags & SEC_RELOC) != 0)
2326 {
2327 error (_("Dwarf Error: DWP format V2 with relocations is not"
2328 " supported in section %s [in module %s]"),
2329 get_section_name (info), get_section_file_name (info));
2330 }
2331 dwarf2_read_section (objfile, containing_section);
2332 /* Other code should have already caught virtual sections that don't
2333 fit. */
2334 gdb_assert (info->virtual_offset + info->size
2335 <= containing_section->size);
2336 /* If the real section is empty or there was a problem reading the
2337 section we shouldn't get here. */
2338 gdb_assert (containing_section->buffer != NULL);
2339 info->buffer = containing_section->buffer + info->virtual_offset;
2340 return;
2341 }
2342
2343 /* If the section has relocations, we must read it ourselves.
2344 Otherwise we attach it to the BFD. */
2345 if ((sectp->flags & SEC_RELOC) == 0)
2346 {
2347 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2348 return;
2349 }
2350
2351 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2352 info->buffer = buf;
2353
2354 /* When debugging .o files, we may need to apply relocations; see
2355 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2356 We never compress sections in .o files, so we only need to
2357 try this when the section is not compressed. */
2358 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2359 if (retbuf != NULL)
2360 {
2361 info->buffer = retbuf;
2362 return;
2363 }
2364
2365 abfd = get_section_bfd_owner (info);
2366 gdb_assert (abfd != NULL);
2367
2368 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2369 || bfd_bread (buf, info->size, abfd) != info->size)
2370 {
2371 error (_("Dwarf Error: Can't read DWARF data"
2372 " in section %s [in module %s]"),
2373 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2374 }
2375 }
2376
2377 /* A helper function that returns the size of a section in a safe way.
2378 If you are positive that the section has been read before using the
2379 size, then it is safe to refer to the dwarf2_section_info object's
2380 "size" field directly. In other cases, you must call this
2381 function, because for compressed sections the size field is not set
2382 correctly until the section has been read. */
2383
2384 static bfd_size_type
2385 dwarf2_section_size (struct objfile *objfile,
2386 struct dwarf2_section_info *info)
2387 {
2388 if (!info->readin)
2389 dwarf2_read_section (objfile, info);
2390 return info->size;
2391 }
2392
2393 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2394 SECTION_NAME. */
2395
2396 void
2397 dwarf2_get_section_info (struct objfile *objfile,
2398 enum dwarf2_section_enum sect,
2399 asection **sectp, const gdb_byte **bufp,
2400 bfd_size_type *sizep)
2401 {
2402 struct dwarf2_per_objfile *data
2403 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2404 dwarf2_objfile_data_key);
2405 struct dwarf2_section_info *info;
2406
2407 /* We may see an objfile without any DWARF, in which case we just
2408 return nothing. */
2409 if (data == NULL)
2410 {
2411 *sectp = NULL;
2412 *bufp = NULL;
2413 *sizep = 0;
2414 return;
2415 }
2416 switch (sect)
2417 {
2418 case DWARF2_DEBUG_FRAME:
2419 info = &data->frame;
2420 break;
2421 case DWARF2_EH_FRAME:
2422 info = &data->eh_frame;
2423 break;
2424 default:
2425 gdb_assert_not_reached ("unexpected section");
2426 }
2427
2428 dwarf2_read_section (objfile, info);
2429
2430 *sectp = get_section_bfd_section (info);
2431 *bufp = info->buffer;
2432 *sizep = info->size;
2433 }
2434
2435 /* A helper function to find the sections for a .dwz file. */
2436
2437 static void
2438 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2439 {
2440 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2441
2442 /* Note that we only support the standard ELF names, because .dwz
2443 is ELF-only (at the time of writing). */
2444 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2445 {
2446 dwz_file->abbrev.s.section = sectp;
2447 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2448 }
2449 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2450 {
2451 dwz_file->info.s.section = sectp;
2452 dwz_file->info.size = bfd_get_section_size (sectp);
2453 }
2454 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2455 {
2456 dwz_file->str.s.section = sectp;
2457 dwz_file->str.size = bfd_get_section_size (sectp);
2458 }
2459 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2460 {
2461 dwz_file->line.s.section = sectp;
2462 dwz_file->line.size = bfd_get_section_size (sectp);
2463 }
2464 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2465 {
2466 dwz_file->macro.s.section = sectp;
2467 dwz_file->macro.size = bfd_get_section_size (sectp);
2468 }
2469 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2470 {
2471 dwz_file->gdb_index.s.section = sectp;
2472 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2473 }
2474 }
2475
2476 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2477 there is no .gnu_debugaltlink section in the file. Error if there
2478 is such a section but the file cannot be found. */
2479
2480 static struct dwz_file *
2481 dwarf2_get_dwz_file (void)
2482 {
2483 char *data;
2484 struct cleanup *cleanup;
2485 const char *filename;
2486 struct dwz_file *result;
2487 bfd_size_type buildid_len_arg;
2488 size_t buildid_len;
2489 bfd_byte *buildid;
2490
2491 if (dwarf2_per_objfile->dwz_file != NULL)
2492 return dwarf2_per_objfile->dwz_file;
2493
2494 bfd_set_error (bfd_error_no_error);
2495 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2496 &buildid_len_arg, &buildid);
2497 if (data == NULL)
2498 {
2499 if (bfd_get_error () == bfd_error_no_error)
2500 return NULL;
2501 error (_("could not read '.gnu_debugaltlink' section: %s"),
2502 bfd_errmsg (bfd_get_error ()));
2503 }
2504 cleanup = make_cleanup (xfree, data);
2505 make_cleanup (xfree, buildid);
2506
2507 buildid_len = (size_t) buildid_len_arg;
2508
2509 filename = (const char *) data;
2510 if (!IS_ABSOLUTE_PATH (filename))
2511 {
2512 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2513 char *rel;
2514
2515 make_cleanup (xfree, abs);
2516 abs = ldirname (abs);
2517 make_cleanup (xfree, abs);
2518
2519 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2520 make_cleanup (xfree, rel);
2521 filename = rel;
2522 }
2523
2524 /* First try the file name given in the section. If that doesn't
2525 work, try to use the build-id instead. */
2526 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2527 if (dwz_bfd != NULL)
2528 {
2529 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2530 dwz_bfd.release ();
2531 }
2532
2533 if (dwz_bfd == NULL)
2534 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2535
2536 if (dwz_bfd == NULL)
2537 error (_("could not find '.gnu_debugaltlink' file for %s"),
2538 objfile_name (dwarf2_per_objfile->objfile));
2539
2540 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2541 struct dwz_file);
2542 result->dwz_bfd = dwz_bfd.release ();
2543
2544 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2545
2546 do_cleanups (cleanup);
2547
2548 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2549 dwarf2_per_objfile->dwz_file = result;
2550 return result;
2551 }
2552 \f
2553 /* DWARF quick_symbols_functions support. */
2554
2555 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2556 unique line tables, so we maintain a separate table of all .debug_line
2557 derived entries to support the sharing.
2558 All the quick functions need is the list of file names. We discard the
2559 line_header when we're done and don't need to record it here. */
2560 struct quick_file_names
2561 {
2562 /* The data used to construct the hash key. */
2563 struct stmt_list_hash hash;
2564
2565 /* The number of entries in file_names, real_names. */
2566 unsigned int num_file_names;
2567
2568 /* The file names from the line table, after being run through
2569 file_full_name. */
2570 const char **file_names;
2571
2572 /* The file names from the line table after being run through
2573 gdb_realpath. These are computed lazily. */
2574 const char **real_names;
2575 };
2576
2577 /* When using the index (and thus not using psymtabs), each CU has an
2578 object of this type. This is used to hold information needed by
2579 the various "quick" methods. */
2580 struct dwarf2_per_cu_quick_data
2581 {
2582 /* The file table. This can be NULL if there was no file table
2583 or it's currently not read in.
2584 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2585 struct quick_file_names *file_names;
2586
2587 /* The corresponding symbol table. This is NULL if symbols for this
2588 CU have not yet been read. */
2589 struct compunit_symtab *compunit_symtab;
2590
2591 /* A temporary mark bit used when iterating over all CUs in
2592 expand_symtabs_matching. */
2593 unsigned int mark : 1;
2594
2595 /* True if we've tried to read the file table and found there isn't one.
2596 There will be no point in trying to read it again next time. */
2597 unsigned int no_file_data : 1;
2598 };
2599
2600 /* Utility hash function for a stmt_list_hash. */
2601
2602 static hashval_t
2603 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2604 {
2605 hashval_t v = 0;
2606
2607 if (stmt_list_hash->dwo_unit != NULL)
2608 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2609 v += stmt_list_hash->line_offset.sect_off;
2610 return v;
2611 }
2612
2613 /* Utility equality function for a stmt_list_hash. */
2614
2615 static int
2616 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2617 const struct stmt_list_hash *rhs)
2618 {
2619 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2620 return 0;
2621 if (lhs->dwo_unit != NULL
2622 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2623 return 0;
2624
2625 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2626 }
2627
2628 /* Hash function for a quick_file_names. */
2629
2630 static hashval_t
2631 hash_file_name_entry (const void *e)
2632 {
2633 const struct quick_file_names *file_data
2634 = (const struct quick_file_names *) e;
2635
2636 return hash_stmt_list_entry (&file_data->hash);
2637 }
2638
2639 /* Equality function for a quick_file_names. */
2640
2641 static int
2642 eq_file_name_entry (const void *a, const void *b)
2643 {
2644 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2645 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2646
2647 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2648 }
2649
2650 /* Delete function for a quick_file_names. */
2651
2652 static void
2653 delete_file_name_entry (void *e)
2654 {
2655 struct quick_file_names *file_data = (struct quick_file_names *) e;
2656 int i;
2657
2658 for (i = 0; i < file_data->num_file_names; ++i)
2659 {
2660 xfree ((void*) file_data->file_names[i]);
2661 if (file_data->real_names)
2662 xfree ((void*) file_data->real_names[i]);
2663 }
2664
2665 /* The space for the struct itself lives on objfile_obstack,
2666 so we don't free it here. */
2667 }
2668
2669 /* Create a quick_file_names hash table. */
2670
2671 static htab_t
2672 create_quick_file_names_table (unsigned int nr_initial_entries)
2673 {
2674 return htab_create_alloc (nr_initial_entries,
2675 hash_file_name_entry, eq_file_name_entry,
2676 delete_file_name_entry, xcalloc, xfree);
2677 }
2678
2679 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2680 have to be created afterwards. You should call age_cached_comp_units after
2681 processing PER_CU->CU. dw2_setup must have been already called. */
2682
2683 static void
2684 load_cu (struct dwarf2_per_cu_data *per_cu)
2685 {
2686 if (per_cu->is_debug_types)
2687 load_full_type_unit (per_cu);
2688 else
2689 load_full_comp_unit (per_cu, language_minimal);
2690
2691 if (per_cu->cu == NULL)
2692 return; /* Dummy CU. */
2693
2694 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2695 }
2696
2697 /* Read in the symbols for PER_CU. */
2698
2699 static void
2700 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2701 {
2702 struct cleanup *back_to;
2703
2704 /* Skip type_unit_groups, reading the type units they contain
2705 is handled elsewhere. */
2706 if (IS_TYPE_UNIT_GROUP (per_cu))
2707 return;
2708
2709 back_to = make_cleanup (dwarf2_release_queue, NULL);
2710
2711 if (dwarf2_per_objfile->using_index
2712 ? per_cu->v.quick->compunit_symtab == NULL
2713 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2714 {
2715 queue_comp_unit (per_cu, language_minimal);
2716 load_cu (per_cu);
2717
2718 /* If we just loaded a CU from a DWO, and we're working with an index
2719 that may badly handle TUs, load all the TUs in that DWO as well.
2720 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2721 if (!per_cu->is_debug_types
2722 && per_cu->cu != NULL
2723 && per_cu->cu->dwo_unit != NULL
2724 && dwarf2_per_objfile->index_table != NULL
2725 && dwarf2_per_objfile->index_table->version <= 7
2726 /* DWP files aren't supported yet. */
2727 && get_dwp_file () == NULL)
2728 queue_and_load_all_dwo_tus (per_cu);
2729 }
2730
2731 process_queue ();
2732
2733 /* Age the cache, releasing compilation units that have not
2734 been used recently. */
2735 age_cached_comp_units ();
2736
2737 do_cleanups (back_to);
2738 }
2739
2740 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2741 the objfile from which this CU came. Returns the resulting symbol
2742 table. */
2743
2744 static struct compunit_symtab *
2745 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2746 {
2747 gdb_assert (dwarf2_per_objfile->using_index);
2748 if (!per_cu->v.quick->compunit_symtab)
2749 {
2750 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2751 increment_reading_symtab ();
2752 dw2_do_instantiate_symtab (per_cu);
2753 process_cu_includes ();
2754 do_cleanups (back_to);
2755 }
2756
2757 return per_cu->v.quick->compunit_symtab;
2758 }
2759
2760 /* Return the CU/TU given its index.
2761
2762 This is intended for loops like:
2763
2764 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2765 + dwarf2_per_objfile->n_type_units); ++i)
2766 {
2767 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2768
2769 ...;
2770 }
2771 */
2772
2773 static struct dwarf2_per_cu_data *
2774 dw2_get_cutu (int index)
2775 {
2776 if (index >= dwarf2_per_objfile->n_comp_units)
2777 {
2778 index -= dwarf2_per_objfile->n_comp_units;
2779 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2780 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2781 }
2782
2783 return dwarf2_per_objfile->all_comp_units[index];
2784 }
2785
2786 /* Return the CU given its index.
2787 This differs from dw2_get_cutu in that it's for when you know INDEX
2788 refers to a CU. */
2789
2790 static struct dwarf2_per_cu_data *
2791 dw2_get_cu (int index)
2792 {
2793 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2794
2795 return dwarf2_per_objfile->all_comp_units[index];
2796 }
2797
2798 /* A helper for create_cus_from_index that handles a given list of
2799 CUs. */
2800
2801 static void
2802 create_cus_from_index_list (struct objfile *objfile,
2803 const gdb_byte *cu_list, offset_type n_elements,
2804 struct dwarf2_section_info *section,
2805 int is_dwz,
2806 int base_offset)
2807 {
2808 offset_type i;
2809
2810 for (i = 0; i < n_elements; i += 2)
2811 {
2812 struct dwarf2_per_cu_data *the_cu;
2813 ULONGEST offset, length;
2814
2815 gdb_static_assert (sizeof (ULONGEST) >= 8);
2816 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2817 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2818 cu_list += 2 * 8;
2819
2820 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2821 struct dwarf2_per_cu_data);
2822 the_cu->offset.sect_off = offset;
2823 the_cu->length = length;
2824 the_cu->objfile = objfile;
2825 the_cu->section = section;
2826 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2827 struct dwarf2_per_cu_quick_data);
2828 the_cu->is_dwz = is_dwz;
2829 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2830 }
2831 }
2832
2833 /* Read the CU list from the mapped index, and use it to create all
2834 the CU objects for this objfile. */
2835
2836 static void
2837 create_cus_from_index (struct objfile *objfile,
2838 const gdb_byte *cu_list, offset_type cu_list_elements,
2839 const gdb_byte *dwz_list, offset_type dwz_elements)
2840 {
2841 struct dwz_file *dwz;
2842
2843 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2844 dwarf2_per_objfile->all_comp_units =
2845 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2846 dwarf2_per_objfile->n_comp_units);
2847
2848 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2849 &dwarf2_per_objfile->info, 0, 0);
2850
2851 if (dwz_elements == 0)
2852 return;
2853
2854 dwz = dwarf2_get_dwz_file ();
2855 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2856 cu_list_elements / 2);
2857 }
2858
2859 /* Create the signatured type hash table from the index. */
2860
2861 static void
2862 create_signatured_type_table_from_index (struct objfile *objfile,
2863 struct dwarf2_section_info *section,
2864 const gdb_byte *bytes,
2865 offset_type elements)
2866 {
2867 offset_type i;
2868 htab_t sig_types_hash;
2869
2870 dwarf2_per_objfile->n_type_units
2871 = dwarf2_per_objfile->n_allocated_type_units
2872 = elements / 3;
2873 dwarf2_per_objfile->all_type_units =
2874 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
2875
2876 sig_types_hash = allocate_signatured_type_table (objfile);
2877
2878 for (i = 0; i < elements; i += 3)
2879 {
2880 struct signatured_type *sig_type;
2881 ULONGEST offset, type_offset_in_tu, signature;
2882 void **slot;
2883
2884 gdb_static_assert (sizeof (ULONGEST) >= 8);
2885 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2886 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2887 BFD_ENDIAN_LITTLE);
2888 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2889 bytes += 3 * 8;
2890
2891 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2892 struct signatured_type);
2893 sig_type->signature = signature;
2894 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2895 sig_type->per_cu.is_debug_types = 1;
2896 sig_type->per_cu.section = section;
2897 sig_type->per_cu.offset.sect_off = offset;
2898 sig_type->per_cu.objfile = objfile;
2899 sig_type->per_cu.v.quick
2900 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2901 struct dwarf2_per_cu_quick_data);
2902
2903 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2904 *slot = sig_type;
2905
2906 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2907 }
2908
2909 dwarf2_per_objfile->signatured_types = sig_types_hash;
2910 }
2911
2912 /* Read the address map data from the mapped index, and use it to
2913 populate the objfile's psymtabs_addrmap. */
2914
2915 static void
2916 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2917 {
2918 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2919 const gdb_byte *iter, *end;
2920 struct obstack temp_obstack;
2921 struct addrmap *mutable_map;
2922 struct cleanup *cleanup;
2923 CORE_ADDR baseaddr;
2924
2925 obstack_init (&temp_obstack);
2926 cleanup = make_cleanup_obstack_free (&temp_obstack);
2927 mutable_map = addrmap_create_mutable (&temp_obstack);
2928
2929 iter = index->address_table;
2930 end = iter + index->address_table_size;
2931
2932 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2933
2934 while (iter < end)
2935 {
2936 ULONGEST hi, lo, cu_index;
2937 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2938 iter += 8;
2939 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2940 iter += 8;
2941 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2942 iter += 4;
2943
2944 if (lo > hi)
2945 {
2946 complaint (&symfile_complaints,
2947 _(".gdb_index address table has invalid range (%s - %s)"),
2948 hex_string (lo), hex_string (hi));
2949 continue;
2950 }
2951
2952 if (cu_index >= dwarf2_per_objfile->n_comp_units)
2953 {
2954 complaint (&symfile_complaints,
2955 _(".gdb_index address table has invalid CU number %u"),
2956 (unsigned) cu_index);
2957 continue;
2958 }
2959
2960 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
2961 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
2962 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
2963 }
2964
2965 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2966 &objfile->objfile_obstack);
2967 do_cleanups (cleanup);
2968 }
2969
2970 /* The hash function for strings in the mapped index. This is the same as
2971 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2972 implementation. This is necessary because the hash function is tied to the
2973 format of the mapped index file. The hash values do not have to match with
2974 SYMBOL_HASH_NEXT.
2975
2976 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2977
2978 static hashval_t
2979 mapped_index_string_hash (int index_version, const void *p)
2980 {
2981 const unsigned char *str = (const unsigned char *) p;
2982 hashval_t r = 0;
2983 unsigned char c;
2984
2985 while ((c = *str++) != 0)
2986 {
2987 if (index_version >= 5)
2988 c = tolower (c);
2989 r = r * 67 + c - 113;
2990 }
2991
2992 return r;
2993 }
2994
2995 /* Find a slot in the mapped index INDEX for the object named NAME.
2996 If NAME is found, set *VEC_OUT to point to the CU vector in the
2997 constant pool and return 1. If NAME cannot be found, return 0. */
2998
2999 static int
3000 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3001 offset_type **vec_out)
3002 {
3003 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3004 offset_type hash;
3005 offset_type slot, step;
3006 int (*cmp) (const char *, const char *);
3007
3008 if (current_language->la_language == language_cplus
3009 || current_language->la_language == language_fortran
3010 || current_language->la_language == language_d)
3011 {
3012 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3013 not contain any. */
3014
3015 if (strchr (name, '(') != NULL)
3016 {
3017 char *without_params = cp_remove_params (name);
3018
3019 if (without_params != NULL)
3020 {
3021 make_cleanup (xfree, without_params);
3022 name = without_params;
3023 }
3024 }
3025 }
3026
3027 /* Index version 4 did not support case insensitive searches. But the
3028 indices for case insensitive languages are built in lowercase, therefore
3029 simulate our NAME being searched is also lowercased. */
3030 hash = mapped_index_string_hash ((index->version == 4
3031 && case_sensitivity == case_sensitive_off
3032 ? 5 : index->version),
3033 name);
3034
3035 slot = hash & (index->symbol_table_slots - 1);
3036 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3037 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3038
3039 for (;;)
3040 {
3041 /* Convert a slot number to an offset into the table. */
3042 offset_type i = 2 * slot;
3043 const char *str;
3044 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3045 {
3046 do_cleanups (back_to);
3047 return 0;
3048 }
3049
3050 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3051 if (!cmp (name, str))
3052 {
3053 *vec_out = (offset_type *) (index->constant_pool
3054 + MAYBE_SWAP (index->symbol_table[i + 1]));
3055 do_cleanups (back_to);
3056 return 1;
3057 }
3058
3059 slot = (slot + step) & (index->symbol_table_slots - 1);
3060 }
3061 }
3062
3063 /* A helper function that reads the .gdb_index from SECTION and fills
3064 in MAP. FILENAME is the name of the file containing the section;
3065 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3066 ok to use deprecated sections.
3067
3068 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3069 out parameters that are filled in with information about the CU and
3070 TU lists in the section.
3071
3072 Returns 1 if all went well, 0 otherwise. */
3073
3074 static int
3075 read_index_from_section (struct objfile *objfile,
3076 const char *filename,
3077 int deprecated_ok,
3078 struct dwarf2_section_info *section,
3079 struct mapped_index *map,
3080 const gdb_byte **cu_list,
3081 offset_type *cu_list_elements,
3082 const gdb_byte **types_list,
3083 offset_type *types_list_elements)
3084 {
3085 const gdb_byte *addr;
3086 offset_type version;
3087 offset_type *metadata;
3088 int i;
3089
3090 if (dwarf2_section_empty_p (section))
3091 return 0;
3092
3093 /* Older elfutils strip versions could keep the section in the main
3094 executable while splitting it for the separate debug info file. */
3095 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3096 return 0;
3097
3098 dwarf2_read_section (objfile, section);
3099
3100 addr = section->buffer;
3101 /* Version check. */
3102 version = MAYBE_SWAP (*(offset_type *) addr);
3103 /* Versions earlier than 3 emitted every copy of a psymbol. This
3104 causes the index to behave very poorly for certain requests. Version 3
3105 contained incomplete addrmap. So, it seems better to just ignore such
3106 indices. */
3107 if (version < 4)
3108 {
3109 static int warning_printed = 0;
3110 if (!warning_printed)
3111 {
3112 warning (_("Skipping obsolete .gdb_index section in %s."),
3113 filename);
3114 warning_printed = 1;
3115 }
3116 return 0;
3117 }
3118 /* Index version 4 uses a different hash function than index version
3119 5 and later.
3120
3121 Versions earlier than 6 did not emit psymbols for inlined
3122 functions. Using these files will cause GDB not to be able to
3123 set breakpoints on inlined functions by name, so we ignore these
3124 indices unless the user has done
3125 "set use-deprecated-index-sections on". */
3126 if (version < 6 && !deprecated_ok)
3127 {
3128 static int warning_printed = 0;
3129 if (!warning_printed)
3130 {
3131 warning (_("\
3132 Skipping deprecated .gdb_index section in %s.\n\
3133 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3134 to use the section anyway."),
3135 filename);
3136 warning_printed = 1;
3137 }
3138 return 0;
3139 }
3140 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3141 of the TU (for symbols coming from TUs),
3142 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3143 Plus gold-generated indices can have duplicate entries for global symbols,
3144 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3145 These are just performance bugs, and we can't distinguish gdb-generated
3146 indices from gold-generated ones, so issue no warning here. */
3147
3148 /* Indexes with higher version than the one supported by GDB may be no
3149 longer backward compatible. */
3150 if (version > 8)
3151 return 0;
3152
3153 map->version = version;
3154 map->total_size = section->size;
3155
3156 metadata = (offset_type *) (addr + sizeof (offset_type));
3157
3158 i = 0;
3159 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3160 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3161 / 8);
3162 ++i;
3163
3164 *types_list = addr + MAYBE_SWAP (metadata[i]);
3165 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3166 - MAYBE_SWAP (metadata[i]))
3167 / 8);
3168 ++i;
3169
3170 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3171 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3172 - MAYBE_SWAP (metadata[i]));
3173 ++i;
3174
3175 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3176 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3177 - MAYBE_SWAP (metadata[i]))
3178 / (2 * sizeof (offset_type)));
3179 ++i;
3180
3181 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3182
3183 return 1;
3184 }
3185
3186
3187 /* Read the index file. If everything went ok, initialize the "quick"
3188 elements of all the CUs and return 1. Otherwise, return 0. */
3189
3190 static int
3191 dwarf2_read_index (struct objfile *objfile)
3192 {
3193 struct mapped_index local_map, *map;
3194 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3195 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3196 struct dwz_file *dwz;
3197
3198 if (!read_index_from_section (objfile, objfile_name (objfile),
3199 use_deprecated_index_sections,
3200 &dwarf2_per_objfile->gdb_index, &local_map,
3201 &cu_list, &cu_list_elements,
3202 &types_list, &types_list_elements))
3203 return 0;
3204
3205 /* Don't use the index if it's empty. */
3206 if (local_map.symbol_table_slots == 0)
3207 return 0;
3208
3209 /* If there is a .dwz file, read it so we can get its CU list as
3210 well. */
3211 dwz = dwarf2_get_dwz_file ();
3212 if (dwz != NULL)
3213 {
3214 struct mapped_index dwz_map;
3215 const gdb_byte *dwz_types_ignore;
3216 offset_type dwz_types_elements_ignore;
3217
3218 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3219 1,
3220 &dwz->gdb_index, &dwz_map,
3221 &dwz_list, &dwz_list_elements,
3222 &dwz_types_ignore,
3223 &dwz_types_elements_ignore))
3224 {
3225 warning (_("could not read '.gdb_index' section from %s; skipping"),
3226 bfd_get_filename (dwz->dwz_bfd));
3227 return 0;
3228 }
3229 }
3230
3231 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3232 dwz_list_elements);
3233
3234 if (types_list_elements)
3235 {
3236 struct dwarf2_section_info *section;
3237
3238 /* We can only handle a single .debug_types when we have an
3239 index. */
3240 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3241 return 0;
3242
3243 section = VEC_index (dwarf2_section_info_def,
3244 dwarf2_per_objfile->types, 0);
3245
3246 create_signatured_type_table_from_index (objfile, section, types_list,
3247 types_list_elements);
3248 }
3249
3250 create_addrmap_from_index (objfile, &local_map);
3251
3252 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3253 *map = local_map;
3254
3255 dwarf2_per_objfile->index_table = map;
3256 dwarf2_per_objfile->using_index = 1;
3257 dwarf2_per_objfile->quick_file_names_table =
3258 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3259
3260 return 1;
3261 }
3262
3263 /* A helper for the "quick" functions which sets the global
3264 dwarf2_per_objfile according to OBJFILE. */
3265
3266 static void
3267 dw2_setup (struct objfile *objfile)
3268 {
3269 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3270 objfile_data (objfile, dwarf2_objfile_data_key));
3271 gdb_assert (dwarf2_per_objfile);
3272 }
3273
3274 /* die_reader_func for dw2_get_file_names. */
3275
3276 static void
3277 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3278 const gdb_byte *info_ptr,
3279 struct die_info *comp_unit_die,
3280 int has_children,
3281 void *data)
3282 {
3283 struct dwarf2_cu *cu = reader->cu;
3284 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3285 struct objfile *objfile = dwarf2_per_objfile->objfile;
3286 struct dwarf2_per_cu_data *lh_cu;
3287 struct line_header *lh;
3288 struct attribute *attr;
3289 int i;
3290 const char *name, *comp_dir;
3291 void **slot;
3292 struct quick_file_names *qfn;
3293 unsigned int line_offset;
3294
3295 gdb_assert (! this_cu->is_debug_types);
3296
3297 /* Our callers never want to match partial units -- instead they
3298 will match the enclosing full CU. */
3299 if (comp_unit_die->tag == DW_TAG_partial_unit)
3300 {
3301 this_cu->v.quick->no_file_data = 1;
3302 return;
3303 }
3304
3305 lh_cu = this_cu;
3306 lh = NULL;
3307 slot = NULL;
3308 line_offset = 0;
3309
3310 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3311 if (attr)
3312 {
3313 struct quick_file_names find_entry;
3314
3315 line_offset = DW_UNSND (attr);
3316
3317 /* We may have already read in this line header (TU line header sharing).
3318 If we have we're done. */
3319 find_entry.hash.dwo_unit = cu->dwo_unit;
3320 find_entry.hash.line_offset.sect_off = line_offset;
3321 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3322 &find_entry, INSERT);
3323 if (*slot != NULL)
3324 {
3325 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3326 return;
3327 }
3328
3329 lh = dwarf_decode_line_header (line_offset, cu);
3330 }
3331 if (lh == NULL)
3332 {
3333 lh_cu->v.quick->no_file_data = 1;
3334 return;
3335 }
3336
3337 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3338 qfn->hash.dwo_unit = cu->dwo_unit;
3339 qfn->hash.line_offset.sect_off = line_offset;
3340 gdb_assert (slot != NULL);
3341 *slot = qfn;
3342
3343 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
3344
3345 qfn->num_file_names = lh->num_file_names;
3346 qfn->file_names =
3347 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->num_file_names);
3348 for (i = 0; i < lh->num_file_names; ++i)
3349 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3350 qfn->real_names = NULL;
3351
3352 free_line_header (lh);
3353
3354 lh_cu->v.quick->file_names = qfn;
3355 }
3356
3357 /* A helper for the "quick" functions which attempts to read the line
3358 table for THIS_CU. */
3359
3360 static struct quick_file_names *
3361 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3362 {
3363 /* This should never be called for TUs. */
3364 gdb_assert (! this_cu->is_debug_types);
3365 /* Nor type unit groups. */
3366 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3367
3368 if (this_cu->v.quick->file_names != NULL)
3369 return this_cu->v.quick->file_names;
3370 /* If we know there is no line data, no point in looking again. */
3371 if (this_cu->v.quick->no_file_data)
3372 return NULL;
3373
3374 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3375
3376 if (this_cu->v.quick->no_file_data)
3377 return NULL;
3378 return this_cu->v.quick->file_names;
3379 }
3380
3381 /* A helper for the "quick" functions which computes and caches the
3382 real path for a given file name from the line table. */
3383
3384 static const char *
3385 dw2_get_real_path (struct objfile *objfile,
3386 struct quick_file_names *qfn, int index)
3387 {
3388 if (qfn->real_names == NULL)
3389 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3390 qfn->num_file_names, const char *);
3391
3392 if (qfn->real_names[index] == NULL)
3393 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3394
3395 return qfn->real_names[index];
3396 }
3397
3398 static struct symtab *
3399 dw2_find_last_source_symtab (struct objfile *objfile)
3400 {
3401 struct compunit_symtab *cust;
3402 int index;
3403
3404 dw2_setup (objfile);
3405 index = dwarf2_per_objfile->n_comp_units - 1;
3406 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3407 if (cust == NULL)
3408 return NULL;
3409 return compunit_primary_filetab (cust);
3410 }
3411
3412 /* Traversal function for dw2_forget_cached_source_info. */
3413
3414 static int
3415 dw2_free_cached_file_names (void **slot, void *info)
3416 {
3417 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3418
3419 if (file_data->real_names)
3420 {
3421 int i;
3422
3423 for (i = 0; i < file_data->num_file_names; ++i)
3424 {
3425 xfree ((void*) file_data->real_names[i]);
3426 file_data->real_names[i] = NULL;
3427 }
3428 }
3429
3430 return 1;
3431 }
3432
3433 static void
3434 dw2_forget_cached_source_info (struct objfile *objfile)
3435 {
3436 dw2_setup (objfile);
3437
3438 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3439 dw2_free_cached_file_names, NULL);
3440 }
3441
3442 /* Helper function for dw2_map_symtabs_matching_filename that expands
3443 the symtabs and calls the iterator. */
3444
3445 static int
3446 dw2_map_expand_apply (struct objfile *objfile,
3447 struct dwarf2_per_cu_data *per_cu,
3448 const char *name, const char *real_path,
3449 int (*callback) (struct symtab *, void *),
3450 void *data)
3451 {
3452 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3453
3454 /* Don't visit already-expanded CUs. */
3455 if (per_cu->v.quick->compunit_symtab)
3456 return 0;
3457
3458 /* This may expand more than one symtab, and we want to iterate over
3459 all of them. */
3460 dw2_instantiate_symtab (per_cu);
3461
3462 return iterate_over_some_symtabs (name, real_path, callback, data,
3463 objfile->compunit_symtabs, last_made);
3464 }
3465
3466 /* Implementation of the map_symtabs_matching_filename method. */
3467
3468 static int
3469 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3470 const char *real_path,
3471 int (*callback) (struct symtab *, void *),
3472 void *data)
3473 {
3474 int i;
3475 const char *name_basename = lbasename (name);
3476
3477 dw2_setup (objfile);
3478
3479 /* The rule is CUs specify all the files, including those used by
3480 any TU, so there's no need to scan TUs here. */
3481
3482 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3483 {
3484 int j;
3485 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3486 struct quick_file_names *file_data;
3487
3488 /* We only need to look at symtabs not already expanded. */
3489 if (per_cu->v.quick->compunit_symtab)
3490 continue;
3491
3492 file_data = dw2_get_file_names (per_cu);
3493 if (file_data == NULL)
3494 continue;
3495
3496 for (j = 0; j < file_data->num_file_names; ++j)
3497 {
3498 const char *this_name = file_data->file_names[j];
3499 const char *this_real_name;
3500
3501 if (compare_filenames_for_search (this_name, name))
3502 {
3503 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3504 callback, data))
3505 return 1;
3506 continue;
3507 }
3508
3509 /* Before we invoke realpath, which can get expensive when many
3510 files are involved, do a quick comparison of the basenames. */
3511 if (! basenames_may_differ
3512 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3513 continue;
3514
3515 this_real_name = dw2_get_real_path (objfile, file_data, j);
3516 if (compare_filenames_for_search (this_real_name, name))
3517 {
3518 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3519 callback, data))
3520 return 1;
3521 continue;
3522 }
3523
3524 if (real_path != NULL)
3525 {
3526 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3527 gdb_assert (IS_ABSOLUTE_PATH (name));
3528 if (this_real_name != NULL
3529 && FILENAME_CMP (real_path, this_real_name) == 0)
3530 {
3531 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3532 callback, data))
3533 return 1;
3534 continue;
3535 }
3536 }
3537 }
3538 }
3539
3540 return 0;
3541 }
3542
3543 /* Struct used to manage iterating over all CUs looking for a symbol. */
3544
3545 struct dw2_symtab_iterator
3546 {
3547 /* The internalized form of .gdb_index. */
3548 struct mapped_index *index;
3549 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3550 int want_specific_block;
3551 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3552 Unused if !WANT_SPECIFIC_BLOCK. */
3553 int block_index;
3554 /* The kind of symbol we're looking for. */
3555 domain_enum domain;
3556 /* The list of CUs from the index entry of the symbol,
3557 or NULL if not found. */
3558 offset_type *vec;
3559 /* The next element in VEC to look at. */
3560 int next;
3561 /* The number of elements in VEC, or zero if there is no match. */
3562 int length;
3563 /* Have we seen a global version of the symbol?
3564 If so we can ignore all further global instances.
3565 This is to work around gold/15646, inefficient gold-generated
3566 indices. */
3567 int global_seen;
3568 };
3569
3570 /* Initialize the index symtab iterator ITER.
3571 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3572 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3573
3574 static void
3575 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3576 struct mapped_index *index,
3577 int want_specific_block,
3578 int block_index,
3579 domain_enum domain,
3580 const char *name)
3581 {
3582 iter->index = index;
3583 iter->want_specific_block = want_specific_block;
3584 iter->block_index = block_index;
3585 iter->domain = domain;
3586 iter->next = 0;
3587 iter->global_seen = 0;
3588
3589 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3590 iter->length = MAYBE_SWAP (*iter->vec);
3591 else
3592 {
3593 iter->vec = NULL;
3594 iter->length = 0;
3595 }
3596 }
3597
3598 /* Return the next matching CU or NULL if there are no more. */
3599
3600 static struct dwarf2_per_cu_data *
3601 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3602 {
3603 for ( ; iter->next < iter->length; ++iter->next)
3604 {
3605 offset_type cu_index_and_attrs =
3606 MAYBE_SWAP (iter->vec[iter->next + 1]);
3607 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3608 struct dwarf2_per_cu_data *per_cu;
3609 int want_static = iter->block_index != GLOBAL_BLOCK;
3610 /* This value is only valid for index versions >= 7. */
3611 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3612 gdb_index_symbol_kind symbol_kind =
3613 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3614 /* Only check the symbol attributes if they're present.
3615 Indices prior to version 7 don't record them,
3616 and indices >= 7 may elide them for certain symbols
3617 (gold does this). */
3618 int attrs_valid =
3619 (iter->index->version >= 7
3620 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3621
3622 /* Don't crash on bad data. */
3623 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3624 + dwarf2_per_objfile->n_type_units))
3625 {
3626 complaint (&symfile_complaints,
3627 _(".gdb_index entry has bad CU index"
3628 " [in module %s]"),
3629 objfile_name (dwarf2_per_objfile->objfile));
3630 continue;
3631 }
3632
3633 per_cu = dw2_get_cutu (cu_index);
3634
3635 /* Skip if already read in. */
3636 if (per_cu->v.quick->compunit_symtab)
3637 continue;
3638
3639 /* Check static vs global. */
3640 if (attrs_valid)
3641 {
3642 if (iter->want_specific_block
3643 && want_static != is_static)
3644 continue;
3645 /* Work around gold/15646. */
3646 if (!is_static && iter->global_seen)
3647 continue;
3648 if (!is_static)
3649 iter->global_seen = 1;
3650 }
3651
3652 /* Only check the symbol's kind if it has one. */
3653 if (attrs_valid)
3654 {
3655 switch (iter->domain)
3656 {
3657 case VAR_DOMAIN:
3658 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3659 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3660 /* Some types are also in VAR_DOMAIN. */
3661 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3662 continue;
3663 break;
3664 case STRUCT_DOMAIN:
3665 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3666 continue;
3667 break;
3668 case LABEL_DOMAIN:
3669 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3670 continue;
3671 break;
3672 default:
3673 break;
3674 }
3675 }
3676
3677 ++iter->next;
3678 return per_cu;
3679 }
3680
3681 return NULL;
3682 }
3683
3684 static struct compunit_symtab *
3685 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3686 const char *name, domain_enum domain)
3687 {
3688 struct compunit_symtab *stab_best = NULL;
3689 struct mapped_index *index;
3690
3691 dw2_setup (objfile);
3692
3693 index = dwarf2_per_objfile->index_table;
3694
3695 /* index is NULL if OBJF_READNOW. */
3696 if (index)
3697 {
3698 struct dw2_symtab_iterator iter;
3699 struct dwarf2_per_cu_data *per_cu;
3700
3701 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3702
3703 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3704 {
3705 struct symbol *sym, *with_opaque = NULL;
3706 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3707 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3708 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3709
3710 sym = block_find_symbol (block, name, domain,
3711 block_find_non_opaque_type_preferred,
3712 &with_opaque);
3713
3714 /* Some caution must be observed with overloaded functions
3715 and methods, since the index will not contain any overload
3716 information (but NAME might contain it). */
3717
3718 if (sym != NULL
3719 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3720 return stab;
3721 if (with_opaque != NULL
3722 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3723 stab_best = stab;
3724
3725 /* Keep looking through other CUs. */
3726 }
3727 }
3728
3729 return stab_best;
3730 }
3731
3732 static void
3733 dw2_print_stats (struct objfile *objfile)
3734 {
3735 int i, total, count;
3736
3737 dw2_setup (objfile);
3738 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3739 count = 0;
3740 for (i = 0; i < total; ++i)
3741 {
3742 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3743
3744 if (!per_cu->v.quick->compunit_symtab)
3745 ++count;
3746 }
3747 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3748 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3749 }
3750
3751 /* This dumps minimal information about the index.
3752 It is called via "mt print objfiles".
3753 One use is to verify .gdb_index has been loaded by the
3754 gdb.dwarf2/gdb-index.exp testcase. */
3755
3756 static void
3757 dw2_dump (struct objfile *objfile)
3758 {
3759 dw2_setup (objfile);
3760 gdb_assert (dwarf2_per_objfile->using_index);
3761 printf_filtered (".gdb_index:");
3762 if (dwarf2_per_objfile->index_table != NULL)
3763 {
3764 printf_filtered (" version %d\n",
3765 dwarf2_per_objfile->index_table->version);
3766 }
3767 else
3768 printf_filtered (" faked for \"readnow\"\n");
3769 printf_filtered ("\n");
3770 }
3771
3772 static void
3773 dw2_relocate (struct objfile *objfile,
3774 const struct section_offsets *new_offsets,
3775 const struct section_offsets *delta)
3776 {
3777 /* There's nothing to relocate here. */
3778 }
3779
3780 static void
3781 dw2_expand_symtabs_for_function (struct objfile *objfile,
3782 const char *func_name)
3783 {
3784 struct mapped_index *index;
3785
3786 dw2_setup (objfile);
3787
3788 index = dwarf2_per_objfile->index_table;
3789
3790 /* index is NULL if OBJF_READNOW. */
3791 if (index)
3792 {
3793 struct dw2_symtab_iterator iter;
3794 struct dwarf2_per_cu_data *per_cu;
3795
3796 /* Note: It doesn't matter what we pass for block_index here. */
3797 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3798 func_name);
3799
3800 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3801 dw2_instantiate_symtab (per_cu);
3802 }
3803 }
3804
3805 static void
3806 dw2_expand_all_symtabs (struct objfile *objfile)
3807 {
3808 int i;
3809
3810 dw2_setup (objfile);
3811
3812 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3813 + dwarf2_per_objfile->n_type_units); ++i)
3814 {
3815 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3816
3817 dw2_instantiate_symtab (per_cu);
3818 }
3819 }
3820
3821 static void
3822 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3823 const char *fullname)
3824 {
3825 int i;
3826
3827 dw2_setup (objfile);
3828
3829 /* We don't need to consider type units here.
3830 This is only called for examining code, e.g. expand_line_sal.
3831 There can be an order of magnitude (or more) more type units
3832 than comp units, and we avoid them if we can. */
3833
3834 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3835 {
3836 int j;
3837 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3838 struct quick_file_names *file_data;
3839
3840 /* We only need to look at symtabs not already expanded. */
3841 if (per_cu->v.quick->compunit_symtab)
3842 continue;
3843
3844 file_data = dw2_get_file_names (per_cu);
3845 if (file_data == NULL)
3846 continue;
3847
3848 for (j = 0; j < file_data->num_file_names; ++j)
3849 {
3850 const char *this_fullname = file_data->file_names[j];
3851
3852 if (filename_cmp (this_fullname, fullname) == 0)
3853 {
3854 dw2_instantiate_symtab (per_cu);
3855 break;
3856 }
3857 }
3858 }
3859 }
3860
3861 static void
3862 dw2_map_matching_symbols (struct objfile *objfile,
3863 const char * name, domain_enum domain,
3864 int global,
3865 int (*callback) (struct block *,
3866 struct symbol *, void *),
3867 void *data, symbol_compare_ftype *match,
3868 symbol_compare_ftype *ordered_compare)
3869 {
3870 /* Currently unimplemented; used for Ada. The function can be called if the
3871 current language is Ada for a non-Ada objfile using GNU index. As Ada
3872 does not look for non-Ada symbols this function should just return. */
3873 }
3874
3875 static void
3876 dw2_expand_symtabs_matching
3877 (struct objfile *objfile,
3878 expand_symtabs_file_matcher_ftype *file_matcher,
3879 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
3880 expand_symtabs_exp_notify_ftype *expansion_notify,
3881 enum search_domain kind,
3882 void *data)
3883 {
3884 int i;
3885 offset_type iter;
3886 struct mapped_index *index;
3887
3888 dw2_setup (objfile);
3889
3890 /* index_table is NULL if OBJF_READNOW. */
3891 if (!dwarf2_per_objfile->index_table)
3892 return;
3893 index = dwarf2_per_objfile->index_table;
3894
3895 if (file_matcher != NULL)
3896 {
3897 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
3898 htab_eq_pointer,
3899 NULL, xcalloc, xfree));
3900 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
3901 htab_eq_pointer,
3902 NULL, xcalloc, xfree));
3903
3904 /* The rule is CUs specify all the files, including those used by
3905 any TU, so there's no need to scan TUs here. */
3906
3907 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3908 {
3909 int j;
3910 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3911 struct quick_file_names *file_data;
3912 void **slot;
3913
3914 QUIT;
3915
3916 per_cu->v.quick->mark = 0;
3917
3918 /* We only need to look at symtabs not already expanded. */
3919 if (per_cu->v.quick->compunit_symtab)
3920 continue;
3921
3922 file_data = dw2_get_file_names (per_cu);
3923 if (file_data == NULL)
3924 continue;
3925
3926 if (htab_find (visited_not_found.get (), file_data) != NULL)
3927 continue;
3928 else if (htab_find (visited_found.get (), file_data) != NULL)
3929 {
3930 per_cu->v.quick->mark = 1;
3931 continue;
3932 }
3933
3934 for (j = 0; j < file_data->num_file_names; ++j)
3935 {
3936 const char *this_real_name;
3937
3938 if (file_matcher (file_data->file_names[j], data, 0))
3939 {
3940 per_cu->v.quick->mark = 1;
3941 break;
3942 }
3943
3944 /* Before we invoke realpath, which can get expensive when many
3945 files are involved, do a quick comparison of the basenames. */
3946 if (!basenames_may_differ
3947 && !file_matcher (lbasename (file_data->file_names[j]),
3948 data, 1))
3949 continue;
3950
3951 this_real_name = dw2_get_real_path (objfile, file_data, j);
3952 if (file_matcher (this_real_name, data, 0))
3953 {
3954 per_cu->v.quick->mark = 1;
3955 break;
3956 }
3957 }
3958
3959 slot = htab_find_slot (per_cu->v.quick->mark
3960 ? visited_found.get ()
3961 : visited_not_found.get (),
3962 file_data, INSERT);
3963 *slot = file_data;
3964 }
3965 }
3966
3967 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3968 {
3969 offset_type idx = 2 * iter;
3970 const char *name;
3971 offset_type *vec, vec_len, vec_idx;
3972 int global_seen = 0;
3973
3974 QUIT;
3975
3976 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3977 continue;
3978
3979 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3980
3981 if (! (*symbol_matcher) (name, data))
3982 continue;
3983
3984 /* The name was matched, now expand corresponding CUs that were
3985 marked. */
3986 vec = (offset_type *) (index->constant_pool
3987 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3988 vec_len = MAYBE_SWAP (vec[0]);
3989 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3990 {
3991 struct dwarf2_per_cu_data *per_cu;
3992 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3993 /* This value is only valid for index versions >= 7. */
3994 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3995 gdb_index_symbol_kind symbol_kind =
3996 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3997 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3998 /* Only check the symbol attributes if they're present.
3999 Indices prior to version 7 don't record them,
4000 and indices >= 7 may elide them for certain symbols
4001 (gold does this). */
4002 int attrs_valid =
4003 (index->version >= 7
4004 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4005
4006 /* Work around gold/15646. */
4007 if (attrs_valid)
4008 {
4009 if (!is_static && global_seen)
4010 continue;
4011 if (!is_static)
4012 global_seen = 1;
4013 }
4014
4015 /* Only check the symbol's kind if it has one. */
4016 if (attrs_valid)
4017 {
4018 switch (kind)
4019 {
4020 case VARIABLES_DOMAIN:
4021 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4022 continue;
4023 break;
4024 case FUNCTIONS_DOMAIN:
4025 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4026 continue;
4027 break;
4028 case TYPES_DOMAIN:
4029 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4030 continue;
4031 break;
4032 default:
4033 break;
4034 }
4035 }
4036
4037 /* Don't crash on bad data. */
4038 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4039 + dwarf2_per_objfile->n_type_units))
4040 {
4041 complaint (&symfile_complaints,
4042 _(".gdb_index entry has bad CU index"
4043 " [in module %s]"), objfile_name (objfile));
4044 continue;
4045 }
4046
4047 per_cu = dw2_get_cutu (cu_index);
4048 if (file_matcher == NULL || per_cu->v.quick->mark)
4049 {
4050 int symtab_was_null =
4051 (per_cu->v.quick->compunit_symtab == NULL);
4052
4053 dw2_instantiate_symtab (per_cu);
4054
4055 if (expansion_notify != NULL
4056 && symtab_was_null
4057 && per_cu->v.quick->compunit_symtab != NULL)
4058 {
4059 expansion_notify (per_cu->v.quick->compunit_symtab,
4060 data);
4061 }
4062 }
4063 }
4064 }
4065 }
4066
4067 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4068 symtab. */
4069
4070 static struct compunit_symtab *
4071 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4072 CORE_ADDR pc)
4073 {
4074 int i;
4075
4076 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4077 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4078 return cust;
4079
4080 if (cust->includes == NULL)
4081 return NULL;
4082
4083 for (i = 0; cust->includes[i]; ++i)
4084 {
4085 struct compunit_symtab *s = cust->includes[i];
4086
4087 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4088 if (s != NULL)
4089 return s;
4090 }
4091
4092 return NULL;
4093 }
4094
4095 static struct compunit_symtab *
4096 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4097 struct bound_minimal_symbol msymbol,
4098 CORE_ADDR pc,
4099 struct obj_section *section,
4100 int warn_if_readin)
4101 {
4102 struct dwarf2_per_cu_data *data;
4103 struct compunit_symtab *result;
4104
4105 dw2_setup (objfile);
4106
4107 if (!objfile->psymtabs_addrmap)
4108 return NULL;
4109
4110 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4111 pc);
4112 if (!data)
4113 return NULL;
4114
4115 if (warn_if_readin && data->v.quick->compunit_symtab)
4116 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4117 paddress (get_objfile_arch (objfile), pc));
4118
4119 result
4120 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4121 pc);
4122 gdb_assert (result != NULL);
4123 return result;
4124 }
4125
4126 static void
4127 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4128 void *data, int need_fullname)
4129 {
4130 int i;
4131 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4132 NULL, xcalloc, xfree));
4133
4134 dw2_setup (objfile);
4135
4136 /* The rule is CUs specify all the files, including those used by
4137 any TU, so there's no need to scan TUs here.
4138 We can ignore file names coming from already-expanded CUs. */
4139
4140 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4141 {
4142 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4143
4144 if (per_cu->v.quick->compunit_symtab)
4145 {
4146 void **slot = htab_find_slot (visited.get (),
4147 per_cu->v.quick->file_names,
4148 INSERT);
4149
4150 *slot = per_cu->v.quick->file_names;
4151 }
4152 }
4153
4154 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4155 {
4156 int j;
4157 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4158 struct quick_file_names *file_data;
4159 void **slot;
4160
4161 /* We only need to look at symtabs not already expanded. */
4162 if (per_cu->v.quick->compunit_symtab)
4163 continue;
4164
4165 file_data = dw2_get_file_names (per_cu);
4166 if (file_data == NULL)
4167 continue;
4168
4169 slot = htab_find_slot (visited.get (), file_data, INSERT);
4170 if (*slot)
4171 {
4172 /* Already visited. */
4173 continue;
4174 }
4175 *slot = file_data;
4176
4177 for (j = 0; j < file_data->num_file_names; ++j)
4178 {
4179 const char *this_real_name;
4180
4181 if (need_fullname)
4182 this_real_name = dw2_get_real_path (objfile, file_data, j);
4183 else
4184 this_real_name = NULL;
4185 (*fun) (file_data->file_names[j], this_real_name, data);
4186 }
4187 }
4188 }
4189
4190 static int
4191 dw2_has_symbols (struct objfile *objfile)
4192 {
4193 return 1;
4194 }
4195
4196 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4197 {
4198 dw2_has_symbols,
4199 dw2_find_last_source_symtab,
4200 dw2_forget_cached_source_info,
4201 dw2_map_symtabs_matching_filename,
4202 dw2_lookup_symbol,
4203 dw2_print_stats,
4204 dw2_dump,
4205 dw2_relocate,
4206 dw2_expand_symtabs_for_function,
4207 dw2_expand_all_symtabs,
4208 dw2_expand_symtabs_with_fullname,
4209 dw2_map_matching_symbols,
4210 dw2_expand_symtabs_matching,
4211 dw2_find_pc_sect_compunit_symtab,
4212 dw2_map_symbol_filenames
4213 };
4214
4215 /* Initialize for reading DWARF for this objfile. Return 0 if this
4216 file will use psymtabs, or 1 if using the GNU index. */
4217
4218 int
4219 dwarf2_initialize_objfile (struct objfile *objfile)
4220 {
4221 /* If we're about to read full symbols, don't bother with the
4222 indices. In this case we also don't care if some other debug
4223 format is making psymtabs, because they are all about to be
4224 expanded anyway. */
4225 if ((objfile->flags & OBJF_READNOW))
4226 {
4227 int i;
4228
4229 dwarf2_per_objfile->using_index = 1;
4230 create_all_comp_units (objfile);
4231 create_all_type_units (objfile);
4232 dwarf2_per_objfile->quick_file_names_table =
4233 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4234
4235 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4236 + dwarf2_per_objfile->n_type_units); ++i)
4237 {
4238 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4239
4240 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4241 struct dwarf2_per_cu_quick_data);
4242 }
4243
4244 /* Return 1 so that gdb sees the "quick" functions. However,
4245 these functions will be no-ops because we will have expanded
4246 all symtabs. */
4247 return 1;
4248 }
4249
4250 if (dwarf2_read_index (objfile))
4251 return 1;
4252
4253 return 0;
4254 }
4255
4256 \f
4257
4258 /* Build a partial symbol table. */
4259
4260 void
4261 dwarf2_build_psymtabs (struct objfile *objfile)
4262 {
4263
4264 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4265 {
4266 init_psymbol_list (objfile, 1024);
4267 }
4268
4269 TRY
4270 {
4271 /* This isn't really ideal: all the data we allocate on the
4272 objfile's obstack is still uselessly kept around. However,
4273 freeing it seems unsafe. */
4274 psymtab_discarder psymtabs (objfile);
4275 dwarf2_build_psymtabs_hard (objfile);
4276 psymtabs.keep ();
4277 }
4278 CATCH (except, RETURN_MASK_ERROR)
4279 {
4280 exception_print (gdb_stderr, except);
4281 }
4282 END_CATCH
4283 }
4284
4285 /* Return the total length of the CU described by HEADER. */
4286
4287 static unsigned int
4288 get_cu_length (const struct comp_unit_head *header)
4289 {
4290 return header->initial_length_size + header->length;
4291 }
4292
4293 /* Return TRUE if OFFSET is within CU_HEADER. */
4294
4295 static inline int
4296 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
4297 {
4298 sect_offset bottom = { cu_header->offset.sect_off };
4299 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
4300
4301 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
4302 }
4303
4304 /* Find the base address of the compilation unit for range lists and
4305 location lists. It will normally be specified by DW_AT_low_pc.
4306 In DWARF-3 draft 4, the base address could be overridden by
4307 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4308 compilation units with discontinuous ranges. */
4309
4310 static void
4311 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4312 {
4313 struct attribute *attr;
4314
4315 cu->base_known = 0;
4316 cu->base_address = 0;
4317
4318 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4319 if (attr)
4320 {
4321 cu->base_address = attr_value_as_address (attr);
4322 cu->base_known = 1;
4323 }
4324 else
4325 {
4326 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4327 if (attr)
4328 {
4329 cu->base_address = attr_value_as_address (attr);
4330 cu->base_known = 1;
4331 }
4332 }
4333 }
4334
4335 /* Read in the comp unit header information from the debug_info at info_ptr.
4336 NOTE: This leaves members offset, first_die_offset to be filled in
4337 by the caller. */
4338
4339 static const gdb_byte *
4340 read_comp_unit_head (struct comp_unit_head *cu_header,
4341 const gdb_byte *info_ptr, bfd *abfd)
4342 {
4343 int signed_addr;
4344 unsigned int bytes_read;
4345
4346 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4347 cu_header->initial_length_size = bytes_read;
4348 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4349 info_ptr += bytes_read;
4350 cu_header->version = read_2_bytes (abfd, info_ptr);
4351 info_ptr += 2;
4352 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4353 &bytes_read);
4354 info_ptr += bytes_read;
4355 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4356 info_ptr += 1;
4357 signed_addr = bfd_get_sign_extend_vma (abfd);
4358 if (signed_addr < 0)
4359 internal_error (__FILE__, __LINE__,
4360 _("read_comp_unit_head: dwarf from non elf file"));
4361 cu_header->signed_addr_p = signed_addr;
4362
4363 return info_ptr;
4364 }
4365
4366 /* Helper function that returns the proper abbrev section for
4367 THIS_CU. */
4368
4369 static struct dwarf2_section_info *
4370 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4371 {
4372 struct dwarf2_section_info *abbrev;
4373
4374 if (this_cu->is_dwz)
4375 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4376 else
4377 abbrev = &dwarf2_per_objfile->abbrev;
4378
4379 return abbrev;
4380 }
4381
4382 /* Subroutine of read_and_check_comp_unit_head and
4383 read_and_check_type_unit_head to simplify them.
4384 Perform various error checking on the header. */
4385
4386 static void
4387 error_check_comp_unit_head (struct comp_unit_head *header,
4388 struct dwarf2_section_info *section,
4389 struct dwarf2_section_info *abbrev_section)
4390 {
4391 const char *filename = get_section_file_name (section);
4392
4393 if (header->version != 2 && header->version != 3 && header->version != 4)
4394 error (_("Dwarf Error: wrong version in compilation unit header "
4395 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4396 filename);
4397
4398 if (header->abbrev_offset.sect_off
4399 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4400 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4401 "(offset 0x%lx + 6) [in module %s]"),
4402 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
4403 filename);
4404
4405 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4406 avoid potential 32-bit overflow. */
4407 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4408 > section->size)
4409 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4410 "(offset 0x%lx + 0) [in module %s]"),
4411 (long) header->length, (long) header->offset.sect_off,
4412 filename);
4413 }
4414
4415 /* Read in a CU/TU header and perform some basic error checking.
4416 The contents of the header are stored in HEADER.
4417 The result is a pointer to the start of the first DIE. */
4418
4419 static const gdb_byte *
4420 read_and_check_comp_unit_head (struct comp_unit_head *header,
4421 struct dwarf2_section_info *section,
4422 struct dwarf2_section_info *abbrev_section,
4423 const gdb_byte *info_ptr,
4424 int is_debug_types_section)
4425 {
4426 const gdb_byte *beg_of_comp_unit = info_ptr;
4427 bfd *abfd = get_section_bfd_owner (section);
4428
4429 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4430
4431 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4432
4433 /* If we're reading a type unit, skip over the signature and
4434 type_offset fields. */
4435 if (is_debug_types_section)
4436 info_ptr += 8 /*signature*/ + header->offset_size;
4437
4438 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4439
4440 error_check_comp_unit_head (header, section, abbrev_section);
4441
4442 return info_ptr;
4443 }
4444
4445 /* Read in the types comp unit header information from .debug_types entry at
4446 types_ptr. The result is a pointer to one past the end of the header. */
4447
4448 static const gdb_byte *
4449 read_and_check_type_unit_head (struct comp_unit_head *header,
4450 struct dwarf2_section_info *section,
4451 struct dwarf2_section_info *abbrev_section,
4452 const gdb_byte *info_ptr,
4453 ULONGEST *signature,
4454 cu_offset *type_offset_in_tu)
4455 {
4456 const gdb_byte *beg_of_comp_unit = info_ptr;
4457 bfd *abfd = get_section_bfd_owner (section);
4458
4459 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4460
4461 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4462
4463 /* If we're reading a type unit, skip over the signature and
4464 type_offset fields. */
4465 if (signature != NULL)
4466 *signature = read_8_bytes (abfd, info_ptr);
4467 info_ptr += 8;
4468 if (type_offset_in_tu != NULL)
4469 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4470 header->offset_size);
4471 info_ptr += header->offset_size;
4472
4473 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4474
4475 error_check_comp_unit_head (header, section, abbrev_section);
4476
4477 return info_ptr;
4478 }
4479
4480 /* Fetch the abbreviation table offset from a comp or type unit header. */
4481
4482 static sect_offset
4483 read_abbrev_offset (struct dwarf2_section_info *section,
4484 sect_offset offset)
4485 {
4486 bfd *abfd = get_section_bfd_owner (section);
4487 const gdb_byte *info_ptr;
4488 unsigned int initial_length_size, offset_size;
4489 sect_offset abbrev_offset;
4490
4491 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4492 info_ptr = section->buffer + offset.sect_off;
4493 read_initial_length (abfd, info_ptr, &initial_length_size);
4494 offset_size = initial_length_size == 4 ? 4 : 8;
4495 info_ptr += initial_length_size + 2 /*version*/;
4496 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4497 return abbrev_offset;
4498 }
4499
4500 /* Allocate a new partial symtab for file named NAME and mark this new
4501 partial symtab as being an include of PST. */
4502
4503 static void
4504 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4505 struct objfile *objfile)
4506 {
4507 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4508
4509 if (!IS_ABSOLUTE_PATH (subpst->filename))
4510 {
4511 /* It shares objfile->objfile_obstack. */
4512 subpst->dirname = pst->dirname;
4513 }
4514
4515 subpst->textlow = 0;
4516 subpst->texthigh = 0;
4517
4518 subpst->dependencies
4519 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4520 subpst->dependencies[0] = pst;
4521 subpst->number_of_dependencies = 1;
4522
4523 subpst->globals_offset = 0;
4524 subpst->n_global_syms = 0;
4525 subpst->statics_offset = 0;
4526 subpst->n_static_syms = 0;
4527 subpst->compunit_symtab = NULL;
4528 subpst->read_symtab = pst->read_symtab;
4529 subpst->readin = 0;
4530
4531 /* No private part is necessary for include psymtabs. This property
4532 can be used to differentiate between such include psymtabs and
4533 the regular ones. */
4534 subpst->read_symtab_private = NULL;
4535 }
4536
4537 /* Read the Line Number Program data and extract the list of files
4538 included by the source file represented by PST. Build an include
4539 partial symtab for each of these included files. */
4540
4541 static void
4542 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4543 struct die_info *die,
4544 struct partial_symtab *pst)
4545 {
4546 struct line_header *lh = NULL;
4547 struct attribute *attr;
4548
4549 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4550 if (attr)
4551 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4552 if (lh == NULL)
4553 return; /* No linetable, so no includes. */
4554
4555 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4556 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
4557
4558 free_line_header (lh);
4559 }
4560
4561 static hashval_t
4562 hash_signatured_type (const void *item)
4563 {
4564 const struct signatured_type *sig_type
4565 = (const struct signatured_type *) item;
4566
4567 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4568 return sig_type->signature;
4569 }
4570
4571 static int
4572 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4573 {
4574 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4575 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4576
4577 return lhs->signature == rhs->signature;
4578 }
4579
4580 /* Allocate a hash table for signatured types. */
4581
4582 static htab_t
4583 allocate_signatured_type_table (struct objfile *objfile)
4584 {
4585 return htab_create_alloc_ex (41,
4586 hash_signatured_type,
4587 eq_signatured_type,
4588 NULL,
4589 &objfile->objfile_obstack,
4590 hashtab_obstack_allocate,
4591 dummy_obstack_deallocate);
4592 }
4593
4594 /* A helper function to add a signatured type CU to a table. */
4595
4596 static int
4597 add_signatured_type_cu_to_table (void **slot, void *datum)
4598 {
4599 struct signatured_type *sigt = (struct signatured_type *) *slot;
4600 struct signatured_type ***datap = (struct signatured_type ***) datum;
4601
4602 **datap = sigt;
4603 ++*datap;
4604
4605 return 1;
4606 }
4607
4608 /* Create the hash table of all entries in the .debug_types
4609 (or .debug_types.dwo) section(s).
4610 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4611 otherwise it is NULL.
4612
4613 The result is a pointer to the hash table or NULL if there are no types.
4614
4615 Note: This function processes DWO files only, not DWP files. */
4616
4617 static htab_t
4618 create_debug_types_hash_table (struct dwo_file *dwo_file,
4619 VEC (dwarf2_section_info_def) *types)
4620 {
4621 struct objfile *objfile = dwarf2_per_objfile->objfile;
4622 htab_t types_htab = NULL;
4623 int ix;
4624 struct dwarf2_section_info *section;
4625 struct dwarf2_section_info *abbrev_section;
4626
4627 if (VEC_empty (dwarf2_section_info_def, types))
4628 return NULL;
4629
4630 abbrev_section = (dwo_file != NULL
4631 ? &dwo_file->sections.abbrev
4632 : &dwarf2_per_objfile->abbrev);
4633
4634 if (dwarf_read_debug)
4635 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4636 dwo_file ? ".dwo" : "",
4637 get_section_file_name (abbrev_section));
4638
4639 for (ix = 0;
4640 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4641 ++ix)
4642 {
4643 bfd *abfd;
4644 const gdb_byte *info_ptr, *end_ptr;
4645
4646 dwarf2_read_section (objfile, section);
4647 info_ptr = section->buffer;
4648
4649 if (info_ptr == NULL)
4650 continue;
4651
4652 /* We can't set abfd until now because the section may be empty or
4653 not present, in which case the bfd is unknown. */
4654 abfd = get_section_bfd_owner (section);
4655
4656 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4657 because we don't need to read any dies: the signature is in the
4658 header. */
4659
4660 end_ptr = info_ptr + section->size;
4661 while (info_ptr < end_ptr)
4662 {
4663 sect_offset offset;
4664 cu_offset type_offset_in_tu;
4665 ULONGEST signature;
4666 struct signatured_type *sig_type;
4667 struct dwo_unit *dwo_tu;
4668 void **slot;
4669 const gdb_byte *ptr = info_ptr;
4670 struct comp_unit_head header;
4671 unsigned int length;
4672
4673 offset.sect_off = ptr - section->buffer;
4674
4675 /* We need to read the type's signature in order to build the hash
4676 table, but we don't need anything else just yet. */
4677
4678 ptr = read_and_check_type_unit_head (&header, section,
4679 abbrev_section, ptr,
4680 &signature, &type_offset_in_tu);
4681
4682 length = get_cu_length (&header);
4683
4684 /* Skip dummy type units. */
4685 if (ptr >= info_ptr + length
4686 || peek_abbrev_code (abfd, ptr) == 0)
4687 {
4688 info_ptr += length;
4689 continue;
4690 }
4691
4692 if (types_htab == NULL)
4693 {
4694 if (dwo_file)
4695 types_htab = allocate_dwo_unit_table (objfile);
4696 else
4697 types_htab = allocate_signatured_type_table (objfile);
4698 }
4699
4700 if (dwo_file)
4701 {
4702 sig_type = NULL;
4703 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4704 struct dwo_unit);
4705 dwo_tu->dwo_file = dwo_file;
4706 dwo_tu->signature = signature;
4707 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4708 dwo_tu->section = section;
4709 dwo_tu->offset = offset;
4710 dwo_tu->length = length;
4711 }
4712 else
4713 {
4714 /* N.B.: type_offset is not usable if this type uses a DWO file.
4715 The real type_offset is in the DWO file. */
4716 dwo_tu = NULL;
4717 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4718 struct signatured_type);
4719 sig_type->signature = signature;
4720 sig_type->type_offset_in_tu = type_offset_in_tu;
4721 sig_type->per_cu.objfile = objfile;
4722 sig_type->per_cu.is_debug_types = 1;
4723 sig_type->per_cu.section = section;
4724 sig_type->per_cu.offset = offset;
4725 sig_type->per_cu.length = length;
4726 }
4727
4728 slot = htab_find_slot (types_htab,
4729 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4730 INSERT);
4731 gdb_assert (slot != NULL);
4732 if (*slot != NULL)
4733 {
4734 sect_offset dup_offset;
4735
4736 if (dwo_file)
4737 {
4738 const struct dwo_unit *dup_tu
4739 = (const struct dwo_unit *) *slot;
4740
4741 dup_offset = dup_tu->offset;
4742 }
4743 else
4744 {
4745 const struct signatured_type *dup_tu
4746 = (const struct signatured_type *) *slot;
4747
4748 dup_offset = dup_tu->per_cu.offset;
4749 }
4750
4751 complaint (&symfile_complaints,
4752 _("debug type entry at offset 0x%x is duplicate to"
4753 " the entry at offset 0x%x, signature %s"),
4754 offset.sect_off, dup_offset.sect_off,
4755 hex_string (signature));
4756 }
4757 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4758
4759 if (dwarf_read_debug > 1)
4760 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4761 offset.sect_off,
4762 hex_string (signature));
4763
4764 info_ptr += length;
4765 }
4766 }
4767
4768 return types_htab;
4769 }
4770
4771 /* Create the hash table of all entries in the .debug_types section,
4772 and initialize all_type_units.
4773 The result is zero if there is an error (e.g. missing .debug_types section),
4774 otherwise non-zero. */
4775
4776 static int
4777 create_all_type_units (struct objfile *objfile)
4778 {
4779 htab_t types_htab;
4780 struct signatured_type **iter;
4781
4782 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4783 if (types_htab == NULL)
4784 {
4785 dwarf2_per_objfile->signatured_types = NULL;
4786 return 0;
4787 }
4788
4789 dwarf2_per_objfile->signatured_types = types_htab;
4790
4791 dwarf2_per_objfile->n_type_units
4792 = dwarf2_per_objfile->n_allocated_type_units
4793 = htab_elements (types_htab);
4794 dwarf2_per_objfile->all_type_units =
4795 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
4796 iter = &dwarf2_per_objfile->all_type_units[0];
4797 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4798 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4799 == dwarf2_per_objfile->n_type_units);
4800
4801 return 1;
4802 }
4803
4804 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4805 If SLOT is non-NULL, it is the entry to use in the hash table.
4806 Otherwise we find one. */
4807
4808 static struct signatured_type *
4809 add_type_unit (ULONGEST sig, void **slot)
4810 {
4811 struct objfile *objfile = dwarf2_per_objfile->objfile;
4812 int n_type_units = dwarf2_per_objfile->n_type_units;
4813 struct signatured_type *sig_type;
4814
4815 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4816 ++n_type_units;
4817 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4818 {
4819 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4820 dwarf2_per_objfile->n_allocated_type_units = 1;
4821 dwarf2_per_objfile->n_allocated_type_units *= 2;
4822 dwarf2_per_objfile->all_type_units
4823 = XRESIZEVEC (struct signatured_type *,
4824 dwarf2_per_objfile->all_type_units,
4825 dwarf2_per_objfile->n_allocated_type_units);
4826 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4827 }
4828 dwarf2_per_objfile->n_type_units = n_type_units;
4829
4830 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4831 struct signatured_type);
4832 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4833 sig_type->signature = sig;
4834 sig_type->per_cu.is_debug_types = 1;
4835 if (dwarf2_per_objfile->using_index)
4836 {
4837 sig_type->per_cu.v.quick =
4838 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4839 struct dwarf2_per_cu_quick_data);
4840 }
4841
4842 if (slot == NULL)
4843 {
4844 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4845 sig_type, INSERT);
4846 }
4847 gdb_assert (*slot == NULL);
4848 *slot = sig_type;
4849 /* The rest of sig_type must be filled in by the caller. */
4850 return sig_type;
4851 }
4852
4853 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4854 Fill in SIG_ENTRY with DWO_ENTRY. */
4855
4856 static void
4857 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4858 struct signatured_type *sig_entry,
4859 struct dwo_unit *dwo_entry)
4860 {
4861 /* Make sure we're not clobbering something we don't expect to. */
4862 gdb_assert (! sig_entry->per_cu.queued);
4863 gdb_assert (sig_entry->per_cu.cu == NULL);
4864 if (dwarf2_per_objfile->using_index)
4865 {
4866 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4867 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
4868 }
4869 else
4870 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
4871 gdb_assert (sig_entry->signature == dwo_entry->signature);
4872 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4873 gdb_assert (sig_entry->type_unit_group == NULL);
4874 gdb_assert (sig_entry->dwo_unit == NULL);
4875
4876 sig_entry->per_cu.section = dwo_entry->section;
4877 sig_entry->per_cu.offset = dwo_entry->offset;
4878 sig_entry->per_cu.length = dwo_entry->length;
4879 sig_entry->per_cu.reading_dwo_directly = 1;
4880 sig_entry->per_cu.objfile = objfile;
4881 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4882 sig_entry->dwo_unit = dwo_entry;
4883 }
4884
4885 /* Subroutine of lookup_signatured_type.
4886 If we haven't read the TU yet, create the signatured_type data structure
4887 for a TU to be read in directly from a DWO file, bypassing the stub.
4888 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4889 using .gdb_index, then when reading a CU we want to stay in the DWO file
4890 containing that CU. Otherwise we could end up reading several other DWO
4891 files (due to comdat folding) to process the transitive closure of all the
4892 mentioned TUs, and that can be slow. The current DWO file will have every
4893 type signature that it needs.
4894 We only do this for .gdb_index because in the psymtab case we already have
4895 to read all the DWOs to build the type unit groups. */
4896
4897 static struct signatured_type *
4898 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4899 {
4900 struct objfile *objfile = dwarf2_per_objfile->objfile;
4901 struct dwo_file *dwo_file;
4902 struct dwo_unit find_dwo_entry, *dwo_entry;
4903 struct signatured_type find_sig_entry, *sig_entry;
4904 void **slot;
4905
4906 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4907
4908 /* If TU skeletons have been removed then we may not have read in any
4909 TUs yet. */
4910 if (dwarf2_per_objfile->signatured_types == NULL)
4911 {
4912 dwarf2_per_objfile->signatured_types
4913 = allocate_signatured_type_table (objfile);
4914 }
4915
4916 /* We only ever need to read in one copy of a signatured type.
4917 Use the global signatured_types array to do our own comdat-folding
4918 of types. If this is the first time we're reading this TU, and
4919 the TU has an entry in .gdb_index, replace the recorded data from
4920 .gdb_index with this TU. */
4921
4922 find_sig_entry.signature = sig;
4923 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4924 &find_sig_entry, INSERT);
4925 sig_entry = (struct signatured_type *) *slot;
4926
4927 /* We can get here with the TU already read, *or* in the process of being
4928 read. Don't reassign the global entry to point to this DWO if that's
4929 the case. Also note that if the TU is already being read, it may not
4930 have come from a DWO, the program may be a mix of Fission-compiled
4931 code and non-Fission-compiled code. */
4932
4933 /* Have we already tried to read this TU?
4934 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
4935 needn't exist in the global table yet). */
4936 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
4937 return sig_entry;
4938
4939 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4940 dwo_unit of the TU itself. */
4941 dwo_file = cu->dwo_unit->dwo_file;
4942
4943 /* Ok, this is the first time we're reading this TU. */
4944 if (dwo_file->tus == NULL)
4945 return NULL;
4946 find_dwo_entry.signature = sig;
4947 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
4948 if (dwo_entry == NULL)
4949 return NULL;
4950
4951 /* If the global table doesn't have an entry for this TU, add one. */
4952 if (sig_entry == NULL)
4953 sig_entry = add_type_unit (sig, slot);
4954
4955 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4956 sig_entry->per_cu.tu_read = 1;
4957 return sig_entry;
4958 }
4959
4960 /* Subroutine of lookup_signatured_type.
4961 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4962 then try the DWP file. If the TU stub (skeleton) has been removed then
4963 it won't be in .gdb_index. */
4964
4965 static struct signatured_type *
4966 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4967 {
4968 struct objfile *objfile = dwarf2_per_objfile->objfile;
4969 struct dwp_file *dwp_file = get_dwp_file ();
4970 struct dwo_unit *dwo_entry;
4971 struct signatured_type find_sig_entry, *sig_entry;
4972 void **slot;
4973
4974 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4975 gdb_assert (dwp_file != NULL);
4976
4977 /* If TU skeletons have been removed then we may not have read in any
4978 TUs yet. */
4979 if (dwarf2_per_objfile->signatured_types == NULL)
4980 {
4981 dwarf2_per_objfile->signatured_types
4982 = allocate_signatured_type_table (objfile);
4983 }
4984
4985 find_sig_entry.signature = sig;
4986 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4987 &find_sig_entry, INSERT);
4988 sig_entry = (struct signatured_type *) *slot;
4989
4990 /* Have we already tried to read this TU?
4991 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
4992 needn't exist in the global table yet). */
4993 if (sig_entry != NULL)
4994 return sig_entry;
4995
4996 if (dwp_file->tus == NULL)
4997 return NULL;
4998 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
4999 sig, 1 /* is_debug_types */);
5000 if (dwo_entry == NULL)
5001 return NULL;
5002
5003 sig_entry = add_type_unit (sig, slot);
5004 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5005
5006 return sig_entry;
5007 }
5008
5009 /* Lookup a signature based type for DW_FORM_ref_sig8.
5010 Returns NULL if signature SIG is not present in the table.
5011 It is up to the caller to complain about this. */
5012
5013 static struct signatured_type *
5014 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5015 {
5016 if (cu->dwo_unit
5017 && dwarf2_per_objfile->using_index)
5018 {
5019 /* We're in a DWO/DWP file, and we're using .gdb_index.
5020 These cases require special processing. */
5021 if (get_dwp_file () == NULL)
5022 return lookup_dwo_signatured_type (cu, sig);
5023 else
5024 return lookup_dwp_signatured_type (cu, sig);
5025 }
5026 else
5027 {
5028 struct signatured_type find_entry, *entry;
5029
5030 if (dwarf2_per_objfile->signatured_types == NULL)
5031 return NULL;
5032 find_entry.signature = sig;
5033 entry = ((struct signatured_type *)
5034 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5035 return entry;
5036 }
5037 }
5038 \f
5039 /* Low level DIE reading support. */
5040
5041 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5042
5043 static void
5044 init_cu_die_reader (struct die_reader_specs *reader,
5045 struct dwarf2_cu *cu,
5046 struct dwarf2_section_info *section,
5047 struct dwo_file *dwo_file)
5048 {
5049 gdb_assert (section->readin && section->buffer != NULL);
5050 reader->abfd = get_section_bfd_owner (section);
5051 reader->cu = cu;
5052 reader->dwo_file = dwo_file;
5053 reader->die_section = section;
5054 reader->buffer = section->buffer;
5055 reader->buffer_end = section->buffer + section->size;
5056 reader->comp_dir = NULL;
5057 }
5058
5059 /* Subroutine of init_cutu_and_read_dies to simplify it.
5060 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5061 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5062 already.
5063
5064 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5065 from it to the DIE in the DWO. If NULL we are skipping the stub.
5066 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5067 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5068 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5069 STUB_COMP_DIR may be non-NULL.
5070 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5071 are filled in with the info of the DIE from the DWO file.
5072 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5073 provided an abbrev table to use.
5074 The result is non-zero if a valid (non-dummy) DIE was found. */
5075
5076 static int
5077 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5078 struct dwo_unit *dwo_unit,
5079 int abbrev_table_provided,
5080 struct die_info *stub_comp_unit_die,
5081 const char *stub_comp_dir,
5082 struct die_reader_specs *result_reader,
5083 const gdb_byte **result_info_ptr,
5084 struct die_info **result_comp_unit_die,
5085 int *result_has_children)
5086 {
5087 struct objfile *objfile = dwarf2_per_objfile->objfile;
5088 struct dwarf2_cu *cu = this_cu->cu;
5089 struct dwarf2_section_info *section;
5090 bfd *abfd;
5091 const gdb_byte *begin_info_ptr, *info_ptr;
5092 ULONGEST signature; /* Or dwo_id. */
5093 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5094 int i,num_extra_attrs;
5095 struct dwarf2_section_info *dwo_abbrev_section;
5096 struct attribute *attr;
5097 struct die_info *comp_unit_die;
5098
5099 /* At most one of these may be provided. */
5100 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5101
5102 /* These attributes aren't processed until later:
5103 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5104 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5105 referenced later. However, these attributes are found in the stub
5106 which we won't have later. In order to not impose this complication
5107 on the rest of the code, we read them here and copy them to the
5108 DWO CU/TU die. */
5109
5110 stmt_list = NULL;
5111 low_pc = NULL;
5112 high_pc = NULL;
5113 ranges = NULL;
5114 comp_dir = NULL;
5115
5116 if (stub_comp_unit_die != NULL)
5117 {
5118 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5119 DWO file. */
5120 if (! this_cu->is_debug_types)
5121 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5122 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5123 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5124 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5125 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5126
5127 /* There should be a DW_AT_addr_base attribute here (if needed).
5128 We need the value before we can process DW_FORM_GNU_addr_index. */
5129 cu->addr_base = 0;
5130 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5131 if (attr)
5132 cu->addr_base = DW_UNSND (attr);
5133
5134 /* There should be a DW_AT_ranges_base attribute here (if needed).
5135 We need the value before we can process DW_AT_ranges. */
5136 cu->ranges_base = 0;
5137 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5138 if (attr)
5139 cu->ranges_base = DW_UNSND (attr);
5140 }
5141 else if (stub_comp_dir != NULL)
5142 {
5143 /* Reconstruct the comp_dir attribute to simplify the code below. */
5144 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5145 comp_dir->name = DW_AT_comp_dir;
5146 comp_dir->form = DW_FORM_string;
5147 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5148 DW_STRING (comp_dir) = stub_comp_dir;
5149 }
5150
5151 /* Set up for reading the DWO CU/TU. */
5152 cu->dwo_unit = dwo_unit;
5153 section = dwo_unit->section;
5154 dwarf2_read_section (objfile, section);
5155 abfd = get_section_bfd_owner (section);
5156 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5157 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5158 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5159
5160 if (this_cu->is_debug_types)
5161 {
5162 ULONGEST header_signature;
5163 cu_offset type_offset_in_tu;
5164 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5165
5166 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5167 dwo_abbrev_section,
5168 info_ptr,
5169 &header_signature,
5170 &type_offset_in_tu);
5171 /* This is not an assert because it can be caused by bad debug info. */
5172 if (sig_type->signature != header_signature)
5173 {
5174 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5175 " TU at offset 0x%x [in module %s]"),
5176 hex_string (sig_type->signature),
5177 hex_string (header_signature),
5178 dwo_unit->offset.sect_off,
5179 bfd_get_filename (abfd));
5180 }
5181 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5182 /* For DWOs coming from DWP files, we don't know the CU length
5183 nor the type's offset in the TU until now. */
5184 dwo_unit->length = get_cu_length (&cu->header);
5185 dwo_unit->type_offset_in_tu = type_offset_in_tu;
5186
5187 /* Establish the type offset that can be used to lookup the type.
5188 For DWO files, we don't know it until now. */
5189 sig_type->type_offset_in_section.sect_off =
5190 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5191 }
5192 else
5193 {
5194 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5195 dwo_abbrev_section,
5196 info_ptr, 0);
5197 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5198 /* For DWOs coming from DWP files, we don't know the CU length
5199 until now. */
5200 dwo_unit->length = get_cu_length (&cu->header);
5201 }
5202
5203 /* Replace the CU's original abbrev table with the DWO's.
5204 Reminder: We can't read the abbrev table until we've read the header. */
5205 if (abbrev_table_provided)
5206 {
5207 /* Don't free the provided abbrev table, the caller of
5208 init_cutu_and_read_dies owns it. */
5209 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5210 /* Ensure the DWO abbrev table gets freed. */
5211 make_cleanup (dwarf2_free_abbrev_table, cu);
5212 }
5213 else
5214 {
5215 dwarf2_free_abbrev_table (cu);
5216 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5217 /* Leave any existing abbrev table cleanup as is. */
5218 }
5219
5220 /* Read in the die, but leave space to copy over the attributes
5221 from the stub. This has the benefit of simplifying the rest of
5222 the code - all the work to maintain the illusion of a single
5223 DW_TAG_{compile,type}_unit DIE is done here. */
5224 num_extra_attrs = ((stmt_list != NULL)
5225 + (low_pc != NULL)
5226 + (high_pc != NULL)
5227 + (ranges != NULL)
5228 + (comp_dir != NULL));
5229 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5230 result_has_children, num_extra_attrs);
5231
5232 /* Copy over the attributes from the stub to the DIE we just read in. */
5233 comp_unit_die = *result_comp_unit_die;
5234 i = comp_unit_die->num_attrs;
5235 if (stmt_list != NULL)
5236 comp_unit_die->attrs[i++] = *stmt_list;
5237 if (low_pc != NULL)
5238 comp_unit_die->attrs[i++] = *low_pc;
5239 if (high_pc != NULL)
5240 comp_unit_die->attrs[i++] = *high_pc;
5241 if (ranges != NULL)
5242 comp_unit_die->attrs[i++] = *ranges;
5243 if (comp_dir != NULL)
5244 comp_unit_die->attrs[i++] = *comp_dir;
5245 comp_unit_die->num_attrs += num_extra_attrs;
5246
5247 if (dwarf_die_debug)
5248 {
5249 fprintf_unfiltered (gdb_stdlog,
5250 "Read die from %s@0x%x of %s:\n",
5251 get_section_name (section),
5252 (unsigned) (begin_info_ptr - section->buffer),
5253 bfd_get_filename (abfd));
5254 dump_die (comp_unit_die, dwarf_die_debug);
5255 }
5256
5257 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5258 TUs by skipping the stub and going directly to the entry in the DWO file.
5259 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5260 to get it via circuitous means. Blech. */
5261 if (comp_dir != NULL)
5262 result_reader->comp_dir = DW_STRING (comp_dir);
5263
5264 /* Skip dummy compilation units. */
5265 if (info_ptr >= begin_info_ptr + dwo_unit->length
5266 || peek_abbrev_code (abfd, info_ptr) == 0)
5267 return 0;
5268
5269 *result_info_ptr = info_ptr;
5270 return 1;
5271 }
5272
5273 /* Subroutine of init_cutu_and_read_dies to simplify it.
5274 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5275 Returns NULL if the specified DWO unit cannot be found. */
5276
5277 static struct dwo_unit *
5278 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5279 struct die_info *comp_unit_die)
5280 {
5281 struct dwarf2_cu *cu = this_cu->cu;
5282 struct attribute *attr;
5283 ULONGEST signature;
5284 struct dwo_unit *dwo_unit;
5285 const char *comp_dir, *dwo_name;
5286
5287 gdb_assert (cu != NULL);
5288
5289 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5290 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5291 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5292
5293 if (this_cu->is_debug_types)
5294 {
5295 struct signatured_type *sig_type;
5296
5297 /* Since this_cu is the first member of struct signatured_type,
5298 we can go from a pointer to one to a pointer to the other. */
5299 sig_type = (struct signatured_type *) this_cu;
5300 signature = sig_type->signature;
5301 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5302 }
5303 else
5304 {
5305 struct attribute *attr;
5306
5307 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5308 if (! attr)
5309 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5310 " [in module %s]"),
5311 dwo_name, objfile_name (this_cu->objfile));
5312 signature = DW_UNSND (attr);
5313 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5314 signature);
5315 }
5316
5317 return dwo_unit;
5318 }
5319
5320 /* Subroutine of init_cutu_and_read_dies to simplify it.
5321 See it for a description of the parameters.
5322 Read a TU directly from a DWO file, bypassing the stub.
5323
5324 Note: This function could be a little bit simpler if we shared cleanups
5325 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5326 to do, so we keep this function self-contained. Or we could move this
5327 into our caller, but it's complex enough already. */
5328
5329 static void
5330 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5331 int use_existing_cu, int keep,
5332 die_reader_func_ftype *die_reader_func,
5333 void *data)
5334 {
5335 struct dwarf2_cu *cu;
5336 struct signatured_type *sig_type;
5337 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5338 struct die_reader_specs reader;
5339 const gdb_byte *info_ptr;
5340 struct die_info *comp_unit_die;
5341 int has_children;
5342
5343 /* Verify we can do the following downcast, and that we have the
5344 data we need. */
5345 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5346 sig_type = (struct signatured_type *) this_cu;
5347 gdb_assert (sig_type->dwo_unit != NULL);
5348
5349 cleanups = make_cleanup (null_cleanup, NULL);
5350
5351 if (use_existing_cu && this_cu->cu != NULL)
5352 {
5353 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5354 cu = this_cu->cu;
5355 /* There's no need to do the rereading_dwo_cu handling that
5356 init_cutu_and_read_dies does since we don't read the stub. */
5357 }
5358 else
5359 {
5360 /* If !use_existing_cu, this_cu->cu must be NULL. */
5361 gdb_assert (this_cu->cu == NULL);
5362 cu = XNEW (struct dwarf2_cu);
5363 init_one_comp_unit (cu, this_cu);
5364 /* If an error occurs while loading, release our storage. */
5365 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5366 }
5367
5368 /* A future optimization, if needed, would be to use an existing
5369 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5370 could share abbrev tables. */
5371
5372 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5373 0 /* abbrev_table_provided */,
5374 NULL /* stub_comp_unit_die */,
5375 sig_type->dwo_unit->dwo_file->comp_dir,
5376 &reader, &info_ptr,
5377 &comp_unit_die, &has_children) == 0)
5378 {
5379 /* Dummy die. */
5380 do_cleanups (cleanups);
5381 return;
5382 }
5383
5384 /* All the "real" work is done here. */
5385 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5386
5387 /* This duplicates the code in init_cutu_and_read_dies,
5388 but the alternative is making the latter more complex.
5389 This function is only for the special case of using DWO files directly:
5390 no point in overly complicating the general case just to handle this. */
5391 if (free_cu_cleanup != NULL)
5392 {
5393 if (keep)
5394 {
5395 /* We've successfully allocated this compilation unit. Let our
5396 caller clean it up when finished with it. */
5397 discard_cleanups (free_cu_cleanup);
5398
5399 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5400 So we have to manually free the abbrev table. */
5401 dwarf2_free_abbrev_table (cu);
5402
5403 /* Link this CU into read_in_chain. */
5404 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5405 dwarf2_per_objfile->read_in_chain = this_cu;
5406 }
5407 else
5408 do_cleanups (free_cu_cleanup);
5409 }
5410
5411 do_cleanups (cleanups);
5412 }
5413
5414 /* Initialize a CU (or TU) and read its DIEs.
5415 If the CU defers to a DWO file, read the DWO file as well.
5416
5417 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5418 Otherwise the table specified in the comp unit header is read in and used.
5419 This is an optimization for when we already have the abbrev table.
5420
5421 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5422 Otherwise, a new CU is allocated with xmalloc.
5423
5424 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5425 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5426
5427 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5428 linker) then DIE_READER_FUNC will not get called. */
5429
5430 static void
5431 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5432 struct abbrev_table *abbrev_table,
5433 int use_existing_cu, int keep,
5434 die_reader_func_ftype *die_reader_func,
5435 void *data)
5436 {
5437 struct objfile *objfile = dwarf2_per_objfile->objfile;
5438 struct dwarf2_section_info *section = this_cu->section;
5439 bfd *abfd = get_section_bfd_owner (section);
5440 struct dwarf2_cu *cu;
5441 const gdb_byte *begin_info_ptr, *info_ptr;
5442 struct die_reader_specs reader;
5443 struct die_info *comp_unit_die;
5444 int has_children;
5445 struct attribute *attr;
5446 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5447 struct signatured_type *sig_type = NULL;
5448 struct dwarf2_section_info *abbrev_section;
5449 /* Non-zero if CU currently points to a DWO file and we need to
5450 reread it. When this happens we need to reread the skeleton die
5451 before we can reread the DWO file (this only applies to CUs, not TUs). */
5452 int rereading_dwo_cu = 0;
5453
5454 if (dwarf_die_debug)
5455 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5456 this_cu->is_debug_types ? "type" : "comp",
5457 this_cu->offset.sect_off);
5458
5459 if (use_existing_cu)
5460 gdb_assert (keep);
5461
5462 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5463 file (instead of going through the stub), short-circuit all of this. */
5464 if (this_cu->reading_dwo_directly)
5465 {
5466 /* Narrow down the scope of possibilities to have to understand. */
5467 gdb_assert (this_cu->is_debug_types);
5468 gdb_assert (abbrev_table == NULL);
5469 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5470 die_reader_func, data);
5471 return;
5472 }
5473
5474 cleanups = make_cleanup (null_cleanup, NULL);
5475
5476 /* This is cheap if the section is already read in. */
5477 dwarf2_read_section (objfile, section);
5478
5479 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5480
5481 abbrev_section = get_abbrev_section_for_cu (this_cu);
5482
5483 if (use_existing_cu && this_cu->cu != NULL)
5484 {
5485 cu = this_cu->cu;
5486 /* If this CU is from a DWO file we need to start over, we need to
5487 refetch the attributes from the skeleton CU.
5488 This could be optimized by retrieving those attributes from when we
5489 were here the first time: the previous comp_unit_die was stored in
5490 comp_unit_obstack. But there's no data yet that we need this
5491 optimization. */
5492 if (cu->dwo_unit != NULL)
5493 rereading_dwo_cu = 1;
5494 }
5495 else
5496 {
5497 /* If !use_existing_cu, this_cu->cu must be NULL. */
5498 gdb_assert (this_cu->cu == NULL);
5499 cu = XNEW (struct dwarf2_cu);
5500 init_one_comp_unit (cu, this_cu);
5501 /* If an error occurs while loading, release our storage. */
5502 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5503 }
5504
5505 /* Get the header. */
5506 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5507 {
5508 /* We already have the header, there's no need to read it in again. */
5509 info_ptr += cu->header.first_die_offset.cu_off;
5510 }
5511 else
5512 {
5513 if (this_cu->is_debug_types)
5514 {
5515 ULONGEST signature;
5516 cu_offset type_offset_in_tu;
5517
5518 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5519 abbrev_section, info_ptr,
5520 &signature,
5521 &type_offset_in_tu);
5522
5523 /* Since per_cu is the first member of struct signatured_type,
5524 we can go from a pointer to one to a pointer to the other. */
5525 sig_type = (struct signatured_type *) this_cu;
5526 gdb_assert (sig_type->signature == signature);
5527 gdb_assert (sig_type->type_offset_in_tu.cu_off
5528 == type_offset_in_tu.cu_off);
5529 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5530
5531 /* LENGTH has not been set yet for type units if we're
5532 using .gdb_index. */
5533 this_cu->length = get_cu_length (&cu->header);
5534
5535 /* Establish the type offset that can be used to lookup the type. */
5536 sig_type->type_offset_in_section.sect_off =
5537 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5538 }
5539 else
5540 {
5541 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5542 abbrev_section,
5543 info_ptr, 0);
5544
5545 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5546 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5547 }
5548 }
5549
5550 /* Skip dummy compilation units. */
5551 if (info_ptr >= begin_info_ptr + this_cu->length
5552 || peek_abbrev_code (abfd, info_ptr) == 0)
5553 {
5554 do_cleanups (cleanups);
5555 return;
5556 }
5557
5558 /* If we don't have them yet, read the abbrevs for this compilation unit.
5559 And if we need to read them now, make sure they're freed when we're
5560 done. Note that it's important that if the CU had an abbrev table
5561 on entry we don't free it when we're done: Somewhere up the call stack
5562 it may be in use. */
5563 if (abbrev_table != NULL)
5564 {
5565 gdb_assert (cu->abbrev_table == NULL);
5566 gdb_assert (cu->header.abbrev_offset.sect_off
5567 == abbrev_table->offset.sect_off);
5568 cu->abbrev_table = abbrev_table;
5569 }
5570 else if (cu->abbrev_table == NULL)
5571 {
5572 dwarf2_read_abbrevs (cu, abbrev_section);
5573 make_cleanup (dwarf2_free_abbrev_table, cu);
5574 }
5575 else if (rereading_dwo_cu)
5576 {
5577 dwarf2_free_abbrev_table (cu);
5578 dwarf2_read_abbrevs (cu, abbrev_section);
5579 }
5580
5581 /* Read the top level CU/TU die. */
5582 init_cu_die_reader (&reader, cu, section, NULL);
5583 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5584
5585 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5586 from the DWO file.
5587 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5588 DWO CU, that this test will fail (the attribute will not be present). */
5589 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5590 if (attr)
5591 {
5592 struct dwo_unit *dwo_unit;
5593 struct die_info *dwo_comp_unit_die;
5594
5595 if (has_children)
5596 {
5597 complaint (&symfile_complaints,
5598 _("compilation unit with DW_AT_GNU_dwo_name"
5599 " has children (offset 0x%x) [in module %s]"),
5600 this_cu->offset.sect_off, bfd_get_filename (abfd));
5601 }
5602 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5603 if (dwo_unit != NULL)
5604 {
5605 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5606 abbrev_table != NULL,
5607 comp_unit_die, NULL,
5608 &reader, &info_ptr,
5609 &dwo_comp_unit_die, &has_children) == 0)
5610 {
5611 /* Dummy die. */
5612 do_cleanups (cleanups);
5613 return;
5614 }
5615 comp_unit_die = dwo_comp_unit_die;
5616 }
5617 else
5618 {
5619 /* Yikes, we couldn't find the rest of the DIE, we only have
5620 the stub. A complaint has already been logged. There's
5621 not much more we can do except pass on the stub DIE to
5622 die_reader_func. We don't want to throw an error on bad
5623 debug info. */
5624 }
5625 }
5626
5627 /* All of the above is setup for this call. Yikes. */
5628 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5629
5630 /* Done, clean up. */
5631 if (free_cu_cleanup != NULL)
5632 {
5633 if (keep)
5634 {
5635 /* We've successfully allocated this compilation unit. Let our
5636 caller clean it up when finished with it. */
5637 discard_cleanups (free_cu_cleanup);
5638
5639 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5640 So we have to manually free the abbrev table. */
5641 dwarf2_free_abbrev_table (cu);
5642
5643 /* Link this CU into read_in_chain. */
5644 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5645 dwarf2_per_objfile->read_in_chain = this_cu;
5646 }
5647 else
5648 do_cleanups (free_cu_cleanup);
5649 }
5650
5651 do_cleanups (cleanups);
5652 }
5653
5654 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5655 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5656 to have already done the lookup to find the DWO file).
5657
5658 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5659 THIS_CU->is_debug_types, but nothing else.
5660
5661 We fill in THIS_CU->length.
5662
5663 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5664 linker) then DIE_READER_FUNC will not get called.
5665
5666 THIS_CU->cu is always freed when done.
5667 This is done in order to not leave THIS_CU->cu in a state where we have
5668 to care whether it refers to the "main" CU or the DWO CU. */
5669
5670 static void
5671 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5672 struct dwo_file *dwo_file,
5673 die_reader_func_ftype *die_reader_func,
5674 void *data)
5675 {
5676 struct objfile *objfile = dwarf2_per_objfile->objfile;
5677 struct dwarf2_section_info *section = this_cu->section;
5678 bfd *abfd = get_section_bfd_owner (section);
5679 struct dwarf2_section_info *abbrev_section;
5680 struct dwarf2_cu cu;
5681 const gdb_byte *begin_info_ptr, *info_ptr;
5682 struct die_reader_specs reader;
5683 struct cleanup *cleanups;
5684 struct die_info *comp_unit_die;
5685 int has_children;
5686
5687 if (dwarf_die_debug)
5688 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5689 this_cu->is_debug_types ? "type" : "comp",
5690 this_cu->offset.sect_off);
5691
5692 gdb_assert (this_cu->cu == NULL);
5693
5694 abbrev_section = (dwo_file != NULL
5695 ? &dwo_file->sections.abbrev
5696 : get_abbrev_section_for_cu (this_cu));
5697
5698 /* This is cheap if the section is already read in. */
5699 dwarf2_read_section (objfile, section);
5700
5701 init_one_comp_unit (&cu, this_cu);
5702
5703 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5704
5705 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5706 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5707 abbrev_section, info_ptr,
5708 this_cu->is_debug_types);
5709
5710 this_cu->length = get_cu_length (&cu.header);
5711
5712 /* Skip dummy compilation units. */
5713 if (info_ptr >= begin_info_ptr + this_cu->length
5714 || peek_abbrev_code (abfd, info_ptr) == 0)
5715 {
5716 do_cleanups (cleanups);
5717 return;
5718 }
5719
5720 dwarf2_read_abbrevs (&cu, abbrev_section);
5721 make_cleanup (dwarf2_free_abbrev_table, &cu);
5722
5723 init_cu_die_reader (&reader, &cu, section, dwo_file);
5724 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5725
5726 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5727
5728 do_cleanups (cleanups);
5729 }
5730
5731 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5732 does not lookup the specified DWO file.
5733 This cannot be used to read DWO files.
5734
5735 THIS_CU->cu is always freed when done.
5736 This is done in order to not leave THIS_CU->cu in a state where we have
5737 to care whether it refers to the "main" CU or the DWO CU.
5738 We can revisit this if the data shows there's a performance issue. */
5739
5740 static void
5741 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5742 die_reader_func_ftype *die_reader_func,
5743 void *data)
5744 {
5745 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5746 }
5747 \f
5748 /* Type Unit Groups.
5749
5750 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5751 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5752 so that all types coming from the same compilation (.o file) are grouped
5753 together. A future step could be to put the types in the same symtab as
5754 the CU the types ultimately came from. */
5755
5756 static hashval_t
5757 hash_type_unit_group (const void *item)
5758 {
5759 const struct type_unit_group *tu_group
5760 = (const struct type_unit_group *) item;
5761
5762 return hash_stmt_list_entry (&tu_group->hash);
5763 }
5764
5765 static int
5766 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5767 {
5768 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5769 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
5770
5771 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5772 }
5773
5774 /* Allocate a hash table for type unit groups. */
5775
5776 static htab_t
5777 allocate_type_unit_groups_table (void)
5778 {
5779 return htab_create_alloc_ex (3,
5780 hash_type_unit_group,
5781 eq_type_unit_group,
5782 NULL,
5783 &dwarf2_per_objfile->objfile->objfile_obstack,
5784 hashtab_obstack_allocate,
5785 dummy_obstack_deallocate);
5786 }
5787
5788 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5789 partial symtabs. We combine several TUs per psymtab to not let the size
5790 of any one psymtab grow too big. */
5791 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5792 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5793
5794 /* Helper routine for get_type_unit_group.
5795 Create the type_unit_group object used to hold one or more TUs. */
5796
5797 static struct type_unit_group *
5798 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5799 {
5800 struct objfile *objfile = dwarf2_per_objfile->objfile;
5801 struct dwarf2_per_cu_data *per_cu;
5802 struct type_unit_group *tu_group;
5803
5804 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5805 struct type_unit_group);
5806 per_cu = &tu_group->per_cu;
5807 per_cu->objfile = objfile;
5808
5809 if (dwarf2_per_objfile->using_index)
5810 {
5811 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5812 struct dwarf2_per_cu_quick_data);
5813 }
5814 else
5815 {
5816 unsigned int line_offset = line_offset_struct.sect_off;
5817 struct partial_symtab *pst;
5818 char *name;
5819
5820 /* Give the symtab a useful name for debug purposes. */
5821 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5822 name = xstrprintf ("<type_units_%d>",
5823 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5824 else
5825 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5826
5827 pst = create_partial_symtab (per_cu, name);
5828 pst->anonymous = 1;
5829
5830 xfree (name);
5831 }
5832
5833 tu_group->hash.dwo_unit = cu->dwo_unit;
5834 tu_group->hash.line_offset = line_offset_struct;
5835
5836 return tu_group;
5837 }
5838
5839 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5840 STMT_LIST is a DW_AT_stmt_list attribute. */
5841
5842 static struct type_unit_group *
5843 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5844 {
5845 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5846 struct type_unit_group *tu_group;
5847 void **slot;
5848 unsigned int line_offset;
5849 struct type_unit_group type_unit_group_for_lookup;
5850
5851 if (dwarf2_per_objfile->type_unit_groups == NULL)
5852 {
5853 dwarf2_per_objfile->type_unit_groups =
5854 allocate_type_unit_groups_table ();
5855 }
5856
5857 /* Do we need to create a new group, or can we use an existing one? */
5858
5859 if (stmt_list)
5860 {
5861 line_offset = DW_UNSND (stmt_list);
5862 ++tu_stats->nr_symtab_sharers;
5863 }
5864 else
5865 {
5866 /* Ugh, no stmt_list. Rare, but we have to handle it.
5867 We can do various things here like create one group per TU or
5868 spread them over multiple groups to split up the expansion work.
5869 To avoid worst case scenarios (too many groups or too large groups)
5870 we, umm, group them in bunches. */
5871 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5872 | (tu_stats->nr_stmt_less_type_units
5873 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5874 ++tu_stats->nr_stmt_less_type_units;
5875 }
5876
5877 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5878 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5879 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5880 &type_unit_group_for_lookup, INSERT);
5881 if (*slot != NULL)
5882 {
5883 tu_group = (struct type_unit_group *) *slot;
5884 gdb_assert (tu_group != NULL);
5885 }
5886 else
5887 {
5888 sect_offset line_offset_struct;
5889
5890 line_offset_struct.sect_off = line_offset;
5891 tu_group = create_type_unit_group (cu, line_offset_struct);
5892 *slot = tu_group;
5893 ++tu_stats->nr_symtabs;
5894 }
5895
5896 return tu_group;
5897 }
5898 \f
5899 /* Partial symbol tables. */
5900
5901 /* Create a psymtab named NAME and assign it to PER_CU.
5902
5903 The caller must fill in the following details:
5904 dirname, textlow, texthigh. */
5905
5906 static struct partial_symtab *
5907 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5908 {
5909 struct objfile *objfile = per_cu->objfile;
5910 struct partial_symtab *pst;
5911
5912 pst = start_psymtab_common (objfile, name, 0,
5913 objfile->global_psymbols.next,
5914 objfile->static_psymbols.next);
5915
5916 pst->psymtabs_addrmap_supported = 1;
5917
5918 /* This is the glue that links PST into GDB's symbol API. */
5919 pst->read_symtab_private = per_cu;
5920 pst->read_symtab = dwarf2_read_symtab;
5921 per_cu->v.psymtab = pst;
5922
5923 return pst;
5924 }
5925
5926 /* The DATA object passed to process_psymtab_comp_unit_reader has this
5927 type. */
5928
5929 struct process_psymtab_comp_unit_data
5930 {
5931 /* True if we are reading a DW_TAG_partial_unit. */
5932
5933 int want_partial_unit;
5934
5935 /* The "pretend" language that is used if the CU doesn't declare a
5936 language. */
5937
5938 enum language pretend_language;
5939 };
5940
5941 /* die_reader_func for process_psymtab_comp_unit. */
5942
5943 static void
5944 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5945 const gdb_byte *info_ptr,
5946 struct die_info *comp_unit_die,
5947 int has_children,
5948 void *data)
5949 {
5950 struct dwarf2_cu *cu = reader->cu;
5951 struct objfile *objfile = cu->objfile;
5952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5953 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5954 CORE_ADDR baseaddr;
5955 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5956 struct partial_symtab *pst;
5957 enum pc_bounds_kind cu_bounds_kind;
5958 const char *filename;
5959 struct process_psymtab_comp_unit_data *info
5960 = (struct process_psymtab_comp_unit_data *) data;
5961
5962 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
5963 return;
5964
5965 gdb_assert (! per_cu->is_debug_types);
5966
5967 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
5968
5969 cu->list_in_scope = &file_symbols;
5970
5971 /* Allocate a new partial symbol table structure. */
5972 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
5973 if (filename == NULL)
5974 filename = "";
5975
5976 pst = create_partial_symtab (per_cu, filename);
5977
5978 /* This must be done before calling dwarf2_build_include_psymtabs. */
5979 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5980
5981 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5982
5983 dwarf2_find_base_address (comp_unit_die, cu);
5984
5985 /* Possibly set the default values of LOWPC and HIGHPC from
5986 `DW_AT_ranges'. */
5987 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5988 &best_highpc, cu, pst);
5989 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
5990 /* Store the contiguous range if it is not empty; it can be empty for
5991 CUs with no code. */
5992 addrmap_set_empty (objfile->psymtabs_addrmap,
5993 gdbarch_adjust_dwarf2_addr (gdbarch,
5994 best_lowpc + baseaddr),
5995 gdbarch_adjust_dwarf2_addr (gdbarch,
5996 best_highpc + baseaddr) - 1,
5997 pst);
5998
5999 /* Check if comp unit has_children.
6000 If so, read the rest of the partial symbols from this comp unit.
6001 If not, there's no more debug_info for this comp unit. */
6002 if (has_children)
6003 {
6004 struct partial_die_info *first_die;
6005 CORE_ADDR lowpc, highpc;
6006
6007 lowpc = ((CORE_ADDR) -1);
6008 highpc = ((CORE_ADDR) 0);
6009
6010 first_die = load_partial_dies (reader, info_ptr, 1);
6011
6012 scan_partial_symbols (first_die, &lowpc, &highpc,
6013 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6014
6015 /* If we didn't find a lowpc, set it to highpc to avoid
6016 complaints from `maint check'. */
6017 if (lowpc == ((CORE_ADDR) -1))
6018 lowpc = highpc;
6019
6020 /* If the compilation unit didn't have an explicit address range,
6021 then use the information extracted from its child dies. */
6022 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6023 {
6024 best_lowpc = lowpc;
6025 best_highpc = highpc;
6026 }
6027 }
6028 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6029 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6030
6031 end_psymtab_common (objfile, pst);
6032
6033 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6034 {
6035 int i;
6036 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6037 struct dwarf2_per_cu_data *iter;
6038
6039 /* Fill in 'dependencies' here; we fill in 'users' in a
6040 post-pass. */
6041 pst->number_of_dependencies = len;
6042 pst->dependencies =
6043 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6044 for (i = 0;
6045 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6046 i, iter);
6047 ++i)
6048 pst->dependencies[i] = iter->v.psymtab;
6049
6050 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6051 }
6052
6053 /* Get the list of files included in the current compilation unit,
6054 and build a psymtab for each of them. */
6055 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6056
6057 if (dwarf_read_debug)
6058 {
6059 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6060
6061 fprintf_unfiltered (gdb_stdlog,
6062 "Psymtab for %s unit @0x%x: %s - %s"
6063 ", %d global, %d static syms\n",
6064 per_cu->is_debug_types ? "type" : "comp",
6065 per_cu->offset.sect_off,
6066 paddress (gdbarch, pst->textlow),
6067 paddress (gdbarch, pst->texthigh),
6068 pst->n_global_syms, pst->n_static_syms);
6069 }
6070 }
6071
6072 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6073 Process compilation unit THIS_CU for a psymtab. */
6074
6075 static void
6076 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6077 int want_partial_unit,
6078 enum language pretend_language)
6079 {
6080 struct process_psymtab_comp_unit_data info;
6081
6082 /* If this compilation unit was already read in, free the
6083 cached copy in order to read it in again. This is
6084 necessary because we skipped some symbols when we first
6085 read in the compilation unit (see load_partial_dies).
6086 This problem could be avoided, but the benefit is unclear. */
6087 if (this_cu->cu != NULL)
6088 free_one_cached_comp_unit (this_cu);
6089
6090 gdb_assert (! this_cu->is_debug_types);
6091 info.want_partial_unit = want_partial_unit;
6092 info.pretend_language = pretend_language;
6093 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6094 process_psymtab_comp_unit_reader,
6095 &info);
6096
6097 /* Age out any secondary CUs. */
6098 age_cached_comp_units ();
6099 }
6100
6101 /* Reader function for build_type_psymtabs. */
6102
6103 static void
6104 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6105 const gdb_byte *info_ptr,
6106 struct die_info *type_unit_die,
6107 int has_children,
6108 void *data)
6109 {
6110 struct objfile *objfile = dwarf2_per_objfile->objfile;
6111 struct dwarf2_cu *cu = reader->cu;
6112 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6113 struct signatured_type *sig_type;
6114 struct type_unit_group *tu_group;
6115 struct attribute *attr;
6116 struct partial_die_info *first_die;
6117 CORE_ADDR lowpc, highpc;
6118 struct partial_symtab *pst;
6119
6120 gdb_assert (data == NULL);
6121 gdb_assert (per_cu->is_debug_types);
6122 sig_type = (struct signatured_type *) per_cu;
6123
6124 if (! has_children)
6125 return;
6126
6127 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6128 tu_group = get_type_unit_group (cu, attr);
6129
6130 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6131
6132 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6133 cu->list_in_scope = &file_symbols;
6134 pst = create_partial_symtab (per_cu, "");
6135 pst->anonymous = 1;
6136
6137 first_die = load_partial_dies (reader, info_ptr, 1);
6138
6139 lowpc = (CORE_ADDR) -1;
6140 highpc = (CORE_ADDR) 0;
6141 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6142
6143 end_psymtab_common (objfile, pst);
6144 }
6145
6146 /* Struct used to sort TUs by their abbreviation table offset. */
6147
6148 struct tu_abbrev_offset
6149 {
6150 struct signatured_type *sig_type;
6151 sect_offset abbrev_offset;
6152 };
6153
6154 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6155
6156 static int
6157 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6158 {
6159 const struct tu_abbrev_offset * const *a
6160 = (const struct tu_abbrev_offset * const*) ap;
6161 const struct tu_abbrev_offset * const *b
6162 = (const struct tu_abbrev_offset * const*) bp;
6163 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6164 unsigned int boff = (*b)->abbrev_offset.sect_off;
6165
6166 return (aoff > boff) - (aoff < boff);
6167 }
6168
6169 /* Efficiently read all the type units.
6170 This does the bulk of the work for build_type_psymtabs.
6171
6172 The efficiency is because we sort TUs by the abbrev table they use and
6173 only read each abbrev table once. In one program there are 200K TUs
6174 sharing 8K abbrev tables.
6175
6176 The main purpose of this function is to support building the
6177 dwarf2_per_objfile->type_unit_groups table.
6178 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6179 can collapse the search space by grouping them by stmt_list.
6180 The savings can be significant, in the same program from above the 200K TUs
6181 share 8K stmt_list tables.
6182
6183 FUNC is expected to call get_type_unit_group, which will create the
6184 struct type_unit_group if necessary and add it to
6185 dwarf2_per_objfile->type_unit_groups. */
6186
6187 static void
6188 build_type_psymtabs_1 (void)
6189 {
6190 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6191 struct cleanup *cleanups;
6192 struct abbrev_table *abbrev_table;
6193 sect_offset abbrev_offset;
6194 struct tu_abbrev_offset *sorted_by_abbrev;
6195 int i;
6196
6197 /* It's up to the caller to not call us multiple times. */
6198 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6199
6200 if (dwarf2_per_objfile->n_type_units == 0)
6201 return;
6202
6203 /* TUs typically share abbrev tables, and there can be way more TUs than
6204 abbrev tables. Sort by abbrev table to reduce the number of times we
6205 read each abbrev table in.
6206 Alternatives are to punt or to maintain a cache of abbrev tables.
6207 This is simpler and efficient enough for now.
6208
6209 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6210 symtab to use). Typically TUs with the same abbrev offset have the same
6211 stmt_list value too so in practice this should work well.
6212
6213 The basic algorithm here is:
6214
6215 sort TUs by abbrev table
6216 for each TU with same abbrev table:
6217 read abbrev table if first user
6218 read TU top level DIE
6219 [IWBN if DWO skeletons had DW_AT_stmt_list]
6220 call FUNC */
6221
6222 if (dwarf_read_debug)
6223 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6224
6225 /* Sort in a separate table to maintain the order of all_type_units
6226 for .gdb_index: TU indices directly index all_type_units. */
6227 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6228 dwarf2_per_objfile->n_type_units);
6229 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6230 {
6231 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6232
6233 sorted_by_abbrev[i].sig_type = sig_type;
6234 sorted_by_abbrev[i].abbrev_offset =
6235 read_abbrev_offset (sig_type->per_cu.section,
6236 sig_type->per_cu.offset);
6237 }
6238 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6239 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6240 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6241
6242 abbrev_offset.sect_off = ~(unsigned) 0;
6243 abbrev_table = NULL;
6244 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6245
6246 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6247 {
6248 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6249
6250 /* Switch to the next abbrev table if necessary. */
6251 if (abbrev_table == NULL
6252 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6253 {
6254 if (abbrev_table != NULL)
6255 {
6256 abbrev_table_free (abbrev_table);
6257 /* Reset to NULL in case abbrev_table_read_table throws
6258 an error: abbrev_table_free_cleanup will get called. */
6259 abbrev_table = NULL;
6260 }
6261 abbrev_offset = tu->abbrev_offset;
6262 abbrev_table =
6263 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6264 abbrev_offset);
6265 ++tu_stats->nr_uniq_abbrev_tables;
6266 }
6267
6268 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6269 build_type_psymtabs_reader, NULL);
6270 }
6271
6272 do_cleanups (cleanups);
6273 }
6274
6275 /* Print collected type unit statistics. */
6276
6277 static void
6278 print_tu_stats (void)
6279 {
6280 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6281
6282 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6283 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6284 dwarf2_per_objfile->n_type_units);
6285 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6286 tu_stats->nr_uniq_abbrev_tables);
6287 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6288 tu_stats->nr_symtabs);
6289 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6290 tu_stats->nr_symtab_sharers);
6291 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6292 tu_stats->nr_stmt_less_type_units);
6293 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6294 tu_stats->nr_all_type_units_reallocs);
6295 }
6296
6297 /* Traversal function for build_type_psymtabs. */
6298
6299 static int
6300 build_type_psymtab_dependencies (void **slot, void *info)
6301 {
6302 struct objfile *objfile = dwarf2_per_objfile->objfile;
6303 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6304 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6305 struct partial_symtab *pst = per_cu->v.psymtab;
6306 int len = VEC_length (sig_type_ptr, tu_group->tus);
6307 struct signatured_type *iter;
6308 int i;
6309
6310 gdb_assert (len > 0);
6311 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6312
6313 pst->number_of_dependencies = len;
6314 pst->dependencies =
6315 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6316 for (i = 0;
6317 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6318 ++i)
6319 {
6320 gdb_assert (iter->per_cu.is_debug_types);
6321 pst->dependencies[i] = iter->per_cu.v.psymtab;
6322 iter->type_unit_group = tu_group;
6323 }
6324
6325 VEC_free (sig_type_ptr, tu_group->tus);
6326
6327 return 1;
6328 }
6329
6330 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6331 Build partial symbol tables for the .debug_types comp-units. */
6332
6333 static void
6334 build_type_psymtabs (struct objfile *objfile)
6335 {
6336 if (! create_all_type_units (objfile))
6337 return;
6338
6339 build_type_psymtabs_1 ();
6340 }
6341
6342 /* Traversal function for process_skeletonless_type_unit.
6343 Read a TU in a DWO file and build partial symbols for it. */
6344
6345 static int
6346 process_skeletonless_type_unit (void **slot, void *info)
6347 {
6348 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6349 struct objfile *objfile = (struct objfile *) info;
6350 struct signatured_type find_entry, *entry;
6351
6352 /* If this TU doesn't exist in the global table, add it and read it in. */
6353
6354 if (dwarf2_per_objfile->signatured_types == NULL)
6355 {
6356 dwarf2_per_objfile->signatured_types
6357 = allocate_signatured_type_table (objfile);
6358 }
6359
6360 find_entry.signature = dwo_unit->signature;
6361 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6362 INSERT);
6363 /* If we've already seen this type there's nothing to do. What's happening
6364 is we're doing our own version of comdat-folding here. */
6365 if (*slot != NULL)
6366 return 1;
6367
6368 /* This does the job that create_all_type_units would have done for
6369 this TU. */
6370 entry = add_type_unit (dwo_unit->signature, slot);
6371 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6372 *slot = entry;
6373
6374 /* This does the job that build_type_psymtabs_1 would have done. */
6375 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6376 build_type_psymtabs_reader, NULL);
6377
6378 return 1;
6379 }
6380
6381 /* Traversal function for process_skeletonless_type_units. */
6382
6383 static int
6384 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6385 {
6386 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6387
6388 if (dwo_file->tus != NULL)
6389 {
6390 htab_traverse_noresize (dwo_file->tus,
6391 process_skeletonless_type_unit, info);
6392 }
6393
6394 return 1;
6395 }
6396
6397 /* Scan all TUs of DWO files, verifying we've processed them.
6398 This is needed in case a TU was emitted without its skeleton.
6399 Note: This can't be done until we know what all the DWO files are. */
6400
6401 static void
6402 process_skeletonless_type_units (struct objfile *objfile)
6403 {
6404 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6405 if (get_dwp_file () == NULL
6406 && dwarf2_per_objfile->dwo_files != NULL)
6407 {
6408 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6409 process_dwo_file_for_skeletonless_type_units,
6410 objfile);
6411 }
6412 }
6413
6414 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6415
6416 static void
6417 psymtabs_addrmap_cleanup (void *o)
6418 {
6419 struct objfile *objfile = (struct objfile *) o;
6420
6421 objfile->psymtabs_addrmap = NULL;
6422 }
6423
6424 /* Compute the 'user' field for each psymtab in OBJFILE. */
6425
6426 static void
6427 set_partial_user (struct objfile *objfile)
6428 {
6429 int i;
6430
6431 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6432 {
6433 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6434 struct partial_symtab *pst = per_cu->v.psymtab;
6435 int j;
6436
6437 if (pst == NULL)
6438 continue;
6439
6440 for (j = 0; j < pst->number_of_dependencies; ++j)
6441 {
6442 /* Set the 'user' field only if it is not already set. */
6443 if (pst->dependencies[j]->user == NULL)
6444 pst->dependencies[j]->user = pst;
6445 }
6446 }
6447 }
6448
6449 /* Build the partial symbol table by doing a quick pass through the
6450 .debug_info and .debug_abbrev sections. */
6451
6452 static void
6453 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6454 {
6455 struct cleanup *back_to, *addrmap_cleanup;
6456 struct obstack temp_obstack;
6457 int i;
6458
6459 if (dwarf_read_debug)
6460 {
6461 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6462 objfile_name (objfile));
6463 }
6464
6465 dwarf2_per_objfile->reading_partial_symbols = 1;
6466
6467 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6468
6469 /* Any cached compilation units will be linked by the per-objfile
6470 read_in_chain. Make sure to free them when we're done. */
6471 back_to = make_cleanup (free_cached_comp_units, NULL);
6472
6473 build_type_psymtabs (objfile);
6474
6475 create_all_comp_units (objfile);
6476
6477 /* Create a temporary address map on a temporary obstack. We later
6478 copy this to the final obstack. */
6479 obstack_init (&temp_obstack);
6480 make_cleanup_obstack_free (&temp_obstack);
6481 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6482 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6483
6484 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6485 {
6486 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6487
6488 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6489 }
6490
6491 /* This has to wait until we read the CUs, we need the list of DWOs. */
6492 process_skeletonless_type_units (objfile);
6493
6494 /* Now that all TUs have been processed we can fill in the dependencies. */
6495 if (dwarf2_per_objfile->type_unit_groups != NULL)
6496 {
6497 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6498 build_type_psymtab_dependencies, NULL);
6499 }
6500
6501 if (dwarf_read_debug)
6502 print_tu_stats ();
6503
6504 set_partial_user (objfile);
6505
6506 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6507 &objfile->objfile_obstack);
6508 discard_cleanups (addrmap_cleanup);
6509
6510 do_cleanups (back_to);
6511
6512 if (dwarf_read_debug)
6513 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6514 objfile_name (objfile));
6515 }
6516
6517 /* die_reader_func for load_partial_comp_unit. */
6518
6519 static void
6520 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6521 const gdb_byte *info_ptr,
6522 struct die_info *comp_unit_die,
6523 int has_children,
6524 void *data)
6525 {
6526 struct dwarf2_cu *cu = reader->cu;
6527
6528 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6529
6530 /* Check if comp unit has_children.
6531 If so, read the rest of the partial symbols from this comp unit.
6532 If not, there's no more debug_info for this comp unit. */
6533 if (has_children)
6534 load_partial_dies (reader, info_ptr, 0);
6535 }
6536
6537 /* Load the partial DIEs for a secondary CU into memory.
6538 This is also used when rereading a primary CU with load_all_dies. */
6539
6540 static void
6541 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6542 {
6543 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6544 load_partial_comp_unit_reader, NULL);
6545 }
6546
6547 static void
6548 read_comp_units_from_section (struct objfile *objfile,
6549 struct dwarf2_section_info *section,
6550 unsigned int is_dwz,
6551 int *n_allocated,
6552 int *n_comp_units,
6553 struct dwarf2_per_cu_data ***all_comp_units)
6554 {
6555 const gdb_byte *info_ptr;
6556 bfd *abfd = get_section_bfd_owner (section);
6557
6558 if (dwarf_read_debug)
6559 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6560 get_section_name (section),
6561 get_section_file_name (section));
6562
6563 dwarf2_read_section (objfile, section);
6564
6565 info_ptr = section->buffer;
6566
6567 while (info_ptr < section->buffer + section->size)
6568 {
6569 unsigned int length, initial_length_size;
6570 struct dwarf2_per_cu_data *this_cu;
6571 sect_offset offset;
6572
6573 offset.sect_off = info_ptr - section->buffer;
6574
6575 /* Read just enough information to find out where the next
6576 compilation unit is. */
6577 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6578
6579 /* Save the compilation unit for later lookup. */
6580 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
6581 memset (this_cu, 0, sizeof (*this_cu));
6582 this_cu->offset = offset;
6583 this_cu->length = length + initial_length_size;
6584 this_cu->is_dwz = is_dwz;
6585 this_cu->objfile = objfile;
6586 this_cu->section = section;
6587
6588 if (*n_comp_units == *n_allocated)
6589 {
6590 *n_allocated *= 2;
6591 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6592 *all_comp_units, *n_allocated);
6593 }
6594 (*all_comp_units)[*n_comp_units] = this_cu;
6595 ++*n_comp_units;
6596
6597 info_ptr = info_ptr + this_cu->length;
6598 }
6599 }
6600
6601 /* Create a list of all compilation units in OBJFILE.
6602 This is only done for -readnow and building partial symtabs. */
6603
6604 static void
6605 create_all_comp_units (struct objfile *objfile)
6606 {
6607 int n_allocated;
6608 int n_comp_units;
6609 struct dwarf2_per_cu_data **all_comp_units;
6610 struct dwz_file *dwz;
6611
6612 n_comp_units = 0;
6613 n_allocated = 10;
6614 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6615
6616 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6617 &n_allocated, &n_comp_units, &all_comp_units);
6618
6619 dwz = dwarf2_get_dwz_file ();
6620 if (dwz != NULL)
6621 read_comp_units_from_section (objfile, &dwz->info, 1,
6622 &n_allocated, &n_comp_units,
6623 &all_comp_units);
6624
6625 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6626 struct dwarf2_per_cu_data *,
6627 n_comp_units);
6628 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6629 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6630 xfree (all_comp_units);
6631 dwarf2_per_objfile->n_comp_units = n_comp_units;
6632 }
6633
6634 /* Process all loaded DIEs for compilation unit CU, starting at
6635 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6636 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6637 DW_AT_ranges). See the comments of add_partial_subprogram on how
6638 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6639
6640 static void
6641 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6642 CORE_ADDR *highpc, int set_addrmap,
6643 struct dwarf2_cu *cu)
6644 {
6645 struct partial_die_info *pdi;
6646
6647 /* Now, march along the PDI's, descending into ones which have
6648 interesting children but skipping the children of the other ones,
6649 until we reach the end of the compilation unit. */
6650
6651 pdi = first_die;
6652
6653 while (pdi != NULL)
6654 {
6655 fixup_partial_die (pdi, cu);
6656
6657 /* Anonymous namespaces or modules have no name but have interesting
6658 children, so we need to look at them. Ditto for anonymous
6659 enums. */
6660
6661 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6662 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6663 || pdi->tag == DW_TAG_imported_unit)
6664 {
6665 switch (pdi->tag)
6666 {
6667 case DW_TAG_subprogram:
6668 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6669 break;
6670 case DW_TAG_constant:
6671 case DW_TAG_variable:
6672 case DW_TAG_typedef:
6673 case DW_TAG_union_type:
6674 if (!pdi->is_declaration)
6675 {
6676 add_partial_symbol (pdi, cu);
6677 }
6678 break;
6679 case DW_TAG_class_type:
6680 case DW_TAG_interface_type:
6681 case DW_TAG_structure_type:
6682 if (!pdi->is_declaration)
6683 {
6684 add_partial_symbol (pdi, cu);
6685 }
6686 if (cu->language == language_rust && pdi->has_children)
6687 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6688 set_addrmap, cu);
6689 break;
6690 case DW_TAG_enumeration_type:
6691 if (!pdi->is_declaration)
6692 add_partial_enumeration (pdi, cu);
6693 break;
6694 case DW_TAG_base_type:
6695 case DW_TAG_subrange_type:
6696 /* File scope base type definitions are added to the partial
6697 symbol table. */
6698 add_partial_symbol (pdi, cu);
6699 break;
6700 case DW_TAG_namespace:
6701 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6702 break;
6703 case DW_TAG_module:
6704 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6705 break;
6706 case DW_TAG_imported_unit:
6707 {
6708 struct dwarf2_per_cu_data *per_cu;
6709
6710 /* For now we don't handle imported units in type units. */
6711 if (cu->per_cu->is_debug_types)
6712 {
6713 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6714 " supported in type units [in module %s]"),
6715 objfile_name (cu->objfile));
6716 }
6717
6718 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6719 pdi->is_dwz,
6720 cu->objfile);
6721
6722 /* Go read the partial unit, if needed. */
6723 if (per_cu->v.psymtab == NULL)
6724 process_psymtab_comp_unit (per_cu, 1, cu->language);
6725
6726 VEC_safe_push (dwarf2_per_cu_ptr,
6727 cu->per_cu->imported_symtabs, per_cu);
6728 }
6729 break;
6730 case DW_TAG_imported_declaration:
6731 add_partial_symbol (pdi, cu);
6732 break;
6733 default:
6734 break;
6735 }
6736 }
6737
6738 /* If the die has a sibling, skip to the sibling. */
6739
6740 pdi = pdi->die_sibling;
6741 }
6742 }
6743
6744 /* Functions used to compute the fully scoped name of a partial DIE.
6745
6746 Normally, this is simple. For C++, the parent DIE's fully scoped
6747 name is concatenated with "::" and the partial DIE's name.
6748 Enumerators are an exception; they use the scope of their parent
6749 enumeration type, i.e. the name of the enumeration type is not
6750 prepended to the enumerator.
6751
6752 There are two complexities. One is DW_AT_specification; in this
6753 case "parent" means the parent of the target of the specification,
6754 instead of the direct parent of the DIE. The other is compilers
6755 which do not emit DW_TAG_namespace; in this case we try to guess
6756 the fully qualified name of structure types from their members'
6757 linkage names. This must be done using the DIE's children rather
6758 than the children of any DW_AT_specification target. We only need
6759 to do this for structures at the top level, i.e. if the target of
6760 any DW_AT_specification (if any; otherwise the DIE itself) does not
6761 have a parent. */
6762
6763 /* Compute the scope prefix associated with PDI's parent, in
6764 compilation unit CU. The result will be allocated on CU's
6765 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6766 field. NULL is returned if no prefix is necessary. */
6767 static const char *
6768 partial_die_parent_scope (struct partial_die_info *pdi,
6769 struct dwarf2_cu *cu)
6770 {
6771 const char *grandparent_scope;
6772 struct partial_die_info *parent, *real_pdi;
6773
6774 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6775 then this means the parent of the specification DIE. */
6776
6777 real_pdi = pdi;
6778 while (real_pdi->has_specification)
6779 real_pdi = find_partial_die (real_pdi->spec_offset,
6780 real_pdi->spec_is_dwz, cu);
6781
6782 parent = real_pdi->die_parent;
6783 if (parent == NULL)
6784 return NULL;
6785
6786 if (parent->scope_set)
6787 return parent->scope;
6788
6789 fixup_partial_die (parent, cu);
6790
6791 grandparent_scope = partial_die_parent_scope (parent, cu);
6792
6793 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6794 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6795 Work around this problem here. */
6796 if (cu->language == language_cplus
6797 && parent->tag == DW_TAG_namespace
6798 && strcmp (parent->name, "::") == 0
6799 && grandparent_scope == NULL)
6800 {
6801 parent->scope = NULL;
6802 parent->scope_set = 1;
6803 return NULL;
6804 }
6805
6806 if (pdi->tag == DW_TAG_enumerator)
6807 /* Enumerators should not get the name of the enumeration as a prefix. */
6808 parent->scope = grandparent_scope;
6809 else if (parent->tag == DW_TAG_namespace
6810 || parent->tag == DW_TAG_module
6811 || parent->tag == DW_TAG_structure_type
6812 || parent->tag == DW_TAG_class_type
6813 || parent->tag == DW_TAG_interface_type
6814 || parent->tag == DW_TAG_union_type
6815 || parent->tag == DW_TAG_enumeration_type)
6816 {
6817 if (grandparent_scope == NULL)
6818 parent->scope = parent->name;
6819 else
6820 parent->scope = typename_concat (&cu->comp_unit_obstack,
6821 grandparent_scope,
6822 parent->name, 0, cu);
6823 }
6824 else
6825 {
6826 /* FIXME drow/2004-04-01: What should we be doing with
6827 function-local names? For partial symbols, we should probably be
6828 ignoring them. */
6829 complaint (&symfile_complaints,
6830 _("unhandled containing DIE tag %d for DIE at %d"),
6831 parent->tag, pdi->offset.sect_off);
6832 parent->scope = grandparent_scope;
6833 }
6834
6835 parent->scope_set = 1;
6836 return parent->scope;
6837 }
6838
6839 /* Return the fully scoped name associated with PDI, from compilation unit
6840 CU. The result will be allocated with malloc. */
6841
6842 static char *
6843 partial_die_full_name (struct partial_die_info *pdi,
6844 struct dwarf2_cu *cu)
6845 {
6846 const char *parent_scope;
6847
6848 /* If this is a template instantiation, we can not work out the
6849 template arguments from partial DIEs. So, unfortunately, we have
6850 to go through the full DIEs. At least any work we do building
6851 types here will be reused if full symbols are loaded later. */
6852 if (pdi->has_template_arguments)
6853 {
6854 fixup_partial_die (pdi, cu);
6855
6856 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6857 {
6858 struct die_info *die;
6859 struct attribute attr;
6860 struct dwarf2_cu *ref_cu = cu;
6861
6862 /* DW_FORM_ref_addr is using section offset. */
6863 attr.name = (enum dwarf_attribute) 0;
6864 attr.form = DW_FORM_ref_addr;
6865 attr.u.unsnd = pdi->offset.sect_off;
6866 die = follow_die_ref (NULL, &attr, &ref_cu);
6867
6868 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6869 }
6870 }
6871
6872 parent_scope = partial_die_parent_scope (pdi, cu);
6873 if (parent_scope == NULL)
6874 return NULL;
6875 else
6876 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6877 }
6878
6879 static void
6880 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6881 {
6882 struct objfile *objfile = cu->objfile;
6883 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6884 CORE_ADDR addr = 0;
6885 const char *actual_name = NULL;
6886 CORE_ADDR baseaddr;
6887 char *built_actual_name;
6888
6889 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6890
6891 built_actual_name = partial_die_full_name (pdi, cu);
6892 if (built_actual_name != NULL)
6893 actual_name = built_actual_name;
6894
6895 if (actual_name == NULL)
6896 actual_name = pdi->name;
6897
6898 switch (pdi->tag)
6899 {
6900 case DW_TAG_subprogram:
6901 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
6902 if (pdi->is_external || cu->language == language_ada)
6903 {
6904 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6905 of the global scope. But in Ada, we want to be able to access
6906 nested procedures globally. So all Ada subprograms are stored
6907 in the global scope. */
6908 add_psymbol_to_list (actual_name, strlen (actual_name),
6909 built_actual_name != NULL,
6910 VAR_DOMAIN, LOC_BLOCK,
6911 &objfile->global_psymbols,
6912 addr, cu->language, objfile);
6913 }
6914 else
6915 {
6916 add_psymbol_to_list (actual_name, strlen (actual_name),
6917 built_actual_name != NULL,
6918 VAR_DOMAIN, LOC_BLOCK,
6919 &objfile->static_psymbols,
6920 addr, cu->language, objfile);
6921 }
6922
6923 if (pdi->main_subprogram && actual_name != NULL)
6924 set_objfile_main_name (objfile, actual_name, cu->language);
6925 break;
6926 case DW_TAG_constant:
6927 {
6928 struct psymbol_allocation_list *list;
6929
6930 if (pdi->is_external)
6931 list = &objfile->global_psymbols;
6932 else
6933 list = &objfile->static_psymbols;
6934 add_psymbol_to_list (actual_name, strlen (actual_name),
6935 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6936 list, 0, cu->language, objfile);
6937 }
6938 break;
6939 case DW_TAG_variable:
6940 if (pdi->d.locdesc)
6941 addr = decode_locdesc (pdi->d.locdesc, cu);
6942
6943 if (pdi->d.locdesc
6944 && addr == 0
6945 && !dwarf2_per_objfile->has_section_at_zero)
6946 {
6947 /* A global or static variable may also have been stripped
6948 out by the linker if unused, in which case its address
6949 will be nullified; do not add such variables into partial
6950 symbol table then. */
6951 }
6952 else if (pdi->is_external)
6953 {
6954 /* Global Variable.
6955 Don't enter into the minimal symbol tables as there is
6956 a minimal symbol table entry from the ELF symbols already.
6957 Enter into partial symbol table if it has a location
6958 descriptor or a type.
6959 If the location descriptor is missing, new_symbol will create
6960 a LOC_UNRESOLVED symbol, the address of the variable will then
6961 be determined from the minimal symbol table whenever the variable
6962 is referenced.
6963 The address for the partial symbol table entry is not
6964 used by GDB, but it comes in handy for debugging partial symbol
6965 table building. */
6966
6967 if (pdi->d.locdesc || pdi->has_type)
6968 add_psymbol_to_list (actual_name, strlen (actual_name),
6969 built_actual_name != NULL,
6970 VAR_DOMAIN, LOC_STATIC,
6971 &objfile->global_psymbols,
6972 addr + baseaddr,
6973 cu->language, objfile);
6974 }
6975 else
6976 {
6977 int has_loc = pdi->d.locdesc != NULL;
6978
6979 /* Static Variable. Skip symbols whose value we cannot know (those
6980 without location descriptors or constant values). */
6981 if (!has_loc && !pdi->has_const_value)
6982 {
6983 xfree (built_actual_name);
6984 return;
6985 }
6986
6987 add_psymbol_to_list (actual_name, strlen (actual_name),
6988 built_actual_name != NULL,
6989 VAR_DOMAIN, LOC_STATIC,
6990 &objfile->static_psymbols,
6991 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
6992 cu->language, objfile);
6993 }
6994 break;
6995 case DW_TAG_typedef:
6996 case DW_TAG_base_type:
6997 case DW_TAG_subrange_type:
6998 add_psymbol_to_list (actual_name, strlen (actual_name),
6999 built_actual_name != NULL,
7000 VAR_DOMAIN, LOC_TYPEDEF,
7001 &objfile->static_psymbols,
7002 0, cu->language, objfile);
7003 break;
7004 case DW_TAG_imported_declaration:
7005 case DW_TAG_namespace:
7006 add_psymbol_to_list (actual_name, strlen (actual_name),
7007 built_actual_name != NULL,
7008 VAR_DOMAIN, LOC_TYPEDEF,
7009 &objfile->global_psymbols,
7010 0, cu->language, objfile);
7011 break;
7012 case DW_TAG_module:
7013 add_psymbol_to_list (actual_name, strlen (actual_name),
7014 built_actual_name != NULL,
7015 MODULE_DOMAIN, LOC_TYPEDEF,
7016 &objfile->global_psymbols,
7017 0, cu->language, objfile);
7018 break;
7019 case DW_TAG_class_type:
7020 case DW_TAG_interface_type:
7021 case DW_TAG_structure_type:
7022 case DW_TAG_union_type:
7023 case DW_TAG_enumeration_type:
7024 /* Skip external references. The DWARF standard says in the section
7025 about "Structure, Union, and Class Type Entries": "An incomplete
7026 structure, union or class type is represented by a structure,
7027 union or class entry that does not have a byte size attribute
7028 and that has a DW_AT_declaration attribute." */
7029 if (!pdi->has_byte_size && pdi->is_declaration)
7030 {
7031 xfree (built_actual_name);
7032 return;
7033 }
7034
7035 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7036 static vs. global. */
7037 add_psymbol_to_list (actual_name, strlen (actual_name),
7038 built_actual_name != NULL,
7039 STRUCT_DOMAIN, LOC_TYPEDEF,
7040 cu->language == language_cplus
7041 ? &objfile->global_psymbols
7042 : &objfile->static_psymbols,
7043 0, cu->language, objfile);
7044
7045 break;
7046 case DW_TAG_enumerator:
7047 add_psymbol_to_list (actual_name, strlen (actual_name),
7048 built_actual_name != NULL,
7049 VAR_DOMAIN, LOC_CONST,
7050 cu->language == language_cplus
7051 ? &objfile->global_psymbols
7052 : &objfile->static_psymbols,
7053 0, cu->language, objfile);
7054 break;
7055 default:
7056 break;
7057 }
7058
7059 xfree (built_actual_name);
7060 }
7061
7062 /* Read a partial die corresponding to a namespace; also, add a symbol
7063 corresponding to that namespace to the symbol table. NAMESPACE is
7064 the name of the enclosing namespace. */
7065
7066 static void
7067 add_partial_namespace (struct partial_die_info *pdi,
7068 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7069 int set_addrmap, struct dwarf2_cu *cu)
7070 {
7071 /* Add a symbol for the namespace. */
7072
7073 add_partial_symbol (pdi, cu);
7074
7075 /* Now scan partial symbols in that namespace. */
7076
7077 if (pdi->has_children)
7078 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7079 }
7080
7081 /* Read a partial die corresponding to a Fortran module. */
7082
7083 static void
7084 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7085 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7086 {
7087 /* Add a symbol for the namespace. */
7088
7089 add_partial_symbol (pdi, cu);
7090
7091 /* Now scan partial symbols in that module. */
7092
7093 if (pdi->has_children)
7094 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7095 }
7096
7097 /* Read a partial die corresponding to a subprogram and create a partial
7098 symbol for that subprogram. When the CU language allows it, this
7099 routine also defines a partial symbol for each nested subprogram
7100 that this subprogram contains. If SET_ADDRMAP is true, record the
7101 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7102 and highest PC values found in PDI.
7103
7104 PDI may also be a lexical block, in which case we simply search
7105 recursively for subprograms defined inside that lexical block.
7106 Again, this is only performed when the CU language allows this
7107 type of definitions. */
7108
7109 static void
7110 add_partial_subprogram (struct partial_die_info *pdi,
7111 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7112 int set_addrmap, struct dwarf2_cu *cu)
7113 {
7114 if (pdi->tag == DW_TAG_subprogram)
7115 {
7116 if (pdi->has_pc_info)
7117 {
7118 if (pdi->lowpc < *lowpc)
7119 *lowpc = pdi->lowpc;
7120 if (pdi->highpc > *highpc)
7121 *highpc = pdi->highpc;
7122 if (set_addrmap)
7123 {
7124 struct objfile *objfile = cu->objfile;
7125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7126 CORE_ADDR baseaddr;
7127 CORE_ADDR highpc;
7128 CORE_ADDR lowpc;
7129
7130 baseaddr = ANOFFSET (objfile->section_offsets,
7131 SECT_OFF_TEXT (objfile));
7132 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7133 pdi->lowpc + baseaddr);
7134 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7135 pdi->highpc + baseaddr);
7136 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7137 cu->per_cu->v.psymtab);
7138 }
7139 }
7140
7141 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7142 {
7143 if (!pdi->is_declaration)
7144 /* Ignore subprogram DIEs that do not have a name, they are
7145 illegal. Do not emit a complaint at this point, we will
7146 do so when we convert this psymtab into a symtab. */
7147 if (pdi->name)
7148 add_partial_symbol (pdi, cu);
7149 }
7150 }
7151
7152 if (! pdi->has_children)
7153 return;
7154
7155 if (cu->language == language_ada)
7156 {
7157 pdi = pdi->die_child;
7158 while (pdi != NULL)
7159 {
7160 fixup_partial_die (pdi, cu);
7161 if (pdi->tag == DW_TAG_subprogram
7162 || pdi->tag == DW_TAG_lexical_block)
7163 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7164 pdi = pdi->die_sibling;
7165 }
7166 }
7167 }
7168
7169 /* Read a partial die corresponding to an enumeration type. */
7170
7171 static void
7172 add_partial_enumeration (struct partial_die_info *enum_pdi,
7173 struct dwarf2_cu *cu)
7174 {
7175 struct partial_die_info *pdi;
7176
7177 if (enum_pdi->name != NULL)
7178 add_partial_symbol (enum_pdi, cu);
7179
7180 pdi = enum_pdi->die_child;
7181 while (pdi)
7182 {
7183 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7184 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7185 else
7186 add_partial_symbol (pdi, cu);
7187 pdi = pdi->die_sibling;
7188 }
7189 }
7190
7191 /* Return the initial uleb128 in the die at INFO_PTR. */
7192
7193 static unsigned int
7194 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7195 {
7196 unsigned int bytes_read;
7197
7198 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7199 }
7200
7201 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7202 Return the corresponding abbrev, or NULL if the number is zero (indicating
7203 an empty DIE). In either case *BYTES_READ will be set to the length of
7204 the initial number. */
7205
7206 static struct abbrev_info *
7207 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7208 struct dwarf2_cu *cu)
7209 {
7210 bfd *abfd = cu->objfile->obfd;
7211 unsigned int abbrev_number;
7212 struct abbrev_info *abbrev;
7213
7214 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7215
7216 if (abbrev_number == 0)
7217 return NULL;
7218
7219 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7220 if (!abbrev)
7221 {
7222 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7223 " at offset 0x%x [in module %s]"),
7224 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7225 cu->header.offset.sect_off, bfd_get_filename (abfd));
7226 }
7227
7228 return abbrev;
7229 }
7230
7231 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7232 Returns a pointer to the end of a series of DIEs, terminated by an empty
7233 DIE. Any children of the skipped DIEs will also be skipped. */
7234
7235 static const gdb_byte *
7236 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7237 {
7238 struct dwarf2_cu *cu = reader->cu;
7239 struct abbrev_info *abbrev;
7240 unsigned int bytes_read;
7241
7242 while (1)
7243 {
7244 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7245 if (abbrev == NULL)
7246 return info_ptr + bytes_read;
7247 else
7248 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7249 }
7250 }
7251
7252 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7253 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7254 abbrev corresponding to that skipped uleb128 should be passed in
7255 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7256 children. */
7257
7258 static const gdb_byte *
7259 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7260 struct abbrev_info *abbrev)
7261 {
7262 unsigned int bytes_read;
7263 struct attribute attr;
7264 bfd *abfd = reader->abfd;
7265 struct dwarf2_cu *cu = reader->cu;
7266 const gdb_byte *buffer = reader->buffer;
7267 const gdb_byte *buffer_end = reader->buffer_end;
7268 unsigned int form, i;
7269
7270 for (i = 0; i < abbrev->num_attrs; i++)
7271 {
7272 /* The only abbrev we care about is DW_AT_sibling. */
7273 if (abbrev->attrs[i].name == DW_AT_sibling)
7274 {
7275 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7276 if (attr.form == DW_FORM_ref_addr)
7277 complaint (&symfile_complaints,
7278 _("ignoring absolute DW_AT_sibling"));
7279 else
7280 {
7281 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7282 const gdb_byte *sibling_ptr = buffer + off;
7283
7284 if (sibling_ptr < info_ptr)
7285 complaint (&symfile_complaints,
7286 _("DW_AT_sibling points backwards"));
7287 else if (sibling_ptr > reader->buffer_end)
7288 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7289 else
7290 return sibling_ptr;
7291 }
7292 }
7293
7294 /* If it isn't DW_AT_sibling, skip this attribute. */
7295 form = abbrev->attrs[i].form;
7296 skip_attribute:
7297 switch (form)
7298 {
7299 case DW_FORM_ref_addr:
7300 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7301 and later it is offset sized. */
7302 if (cu->header.version == 2)
7303 info_ptr += cu->header.addr_size;
7304 else
7305 info_ptr += cu->header.offset_size;
7306 break;
7307 case DW_FORM_GNU_ref_alt:
7308 info_ptr += cu->header.offset_size;
7309 break;
7310 case DW_FORM_addr:
7311 info_ptr += cu->header.addr_size;
7312 break;
7313 case DW_FORM_data1:
7314 case DW_FORM_ref1:
7315 case DW_FORM_flag:
7316 info_ptr += 1;
7317 break;
7318 case DW_FORM_flag_present:
7319 break;
7320 case DW_FORM_data2:
7321 case DW_FORM_ref2:
7322 info_ptr += 2;
7323 break;
7324 case DW_FORM_data4:
7325 case DW_FORM_ref4:
7326 info_ptr += 4;
7327 break;
7328 case DW_FORM_data8:
7329 case DW_FORM_ref8:
7330 case DW_FORM_ref_sig8:
7331 info_ptr += 8;
7332 break;
7333 case DW_FORM_string:
7334 read_direct_string (abfd, info_ptr, &bytes_read);
7335 info_ptr += bytes_read;
7336 break;
7337 case DW_FORM_sec_offset:
7338 case DW_FORM_strp:
7339 case DW_FORM_GNU_strp_alt:
7340 info_ptr += cu->header.offset_size;
7341 break;
7342 case DW_FORM_exprloc:
7343 case DW_FORM_block:
7344 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7345 info_ptr += bytes_read;
7346 break;
7347 case DW_FORM_block1:
7348 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7349 break;
7350 case DW_FORM_block2:
7351 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7352 break;
7353 case DW_FORM_block4:
7354 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7355 break;
7356 case DW_FORM_sdata:
7357 case DW_FORM_udata:
7358 case DW_FORM_ref_udata:
7359 case DW_FORM_GNU_addr_index:
7360 case DW_FORM_GNU_str_index:
7361 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7362 break;
7363 case DW_FORM_indirect:
7364 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7365 info_ptr += bytes_read;
7366 /* We need to continue parsing from here, so just go back to
7367 the top. */
7368 goto skip_attribute;
7369
7370 default:
7371 error (_("Dwarf Error: Cannot handle %s "
7372 "in DWARF reader [in module %s]"),
7373 dwarf_form_name (form),
7374 bfd_get_filename (abfd));
7375 }
7376 }
7377
7378 if (abbrev->has_children)
7379 return skip_children (reader, info_ptr);
7380 else
7381 return info_ptr;
7382 }
7383
7384 /* Locate ORIG_PDI's sibling.
7385 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7386
7387 static const gdb_byte *
7388 locate_pdi_sibling (const struct die_reader_specs *reader,
7389 struct partial_die_info *orig_pdi,
7390 const gdb_byte *info_ptr)
7391 {
7392 /* Do we know the sibling already? */
7393
7394 if (orig_pdi->sibling)
7395 return orig_pdi->sibling;
7396
7397 /* Are there any children to deal with? */
7398
7399 if (!orig_pdi->has_children)
7400 return info_ptr;
7401
7402 /* Skip the children the long way. */
7403
7404 return skip_children (reader, info_ptr);
7405 }
7406
7407 /* Expand this partial symbol table into a full symbol table. SELF is
7408 not NULL. */
7409
7410 static void
7411 dwarf2_read_symtab (struct partial_symtab *self,
7412 struct objfile *objfile)
7413 {
7414 if (self->readin)
7415 {
7416 warning (_("bug: psymtab for %s is already read in."),
7417 self->filename);
7418 }
7419 else
7420 {
7421 if (info_verbose)
7422 {
7423 printf_filtered (_("Reading in symbols for %s..."),
7424 self->filename);
7425 gdb_flush (gdb_stdout);
7426 }
7427
7428 /* Restore our global data. */
7429 dwarf2_per_objfile
7430 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7431 dwarf2_objfile_data_key);
7432
7433 /* If this psymtab is constructed from a debug-only objfile, the
7434 has_section_at_zero flag will not necessarily be correct. We
7435 can get the correct value for this flag by looking at the data
7436 associated with the (presumably stripped) associated objfile. */
7437 if (objfile->separate_debug_objfile_backlink)
7438 {
7439 struct dwarf2_per_objfile *dpo_backlink
7440 = ((struct dwarf2_per_objfile *)
7441 objfile_data (objfile->separate_debug_objfile_backlink,
7442 dwarf2_objfile_data_key));
7443
7444 dwarf2_per_objfile->has_section_at_zero
7445 = dpo_backlink->has_section_at_zero;
7446 }
7447
7448 dwarf2_per_objfile->reading_partial_symbols = 0;
7449
7450 psymtab_to_symtab_1 (self);
7451
7452 /* Finish up the debug error message. */
7453 if (info_verbose)
7454 printf_filtered (_("done.\n"));
7455 }
7456
7457 process_cu_includes ();
7458 }
7459 \f
7460 /* Reading in full CUs. */
7461
7462 /* Add PER_CU to the queue. */
7463
7464 static void
7465 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7466 enum language pretend_language)
7467 {
7468 struct dwarf2_queue_item *item;
7469
7470 per_cu->queued = 1;
7471 item = XNEW (struct dwarf2_queue_item);
7472 item->per_cu = per_cu;
7473 item->pretend_language = pretend_language;
7474 item->next = NULL;
7475
7476 if (dwarf2_queue == NULL)
7477 dwarf2_queue = item;
7478 else
7479 dwarf2_queue_tail->next = item;
7480
7481 dwarf2_queue_tail = item;
7482 }
7483
7484 /* If PER_CU is not yet queued, add it to the queue.
7485 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7486 dependency.
7487 The result is non-zero if PER_CU was queued, otherwise the result is zero
7488 meaning either PER_CU is already queued or it is already loaded.
7489
7490 N.B. There is an invariant here that if a CU is queued then it is loaded.
7491 The caller is required to load PER_CU if we return non-zero. */
7492
7493 static int
7494 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7495 struct dwarf2_per_cu_data *per_cu,
7496 enum language pretend_language)
7497 {
7498 /* We may arrive here during partial symbol reading, if we need full
7499 DIEs to process an unusual case (e.g. template arguments). Do
7500 not queue PER_CU, just tell our caller to load its DIEs. */
7501 if (dwarf2_per_objfile->reading_partial_symbols)
7502 {
7503 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7504 return 1;
7505 return 0;
7506 }
7507
7508 /* Mark the dependence relation so that we don't flush PER_CU
7509 too early. */
7510 if (dependent_cu != NULL)
7511 dwarf2_add_dependence (dependent_cu, per_cu);
7512
7513 /* If it's already on the queue, we have nothing to do. */
7514 if (per_cu->queued)
7515 return 0;
7516
7517 /* If the compilation unit is already loaded, just mark it as
7518 used. */
7519 if (per_cu->cu != NULL)
7520 {
7521 per_cu->cu->last_used = 0;
7522 return 0;
7523 }
7524
7525 /* Add it to the queue. */
7526 queue_comp_unit (per_cu, pretend_language);
7527
7528 return 1;
7529 }
7530
7531 /* Process the queue. */
7532
7533 static void
7534 process_queue (void)
7535 {
7536 struct dwarf2_queue_item *item, *next_item;
7537
7538 if (dwarf_read_debug)
7539 {
7540 fprintf_unfiltered (gdb_stdlog,
7541 "Expanding one or more symtabs of objfile %s ...\n",
7542 objfile_name (dwarf2_per_objfile->objfile));
7543 }
7544
7545 /* The queue starts out with one item, but following a DIE reference
7546 may load a new CU, adding it to the end of the queue. */
7547 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7548 {
7549 if ((dwarf2_per_objfile->using_index
7550 ? !item->per_cu->v.quick->compunit_symtab
7551 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7552 /* Skip dummy CUs. */
7553 && item->per_cu->cu != NULL)
7554 {
7555 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7556 unsigned int debug_print_threshold;
7557 char buf[100];
7558
7559 if (per_cu->is_debug_types)
7560 {
7561 struct signatured_type *sig_type =
7562 (struct signatured_type *) per_cu;
7563
7564 sprintf (buf, "TU %s at offset 0x%x",
7565 hex_string (sig_type->signature),
7566 per_cu->offset.sect_off);
7567 /* There can be 100s of TUs.
7568 Only print them in verbose mode. */
7569 debug_print_threshold = 2;
7570 }
7571 else
7572 {
7573 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7574 debug_print_threshold = 1;
7575 }
7576
7577 if (dwarf_read_debug >= debug_print_threshold)
7578 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7579
7580 if (per_cu->is_debug_types)
7581 process_full_type_unit (per_cu, item->pretend_language);
7582 else
7583 process_full_comp_unit (per_cu, item->pretend_language);
7584
7585 if (dwarf_read_debug >= debug_print_threshold)
7586 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7587 }
7588
7589 item->per_cu->queued = 0;
7590 next_item = item->next;
7591 xfree (item);
7592 }
7593
7594 dwarf2_queue_tail = NULL;
7595
7596 if (dwarf_read_debug)
7597 {
7598 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7599 objfile_name (dwarf2_per_objfile->objfile));
7600 }
7601 }
7602
7603 /* Free all allocated queue entries. This function only releases anything if
7604 an error was thrown; if the queue was processed then it would have been
7605 freed as we went along. */
7606
7607 static void
7608 dwarf2_release_queue (void *dummy)
7609 {
7610 struct dwarf2_queue_item *item, *last;
7611
7612 item = dwarf2_queue;
7613 while (item)
7614 {
7615 /* Anything still marked queued is likely to be in an
7616 inconsistent state, so discard it. */
7617 if (item->per_cu->queued)
7618 {
7619 if (item->per_cu->cu != NULL)
7620 free_one_cached_comp_unit (item->per_cu);
7621 item->per_cu->queued = 0;
7622 }
7623
7624 last = item;
7625 item = item->next;
7626 xfree (last);
7627 }
7628
7629 dwarf2_queue = dwarf2_queue_tail = NULL;
7630 }
7631
7632 /* Read in full symbols for PST, and anything it depends on. */
7633
7634 static void
7635 psymtab_to_symtab_1 (struct partial_symtab *pst)
7636 {
7637 struct dwarf2_per_cu_data *per_cu;
7638 int i;
7639
7640 if (pst->readin)
7641 return;
7642
7643 for (i = 0; i < pst->number_of_dependencies; i++)
7644 if (!pst->dependencies[i]->readin
7645 && pst->dependencies[i]->user == NULL)
7646 {
7647 /* Inform about additional files that need to be read in. */
7648 if (info_verbose)
7649 {
7650 /* FIXME: i18n: Need to make this a single string. */
7651 fputs_filtered (" ", gdb_stdout);
7652 wrap_here ("");
7653 fputs_filtered ("and ", gdb_stdout);
7654 wrap_here ("");
7655 printf_filtered ("%s...", pst->dependencies[i]->filename);
7656 wrap_here (""); /* Flush output. */
7657 gdb_flush (gdb_stdout);
7658 }
7659 psymtab_to_symtab_1 (pst->dependencies[i]);
7660 }
7661
7662 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7663
7664 if (per_cu == NULL)
7665 {
7666 /* It's an include file, no symbols to read for it.
7667 Everything is in the parent symtab. */
7668 pst->readin = 1;
7669 return;
7670 }
7671
7672 dw2_do_instantiate_symtab (per_cu);
7673 }
7674
7675 /* Trivial hash function for die_info: the hash value of a DIE
7676 is its offset in .debug_info for this objfile. */
7677
7678 static hashval_t
7679 die_hash (const void *item)
7680 {
7681 const struct die_info *die = (const struct die_info *) item;
7682
7683 return die->offset.sect_off;
7684 }
7685
7686 /* Trivial comparison function for die_info structures: two DIEs
7687 are equal if they have the same offset. */
7688
7689 static int
7690 die_eq (const void *item_lhs, const void *item_rhs)
7691 {
7692 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7693 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7694
7695 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7696 }
7697
7698 /* die_reader_func for load_full_comp_unit.
7699 This is identical to read_signatured_type_reader,
7700 but is kept separate for now. */
7701
7702 static void
7703 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7704 const gdb_byte *info_ptr,
7705 struct die_info *comp_unit_die,
7706 int has_children,
7707 void *data)
7708 {
7709 struct dwarf2_cu *cu = reader->cu;
7710 enum language *language_ptr = (enum language *) data;
7711
7712 gdb_assert (cu->die_hash == NULL);
7713 cu->die_hash =
7714 htab_create_alloc_ex (cu->header.length / 12,
7715 die_hash,
7716 die_eq,
7717 NULL,
7718 &cu->comp_unit_obstack,
7719 hashtab_obstack_allocate,
7720 dummy_obstack_deallocate);
7721
7722 if (has_children)
7723 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7724 &info_ptr, comp_unit_die);
7725 cu->dies = comp_unit_die;
7726 /* comp_unit_die is not stored in die_hash, no need. */
7727
7728 /* We try not to read any attributes in this function, because not
7729 all CUs needed for references have been loaded yet, and symbol
7730 table processing isn't initialized. But we have to set the CU language,
7731 or we won't be able to build types correctly.
7732 Similarly, if we do not read the producer, we can not apply
7733 producer-specific interpretation. */
7734 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7735 }
7736
7737 /* Load the DIEs associated with PER_CU into memory. */
7738
7739 static void
7740 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7741 enum language pretend_language)
7742 {
7743 gdb_assert (! this_cu->is_debug_types);
7744
7745 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7746 load_full_comp_unit_reader, &pretend_language);
7747 }
7748
7749 /* Add a DIE to the delayed physname list. */
7750
7751 static void
7752 add_to_method_list (struct type *type, int fnfield_index, int index,
7753 const char *name, struct die_info *die,
7754 struct dwarf2_cu *cu)
7755 {
7756 struct delayed_method_info mi;
7757 mi.type = type;
7758 mi.fnfield_index = fnfield_index;
7759 mi.index = index;
7760 mi.name = name;
7761 mi.die = die;
7762 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7763 }
7764
7765 /* A cleanup for freeing the delayed method list. */
7766
7767 static void
7768 free_delayed_list (void *ptr)
7769 {
7770 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7771 if (cu->method_list != NULL)
7772 {
7773 VEC_free (delayed_method_info, cu->method_list);
7774 cu->method_list = NULL;
7775 }
7776 }
7777
7778 /* Compute the physnames of any methods on the CU's method list.
7779
7780 The computation of method physnames is delayed in order to avoid the
7781 (bad) condition that one of the method's formal parameters is of an as yet
7782 incomplete type. */
7783
7784 static void
7785 compute_delayed_physnames (struct dwarf2_cu *cu)
7786 {
7787 int i;
7788 struct delayed_method_info *mi;
7789 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7790 {
7791 const char *physname;
7792 struct fn_fieldlist *fn_flp
7793 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7794 physname = dwarf2_physname (mi->name, mi->die, cu);
7795 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7796 = physname ? physname : "";
7797 }
7798 }
7799
7800 /* Go objects should be embedded in a DW_TAG_module DIE,
7801 and it's not clear if/how imported objects will appear.
7802 To keep Go support simple until that's worked out,
7803 go back through what we've read and create something usable.
7804 We could do this while processing each DIE, and feels kinda cleaner,
7805 but that way is more invasive.
7806 This is to, for example, allow the user to type "p var" or "b main"
7807 without having to specify the package name, and allow lookups
7808 of module.object to work in contexts that use the expression
7809 parser. */
7810
7811 static void
7812 fixup_go_packaging (struct dwarf2_cu *cu)
7813 {
7814 char *package_name = NULL;
7815 struct pending *list;
7816 int i;
7817
7818 for (list = global_symbols; list != NULL; list = list->next)
7819 {
7820 for (i = 0; i < list->nsyms; ++i)
7821 {
7822 struct symbol *sym = list->symbol[i];
7823
7824 if (SYMBOL_LANGUAGE (sym) == language_go
7825 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7826 {
7827 char *this_package_name = go_symbol_package_name (sym);
7828
7829 if (this_package_name == NULL)
7830 continue;
7831 if (package_name == NULL)
7832 package_name = this_package_name;
7833 else
7834 {
7835 if (strcmp (package_name, this_package_name) != 0)
7836 complaint (&symfile_complaints,
7837 _("Symtab %s has objects from two different Go packages: %s and %s"),
7838 (symbol_symtab (sym) != NULL
7839 ? symtab_to_filename_for_display
7840 (symbol_symtab (sym))
7841 : objfile_name (cu->objfile)),
7842 this_package_name, package_name);
7843 xfree (this_package_name);
7844 }
7845 }
7846 }
7847 }
7848
7849 if (package_name != NULL)
7850 {
7851 struct objfile *objfile = cu->objfile;
7852 const char *saved_package_name
7853 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
7854 package_name,
7855 strlen (package_name));
7856 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7857 saved_package_name);
7858 struct symbol *sym;
7859
7860 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7861
7862 sym = allocate_symbol (objfile);
7863 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7864 SYMBOL_SET_NAMES (sym, saved_package_name,
7865 strlen (saved_package_name), 0, objfile);
7866 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7867 e.g., "main" finds the "main" module and not C's main(). */
7868 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7869 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7870 SYMBOL_TYPE (sym) = type;
7871
7872 add_symbol_to_list (sym, &global_symbols);
7873
7874 xfree (package_name);
7875 }
7876 }
7877
7878 /* Return the symtab for PER_CU. This works properly regardless of
7879 whether we're using the index or psymtabs. */
7880
7881 static struct compunit_symtab *
7882 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
7883 {
7884 return (dwarf2_per_objfile->using_index
7885 ? per_cu->v.quick->compunit_symtab
7886 : per_cu->v.psymtab->compunit_symtab);
7887 }
7888
7889 /* A helper function for computing the list of all symbol tables
7890 included by PER_CU. */
7891
7892 static void
7893 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
7894 htab_t all_children, htab_t all_type_symtabs,
7895 struct dwarf2_per_cu_data *per_cu,
7896 struct compunit_symtab *immediate_parent)
7897 {
7898 void **slot;
7899 int ix;
7900 struct compunit_symtab *cust;
7901 struct dwarf2_per_cu_data *iter;
7902
7903 slot = htab_find_slot (all_children, per_cu, INSERT);
7904 if (*slot != NULL)
7905 {
7906 /* This inclusion and its children have been processed. */
7907 return;
7908 }
7909
7910 *slot = per_cu;
7911 /* Only add a CU if it has a symbol table. */
7912 cust = get_compunit_symtab (per_cu);
7913 if (cust != NULL)
7914 {
7915 /* If this is a type unit only add its symbol table if we haven't
7916 seen it yet (type unit per_cu's can share symtabs). */
7917 if (per_cu->is_debug_types)
7918 {
7919 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
7920 if (*slot == NULL)
7921 {
7922 *slot = cust;
7923 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7924 if (cust->user == NULL)
7925 cust->user = immediate_parent;
7926 }
7927 }
7928 else
7929 {
7930 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7931 if (cust->user == NULL)
7932 cust->user = immediate_parent;
7933 }
7934 }
7935
7936 for (ix = 0;
7937 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7938 ++ix)
7939 {
7940 recursively_compute_inclusions (result, all_children,
7941 all_type_symtabs, iter, cust);
7942 }
7943 }
7944
7945 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
7946 PER_CU. */
7947
7948 static void
7949 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7950 {
7951 gdb_assert (! per_cu->is_debug_types);
7952
7953 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7954 {
7955 int ix, len;
7956 struct dwarf2_per_cu_data *per_cu_iter;
7957 struct compunit_symtab *compunit_symtab_iter;
7958 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
7959 htab_t all_children, all_type_symtabs;
7960 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
7961
7962 /* If we don't have a symtab, we can just skip this case. */
7963 if (cust == NULL)
7964 return;
7965
7966 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7967 NULL, xcalloc, xfree);
7968 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7969 NULL, xcalloc, xfree);
7970
7971 for (ix = 0;
7972 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7973 ix, per_cu_iter);
7974 ++ix)
7975 {
7976 recursively_compute_inclusions (&result_symtabs, all_children,
7977 all_type_symtabs, per_cu_iter,
7978 cust);
7979 }
7980
7981 /* Now we have a transitive closure of all the included symtabs. */
7982 len = VEC_length (compunit_symtab_ptr, result_symtabs);
7983 cust->includes
7984 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
7985 struct compunit_symtab *, len + 1);
7986 for (ix = 0;
7987 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
7988 compunit_symtab_iter);
7989 ++ix)
7990 cust->includes[ix] = compunit_symtab_iter;
7991 cust->includes[len] = NULL;
7992
7993 VEC_free (compunit_symtab_ptr, result_symtabs);
7994 htab_delete (all_children);
7995 htab_delete (all_type_symtabs);
7996 }
7997 }
7998
7999 /* Compute the 'includes' field for the symtabs of all the CUs we just
8000 read. */
8001
8002 static void
8003 process_cu_includes (void)
8004 {
8005 int ix;
8006 struct dwarf2_per_cu_data *iter;
8007
8008 for (ix = 0;
8009 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8010 ix, iter);
8011 ++ix)
8012 {
8013 if (! iter->is_debug_types)
8014 compute_compunit_symtab_includes (iter);
8015 }
8016
8017 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8018 }
8019
8020 /* Generate full symbol information for PER_CU, whose DIEs have
8021 already been loaded into memory. */
8022
8023 static void
8024 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8025 enum language pretend_language)
8026 {
8027 struct dwarf2_cu *cu = per_cu->cu;
8028 struct objfile *objfile = per_cu->objfile;
8029 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8030 CORE_ADDR lowpc, highpc;
8031 struct compunit_symtab *cust;
8032 struct cleanup *back_to, *delayed_list_cleanup;
8033 CORE_ADDR baseaddr;
8034 struct block *static_block;
8035 CORE_ADDR addr;
8036
8037 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8038
8039 buildsym_init ();
8040 back_to = make_cleanup (really_free_pendings, NULL);
8041 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8042
8043 cu->list_in_scope = &file_symbols;
8044
8045 cu->language = pretend_language;
8046 cu->language_defn = language_def (cu->language);
8047
8048 /* Do line number decoding in read_file_scope () */
8049 process_die (cu->dies, cu);
8050
8051 /* For now fudge the Go package. */
8052 if (cu->language == language_go)
8053 fixup_go_packaging (cu);
8054
8055 /* Now that we have processed all the DIEs in the CU, all the types
8056 should be complete, and it should now be safe to compute all of the
8057 physnames. */
8058 compute_delayed_physnames (cu);
8059 do_cleanups (delayed_list_cleanup);
8060
8061 /* Some compilers don't define a DW_AT_high_pc attribute for the
8062 compilation unit. If the DW_AT_high_pc is missing, synthesize
8063 it, by scanning the DIE's below the compilation unit. */
8064 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8065
8066 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8067 static_block = end_symtab_get_static_block (addr, 0, 1);
8068
8069 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8070 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8071 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8072 addrmap to help ensure it has an accurate map of pc values belonging to
8073 this comp unit. */
8074 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8075
8076 cust = end_symtab_from_static_block (static_block,
8077 SECT_OFF_TEXT (objfile), 0);
8078
8079 if (cust != NULL)
8080 {
8081 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8082
8083 /* Set symtab language to language from DW_AT_language. If the
8084 compilation is from a C file generated by language preprocessors, do
8085 not set the language if it was already deduced by start_subfile. */
8086 if (!(cu->language == language_c
8087 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8088 COMPUNIT_FILETABS (cust)->language = cu->language;
8089
8090 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8091 produce DW_AT_location with location lists but it can be possibly
8092 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8093 there were bugs in prologue debug info, fixed later in GCC-4.5
8094 by "unwind info for epilogues" patch (which is not directly related).
8095
8096 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8097 needed, it would be wrong due to missing DW_AT_producer there.
8098
8099 Still one can confuse GDB by using non-standard GCC compilation
8100 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8101 */
8102 if (cu->has_loclist && gcc_4_minor >= 5)
8103 cust->locations_valid = 1;
8104
8105 if (gcc_4_minor >= 5)
8106 cust->epilogue_unwind_valid = 1;
8107
8108 cust->call_site_htab = cu->call_site_htab;
8109 }
8110
8111 if (dwarf2_per_objfile->using_index)
8112 per_cu->v.quick->compunit_symtab = cust;
8113 else
8114 {
8115 struct partial_symtab *pst = per_cu->v.psymtab;
8116 pst->compunit_symtab = cust;
8117 pst->readin = 1;
8118 }
8119
8120 /* Push it for inclusion processing later. */
8121 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8122
8123 do_cleanups (back_to);
8124 }
8125
8126 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8127 already been loaded into memory. */
8128
8129 static void
8130 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8131 enum language pretend_language)
8132 {
8133 struct dwarf2_cu *cu = per_cu->cu;
8134 struct objfile *objfile = per_cu->objfile;
8135 struct compunit_symtab *cust;
8136 struct cleanup *back_to, *delayed_list_cleanup;
8137 struct signatured_type *sig_type;
8138
8139 gdb_assert (per_cu->is_debug_types);
8140 sig_type = (struct signatured_type *) per_cu;
8141
8142 buildsym_init ();
8143 back_to = make_cleanup (really_free_pendings, NULL);
8144 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8145
8146 cu->list_in_scope = &file_symbols;
8147
8148 cu->language = pretend_language;
8149 cu->language_defn = language_def (cu->language);
8150
8151 /* The symbol tables are set up in read_type_unit_scope. */
8152 process_die (cu->dies, cu);
8153
8154 /* For now fudge the Go package. */
8155 if (cu->language == language_go)
8156 fixup_go_packaging (cu);
8157
8158 /* Now that we have processed all the DIEs in the CU, all the types
8159 should be complete, and it should now be safe to compute all of the
8160 physnames. */
8161 compute_delayed_physnames (cu);
8162 do_cleanups (delayed_list_cleanup);
8163
8164 /* TUs share symbol tables.
8165 If this is the first TU to use this symtab, complete the construction
8166 of it with end_expandable_symtab. Otherwise, complete the addition of
8167 this TU's symbols to the existing symtab. */
8168 if (sig_type->type_unit_group->compunit_symtab == NULL)
8169 {
8170 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8171 sig_type->type_unit_group->compunit_symtab = cust;
8172
8173 if (cust != NULL)
8174 {
8175 /* Set symtab language to language from DW_AT_language. If the
8176 compilation is from a C file generated by language preprocessors,
8177 do not set the language if it was already deduced by
8178 start_subfile. */
8179 if (!(cu->language == language_c
8180 && COMPUNIT_FILETABS (cust)->language != language_c))
8181 COMPUNIT_FILETABS (cust)->language = cu->language;
8182 }
8183 }
8184 else
8185 {
8186 augment_type_symtab ();
8187 cust = sig_type->type_unit_group->compunit_symtab;
8188 }
8189
8190 if (dwarf2_per_objfile->using_index)
8191 per_cu->v.quick->compunit_symtab = cust;
8192 else
8193 {
8194 struct partial_symtab *pst = per_cu->v.psymtab;
8195 pst->compunit_symtab = cust;
8196 pst->readin = 1;
8197 }
8198
8199 do_cleanups (back_to);
8200 }
8201
8202 /* Process an imported unit DIE. */
8203
8204 static void
8205 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8206 {
8207 struct attribute *attr;
8208
8209 /* For now we don't handle imported units in type units. */
8210 if (cu->per_cu->is_debug_types)
8211 {
8212 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8213 " supported in type units [in module %s]"),
8214 objfile_name (cu->objfile));
8215 }
8216
8217 attr = dwarf2_attr (die, DW_AT_import, cu);
8218 if (attr != NULL)
8219 {
8220 struct dwarf2_per_cu_data *per_cu;
8221 sect_offset offset;
8222 int is_dwz;
8223
8224 offset = dwarf2_get_ref_die_offset (attr);
8225 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8226 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
8227
8228 /* If necessary, add it to the queue and load its DIEs. */
8229 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8230 load_full_comp_unit (per_cu, cu->language);
8231
8232 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8233 per_cu);
8234 }
8235 }
8236
8237 /* Reset the in_process bit of a die. */
8238
8239 static void
8240 reset_die_in_process (void *arg)
8241 {
8242 struct die_info *die = (struct die_info *) arg;
8243
8244 die->in_process = 0;
8245 }
8246
8247 /* Process a die and its children. */
8248
8249 static void
8250 process_die (struct die_info *die, struct dwarf2_cu *cu)
8251 {
8252 struct cleanup *in_process;
8253
8254 /* We should only be processing those not already in process. */
8255 gdb_assert (!die->in_process);
8256
8257 die->in_process = 1;
8258 in_process = make_cleanup (reset_die_in_process,die);
8259
8260 switch (die->tag)
8261 {
8262 case DW_TAG_padding:
8263 break;
8264 case DW_TAG_compile_unit:
8265 case DW_TAG_partial_unit:
8266 read_file_scope (die, cu);
8267 break;
8268 case DW_TAG_type_unit:
8269 read_type_unit_scope (die, cu);
8270 break;
8271 case DW_TAG_subprogram:
8272 case DW_TAG_inlined_subroutine:
8273 read_func_scope (die, cu);
8274 break;
8275 case DW_TAG_lexical_block:
8276 case DW_TAG_try_block:
8277 case DW_TAG_catch_block:
8278 read_lexical_block_scope (die, cu);
8279 break;
8280 case DW_TAG_GNU_call_site:
8281 read_call_site_scope (die, cu);
8282 break;
8283 case DW_TAG_class_type:
8284 case DW_TAG_interface_type:
8285 case DW_TAG_structure_type:
8286 case DW_TAG_union_type:
8287 process_structure_scope (die, cu);
8288 break;
8289 case DW_TAG_enumeration_type:
8290 process_enumeration_scope (die, cu);
8291 break;
8292
8293 /* These dies have a type, but processing them does not create
8294 a symbol or recurse to process the children. Therefore we can
8295 read them on-demand through read_type_die. */
8296 case DW_TAG_subroutine_type:
8297 case DW_TAG_set_type:
8298 case DW_TAG_array_type:
8299 case DW_TAG_pointer_type:
8300 case DW_TAG_ptr_to_member_type:
8301 case DW_TAG_reference_type:
8302 case DW_TAG_string_type:
8303 break;
8304
8305 case DW_TAG_base_type:
8306 case DW_TAG_subrange_type:
8307 case DW_TAG_typedef:
8308 /* Add a typedef symbol for the type definition, if it has a
8309 DW_AT_name. */
8310 new_symbol (die, read_type_die (die, cu), cu);
8311 break;
8312 case DW_TAG_common_block:
8313 read_common_block (die, cu);
8314 break;
8315 case DW_TAG_common_inclusion:
8316 break;
8317 case DW_TAG_namespace:
8318 cu->processing_has_namespace_info = 1;
8319 read_namespace (die, cu);
8320 break;
8321 case DW_TAG_module:
8322 cu->processing_has_namespace_info = 1;
8323 read_module (die, cu);
8324 break;
8325 case DW_TAG_imported_declaration:
8326 cu->processing_has_namespace_info = 1;
8327 if (read_namespace_alias (die, cu))
8328 break;
8329 /* The declaration is not a global namespace alias: fall through. */
8330 case DW_TAG_imported_module:
8331 cu->processing_has_namespace_info = 1;
8332 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8333 || cu->language != language_fortran))
8334 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8335 dwarf_tag_name (die->tag));
8336 read_import_statement (die, cu);
8337 break;
8338
8339 case DW_TAG_imported_unit:
8340 process_imported_unit_die (die, cu);
8341 break;
8342
8343 default:
8344 new_symbol (die, NULL, cu);
8345 break;
8346 }
8347
8348 do_cleanups (in_process);
8349 }
8350 \f
8351 /* DWARF name computation. */
8352
8353 /* A helper function for dwarf2_compute_name which determines whether DIE
8354 needs to have the name of the scope prepended to the name listed in the
8355 die. */
8356
8357 static int
8358 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8359 {
8360 struct attribute *attr;
8361
8362 switch (die->tag)
8363 {
8364 case DW_TAG_namespace:
8365 case DW_TAG_typedef:
8366 case DW_TAG_class_type:
8367 case DW_TAG_interface_type:
8368 case DW_TAG_structure_type:
8369 case DW_TAG_union_type:
8370 case DW_TAG_enumeration_type:
8371 case DW_TAG_enumerator:
8372 case DW_TAG_subprogram:
8373 case DW_TAG_inlined_subroutine:
8374 case DW_TAG_member:
8375 case DW_TAG_imported_declaration:
8376 return 1;
8377
8378 case DW_TAG_variable:
8379 case DW_TAG_constant:
8380 /* We only need to prefix "globally" visible variables. These include
8381 any variable marked with DW_AT_external or any variable that
8382 lives in a namespace. [Variables in anonymous namespaces
8383 require prefixing, but they are not DW_AT_external.] */
8384
8385 if (dwarf2_attr (die, DW_AT_specification, cu))
8386 {
8387 struct dwarf2_cu *spec_cu = cu;
8388
8389 return die_needs_namespace (die_specification (die, &spec_cu),
8390 spec_cu);
8391 }
8392
8393 attr = dwarf2_attr (die, DW_AT_external, cu);
8394 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8395 && die->parent->tag != DW_TAG_module)
8396 return 0;
8397 /* A variable in a lexical block of some kind does not need a
8398 namespace, even though in C++ such variables may be external
8399 and have a mangled name. */
8400 if (die->parent->tag == DW_TAG_lexical_block
8401 || die->parent->tag == DW_TAG_try_block
8402 || die->parent->tag == DW_TAG_catch_block
8403 || die->parent->tag == DW_TAG_subprogram)
8404 return 0;
8405 return 1;
8406
8407 default:
8408 return 0;
8409 }
8410 }
8411
8412 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8413 compute the physname for the object, which include a method's:
8414 - formal parameters (C++),
8415 - receiver type (Go),
8416
8417 The term "physname" is a bit confusing.
8418 For C++, for example, it is the demangled name.
8419 For Go, for example, it's the mangled name.
8420
8421 For Ada, return the DIE's linkage name rather than the fully qualified
8422 name. PHYSNAME is ignored..
8423
8424 The result is allocated on the objfile_obstack and canonicalized. */
8425
8426 static const char *
8427 dwarf2_compute_name (const char *name,
8428 struct die_info *die, struct dwarf2_cu *cu,
8429 int physname)
8430 {
8431 struct objfile *objfile = cu->objfile;
8432
8433 if (name == NULL)
8434 name = dwarf2_name (die, cu);
8435
8436 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8437 but otherwise compute it by typename_concat inside GDB.
8438 FIXME: Actually this is not really true, or at least not always true.
8439 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8440 Fortran names because there is no mangling standard. So new_symbol_full
8441 will set the demangled name to the result of dwarf2_full_name, and it is
8442 the demangled name that GDB uses if it exists. */
8443 if (cu->language == language_ada
8444 || (cu->language == language_fortran && physname))
8445 {
8446 /* For Ada unit, we prefer the linkage name over the name, as
8447 the former contains the exported name, which the user expects
8448 to be able to reference. Ideally, we want the user to be able
8449 to reference this entity using either natural or linkage name,
8450 but we haven't started looking at this enhancement yet. */
8451 const char *linkage_name;
8452
8453 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8454 if (linkage_name == NULL)
8455 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8456 if (linkage_name != NULL)
8457 return linkage_name;
8458 }
8459
8460 /* These are the only languages we know how to qualify names in. */
8461 if (name != NULL
8462 && (cu->language == language_cplus
8463 || cu->language == language_fortran || cu->language == language_d
8464 || cu->language == language_rust))
8465 {
8466 if (die_needs_namespace (die, cu))
8467 {
8468 long length;
8469 const char *prefix;
8470 const char *canonical_name = NULL;
8471
8472 string_file buf;
8473
8474 prefix = determine_prefix (die, cu);
8475 if (*prefix != '\0')
8476 {
8477 char *prefixed_name = typename_concat (NULL, prefix, name,
8478 physname, cu);
8479
8480 buf.puts (prefixed_name);
8481 xfree (prefixed_name);
8482 }
8483 else
8484 buf.puts (name);
8485
8486 /* Template parameters may be specified in the DIE's DW_AT_name, or
8487 as children with DW_TAG_template_type_param or
8488 DW_TAG_value_type_param. If the latter, add them to the name
8489 here. If the name already has template parameters, then
8490 skip this step; some versions of GCC emit both, and
8491 it is more efficient to use the pre-computed name.
8492
8493 Something to keep in mind about this process: it is very
8494 unlikely, or in some cases downright impossible, to produce
8495 something that will match the mangled name of a function.
8496 If the definition of the function has the same debug info,
8497 we should be able to match up with it anyway. But fallbacks
8498 using the minimal symbol, for instance to find a method
8499 implemented in a stripped copy of libstdc++, will not work.
8500 If we do not have debug info for the definition, we will have to
8501 match them up some other way.
8502
8503 When we do name matching there is a related problem with function
8504 templates; two instantiated function templates are allowed to
8505 differ only by their return types, which we do not add here. */
8506
8507 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8508 {
8509 struct attribute *attr;
8510 struct die_info *child;
8511 int first = 1;
8512
8513 die->building_fullname = 1;
8514
8515 for (child = die->child; child != NULL; child = child->sibling)
8516 {
8517 struct type *type;
8518 LONGEST value;
8519 const gdb_byte *bytes;
8520 struct dwarf2_locexpr_baton *baton;
8521 struct value *v;
8522
8523 if (child->tag != DW_TAG_template_type_param
8524 && child->tag != DW_TAG_template_value_param)
8525 continue;
8526
8527 if (first)
8528 {
8529 buf.puts ("<");
8530 first = 0;
8531 }
8532 else
8533 buf.puts (", ");
8534
8535 attr = dwarf2_attr (child, DW_AT_type, cu);
8536 if (attr == NULL)
8537 {
8538 complaint (&symfile_complaints,
8539 _("template parameter missing DW_AT_type"));
8540 buf.puts ("UNKNOWN_TYPE");
8541 continue;
8542 }
8543 type = die_type (child, cu);
8544
8545 if (child->tag == DW_TAG_template_type_param)
8546 {
8547 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8548 continue;
8549 }
8550
8551 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8552 if (attr == NULL)
8553 {
8554 complaint (&symfile_complaints,
8555 _("template parameter missing "
8556 "DW_AT_const_value"));
8557 buf.puts ("UNKNOWN_VALUE");
8558 continue;
8559 }
8560
8561 dwarf2_const_value_attr (attr, type, name,
8562 &cu->comp_unit_obstack, cu,
8563 &value, &bytes, &baton);
8564
8565 if (TYPE_NOSIGN (type))
8566 /* GDB prints characters as NUMBER 'CHAR'. If that's
8567 changed, this can use value_print instead. */
8568 c_printchar (value, type, &buf);
8569 else
8570 {
8571 struct value_print_options opts;
8572
8573 if (baton != NULL)
8574 v = dwarf2_evaluate_loc_desc (type, NULL,
8575 baton->data,
8576 baton->size,
8577 baton->per_cu);
8578 else if (bytes != NULL)
8579 {
8580 v = allocate_value (type);
8581 memcpy (value_contents_writeable (v), bytes,
8582 TYPE_LENGTH (type));
8583 }
8584 else
8585 v = value_from_longest (type, value);
8586
8587 /* Specify decimal so that we do not depend on
8588 the radix. */
8589 get_formatted_print_options (&opts, 'd');
8590 opts.raw = 1;
8591 value_print (v, &buf, &opts);
8592 release_value (v);
8593 value_free (v);
8594 }
8595 }
8596
8597 die->building_fullname = 0;
8598
8599 if (!first)
8600 {
8601 /* Close the argument list, with a space if necessary
8602 (nested templates). */
8603 if (!buf.empty () && buf.string ().back () == '>')
8604 buf.puts (" >");
8605 else
8606 buf.puts (">");
8607 }
8608 }
8609
8610 /* For C++ methods, append formal parameter type
8611 information, if PHYSNAME. */
8612
8613 if (physname && die->tag == DW_TAG_subprogram
8614 && cu->language == language_cplus)
8615 {
8616 struct type *type = read_type_die (die, cu);
8617
8618 c_type_print_args (type, &buf, 1, cu->language,
8619 &type_print_raw_options);
8620
8621 if (cu->language == language_cplus)
8622 {
8623 /* Assume that an artificial first parameter is
8624 "this", but do not crash if it is not. RealView
8625 marks unnamed (and thus unused) parameters as
8626 artificial; there is no way to differentiate
8627 the two cases. */
8628 if (TYPE_NFIELDS (type) > 0
8629 && TYPE_FIELD_ARTIFICIAL (type, 0)
8630 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8631 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8632 0))))
8633 buf.puts (" const");
8634 }
8635 }
8636
8637 const std::string &intermediate_name = buf.string ();
8638
8639 if (cu->language == language_cplus)
8640 canonical_name
8641 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8642 &objfile->per_bfd->storage_obstack);
8643
8644 /* If we only computed INTERMEDIATE_NAME, or if
8645 INTERMEDIATE_NAME is already canonical, then we need to
8646 copy it to the appropriate obstack. */
8647 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8648 name = ((const char *)
8649 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8650 intermediate_name.c_str (),
8651 intermediate_name.length ()));
8652 else
8653 name = canonical_name;
8654 }
8655 }
8656
8657 return name;
8658 }
8659
8660 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8661 If scope qualifiers are appropriate they will be added. The result
8662 will be allocated on the storage_obstack, or NULL if the DIE does
8663 not have a name. NAME may either be from a previous call to
8664 dwarf2_name or NULL.
8665
8666 The output string will be canonicalized (if C++). */
8667
8668 static const char *
8669 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8670 {
8671 return dwarf2_compute_name (name, die, cu, 0);
8672 }
8673
8674 /* Construct a physname for the given DIE in CU. NAME may either be
8675 from a previous call to dwarf2_name or NULL. The result will be
8676 allocated on the objfile_objstack or NULL if the DIE does not have a
8677 name.
8678
8679 The output string will be canonicalized (if C++). */
8680
8681 static const char *
8682 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8683 {
8684 struct objfile *objfile = cu->objfile;
8685 const char *retval, *mangled = NULL, *canon = NULL;
8686 struct cleanup *back_to;
8687 int need_copy = 1;
8688
8689 /* In this case dwarf2_compute_name is just a shortcut not building anything
8690 on its own. */
8691 if (!die_needs_namespace (die, cu))
8692 return dwarf2_compute_name (name, die, cu, 1);
8693
8694 back_to = make_cleanup (null_cleanup, NULL);
8695
8696 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8697 if (mangled == NULL)
8698 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8699
8700 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8701 See https://github.com/rust-lang/rust/issues/32925. */
8702 if (cu->language == language_rust && mangled != NULL
8703 && strchr (mangled, '{') != NULL)
8704 mangled = NULL;
8705
8706 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8707 has computed. */
8708 if (mangled != NULL)
8709 {
8710 char *demangled;
8711
8712 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8713 type. It is easier for GDB users to search for such functions as
8714 `name(params)' than `long name(params)'. In such case the minimal
8715 symbol names do not match the full symbol names but for template
8716 functions there is never a need to look up their definition from their
8717 declaration so the only disadvantage remains the minimal symbol
8718 variant `long name(params)' does not have the proper inferior type.
8719 */
8720
8721 if (cu->language == language_go)
8722 {
8723 /* This is a lie, but we already lie to the caller new_symbol_full.
8724 new_symbol_full assumes we return the mangled name.
8725 This just undoes that lie until things are cleaned up. */
8726 demangled = NULL;
8727 }
8728 else
8729 {
8730 demangled = gdb_demangle (mangled,
8731 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
8732 }
8733 if (demangled)
8734 {
8735 make_cleanup (xfree, demangled);
8736 canon = demangled;
8737 }
8738 else
8739 {
8740 canon = mangled;
8741 need_copy = 0;
8742 }
8743 }
8744
8745 if (canon == NULL || check_physname)
8746 {
8747 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8748
8749 if (canon != NULL && strcmp (physname, canon) != 0)
8750 {
8751 /* It may not mean a bug in GDB. The compiler could also
8752 compute DW_AT_linkage_name incorrectly. But in such case
8753 GDB would need to be bug-to-bug compatible. */
8754
8755 complaint (&symfile_complaints,
8756 _("Computed physname <%s> does not match demangled <%s> "
8757 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8758 physname, canon, mangled, die->offset.sect_off,
8759 objfile_name (objfile));
8760
8761 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8762 is available here - over computed PHYSNAME. It is safer
8763 against both buggy GDB and buggy compilers. */
8764
8765 retval = canon;
8766 }
8767 else
8768 {
8769 retval = physname;
8770 need_copy = 0;
8771 }
8772 }
8773 else
8774 retval = canon;
8775
8776 if (need_copy)
8777 retval = ((const char *)
8778 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8779 retval, strlen (retval)));
8780
8781 do_cleanups (back_to);
8782 return retval;
8783 }
8784
8785 /* Inspect DIE in CU for a namespace alias. If one exists, record
8786 a new symbol for it.
8787
8788 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8789
8790 static int
8791 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8792 {
8793 struct attribute *attr;
8794
8795 /* If the die does not have a name, this is not a namespace
8796 alias. */
8797 attr = dwarf2_attr (die, DW_AT_name, cu);
8798 if (attr != NULL)
8799 {
8800 int num;
8801 struct die_info *d = die;
8802 struct dwarf2_cu *imported_cu = cu;
8803
8804 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8805 keep inspecting DIEs until we hit the underlying import. */
8806 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8807 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8808 {
8809 attr = dwarf2_attr (d, DW_AT_import, cu);
8810 if (attr == NULL)
8811 break;
8812
8813 d = follow_die_ref (d, attr, &imported_cu);
8814 if (d->tag != DW_TAG_imported_declaration)
8815 break;
8816 }
8817
8818 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8819 {
8820 complaint (&symfile_complaints,
8821 _("DIE at 0x%x has too many recursively imported "
8822 "declarations"), d->offset.sect_off);
8823 return 0;
8824 }
8825
8826 if (attr != NULL)
8827 {
8828 struct type *type;
8829 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8830
8831 type = get_die_type_at_offset (offset, cu->per_cu);
8832 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8833 {
8834 /* This declaration is a global namespace alias. Add
8835 a symbol for it whose type is the aliased namespace. */
8836 new_symbol (die, type, cu);
8837 return 1;
8838 }
8839 }
8840 }
8841
8842 return 0;
8843 }
8844
8845 /* Return the using directives repository (global or local?) to use in the
8846 current context for LANGUAGE.
8847
8848 For Ada, imported declarations can materialize renamings, which *may* be
8849 global. However it is impossible (for now?) in DWARF to distinguish
8850 "external" imported declarations and "static" ones. As all imported
8851 declarations seem to be static in all other languages, make them all CU-wide
8852 global only in Ada. */
8853
8854 static struct using_direct **
8855 using_directives (enum language language)
8856 {
8857 if (language == language_ada && context_stack_depth == 0)
8858 return &global_using_directives;
8859 else
8860 return &local_using_directives;
8861 }
8862
8863 /* Read the import statement specified by the given die and record it. */
8864
8865 static void
8866 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8867 {
8868 struct objfile *objfile = cu->objfile;
8869 struct attribute *import_attr;
8870 struct die_info *imported_die, *child_die;
8871 struct dwarf2_cu *imported_cu;
8872 const char *imported_name;
8873 const char *imported_name_prefix;
8874 const char *canonical_name;
8875 const char *import_alias;
8876 const char *imported_declaration = NULL;
8877 const char *import_prefix;
8878 VEC (const_char_ptr) *excludes = NULL;
8879 struct cleanup *cleanups;
8880
8881 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8882 if (import_attr == NULL)
8883 {
8884 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8885 dwarf_tag_name (die->tag));
8886 return;
8887 }
8888
8889 imported_cu = cu;
8890 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8891 imported_name = dwarf2_name (imported_die, imported_cu);
8892 if (imported_name == NULL)
8893 {
8894 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8895
8896 The import in the following code:
8897 namespace A
8898 {
8899 typedef int B;
8900 }
8901
8902 int main ()
8903 {
8904 using A::B;
8905 B b;
8906 return b;
8907 }
8908
8909 ...
8910 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8911 <52> DW_AT_decl_file : 1
8912 <53> DW_AT_decl_line : 6
8913 <54> DW_AT_import : <0x75>
8914 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8915 <59> DW_AT_name : B
8916 <5b> DW_AT_decl_file : 1
8917 <5c> DW_AT_decl_line : 2
8918 <5d> DW_AT_type : <0x6e>
8919 ...
8920 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8921 <76> DW_AT_byte_size : 4
8922 <77> DW_AT_encoding : 5 (signed)
8923
8924 imports the wrong die ( 0x75 instead of 0x58 ).
8925 This case will be ignored until the gcc bug is fixed. */
8926 return;
8927 }
8928
8929 /* Figure out the local name after import. */
8930 import_alias = dwarf2_name (die, cu);
8931
8932 /* Figure out where the statement is being imported to. */
8933 import_prefix = determine_prefix (die, cu);
8934
8935 /* Figure out what the scope of the imported die is and prepend it
8936 to the name of the imported die. */
8937 imported_name_prefix = determine_prefix (imported_die, imported_cu);
8938
8939 if (imported_die->tag != DW_TAG_namespace
8940 && imported_die->tag != DW_TAG_module)
8941 {
8942 imported_declaration = imported_name;
8943 canonical_name = imported_name_prefix;
8944 }
8945 else if (strlen (imported_name_prefix) > 0)
8946 canonical_name = obconcat (&objfile->objfile_obstack,
8947 imported_name_prefix,
8948 (cu->language == language_d ? "." : "::"),
8949 imported_name, (char *) NULL);
8950 else
8951 canonical_name = imported_name;
8952
8953 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8954
8955 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8956 for (child_die = die->child; child_die && child_die->tag;
8957 child_die = sibling_die (child_die))
8958 {
8959 /* DWARF-4: A Fortran use statement with a “rename list” may be
8960 represented by an imported module entry with an import attribute
8961 referring to the module and owned entries corresponding to those
8962 entities that are renamed as part of being imported. */
8963
8964 if (child_die->tag != DW_TAG_imported_declaration)
8965 {
8966 complaint (&symfile_complaints,
8967 _("child DW_TAG_imported_declaration expected "
8968 "- DIE at 0x%x [in module %s]"),
8969 child_die->offset.sect_off, objfile_name (objfile));
8970 continue;
8971 }
8972
8973 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8974 if (import_attr == NULL)
8975 {
8976 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8977 dwarf_tag_name (child_die->tag));
8978 continue;
8979 }
8980
8981 imported_cu = cu;
8982 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8983 &imported_cu);
8984 imported_name = dwarf2_name (imported_die, imported_cu);
8985 if (imported_name == NULL)
8986 {
8987 complaint (&symfile_complaints,
8988 _("child DW_TAG_imported_declaration has unknown "
8989 "imported name - DIE at 0x%x [in module %s]"),
8990 child_die->offset.sect_off, objfile_name (objfile));
8991 continue;
8992 }
8993
8994 VEC_safe_push (const_char_ptr, excludes, imported_name);
8995
8996 process_die (child_die, cu);
8997 }
8998
8999 add_using_directive (using_directives (cu->language),
9000 import_prefix,
9001 canonical_name,
9002 import_alias,
9003 imported_declaration,
9004 excludes,
9005 0,
9006 &objfile->objfile_obstack);
9007
9008 do_cleanups (cleanups);
9009 }
9010
9011 /* Cleanup function for handle_DW_AT_stmt_list. */
9012
9013 static void
9014 free_cu_line_header (void *arg)
9015 {
9016 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
9017
9018 free_line_header (cu->line_header);
9019 cu->line_header = NULL;
9020 }
9021
9022 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9023 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9024 this, it was first present in GCC release 4.3.0. */
9025
9026 static int
9027 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9028 {
9029 if (!cu->checked_producer)
9030 check_producer (cu);
9031
9032 return cu->producer_is_gcc_lt_4_3;
9033 }
9034
9035 static void
9036 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
9037 const char **name, const char **comp_dir)
9038 {
9039 /* Find the filename. Do not use dwarf2_name here, since the filename
9040 is not a source language identifier. */
9041 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9042 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9043
9044 if (*comp_dir == NULL
9045 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9046 && IS_ABSOLUTE_PATH (*name))
9047 {
9048 char *d = ldirname (*name);
9049
9050 *comp_dir = d;
9051 if (d != NULL)
9052 make_cleanup (xfree, d);
9053 }
9054 if (*comp_dir != NULL)
9055 {
9056 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9057 directory, get rid of it. */
9058 const char *cp = strchr (*comp_dir, ':');
9059
9060 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9061 *comp_dir = cp + 1;
9062 }
9063
9064 if (*name == NULL)
9065 *name = "<unknown>";
9066 }
9067
9068 /* Handle DW_AT_stmt_list for a compilation unit.
9069 DIE is the DW_TAG_compile_unit die for CU.
9070 COMP_DIR is the compilation directory. LOWPC is passed to
9071 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9072
9073 static void
9074 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9075 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9076 {
9077 struct objfile *objfile = dwarf2_per_objfile->objfile;
9078 struct attribute *attr;
9079 unsigned int line_offset;
9080 struct line_header line_header_local;
9081 hashval_t line_header_local_hash;
9082 unsigned u;
9083 void **slot;
9084 int decode_mapping;
9085
9086 gdb_assert (! cu->per_cu->is_debug_types);
9087
9088 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9089 if (attr == NULL)
9090 return;
9091
9092 line_offset = DW_UNSND (attr);
9093
9094 /* The line header hash table is only created if needed (it exists to
9095 prevent redundant reading of the line table for partial_units).
9096 If we're given a partial_unit, we'll need it. If we're given a
9097 compile_unit, then use the line header hash table if it's already
9098 created, but don't create one just yet. */
9099
9100 if (dwarf2_per_objfile->line_header_hash == NULL
9101 && die->tag == DW_TAG_partial_unit)
9102 {
9103 dwarf2_per_objfile->line_header_hash
9104 = htab_create_alloc_ex (127, line_header_hash_voidp,
9105 line_header_eq_voidp,
9106 free_line_header_voidp,
9107 &objfile->objfile_obstack,
9108 hashtab_obstack_allocate,
9109 dummy_obstack_deallocate);
9110 }
9111
9112 line_header_local.offset.sect_off = line_offset;
9113 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9114 line_header_local_hash = line_header_hash (&line_header_local);
9115 if (dwarf2_per_objfile->line_header_hash != NULL)
9116 {
9117 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9118 &line_header_local,
9119 line_header_local_hash, NO_INSERT);
9120
9121 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9122 is not present in *SLOT (since if there is something in *SLOT then
9123 it will be for a partial_unit). */
9124 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9125 {
9126 gdb_assert (*slot != NULL);
9127 cu->line_header = (struct line_header *) *slot;
9128 return;
9129 }
9130 }
9131
9132 /* dwarf_decode_line_header does not yet provide sufficient information.
9133 We always have to call also dwarf_decode_lines for it. */
9134 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9135 if (cu->line_header == NULL)
9136 return;
9137
9138 if (dwarf2_per_objfile->line_header_hash == NULL)
9139 slot = NULL;
9140 else
9141 {
9142 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9143 &line_header_local,
9144 line_header_local_hash, INSERT);
9145 gdb_assert (slot != NULL);
9146 }
9147 if (slot != NULL && *slot == NULL)
9148 {
9149 /* This newly decoded line number information unit will be owned
9150 by line_header_hash hash table. */
9151 *slot = cu->line_header;
9152 }
9153 else
9154 {
9155 /* We cannot free any current entry in (*slot) as that struct line_header
9156 may be already used by multiple CUs. Create only temporary decoded
9157 line_header for this CU - it may happen at most once for each line
9158 number information unit. And if we're not using line_header_hash
9159 then this is what we want as well. */
9160 gdb_assert (die->tag != DW_TAG_partial_unit);
9161 make_cleanup (free_cu_line_header, cu);
9162 }
9163 decode_mapping = (die->tag != DW_TAG_partial_unit);
9164 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9165 decode_mapping);
9166 }
9167
9168 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9169
9170 static void
9171 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9172 {
9173 struct objfile *objfile = dwarf2_per_objfile->objfile;
9174 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9175 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9176 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9177 CORE_ADDR highpc = ((CORE_ADDR) 0);
9178 struct attribute *attr;
9179 const char *name = NULL;
9180 const char *comp_dir = NULL;
9181 struct die_info *child_die;
9182 CORE_ADDR baseaddr;
9183
9184 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9185
9186 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9187
9188 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9189 from finish_block. */
9190 if (lowpc == ((CORE_ADDR) -1))
9191 lowpc = highpc;
9192 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9193
9194 find_file_and_directory (die, cu, &name, &comp_dir);
9195
9196 prepare_one_comp_unit (cu, die, cu->language);
9197
9198 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9199 standardised yet. As a workaround for the language detection we fall
9200 back to the DW_AT_producer string. */
9201 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9202 cu->language = language_opencl;
9203
9204 /* Similar hack for Go. */
9205 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9206 set_cu_language (DW_LANG_Go, cu);
9207
9208 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
9209
9210 /* Decode line number information if present. We do this before
9211 processing child DIEs, so that the line header table is available
9212 for DW_AT_decl_file. */
9213 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
9214
9215 /* Process all dies in compilation unit. */
9216 if (die->child != NULL)
9217 {
9218 child_die = die->child;
9219 while (child_die && child_die->tag)
9220 {
9221 process_die (child_die, cu);
9222 child_die = sibling_die (child_die);
9223 }
9224 }
9225
9226 /* Decode macro information, if present. Dwarf 2 macro information
9227 refers to information in the line number info statement program
9228 header, so we can only read it if we've read the header
9229 successfully. */
9230 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9231 if (attr && cu->line_header)
9232 {
9233 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9234 complaint (&symfile_complaints,
9235 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
9236
9237 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9238 }
9239 else
9240 {
9241 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9242 if (attr && cu->line_header)
9243 {
9244 unsigned int macro_offset = DW_UNSND (attr);
9245
9246 dwarf_decode_macros (cu, macro_offset, 0);
9247 }
9248 }
9249
9250 do_cleanups (back_to);
9251 }
9252
9253 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9254 Create the set of symtabs used by this TU, or if this TU is sharing
9255 symtabs with another TU and the symtabs have already been created
9256 then restore those symtabs in the line header.
9257 We don't need the pc/line-number mapping for type units. */
9258
9259 static void
9260 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9261 {
9262 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9263 struct type_unit_group *tu_group;
9264 int first_time;
9265 struct line_header *lh;
9266 struct attribute *attr;
9267 unsigned int i, line_offset;
9268 struct signatured_type *sig_type;
9269
9270 gdb_assert (per_cu->is_debug_types);
9271 sig_type = (struct signatured_type *) per_cu;
9272
9273 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9274
9275 /* If we're using .gdb_index (includes -readnow) then
9276 per_cu->type_unit_group may not have been set up yet. */
9277 if (sig_type->type_unit_group == NULL)
9278 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9279 tu_group = sig_type->type_unit_group;
9280
9281 /* If we've already processed this stmt_list there's no real need to
9282 do it again, we could fake it and just recreate the part we need
9283 (file name,index -> symtab mapping). If data shows this optimization
9284 is useful we can do it then. */
9285 first_time = tu_group->compunit_symtab == NULL;
9286
9287 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9288 debug info. */
9289 lh = NULL;
9290 if (attr != NULL)
9291 {
9292 line_offset = DW_UNSND (attr);
9293 lh = dwarf_decode_line_header (line_offset, cu);
9294 }
9295 if (lh == NULL)
9296 {
9297 if (first_time)
9298 dwarf2_start_symtab (cu, "", NULL, 0);
9299 else
9300 {
9301 gdb_assert (tu_group->symtabs == NULL);
9302 restart_symtab (tu_group->compunit_symtab, "", 0);
9303 }
9304 return;
9305 }
9306
9307 cu->line_header = lh;
9308 make_cleanup (free_cu_line_header, cu);
9309
9310 if (first_time)
9311 {
9312 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9313
9314 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9315 still initializing it, and our caller (a few levels up)
9316 process_full_type_unit still needs to know if this is the first
9317 time. */
9318
9319 tu_group->num_symtabs = lh->num_file_names;
9320 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
9321
9322 for (i = 0; i < lh->num_file_names; ++i)
9323 {
9324 const char *dir = NULL;
9325 struct file_entry *fe = &lh->file_names[i];
9326
9327 if (fe->dir_index && lh->include_dirs != NULL)
9328 dir = lh->include_dirs[fe->dir_index - 1];
9329 dwarf2_start_subfile (fe->name, dir);
9330
9331 if (current_subfile->symtab == NULL)
9332 {
9333 /* NOTE: start_subfile will recognize when it's been passed
9334 a file it has already seen. So we can't assume there's a
9335 simple mapping from lh->file_names to subfiles, plus
9336 lh->file_names may contain dups. */
9337 current_subfile->symtab
9338 = allocate_symtab (cust, current_subfile->name);
9339 }
9340
9341 fe->symtab = current_subfile->symtab;
9342 tu_group->symtabs[i] = fe->symtab;
9343 }
9344 }
9345 else
9346 {
9347 restart_symtab (tu_group->compunit_symtab, "", 0);
9348
9349 for (i = 0; i < lh->num_file_names; ++i)
9350 {
9351 struct file_entry *fe = &lh->file_names[i];
9352
9353 fe->symtab = tu_group->symtabs[i];
9354 }
9355 }
9356
9357 /* The main symtab is allocated last. Type units don't have DW_AT_name
9358 so they don't have a "real" (so to speak) symtab anyway.
9359 There is later code that will assign the main symtab to all symbols
9360 that don't have one. We need to handle the case of a symbol with a
9361 missing symtab (DW_AT_decl_file) anyway. */
9362 }
9363
9364 /* Process DW_TAG_type_unit.
9365 For TUs we want to skip the first top level sibling if it's not the
9366 actual type being defined by this TU. In this case the first top
9367 level sibling is there to provide context only. */
9368
9369 static void
9370 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9371 {
9372 struct die_info *child_die;
9373
9374 prepare_one_comp_unit (cu, die, language_minimal);
9375
9376 /* Initialize (or reinitialize) the machinery for building symtabs.
9377 We do this before processing child DIEs, so that the line header table
9378 is available for DW_AT_decl_file. */
9379 setup_type_unit_groups (die, cu);
9380
9381 if (die->child != NULL)
9382 {
9383 child_die = die->child;
9384 while (child_die && child_die->tag)
9385 {
9386 process_die (child_die, cu);
9387 child_die = sibling_die (child_die);
9388 }
9389 }
9390 }
9391 \f
9392 /* DWO/DWP files.
9393
9394 http://gcc.gnu.org/wiki/DebugFission
9395 http://gcc.gnu.org/wiki/DebugFissionDWP
9396
9397 To simplify handling of both DWO files ("object" files with the DWARF info)
9398 and DWP files (a file with the DWOs packaged up into one file), we treat
9399 DWP files as having a collection of virtual DWO files. */
9400
9401 static hashval_t
9402 hash_dwo_file (const void *item)
9403 {
9404 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9405 hashval_t hash;
9406
9407 hash = htab_hash_string (dwo_file->dwo_name);
9408 if (dwo_file->comp_dir != NULL)
9409 hash += htab_hash_string (dwo_file->comp_dir);
9410 return hash;
9411 }
9412
9413 static int
9414 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9415 {
9416 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9417 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9418
9419 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9420 return 0;
9421 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9422 return lhs->comp_dir == rhs->comp_dir;
9423 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9424 }
9425
9426 /* Allocate a hash table for DWO files. */
9427
9428 static htab_t
9429 allocate_dwo_file_hash_table (void)
9430 {
9431 struct objfile *objfile = dwarf2_per_objfile->objfile;
9432
9433 return htab_create_alloc_ex (41,
9434 hash_dwo_file,
9435 eq_dwo_file,
9436 NULL,
9437 &objfile->objfile_obstack,
9438 hashtab_obstack_allocate,
9439 dummy_obstack_deallocate);
9440 }
9441
9442 /* Lookup DWO file DWO_NAME. */
9443
9444 static void **
9445 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9446 {
9447 struct dwo_file find_entry;
9448 void **slot;
9449
9450 if (dwarf2_per_objfile->dwo_files == NULL)
9451 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9452
9453 memset (&find_entry, 0, sizeof (find_entry));
9454 find_entry.dwo_name = dwo_name;
9455 find_entry.comp_dir = comp_dir;
9456 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9457
9458 return slot;
9459 }
9460
9461 static hashval_t
9462 hash_dwo_unit (const void *item)
9463 {
9464 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9465
9466 /* This drops the top 32 bits of the id, but is ok for a hash. */
9467 return dwo_unit->signature;
9468 }
9469
9470 static int
9471 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9472 {
9473 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9474 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9475
9476 /* The signature is assumed to be unique within the DWO file.
9477 So while object file CU dwo_id's always have the value zero,
9478 that's OK, assuming each object file DWO file has only one CU,
9479 and that's the rule for now. */
9480 return lhs->signature == rhs->signature;
9481 }
9482
9483 /* Allocate a hash table for DWO CUs,TUs.
9484 There is one of these tables for each of CUs,TUs for each DWO file. */
9485
9486 static htab_t
9487 allocate_dwo_unit_table (struct objfile *objfile)
9488 {
9489 /* Start out with a pretty small number.
9490 Generally DWO files contain only one CU and maybe some TUs. */
9491 return htab_create_alloc_ex (3,
9492 hash_dwo_unit,
9493 eq_dwo_unit,
9494 NULL,
9495 &objfile->objfile_obstack,
9496 hashtab_obstack_allocate,
9497 dummy_obstack_deallocate);
9498 }
9499
9500 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9501
9502 struct create_dwo_cu_data
9503 {
9504 struct dwo_file *dwo_file;
9505 struct dwo_unit dwo_unit;
9506 };
9507
9508 /* die_reader_func for create_dwo_cu. */
9509
9510 static void
9511 create_dwo_cu_reader (const struct die_reader_specs *reader,
9512 const gdb_byte *info_ptr,
9513 struct die_info *comp_unit_die,
9514 int has_children,
9515 void *datap)
9516 {
9517 struct dwarf2_cu *cu = reader->cu;
9518 sect_offset offset = cu->per_cu->offset;
9519 struct dwarf2_section_info *section = cu->per_cu->section;
9520 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9521 struct dwo_file *dwo_file = data->dwo_file;
9522 struct dwo_unit *dwo_unit = &data->dwo_unit;
9523 struct attribute *attr;
9524
9525 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9526 if (attr == NULL)
9527 {
9528 complaint (&symfile_complaints,
9529 _("Dwarf Error: debug entry at offset 0x%x is missing"
9530 " its dwo_id [in module %s]"),
9531 offset.sect_off, dwo_file->dwo_name);
9532 return;
9533 }
9534
9535 dwo_unit->dwo_file = dwo_file;
9536 dwo_unit->signature = DW_UNSND (attr);
9537 dwo_unit->section = section;
9538 dwo_unit->offset = offset;
9539 dwo_unit->length = cu->per_cu->length;
9540
9541 if (dwarf_read_debug)
9542 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9543 offset.sect_off, hex_string (dwo_unit->signature));
9544 }
9545
9546 /* Create the dwo_unit for the lone CU in DWO_FILE.
9547 Note: This function processes DWO files only, not DWP files. */
9548
9549 static struct dwo_unit *
9550 create_dwo_cu (struct dwo_file *dwo_file)
9551 {
9552 struct objfile *objfile = dwarf2_per_objfile->objfile;
9553 struct dwarf2_section_info *section = &dwo_file->sections.info;
9554 const gdb_byte *info_ptr, *end_ptr;
9555 struct create_dwo_cu_data create_dwo_cu_data;
9556 struct dwo_unit *dwo_unit;
9557
9558 dwarf2_read_section (objfile, section);
9559 info_ptr = section->buffer;
9560
9561 if (info_ptr == NULL)
9562 return NULL;
9563
9564 if (dwarf_read_debug)
9565 {
9566 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9567 get_section_name (section),
9568 get_section_file_name (section));
9569 }
9570
9571 create_dwo_cu_data.dwo_file = dwo_file;
9572 dwo_unit = NULL;
9573
9574 end_ptr = info_ptr + section->size;
9575 while (info_ptr < end_ptr)
9576 {
9577 struct dwarf2_per_cu_data per_cu;
9578
9579 memset (&create_dwo_cu_data.dwo_unit, 0,
9580 sizeof (create_dwo_cu_data.dwo_unit));
9581 memset (&per_cu, 0, sizeof (per_cu));
9582 per_cu.objfile = objfile;
9583 per_cu.is_debug_types = 0;
9584 per_cu.offset.sect_off = info_ptr - section->buffer;
9585 per_cu.section = section;
9586
9587 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
9588 create_dwo_cu_reader,
9589 &create_dwo_cu_data);
9590
9591 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9592 {
9593 /* If we've already found one, complain. We only support one
9594 because having more than one requires hacking the dwo_name of
9595 each to match, which is highly unlikely to happen. */
9596 if (dwo_unit != NULL)
9597 {
9598 complaint (&symfile_complaints,
9599 _("Multiple CUs in DWO file %s [in module %s]"),
9600 dwo_file->dwo_name, objfile_name (objfile));
9601 break;
9602 }
9603
9604 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9605 *dwo_unit = create_dwo_cu_data.dwo_unit;
9606 }
9607
9608 info_ptr += per_cu.length;
9609 }
9610
9611 return dwo_unit;
9612 }
9613
9614 /* DWP file .debug_{cu,tu}_index section format:
9615 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9616
9617 DWP Version 1:
9618
9619 Both index sections have the same format, and serve to map a 64-bit
9620 signature to a set of section numbers. Each section begins with a header,
9621 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9622 indexes, and a pool of 32-bit section numbers. The index sections will be
9623 aligned at 8-byte boundaries in the file.
9624
9625 The index section header consists of:
9626
9627 V, 32 bit version number
9628 -, 32 bits unused
9629 N, 32 bit number of compilation units or type units in the index
9630 M, 32 bit number of slots in the hash table
9631
9632 Numbers are recorded using the byte order of the application binary.
9633
9634 The hash table begins at offset 16 in the section, and consists of an array
9635 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9636 order of the application binary). Unused slots in the hash table are 0.
9637 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9638
9639 The parallel table begins immediately after the hash table
9640 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9641 array of 32-bit indexes (using the byte order of the application binary),
9642 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9643 table contains a 32-bit index into the pool of section numbers. For unused
9644 hash table slots, the corresponding entry in the parallel table will be 0.
9645
9646 The pool of section numbers begins immediately following the hash table
9647 (at offset 16 + 12 * M from the beginning of the section). The pool of
9648 section numbers consists of an array of 32-bit words (using the byte order
9649 of the application binary). Each item in the array is indexed starting
9650 from 0. The hash table entry provides the index of the first section
9651 number in the set. Additional section numbers in the set follow, and the
9652 set is terminated by a 0 entry (section number 0 is not used in ELF).
9653
9654 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9655 section must be the first entry in the set, and the .debug_abbrev.dwo must
9656 be the second entry. Other members of the set may follow in any order.
9657
9658 ---
9659
9660 DWP Version 2:
9661
9662 DWP Version 2 combines all the .debug_info, etc. sections into one,
9663 and the entries in the index tables are now offsets into these sections.
9664 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9665 section.
9666
9667 Index Section Contents:
9668 Header
9669 Hash Table of Signatures dwp_hash_table.hash_table
9670 Parallel Table of Indices dwp_hash_table.unit_table
9671 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9672 Table of Section Sizes dwp_hash_table.v2.sizes
9673
9674 The index section header consists of:
9675
9676 V, 32 bit version number
9677 L, 32 bit number of columns in the table of section offsets
9678 N, 32 bit number of compilation units or type units in the index
9679 M, 32 bit number of slots in the hash table
9680
9681 Numbers are recorded using the byte order of the application binary.
9682
9683 The hash table has the same format as version 1.
9684 The parallel table of indices has the same format as version 1,
9685 except that the entries are origin-1 indices into the table of sections
9686 offsets and the table of section sizes.
9687
9688 The table of offsets begins immediately following the parallel table
9689 (at offset 16 + 12 * M from the beginning of the section). The table is
9690 a two-dimensional array of 32-bit words (using the byte order of the
9691 application binary), with L columns and N+1 rows, in row-major order.
9692 Each row in the array is indexed starting from 0. The first row provides
9693 a key to the remaining rows: each column in this row provides an identifier
9694 for a debug section, and the offsets in the same column of subsequent rows
9695 refer to that section. The section identifiers are:
9696
9697 DW_SECT_INFO 1 .debug_info.dwo
9698 DW_SECT_TYPES 2 .debug_types.dwo
9699 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9700 DW_SECT_LINE 4 .debug_line.dwo
9701 DW_SECT_LOC 5 .debug_loc.dwo
9702 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9703 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9704 DW_SECT_MACRO 8 .debug_macro.dwo
9705
9706 The offsets provided by the CU and TU index sections are the base offsets
9707 for the contributions made by each CU or TU to the corresponding section
9708 in the package file. Each CU and TU header contains an abbrev_offset
9709 field, used to find the abbreviations table for that CU or TU within the
9710 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9711 be interpreted as relative to the base offset given in the index section.
9712 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9713 should be interpreted as relative to the base offset for .debug_line.dwo,
9714 and offsets into other debug sections obtained from DWARF attributes should
9715 also be interpreted as relative to the corresponding base offset.
9716
9717 The table of sizes begins immediately following the table of offsets.
9718 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9719 with L columns and N rows, in row-major order. Each row in the array is
9720 indexed starting from 1 (row 0 is shared by the two tables).
9721
9722 ---
9723
9724 Hash table lookup is handled the same in version 1 and 2:
9725
9726 We assume that N and M will not exceed 2^32 - 1.
9727 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9728
9729 Given a 64-bit compilation unit signature or a type signature S, an entry
9730 in the hash table is located as follows:
9731
9732 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9733 the low-order k bits all set to 1.
9734
9735 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9736
9737 3) If the hash table entry at index H matches the signature, use that
9738 entry. If the hash table entry at index H is unused (all zeroes),
9739 terminate the search: the signature is not present in the table.
9740
9741 4) Let H = (H + H') modulo M. Repeat at Step 3.
9742
9743 Because M > N and H' and M are relatively prime, the search is guaranteed
9744 to stop at an unused slot or find the match. */
9745
9746 /* Create a hash table to map DWO IDs to their CU/TU entry in
9747 .debug_{info,types}.dwo in DWP_FILE.
9748 Returns NULL if there isn't one.
9749 Note: This function processes DWP files only, not DWO files. */
9750
9751 static struct dwp_hash_table *
9752 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9753 {
9754 struct objfile *objfile = dwarf2_per_objfile->objfile;
9755 bfd *dbfd = dwp_file->dbfd;
9756 const gdb_byte *index_ptr, *index_end;
9757 struct dwarf2_section_info *index;
9758 uint32_t version, nr_columns, nr_units, nr_slots;
9759 struct dwp_hash_table *htab;
9760
9761 if (is_debug_types)
9762 index = &dwp_file->sections.tu_index;
9763 else
9764 index = &dwp_file->sections.cu_index;
9765
9766 if (dwarf2_section_empty_p (index))
9767 return NULL;
9768 dwarf2_read_section (objfile, index);
9769
9770 index_ptr = index->buffer;
9771 index_end = index_ptr + index->size;
9772
9773 version = read_4_bytes (dbfd, index_ptr);
9774 index_ptr += 4;
9775 if (version == 2)
9776 nr_columns = read_4_bytes (dbfd, index_ptr);
9777 else
9778 nr_columns = 0;
9779 index_ptr += 4;
9780 nr_units = read_4_bytes (dbfd, index_ptr);
9781 index_ptr += 4;
9782 nr_slots = read_4_bytes (dbfd, index_ptr);
9783 index_ptr += 4;
9784
9785 if (version != 1 && version != 2)
9786 {
9787 error (_("Dwarf Error: unsupported DWP file version (%s)"
9788 " [in module %s]"),
9789 pulongest (version), dwp_file->name);
9790 }
9791 if (nr_slots != (nr_slots & -nr_slots))
9792 {
9793 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9794 " is not power of 2 [in module %s]"),
9795 pulongest (nr_slots), dwp_file->name);
9796 }
9797
9798 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9799 htab->version = version;
9800 htab->nr_columns = nr_columns;
9801 htab->nr_units = nr_units;
9802 htab->nr_slots = nr_slots;
9803 htab->hash_table = index_ptr;
9804 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9805
9806 /* Exit early if the table is empty. */
9807 if (nr_slots == 0 || nr_units == 0
9808 || (version == 2 && nr_columns == 0))
9809 {
9810 /* All must be zero. */
9811 if (nr_slots != 0 || nr_units != 0
9812 || (version == 2 && nr_columns != 0))
9813 {
9814 complaint (&symfile_complaints,
9815 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9816 " all zero [in modules %s]"),
9817 dwp_file->name);
9818 }
9819 return htab;
9820 }
9821
9822 if (version == 1)
9823 {
9824 htab->section_pool.v1.indices =
9825 htab->unit_table + sizeof (uint32_t) * nr_slots;
9826 /* It's harder to decide whether the section is too small in v1.
9827 V1 is deprecated anyway so we punt. */
9828 }
9829 else
9830 {
9831 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9832 int *ids = htab->section_pool.v2.section_ids;
9833 /* Reverse map for error checking. */
9834 int ids_seen[DW_SECT_MAX + 1];
9835 int i;
9836
9837 if (nr_columns < 2)
9838 {
9839 error (_("Dwarf Error: bad DWP hash table, too few columns"
9840 " in section table [in module %s]"),
9841 dwp_file->name);
9842 }
9843 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9844 {
9845 error (_("Dwarf Error: bad DWP hash table, too many columns"
9846 " in section table [in module %s]"),
9847 dwp_file->name);
9848 }
9849 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9850 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9851 for (i = 0; i < nr_columns; ++i)
9852 {
9853 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9854
9855 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9856 {
9857 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9858 " in section table [in module %s]"),
9859 id, dwp_file->name);
9860 }
9861 if (ids_seen[id] != -1)
9862 {
9863 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9864 " id %d in section table [in module %s]"),
9865 id, dwp_file->name);
9866 }
9867 ids_seen[id] = i;
9868 ids[i] = id;
9869 }
9870 /* Must have exactly one info or types section. */
9871 if (((ids_seen[DW_SECT_INFO] != -1)
9872 + (ids_seen[DW_SECT_TYPES] != -1))
9873 != 1)
9874 {
9875 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9876 " DWO info/types section [in module %s]"),
9877 dwp_file->name);
9878 }
9879 /* Must have an abbrev section. */
9880 if (ids_seen[DW_SECT_ABBREV] == -1)
9881 {
9882 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9883 " section [in module %s]"),
9884 dwp_file->name);
9885 }
9886 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9887 htab->section_pool.v2.sizes =
9888 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9889 * nr_units * nr_columns);
9890 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9891 * nr_units * nr_columns))
9892 > index_end)
9893 {
9894 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9895 " [in module %s]"),
9896 dwp_file->name);
9897 }
9898 }
9899
9900 return htab;
9901 }
9902
9903 /* Update SECTIONS with the data from SECTP.
9904
9905 This function is like the other "locate" section routines that are
9906 passed to bfd_map_over_sections, but in this context the sections to
9907 read comes from the DWP V1 hash table, not the full ELF section table.
9908
9909 The result is non-zero for success, or zero if an error was found. */
9910
9911 static int
9912 locate_v1_virtual_dwo_sections (asection *sectp,
9913 struct virtual_v1_dwo_sections *sections)
9914 {
9915 const struct dwop_section_names *names = &dwop_section_names;
9916
9917 if (section_is_p (sectp->name, &names->abbrev_dwo))
9918 {
9919 /* There can be only one. */
9920 if (sections->abbrev.s.section != NULL)
9921 return 0;
9922 sections->abbrev.s.section = sectp;
9923 sections->abbrev.size = bfd_get_section_size (sectp);
9924 }
9925 else if (section_is_p (sectp->name, &names->info_dwo)
9926 || section_is_p (sectp->name, &names->types_dwo))
9927 {
9928 /* There can be only one. */
9929 if (sections->info_or_types.s.section != NULL)
9930 return 0;
9931 sections->info_or_types.s.section = sectp;
9932 sections->info_or_types.size = bfd_get_section_size (sectp);
9933 }
9934 else if (section_is_p (sectp->name, &names->line_dwo))
9935 {
9936 /* There can be only one. */
9937 if (sections->line.s.section != NULL)
9938 return 0;
9939 sections->line.s.section = sectp;
9940 sections->line.size = bfd_get_section_size (sectp);
9941 }
9942 else if (section_is_p (sectp->name, &names->loc_dwo))
9943 {
9944 /* There can be only one. */
9945 if (sections->loc.s.section != NULL)
9946 return 0;
9947 sections->loc.s.section = sectp;
9948 sections->loc.size = bfd_get_section_size (sectp);
9949 }
9950 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9951 {
9952 /* There can be only one. */
9953 if (sections->macinfo.s.section != NULL)
9954 return 0;
9955 sections->macinfo.s.section = sectp;
9956 sections->macinfo.size = bfd_get_section_size (sectp);
9957 }
9958 else if (section_is_p (sectp->name, &names->macro_dwo))
9959 {
9960 /* There can be only one. */
9961 if (sections->macro.s.section != NULL)
9962 return 0;
9963 sections->macro.s.section = sectp;
9964 sections->macro.size = bfd_get_section_size (sectp);
9965 }
9966 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9967 {
9968 /* There can be only one. */
9969 if (sections->str_offsets.s.section != NULL)
9970 return 0;
9971 sections->str_offsets.s.section = sectp;
9972 sections->str_offsets.size = bfd_get_section_size (sectp);
9973 }
9974 else
9975 {
9976 /* No other kind of section is valid. */
9977 return 0;
9978 }
9979
9980 return 1;
9981 }
9982
9983 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
9984 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
9985 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
9986 This is for DWP version 1 files. */
9987
9988 static struct dwo_unit *
9989 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
9990 uint32_t unit_index,
9991 const char *comp_dir,
9992 ULONGEST signature, int is_debug_types)
9993 {
9994 struct objfile *objfile = dwarf2_per_objfile->objfile;
9995 const struct dwp_hash_table *dwp_htab =
9996 is_debug_types ? dwp_file->tus : dwp_file->cus;
9997 bfd *dbfd = dwp_file->dbfd;
9998 const char *kind = is_debug_types ? "TU" : "CU";
9999 struct dwo_file *dwo_file;
10000 struct dwo_unit *dwo_unit;
10001 struct virtual_v1_dwo_sections sections;
10002 void **dwo_file_slot;
10003 char *virtual_dwo_name;
10004 struct cleanup *cleanups;
10005 int i;
10006
10007 gdb_assert (dwp_file->version == 1);
10008
10009 if (dwarf_read_debug)
10010 {
10011 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10012 kind,
10013 pulongest (unit_index), hex_string (signature),
10014 dwp_file->name);
10015 }
10016
10017 /* Fetch the sections of this DWO unit.
10018 Put a limit on the number of sections we look for so that bad data
10019 doesn't cause us to loop forever. */
10020
10021 #define MAX_NR_V1_DWO_SECTIONS \
10022 (1 /* .debug_info or .debug_types */ \
10023 + 1 /* .debug_abbrev */ \
10024 + 1 /* .debug_line */ \
10025 + 1 /* .debug_loc */ \
10026 + 1 /* .debug_str_offsets */ \
10027 + 1 /* .debug_macro or .debug_macinfo */ \
10028 + 1 /* trailing zero */)
10029
10030 memset (&sections, 0, sizeof (sections));
10031 cleanups = make_cleanup (null_cleanup, 0);
10032
10033 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10034 {
10035 asection *sectp;
10036 uint32_t section_nr =
10037 read_4_bytes (dbfd,
10038 dwp_htab->section_pool.v1.indices
10039 + (unit_index + i) * sizeof (uint32_t));
10040
10041 if (section_nr == 0)
10042 break;
10043 if (section_nr >= dwp_file->num_sections)
10044 {
10045 error (_("Dwarf Error: bad DWP hash table, section number too large"
10046 " [in module %s]"),
10047 dwp_file->name);
10048 }
10049
10050 sectp = dwp_file->elf_sections[section_nr];
10051 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10052 {
10053 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10054 " [in module %s]"),
10055 dwp_file->name);
10056 }
10057 }
10058
10059 if (i < 2
10060 || dwarf2_section_empty_p (&sections.info_or_types)
10061 || dwarf2_section_empty_p (&sections.abbrev))
10062 {
10063 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10064 " [in module %s]"),
10065 dwp_file->name);
10066 }
10067 if (i == MAX_NR_V1_DWO_SECTIONS)
10068 {
10069 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10070 " [in module %s]"),
10071 dwp_file->name);
10072 }
10073
10074 /* It's easier for the rest of the code if we fake a struct dwo_file and
10075 have dwo_unit "live" in that. At least for now.
10076
10077 The DWP file can be made up of a random collection of CUs and TUs.
10078 However, for each CU + set of TUs that came from the same original DWO
10079 file, we can combine them back into a virtual DWO file to save space
10080 (fewer struct dwo_file objects to allocate). Remember that for really
10081 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10082
10083 virtual_dwo_name =
10084 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10085 get_section_id (&sections.abbrev),
10086 get_section_id (&sections.line),
10087 get_section_id (&sections.loc),
10088 get_section_id (&sections.str_offsets));
10089 make_cleanup (xfree, virtual_dwo_name);
10090 /* Can we use an existing virtual DWO file? */
10091 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10092 /* Create one if necessary. */
10093 if (*dwo_file_slot == NULL)
10094 {
10095 if (dwarf_read_debug)
10096 {
10097 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10098 virtual_dwo_name);
10099 }
10100 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10101 dwo_file->dwo_name
10102 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10103 virtual_dwo_name,
10104 strlen (virtual_dwo_name));
10105 dwo_file->comp_dir = comp_dir;
10106 dwo_file->sections.abbrev = sections.abbrev;
10107 dwo_file->sections.line = sections.line;
10108 dwo_file->sections.loc = sections.loc;
10109 dwo_file->sections.macinfo = sections.macinfo;
10110 dwo_file->sections.macro = sections.macro;
10111 dwo_file->sections.str_offsets = sections.str_offsets;
10112 /* The "str" section is global to the entire DWP file. */
10113 dwo_file->sections.str = dwp_file->sections.str;
10114 /* The info or types section is assigned below to dwo_unit,
10115 there's no need to record it in dwo_file.
10116 Also, we can't simply record type sections in dwo_file because
10117 we record a pointer into the vector in dwo_unit. As we collect more
10118 types we'll grow the vector and eventually have to reallocate space
10119 for it, invalidating all copies of pointers into the previous
10120 contents. */
10121 *dwo_file_slot = dwo_file;
10122 }
10123 else
10124 {
10125 if (dwarf_read_debug)
10126 {
10127 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10128 virtual_dwo_name);
10129 }
10130 dwo_file = (struct dwo_file *) *dwo_file_slot;
10131 }
10132 do_cleanups (cleanups);
10133
10134 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10135 dwo_unit->dwo_file = dwo_file;
10136 dwo_unit->signature = signature;
10137 dwo_unit->section =
10138 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10139 *dwo_unit->section = sections.info_or_types;
10140 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10141
10142 return dwo_unit;
10143 }
10144
10145 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10146 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10147 piece within that section used by a TU/CU, return a virtual section
10148 of just that piece. */
10149
10150 static struct dwarf2_section_info
10151 create_dwp_v2_section (struct dwarf2_section_info *section,
10152 bfd_size_type offset, bfd_size_type size)
10153 {
10154 struct dwarf2_section_info result;
10155 asection *sectp;
10156
10157 gdb_assert (section != NULL);
10158 gdb_assert (!section->is_virtual);
10159
10160 memset (&result, 0, sizeof (result));
10161 result.s.containing_section = section;
10162 result.is_virtual = 1;
10163
10164 if (size == 0)
10165 return result;
10166
10167 sectp = get_section_bfd_section (section);
10168
10169 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10170 bounds of the real section. This is a pretty-rare event, so just
10171 flag an error (easier) instead of a warning and trying to cope. */
10172 if (sectp == NULL
10173 || offset + size > bfd_get_section_size (sectp))
10174 {
10175 bfd *abfd = sectp->owner;
10176
10177 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10178 " in section %s [in module %s]"),
10179 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10180 objfile_name (dwarf2_per_objfile->objfile));
10181 }
10182
10183 result.virtual_offset = offset;
10184 result.size = size;
10185 return result;
10186 }
10187
10188 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10189 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10190 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10191 This is for DWP version 2 files. */
10192
10193 static struct dwo_unit *
10194 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10195 uint32_t unit_index,
10196 const char *comp_dir,
10197 ULONGEST signature, int is_debug_types)
10198 {
10199 struct objfile *objfile = dwarf2_per_objfile->objfile;
10200 const struct dwp_hash_table *dwp_htab =
10201 is_debug_types ? dwp_file->tus : dwp_file->cus;
10202 bfd *dbfd = dwp_file->dbfd;
10203 const char *kind = is_debug_types ? "TU" : "CU";
10204 struct dwo_file *dwo_file;
10205 struct dwo_unit *dwo_unit;
10206 struct virtual_v2_dwo_sections sections;
10207 void **dwo_file_slot;
10208 char *virtual_dwo_name;
10209 struct cleanup *cleanups;
10210 int i;
10211
10212 gdb_assert (dwp_file->version == 2);
10213
10214 if (dwarf_read_debug)
10215 {
10216 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10217 kind,
10218 pulongest (unit_index), hex_string (signature),
10219 dwp_file->name);
10220 }
10221
10222 /* Fetch the section offsets of this DWO unit. */
10223
10224 memset (&sections, 0, sizeof (sections));
10225 cleanups = make_cleanup (null_cleanup, 0);
10226
10227 for (i = 0; i < dwp_htab->nr_columns; ++i)
10228 {
10229 uint32_t offset = read_4_bytes (dbfd,
10230 dwp_htab->section_pool.v2.offsets
10231 + (((unit_index - 1) * dwp_htab->nr_columns
10232 + i)
10233 * sizeof (uint32_t)));
10234 uint32_t size = read_4_bytes (dbfd,
10235 dwp_htab->section_pool.v2.sizes
10236 + (((unit_index - 1) * dwp_htab->nr_columns
10237 + i)
10238 * sizeof (uint32_t)));
10239
10240 switch (dwp_htab->section_pool.v2.section_ids[i])
10241 {
10242 case DW_SECT_INFO:
10243 case DW_SECT_TYPES:
10244 sections.info_or_types_offset = offset;
10245 sections.info_or_types_size = size;
10246 break;
10247 case DW_SECT_ABBREV:
10248 sections.abbrev_offset = offset;
10249 sections.abbrev_size = size;
10250 break;
10251 case DW_SECT_LINE:
10252 sections.line_offset = offset;
10253 sections.line_size = size;
10254 break;
10255 case DW_SECT_LOC:
10256 sections.loc_offset = offset;
10257 sections.loc_size = size;
10258 break;
10259 case DW_SECT_STR_OFFSETS:
10260 sections.str_offsets_offset = offset;
10261 sections.str_offsets_size = size;
10262 break;
10263 case DW_SECT_MACINFO:
10264 sections.macinfo_offset = offset;
10265 sections.macinfo_size = size;
10266 break;
10267 case DW_SECT_MACRO:
10268 sections.macro_offset = offset;
10269 sections.macro_size = size;
10270 break;
10271 }
10272 }
10273
10274 /* It's easier for the rest of the code if we fake a struct dwo_file and
10275 have dwo_unit "live" in that. At least for now.
10276
10277 The DWP file can be made up of a random collection of CUs and TUs.
10278 However, for each CU + set of TUs that came from the same original DWO
10279 file, we can combine them back into a virtual DWO file to save space
10280 (fewer struct dwo_file objects to allocate). Remember that for really
10281 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10282
10283 virtual_dwo_name =
10284 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10285 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10286 (long) (sections.line_size ? sections.line_offset : 0),
10287 (long) (sections.loc_size ? sections.loc_offset : 0),
10288 (long) (sections.str_offsets_size
10289 ? sections.str_offsets_offset : 0));
10290 make_cleanup (xfree, virtual_dwo_name);
10291 /* Can we use an existing virtual DWO file? */
10292 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10293 /* Create one if necessary. */
10294 if (*dwo_file_slot == NULL)
10295 {
10296 if (dwarf_read_debug)
10297 {
10298 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10299 virtual_dwo_name);
10300 }
10301 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10302 dwo_file->dwo_name
10303 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10304 virtual_dwo_name,
10305 strlen (virtual_dwo_name));
10306 dwo_file->comp_dir = comp_dir;
10307 dwo_file->sections.abbrev =
10308 create_dwp_v2_section (&dwp_file->sections.abbrev,
10309 sections.abbrev_offset, sections.abbrev_size);
10310 dwo_file->sections.line =
10311 create_dwp_v2_section (&dwp_file->sections.line,
10312 sections.line_offset, sections.line_size);
10313 dwo_file->sections.loc =
10314 create_dwp_v2_section (&dwp_file->sections.loc,
10315 sections.loc_offset, sections.loc_size);
10316 dwo_file->sections.macinfo =
10317 create_dwp_v2_section (&dwp_file->sections.macinfo,
10318 sections.macinfo_offset, sections.macinfo_size);
10319 dwo_file->sections.macro =
10320 create_dwp_v2_section (&dwp_file->sections.macro,
10321 sections.macro_offset, sections.macro_size);
10322 dwo_file->sections.str_offsets =
10323 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10324 sections.str_offsets_offset,
10325 sections.str_offsets_size);
10326 /* The "str" section is global to the entire DWP file. */
10327 dwo_file->sections.str = dwp_file->sections.str;
10328 /* The info or types section is assigned below to dwo_unit,
10329 there's no need to record it in dwo_file.
10330 Also, we can't simply record type sections in dwo_file because
10331 we record a pointer into the vector in dwo_unit. As we collect more
10332 types we'll grow the vector and eventually have to reallocate space
10333 for it, invalidating all copies of pointers into the previous
10334 contents. */
10335 *dwo_file_slot = dwo_file;
10336 }
10337 else
10338 {
10339 if (dwarf_read_debug)
10340 {
10341 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10342 virtual_dwo_name);
10343 }
10344 dwo_file = (struct dwo_file *) *dwo_file_slot;
10345 }
10346 do_cleanups (cleanups);
10347
10348 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10349 dwo_unit->dwo_file = dwo_file;
10350 dwo_unit->signature = signature;
10351 dwo_unit->section =
10352 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10353 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10354 ? &dwp_file->sections.types
10355 : &dwp_file->sections.info,
10356 sections.info_or_types_offset,
10357 sections.info_or_types_size);
10358 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10359
10360 return dwo_unit;
10361 }
10362
10363 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10364 Returns NULL if the signature isn't found. */
10365
10366 static struct dwo_unit *
10367 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10368 ULONGEST signature, int is_debug_types)
10369 {
10370 const struct dwp_hash_table *dwp_htab =
10371 is_debug_types ? dwp_file->tus : dwp_file->cus;
10372 bfd *dbfd = dwp_file->dbfd;
10373 uint32_t mask = dwp_htab->nr_slots - 1;
10374 uint32_t hash = signature & mask;
10375 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10376 unsigned int i;
10377 void **slot;
10378 struct dwo_unit find_dwo_cu;
10379
10380 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10381 find_dwo_cu.signature = signature;
10382 slot = htab_find_slot (is_debug_types
10383 ? dwp_file->loaded_tus
10384 : dwp_file->loaded_cus,
10385 &find_dwo_cu, INSERT);
10386
10387 if (*slot != NULL)
10388 return (struct dwo_unit *) *slot;
10389
10390 /* Use a for loop so that we don't loop forever on bad debug info. */
10391 for (i = 0; i < dwp_htab->nr_slots; ++i)
10392 {
10393 ULONGEST signature_in_table;
10394
10395 signature_in_table =
10396 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10397 if (signature_in_table == signature)
10398 {
10399 uint32_t unit_index =
10400 read_4_bytes (dbfd,
10401 dwp_htab->unit_table + hash * sizeof (uint32_t));
10402
10403 if (dwp_file->version == 1)
10404 {
10405 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10406 comp_dir, signature,
10407 is_debug_types);
10408 }
10409 else
10410 {
10411 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10412 comp_dir, signature,
10413 is_debug_types);
10414 }
10415 return (struct dwo_unit *) *slot;
10416 }
10417 if (signature_in_table == 0)
10418 return NULL;
10419 hash = (hash + hash2) & mask;
10420 }
10421
10422 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10423 " [in module %s]"),
10424 dwp_file->name);
10425 }
10426
10427 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10428 Open the file specified by FILE_NAME and hand it off to BFD for
10429 preliminary analysis. Return a newly initialized bfd *, which
10430 includes a canonicalized copy of FILE_NAME.
10431 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10432 SEARCH_CWD is true if the current directory is to be searched.
10433 It will be searched before debug-file-directory.
10434 If successful, the file is added to the bfd include table of the
10435 objfile's bfd (see gdb_bfd_record_inclusion).
10436 If unable to find/open the file, return NULL.
10437 NOTE: This function is derived from symfile_bfd_open. */
10438
10439 static gdb_bfd_ref_ptr
10440 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10441 {
10442 int desc, flags;
10443 char *absolute_name;
10444 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10445 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10446 to debug_file_directory. */
10447 char *search_path;
10448 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10449
10450 if (search_cwd)
10451 {
10452 if (*debug_file_directory != '\0')
10453 search_path = concat (".", dirname_separator_string,
10454 debug_file_directory, (char *) NULL);
10455 else
10456 search_path = xstrdup (".");
10457 }
10458 else
10459 search_path = xstrdup (debug_file_directory);
10460
10461 flags = OPF_RETURN_REALPATH;
10462 if (is_dwp)
10463 flags |= OPF_SEARCH_IN_PATH;
10464 desc = openp (search_path, flags, file_name,
10465 O_RDONLY | O_BINARY, &absolute_name);
10466 xfree (search_path);
10467 if (desc < 0)
10468 return NULL;
10469
10470 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10471 xfree (absolute_name);
10472 if (sym_bfd == NULL)
10473 return NULL;
10474 bfd_set_cacheable (sym_bfd.get (), 1);
10475
10476 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10477 return NULL;
10478
10479 /* Success. Record the bfd as having been included by the objfile's bfd.
10480 This is important because things like demangled_names_hash lives in the
10481 objfile's per_bfd space and may have references to things like symbol
10482 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10483 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10484
10485 return sym_bfd;
10486 }
10487
10488 /* Try to open DWO file FILE_NAME.
10489 COMP_DIR is the DW_AT_comp_dir attribute.
10490 The result is the bfd handle of the file.
10491 If there is a problem finding or opening the file, return NULL.
10492 Upon success, the canonicalized path of the file is stored in the bfd,
10493 same as symfile_bfd_open. */
10494
10495 static gdb_bfd_ref_ptr
10496 open_dwo_file (const char *file_name, const char *comp_dir)
10497 {
10498 if (IS_ABSOLUTE_PATH (file_name))
10499 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10500
10501 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10502
10503 if (comp_dir != NULL)
10504 {
10505 char *path_to_try = concat (comp_dir, SLASH_STRING,
10506 file_name, (char *) NULL);
10507
10508 /* NOTE: If comp_dir is a relative path, this will also try the
10509 search path, which seems useful. */
10510 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10511 1 /*search_cwd*/));
10512 xfree (path_to_try);
10513 if (abfd != NULL)
10514 return abfd;
10515 }
10516
10517 /* That didn't work, try debug-file-directory, which, despite its name,
10518 is a list of paths. */
10519
10520 if (*debug_file_directory == '\0')
10521 return NULL;
10522
10523 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10524 }
10525
10526 /* This function is mapped across the sections and remembers the offset and
10527 size of each of the DWO debugging sections we are interested in. */
10528
10529 static void
10530 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10531 {
10532 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10533 const struct dwop_section_names *names = &dwop_section_names;
10534
10535 if (section_is_p (sectp->name, &names->abbrev_dwo))
10536 {
10537 dwo_sections->abbrev.s.section = sectp;
10538 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10539 }
10540 else if (section_is_p (sectp->name, &names->info_dwo))
10541 {
10542 dwo_sections->info.s.section = sectp;
10543 dwo_sections->info.size = bfd_get_section_size (sectp);
10544 }
10545 else if (section_is_p (sectp->name, &names->line_dwo))
10546 {
10547 dwo_sections->line.s.section = sectp;
10548 dwo_sections->line.size = bfd_get_section_size (sectp);
10549 }
10550 else if (section_is_p (sectp->name, &names->loc_dwo))
10551 {
10552 dwo_sections->loc.s.section = sectp;
10553 dwo_sections->loc.size = bfd_get_section_size (sectp);
10554 }
10555 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10556 {
10557 dwo_sections->macinfo.s.section = sectp;
10558 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10559 }
10560 else if (section_is_p (sectp->name, &names->macro_dwo))
10561 {
10562 dwo_sections->macro.s.section = sectp;
10563 dwo_sections->macro.size = bfd_get_section_size (sectp);
10564 }
10565 else if (section_is_p (sectp->name, &names->str_dwo))
10566 {
10567 dwo_sections->str.s.section = sectp;
10568 dwo_sections->str.size = bfd_get_section_size (sectp);
10569 }
10570 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10571 {
10572 dwo_sections->str_offsets.s.section = sectp;
10573 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10574 }
10575 else if (section_is_p (sectp->name, &names->types_dwo))
10576 {
10577 struct dwarf2_section_info type_section;
10578
10579 memset (&type_section, 0, sizeof (type_section));
10580 type_section.s.section = sectp;
10581 type_section.size = bfd_get_section_size (sectp);
10582 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10583 &type_section);
10584 }
10585 }
10586
10587 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10588 by PER_CU. This is for the non-DWP case.
10589 The result is NULL if DWO_NAME can't be found. */
10590
10591 static struct dwo_file *
10592 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10593 const char *dwo_name, const char *comp_dir)
10594 {
10595 struct objfile *objfile = dwarf2_per_objfile->objfile;
10596 struct dwo_file *dwo_file;
10597 struct cleanup *cleanups;
10598
10599 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10600 if (dbfd == NULL)
10601 {
10602 if (dwarf_read_debug)
10603 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10604 return NULL;
10605 }
10606 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10607 dwo_file->dwo_name = dwo_name;
10608 dwo_file->comp_dir = comp_dir;
10609 dwo_file->dbfd = dbfd.release ();
10610
10611 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10612
10613 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10614 &dwo_file->sections);
10615
10616 dwo_file->cu = create_dwo_cu (dwo_file);
10617
10618 dwo_file->tus = create_debug_types_hash_table (dwo_file,
10619 dwo_file->sections.types);
10620
10621 discard_cleanups (cleanups);
10622
10623 if (dwarf_read_debug)
10624 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10625
10626 return dwo_file;
10627 }
10628
10629 /* This function is mapped across the sections and remembers the offset and
10630 size of each of the DWP debugging sections common to version 1 and 2 that
10631 we are interested in. */
10632
10633 static void
10634 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10635 void *dwp_file_ptr)
10636 {
10637 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10638 const struct dwop_section_names *names = &dwop_section_names;
10639 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10640
10641 /* Record the ELF section number for later lookup: this is what the
10642 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10643 gdb_assert (elf_section_nr < dwp_file->num_sections);
10644 dwp_file->elf_sections[elf_section_nr] = sectp;
10645
10646 /* Look for specific sections that we need. */
10647 if (section_is_p (sectp->name, &names->str_dwo))
10648 {
10649 dwp_file->sections.str.s.section = sectp;
10650 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10651 }
10652 else if (section_is_p (sectp->name, &names->cu_index))
10653 {
10654 dwp_file->sections.cu_index.s.section = sectp;
10655 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10656 }
10657 else if (section_is_p (sectp->name, &names->tu_index))
10658 {
10659 dwp_file->sections.tu_index.s.section = sectp;
10660 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10661 }
10662 }
10663
10664 /* This function is mapped across the sections and remembers the offset and
10665 size of each of the DWP version 2 debugging sections that we are interested
10666 in. This is split into a separate function because we don't know if we
10667 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10668
10669 static void
10670 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10671 {
10672 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10673 const struct dwop_section_names *names = &dwop_section_names;
10674 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10675
10676 /* Record the ELF section number for later lookup: this is what the
10677 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10678 gdb_assert (elf_section_nr < dwp_file->num_sections);
10679 dwp_file->elf_sections[elf_section_nr] = sectp;
10680
10681 /* Look for specific sections that we need. */
10682 if (section_is_p (sectp->name, &names->abbrev_dwo))
10683 {
10684 dwp_file->sections.abbrev.s.section = sectp;
10685 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10686 }
10687 else if (section_is_p (sectp->name, &names->info_dwo))
10688 {
10689 dwp_file->sections.info.s.section = sectp;
10690 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10691 }
10692 else if (section_is_p (sectp->name, &names->line_dwo))
10693 {
10694 dwp_file->sections.line.s.section = sectp;
10695 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10696 }
10697 else if (section_is_p (sectp->name, &names->loc_dwo))
10698 {
10699 dwp_file->sections.loc.s.section = sectp;
10700 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10701 }
10702 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10703 {
10704 dwp_file->sections.macinfo.s.section = sectp;
10705 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10706 }
10707 else if (section_is_p (sectp->name, &names->macro_dwo))
10708 {
10709 dwp_file->sections.macro.s.section = sectp;
10710 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10711 }
10712 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10713 {
10714 dwp_file->sections.str_offsets.s.section = sectp;
10715 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10716 }
10717 else if (section_is_p (sectp->name, &names->types_dwo))
10718 {
10719 dwp_file->sections.types.s.section = sectp;
10720 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10721 }
10722 }
10723
10724 /* Hash function for dwp_file loaded CUs/TUs. */
10725
10726 static hashval_t
10727 hash_dwp_loaded_cutus (const void *item)
10728 {
10729 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10730
10731 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10732 return dwo_unit->signature;
10733 }
10734
10735 /* Equality function for dwp_file loaded CUs/TUs. */
10736
10737 static int
10738 eq_dwp_loaded_cutus (const void *a, const void *b)
10739 {
10740 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10741 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10742
10743 return dua->signature == dub->signature;
10744 }
10745
10746 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10747
10748 static htab_t
10749 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10750 {
10751 return htab_create_alloc_ex (3,
10752 hash_dwp_loaded_cutus,
10753 eq_dwp_loaded_cutus,
10754 NULL,
10755 &objfile->objfile_obstack,
10756 hashtab_obstack_allocate,
10757 dummy_obstack_deallocate);
10758 }
10759
10760 /* Try to open DWP file FILE_NAME.
10761 The result is the bfd handle of the file.
10762 If there is a problem finding or opening the file, return NULL.
10763 Upon success, the canonicalized path of the file is stored in the bfd,
10764 same as symfile_bfd_open. */
10765
10766 static gdb_bfd_ref_ptr
10767 open_dwp_file (const char *file_name)
10768 {
10769 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10770 1 /*search_cwd*/));
10771 if (abfd != NULL)
10772 return abfd;
10773
10774 /* Work around upstream bug 15652.
10775 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10776 [Whether that's a "bug" is debatable, but it is getting in our way.]
10777 We have no real idea where the dwp file is, because gdb's realpath-ing
10778 of the executable's path may have discarded the needed info.
10779 [IWBN if the dwp file name was recorded in the executable, akin to
10780 .gnu_debuglink, but that doesn't exist yet.]
10781 Strip the directory from FILE_NAME and search again. */
10782 if (*debug_file_directory != '\0')
10783 {
10784 /* Don't implicitly search the current directory here.
10785 If the user wants to search "." to handle this case,
10786 it must be added to debug-file-directory. */
10787 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10788 0 /*search_cwd*/);
10789 }
10790
10791 return NULL;
10792 }
10793
10794 /* Initialize the use of the DWP file for the current objfile.
10795 By convention the name of the DWP file is ${objfile}.dwp.
10796 The result is NULL if it can't be found. */
10797
10798 static struct dwp_file *
10799 open_and_init_dwp_file (void)
10800 {
10801 struct objfile *objfile = dwarf2_per_objfile->objfile;
10802 struct dwp_file *dwp_file;
10803 char *dwp_name;
10804 struct cleanup *cleanups = make_cleanup (null_cleanup, 0);
10805
10806 /* Try to find first .dwp for the binary file before any symbolic links
10807 resolving. */
10808
10809 /* If the objfile is a debug file, find the name of the real binary
10810 file and get the name of dwp file from there. */
10811 if (objfile->separate_debug_objfile_backlink != NULL)
10812 {
10813 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10814 const char *backlink_basename = lbasename (backlink->original_name);
10815 char *debug_dirname = ldirname (objfile->original_name);
10816
10817 make_cleanup (xfree, debug_dirname);
10818 dwp_name = xstrprintf ("%s%s%s.dwp", debug_dirname,
10819 SLASH_STRING, backlink_basename);
10820 }
10821 else
10822 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10823 make_cleanup (xfree, dwp_name);
10824
10825 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name));
10826 if (dbfd == NULL
10827 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10828 {
10829 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10830 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10831 make_cleanup (xfree, dwp_name);
10832 dbfd = open_dwp_file (dwp_name);
10833 }
10834
10835 if (dbfd == NULL)
10836 {
10837 if (dwarf_read_debug)
10838 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10839 do_cleanups (cleanups);
10840 return NULL;
10841 }
10842 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10843 dwp_file->name = bfd_get_filename (dbfd.get ());
10844 dwp_file->dbfd = dbfd.release ();
10845 do_cleanups (cleanups);
10846
10847 /* +1: section 0 is unused */
10848 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
10849 dwp_file->elf_sections =
10850 OBSTACK_CALLOC (&objfile->objfile_obstack,
10851 dwp_file->num_sections, asection *);
10852
10853 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
10854 dwp_file);
10855
10856 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10857
10858 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10859
10860 /* The DWP file version is stored in the hash table. Oh well. */
10861 if (dwp_file->cus->version != dwp_file->tus->version)
10862 {
10863 /* Technically speaking, we should try to limp along, but this is
10864 pretty bizarre. We use pulongest here because that's the established
10865 portability solution (e.g, we cannot use %u for uint32_t). */
10866 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10867 " TU version %s [in DWP file %s]"),
10868 pulongest (dwp_file->cus->version),
10869 pulongest (dwp_file->tus->version), dwp_name);
10870 }
10871 dwp_file->version = dwp_file->cus->version;
10872
10873 if (dwp_file->version == 2)
10874 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
10875 dwp_file);
10876
10877 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10878 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
10879
10880 if (dwarf_read_debug)
10881 {
10882 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10883 fprintf_unfiltered (gdb_stdlog,
10884 " %s CUs, %s TUs\n",
10885 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10886 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
10887 }
10888
10889 return dwp_file;
10890 }
10891
10892 /* Wrapper around open_and_init_dwp_file, only open it once. */
10893
10894 static struct dwp_file *
10895 get_dwp_file (void)
10896 {
10897 if (! dwarf2_per_objfile->dwp_checked)
10898 {
10899 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10900 dwarf2_per_objfile->dwp_checked = 1;
10901 }
10902 return dwarf2_per_objfile->dwp_file;
10903 }
10904
10905 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10906 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10907 or in the DWP file for the objfile, referenced by THIS_UNIT.
10908 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
10909 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10910
10911 This is called, for example, when wanting to read a variable with a
10912 complex location. Therefore we don't want to do file i/o for every call.
10913 Therefore we don't want to look for a DWO file on every call.
10914 Therefore we first see if we've already seen SIGNATURE in a DWP file,
10915 then we check if we've already seen DWO_NAME, and only THEN do we check
10916 for a DWO file.
10917
10918 The result is a pointer to the dwo_unit object or NULL if we didn't find it
10919 (dwo_id mismatch or couldn't find the DWO/DWP file). */
10920
10921 static struct dwo_unit *
10922 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
10923 const char *dwo_name, const char *comp_dir,
10924 ULONGEST signature, int is_debug_types)
10925 {
10926 struct objfile *objfile = dwarf2_per_objfile->objfile;
10927 const char *kind = is_debug_types ? "TU" : "CU";
10928 void **dwo_file_slot;
10929 struct dwo_file *dwo_file;
10930 struct dwp_file *dwp_file;
10931
10932 /* First see if there's a DWP file.
10933 If we have a DWP file but didn't find the DWO inside it, don't
10934 look for the original DWO file. It makes gdb behave differently
10935 depending on whether one is debugging in the build tree. */
10936
10937 dwp_file = get_dwp_file ();
10938 if (dwp_file != NULL)
10939 {
10940 const struct dwp_hash_table *dwp_htab =
10941 is_debug_types ? dwp_file->tus : dwp_file->cus;
10942
10943 if (dwp_htab != NULL)
10944 {
10945 struct dwo_unit *dwo_cutu =
10946 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
10947 signature, is_debug_types);
10948
10949 if (dwo_cutu != NULL)
10950 {
10951 if (dwarf_read_debug)
10952 {
10953 fprintf_unfiltered (gdb_stdlog,
10954 "Virtual DWO %s %s found: @%s\n",
10955 kind, hex_string (signature),
10956 host_address_to_string (dwo_cutu));
10957 }
10958 return dwo_cutu;
10959 }
10960 }
10961 }
10962 else
10963 {
10964 /* No DWP file, look for the DWO file. */
10965
10966 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
10967 if (*dwo_file_slot == NULL)
10968 {
10969 /* Read in the file and build a table of the CUs/TUs it contains. */
10970 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
10971 }
10972 /* NOTE: This will be NULL if unable to open the file. */
10973 dwo_file = (struct dwo_file *) *dwo_file_slot;
10974
10975 if (dwo_file != NULL)
10976 {
10977 struct dwo_unit *dwo_cutu = NULL;
10978
10979 if (is_debug_types && dwo_file->tus)
10980 {
10981 struct dwo_unit find_dwo_cutu;
10982
10983 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
10984 find_dwo_cutu.signature = signature;
10985 dwo_cutu
10986 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
10987 }
10988 else if (!is_debug_types && dwo_file->cu)
10989 {
10990 if (signature == dwo_file->cu->signature)
10991 dwo_cutu = dwo_file->cu;
10992 }
10993
10994 if (dwo_cutu != NULL)
10995 {
10996 if (dwarf_read_debug)
10997 {
10998 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
10999 kind, dwo_name, hex_string (signature),
11000 host_address_to_string (dwo_cutu));
11001 }
11002 return dwo_cutu;
11003 }
11004 }
11005 }
11006
11007 /* We didn't find it. This could mean a dwo_id mismatch, or
11008 someone deleted the DWO/DWP file, or the search path isn't set up
11009 correctly to find the file. */
11010
11011 if (dwarf_read_debug)
11012 {
11013 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11014 kind, dwo_name, hex_string (signature));
11015 }
11016
11017 /* This is a warning and not a complaint because it can be caused by
11018 pilot error (e.g., user accidentally deleting the DWO). */
11019 {
11020 /* Print the name of the DWP file if we looked there, helps the user
11021 better diagnose the problem. */
11022 char *dwp_text = NULL;
11023 struct cleanup *cleanups;
11024
11025 if (dwp_file != NULL)
11026 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11027 cleanups = make_cleanup (xfree, dwp_text);
11028
11029 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11030 " [in module %s]"),
11031 kind, dwo_name, hex_string (signature),
11032 dwp_text != NULL ? dwp_text : "",
11033 this_unit->is_debug_types ? "TU" : "CU",
11034 this_unit->offset.sect_off, objfile_name (objfile));
11035
11036 do_cleanups (cleanups);
11037 }
11038 return NULL;
11039 }
11040
11041 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11042 See lookup_dwo_cutu_unit for details. */
11043
11044 static struct dwo_unit *
11045 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11046 const char *dwo_name, const char *comp_dir,
11047 ULONGEST signature)
11048 {
11049 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11050 }
11051
11052 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11053 See lookup_dwo_cutu_unit for details. */
11054
11055 static struct dwo_unit *
11056 lookup_dwo_type_unit (struct signatured_type *this_tu,
11057 const char *dwo_name, const char *comp_dir)
11058 {
11059 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11060 }
11061
11062 /* Traversal function for queue_and_load_all_dwo_tus. */
11063
11064 static int
11065 queue_and_load_dwo_tu (void **slot, void *info)
11066 {
11067 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11068 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11069 ULONGEST signature = dwo_unit->signature;
11070 struct signatured_type *sig_type =
11071 lookup_dwo_signatured_type (per_cu->cu, signature);
11072
11073 if (sig_type != NULL)
11074 {
11075 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11076
11077 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11078 a real dependency of PER_CU on SIG_TYPE. That is detected later
11079 while processing PER_CU. */
11080 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11081 load_full_type_unit (sig_cu);
11082 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11083 }
11084
11085 return 1;
11086 }
11087
11088 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11089 The DWO may have the only definition of the type, though it may not be
11090 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11091 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11092
11093 static void
11094 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11095 {
11096 struct dwo_unit *dwo_unit;
11097 struct dwo_file *dwo_file;
11098
11099 gdb_assert (!per_cu->is_debug_types);
11100 gdb_assert (get_dwp_file () == NULL);
11101 gdb_assert (per_cu->cu != NULL);
11102
11103 dwo_unit = per_cu->cu->dwo_unit;
11104 gdb_assert (dwo_unit != NULL);
11105
11106 dwo_file = dwo_unit->dwo_file;
11107 if (dwo_file->tus != NULL)
11108 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11109 }
11110
11111 /* Free all resources associated with DWO_FILE.
11112 Close the DWO file and munmap the sections.
11113 All memory should be on the objfile obstack. */
11114
11115 static void
11116 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11117 {
11118
11119 /* Note: dbfd is NULL for virtual DWO files. */
11120 gdb_bfd_unref (dwo_file->dbfd);
11121
11122 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11123 }
11124
11125 /* Wrapper for free_dwo_file for use in cleanups. */
11126
11127 static void
11128 free_dwo_file_cleanup (void *arg)
11129 {
11130 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11131 struct objfile *objfile = dwarf2_per_objfile->objfile;
11132
11133 free_dwo_file (dwo_file, objfile);
11134 }
11135
11136 /* Traversal function for free_dwo_files. */
11137
11138 static int
11139 free_dwo_file_from_slot (void **slot, void *info)
11140 {
11141 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11142 struct objfile *objfile = (struct objfile *) info;
11143
11144 free_dwo_file (dwo_file, objfile);
11145
11146 return 1;
11147 }
11148
11149 /* Free all resources associated with DWO_FILES. */
11150
11151 static void
11152 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11153 {
11154 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11155 }
11156 \f
11157 /* Read in various DIEs. */
11158
11159 /* qsort helper for inherit_abstract_dies. */
11160
11161 static int
11162 unsigned_int_compar (const void *ap, const void *bp)
11163 {
11164 unsigned int a = *(unsigned int *) ap;
11165 unsigned int b = *(unsigned int *) bp;
11166
11167 return (a > b) - (b > a);
11168 }
11169
11170 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11171 Inherit only the children of the DW_AT_abstract_origin DIE not being
11172 already referenced by DW_AT_abstract_origin from the children of the
11173 current DIE. */
11174
11175 static void
11176 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11177 {
11178 struct die_info *child_die;
11179 unsigned die_children_count;
11180 /* CU offsets which were referenced by children of the current DIE. */
11181 sect_offset *offsets;
11182 sect_offset *offsets_end, *offsetp;
11183 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11184 struct die_info *origin_die;
11185 /* Iterator of the ORIGIN_DIE children. */
11186 struct die_info *origin_child_die;
11187 struct cleanup *cleanups;
11188 struct attribute *attr;
11189 struct dwarf2_cu *origin_cu;
11190 struct pending **origin_previous_list_in_scope;
11191
11192 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11193 if (!attr)
11194 return;
11195
11196 /* Note that following die references may follow to a die in a
11197 different cu. */
11198
11199 origin_cu = cu;
11200 origin_die = follow_die_ref (die, attr, &origin_cu);
11201
11202 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11203 symbols in. */
11204 origin_previous_list_in_scope = origin_cu->list_in_scope;
11205 origin_cu->list_in_scope = cu->list_in_scope;
11206
11207 if (die->tag != origin_die->tag
11208 && !(die->tag == DW_TAG_inlined_subroutine
11209 && origin_die->tag == DW_TAG_subprogram))
11210 complaint (&symfile_complaints,
11211 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11212 die->offset.sect_off, origin_die->offset.sect_off);
11213
11214 child_die = die->child;
11215 die_children_count = 0;
11216 while (child_die && child_die->tag)
11217 {
11218 child_die = sibling_die (child_die);
11219 die_children_count++;
11220 }
11221 offsets = XNEWVEC (sect_offset, die_children_count);
11222 cleanups = make_cleanup (xfree, offsets);
11223
11224 offsets_end = offsets;
11225 for (child_die = die->child;
11226 child_die && child_die->tag;
11227 child_die = sibling_die (child_die))
11228 {
11229 struct die_info *child_origin_die;
11230 struct dwarf2_cu *child_origin_cu;
11231
11232 /* We are trying to process concrete instance entries:
11233 DW_TAG_GNU_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11234 it's not relevant to our analysis here. i.e. detecting DIEs that are
11235 present in the abstract instance but not referenced in the concrete
11236 one. */
11237 if (child_die->tag == DW_TAG_GNU_call_site)
11238 continue;
11239
11240 /* For each CHILD_DIE, find the corresponding child of
11241 ORIGIN_DIE. If there is more than one layer of
11242 DW_AT_abstract_origin, follow them all; there shouldn't be,
11243 but GCC versions at least through 4.4 generate this (GCC PR
11244 40573). */
11245 child_origin_die = child_die;
11246 child_origin_cu = cu;
11247 while (1)
11248 {
11249 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11250 child_origin_cu);
11251 if (attr == NULL)
11252 break;
11253 child_origin_die = follow_die_ref (child_origin_die, attr,
11254 &child_origin_cu);
11255 }
11256
11257 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11258 counterpart may exist. */
11259 if (child_origin_die != child_die)
11260 {
11261 if (child_die->tag != child_origin_die->tag
11262 && !(child_die->tag == DW_TAG_inlined_subroutine
11263 && child_origin_die->tag == DW_TAG_subprogram))
11264 complaint (&symfile_complaints,
11265 _("Child DIE 0x%x and its abstract origin 0x%x have "
11266 "different tags"), child_die->offset.sect_off,
11267 child_origin_die->offset.sect_off);
11268 if (child_origin_die->parent != origin_die)
11269 complaint (&symfile_complaints,
11270 _("Child DIE 0x%x and its abstract origin 0x%x have "
11271 "different parents"), child_die->offset.sect_off,
11272 child_origin_die->offset.sect_off);
11273 else
11274 *offsets_end++ = child_origin_die->offset;
11275 }
11276 }
11277 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11278 unsigned_int_compar);
11279 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11280 if (offsetp[-1].sect_off == offsetp->sect_off)
11281 complaint (&symfile_complaints,
11282 _("Multiple children of DIE 0x%x refer "
11283 "to DIE 0x%x as their abstract origin"),
11284 die->offset.sect_off, offsetp->sect_off);
11285
11286 offsetp = offsets;
11287 origin_child_die = origin_die->child;
11288 while (origin_child_die && origin_child_die->tag)
11289 {
11290 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11291 while (offsetp < offsets_end
11292 && offsetp->sect_off < origin_child_die->offset.sect_off)
11293 offsetp++;
11294 if (offsetp >= offsets_end
11295 || offsetp->sect_off > origin_child_die->offset.sect_off)
11296 {
11297 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11298 Check whether we're already processing ORIGIN_CHILD_DIE.
11299 This can happen with mutually referenced abstract_origins.
11300 PR 16581. */
11301 if (!origin_child_die->in_process)
11302 process_die (origin_child_die, origin_cu);
11303 }
11304 origin_child_die = sibling_die (origin_child_die);
11305 }
11306 origin_cu->list_in_scope = origin_previous_list_in_scope;
11307
11308 do_cleanups (cleanups);
11309 }
11310
11311 static void
11312 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11313 {
11314 struct objfile *objfile = cu->objfile;
11315 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11316 struct context_stack *newobj;
11317 CORE_ADDR lowpc;
11318 CORE_ADDR highpc;
11319 struct die_info *child_die;
11320 struct attribute *attr, *call_line, *call_file;
11321 const char *name;
11322 CORE_ADDR baseaddr;
11323 struct block *block;
11324 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11325 VEC (symbolp) *template_args = NULL;
11326 struct template_symbol *templ_func = NULL;
11327
11328 if (inlined_func)
11329 {
11330 /* If we do not have call site information, we can't show the
11331 caller of this inlined function. That's too confusing, so
11332 only use the scope for local variables. */
11333 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11334 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11335 if (call_line == NULL || call_file == NULL)
11336 {
11337 read_lexical_block_scope (die, cu);
11338 return;
11339 }
11340 }
11341
11342 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11343
11344 name = dwarf2_name (die, cu);
11345
11346 /* Ignore functions with missing or empty names. These are actually
11347 illegal according to the DWARF standard. */
11348 if (name == NULL)
11349 {
11350 complaint (&symfile_complaints,
11351 _("missing name for subprogram DIE at %d"),
11352 die->offset.sect_off);
11353 return;
11354 }
11355
11356 /* Ignore functions with missing or invalid low and high pc attributes. */
11357 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11358 <= PC_BOUNDS_INVALID)
11359 {
11360 attr = dwarf2_attr (die, DW_AT_external, cu);
11361 if (!attr || !DW_UNSND (attr))
11362 complaint (&symfile_complaints,
11363 _("cannot get low and high bounds "
11364 "for subprogram DIE at %d"),
11365 die->offset.sect_off);
11366 return;
11367 }
11368
11369 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11370 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11371
11372 /* If we have any template arguments, then we must allocate a
11373 different sort of symbol. */
11374 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11375 {
11376 if (child_die->tag == DW_TAG_template_type_param
11377 || child_die->tag == DW_TAG_template_value_param)
11378 {
11379 templ_func = allocate_template_symbol (objfile);
11380 templ_func->base.is_cplus_template_function = 1;
11381 break;
11382 }
11383 }
11384
11385 newobj = push_context (0, lowpc);
11386 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11387 (struct symbol *) templ_func);
11388
11389 /* If there is a location expression for DW_AT_frame_base, record
11390 it. */
11391 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11392 if (attr)
11393 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11394
11395 /* If there is a location for the static link, record it. */
11396 newobj->static_link = NULL;
11397 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11398 if (attr)
11399 {
11400 newobj->static_link
11401 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11402 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11403 }
11404
11405 cu->list_in_scope = &local_symbols;
11406
11407 if (die->child != NULL)
11408 {
11409 child_die = die->child;
11410 while (child_die && child_die->tag)
11411 {
11412 if (child_die->tag == DW_TAG_template_type_param
11413 || child_die->tag == DW_TAG_template_value_param)
11414 {
11415 struct symbol *arg = new_symbol (child_die, NULL, cu);
11416
11417 if (arg != NULL)
11418 VEC_safe_push (symbolp, template_args, arg);
11419 }
11420 else
11421 process_die (child_die, cu);
11422 child_die = sibling_die (child_die);
11423 }
11424 }
11425
11426 inherit_abstract_dies (die, cu);
11427
11428 /* If we have a DW_AT_specification, we might need to import using
11429 directives from the context of the specification DIE. See the
11430 comment in determine_prefix. */
11431 if (cu->language == language_cplus
11432 && dwarf2_attr (die, DW_AT_specification, cu))
11433 {
11434 struct dwarf2_cu *spec_cu = cu;
11435 struct die_info *spec_die = die_specification (die, &spec_cu);
11436
11437 while (spec_die)
11438 {
11439 child_die = spec_die->child;
11440 while (child_die && child_die->tag)
11441 {
11442 if (child_die->tag == DW_TAG_imported_module)
11443 process_die (child_die, spec_cu);
11444 child_die = sibling_die (child_die);
11445 }
11446
11447 /* In some cases, GCC generates specification DIEs that
11448 themselves contain DW_AT_specification attributes. */
11449 spec_die = die_specification (spec_die, &spec_cu);
11450 }
11451 }
11452
11453 newobj = pop_context ();
11454 /* Make a block for the local symbols within. */
11455 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11456 newobj->static_link, lowpc, highpc);
11457
11458 /* For C++, set the block's scope. */
11459 if ((cu->language == language_cplus
11460 || cu->language == language_fortran
11461 || cu->language == language_d
11462 || cu->language == language_rust)
11463 && cu->processing_has_namespace_info)
11464 block_set_scope (block, determine_prefix (die, cu),
11465 &objfile->objfile_obstack);
11466
11467 /* If we have address ranges, record them. */
11468 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11469
11470 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11471
11472 /* Attach template arguments to function. */
11473 if (! VEC_empty (symbolp, template_args))
11474 {
11475 gdb_assert (templ_func != NULL);
11476
11477 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11478 templ_func->template_arguments
11479 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11480 templ_func->n_template_arguments);
11481 memcpy (templ_func->template_arguments,
11482 VEC_address (symbolp, template_args),
11483 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11484 VEC_free (symbolp, template_args);
11485 }
11486
11487 /* In C++, we can have functions nested inside functions (e.g., when
11488 a function declares a class that has methods). This means that
11489 when we finish processing a function scope, we may need to go
11490 back to building a containing block's symbol lists. */
11491 local_symbols = newobj->locals;
11492 local_using_directives = newobj->local_using_directives;
11493
11494 /* If we've finished processing a top-level function, subsequent
11495 symbols go in the file symbol list. */
11496 if (outermost_context_p ())
11497 cu->list_in_scope = &file_symbols;
11498 }
11499
11500 /* Process all the DIES contained within a lexical block scope. Start
11501 a new scope, process the dies, and then close the scope. */
11502
11503 static void
11504 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11505 {
11506 struct objfile *objfile = cu->objfile;
11507 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11508 struct context_stack *newobj;
11509 CORE_ADDR lowpc, highpc;
11510 struct die_info *child_die;
11511 CORE_ADDR baseaddr;
11512
11513 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11514
11515 /* Ignore blocks with missing or invalid low and high pc attributes. */
11516 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11517 as multiple lexical blocks? Handling children in a sane way would
11518 be nasty. Might be easier to properly extend generic blocks to
11519 describe ranges. */
11520 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11521 {
11522 case PC_BOUNDS_NOT_PRESENT:
11523 /* DW_TAG_lexical_block has no attributes, process its children as if
11524 there was no wrapping by that DW_TAG_lexical_block.
11525 GCC does no longer produces such DWARF since GCC r224161. */
11526 for (child_die = die->child;
11527 child_die != NULL && child_die->tag;
11528 child_die = sibling_die (child_die))
11529 process_die (child_die, cu);
11530 return;
11531 case PC_BOUNDS_INVALID:
11532 return;
11533 }
11534 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11535 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11536
11537 push_context (0, lowpc);
11538 if (die->child != NULL)
11539 {
11540 child_die = die->child;
11541 while (child_die && child_die->tag)
11542 {
11543 process_die (child_die, cu);
11544 child_die = sibling_die (child_die);
11545 }
11546 }
11547 inherit_abstract_dies (die, cu);
11548 newobj = pop_context ();
11549
11550 if (local_symbols != NULL || local_using_directives != NULL)
11551 {
11552 struct block *block
11553 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11554 newobj->start_addr, highpc);
11555
11556 /* Note that recording ranges after traversing children, as we
11557 do here, means that recording a parent's ranges entails
11558 walking across all its children's ranges as they appear in
11559 the address map, which is quadratic behavior.
11560
11561 It would be nicer to record the parent's ranges before
11562 traversing its children, simply overriding whatever you find
11563 there. But since we don't even decide whether to create a
11564 block until after we've traversed its children, that's hard
11565 to do. */
11566 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11567 }
11568 local_symbols = newobj->locals;
11569 local_using_directives = newobj->local_using_directives;
11570 }
11571
11572 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
11573
11574 static void
11575 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11576 {
11577 struct objfile *objfile = cu->objfile;
11578 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11579 CORE_ADDR pc, baseaddr;
11580 struct attribute *attr;
11581 struct call_site *call_site, call_site_local;
11582 void **slot;
11583 int nparams;
11584 struct die_info *child_die;
11585
11586 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11587
11588 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11589 if (!attr)
11590 {
11591 complaint (&symfile_complaints,
11592 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
11593 "DIE 0x%x [in module %s]"),
11594 die->offset.sect_off, objfile_name (objfile));
11595 return;
11596 }
11597 pc = attr_value_as_address (attr) + baseaddr;
11598 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11599
11600 if (cu->call_site_htab == NULL)
11601 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11602 NULL, &objfile->objfile_obstack,
11603 hashtab_obstack_allocate, NULL);
11604 call_site_local.pc = pc;
11605 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11606 if (*slot != NULL)
11607 {
11608 complaint (&symfile_complaints,
11609 _("Duplicate PC %s for DW_TAG_GNU_call_site "
11610 "DIE 0x%x [in module %s]"),
11611 paddress (gdbarch, pc), die->offset.sect_off,
11612 objfile_name (objfile));
11613 return;
11614 }
11615
11616 /* Count parameters at the caller. */
11617
11618 nparams = 0;
11619 for (child_die = die->child; child_die && child_die->tag;
11620 child_die = sibling_die (child_die))
11621 {
11622 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11623 {
11624 complaint (&symfile_complaints,
11625 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
11626 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11627 child_die->tag, child_die->offset.sect_off,
11628 objfile_name (objfile));
11629 continue;
11630 }
11631
11632 nparams++;
11633 }
11634
11635 call_site
11636 = ((struct call_site *)
11637 obstack_alloc (&objfile->objfile_obstack,
11638 sizeof (*call_site)
11639 + (sizeof (*call_site->parameter) * (nparams - 1))));
11640 *slot = call_site;
11641 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11642 call_site->pc = pc;
11643
11644 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11645 {
11646 struct die_info *func_die;
11647
11648 /* Skip also over DW_TAG_inlined_subroutine. */
11649 for (func_die = die->parent;
11650 func_die && func_die->tag != DW_TAG_subprogram
11651 && func_die->tag != DW_TAG_subroutine_type;
11652 func_die = func_die->parent);
11653
11654 /* DW_AT_GNU_all_call_sites is a superset
11655 of DW_AT_GNU_all_tail_call_sites. */
11656 if (func_die
11657 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11658 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11659 {
11660 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11661 not complete. But keep CALL_SITE for look ups via call_site_htab,
11662 both the initial caller containing the real return address PC and
11663 the final callee containing the current PC of a chain of tail
11664 calls do not need to have the tail call list complete. But any
11665 function candidate for a virtual tail call frame searched via
11666 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11667 determined unambiguously. */
11668 }
11669 else
11670 {
11671 struct type *func_type = NULL;
11672
11673 if (func_die)
11674 func_type = get_die_type (func_die, cu);
11675 if (func_type != NULL)
11676 {
11677 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11678
11679 /* Enlist this call site to the function. */
11680 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11681 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11682 }
11683 else
11684 complaint (&symfile_complaints,
11685 _("Cannot find function owning DW_TAG_GNU_call_site "
11686 "DIE 0x%x [in module %s]"),
11687 die->offset.sect_off, objfile_name (objfile));
11688 }
11689 }
11690
11691 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11692 if (attr == NULL)
11693 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11694 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11695 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11696 /* Keep NULL DWARF_BLOCK. */;
11697 else if (attr_form_is_block (attr))
11698 {
11699 struct dwarf2_locexpr_baton *dlbaton;
11700
11701 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11702 dlbaton->data = DW_BLOCK (attr)->data;
11703 dlbaton->size = DW_BLOCK (attr)->size;
11704 dlbaton->per_cu = cu->per_cu;
11705
11706 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11707 }
11708 else if (attr_form_is_ref (attr))
11709 {
11710 struct dwarf2_cu *target_cu = cu;
11711 struct die_info *target_die;
11712
11713 target_die = follow_die_ref (die, attr, &target_cu);
11714 gdb_assert (target_cu->objfile == objfile);
11715 if (die_is_declaration (target_die, target_cu))
11716 {
11717 const char *target_physname;
11718
11719 /* Prefer the mangled name; otherwise compute the demangled one. */
11720 target_physname = dwarf2_string_attr (target_die,
11721 DW_AT_linkage_name,
11722 target_cu);
11723 if (target_physname == NULL)
11724 target_physname = dwarf2_string_attr (target_die,
11725 DW_AT_MIPS_linkage_name,
11726 target_cu);
11727 if (target_physname == NULL)
11728 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11729 if (target_physname == NULL)
11730 complaint (&symfile_complaints,
11731 _("DW_AT_GNU_call_site_target target DIE has invalid "
11732 "physname, for referencing DIE 0x%x [in module %s]"),
11733 die->offset.sect_off, objfile_name (objfile));
11734 else
11735 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11736 }
11737 else
11738 {
11739 CORE_ADDR lowpc;
11740
11741 /* DW_AT_entry_pc should be preferred. */
11742 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11743 <= PC_BOUNDS_INVALID)
11744 complaint (&symfile_complaints,
11745 _("DW_AT_GNU_call_site_target target DIE has invalid "
11746 "low pc, for referencing DIE 0x%x [in module %s]"),
11747 die->offset.sect_off, objfile_name (objfile));
11748 else
11749 {
11750 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11751 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11752 }
11753 }
11754 }
11755 else
11756 complaint (&symfile_complaints,
11757 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
11758 "block nor reference, for DIE 0x%x [in module %s]"),
11759 die->offset.sect_off, objfile_name (objfile));
11760
11761 call_site->per_cu = cu->per_cu;
11762
11763 for (child_die = die->child;
11764 child_die && child_die->tag;
11765 child_die = sibling_die (child_die))
11766 {
11767 struct call_site_parameter *parameter;
11768 struct attribute *loc, *origin;
11769
11770 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11771 {
11772 /* Already printed the complaint above. */
11773 continue;
11774 }
11775
11776 gdb_assert (call_site->parameter_count < nparams);
11777 parameter = &call_site->parameter[call_site->parameter_count];
11778
11779 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11780 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11781 register is contained in DW_AT_GNU_call_site_value. */
11782
11783 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11784 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11785 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11786 {
11787 sect_offset offset;
11788
11789 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11790 offset = dwarf2_get_ref_die_offset (origin);
11791 if (!offset_in_cu_p (&cu->header, offset))
11792 {
11793 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11794 binding can be done only inside one CU. Such referenced DIE
11795 therefore cannot be even moved to DW_TAG_partial_unit. */
11796 complaint (&symfile_complaints,
11797 _("DW_AT_abstract_origin offset is not in CU for "
11798 "DW_TAG_GNU_call_site child DIE 0x%x "
11799 "[in module %s]"),
11800 child_die->offset.sect_off, objfile_name (objfile));
11801 continue;
11802 }
11803 parameter->u.param_offset.cu_off = (offset.sect_off
11804 - cu->header.offset.sect_off);
11805 }
11806 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11807 {
11808 complaint (&symfile_complaints,
11809 _("No DW_FORM_block* DW_AT_location for "
11810 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11811 child_die->offset.sect_off, objfile_name (objfile));
11812 continue;
11813 }
11814 else
11815 {
11816 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11817 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11818 if (parameter->u.dwarf_reg != -1)
11819 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11820 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11821 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11822 &parameter->u.fb_offset))
11823 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11824 else
11825 {
11826 complaint (&symfile_complaints,
11827 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11828 "for DW_FORM_block* DW_AT_location is supported for "
11829 "DW_TAG_GNU_call_site child DIE 0x%x "
11830 "[in module %s]"),
11831 child_die->offset.sect_off, objfile_name (objfile));
11832 continue;
11833 }
11834 }
11835
11836 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11837 if (!attr_form_is_block (attr))
11838 {
11839 complaint (&symfile_complaints,
11840 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
11841 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11842 child_die->offset.sect_off, objfile_name (objfile));
11843 continue;
11844 }
11845 parameter->value = DW_BLOCK (attr)->data;
11846 parameter->value_size = DW_BLOCK (attr)->size;
11847
11848 /* Parameters are not pre-cleared by memset above. */
11849 parameter->data_value = NULL;
11850 parameter->data_value_size = 0;
11851 call_site->parameter_count++;
11852
11853 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11854 if (attr)
11855 {
11856 if (!attr_form_is_block (attr))
11857 complaint (&symfile_complaints,
11858 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
11859 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11860 child_die->offset.sect_off, objfile_name (objfile));
11861 else
11862 {
11863 parameter->data_value = DW_BLOCK (attr)->data;
11864 parameter->data_value_size = DW_BLOCK (attr)->size;
11865 }
11866 }
11867 }
11868 }
11869
11870 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
11871 Return 1 if the attributes are present and valid, otherwise, return 0.
11872 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
11873
11874 static int
11875 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
11876 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11877 struct partial_symtab *ranges_pst)
11878 {
11879 struct objfile *objfile = cu->objfile;
11880 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11881 struct comp_unit_head *cu_header = &cu->header;
11882 bfd *obfd = objfile->obfd;
11883 unsigned int addr_size = cu_header->addr_size;
11884 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11885 /* Base address selection entry. */
11886 CORE_ADDR base;
11887 int found_base;
11888 unsigned int dummy;
11889 const gdb_byte *buffer;
11890 int low_set;
11891 CORE_ADDR low = 0;
11892 CORE_ADDR high = 0;
11893 CORE_ADDR baseaddr;
11894
11895 found_base = cu->base_known;
11896 base = cu->base_address;
11897
11898 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
11899 if (offset >= dwarf2_per_objfile->ranges.size)
11900 {
11901 complaint (&symfile_complaints,
11902 _("Offset %d out of bounds for DW_AT_ranges attribute"),
11903 offset);
11904 return 0;
11905 }
11906 buffer = dwarf2_per_objfile->ranges.buffer + offset;
11907
11908 low_set = 0;
11909
11910 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11911
11912 while (1)
11913 {
11914 CORE_ADDR range_beginning, range_end;
11915
11916 range_beginning = read_address (obfd, buffer, cu, &dummy);
11917 buffer += addr_size;
11918 range_end = read_address (obfd, buffer, cu, &dummy);
11919 buffer += addr_size;
11920 offset += 2 * addr_size;
11921
11922 /* An end of list marker is a pair of zero addresses. */
11923 if (range_beginning == 0 && range_end == 0)
11924 /* Found the end of list entry. */
11925 break;
11926
11927 /* Each base address selection entry is a pair of 2 values.
11928 The first is the largest possible address, the second is
11929 the base address. Check for a base address here. */
11930 if ((range_beginning & mask) == mask)
11931 {
11932 /* If we found the largest possible address, then we already
11933 have the base address in range_end. */
11934 base = range_end;
11935 found_base = 1;
11936 continue;
11937 }
11938
11939 if (!found_base)
11940 {
11941 /* We have no valid base address for the ranges
11942 data. */
11943 complaint (&symfile_complaints,
11944 _("Invalid .debug_ranges data (no base address)"));
11945 return 0;
11946 }
11947
11948 if (range_beginning > range_end)
11949 {
11950 /* Inverted range entries are invalid. */
11951 complaint (&symfile_complaints,
11952 _("Invalid .debug_ranges data (inverted range)"));
11953 return 0;
11954 }
11955
11956 /* Empty range entries have no effect. */
11957 if (range_beginning == range_end)
11958 continue;
11959
11960 range_beginning += base;
11961 range_end += base;
11962
11963 /* A not-uncommon case of bad debug info.
11964 Don't pollute the addrmap with bad data. */
11965 if (range_beginning + baseaddr == 0
11966 && !dwarf2_per_objfile->has_section_at_zero)
11967 {
11968 complaint (&symfile_complaints,
11969 _(".debug_ranges entry has start address of zero"
11970 " [in module %s]"), objfile_name (objfile));
11971 continue;
11972 }
11973
11974 if (ranges_pst != NULL)
11975 {
11976 CORE_ADDR lowpc;
11977 CORE_ADDR highpc;
11978
11979 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
11980 range_beginning + baseaddr);
11981 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
11982 range_end + baseaddr);
11983 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
11984 ranges_pst);
11985 }
11986
11987 /* FIXME: This is recording everything as a low-high
11988 segment of consecutive addresses. We should have a
11989 data structure for discontiguous block ranges
11990 instead. */
11991 if (! low_set)
11992 {
11993 low = range_beginning;
11994 high = range_end;
11995 low_set = 1;
11996 }
11997 else
11998 {
11999 if (range_beginning < low)
12000 low = range_beginning;
12001 if (range_end > high)
12002 high = range_end;
12003 }
12004 }
12005
12006 if (! low_set)
12007 /* If the first entry is an end-of-list marker, the range
12008 describes an empty scope, i.e. no instructions. */
12009 return 0;
12010
12011 if (low_return)
12012 *low_return = low;
12013 if (high_return)
12014 *high_return = high;
12015 return 1;
12016 }
12017
12018 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12019 definition for the return value. *LOWPC and *HIGHPC are set iff
12020 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12021
12022 static enum pc_bounds_kind
12023 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12024 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12025 struct partial_symtab *pst)
12026 {
12027 struct attribute *attr;
12028 struct attribute *attr_high;
12029 CORE_ADDR low = 0;
12030 CORE_ADDR high = 0;
12031 enum pc_bounds_kind ret;
12032
12033 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12034 if (attr_high)
12035 {
12036 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12037 if (attr)
12038 {
12039 low = attr_value_as_address (attr);
12040 high = attr_value_as_address (attr_high);
12041 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12042 high += low;
12043 }
12044 else
12045 /* Found high w/o low attribute. */
12046 return PC_BOUNDS_INVALID;
12047
12048 /* Found consecutive range of addresses. */
12049 ret = PC_BOUNDS_HIGH_LOW;
12050 }
12051 else
12052 {
12053 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12054 if (attr != NULL)
12055 {
12056 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12057 We take advantage of the fact that DW_AT_ranges does not appear
12058 in DW_TAG_compile_unit of DWO files. */
12059 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12060 unsigned int ranges_offset = (DW_UNSND (attr)
12061 + (need_ranges_base
12062 ? cu->ranges_base
12063 : 0));
12064
12065 /* Value of the DW_AT_ranges attribute is the offset in the
12066 .debug_ranges section. */
12067 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12068 return PC_BOUNDS_INVALID;
12069 /* Found discontinuous range of addresses. */
12070 ret = PC_BOUNDS_RANGES;
12071 }
12072 else
12073 return PC_BOUNDS_NOT_PRESENT;
12074 }
12075
12076 /* read_partial_die has also the strict LOW < HIGH requirement. */
12077 if (high <= low)
12078 return PC_BOUNDS_INVALID;
12079
12080 /* When using the GNU linker, .gnu.linkonce. sections are used to
12081 eliminate duplicate copies of functions and vtables and such.
12082 The linker will arbitrarily choose one and discard the others.
12083 The AT_*_pc values for such functions refer to local labels in
12084 these sections. If the section from that file was discarded, the
12085 labels are not in the output, so the relocs get a value of 0.
12086 If this is a discarded function, mark the pc bounds as invalid,
12087 so that GDB will ignore it. */
12088 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12089 return PC_BOUNDS_INVALID;
12090
12091 *lowpc = low;
12092 if (highpc)
12093 *highpc = high;
12094 return ret;
12095 }
12096
12097 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12098 its low and high PC addresses. Do nothing if these addresses could not
12099 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12100 and HIGHPC to the high address if greater than HIGHPC. */
12101
12102 static void
12103 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12104 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12105 struct dwarf2_cu *cu)
12106 {
12107 CORE_ADDR low, high;
12108 struct die_info *child = die->child;
12109
12110 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12111 {
12112 *lowpc = std::min (*lowpc, low);
12113 *highpc = std::max (*highpc, high);
12114 }
12115
12116 /* If the language does not allow nested subprograms (either inside
12117 subprograms or lexical blocks), we're done. */
12118 if (cu->language != language_ada)
12119 return;
12120
12121 /* Check all the children of the given DIE. If it contains nested
12122 subprograms, then check their pc bounds. Likewise, we need to
12123 check lexical blocks as well, as they may also contain subprogram
12124 definitions. */
12125 while (child && child->tag)
12126 {
12127 if (child->tag == DW_TAG_subprogram
12128 || child->tag == DW_TAG_lexical_block)
12129 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12130 child = sibling_die (child);
12131 }
12132 }
12133
12134 /* Get the low and high pc's represented by the scope DIE, and store
12135 them in *LOWPC and *HIGHPC. If the correct values can't be
12136 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12137
12138 static void
12139 get_scope_pc_bounds (struct die_info *die,
12140 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12141 struct dwarf2_cu *cu)
12142 {
12143 CORE_ADDR best_low = (CORE_ADDR) -1;
12144 CORE_ADDR best_high = (CORE_ADDR) 0;
12145 CORE_ADDR current_low, current_high;
12146
12147 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
12148 >= PC_BOUNDS_RANGES)
12149 {
12150 best_low = current_low;
12151 best_high = current_high;
12152 }
12153 else
12154 {
12155 struct die_info *child = die->child;
12156
12157 while (child && child->tag)
12158 {
12159 switch (child->tag) {
12160 case DW_TAG_subprogram:
12161 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12162 break;
12163 case DW_TAG_namespace:
12164 case DW_TAG_module:
12165 /* FIXME: carlton/2004-01-16: Should we do this for
12166 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12167 that current GCC's always emit the DIEs corresponding
12168 to definitions of methods of classes as children of a
12169 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12170 the DIEs giving the declarations, which could be
12171 anywhere). But I don't see any reason why the
12172 standards says that they have to be there. */
12173 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12174
12175 if (current_low != ((CORE_ADDR) -1))
12176 {
12177 best_low = std::min (best_low, current_low);
12178 best_high = std::max (best_high, current_high);
12179 }
12180 break;
12181 default:
12182 /* Ignore. */
12183 break;
12184 }
12185
12186 child = sibling_die (child);
12187 }
12188 }
12189
12190 *lowpc = best_low;
12191 *highpc = best_high;
12192 }
12193
12194 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12195 in DIE. */
12196
12197 static void
12198 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12199 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12200 {
12201 struct objfile *objfile = cu->objfile;
12202 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12203 struct attribute *attr;
12204 struct attribute *attr_high;
12205
12206 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12207 if (attr_high)
12208 {
12209 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12210 if (attr)
12211 {
12212 CORE_ADDR low = attr_value_as_address (attr);
12213 CORE_ADDR high = attr_value_as_address (attr_high);
12214
12215 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12216 high += low;
12217
12218 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12219 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12220 record_block_range (block, low, high - 1);
12221 }
12222 }
12223
12224 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12225 if (attr)
12226 {
12227 bfd *obfd = objfile->obfd;
12228 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12229 We take advantage of the fact that DW_AT_ranges does not appear
12230 in DW_TAG_compile_unit of DWO files. */
12231 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12232
12233 /* The value of the DW_AT_ranges attribute is the offset of the
12234 address range list in the .debug_ranges section. */
12235 unsigned long offset = (DW_UNSND (attr)
12236 + (need_ranges_base ? cu->ranges_base : 0));
12237 const gdb_byte *buffer;
12238
12239 /* For some target architectures, but not others, the
12240 read_address function sign-extends the addresses it returns.
12241 To recognize base address selection entries, we need a
12242 mask. */
12243 unsigned int addr_size = cu->header.addr_size;
12244 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12245
12246 /* The base address, to which the next pair is relative. Note
12247 that this 'base' is a DWARF concept: most entries in a range
12248 list are relative, to reduce the number of relocs against the
12249 debugging information. This is separate from this function's
12250 'baseaddr' argument, which GDB uses to relocate debugging
12251 information from a shared library based on the address at
12252 which the library was loaded. */
12253 CORE_ADDR base = cu->base_address;
12254 int base_known = cu->base_known;
12255
12256 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12257 if (offset >= dwarf2_per_objfile->ranges.size)
12258 {
12259 complaint (&symfile_complaints,
12260 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
12261 offset);
12262 return;
12263 }
12264 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12265
12266 for (;;)
12267 {
12268 unsigned int bytes_read;
12269 CORE_ADDR start, end;
12270
12271 start = read_address (obfd, buffer, cu, &bytes_read);
12272 buffer += bytes_read;
12273 end = read_address (obfd, buffer, cu, &bytes_read);
12274 buffer += bytes_read;
12275
12276 /* Did we find the end of the range list? */
12277 if (start == 0 && end == 0)
12278 break;
12279
12280 /* Did we find a base address selection entry? */
12281 else if ((start & base_select_mask) == base_select_mask)
12282 {
12283 base = end;
12284 base_known = 1;
12285 }
12286
12287 /* We found an ordinary address range. */
12288 else
12289 {
12290 if (!base_known)
12291 {
12292 complaint (&symfile_complaints,
12293 _("Invalid .debug_ranges data "
12294 "(no base address)"));
12295 return;
12296 }
12297
12298 if (start > end)
12299 {
12300 /* Inverted range entries are invalid. */
12301 complaint (&symfile_complaints,
12302 _("Invalid .debug_ranges data "
12303 "(inverted range)"));
12304 return;
12305 }
12306
12307 /* Empty range entries have no effect. */
12308 if (start == end)
12309 continue;
12310
12311 start += base + baseaddr;
12312 end += base + baseaddr;
12313
12314 /* A not-uncommon case of bad debug info.
12315 Don't pollute the addrmap with bad data. */
12316 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
12317 {
12318 complaint (&symfile_complaints,
12319 _(".debug_ranges entry has start address of zero"
12320 " [in module %s]"), objfile_name (objfile));
12321 continue;
12322 }
12323
12324 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12325 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12326 record_block_range (block, start, end - 1);
12327 }
12328 }
12329 }
12330 }
12331
12332 /* Check whether the producer field indicates either of GCC < 4.6, or the
12333 Intel C/C++ compiler, and cache the result in CU. */
12334
12335 static void
12336 check_producer (struct dwarf2_cu *cu)
12337 {
12338 int major, minor;
12339
12340 if (cu->producer == NULL)
12341 {
12342 /* For unknown compilers expect their behavior is DWARF version
12343 compliant.
12344
12345 GCC started to support .debug_types sections by -gdwarf-4 since
12346 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12347 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12348 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12349 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12350 }
12351 else if (producer_is_gcc (cu->producer, &major, &minor))
12352 {
12353 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12354 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12355 }
12356 else if (startswith (cu->producer, "Intel(R) C"))
12357 cu->producer_is_icc = 1;
12358 else
12359 {
12360 /* For other non-GCC compilers, expect their behavior is DWARF version
12361 compliant. */
12362 }
12363
12364 cu->checked_producer = 1;
12365 }
12366
12367 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12368 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12369 during 4.6.0 experimental. */
12370
12371 static int
12372 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12373 {
12374 if (!cu->checked_producer)
12375 check_producer (cu);
12376
12377 return cu->producer_is_gxx_lt_4_6;
12378 }
12379
12380 /* Return the default accessibility type if it is not overriden by
12381 DW_AT_accessibility. */
12382
12383 static enum dwarf_access_attribute
12384 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12385 {
12386 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12387 {
12388 /* The default DWARF 2 accessibility for members is public, the default
12389 accessibility for inheritance is private. */
12390
12391 if (die->tag != DW_TAG_inheritance)
12392 return DW_ACCESS_public;
12393 else
12394 return DW_ACCESS_private;
12395 }
12396 else
12397 {
12398 /* DWARF 3+ defines the default accessibility a different way. The same
12399 rules apply now for DW_TAG_inheritance as for the members and it only
12400 depends on the container kind. */
12401
12402 if (die->parent->tag == DW_TAG_class_type)
12403 return DW_ACCESS_private;
12404 else
12405 return DW_ACCESS_public;
12406 }
12407 }
12408
12409 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12410 offset. If the attribute was not found return 0, otherwise return
12411 1. If it was found but could not properly be handled, set *OFFSET
12412 to 0. */
12413
12414 static int
12415 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12416 LONGEST *offset)
12417 {
12418 struct attribute *attr;
12419
12420 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12421 if (attr != NULL)
12422 {
12423 *offset = 0;
12424
12425 /* Note that we do not check for a section offset first here.
12426 This is because DW_AT_data_member_location is new in DWARF 4,
12427 so if we see it, we can assume that a constant form is really
12428 a constant and not a section offset. */
12429 if (attr_form_is_constant (attr))
12430 *offset = dwarf2_get_attr_constant_value (attr, 0);
12431 else if (attr_form_is_section_offset (attr))
12432 dwarf2_complex_location_expr_complaint ();
12433 else if (attr_form_is_block (attr))
12434 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12435 else
12436 dwarf2_complex_location_expr_complaint ();
12437
12438 return 1;
12439 }
12440
12441 return 0;
12442 }
12443
12444 /* Add an aggregate field to the field list. */
12445
12446 static void
12447 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12448 struct dwarf2_cu *cu)
12449 {
12450 struct objfile *objfile = cu->objfile;
12451 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12452 struct nextfield *new_field;
12453 struct attribute *attr;
12454 struct field *fp;
12455 const char *fieldname = "";
12456
12457 /* Allocate a new field list entry and link it in. */
12458 new_field = XNEW (struct nextfield);
12459 make_cleanup (xfree, new_field);
12460 memset (new_field, 0, sizeof (struct nextfield));
12461
12462 if (die->tag == DW_TAG_inheritance)
12463 {
12464 new_field->next = fip->baseclasses;
12465 fip->baseclasses = new_field;
12466 }
12467 else
12468 {
12469 new_field->next = fip->fields;
12470 fip->fields = new_field;
12471 }
12472 fip->nfields++;
12473
12474 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12475 if (attr)
12476 new_field->accessibility = DW_UNSND (attr);
12477 else
12478 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12479 if (new_field->accessibility != DW_ACCESS_public)
12480 fip->non_public_fields = 1;
12481
12482 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12483 if (attr)
12484 new_field->virtuality = DW_UNSND (attr);
12485 else
12486 new_field->virtuality = DW_VIRTUALITY_none;
12487
12488 fp = &new_field->field;
12489
12490 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12491 {
12492 LONGEST offset;
12493
12494 /* Data member other than a C++ static data member. */
12495
12496 /* Get type of field. */
12497 fp->type = die_type (die, cu);
12498
12499 SET_FIELD_BITPOS (*fp, 0);
12500
12501 /* Get bit size of field (zero if none). */
12502 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12503 if (attr)
12504 {
12505 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12506 }
12507 else
12508 {
12509 FIELD_BITSIZE (*fp) = 0;
12510 }
12511
12512 /* Get bit offset of field. */
12513 if (handle_data_member_location (die, cu, &offset))
12514 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12515 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12516 if (attr)
12517 {
12518 if (gdbarch_bits_big_endian (gdbarch))
12519 {
12520 /* For big endian bits, the DW_AT_bit_offset gives the
12521 additional bit offset from the MSB of the containing
12522 anonymous object to the MSB of the field. We don't
12523 have to do anything special since we don't need to
12524 know the size of the anonymous object. */
12525 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12526 }
12527 else
12528 {
12529 /* For little endian bits, compute the bit offset to the
12530 MSB of the anonymous object, subtract off the number of
12531 bits from the MSB of the field to the MSB of the
12532 object, and then subtract off the number of bits of
12533 the field itself. The result is the bit offset of
12534 the LSB of the field. */
12535 int anonymous_size;
12536 int bit_offset = DW_UNSND (attr);
12537
12538 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12539 if (attr)
12540 {
12541 /* The size of the anonymous object containing
12542 the bit field is explicit, so use the
12543 indicated size (in bytes). */
12544 anonymous_size = DW_UNSND (attr);
12545 }
12546 else
12547 {
12548 /* The size of the anonymous object containing
12549 the bit field must be inferred from the type
12550 attribute of the data member containing the
12551 bit field. */
12552 anonymous_size = TYPE_LENGTH (fp->type);
12553 }
12554 SET_FIELD_BITPOS (*fp,
12555 (FIELD_BITPOS (*fp)
12556 + anonymous_size * bits_per_byte
12557 - bit_offset - FIELD_BITSIZE (*fp)));
12558 }
12559 }
12560 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12561 if (attr != NULL)
12562 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12563 + dwarf2_get_attr_constant_value (attr, 0)));
12564
12565 /* Get name of field. */
12566 fieldname = dwarf2_name (die, cu);
12567 if (fieldname == NULL)
12568 fieldname = "";
12569
12570 /* The name is already allocated along with this objfile, so we don't
12571 need to duplicate it for the type. */
12572 fp->name = fieldname;
12573
12574 /* Change accessibility for artificial fields (e.g. virtual table
12575 pointer or virtual base class pointer) to private. */
12576 if (dwarf2_attr (die, DW_AT_artificial, cu))
12577 {
12578 FIELD_ARTIFICIAL (*fp) = 1;
12579 new_field->accessibility = DW_ACCESS_private;
12580 fip->non_public_fields = 1;
12581 }
12582 }
12583 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12584 {
12585 /* C++ static member. */
12586
12587 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12588 is a declaration, but all versions of G++ as of this writing
12589 (so through at least 3.2.1) incorrectly generate
12590 DW_TAG_variable tags. */
12591
12592 const char *physname;
12593
12594 /* Get name of field. */
12595 fieldname = dwarf2_name (die, cu);
12596 if (fieldname == NULL)
12597 return;
12598
12599 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12600 if (attr
12601 /* Only create a symbol if this is an external value.
12602 new_symbol checks this and puts the value in the global symbol
12603 table, which we want. If it is not external, new_symbol
12604 will try to put the value in cu->list_in_scope which is wrong. */
12605 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12606 {
12607 /* A static const member, not much different than an enum as far as
12608 we're concerned, except that we can support more types. */
12609 new_symbol (die, NULL, cu);
12610 }
12611
12612 /* Get physical name. */
12613 physname = dwarf2_physname (fieldname, die, cu);
12614
12615 /* The name is already allocated along with this objfile, so we don't
12616 need to duplicate it for the type. */
12617 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12618 FIELD_TYPE (*fp) = die_type (die, cu);
12619 FIELD_NAME (*fp) = fieldname;
12620 }
12621 else if (die->tag == DW_TAG_inheritance)
12622 {
12623 LONGEST offset;
12624
12625 /* C++ base class field. */
12626 if (handle_data_member_location (die, cu, &offset))
12627 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12628 FIELD_BITSIZE (*fp) = 0;
12629 FIELD_TYPE (*fp) = die_type (die, cu);
12630 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12631 fip->nbaseclasses++;
12632 }
12633 }
12634
12635 /* Add a typedef defined in the scope of the FIP's class. */
12636
12637 static void
12638 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12639 struct dwarf2_cu *cu)
12640 {
12641 struct typedef_field_list *new_field;
12642 struct typedef_field *fp;
12643
12644 /* Allocate a new field list entry and link it in. */
12645 new_field = XCNEW (struct typedef_field_list);
12646 make_cleanup (xfree, new_field);
12647
12648 gdb_assert (die->tag == DW_TAG_typedef);
12649
12650 fp = &new_field->field;
12651
12652 /* Get name of field. */
12653 fp->name = dwarf2_name (die, cu);
12654 if (fp->name == NULL)
12655 return;
12656
12657 fp->type = read_type_die (die, cu);
12658
12659 new_field->next = fip->typedef_field_list;
12660 fip->typedef_field_list = new_field;
12661 fip->typedef_field_list_count++;
12662 }
12663
12664 /* Create the vector of fields, and attach it to the type. */
12665
12666 static void
12667 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12668 struct dwarf2_cu *cu)
12669 {
12670 int nfields = fip->nfields;
12671
12672 /* Record the field count, allocate space for the array of fields,
12673 and create blank accessibility bitfields if necessary. */
12674 TYPE_NFIELDS (type) = nfields;
12675 TYPE_FIELDS (type) = (struct field *)
12676 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12677 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12678
12679 if (fip->non_public_fields && cu->language != language_ada)
12680 {
12681 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12682
12683 TYPE_FIELD_PRIVATE_BITS (type) =
12684 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12685 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12686
12687 TYPE_FIELD_PROTECTED_BITS (type) =
12688 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12689 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12690
12691 TYPE_FIELD_IGNORE_BITS (type) =
12692 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12693 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12694 }
12695
12696 /* If the type has baseclasses, allocate and clear a bit vector for
12697 TYPE_FIELD_VIRTUAL_BITS. */
12698 if (fip->nbaseclasses && cu->language != language_ada)
12699 {
12700 int num_bytes = B_BYTES (fip->nbaseclasses);
12701 unsigned char *pointer;
12702
12703 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12704 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
12705 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12706 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12707 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12708 }
12709
12710 /* Copy the saved-up fields into the field vector. Start from the head of
12711 the list, adding to the tail of the field array, so that they end up in
12712 the same order in the array in which they were added to the list. */
12713 while (nfields-- > 0)
12714 {
12715 struct nextfield *fieldp;
12716
12717 if (fip->fields)
12718 {
12719 fieldp = fip->fields;
12720 fip->fields = fieldp->next;
12721 }
12722 else
12723 {
12724 fieldp = fip->baseclasses;
12725 fip->baseclasses = fieldp->next;
12726 }
12727
12728 TYPE_FIELD (type, nfields) = fieldp->field;
12729 switch (fieldp->accessibility)
12730 {
12731 case DW_ACCESS_private:
12732 if (cu->language != language_ada)
12733 SET_TYPE_FIELD_PRIVATE (type, nfields);
12734 break;
12735
12736 case DW_ACCESS_protected:
12737 if (cu->language != language_ada)
12738 SET_TYPE_FIELD_PROTECTED (type, nfields);
12739 break;
12740
12741 case DW_ACCESS_public:
12742 break;
12743
12744 default:
12745 /* Unknown accessibility. Complain and treat it as public. */
12746 {
12747 complaint (&symfile_complaints, _("unsupported accessibility %d"),
12748 fieldp->accessibility);
12749 }
12750 break;
12751 }
12752 if (nfields < fip->nbaseclasses)
12753 {
12754 switch (fieldp->virtuality)
12755 {
12756 case DW_VIRTUALITY_virtual:
12757 case DW_VIRTUALITY_pure_virtual:
12758 if (cu->language == language_ada)
12759 error (_("unexpected virtuality in component of Ada type"));
12760 SET_TYPE_FIELD_VIRTUAL (type, nfields);
12761 break;
12762 }
12763 }
12764 }
12765 }
12766
12767 /* Return true if this member function is a constructor, false
12768 otherwise. */
12769
12770 static int
12771 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12772 {
12773 const char *fieldname;
12774 const char *type_name;
12775 int len;
12776
12777 if (die->parent == NULL)
12778 return 0;
12779
12780 if (die->parent->tag != DW_TAG_structure_type
12781 && die->parent->tag != DW_TAG_union_type
12782 && die->parent->tag != DW_TAG_class_type)
12783 return 0;
12784
12785 fieldname = dwarf2_name (die, cu);
12786 type_name = dwarf2_name (die->parent, cu);
12787 if (fieldname == NULL || type_name == NULL)
12788 return 0;
12789
12790 len = strlen (fieldname);
12791 return (strncmp (fieldname, type_name, len) == 0
12792 && (type_name[len] == '\0' || type_name[len] == '<'));
12793 }
12794
12795 /* Add a member function to the proper fieldlist. */
12796
12797 static void
12798 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
12799 struct type *type, struct dwarf2_cu *cu)
12800 {
12801 struct objfile *objfile = cu->objfile;
12802 struct attribute *attr;
12803 struct fnfieldlist *flp;
12804 int i;
12805 struct fn_field *fnp;
12806 const char *fieldname;
12807 struct nextfnfield *new_fnfield;
12808 struct type *this_type;
12809 enum dwarf_access_attribute accessibility;
12810
12811 if (cu->language == language_ada)
12812 error (_("unexpected member function in Ada type"));
12813
12814 /* Get name of member function. */
12815 fieldname = dwarf2_name (die, cu);
12816 if (fieldname == NULL)
12817 return;
12818
12819 /* Look up member function name in fieldlist. */
12820 for (i = 0; i < fip->nfnfields; i++)
12821 {
12822 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
12823 break;
12824 }
12825
12826 /* Create new list element if necessary. */
12827 if (i < fip->nfnfields)
12828 flp = &fip->fnfieldlists[i];
12829 else
12830 {
12831 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
12832 {
12833 fip->fnfieldlists = (struct fnfieldlist *)
12834 xrealloc (fip->fnfieldlists,
12835 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
12836 * sizeof (struct fnfieldlist));
12837 if (fip->nfnfields == 0)
12838 make_cleanup (free_current_contents, &fip->fnfieldlists);
12839 }
12840 flp = &fip->fnfieldlists[fip->nfnfields];
12841 flp->name = fieldname;
12842 flp->length = 0;
12843 flp->head = NULL;
12844 i = fip->nfnfields++;
12845 }
12846
12847 /* Create a new member function field and chain it to the field list
12848 entry. */
12849 new_fnfield = XNEW (struct nextfnfield);
12850 make_cleanup (xfree, new_fnfield);
12851 memset (new_fnfield, 0, sizeof (struct nextfnfield));
12852 new_fnfield->next = flp->head;
12853 flp->head = new_fnfield;
12854 flp->length++;
12855
12856 /* Fill in the member function field info. */
12857 fnp = &new_fnfield->fnfield;
12858
12859 /* Delay processing of the physname until later. */
12860 if (cu->language == language_cplus)
12861 {
12862 add_to_method_list (type, i, flp->length - 1, fieldname,
12863 die, cu);
12864 }
12865 else
12866 {
12867 const char *physname = dwarf2_physname (fieldname, die, cu);
12868 fnp->physname = physname ? physname : "";
12869 }
12870
12871 fnp->type = alloc_type (objfile);
12872 this_type = read_type_die (die, cu);
12873 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
12874 {
12875 int nparams = TYPE_NFIELDS (this_type);
12876
12877 /* TYPE is the domain of this method, and THIS_TYPE is the type
12878 of the method itself (TYPE_CODE_METHOD). */
12879 smash_to_method_type (fnp->type, type,
12880 TYPE_TARGET_TYPE (this_type),
12881 TYPE_FIELDS (this_type),
12882 TYPE_NFIELDS (this_type),
12883 TYPE_VARARGS (this_type));
12884
12885 /* Handle static member functions.
12886 Dwarf2 has no clean way to discern C++ static and non-static
12887 member functions. G++ helps GDB by marking the first
12888 parameter for non-static member functions (which is the this
12889 pointer) as artificial. We obtain this information from
12890 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12891 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
12892 fnp->voffset = VOFFSET_STATIC;
12893 }
12894 else
12895 complaint (&symfile_complaints, _("member function type missing for '%s'"),
12896 dwarf2_full_name (fieldname, die, cu));
12897
12898 /* Get fcontext from DW_AT_containing_type if present. */
12899 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12900 fnp->fcontext = die_containing_type (die, cu);
12901
12902 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12903 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12904
12905 /* Get accessibility. */
12906 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12907 if (attr)
12908 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
12909 else
12910 accessibility = dwarf2_default_access_attribute (die, cu);
12911 switch (accessibility)
12912 {
12913 case DW_ACCESS_private:
12914 fnp->is_private = 1;
12915 break;
12916 case DW_ACCESS_protected:
12917 fnp->is_protected = 1;
12918 break;
12919 }
12920
12921 /* Check for artificial methods. */
12922 attr = dwarf2_attr (die, DW_AT_artificial, cu);
12923 if (attr && DW_UNSND (attr) != 0)
12924 fnp->is_artificial = 1;
12925
12926 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12927
12928 /* Get index in virtual function table if it is a virtual member
12929 function. For older versions of GCC, this is an offset in the
12930 appropriate virtual table, as specified by DW_AT_containing_type.
12931 For everyone else, it is an expression to be evaluated relative
12932 to the object address. */
12933
12934 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
12935 if (attr)
12936 {
12937 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
12938 {
12939 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
12940 {
12941 /* Old-style GCC. */
12942 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
12943 }
12944 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
12945 || (DW_BLOCK (attr)->size > 1
12946 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
12947 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
12948 {
12949 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
12950 if ((fnp->voffset % cu->header.addr_size) != 0)
12951 dwarf2_complex_location_expr_complaint ();
12952 else
12953 fnp->voffset /= cu->header.addr_size;
12954 fnp->voffset += 2;
12955 }
12956 else
12957 dwarf2_complex_location_expr_complaint ();
12958
12959 if (!fnp->fcontext)
12960 {
12961 /* If there is no `this' field and no DW_AT_containing_type,
12962 we cannot actually find a base class context for the
12963 vtable! */
12964 if (TYPE_NFIELDS (this_type) == 0
12965 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
12966 {
12967 complaint (&symfile_complaints,
12968 _("cannot determine context for virtual member "
12969 "function \"%s\" (offset %d)"),
12970 fieldname, die->offset.sect_off);
12971 }
12972 else
12973 {
12974 fnp->fcontext
12975 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
12976 }
12977 }
12978 }
12979 else if (attr_form_is_section_offset (attr))
12980 {
12981 dwarf2_complex_location_expr_complaint ();
12982 }
12983 else
12984 {
12985 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12986 fieldname);
12987 }
12988 }
12989 else
12990 {
12991 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12992 if (attr && DW_UNSND (attr))
12993 {
12994 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12995 complaint (&symfile_complaints,
12996 _("Member function \"%s\" (offset %d) is virtual "
12997 "but the vtable offset is not specified"),
12998 fieldname, die->offset.sect_off);
12999 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13000 TYPE_CPLUS_DYNAMIC (type) = 1;
13001 }
13002 }
13003 }
13004
13005 /* Create the vector of member function fields, and attach it to the type. */
13006
13007 static void
13008 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13009 struct dwarf2_cu *cu)
13010 {
13011 struct fnfieldlist *flp;
13012 int i;
13013
13014 if (cu->language == language_ada)
13015 error (_("unexpected member functions in Ada type"));
13016
13017 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13018 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13019 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13020
13021 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13022 {
13023 struct nextfnfield *nfp = flp->head;
13024 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13025 int k;
13026
13027 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13028 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13029 fn_flp->fn_fields = (struct fn_field *)
13030 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13031 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13032 fn_flp->fn_fields[k] = nfp->fnfield;
13033 }
13034
13035 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13036 }
13037
13038 /* Returns non-zero if NAME is the name of a vtable member in CU's
13039 language, zero otherwise. */
13040 static int
13041 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13042 {
13043 static const char vptr[] = "_vptr";
13044 static const char vtable[] = "vtable";
13045
13046 /* Look for the C++ form of the vtable. */
13047 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13048 return 1;
13049
13050 return 0;
13051 }
13052
13053 /* GCC outputs unnamed structures that are really pointers to member
13054 functions, with the ABI-specified layout. If TYPE describes
13055 such a structure, smash it into a member function type.
13056
13057 GCC shouldn't do this; it should just output pointer to member DIEs.
13058 This is GCC PR debug/28767. */
13059
13060 static void
13061 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13062 {
13063 struct type *pfn_type, *self_type, *new_type;
13064
13065 /* Check for a structure with no name and two children. */
13066 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13067 return;
13068
13069 /* Check for __pfn and __delta members. */
13070 if (TYPE_FIELD_NAME (type, 0) == NULL
13071 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13072 || TYPE_FIELD_NAME (type, 1) == NULL
13073 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13074 return;
13075
13076 /* Find the type of the method. */
13077 pfn_type = TYPE_FIELD_TYPE (type, 0);
13078 if (pfn_type == NULL
13079 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13080 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13081 return;
13082
13083 /* Look for the "this" argument. */
13084 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13085 if (TYPE_NFIELDS (pfn_type) == 0
13086 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13087 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13088 return;
13089
13090 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13091 new_type = alloc_type (objfile);
13092 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13093 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13094 TYPE_VARARGS (pfn_type));
13095 smash_to_methodptr_type (type, new_type);
13096 }
13097
13098 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13099 (icc). */
13100
13101 static int
13102 producer_is_icc (struct dwarf2_cu *cu)
13103 {
13104 if (!cu->checked_producer)
13105 check_producer (cu);
13106
13107 return cu->producer_is_icc;
13108 }
13109
13110 /* Called when we find the DIE that starts a structure or union scope
13111 (definition) to create a type for the structure or union. Fill in
13112 the type's name and general properties; the members will not be
13113 processed until process_structure_scope. A symbol table entry for
13114 the type will also not be done until process_structure_scope (assuming
13115 the type has a name).
13116
13117 NOTE: we need to call these functions regardless of whether or not the
13118 DIE has a DW_AT_name attribute, since it might be an anonymous
13119 structure or union. This gets the type entered into our set of
13120 user defined types. */
13121
13122 static struct type *
13123 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13124 {
13125 struct objfile *objfile = cu->objfile;
13126 struct type *type;
13127 struct attribute *attr;
13128 const char *name;
13129
13130 /* If the definition of this type lives in .debug_types, read that type.
13131 Don't follow DW_AT_specification though, that will take us back up
13132 the chain and we want to go down. */
13133 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13134 if (attr)
13135 {
13136 type = get_DW_AT_signature_type (die, attr, cu);
13137
13138 /* The type's CU may not be the same as CU.
13139 Ensure TYPE is recorded with CU in die_type_hash. */
13140 return set_die_type (die, type, cu);
13141 }
13142
13143 type = alloc_type (objfile);
13144 INIT_CPLUS_SPECIFIC (type);
13145
13146 name = dwarf2_name (die, cu);
13147 if (name != NULL)
13148 {
13149 if (cu->language == language_cplus
13150 || cu->language == language_d
13151 || cu->language == language_rust)
13152 {
13153 const char *full_name = dwarf2_full_name (name, die, cu);
13154
13155 /* dwarf2_full_name might have already finished building the DIE's
13156 type. If so, there is no need to continue. */
13157 if (get_die_type (die, cu) != NULL)
13158 return get_die_type (die, cu);
13159
13160 TYPE_TAG_NAME (type) = full_name;
13161 if (die->tag == DW_TAG_structure_type
13162 || die->tag == DW_TAG_class_type)
13163 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13164 }
13165 else
13166 {
13167 /* The name is already allocated along with this objfile, so
13168 we don't need to duplicate it for the type. */
13169 TYPE_TAG_NAME (type) = name;
13170 if (die->tag == DW_TAG_class_type)
13171 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13172 }
13173 }
13174
13175 if (die->tag == DW_TAG_structure_type)
13176 {
13177 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13178 }
13179 else if (die->tag == DW_TAG_union_type)
13180 {
13181 TYPE_CODE (type) = TYPE_CODE_UNION;
13182 }
13183 else
13184 {
13185 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13186 }
13187
13188 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13189 TYPE_DECLARED_CLASS (type) = 1;
13190
13191 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13192 if (attr)
13193 {
13194 if (attr_form_is_constant (attr))
13195 TYPE_LENGTH (type) = DW_UNSND (attr);
13196 else
13197 {
13198 /* For the moment, dynamic type sizes are not supported
13199 by GDB's struct type. The actual size is determined
13200 on-demand when resolving the type of a given object,
13201 so set the type's length to zero for now. Otherwise,
13202 we record an expression as the length, and that expression
13203 could lead to a very large value, which could eventually
13204 lead to us trying to allocate that much memory when creating
13205 a value of that type. */
13206 TYPE_LENGTH (type) = 0;
13207 }
13208 }
13209 else
13210 {
13211 TYPE_LENGTH (type) = 0;
13212 }
13213
13214 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13215 {
13216 /* ICC does not output the required DW_AT_declaration
13217 on incomplete types, but gives them a size of zero. */
13218 TYPE_STUB (type) = 1;
13219 }
13220 else
13221 TYPE_STUB_SUPPORTED (type) = 1;
13222
13223 if (die_is_declaration (die, cu))
13224 TYPE_STUB (type) = 1;
13225 else if (attr == NULL && die->child == NULL
13226 && producer_is_realview (cu->producer))
13227 /* RealView does not output the required DW_AT_declaration
13228 on incomplete types. */
13229 TYPE_STUB (type) = 1;
13230
13231 /* We need to add the type field to the die immediately so we don't
13232 infinitely recurse when dealing with pointers to the structure
13233 type within the structure itself. */
13234 set_die_type (die, type, cu);
13235
13236 /* set_die_type should be already done. */
13237 set_descriptive_type (type, die, cu);
13238
13239 return type;
13240 }
13241
13242 /* Finish creating a structure or union type, including filling in
13243 its members and creating a symbol for it. */
13244
13245 static void
13246 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13247 {
13248 struct objfile *objfile = cu->objfile;
13249 struct die_info *child_die;
13250 struct type *type;
13251
13252 type = get_die_type (die, cu);
13253 if (type == NULL)
13254 type = read_structure_type (die, cu);
13255
13256 if (die->child != NULL && ! die_is_declaration (die, cu))
13257 {
13258 struct field_info fi;
13259 VEC (symbolp) *template_args = NULL;
13260 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13261
13262 memset (&fi, 0, sizeof (struct field_info));
13263
13264 child_die = die->child;
13265
13266 while (child_die && child_die->tag)
13267 {
13268 if (child_die->tag == DW_TAG_member
13269 || child_die->tag == DW_TAG_variable)
13270 {
13271 /* NOTE: carlton/2002-11-05: A C++ static data member
13272 should be a DW_TAG_member that is a declaration, but
13273 all versions of G++ as of this writing (so through at
13274 least 3.2.1) incorrectly generate DW_TAG_variable
13275 tags for them instead. */
13276 dwarf2_add_field (&fi, child_die, cu);
13277 }
13278 else if (child_die->tag == DW_TAG_subprogram)
13279 {
13280 /* Rust doesn't have member functions in the C++ sense.
13281 However, it does emit ordinary functions as children
13282 of a struct DIE. */
13283 if (cu->language == language_rust)
13284 read_func_scope (child_die, cu);
13285 else
13286 {
13287 /* C++ member function. */
13288 dwarf2_add_member_fn (&fi, child_die, type, cu);
13289 }
13290 }
13291 else if (child_die->tag == DW_TAG_inheritance)
13292 {
13293 /* C++ base class field. */
13294 dwarf2_add_field (&fi, child_die, cu);
13295 }
13296 else if (child_die->tag == DW_TAG_typedef)
13297 dwarf2_add_typedef (&fi, child_die, cu);
13298 else if (child_die->tag == DW_TAG_template_type_param
13299 || child_die->tag == DW_TAG_template_value_param)
13300 {
13301 struct symbol *arg = new_symbol (child_die, NULL, cu);
13302
13303 if (arg != NULL)
13304 VEC_safe_push (symbolp, template_args, arg);
13305 }
13306
13307 child_die = sibling_die (child_die);
13308 }
13309
13310 /* Attach template arguments to type. */
13311 if (! VEC_empty (symbolp, template_args))
13312 {
13313 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13314 TYPE_N_TEMPLATE_ARGUMENTS (type)
13315 = VEC_length (symbolp, template_args);
13316 TYPE_TEMPLATE_ARGUMENTS (type)
13317 = XOBNEWVEC (&objfile->objfile_obstack,
13318 struct symbol *,
13319 TYPE_N_TEMPLATE_ARGUMENTS (type));
13320 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13321 VEC_address (symbolp, template_args),
13322 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13323 * sizeof (struct symbol *)));
13324 VEC_free (symbolp, template_args);
13325 }
13326
13327 /* Attach fields and member functions to the type. */
13328 if (fi.nfields)
13329 dwarf2_attach_fields_to_type (&fi, type, cu);
13330 if (fi.nfnfields)
13331 {
13332 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13333
13334 /* Get the type which refers to the base class (possibly this
13335 class itself) which contains the vtable pointer for the current
13336 class from the DW_AT_containing_type attribute. This use of
13337 DW_AT_containing_type is a GNU extension. */
13338
13339 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13340 {
13341 struct type *t = die_containing_type (die, cu);
13342
13343 set_type_vptr_basetype (type, t);
13344 if (type == t)
13345 {
13346 int i;
13347
13348 /* Our own class provides vtbl ptr. */
13349 for (i = TYPE_NFIELDS (t) - 1;
13350 i >= TYPE_N_BASECLASSES (t);
13351 --i)
13352 {
13353 const char *fieldname = TYPE_FIELD_NAME (t, i);
13354
13355 if (is_vtable_name (fieldname, cu))
13356 {
13357 set_type_vptr_fieldno (type, i);
13358 break;
13359 }
13360 }
13361
13362 /* Complain if virtual function table field not found. */
13363 if (i < TYPE_N_BASECLASSES (t))
13364 complaint (&symfile_complaints,
13365 _("virtual function table pointer "
13366 "not found when defining class '%s'"),
13367 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13368 "");
13369 }
13370 else
13371 {
13372 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13373 }
13374 }
13375 else if (cu->producer
13376 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13377 {
13378 /* The IBM XLC compiler does not provide direct indication
13379 of the containing type, but the vtable pointer is
13380 always named __vfp. */
13381
13382 int i;
13383
13384 for (i = TYPE_NFIELDS (type) - 1;
13385 i >= TYPE_N_BASECLASSES (type);
13386 --i)
13387 {
13388 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13389 {
13390 set_type_vptr_fieldno (type, i);
13391 set_type_vptr_basetype (type, type);
13392 break;
13393 }
13394 }
13395 }
13396 }
13397
13398 /* Copy fi.typedef_field_list linked list elements content into the
13399 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13400 if (fi.typedef_field_list)
13401 {
13402 int i = fi.typedef_field_list_count;
13403
13404 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13405 TYPE_TYPEDEF_FIELD_ARRAY (type)
13406 = ((struct typedef_field *)
13407 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13408 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13409
13410 /* Reverse the list order to keep the debug info elements order. */
13411 while (--i >= 0)
13412 {
13413 struct typedef_field *dest, *src;
13414
13415 dest = &TYPE_TYPEDEF_FIELD (type, i);
13416 src = &fi.typedef_field_list->field;
13417 fi.typedef_field_list = fi.typedef_field_list->next;
13418 *dest = *src;
13419 }
13420 }
13421
13422 do_cleanups (back_to);
13423 }
13424
13425 quirk_gcc_member_function_pointer (type, objfile);
13426
13427 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13428 snapshots) has been known to create a die giving a declaration
13429 for a class that has, as a child, a die giving a definition for a
13430 nested class. So we have to process our children even if the
13431 current die is a declaration. Normally, of course, a declaration
13432 won't have any children at all. */
13433
13434 child_die = die->child;
13435
13436 while (child_die != NULL && child_die->tag)
13437 {
13438 if (child_die->tag == DW_TAG_member
13439 || child_die->tag == DW_TAG_variable
13440 || child_die->tag == DW_TAG_inheritance
13441 || child_die->tag == DW_TAG_template_value_param
13442 || child_die->tag == DW_TAG_template_type_param)
13443 {
13444 /* Do nothing. */
13445 }
13446 else
13447 process_die (child_die, cu);
13448
13449 child_die = sibling_die (child_die);
13450 }
13451
13452 /* Do not consider external references. According to the DWARF standard,
13453 these DIEs are identified by the fact that they have no byte_size
13454 attribute, and a declaration attribute. */
13455 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13456 || !die_is_declaration (die, cu))
13457 new_symbol (die, type, cu);
13458 }
13459
13460 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13461 update TYPE using some information only available in DIE's children. */
13462
13463 static void
13464 update_enumeration_type_from_children (struct die_info *die,
13465 struct type *type,
13466 struct dwarf2_cu *cu)
13467 {
13468 struct obstack obstack;
13469 struct die_info *child_die;
13470 int unsigned_enum = 1;
13471 int flag_enum = 1;
13472 ULONGEST mask = 0;
13473 struct cleanup *old_chain;
13474
13475 obstack_init (&obstack);
13476 old_chain = make_cleanup_obstack_free (&obstack);
13477
13478 for (child_die = die->child;
13479 child_die != NULL && child_die->tag;
13480 child_die = sibling_die (child_die))
13481 {
13482 struct attribute *attr;
13483 LONGEST value;
13484 const gdb_byte *bytes;
13485 struct dwarf2_locexpr_baton *baton;
13486 const char *name;
13487
13488 if (child_die->tag != DW_TAG_enumerator)
13489 continue;
13490
13491 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13492 if (attr == NULL)
13493 continue;
13494
13495 name = dwarf2_name (child_die, cu);
13496 if (name == NULL)
13497 name = "<anonymous enumerator>";
13498
13499 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13500 &value, &bytes, &baton);
13501 if (value < 0)
13502 {
13503 unsigned_enum = 0;
13504 flag_enum = 0;
13505 }
13506 else if ((mask & value) != 0)
13507 flag_enum = 0;
13508 else
13509 mask |= value;
13510
13511 /* If we already know that the enum type is neither unsigned, nor
13512 a flag type, no need to look at the rest of the enumerates. */
13513 if (!unsigned_enum && !flag_enum)
13514 break;
13515 }
13516
13517 if (unsigned_enum)
13518 TYPE_UNSIGNED (type) = 1;
13519 if (flag_enum)
13520 TYPE_FLAG_ENUM (type) = 1;
13521
13522 do_cleanups (old_chain);
13523 }
13524
13525 /* Given a DW_AT_enumeration_type die, set its type. We do not
13526 complete the type's fields yet, or create any symbols. */
13527
13528 static struct type *
13529 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13530 {
13531 struct objfile *objfile = cu->objfile;
13532 struct type *type;
13533 struct attribute *attr;
13534 const char *name;
13535
13536 /* If the definition of this type lives in .debug_types, read that type.
13537 Don't follow DW_AT_specification though, that will take us back up
13538 the chain and we want to go down. */
13539 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13540 if (attr)
13541 {
13542 type = get_DW_AT_signature_type (die, attr, cu);
13543
13544 /* The type's CU may not be the same as CU.
13545 Ensure TYPE is recorded with CU in die_type_hash. */
13546 return set_die_type (die, type, cu);
13547 }
13548
13549 type = alloc_type (objfile);
13550
13551 TYPE_CODE (type) = TYPE_CODE_ENUM;
13552 name = dwarf2_full_name (NULL, die, cu);
13553 if (name != NULL)
13554 TYPE_TAG_NAME (type) = name;
13555
13556 attr = dwarf2_attr (die, DW_AT_type, cu);
13557 if (attr != NULL)
13558 {
13559 struct type *underlying_type = die_type (die, cu);
13560
13561 TYPE_TARGET_TYPE (type) = underlying_type;
13562 }
13563
13564 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13565 if (attr)
13566 {
13567 TYPE_LENGTH (type) = DW_UNSND (attr);
13568 }
13569 else
13570 {
13571 TYPE_LENGTH (type) = 0;
13572 }
13573
13574 /* The enumeration DIE can be incomplete. In Ada, any type can be
13575 declared as private in the package spec, and then defined only
13576 inside the package body. Such types are known as Taft Amendment
13577 Types. When another package uses such a type, an incomplete DIE
13578 may be generated by the compiler. */
13579 if (die_is_declaration (die, cu))
13580 TYPE_STUB (type) = 1;
13581
13582 /* Finish the creation of this type by using the enum's children.
13583 We must call this even when the underlying type has been provided
13584 so that we can determine if we're looking at a "flag" enum. */
13585 update_enumeration_type_from_children (die, type, cu);
13586
13587 /* If this type has an underlying type that is not a stub, then we
13588 may use its attributes. We always use the "unsigned" attribute
13589 in this situation, because ordinarily we guess whether the type
13590 is unsigned -- but the guess can be wrong and the underlying type
13591 can tell us the reality. However, we defer to a local size
13592 attribute if one exists, because this lets the compiler override
13593 the underlying type if needed. */
13594 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13595 {
13596 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13597 if (TYPE_LENGTH (type) == 0)
13598 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13599 }
13600
13601 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13602
13603 return set_die_type (die, type, cu);
13604 }
13605
13606 /* Given a pointer to a die which begins an enumeration, process all
13607 the dies that define the members of the enumeration, and create the
13608 symbol for the enumeration type.
13609
13610 NOTE: We reverse the order of the element list. */
13611
13612 static void
13613 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13614 {
13615 struct type *this_type;
13616
13617 this_type = get_die_type (die, cu);
13618 if (this_type == NULL)
13619 this_type = read_enumeration_type (die, cu);
13620
13621 if (die->child != NULL)
13622 {
13623 struct die_info *child_die;
13624 struct symbol *sym;
13625 struct field *fields = NULL;
13626 int num_fields = 0;
13627 const char *name;
13628
13629 child_die = die->child;
13630 while (child_die && child_die->tag)
13631 {
13632 if (child_die->tag != DW_TAG_enumerator)
13633 {
13634 process_die (child_die, cu);
13635 }
13636 else
13637 {
13638 name = dwarf2_name (child_die, cu);
13639 if (name)
13640 {
13641 sym = new_symbol (child_die, this_type, cu);
13642
13643 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13644 {
13645 fields = (struct field *)
13646 xrealloc (fields,
13647 (num_fields + DW_FIELD_ALLOC_CHUNK)
13648 * sizeof (struct field));
13649 }
13650
13651 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13652 FIELD_TYPE (fields[num_fields]) = NULL;
13653 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13654 FIELD_BITSIZE (fields[num_fields]) = 0;
13655
13656 num_fields++;
13657 }
13658 }
13659
13660 child_die = sibling_die (child_die);
13661 }
13662
13663 if (num_fields)
13664 {
13665 TYPE_NFIELDS (this_type) = num_fields;
13666 TYPE_FIELDS (this_type) = (struct field *)
13667 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13668 memcpy (TYPE_FIELDS (this_type), fields,
13669 sizeof (struct field) * num_fields);
13670 xfree (fields);
13671 }
13672 }
13673
13674 /* If we are reading an enum from a .debug_types unit, and the enum
13675 is a declaration, and the enum is not the signatured type in the
13676 unit, then we do not want to add a symbol for it. Adding a
13677 symbol would in some cases obscure the true definition of the
13678 enum, giving users an incomplete type when the definition is
13679 actually available. Note that we do not want to do this for all
13680 enums which are just declarations, because C++0x allows forward
13681 enum declarations. */
13682 if (cu->per_cu->is_debug_types
13683 && die_is_declaration (die, cu))
13684 {
13685 struct signatured_type *sig_type;
13686
13687 sig_type = (struct signatured_type *) cu->per_cu;
13688 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13689 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
13690 return;
13691 }
13692
13693 new_symbol (die, this_type, cu);
13694 }
13695
13696 /* Extract all information from a DW_TAG_array_type DIE and put it in
13697 the DIE's type field. For now, this only handles one dimensional
13698 arrays. */
13699
13700 static struct type *
13701 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13702 {
13703 struct objfile *objfile = cu->objfile;
13704 struct die_info *child_die;
13705 struct type *type;
13706 struct type *element_type, *range_type, *index_type;
13707 struct type **range_types = NULL;
13708 struct attribute *attr;
13709 int ndim = 0;
13710 struct cleanup *back_to;
13711 const char *name;
13712 unsigned int bit_stride = 0;
13713
13714 element_type = die_type (die, cu);
13715
13716 /* The die_type call above may have already set the type for this DIE. */
13717 type = get_die_type (die, cu);
13718 if (type)
13719 return type;
13720
13721 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13722 if (attr != NULL)
13723 bit_stride = DW_UNSND (attr) * 8;
13724
13725 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13726 if (attr != NULL)
13727 bit_stride = DW_UNSND (attr);
13728
13729 /* Irix 6.2 native cc creates array types without children for
13730 arrays with unspecified length. */
13731 if (die->child == NULL)
13732 {
13733 index_type = objfile_type (objfile)->builtin_int;
13734 range_type = create_static_range_type (NULL, index_type, 0, -1);
13735 type = create_array_type_with_stride (NULL, element_type, range_type,
13736 bit_stride);
13737 return set_die_type (die, type, cu);
13738 }
13739
13740 back_to = make_cleanup (null_cleanup, NULL);
13741 child_die = die->child;
13742 while (child_die && child_die->tag)
13743 {
13744 if (child_die->tag == DW_TAG_subrange_type)
13745 {
13746 struct type *child_type = read_type_die (child_die, cu);
13747
13748 if (child_type != NULL)
13749 {
13750 /* The range type was succesfully read. Save it for the
13751 array type creation. */
13752 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
13753 {
13754 range_types = (struct type **)
13755 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
13756 * sizeof (struct type *));
13757 if (ndim == 0)
13758 make_cleanup (free_current_contents, &range_types);
13759 }
13760 range_types[ndim++] = child_type;
13761 }
13762 }
13763 child_die = sibling_die (child_die);
13764 }
13765
13766 /* Dwarf2 dimensions are output from left to right, create the
13767 necessary array types in backwards order. */
13768
13769 type = element_type;
13770
13771 if (read_array_order (die, cu) == DW_ORD_col_major)
13772 {
13773 int i = 0;
13774
13775 while (i < ndim)
13776 type = create_array_type_with_stride (NULL, type, range_types[i++],
13777 bit_stride);
13778 }
13779 else
13780 {
13781 while (ndim-- > 0)
13782 type = create_array_type_with_stride (NULL, type, range_types[ndim],
13783 bit_stride);
13784 }
13785
13786 /* Understand Dwarf2 support for vector types (like they occur on
13787 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13788 array type. This is not part of the Dwarf2/3 standard yet, but a
13789 custom vendor extension. The main difference between a regular
13790 array and the vector variant is that vectors are passed by value
13791 to functions. */
13792 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13793 if (attr)
13794 make_vector_type (type);
13795
13796 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13797 implementation may choose to implement triple vectors using this
13798 attribute. */
13799 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13800 if (attr)
13801 {
13802 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
13803 TYPE_LENGTH (type) = DW_UNSND (attr);
13804 else
13805 complaint (&symfile_complaints,
13806 _("DW_AT_byte_size for array type smaller "
13807 "than the total size of elements"));
13808 }
13809
13810 name = dwarf2_name (die, cu);
13811 if (name)
13812 TYPE_NAME (type) = name;
13813
13814 /* Install the type in the die. */
13815 set_die_type (die, type, cu);
13816
13817 /* set_die_type should be already done. */
13818 set_descriptive_type (type, die, cu);
13819
13820 do_cleanups (back_to);
13821
13822 return type;
13823 }
13824
13825 static enum dwarf_array_dim_ordering
13826 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13827 {
13828 struct attribute *attr;
13829
13830 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13831
13832 if (attr)
13833 return (enum dwarf_array_dim_ordering) DW_SND (attr);
13834
13835 /* GNU F77 is a special case, as at 08/2004 array type info is the
13836 opposite order to the dwarf2 specification, but data is still
13837 laid out as per normal fortran.
13838
13839 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13840 version checking. */
13841
13842 if (cu->language == language_fortran
13843 && cu->producer && strstr (cu->producer, "GNU F77"))
13844 {
13845 return DW_ORD_row_major;
13846 }
13847
13848 switch (cu->language_defn->la_array_ordering)
13849 {
13850 case array_column_major:
13851 return DW_ORD_col_major;
13852 case array_row_major:
13853 default:
13854 return DW_ORD_row_major;
13855 };
13856 }
13857
13858 /* Extract all information from a DW_TAG_set_type DIE and put it in
13859 the DIE's type field. */
13860
13861 static struct type *
13862 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13863 {
13864 struct type *domain_type, *set_type;
13865 struct attribute *attr;
13866
13867 domain_type = die_type (die, cu);
13868
13869 /* The die_type call above may have already set the type for this DIE. */
13870 set_type = get_die_type (die, cu);
13871 if (set_type)
13872 return set_type;
13873
13874 set_type = create_set_type (NULL, domain_type);
13875
13876 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13877 if (attr)
13878 TYPE_LENGTH (set_type) = DW_UNSND (attr);
13879
13880 return set_die_type (die, set_type, cu);
13881 }
13882
13883 /* A helper for read_common_block that creates a locexpr baton.
13884 SYM is the symbol which we are marking as computed.
13885 COMMON_DIE is the DIE for the common block.
13886 COMMON_LOC is the location expression attribute for the common
13887 block itself.
13888 MEMBER_LOC is the location expression attribute for the particular
13889 member of the common block that we are processing.
13890 CU is the CU from which the above come. */
13891
13892 static void
13893 mark_common_block_symbol_computed (struct symbol *sym,
13894 struct die_info *common_die,
13895 struct attribute *common_loc,
13896 struct attribute *member_loc,
13897 struct dwarf2_cu *cu)
13898 {
13899 struct objfile *objfile = dwarf2_per_objfile->objfile;
13900 struct dwarf2_locexpr_baton *baton;
13901 gdb_byte *ptr;
13902 unsigned int cu_off;
13903 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
13904 LONGEST offset = 0;
13905
13906 gdb_assert (common_loc && member_loc);
13907 gdb_assert (attr_form_is_block (common_loc));
13908 gdb_assert (attr_form_is_block (member_loc)
13909 || attr_form_is_constant (member_loc));
13910
13911 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13912 baton->per_cu = cu->per_cu;
13913 gdb_assert (baton->per_cu);
13914
13915 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13916
13917 if (attr_form_is_constant (member_loc))
13918 {
13919 offset = dwarf2_get_attr_constant_value (member_loc, 0);
13920 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13921 }
13922 else
13923 baton->size += DW_BLOCK (member_loc)->size;
13924
13925 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
13926 baton->data = ptr;
13927
13928 *ptr++ = DW_OP_call4;
13929 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
13930 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13931 ptr += 4;
13932
13933 if (attr_form_is_constant (member_loc))
13934 {
13935 *ptr++ = DW_OP_addr;
13936 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13937 ptr += cu->header.addr_size;
13938 }
13939 else
13940 {
13941 /* We have to copy the data here, because DW_OP_call4 will only
13942 use a DW_AT_location attribute. */
13943 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
13944 ptr += DW_BLOCK (member_loc)->size;
13945 }
13946
13947 *ptr++ = DW_OP_plus;
13948 gdb_assert (ptr - baton->data == baton->size);
13949
13950 SYMBOL_LOCATION_BATON (sym) = baton;
13951 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
13952 }
13953
13954 /* Create appropriate locally-scoped variables for all the
13955 DW_TAG_common_block entries. Also create a struct common_block
13956 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13957 is used to sepate the common blocks name namespace from regular
13958 variable names. */
13959
13960 static void
13961 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13962 {
13963 struct attribute *attr;
13964
13965 attr = dwarf2_attr (die, DW_AT_location, cu);
13966 if (attr)
13967 {
13968 /* Support the .debug_loc offsets. */
13969 if (attr_form_is_block (attr))
13970 {
13971 /* Ok. */
13972 }
13973 else if (attr_form_is_section_offset (attr))
13974 {
13975 dwarf2_complex_location_expr_complaint ();
13976 attr = NULL;
13977 }
13978 else
13979 {
13980 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13981 "common block member");
13982 attr = NULL;
13983 }
13984 }
13985
13986 if (die->child != NULL)
13987 {
13988 struct objfile *objfile = cu->objfile;
13989 struct die_info *child_die;
13990 size_t n_entries = 0, size;
13991 struct common_block *common_block;
13992 struct symbol *sym;
13993
13994 for (child_die = die->child;
13995 child_die && child_die->tag;
13996 child_die = sibling_die (child_die))
13997 ++n_entries;
13998
13999 size = (sizeof (struct common_block)
14000 + (n_entries - 1) * sizeof (struct symbol *));
14001 common_block
14002 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14003 size);
14004 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14005 common_block->n_entries = 0;
14006
14007 for (child_die = die->child;
14008 child_die && child_die->tag;
14009 child_die = sibling_die (child_die))
14010 {
14011 /* Create the symbol in the DW_TAG_common_block block in the current
14012 symbol scope. */
14013 sym = new_symbol (child_die, NULL, cu);
14014 if (sym != NULL)
14015 {
14016 struct attribute *member_loc;
14017
14018 common_block->contents[common_block->n_entries++] = sym;
14019
14020 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14021 cu);
14022 if (member_loc)
14023 {
14024 /* GDB has handled this for a long time, but it is
14025 not specified by DWARF. It seems to have been
14026 emitted by gfortran at least as recently as:
14027 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14028 complaint (&symfile_complaints,
14029 _("Variable in common block has "
14030 "DW_AT_data_member_location "
14031 "- DIE at 0x%x [in module %s]"),
14032 child_die->offset.sect_off,
14033 objfile_name (cu->objfile));
14034
14035 if (attr_form_is_section_offset (member_loc))
14036 dwarf2_complex_location_expr_complaint ();
14037 else if (attr_form_is_constant (member_loc)
14038 || attr_form_is_block (member_loc))
14039 {
14040 if (attr)
14041 mark_common_block_symbol_computed (sym, die, attr,
14042 member_loc, cu);
14043 }
14044 else
14045 dwarf2_complex_location_expr_complaint ();
14046 }
14047 }
14048 }
14049
14050 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14051 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14052 }
14053 }
14054
14055 /* Create a type for a C++ namespace. */
14056
14057 static struct type *
14058 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14059 {
14060 struct objfile *objfile = cu->objfile;
14061 const char *previous_prefix, *name;
14062 int is_anonymous;
14063 struct type *type;
14064
14065 /* For extensions, reuse the type of the original namespace. */
14066 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14067 {
14068 struct die_info *ext_die;
14069 struct dwarf2_cu *ext_cu = cu;
14070
14071 ext_die = dwarf2_extension (die, &ext_cu);
14072 type = read_type_die (ext_die, ext_cu);
14073
14074 /* EXT_CU may not be the same as CU.
14075 Ensure TYPE is recorded with CU in die_type_hash. */
14076 return set_die_type (die, type, cu);
14077 }
14078
14079 name = namespace_name (die, &is_anonymous, cu);
14080
14081 /* Now build the name of the current namespace. */
14082
14083 previous_prefix = determine_prefix (die, cu);
14084 if (previous_prefix[0] != '\0')
14085 name = typename_concat (&objfile->objfile_obstack,
14086 previous_prefix, name, 0, cu);
14087
14088 /* Create the type. */
14089 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14090 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14091
14092 return set_die_type (die, type, cu);
14093 }
14094
14095 /* Read a namespace scope. */
14096
14097 static void
14098 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14099 {
14100 struct objfile *objfile = cu->objfile;
14101 int is_anonymous;
14102
14103 /* Add a symbol associated to this if we haven't seen the namespace
14104 before. Also, add a using directive if it's an anonymous
14105 namespace. */
14106
14107 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14108 {
14109 struct type *type;
14110
14111 type = read_type_die (die, cu);
14112 new_symbol (die, type, cu);
14113
14114 namespace_name (die, &is_anonymous, cu);
14115 if (is_anonymous)
14116 {
14117 const char *previous_prefix = determine_prefix (die, cu);
14118
14119 add_using_directive (using_directives (cu->language),
14120 previous_prefix, TYPE_NAME (type), NULL,
14121 NULL, NULL, 0, &objfile->objfile_obstack);
14122 }
14123 }
14124
14125 if (die->child != NULL)
14126 {
14127 struct die_info *child_die = die->child;
14128
14129 while (child_die && child_die->tag)
14130 {
14131 process_die (child_die, cu);
14132 child_die = sibling_die (child_die);
14133 }
14134 }
14135 }
14136
14137 /* Read a Fortran module as type. This DIE can be only a declaration used for
14138 imported module. Still we need that type as local Fortran "use ... only"
14139 declaration imports depend on the created type in determine_prefix. */
14140
14141 static struct type *
14142 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14143 {
14144 struct objfile *objfile = cu->objfile;
14145 const char *module_name;
14146 struct type *type;
14147
14148 module_name = dwarf2_name (die, cu);
14149 if (!module_name)
14150 complaint (&symfile_complaints,
14151 _("DW_TAG_module has no name, offset 0x%x"),
14152 die->offset.sect_off);
14153 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14154
14155 /* determine_prefix uses TYPE_TAG_NAME. */
14156 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14157
14158 return set_die_type (die, type, cu);
14159 }
14160
14161 /* Read a Fortran module. */
14162
14163 static void
14164 read_module (struct die_info *die, struct dwarf2_cu *cu)
14165 {
14166 struct die_info *child_die = die->child;
14167 struct type *type;
14168
14169 type = read_type_die (die, cu);
14170 new_symbol (die, type, cu);
14171
14172 while (child_die && child_die->tag)
14173 {
14174 process_die (child_die, cu);
14175 child_die = sibling_die (child_die);
14176 }
14177 }
14178
14179 /* Return the name of the namespace represented by DIE. Set
14180 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14181 namespace. */
14182
14183 static const char *
14184 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14185 {
14186 struct die_info *current_die;
14187 const char *name = NULL;
14188
14189 /* Loop through the extensions until we find a name. */
14190
14191 for (current_die = die;
14192 current_die != NULL;
14193 current_die = dwarf2_extension (die, &cu))
14194 {
14195 /* We don't use dwarf2_name here so that we can detect the absence
14196 of a name -> anonymous namespace. */
14197 name = dwarf2_string_attr (die, DW_AT_name, cu);
14198
14199 if (name != NULL)
14200 break;
14201 }
14202
14203 /* Is it an anonymous namespace? */
14204
14205 *is_anonymous = (name == NULL);
14206 if (*is_anonymous)
14207 name = CP_ANONYMOUS_NAMESPACE_STR;
14208
14209 return name;
14210 }
14211
14212 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14213 the user defined type vector. */
14214
14215 static struct type *
14216 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14217 {
14218 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14219 struct comp_unit_head *cu_header = &cu->header;
14220 struct type *type;
14221 struct attribute *attr_byte_size;
14222 struct attribute *attr_address_class;
14223 int byte_size, addr_class;
14224 struct type *target_type;
14225
14226 target_type = die_type (die, cu);
14227
14228 /* The die_type call above may have already set the type for this DIE. */
14229 type = get_die_type (die, cu);
14230 if (type)
14231 return type;
14232
14233 type = lookup_pointer_type (target_type);
14234
14235 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14236 if (attr_byte_size)
14237 byte_size = DW_UNSND (attr_byte_size);
14238 else
14239 byte_size = cu_header->addr_size;
14240
14241 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14242 if (attr_address_class)
14243 addr_class = DW_UNSND (attr_address_class);
14244 else
14245 addr_class = DW_ADDR_none;
14246
14247 /* If the pointer size or address class is different than the
14248 default, create a type variant marked as such and set the
14249 length accordingly. */
14250 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14251 {
14252 if (gdbarch_address_class_type_flags_p (gdbarch))
14253 {
14254 int type_flags;
14255
14256 type_flags = gdbarch_address_class_type_flags
14257 (gdbarch, byte_size, addr_class);
14258 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14259 == 0);
14260 type = make_type_with_address_space (type, type_flags);
14261 }
14262 else if (TYPE_LENGTH (type) != byte_size)
14263 {
14264 complaint (&symfile_complaints,
14265 _("invalid pointer size %d"), byte_size);
14266 }
14267 else
14268 {
14269 /* Should we also complain about unhandled address classes? */
14270 }
14271 }
14272
14273 TYPE_LENGTH (type) = byte_size;
14274 return set_die_type (die, type, cu);
14275 }
14276
14277 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14278 the user defined type vector. */
14279
14280 static struct type *
14281 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14282 {
14283 struct type *type;
14284 struct type *to_type;
14285 struct type *domain;
14286
14287 to_type = die_type (die, cu);
14288 domain = die_containing_type (die, cu);
14289
14290 /* The calls above may have already set the type for this DIE. */
14291 type = get_die_type (die, cu);
14292 if (type)
14293 return type;
14294
14295 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14296 type = lookup_methodptr_type (to_type);
14297 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14298 {
14299 struct type *new_type = alloc_type (cu->objfile);
14300
14301 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14302 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14303 TYPE_VARARGS (to_type));
14304 type = lookup_methodptr_type (new_type);
14305 }
14306 else
14307 type = lookup_memberptr_type (to_type, domain);
14308
14309 return set_die_type (die, type, cu);
14310 }
14311
14312 /* Extract all information from a DW_TAG_reference_type DIE and add to
14313 the user defined type vector. */
14314
14315 static struct type *
14316 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
14317 {
14318 struct comp_unit_head *cu_header = &cu->header;
14319 struct type *type, *target_type;
14320 struct attribute *attr;
14321
14322 target_type = die_type (die, cu);
14323
14324 /* The die_type call above may have already set the type for this DIE. */
14325 type = get_die_type (die, cu);
14326 if (type)
14327 return type;
14328
14329 type = lookup_reference_type (target_type);
14330 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14331 if (attr)
14332 {
14333 TYPE_LENGTH (type) = DW_UNSND (attr);
14334 }
14335 else
14336 {
14337 TYPE_LENGTH (type) = cu_header->addr_size;
14338 }
14339 return set_die_type (die, type, cu);
14340 }
14341
14342 /* Add the given cv-qualifiers to the element type of the array. GCC
14343 outputs DWARF type qualifiers that apply to an array, not the
14344 element type. But GDB relies on the array element type to carry
14345 the cv-qualifiers. This mimics section 6.7.3 of the C99
14346 specification. */
14347
14348 static struct type *
14349 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14350 struct type *base_type, int cnst, int voltl)
14351 {
14352 struct type *el_type, *inner_array;
14353
14354 base_type = copy_type (base_type);
14355 inner_array = base_type;
14356
14357 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14358 {
14359 TYPE_TARGET_TYPE (inner_array) =
14360 copy_type (TYPE_TARGET_TYPE (inner_array));
14361 inner_array = TYPE_TARGET_TYPE (inner_array);
14362 }
14363
14364 el_type = TYPE_TARGET_TYPE (inner_array);
14365 cnst |= TYPE_CONST (el_type);
14366 voltl |= TYPE_VOLATILE (el_type);
14367 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14368
14369 return set_die_type (die, base_type, cu);
14370 }
14371
14372 static struct type *
14373 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14374 {
14375 struct type *base_type, *cv_type;
14376
14377 base_type = die_type (die, cu);
14378
14379 /* The die_type call above may have already set the type for this DIE. */
14380 cv_type = get_die_type (die, cu);
14381 if (cv_type)
14382 return cv_type;
14383
14384 /* In case the const qualifier is applied to an array type, the element type
14385 is so qualified, not the array type (section 6.7.3 of C99). */
14386 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14387 return add_array_cv_type (die, cu, base_type, 1, 0);
14388
14389 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14390 return set_die_type (die, cv_type, cu);
14391 }
14392
14393 static struct type *
14394 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14395 {
14396 struct type *base_type, *cv_type;
14397
14398 base_type = die_type (die, cu);
14399
14400 /* The die_type call above may have already set the type for this DIE. */
14401 cv_type = get_die_type (die, cu);
14402 if (cv_type)
14403 return cv_type;
14404
14405 /* In case the volatile qualifier is applied to an array type, the
14406 element type is so qualified, not the array type (section 6.7.3
14407 of C99). */
14408 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14409 return add_array_cv_type (die, cu, base_type, 0, 1);
14410
14411 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14412 return set_die_type (die, cv_type, cu);
14413 }
14414
14415 /* Handle DW_TAG_restrict_type. */
14416
14417 static struct type *
14418 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14419 {
14420 struct type *base_type, *cv_type;
14421
14422 base_type = die_type (die, cu);
14423
14424 /* The die_type call above may have already set the type for this DIE. */
14425 cv_type = get_die_type (die, cu);
14426 if (cv_type)
14427 return cv_type;
14428
14429 cv_type = make_restrict_type (base_type);
14430 return set_die_type (die, cv_type, cu);
14431 }
14432
14433 /* Handle DW_TAG_atomic_type. */
14434
14435 static struct type *
14436 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14437 {
14438 struct type *base_type, *cv_type;
14439
14440 base_type = die_type (die, cu);
14441
14442 /* The die_type call above may have already set the type for this DIE. */
14443 cv_type = get_die_type (die, cu);
14444 if (cv_type)
14445 return cv_type;
14446
14447 cv_type = make_atomic_type (base_type);
14448 return set_die_type (die, cv_type, cu);
14449 }
14450
14451 /* Extract all information from a DW_TAG_string_type DIE and add to
14452 the user defined type vector. It isn't really a user defined type,
14453 but it behaves like one, with other DIE's using an AT_user_def_type
14454 attribute to reference it. */
14455
14456 static struct type *
14457 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14458 {
14459 struct objfile *objfile = cu->objfile;
14460 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14461 struct type *type, *range_type, *index_type, *char_type;
14462 struct attribute *attr;
14463 unsigned int length;
14464
14465 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14466 if (attr)
14467 {
14468 length = DW_UNSND (attr);
14469 }
14470 else
14471 {
14472 /* Check for the DW_AT_byte_size attribute. */
14473 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14474 if (attr)
14475 {
14476 length = DW_UNSND (attr);
14477 }
14478 else
14479 {
14480 length = 1;
14481 }
14482 }
14483
14484 index_type = objfile_type (objfile)->builtin_int;
14485 range_type = create_static_range_type (NULL, index_type, 1, length);
14486 char_type = language_string_char_type (cu->language_defn, gdbarch);
14487 type = create_string_type (NULL, char_type, range_type);
14488
14489 return set_die_type (die, type, cu);
14490 }
14491
14492 /* Assuming that DIE corresponds to a function, returns nonzero
14493 if the function is prototyped. */
14494
14495 static int
14496 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14497 {
14498 struct attribute *attr;
14499
14500 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14501 if (attr && (DW_UNSND (attr) != 0))
14502 return 1;
14503
14504 /* The DWARF standard implies that the DW_AT_prototyped attribute
14505 is only meaninful for C, but the concept also extends to other
14506 languages that allow unprototyped functions (Eg: Objective C).
14507 For all other languages, assume that functions are always
14508 prototyped. */
14509 if (cu->language != language_c
14510 && cu->language != language_objc
14511 && cu->language != language_opencl)
14512 return 1;
14513
14514 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14515 prototyped and unprototyped functions; default to prototyped,
14516 since that is more common in modern code (and RealView warns
14517 about unprototyped functions). */
14518 if (producer_is_realview (cu->producer))
14519 return 1;
14520
14521 return 0;
14522 }
14523
14524 /* Handle DIES due to C code like:
14525
14526 struct foo
14527 {
14528 int (*funcp)(int a, long l);
14529 int b;
14530 };
14531
14532 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14533
14534 static struct type *
14535 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14536 {
14537 struct objfile *objfile = cu->objfile;
14538 struct type *type; /* Type that this function returns. */
14539 struct type *ftype; /* Function that returns above type. */
14540 struct attribute *attr;
14541
14542 type = die_type (die, cu);
14543
14544 /* The die_type call above may have already set the type for this DIE. */
14545 ftype = get_die_type (die, cu);
14546 if (ftype)
14547 return ftype;
14548
14549 ftype = lookup_function_type (type);
14550
14551 if (prototyped_function_p (die, cu))
14552 TYPE_PROTOTYPED (ftype) = 1;
14553
14554 /* Store the calling convention in the type if it's available in
14555 the subroutine die. Otherwise set the calling convention to
14556 the default value DW_CC_normal. */
14557 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14558 if (attr)
14559 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14560 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14561 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14562 else
14563 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14564
14565 /* Record whether the function returns normally to its caller or not
14566 if the DWARF producer set that information. */
14567 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14568 if (attr && (DW_UNSND (attr) != 0))
14569 TYPE_NO_RETURN (ftype) = 1;
14570
14571 /* We need to add the subroutine type to the die immediately so
14572 we don't infinitely recurse when dealing with parameters
14573 declared as the same subroutine type. */
14574 set_die_type (die, ftype, cu);
14575
14576 if (die->child != NULL)
14577 {
14578 struct type *void_type = objfile_type (objfile)->builtin_void;
14579 struct die_info *child_die;
14580 int nparams, iparams;
14581
14582 /* Count the number of parameters.
14583 FIXME: GDB currently ignores vararg functions, but knows about
14584 vararg member functions. */
14585 nparams = 0;
14586 child_die = die->child;
14587 while (child_die && child_die->tag)
14588 {
14589 if (child_die->tag == DW_TAG_formal_parameter)
14590 nparams++;
14591 else if (child_die->tag == DW_TAG_unspecified_parameters)
14592 TYPE_VARARGS (ftype) = 1;
14593 child_die = sibling_die (child_die);
14594 }
14595
14596 /* Allocate storage for parameters and fill them in. */
14597 TYPE_NFIELDS (ftype) = nparams;
14598 TYPE_FIELDS (ftype) = (struct field *)
14599 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14600
14601 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14602 even if we error out during the parameters reading below. */
14603 for (iparams = 0; iparams < nparams; iparams++)
14604 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14605
14606 iparams = 0;
14607 child_die = die->child;
14608 while (child_die && child_die->tag)
14609 {
14610 if (child_die->tag == DW_TAG_formal_parameter)
14611 {
14612 struct type *arg_type;
14613
14614 /* DWARF version 2 has no clean way to discern C++
14615 static and non-static member functions. G++ helps
14616 GDB by marking the first parameter for non-static
14617 member functions (which is the this pointer) as
14618 artificial. We pass this information to
14619 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14620
14621 DWARF version 3 added DW_AT_object_pointer, which GCC
14622 4.5 does not yet generate. */
14623 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14624 if (attr)
14625 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14626 else
14627 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14628 arg_type = die_type (child_die, cu);
14629
14630 /* RealView does not mark THIS as const, which the testsuite
14631 expects. GCC marks THIS as const in method definitions,
14632 but not in the class specifications (GCC PR 43053). */
14633 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14634 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14635 {
14636 int is_this = 0;
14637 struct dwarf2_cu *arg_cu = cu;
14638 const char *name = dwarf2_name (child_die, cu);
14639
14640 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14641 if (attr)
14642 {
14643 /* If the compiler emits this, use it. */
14644 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14645 is_this = 1;
14646 }
14647 else if (name && strcmp (name, "this") == 0)
14648 /* Function definitions will have the argument names. */
14649 is_this = 1;
14650 else if (name == NULL && iparams == 0)
14651 /* Declarations may not have the names, so like
14652 elsewhere in GDB, assume an artificial first
14653 argument is "this". */
14654 is_this = 1;
14655
14656 if (is_this)
14657 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14658 arg_type, 0);
14659 }
14660
14661 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14662 iparams++;
14663 }
14664 child_die = sibling_die (child_die);
14665 }
14666 }
14667
14668 return ftype;
14669 }
14670
14671 static struct type *
14672 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14673 {
14674 struct objfile *objfile = cu->objfile;
14675 const char *name = NULL;
14676 struct type *this_type, *target_type;
14677
14678 name = dwarf2_full_name (NULL, die, cu);
14679 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14680 TYPE_TARGET_STUB (this_type) = 1;
14681 set_die_type (die, this_type, cu);
14682 target_type = die_type (die, cu);
14683 if (target_type != this_type)
14684 TYPE_TARGET_TYPE (this_type) = target_type;
14685 else
14686 {
14687 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14688 spec and cause infinite loops in GDB. */
14689 complaint (&symfile_complaints,
14690 _("Self-referential DW_TAG_typedef "
14691 "- DIE at 0x%x [in module %s]"),
14692 die->offset.sect_off, objfile_name (objfile));
14693 TYPE_TARGET_TYPE (this_type) = NULL;
14694 }
14695 return this_type;
14696 }
14697
14698 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14699 (which may be different from NAME) to the architecture back-end to allow
14700 it to guess the correct format if necessary. */
14701
14702 static struct type *
14703 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
14704 const char *name_hint)
14705 {
14706 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14707 const struct floatformat **format;
14708 struct type *type;
14709
14710 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14711 if (format)
14712 type = init_float_type (objfile, bits, name, format);
14713 else
14714 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
14715
14716 return type;
14717 }
14718
14719 /* Find a representation of a given base type and install
14720 it in the TYPE field of the die. */
14721
14722 static struct type *
14723 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
14724 {
14725 struct objfile *objfile = cu->objfile;
14726 struct type *type;
14727 struct attribute *attr;
14728 int encoding = 0, bits = 0;
14729 const char *name;
14730
14731 attr = dwarf2_attr (die, DW_AT_encoding, cu);
14732 if (attr)
14733 {
14734 encoding = DW_UNSND (attr);
14735 }
14736 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14737 if (attr)
14738 {
14739 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
14740 }
14741 name = dwarf2_name (die, cu);
14742 if (!name)
14743 {
14744 complaint (&symfile_complaints,
14745 _("DW_AT_name missing from DW_TAG_base_type"));
14746 }
14747
14748 switch (encoding)
14749 {
14750 case DW_ATE_address:
14751 /* Turn DW_ATE_address into a void * pointer. */
14752 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
14753 type = init_pointer_type (objfile, bits, name, type);
14754 break;
14755 case DW_ATE_boolean:
14756 type = init_boolean_type (objfile, bits, 1, name);
14757 break;
14758 case DW_ATE_complex_float:
14759 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
14760 type = init_complex_type (objfile, name, type);
14761 break;
14762 case DW_ATE_decimal_float:
14763 type = init_decfloat_type (objfile, bits, name);
14764 break;
14765 case DW_ATE_float:
14766 type = dwarf2_init_float_type (objfile, bits, name, name);
14767 break;
14768 case DW_ATE_signed:
14769 type = init_integer_type (objfile, bits, 0, name);
14770 break;
14771 case DW_ATE_unsigned:
14772 if (cu->language == language_fortran
14773 && name
14774 && startswith (name, "character("))
14775 type = init_character_type (objfile, bits, 1, name);
14776 else
14777 type = init_integer_type (objfile, bits, 1, name);
14778 break;
14779 case DW_ATE_signed_char:
14780 if (cu->language == language_ada || cu->language == language_m2
14781 || cu->language == language_pascal
14782 || cu->language == language_fortran)
14783 type = init_character_type (objfile, bits, 0, name);
14784 else
14785 type = init_integer_type (objfile, bits, 0, name);
14786 break;
14787 case DW_ATE_unsigned_char:
14788 if (cu->language == language_ada || cu->language == language_m2
14789 || cu->language == language_pascal
14790 || cu->language == language_fortran
14791 || cu->language == language_rust)
14792 type = init_character_type (objfile, bits, 1, name);
14793 else
14794 type = init_integer_type (objfile, bits, 1, name);
14795 break;
14796 case DW_ATE_UTF:
14797 /* We just treat this as an integer and then recognize the
14798 type by name elsewhere. */
14799 type = init_integer_type (objfile, bits, 0, name);
14800 break;
14801
14802 default:
14803 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
14804 dwarf_type_encoding_name (encoding));
14805 type = init_type (objfile, TYPE_CODE_ERROR,
14806 bits / TARGET_CHAR_BIT, name);
14807 break;
14808 }
14809
14810 if (name && strcmp (name, "char") == 0)
14811 TYPE_NOSIGN (type) = 1;
14812
14813 return set_die_type (die, type, cu);
14814 }
14815
14816 /* Parse dwarf attribute if it's a block, reference or constant and put the
14817 resulting value of the attribute into struct bound_prop.
14818 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
14819
14820 static int
14821 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
14822 struct dwarf2_cu *cu, struct dynamic_prop *prop)
14823 {
14824 struct dwarf2_property_baton *baton;
14825 struct obstack *obstack = &cu->objfile->objfile_obstack;
14826
14827 if (attr == NULL || prop == NULL)
14828 return 0;
14829
14830 if (attr_form_is_block (attr))
14831 {
14832 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14833 baton->referenced_type = NULL;
14834 baton->locexpr.per_cu = cu->per_cu;
14835 baton->locexpr.size = DW_BLOCK (attr)->size;
14836 baton->locexpr.data = DW_BLOCK (attr)->data;
14837 prop->data.baton = baton;
14838 prop->kind = PROP_LOCEXPR;
14839 gdb_assert (prop->data.baton != NULL);
14840 }
14841 else if (attr_form_is_ref (attr))
14842 {
14843 struct dwarf2_cu *target_cu = cu;
14844 struct die_info *target_die;
14845 struct attribute *target_attr;
14846
14847 target_die = follow_die_ref (die, attr, &target_cu);
14848 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
14849 if (target_attr == NULL)
14850 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
14851 target_cu);
14852 if (target_attr == NULL)
14853 return 0;
14854
14855 switch (target_attr->name)
14856 {
14857 case DW_AT_location:
14858 if (attr_form_is_section_offset (target_attr))
14859 {
14860 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14861 baton->referenced_type = die_type (target_die, target_cu);
14862 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
14863 prop->data.baton = baton;
14864 prop->kind = PROP_LOCLIST;
14865 gdb_assert (prop->data.baton != NULL);
14866 }
14867 else if (attr_form_is_block (target_attr))
14868 {
14869 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14870 baton->referenced_type = die_type (target_die, target_cu);
14871 baton->locexpr.per_cu = cu->per_cu;
14872 baton->locexpr.size = DW_BLOCK (target_attr)->size;
14873 baton->locexpr.data = DW_BLOCK (target_attr)->data;
14874 prop->data.baton = baton;
14875 prop->kind = PROP_LOCEXPR;
14876 gdb_assert (prop->data.baton != NULL);
14877 }
14878 else
14879 {
14880 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14881 "dynamic property");
14882 return 0;
14883 }
14884 break;
14885 case DW_AT_data_member_location:
14886 {
14887 LONGEST offset;
14888
14889 if (!handle_data_member_location (target_die, target_cu,
14890 &offset))
14891 return 0;
14892
14893 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14894 baton->referenced_type = read_type_die (target_die->parent,
14895 target_cu);
14896 baton->offset_info.offset = offset;
14897 baton->offset_info.type = die_type (target_die, target_cu);
14898 prop->data.baton = baton;
14899 prop->kind = PROP_ADDR_OFFSET;
14900 break;
14901 }
14902 }
14903 }
14904 else if (attr_form_is_constant (attr))
14905 {
14906 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
14907 prop->kind = PROP_CONST;
14908 }
14909 else
14910 {
14911 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
14912 dwarf2_name (die, cu));
14913 return 0;
14914 }
14915
14916 return 1;
14917 }
14918
14919 /* Read the given DW_AT_subrange DIE. */
14920
14921 static struct type *
14922 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
14923 {
14924 struct type *base_type, *orig_base_type;
14925 struct type *range_type;
14926 struct attribute *attr;
14927 struct dynamic_prop low, high;
14928 int low_default_is_valid;
14929 int high_bound_is_count = 0;
14930 const char *name;
14931 LONGEST negative_mask;
14932
14933 orig_base_type = die_type (die, cu);
14934 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
14935 whereas the real type might be. So, we use ORIG_BASE_TYPE when
14936 creating the range type, but we use the result of check_typedef
14937 when examining properties of the type. */
14938 base_type = check_typedef (orig_base_type);
14939
14940 /* The die_type call above may have already set the type for this DIE. */
14941 range_type = get_die_type (die, cu);
14942 if (range_type)
14943 return range_type;
14944
14945 low.kind = PROP_CONST;
14946 high.kind = PROP_CONST;
14947 high.data.const_val = 0;
14948
14949 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
14950 omitting DW_AT_lower_bound. */
14951 switch (cu->language)
14952 {
14953 case language_c:
14954 case language_cplus:
14955 low.data.const_val = 0;
14956 low_default_is_valid = 1;
14957 break;
14958 case language_fortran:
14959 low.data.const_val = 1;
14960 low_default_is_valid = 1;
14961 break;
14962 case language_d:
14963 case language_objc:
14964 case language_rust:
14965 low.data.const_val = 0;
14966 low_default_is_valid = (cu->header.version >= 4);
14967 break;
14968 case language_ada:
14969 case language_m2:
14970 case language_pascal:
14971 low.data.const_val = 1;
14972 low_default_is_valid = (cu->header.version >= 4);
14973 break;
14974 default:
14975 low.data.const_val = 0;
14976 low_default_is_valid = 0;
14977 break;
14978 }
14979
14980 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
14981 if (attr)
14982 attr_to_dynamic_prop (attr, die, cu, &low);
14983 else if (!low_default_is_valid)
14984 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
14985 "- DIE at 0x%x [in module %s]"),
14986 die->offset.sect_off, objfile_name (cu->objfile));
14987
14988 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
14989 if (!attr_to_dynamic_prop (attr, die, cu, &high))
14990 {
14991 attr = dwarf2_attr (die, DW_AT_count, cu);
14992 if (attr_to_dynamic_prop (attr, die, cu, &high))
14993 {
14994 /* If bounds are constant do the final calculation here. */
14995 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
14996 high.data.const_val = low.data.const_val + high.data.const_val - 1;
14997 else
14998 high_bound_is_count = 1;
14999 }
15000 }
15001
15002 /* Dwarf-2 specifications explicitly allows to create subrange types
15003 without specifying a base type.
15004 In that case, the base type must be set to the type of
15005 the lower bound, upper bound or count, in that order, if any of these
15006 three attributes references an object that has a type.
15007 If no base type is found, the Dwarf-2 specifications say that
15008 a signed integer type of size equal to the size of an address should
15009 be used.
15010 For the following C code: `extern char gdb_int [];'
15011 GCC produces an empty range DIE.
15012 FIXME: muller/2010-05-28: Possible references to object for low bound,
15013 high bound or count are not yet handled by this code. */
15014 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15015 {
15016 struct objfile *objfile = cu->objfile;
15017 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15018 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15019 struct type *int_type = objfile_type (objfile)->builtin_int;
15020
15021 /* Test "int", "long int", and "long long int" objfile types,
15022 and select the first one having a size above or equal to the
15023 architecture address size. */
15024 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15025 base_type = int_type;
15026 else
15027 {
15028 int_type = objfile_type (objfile)->builtin_long;
15029 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15030 base_type = int_type;
15031 else
15032 {
15033 int_type = objfile_type (objfile)->builtin_long_long;
15034 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15035 base_type = int_type;
15036 }
15037 }
15038 }
15039
15040 /* Normally, the DWARF producers are expected to use a signed
15041 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15042 But this is unfortunately not always the case, as witnessed
15043 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15044 is used instead. To work around that ambiguity, we treat
15045 the bounds as signed, and thus sign-extend their values, when
15046 the base type is signed. */
15047 negative_mask =
15048 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15049 if (low.kind == PROP_CONST
15050 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15051 low.data.const_val |= negative_mask;
15052 if (high.kind == PROP_CONST
15053 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15054 high.data.const_val |= negative_mask;
15055
15056 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15057
15058 if (high_bound_is_count)
15059 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15060
15061 /* Ada expects an empty array on no boundary attributes. */
15062 if (attr == NULL && cu->language != language_ada)
15063 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15064
15065 name = dwarf2_name (die, cu);
15066 if (name)
15067 TYPE_NAME (range_type) = name;
15068
15069 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15070 if (attr)
15071 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15072
15073 set_die_type (die, range_type, cu);
15074
15075 /* set_die_type should be already done. */
15076 set_descriptive_type (range_type, die, cu);
15077
15078 return range_type;
15079 }
15080
15081 static struct type *
15082 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15083 {
15084 struct type *type;
15085
15086 /* For now, we only support the C meaning of an unspecified type: void. */
15087
15088 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15089 TYPE_NAME (type) = dwarf2_name (die, cu);
15090
15091 return set_die_type (die, type, cu);
15092 }
15093
15094 /* Read a single die and all its descendents. Set the die's sibling
15095 field to NULL; set other fields in the die correctly, and set all
15096 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15097 location of the info_ptr after reading all of those dies. PARENT
15098 is the parent of the die in question. */
15099
15100 static struct die_info *
15101 read_die_and_children (const struct die_reader_specs *reader,
15102 const gdb_byte *info_ptr,
15103 const gdb_byte **new_info_ptr,
15104 struct die_info *parent)
15105 {
15106 struct die_info *die;
15107 const gdb_byte *cur_ptr;
15108 int has_children;
15109
15110 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15111 if (die == NULL)
15112 {
15113 *new_info_ptr = cur_ptr;
15114 return NULL;
15115 }
15116 store_in_ref_table (die, reader->cu);
15117
15118 if (has_children)
15119 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15120 else
15121 {
15122 die->child = NULL;
15123 *new_info_ptr = cur_ptr;
15124 }
15125
15126 die->sibling = NULL;
15127 die->parent = parent;
15128 return die;
15129 }
15130
15131 /* Read a die, all of its descendents, and all of its siblings; set
15132 all of the fields of all of the dies correctly. Arguments are as
15133 in read_die_and_children. */
15134
15135 static struct die_info *
15136 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15137 const gdb_byte *info_ptr,
15138 const gdb_byte **new_info_ptr,
15139 struct die_info *parent)
15140 {
15141 struct die_info *first_die, *last_sibling;
15142 const gdb_byte *cur_ptr;
15143
15144 cur_ptr = info_ptr;
15145 first_die = last_sibling = NULL;
15146
15147 while (1)
15148 {
15149 struct die_info *die
15150 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15151
15152 if (die == NULL)
15153 {
15154 *new_info_ptr = cur_ptr;
15155 return first_die;
15156 }
15157
15158 if (!first_die)
15159 first_die = die;
15160 else
15161 last_sibling->sibling = die;
15162
15163 last_sibling = die;
15164 }
15165 }
15166
15167 /* Read a die, all of its descendents, and all of its siblings; set
15168 all of the fields of all of the dies correctly. Arguments are as
15169 in read_die_and_children.
15170 This the main entry point for reading a DIE and all its children. */
15171
15172 static struct die_info *
15173 read_die_and_siblings (const struct die_reader_specs *reader,
15174 const gdb_byte *info_ptr,
15175 const gdb_byte **new_info_ptr,
15176 struct die_info *parent)
15177 {
15178 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15179 new_info_ptr, parent);
15180
15181 if (dwarf_die_debug)
15182 {
15183 fprintf_unfiltered (gdb_stdlog,
15184 "Read die from %s@0x%x of %s:\n",
15185 get_section_name (reader->die_section),
15186 (unsigned) (info_ptr - reader->die_section->buffer),
15187 bfd_get_filename (reader->abfd));
15188 dump_die (die, dwarf_die_debug);
15189 }
15190
15191 return die;
15192 }
15193
15194 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15195 attributes.
15196 The caller is responsible for filling in the extra attributes
15197 and updating (*DIEP)->num_attrs.
15198 Set DIEP to point to a newly allocated die with its information,
15199 except for its child, sibling, and parent fields.
15200 Set HAS_CHILDREN to tell whether the die has children or not. */
15201
15202 static const gdb_byte *
15203 read_full_die_1 (const struct die_reader_specs *reader,
15204 struct die_info **diep, const gdb_byte *info_ptr,
15205 int *has_children, int num_extra_attrs)
15206 {
15207 unsigned int abbrev_number, bytes_read, i;
15208 sect_offset offset;
15209 struct abbrev_info *abbrev;
15210 struct die_info *die;
15211 struct dwarf2_cu *cu = reader->cu;
15212 bfd *abfd = reader->abfd;
15213
15214 offset.sect_off = info_ptr - reader->buffer;
15215 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15216 info_ptr += bytes_read;
15217 if (!abbrev_number)
15218 {
15219 *diep = NULL;
15220 *has_children = 0;
15221 return info_ptr;
15222 }
15223
15224 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15225 if (!abbrev)
15226 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15227 abbrev_number,
15228 bfd_get_filename (abfd));
15229
15230 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15231 die->offset = offset;
15232 die->tag = abbrev->tag;
15233 die->abbrev = abbrev_number;
15234
15235 /* Make the result usable.
15236 The caller needs to update num_attrs after adding the extra
15237 attributes. */
15238 die->num_attrs = abbrev->num_attrs;
15239
15240 for (i = 0; i < abbrev->num_attrs; ++i)
15241 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15242 info_ptr);
15243
15244 *diep = die;
15245 *has_children = abbrev->has_children;
15246 return info_ptr;
15247 }
15248
15249 /* Read a die and all its attributes.
15250 Set DIEP to point to a newly allocated die with its information,
15251 except for its child, sibling, and parent fields.
15252 Set HAS_CHILDREN to tell whether the die has children or not. */
15253
15254 static const gdb_byte *
15255 read_full_die (const struct die_reader_specs *reader,
15256 struct die_info **diep, const gdb_byte *info_ptr,
15257 int *has_children)
15258 {
15259 const gdb_byte *result;
15260
15261 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15262
15263 if (dwarf_die_debug)
15264 {
15265 fprintf_unfiltered (gdb_stdlog,
15266 "Read die from %s@0x%x of %s:\n",
15267 get_section_name (reader->die_section),
15268 (unsigned) (info_ptr - reader->die_section->buffer),
15269 bfd_get_filename (reader->abfd));
15270 dump_die (*diep, dwarf_die_debug);
15271 }
15272
15273 return result;
15274 }
15275 \f
15276 /* Abbreviation tables.
15277
15278 In DWARF version 2, the description of the debugging information is
15279 stored in a separate .debug_abbrev section. Before we read any
15280 dies from a section we read in all abbreviations and install them
15281 in a hash table. */
15282
15283 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15284
15285 static struct abbrev_info *
15286 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15287 {
15288 struct abbrev_info *abbrev;
15289
15290 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15291 memset (abbrev, 0, sizeof (struct abbrev_info));
15292
15293 return abbrev;
15294 }
15295
15296 /* Add an abbreviation to the table. */
15297
15298 static void
15299 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15300 unsigned int abbrev_number,
15301 struct abbrev_info *abbrev)
15302 {
15303 unsigned int hash_number;
15304
15305 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15306 abbrev->next = abbrev_table->abbrevs[hash_number];
15307 abbrev_table->abbrevs[hash_number] = abbrev;
15308 }
15309
15310 /* Look up an abbrev in the table.
15311 Returns NULL if the abbrev is not found. */
15312
15313 static struct abbrev_info *
15314 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15315 unsigned int abbrev_number)
15316 {
15317 unsigned int hash_number;
15318 struct abbrev_info *abbrev;
15319
15320 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15321 abbrev = abbrev_table->abbrevs[hash_number];
15322
15323 while (abbrev)
15324 {
15325 if (abbrev->number == abbrev_number)
15326 return abbrev;
15327 abbrev = abbrev->next;
15328 }
15329 return NULL;
15330 }
15331
15332 /* Read in an abbrev table. */
15333
15334 static struct abbrev_table *
15335 abbrev_table_read_table (struct dwarf2_section_info *section,
15336 sect_offset offset)
15337 {
15338 struct objfile *objfile = dwarf2_per_objfile->objfile;
15339 bfd *abfd = get_section_bfd_owner (section);
15340 struct abbrev_table *abbrev_table;
15341 const gdb_byte *abbrev_ptr;
15342 struct abbrev_info *cur_abbrev;
15343 unsigned int abbrev_number, bytes_read, abbrev_name;
15344 unsigned int abbrev_form;
15345 struct attr_abbrev *cur_attrs;
15346 unsigned int allocated_attrs;
15347
15348 abbrev_table = XNEW (struct abbrev_table);
15349 abbrev_table->offset = offset;
15350 obstack_init (&abbrev_table->abbrev_obstack);
15351 abbrev_table->abbrevs =
15352 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15353 ABBREV_HASH_SIZE);
15354 memset (abbrev_table->abbrevs, 0,
15355 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15356
15357 dwarf2_read_section (objfile, section);
15358 abbrev_ptr = section->buffer + offset.sect_off;
15359 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15360 abbrev_ptr += bytes_read;
15361
15362 allocated_attrs = ATTR_ALLOC_CHUNK;
15363 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15364
15365 /* Loop until we reach an abbrev number of 0. */
15366 while (abbrev_number)
15367 {
15368 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15369
15370 /* read in abbrev header */
15371 cur_abbrev->number = abbrev_number;
15372 cur_abbrev->tag
15373 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15374 abbrev_ptr += bytes_read;
15375 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15376 abbrev_ptr += 1;
15377
15378 /* now read in declarations */
15379 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15380 abbrev_ptr += bytes_read;
15381 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15382 abbrev_ptr += bytes_read;
15383 while (abbrev_name)
15384 {
15385 if (cur_abbrev->num_attrs == allocated_attrs)
15386 {
15387 allocated_attrs += ATTR_ALLOC_CHUNK;
15388 cur_attrs
15389 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15390 }
15391
15392 cur_attrs[cur_abbrev->num_attrs].name
15393 = (enum dwarf_attribute) abbrev_name;
15394 cur_attrs[cur_abbrev->num_attrs++].form
15395 = (enum dwarf_form) abbrev_form;
15396 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15397 abbrev_ptr += bytes_read;
15398 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15399 abbrev_ptr += bytes_read;
15400 }
15401
15402 cur_abbrev->attrs =
15403 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15404 cur_abbrev->num_attrs);
15405 memcpy (cur_abbrev->attrs, cur_attrs,
15406 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15407
15408 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15409
15410 /* Get next abbreviation.
15411 Under Irix6 the abbreviations for a compilation unit are not
15412 always properly terminated with an abbrev number of 0.
15413 Exit loop if we encounter an abbreviation which we have
15414 already read (which means we are about to read the abbreviations
15415 for the next compile unit) or if the end of the abbreviation
15416 table is reached. */
15417 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15418 break;
15419 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15420 abbrev_ptr += bytes_read;
15421 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15422 break;
15423 }
15424
15425 xfree (cur_attrs);
15426 return abbrev_table;
15427 }
15428
15429 /* Free the resources held by ABBREV_TABLE. */
15430
15431 static void
15432 abbrev_table_free (struct abbrev_table *abbrev_table)
15433 {
15434 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15435 xfree (abbrev_table);
15436 }
15437
15438 /* Same as abbrev_table_free but as a cleanup.
15439 We pass in a pointer to the pointer to the table so that we can
15440 set the pointer to NULL when we're done. It also simplifies
15441 build_type_psymtabs_1. */
15442
15443 static void
15444 abbrev_table_free_cleanup (void *table_ptr)
15445 {
15446 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15447
15448 if (*abbrev_table_ptr != NULL)
15449 abbrev_table_free (*abbrev_table_ptr);
15450 *abbrev_table_ptr = NULL;
15451 }
15452
15453 /* Read the abbrev table for CU from ABBREV_SECTION. */
15454
15455 static void
15456 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15457 struct dwarf2_section_info *abbrev_section)
15458 {
15459 cu->abbrev_table =
15460 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15461 }
15462
15463 /* Release the memory used by the abbrev table for a compilation unit. */
15464
15465 static void
15466 dwarf2_free_abbrev_table (void *ptr_to_cu)
15467 {
15468 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15469
15470 if (cu->abbrev_table != NULL)
15471 abbrev_table_free (cu->abbrev_table);
15472 /* Set this to NULL so that we SEGV if we try to read it later,
15473 and also because free_comp_unit verifies this is NULL. */
15474 cu->abbrev_table = NULL;
15475 }
15476 \f
15477 /* Returns nonzero if TAG represents a type that we might generate a partial
15478 symbol for. */
15479
15480 static int
15481 is_type_tag_for_partial (int tag)
15482 {
15483 switch (tag)
15484 {
15485 #if 0
15486 /* Some types that would be reasonable to generate partial symbols for,
15487 that we don't at present. */
15488 case DW_TAG_array_type:
15489 case DW_TAG_file_type:
15490 case DW_TAG_ptr_to_member_type:
15491 case DW_TAG_set_type:
15492 case DW_TAG_string_type:
15493 case DW_TAG_subroutine_type:
15494 #endif
15495 case DW_TAG_base_type:
15496 case DW_TAG_class_type:
15497 case DW_TAG_interface_type:
15498 case DW_TAG_enumeration_type:
15499 case DW_TAG_structure_type:
15500 case DW_TAG_subrange_type:
15501 case DW_TAG_typedef:
15502 case DW_TAG_union_type:
15503 return 1;
15504 default:
15505 return 0;
15506 }
15507 }
15508
15509 /* Load all DIEs that are interesting for partial symbols into memory. */
15510
15511 static struct partial_die_info *
15512 load_partial_dies (const struct die_reader_specs *reader,
15513 const gdb_byte *info_ptr, int building_psymtab)
15514 {
15515 struct dwarf2_cu *cu = reader->cu;
15516 struct objfile *objfile = cu->objfile;
15517 struct partial_die_info *part_die;
15518 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15519 struct abbrev_info *abbrev;
15520 unsigned int bytes_read;
15521 unsigned int load_all = 0;
15522 int nesting_level = 1;
15523
15524 parent_die = NULL;
15525 last_die = NULL;
15526
15527 gdb_assert (cu->per_cu != NULL);
15528 if (cu->per_cu->load_all_dies)
15529 load_all = 1;
15530
15531 cu->partial_dies
15532 = htab_create_alloc_ex (cu->header.length / 12,
15533 partial_die_hash,
15534 partial_die_eq,
15535 NULL,
15536 &cu->comp_unit_obstack,
15537 hashtab_obstack_allocate,
15538 dummy_obstack_deallocate);
15539
15540 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15541
15542 while (1)
15543 {
15544 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15545
15546 /* A NULL abbrev means the end of a series of children. */
15547 if (abbrev == NULL)
15548 {
15549 if (--nesting_level == 0)
15550 {
15551 /* PART_DIE was probably the last thing allocated on the
15552 comp_unit_obstack, so we could call obstack_free
15553 here. We don't do that because the waste is small,
15554 and will be cleaned up when we're done with this
15555 compilation unit. This way, we're also more robust
15556 against other users of the comp_unit_obstack. */
15557 return first_die;
15558 }
15559 info_ptr += bytes_read;
15560 last_die = parent_die;
15561 parent_die = parent_die->die_parent;
15562 continue;
15563 }
15564
15565 /* Check for template arguments. We never save these; if
15566 they're seen, we just mark the parent, and go on our way. */
15567 if (parent_die != NULL
15568 && cu->language == language_cplus
15569 && (abbrev->tag == DW_TAG_template_type_param
15570 || abbrev->tag == DW_TAG_template_value_param))
15571 {
15572 parent_die->has_template_arguments = 1;
15573
15574 if (!load_all)
15575 {
15576 /* We don't need a partial DIE for the template argument. */
15577 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15578 continue;
15579 }
15580 }
15581
15582 /* We only recurse into c++ subprograms looking for template arguments.
15583 Skip their other children. */
15584 if (!load_all
15585 && cu->language == language_cplus
15586 && parent_die != NULL
15587 && parent_die->tag == DW_TAG_subprogram)
15588 {
15589 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15590 continue;
15591 }
15592
15593 /* Check whether this DIE is interesting enough to save. Normally
15594 we would not be interested in members here, but there may be
15595 later variables referencing them via DW_AT_specification (for
15596 static members). */
15597 if (!load_all
15598 && !is_type_tag_for_partial (abbrev->tag)
15599 && abbrev->tag != DW_TAG_constant
15600 && abbrev->tag != DW_TAG_enumerator
15601 && abbrev->tag != DW_TAG_subprogram
15602 && abbrev->tag != DW_TAG_lexical_block
15603 && abbrev->tag != DW_TAG_variable
15604 && abbrev->tag != DW_TAG_namespace
15605 && abbrev->tag != DW_TAG_module
15606 && abbrev->tag != DW_TAG_member
15607 && abbrev->tag != DW_TAG_imported_unit
15608 && abbrev->tag != DW_TAG_imported_declaration)
15609 {
15610 /* Otherwise we skip to the next sibling, if any. */
15611 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15612 continue;
15613 }
15614
15615 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15616 info_ptr);
15617
15618 /* This two-pass algorithm for processing partial symbols has a
15619 high cost in cache pressure. Thus, handle some simple cases
15620 here which cover the majority of C partial symbols. DIEs
15621 which neither have specification tags in them, nor could have
15622 specification tags elsewhere pointing at them, can simply be
15623 processed and discarded.
15624
15625 This segment is also optional; scan_partial_symbols and
15626 add_partial_symbol will handle these DIEs if we chain
15627 them in normally. When compilers which do not emit large
15628 quantities of duplicate debug information are more common,
15629 this code can probably be removed. */
15630
15631 /* Any complete simple types at the top level (pretty much all
15632 of them, for a language without namespaces), can be processed
15633 directly. */
15634 if (parent_die == NULL
15635 && part_die->has_specification == 0
15636 && part_die->is_declaration == 0
15637 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15638 || part_die->tag == DW_TAG_base_type
15639 || part_die->tag == DW_TAG_subrange_type))
15640 {
15641 if (building_psymtab && part_die->name != NULL)
15642 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15643 VAR_DOMAIN, LOC_TYPEDEF,
15644 &objfile->static_psymbols,
15645 0, cu->language, objfile);
15646 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15647 continue;
15648 }
15649
15650 /* The exception for DW_TAG_typedef with has_children above is
15651 a workaround of GCC PR debug/47510. In the case of this complaint
15652 type_name_no_tag_or_error will error on such types later.
15653
15654 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15655 it could not find the child DIEs referenced later, this is checked
15656 above. In correct DWARF DW_TAG_typedef should have no children. */
15657
15658 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15659 complaint (&symfile_complaints,
15660 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15661 "- DIE at 0x%x [in module %s]"),
15662 part_die->offset.sect_off, objfile_name (objfile));
15663
15664 /* If we're at the second level, and we're an enumerator, and
15665 our parent has no specification (meaning possibly lives in a
15666 namespace elsewhere), then we can add the partial symbol now
15667 instead of queueing it. */
15668 if (part_die->tag == DW_TAG_enumerator
15669 && parent_die != NULL
15670 && parent_die->die_parent == NULL
15671 && parent_die->tag == DW_TAG_enumeration_type
15672 && parent_die->has_specification == 0)
15673 {
15674 if (part_die->name == NULL)
15675 complaint (&symfile_complaints,
15676 _("malformed enumerator DIE ignored"));
15677 else if (building_psymtab)
15678 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15679 VAR_DOMAIN, LOC_CONST,
15680 cu->language == language_cplus
15681 ? &objfile->global_psymbols
15682 : &objfile->static_psymbols,
15683 0, cu->language, objfile);
15684
15685 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15686 continue;
15687 }
15688
15689 /* We'll save this DIE so link it in. */
15690 part_die->die_parent = parent_die;
15691 part_die->die_sibling = NULL;
15692 part_die->die_child = NULL;
15693
15694 if (last_die && last_die == parent_die)
15695 last_die->die_child = part_die;
15696 else if (last_die)
15697 last_die->die_sibling = part_die;
15698
15699 last_die = part_die;
15700
15701 if (first_die == NULL)
15702 first_die = part_die;
15703
15704 /* Maybe add the DIE to the hash table. Not all DIEs that we
15705 find interesting need to be in the hash table, because we
15706 also have the parent/sibling/child chains; only those that we
15707 might refer to by offset later during partial symbol reading.
15708
15709 For now this means things that might have be the target of a
15710 DW_AT_specification, DW_AT_abstract_origin, or
15711 DW_AT_extension. DW_AT_extension will refer only to
15712 namespaces; DW_AT_abstract_origin refers to functions (and
15713 many things under the function DIE, but we do not recurse
15714 into function DIEs during partial symbol reading) and
15715 possibly variables as well; DW_AT_specification refers to
15716 declarations. Declarations ought to have the DW_AT_declaration
15717 flag. It happens that GCC forgets to put it in sometimes, but
15718 only for functions, not for types.
15719
15720 Adding more things than necessary to the hash table is harmless
15721 except for the performance cost. Adding too few will result in
15722 wasted time in find_partial_die, when we reread the compilation
15723 unit with load_all_dies set. */
15724
15725 if (load_all
15726 || abbrev->tag == DW_TAG_constant
15727 || abbrev->tag == DW_TAG_subprogram
15728 || abbrev->tag == DW_TAG_variable
15729 || abbrev->tag == DW_TAG_namespace
15730 || part_die->is_declaration)
15731 {
15732 void **slot;
15733
15734 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
15735 part_die->offset.sect_off, INSERT);
15736 *slot = part_die;
15737 }
15738
15739 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15740
15741 /* For some DIEs we want to follow their children (if any). For C
15742 we have no reason to follow the children of structures; for other
15743 languages we have to, so that we can get at method physnames
15744 to infer fully qualified class names, for DW_AT_specification,
15745 and for C++ template arguments. For C++, we also look one level
15746 inside functions to find template arguments (if the name of the
15747 function does not already contain the template arguments).
15748
15749 For Ada, we need to scan the children of subprograms and lexical
15750 blocks as well because Ada allows the definition of nested
15751 entities that could be interesting for the debugger, such as
15752 nested subprograms for instance. */
15753 if (last_die->has_children
15754 && (load_all
15755 || last_die->tag == DW_TAG_namespace
15756 || last_die->tag == DW_TAG_module
15757 || last_die->tag == DW_TAG_enumeration_type
15758 || (cu->language == language_cplus
15759 && last_die->tag == DW_TAG_subprogram
15760 && (last_die->name == NULL
15761 || strchr (last_die->name, '<') == NULL))
15762 || (cu->language != language_c
15763 && (last_die->tag == DW_TAG_class_type
15764 || last_die->tag == DW_TAG_interface_type
15765 || last_die->tag == DW_TAG_structure_type
15766 || last_die->tag == DW_TAG_union_type))
15767 || (cu->language == language_ada
15768 && (last_die->tag == DW_TAG_subprogram
15769 || last_die->tag == DW_TAG_lexical_block))))
15770 {
15771 nesting_level++;
15772 parent_die = last_die;
15773 continue;
15774 }
15775
15776 /* Otherwise we skip to the next sibling, if any. */
15777 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
15778
15779 /* Back to the top, do it again. */
15780 }
15781 }
15782
15783 /* Read a minimal amount of information into the minimal die structure. */
15784
15785 static const gdb_byte *
15786 read_partial_die (const struct die_reader_specs *reader,
15787 struct partial_die_info *part_die,
15788 struct abbrev_info *abbrev, unsigned int abbrev_len,
15789 const gdb_byte *info_ptr)
15790 {
15791 struct dwarf2_cu *cu = reader->cu;
15792 struct objfile *objfile = cu->objfile;
15793 const gdb_byte *buffer = reader->buffer;
15794 unsigned int i;
15795 struct attribute attr;
15796 int has_low_pc_attr = 0;
15797 int has_high_pc_attr = 0;
15798 int high_pc_relative = 0;
15799
15800 memset (part_die, 0, sizeof (struct partial_die_info));
15801
15802 part_die->offset.sect_off = info_ptr - buffer;
15803
15804 info_ptr += abbrev_len;
15805
15806 if (abbrev == NULL)
15807 return info_ptr;
15808
15809 part_die->tag = abbrev->tag;
15810 part_die->has_children = abbrev->has_children;
15811
15812 for (i = 0; i < abbrev->num_attrs; ++i)
15813 {
15814 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
15815
15816 /* Store the data if it is of an attribute we want to keep in a
15817 partial symbol table. */
15818 switch (attr.name)
15819 {
15820 case DW_AT_name:
15821 switch (part_die->tag)
15822 {
15823 case DW_TAG_compile_unit:
15824 case DW_TAG_partial_unit:
15825 case DW_TAG_type_unit:
15826 /* Compilation units have a DW_AT_name that is a filename, not
15827 a source language identifier. */
15828 case DW_TAG_enumeration_type:
15829 case DW_TAG_enumerator:
15830 /* These tags always have simple identifiers already; no need
15831 to canonicalize them. */
15832 part_die->name = DW_STRING (&attr);
15833 break;
15834 default:
15835 part_die->name
15836 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
15837 &objfile->per_bfd->storage_obstack);
15838 break;
15839 }
15840 break;
15841 case DW_AT_linkage_name:
15842 case DW_AT_MIPS_linkage_name:
15843 /* Note that both forms of linkage name might appear. We
15844 assume they will be the same, and we only store the last
15845 one we see. */
15846 if (cu->language == language_ada)
15847 part_die->name = DW_STRING (&attr);
15848 part_die->linkage_name = DW_STRING (&attr);
15849 break;
15850 case DW_AT_low_pc:
15851 has_low_pc_attr = 1;
15852 part_die->lowpc = attr_value_as_address (&attr);
15853 break;
15854 case DW_AT_high_pc:
15855 has_high_pc_attr = 1;
15856 part_die->highpc = attr_value_as_address (&attr);
15857 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
15858 high_pc_relative = 1;
15859 break;
15860 case DW_AT_location:
15861 /* Support the .debug_loc offsets. */
15862 if (attr_form_is_block (&attr))
15863 {
15864 part_die->d.locdesc = DW_BLOCK (&attr);
15865 }
15866 else if (attr_form_is_section_offset (&attr))
15867 {
15868 dwarf2_complex_location_expr_complaint ();
15869 }
15870 else
15871 {
15872 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15873 "partial symbol information");
15874 }
15875 break;
15876 case DW_AT_external:
15877 part_die->is_external = DW_UNSND (&attr);
15878 break;
15879 case DW_AT_declaration:
15880 part_die->is_declaration = DW_UNSND (&attr);
15881 break;
15882 case DW_AT_type:
15883 part_die->has_type = 1;
15884 break;
15885 case DW_AT_abstract_origin:
15886 case DW_AT_specification:
15887 case DW_AT_extension:
15888 part_die->has_specification = 1;
15889 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
15890 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15891 || cu->per_cu->is_dwz);
15892 break;
15893 case DW_AT_sibling:
15894 /* Ignore absolute siblings, they might point outside of
15895 the current compile unit. */
15896 if (attr.form == DW_FORM_ref_addr)
15897 complaint (&symfile_complaints,
15898 _("ignoring absolute DW_AT_sibling"));
15899 else
15900 {
15901 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
15902 const gdb_byte *sibling_ptr = buffer + off;
15903
15904 if (sibling_ptr < info_ptr)
15905 complaint (&symfile_complaints,
15906 _("DW_AT_sibling points backwards"));
15907 else if (sibling_ptr > reader->buffer_end)
15908 dwarf2_section_buffer_overflow_complaint (reader->die_section);
15909 else
15910 part_die->sibling = sibling_ptr;
15911 }
15912 break;
15913 case DW_AT_byte_size:
15914 part_die->has_byte_size = 1;
15915 break;
15916 case DW_AT_const_value:
15917 part_die->has_const_value = 1;
15918 break;
15919 case DW_AT_calling_convention:
15920 /* DWARF doesn't provide a way to identify a program's source-level
15921 entry point. DW_AT_calling_convention attributes are only meant
15922 to describe functions' calling conventions.
15923
15924 However, because it's a necessary piece of information in
15925 Fortran, and before DWARF 4 DW_CC_program was the only
15926 piece of debugging information whose definition refers to
15927 a 'main program' at all, several compilers marked Fortran
15928 main programs with DW_CC_program --- even when those
15929 functions use the standard calling conventions.
15930
15931 Although DWARF now specifies a way to provide this
15932 information, we support this practice for backward
15933 compatibility. */
15934 if (DW_UNSND (&attr) == DW_CC_program
15935 && cu->language == language_fortran)
15936 part_die->main_subprogram = 1;
15937 break;
15938 case DW_AT_inline:
15939 if (DW_UNSND (&attr) == DW_INL_inlined
15940 || DW_UNSND (&attr) == DW_INL_declared_inlined)
15941 part_die->may_be_inlined = 1;
15942 break;
15943
15944 case DW_AT_import:
15945 if (part_die->tag == DW_TAG_imported_unit)
15946 {
15947 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
15948 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15949 || cu->per_cu->is_dwz);
15950 }
15951 break;
15952
15953 case DW_AT_main_subprogram:
15954 part_die->main_subprogram = DW_UNSND (&attr);
15955 break;
15956
15957 default:
15958 break;
15959 }
15960 }
15961
15962 if (high_pc_relative)
15963 part_die->highpc += part_die->lowpc;
15964
15965 if (has_low_pc_attr && has_high_pc_attr)
15966 {
15967 /* When using the GNU linker, .gnu.linkonce. sections are used to
15968 eliminate duplicate copies of functions and vtables and such.
15969 The linker will arbitrarily choose one and discard the others.
15970 The AT_*_pc values for such functions refer to local labels in
15971 these sections. If the section from that file was discarded, the
15972 labels are not in the output, so the relocs get a value of 0.
15973 If this is a discarded function, mark the pc bounds as invalid,
15974 so that GDB will ignore it. */
15975 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
15976 {
15977 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15978
15979 complaint (&symfile_complaints,
15980 _("DW_AT_low_pc %s is zero "
15981 "for DIE at 0x%x [in module %s]"),
15982 paddress (gdbarch, part_die->lowpc),
15983 part_die->offset.sect_off, objfile_name (objfile));
15984 }
15985 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
15986 else if (part_die->lowpc >= part_die->highpc)
15987 {
15988 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15989
15990 complaint (&symfile_complaints,
15991 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
15992 "for DIE at 0x%x [in module %s]"),
15993 paddress (gdbarch, part_die->lowpc),
15994 paddress (gdbarch, part_die->highpc),
15995 part_die->offset.sect_off, objfile_name (objfile));
15996 }
15997 else
15998 part_die->has_pc_info = 1;
15999 }
16000
16001 return info_ptr;
16002 }
16003
16004 /* Find a cached partial DIE at OFFSET in CU. */
16005
16006 static struct partial_die_info *
16007 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
16008 {
16009 struct partial_die_info *lookup_die = NULL;
16010 struct partial_die_info part_die;
16011
16012 part_die.offset = offset;
16013 lookup_die = ((struct partial_die_info *)
16014 htab_find_with_hash (cu->partial_dies, &part_die,
16015 offset.sect_off));
16016
16017 return lookup_die;
16018 }
16019
16020 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16021 except in the case of .debug_types DIEs which do not reference
16022 outside their CU (they do however referencing other types via
16023 DW_FORM_ref_sig8). */
16024
16025 static struct partial_die_info *
16026 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
16027 {
16028 struct objfile *objfile = cu->objfile;
16029 struct dwarf2_per_cu_data *per_cu = NULL;
16030 struct partial_die_info *pd = NULL;
16031
16032 if (offset_in_dwz == cu->per_cu->is_dwz
16033 && offset_in_cu_p (&cu->header, offset))
16034 {
16035 pd = find_partial_die_in_comp_unit (offset, cu);
16036 if (pd != NULL)
16037 return pd;
16038 /* We missed recording what we needed.
16039 Load all dies and try again. */
16040 per_cu = cu->per_cu;
16041 }
16042 else
16043 {
16044 /* TUs don't reference other CUs/TUs (except via type signatures). */
16045 if (cu->per_cu->is_debug_types)
16046 {
16047 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16048 " external reference to offset 0x%lx [in module %s].\n"),
16049 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16050 bfd_get_filename (objfile->obfd));
16051 }
16052 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16053 objfile);
16054
16055 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16056 load_partial_comp_unit (per_cu);
16057
16058 per_cu->cu->last_used = 0;
16059 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16060 }
16061
16062 /* If we didn't find it, and not all dies have been loaded,
16063 load them all and try again. */
16064
16065 if (pd == NULL && per_cu->load_all_dies == 0)
16066 {
16067 per_cu->load_all_dies = 1;
16068
16069 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16070 THIS_CU->cu may already be in use. So we can't just free it and
16071 replace its DIEs with the ones we read in. Instead, we leave those
16072 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16073 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16074 set. */
16075 load_partial_comp_unit (per_cu);
16076
16077 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16078 }
16079
16080 if (pd == NULL)
16081 internal_error (__FILE__, __LINE__,
16082 _("could not find partial DIE 0x%x "
16083 "in cache [from module %s]\n"),
16084 offset.sect_off, bfd_get_filename (objfile->obfd));
16085 return pd;
16086 }
16087
16088 /* See if we can figure out if the class lives in a namespace. We do
16089 this by looking for a member function; its demangled name will
16090 contain namespace info, if there is any. */
16091
16092 static void
16093 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16094 struct dwarf2_cu *cu)
16095 {
16096 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16097 what template types look like, because the demangler
16098 frequently doesn't give the same name as the debug info. We
16099 could fix this by only using the demangled name to get the
16100 prefix (but see comment in read_structure_type). */
16101
16102 struct partial_die_info *real_pdi;
16103 struct partial_die_info *child_pdi;
16104
16105 /* If this DIE (this DIE's specification, if any) has a parent, then
16106 we should not do this. We'll prepend the parent's fully qualified
16107 name when we create the partial symbol. */
16108
16109 real_pdi = struct_pdi;
16110 while (real_pdi->has_specification)
16111 real_pdi = find_partial_die (real_pdi->spec_offset,
16112 real_pdi->spec_is_dwz, cu);
16113
16114 if (real_pdi->die_parent != NULL)
16115 return;
16116
16117 for (child_pdi = struct_pdi->die_child;
16118 child_pdi != NULL;
16119 child_pdi = child_pdi->die_sibling)
16120 {
16121 if (child_pdi->tag == DW_TAG_subprogram
16122 && child_pdi->linkage_name != NULL)
16123 {
16124 char *actual_class_name
16125 = language_class_name_from_physname (cu->language_defn,
16126 child_pdi->linkage_name);
16127 if (actual_class_name != NULL)
16128 {
16129 struct_pdi->name
16130 = ((const char *)
16131 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16132 actual_class_name,
16133 strlen (actual_class_name)));
16134 xfree (actual_class_name);
16135 }
16136 break;
16137 }
16138 }
16139 }
16140
16141 /* Adjust PART_DIE before generating a symbol for it. This function
16142 may set the is_external flag or change the DIE's name. */
16143
16144 static void
16145 fixup_partial_die (struct partial_die_info *part_die,
16146 struct dwarf2_cu *cu)
16147 {
16148 /* Once we've fixed up a die, there's no point in doing so again.
16149 This also avoids a memory leak if we were to call
16150 guess_partial_die_structure_name multiple times. */
16151 if (part_die->fixup_called)
16152 return;
16153
16154 /* If we found a reference attribute and the DIE has no name, try
16155 to find a name in the referred to DIE. */
16156
16157 if (part_die->name == NULL && part_die->has_specification)
16158 {
16159 struct partial_die_info *spec_die;
16160
16161 spec_die = find_partial_die (part_die->spec_offset,
16162 part_die->spec_is_dwz, cu);
16163
16164 fixup_partial_die (spec_die, cu);
16165
16166 if (spec_die->name)
16167 {
16168 part_die->name = spec_die->name;
16169
16170 /* Copy DW_AT_external attribute if it is set. */
16171 if (spec_die->is_external)
16172 part_die->is_external = spec_die->is_external;
16173 }
16174 }
16175
16176 /* Set default names for some unnamed DIEs. */
16177
16178 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16179 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16180
16181 /* If there is no parent die to provide a namespace, and there are
16182 children, see if we can determine the namespace from their linkage
16183 name. */
16184 if (cu->language == language_cplus
16185 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16186 && part_die->die_parent == NULL
16187 && part_die->has_children
16188 && (part_die->tag == DW_TAG_class_type
16189 || part_die->tag == DW_TAG_structure_type
16190 || part_die->tag == DW_TAG_union_type))
16191 guess_partial_die_structure_name (part_die, cu);
16192
16193 /* GCC might emit a nameless struct or union that has a linkage
16194 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16195 if (part_die->name == NULL
16196 && (part_die->tag == DW_TAG_class_type
16197 || part_die->tag == DW_TAG_interface_type
16198 || part_die->tag == DW_TAG_structure_type
16199 || part_die->tag == DW_TAG_union_type)
16200 && part_die->linkage_name != NULL)
16201 {
16202 char *demangled;
16203
16204 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16205 if (demangled)
16206 {
16207 const char *base;
16208
16209 /* Strip any leading namespaces/classes, keep only the base name.
16210 DW_AT_name for named DIEs does not contain the prefixes. */
16211 base = strrchr (demangled, ':');
16212 if (base && base > demangled && base[-1] == ':')
16213 base++;
16214 else
16215 base = demangled;
16216
16217 part_die->name
16218 = ((const char *)
16219 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16220 base, strlen (base)));
16221 xfree (demangled);
16222 }
16223 }
16224
16225 part_die->fixup_called = 1;
16226 }
16227
16228 /* Read an attribute value described by an attribute form. */
16229
16230 static const gdb_byte *
16231 read_attribute_value (const struct die_reader_specs *reader,
16232 struct attribute *attr, unsigned form,
16233 const gdb_byte *info_ptr)
16234 {
16235 struct dwarf2_cu *cu = reader->cu;
16236 struct objfile *objfile = cu->objfile;
16237 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16238 bfd *abfd = reader->abfd;
16239 struct comp_unit_head *cu_header = &cu->header;
16240 unsigned int bytes_read;
16241 struct dwarf_block *blk;
16242
16243 attr->form = (enum dwarf_form) form;
16244 switch (form)
16245 {
16246 case DW_FORM_ref_addr:
16247 if (cu->header.version == 2)
16248 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16249 else
16250 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16251 &cu->header, &bytes_read);
16252 info_ptr += bytes_read;
16253 break;
16254 case DW_FORM_GNU_ref_alt:
16255 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16256 info_ptr += bytes_read;
16257 break;
16258 case DW_FORM_addr:
16259 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16260 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16261 info_ptr += bytes_read;
16262 break;
16263 case DW_FORM_block2:
16264 blk = dwarf_alloc_block (cu);
16265 blk->size = read_2_bytes (abfd, info_ptr);
16266 info_ptr += 2;
16267 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16268 info_ptr += blk->size;
16269 DW_BLOCK (attr) = blk;
16270 break;
16271 case DW_FORM_block4:
16272 blk = dwarf_alloc_block (cu);
16273 blk->size = read_4_bytes (abfd, info_ptr);
16274 info_ptr += 4;
16275 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16276 info_ptr += blk->size;
16277 DW_BLOCK (attr) = blk;
16278 break;
16279 case DW_FORM_data2:
16280 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16281 info_ptr += 2;
16282 break;
16283 case DW_FORM_data4:
16284 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16285 info_ptr += 4;
16286 break;
16287 case DW_FORM_data8:
16288 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16289 info_ptr += 8;
16290 break;
16291 case DW_FORM_sec_offset:
16292 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16293 info_ptr += bytes_read;
16294 break;
16295 case DW_FORM_string:
16296 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16297 DW_STRING_IS_CANONICAL (attr) = 0;
16298 info_ptr += bytes_read;
16299 break;
16300 case DW_FORM_strp:
16301 if (!cu->per_cu->is_dwz)
16302 {
16303 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16304 &bytes_read);
16305 DW_STRING_IS_CANONICAL (attr) = 0;
16306 info_ptr += bytes_read;
16307 break;
16308 }
16309 /* FALLTHROUGH */
16310 case DW_FORM_GNU_strp_alt:
16311 {
16312 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16313 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16314 &bytes_read);
16315
16316 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16317 DW_STRING_IS_CANONICAL (attr) = 0;
16318 info_ptr += bytes_read;
16319 }
16320 break;
16321 case DW_FORM_exprloc:
16322 case DW_FORM_block:
16323 blk = dwarf_alloc_block (cu);
16324 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16325 info_ptr += bytes_read;
16326 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16327 info_ptr += blk->size;
16328 DW_BLOCK (attr) = blk;
16329 break;
16330 case DW_FORM_block1:
16331 blk = dwarf_alloc_block (cu);
16332 blk->size = read_1_byte (abfd, info_ptr);
16333 info_ptr += 1;
16334 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16335 info_ptr += blk->size;
16336 DW_BLOCK (attr) = blk;
16337 break;
16338 case DW_FORM_data1:
16339 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16340 info_ptr += 1;
16341 break;
16342 case DW_FORM_flag:
16343 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16344 info_ptr += 1;
16345 break;
16346 case DW_FORM_flag_present:
16347 DW_UNSND (attr) = 1;
16348 break;
16349 case DW_FORM_sdata:
16350 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16351 info_ptr += bytes_read;
16352 break;
16353 case DW_FORM_udata:
16354 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16355 info_ptr += bytes_read;
16356 break;
16357 case DW_FORM_ref1:
16358 DW_UNSND (attr) = (cu->header.offset.sect_off
16359 + read_1_byte (abfd, info_ptr));
16360 info_ptr += 1;
16361 break;
16362 case DW_FORM_ref2:
16363 DW_UNSND (attr) = (cu->header.offset.sect_off
16364 + read_2_bytes (abfd, info_ptr));
16365 info_ptr += 2;
16366 break;
16367 case DW_FORM_ref4:
16368 DW_UNSND (attr) = (cu->header.offset.sect_off
16369 + read_4_bytes (abfd, info_ptr));
16370 info_ptr += 4;
16371 break;
16372 case DW_FORM_ref8:
16373 DW_UNSND (attr) = (cu->header.offset.sect_off
16374 + read_8_bytes (abfd, info_ptr));
16375 info_ptr += 8;
16376 break;
16377 case DW_FORM_ref_sig8:
16378 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16379 info_ptr += 8;
16380 break;
16381 case DW_FORM_ref_udata:
16382 DW_UNSND (attr) = (cu->header.offset.sect_off
16383 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16384 info_ptr += bytes_read;
16385 break;
16386 case DW_FORM_indirect:
16387 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16388 info_ptr += bytes_read;
16389 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
16390 break;
16391 case DW_FORM_GNU_addr_index:
16392 if (reader->dwo_file == NULL)
16393 {
16394 /* For now flag a hard error.
16395 Later we can turn this into a complaint. */
16396 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16397 dwarf_form_name (form),
16398 bfd_get_filename (abfd));
16399 }
16400 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16401 info_ptr += bytes_read;
16402 break;
16403 case DW_FORM_GNU_str_index:
16404 if (reader->dwo_file == NULL)
16405 {
16406 /* For now flag a hard error.
16407 Later we can turn this into a complaint if warranted. */
16408 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16409 dwarf_form_name (form),
16410 bfd_get_filename (abfd));
16411 }
16412 {
16413 ULONGEST str_index =
16414 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16415
16416 DW_STRING (attr) = read_str_index (reader, str_index);
16417 DW_STRING_IS_CANONICAL (attr) = 0;
16418 info_ptr += bytes_read;
16419 }
16420 break;
16421 default:
16422 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16423 dwarf_form_name (form),
16424 bfd_get_filename (abfd));
16425 }
16426
16427 /* Super hack. */
16428 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16429 attr->form = DW_FORM_GNU_ref_alt;
16430
16431 /* We have seen instances where the compiler tried to emit a byte
16432 size attribute of -1 which ended up being encoded as an unsigned
16433 0xffffffff. Although 0xffffffff is technically a valid size value,
16434 an object of this size seems pretty unlikely so we can relatively
16435 safely treat these cases as if the size attribute was invalid and
16436 treat them as zero by default. */
16437 if (attr->name == DW_AT_byte_size
16438 && form == DW_FORM_data4
16439 && DW_UNSND (attr) >= 0xffffffff)
16440 {
16441 complaint
16442 (&symfile_complaints,
16443 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16444 hex_string (DW_UNSND (attr)));
16445 DW_UNSND (attr) = 0;
16446 }
16447
16448 return info_ptr;
16449 }
16450
16451 /* Read an attribute described by an abbreviated attribute. */
16452
16453 static const gdb_byte *
16454 read_attribute (const struct die_reader_specs *reader,
16455 struct attribute *attr, struct attr_abbrev *abbrev,
16456 const gdb_byte *info_ptr)
16457 {
16458 attr->name = abbrev->name;
16459 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
16460 }
16461
16462 /* Read dwarf information from a buffer. */
16463
16464 static unsigned int
16465 read_1_byte (bfd *abfd, const gdb_byte *buf)
16466 {
16467 return bfd_get_8 (abfd, buf);
16468 }
16469
16470 static int
16471 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16472 {
16473 return bfd_get_signed_8 (abfd, buf);
16474 }
16475
16476 static unsigned int
16477 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16478 {
16479 return bfd_get_16 (abfd, buf);
16480 }
16481
16482 static int
16483 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16484 {
16485 return bfd_get_signed_16 (abfd, buf);
16486 }
16487
16488 static unsigned int
16489 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16490 {
16491 return bfd_get_32 (abfd, buf);
16492 }
16493
16494 static int
16495 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16496 {
16497 return bfd_get_signed_32 (abfd, buf);
16498 }
16499
16500 static ULONGEST
16501 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16502 {
16503 return bfd_get_64 (abfd, buf);
16504 }
16505
16506 static CORE_ADDR
16507 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16508 unsigned int *bytes_read)
16509 {
16510 struct comp_unit_head *cu_header = &cu->header;
16511 CORE_ADDR retval = 0;
16512
16513 if (cu_header->signed_addr_p)
16514 {
16515 switch (cu_header->addr_size)
16516 {
16517 case 2:
16518 retval = bfd_get_signed_16 (abfd, buf);
16519 break;
16520 case 4:
16521 retval = bfd_get_signed_32 (abfd, buf);
16522 break;
16523 case 8:
16524 retval = bfd_get_signed_64 (abfd, buf);
16525 break;
16526 default:
16527 internal_error (__FILE__, __LINE__,
16528 _("read_address: bad switch, signed [in module %s]"),
16529 bfd_get_filename (abfd));
16530 }
16531 }
16532 else
16533 {
16534 switch (cu_header->addr_size)
16535 {
16536 case 2:
16537 retval = bfd_get_16 (abfd, buf);
16538 break;
16539 case 4:
16540 retval = bfd_get_32 (abfd, buf);
16541 break;
16542 case 8:
16543 retval = bfd_get_64 (abfd, buf);
16544 break;
16545 default:
16546 internal_error (__FILE__, __LINE__,
16547 _("read_address: bad switch, "
16548 "unsigned [in module %s]"),
16549 bfd_get_filename (abfd));
16550 }
16551 }
16552
16553 *bytes_read = cu_header->addr_size;
16554 return retval;
16555 }
16556
16557 /* Read the initial length from a section. The (draft) DWARF 3
16558 specification allows the initial length to take up either 4 bytes
16559 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16560 bytes describe the length and all offsets will be 8 bytes in length
16561 instead of 4.
16562
16563 An older, non-standard 64-bit format is also handled by this
16564 function. The older format in question stores the initial length
16565 as an 8-byte quantity without an escape value. Lengths greater
16566 than 2^32 aren't very common which means that the initial 4 bytes
16567 is almost always zero. Since a length value of zero doesn't make
16568 sense for the 32-bit format, this initial zero can be considered to
16569 be an escape value which indicates the presence of the older 64-bit
16570 format. As written, the code can't detect (old format) lengths
16571 greater than 4GB. If it becomes necessary to handle lengths
16572 somewhat larger than 4GB, we could allow other small values (such
16573 as the non-sensical values of 1, 2, and 3) to also be used as
16574 escape values indicating the presence of the old format.
16575
16576 The value returned via bytes_read should be used to increment the
16577 relevant pointer after calling read_initial_length().
16578
16579 [ Note: read_initial_length() and read_offset() are based on the
16580 document entitled "DWARF Debugging Information Format", revision
16581 3, draft 8, dated November 19, 2001. This document was obtained
16582 from:
16583
16584 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16585
16586 This document is only a draft and is subject to change. (So beware.)
16587
16588 Details regarding the older, non-standard 64-bit format were
16589 determined empirically by examining 64-bit ELF files produced by
16590 the SGI toolchain on an IRIX 6.5 machine.
16591
16592 - Kevin, July 16, 2002
16593 ] */
16594
16595 static LONGEST
16596 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16597 {
16598 LONGEST length = bfd_get_32 (abfd, buf);
16599
16600 if (length == 0xffffffff)
16601 {
16602 length = bfd_get_64 (abfd, buf + 4);
16603 *bytes_read = 12;
16604 }
16605 else if (length == 0)
16606 {
16607 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16608 length = bfd_get_64 (abfd, buf);
16609 *bytes_read = 8;
16610 }
16611 else
16612 {
16613 *bytes_read = 4;
16614 }
16615
16616 return length;
16617 }
16618
16619 /* Cover function for read_initial_length.
16620 Returns the length of the object at BUF, and stores the size of the
16621 initial length in *BYTES_READ and stores the size that offsets will be in
16622 *OFFSET_SIZE.
16623 If the initial length size is not equivalent to that specified in
16624 CU_HEADER then issue a complaint.
16625 This is useful when reading non-comp-unit headers. */
16626
16627 static LONGEST
16628 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
16629 const struct comp_unit_head *cu_header,
16630 unsigned int *bytes_read,
16631 unsigned int *offset_size)
16632 {
16633 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16634
16635 gdb_assert (cu_header->initial_length_size == 4
16636 || cu_header->initial_length_size == 8
16637 || cu_header->initial_length_size == 12);
16638
16639 if (cu_header->initial_length_size != *bytes_read)
16640 complaint (&symfile_complaints,
16641 _("intermixed 32-bit and 64-bit DWARF sections"));
16642
16643 *offset_size = (*bytes_read == 4) ? 4 : 8;
16644 return length;
16645 }
16646
16647 /* Read an offset from the data stream. The size of the offset is
16648 given by cu_header->offset_size. */
16649
16650 static LONGEST
16651 read_offset (bfd *abfd, const gdb_byte *buf,
16652 const struct comp_unit_head *cu_header,
16653 unsigned int *bytes_read)
16654 {
16655 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
16656
16657 *bytes_read = cu_header->offset_size;
16658 return offset;
16659 }
16660
16661 /* Read an offset from the data stream. */
16662
16663 static LONGEST
16664 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
16665 {
16666 LONGEST retval = 0;
16667
16668 switch (offset_size)
16669 {
16670 case 4:
16671 retval = bfd_get_32 (abfd, buf);
16672 break;
16673 case 8:
16674 retval = bfd_get_64 (abfd, buf);
16675 break;
16676 default:
16677 internal_error (__FILE__, __LINE__,
16678 _("read_offset_1: bad switch [in module %s]"),
16679 bfd_get_filename (abfd));
16680 }
16681
16682 return retval;
16683 }
16684
16685 static const gdb_byte *
16686 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
16687 {
16688 /* If the size of a host char is 8 bits, we can return a pointer
16689 to the buffer, otherwise we have to copy the data to a buffer
16690 allocated on the temporary obstack. */
16691 gdb_assert (HOST_CHAR_BIT == 8);
16692 return buf;
16693 }
16694
16695 static const char *
16696 read_direct_string (bfd *abfd, const gdb_byte *buf,
16697 unsigned int *bytes_read_ptr)
16698 {
16699 /* If the size of a host char is 8 bits, we can return a pointer
16700 to the string, otherwise we have to copy the string to a buffer
16701 allocated on the temporary obstack. */
16702 gdb_assert (HOST_CHAR_BIT == 8);
16703 if (*buf == '\0')
16704 {
16705 *bytes_read_ptr = 1;
16706 return NULL;
16707 }
16708 *bytes_read_ptr = strlen ((const char *) buf) + 1;
16709 return (const char *) buf;
16710 }
16711
16712 static const char *
16713 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
16714 {
16715 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
16716 if (dwarf2_per_objfile->str.buffer == NULL)
16717 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
16718 bfd_get_filename (abfd));
16719 if (str_offset >= dwarf2_per_objfile->str.size)
16720 error (_("DW_FORM_strp pointing outside of "
16721 ".debug_str section [in module %s]"),
16722 bfd_get_filename (abfd));
16723 gdb_assert (HOST_CHAR_BIT == 8);
16724 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
16725 return NULL;
16726 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
16727 }
16728
16729 /* Read a string at offset STR_OFFSET in the .debug_str section from
16730 the .dwz file DWZ. Throw an error if the offset is too large. If
16731 the string consists of a single NUL byte, return NULL; otherwise
16732 return a pointer to the string. */
16733
16734 static const char *
16735 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
16736 {
16737 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
16738
16739 if (dwz->str.buffer == NULL)
16740 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
16741 "section [in module %s]"),
16742 bfd_get_filename (dwz->dwz_bfd));
16743 if (str_offset >= dwz->str.size)
16744 error (_("DW_FORM_GNU_strp_alt pointing outside of "
16745 ".debug_str section [in module %s]"),
16746 bfd_get_filename (dwz->dwz_bfd));
16747 gdb_assert (HOST_CHAR_BIT == 8);
16748 if (dwz->str.buffer[str_offset] == '\0')
16749 return NULL;
16750 return (const char *) (dwz->str.buffer + str_offset);
16751 }
16752
16753 static const char *
16754 read_indirect_string (bfd *abfd, const gdb_byte *buf,
16755 const struct comp_unit_head *cu_header,
16756 unsigned int *bytes_read_ptr)
16757 {
16758 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
16759
16760 return read_indirect_string_at_offset (abfd, str_offset);
16761 }
16762
16763 static ULONGEST
16764 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
16765 unsigned int *bytes_read_ptr)
16766 {
16767 ULONGEST result;
16768 unsigned int num_read;
16769 int shift;
16770 unsigned char byte;
16771
16772 result = 0;
16773 shift = 0;
16774 num_read = 0;
16775 while (1)
16776 {
16777 byte = bfd_get_8 (abfd, buf);
16778 buf++;
16779 num_read++;
16780 result |= ((ULONGEST) (byte & 127) << shift);
16781 if ((byte & 128) == 0)
16782 {
16783 break;
16784 }
16785 shift += 7;
16786 }
16787 *bytes_read_ptr = num_read;
16788 return result;
16789 }
16790
16791 static LONGEST
16792 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
16793 unsigned int *bytes_read_ptr)
16794 {
16795 LONGEST result;
16796 int shift, num_read;
16797 unsigned char byte;
16798
16799 result = 0;
16800 shift = 0;
16801 num_read = 0;
16802 while (1)
16803 {
16804 byte = bfd_get_8 (abfd, buf);
16805 buf++;
16806 num_read++;
16807 result |= ((LONGEST) (byte & 127) << shift);
16808 shift += 7;
16809 if ((byte & 128) == 0)
16810 {
16811 break;
16812 }
16813 }
16814 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
16815 result |= -(((LONGEST) 1) << shift);
16816 *bytes_read_ptr = num_read;
16817 return result;
16818 }
16819
16820 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
16821 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
16822 ADDR_SIZE is the size of addresses from the CU header. */
16823
16824 static CORE_ADDR
16825 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
16826 {
16827 struct objfile *objfile = dwarf2_per_objfile->objfile;
16828 bfd *abfd = objfile->obfd;
16829 const gdb_byte *info_ptr;
16830
16831 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
16832 if (dwarf2_per_objfile->addr.buffer == NULL)
16833 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
16834 objfile_name (objfile));
16835 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
16836 error (_("DW_FORM_addr_index pointing outside of "
16837 ".debug_addr section [in module %s]"),
16838 objfile_name (objfile));
16839 info_ptr = (dwarf2_per_objfile->addr.buffer
16840 + addr_base + addr_index * addr_size);
16841 if (addr_size == 4)
16842 return bfd_get_32 (abfd, info_ptr);
16843 else
16844 return bfd_get_64 (abfd, info_ptr);
16845 }
16846
16847 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
16848
16849 static CORE_ADDR
16850 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
16851 {
16852 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
16853 }
16854
16855 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
16856
16857 static CORE_ADDR
16858 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
16859 unsigned int *bytes_read)
16860 {
16861 bfd *abfd = cu->objfile->obfd;
16862 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
16863
16864 return read_addr_index (cu, addr_index);
16865 }
16866
16867 /* Data structure to pass results from dwarf2_read_addr_index_reader
16868 back to dwarf2_read_addr_index. */
16869
16870 struct dwarf2_read_addr_index_data
16871 {
16872 ULONGEST addr_base;
16873 int addr_size;
16874 };
16875
16876 /* die_reader_func for dwarf2_read_addr_index. */
16877
16878 static void
16879 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
16880 const gdb_byte *info_ptr,
16881 struct die_info *comp_unit_die,
16882 int has_children,
16883 void *data)
16884 {
16885 struct dwarf2_cu *cu = reader->cu;
16886 struct dwarf2_read_addr_index_data *aidata =
16887 (struct dwarf2_read_addr_index_data *) data;
16888
16889 aidata->addr_base = cu->addr_base;
16890 aidata->addr_size = cu->header.addr_size;
16891 }
16892
16893 /* Given an index in .debug_addr, fetch the value.
16894 NOTE: This can be called during dwarf expression evaluation,
16895 long after the debug information has been read, and thus per_cu->cu
16896 may no longer exist. */
16897
16898 CORE_ADDR
16899 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
16900 unsigned int addr_index)
16901 {
16902 struct objfile *objfile = per_cu->objfile;
16903 struct dwarf2_cu *cu = per_cu->cu;
16904 ULONGEST addr_base;
16905 int addr_size;
16906
16907 /* This is intended to be called from outside this file. */
16908 dw2_setup (objfile);
16909
16910 /* We need addr_base and addr_size.
16911 If we don't have PER_CU->cu, we have to get it.
16912 Nasty, but the alternative is storing the needed info in PER_CU,
16913 which at this point doesn't seem justified: it's not clear how frequently
16914 it would get used and it would increase the size of every PER_CU.
16915 Entry points like dwarf2_per_cu_addr_size do a similar thing
16916 so we're not in uncharted territory here.
16917 Alas we need to be a bit more complicated as addr_base is contained
16918 in the DIE.
16919
16920 We don't need to read the entire CU(/TU).
16921 We just need the header and top level die.
16922
16923 IWBN to use the aging mechanism to let us lazily later discard the CU.
16924 For now we skip this optimization. */
16925
16926 if (cu != NULL)
16927 {
16928 addr_base = cu->addr_base;
16929 addr_size = cu->header.addr_size;
16930 }
16931 else
16932 {
16933 struct dwarf2_read_addr_index_data aidata;
16934
16935 /* Note: We can't use init_cutu_and_read_dies_simple here,
16936 we need addr_base. */
16937 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
16938 dwarf2_read_addr_index_reader, &aidata);
16939 addr_base = aidata.addr_base;
16940 addr_size = aidata.addr_size;
16941 }
16942
16943 return read_addr_index_1 (addr_index, addr_base, addr_size);
16944 }
16945
16946 /* Given a DW_FORM_GNU_str_index, fetch the string.
16947 This is only used by the Fission support. */
16948
16949 static const char *
16950 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
16951 {
16952 struct objfile *objfile = dwarf2_per_objfile->objfile;
16953 const char *objf_name = objfile_name (objfile);
16954 bfd *abfd = objfile->obfd;
16955 struct dwarf2_cu *cu = reader->cu;
16956 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
16957 struct dwarf2_section_info *str_offsets_section =
16958 &reader->dwo_file->sections.str_offsets;
16959 const gdb_byte *info_ptr;
16960 ULONGEST str_offset;
16961 static const char form_name[] = "DW_FORM_GNU_str_index";
16962
16963 dwarf2_read_section (objfile, str_section);
16964 dwarf2_read_section (objfile, str_offsets_section);
16965 if (str_section->buffer == NULL)
16966 error (_("%s used without .debug_str.dwo section"
16967 " in CU at offset 0x%lx [in module %s]"),
16968 form_name, (long) cu->header.offset.sect_off, objf_name);
16969 if (str_offsets_section->buffer == NULL)
16970 error (_("%s used without .debug_str_offsets.dwo section"
16971 " in CU at offset 0x%lx [in module %s]"),
16972 form_name, (long) cu->header.offset.sect_off, objf_name);
16973 if (str_index * cu->header.offset_size >= str_offsets_section->size)
16974 error (_("%s pointing outside of .debug_str_offsets.dwo"
16975 " section in CU at offset 0x%lx [in module %s]"),
16976 form_name, (long) cu->header.offset.sect_off, objf_name);
16977 info_ptr = (str_offsets_section->buffer
16978 + str_index * cu->header.offset_size);
16979 if (cu->header.offset_size == 4)
16980 str_offset = bfd_get_32 (abfd, info_ptr);
16981 else
16982 str_offset = bfd_get_64 (abfd, info_ptr);
16983 if (str_offset >= str_section->size)
16984 error (_("Offset from %s pointing outside of"
16985 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
16986 form_name, (long) cu->header.offset.sect_off, objf_name);
16987 return (const char *) (str_section->buffer + str_offset);
16988 }
16989
16990 /* Return the length of an LEB128 number in BUF. */
16991
16992 static int
16993 leb128_size (const gdb_byte *buf)
16994 {
16995 const gdb_byte *begin = buf;
16996 gdb_byte byte;
16997
16998 while (1)
16999 {
17000 byte = *buf++;
17001 if ((byte & 128) == 0)
17002 return buf - begin;
17003 }
17004 }
17005
17006 static void
17007 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17008 {
17009 switch (lang)
17010 {
17011 case DW_LANG_C89:
17012 case DW_LANG_C99:
17013 case DW_LANG_C11:
17014 case DW_LANG_C:
17015 case DW_LANG_UPC:
17016 cu->language = language_c;
17017 break;
17018 case DW_LANG_Java:
17019 case DW_LANG_C_plus_plus:
17020 case DW_LANG_C_plus_plus_11:
17021 case DW_LANG_C_plus_plus_14:
17022 cu->language = language_cplus;
17023 break;
17024 case DW_LANG_D:
17025 cu->language = language_d;
17026 break;
17027 case DW_LANG_Fortran77:
17028 case DW_LANG_Fortran90:
17029 case DW_LANG_Fortran95:
17030 case DW_LANG_Fortran03:
17031 case DW_LANG_Fortran08:
17032 cu->language = language_fortran;
17033 break;
17034 case DW_LANG_Go:
17035 cu->language = language_go;
17036 break;
17037 case DW_LANG_Mips_Assembler:
17038 cu->language = language_asm;
17039 break;
17040 case DW_LANG_Ada83:
17041 case DW_LANG_Ada95:
17042 cu->language = language_ada;
17043 break;
17044 case DW_LANG_Modula2:
17045 cu->language = language_m2;
17046 break;
17047 case DW_LANG_Pascal83:
17048 cu->language = language_pascal;
17049 break;
17050 case DW_LANG_ObjC:
17051 cu->language = language_objc;
17052 break;
17053 case DW_LANG_Rust:
17054 case DW_LANG_Rust_old:
17055 cu->language = language_rust;
17056 break;
17057 case DW_LANG_Cobol74:
17058 case DW_LANG_Cobol85:
17059 default:
17060 cu->language = language_minimal;
17061 break;
17062 }
17063 cu->language_defn = language_def (cu->language);
17064 }
17065
17066 /* Return the named attribute or NULL if not there. */
17067
17068 static struct attribute *
17069 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17070 {
17071 for (;;)
17072 {
17073 unsigned int i;
17074 struct attribute *spec = NULL;
17075
17076 for (i = 0; i < die->num_attrs; ++i)
17077 {
17078 if (die->attrs[i].name == name)
17079 return &die->attrs[i];
17080 if (die->attrs[i].name == DW_AT_specification
17081 || die->attrs[i].name == DW_AT_abstract_origin)
17082 spec = &die->attrs[i];
17083 }
17084
17085 if (!spec)
17086 break;
17087
17088 die = follow_die_ref (die, spec, &cu);
17089 }
17090
17091 return NULL;
17092 }
17093
17094 /* Return the named attribute or NULL if not there,
17095 but do not follow DW_AT_specification, etc.
17096 This is for use in contexts where we're reading .debug_types dies.
17097 Following DW_AT_specification, DW_AT_abstract_origin will take us
17098 back up the chain, and we want to go down. */
17099
17100 static struct attribute *
17101 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17102 {
17103 unsigned int i;
17104
17105 for (i = 0; i < die->num_attrs; ++i)
17106 if (die->attrs[i].name == name)
17107 return &die->attrs[i];
17108
17109 return NULL;
17110 }
17111
17112 /* Return the string associated with a string-typed attribute, or NULL if it
17113 is either not found or is of an incorrect type. */
17114
17115 static const char *
17116 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17117 {
17118 struct attribute *attr;
17119 const char *str = NULL;
17120
17121 attr = dwarf2_attr (die, name, cu);
17122
17123 if (attr != NULL)
17124 {
17125 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_string
17126 || attr->form == DW_FORM_GNU_strp_alt)
17127 str = DW_STRING (attr);
17128 else
17129 complaint (&symfile_complaints,
17130 _("string type expected for attribute %s for "
17131 "DIE at 0x%x in module %s"),
17132 dwarf_attr_name (name), die->offset.sect_off,
17133 objfile_name (cu->objfile));
17134 }
17135
17136 return str;
17137 }
17138
17139 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17140 and holds a non-zero value. This function should only be used for
17141 DW_FORM_flag or DW_FORM_flag_present attributes. */
17142
17143 static int
17144 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17145 {
17146 struct attribute *attr = dwarf2_attr (die, name, cu);
17147
17148 return (attr && DW_UNSND (attr));
17149 }
17150
17151 static int
17152 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17153 {
17154 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17155 which value is non-zero. However, we have to be careful with
17156 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17157 (via dwarf2_flag_true_p) follows this attribute. So we may
17158 end up accidently finding a declaration attribute that belongs
17159 to a different DIE referenced by the specification attribute,
17160 even though the given DIE does not have a declaration attribute. */
17161 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17162 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17163 }
17164
17165 /* Return the die giving the specification for DIE, if there is
17166 one. *SPEC_CU is the CU containing DIE on input, and the CU
17167 containing the return value on output. If there is no
17168 specification, but there is an abstract origin, that is
17169 returned. */
17170
17171 static struct die_info *
17172 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17173 {
17174 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17175 *spec_cu);
17176
17177 if (spec_attr == NULL)
17178 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17179
17180 if (spec_attr == NULL)
17181 return NULL;
17182 else
17183 return follow_die_ref (die, spec_attr, spec_cu);
17184 }
17185
17186 /* Free the line_header structure *LH, and any arrays and strings it
17187 refers to.
17188 NOTE: This is also used as a "cleanup" function. */
17189
17190 static void
17191 free_line_header (struct line_header *lh)
17192 {
17193 if (lh->standard_opcode_lengths)
17194 xfree (lh->standard_opcode_lengths);
17195
17196 /* Remember that all the lh->file_names[i].name pointers are
17197 pointers into debug_line_buffer, and don't need to be freed. */
17198 if (lh->file_names)
17199 xfree (lh->file_names);
17200
17201 /* Similarly for the include directory names. */
17202 if (lh->include_dirs)
17203 xfree (lh->include_dirs);
17204
17205 xfree (lh);
17206 }
17207
17208 /* Stub for free_line_header to match void * callback types. */
17209
17210 static void
17211 free_line_header_voidp (void *arg)
17212 {
17213 struct line_header *lh = (struct line_header *) arg;
17214
17215 free_line_header (lh);
17216 }
17217
17218 /* Add an entry to LH's include directory table. */
17219
17220 static void
17221 add_include_dir (struct line_header *lh, const char *include_dir)
17222 {
17223 if (dwarf_line_debug >= 2)
17224 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17225 lh->num_include_dirs + 1, include_dir);
17226
17227 /* Grow the array if necessary. */
17228 if (lh->include_dirs_size == 0)
17229 {
17230 lh->include_dirs_size = 1; /* for testing */
17231 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
17232 }
17233 else if (lh->num_include_dirs >= lh->include_dirs_size)
17234 {
17235 lh->include_dirs_size *= 2;
17236 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17237 lh->include_dirs_size);
17238 }
17239
17240 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17241 }
17242
17243 /* Add an entry to LH's file name table. */
17244
17245 static void
17246 add_file_name (struct line_header *lh,
17247 const char *name,
17248 unsigned int dir_index,
17249 unsigned int mod_time,
17250 unsigned int length)
17251 {
17252 struct file_entry *fe;
17253
17254 if (dwarf_line_debug >= 2)
17255 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17256 lh->num_file_names + 1, name);
17257
17258 /* Grow the array if necessary. */
17259 if (lh->file_names_size == 0)
17260 {
17261 lh->file_names_size = 1; /* for testing */
17262 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
17263 }
17264 else if (lh->num_file_names >= lh->file_names_size)
17265 {
17266 lh->file_names_size *= 2;
17267 lh->file_names
17268 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
17269 }
17270
17271 fe = &lh->file_names[lh->num_file_names++];
17272 fe->name = name;
17273 fe->dir_index = dir_index;
17274 fe->mod_time = mod_time;
17275 fe->length = length;
17276 fe->included_p = 0;
17277 fe->symtab = NULL;
17278 }
17279
17280 /* A convenience function to find the proper .debug_line section for a CU. */
17281
17282 static struct dwarf2_section_info *
17283 get_debug_line_section (struct dwarf2_cu *cu)
17284 {
17285 struct dwarf2_section_info *section;
17286
17287 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17288 DWO file. */
17289 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17290 section = &cu->dwo_unit->dwo_file->sections.line;
17291 else if (cu->per_cu->is_dwz)
17292 {
17293 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17294
17295 section = &dwz->line;
17296 }
17297 else
17298 section = &dwarf2_per_objfile->line;
17299
17300 return section;
17301 }
17302
17303 /* Read the statement program header starting at OFFSET in
17304 .debug_line, or .debug_line.dwo. Return a pointer
17305 to a struct line_header, allocated using xmalloc.
17306 Returns NULL if there is a problem reading the header, e.g., if it
17307 has a version we don't understand.
17308
17309 NOTE: the strings in the include directory and file name tables of
17310 the returned object point into the dwarf line section buffer,
17311 and must not be freed. */
17312
17313 static struct line_header *
17314 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
17315 {
17316 struct cleanup *back_to;
17317 struct line_header *lh;
17318 const gdb_byte *line_ptr;
17319 unsigned int bytes_read, offset_size;
17320 int i;
17321 const char *cur_dir, *cur_file;
17322 struct dwarf2_section_info *section;
17323 bfd *abfd;
17324
17325 section = get_debug_line_section (cu);
17326 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17327 if (section->buffer == NULL)
17328 {
17329 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17330 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17331 else
17332 complaint (&symfile_complaints, _("missing .debug_line section"));
17333 return 0;
17334 }
17335
17336 /* We can't do this until we know the section is non-empty.
17337 Only then do we know we have such a section. */
17338 abfd = get_section_bfd_owner (section);
17339
17340 /* Make sure that at least there's room for the total_length field.
17341 That could be 12 bytes long, but we're just going to fudge that. */
17342 if (offset + 4 >= section->size)
17343 {
17344 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17345 return 0;
17346 }
17347
17348 lh = XNEW (struct line_header);
17349 memset (lh, 0, sizeof (*lh));
17350 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17351 (void *) lh);
17352
17353 lh->offset.sect_off = offset;
17354 lh->offset_in_dwz = cu->per_cu->is_dwz;
17355
17356 line_ptr = section->buffer + offset;
17357
17358 /* Read in the header. */
17359 lh->total_length =
17360 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17361 &bytes_read, &offset_size);
17362 line_ptr += bytes_read;
17363 if (line_ptr + lh->total_length > (section->buffer + section->size))
17364 {
17365 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17366 do_cleanups (back_to);
17367 return 0;
17368 }
17369 lh->statement_program_end = line_ptr + lh->total_length;
17370 lh->version = read_2_bytes (abfd, line_ptr);
17371 line_ptr += 2;
17372 if (lh->version > 4)
17373 {
17374 /* This is a version we don't understand. The format could have
17375 changed in ways we don't handle properly so just punt. */
17376 complaint (&symfile_complaints,
17377 _("unsupported version in .debug_line section"));
17378 return NULL;
17379 }
17380 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17381 line_ptr += offset_size;
17382 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17383 line_ptr += 1;
17384 if (lh->version >= 4)
17385 {
17386 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17387 line_ptr += 1;
17388 }
17389 else
17390 lh->maximum_ops_per_instruction = 1;
17391
17392 if (lh->maximum_ops_per_instruction == 0)
17393 {
17394 lh->maximum_ops_per_instruction = 1;
17395 complaint (&symfile_complaints,
17396 _("invalid maximum_ops_per_instruction "
17397 "in `.debug_line' section"));
17398 }
17399
17400 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17401 line_ptr += 1;
17402 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17403 line_ptr += 1;
17404 lh->line_range = read_1_byte (abfd, line_ptr);
17405 line_ptr += 1;
17406 lh->opcode_base = read_1_byte (abfd, line_ptr);
17407 line_ptr += 1;
17408 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
17409
17410 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17411 for (i = 1; i < lh->opcode_base; ++i)
17412 {
17413 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17414 line_ptr += 1;
17415 }
17416
17417 /* Read directory table. */
17418 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17419 {
17420 line_ptr += bytes_read;
17421 add_include_dir (lh, cur_dir);
17422 }
17423 line_ptr += bytes_read;
17424
17425 /* Read file name table. */
17426 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17427 {
17428 unsigned int dir_index, mod_time, length;
17429
17430 line_ptr += bytes_read;
17431 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17432 line_ptr += bytes_read;
17433 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17434 line_ptr += bytes_read;
17435 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17436 line_ptr += bytes_read;
17437
17438 add_file_name (lh, cur_file, dir_index, mod_time, length);
17439 }
17440 line_ptr += bytes_read;
17441 lh->statement_program_start = line_ptr;
17442
17443 if (line_ptr > (section->buffer + section->size))
17444 complaint (&symfile_complaints,
17445 _("line number info header doesn't "
17446 "fit in `.debug_line' section"));
17447
17448 discard_cleanups (back_to);
17449 return lh;
17450 }
17451
17452 /* Subroutine of dwarf_decode_lines to simplify it.
17453 Return the file name of the psymtab for included file FILE_INDEX
17454 in line header LH of PST.
17455 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17456 If space for the result is malloc'd, it will be freed by a cleanup.
17457 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17458
17459 The function creates dangling cleanup registration. */
17460
17461 static const char *
17462 psymtab_include_file_name (const struct line_header *lh, int file_index,
17463 const struct partial_symtab *pst,
17464 const char *comp_dir)
17465 {
17466 const struct file_entry fe = lh->file_names [file_index];
17467 const char *include_name = fe.name;
17468 const char *include_name_to_compare = include_name;
17469 const char *dir_name = NULL;
17470 const char *pst_filename;
17471 char *copied_name = NULL;
17472 int file_is_pst;
17473
17474 if (fe.dir_index && lh->include_dirs != NULL)
17475 dir_name = lh->include_dirs[fe.dir_index - 1];
17476
17477 if (!IS_ABSOLUTE_PATH (include_name)
17478 && (dir_name != NULL || comp_dir != NULL))
17479 {
17480 /* Avoid creating a duplicate psymtab for PST.
17481 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17482 Before we do the comparison, however, we need to account
17483 for DIR_NAME and COMP_DIR.
17484 First prepend dir_name (if non-NULL). If we still don't
17485 have an absolute path prepend comp_dir (if non-NULL).
17486 However, the directory we record in the include-file's
17487 psymtab does not contain COMP_DIR (to match the
17488 corresponding symtab(s)).
17489
17490 Example:
17491
17492 bash$ cd /tmp
17493 bash$ gcc -g ./hello.c
17494 include_name = "hello.c"
17495 dir_name = "."
17496 DW_AT_comp_dir = comp_dir = "/tmp"
17497 DW_AT_name = "./hello.c"
17498
17499 */
17500
17501 if (dir_name != NULL)
17502 {
17503 char *tem = concat (dir_name, SLASH_STRING,
17504 include_name, (char *)NULL);
17505
17506 make_cleanup (xfree, tem);
17507 include_name = tem;
17508 include_name_to_compare = include_name;
17509 }
17510 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
17511 {
17512 char *tem = concat (comp_dir, SLASH_STRING,
17513 include_name, (char *)NULL);
17514
17515 make_cleanup (xfree, tem);
17516 include_name_to_compare = tem;
17517 }
17518 }
17519
17520 pst_filename = pst->filename;
17521 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
17522 {
17523 copied_name = concat (pst->dirname, SLASH_STRING,
17524 pst_filename, (char *)NULL);
17525 pst_filename = copied_name;
17526 }
17527
17528 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
17529
17530 if (copied_name != NULL)
17531 xfree (copied_name);
17532
17533 if (file_is_pst)
17534 return NULL;
17535 return include_name;
17536 }
17537
17538 /* State machine to track the state of the line number program. */
17539
17540 typedef struct
17541 {
17542 /* These are part of the standard DWARF line number state machine. */
17543
17544 unsigned char op_index;
17545 unsigned int file;
17546 unsigned int line;
17547 CORE_ADDR address;
17548 int is_stmt;
17549 unsigned int discriminator;
17550
17551 /* Additional bits of state we need to track. */
17552
17553 /* The last file that we called dwarf2_start_subfile for.
17554 This is only used for TLLs. */
17555 unsigned int last_file;
17556 /* The last file a line number was recorded for. */
17557 struct subfile *last_subfile;
17558
17559 /* The function to call to record a line. */
17560 record_line_ftype *record_line;
17561
17562 /* The last line number that was recorded, used to coalesce
17563 consecutive entries for the same line. This can happen, for
17564 example, when discriminators are present. PR 17276. */
17565 unsigned int last_line;
17566 int line_has_non_zero_discriminator;
17567 } lnp_state_machine;
17568
17569 /* There's a lot of static state to pass to dwarf_record_line.
17570 This keeps it all together. */
17571
17572 typedef struct
17573 {
17574 /* The gdbarch. */
17575 struct gdbarch *gdbarch;
17576
17577 /* The line number header. */
17578 struct line_header *line_header;
17579
17580 /* Non-zero if we're recording lines.
17581 Otherwise we're building partial symtabs and are just interested in
17582 finding include files mentioned by the line number program. */
17583 int record_lines_p;
17584 } lnp_reader_state;
17585
17586 /* Ignore this record_line request. */
17587
17588 static void
17589 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
17590 {
17591 return;
17592 }
17593
17594 /* Return non-zero if we should add LINE to the line number table.
17595 LINE is the line to add, LAST_LINE is the last line that was added,
17596 LAST_SUBFILE is the subfile for LAST_LINE.
17597 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
17598 had a non-zero discriminator.
17599
17600 We have to be careful in the presence of discriminators.
17601 E.g., for this line:
17602
17603 for (i = 0; i < 100000; i++);
17604
17605 clang can emit four line number entries for that one line,
17606 each with a different discriminator.
17607 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
17608
17609 However, we want gdb to coalesce all four entries into one.
17610 Otherwise the user could stepi into the middle of the line and
17611 gdb would get confused about whether the pc really was in the
17612 middle of the line.
17613
17614 Things are further complicated by the fact that two consecutive
17615 line number entries for the same line is a heuristic used by gcc
17616 to denote the end of the prologue. So we can't just discard duplicate
17617 entries, we have to be selective about it. The heuristic we use is
17618 that we only collapse consecutive entries for the same line if at least
17619 one of those entries has a non-zero discriminator. PR 17276.
17620
17621 Note: Addresses in the line number state machine can never go backwards
17622 within one sequence, thus this coalescing is ok. */
17623
17624 static int
17625 dwarf_record_line_p (unsigned int line, unsigned int last_line,
17626 int line_has_non_zero_discriminator,
17627 struct subfile *last_subfile)
17628 {
17629 if (current_subfile != last_subfile)
17630 return 1;
17631 if (line != last_line)
17632 return 1;
17633 /* Same line for the same file that we've seen already.
17634 As a last check, for pr 17276, only record the line if the line
17635 has never had a non-zero discriminator. */
17636 if (!line_has_non_zero_discriminator)
17637 return 1;
17638 return 0;
17639 }
17640
17641 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
17642 in the line table of subfile SUBFILE. */
17643
17644 static void
17645 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
17646 unsigned int line, CORE_ADDR address,
17647 record_line_ftype p_record_line)
17648 {
17649 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
17650
17651 if (dwarf_line_debug)
17652 {
17653 fprintf_unfiltered (gdb_stdlog,
17654 "Recording line %u, file %s, address %s\n",
17655 line, lbasename (subfile->name),
17656 paddress (gdbarch, address));
17657 }
17658
17659 (*p_record_line) (subfile, line, addr);
17660 }
17661
17662 /* Subroutine of dwarf_decode_lines_1 to simplify it.
17663 Mark the end of a set of line number records.
17664 The arguments are the same as for dwarf_record_line_1.
17665 If SUBFILE is NULL the request is ignored. */
17666
17667 static void
17668 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
17669 CORE_ADDR address, record_line_ftype p_record_line)
17670 {
17671 if (subfile == NULL)
17672 return;
17673
17674 if (dwarf_line_debug)
17675 {
17676 fprintf_unfiltered (gdb_stdlog,
17677 "Finishing current line, file %s, address %s\n",
17678 lbasename (subfile->name),
17679 paddress (gdbarch, address));
17680 }
17681
17682 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
17683 }
17684
17685 /* Record the line in STATE.
17686 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
17687
17688 static void
17689 dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
17690 int end_sequence)
17691 {
17692 const struct line_header *lh = reader->line_header;
17693 unsigned int file, line, discriminator;
17694 int is_stmt;
17695
17696 file = state->file;
17697 line = state->line;
17698 is_stmt = state->is_stmt;
17699 discriminator = state->discriminator;
17700
17701 if (dwarf_line_debug)
17702 {
17703 fprintf_unfiltered (gdb_stdlog,
17704 "Processing actual line %u: file %u,"
17705 " address %s, is_stmt %u, discrim %u\n",
17706 line, file,
17707 paddress (reader->gdbarch, state->address),
17708 is_stmt, discriminator);
17709 }
17710
17711 if (file == 0 || file - 1 >= lh->num_file_names)
17712 dwarf2_debug_line_missing_file_complaint ();
17713 /* For now we ignore lines not starting on an instruction boundary.
17714 But not when processing end_sequence for compatibility with the
17715 previous version of the code. */
17716 else if (state->op_index == 0 || end_sequence)
17717 {
17718 lh->file_names[file - 1].included_p = 1;
17719 if (reader->record_lines_p && is_stmt)
17720 {
17721 if (state->last_subfile != current_subfile || end_sequence)
17722 {
17723 dwarf_finish_line (reader->gdbarch, state->last_subfile,
17724 state->address, state->record_line);
17725 }
17726
17727 if (!end_sequence)
17728 {
17729 if (dwarf_record_line_p (line, state->last_line,
17730 state->line_has_non_zero_discriminator,
17731 state->last_subfile))
17732 {
17733 dwarf_record_line_1 (reader->gdbarch, current_subfile,
17734 line, state->address,
17735 state->record_line);
17736 }
17737 state->last_subfile = current_subfile;
17738 state->last_line = line;
17739 }
17740 }
17741 }
17742 }
17743
17744 /* Initialize STATE for the start of a line number program. */
17745
17746 static void
17747 init_lnp_state_machine (lnp_state_machine *state,
17748 const lnp_reader_state *reader)
17749 {
17750 memset (state, 0, sizeof (*state));
17751
17752 /* Just starting, there is no "last file". */
17753 state->last_file = 0;
17754 state->last_subfile = NULL;
17755
17756 state->record_line = record_line;
17757
17758 state->last_line = 0;
17759 state->line_has_non_zero_discriminator = 0;
17760
17761 /* Initialize these according to the DWARF spec. */
17762 state->op_index = 0;
17763 state->file = 1;
17764 state->line = 1;
17765 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
17766 was a line entry for it so that the backend has a chance to adjust it
17767 and also record it in case it needs it. This is currently used by MIPS
17768 code, cf. `mips_adjust_dwarf2_line'. */
17769 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
17770 state->is_stmt = reader->line_header->default_is_stmt;
17771 state->discriminator = 0;
17772 }
17773
17774 /* Check address and if invalid nop-out the rest of the lines in this
17775 sequence. */
17776
17777 static void
17778 check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
17779 const gdb_byte *line_ptr,
17780 CORE_ADDR lowpc, CORE_ADDR address)
17781 {
17782 /* If address < lowpc then it's not a usable value, it's outside the
17783 pc range of the CU. However, we restrict the test to only address
17784 values of zero to preserve GDB's previous behaviour which is to
17785 handle the specific case of a function being GC'd by the linker. */
17786
17787 if (address == 0 && address < lowpc)
17788 {
17789 /* This line table is for a function which has been
17790 GCd by the linker. Ignore it. PR gdb/12528 */
17791
17792 struct objfile *objfile = cu->objfile;
17793 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
17794
17795 complaint (&symfile_complaints,
17796 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
17797 line_offset, objfile_name (objfile));
17798 state->record_line = noop_record_line;
17799 /* Note: sm.record_line is left as noop_record_line
17800 until we see DW_LNE_end_sequence. */
17801 }
17802 }
17803
17804 /* Subroutine of dwarf_decode_lines to simplify it.
17805 Process the line number information in LH.
17806 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
17807 program in order to set included_p for every referenced header. */
17808
17809 static void
17810 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
17811 const int decode_for_pst_p, CORE_ADDR lowpc)
17812 {
17813 const gdb_byte *line_ptr, *extended_end;
17814 const gdb_byte *line_end;
17815 unsigned int bytes_read, extended_len;
17816 unsigned char op_code, extended_op;
17817 CORE_ADDR baseaddr;
17818 struct objfile *objfile = cu->objfile;
17819 bfd *abfd = objfile->obfd;
17820 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17821 /* Non-zero if we're recording line info (as opposed to building partial
17822 symtabs). */
17823 int record_lines_p = !decode_for_pst_p;
17824 /* A collection of things we need to pass to dwarf_record_line. */
17825 lnp_reader_state reader_state;
17826
17827 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17828
17829 line_ptr = lh->statement_program_start;
17830 line_end = lh->statement_program_end;
17831
17832 reader_state.gdbarch = gdbarch;
17833 reader_state.line_header = lh;
17834 reader_state.record_lines_p = record_lines_p;
17835
17836 /* Read the statement sequences until there's nothing left. */
17837 while (line_ptr < line_end)
17838 {
17839 /* The DWARF line number program state machine. */
17840 lnp_state_machine state_machine;
17841 int end_sequence = 0;
17842
17843 /* Reset the state machine at the start of each sequence. */
17844 init_lnp_state_machine (&state_machine, &reader_state);
17845
17846 if (record_lines_p && lh->num_file_names >= state_machine.file)
17847 {
17848 /* Start a subfile for the current file of the state machine. */
17849 /* lh->include_dirs and lh->file_names are 0-based, but the
17850 directory and file name numbers in the statement program
17851 are 1-based. */
17852 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
17853 const char *dir = NULL;
17854
17855 if (fe->dir_index && lh->include_dirs != NULL)
17856 dir = lh->include_dirs[fe->dir_index - 1];
17857
17858 dwarf2_start_subfile (fe->name, dir);
17859 }
17860
17861 /* Decode the table. */
17862 while (line_ptr < line_end && !end_sequence)
17863 {
17864 op_code = read_1_byte (abfd, line_ptr);
17865 line_ptr += 1;
17866
17867 if (op_code >= lh->opcode_base)
17868 {
17869 /* Special opcode. */
17870 unsigned char adj_opcode;
17871 CORE_ADDR addr_adj;
17872 int line_delta;
17873
17874 adj_opcode = op_code - lh->opcode_base;
17875 addr_adj = (((state_machine.op_index
17876 + (adj_opcode / lh->line_range))
17877 / lh->maximum_ops_per_instruction)
17878 * lh->minimum_instruction_length);
17879 state_machine.address
17880 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
17881 state_machine.op_index = ((state_machine.op_index
17882 + (adj_opcode / lh->line_range))
17883 % lh->maximum_ops_per_instruction);
17884 line_delta = lh->line_base + (adj_opcode % lh->line_range);
17885 state_machine.line += line_delta;
17886 if (line_delta != 0)
17887 state_machine.line_has_non_zero_discriminator
17888 = state_machine.discriminator != 0;
17889
17890 dwarf_record_line (&reader_state, &state_machine, 0);
17891 state_machine.discriminator = 0;
17892 }
17893 else switch (op_code)
17894 {
17895 case DW_LNS_extended_op:
17896 extended_len = read_unsigned_leb128 (abfd, line_ptr,
17897 &bytes_read);
17898 line_ptr += bytes_read;
17899 extended_end = line_ptr + extended_len;
17900 extended_op = read_1_byte (abfd, line_ptr);
17901 line_ptr += 1;
17902 switch (extended_op)
17903 {
17904 case DW_LNE_end_sequence:
17905 state_machine.record_line = record_line;
17906 end_sequence = 1;
17907 break;
17908 case DW_LNE_set_address:
17909 {
17910 CORE_ADDR address
17911 = read_address (abfd, line_ptr, cu, &bytes_read);
17912
17913 line_ptr += bytes_read;
17914 check_line_address (cu, &state_machine, line_ptr,
17915 lowpc, address);
17916 state_machine.op_index = 0;
17917 address += baseaddr;
17918 state_machine.address
17919 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
17920 }
17921 break;
17922 case DW_LNE_define_file:
17923 {
17924 const char *cur_file;
17925 unsigned int dir_index, mod_time, length;
17926
17927 cur_file = read_direct_string (abfd, line_ptr,
17928 &bytes_read);
17929 line_ptr += bytes_read;
17930 dir_index =
17931 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17932 line_ptr += bytes_read;
17933 mod_time =
17934 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17935 line_ptr += bytes_read;
17936 length =
17937 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17938 line_ptr += bytes_read;
17939 add_file_name (lh, cur_file, dir_index, mod_time, length);
17940 }
17941 break;
17942 case DW_LNE_set_discriminator:
17943 /* The discriminator is not interesting to the debugger;
17944 just ignore it. We still need to check its value though:
17945 if there are consecutive entries for the same
17946 (non-prologue) line we want to coalesce them.
17947 PR 17276. */
17948 state_machine.discriminator
17949 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17950 state_machine.line_has_non_zero_discriminator
17951 |= state_machine.discriminator != 0;
17952 line_ptr += bytes_read;
17953 break;
17954 default:
17955 complaint (&symfile_complaints,
17956 _("mangled .debug_line section"));
17957 return;
17958 }
17959 /* Make sure that we parsed the extended op correctly. If e.g.
17960 we expected a different address size than the producer used,
17961 we may have read the wrong number of bytes. */
17962 if (line_ptr != extended_end)
17963 {
17964 complaint (&symfile_complaints,
17965 _("mangled .debug_line section"));
17966 return;
17967 }
17968 break;
17969 case DW_LNS_copy:
17970 dwarf_record_line (&reader_state, &state_machine, 0);
17971 state_machine.discriminator = 0;
17972 break;
17973 case DW_LNS_advance_pc:
17974 {
17975 CORE_ADDR adjust
17976 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17977 CORE_ADDR addr_adj;
17978
17979 addr_adj = (((state_machine.op_index + adjust)
17980 / lh->maximum_ops_per_instruction)
17981 * lh->minimum_instruction_length);
17982 state_machine.address
17983 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
17984 state_machine.op_index = ((state_machine.op_index + adjust)
17985 % lh->maximum_ops_per_instruction);
17986 line_ptr += bytes_read;
17987 }
17988 break;
17989 case DW_LNS_advance_line:
17990 {
17991 int line_delta
17992 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
17993
17994 state_machine.line += line_delta;
17995 if (line_delta != 0)
17996 state_machine.line_has_non_zero_discriminator
17997 = state_machine.discriminator != 0;
17998 line_ptr += bytes_read;
17999 }
18000 break;
18001 case DW_LNS_set_file:
18002 {
18003 /* The arrays lh->include_dirs and lh->file_names are
18004 0-based, but the directory and file name numbers in
18005 the statement program are 1-based. */
18006 struct file_entry *fe;
18007 const char *dir = NULL;
18008
18009 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
18010 &bytes_read);
18011 line_ptr += bytes_read;
18012 if (state_machine.file == 0
18013 || state_machine.file - 1 >= lh->num_file_names)
18014 dwarf2_debug_line_missing_file_complaint ();
18015 else
18016 {
18017 fe = &lh->file_names[state_machine.file - 1];
18018 if (fe->dir_index && lh->include_dirs != NULL)
18019 dir = lh->include_dirs[fe->dir_index - 1];
18020 if (record_lines_p)
18021 {
18022 state_machine.last_subfile = current_subfile;
18023 state_machine.line_has_non_zero_discriminator
18024 = state_machine.discriminator != 0;
18025 dwarf2_start_subfile (fe->name, dir);
18026 }
18027 }
18028 }
18029 break;
18030 case DW_LNS_set_column:
18031 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18032 line_ptr += bytes_read;
18033 break;
18034 case DW_LNS_negate_stmt:
18035 state_machine.is_stmt = (!state_machine.is_stmt);
18036 break;
18037 case DW_LNS_set_basic_block:
18038 break;
18039 /* Add to the address register of the state machine the
18040 address increment value corresponding to special opcode
18041 255. I.e., this value is scaled by the minimum
18042 instruction length since special opcode 255 would have
18043 scaled the increment. */
18044 case DW_LNS_const_add_pc:
18045 {
18046 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
18047 CORE_ADDR addr_adj;
18048
18049 addr_adj = (((state_machine.op_index + adjust)
18050 / lh->maximum_ops_per_instruction)
18051 * lh->minimum_instruction_length);
18052 state_machine.address
18053 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18054 state_machine.op_index = ((state_machine.op_index + adjust)
18055 % lh->maximum_ops_per_instruction);
18056 }
18057 break;
18058 case DW_LNS_fixed_advance_pc:
18059 {
18060 CORE_ADDR addr_adj;
18061
18062 addr_adj = read_2_bytes (abfd, line_ptr);
18063 state_machine.address
18064 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18065 state_machine.op_index = 0;
18066 line_ptr += 2;
18067 }
18068 break;
18069 default:
18070 {
18071 /* Unknown standard opcode, ignore it. */
18072 int i;
18073
18074 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18075 {
18076 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18077 line_ptr += bytes_read;
18078 }
18079 }
18080 }
18081 }
18082
18083 if (!end_sequence)
18084 dwarf2_debug_line_missing_end_sequence_complaint ();
18085
18086 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18087 in which case we still finish recording the last line). */
18088 dwarf_record_line (&reader_state, &state_machine, 1);
18089 }
18090 }
18091
18092 /* Decode the Line Number Program (LNP) for the given line_header
18093 structure and CU. The actual information extracted and the type
18094 of structures created from the LNP depends on the value of PST.
18095
18096 1. If PST is NULL, then this procedure uses the data from the program
18097 to create all necessary symbol tables, and their linetables.
18098
18099 2. If PST is not NULL, this procedure reads the program to determine
18100 the list of files included by the unit represented by PST, and
18101 builds all the associated partial symbol tables.
18102
18103 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18104 It is used for relative paths in the line table.
18105 NOTE: When processing partial symtabs (pst != NULL),
18106 comp_dir == pst->dirname.
18107
18108 NOTE: It is important that psymtabs have the same file name (via strcmp)
18109 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18110 symtab we don't use it in the name of the psymtabs we create.
18111 E.g. expand_line_sal requires this when finding psymtabs to expand.
18112 A good testcase for this is mb-inline.exp.
18113
18114 LOWPC is the lowest address in CU (or 0 if not known).
18115
18116 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18117 for its PC<->lines mapping information. Otherwise only the filename
18118 table is read in. */
18119
18120 static void
18121 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18122 struct dwarf2_cu *cu, struct partial_symtab *pst,
18123 CORE_ADDR lowpc, int decode_mapping)
18124 {
18125 struct objfile *objfile = cu->objfile;
18126 const int decode_for_pst_p = (pst != NULL);
18127
18128 if (decode_mapping)
18129 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18130
18131 if (decode_for_pst_p)
18132 {
18133 int file_index;
18134
18135 /* Now that we're done scanning the Line Header Program, we can
18136 create the psymtab of each included file. */
18137 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18138 if (lh->file_names[file_index].included_p == 1)
18139 {
18140 const char *include_name =
18141 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18142 if (include_name != NULL)
18143 dwarf2_create_include_psymtab (include_name, pst, objfile);
18144 }
18145 }
18146 else
18147 {
18148 /* Make sure a symtab is created for every file, even files
18149 which contain only variables (i.e. no code with associated
18150 line numbers). */
18151 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18152 int i;
18153
18154 for (i = 0; i < lh->num_file_names; i++)
18155 {
18156 const char *dir = NULL;
18157 struct file_entry *fe;
18158
18159 fe = &lh->file_names[i];
18160 if (fe->dir_index && lh->include_dirs != NULL)
18161 dir = lh->include_dirs[fe->dir_index - 1];
18162 dwarf2_start_subfile (fe->name, dir);
18163
18164 if (current_subfile->symtab == NULL)
18165 {
18166 current_subfile->symtab
18167 = allocate_symtab (cust, current_subfile->name);
18168 }
18169 fe->symtab = current_subfile->symtab;
18170 }
18171 }
18172 }
18173
18174 /* Start a subfile for DWARF. FILENAME is the name of the file and
18175 DIRNAME the name of the source directory which contains FILENAME
18176 or NULL if not known.
18177 This routine tries to keep line numbers from identical absolute and
18178 relative file names in a common subfile.
18179
18180 Using the `list' example from the GDB testsuite, which resides in
18181 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18182 of /srcdir/list0.c yields the following debugging information for list0.c:
18183
18184 DW_AT_name: /srcdir/list0.c
18185 DW_AT_comp_dir: /compdir
18186 files.files[0].name: list0.h
18187 files.files[0].dir: /srcdir
18188 files.files[1].name: list0.c
18189 files.files[1].dir: /srcdir
18190
18191 The line number information for list0.c has to end up in a single
18192 subfile, so that `break /srcdir/list0.c:1' works as expected.
18193 start_subfile will ensure that this happens provided that we pass the
18194 concatenation of files.files[1].dir and files.files[1].name as the
18195 subfile's name. */
18196
18197 static void
18198 dwarf2_start_subfile (const char *filename, const char *dirname)
18199 {
18200 char *copy = NULL;
18201
18202 /* In order not to lose the line information directory,
18203 we concatenate it to the filename when it makes sense.
18204 Note that the Dwarf3 standard says (speaking of filenames in line
18205 information): ``The directory index is ignored for file names
18206 that represent full path names''. Thus ignoring dirname in the
18207 `else' branch below isn't an issue. */
18208
18209 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18210 {
18211 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18212 filename = copy;
18213 }
18214
18215 start_subfile (filename);
18216
18217 if (copy != NULL)
18218 xfree (copy);
18219 }
18220
18221 /* Start a symtab for DWARF.
18222 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18223
18224 static struct compunit_symtab *
18225 dwarf2_start_symtab (struct dwarf2_cu *cu,
18226 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18227 {
18228 struct compunit_symtab *cust
18229 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18230
18231 record_debugformat ("DWARF 2");
18232 record_producer (cu->producer);
18233
18234 /* We assume that we're processing GCC output. */
18235 processing_gcc_compilation = 2;
18236
18237 cu->processing_has_namespace_info = 0;
18238
18239 return cust;
18240 }
18241
18242 static void
18243 var_decode_location (struct attribute *attr, struct symbol *sym,
18244 struct dwarf2_cu *cu)
18245 {
18246 struct objfile *objfile = cu->objfile;
18247 struct comp_unit_head *cu_header = &cu->header;
18248
18249 /* NOTE drow/2003-01-30: There used to be a comment and some special
18250 code here to turn a symbol with DW_AT_external and a
18251 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18252 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18253 with some versions of binutils) where shared libraries could have
18254 relocations against symbols in their debug information - the
18255 minimal symbol would have the right address, but the debug info
18256 would not. It's no longer necessary, because we will explicitly
18257 apply relocations when we read in the debug information now. */
18258
18259 /* A DW_AT_location attribute with no contents indicates that a
18260 variable has been optimized away. */
18261 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18262 {
18263 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18264 return;
18265 }
18266
18267 /* Handle one degenerate form of location expression specially, to
18268 preserve GDB's previous behavior when section offsets are
18269 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18270 then mark this symbol as LOC_STATIC. */
18271
18272 if (attr_form_is_block (attr)
18273 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18274 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18275 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18276 && (DW_BLOCK (attr)->size
18277 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18278 {
18279 unsigned int dummy;
18280
18281 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18282 SYMBOL_VALUE_ADDRESS (sym) =
18283 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18284 else
18285 SYMBOL_VALUE_ADDRESS (sym) =
18286 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18287 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18288 fixup_symbol_section (sym, objfile);
18289 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18290 SYMBOL_SECTION (sym));
18291 return;
18292 }
18293
18294 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18295 expression evaluator, and use LOC_COMPUTED only when necessary
18296 (i.e. when the value of a register or memory location is
18297 referenced, or a thread-local block, etc.). Then again, it might
18298 not be worthwhile. I'm assuming that it isn't unless performance
18299 or memory numbers show me otherwise. */
18300
18301 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18302
18303 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18304 cu->has_loclist = 1;
18305 }
18306
18307 /* Given a pointer to a DWARF information entry, figure out if we need
18308 to make a symbol table entry for it, and if so, create a new entry
18309 and return a pointer to it.
18310 If TYPE is NULL, determine symbol type from the die, otherwise
18311 used the passed type.
18312 If SPACE is not NULL, use it to hold the new symbol. If it is
18313 NULL, allocate a new symbol on the objfile's obstack. */
18314
18315 static struct symbol *
18316 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18317 struct symbol *space)
18318 {
18319 struct objfile *objfile = cu->objfile;
18320 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18321 struct symbol *sym = NULL;
18322 const char *name;
18323 struct attribute *attr = NULL;
18324 struct attribute *attr2 = NULL;
18325 CORE_ADDR baseaddr;
18326 struct pending **list_to_add = NULL;
18327
18328 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18329
18330 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18331
18332 name = dwarf2_name (die, cu);
18333 if (name)
18334 {
18335 const char *linkagename;
18336 int suppress_add = 0;
18337
18338 if (space)
18339 sym = space;
18340 else
18341 sym = allocate_symbol (objfile);
18342 OBJSTAT (objfile, n_syms++);
18343
18344 /* Cache this symbol's name and the name's demangled form (if any). */
18345 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18346 linkagename = dwarf2_physname (name, die, cu);
18347 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18348
18349 /* Fortran does not have mangling standard and the mangling does differ
18350 between gfortran, iFort etc. */
18351 if (cu->language == language_fortran
18352 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18353 symbol_set_demangled_name (&(sym->ginfo),
18354 dwarf2_full_name (name, die, cu),
18355 NULL);
18356
18357 /* Default assumptions.
18358 Use the passed type or decode it from the die. */
18359 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18360 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18361 if (type != NULL)
18362 SYMBOL_TYPE (sym) = type;
18363 else
18364 SYMBOL_TYPE (sym) = die_type (die, cu);
18365 attr = dwarf2_attr (die,
18366 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18367 cu);
18368 if (attr)
18369 {
18370 SYMBOL_LINE (sym) = DW_UNSND (attr);
18371 }
18372
18373 attr = dwarf2_attr (die,
18374 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18375 cu);
18376 if (attr)
18377 {
18378 int file_index = DW_UNSND (attr);
18379
18380 if (cu->line_header == NULL
18381 || file_index > cu->line_header->num_file_names)
18382 complaint (&symfile_complaints,
18383 _("file index out of range"));
18384 else if (file_index > 0)
18385 {
18386 struct file_entry *fe;
18387
18388 fe = &cu->line_header->file_names[file_index - 1];
18389 symbol_set_symtab (sym, fe->symtab);
18390 }
18391 }
18392
18393 switch (die->tag)
18394 {
18395 case DW_TAG_label:
18396 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18397 if (attr)
18398 {
18399 CORE_ADDR addr;
18400
18401 addr = attr_value_as_address (attr);
18402 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18403 SYMBOL_VALUE_ADDRESS (sym) = addr;
18404 }
18405 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18406 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18407 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18408 add_symbol_to_list (sym, cu->list_in_scope);
18409 break;
18410 case DW_TAG_subprogram:
18411 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18412 finish_block. */
18413 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18414 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18415 if ((attr2 && (DW_UNSND (attr2) != 0))
18416 || cu->language == language_ada)
18417 {
18418 /* Subprograms marked external are stored as a global symbol.
18419 Ada subprograms, whether marked external or not, are always
18420 stored as a global symbol, because we want to be able to
18421 access them globally. For instance, we want to be able
18422 to break on a nested subprogram without having to
18423 specify the context. */
18424 list_to_add = &global_symbols;
18425 }
18426 else
18427 {
18428 list_to_add = cu->list_in_scope;
18429 }
18430 break;
18431 case DW_TAG_inlined_subroutine:
18432 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18433 finish_block. */
18434 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18435 SYMBOL_INLINED (sym) = 1;
18436 list_to_add = cu->list_in_scope;
18437 break;
18438 case DW_TAG_template_value_param:
18439 suppress_add = 1;
18440 /* Fall through. */
18441 case DW_TAG_constant:
18442 case DW_TAG_variable:
18443 case DW_TAG_member:
18444 /* Compilation with minimal debug info may result in
18445 variables with missing type entries. Change the
18446 misleading `void' type to something sensible. */
18447 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
18448 SYMBOL_TYPE (sym)
18449 = objfile_type (objfile)->nodebug_data_symbol;
18450
18451 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18452 /* In the case of DW_TAG_member, we should only be called for
18453 static const members. */
18454 if (die->tag == DW_TAG_member)
18455 {
18456 /* dwarf2_add_field uses die_is_declaration,
18457 so we do the same. */
18458 gdb_assert (die_is_declaration (die, cu));
18459 gdb_assert (attr);
18460 }
18461 if (attr)
18462 {
18463 dwarf2_const_value (attr, sym, cu);
18464 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18465 if (!suppress_add)
18466 {
18467 if (attr2 && (DW_UNSND (attr2) != 0))
18468 list_to_add = &global_symbols;
18469 else
18470 list_to_add = cu->list_in_scope;
18471 }
18472 break;
18473 }
18474 attr = dwarf2_attr (die, DW_AT_location, cu);
18475 if (attr)
18476 {
18477 var_decode_location (attr, sym, cu);
18478 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18479
18480 /* Fortran explicitly imports any global symbols to the local
18481 scope by DW_TAG_common_block. */
18482 if (cu->language == language_fortran && die->parent
18483 && die->parent->tag == DW_TAG_common_block)
18484 attr2 = NULL;
18485
18486 if (SYMBOL_CLASS (sym) == LOC_STATIC
18487 && SYMBOL_VALUE_ADDRESS (sym) == 0
18488 && !dwarf2_per_objfile->has_section_at_zero)
18489 {
18490 /* When a static variable is eliminated by the linker,
18491 the corresponding debug information is not stripped
18492 out, but the variable address is set to null;
18493 do not add such variables into symbol table. */
18494 }
18495 else if (attr2 && (DW_UNSND (attr2) != 0))
18496 {
18497 /* Workaround gfortran PR debug/40040 - it uses
18498 DW_AT_location for variables in -fPIC libraries which may
18499 get overriden by other libraries/executable and get
18500 a different address. Resolve it by the minimal symbol
18501 which may come from inferior's executable using copy
18502 relocation. Make this workaround only for gfortran as for
18503 other compilers GDB cannot guess the minimal symbol
18504 Fortran mangling kind. */
18505 if (cu->language == language_fortran && die->parent
18506 && die->parent->tag == DW_TAG_module
18507 && cu->producer
18508 && startswith (cu->producer, "GNU Fortran"))
18509 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
18510
18511 /* A variable with DW_AT_external is never static,
18512 but it may be block-scoped. */
18513 list_to_add = (cu->list_in_scope == &file_symbols
18514 ? &global_symbols : cu->list_in_scope);
18515 }
18516 else
18517 list_to_add = cu->list_in_scope;
18518 }
18519 else
18520 {
18521 /* We do not know the address of this symbol.
18522 If it is an external symbol and we have type information
18523 for it, enter the symbol as a LOC_UNRESOLVED symbol.
18524 The address of the variable will then be determined from
18525 the minimal symbol table whenever the variable is
18526 referenced. */
18527 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18528
18529 /* Fortran explicitly imports any global symbols to the local
18530 scope by DW_TAG_common_block. */
18531 if (cu->language == language_fortran && die->parent
18532 && die->parent->tag == DW_TAG_common_block)
18533 {
18534 /* SYMBOL_CLASS doesn't matter here because
18535 read_common_block is going to reset it. */
18536 if (!suppress_add)
18537 list_to_add = cu->list_in_scope;
18538 }
18539 else if (attr2 && (DW_UNSND (attr2) != 0)
18540 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
18541 {
18542 /* A variable with DW_AT_external is never static, but it
18543 may be block-scoped. */
18544 list_to_add = (cu->list_in_scope == &file_symbols
18545 ? &global_symbols : cu->list_in_scope);
18546
18547 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
18548 }
18549 else if (!die_is_declaration (die, cu))
18550 {
18551 /* Use the default LOC_OPTIMIZED_OUT class. */
18552 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
18553 if (!suppress_add)
18554 list_to_add = cu->list_in_scope;
18555 }
18556 }
18557 break;
18558 case DW_TAG_formal_parameter:
18559 /* If we are inside a function, mark this as an argument. If
18560 not, we might be looking at an argument to an inlined function
18561 when we do not have enough information to show inlined frames;
18562 pretend it's a local variable in that case so that the user can
18563 still see it. */
18564 if (context_stack_depth > 0
18565 && context_stack[context_stack_depth - 1].name != NULL)
18566 SYMBOL_IS_ARGUMENT (sym) = 1;
18567 attr = dwarf2_attr (die, DW_AT_location, cu);
18568 if (attr)
18569 {
18570 var_decode_location (attr, sym, cu);
18571 }
18572 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18573 if (attr)
18574 {
18575 dwarf2_const_value (attr, sym, cu);
18576 }
18577
18578 list_to_add = cu->list_in_scope;
18579 break;
18580 case DW_TAG_unspecified_parameters:
18581 /* From varargs functions; gdb doesn't seem to have any
18582 interest in this information, so just ignore it for now.
18583 (FIXME?) */
18584 break;
18585 case DW_TAG_template_type_param:
18586 suppress_add = 1;
18587 /* Fall through. */
18588 case DW_TAG_class_type:
18589 case DW_TAG_interface_type:
18590 case DW_TAG_structure_type:
18591 case DW_TAG_union_type:
18592 case DW_TAG_set_type:
18593 case DW_TAG_enumeration_type:
18594 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18595 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
18596
18597 {
18598 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
18599 really ever be static objects: otherwise, if you try
18600 to, say, break of a class's method and you're in a file
18601 which doesn't mention that class, it won't work unless
18602 the check for all static symbols in lookup_symbol_aux
18603 saves you. See the OtherFileClass tests in
18604 gdb.c++/namespace.exp. */
18605
18606 if (!suppress_add)
18607 {
18608 list_to_add = (cu->list_in_scope == &file_symbols
18609 && cu->language == language_cplus
18610 ? &global_symbols : cu->list_in_scope);
18611
18612 /* The semantics of C++ state that "struct foo {
18613 ... }" also defines a typedef for "foo". */
18614 if (cu->language == language_cplus
18615 || cu->language == language_ada
18616 || cu->language == language_d
18617 || cu->language == language_rust)
18618 {
18619 /* The symbol's name is already allocated along
18620 with this objfile, so we don't need to
18621 duplicate it for the type. */
18622 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
18623 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
18624 }
18625 }
18626 }
18627 break;
18628 case DW_TAG_typedef:
18629 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18630 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18631 list_to_add = cu->list_in_scope;
18632 break;
18633 case DW_TAG_base_type:
18634 case DW_TAG_subrange_type:
18635 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18636 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18637 list_to_add = cu->list_in_scope;
18638 break;
18639 case DW_TAG_enumerator:
18640 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18641 if (attr)
18642 {
18643 dwarf2_const_value (attr, sym, cu);
18644 }
18645 {
18646 /* NOTE: carlton/2003-11-10: See comment above in the
18647 DW_TAG_class_type, etc. block. */
18648
18649 list_to_add = (cu->list_in_scope == &file_symbols
18650 && cu->language == language_cplus
18651 ? &global_symbols : cu->list_in_scope);
18652 }
18653 break;
18654 case DW_TAG_imported_declaration:
18655 case DW_TAG_namespace:
18656 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18657 list_to_add = &global_symbols;
18658 break;
18659 case DW_TAG_module:
18660 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
18661 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
18662 list_to_add = &global_symbols;
18663 break;
18664 case DW_TAG_common_block:
18665 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
18666 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
18667 add_symbol_to_list (sym, cu->list_in_scope);
18668 break;
18669 default:
18670 /* Not a tag we recognize. Hopefully we aren't processing
18671 trash data, but since we must specifically ignore things
18672 we don't recognize, there is nothing else we should do at
18673 this point. */
18674 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
18675 dwarf_tag_name (die->tag));
18676 break;
18677 }
18678
18679 if (suppress_add)
18680 {
18681 sym->hash_next = objfile->template_symbols;
18682 objfile->template_symbols = sym;
18683 list_to_add = NULL;
18684 }
18685
18686 if (list_to_add != NULL)
18687 add_symbol_to_list (sym, list_to_add);
18688
18689 /* For the benefit of old versions of GCC, check for anonymous
18690 namespaces based on the demangled name. */
18691 if (!cu->processing_has_namespace_info
18692 && cu->language == language_cplus)
18693 cp_scan_for_anonymous_namespaces (sym, objfile);
18694 }
18695 return (sym);
18696 }
18697
18698 /* A wrapper for new_symbol_full that always allocates a new symbol. */
18699
18700 static struct symbol *
18701 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18702 {
18703 return new_symbol_full (die, type, cu, NULL);
18704 }
18705
18706 /* Given an attr with a DW_FORM_dataN value in host byte order,
18707 zero-extend it as appropriate for the symbol's type. The DWARF
18708 standard (v4) is not entirely clear about the meaning of using
18709 DW_FORM_dataN for a constant with a signed type, where the type is
18710 wider than the data. The conclusion of a discussion on the DWARF
18711 list was that this is unspecified. We choose to always zero-extend
18712 because that is the interpretation long in use by GCC. */
18713
18714 static gdb_byte *
18715 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
18716 struct dwarf2_cu *cu, LONGEST *value, int bits)
18717 {
18718 struct objfile *objfile = cu->objfile;
18719 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
18720 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
18721 LONGEST l = DW_UNSND (attr);
18722
18723 if (bits < sizeof (*value) * 8)
18724 {
18725 l &= ((LONGEST) 1 << bits) - 1;
18726 *value = l;
18727 }
18728 else if (bits == sizeof (*value) * 8)
18729 *value = l;
18730 else
18731 {
18732 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
18733 store_unsigned_integer (bytes, bits / 8, byte_order, l);
18734 return bytes;
18735 }
18736
18737 return NULL;
18738 }
18739
18740 /* Read a constant value from an attribute. Either set *VALUE, or if
18741 the value does not fit in *VALUE, set *BYTES - either already
18742 allocated on the objfile obstack, or newly allocated on OBSTACK,
18743 or, set *BATON, if we translated the constant to a location
18744 expression. */
18745
18746 static void
18747 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
18748 const char *name, struct obstack *obstack,
18749 struct dwarf2_cu *cu,
18750 LONGEST *value, const gdb_byte **bytes,
18751 struct dwarf2_locexpr_baton **baton)
18752 {
18753 struct objfile *objfile = cu->objfile;
18754 struct comp_unit_head *cu_header = &cu->header;
18755 struct dwarf_block *blk;
18756 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
18757 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18758
18759 *value = 0;
18760 *bytes = NULL;
18761 *baton = NULL;
18762
18763 switch (attr->form)
18764 {
18765 case DW_FORM_addr:
18766 case DW_FORM_GNU_addr_index:
18767 {
18768 gdb_byte *data;
18769
18770 if (TYPE_LENGTH (type) != cu_header->addr_size)
18771 dwarf2_const_value_length_mismatch_complaint (name,
18772 cu_header->addr_size,
18773 TYPE_LENGTH (type));
18774 /* Symbols of this form are reasonably rare, so we just
18775 piggyback on the existing location code rather than writing
18776 a new implementation of symbol_computed_ops. */
18777 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
18778 (*baton)->per_cu = cu->per_cu;
18779 gdb_assert ((*baton)->per_cu);
18780
18781 (*baton)->size = 2 + cu_header->addr_size;
18782 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
18783 (*baton)->data = data;
18784
18785 data[0] = DW_OP_addr;
18786 store_unsigned_integer (&data[1], cu_header->addr_size,
18787 byte_order, DW_ADDR (attr));
18788 data[cu_header->addr_size + 1] = DW_OP_stack_value;
18789 }
18790 break;
18791 case DW_FORM_string:
18792 case DW_FORM_strp:
18793 case DW_FORM_GNU_str_index:
18794 case DW_FORM_GNU_strp_alt:
18795 /* DW_STRING is already allocated on the objfile obstack, point
18796 directly to it. */
18797 *bytes = (const gdb_byte *) DW_STRING (attr);
18798 break;
18799 case DW_FORM_block1:
18800 case DW_FORM_block2:
18801 case DW_FORM_block4:
18802 case DW_FORM_block:
18803 case DW_FORM_exprloc:
18804 blk = DW_BLOCK (attr);
18805 if (TYPE_LENGTH (type) != blk->size)
18806 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
18807 TYPE_LENGTH (type));
18808 *bytes = blk->data;
18809 break;
18810
18811 /* The DW_AT_const_value attributes are supposed to carry the
18812 symbol's value "represented as it would be on the target
18813 architecture." By the time we get here, it's already been
18814 converted to host endianness, so we just need to sign- or
18815 zero-extend it as appropriate. */
18816 case DW_FORM_data1:
18817 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
18818 break;
18819 case DW_FORM_data2:
18820 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
18821 break;
18822 case DW_FORM_data4:
18823 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
18824 break;
18825 case DW_FORM_data8:
18826 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
18827 break;
18828
18829 case DW_FORM_sdata:
18830 *value = DW_SND (attr);
18831 break;
18832
18833 case DW_FORM_udata:
18834 *value = DW_UNSND (attr);
18835 break;
18836
18837 default:
18838 complaint (&symfile_complaints,
18839 _("unsupported const value attribute form: '%s'"),
18840 dwarf_form_name (attr->form));
18841 *value = 0;
18842 break;
18843 }
18844 }
18845
18846
18847 /* Copy constant value from an attribute to a symbol. */
18848
18849 static void
18850 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
18851 struct dwarf2_cu *cu)
18852 {
18853 struct objfile *objfile = cu->objfile;
18854 LONGEST value;
18855 const gdb_byte *bytes;
18856 struct dwarf2_locexpr_baton *baton;
18857
18858 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
18859 SYMBOL_PRINT_NAME (sym),
18860 &objfile->objfile_obstack, cu,
18861 &value, &bytes, &baton);
18862
18863 if (baton != NULL)
18864 {
18865 SYMBOL_LOCATION_BATON (sym) = baton;
18866 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
18867 }
18868 else if (bytes != NULL)
18869 {
18870 SYMBOL_VALUE_BYTES (sym) = bytes;
18871 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
18872 }
18873 else
18874 {
18875 SYMBOL_VALUE (sym) = value;
18876 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
18877 }
18878 }
18879
18880 /* Return the type of the die in question using its DW_AT_type attribute. */
18881
18882 static struct type *
18883 die_type (struct die_info *die, struct dwarf2_cu *cu)
18884 {
18885 struct attribute *type_attr;
18886
18887 type_attr = dwarf2_attr (die, DW_AT_type, cu);
18888 if (!type_attr)
18889 {
18890 /* A missing DW_AT_type represents a void type. */
18891 return objfile_type (cu->objfile)->builtin_void;
18892 }
18893
18894 return lookup_die_type (die, type_attr, cu);
18895 }
18896
18897 /* True iff CU's producer generates GNAT Ada auxiliary information
18898 that allows to find parallel types through that information instead
18899 of having to do expensive parallel lookups by type name. */
18900
18901 static int
18902 need_gnat_info (struct dwarf2_cu *cu)
18903 {
18904 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
18905 of GNAT produces this auxiliary information, without any indication
18906 that it is produced. Part of enhancing the FSF version of GNAT
18907 to produce that information will be to put in place an indicator
18908 that we can use in order to determine whether the descriptive type
18909 info is available or not. One suggestion that has been made is
18910 to use a new attribute, attached to the CU die. For now, assume
18911 that the descriptive type info is not available. */
18912 return 0;
18913 }
18914
18915 /* Return the auxiliary type of the die in question using its
18916 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
18917 attribute is not present. */
18918
18919 static struct type *
18920 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
18921 {
18922 struct attribute *type_attr;
18923
18924 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
18925 if (!type_attr)
18926 return NULL;
18927
18928 return lookup_die_type (die, type_attr, cu);
18929 }
18930
18931 /* If DIE has a descriptive_type attribute, then set the TYPE's
18932 descriptive type accordingly. */
18933
18934 static void
18935 set_descriptive_type (struct type *type, struct die_info *die,
18936 struct dwarf2_cu *cu)
18937 {
18938 struct type *descriptive_type = die_descriptive_type (die, cu);
18939
18940 if (descriptive_type)
18941 {
18942 ALLOCATE_GNAT_AUX_TYPE (type);
18943 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
18944 }
18945 }
18946
18947 /* Return the containing type of the die in question using its
18948 DW_AT_containing_type attribute. */
18949
18950 static struct type *
18951 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
18952 {
18953 struct attribute *type_attr;
18954
18955 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
18956 if (!type_attr)
18957 error (_("Dwarf Error: Problem turning containing type into gdb type "
18958 "[in module %s]"), objfile_name (cu->objfile));
18959
18960 return lookup_die_type (die, type_attr, cu);
18961 }
18962
18963 /* Return an error marker type to use for the ill formed type in DIE/CU. */
18964
18965 static struct type *
18966 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
18967 {
18968 struct objfile *objfile = dwarf2_per_objfile->objfile;
18969 char *message, *saved;
18970
18971 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
18972 objfile_name (objfile),
18973 cu->header.offset.sect_off,
18974 die->offset.sect_off);
18975 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
18976 message, strlen (message));
18977 xfree (message);
18978
18979 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
18980 }
18981
18982 /* Look up the type of DIE in CU using its type attribute ATTR.
18983 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
18984 DW_AT_containing_type.
18985 If there is no type substitute an error marker. */
18986
18987 static struct type *
18988 lookup_die_type (struct die_info *die, const struct attribute *attr,
18989 struct dwarf2_cu *cu)
18990 {
18991 struct objfile *objfile = cu->objfile;
18992 struct type *this_type;
18993
18994 gdb_assert (attr->name == DW_AT_type
18995 || attr->name == DW_AT_GNAT_descriptive_type
18996 || attr->name == DW_AT_containing_type);
18997
18998 /* First see if we have it cached. */
18999
19000 if (attr->form == DW_FORM_GNU_ref_alt)
19001 {
19002 struct dwarf2_per_cu_data *per_cu;
19003 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19004
19005 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19006 this_type = get_die_type_at_offset (offset, per_cu);
19007 }
19008 else if (attr_form_is_ref (attr))
19009 {
19010 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19011
19012 this_type = get_die_type_at_offset (offset, cu->per_cu);
19013 }
19014 else if (attr->form == DW_FORM_ref_sig8)
19015 {
19016 ULONGEST signature = DW_SIGNATURE (attr);
19017
19018 return get_signatured_type (die, signature, cu);
19019 }
19020 else
19021 {
19022 complaint (&symfile_complaints,
19023 _("Dwarf Error: Bad type attribute %s in DIE"
19024 " at 0x%x [in module %s]"),
19025 dwarf_attr_name (attr->name), die->offset.sect_off,
19026 objfile_name (objfile));
19027 return build_error_marker_type (cu, die);
19028 }
19029
19030 /* If not cached we need to read it in. */
19031
19032 if (this_type == NULL)
19033 {
19034 struct die_info *type_die = NULL;
19035 struct dwarf2_cu *type_cu = cu;
19036
19037 if (attr_form_is_ref (attr))
19038 type_die = follow_die_ref (die, attr, &type_cu);
19039 if (type_die == NULL)
19040 return build_error_marker_type (cu, die);
19041 /* If we find the type now, it's probably because the type came
19042 from an inter-CU reference and the type's CU got expanded before
19043 ours. */
19044 this_type = read_type_die (type_die, type_cu);
19045 }
19046
19047 /* If we still don't have a type use an error marker. */
19048
19049 if (this_type == NULL)
19050 return build_error_marker_type (cu, die);
19051
19052 return this_type;
19053 }
19054
19055 /* Return the type in DIE, CU.
19056 Returns NULL for invalid types.
19057
19058 This first does a lookup in die_type_hash,
19059 and only reads the die in if necessary.
19060
19061 NOTE: This can be called when reading in partial or full symbols. */
19062
19063 static struct type *
19064 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19065 {
19066 struct type *this_type;
19067
19068 this_type = get_die_type (die, cu);
19069 if (this_type)
19070 return this_type;
19071
19072 return read_type_die_1 (die, cu);
19073 }
19074
19075 /* Read the type in DIE, CU.
19076 Returns NULL for invalid types. */
19077
19078 static struct type *
19079 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19080 {
19081 struct type *this_type = NULL;
19082
19083 switch (die->tag)
19084 {
19085 case DW_TAG_class_type:
19086 case DW_TAG_interface_type:
19087 case DW_TAG_structure_type:
19088 case DW_TAG_union_type:
19089 this_type = read_structure_type (die, cu);
19090 break;
19091 case DW_TAG_enumeration_type:
19092 this_type = read_enumeration_type (die, cu);
19093 break;
19094 case DW_TAG_subprogram:
19095 case DW_TAG_subroutine_type:
19096 case DW_TAG_inlined_subroutine:
19097 this_type = read_subroutine_type (die, cu);
19098 break;
19099 case DW_TAG_array_type:
19100 this_type = read_array_type (die, cu);
19101 break;
19102 case DW_TAG_set_type:
19103 this_type = read_set_type (die, cu);
19104 break;
19105 case DW_TAG_pointer_type:
19106 this_type = read_tag_pointer_type (die, cu);
19107 break;
19108 case DW_TAG_ptr_to_member_type:
19109 this_type = read_tag_ptr_to_member_type (die, cu);
19110 break;
19111 case DW_TAG_reference_type:
19112 this_type = read_tag_reference_type (die, cu);
19113 break;
19114 case DW_TAG_const_type:
19115 this_type = read_tag_const_type (die, cu);
19116 break;
19117 case DW_TAG_volatile_type:
19118 this_type = read_tag_volatile_type (die, cu);
19119 break;
19120 case DW_TAG_restrict_type:
19121 this_type = read_tag_restrict_type (die, cu);
19122 break;
19123 case DW_TAG_string_type:
19124 this_type = read_tag_string_type (die, cu);
19125 break;
19126 case DW_TAG_typedef:
19127 this_type = read_typedef (die, cu);
19128 break;
19129 case DW_TAG_subrange_type:
19130 this_type = read_subrange_type (die, cu);
19131 break;
19132 case DW_TAG_base_type:
19133 this_type = read_base_type (die, cu);
19134 break;
19135 case DW_TAG_unspecified_type:
19136 this_type = read_unspecified_type (die, cu);
19137 break;
19138 case DW_TAG_namespace:
19139 this_type = read_namespace_type (die, cu);
19140 break;
19141 case DW_TAG_module:
19142 this_type = read_module_type (die, cu);
19143 break;
19144 case DW_TAG_atomic_type:
19145 this_type = read_tag_atomic_type (die, cu);
19146 break;
19147 default:
19148 complaint (&symfile_complaints,
19149 _("unexpected tag in read_type_die: '%s'"),
19150 dwarf_tag_name (die->tag));
19151 break;
19152 }
19153
19154 return this_type;
19155 }
19156
19157 /* See if we can figure out if the class lives in a namespace. We do
19158 this by looking for a member function; its demangled name will
19159 contain namespace info, if there is any.
19160 Return the computed name or NULL.
19161 Space for the result is allocated on the objfile's obstack.
19162 This is the full-die version of guess_partial_die_structure_name.
19163 In this case we know DIE has no useful parent. */
19164
19165 static char *
19166 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19167 {
19168 struct die_info *spec_die;
19169 struct dwarf2_cu *spec_cu;
19170 struct die_info *child;
19171
19172 spec_cu = cu;
19173 spec_die = die_specification (die, &spec_cu);
19174 if (spec_die != NULL)
19175 {
19176 die = spec_die;
19177 cu = spec_cu;
19178 }
19179
19180 for (child = die->child;
19181 child != NULL;
19182 child = child->sibling)
19183 {
19184 if (child->tag == DW_TAG_subprogram)
19185 {
19186 const char *linkage_name;
19187
19188 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19189 if (linkage_name == NULL)
19190 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19191 cu);
19192 if (linkage_name != NULL)
19193 {
19194 char *actual_name
19195 = language_class_name_from_physname (cu->language_defn,
19196 linkage_name);
19197 char *name = NULL;
19198
19199 if (actual_name != NULL)
19200 {
19201 const char *die_name = dwarf2_name (die, cu);
19202
19203 if (die_name != NULL
19204 && strcmp (die_name, actual_name) != 0)
19205 {
19206 /* Strip off the class name from the full name.
19207 We want the prefix. */
19208 int die_name_len = strlen (die_name);
19209 int actual_name_len = strlen (actual_name);
19210
19211 /* Test for '::' as a sanity check. */
19212 if (actual_name_len > die_name_len + 2
19213 && actual_name[actual_name_len
19214 - die_name_len - 1] == ':')
19215 name = (char *) obstack_copy0 (
19216 &cu->objfile->per_bfd->storage_obstack,
19217 actual_name, actual_name_len - die_name_len - 2);
19218 }
19219 }
19220 xfree (actual_name);
19221 return name;
19222 }
19223 }
19224 }
19225
19226 return NULL;
19227 }
19228
19229 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19230 prefix part in such case. See
19231 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19232
19233 static char *
19234 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19235 {
19236 struct attribute *attr;
19237 const char *base;
19238
19239 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19240 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19241 return NULL;
19242
19243 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19244 return NULL;
19245
19246 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19247 if (attr == NULL)
19248 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19249 if (attr == NULL || DW_STRING (attr) == NULL)
19250 return NULL;
19251
19252 /* dwarf2_name had to be already called. */
19253 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19254
19255 /* Strip the base name, keep any leading namespaces/classes. */
19256 base = strrchr (DW_STRING (attr), ':');
19257 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19258 return "";
19259
19260 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19261 DW_STRING (attr),
19262 &base[-1] - DW_STRING (attr));
19263 }
19264
19265 /* Return the name of the namespace/class that DIE is defined within,
19266 or "" if we can't tell. The caller should not xfree the result.
19267
19268 For example, if we're within the method foo() in the following
19269 code:
19270
19271 namespace N {
19272 class C {
19273 void foo () {
19274 }
19275 };
19276 }
19277
19278 then determine_prefix on foo's die will return "N::C". */
19279
19280 static const char *
19281 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19282 {
19283 struct die_info *parent, *spec_die;
19284 struct dwarf2_cu *spec_cu;
19285 struct type *parent_type;
19286 char *retval;
19287
19288 if (cu->language != language_cplus
19289 && cu->language != language_fortran && cu->language != language_d
19290 && cu->language != language_rust)
19291 return "";
19292
19293 retval = anonymous_struct_prefix (die, cu);
19294 if (retval)
19295 return retval;
19296
19297 /* We have to be careful in the presence of DW_AT_specification.
19298 For example, with GCC 3.4, given the code
19299
19300 namespace N {
19301 void foo() {
19302 // Definition of N::foo.
19303 }
19304 }
19305
19306 then we'll have a tree of DIEs like this:
19307
19308 1: DW_TAG_compile_unit
19309 2: DW_TAG_namespace // N
19310 3: DW_TAG_subprogram // declaration of N::foo
19311 4: DW_TAG_subprogram // definition of N::foo
19312 DW_AT_specification // refers to die #3
19313
19314 Thus, when processing die #4, we have to pretend that we're in
19315 the context of its DW_AT_specification, namely the contex of die
19316 #3. */
19317 spec_cu = cu;
19318 spec_die = die_specification (die, &spec_cu);
19319 if (spec_die == NULL)
19320 parent = die->parent;
19321 else
19322 {
19323 parent = spec_die->parent;
19324 cu = spec_cu;
19325 }
19326
19327 if (parent == NULL)
19328 return "";
19329 else if (parent->building_fullname)
19330 {
19331 const char *name;
19332 const char *parent_name;
19333
19334 /* It has been seen on RealView 2.2 built binaries,
19335 DW_TAG_template_type_param types actually _defined_ as
19336 children of the parent class:
19337
19338 enum E {};
19339 template class <class Enum> Class{};
19340 Class<enum E> class_e;
19341
19342 1: DW_TAG_class_type (Class)
19343 2: DW_TAG_enumeration_type (E)
19344 3: DW_TAG_enumerator (enum1:0)
19345 3: DW_TAG_enumerator (enum2:1)
19346 ...
19347 2: DW_TAG_template_type_param
19348 DW_AT_type DW_FORM_ref_udata (E)
19349
19350 Besides being broken debug info, it can put GDB into an
19351 infinite loop. Consider:
19352
19353 When we're building the full name for Class<E>, we'll start
19354 at Class, and go look over its template type parameters,
19355 finding E. We'll then try to build the full name of E, and
19356 reach here. We're now trying to build the full name of E,
19357 and look over the parent DIE for containing scope. In the
19358 broken case, if we followed the parent DIE of E, we'd again
19359 find Class, and once again go look at its template type
19360 arguments, etc., etc. Simply don't consider such parent die
19361 as source-level parent of this die (it can't be, the language
19362 doesn't allow it), and break the loop here. */
19363 name = dwarf2_name (die, cu);
19364 parent_name = dwarf2_name (parent, cu);
19365 complaint (&symfile_complaints,
19366 _("template param type '%s' defined within parent '%s'"),
19367 name ? name : "<unknown>",
19368 parent_name ? parent_name : "<unknown>");
19369 return "";
19370 }
19371 else
19372 switch (parent->tag)
19373 {
19374 case DW_TAG_namespace:
19375 parent_type = read_type_die (parent, cu);
19376 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19377 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19378 Work around this problem here. */
19379 if (cu->language == language_cplus
19380 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19381 return "";
19382 /* We give a name to even anonymous namespaces. */
19383 return TYPE_TAG_NAME (parent_type);
19384 case DW_TAG_class_type:
19385 case DW_TAG_interface_type:
19386 case DW_TAG_structure_type:
19387 case DW_TAG_union_type:
19388 case DW_TAG_module:
19389 parent_type = read_type_die (parent, cu);
19390 if (TYPE_TAG_NAME (parent_type) != NULL)
19391 return TYPE_TAG_NAME (parent_type);
19392 else
19393 /* An anonymous structure is only allowed non-static data
19394 members; no typedefs, no member functions, et cetera.
19395 So it does not need a prefix. */
19396 return "";
19397 case DW_TAG_compile_unit:
19398 case DW_TAG_partial_unit:
19399 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19400 if (cu->language == language_cplus
19401 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19402 && die->child != NULL
19403 && (die->tag == DW_TAG_class_type
19404 || die->tag == DW_TAG_structure_type
19405 || die->tag == DW_TAG_union_type))
19406 {
19407 char *name = guess_full_die_structure_name (die, cu);
19408 if (name != NULL)
19409 return name;
19410 }
19411 return "";
19412 case DW_TAG_enumeration_type:
19413 parent_type = read_type_die (parent, cu);
19414 if (TYPE_DECLARED_CLASS (parent_type))
19415 {
19416 if (TYPE_TAG_NAME (parent_type) != NULL)
19417 return TYPE_TAG_NAME (parent_type);
19418 return "";
19419 }
19420 /* Fall through. */
19421 default:
19422 return determine_prefix (parent, cu);
19423 }
19424 }
19425
19426 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19427 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19428 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19429 an obconcat, otherwise allocate storage for the result. The CU argument is
19430 used to determine the language and hence, the appropriate separator. */
19431
19432 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19433
19434 static char *
19435 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19436 int physname, struct dwarf2_cu *cu)
19437 {
19438 const char *lead = "";
19439 const char *sep;
19440
19441 if (suffix == NULL || suffix[0] == '\0'
19442 || prefix == NULL || prefix[0] == '\0')
19443 sep = "";
19444 else if (cu->language == language_d)
19445 {
19446 /* For D, the 'main' function could be defined in any module, but it
19447 should never be prefixed. */
19448 if (strcmp (suffix, "D main") == 0)
19449 {
19450 prefix = "";
19451 sep = "";
19452 }
19453 else
19454 sep = ".";
19455 }
19456 else if (cu->language == language_fortran && physname)
19457 {
19458 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19459 DW_AT_MIPS_linkage_name is preferred and used instead. */
19460
19461 lead = "__";
19462 sep = "_MOD_";
19463 }
19464 else
19465 sep = "::";
19466
19467 if (prefix == NULL)
19468 prefix = "";
19469 if (suffix == NULL)
19470 suffix = "";
19471
19472 if (obs == NULL)
19473 {
19474 char *retval
19475 = ((char *)
19476 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
19477
19478 strcpy (retval, lead);
19479 strcat (retval, prefix);
19480 strcat (retval, sep);
19481 strcat (retval, suffix);
19482 return retval;
19483 }
19484 else
19485 {
19486 /* We have an obstack. */
19487 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
19488 }
19489 }
19490
19491 /* Return sibling of die, NULL if no sibling. */
19492
19493 static struct die_info *
19494 sibling_die (struct die_info *die)
19495 {
19496 return die->sibling;
19497 }
19498
19499 /* Get name of a die, return NULL if not found. */
19500
19501 static const char *
19502 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
19503 struct obstack *obstack)
19504 {
19505 if (name && cu->language == language_cplus)
19506 {
19507 std::string canon_name = cp_canonicalize_string (name);
19508
19509 if (!canon_name.empty ())
19510 {
19511 if (canon_name != name)
19512 name = (const char *) obstack_copy0 (obstack,
19513 canon_name.c_str (),
19514 canon_name.length ());
19515 }
19516 }
19517
19518 return name;
19519 }
19520
19521 /* Get name of a die, return NULL if not found.
19522 Anonymous namespaces are converted to their magic string. */
19523
19524 static const char *
19525 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
19526 {
19527 struct attribute *attr;
19528
19529 attr = dwarf2_attr (die, DW_AT_name, cu);
19530 if ((!attr || !DW_STRING (attr))
19531 && die->tag != DW_TAG_namespace
19532 && die->tag != DW_TAG_class_type
19533 && die->tag != DW_TAG_interface_type
19534 && die->tag != DW_TAG_structure_type
19535 && die->tag != DW_TAG_union_type)
19536 return NULL;
19537
19538 switch (die->tag)
19539 {
19540 case DW_TAG_compile_unit:
19541 case DW_TAG_partial_unit:
19542 /* Compilation units have a DW_AT_name that is a filename, not
19543 a source language identifier. */
19544 case DW_TAG_enumeration_type:
19545 case DW_TAG_enumerator:
19546 /* These tags always have simple identifiers already; no need
19547 to canonicalize them. */
19548 return DW_STRING (attr);
19549
19550 case DW_TAG_namespace:
19551 if (attr != NULL && DW_STRING (attr) != NULL)
19552 return DW_STRING (attr);
19553 return CP_ANONYMOUS_NAMESPACE_STR;
19554
19555 case DW_TAG_class_type:
19556 case DW_TAG_interface_type:
19557 case DW_TAG_structure_type:
19558 case DW_TAG_union_type:
19559 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
19560 structures or unions. These were of the form "._%d" in GCC 4.1,
19561 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
19562 and GCC 4.4. We work around this problem by ignoring these. */
19563 if (attr && DW_STRING (attr)
19564 && (startswith (DW_STRING (attr), "._")
19565 || startswith (DW_STRING (attr), "<anonymous")))
19566 return NULL;
19567
19568 /* GCC might emit a nameless typedef that has a linkage name. See
19569 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19570 if (!attr || DW_STRING (attr) == NULL)
19571 {
19572 char *demangled = NULL;
19573
19574 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19575 if (attr == NULL)
19576 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19577
19578 if (attr == NULL || DW_STRING (attr) == NULL)
19579 return NULL;
19580
19581 /* Avoid demangling DW_STRING (attr) the second time on a second
19582 call for the same DIE. */
19583 if (!DW_STRING_IS_CANONICAL (attr))
19584 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
19585
19586 if (demangled)
19587 {
19588 const char *base;
19589
19590 /* FIXME: we already did this for the partial symbol... */
19591 DW_STRING (attr)
19592 = ((const char *)
19593 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19594 demangled, strlen (demangled)));
19595 DW_STRING_IS_CANONICAL (attr) = 1;
19596 xfree (demangled);
19597
19598 /* Strip any leading namespaces/classes, keep only the base name.
19599 DW_AT_name for named DIEs does not contain the prefixes. */
19600 base = strrchr (DW_STRING (attr), ':');
19601 if (base && base > DW_STRING (attr) && base[-1] == ':')
19602 return &base[1];
19603 else
19604 return DW_STRING (attr);
19605 }
19606 }
19607 break;
19608
19609 default:
19610 break;
19611 }
19612
19613 if (!DW_STRING_IS_CANONICAL (attr))
19614 {
19615 DW_STRING (attr)
19616 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
19617 &cu->objfile->per_bfd->storage_obstack);
19618 DW_STRING_IS_CANONICAL (attr) = 1;
19619 }
19620 return DW_STRING (attr);
19621 }
19622
19623 /* Return the die that this die in an extension of, or NULL if there
19624 is none. *EXT_CU is the CU containing DIE on input, and the CU
19625 containing the return value on output. */
19626
19627 static struct die_info *
19628 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
19629 {
19630 struct attribute *attr;
19631
19632 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
19633 if (attr == NULL)
19634 return NULL;
19635
19636 return follow_die_ref (die, attr, ext_cu);
19637 }
19638
19639 /* Convert a DIE tag into its string name. */
19640
19641 static const char *
19642 dwarf_tag_name (unsigned tag)
19643 {
19644 const char *name = get_DW_TAG_name (tag);
19645
19646 if (name == NULL)
19647 return "DW_TAG_<unknown>";
19648
19649 return name;
19650 }
19651
19652 /* Convert a DWARF attribute code into its string name. */
19653
19654 static const char *
19655 dwarf_attr_name (unsigned attr)
19656 {
19657 const char *name;
19658
19659 #ifdef MIPS /* collides with DW_AT_HP_block_index */
19660 if (attr == DW_AT_MIPS_fde)
19661 return "DW_AT_MIPS_fde";
19662 #else
19663 if (attr == DW_AT_HP_block_index)
19664 return "DW_AT_HP_block_index";
19665 #endif
19666
19667 name = get_DW_AT_name (attr);
19668
19669 if (name == NULL)
19670 return "DW_AT_<unknown>";
19671
19672 return name;
19673 }
19674
19675 /* Convert a DWARF value form code into its string name. */
19676
19677 static const char *
19678 dwarf_form_name (unsigned form)
19679 {
19680 const char *name = get_DW_FORM_name (form);
19681
19682 if (name == NULL)
19683 return "DW_FORM_<unknown>";
19684
19685 return name;
19686 }
19687
19688 static char *
19689 dwarf_bool_name (unsigned mybool)
19690 {
19691 if (mybool)
19692 return "TRUE";
19693 else
19694 return "FALSE";
19695 }
19696
19697 /* Convert a DWARF type code into its string name. */
19698
19699 static const char *
19700 dwarf_type_encoding_name (unsigned enc)
19701 {
19702 const char *name = get_DW_ATE_name (enc);
19703
19704 if (name == NULL)
19705 return "DW_ATE_<unknown>";
19706
19707 return name;
19708 }
19709
19710 static void
19711 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
19712 {
19713 unsigned int i;
19714
19715 print_spaces (indent, f);
19716 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
19717 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
19718
19719 if (die->parent != NULL)
19720 {
19721 print_spaces (indent, f);
19722 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
19723 die->parent->offset.sect_off);
19724 }
19725
19726 print_spaces (indent, f);
19727 fprintf_unfiltered (f, " has children: %s\n",
19728 dwarf_bool_name (die->child != NULL));
19729
19730 print_spaces (indent, f);
19731 fprintf_unfiltered (f, " attributes:\n");
19732
19733 for (i = 0; i < die->num_attrs; ++i)
19734 {
19735 print_spaces (indent, f);
19736 fprintf_unfiltered (f, " %s (%s) ",
19737 dwarf_attr_name (die->attrs[i].name),
19738 dwarf_form_name (die->attrs[i].form));
19739
19740 switch (die->attrs[i].form)
19741 {
19742 case DW_FORM_addr:
19743 case DW_FORM_GNU_addr_index:
19744 fprintf_unfiltered (f, "address: ");
19745 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
19746 break;
19747 case DW_FORM_block2:
19748 case DW_FORM_block4:
19749 case DW_FORM_block:
19750 case DW_FORM_block1:
19751 fprintf_unfiltered (f, "block: size %s",
19752 pulongest (DW_BLOCK (&die->attrs[i])->size));
19753 break;
19754 case DW_FORM_exprloc:
19755 fprintf_unfiltered (f, "expression: size %s",
19756 pulongest (DW_BLOCK (&die->attrs[i])->size));
19757 break;
19758 case DW_FORM_ref_addr:
19759 fprintf_unfiltered (f, "ref address: ");
19760 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
19761 break;
19762 case DW_FORM_GNU_ref_alt:
19763 fprintf_unfiltered (f, "alt ref address: ");
19764 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
19765 break;
19766 case DW_FORM_ref1:
19767 case DW_FORM_ref2:
19768 case DW_FORM_ref4:
19769 case DW_FORM_ref8:
19770 case DW_FORM_ref_udata:
19771 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
19772 (long) (DW_UNSND (&die->attrs[i])));
19773 break;
19774 case DW_FORM_data1:
19775 case DW_FORM_data2:
19776 case DW_FORM_data4:
19777 case DW_FORM_data8:
19778 case DW_FORM_udata:
19779 case DW_FORM_sdata:
19780 fprintf_unfiltered (f, "constant: %s",
19781 pulongest (DW_UNSND (&die->attrs[i])));
19782 break;
19783 case DW_FORM_sec_offset:
19784 fprintf_unfiltered (f, "section offset: %s",
19785 pulongest (DW_UNSND (&die->attrs[i])));
19786 break;
19787 case DW_FORM_ref_sig8:
19788 fprintf_unfiltered (f, "signature: %s",
19789 hex_string (DW_SIGNATURE (&die->attrs[i])));
19790 break;
19791 case DW_FORM_string:
19792 case DW_FORM_strp:
19793 case DW_FORM_GNU_str_index:
19794 case DW_FORM_GNU_strp_alt:
19795 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
19796 DW_STRING (&die->attrs[i])
19797 ? DW_STRING (&die->attrs[i]) : "",
19798 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
19799 break;
19800 case DW_FORM_flag:
19801 if (DW_UNSND (&die->attrs[i]))
19802 fprintf_unfiltered (f, "flag: TRUE");
19803 else
19804 fprintf_unfiltered (f, "flag: FALSE");
19805 break;
19806 case DW_FORM_flag_present:
19807 fprintf_unfiltered (f, "flag: TRUE");
19808 break;
19809 case DW_FORM_indirect:
19810 /* The reader will have reduced the indirect form to
19811 the "base form" so this form should not occur. */
19812 fprintf_unfiltered (f,
19813 "unexpected attribute form: DW_FORM_indirect");
19814 break;
19815 default:
19816 fprintf_unfiltered (f, "unsupported attribute form: %d.",
19817 die->attrs[i].form);
19818 break;
19819 }
19820 fprintf_unfiltered (f, "\n");
19821 }
19822 }
19823
19824 static void
19825 dump_die_for_error (struct die_info *die)
19826 {
19827 dump_die_shallow (gdb_stderr, 0, die);
19828 }
19829
19830 static void
19831 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
19832 {
19833 int indent = level * 4;
19834
19835 gdb_assert (die != NULL);
19836
19837 if (level >= max_level)
19838 return;
19839
19840 dump_die_shallow (f, indent, die);
19841
19842 if (die->child != NULL)
19843 {
19844 print_spaces (indent, f);
19845 fprintf_unfiltered (f, " Children:");
19846 if (level + 1 < max_level)
19847 {
19848 fprintf_unfiltered (f, "\n");
19849 dump_die_1 (f, level + 1, max_level, die->child);
19850 }
19851 else
19852 {
19853 fprintf_unfiltered (f,
19854 " [not printed, max nesting level reached]\n");
19855 }
19856 }
19857
19858 if (die->sibling != NULL && level > 0)
19859 {
19860 dump_die_1 (f, level, max_level, die->sibling);
19861 }
19862 }
19863
19864 /* This is called from the pdie macro in gdbinit.in.
19865 It's not static so gcc will keep a copy callable from gdb. */
19866
19867 void
19868 dump_die (struct die_info *die, int max_level)
19869 {
19870 dump_die_1 (gdb_stdlog, 0, max_level, die);
19871 }
19872
19873 static void
19874 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
19875 {
19876 void **slot;
19877
19878 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
19879 INSERT);
19880
19881 *slot = die;
19882 }
19883
19884 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
19885 required kind. */
19886
19887 static sect_offset
19888 dwarf2_get_ref_die_offset (const struct attribute *attr)
19889 {
19890 sect_offset retval = { DW_UNSND (attr) };
19891
19892 if (attr_form_is_ref (attr))
19893 return retval;
19894
19895 retval.sect_off = 0;
19896 complaint (&symfile_complaints,
19897 _("unsupported die ref attribute form: '%s'"),
19898 dwarf_form_name (attr->form));
19899 return retval;
19900 }
19901
19902 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
19903 * the value held by the attribute is not constant. */
19904
19905 static LONGEST
19906 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
19907 {
19908 if (attr->form == DW_FORM_sdata)
19909 return DW_SND (attr);
19910 else if (attr->form == DW_FORM_udata
19911 || attr->form == DW_FORM_data1
19912 || attr->form == DW_FORM_data2
19913 || attr->form == DW_FORM_data4
19914 || attr->form == DW_FORM_data8)
19915 return DW_UNSND (attr);
19916 else
19917 {
19918 complaint (&symfile_complaints,
19919 _("Attribute value is not a constant (%s)"),
19920 dwarf_form_name (attr->form));
19921 return default_value;
19922 }
19923 }
19924
19925 /* Follow reference or signature attribute ATTR of SRC_DIE.
19926 On entry *REF_CU is the CU of SRC_DIE.
19927 On exit *REF_CU is the CU of the result. */
19928
19929 static struct die_info *
19930 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
19931 struct dwarf2_cu **ref_cu)
19932 {
19933 struct die_info *die;
19934
19935 if (attr_form_is_ref (attr))
19936 die = follow_die_ref (src_die, attr, ref_cu);
19937 else if (attr->form == DW_FORM_ref_sig8)
19938 die = follow_die_sig (src_die, attr, ref_cu);
19939 else
19940 {
19941 dump_die_for_error (src_die);
19942 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
19943 objfile_name ((*ref_cu)->objfile));
19944 }
19945
19946 return die;
19947 }
19948
19949 /* Follow reference OFFSET.
19950 On entry *REF_CU is the CU of the source die referencing OFFSET.
19951 On exit *REF_CU is the CU of the result.
19952 Returns NULL if OFFSET is invalid. */
19953
19954 static struct die_info *
19955 follow_die_offset (sect_offset offset, int offset_in_dwz,
19956 struct dwarf2_cu **ref_cu)
19957 {
19958 struct die_info temp_die;
19959 struct dwarf2_cu *target_cu, *cu = *ref_cu;
19960
19961 gdb_assert (cu->per_cu != NULL);
19962
19963 target_cu = cu;
19964
19965 if (cu->per_cu->is_debug_types)
19966 {
19967 /* .debug_types CUs cannot reference anything outside their CU.
19968 If they need to, they have to reference a signatured type via
19969 DW_FORM_ref_sig8. */
19970 if (! offset_in_cu_p (&cu->header, offset))
19971 return NULL;
19972 }
19973 else if (offset_in_dwz != cu->per_cu->is_dwz
19974 || ! offset_in_cu_p (&cu->header, offset))
19975 {
19976 struct dwarf2_per_cu_data *per_cu;
19977
19978 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
19979 cu->objfile);
19980
19981 /* If necessary, add it to the queue and load its DIEs. */
19982 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
19983 load_full_comp_unit (per_cu, cu->language);
19984
19985 target_cu = per_cu->cu;
19986 }
19987 else if (cu->dies == NULL)
19988 {
19989 /* We're loading full DIEs during partial symbol reading. */
19990 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
19991 load_full_comp_unit (cu->per_cu, language_minimal);
19992 }
19993
19994 *ref_cu = target_cu;
19995 temp_die.offset = offset;
19996 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
19997 &temp_die, offset.sect_off);
19998 }
19999
20000 /* Follow reference attribute ATTR of SRC_DIE.
20001 On entry *REF_CU is the CU of SRC_DIE.
20002 On exit *REF_CU is the CU of the result. */
20003
20004 static struct die_info *
20005 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20006 struct dwarf2_cu **ref_cu)
20007 {
20008 sect_offset offset = dwarf2_get_ref_die_offset (attr);
20009 struct dwarf2_cu *cu = *ref_cu;
20010 struct die_info *die;
20011
20012 die = follow_die_offset (offset,
20013 (attr->form == DW_FORM_GNU_ref_alt
20014 || cu->per_cu->is_dwz),
20015 ref_cu);
20016 if (!die)
20017 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20018 "at 0x%x [in module %s]"),
20019 offset.sect_off, src_die->offset.sect_off,
20020 objfile_name (cu->objfile));
20021
20022 return die;
20023 }
20024
20025 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20026 Returned value is intended for DW_OP_call*. Returned
20027 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20028
20029 struct dwarf2_locexpr_baton
20030 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20031 struct dwarf2_per_cu_data *per_cu,
20032 CORE_ADDR (*get_frame_pc) (void *baton),
20033 void *baton)
20034 {
20035 struct dwarf2_cu *cu;
20036 struct die_info *die;
20037 struct attribute *attr;
20038 struct dwarf2_locexpr_baton retval;
20039
20040 dw2_setup (per_cu->objfile);
20041
20042 if (per_cu->cu == NULL)
20043 load_cu (per_cu);
20044 cu = per_cu->cu;
20045 if (cu == NULL)
20046 {
20047 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20048 Instead just throw an error, not much else we can do. */
20049 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20050 offset.sect_off, objfile_name (per_cu->objfile));
20051 }
20052
20053 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20054 if (!die)
20055 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20056 offset.sect_off, objfile_name (per_cu->objfile));
20057
20058 attr = dwarf2_attr (die, DW_AT_location, cu);
20059 if (!attr)
20060 {
20061 /* DWARF: "If there is no such attribute, then there is no effect.".
20062 DATA is ignored if SIZE is 0. */
20063
20064 retval.data = NULL;
20065 retval.size = 0;
20066 }
20067 else if (attr_form_is_section_offset (attr))
20068 {
20069 struct dwarf2_loclist_baton loclist_baton;
20070 CORE_ADDR pc = (*get_frame_pc) (baton);
20071 size_t size;
20072
20073 fill_in_loclist_baton (cu, &loclist_baton, attr);
20074
20075 retval.data = dwarf2_find_location_expression (&loclist_baton,
20076 &size, pc);
20077 retval.size = size;
20078 }
20079 else
20080 {
20081 if (!attr_form_is_block (attr))
20082 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20083 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20084 offset.sect_off, objfile_name (per_cu->objfile));
20085
20086 retval.data = DW_BLOCK (attr)->data;
20087 retval.size = DW_BLOCK (attr)->size;
20088 }
20089 retval.per_cu = cu->per_cu;
20090
20091 age_cached_comp_units ();
20092
20093 return retval;
20094 }
20095
20096 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20097 offset. */
20098
20099 struct dwarf2_locexpr_baton
20100 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20101 struct dwarf2_per_cu_data *per_cu,
20102 CORE_ADDR (*get_frame_pc) (void *baton),
20103 void *baton)
20104 {
20105 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20106
20107 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20108 }
20109
20110 /* Write a constant of a given type as target-ordered bytes into
20111 OBSTACK. */
20112
20113 static const gdb_byte *
20114 write_constant_as_bytes (struct obstack *obstack,
20115 enum bfd_endian byte_order,
20116 struct type *type,
20117 ULONGEST value,
20118 LONGEST *len)
20119 {
20120 gdb_byte *result;
20121
20122 *len = TYPE_LENGTH (type);
20123 result = (gdb_byte *) obstack_alloc (obstack, *len);
20124 store_unsigned_integer (result, *len, byte_order, value);
20125
20126 return result;
20127 }
20128
20129 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20130 pointer to the constant bytes and set LEN to the length of the
20131 data. If memory is needed, allocate it on OBSTACK. If the DIE
20132 does not have a DW_AT_const_value, return NULL. */
20133
20134 const gdb_byte *
20135 dwarf2_fetch_constant_bytes (sect_offset offset,
20136 struct dwarf2_per_cu_data *per_cu,
20137 struct obstack *obstack,
20138 LONGEST *len)
20139 {
20140 struct dwarf2_cu *cu;
20141 struct die_info *die;
20142 struct attribute *attr;
20143 const gdb_byte *result = NULL;
20144 struct type *type;
20145 LONGEST value;
20146 enum bfd_endian byte_order;
20147
20148 dw2_setup (per_cu->objfile);
20149
20150 if (per_cu->cu == NULL)
20151 load_cu (per_cu);
20152 cu = per_cu->cu;
20153 if (cu == NULL)
20154 {
20155 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20156 Instead just throw an error, not much else we can do. */
20157 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20158 offset.sect_off, objfile_name (per_cu->objfile));
20159 }
20160
20161 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20162 if (!die)
20163 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20164 offset.sect_off, objfile_name (per_cu->objfile));
20165
20166
20167 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20168 if (attr == NULL)
20169 return NULL;
20170
20171 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20172 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20173
20174 switch (attr->form)
20175 {
20176 case DW_FORM_addr:
20177 case DW_FORM_GNU_addr_index:
20178 {
20179 gdb_byte *tem;
20180
20181 *len = cu->header.addr_size;
20182 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20183 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20184 result = tem;
20185 }
20186 break;
20187 case DW_FORM_string:
20188 case DW_FORM_strp:
20189 case DW_FORM_GNU_str_index:
20190 case DW_FORM_GNU_strp_alt:
20191 /* DW_STRING is already allocated on the objfile obstack, point
20192 directly to it. */
20193 result = (const gdb_byte *) DW_STRING (attr);
20194 *len = strlen (DW_STRING (attr));
20195 break;
20196 case DW_FORM_block1:
20197 case DW_FORM_block2:
20198 case DW_FORM_block4:
20199 case DW_FORM_block:
20200 case DW_FORM_exprloc:
20201 result = DW_BLOCK (attr)->data;
20202 *len = DW_BLOCK (attr)->size;
20203 break;
20204
20205 /* The DW_AT_const_value attributes are supposed to carry the
20206 symbol's value "represented as it would be on the target
20207 architecture." By the time we get here, it's already been
20208 converted to host endianness, so we just need to sign- or
20209 zero-extend it as appropriate. */
20210 case DW_FORM_data1:
20211 type = die_type (die, cu);
20212 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20213 if (result == NULL)
20214 result = write_constant_as_bytes (obstack, byte_order,
20215 type, value, len);
20216 break;
20217 case DW_FORM_data2:
20218 type = die_type (die, cu);
20219 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20220 if (result == NULL)
20221 result = write_constant_as_bytes (obstack, byte_order,
20222 type, value, len);
20223 break;
20224 case DW_FORM_data4:
20225 type = die_type (die, cu);
20226 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20227 if (result == NULL)
20228 result = write_constant_as_bytes (obstack, byte_order,
20229 type, value, len);
20230 break;
20231 case DW_FORM_data8:
20232 type = die_type (die, cu);
20233 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20234 if (result == NULL)
20235 result = write_constant_as_bytes (obstack, byte_order,
20236 type, value, len);
20237 break;
20238
20239 case DW_FORM_sdata:
20240 type = die_type (die, cu);
20241 result = write_constant_as_bytes (obstack, byte_order,
20242 type, DW_SND (attr), len);
20243 break;
20244
20245 case DW_FORM_udata:
20246 type = die_type (die, cu);
20247 result = write_constant_as_bytes (obstack, byte_order,
20248 type, DW_UNSND (attr), len);
20249 break;
20250
20251 default:
20252 complaint (&symfile_complaints,
20253 _("unsupported const value attribute form: '%s'"),
20254 dwarf_form_name (attr->form));
20255 break;
20256 }
20257
20258 return result;
20259 }
20260
20261 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20262 PER_CU. */
20263
20264 struct type *
20265 dwarf2_get_die_type (cu_offset die_offset,
20266 struct dwarf2_per_cu_data *per_cu)
20267 {
20268 sect_offset die_offset_sect;
20269
20270 dw2_setup (per_cu->objfile);
20271
20272 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20273 return get_die_type_at_offset (die_offset_sect, per_cu);
20274 }
20275
20276 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20277 On entry *REF_CU is the CU of SRC_DIE.
20278 On exit *REF_CU is the CU of the result.
20279 Returns NULL if the referenced DIE isn't found. */
20280
20281 static struct die_info *
20282 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20283 struct dwarf2_cu **ref_cu)
20284 {
20285 struct die_info temp_die;
20286 struct dwarf2_cu *sig_cu;
20287 struct die_info *die;
20288
20289 /* While it might be nice to assert sig_type->type == NULL here,
20290 we can get here for DW_AT_imported_declaration where we need
20291 the DIE not the type. */
20292
20293 /* If necessary, add it to the queue and load its DIEs. */
20294
20295 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20296 read_signatured_type (sig_type);
20297
20298 sig_cu = sig_type->per_cu.cu;
20299 gdb_assert (sig_cu != NULL);
20300 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20301 temp_die.offset = sig_type->type_offset_in_section;
20302 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20303 temp_die.offset.sect_off);
20304 if (die)
20305 {
20306 /* For .gdb_index version 7 keep track of included TUs.
20307 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20308 if (dwarf2_per_objfile->index_table != NULL
20309 && dwarf2_per_objfile->index_table->version <= 7)
20310 {
20311 VEC_safe_push (dwarf2_per_cu_ptr,
20312 (*ref_cu)->per_cu->imported_symtabs,
20313 sig_cu->per_cu);
20314 }
20315
20316 *ref_cu = sig_cu;
20317 return die;
20318 }
20319
20320 return NULL;
20321 }
20322
20323 /* Follow signatured type referenced by ATTR in SRC_DIE.
20324 On entry *REF_CU is the CU of SRC_DIE.
20325 On exit *REF_CU is the CU of the result.
20326 The result is the DIE of the type.
20327 If the referenced type cannot be found an error is thrown. */
20328
20329 static struct die_info *
20330 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20331 struct dwarf2_cu **ref_cu)
20332 {
20333 ULONGEST signature = DW_SIGNATURE (attr);
20334 struct signatured_type *sig_type;
20335 struct die_info *die;
20336
20337 gdb_assert (attr->form == DW_FORM_ref_sig8);
20338
20339 sig_type = lookup_signatured_type (*ref_cu, signature);
20340 /* sig_type will be NULL if the signatured type is missing from
20341 the debug info. */
20342 if (sig_type == NULL)
20343 {
20344 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20345 " from DIE at 0x%x [in module %s]"),
20346 hex_string (signature), src_die->offset.sect_off,
20347 objfile_name ((*ref_cu)->objfile));
20348 }
20349
20350 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20351 if (die == NULL)
20352 {
20353 dump_die_for_error (src_die);
20354 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20355 " from DIE at 0x%x [in module %s]"),
20356 hex_string (signature), src_die->offset.sect_off,
20357 objfile_name ((*ref_cu)->objfile));
20358 }
20359
20360 return die;
20361 }
20362
20363 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20364 reading in and processing the type unit if necessary. */
20365
20366 static struct type *
20367 get_signatured_type (struct die_info *die, ULONGEST signature,
20368 struct dwarf2_cu *cu)
20369 {
20370 struct signatured_type *sig_type;
20371 struct dwarf2_cu *type_cu;
20372 struct die_info *type_die;
20373 struct type *type;
20374
20375 sig_type = lookup_signatured_type (cu, signature);
20376 /* sig_type will be NULL if the signatured type is missing from
20377 the debug info. */
20378 if (sig_type == NULL)
20379 {
20380 complaint (&symfile_complaints,
20381 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20382 " from DIE at 0x%x [in module %s]"),
20383 hex_string (signature), die->offset.sect_off,
20384 objfile_name (dwarf2_per_objfile->objfile));
20385 return build_error_marker_type (cu, die);
20386 }
20387
20388 /* If we already know the type we're done. */
20389 if (sig_type->type != NULL)
20390 return sig_type->type;
20391
20392 type_cu = cu;
20393 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20394 if (type_die != NULL)
20395 {
20396 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20397 is created. This is important, for example, because for c++ classes
20398 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20399 type = read_type_die (type_die, type_cu);
20400 if (type == NULL)
20401 {
20402 complaint (&symfile_complaints,
20403 _("Dwarf Error: Cannot build signatured type %s"
20404 " referenced from DIE at 0x%x [in module %s]"),
20405 hex_string (signature), die->offset.sect_off,
20406 objfile_name (dwarf2_per_objfile->objfile));
20407 type = build_error_marker_type (cu, die);
20408 }
20409 }
20410 else
20411 {
20412 complaint (&symfile_complaints,
20413 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20414 " from DIE at 0x%x [in module %s]"),
20415 hex_string (signature), die->offset.sect_off,
20416 objfile_name (dwarf2_per_objfile->objfile));
20417 type = build_error_marker_type (cu, die);
20418 }
20419 sig_type->type = type;
20420
20421 return type;
20422 }
20423
20424 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20425 reading in and processing the type unit if necessary. */
20426
20427 static struct type *
20428 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20429 struct dwarf2_cu *cu) /* ARI: editCase function */
20430 {
20431 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20432 if (attr_form_is_ref (attr))
20433 {
20434 struct dwarf2_cu *type_cu = cu;
20435 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20436
20437 return read_type_die (type_die, type_cu);
20438 }
20439 else if (attr->form == DW_FORM_ref_sig8)
20440 {
20441 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20442 }
20443 else
20444 {
20445 complaint (&symfile_complaints,
20446 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20447 " at 0x%x [in module %s]"),
20448 dwarf_form_name (attr->form), die->offset.sect_off,
20449 objfile_name (dwarf2_per_objfile->objfile));
20450 return build_error_marker_type (cu, die);
20451 }
20452 }
20453
20454 /* Load the DIEs associated with type unit PER_CU into memory. */
20455
20456 static void
20457 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
20458 {
20459 struct signatured_type *sig_type;
20460
20461 /* Caller is responsible for ensuring type_unit_groups don't get here. */
20462 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
20463
20464 /* We have the per_cu, but we need the signatured_type.
20465 Fortunately this is an easy translation. */
20466 gdb_assert (per_cu->is_debug_types);
20467 sig_type = (struct signatured_type *) per_cu;
20468
20469 gdb_assert (per_cu->cu == NULL);
20470
20471 read_signatured_type (sig_type);
20472
20473 gdb_assert (per_cu->cu != NULL);
20474 }
20475
20476 /* die_reader_func for read_signatured_type.
20477 This is identical to load_full_comp_unit_reader,
20478 but is kept separate for now. */
20479
20480 static void
20481 read_signatured_type_reader (const struct die_reader_specs *reader,
20482 const gdb_byte *info_ptr,
20483 struct die_info *comp_unit_die,
20484 int has_children,
20485 void *data)
20486 {
20487 struct dwarf2_cu *cu = reader->cu;
20488
20489 gdb_assert (cu->die_hash == NULL);
20490 cu->die_hash =
20491 htab_create_alloc_ex (cu->header.length / 12,
20492 die_hash,
20493 die_eq,
20494 NULL,
20495 &cu->comp_unit_obstack,
20496 hashtab_obstack_allocate,
20497 dummy_obstack_deallocate);
20498
20499 if (has_children)
20500 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
20501 &info_ptr, comp_unit_die);
20502 cu->dies = comp_unit_die;
20503 /* comp_unit_die is not stored in die_hash, no need. */
20504
20505 /* We try not to read any attributes in this function, because not
20506 all CUs needed for references have been loaded yet, and symbol
20507 table processing isn't initialized. But we have to set the CU language,
20508 or we won't be able to build types correctly.
20509 Similarly, if we do not read the producer, we can not apply
20510 producer-specific interpretation. */
20511 prepare_one_comp_unit (cu, cu->dies, language_minimal);
20512 }
20513
20514 /* Read in a signatured type and build its CU and DIEs.
20515 If the type is a stub for the real type in a DWO file,
20516 read in the real type from the DWO file as well. */
20517
20518 static void
20519 read_signatured_type (struct signatured_type *sig_type)
20520 {
20521 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
20522
20523 gdb_assert (per_cu->is_debug_types);
20524 gdb_assert (per_cu->cu == NULL);
20525
20526 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
20527 read_signatured_type_reader, NULL);
20528 sig_type->per_cu.tu_read = 1;
20529 }
20530
20531 /* Decode simple location descriptions.
20532 Given a pointer to a dwarf block that defines a location, compute
20533 the location and return the value.
20534
20535 NOTE drow/2003-11-18: This function is called in two situations
20536 now: for the address of static or global variables (partial symbols
20537 only) and for offsets into structures which are expected to be
20538 (more or less) constant. The partial symbol case should go away,
20539 and only the constant case should remain. That will let this
20540 function complain more accurately. A few special modes are allowed
20541 without complaint for global variables (for instance, global
20542 register values and thread-local values).
20543
20544 A location description containing no operations indicates that the
20545 object is optimized out. The return value is 0 for that case.
20546 FIXME drow/2003-11-16: No callers check for this case any more; soon all
20547 callers will only want a very basic result and this can become a
20548 complaint.
20549
20550 Note that stack[0] is unused except as a default error return. */
20551
20552 static CORE_ADDR
20553 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
20554 {
20555 struct objfile *objfile = cu->objfile;
20556 size_t i;
20557 size_t size = blk->size;
20558 const gdb_byte *data = blk->data;
20559 CORE_ADDR stack[64];
20560 int stacki;
20561 unsigned int bytes_read, unsnd;
20562 gdb_byte op;
20563
20564 i = 0;
20565 stacki = 0;
20566 stack[stacki] = 0;
20567 stack[++stacki] = 0;
20568
20569 while (i < size)
20570 {
20571 op = data[i++];
20572 switch (op)
20573 {
20574 case DW_OP_lit0:
20575 case DW_OP_lit1:
20576 case DW_OP_lit2:
20577 case DW_OP_lit3:
20578 case DW_OP_lit4:
20579 case DW_OP_lit5:
20580 case DW_OP_lit6:
20581 case DW_OP_lit7:
20582 case DW_OP_lit8:
20583 case DW_OP_lit9:
20584 case DW_OP_lit10:
20585 case DW_OP_lit11:
20586 case DW_OP_lit12:
20587 case DW_OP_lit13:
20588 case DW_OP_lit14:
20589 case DW_OP_lit15:
20590 case DW_OP_lit16:
20591 case DW_OP_lit17:
20592 case DW_OP_lit18:
20593 case DW_OP_lit19:
20594 case DW_OP_lit20:
20595 case DW_OP_lit21:
20596 case DW_OP_lit22:
20597 case DW_OP_lit23:
20598 case DW_OP_lit24:
20599 case DW_OP_lit25:
20600 case DW_OP_lit26:
20601 case DW_OP_lit27:
20602 case DW_OP_lit28:
20603 case DW_OP_lit29:
20604 case DW_OP_lit30:
20605 case DW_OP_lit31:
20606 stack[++stacki] = op - DW_OP_lit0;
20607 break;
20608
20609 case DW_OP_reg0:
20610 case DW_OP_reg1:
20611 case DW_OP_reg2:
20612 case DW_OP_reg3:
20613 case DW_OP_reg4:
20614 case DW_OP_reg5:
20615 case DW_OP_reg6:
20616 case DW_OP_reg7:
20617 case DW_OP_reg8:
20618 case DW_OP_reg9:
20619 case DW_OP_reg10:
20620 case DW_OP_reg11:
20621 case DW_OP_reg12:
20622 case DW_OP_reg13:
20623 case DW_OP_reg14:
20624 case DW_OP_reg15:
20625 case DW_OP_reg16:
20626 case DW_OP_reg17:
20627 case DW_OP_reg18:
20628 case DW_OP_reg19:
20629 case DW_OP_reg20:
20630 case DW_OP_reg21:
20631 case DW_OP_reg22:
20632 case DW_OP_reg23:
20633 case DW_OP_reg24:
20634 case DW_OP_reg25:
20635 case DW_OP_reg26:
20636 case DW_OP_reg27:
20637 case DW_OP_reg28:
20638 case DW_OP_reg29:
20639 case DW_OP_reg30:
20640 case DW_OP_reg31:
20641 stack[++stacki] = op - DW_OP_reg0;
20642 if (i < size)
20643 dwarf2_complex_location_expr_complaint ();
20644 break;
20645
20646 case DW_OP_regx:
20647 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
20648 i += bytes_read;
20649 stack[++stacki] = unsnd;
20650 if (i < size)
20651 dwarf2_complex_location_expr_complaint ();
20652 break;
20653
20654 case DW_OP_addr:
20655 stack[++stacki] = read_address (objfile->obfd, &data[i],
20656 cu, &bytes_read);
20657 i += bytes_read;
20658 break;
20659
20660 case DW_OP_const1u:
20661 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
20662 i += 1;
20663 break;
20664
20665 case DW_OP_const1s:
20666 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
20667 i += 1;
20668 break;
20669
20670 case DW_OP_const2u:
20671 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
20672 i += 2;
20673 break;
20674
20675 case DW_OP_const2s:
20676 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
20677 i += 2;
20678 break;
20679
20680 case DW_OP_const4u:
20681 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
20682 i += 4;
20683 break;
20684
20685 case DW_OP_const4s:
20686 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
20687 i += 4;
20688 break;
20689
20690 case DW_OP_const8u:
20691 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
20692 i += 8;
20693 break;
20694
20695 case DW_OP_constu:
20696 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
20697 &bytes_read);
20698 i += bytes_read;
20699 break;
20700
20701 case DW_OP_consts:
20702 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
20703 i += bytes_read;
20704 break;
20705
20706 case DW_OP_dup:
20707 stack[stacki + 1] = stack[stacki];
20708 stacki++;
20709 break;
20710
20711 case DW_OP_plus:
20712 stack[stacki - 1] += stack[stacki];
20713 stacki--;
20714 break;
20715
20716 case DW_OP_plus_uconst:
20717 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
20718 &bytes_read);
20719 i += bytes_read;
20720 break;
20721
20722 case DW_OP_minus:
20723 stack[stacki - 1] -= stack[stacki];
20724 stacki--;
20725 break;
20726
20727 case DW_OP_deref:
20728 /* If we're not the last op, then we definitely can't encode
20729 this using GDB's address_class enum. This is valid for partial
20730 global symbols, although the variable's address will be bogus
20731 in the psymtab. */
20732 if (i < size)
20733 dwarf2_complex_location_expr_complaint ();
20734 break;
20735
20736 case DW_OP_GNU_push_tls_address:
20737 case DW_OP_form_tls_address:
20738 /* The top of the stack has the offset from the beginning
20739 of the thread control block at which the variable is located. */
20740 /* Nothing should follow this operator, so the top of stack would
20741 be returned. */
20742 /* This is valid for partial global symbols, but the variable's
20743 address will be bogus in the psymtab. Make it always at least
20744 non-zero to not look as a variable garbage collected by linker
20745 which have DW_OP_addr 0. */
20746 if (i < size)
20747 dwarf2_complex_location_expr_complaint ();
20748 stack[stacki]++;
20749 break;
20750
20751 case DW_OP_GNU_uninit:
20752 break;
20753
20754 case DW_OP_GNU_addr_index:
20755 case DW_OP_GNU_const_index:
20756 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
20757 &bytes_read);
20758 i += bytes_read;
20759 break;
20760
20761 default:
20762 {
20763 const char *name = get_DW_OP_name (op);
20764
20765 if (name)
20766 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
20767 name);
20768 else
20769 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
20770 op);
20771 }
20772
20773 return (stack[stacki]);
20774 }
20775
20776 /* Enforce maximum stack depth of SIZE-1 to avoid writing
20777 outside of the allocated space. Also enforce minimum>0. */
20778 if (stacki >= ARRAY_SIZE (stack) - 1)
20779 {
20780 complaint (&symfile_complaints,
20781 _("location description stack overflow"));
20782 return 0;
20783 }
20784
20785 if (stacki <= 0)
20786 {
20787 complaint (&symfile_complaints,
20788 _("location description stack underflow"));
20789 return 0;
20790 }
20791 }
20792 return (stack[stacki]);
20793 }
20794
20795 /* memory allocation interface */
20796
20797 static struct dwarf_block *
20798 dwarf_alloc_block (struct dwarf2_cu *cu)
20799 {
20800 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
20801 }
20802
20803 static struct die_info *
20804 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
20805 {
20806 struct die_info *die;
20807 size_t size = sizeof (struct die_info);
20808
20809 if (num_attrs > 1)
20810 size += (num_attrs - 1) * sizeof (struct attribute);
20811
20812 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
20813 memset (die, 0, sizeof (struct die_info));
20814 return (die);
20815 }
20816
20817 \f
20818 /* Macro support. */
20819
20820 /* Return file name relative to the compilation directory of file number I in
20821 *LH's file name table. The result is allocated using xmalloc; the caller is
20822 responsible for freeing it. */
20823
20824 static char *
20825 file_file_name (int file, struct line_header *lh)
20826 {
20827 /* Is the file number a valid index into the line header's file name
20828 table? Remember that file numbers start with one, not zero. */
20829 if (1 <= file && file <= lh->num_file_names)
20830 {
20831 struct file_entry *fe = &lh->file_names[file - 1];
20832
20833 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
20834 || lh->include_dirs == NULL)
20835 return xstrdup (fe->name);
20836 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
20837 fe->name, (char *) NULL);
20838 }
20839 else
20840 {
20841 /* The compiler produced a bogus file number. We can at least
20842 record the macro definitions made in the file, even if we
20843 won't be able to find the file by name. */
20844 char fake_name[80];
20845
20846 xsnprintf (fake_name, sizeof (fake_name),
20847 "<bad macro file number %d>", file);
20848
20849 complaint (&symfile_complaints,
20850 _("bad file number in macro information (%d)"),
20851 file);
20852
20853 return xstrdup (fake_name);
20854 }
20855 }
20856
20857 /* Return the full name of file number I in *LH's file name table.
20858 Use COMP_DIR as the name of the current directory of the
20859 compilation. The result is allocated using xmalloc; the caller is
20860 responsible for freeing it. */
20861 static char *
20862 file_full_name (int file, struct line_header *lh, const char *comp_dir)
20863 {
20864 /* Is the file number a valid index into the line header's file name
20865 table? Remember that file numbers start with one, not zero. */
20866 if (1 <= file && file <= lh->num_file_names)
20867 {
20868 char *relative = file_file_name (file, lh);
20869
20870 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
20871 return relative;
20872 return reconcat (relative, comp_dir, SLASH_STRING,
20873 relative, (char *) NULL);
20874 }
20875 else
20876 return file_file_name (file, lh);
20877 }
20878
20879
20880 static struct macro_source_file *
20881 macro_start_file (int file, int line,
20882 struct macro_source_file *current_file,
20883 struct line_header *lh)
20884 {
20885 /* File name relative to the compilation directory of this source file. */
20886 char *file_name = file_file_name (file, lh);
20887
20888 if (! current_file)
20889 {
20890 /* Note: We don't create a macro table for this compilation unit
20891 at all until we actually get a filename. */
20892 struct macro_table *macro_table = get_macro_table ();
20893
20894 /* If we have no current file, then this must be the start_file
20895 directive for the compilation unit's main source file. */
20896 current_file = macro_set_main (macro_table, file_name);
20897 macro_define_special (macro_table);
20898 }
20899 else
20900 current_file = macro_include (current_file, line, file_name);
20901
20902 xfree (file_name);
20903
20904 return current_file;
20905 }
20906
20907
20908 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
20909 followed by a null byte. */
20910 static char *
20911 copy_string (const char *buf, int len)
20912 {
20913 char *s = (char *) xmalloc (len + 1);
20914
20915 memcpy (s, buf, len);
20916 s[len] = '\0';
20917 return s;
20918 }
20919
20920
20921 static const char *
20922 consume_improper_spaces (const char *p, const char *body)
20923 {
20924 if (*p == ' ')
20925 {
20926 complaint (&symfile_complaints,
20927 _("macro definition contains spaces "
20928 "in formal argument list:\n`%s'"),
20929 body);
20930
20931 while (*p == ' ')
20932 p++;
20933 }
20934
20935 return p;
20936 }
20937
20938
20939 static void
20940 parse_macro_definition (struct macro_source_file *file, int line,
20941 const char *body)
20942 {
20943 const char *p;
20944
20945 /* The body string takes one of two forms. For object-like macro
20946 definitions, it should be:
20947
20948 <macro name> " " <definition>
20949
20950 For function-like macro definitions, it should be:
20951
20952 <macro name> "() " <definition>
20953 or
20954 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
20955
20956 Spaces may appear only where explicitly indicated, and in the
20957 <definition>.
20958
20959 The Dwarf 2 spec says that an object-like macro's name is always
20960 followed by a space, but versions of GCC around March 2002 omit
20961 the space when the macro's definition is the empty string.
20962
20963 The Dwarf 2 spec says that there should be no spaces between the
20964 formal arguments in a function-like macro's formal argument list,
20965 but versions of GCC around March 2002 include spaces after the
20966 commas. */
20967
20968
20969 /* Find the extent of the macro name. The macro name is terminated
20970 by either a space or null character (for an object-like macro) or
20971 an opening paren (for a function-like macro). */
20972 for (p = body; *p; p++)
20973 if (*p == ' ' || *p == '(')
20974 break;
20975
20976 if (*p == ' ' || *p == '\0')
20977 {
20978 /* It's an object-like macro. */
20979 int name_len = p - body;
20980 char *name = copy_string (body, name_len);
20981 const char *replacement;
20982
20983 if (*p == ' ')
20984 replacement = body + name_len + 1;
20985 else
20986 {
20987 dwarf2_macro_malformed_definition_complaint (body);
20988 replacement = body + name_len;
20989 }
20990
20991 macro_define_object (file, line, name, replacement);
20992
20993 xfree (name);
20994 }
20995 else if (*p == '(')
20996 {
20997 /* It's a function-like macro. */
20998 char *name = copy_string (body, p - body);
20999 int argc = 0;
21000 int argv_size = 1;
21001 char **argv = XNEWVEC (char *, argv_size);
21002
21003 p++;
21004
21005 p = consume_improper_spaces (p, body);
21006
21007 /* Parse the formal argument list. */
21008 while (*p && *p != ')')
21009 {
21010 /* Find the extent of the current argument name. */
21011 const char *arg_start = p;
21012
21013 while (*p && *p != ',' && *p != ')' && *p != ' ')
21014 p++;
21015
21016 if (! *p || p == arg_start)
21017 dwarf2_macro_malformed_definition_complaint (body);
21018 else
21019 {
21020 /* Make sure argv has room for the new argument. */
21021 if (argc >= argv_size)
21022 {
21023 argv_size *= 2;
21024 argv = XRESIZEVEC (char *, argv, argv_size);
21025 }
21026
21027 argv[argc++] = copy_string (arg_start, p - arg_start);
21028 }
21029
21030 p = consume_improper_spaces (p, body);
21031
21032 /* Consume the comma, if present. */
21033 if (*p == ',')
21034 {
21035 p++;
21036
21037 p = consume_improper_spaces (p, body);
21038 }
21039 }
21040
21041 if (*p == ')')
21042 {
21043 p++;
21044
21045 if (*p == ' ')
21046 /* Perfectly formed definition, no complaints. */
21047 macro_define_function (file, line, name,
21048 argc, (const char **) argv,
21049 p + 1);
21050 else if (*p == '\0')
21051 {
21052 /* Complain, but do define it. */
21053 dwarf2_macro_malformed_definition_complaint (body);
21054 macro_define_function (file, line, name,
21055 argc, (const char **) argv,
21056 p);
21057 }
21058 else
21059 /* Just complain. */
21060 dwarf2_macro_malformed_definition_complaint (body);
21061 }
21062 else
21063 /* Just complain. */
21064 dwarf2_macro_malformed_definition_complaint (body);
21065
21066 xfree (name);
21067 {
21068 int i;
21069
21070 for (i = 0; i < argc; i++)
21071 xfree (argv[i]);
21072 }
21073 xfree (argv);
21074 }
21075 else
21076 dwarf2_macro_malformed_definition_complaint (body);
21077 }
21078
21079 /* Skip some bytes from BYTES according to the form given in FORM.
21080 Returns the new pointer. */
21081
21082 static const gdb_byte *
21083 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21084 enum dwarf_form form,
21085 unsigned int offset_size,
21086 struct dwarf2_section_info *section)
21087 {
21088 unsigned int bytes_read;
21089
21090 switch (form)
21091 {
21092 case DW_FORM_data1:
21093 case DW_FORM_flag:
21094 ++bytes;
21095 break;
21096
21097 case DW_FORM_data2:
21098 bytes += 2;
21099 break;
21100
21101 case DW_FORM_data4:
21102 bytes += 4;
21103 break;
21104
21105 case DW_FORM_data8:
21106 bytes += 8;
21107 break;
21108
21109 case DW_FORM_string:
21110 read_direct_string (abfd, bytes, &bytes_read);
21111 bytes += bytes_read;
21112 break;
21113
21114 case DW_FORM_sec_offset:
21115 case DW_FORM_strp:
21116 case DW_FORM_GNU_strp_alt:
21117 bytes += offset_size;
21118 break;
21119
21120 case DW_FORM_block:
21121 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21122 bytes += bytes_read;
21123 break;
21124
21125 case DW_FORM_block1:
21126 bytes += 1 + read_1_byte (abfd, bytes);
21127 break;
21128 case DW_FORM_block2:
21129 bytes += 2 + read_2_bytes (abfd, bytes);
21130 break;
21131 case DW_FORM_block4:
21132 bytes += 4 + read_4_bytes (abfd, bytes);
21133 break;
21134
21135 case DW_FORM_sdata:
21136 case DW_FORM_udata:
21137 case DW_FORM_GNU_addr_index:
21138 case DW_FORM_GNU_str_index:
21139 bytes = gdb_skip_leb128 (bytes, buffer_end);
21140 if (bytes == NULL)
21141 {
21142 dwarf2_section_buffer_overflow_complaint (section);
21143 return NULL;
21144 }
21145 break;
21146
21147 default:
21148 {
21149 complain:
21150 complaint (&symfile_complaints,
21151 _("invalid form 0x%x in `%s'"),
21152 form, get_section_name (section));
21153 return NULL;
21154 }
21155 }
21156
21157 return bytes;
21158 }
21159
21160 /* A helper for dwarf_decode_macros that handles skipping an unknown
21161 opcode. Returns an updated pointer to the macro data buffer; or,
21162 on error, issues a complaint and returns NULL. */
21163
21164 static const gdb_byte *
21165 skip_unknown_opcode (unsigned int opcode,
21166 const gdb_byte **opcode_definitions,
21167 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21168 bfd *abfd,
21169 unsigned int offset_size,
21170 struct dwarf2_section_info *section)
21171 {
21172 unsigned int bytes_read, i;
21173 unsigned long arg;
21174 const gdb_byte *defn;
21175
21176 if (opcode_definitions[opcode] == NULL)
21177 {
21178 complaint (&symfile_complaints,
21179 _("unrecognized DW_MACFINO opcode 0x%x"),
21180 opcode);
21181 return NULL;
21182 }
21183
21184 defn = opcode_definitions[opcode];
21185 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21186 defn += bytes_read;
21187
21188 for (i = 0; i < arg; ++i)
21189 {
21190 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21191 (enum dwarf_form) defn[i], offset_size,
21192 section);
21193 if (mac_ptr == NULL)
21194 {
21195 /* skip_form_bytes already issued the complaint. */
21196 return NULL;
21197 }
21198 }
21199
21200 return mac_ptr;
21201 }
21202
21203 /* A helper function which parses the header of a macro section.
21204 If the macro section is the extended (for now called "GNU") type,
21205 then this updates *OFFSET_SIZE. Returns a pointer to just after
21206 the header, or issues a complaint and returns NULL on error. */
21207
21208 static const gdb_byte *
21209 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21210 bfd *abfd,
21211 const gdb_byte *mac_ptr,
21212 unsigned int *offset_size,
21213 int section_is_gnu)
21214 {
21215 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21216
21217 if (section_is_gnu)
21218 {
21219 unsigned int version, flags;
21220
21221 version = read_2_bytes (abfd, mac_ptr);
21222 if (version != 4)
21223 {
21224 complaint (&symfile_complaints,
21225 _("unrecognized version `%d' in .debug_macro section"),
21226 version);
21227 return NULL;
21228 }
21229 mac_ptr += 2;
21230
21231 flags = read_1_byte (abfd, mac_ptr);
21232 ++mac_ptr;
21233 *offset_size = (flags & 1) ? 8 : 4;
21234
21235 if ((flags & 2) != 0)
21236 /* We don't need the line table offset. */
21237 mac_ptr += *offset_size;
21238
21239 /* Vendor opcode descriptions. */
21240 if ((flags & 4) != 0)
21241 {
21242 unsigned int i, count;
21243
21244 count = read_1_byte (abfd, mac_ptr);
21245 ++mac_ptr;
21246 for (i = 0; i < count; ++i)
21247 {
21248 unsigned int opcode, bytes_read;
21249 unsigned long arg;
21250
21251 opcode = read_1_byte (abfd, mac_ptr);
21252 ++mac_ptr;
21253 opcode_definitions[opcode] = mac_ptr;
21254 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21255 mac_ptr += bytes_read;
21256 mac_ptr += arg;
21257 }
21258 }
21259 }
21260
21261 return mac_ptr;
21262 }
21263
21264 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21265 including DW_MACRO_GNU_transparent_include. */
21266
21267 static void
21268 dwarf_decode_macro_bytes (bfd *abfd,
21269 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21270 struct macro_source_file *current_file,
21271 struct line_header *lh,
21272 struct dwarf2_section_info *section,
21273 int section_is_gnu, int section_is_dwz,
21274 unsigned int offset_size,
21275 htab_t include_hash)
21276 {
21277 struct objfile *objfile = dwarf2_per_objfile->objfile;
21278 enum dwarf_macro_record_type macinfo_type;
21279 int at_commandline;
21280 const gdb_byte *opcode_definitions[256];
21281
21282 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21283 &offset_size, section_is_gnu);
21284 if (mac_ptr == NULL)
21285 {
21286 /* We already issued a complaint. */
21287 return;
21288 }
21289
21290 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21291 GDB is still reading the definitions from command line. First
21292 DW_MACINFO_start_file will need to be ignored as it was already executed
21293 to create CURRENT_FILE for the main source holding also the command line
21294 definitions. On first met DW_MACINFO_start_file this flag is reset to
21295 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21296
21297 at_commandline = 1;
21298
21299 do
21300 {
21301 /* Do we at least have room for a macinfo type byte? */
21302 if (mac_ptr >= mac_end)
21303 {
21304 dwarf2_section_buffer_overflow_complaint (section);
21305 break;
21306 }
21307
21308 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21309 mac_ptr++;
21310
21311 /* Note that we rely on the fact that the corresponding GNU and
21312 DWARF constants are the same. */
21313 switch (macinfo_type)
21314 {
21315 /* A zero macinfo type indicates the end of the macro
21316 information. */
21317 case 0:
21318 break;
21319
21320 case DW_MACRO_GNU_define:
21321 case DW_MACRO_GNU_undef:
21322 case DW_MACRO_GNU_define_indirect:
21323 case DW_MACRO_GNU_undef_indirect:
21324 case DW_MACRO_GNU_define_indirect_alt:
21325 case DW_MACRO_GNU_undef_indirect_alt:
21326 {
21327 unsigned int bytes_read;
21328 int line;
21329 const char *body;
21330 int is_define;
21331
21332 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21333 mac_ptr += bytes_read;
21334
21335 if (macinfo_type == DW_MACRO_GNU_define
21336 || macinfo_type == DW_MACRO_GNU_undef)
21337 {
21338 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21339 mac_ptr += bytes_read;
21340 }
21341 else
21342 {
21343 LONGEST str_offset;
21344
21345 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21346 mac_ptr += offset_size;
21347
21348 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
21349 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
21350 || section_is_dwz)
21351 {
21352 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21353
21354 body = read_indirect_string_from_dwz (dwz, str_offset);
21355 }
21356 else
21357 body = read_indirect_string_at_offset (abfd, str_offset);
21358 }
21359
21360 is_define = (macinfo_type == DW_MACRO_GNU_define
21361 || macinfo_type == DW_MACRO_GNU_define_indirect
21362 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
21363 if (! current_file)
21364 {
21365 /* DWARF violation as no main source is present. */
21366 complaint (&symfile_complaints,
21367 _("debug info with no main source gives macro %s "
21368 "on line %d: %s"),
21369 is_define ? _("definition") : _("undefinition"),
21370 line, body);
21371 break;
21372 }
21373 if ((line == 0 && !at_commandline)
21374 || (line != 0 && at_commandline))
21375 complaint (&symfile_complaints,
21376 _("debug info gives %s macro %s with %s line %d: %s"),
21377 at_commandline ? _("command-line") : _("in-file"),
21378 is_define ? _("definition") : _("undefinition"),
21379 line == 0 ? _("zero") : _("non-zero"), line, body);
21380
21381 if (is_define)
21382 parse_macro_definition (current_file, line, body);
21383 else
21384 {
21385 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
21386 || macinfo_type == DW_MACRO_GNU_undef_indirect
21387 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
21388 macro_undef (current_file, line, body);
21389 }
21390 }
21391 break;
21392
21393 case DW_MACRO_GNU_start_file:
21394 {
21395 unsigned int bytes_read;
21396 int line, file;
21397
21398 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21399 mac_ptr += bytes_read;
21400 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21401 mac_ptr += bytes_read;
21402
21403 if ((line == 0 && !at_commandline)
21404 || (line != 0 && at_commandline))
21405 complaint (&symfile_complaints,
21406 _("debug info gives source %d included "
21407 "from %s at %s line %d"),
21408 file, at_commandline ? _("command-line") : _("file"),
21409 line == 0 ? _("zero") : _("non-zero"), line);
21410
21411 if (at_commandline)
21412 {
21413 /* This DW_MACRO_GNU_start_file was executed in the
21414 pass one. */
21415 at_commandline = 0;
21416 }
21417 else
21418 current_file = macro_start_file (file, line, current_file, lh);
21419 }
21420 break;
21421
21422 case DW_MACRO_GNU_end_file:
21423 if (! current_file)
21424 complaint (&symfile_complaints,
21425 _("macro debug info has an unmatched "
21426 "`close_file' directive"));
21427 else
21428 {
21429 current_file = current_file->included_by;
21430 if (! current_file)
21431 {
21432 enum dwarf_macro_record_type next_type;
21433
21434 /* GCC circa March 2002 doesn't produce the zero
21435 type byte marking the end of the compilation
21436 unit. Complain if it's not there, but exit no
21437 matter what. */
21438
21439 /* Do we at least have room for a macinfo type byte? */
21440 if (mac_ptr >= mac_end)
21441 {
21442 dwarf2_section_buffer_overflow_complaint (section);
21443 return;
21444 }
21445
21446 /* We don't increment mac_ptr here, so this is just
21447 a look-ahead. */
21448 next_type
21449 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21450 mac_ptr);
21451 if (next_type != 0)
21452 complaint (&symfile_complaints,
21453 _("no terminating 0-type entry for "
21454 "macros in `.debug_macinfo' section"));
21455
21456 return;
21457 }
21458 }
21459 break;
21460
21461 case DW_MACRO_GNU_transparent_include:
21462 case DW_MACRO_GNU_transparent_include_alt:
21463 {
21464 LONGEST offset;
21465 void **slot;
21466 bfd *include_bfd = abfd;
21467 struct dwarf2_section_info *include_section = section;
21468 const gdb_byte *include_mac_end = mac_end;
21469 int is_dwz = section_is_dwz;
21470 const gdb_byte *new_mac_ptr;
21471
21472 offset = read_offset_1 (abfd, mac_ptr, offset_size);
21473 mac_ptr += offset_size;
21474
21475 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
21476 {
21477 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21478
21479 dwarf2_read_section (objfile, &dwz->macro);
21480
21481 include_section = &dwz->macro;
21482 include_bfd = get_section_bfd_owner (include_section);
21483 include_mac_end = dwz->macro.buffer + dwz->macro.size;
21484 is_dwz = 1;
21485 }
21486
21487 new_mac_ptr = include_section->buffer + offset;
21488 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
21489
21490 if (*slot != NULL)
21491 {
21492 /* This has actually happened; see
21493 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
21494 complaint (&symfile_complaints,
21495 _("recursive DW_MACRO_GNU_transparent_include in "
21496 ".debug_macro section"));
21497 }
21498 else
21499 {
21500 *slot = (void *) new_mac_ptr;
21501
21502 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
21503 include_mac_end, current_file, lh,
21504 section, section_is_gnu, is_dwz,
21505 offset_size, include_hash);
21506
21507 htab_remove_elt (include_hash, (void *) new_mac_ptr);
21508 }
21509 }
21510 break;
21511
21512 case DW_MACINFO_vendor_ext:
21513 if (!section_is_gnu)
21514 {
21515 unsigned int bytes_read;
21516
21517 /* This reads the constant, but since we don't recognize
21518 any vendor extensions, we ignore it. */
21519 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21520 mac_ptr += bytes_read;
21521 read_direct_string (abfd, mac_ptr, &bytes_read);
21522 mac_ptr += bytes_read;
21523
21524 /* We don't recognize any vendor extensions. */
21525 break;
21526 }
21527 /* FALLTHROUGH */
21528
21529 default:
21530 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
21531 mac_ptr, mac_end, abfd, offset_size,
21532 section);
21533 if (mac_ptr == NULL)
21534 return;
21535 break;
21536 }
21537 } while (macinfo_type != 0);
21538 }
21539
21540 static void
21541 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
21542 int section_is_gnu)
21543 {
21544 struct objfile *objfile = dwarf2_per_objfile->objfile;
21545 struct line_header *lh = cu->line_header;
21546 bfd *abfd;
21547 const gdb_byte *mac_ptr, *mac_end;
21548 struct macro_source_file *current_file = 0;
21549 enum dwarf_macro_record_type macinfo_type;
21550 unsigned int offset_size = cu->header.offset_size;
21551 const gdb_byte *opcode_definitions[256];
21552 struct cleanup *cleanup;
21553 void **slot;
21554 struct dwarf2_section_info *section;
21555 const char *section_name;
21556
21557 if (cu->dwo_unit != NULL)
21558 {
21559 if (section_is_gnu)
21560 {
21561 section = &cu->dwo_unit->dwo_file->sections.macro;
21562 section_name = ".debug_macro.dwo";
21563 }
21564 else
21565 {
21566 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21567 section_name = ".debug_macinfo.dwo";
21568 }
21569 }
21570 else
21571 {
21572 if (section_is_gnu)
21573 {
21574 section = &dwarf2_per_objfile->macro;
21575 section_name = ".debug_macro";
21576 }
21577 else
21578 {
21579 section = &dwarf2_per_objfile->macinfo;
21580 section_name = ".debug_macinfo";
21581 }
21582 }
21583
21584 dwarf2_read_section (objfile, section);
21585 if (section->buffer == NULL)
21586 {
21587 complaint (&symfile_complaints, _("missing %s section"), section_name);
21588 return;
21589 }
21590 abfd = get_section_bfd_owner (section);
21591
21592 /* First pass: Find the name of the base filename.
21593 This filename is needed in order to process all macros whose definition
21594 (or undefinition) comes from the command line. These macros are defined
21595 before the first DW_MACINFO_start_file entry, and yet still need to be
21596 associated to the base file.
21597
21598 To determine the base file name, we scan the macro definitions until we
21599 reach the first DW_MACINFO_start_file entry. We then initialize
21600 CURRENT_FILE accordingly so that any macro definition found before the
21601 first DW_MACINFO_start_file can still be associated to the base file. */
21602
21603 mac_ptr = section->buffer + offset;
21604 mac_end = section->buffer + section->size;
21605
21606 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21607 &offset_size, section_is_gnu);
21608 if (mac_ptr == NULL)
21609 {
21610 /* We already issued a complaint. */
21611 return;
21612 }
21613
21614 do
21615 {
21616 /* Do we at least have room for a macinfo type byte? */
21617 if (mac_ptr >= mac_end)
21618 {
21619 /* Complaint is printed during the second pass as GDB will probably
21620 stop the first pass earlier upon finding
21621 DW_MACINFO_start_file. */
21622 break;
21623 }
21624
21625 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21626 mac_ptr++;
21627
21628 /* Note that we rely on the fact that the corresponding GNU and
21629 DWARF constants are the same. */
21630 switch (macinfo_type)
21631 {
21632 /* A zero macinfo type indicates the end of the macro
21633 information. */
21634 case 0:
21635 break;
21636
21637 case DW_MACRO_GNU_define:
21638 case DW_MACRO_GNU_undef:
21639 /* Only skip the data by MAC_PTR. */
21640 {
21641 unsigned int bytes_read;
21642
21643 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21644 mac_ptr += bytes_read;
21645 read_direct_string (abfd, mac_ptr, &bytes_read);
21646 mac_ptr += bytes_read;
21647 }
21648 break;
21649
21650 case DW_MACRO_GNU_start_file:
21651 {
21652 unsigned int bytes_read;
21653 int line, file;
21654
21655 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21656 mac_ptr += bytes_read;
21657 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21658 mac_ptr += bytes_read;
21659
21660 current_file = macro_start_file (file, line, current_file, lh);
21661 }
21662 break;
21663
21664 case DW_MACRO_GNU_end_file:
21665 /* No data to skip by MAC_PTR. */
21666 break;
21667
21668 case DW_MACRO_GNU_define_indirect:
21669 case DW_MACRO_GNU_undef_indirect:
21670 case DW_MACRO_GNU_define_indirect_alt:
21671 case DW_MACRO_GNU_undef_indirect_alt:
21672 {
21673 unsigned int bytes_read;
21674
21675 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21676 mac_ptr += bytes_read;
21677 mac_ptr += offset_size;
21678 }
21679 break;
21680
21681 case DW_MACRO_GNU_transparent_include:
21682 case DW_MACRO_GNU_transparent_include_alt:
21683 /* Note that, according to the spec, a transparent include
21684 chain cannot call DW_MACRO_GNU_start_file. So, we can just
21685 skip this opcode. */
21686 mac_ptr += offset_size;
21687 break;
21688
21689 case DW_MACINFO_vendor_ext:
21690 /* Only skip the data by MAC_PTR. */
21691 if (!section_is_gnu)
21692 {
21693 unsigned int bytes_read;
21694
21695 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21696 mac_ptr += bytes_read;
21697 read_direct_string (abfd, mac_ptr, &bytes_read);
21698 mac_ptr += bytes_read;
21699 }
21700 /* FALLTHROUGH */
21701
21702 default:
21703 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
21704 mac_ptr, mac_end, abfd, offset_size,
21705 section);
21706 if (mac_ptr == NULL)
21707 return;
21708 break;
21709 }
21710 } while (macinfo_type != 0 && current_file == NULL);
21711
21712 /* Second pass: Process all entries.
21713
21714 Use the AT_COMMAND_LINE flag to determine whether we are still processing
21715 command-line macro definitions/undefinitions. This flag is unset when we
21716 reach the first DW_MACINFO_start_file entry. */
21717
21718 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
21719 htab_eq_pointer,
21720 NULL, xcalloc, xfree));
21721 mac_ptr = section->buffer + offset;
21722 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
21723 *slot = (void *) mac_ptr;
21724 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
21725 current_file, lh, section,
21726 section_is_gnu, 0, offset_size,
21727 include_hash.get ());
21728 }
21729
21730 /* Check if the attribute's form is a DW_FORM_block*
21731 if so return true else false. */
21732
21733 static int
21734 attr_form_is_block (const struct attribute *attr)
21735 {
21736 return (attr == NULL ? 0 :
21737 attr->form == DW_FORM_block1
21738 || attr->form == DW_FORM_block2
21739 || attr->form == DW_FORM_block4
21740 || attr->form == DW_FORM_block
21741 || attr->form == DW_FORM_exprloc);
21742 }
21743
21744 /* Return non-zero if ATTR's value is a section offset --- classes
21745 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
21746 You may use DW_UNSND (attr) to retrieve such offsets.
21747
21748 Section 7.5.4, "Attribute Encodings", explains that no attribute
21749 may have a value that belongs to more than one of these classes; it
21750 would be ambiguous if we did, because we use the same forms for all
21751 of them. */
21752
21753 static int
21754 attr_form_is_section_offset (const struct attribute *attr)
21755 {
21756 return (attr->form == DW_FORM_data4
21757 || attr->form == DW_FORM_data8
21758 || attr->form == DW_FORM_sec_offset);
21759 }
21760
21761 /* Return non-zero if ATTR's value falls in the 'constant' class, or
21762 zero otherwise. When this function returns true, you can apply
21763 dwarf2_get_attr_constant_value to it.
21764
21765 However, note that for some attributes you must check
21766 attr_form_is_section_offset before using this test. DW_FORM_data4
21767 and DW_FORM_data8 are members of both the constant class, and of
21768 the classes that contain offsets into other debug sections
21769 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
21770 that, if an attribute's can be either a constant or one of the
21771 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
21772 taken as section offsets, not constants. */
21773
21774 static int
21775 attr_form_is_constant (const struct attribute *attr)
21776 {
21777 switch (attr->form)
21778 {
21779 case DW_FORM_sdata:
21780 case DW_FORM_udata:
21781 case DW_FORM_data1:
21782 case DW_FORM_data2:
21783 case DW_FORM_data4:
21784 case DW_FORM_data8:
21785 return 1;
21786 default:
21787 return 0;
21788 }
21789 }
21790
21791
21792 /* DW_ADDR is always stored already as sect_offset; despite for the forms
21793 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
21794
21795 static int
21796 attr_form_is_ref (const struct attribute *attr)
21797 {
21798 switch (attr->form)
21799 {
21800 case DW_FORM_ref_addr:
21801 case DW_FORM_ref1:
21802 case DW_FORM_ref2:
21803 case DW_FORM_ref4:
21804 case DW_FORM_ref8:
21805 case DW_FORM_ref_udata:
21806 case DW_FORM_GNU_ref_alt:
21807 return 1;
21808 default:
21809 return 0;
21810 }
21811 }
21812
21813 /* Return the .debug_loc section to use for CU.
21814 For DWO files use .debug_loc.dwo. */
21815
21816 static struct dwarf2_section_info *
21817 cu_debug_loc_section (struct dwarf2_cu *cu)
21818 {
21819 if (cu->dwo_unit)
21820 return &cu->dwo_unit->dwo_file->sections.loc;
21821 return &dwarf2_per_objfile->loc;
21822 }
21823
21824 /* A helper function that fills in a dwarf2_loclist_baton. */
21825
21826 static void
21827 fill_in_loclist_baton (struct dwarf2_cu *cu,
21828 struct dwarf2_loclist_baton *baton,
21829 const struct attribute *attr)
21830 {
21831 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21832
21833 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
21834
21835 baton->per_cu = cu->per_cu;
21836 gdb_assert (baton->per_cu);
21837 /* We don't know how long the location list is, but make sure we
21838 don't run off the edge of the section. */
21839 baton->size = section->size - DW_UNSND (attr);
21840 baton->data = section->buffer + DW_UNSND (attr);
21841 baton->base_address = cu->base_address;
21842 baton->from_dwo = cu->dwo_unit != NULL;
21843 }
21844
21845 static void
21846 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
21847 struct dwarf2_cu *cu, int is_block)
21848 {
21849 struct objfile *objfile = dwarf2_per_objfile->objfile;
21850 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21851
21852 if (attr_form_is_section_offset (attr)
21853 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21854 the section. If so, fall through to the complaint in the
21855 other branch. */
21856 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
21857 {
21858 struct dwarf2_loclist_baton *baton;
21859
21860 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
21861
21862 fill_in_loclist_baton (cu, baton, attr);
21863
21864 if (cu->base_known == 0)
21865 complaint (&symfile_complaints,
21866 _("Location list used without "
21867 "specifying the CU base address."));
21868
21869 SYMBOL_ACLASS_INDEX (sym) = (is_block
21870 ? dwarf2_loclist_block_index
21871 : dwarf2_loclist_index);
21872 SYMBOL_LOCATION_BATON (sym) = baton;
21873 }
21874 else
21875 {
21876 struct dwarf2_locexpr_baton *baton;
21877
21878 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
21879 baton->per_cu = cu->per_cu;
21880 gdb_assert (baton->per_cu);
21881
21882 if (attr_form_is_block (attr))
21883 {
21884 /* Note that we're just copying the block's data pointer
21885 here, not the actual data. We're still pointing into the
21886 info_buffer for SYM's objfile; right now we never release
21887 that buffer, but when we do clean up properly this may
21888 need to change. */
21889 baton->size = DW_BLOCK (attr)->size;
21890 baton->data = DW_BLOCK (attr)->data;
21891 }
21892 else
21893 {
21894 dwarf2_invalid_attrib_class_complaint ("location description",
21895 SYMBOL_NATURAL_NAME (sym));
21896 baton->size = 0;
21897 }
21898
21899 SYMBOL_ACLASS_INDEX (sym) = (is_block
21900 ? dwarf2_locexpr_block_index
21901 : dwarf2_locexpr_index);
21902 SYMBOL_LOCATION_BATON (sym) = baton;
21903 }
21904 }
21905
21906 /* Return the OBJFILE associated with the compilation unit CU. If CU
21907 came from a separate debuginfo file, then the master objfile is
21908 returned. */
21909
21910 struct objfile *
21911 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
21912 {
21913 struct objfile *objfile = per_cu->objfile;
21914
21915 /* Return the master objfile, so that we can report and look up the
21916 correct file containing this variable. */
21917 if (objfile->separate_debug_objfile_backlink)
21918 objfile = objfile->separate_debug_objfile_backlink;
21919
21920 return objfile;
21921 }
21922
21923 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
21924 (CU_HEADERP is unused in such case) or prepare a temporary copy at
21925 CU_HEADERP first. */
21926
21927 static const struct comp_unit_head *
21928 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
21929 struct dwarf2_per_cu_data *per_cu)
21930 {
21931 const gdb_byte *info_ptr;
21932
21933 if (per_cu->cu)
21934 return &per_cu->cu->header;
21935
21936 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
21937
21938 memset (cu_headerp, 0, sizeof (*cu_headerp));
21939 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
21940
21941 return cu_headerp;
21942 }
21943
21944 /* Return the address size given in the compilation unit header for CU. */
21945
21946 int
21947 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
21948 {
21949 struct comp_unit_head cu_header_local;
21950 const struct comp_unit_head *cu_headerp;
21951
21952 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
21953
21954 return cu_headerp->addr_size;
21955 }
21956
21957 /* Return the offset size given in the compilation unit header for CU. */
21958
21959 int
21960 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
21961 {
21962 struct comp_unit_head cu_header_local;
21963 const struct comp_unit_head *cu_headerp;
21964
21965 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
21966
21967 return cu_headerp->offset_size;
21968 }
21969
21970 /* See its dwarf2loc.h declaration. */
21971
21972 int
21973 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
21974 {
21975 struct comp_unit_head cu_header_local;
21976 const struct comp_unit_head *cu_headerp;
21977
21978 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
21979
21980 if (cu_headerp->version == 2)
21981 return cu_headerp->addr_size;
21982 else
21983 return cu_headerp->offset_size;
21984 }
21985
21986 /* Return the text offset of the CU. The returned offset comes from
21987 this CU's objfile. If this objfile came from a separate debuginfo
21988 file, then the offset may be different from the corresponding
21989 offset in the parent objfile. */
21990
21991 CORE_ADDR
21992 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
21993 {
21994 struct objfile *objfile = per_cu->objfile;
21995
21996 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21997 }
21998
21999 /* Locate the .debug_info compilation unit from CU's objfile which contains
22000 the DIE at OFFSET. Raises an error on failure. */
22001
22002 static struct dwarf2_per_cu_data *
22003 dwarf2_find_containing_comp_unit (sect_offset offset,
22004 unsigned int offset_in_dwz,
22005 struct objfile *objfile)
22006 {
22007 struct dwarf2_per_cu_data *this_cu;
22008 int low, high;
22009 const sect_offset *cu_off;
22010
22011 low = 0;
22012 high = dwarf2_per_objfile->n_comp_units - 1;
22013 while (high > low)
22014 {
22015 struct dwarf2_per_cu_data *mid_cu;
22016 int mid = low + (high - low) / 2;
22017
22018 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22019 cu_off = &mid_cu->offset;
22020 if (mid_cu->is_dwz > offset_in_dwz
22021 || (mid_cu->is_dwz == offset_in_dwz
22022 && cu_off->sect_off >= offset.sect_off))
22023 high = mid;
22024 else
22025 low = mid + 1;
22026 }
22027 gdb_assert (low == high);
22028 this_cu = dwarf2_per_objfile->all_comp_units[low];
22029 cu_off = &this_cu->offset;
22030 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
22031 {
22032 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22033 error (_("Dwarf Error: could not find partial DIE containing "
22034 "offset 0x%lx [in module %s]"),
22035 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
22036
22037 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22038 <= offset.sect_off);
22039 return dwarf2_per_objfile->all_comp_units[low-1];
22040 }
22041 else
22042 {
22043 this_cu = dwarf2_per_objfile->all_comp_units[low];
22044 if (low == dwarf2_per_objfile->n_comp_units - 1
22045 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22046 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22047 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
22048 return this_cu;
22049 }
22050 }
22051
22052 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22053
22054 static void
22055 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22056 {
22057 memset (cu, 0, sizeof (*cu));
22058 per_cu->cu = cu;
22059 cu->per_cu = per_cu;
22060 cu->objfile = per_cu->objfile;
22061 obstack_init (&cu->comp_unit_obstack);
22062 }
22063
22064 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22065
22066 static void
22067 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22068 enum language pretend_language)
22069 {
22070 struct attribute *attr;
22071
22072 /* Set the language we're debugging. */
22073 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22074 if (attr)
22075 set_cu_language (DW_UNSND (attr), cu);
22076 else
22077 {
22078 cu->language = pretend_language;
22079 cu->language_defn = language_def (cu->language);
22080 }
22081
22082 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22083 }
22084
22085 /* Release one cached compilation unit, CU. We unlink it from the tree
22086 of compilation units, but we don't remove it from the read_in_chain;
22087 the caller is responsible for that.
22088 NOTE: DATA is a void * because this function is also used as a
22089 cleanup routine. */
22090
22091 static void
22092 free_heap_comp_unit (void *data)
22093 {
22094 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22095
22096 gdb_assert (cu->per_cu != NULL);
22097 cu->per_cu->cu = NULL;
22098 cu->per_cu = NULL;
22099
22100 obstack_free (&cu->comp_unit_obstack, NULL);
22101
22102 xfree (cu);
22103 }
22104
22105 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22106 when we're finished with it. We can't free the pointer itself, but be
22107 sure to unlink it from the cache. Also release any associated storage. */
22108
22109 static void
22110 free_stack_comp_unit (void *data)
22111 {
22112 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22113
22114 gdb_assert (cu->per_cu != NULL);
22115 cu->per_cu->cu = NULL;
22116 cu->per_cu = NULL;
22117
22118 obstack_free (&cu->comp_unit_obstack, NULL);
22119 cu->partial_dies = NULL;
22120 }
22121
22122 /* Free all cached compilation units. */
22123
22124 static void
22125 free_cached_comp_units (void *data)
22126 {
22127 struct dwarf2_per_cu_data *per_cu, **last_chain;
22128
22129 per_cu = dwarf2_per_objfile->read_in_chain;
22130 last_chain = &dwarf2_per_objfile->read_in_chain;
22131 while (per_cu != NULL)
22132 {
22133 struct dwarf2_per_cu_data *next_cu;
22134
22135 next_cu = per_cu->cu->read_in_chain;
22136
22137 free_heap_comp_unit (per_cu->cu);
22138 *last_chain = next_cu;
22139
22140 per_cu = next_cu;
22141 }
22142 }
22143
22144 /* Increase the age counter on each cached compilation unit, and free
22145 any that are too old. */
22146
22147 static void
22148 age_cached_comp_units (void)
22149 {
22150 struct dwarf2_per_cu_data *per_cu, **last_chain;
22151
22152 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22153 per_cu = dwarf2_per_objfile->read_in_chain;
22154 while (per_cu != NULL)
22155 {
22156 per_cu->cu->last_used ++;
22157 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22158 dwarf2_mark (per_cu->cu);
22159 per_cu = per_cu->cu->read_in_chain;
22160 }
22161
22162 per_cu = dwarf2_per_objfile->read_in_chain;
22163 last_chain = &dwarf2_per_objfile->read_in_chain;
22164 while (per_cu != NULL)
22165 {
22166 struct dwarf2_per_cu_data *next_cu;
22167
22168 next_cu = per_cu->cu->read_in_chain;
22169
22170 if (!per_cu->cu->mark)
22171 {
22172 free_heap_comp_unit (per_cu->cu);
22173 *last_chain = next_cu;
22174 }
22175 else
22176 last_chain = &per_cu->cu->read_in_chain;
22177
22178 per_cu = next_cu;
22179 }
22180 }
22181
22182 /* Remove a single compilation unit from the cache. */
22183
22184 static void
22185 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22186 {
22187 struct dwarf2_per_cu_data *per_cu, **last_chain;
22188
22189 per_cu = dwarf2_per_objfile->read_in_chain;
22190 last_chain = &dwarf2_per_objfile->read_in_chain;
22191 while (per_cu != NULL)
22192 {
22193 struct dwarf2_per_cu_data *next_cu;
22194
22195 next_cu = per_cu->cu->read_in_chain;
22196
22197 if (per_cu == target_per_cu)
22198 {
22199 free_heap_comp_unit (per_cu->cu);
22200 per_cu->cu = NULL;
22201 *last_chain = next_cu;
22202 break;
22203 }
22204 else
22205 last_chain = &per_cu->cu->read_in_chain;
22206
22207 per_cu = next_cu;
22208 }
22209 }
22210
22211 /* Release all extra memory associated with OBJFILE. */
22212
22213 void
22214 dwarf2_free_objfile (struct objfile *objfile)
22215 {
22216 dwarf2_per_objfile
22217 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22218 dwarf2_objfile_data_key);
22219
22220 if (dwarf2_per_objfile == NULL)
22221 return;
22222
22223 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22224 free_cached_comp_units (NULL);
22225
22226 if (dwarf2_per_objfile->quick_file_names_table)
22227 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22228
22229 if (dwarf2_per_objfile->line_header_hash)
22230 htab_delete (dwarf2_per_objfile->line_header_hash);
22231
22232 /* Everything else should be on the objfile obstack. */
22233 }
22234
22235 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22236 We store these in a hash table separate from the DIEs, and preserve them
22237 when the DIEs are flushed out of cache.
22238
22239 The CU "per_cu" pointer is needed because offset alone is not enough to
22240 uniquely identify the type. A file may have multiple .debug_types sections,
22241 or the type may come from a DWO file. Furthermore, while it's more logical
22242 to use per_cu->section+offset, with Fission the section with the data is in
22243 the DWO file but we don't know that section at the point we need it.
22244 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22245 because we can enter the lookup routine, get_die_type_at_offset, from
22246 outside this file, and thus won't necessarily have PER_CU->cu.
22247 Fortunately, PER_CU is stable for the life of the objfile. */
22248
22249 struct dwarf2_per_cu_offset_and_type
22250 {
22251 const struct dwarf2_per_cu_data *per_cu;
22252 sect_offset offset;
22253 struct type *type;
22254 };
22255
22256 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22257
22258 static hashval_t
22259 per_cu_offset_and_type_hash (const void *item)
22260 {
22261 const struct dwarf2_per_cu_offset_and_type *ofs
22262 = (const struct dwarf2_per_cu_offset_and_type *) item;
22263
22264 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
22265 }
22266
22267 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22268
22269 static int
22270 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22271 {
22272 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22273 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22274 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22275 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22276
22277 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22278 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
22279 }
22280
22281 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22282 table if necessary. For convenience, return TYPE.
22283
22284 The DIEs reading must have careful ordering to:
22285 * Not cause infite loops trying to read in DIEs as a prerequisite for
22286 reading current DIE.
22287 * Not trying to dereference contents of still incompletely read in types
22288 while reading in other DIEs.
22289 * Enable referencing still incompletely read in types just by a pointer to
22290 the type without accessing its fields.
22291
22292 Therefore caller should follow these rules:
22293 * Try to fetch any prerequisite types we may need to build this DIE type
22294 before building the type and calling set_die_type.
22295 * After building type call set_die_type for current DIE as soon as
22296 possible before fetching more types to complete the current type.
22297 * Make the type as complete as possible before fetching more types. */
22298
22299 static struct type *
22300 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22301 {
22302 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22303 struct objfile *objfile = cu->objfile;
22304 struct attribute *attr;
22305 struct dynamic_prop prop;
22306
22307 /* For Ada types, make sure that the gnat-specific data is always
22308 initialized (if not already set). There are a few types where
22309 we should not be doing so, because the type-specific area is
22310 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22311 where the type-specific area is used to store the floatformat).
22312 But this is not a problem, because the gnat-specific information
22313 is actually not needed for these types. */
22314 if (need_gnat_info (cu)
22315 && TYPE_CODE (type) != TYPE_CODE_FUNC
22316 && TYPE_CODE (type) != TYPE_CODE_FLT
22317 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22318 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22319 && TYPE_CODE (type) != TYPE_CODE_METHOD
22320 && !HAVE_GNAT_AUX_INFO (type))
22321 INIT_GNAT_SPECIFIC (type);
22322
22323 /* Read DW_AT_allocated and set in type. */
22324 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22325 if (attr_form_is_block (attr))
22326 {
22327 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22328 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22329 }
22330 else if (attr != NULL)
22331 {
22332 complaint (&symfile_complaints,
22333 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22334 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22335 die->offset.sect_off);
22336 }
22337
22338 /* Read DW_AT_associated and set in type. */
22339 attr = dwarf2_attr (die, DW_AT_associated, cu);
22340 if (attr_form_is_block (attr))
22341 {
22342 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22343 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22344 }
22345 else if (attr != NULL)
22346 {
22347 complaint (&symfile_complaints,
22348 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22349 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22350 die->offset.sect_off);
22351 }
22352
22353 /* Read DW_AT_data_location and set in type. */
22354 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22355 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22356 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22357
22358 if (dwarf2_per_objfile->die_type_hash == NULL)
22359 {
22360 dwarf2_per_objfile->die_type_hash =
22361 htab_create_alloc_ex (127,
22362 per_cu_offset_and_type_hash,
22363 per_cu_offset_and_type_eq,
22364 NULL,
22365 &objfile->objfile_obstack,
22366 hashtab_obstack_allocate,
22367 dummy_obstack_deallocate);
22368 }
22369
22370 ofs.per_cu = cu->per_cu;
22371 ofs.offset = die->offset;
22372 ofs.type = type;
22373 slot = (struct dwarf2_per_cu_offset_and_type **)
22374 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
22375 if (*slot)
22376 complaint (&symfile_complaints,
22377 _("A problem internal to GDB: DIE 0x%x has type already set"),
22378 die->offset.sect_off);
22379 *slot = XOBNEW (&objfile->objfile_obstack,
22380 struct dwarf2_per_cu_offset_and_type);
22381 **slot = ofs;
22382 return type;
22383 }
22384
22385 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22386 or return NULL if the die does not have a saved type. */
22387
22388 static struct type *
22389 get_die_type_at_offset (sect_offset offset,
22390 struct dwarf2_per_cu_data *per_cu)
22391 {
22392 struct dwarf2_per_cu_offset_and_type *slot, ofs;
22393
22394 if (dwarf2_per_objfile->die_type_hash == NULL)
22395 return NULL;
22396
22397 ofs.per_cu = per_cu;
22398 ofs.offset = offset;
22399 slot = ((struct dwarf2_per_cu_offset_and_type *)
22400 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
22401 if (slot)
22402 return slot->type;
22403 else
22404 return NULL;
22405 }
22406
22407 /* Look up the type for DIE in CU in die_type_hash,
22408 or return NULL if DIE does not have a saved type. */
22409
22410 static struct type *
22411 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22412 {
22413 return get_die_type_at_offset (die->offset, cu->per_cu);
22414 }
22415
22416 /* Add a dependence relationship from CU to REF_PER_CU. */
22417
22418 static void
22419 dwarf2_add_dependence (struct dwarf2_cu *cu,
22420 struct dwarf2_per_cu_data *ref_per_cu)
22421 {
22422 void **slot;
22423
22424 if (cu->dependencies == NULL)
22425 cu->dependencies
22426 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
22427 NULL, &cu->comp_unit_obstack,
22428 hashtab_obstack_allocate,
22429 dummy_obstack_deallocate);
22430
22431 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
22432 if (*slot == NULL)
22433 *slot = ref_per_cu;
22434 }
22435
22436 /* Subroutine of dwarf2_mark to pass to htab_traverse.
22437 Set the mark field in every compilation unit in the
22438 cache that we must keep because we are keeping CU. */
22439
22440 static int
22441 dwarf2_mark_helper (void **slot, void *data)
22442 {
22443 struct dwarf2_per_cu_data *per_cu;
22444
22445 per_cu = (struct dwarf2_per_cu_data *) *slot;
22446
22447 /* cu->dependencies references may not yet have been ever read if QUIT aborts
22448 reading of the chain. As such dependencies remain valid it is not much
22449 useful to track and undo them during QUIT cleanups. */
22450 if (per_cu->cu == NULL)
22451 return 1;
22452
22453 if (per_cu->cu->mark)
22454 return 1;
22455 per_cu->cu->mark = 1;
22456
22457 if (per_cu->cu->dependencies != NULL)
22458 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
22459
22460 return 1;
22461 }
22462
22463 /* Set the mark field in CU and in every other compilation unit in the
22464 cache that we must keep because we are keeping CU. */
22465
22466 static void
22467 dwarf2_mark (struct dwarf2_cu *cu)
22468 {
22469 if (cu->mark)
22470 return;
22471 cu->mark = 1;
22472 if (cu->dependencies != NULL)
22473 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
22474 }
22475
22476 static void
22477 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
22478 {
22479 while (per_cu)
22480 {
22481 per_cu->cu->mark = 0;
22482 per_cu = per_cu->cu->read_in_chain;
22483 }
22484 }
22485
22486 /* Trivial hash function for partial_die_info: the hash value of a DIE
22487 is its offset in .debug_info for this objfile. */
22488
22489 static hashval_t
22490 partial_die_hash (const void *item)
22491 {
22492 const struct partial_die_info *part_die
22493 = (const struct partial_die_info *) item;
22494
22495 return part_die->offset.sect_off;
22496 }
22497
22498 /* Trivial comparison function for partial_die_info structures: two DIEs
22499 are equal if they have the same offset. */
22500
22501 static int
22502 partial_die_eq (const void *item_lhs, const void *item_rhs)
22503 {
22504 const struct partial_die_info *part_die_lhs
22505 = (const struct partial_die_info *) item_lhs;
22506 const struct partial_die_info *part_die_rhs
22507 = (const struct partial_die_info *) item_rhs;
22508
22509 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
22510 }
22511
22512 static struct cmd_list_element *set_dwarf_cmdlist;
22513 static struct cmd_list_element *show_dwarf_cmdlist;
22514
22515 static void
22516 set_dwarf_cmd (char *args, int from_tty)
22517 {
22518 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
22519 gdb_stdout);
22520 }
22521
22522 static void
22523 show_dwarf_cmd (char *args, int from_tty)
22524 {
22525 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
22526 }
22527
22528 /* Free data associated with OBJFILE, if necessary. */
22529
22530 static void
22531 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
22532 {
22533 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
22534 int ix;
22535
22536 /* Make sure we don't accidentally use dwarf2_per_objfile while
22537 cleaning up. */
22538 dwarf2_per_objfile = NULL;
22539
22540 for (ix = 0; ix < data->n_comp_units; ++ix)
22541 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
22542
22543 for (ix = 0; ix < data->n_type_units; ++ix)
22544 VEC_free (dwarf2_per_cu_ptr,
22545 data->all_type_units[ix]->per_cu.imported_symtabs);
22546 xfree (data->all_type_units);
22547
22548 VEC_free (dwarf2_section_info_def, data->types);
22549
22550 if (data->dwo_files)
22551 free_dwo_files (data->dwo_files, objfile);
22552 if (data->dwp_file)
22553 gdb_bfd_unref (data->dwp_file->dbfd);
22554
22555 if (data->dwz_file && data->dwz_file->dwz_bfd)
22556 gdb_bfd_unref (data->dwz_file->dwz_bfd);
22557 }
22558
22559 \f
22560 /* The "save gdb-index" command. */
22561
22562 /* The contents of the hash table we create when building the string
22563 table. */
22564 struct strtab_entry
22565 {
22566 offset_type offset;
22567 const char *str;
22568 };
22569
22570 /* Hash function for a strtab_entry.
22571
22572 Function is used only during write_hash_table so no index format backward
22573 compatibility is needed. */
22574
22575 static hashval_t
22576 hash_strtab_entry (const void *e)
22577 {
22578 const struct strtab_entry *entry = (const struct strtab_entry *) e;
22579 return mapped_index_string_hash (INT_MAX, entry->str);
22580 }
22581
22582 /* Equality function for a strtab_entry. */
22583
22584 static int
22585 eq_strtab_entry (const void *a, const void *b)
22586 {
22587 const struct strtab_entry *ea = (const struct strtab_entry *) a;
22588 const struct strtab_entry *eb = (const struct strtab_entry *) b;
22589 return !strcmp (ea->str, eb->str);
22590 }
22591
22592 /* Create a strtab_entry hash table. */
22593
22594 static htab_t
22595 create_strtab (void)
22596 {
22597 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
22598 xfree, xcalloc, xfree);
22599 }
22600
22601 /* Add a string to the constant pool. Return the string's offset in
22602 host order. */
22603
22604 static offset_type
22605 add_string (htab_t table, struct obstack *cpool, const char *str)
22606 {
22607 void **slot;
22608 struct strtab_entry entry;
22609 struct strtab_entry *result;
22610
22611 entry.str = str;
22612 slot = htab_find_slot (table, &entry, INSERT);
22613 if (*slot)
22614 result = (struct strtab_entry *) *slot;
22615 else
22616 {
22617 result = XNEW (struct strtab_entry);
22618 result->offset = obstack_object_size (cpool);
22619 result->str = str;
22620 obstack_grow_str0 (cpool, str);
22621 *slot = result;
22622 }
22623 return result->offset;
22624 }
22625
22626 /* An entry in the symbol table. */
22627 struct symtab_index_entry
22628 {
22629 /* The name of the symbol. */
22630 const char *name;
22631 /* The offset of the name in the constant pool. */
22632 offset_type index_offset;
22633 /* A sorted vector of the indices of all the CUs that hold an object
22634 of this name. */
22635 VEC (offset_type) *cu_indices;
22636 };
22637
22638 /* The symbol table. This is a power-of-2-sized hash table. */
22639 struct mapped_symtab
22640 {
22641 offset_type n_elements;
22642 offset_type size;
22643 struct symtab_index_entry **data;
22644 };
22645
22646 /* Hash function for a symtab_index_entry. */
22647
22648 static hashval_t
22649 hash_symtab_entry (const void *e)
22650 {
22651 const struct symtab_index_entry *entry
22652 = (const struct symtab_index_entry *) e;
22653 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
22654 sizeof (offset_type) * VEC_length (offset_type,
22655 entry->cu_indices),
22656 0);
22657 }
22658
22659 /* Equality function for a symtab_index_entry. */
22660
22661 static int
22662 eq_symtab_entry (const void *a, const void *b)
22663 {
22664 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
22665 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
22666 int len = VEC_length (offset_type, ea->cu_indices);
22667 if (len != VEC_length (offset_type, eb->cu_indices))
22668 return 0;
22669 return !memcmp (VEC_address (offset_type, ea->cu_indices),
22670 VEC_address (offset_type, eb->cu_indices),
22671 sizeof (offset_type) * len);
22672 }
22673
22674 /* Destroy a symtab_index_entry. */
22675
22676 static void
22677 delete_symtab_entry (void *p)
22678 {
22679 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
22680 VEC_free (offset_type, entry->cu_indices);
22681 xfree (entry);
22682 }
22683
22684 /* Create a hash table holding symtab_index_entry objects. */
22685
22686 static htab_t
22687 create_symbol_hash_table (void)
22688 {
22689 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
22690 delete_symtab_entry, xcalloc, xfree);
22691 }
22692
22693 /* Create a new mapped symtab object. */
22694
22695 static struct mapped_symtab *
22696 create_mapped_symtab (void)
22697 {
22698 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
22699 symtab->n_elements = 0;
22700 symtab->size = 1024;
22701 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
22702 return symtab;
22703 }
22704
22705 /* Destroy a mapped_symtab. */
22706
22707 static void
22708 cleanup_mapped_symtab (void *p)
22709 {
22710 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
22711 /* The contents of the array are freed when the other hash table is
22712 destroyed. */
22713 xfree (symtab->data);
22714 xfree (symtab);
22715 }
22716
22717 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
22718 the slot.
22719
22720 Function is used only during write_hash_table so no index format backward
22721 compatibility is needed. */
22722
22723 static struct symtab_index_entry **
22724 find_slot (struct mapped_symtab *symtab, const char *name)
22725 {
22726 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
22727
22728 index = hash & (symtab->size - 1);
22729 step = ((hash * 17) & (symtab->size - 1)) | 1;
22730
22731 for (;;)
22732 {
22733 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
22734 return &symtab->data[index];
22735 index = (index + step) & (symtab->size - 1);
22736 }
22737 }
22738
22739 /* Expand SYMTAB's hash table. */
22740
22741 static void
22742 hash_expand (struct mapped_symtab *symtab)
22743 {
22744 offset_type old_size = symtab->size;
22745 offset_type i;
22746 struct symtab_index_entry **old_entries = symtab->data;
22747
22748 symtab->size *= 2;
22749 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
22750
22751 for (i = 0; i < old_size; ++i)
22752 {
22753 if (old_entries[i])
22754 {
22755 struct symtab_index_entry **slot = find_slot (symtab,
22756 old_entries[i]->name);
22757 *slot = old_entries[i];
22758 }
22759 }
22760
22761 xfree (old_entries);
22762 }
22763
22764 /* Add an entry to SYMTAB. NAME is the name of the symbol.
22765 CU_INDEX is the index of the CU in which the symbol appears.
22766 IS_STATIC is one if the symbol is static, otherwise zero (global). */
22767
22768 static void
22769 add_index_entry (struct mapped_symtab *symtab, const char *name,
22770 int is_static, gdb_index_symbol_kind kind,
22771 offset_type cu_index)
22772 {
22773 struct symtab_index_entry **slot;
22774 offset_type cu_index_and_attrs;
22775
22776 ++symtab->n_elements;
22777 if (4 * symtab->n_elements / 3 >= symtab->size)
22778 hash_expand (symtab);
22779
22780 slot = find_slot (symtab, name);
22781 if (!*slot)
22782 {
22783 *slot = XNEW (struct symtab_index_entry);
22784 (*slot)->name = name;
22785 /* index_offset is set later. */
22786 (*slot)->cu_indices = NULL;
22787 }
22788
22789 cu_index_and_attrs = 0;
22790 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
22791 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
22792 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
22793
22794 /* We don't want to record an index value twice as we want to avoid the
22795 duplication.
22796 We process all global symbols and then all static symbols
22797 (which would allow us to avoid the duplication by only having to check
22798 the last entry pushed), but a symbol could have multiple kinds in one CU.
22799 To keep things simple we don't worry about the duplication here and
22800 sort and uniqufy the list after we've processed all symbols. */
22801 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
22802 }
22803
22804 /* qsort helper routine for uniquify_cu_indices. */
22805
22806 static int
22807 offset_type_compare (const void *ap, const void *bp)
22808 {
22809 offset_type a = *(offset_type *) ap;
22810 offset_type b = *(offset_type *) bp;
22811
22812 return (a > b) - (b > a);
22813 }
22814
22815 /* Sort and remove duplicates of all symbols' cu_indices lists. */
22816
22817 static void
22818 uniquify_cu_indices (struct mapped_symtab *symtab)
22819 {
22820 int i;
22821
22822 for (i = 0; i < symtab->size; ++i)
22823 {
22824 struct symtab_index_entry *entry = symtab->data[i];
22825
22826 if (entry
22827 && entry->cu_indices != NULL)
22828 {
22829 unsigned int next_to_insert, next_to_check;
22830 offset_type last_value;
22831
22832 qsort (VEC_address (offset_type, entry->cu_indices),
22833 VEC_length (offset_type, entry->cu_indices),
22834 sizeof (offset_type), offset_type_compare);
22835
22836 last_value = VEC_index (offset_type, entry->cu_indices, 0);
22837 next_to_insert = 1;
22838 for (next_to_check = 1;
22839 next_to_check < VEC_length (offset_type, entry->cu_indices);
22840 ++next_to_check)
22841 {
22842 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
22843 != last_value)
22844 {
22845 last_value = VEC_index (offset_type, entry->cu_indices,
22846 next_to_check);
22847 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
22848 last_value);
22849 ++next_to_insert;
22850 }
22851 }
22852 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
22853 }
22854 }
22855 }
22856
22857 /* Add a vector of indices to the constant pool. */
22858
22859 static offset_type
22860 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
22861 struct symtab_index_entry *entry)
22862 {
22863 void **slot;
22864
22865 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
22866 if (!*slot)
22867 {
22868 offset_type len = VEC_length (offset_type, entry->cu_indices);
22869 offset_type val = MAYBE_SWAP (len);
22870 offset_type iter;
22871 int i;
22872
22873 *slot = entry;
22874 entry->index_offset = obstack_object_size (cpool);
22875
22876 obstack_grow (cpool, &val, sizeof (val));
22877 for (i = 0;
22878 VEC_iterate (offset_type, entry->cu_indices, i, iter);
22879 ++i)
22880 {
22881 val = MAYBE_SWAP (iter);
22882 obstack_grow (cpool, &val, sizeof (val));
22883 }
22884 }
22885 else
22886 {
22887 struct symtab_index_entry *old_entry
22888 = (struct symtab_index_entry *) *slot;
22889 entry->index_offset = old_entry->index_offset;
22890 entry = old_entry;
22891 }
22892 return entry->index_offset;
22893 }
22894
22895 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
22896 constant pool entries going into the obstack CPOOL. */
22897
22898 static void
22899 write_hash_table (struct mapped_symtab *symtab,
22900 struct obstack *output, struct obstack *cpool)
22901 {
22902 offset_type i;
22903 htab_t symbol_hash_table;
22904 htab_t str_table;
22905
22906 symbol_hash_table = create_symbol_hash_table ();
22907 str_table = create_strtab ();
22908
22909 /* We add all the index vectors to the constant pool first, to
22910 ensure alignment is ok. */
22911 for (i = 0; i < symtab->size; ++i)
22912 {
22913 if (symtab->data[i])
22914 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
22915 }
22916
22917 /* Now write out the hash table. */
22918 for (i = 0; i < symtab->size; ++i)
22919 {
22920 offset_type str_off, vec_off;
22921
22922 if (symtab->data[i])
22923 {
22924 str_off = add_string (str_table, cpool, symtab->data[i]->name);
22925 vec_off = symtab->data[i]->index_offset;
22926 }
22927 else
22928 {
22929 /* While 0 is a valid constant pool index, it is not valid
22930 to have 0 for both offsets. */
22931 str_off = 0;
22932 vec_off = 0;
22933 }
22934
22935 str_off = MAYBE_SWAP (str_off);
22936 vec_off = MAYBE_SWAP (vec_off);
22937
22938 obstack_grow (output, &str_off, sizeof (str_off));
22939 obstack_grow (output, &vec_off, sizeof (vec_off));
22940 }
22941
22942 htab_delete (str_table);
22943 htab_delete (symbol_hash_table);
22944 }
22945
22946 /* Struct to map psymtab to CU index in the index file. */
22947 struct psymtab_cu_index_map
22948 {
22949 struct partial_symtab *psymtab;
22950 unsigned int cu_index;
22951 };
22952
22953 static hashval_t
22954 hash_psymtab_cu_index (const void *item)
22955 {
22956 const struct psymtab_cu_index_map *map
22957 = (const struct psymtab_cu_index_map *) item;
22958
22959 return htab_hash_pointer (map->psymtab);
22960 }
22961
22962 static int
22963 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
22964 {
22965 const struct psymtab_cu_index_map *lhs
22966 = (const struct psymtab_cu_index_map *) item_lhs;
22967 const struct psymtab_cu_index_map *rhs
22968 = (const struct psymtab_cu_index_map *) item_rhs;
22969
22970 return lhs->psymtab == rhs->psymtab;
22971 }
22972
22973 /* Helper struct for building the address table. */
22974 struct addrmap_index_data
22975 {
22976 struct objfile *objfile;
22977 struct obstack *addr_obstack;
22978 htab_t cu_index_htab;
22979
22980 /* Non-zero if the previous_* fields are valid.
22981 We can't write an entry until we see the next entry (since it is only then
22982 that we know the end of the entry). */
22983 int previous_valid;
22984 /* Index of the CU in the table of all CUs in the index file. */
22985 unsigned int previous_cu_index;
22986 /* Start address of the CU. */
22987 CORE_ADDR previous_cu_start;
22988 };
22989
22990 /* Write an address entry to OBSTACK. */
22991
22992 static void
22993 add_address_entry (struct objfile *objfile, struct obstack *obstack,
22994 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
22995 {
22996 offset_type cu_index_to_write;
22997 gdb_byte addr[8];
22998 CORE_ADDR baseaddr;
22999
23000 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23001
23002 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23003 obstack_grow (obstack, addr, 8);
23004 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23005 obstack_grow (obstack, addr, 8);
23006 cu_index_to_write = MAYBE_SWAP (cu_index);
23007 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23008 }
23009
23010 /* Worker function for traversing an addrmap to build the address table. */
23011
23012 static int
23013 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23014 {
23015 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23016 struct partial_symtab *pst = (struct partial_symtab *) obj;
23017
23018 if (data->previous_valid)
23019 add_address_entry (data->objfile, data->addr_obstack,
23020 data->previous_cu_start, start_addr,
23021 data->previous_cu_index);
23022
23023 data->previous_cu_start = start_addr;
23024 if (pst != NULL)
23025 {
23026 struct psymtab_cu_index_map find_map, *map;
23027 find_map.psymtab = pst;
23028 map = ((struct psymtab_cu_index_map *)
23029 htab_find (data->cu_index_htab, &find_map));
23030 gdb_assert (map != NULL);
23031 data->previous_cu_index = map->cu_index;
23032 data->previous_valid = 1;
23033 }
23034 else
23035 data->previous_valid = 0;
23036
23037 return 0;
23038 }
23039
23040 /* Write OBJFILE's address map to OBSTACK.
23041 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23042 in the index file. */
23043
23044 static void
23045 write_address_map (struct objfile *objfile, struct obstack *obstack,
23046 htab_t cu_index_htab)
23047 {
23048 struct addrmap_index_data addrmap_index_data;
23049
23050 /* When writing the address table, we have to cope with the fact that
23051 the addrmap iterator only provides the start of a region; we have to
23052 wait until the next invocation to get the start of the next region. */
23053
23054 addrmap_index_data.objfile = objfile;
23055 addrmap_index_data.addr_obstack = obstack;
23056 addrmap_index_data.cu_index_htab = cu_index_htab;
23057 addrmap_index_data.previous_valid = 0;
23058
23059 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23060 &addrmap_index_data);
23061
23062 /* It's highly unlikely the last entry (end address = 0xff...ff)
23063 is valid, but we should still handle it.
23064 The end address is recorded as the start of the next region, but that
23065 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23066 anyway. */
23067 if (addrmap_index_data.previous_valid)
23068 add_address_entry (objfile, obstack,
23069 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23070 addrmap_index_data.previous_cu_index);
23071 }
23072
23073 /* Return the symbol kind of PSYM. */
23074
23075 static gdb_index_symbol_kind
23076 symbol_kind (struct partial_symbol *psym)
23077 {
23078 domain_enum domain = PSYMBOL_DOMAIN (psym);
23079 enum address_class aclass = PSYMBOL_CLASS (psym);
23080
23081 switch (domain)
23082 {
23083 case VAR_DOMAIN:
23084 switch (aclass)
23085 {
23086 case LOC_BLOCK:
23087 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23088 case LOC_TYPEDEF:
23089 return GDB_INDEX_SYMBOL_KIND_TYPE;
23090 case LOC_COMPUTED:
23091 case LOC_CONST_BYTES:
23092 case LOC_OPTIMIZED_OUT:
23093 case LOC_STATIC:
23094 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23095 case LOC_CONST:
23096 /* Note: It's currently impossible to recognize psyms as enum values
23097 short of reading the type info. For now punt. */
23098 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23099 default:
23100 /* There are other LOC_FOO values that one might want to classify
23101 as variables, but dwarf2read.c doesn't currently use them. */
23102 return GDB_INDEX_SYMBOL_KIND_OTHER;
23103 }
23104 case STRUCT_DOMAIN:
23105 return GDB_INDEX_SYMBOL_KIND_TYPE;
23106 default:
23107 return GDB_INDEX_SYMBOL_KIND_OTHER;
23108 }
23109 }
23110
23111 /* Add a list of partial symbols to SYMTAB. */
23112
23113 static void
23114 write_psymbols (struct mapped_symtab *symtab,
23115 htab_t psyms_seen,
23116 struct partial_symbol **psymp,
23117 int count,
23118 offset_type cu_index,
23119 int is_static)
23120 {
23121 for (; count-- > 0; ++psymp)
23122 {
23123 struct partial_symbol *psym = *psymp;
23124 void **slot;
23125
23126 if (SYMBOL_LANGUAGE (psym) == language_ada)
23127 error (_("Ada is not currently supported by the index"));
23128
23129 /* Only add a given psymbol once. */
23130 slot = htab_find_slot (psyms_seen, psym, INSERT);
23131 if (!*slot)
23132 {
23133 gdb_index_symbol_kind kind = symbol_kind (psym);
23134
23135 *slot = psym;
23136 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23137 is_static, kind, cu_index);
23138 }
23139 }
23140 }
23141
23142 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
23143 exception if there is an error. */
23144
23145 static void
23146 write_obstack (FILE *file, struct obstack *obstack)
23147 {
23148 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23149 file)
23150 != obstack_object_size (obstack))
23151 error (_("couldn't data write to file"));
23152 }
23153
23154 /* A helper struct used when iterating over debug_types. */
23155 struct signatured_type_index_data
23156 {
23157 struct objfile *objfile;
23158 struct mapped_symtab *symtab;
23159 struct obstack *types_list;
23160 htab_t psyms_seen;
23161 int cu_index;
23162 };
23163
23164 /* A helper function that writes a single signatured_type to an
23165 obstack. */
23166
23167 static int
23168 write_one_signatured_type (void **slot, void *d)
23169 {
23170 struct signatured_type_index_data *info
23171 = (struct signatured_type_index_data *) d;
23172 struct signatured_type *entry = (struct signatured_type *) *slot;
23173 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23174 gdb_byte val[8];
23175
23176 write_psymbols (info->symtab,
23177 info->psyms_seen,
23178 info->objfile->global_psymbols.list
23179 + psymtab->globals_offset,
23180 psymtab->n_global_syms, info->cu_index,
23181 0);
23182 write_psymbols (info->symtab,
23183 info->psyms_seen,
23184 info->objfile->static_psymbols.list
23185 + psymtab->statics_offset,
23186 psymtab->n_static_syms, info->cu_index,
23187 1);
23188
23189 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23190 entry->per_cu.offset.sect_off);
23191 obstack_grow (info->types_list, val, 8);
23192 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23193 entry->type_offset_in_tu.cu_off);
23194 obstack_grow (info->types_list, val, 8);
23195 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23196 obstack_grow (info->types_list, val, 8);
23197
23198 ++info->cu_index;
23199
23200 return 1;
23201 }
23202
23203 /* Recurse into all "included" dependencies and write their symbols as
23204 if they appeared in this psymtab. */
23205
23206 static void
23207 recursively_write_psymbols (struct objfile *objfile,
23208 struct partial_symtab *psymtab,
23209 struct mapped_symtab *symtab,
23210 htab_t psyms_seen,
23211 offset_type cu_index)
23212 {
23213 int i;
23214
23215 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23216 if (psymtab->dependencies[i]->user != NULL)
23217 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23218 symtab, psyms_seen, cu_index);
23219
23220 write_psymbols (symtab,
23221 psyms_seen,
23222 objfile->global_psymbols.list + psymtab->globals_offset,
23223 psymtab->n_global_syms, cu_index,
23224 0);
23225 write_psymbols (symtab,
23226 psyms_seen,
23227 objfile->static_psymbols.list + psymtab->statics_offset,
23228 psymtab->n_static_syms, cu_index,
23229 1);
23230 }
23231
23232 /* Create an index file for OBJFILE in the directory DIR. */
23233
23234 static void
23235 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23236 {
23237 struct cleanup *cleanup;
23238 char *filename;
23239 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23240 struct obstack cu_list, types_cu_list;
23241 int i;
23242 FILE *out_file;
23243 struct mapped_symtab *symtab;
23244 offset_type val, size_of_contents, total_len;
23245 struct stat st;
23246 struct psymtab_cu_index_map *psymtab_cu_index_map;
23247
23248 if (dwarf2_per_objfile->using_index)
23249 error (_("Cannot use an index to create the index"));
23250
23251 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23252 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23253
23254 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23255 return;
23256
23257 if (stat (objfile_name (objfile), &st) < 0)
23258 perror_with_name (objfile_name (objfile));
23259
23260 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
23261 INDEX_SUFFIX, (char *) NULL);
23262 cleanup = make_cleanup (xfree, filename);
23263
23264 out_file = gdb_fopen_cloexec (filename, "wb");
23265 if (!out_file)
23266 error (_("Can't open `%s' for writing"), filename);
23267
23268 gdb::unlinker unlink_file (filename);
23269
23270 symtab = create_mapped_symtab ();
23271 make_cleanup (cleanup_mapped_symtab, symtab);
23272
23273 obstack_init (&addr_obstack);
23274 make_cleanup_obstack_free (&addr_obstack);
23275
23276 obstack_init (&cu_list);
23277 make_cleanup_obstack_free (&cu_list);
23278
23279 obstack_init (&types_cu_list);
23280 make_cleanup_obstack_free (&types_cu_list);
23281
23282 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23283 htab_eq_pointer,
23284 NULL, xcalloc, xfree));
23285
23286 /* While we're scanning CU's create a table that maps a psymtab pointer
23287 (which is what addrmap records) to its index (which is what is recorded
23288 in the index file). This will later be needed to write the address
23289 table. */
23290 htab_up cu_index_htab (htab_create_alloc (100,
23291 hash_psymtab_cu_index,
23292 eq_psymtab_cu_index,
23293 NULL, xcalloc, xfree));
23294 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23295 dwarf2_per_objfile->n_comp_units);
23296 make_cleanup (xfree, psymtab_cu_index_map);
23297
23298 /* The CU list is already sorted, so we don't need to do additional
23299 work here. Also, the debug_types entries do not appear in
23300 all_comp_units, but only in their own hash table. */
23301 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23302 {
23303 struct dwarf2_per_cu_data *per_cu
23304 = dwarf2_per_objfile->all_comp_units[i];
23305 struct partial_symtab *psymtab = per_cu->v.psymtab;
23306 gdb_byte val[8];
23307 struct psymtab_cu_index_map *map;
23308 void **slot;
23309
23310 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23311 It may be referenced from a local scope but in such case it does not
23312 need to be present in .gdb_index. */
23313 if (psymtab == NULL)
23314 continue;
23315
23316 if (psymtab->user == NULL)
23317 recursively_write_psymbols (objfile, psymtab, symtab,
23318 psyms_seen.get (), i);
23319
23320 map = &psymtab_cu_index_map[i];
23321 map->psymtab = psymtab;
23322 map->cu_index = i;
23323 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
23324 gdb_assert (slot != NULL);
23325 gdb_assert (*slot == NULL);
23326 *slot = map;
23327
23328 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23329 per_cu->offset.sect_off);
23330 obstack_grow (&cu_list, val, 8);
23331 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
23332 obstack_grow (&cu_list, val, 8);
23333 }
23334
23335 /* Dump the address map. */
23336 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
23337
23338 /* Write out the .debug_type entries, if any. */
23339 if (dwarf2_per_objfile->signatured_types)
23340 {
23341 struct signatured_type_index_data sig_data;
23342
23343 sig_data.objfile = objfile;
23344 sig_data.symtab = symtab;
23345 sig_data.types_list = &types_cu_list;
23346 sig_data.psyms_seen = psyms_seen.get ();
23347 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23348 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23349 write_one_signatured_type, &sig_data);
23350 }
23351
23352 /* Now that we've processed all symbols we can shrink their cu_indices
23353 lists. */
23354 uniquify_cu_indices (symtab);
23355
23356 obstack_init (&constant_pool);
23357 make_cleanup_obstack_free (&constant_pool);
23358 obstack_init (&symtab_obstack);
23359 make_cleanup_obstack_free (&symtab_obstack);
23360 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23361
23362 obstack_init (&contents);
23363 make_cleanup_obstack_free (&contents);
23364 size_of_contents = 6 * sizeof (offset_type);
23365 total_len = size_of_contents;
23366
23367 /* The version number. */
23368 val = MAYBE_SWAP (8);
23369 obstack_grow (&contents, &val, sizeof (val));
23370
23371 /* The offset of the CU list from the start of the file. */
23372 val = MAYBE_SWAP (total_len);
23373 obstack_grow (&contents, &val, sizeof (val));
23374 total_len += obstack_object_size (&cu_list);
23375
23376 /* The offset of the types CU list from the start of the file. */
23377 val = MAYBE_SWAP (total_len);
23378 obstack_grow (&contents, &val, sizeof (val));
23379 total_len += obstack_object_size (&types_cu_list);
23380
23381 /* The offset of the address table from the start of the file. */
23382 val = MAYBE_SWAP (total_len);
23383 obstack_grow (&contents, &val, sizeof (val));
23384 total_len += obstack_object_size (&addr_obstack);
23385
23386 /* The offset of the symbol table from the start of the file. */
23387 val = MAYBE_SWAP (total_len);
23388 obstack_grow (&contents, &val, sizeof (val));
23389 total_len += obstack_object_size (&symtab_obstack);
23390
23391 /* The offset of the constant pool from the start of the file. */
23392 val = MAYBE_SWAP (total_len);
23393 obstack_grow (&contents, &val, sizeof (val));
23394 total_len += obstack_object_size (&constant_pool);
23395
23396 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23397
23398 write_obstack (out_file, &contents);
23399 write_obstack (out_file, &cu_list);
23400 write_obstack (out_file, &types_cu_list);
23401 write_obstack (out_file, &addr_obstack);
23402 write_obstack (out_file, &symtab_obstack);
23403 write_obstack (out_file, &constant_pool);
23404
23405 fclose (out_file);
23406
23407 /* We want to keep the file. */
23408 unlink_file.keep ();
23409
23410 do_cleanups (cleanup);
23411 }
23412
23413 /* Implementation of the `save gdb-index' command.
23414
23415 Note that the file format used by this command is documented in the
23416 GDB manual. Any changes here must be documented there. */
23417
23418 static void
23419 save_gdb_index_command (char *arg, int from_tty)
23420 {
23421 struct objfile *objfile;
23422
23423 if (!arg || !*arg)
23424 error (_("usage: save gdb-index DIRECTORY"));
23425
23426 ALL_OBJFILES (objfile)
23427 {
23428 struct stat st;
23429
23430 /* If the objfile does not correspond to an actual file, skip it. */
23431 if (stat (objfile_name (objfile), &st) < 0)
23432 continue;
23433
23434 dwarf2_per_objfile
23435 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23436 dwarf2_objfile_data_key);
23437 if (dwarf2_per_objfile)
23438 {
23439
23440 TRY
23441 {
23442 write_psymtabs_to_index (objfile, arg);
23443 }
23444 CATCH (except, RETURN_MASK_ERROR)
23445 {
23446 exception_fprintf (gdb_stderr, except,
23447 _("Error while writing index for `%s': "),
23448 objfile_name (objfile));
23449 }
23450 END_CATCH
23451 }
23452 }
23453 }
23454
23455 \f
23456
23457 int dwarf_always_disassemble;
23458
23459 static void
23460 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23461 struct cmd_list_element *c, const char *value)
23462 {
23463 fprintf_filtered (file,
23464 _("Whether to always disassemble "
23465 "DWARF expressions is %s.\n"),
23466 value);
23467 }
23468
23469 static void
23470 show_check_physname (struct ui_file *file, int from_tty,
23471 struct cmd_list_element *c, const char *value)
23472 {
23473 fprintf_filtered (file,
23474 _("Whether to check \"physname\" is %s.\n"),
23475 value);
23476 }
23477
23478 void _initialize_dwarf2_read (void);
23479
23480 void
23481 _initialize_dwarf2_read (void)
23482 {
23483 struct cmd_list_element *c;
23484
23485 dwarf2_objfile_data_key
23486 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
23487
23488 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23489 Set DWARF specific variables.\n\
23490 Configure DWARF variables such as the cache size"),
23491 &set_dwarf_cmdlist, "maintenance set dwarf ",
23492 0/*allow-unknown*/, &maintenance_set_cmdlist);
23493
23494 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23495 Show DWARF specific variables\n\
23496 Show DWARF variables such as the cache size"),
23497 &show_dwarf_cmdlist, "maintenance show dwarf ",
23498 0/*allow-unknown*/, &maintenance_show_cmdlist);
23499
23500 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23501 &dwarf_max_cache_age, _("\
23502 Set the upper bound on the age of cached DWARF compilation units."), _("\
23503 Show the upper bound on the age of cached DWARF compilation units."), _("\
23504 A higher limit means that cached compilation units will be stored\n\
23505 in memory longer, and more total memory will be used. Zero disables\n\
23506 caching, which can slow down startup."),
23507 NULL,
23508 show_dwarf_max_cache_age,
23509 &set_dwarf_cmdlist,
23510 &show_dwarf_cmdlist);
23511
23512 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
23513 &dwarf_always_disassemble, _("\
23514 Set whether `info address' always disassembles DWARF expressions."), _("\
23515 Show whether `info address' always disassembles DWARF expressions."), _("\
23516 When enabled, DWARF expressions are always printed in an assembly-like\n\
23517 syntax. When disabled, expressions will be printed in a more\n\
23518 conversational style, when possible."),
23519 NULL,
23520 show_dwarf_always_disassemble,
23521 &set_dwarf_cmdlist,
23522 &show_dwarf_cmdlist);
23523
23524 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23525 Set debugging of the DWARF reader."), _("\
23526 Show debugging of the DWARF reader."), _("\
23527 When enabled (non-zero), debugging messages are printed during DWARF\n\
23528 reading and symtab expansion. A value of 1 (one) provides basic\n\
23529 information. A value greater than 1 provides more verbose information."),
23530 NULL,
23531 NULL,
23532 &setdebuglist, &showdebuglist);
23533
23534 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23535 Set debugging of the DWARF DIE reader."), _("\
23536 Show debugging of the DWARF DIE reader."), _("\
23537 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23538 The value is the maximum depth to print."),
23539 NULL,
23540 NULL,
23541 &setdebuglist, &showdebuglist);
23542
23543 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23544 Set debugging of the dwarf line reader."), _("\
23545 Show debugging of the dwarf line reader."), _("\
23546 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23547 A value of 1 (one) provides basic information.\n\
23548 A value greater than 1 provides more verbose information."),
23549 NULL,
23550 NULL,
23551 &setdebuglist, &showdebuglist);
23552
23553 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23554 Set cross-checking of \"physname\" code against demangler."), _("\
23555 Show cross-checking of \"physname\" code against demangler."), _("\
23556 When enabled, GDB's internal \"physname\" code is checked against\n\
23557 the demangler."),
23558 NULL, show_check_physname,
23559 &setdebuglist, &showdebuglist);
23560
23561 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23562 no_class, &use_deprecated_index_sections, _("\
23563 Set whether to use deprecated gdb_index sections."), _("\
23564 Show whether to use deprecated gdb_index sections."), _("\
23565 When enabled, deprecated .gdb_index sections are used anyway.\n\
23566 Normally they are ignored either because of a missing feature or\n\
23567 performance issue.\n\
23568 Warning: This option must be enabled before gdb reads the file."),
23569 NULL,
23570 NULL,
23571 &setlist, &showlist);
23572
23573 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
23574 _("\
23575 Save a gdb-index file.\n\
23576 Usage: save gdb-index DIRECTORY"),
23577 &save_cmdlist);
23578 set_cmd_completer (c, filename_completer);
23579
23580 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23581 &dwarf2_locexpr_funcs);
23582 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23583 &dwarf2_loclist_funcs);
23584
23585 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23586 &dwarf2_block_frame_base_locexpr_funcs);
23587 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23588 &dwarf2_block_frame_base_loclist_funcs);
23589 }