Fix another memory access error in readelf when parsing a corrupt binary.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
61baf725 3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
c906108c
SS
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
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
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
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
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.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
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
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
4c2df51b 73
c906108c 74#include <fcntl.h>
c906108c 75#include <sys/types.h>
325fac50 76#include <algorithm>
d8151005 77
34eaf542
TT
78typedef struct symbol *symbolp;
79DEF_VEC_P (symbolp);
80
73be47f5
DE
81/* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
b4f54984
DE
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84static unsigned int dwarf_read_debug = 0;
45cfd468 85
d97bc12b 86/* When non-zero, dump DIEs after they are read in. */
b4f54984 87static unsigned int dwarf_die_debug = 0;
d97bc12b 88
27e0867f
DE
89/* When non-zero, dump line number entries as they are read in. */
90static unsigned int dwarf_line_debug = 0;
91
900e11f9
JK
92/* When non-zero, cross-check physname against demangler. */
93static int check_physname = 0;
94
481860b3 95/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 96static int use_deprecated_index_sections = 0;
481860b3 97
6502dd73
DJ
98static const struct objfile_data *dwarf2_objfile_data_key;
99
f1e6e072
TT
100/* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102static int dwarf2_locexpr_index;
103static int dwarf2_loclist_index;
104static int dwarf2_locexpr_block_index;
105static int dwarf2_loclist_block_index;
106
73869dc2
DE
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
dce234bc
PP
123struct dwarf2_section_info
124{
73869dc2
DE
125 union
126 {
e5aa3347 127 /* If this is a real section, the bfd section. */
049412e3 128 asection *section;
73869dc2 129 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 130 section. */
73869dc2
DE
131 struct dwarf2_section_info *containing_section;
132 } s;
19ac8c2e 133 /* Pointer to section data, only valid if readin. */
d521ce57 134 const gdb_byte *buffer;
73869dc2 135 /* The size of the section, real or virtual. */
dce234bc 136 bfd_size_type size;
73869dc2
DE
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;
be391dca 140 /* True if we have tried to read this section. */
73869dc2
DE
141 char readin;
142 /* True if this is a virtual section, False otherwise.
049412e3 143 This specifies which of s.section and s.containing_section to use. */
73869dc2 144 char is_virtual;
dce234bc
PP
145};
146
8b70b953
TT
147typedef struct dwarf2_section_info dwarf2_section_info_def;
148DEF_VEC_O (dwarf2_section_info_def);
149
9291a0cd
TT
150/* All offsets in the index are of this type. It must be
151 architecture-independent. */
152typedef uint32_t offset_type;
153
154DEF_VEC_I (offset_type);
155
156942c7
DE
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
9291a0cd
TT
178/* A description of the mapped index. The file format is described in
179 a comment by the code that writes the index. */
180struct mapped_index
181{
559a7a62
JK
182 /* Index data format version. */
183 int version;
184
9291a0cd
TT
185 /* The total length of the buffer. */
186 off_t total_size;
b11b1f88 187
9291a0cd
TT
188 /* A pointer to the address table data. */
189 const gdb_byte *address_table;
b11b1f88 190
9291a0cd
TT
191 /* Size of the address table data in bytes. */
192 offset_type address_table_size;
b11b1f88 193
3876f04e
DE
194 /* The symbol table, implemented as a hash table. */
195 const offset_type *symbol_table;
b11b1f88 196
9291a0cd 197 /* Size in slots, each slot is 2 offset_types. */
3876f04e 198 offset_type symbol_table_slots;
b11b1f88 199
9291a0cd
TT
200 /* A pointer to the constant pool. */
201 const char *constant_pool;
202};
203
95554aad
TT
204typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
205DEF_VEC_P (dwarf2_per_cu_ptr);
206
52059ffd
TT
207struct 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
9cdd5dbd
DE
216/* Collection of data recorded per objfile.
217 This hangs off of dwarf2_objfile_data_key. */
218
6502dd73
DJ
219struct dwarf2_per_objfile
220{
dce234bc
PP
221 struct dwarf2_section_info info;
222 struct dwarf2_section_info abbrev;
223 struct dwarf2_section_info line;
dce234bc
PP
224 struct dwarf2_section_info loc;
225 struct dwarf2_section_info macinfo;
cf2c3c16 226 struct dwarf2_section_info macro;
dce234bc
PP
227 struct dwarf2_section_info str;
228 struct dwarf2_section_info ranges;
3019eac3 229 struct dwarf2_section_info addr;
dce234bc
PP
230 struct dwarf2_section_info frame;
231 struct dwarf2_section_info eh_frame;
9291a0cd 232 struct dwarf2_section_info gdb_index;
ae038cb0 233
8b70b953
TT
234 VEC (dwarf2_section_info_def) *types;
235
be391dca
TT
236 /* Back link. */
237 struct objfile *objfile;
238
d467dd73 239 /* Table of all the compilation units. This is used to locate
10b3939b 240 the target compilation unit of a particular reference. */
ae038cb0
DJ
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
1fd400ff 246 /* The number of .debug_types-related CUs. */
d467dd73 247 int n_type_units;
1fd400ff 248
6aa5f3a6
DE
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
a2ce51a0
DE
253 /* The .debug_types-related CUs (TUs).
254 This is stored in malloc space because we may realloc it. */
b4dd5633 255 struct signatured_type **all_type_units;
1fd400ff 256
f4dc4d17
DE
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;
72dca2f5 260
348e048f
DE
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
f4dc4d17
DE
265 /* Type unit statistics, to see how well the scaling improvements
266 are doing. */
52059ffd 267 struct tu_stats tu_stats;
f4dc4d17
DE
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
3019eac3
DE
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
80626a55
DE
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
36586728
TT
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
72dca2f5
FR
287 /* A flag indicating wether this objfile has a section loaded at a
288 VMA of 0. */
289 int has_section_at_zero;
9291a0cd 290
ae2de4f8
DE
291 /* True if we are using the mapped index,
292 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
293 unsigned char using_index;
294
ae2de4f8 295 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 296 struct mapped_index *index_table;
98bfdba5 297
7b9f3c50 298 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
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. */
7b9f3c50
DE
305 htab_t quick_file_names_table;
306
98bfdba5
PA
307 /* Set during partial symbol reading, to prevent queueing of full
308 symbols. */
309 int reading_partial_symbols;
673bfd45 310
dee91e82 311 /* Table mapping type DIEs to their struct type *.
673bfd45 312 This is NULL if not allocated yet.
02142a6c 313 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 314 htab_t die_type_hash;
95554aad
TT
315
316 /* The CUs we recently read. */
317 VEC (dwarf2_per_cu_ptr) *just_read_cus;
527f3840
JK
318
319 /* Table containing line_header indexed by offset and offset_in_dwz. */
320 htab_t line_header_hash;
6502dd73
DJ
321};
322
323static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 324
251d32d9 325/* Default names of the debugging sections. */
c906108c 326
233a11ab
CS
327/* Note that if the debugging section has been compressed, it might
328 have a name like .zdebug_info. */
329
9cdd5dbd
DE
330static const struct dwarf2_debug_sections dwarf2_elf_names =
331{
251d32d9
TG
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" },
cf2c3c16 337 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
338 { ".debug_str", ".zdebug_str" },
339 { ".debug_ranges", ".zdebug_ranges" },
340 { ".debug_types", ".zdebug_types" },
3019eac3 341 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
342 { ".debug_frame", ".zdebug_frame" },
343 { ".eh_frame", NULL },
24d3216f
TT
344 { ".gdb_index", ".zgdb_index" },
345 23
251d32d9 346};
c906108c 347
80626a55 348/* List of DWO/DWP sections. */
3019eac3 349
80626a55 350static const struct dwop_section_names
3019eac3
DE
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;
09262596
DE
356 struct dwarf2_section_names macinfo_dwo;
357 struct dwarf2_section_names macro_dwo;
3019eac3
DE
358 struct dwarf2_section_names str_dwo;
359 struct dwarf2_section_names str_offsets_dwo;
360 struct dwarf2_section_names types_dwo;
80626a55
DE
361 struct dwarf2_section_names cu_index;
362 struct dwarf2_section_names tu_index;
3019eac3 363}
80626a55 364dwop_section_names =
3019eac3
DE
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" },
09262596
DE
370 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
371 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
372 { ".debug_str.dwo", ".zdebug_str.dwo" },
373 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
374 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
375 { ".debug_cu_index", ".zdebug_cu_index" },
376 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
377};
378
c906108c
SS
379/* local data types */
380
107d2387
AC
381/* The data in a compilation unit header, after target2host
382 translation, looks like this. */
c906108c 383struct comp_unit_head
a738430d 384{
c764a876 385 unsigned int length;
a738430d 386 short version;
a738430d
MK
387 unsigned char addr_size;
388 unsigned char signed_addr_p;
b64f50a1 389 sect_offset abbrev_offset;
57349743 390
a738430d
MK
391 /* Size of file offsets; either 4 or 8. */
392 unsigned int offset_size;
57349743 393
a738430d
MK
394 /* Size of the length field; either 4 or 12. */
395 unsigned int initial_length_size;
57349743 396
a738430d
MK
397 /* Offset to the first byte of this compilation unit header in the
398 .debug_info section, for resolving relative reference dies. */
b64f50a1 399 sect_offset offset;
57349743 400
d00adf39
DE
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. */
b64f50a1 403 cu_offset first_die_offset;
a738430d 404};
c906108c 405
3da10d80
KS
406/* Type used for delaying computation of method physnames.
407 See comments for compute_delayed_physnames. */
408struct 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
426typedef struct delayed_method_info delayed_method_info;
427DEF_VEC_O (delayed_method_info);
428
e7c27a73
DJ
429/* Internal state when decoding a particular compilation unit. */
430struct dwarf2_cu
431{
432 /* The objfile containing this compilation unit. */
433 struct objfile *objfile;
434
d00adf39 435 /* The header of the compilation unit. */
e7c27a73 436 struct comp_unit_head header;
e142c38c 437
d00adf39
DE
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
e142c38c
DJ
444 /* The language we are debugging. */
445 enum language language;
446 const struct language_defn *language_defn;
447
b0f35d58
DL
448 const char *producer;
449
e142c38c
DJ
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
433df2d4
DE
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;
72bf9492 465
b64f50a1
JK
466 /* Hash table holding all the loaded partial DIEs
467 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
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
ae038cb0
DJ
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
69d751e3 480 /* Backlink to our per_cu entry. */
ae038cb0
DJ
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
b64f50a1
JK
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
51545339 488 htab_t die_hash;
10b3939b
DJ
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
cb1df416
DJ
498 /* Header data from the line table, during full symbol processing. */
499 struct line_header *line_header;
500
3da10d80
KS
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
96408a79
SA
505 /* To be copied to symtab->call_site_htab. */
506 htab_t call_site_htab;
507
034e5797
DE
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). */
3019eac3
DE
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).
1dbab08b 521 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
522 ULONGEST addr_base;
523
2e3cf129
DE
524 /* The DW_AT_ranges_base attribute if present, zero otherwise
525 (zero is a valid value though).
1dbab08b 526 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 527 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
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. */
2e3cf129
DE
534 ULONGEST ranges_base;
535
ae038cb0
DJ
536 /* Mark used when releasing cached dies. */
537 unsigned int mark : 1;
538
8be455d7
JK
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. */
8be455d7 543 unsigned int has_loclist : 1;
ba919b58 544
1b80a9fa
JK
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. */
ba919b58
TT
549 unsigned int checked_producer : 1;
550 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 551 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 552 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
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;
e7c27a73
DJ
559};
560
10b3939b
DJ
561/* Persistent data held for a compilation unit, even when not
562 processing it. We put a pointer to this structure in the
28dee7f5 563 read_symtab_private field of the psymtab. */
10b3939b 564
ae038cb0
DJ
565struct dwarf2_per_cu_data
566{
36586728 567 /* The start offset and length of this compilation unit.
45452591 568 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
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. */
b64f50a1 572 sect_offset offset;
36586728 573 unsigned int length;
ae038cb0
DJ
574
575 /* Flag indicating this compilation unit will be read in before
576 any of the current compilation units are processed. */
c764a876 577 unsigned int queued : 1;
ae038cb0 578
0d99eb77
DE
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
5afb4e99
DJ
582 hash table and don't find it. */
583 unsigned int load_all_dies : 1;
584
0186c6a7
DE
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. */
3019eac3
DE
588 unsigned int is_debug_types : 1;
589
36586728
TT
590 /* Non-zero if this CU is from the .dwz file. */
591 unsigned int is_dwz : 1;
592
a2ce51a0
DE
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
7ee85ab1
DE
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
3019eac3
DE
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. */
8a0459fd 612 struct dwarf2_section_info *section;
348e048f 613
17ea53c3 614 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
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). */
ae038cb0 617 struct dwarf2_cu *cu;
1c379e20 618
9cdd5dbd
DE
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. */
9291a0cd
TT
622 struct objfile *objfile;
623
fffbe6a8
YQ
624 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
625 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
626 union
627 {
628 /* The partial symbol table associated with this compilation unit,
95554aad 629 or NULL for unread partial units. */
9291a0cd
TT
630 struct partial_symtab *psymtab;
631
632 /* Data needed by the "quick" functions. */
633 struct dwarf2_per_cu_quick_data *quick;
634 } v;
95554aad 635
796a7ff8
DE
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
69d751e3
DE
652 indices so we only pay a price for gold generated indices.
653 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 654 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
655};
656
348e048f
DE
657/* Entry in the signatured_types hash table. */
658
659struct signatured_type
660{
42e7ad6c 661 /* The "per_cu" object of this type.
ac9ec31b 662 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
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
3019eac3 667 /* The type's signature. */
348e048f
DE
668 ULONGEST signature;
669
3019eac3 670 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
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. */
3019eac3
DE
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;
0186c6a7
DE
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;
ac9ec31b
DE
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;
a2ce51a0
DE
690
691 /* Containing DWO unit.
692 This field is valid iff per_cu.reading_dwo_directly. */
693 struct dwo_unit *dwo_unit;
348e048f
DE
694};
695
0186c6a7
DE
696typedef struct signatured_type *sig_type_ptr;
697DEF_VEC_P (sig_type_ptr);
698
094b34ac
DE
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
702struct 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
f4dc4d17
DE
711/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
712 an object of this type. */
713
714struct type_unit_group
715{
0186c6a7 716 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
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. */
8a0459fd 721#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
722 struct dwarf2_per_cu_data per_cu;
723
0186c6a7
DE
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;
f4dc4d17 728
43f3e411 729 /* The compunit symtab.
094b34ac 730 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
731 so we create an essentially anonymous symtab as the compunit symtab. */
732 struct compunit_symtab *compunit_symtab;
f4dc4d17 733
094b34ac
DE
734 /* The data used to construct the hash key. */
735 struct stmt_list_hash hash;
f4dc4d17
DE
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
73869dc2 752/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
753
754struct dwo_sections
755{
756 struct dwarf2_section_info abbrev;
3019eac3
DE
757 struct dwarf2_section_info line;
758 struct dwarf2_section_info loc;
09262596
DE
759 struct dwarf2_section_info macinfo;
760 struct dwarf2_section_info macro;
3019eac3
DE
761 struct dwarf2_section_info str;
762 struct dwarf2_section_info str_offsets;
80626a55
DE
763 /* In the case of a virtual DWO file, these two are unused. */
764 struct dwarf2_section_info info;
3019eac3
DE
765 VEC (dwarf2_section_info_def) *types;
766};
767
c88ee1f0 768/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
769
770struct 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. */
8a0459fd 781 struct dwarf2_section_info *section;
3019eac3 782
19ac8c2e 783 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
3019eac3
DE
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
73869dc2
DE
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
795enum dwp_v2_section_ids
796{
797 DW_SECT_MIN = 1
798};
799
80626a55 800/* Data for one DWO file.
57d63ce2
DE
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). */
3019eac3
DE
810
811struct dwo_file
812{
0ac5b59e 813 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
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. */
0ac5b59e
DE
817 const char *dwo_name;
818
819 /* The DW_AT_comp_dir attribute. */
820 const char *comp_dir;
3019eac3 821
80626a55
DE
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;
3019eac3 825
73869dc2
DE
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). */
3019eac3
DE
829 struct dwo_sections sections;
830
19c3d4c9
DE
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;
3019eac3
DE
837
838 /* Table of TUs in the file.
839 Each element is a struct dwo_unit. */
840 htab_t tus;
841};
842
80626a55
DE
843/* These sections are what may appear in a DWP file. */
844
845struct dwp_sections
846{
73869dc2 847 /* These are used by both DWP version 1 and 2. */
80626a55
DE
848 struct dwarf2_section_info str;
849 struct dwarf2_section_info cu_index;
850 struct dwarf2_section_info tu_index;
73869dc2
DE
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;
80626a55
DE
868};
869
73869dc2
DE
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. */
80626a55 872
73869dc2 873struct virtual_v1_dwo_sections
80626a55
DE
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.
8a0459fd 882 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
883 struct dwarf2_section_info info_or_types;
884};
885
73869dc2
DE
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
891struct 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
80626a55
DE
917/* Contents of DWP hash tables. */
918
919struct dwp_hash_table
920{
73869dc2 921 uint32_t version, nr_columns;
80626a55 922 uint32_t nr_units, nr_slots;
73869dc2
DE
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;
80626a55
DE
946};
947
948/* Data for one DWP file. */
949
950struct dwp_file
951{
952 /* Name of the file. */
953 const char *name;
954
73869dc2
DE
955 /* File format version. */
956 int version;
957
93417882 958 /* The bfd. */
80626a55
DE
959 bfd *dbfd;
960
961 /* Section info for this file. */
962 struct dwp_sections sections;
963
57d63ce2 964 /* Table of CUs in the file. */
80626a55
DE
965 const struct dwp_hash_table *cus;
966
967 /* Table of TUs in the file. */
968 const struct dwp_hash_table *tus;
969
19ac8c2e
DE
970 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
971 htab_t loaded_cus;
972 htab_t loaded_tus;
80626a55 973
73869dc2
DE
974 /* Table to map ELF section numbers to their sections.
975 This is only needed for the DWP V1 file format. */
80626a55
DE
976 unsigned int num_sections;
977 asection **elf_sections;
978};
979
36586728
TT
980/* This represents a '.dwz' file. */
981
982struct 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;
2ec9a5e0 990 struct dwarf2_section_info gdb_index;
36586728
TT
991
992 /* The dwz's BFD. */
993 bfd *dwz_bfd;
994};
995
0963b4bd
MS
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
dee91e82 999 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1000
1001struct die_reader_specs
1002{
a32a8923 1003 /* The bfd of die_section. */
93311388
DE
1004 bfd* abfd;
1005
1006 /* The CU of the DIE we are parsing. */
1007 struct dwarf2_cu *cu;
1008
80626a55 1009 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1010 struct dwo_file *dwo_file;
1011
dee91e82 1012 /* The section the die comes from.
3019eac3 1013 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1014 struct dwarf2_section_info *die_section;
1015
1016 /* die_section->buffer. */
d521ce57 1017 const gdb_byte *buffer;
f664829e
DE
1018
1019 /* The end of the buffer. */
1020 const gdb_byte *buffer_end;
a2ce51a0
DE
1021
1022 /* The value of the DW_AT_comp_dir attribute. */
1023 const char *comp_dir;
93311388
DE
1024};
1025
fd820528 1026/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1027typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1028 const gdb_byte *info_ptr,
dee91e82
DE
1029 struct die_info *comp_unit_die,
1030 int has_children,
1031 void *data);
1032
52059ffd
TT
1033struct file_entry
1034{
1035 const char *name;
1036 unsigned int dir_index;
1037 unsigned int mod_time;
1038 unsigned int length;
83769d0b
DE
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;
52059ffd
TT
1043};
1044
debd256d
JB
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. */
1048struct line_header
1049{
527f3840
JK
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
debd256d
JB
1056 unsigned int total_length;
1057 unsigned short version;
1058 unsigned int header_length;
1059 unsigned char minimum_instruction_length;
2dc7f7b3 1060 unsigned char maximum_ops_per_instruction;
debd256d
JB
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;
d521ce57 1077 const char **include_dirs;
debd256d
JB
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;
52059ffd 1083 struct file_entry *file_names;
debd256d
JB
1084
1085 /* The start and end of the statement program following this
6502dd73 1086 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 1087 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 1088};
c906108c
SS
1089
1090/* When we construct a partial symbol table entry we only
0963b4bd 1091 need this much information. */
c906108c
SS
1092struct partial_die_info
1093 {
72bf9492 1094 /* Offset of this DIE. */
b64f50a1 1095 sect_offset offset;
72bf9492
DJ
1096
1097 /* DWARF-2 tag for this DIE. */
1098 ENUM_BITFIELD(dwarf_tag) tag : 16;
1099
72bf9492
DJ
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;
481860b3 1107 unsigned int may_be_inlined : 1;
72bf9492 1108
0c1b455e
TT
1109 /* This DIE has been marked DW_AT_main_subprogram. */
1110 unsigned int main_subprogram : 1;
1111
72bf9492
DJ
1112 /* Flag set if the SCOPE field of this structure has been
1113 computed. */
1114 unsigned int scope_set : 1;
1115
fa4028e9
JB
1116 /* Flag set if the DIE has a byte_size attribute. */
1117 unsigned int has_byte_size : 1;
1118
ff908ebf
AW
1119 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1120 unsigned int has_const_value : 1;
1121
98bfdba5
PA
1122 /* Flag set if any of the DIE's children are template arguments. */
1123 unsigned int has_template_arguments : 1;
1124
abc72ce4
DE
1125 /* Flag set if fixup_partial_die has been called on this die. */
1126 unsigned int fixup_called : 1;
1127
36586728
TT
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
72bf9492 1134 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1135 sometimes a default name for unnamed DIEs. */
15d034d0 1136 const char *name;
72bf9492 1137
abc72ce4
DE
1138 /* The linkage name, if present. */
1139 const char *linkage_name;
1140
72bf9492
DJ
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. */
15d034d0 1144 const char *scope;
72bf9492 1145
95554aad
TT
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;
72bf9492
DJ
1155
1156 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1157 CORE_ADDR lowpc;
1158 CORE_ADDR highpc;
72bf9492 1159
93311388 1160 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1161 DW_AT_sibling, if any. */
abc72ce4
DE
1162 /* NOTE: This member isn't strictly necessary, read_partial_die could
1163 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1164 const gdb_byte *sibling;
72bf9492
DJ
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). */
b64f50a1 1169 sect_offset spec_offset;
72bf9492
DJ
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;
c906108c
SS
1174 };
1175
0963b4bd 1176/* This data structure holds the information of an abbrev. */
c906108c
SS
1177struct abbrev_info
1178 {
1179 unsigned int number; /* number identifying abbrev */
1180 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1181 unsigned short has_children; /* boolean */
1182 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1183 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1184 struct abbrev_info *next; /* next in chain */
1185 };
1186
1187struct attr_abbrev
1188 {
9d25dd43
DE
1189 ENUM_BITFIELD(dwarf_attribute) name : 16;
1190 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1191 };
1192
433df2d4
DE
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
1198struct abbrev_table
1199{
f4dc4d17
DE
1200 /* Where the abbrev table came from.
1201 This is used as a sanity check when the table is used. */
433df2d4
DE
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
0963b4bd 1214/* Attributes have a name and a value. */
b60c80d6
DJ
1215struct attribute
1216 {
9d25dd43 1217 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
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
b60c80d6
DJ
1225 union
1226 {
15d034d0 1227 const char *str;
b60c80d6 1228 struct dwarf_block *blk;
43bbcdc2
PH
1229 ULONGEST unsnd;
1230 LONGEST snd;
b60c80d6 1231 CORE_ADDR addr;
ac9ec31b 1232 ULONGEST signature;
b60c80d6
DJ
1233 }
1234 u;
1235 };
1236
0963b4bd 1237/* This data structure holds a complete die structure. */
c906108c
SS
1238struct die_info
1239 {
76815b17
DE
1240 /* DWARF-2 tag for this DIE. */
1241 ENUM_BITFIELD(dwarf_tag) tag : 16;
1242
1243 /* Number of attributes */
98bfdba5
PA
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;
76815b17 1249
adde2bff
DE
1250 /* True if this die is in process. PR 16581. */
1251 unsigned char in_process : 1;
1252
76815b17
DE
1253 /* Abbrev number */
1254 unsigned int abbrev;
1255
93311388 1256 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1257 sect_offset offset;
78ba4af6
JB
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
4950bc1c 1262 together via their SIBLING fields. */
639d11d3
DC
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. */
c906108c 1266
b60c80d6
DJ
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];
c906108c
SS
1271 };
1272
0963b4bd 1273/* Get at parts of an attribute structure. */
c906108c
SS
1274
1275#define DW_STRING(attr) ((attr)->u.str)
8285870a 1276#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
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)
ac9ec31b 1281#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1282
0963b4bd 1283/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1284struct dwarf_block
1285 {
56eb65bd 1286 size_t size;
1d6edc3c
JK
1287
1288 /* Valid only if SIZE is not zero. */
d521ce57 1289 const gdb_byte *data;
c906108c
SS
1290 };
1291
c906108c
SS
1292#ifndef ATTR_ALLOC_CHUNK
1293#define ATTR_ALLOC_CHUNK 4
1294#endif
1295
c906108c
SS
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
c906108c
SS
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. */
1304static int bits_per_byte = 8;
1305
52059ffd
TT
1306struct nextfield
1307{
1308 struct nextfield *next;
1309 int accessibility;
1310 int virtuality;
1311 struct field field;
1312};
1313
1314struct nextfnfield
1315{
1316 struct nextfnfield *next;
1317 struct fn_field fnfield;
1318};
1319
1320struct fnfieldlist
1321{
1322 const char *name;
1323 int length;
1324 struct nextfnfield *head;
1325};
1326
1327struct typedef_field_list
1328{
1329 struct typedef_field field;
1330 struct typedef_field_list *next;
1331};
1332
c906108c
SS
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. */
1336struct field_info
c5aa993b 1337 {
0963b4bd 1338 /* List of data member and baseclasses fields. */
52059ffd 1339 struct nextfield *fields, *baseclasses;
c906108c 1340
7d0ccb61 1341 /* Number of fields (including baseclasses). */
c5aa993b 1342 int nfields;
c906108c 1343
c5aa993b
JM
1344 /* Number of baseclasses. */
1345 int nbaseclasses;
c906108c 1346
c5aa993b
JM
1347 /* Set if the accesibility of one of the fields is not public. */
1348 int non_public_fields;
c906108c 1349
c5aa993b
JM
1350 /* Member function fields array, entries are allocated in the order they
1351 are encountered in the object file. */
52059ffd 1352 struct nextfnfield *fnfields;
c906108c 1353
c5aa993b
JM
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. */
52059ffd 1357 struct fnfieldlist *fnfieldlists;
c906108c 1358
c5aa993b
JM
1359 /* Number of entries in the fnfieldlists array. */
1360 int nfnfields;
98751a41
JK
1361
1362 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1363 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1364 struct typedef_field_list *typedef_field_list;
98751a41 1365 unsigned typedef_field_list_count;
c5aa993b 1366 };
c906108c 1367
10b3939b
DJ
1368/* One item on the queue of compilation units to read in full symbols
1369 for. */
1370struct dwarf2_queue_item
1371{
1372 struct dwarf2_per_cu_data *per_cu;
95554aad 1373 enum language pretend_language;
10b3939b
DJ
1374 struct dwarf2_queue_item *next;
1375};
1376
1377/* The current queue. */
1378static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1379
ae038cb0
DJ
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. */
b4f54984 1385static int dwarf_max_cache_age = 5;
920d2a44 1386static void
b4f54984
DE
1387show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1388 struct cmd_list_element *c, const char *value)
920d2a44 1389{
3e43a32a 1390 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1391 "DWARF compilation units is %s.\n"),
920d2a44
AC
1392 value);
1393}
4390d890 1394\f
c906108c
SS
1395/* local function prototypes */
1396
a32a8923
DE
1397static const char *get_section_name (const struct dwarf2_section_info *);
1398
1399static const char *get_section_file_name (const struct dwarf2_section_info *);
1400
4efb68b1 1401static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1402
918dd910
JK
1403static void dwarf2_find_base_address (struct die_info *die,
1404 struct dwarf2_cu *cu);
1405
0018ea6f
DE
1406static struct partial_symtab *create_partial_symtab
1407 (struct dwarf2_per_cu_data *per_cu, const char *name);
1408
c67a9c90 1409static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1410
72bf9492
DJ
1411static void scan_partial_symbols (struct partial_die_info *,
1412 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1413 int, struct dwarf2_cu *);
c906108c 1414
72bf9492
DJ
1415static void add_partial_symbol (struct partial_die_info *,
1416 struct dwarf2_cu *);
63d06c5c 1417
72bf9492
DJ
1418static void add_partial_namespace (struct partial_die_info *pdi,
1419 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1420 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1421
5d7cb8df 1422static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1423 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1424 struct dwarf2_cu *cu);
1425
72bf9492
DJ
1426static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1427 struct dwarf2_cu *cu);
91c24f0a 1428
bc30ff58
JB
1429static void add_partial_subprogram (struct partial_die_info *pdi,
1430 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1431 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1432
257e7a09
YQ
1433static void dwarf2_read_symtab (struct partial_symtab *,
1434 struct objfile *);
c906108c 1435
a14ed312 1436static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1437
433df2d4
DE
1438static struct abbrev_info *abbrev_table_lookup_abbrev
1439 (const struct abbrev_table *, unsigned int);
1440
1441static struct abbrev_table *abbrev_table_read_table
1442 (struct dwarf2_section_info *, sect_offset);
1443
1444static void abbrev_table_free (struct abbrev_table *);
1445
f4dc4d17
DE
1446static void abbrev_table_free_cleanup (void *);
1447
dee91e82
DE
1448static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1449 struct dwarf2_section_info *);
c906108c 1450
f3dd6933 1451static void dwarf2_free_abbrev_table (void *);
c906108c 1452
d521ce57 1453static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1454
dee91e82 1455static struct partial_die_info *load_partial_dies
d521ce57 1456 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1457
d521ce57
TT
1458static 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 *);
c906108c 1463
36586728 1464static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1465 struct dwarf2_cu *);
72bf9492
DJ
1466
1467static void fixup_partial_die (struct partial_die_info *,
1468 struct dwarf2_cu *);
1469
d521ce57
TT
1470static const gdb_byte *read_attribute (const struct die_reader_specs *,
1471 struct attribute *, struct attr_abbrev *,
1472 const gdb_byte *);
a8329558 1473
a1855c1d 1474static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1475
a1855c1d 1476static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1477
a1855c1d 1478static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1479
a1855c1d 1480static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1481
a1855c1d 1482static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1483
d521ce57 1484static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1485 unsigned int *);
c906108c 1486
d521ce57 1487static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1488
1489static LONGEST read_checked_initial_length_and_offset
d521ce57 1490 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1491 unsigned int *, unsigned int *);
613e1657 1492
d521ce57
TT
1493static LONGEST read_offset (bfd *, const gdb_byte *,
1494 const struct comp_unit_head *,
c764a876
DE
1495 unsigned int *);
1496
d521ce57 1497static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1498
f4dc4d17
DE
1499static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1500 sect_offset);
1501
d521ce57 1502static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1503
d521ce57 1504static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1505
d521ce57
TT
1506static const char *read_indirect_string (bfd *, const gdb_byte *,
1507 const struct comp_unit_head *,
1508 unsigned int *);
4bdf3d34 1509
d521ce57 1510static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1511
d521ce57 1512static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1513
d521ce57 1514static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1515
d521ce57
TT
1516static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1517 const gdb_byte *,
3019eac3
DE
1518 unsigned int *);
1519
d521ce57 1520static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1521 ULONGEST str_index);
3019eac3 1522
e142c38c 1523static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1524
e142c38c
DJ
1525static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1526 struct dwarf2_cu *);
c906108c 1527
348e048f 1528static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1529 unsigned int);
348e048f 1530
7d45c7c3
KB
1531static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1532 struct dwarf2_cu *cu);
1533
05cf31d1
JB
1534static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1535 struct dwarf2_cu *cu);
1536
e142c38c 1537static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1538
e142c38c 1539static struct die_info *die_specification (struct die_info *die,
f2f0e013 1540 struct dwarf2_cu **);
63d06c5c 1541
debd256d
JB
1542static void free_line_header (struct line_header *lh);
1543
3019eac3
DE
1544static struct line_header *dwarf_decode_line_header (unsigned int offset,
1545 struct dwarf2_cu *cu);
debd256d 1546
f3f5162e 1547static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1548 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1549 CORE_ADDR, int decode_mapping);
c906108c 1550
4d663531 1551static void dwarf2_start_subfile (const char *, const char *);
c906108c 1552
43f3e411
DE
1553static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1554 const char *, const char *,
1555 CORE_ADDR);
f4dc4d17 1556
a14ed312 1557static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1558 struct dwarf2_cu *);
c906108c 1559
34eaf542
TT
1560static struct symbol *new_symbol_full (struct die_info *, struct type *,
1561 struct dwarf2_cu *, struct symbol *);
1562
ff39bb5e 1563static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1564 struct dwarf2_cu *);
c906108c 1565
ff39bb5e 1566static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1567 struct type *type,
1568 const char *name,
1569 struct obstack *obstack,
12df843f 1570 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1571 const gdb_byte **bytes,
98bfdba5 1572 struct dwarf2_locexpr_baton **baton);
2df3850c 1573
e7c27a73 1574static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1575
b4ba55a1
JB
1576static int need_gnat_info (struct dwarf2_cu *);
1577
3e43a32a
MS
1578static struct type *die_descriptive_type (struct die_info *,
1579 struct dwarf2_cu *);
b4ba55a1
JB
1580
1581static void set_descriptive_type (struct type *, struct die_info *,
1582 struct dwarf2_cu *);
1583
e7c27a73
DJ
1584static struct type *die_containing_type (struct die_info *,
1585 struct dwarf2_cu *);
c906108c 1586
ff39bb5e 1587static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1588 struct dwarf2_cu *);
c906108c 1589
f792889a 1590static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1591
673bfd45
DE
1592static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1593
0d5cff50 1594static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1595
6e70227d 1596static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1597 const char *suffix, int physname,
1598 struct dwarf2_cu *cu);
63d06c5c 1599
e7c27a73 1600static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1601
348e048f
DE
1602static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1603
e7c27a73 1604static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1605
e7c27a73 1606static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1607
96408a79
SA
1608static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1609
ff013f42
JK
1610static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1611 struct dwarf2_cu *, struct partial_symtab *);
1612
3a2b436a 1613/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1614 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1615enum pc_bounds_kind
1616{
e385593e 1617 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1618 PC_BOUNDS_NOT_PRESENT,
1619
e385593e
JK
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
3a2b436a
JK
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
1631static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1632 CORE_ADDR *, CORE_ADDR *,
1633 struct dwarf2_cu *,
1634 struct partial_symtab *);
c906108c 1635
fae299cd
DC
1636static void get_scope_pc_bounds (struct die_info *,
1637 CORE_ADDR *, CORE_ADDR *,
1638 struct dwarf2_cu *);
1639
801e3a5b
JB
1640static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1641 CORE_ADDR, struct dwarf2_cu *);
1642
a14ed312 1643static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1644 struct dwarf2_cu *);
c906108c 1645
a14ed312 1646static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1647 struct type *, struct dwarf2_cu *);
c906108c 1648
a14ed312 1649static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1650 struct die_info *, struct type *,
e7c27a73 1651 struct dwarf2_cu *);
c906108c 1652
a14ed312 1653static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1654 struct type *,
1655 struct dwarf2_cu *);
c906108c 1656
134d01f1 1657static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1658
e7c27a73 1659static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1660
e7c27a73 1661static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1662
5d7cb8df
JK
1663static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1664
22cee43f
PMR
1665static struct using_direct **using_directives (enum language);
1666
27aa8d6a
SW
1667static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1668
74921315
KS
1669static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1670
f55ee35c
JK
1671static struct type *read_module_type (struct die_info *die,
1672 struct dwarf2_cu *cu);
1673
38d518c9 1674static const char *namespace_name (struct die_info *die,
e142c38c 1675 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1676
134d01f1 1677static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1678
e7c27a73 1679static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1680
6e70227d 1681static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1682 struct dwarf2_cu *);
1683
bf6af496 1684static struct die_info *read_die_and_siblings_1
d521ce57 1685 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1686 struct die_info *);
639d11d3 1687
dee91e82 1688static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1689 const gdb_byte *info_ptr,
1690 const gdb_byte **new_info_ptr,
639d11d3
DC
1691 struct die_info *parent);
1692
d521ce57
TT
1693static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1694 struct die_info **, const gdb_byte *,
1695 int *, int);
3019eac3 1696
d521ce57
TT
1697static const gdb_byte *read_full_die (const struct die_reader_specs *,
1698 struct die_info **, const gdb_byte *,
1699 int *);
93311388 1700
e7c27a73 1701static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1702
15d034d0
TT
1703static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1704 struct obstack *);
71c25dea 1705
15d034d0 1706static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1707
15d034d0 1708static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1709 struct die_info *die,
1710 struct dwarf2_cu *cu);
1711
ca69b9e6
DE
1712static const char *dwarf2_physname (const char *name, struct die_info *die,
1713 struct dwarf2_cu *cu);
1714
e142c38c 1715static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1716 struct dwarf2_cu **);
9219021c 1717
f39c6ffd 1718static const char *dwarf_tag_name (unsigned int);
c906108c 1719
f39c6ffd 1720static const char *dwarf_attr_name (unsigned int);
c906108c 1721
f39c6ffd 1722static const char *dwarf_form_name (unsigned int);
c906108c 1723
a14ed312 1724static char *dwarf_bool_name (unsigned int);
c906108c 1725
f39c6ffd 1726static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1727
f9aca02d 1728static struct die_info *sibling_die (struct die_info *);
c906108c 1729
d97bc12b
DE
1730static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1731
1732static void dump_die_for_error (struct die_info *);
1733
1734static void dump_die_1 (struct ui_file *, int level, int max_level,
1735 struct die_info *);
c906108c 1736
d97bc12b 1737/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1738
51545339 1739static void store_in_ref_table (struct die_info *,
10b3939b 1740 struct dwarf2_cu *);
c906108c 1741
ff39bb5e 1742static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1743
ff39bb5e 1744static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1745
348e048f 1746static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1747 const struct attribute *,
348e048f
DE
1748 struct dwarf2_cu **);
1749
10b3939b 1750static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1751 const struct attribute *,
f2f0e013 1752 struct dwarf2_cu **);
c906108c 1753
348e048f 1754static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1755 const struct attribute *,
348e048f
DE
1756 struct dwarf2_cu **);
1757
ac9ec31b
DE
1758static struct type *get_signatured_type (struct die_info *, ULONGEST,
1759 struct dwarf2_cu *);
1760
1761static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1762 const struct attribute *,
ac9ec31b
DE
1763 struct dwarf2_cu *);
1764
e5fe5e75 1765static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1766
52dc124a 1767static void read_signatured_type (struct signatured_type *);
348e048f 1768
63e43d3a
PMR
1769static int attr_to_dynamic_prop (const struct attribute *attr,
1770 struct die_info *die, struct dwarf2_cu *cu,
1771 struct dynamic_prop *prop);
1772
c906108c
SS
1773/* memory allocation interface */
1774
7b5a2f43 1775static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1776
b60c80d6 1777static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1778
43f3e411 1779static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1780
6e5a29e1 1781static int attr_form_is_block (const struct attribute *);
8e19ed76 1782
6e5a29e1 1783static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1784
6e5a29e1 1785static int attr_form_is_constant (const struct attribute *);
3690dd37 1786
6e5a29e1 1787static int attr_form_is_ref (const struct attribute *);
7771576e 1788
8cf6f0b1
TT
1789static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1790 struct dwarf2_loclist_baton *baton,
ff39bb5e 1791 const struct attribute *attr);
8cf6f0b1 1792
ff39bb5e 1793static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1794 struct symbol *sym,
f1e6e072
TT
1795 struct dwarf2_cu *cu,
1796 int is_block);
4c2df51b 1797
d521ce57
TT
1798static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1799 const gdb_byte *info_ptr,
1800 struct abbrev_info *abbrev);
4bb7a0a7 1801
72bf9492
DJ
1802static void free_stack_comp_unit (void *);
1803
72bf9492
DJ
1804static hashval_t partial_die_hash (const void *item);
1805
1806static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1807
ae038cb0 1808static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1809 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1810
9816fde3 1811static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1812 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1813
1814static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1815 struct die_info *comp_unit_die,
1816 enum language pretend_language);
93311388 1817
68dc6402 1818static void free_heap_comp_unit (void *);
ae038cb0
DJ
1819
1820static void free_cached_comp_units (void *);
1821
1822static void age_cached_comp_units (void);
1823
dee91e82 1824static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1825
f792889a
DJ
1826static struct type *set_die_type (struct die_info *, struct type *,
1827 struct dwarf2_cu *);
1c379e20 1828
ae038cb0
DJ
1829static void create_all_comp_units (struct objfile *);
1830
0e50663e 1831static int create_all_type_units (struct objfile *);
1fd400ff 1832
95554aad
TT
1833static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1834 enum language);
10b3939b 1835
95554aad
TT
1836static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1837 enum language);
10b3939b 1838
f4dc4d17
DE
1839static void process_full_type_unit (struct dwarf2_per_cu_data *,
1840 enum language);
1841
10b3939b
DJ
1842static void dwarf2_add_dependence (struct dwarf2_cu *,
1843 struct dwarf2_per_cu_data *);
1844
ae038cb0
DJ
1845static void dwarf2_mark (struct dwarf2_cu *);
1846
1847static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1848
b64f50a1 1849static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1850 struct dwarf2_per_cu_data *);
673bfd45 1851
f792889a 1852static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1853
9291a0cd
TT
1854static void dwarf2_release_queue (void *dummy);
1855
95554aad
TT
1856static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1857 enum language pretend_language);
1858
a0f42c21 1859static void process_queue (void);
9291a0cd
TT
1860
1861static void find_file_and_directory (struct die_info *die,
1862 struct dwarf2_cu *cu,
15d034d0 1863 const char **name, const char **comp_dir);
9291a0cd
TT
1864
1865static char *file_full_name (int file, struct line_header *lh,
1866 const char *comp_dir);
1867
d521ce57 1868static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1869 (struct comp_unit_head *header,
1870 struct dwarf2_section_info *section,
d521ce57 1871 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1872 int is_debug_types_section);
1873
fd820528 1874static void init_cutu_and_read_dies
f4dc4d17
DE
1875 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1876 int use_existing_cu, int keep,
3019eac3
DE
1877 die_reader_func_ftype *die_reader_func, void *data);
1878
dee91e82
DE
1879static 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);
9291a0cd 1882
673bfd45 1883static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1884
3019eac3
DE
1885static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1886
57d63ce2
DE
1887static 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);
a2ce51a0
DE
1890
1891static struct dwp_file *get_dwp_file (void);
1892
3019eac3 1893static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1894 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1895
1896static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1897 (struct signatured_type *, const char *, const char *);
3019eac3 1898
89e63ee4
DE
1899static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1900
3019eac3
DE
1901static void free_dwo_file_cleanup (void *);
1902
95554aad
TT
1903static void process_cu_includes (void);
1904
1b80a9fa 1905static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1906
1907static void free_line_header_voidp (void *arg);
4390d890
DE
1908\f
1909/* Various complaints about symbol reading that don't abort the process. */
1910
1911static void
1912dwarf2_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
1918static void
1919dwarf2_debug_line_missing_file_complaint (void)
1920{
1921 complaint (&symfile_complaints,
1922 _(".debug_line section has line data without a file"));
1923}
1924
1925static void
1926dwarf2_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
1933static void
1934dwarf2_complex_location_expr_complaint (void)
1935{
1936 complaint (&symfile_complaints, _("location expression too complex"));
1937}
1938
1939static void
1940dwarf2_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
1948static void
1949dwarf2_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]"),
a32a8923
DE
1954 get_section_name (section),
1955 get_section_file_name (section));
4390d890 1956}
1b80a9fa 1957
4390d890
DE
1958static void
1959dwarf2_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
1967static void
1968dwarf2_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}
527f3840
JK
1974
1975/* Hash function for line_header_hash. */
1976
1977static hashval_t
1978line_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
1985static hashval_t
1986line_header_hash_voidp (const void *item)
1987{
9a3c8263 1988 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1989
1990 return line_header_hash (ofs);
1991}
1992
1993/* Equality function for line_header_hash. */
1994
1995static int
1996line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1997{
9a3c8263
SM
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;
527f3840
JK
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
4390d890 2005\f
9291a0cd
TT
2006#if WORDS_BIGENDIAN
2007
2008/* Convert VALUE between big- and little-endian. */
2009static offset_type
2010byte_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
31aa7e4e
JB
2027/* Read the given attribute value as an address, taking the attribute's
2028 form into account. */
2029
2030static CORE_ADDR
2031attr_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
9291a0cd
TT
2057/* The suffix for an index file. */
2058#define INDEX_SUFFIX ".gdb-index"
2059
c906108c 2060/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
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. */
c906108c
SS
2064
2065int
251d32d9
TG
2066dwarf2_has_info (struct objfile *objfile,
2067 const struct dwarf2_debug_sections *names)
c906108c 2068{
9a3c8263
SM
2069 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2070 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2071 if (!dwarf2_per_objfile)
2072 {
2073 /* Initialize per-objfile state. */
2074 struct dwarf2_per_objfile *data
8d749320 2075 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2076
be391dca
TT
2077 memset (data, 0, sizeof (*data));
2078 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2079 dwarf2_per_objfile = data;
6502dd73 2080
251d32d9
TG
2081 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2082 (void *) names);
be391dca
TT
2083 dwarf2_per_objfile->objfile = objfile;
2084 }
73869dc2 2085 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2086 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2087 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2088 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2089}
2090
2091/* Return the containing section of virtual section SECTION. */
2092
2093static struct dwarf2_section_info *
2094get_containing_section (const struct dwarf2_section_info *section)
2095{
2096 gdb_assert (section->is_virtual);
2097 return section->s.containing_section;
c906108c
SS
2098}
2099
a32a8923
DE
2100/* Return the bfd owner of SECTION. */
2101
2102static struct bfd *
2103get_section_bfd_owner (const struct dwarf2_section_info *section)
2104{
73869dc2
DE
2105 if (section->is_virtual)
2106 {
2107 section = get_containing_section (section);
2108 gdb_assert (!section->is_virtual);
2109 }
049412e3 2110 return section->s.section->owner;
a32a8923
DE
2111}
2112
2113/* Return the bfd section of SECTION.
2114 Returns NULL if the section is not present. */
2115
2116static asection *
2117get_section_bfd_section (const struct dwarf2_section_info *section)
2118{
73869dc2
DE
2119 if (section->is_virtual)
2120 {
2121 section = get_containing_section (section);
2122 gdb_assert (!section->is_virtual);
2123 }
049412e3 2124 return section->s.section;
a32a8923
DE
2125}
2126
2127/* Return the name of SECTION. */
2128
2129static const char *
2130get_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
2140static const char *
2141get_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
2151static int
2152get_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.
73869dc2 2162 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2163
2164static int
2165get_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
251d32d9
TG
2173/* When loading sections, we look either for uncompressed section or for
2174 compressed section names. */
233a11ab
CS
2175
2176static int
251d32d9
TG
2177section_is_p (const char *section_name,
2178 const struct dwarf2_section_names *names)
233a11ab 2179{
251d32d9
TG
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;
233a11ab
CS
2187}
2188
c906108c
SS
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
2193static void
251d32d9 2194dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 2195{
251d32d9 2196 const struct dwarf2_debug_sections *names;
dc7650b8 2197 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
2198
2199 if (vnames == NULL)
2200 names = &dwarf2_elf_names;
2201 else
2202 names = (const struct dwarf2_debug_sections *) vnames;
2203
dc7650b8
JK
2204 if ((aflag & SEC_HAS_CONTENTS) == 0)
2205 {
2206 }
2207 else if (section_is_p (sectp->name, &names->info))
c906108c 2208 {
049412e3 2209 dwarf2_per_objfile->info.s.section = sectp;
dce234bc 2210 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 2211 }
251d32d9 2212 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 2213 {
049412e3 2214 dwarf2_per_objfile->abbrev.s.section = sectp;
dce234bc 2215 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 2216 }
251d32d9 2217 else if (section_is_p (sectp->name, &names->line))
c906108c 2218 {
049412e3 2219 dwarf2_per_objfile->line.s.section = sectp;
dce234bc 2220 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 2221 }
251d32d9 2222 else if (section_is_p (sectp->name, &names->loc))
c906108c 2223 {
049412e3 2224 dwarf2_per_objfile->loc.s.section = sectp;
dce234bc 2225 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 2226 }
251d32d9 2227 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 2228 {
049412e3 2229 dwarf2_per_objfile->macinfo.s.section = sectp;
dce234bc 2230 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 2231 }
cf2c3c16
TT
2232 else if (section_is_p (sectp->name, &names->macro))
2233 {
049412e3 2234 dwarf2_per_objfile->macro.s.section = sectp;
cf2c3c16
TT
2235 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2236 }
251d32d9 2237 else if (section_is_p (sectp->name, &names->str))
c906108c 2238 {
049412e3 2239 dwarf2_per_objfile->str.s.section = sectp;
dce234bc 2240 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 2241 }
3019eac3
DE
2242 else if (section_is_p (sectp->name, &names->addr))
2243 {
049412e3 2244 dwarf2_per_objfile->addr.s.section = sectp;
3019eac3
DE
2245 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2246 }
251d32d9 2247 else if (section_is_p (sectp->name, &names->frame))
b6af0555 2248 {
049412e3 2249 dwarf2_per_objfile->frame.s.section = sectp;
dce234bc 2250 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 2251 }
251d32d9 2252 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 2253 {
049412e3 2254 dwarf2_per_objfile->eh_frame.s.section = sectp;
dc7650b8 2255 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2256 }
251d32d9 2257 else if (section_is_p (sectp->name, &names->ranges))
af34e669 2258 {
049412e3 2259 dwarf2_per_objfile->ranges.s.section = sectp;
dce234bc 2260 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 2261 }
251d32d9 2262 else if (section_is_p (sectp->name, &names->types))
348e048f 2263 {
8b70b953
TT
2264 struct dwarf2_section_info type_section;
2265
2266 memset (&type_section, 0, sizeof (type_section));
049412e3 2267 type_section.s.section = sectp;
8b70b953
TT
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);
348e048f 2272 }
251d32d9 2273 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd 2274 {
049412e3 2275 dwarf2_per_objfile->gdb_index.s.section = sectp;
9291a0cd
TT
2276 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2277 }
dce234bc 2278
b4e1fd61 2279 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5
FR
2280 && bfd_section_vma (abfd, sectp) == 0)
2281 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
2282}
2283
fceca515
DE
2284/* A helper function that decides whether a section is empty,
2285 or not present. */
9e0ac564
TT
2286
2287static int
19ac8c2e 2288dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2289{
73869dc2
DE
2290 if (section->is_virtual)
2291 return section->size == 0;
049412e3 2292 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2293}
2294
3019eac3
DE
2295/* Read the contents of the section INFO.
2296 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2297 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2298 of the DWO file.
dce234bc 2299 If the section is compressed, uncompress it before returning. */
c906108c 2300
dce234bc
PP
2301static void
2302dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2303{
a32a8923 2304 asection *sectp;
3019eac3 2305 bfd *abfd;
dce234bc 2306 gdb_byte *buf, *retbuf;
c906108c 2307
be391dca
TT
2308 if (info->readin)
2309 return;
dce234bc 2310 info->buffer = NULL;
be391dca 2311 info->readin = 1;
188dd5d6 2312
9e0ac564 2313 if (dwarf2_section_empty_p (info))
dce234bc 2314 return;
c906108c 2315
a32a8923 2316 sectp = get_section_bfd_section (info);
3019eac3 2317
73869dc2
DE
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
4bf44c1c
TT
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)
dce234bc 2346 {
d521ce57 2347 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2348 return;
dce234bc 2349 }
dce234bc 2350
224c3ddb 2351 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2352 info->buffer = buf;
dce234bc
PP
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. */
ac8035ab 2358 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2359 if (retbuf != NULL)
2360 {
2361 info->buffer = retbuf;
2362 return;
2363 }
2364
a32a8923
DE
2365 abfd = get_section_bfd_owner (info);
2366 gdb_assert (abfd != NULL);
2367
dce234bc
PP
2368 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2369 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
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 }
dce234bc
PP
2375}
2376
9e0ac564
TT
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
2384static bfd_size_type
2385dwarf2_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
dce234bc 2393/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2394 SECTION_NAME. */
af34e669 2395
dce234bc 2396void
3017a003
TG
2397dwarf2_get_section_info (struct objfile *objfile,
2398 enum dwarf2_section_enum sect,
d521ce57 2399 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2400 bfd_size_type *sizep)
2401{
2402 struct dwarf2_per_objfile *data
9a3c8263
SM
2403 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2404 dwarf2_objfile_data_key);
dce234bc 2405 struct dwarf2_section_info *info;
a3b2a86b
TT
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 }
3017a003
TG
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 }
dce234bc 2427
9e0ac564 2428 dwarf2_read_section (objfile, info);
dce234bc 2429
a32a8923 2430 *sectp = get_section_bfd_section (info);
dce234bc
PP
2431 *bufp = info->buffer;
2432 *sizep = info->size;
2433}
2434
36586728
TT
2435/* A helper function to find the sections for a .dwz file. */
2436
2437static void
2438locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2439{
9a3c8263 2440 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
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 {
049412e3 2446 dwz_file->abbrev.s.section = sectp;
36586728
TT
2447 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2448 }
2449 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2450 {
049412e3 2451 dwz_file->info.s.section = sectp;
36586728
TT
2452 dwz_file->info.size = bfd_get_section_size (sectp);
2453 }
2454 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2455 {
049412e3 2456 dwz_file->str.s.section = sectp;
36586728
TT
2457 dwz_file->str.size = bfd_get_section_size (sectp);
2458 }
2459 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2460 {
049412e3 2461 dwz_file->line.s.section = sectp;
36586728
TT
2462 dwz_file->line.size = bfd_get_section_size (sectp);
2463 }
2464 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2465 {
049412e3 2466 dwz_file->macro.s.section = sectp;
36586728
TT
2467 dwz_file->macro.size = bfd_get_section_size (sectp);
2468 }
2ec9a5e0
TT
2469 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2470 {
049412e3 2471 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2472 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2473 }
36586728
TT
2474}
2475
4db1a1dc
TT
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. */
36586728
TT
2479
2480static struct dwz_file *
2481dwarf2_get_dwz_file (void)
2482{
4db1a1dc 2483 char *data;
36586728
TT
2484 struct cleanup *cleanup;
2485 const char *filename;
2486 struct dwz_file *result;
acd13123 2487 bfd_size_type buildid_len_arg;
dc294be5
TT
2488 size_t buildid_len;
2489 bfd_byte *buildid;
36586728
TT
2490
2491 if (dwarf2_per_objfile->dwz_file != NULL)
2492 return dwarf2_per_objfile->dwz_file;
2493
4db1a1dc
TT
2494 bfd_set_error (bfd_error_no_error);
2495 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2496 &buildid_len_arg, &buildid);
4db1a1dc
TT
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 }
36586728 2504 cleanup = make_cleanup (xfree, data);
dc294be5 2505 make_cleanup (xfree, buildid);
36586728 2506
acd13123
TT
2507 buildid_len = (size_t) buildid_len_arg;
2508
f9d83a0b 2509 filename = (const char *) data;
36586728
TT
2510 if (!IS_ABSOLUTE_PATH (filename))
2511 {
4262abfb 2512 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
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
dc294be5
TT
2524 /* First try the file name given in the section. If that doesn't
2525 work, try to use the build-id instead. */
192b62ce 2526 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2527 if (dwz_bfd != NULL)
36586728 2528 {
192b62ce
TT
2529 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2530 dwz_bfd.release ();
36586728
TT
2531 }
2532
dc294be5
TT
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
36586728
TT
2540 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2541 struct dwz_file);
192b62ce 2542 result->dwz_bfd = dwz_bfd.release ();
36586728 2543
192b62ce 2544 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728
TT
2545
2546 do_cleanups (cleanup);
2547
192b62ce 2548 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2549 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2550 return result;
2551}
9291a0cd 2552\f
7b9f3c50
DE
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. */
2560struct quick_file_names
2561{
094b34ac
DE
2562 /* The data used to construct the hash key. */
2563 struct stmt_list_hash hash;
7b9f3c50
DE
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. */
2580struct 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. */
43f3e411 2589 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
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
094b34ac
DE
2600/* Utility hash function for a stmt_list_hash. */
2601
2602static hashval_t
2603hash_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
2615static int
2616eq_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
7b9f3c50
DE
2628/* Hash function for a quick_file_names. */
2629
2630static hashval_t
2631hash_file_name_entry (const void *e)
2632{
9a3c8263
SM
2633 const struct quick_file_names *file_data
2634 = (const struct quick_file_names *) e;
7b9f3c50 2635
094b34ac 2636 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2637}
2638
2639/* Equality function for a quick_file_names. */
2640
2641static int
2642eq_file_name_entry (const void *a, const void *b)
2643{
9a3c8263
SM
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;
7b9f3c50 2646
094b34ac 2647 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2648}
2649
2650/* Delete function for a quick_file_names. */
2651
2652static void
2653delete_file_name_entry (void *e)
2654{
9a3c8263 2655 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
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
2671static htab_t
2672create_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}
9291a0cd 2678
918dd910
JK
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
2683static void
2684load_cu (struct dwarf2_per_cu_data *per_cu)
2685{
3019eac3 2686 if (per_cu->is_debug_types)
e5fe5e75 2687 load_full_type_unit (per_cu);
918dd910 2688 else
95554aad 2689 load_full_comp_unit (per_cu, language_minimal);
918dd910 2690
cc12ce38
DE
2691 if (per_cu->cu == NULL)
2692 return; /* Dummy CU. */
2dc860c0
DE
2693
2694 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2695}
2696
a0f42c21 2697/* Read in the symbols for PER_CU. */
2fdf6df6 2698
9291a0cd 2699static void
a0f42c21 2700dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2701{
2702 struct cleanup *back_to;
2703
f4dc4d17
DE
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
9291a0cd
TT
2709 back_to = make_cleanup (dwarf2_release_queue, NULL);
2710
95554aad 2711 if (dwarf2_per_objfile->using_index
43f3e411 2712 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
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);
89e63ee4
DE
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
cc12ce38 2722 && per_cu->cu != NULL
89e63ee4
DE
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);
95554aad 2729 }
9291a0cd 2730
a0f42c21 2731 process_queue ();
9291a0cd
TT
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. */
2fdf6df6 2743
43f3e411 2744static struct compunit_symtab *
a0f42c21 2745dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2746{
95554aad 2747 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2748 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2749 {
2750 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2751 increment_reading_symtab ();
a0f42c21 2752 dw2_do_instantiate_symtab (per_cu);
95554aad 2753 process_cu_includes ();
9291a0cd
TT
2754 do_cleanups (back_to);
2755 }
f194fefb 2756
43f3e411 2757 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2758}
2759
8832e7e3 2760/* Return the CU/TU given its index.
f4dc4d17
DE
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 {
8832e7e3 2767 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2768
2769 ...;
2770 }
2771*/
2fdf6df6 2772
1fd400ff 2773static struct dwarf2_per_cu_data *
8832e7e3 2774dw2_get_cutu (int index)
1fd400ff
TT
2775{
2776 if (index >= dwarf2_per_objfile->n_comp_units)
2777 {
f4dc4d17 2778 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2779 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2780 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2781 }
2782
2783 return dwarf2_per_objfile->all_comp_units[index];
2784}
2785
8832e7e3
DE
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. */
f4dc4d17
DE
2789
2790static struct dwarf2_per_cu_data *
8832e7e3 2791dw2_get_cu (int index)
f4dc4d17 2792{
8832e7e3 2793 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2794
1fd400ff
TT
2795 return dwarf2_per_objfile->all_comp_units[index];
2796}
2797
2ec9a5e0
TT
2798/* A helper for create_cus_from_index that handles a given list of
2799 CUs. */
2fdf6df6 2800
74a0d9f6 2801static void
2ec9a5e0
TT
2802create_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)
9291a0cd
TT
2807{
2808 offset_type i;
9291a0cd 2809
2ec9a5e0 2810 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2811 {
2812 struct dwarf2_per_cu_data *the_cu;
2813 ULONGEST offset, length;
2814
74a0d9f6
JK
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);
9291a0cd
TT
2818 cu_list += 2 * 8;
2819
2820 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2821 struct dwarf2_per_cu_data);
b64f50a1 2822 the_cu->offset.sect_off = offset;
9291a0cd
TT
2823 the_cu->length = length;
2824 the_cu->objfile = objfile;
8a0459fd 2825 the_cu->section = section;
9291a0cd
TT
2826 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2827 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2828 the_cu->is_dwz = is_dwz;
2829 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2830 }
9291a0cd
TT
2831}
2832
2ec9a5e0 2833/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2834 the CU objects for this objfile. */
2ec9a5e0 2835
74a0d9f6 2836static void
2ec9a5e0
TT
2837create_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;
8d749320
SM
2844 dwarf2_per_objfile->all_comp_units =
2845 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2846 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 2847
74a0d9f6
JK
2848 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2849 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2850
2851 if (dwz_elements == 0)
74a0d9f6 2852 return;
2ec9a5e0
TT
2853
2854 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2855 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2856 cu_list_elements / 2);
2ec9a5e0
TT
2857}
2858
1fd400ff 2859/* Create the signatured type hash table from the index. */
673bfd45 2860
74a0d9f6 2861static void
673bfd45 2862create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2863 struct dwarf2_section_info *section,
673bfd45
DE
2864 const gdb_byte *bytes,
2865 offset_type elements)
1fd400ff
TT
2866{
2867 offset_type i;
673bfd45 2868 htab_t sig_types_hash;
1fd400ff 2869
6aa5f3a6
DE
2870 dwarf2_per_objfile->n_type_units
2871 = dwarf2_per_objfile->n_allocated_type_units
2872 = elements / 3;
8d749320
SM
2873 dwarf2_per_objfile->all_type_units =
2874 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 2875
673bfd45 2876 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2877
2878 for (i = 0; i < elements; i += 3)
2879 {
52dc124a
DE
2880 struct signatured_type *sig_type;
2881 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2882 void **slot;
2883
74a0d9f6
JK
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);
1fd400ff
TT
2888 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2889 bytes += 3 * 8;
2890
52dc124a 2891 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2892 struct signatured_type);
52dc124a 2893 sig_type->signature = signature;
3019eac3
DE
2894 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2895 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2896 sig_type->per_cu.section = section;
52dc124a
DE
2897 sig_type->per_cu.offset.sect_off = offset;
2898 sig_type->per_cu.objfile = objfile;
2899 sig_type->per_cu.v.quick
1fd400ff
TT
2900 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2901 struct dwarf2_per_cu_quick_data);
2902
52dc124a
DE
2903 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2904 *slot = sig_type;
1fd400ff 2905
b4dd5633 2906 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2907 }
2908
673bfd45 2909 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2910}
2911
9291a0cd
TT
2912/* Read the address map data from the mapped index, and use it to
2913 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2914
9291a0cd
TT
2915static void
2916create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2917{
3e29f34a 2918 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd
TT
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;
f652bce2 2943
24a55014 2944 if (lo > hi)
f652bce2 2945 {
24a55014
DE
2946 complaint (&symfile_complaints,
2947 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2948 hex_string (lo), hex_string (hi));
24a55014 2949 continue;
f652bce2 2950 }
24a55014
DE
2951
2952 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2953 {
2954 complaint (&symfile_complaints,
2955 _(".gdb_index address table has invalid CU number %u"),
2956 (unsigned) cu_index);
24a55014 2957 continue;
f652bce2 2958 }
24a55014 2959
3e29f34a
MR
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));
9291a0cd
TT
2963 }
2964
2965 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2966 &objfile->objfile_obstack);
2967 do_cleanups (cleanup);
2968}
2969
59d7bcaf
JK
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
559a7a62
JK
2974 SYMBOL_HASH_NEXT.
2975
2976 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2977
9291a0cd 2978static hashval_t
559a7a62 2979mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
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)
559a7a62
JK
2986 {
2987 if (index_version >= 5)
2988 c = tolower (c);
2989 r = r * 67 + c - 113;
2990 }
9291a0cd
TT
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. */
2fdf6df6 2998
9291a0cd
TT
2999static int
3000find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3001 offset_type **vec_out)
3002{
0cf03b49
JK
3003 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3004 offset_type hash;
9291a0cd 3005 offset_type slot, step;
559a7a62 3006 int (*cmp) (const char *, const char *);
9291a0cd 3007
0cf03b49 3008 if (current_language->la_language == language_cplus
45280282
IB
3009 || current_language->la_language == language_fortran
3010 || current_language->la_language == language_d)
0cf03b49
JK
3011 {
3012 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3013 not contain any. */
a8719064 3014
72998fb3 3015 if (strchr (name, '(') != NULL)
0cf03b49 3016 {
72998fb3 3017 char *without_params = cp_remove_params (name);
0cf03b49 3018
72998fb3
DE
3019 if (without_params != NULL)
3020 {
3021 make_cleanup (xfree, without_params);
3022 name = without_params;
3023 }
0cf03b49
JK
3024 }
3025 }
3026
559a7a62 3027 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3028 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
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
3876f04e
DE
3035 slot = hash & (index->symbol_table_slots - 1);
3036 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3037 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
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;
3876f04e 3044 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3045 {
3046 do_cleanups (back_to);
3047 return 0;
3048 }
9291a0cd 3049
3876f04e 3050 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3051 if (!cmp (name, str))
9291a0cd
TT
3052 {
3053 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3054 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3055 do_cleanups (back_to);
9291a0cd
TT
3056 return 1;
3057 }
3058
3876f04e 3059 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3060 }
3061}
3062
2ec9a5e0
TT
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. */
2fdf6df6 3073
9291a0cd 3074static int
2ec9a5e0
TT
3075read_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)
9291a0cd 3084{
948f8e3d 3085 const gdb_byte *addr;
2ec9a5e0 3086 offset_type version;
b3b272e1 3087 offset_type *metadata;
1fd400ff 3088 int i;
9291a0cd 3089
2ec9a5e0 3090 if (dwarf2_section_empty_p (section))
9291a0cd 3091 return 0;
82430852
JK
3092
3093 /* Older elfutils strip versions could keep the section in the main
3094 executable while splitting it for the separate debug info file. */
a32a8923 3095 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3096 return 0;
3097
2ec9a5e0 3098 dwarf2_read_section (objfile, section);
9291a0cd 3099
2ec9a5e0 3100 addr = section->buffer;
9291a0cd 3101 /* Version check. */
1fd400ff 3102 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3103 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3104 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3105 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3106 indices. */
831adc1f 3107 if (version < 4)
481860b3
GB
3108 {
3109 static int warning_printed = 0;
3110 if (!warning_printed)
3111 {
3112 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3113 filename);
481860b3
GB
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
e615022a
DE
3124 indices unless the user has done
3125 "set use-deprecated-index-sections on". */
2ec9a5e0 3126 if (version < 6 && !deprecated_ok)
481860b3
GB
3127 {
3128 static int warning_printed = 0;
3129 if (!warning_printed)
3130 {
e615022a
DE
3131 warning (_("\
3132Skipping deprecated .gdb_index section in %s.\n\
3133Do \"set use-deprecated-index-sections on\" before the file is read\n\
3134to use the section anyway."),
2ec9a5e0 3135 filename);
481860b3
GB
3136 warning_printed = 1;
3137 }
3138 return 0;
3139 }
796a7ff8 3140 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
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. */
796a7ff8 3147
481860b3 3148 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3149 longer backward compatible. */
796a7ff8 3150 if (version > 8)
594e8718 3151 return 0;
9291a0cd 3152
559a7a62 3153 map->version = version;
2ec9a5e0 3154 map->total_size = section->size;
9291a0cd
TT
3155
3156 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3157
3158 i = 0;
2ec9a5e0
TT
3159 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3160 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3161 / 8);
1fd400ff
TT
3162 ++i;
3163
2ec9a5e0
TT
3164 *types_list = addr + MAYBE_SWAP (metadata[i]);
3165 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3166 - MAYBE_SWAP (metadata[i]))
3167 / 8);
987d643c 3168 ++i;
1fd400ff
TT
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
3876f04e
DE
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)));
1fd400ff 3179 ++i;
9291a0cd 3180
f9d83a0b 3181 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3182
2ec9a5e0
TT
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
3190static int
3191dwarf2_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;
4db1a1dc 3196 struct dwz_file *dwz;
2ec9a5e0 3197
4262abfb 3198 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
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
0fefef59 3205 /* Don't use the index if it's empty. */
2ec9a5e0 3206 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3207 return 0;
3208
2ec9a5e0
TT
3209 /* If there is a .dwz file, read it so we can get its CU list as
3210 well. */
4db1a1dc
TT
3211 dwz = dwarf2_get_dwz_file ();
3212 if (dwz != NULL)
2ec9a5e0 3213 {
2ec9a5e0
TT
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
74a0d9f6
JK
3231 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3232 dwz_list_elements);
1fd400ff 3233
8b70b953
TT
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
74a0d9f6
JK
3246 create_signatured_type_table_from_index (objfile, section, types_list,
3247 types_list_elements);
8b70b953 3248 }
9291a0cd 3249
2ec9a5e0
TT
3250 create_addrmap_from_index (objfile, &local_map);
3251
8d749320 3252 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3253 *map = local_map;
9291a0cd
TT
3254
3255 dwarf2_per_objfile->index_table = map;
3256 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3257 dwarf2_per_objfile->quick_file_names_table =
3258 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3259
3260 return 1;
3261}
3262
3263/* A helper for the "quick" functions which sets the global
3264 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3265
9291a0cd
TT
3266static void
3267dw2_setup (struct objfile *objfile)
3268{
9a3c8263
SM
3269 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3270 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3271 gdb_assert (dwarf2_per_objfile);
3272}
3273
dee91e82 3274/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3275
dee91e82
DE
3276static void
3277dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3278 const gdb_byte *info_ptr,
dee91e82
DE
3279 struct die_info *comp_unit_die,
3280 int has_children,
3281 void *data)
9291a0cd 3282{
dee91e82
DE
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;
094b34ac 3286 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 3287 struct line_header *lh;
9291a0cd 3288 struct attribute *attr;
dee91e82 3289 int i;
15d034d0 3290 const char *name, *comp_dir;
7b9f3c50
DE
3291 void **slot;
3292 struct quick_file_names *qfn;
3293 unsigned int line_offset;
9291a0cd 3294
0186c6a7
DE
3295 gdb_assert (! this_cu->is_debug_types);
3296
07261596
TT
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
0186c6a7 3305 lh_cu = this_cu;
7b9f3c50
DE
3306 lh = NULL;
3307 slot = NULL;
3308 line_offset = 0;
dee91e82
DE
3309
3310 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3311 if (attr)
3312 {
7b9f3c50
DE
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. */
094b34ac
DE
3319 find_entry.hash.dwo_unit = cu->dwo_unit;
3320 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3321 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3322 &find_entry, INSERT);
3323 if (*slot != NULL)
3324 {
9a3c8263 3325 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3326 return;
7b9f3c50
DE
3327 }
3328
3019eac3 3329 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3330 }
3331 if (lh == NULL)
3332 {
094b34ac 3333 lh_cu->v.quick->no_file_data = 1;
dee91e82 3334 return;
9291a0cd
TT
3335 }
3336
8d749320 3337 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac
DE
3338 qfn->hash.dwo_unit = cu->dwo_unit;
3339 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3340 gdb_assert (slot != NULL);
3341 *slot = qfn;
9291a0cd 3342
dee91e82 3343 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 3344
7b9f3c50 3345 qfn->num_file_names = lh->num_file_names;
8d749320
SM
3346 qfn->file_names =
3347 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->num_file_names);
9291a0cd 3348 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
3349 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3350 qfn->real_names = NULL;
9291a0cd 3351
7b9f3c50 3352 free_line_header (lh);
7b9f3c50 3353
094b34ac 3354 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3355}
3356
3357/* A helper for the "quick" functions which attempts to read the line
3358 table for THIS_CU. */
3359
3360static struct quick_file_names *
e4a48d9d 3361dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3362{
0186c6a7
DE
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));
f4dc4d17 3367
dee91e82
DE
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
0186c6a7 3374 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3375
3376 if (this_cu->v.quick->no_file_data)
3377 return NULL;
3378 return this_cu->v.quick->file_names;
9291a0cd
TT
3379}
3380
3381/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3382 real path for a given file name from the line table. */
2fdf6df6 3383
9291a0cd 3384static const char *
7b9f3c50
DE
3385dw2_get_real_path (struct objfile *objfile,
3386 struct quick_file_names *qfn, int index)
9291a0cd 3387{
7b9f3c50
DE
3388 if (qfn->real_names == NULL)
3389 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3390 qfn->num_file_names, const char *);
9291a0cd 3391
7b9f3c50
DE
3392 if (qfn->real_names[index] == NULL)
3393 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3394
7b9f3c50 3395 return qfn->real_names[index];
9291a0cd
TT
3396}
3397
3398static struct symtab *
3399dw2_find_last_source_symtab (struct objfile *objfile)
3400{
43f3e411 3401 struct compunit_symtab *cust;
9291a0cd 3402 int index;
ae2de4f8 3403
9291a0cd
TT
3404 dw2_setup (objfile);
3405 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3406 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3407 if (cust == NULL)
3408 return NULL;
3409 return compunit_primary_filetab (cust);
9291a0cd
TT
3410}
3411
7b9f3c50
DE
3412/* Traversal function for dw2_forget_cached_source_info. */
3413
3414static int
3415dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3416{
7b9f3c50 3417 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3418
7b9f3c50 3419 if (file_data->real_names)
9291a0cd 3420 {
7b9f3c50 3421 int i;
9291a0cd 3422
7b9f3c50 3423 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3424 {
7b9f3c50
DE
3425 xfree ((void*) file_data->real_names[i]);
3426 file_data->real_names[i] = NULL;
9291a0cd
TT
3427 }
3428 }
7b9f3c50
DE
3429
3430 return 1;
3431}
3432
3433static void
3434dw2_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);
9291a0cd
TT
3440}
3441
f8eba3c6
TT
3442/* Helper function for dw2_map_symtabs_matching_filename that expands
3443 the symtabs and calls the iterator. */
3444
3445static int
3446dw2_map_expand_apply (struct objfile *objfile,
3447 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3448 const char *name, const char *real_path,
f8eba3c6
TT
3449 int (*callback) (struct symtab *, void *),
3450 void *data)
3451{
43f3e411 3452 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3453
3454 /* Don't visit already-expanded CUs. */
43f3e411 3455 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3456 return 0;
3457
3458 /* This may expand more than one symtab, and we want to iterate over
3459 all of them. */
a0f42c21 3460 dw2_instantiate_symtab (per_cu);
f8eba3c6 3461
f5b95b50 3462 return iterate_over_some_symtabs (name, real_path, callback, data,
43f3e411 3463 objfile->compunit_symtabs, last_made);
f8eba3c6
TT
3464}
3465
3466/* Implementation of the map_symtabs_matching_filename method. */
3467
9291a0cd 3468static int
f8eba3c6 3469dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3470 const char *real_path,
f8eba3c6
TT
3471 int (*callback) (struct symtab *, void *),
3472 void *data)
9291a0cd
TT
3473{
3474 int i;
c011a4f4 3475 const char *name_basename = lbasename (name);
9291a0cd
TT
3476
3477 dw2_setup (objfile);
ae2de4f8 3478
848e3e78
DE
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. */
f4dc4d17 3481
848e3e78 3482 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3483 {
3484 int j;
8832e7e3 3485 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3486 struct quick_file_names *file_data;
9291a0cd 3487
3d7bb9d9 3488 /* We only need to look at symtabs not already expanded. */
43f3e411 3489 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3490 continue;
3491
e4a48d9d 3492 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3493 if (file_data == NULL)
9291a0cd
TT
3494 continue;
3495
7b9f3c50 3496 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3497 {
7b9f3c50 3498 const char *this_name = file_data->file_names[j];
da235a7c 3499 const char *this_real_name;
9291a0cd 3500
af529f8f 3501 if (compare_filenames_for_search (this_name, name))
9291a0cd 3502 {
f5b95b50 3503 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3504 callback, data))
3505 return 1;
288e77a7 3506 continue;
4aac40c8 3507 }
9291a0cd 3508
c011a4f4
DE
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
da235a7c
JK
3515 this_real_name = dw2_get_real_path (objfile, file_data, j);
3516 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3517 {
da235a7c
JK
3518 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3519 callback, data))
3520 return 1;
288e77a7 3521 continue;
da235a7c 3522 }
9291a0cd 3523
da235a7c
JK
3524 if (real_path != NULL)
3525 {
af529f8f
JK
3526 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3527 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3528 if (this_real_name != NULL
af529f8f 3529 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3530 {
f5b95b50 3531 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3532 callback, data))
3533 return 1;
288e77a7 3534 continue;
9291a0cd
TT
3535 }
3536 }
3537 }
3538 }
3539
9291a0cd
TT
3540 return 0;
3541}
3542
da51c347
DE
3543/* Struct used to manage iterating over all CUs looking for a symbol. */
3544
3545struct dw2_symtab_iterator
9291a0cd 3546{
da51c347
DE
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;
8943b874
DE
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;
da51c347 3568};
9291a0cd 3569
da51c347
DE
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. */
2fdf6df6 3573
9291a0cd 3574static void
da51c347
DE
3575dw2_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;
8943b874 3587 iter->global_seen = 0;
da51c347
DE
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
3600static struct dwarf2_per_cu_data *
3601dw2_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);
3190f0c6 3608 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
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
3190f0c6
DE
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"
4262abfb
JK
3628 " [in module %s]"),
3629 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3630 continue;
3631 }
3632
8832e7e3 3633 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3634
da51c347 3635 /* Skip if already read in. */
43f3e411 3636 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3637 continue;
3638
8943b874
DE
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 }
da51c347
DE
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
43f3e411 3684static struct compunit_symtab *
da51c347
DE
3685dw2_lookup_symbol (struct objfile *objfile, int block_index,
3686 const char *name, domain_enum domain)
9291a0cd 3687{
43f3e411 3688 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3689 struct mapped_index *index;
3690
9291a0cd
TT
3691 dw2_setup (objfile);
3692
156942c7
DE
3693 index = dwarf2_per_objfile->index_table;
3694
da51c347 3695 /* index is NULL if OBJF_READNOW. */
156942c7 3696 if (index)
9291a0cd 3697 {
da51c347
DE
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);
9291a0cd 3702
da51c347 3703 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3704 {
b2e2f908 3705 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3706 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3707 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3708 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3709
b2e2f908
DE
3710 sym = block_find_symbol (block, name, domain,
3711 block_find_non_opaque_type_preferred,
3712 &with_opaque);
3713
da51c347
DE
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). */
da51c347 3717
b2e2f908
DE
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;
da51c347
DE
3724
3725 /* Keep looking through other CUs. */
9291a0cd
TT
3726 }
3727 }
9291a0cd 3728
da51c347 3729 return stab_best;
9291a0cd
TT
3730}
3731
3732static void
3733dw2_print_stats (struct objfile *objfile)
3734{
e4a48d9d 3735 int i, total, count;
9291a0cd
TT
3736
3737 dw2_setup (objfile);
e4a48d9d 3738 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3739 count = 0;
e4a48d9d 3740 for (i = 0; i < total; ++i)
9291a0cd 3741 {
8832e7e3 3742 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3743
43f3e411 3744 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3745 ++count;
3746 }
e4a48d9d 3747 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3748 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3749}
3750
779bd270
DE
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
9291a0cd
TT
3756static void
3757dw2_dump (struct objfile *objfile)
3758{
779bd270
DE
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");
9291a0cd
TT
3770}
3771
3772static void
3189cb12
DE
3773dw2_relocate (struct objfile *objfile,
3774 const struct section_offsets *new_offsets,
3775 const struct section_offsets *delta)
9291a0cd
TT
3776{
3777 /* There's nothing to relocate here. */
3778}
3779
3780static void
3781dw2_expand_symtabs_for_function (struct objfile *objfile,
3782 const char *func_name)
3783{
da51c347
DE
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 }
9291a0cd
TT
3803}
3804
3805static void
3806dw2_expand_all_symtabs (struct objfile *objfile)
3807{
3808 int i;
3809
3810 dw2_setup (objfile);
1fd400ff
TT
3811
3812 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3813 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3814 {
8832e7e3 3815 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3816
a0f42c21 3817 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3818 }
3819}
3820
3821static void
652a8996
JK
3822dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3823 const char *fullname)
9291a0cd
TT
3824{
3825 int i;
3826
3827 dw2_setup (objfile);
d4637a04
DE
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)
9291a0cd
TT
3835 {
3836 int j;
8832e7e3 3837 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 3838 struct quick_file_names *file_data;
9291a0cd 3839
3d7bb9d9 3840 /* We only need to look at symtabs not already expanded. */
43f3e411 3841 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3842 continue;
3843
e4a48d9d 3844 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3845 if (file_data == NULL)
9291a0cd
TT
3846 continue;
3847
7b9f3c50 3848 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3849 {
652a8996
JK
3850 const char *this_fullname = file_data->file_names[j];
3851
3852 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3853 {
a0f42c21 3854 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3855 break;
3856 }
3857 }
3858 }
3859}
3860
9291a0cd 3861static void
ade7ed9e 3862dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 3863 const char * name, domain_enum domain,
ade7ed9e 3864 int global,
40658b94
PH
3865 int (*callback) (struct block *,
3866 struct symbol *, void *),
2edb89d3
JK
3867 void *data, symbol_compare_ftype *match,
3868 symbol_compare_ftype *ordered_compare)
9291a0cd 3869{
40658b94 3870 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
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. */
9291a0cd
TT
3873}
3874
3875static void
f8eba3c6
TT
3876dw2_expand_symtabs_matching
3877 (struct objfile *objfile,
206f2a57
DE
3878 expand_symtabs_file_matcher_ftype *file_matcher,
3879 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
276d885b 3880 expand_symtabs_exp_notify_ftype *expansion_notify,
f8eba3c6
TT
3881 enum search_domain kind,
3882 void *data)
9291a0cd
TT
3883{
3884 int i;
3885 offset_type iter;
4b5246aa 3886 struct mapped_index *index;
9291a0cd
TT
3887
3888 dw2_setup (objfile);
ae2de4f8
DE
3889
3890 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3891 if (!dwarf2_per_objfile->index_table)
3892 return;
4b5246aa 3893 index = dwarf2_per_objfile->index_table;
9291a0cd 3894
7b08b9eb 3895 if (file_matcher != NULL)
24c79950 3896 {
fc4007c9
TT
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));
24c79950 3903
848e3e78
DE
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)
24c79950
TT
3908 {
3909 int j;
8832e7e3 3910 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
3911 struct quick_file_names *file_data;
3912 void **slot;
7b08b9eb 3913
61d96d7e
DE
3914 QUIT;
3915
24c79950 3916 per_cu->v.quick->mark = 0;
3d7bb9d9 3917
24c79950 3918 /* We only need to look at symtabs not already expanded. */
43f3e411 3919 if (per_cu->v.quick->compunit_symtab)
24c79950 3920 continue;
7b08b9eb 3921
e4a48d9d 3922 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3923 if (file_data == NULL)
3924 continue;
7b08b9eb 3925
fc4007c9 3926 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 3927 continue;
fc4007c9 3928 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
3929 {
3930 per_cu->v.quick->mark = 1;
3931 continue;
3932 }
3933
3934 for (j = 0; j < file_data->num_file_names; ++j)
3935 {
da235a7c
JK
3936 const char *this_real_name;
3937
fbd9ab74 3938 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3939 {
3940 per_cu->v.quick->mark = 1;
3941 break;
3942 }
da235a7c
JK
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 }
24c79950
TT
3957 }
3958
3959 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
3960 ? visited_found.get ()
3961 : visited_not_found.get (),
24c79950
TT
3962 file_data, INSERT);
3963 *slot = file_data;
3964 }
24c79950 3965 }
9291a0cd 3966
3876f04e 3967 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3968 {
3969 offset_type idx = 2 * iter;
3970 const char *name;
3971 offset_type *vec, vec_len, vec_idx;
8943b874 3972 int global_seen = 0;
9291a0cd 3973
61d96d7e
DE
3974 QUIT;
3975
3876f04e 3976 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3977 continue;
3978
3876f04e 3979 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3980
206f2a57 3981 if (! (*symbol_matcher) (name, data))
9291a0cd
TT
3982 continue;
3983
3984 /* The name was matched, now expand corresponding CUs that were
3985 marked. */
4b5246aa 3986 vec = (offset_type *) (index->constant_pool
3876f04e 3987 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3988 vec_len = MAYBE_SWAP (vec[0]);
3989 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3990 {
e254ef6a 3991 struct dwarf2_per_cu_data *per_cu;
156942c7 3992 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
3993 /* This value is only valid for index versions >= 7. */
3994 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
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);
3190f0c6
DE
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
8943b874
DE
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
3190f0c6
DE
4015 /* Only check the symbol's kind if it has one. */
4016 if (attrs_valid)
156942c7
DE
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
3190f0c6
DE
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"
4262abfb 4043 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4044 continue;
4045 }
4046
8832e7e3 4047 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4048 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
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 }
9291a0cd
TT
4063 }
4064 }
4065}
4066
43f3e411 4067/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4068 symtab. */
4069
43f3e411
DE
4070static struct compunit_symtab *
4071recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4072 CORE_ADDR pc)
9703b513
TT
4073{
4074 int i;
4075
43f3e411
DE
4076 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4077 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4078 return cust;
9703b513 4079
43f3e411 4080 if (cust->includes == NULL)
a3ec0bb1
DE
4081 return NULL;
4082
43f3e411 4083 for (i = 0; cust->includes[i]; ++i)
9703b513 4084 {
43f3e411 4085 struct compunit_symtab *s = cust->includes[i];
9703b513 4086
43f3e411 4087 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4088 if (s != NULL)
4089 return s;
4090 }
4091
4092 return NULL;
4093}
4094
43f3e411
DE
4095static struct compunit_symtab *
4096dw2_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)
9291a0cd
TT
4101{
4102 struct dwarf2_per_cu_data *data;
43f3e411 4103 struct compunit_symtab *result;
9291a0cd
TT
4104
4105 dw2_setup (objfile);
4106
4107 if (!objfile->psymtabs_addrmap)
4108 return NULL;
4109
9a3c8263
SM
4110 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4111 pc);
9291a0cd
TT
4112 if (!data)
4113 return NULL;
4114
43f3e411 4115 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4116 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4117 paddress (get_objfile_arch (objfile), pc));
4118
43f3e411
DE
4119 result
4120 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4121 pc);
9703b513
TT
4122 gdb_assert (result != NULL);
4123 return result;
9291a0cd
TT
4124}
4125
9291a0cd 4126static void
44b13c5a 4127dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4128 void *data, int need_fullname)
9291a0cd
TT
4129{
4130 int i;
fc4007c9
TT
4131 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4132 NULL, xcalloc, xfree));
9291a0cd
TT
4133
4134 dw2_setup (objfile);
ae2de4f8 4135
848e3e78
DE
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. */
f4dc4d17 4139
848e3e78 4140 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950 4141 {
8832e7e3 4142 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
24c79950 4143
43f3e411 4144 if (per_cu->v.quick->compunit_symtab)
24c79950 4145 {
fc4007c9
TT
4146 void **slot = htab_find_slot (visited.get (),
4147 per_cu->v.quick->file_names,
24c79950
TT
4148 INSERT);
4149
4150 *slot = per_cu->v.quick->file_names;
4151 }
4152 }
4153
848e3e78 4154 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4155 {
4156 int j;
8832e7e3 4157 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 4158 struct quick_file_names *file_data;
24c79950 4159 void **slot;
9291a0cd 4160
3d7bb9d9 4161 /* We only need to look at symtabs not already expanded. */
43f3e411 4162 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4163 continue;
4164
e4a48d9d 4165 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4166 if (file_data == NULL)
9291a0cd
TT
4167 continue;
4168
fc4007c9 4169 slot = htab_find_slot (visited.get (), file_data, INSERT);
24c79950
TT
4170 if (*slot)
4171 {
4172 /* Already visited. */
4173 continue;
4174 }
4175 *slot = file_data;
4176
7b9f3c50 4177 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4178 {
74e2f255
DE
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;
7b9f3c50 4185 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
4186 }
4187 }
4188}
4189
4190static int
4191dw2_has_symbols (struct objfile *objfile)
4192{
4193 return 1;
4194}
4195
4196const 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,
f8eba3c6 4201 dw2_map_symtabs_matching_filename,
9291a0cd 4202 dw2_lookup_symbol,
9291a0cd
TT
4203 dw2_print_stats,
4204 dw2_dump,
4205 dw2_relocate,
4206 dw2_expand_symtabs_for_function,
4207 dw2_expand_all_symtabs,
652a8996 4208 dw2_expand_symtabs_with_fullname,
40658b94 4209 dw2_map_matching_symbols,
9291a0cd 4210 dw2_expand_symtabs_matching,
43f3e411 4211 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
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
4218int
4219dwarf2_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);
0e50663e 4231 create_all_type_units (objfile);
7b9f3c50
DE
4232 dwarf2_per_objfile->quick_file_names_table =
4233 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4234
1fd400ff 4235 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4236 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4237 {
8832e7e3 4238 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4239
e254ef6a
DE
4240 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4241 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
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
9291a0cd
TT
4253 return 0;
4254}
4255
4256\f
4257
dce234bc
PP
4258/* Build a partial symbol table. */
4259
4260void
f29dff0a 4261dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4262{
c9bf0622 4263
f29dff0a 4264 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4265 {
4266 init_psymbol_list (objfile, 1024);
4267 }
4268
492d29ea 4269 TRY
c9bf0622
TT
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. */
906768f9 4274 psymtab_discarder psymtabs (objfile);
c9bf0622 4275 dwarf2_build_psymtabs_hard (objfile);
906768f9 4276 psymtabs.keep ();
c9bf0622 4277 }
492d29ea
PA
4278 CATCH (except, RETURN_MASK_ERROR)
4279 {
4280 exception_print (gdb_stderr, except);
4281 }
4282 END_CATCH
c906108c 4283}
c906108c 4284
1ce1cefd
DE
4285/* Return the total length of the CU described by HEADER. */
4286
4287static unsigned int
4288get_cu_length (const struct comp_unit_head *header)
4289{
4290 return header->initial_length_size + header->length;
4291}
4292
45452591
DE
4293/* Return TRUE if OFFSET is within CU_HEADER. */
4294
4295static inline int
b64f50a1 4296offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 4297{
b64f50a1 4298 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 4299 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 4300
b64f50a1 4301 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
4302}
4303
3b80fe9b
DE
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
4310static void
4311dwarf2_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 {
31aa7e4e 4321 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4322 cu->base_known = 1;
4323 }
4324 else
4325 {
4326 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4327 if (attr)
4328 {
31aa7e4e 4329 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4330 cu->base_known = 1;
4331 }
4332 }
4333}
4334
93311388
DE
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. */
107d2387 4338
d521ce57 4339static const gdb_byte *
107d2387 4340read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 4341 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
4342{
4343 int signed_addr;
891d2f0b 4344 unsigned int bytes_read;
c764a876
DE
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;
613e1657 4349 info_ptr += bytes_read;
107d2387
AC
4350 cu_header->version = read_2_bytes (abfd, info_ptr);
4351 info_ptr += 2;
b64f50a1
JK
4352 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4353 &bytes_read);
613e1657 4354 info_ptr += bytes_read;
107d2387
AC
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)
8e65ff28 4359 internal_error (__FILE__, __LINE__,
e2e0b3e5 4360 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4361 cu_header->signed_addr_p = signed_addr;
c764a876 4362
107d2387
AC
4363 return info_ptr;
4364}
4365
36586728
TT
4366/* Helper function that returns the proper abbrev section for
4367 THIS_CU. */
4368
4369static struct dwarf2_section_info *
4370get_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
9ff913ba
DE
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
4386static void
4387error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4388 struct dwarf2_section_info *section,
4389 struct dwarf2_section_info *abbrev_section)
9ff913ba 4390{
a32a8923 4391 const char *filename = get_section_file_name (section);
9ff913ba
DE
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
b64f50a1 4398 if (header->abbrev_offset.sect_off
36586728 4399 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4400 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4401 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4402 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4403 filename);
4404
4405 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4406 avoid potential 32-bit overflow. */
1ce1cefd 4407 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4408 > section->size)
4409 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4410 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4411 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
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. */
adabb602 4418
d521ce57 4419static const gdb_byte *
9ff913ba
DE
4420read_and_check_comp_unit_head (struct comp_unit_head *header,
4421 struct dwarf2_section_info *section,
4bdcc0c1 4422 struct dwarf2_section_info *abbrev_section,
d521ce57 4423 const gdb_byte *info_ptr,
9ff913ba 4424 int is_debug_types_section)
72bf9492 4425{
d521ce57 4426 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4427 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4428
b64f50a1 4429 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4430
72bf9492
DJ
4431 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4432
460c1c54
CC
4433 /* If we're reading a type unit, skip over the signature and
4434 type_offset fields. */
b0df02fd 4435 if (is_debug_types_section)
460c1c54
CC
4436 info_ptr += 8 /*signature*/ + header->offset_size;
4437
b64f50a1 4438 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4439
4bdcc0c1 4440 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4441
4442 return info_ptr;
4443}
4444
348e048f
DE
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
d521ce57 4448static const gdb_byte *
9ff913ba
DE
4449read_and_check_type_unit_head (struct comp_unit_head *header,
4450 struct dwarf2_section_info *section,
4bdcc0c1 4451 struct dwarf2_section_info *abbrev_section,
d521ce57 4452 const gdb_byte *info_ptr,
dee91e82
DE
4453 ULONGEST *signature,
4454 cu_offset *type_offset_in_tu)
348e048f 4455{
d521ce57 4456 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4457 bfd *abfd = get_section_bfd_owner (section);
348e048f 4458
b64f50a1 4459 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4460
9ff913ba 4461 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4462
9ff913ba
DE
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;
dee91e82
DE
4468 if (type_offset_in_tu != NULL)
4469 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4470 header->offset_size);
9ff913ba
DE
4471 info_ptr += header->offset_size;
4472
b64f50a1 4473 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4474
4bdcc0c1 4475 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4476
4477 return info_ptr;
348e048f
DE
4478}
4479
f4dc4d17
DE
4480/* Fetch the abbreviation table offset from a comp or type unit header. */
4481
4482static sect_offset
4483read_abbrev_offset (struct dwarf2_section_info *section,
4484 sect_offset offset)
4485{
a32a8923 4486 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4487 const gdb_byte *info_ptr;
ac298888 4488 unsigned int initial_length_size, offset_size;
f4dc4d17
DE
4489 sect_offset abbrev_offset;
4490
4491 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4492 info_ptr = section->buffer + offset.sect_off;
ac298888 4493 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17
DE
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
aaa75496
JB
4500/* Allocate a new partial symtab for file named NAME and mark this new
4501 partial symtab as being an include of PST. */
4502
4503static void
d521ce57 4504dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4505 struct objfile *objfile)
4506{
4507 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4508
fbd9ab74
JK
4509 if (!IS_ABSOLUTE_PATH (subpst->filename))
4510 {
4511 /* It shares objfile->objfile_obstack. */
4512 subpst->dirname = pst->dirname;
4513 }
4514
aaa75496
JB
4515 subpst->textlow = 0;
4516 subpst->texthigh = 0;
4517
8d749320
SM
4518 subpst->dependencies
4519 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
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;
43f3e411 4527 subpst->compunit_symtab = NULL;
aaa75496
JB
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
10b3939b 4533 the regular ones. */
58a9656e 4534 subpst->read_symtab_private = NULL;
aaa75496
JB
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
d85a05f0 4539 partial symtab for each of these included files. */
aaa75496
JB
4540
4541static void
4542dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4543 struct die_info *die,
4544 struct partial_symtab *pst)
aaa75496 4545{
d85a05f0
DJ
4546 struct line_header *lh = NULL;
4547 struct attribute *attr;
aaa75496 4548
d85a05f0
DJ
4549 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4550 if (attr)
3019eac3 4551 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4552 if (lh == NULL)
4553 return; /* No linetable, so no includes. */
4554
c6da4cef 4555 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
527f3840 4556 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4557
4558 free_line_header (lh);
4559}
4560
348e048f 4561static hashval_t
52dc124a 4562hash_signatured_type (const void *item)
348e048f 4563{
9a3c8263
SM
4564 const struct signatured_type *sig_type
4565 = (const struct signatured_type *) item;
9a619af0 4566
348e048f 4567 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4568 return sig_type->signature;
348e048f
DE
4569}
4570
4571static int
52dc124a 4572eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4573{
9a3c8263
SM
4574 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4575 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4576
348e048f
DE
4577 return lhs->signature == rhs->signature;
4578}
4579
1fd400ff
TT
4580/* Allocate a hash table for signatured types. */
4581
4582static htab_t
673bfd45 4583allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4584{
4585 return htab_create_alloc_ex (41,
52dc124a
DE
4586 hash_signatured_type,
4587 eq_signatured_type,
1fd400ff
TT
4588 NULL,
4589 &objfile->objfile_obstack,
4590 hashtab_obstack_allocate,
4591 dummy_obstack_deallocate);
4592}
4593
d467dd73 4594/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4595
4596static int
d467dd73 4597add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4598{
9a3c8263
SM
4599 struct signatured_type *sigt = (struct signatured_type *) *slot;
4600 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4601
b4dd5633 4602 **datap = sigt;
1fd400ff
TT
4603 ++*datap;
4604
4605 return 1;
4606}
4607
c88ee1f0
DE
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. */
348e048f 4616
3019eac3
DE
4617static htab_t
4618create_debug_types_hash_table (struct dwo_file *dwo_file,
4619 VEC (dwarf2_section_info_def) *types)
348e048f 4620{
3019eac3 4621 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4622 htab_t types_htab = NULL;
8b70b953
TT
4623 int ix;
4624 struct dwarf2_section_info *section;
4bdcc0c1 4625 struct dwarf2_section_info *abbrev_section;
348e048f 4626
3019eac3
DE
4627 if (VEC_empty (dwarf2_section_info_def, types))
4628 return NULL;
348e048f 4629
4bdcc0c1
DE
4630 abbrev_section = (dwo_file != NULL
4631 ? &dwo_file->sections.abbrev
4632 : &dwarf2_per_objfile->abbrev);
4633
b4f54984 4634 if (dwarf_read_debug)
09406207
DE
4635 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4636 dwo_file ? ".dwo" : "",
a32a8923 4637 get_section_file_name (abbrev_section));
09406207 4638
8b70b953 4639 for (ix = 0;
3019eac3 4640 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4641 ++ix)
4642 {
3019eac3 4643 bfd *abfd;
d521ce57 4644 const gdb_byte *info_ptr, *end_ptr;
348e048f 4645
8b70b953
TT
4646 dwarf2_read_section (objfile, section);
4647 info_ptr = section->buffer;
348e048f 4648
8b70b953
TT
4649 if (info_ptr == NULL)
4650 continue;
348e048f 4651
3019eac3 4652 /* We can't set abfd until now because the section may be empty or
a32a8923
DE
4653 not present, in which case the bfd is unknown. */
4654 abfd = get_section_bfd_owner (section);
3019eac3 4655
dee91e82
DE
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. */
8b70b953
TT
4659
4660 end_ptr = info_ptr + section->size;
4661 while (info_ptr < end_ptr)
4662 {
b64f50a1 4663 sect_offset offset;
3019eac3 4664 cu_offset type_offset_in_tu;
8b70b953 4665 ULONGEST signature;
52dc124a 4666 struct signatured_type *sig_type;
3019eac3 4667 struct dwo_unit *dwo_tu;
8b70b953 4668 void **slot;
d521ce57 4669 const gdb_byte *ptr = info_ptr;
9ff913ba 4670 struct comp_unit_head header;
dee91e82 4671 unsigned int length;
348e048f 4672
b64f50a1 4673 offset.sect_off = ptr - section->buffer;
348e048f 4674
8b70b953 4675 /* We need to read the type's signature in order to build the hash
9ff913ba 4676 table, but we don't need anything else just yet. */
348e048f 4677
4bdcc0c1
DE
4678 ptr = read_and_check_type_unit_head (&header, section,
4679 abbrev_section, ptr,
3019eac3 4680 &signature, &type_offset_in_tu);
6caca83c 4681
1ce1cefd 4682 length = get_cu_length (&header);
dee91e82 4683
6caca83c 4684 /* Skip dummy type units. */
dee91e82
DE
4685 if (ptr >= info_ptr + length
4686 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4687 {
1ce1cefd 4688 info_ptr += length;
6caca83c
CC
4689 continue;
4690 }
8b70b953 4691
0349ea22
DE
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
3019eac3
DE
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;
8a0459fd 4708 dwo_tu->section = section;
3019eac3
DE
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;
8a0459fd 4723 sig_type->per_cu.section = section;
3019eac3
DE
4724 sig_type->per_cu.offset = offset;
4725 sig_type->per_cu.length = length;
4726 }
8b70b953 4727
3019eac3
DE
4728 slot = htab_find_slot (types_htab,
4729 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4730 INSERT);
8b70b953
TT
4731 gdb_assert (slot != NULL);
4732 if (*slot != NULL)
4733 {
3019eac3
DE
4734 sect_offset dup_offset;
4735
4736 if (dwo_file)
4737 {
9a3c8263
SM
4738 const struct dwo_unit *dup_tu
4739 = (const struct dwo_unit *) *slot;
3019eac3
DE
4740
4741 dup_offset = dup_tu->offset;
4742 }
4743 else
4744 {
9a3c8263
SM
4745 const struct signatured_type *dup_tu
4746 = (const struct signatured_type *) *slot;
3019eac3
DE
4747
4748 dup_offset = dup_tu->per_cu.offset;
4749 }
b3c8eb43 4750
8b70b953 4751 complaint (&symfile_complaints,
c88ee1f0 4752 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4753 " the entry at offset 0x%x, signature %s"),
3019eac3 4754 offset.sect_off, dup_offset.sect_off,
4031ecc5 4755 hex_string (signature));
8b70b953 4756 }
3019eac3 4757 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4758
b4f54984 4759 if (dwarf_read_debug > 1)
4031ecc5 4760 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4761 offset.sect_off,
4031ecc5 4762 hex_string (signature));
348e048f 4763
dee91e82 4764 info_ptr += length;
8b70b953 4765 }
348e048f
DE
4766 }
4767
3019eac3
DE
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
4776static int
4777create_all_type_units (struct objfile *objfile)
4778{
4779 htab_t types_htab;
b4dd5633 4780 struct signatured_type **iter;
3019eac3
DE
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
348e048f
DE
4789 dwarf2_per_objfile->signatured_types = types_htab;
4790
6aa5f3a6
DE
4791 dwarf2_per_objfile->n_type_units
4792 = dwarf2_per_objfile->n_allocated_type_units
4793 = htab_elements (types_htab);
8d749320
SM
4794 dwarf2_per_objfile->all_type_units =
4795 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
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);
1fd400ff 4800
348e048f
DE
4801 return 1;
4802}
4803
6aa5f3a6
DE
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
4808static struct signatured_type *
4809add_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
224c3ddb
SM
4823 = XRESIZEVEC (struct signatured_type *,
4824 dwarf2_per_objfile->all_type_units,
4825 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
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
a2ce51a0
DE
4853/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4854 Fill in SIG_ENTRY with DWO_ENTRY. */
4855
4856static void
4857fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4858 struct signatured_type *sig_entry,
4859 struct dwo_unit *dwo_entry)
4860{
7ee85ab1 4861 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4862 gdb_assert (! sig_entry->per_cu.queued);
4863 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
4864 if (dwarf2_per_objfile->using_index)
4865 {
4866 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 4867 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
4868 }
4869 else
4870 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0
DE
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);
7ee85ab1
DE
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;
a2ce51a0
DE
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.
7ee85ab1
DE
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.
a2ce51a0
DE
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
4897static struct signatured_type *
4898lookup_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;
6aa5f3a6 4904 void **slot;
a2ce51a0
DE
4905
4906 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4907
6aa5f3a6
DE
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 }
a2ce51a0
DE
4915
4916 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
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. */
a2ce51a0 4921
a2ce51a0 4922 find_sig_entry.signature = sig;
6aa5f3a6
DE
4923 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4924 &find_sig_entry, INSERT);
9a3c8263 4925 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
4926
4927 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
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)
a2ce51a0
DE
4937 return sig_entry;
4938
6aa5f3a6
DE
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
a2ce51a0
DE
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;
9a3c8263 4947 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
4948 if (dwo_entry == NULL)
4949 return NULL;
4950
6aa5f3a6
DE
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
a2ce51a0 4955 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 4956 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
4957 return sig_entry;
4958}
4959
a2ce51a0
DE
4960/* Subroutine of lookup_signatured_type.
4961 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
4962 then try the DWP file. If the TU stub (skeleton) has been removed then
4963 it won't be in .gdb_index. */
a2ce51a0
DE
4964
4965static struct signatured_type *
4966lookup_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;
6aa5f3a6 4972 void **slot;
a2ce51a0
DE
4973
4974 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4975 gdb_assert (dwp_file != NULL);
4976
6aa5f3a6
DE
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)
a2ce51a0 4980 {
6aa5f3a6
DE
4981 dwarf2_per_objfile->signatured_types
4982 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
4983 }
4984
6aa5f3a6
DE
4985 find_sig_entry.signature = sig;
4986 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4987 &find_sig_entry, INSERT);
9a3c8263 4988 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
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
a2ce51a0
DE
4996 if (dwp_file->tus == NULL)
4997 return NULL;
57d63ce2
DE
4998 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
4999 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5000 if (dwo_entry == NULL)
5001 return NULL;
5002
6aa5f3a6 5003 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5004 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5005
a2ce51a0
DE
5006 return sig_entry;
5007}
5008
380bca97 5009/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5010 Returns NULL if signature SIG is not present in the table.
5011 It is up to the caller to complain about this. */
348e048f
DE
5012
5013static struct signatured_type *
a2ce51a0 5014lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5015{
a2ce51a0
DE
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;
348e048f 5029
a2ce51a0
DE
5030 if (dwarf2_per_objfile->signatured_types == NULL)
5031 return NULL;
5032 find_entry.signature = sig;
9a3c8263
SM
5033 entry = ((struct signatured_type *)
5034 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5035 return entry;
5036 }
348e048f 5037}
42e7ad6c
DE
5038\f
5039/* Low level DIE reading support. */
348e048f 5040
d85a05f0
DJ
5041/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5042
5043static void
5044init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5045 struct dwarf2_cu *cu,
3019eac3
DE
5046 struct dwarf2_section_info *section,
5047 struct dwo_file *dwo_file)
d85a05f0 5048{
fceca515 5049 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5050 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5051 reader->cu = cu;
3019eac3 5052 reader->dwo_file = dwo_file;
dee91e82
DE
5053 reader->die_section = section;
5054 reader->buffer = section->buffer;
f664829e 5055 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5056 reader->comp_dir = NULL;
d85a05f0
DJ
5057}
5058
b0c7bfa9
DE
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.
a2ce51a0
DE
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
c54a1dd8
DE
5068 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5069 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
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
5076static int
5077read_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,
a2ce51a0 5081 const char *stub_comp_dir,
b0c7bfa9 5082 struct die_reader_specs *result_reader,
d521ce57 5083 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
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;
d521ce57 5091 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
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
b0aeadb3
DE
5099 /* At most one of these may be provided. */
5100 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5101
b0c7bfa9
DE
5102 /* These attributes aren't processed until later:
5103 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
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. */
b0c7bfa9
DE
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 }
a2ce51a0
DE
5141 else if (stub_comp_dir != NULL)
5142 {
5143 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5144 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
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 }
b0c7bfa9
DE
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);
a32a8923 5155 abfd = get_section_bfd_owner (section);
b0c7bfa9
DE
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);
a2ce51a0
DE
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 }
b0c7bfa9
DE
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
02142a6c
DE
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. */
b0c7bfa9
DE
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);
02142a6c 5210 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
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);
02142a6c 5217 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
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
b4f54984 5247 if (dwarf_die_debug)
bf6af496
DE
5248 {
5249 fprintf_unfiltered (gdb_stdlog,
5250 "Read die from %s@0x%x of %s:\n",
a32a8923 5251 get_section_name (section),
bf6af496
DE
5252 (unsigned) (begin_info_ptr - section->buffer),
5253 bfd_get_filename (abfd));
b4f54984 5254 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5255 }
5256
a2ce51a0
DE
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
b0c7bfa9
DE
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.
6a506a2d 5275 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5276
5277static struct dwo_unit *
5278lookup_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
a2ce51a0
DE
5287 gdb_assert (cu != NULL);
5288
b0c7bfa9 5289 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
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);
b0c7bfa9
DE
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]"),
4262abfb 5311 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5312 signature = DW_UNSND (attr);
5313 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5314 signature);
5315 }
5316
b0c7bfa9
DE
5317 return dwo_unit;
5318}
5319
a2ce51a0 5320/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
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. */
a2ce51a0
DE
5328
5329static void
6aa5f3a6
DE
5330init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5331 int use_existing_cu, int keep,
a2ce51a0
DE
5332 die_reader_func_ftype *die_reader_func,
5333 void *data)
5334{
5335 struct dwarf2_cu *cu;
5336 struct signatured_type *sig_type;
6aa5f3a6 5337 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
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
6aa5f3a6
DE
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);
8d749320 5362 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
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. */
a2ce51a0
DE
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
6aa5f3a6 5387 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
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. */
6aa5f3a6 5391 if (free_cu_cleanup != NULL)
a2ce51a0 5392 {
6aa5f3a6
DE
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);
a2ce51a0 5398
6aa5f3a6
DE
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);
a2ce51a0 5402
6aa5f3a6
DE
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);
a2ce51a0 5409 }
a2ce51a0
DE
5410
5411 do_cleanups (cleanups);
5412}
5413
fd820528 5414/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5415 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5416
f4dc4d17
DE
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
dee91e82
DE
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
fd820528 5428 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5429
70221824 5430static void
fd820528 5431init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5432 struct abbrev_table *abbrev_table,
fd820528
DE
5433 int use_existing_cu, int keep,
5434 die_reader_func_ftype *die_reader_func,
5435 void *data)
c906108c 5436{
dee91e82 5437 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5438 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5439 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5440 struct dwarf2_cu *cu;
d521ce57 5441 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5442 struct die_reader_specs reader;
d85a05f0 5443 struct die_info *comp_unit_die;
dee91e82 5444 int has_children;
d85a05f0 5445 struct attribute *attr;
365156ad 5446 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5447 struct signatured_type *sig_type = NULL;
4bdcc0c1 5448 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
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
a2ce51a0 5451 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5452 int rereading_dwo_cu = 0;
c906108c 5453
b4f54984 5454 if (dwarf_die_debug)
09406207
DE
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
dee91e82
DE
5459 if (use_existing_cu)
5460 gdb_assert (keep);
23745b47 5461
a2ce51a0
DE
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);
6aa5f3a6
DE
5469 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5470 die_reader_func, data);
a2ce51a0
DE
5471 return;
5472 }
5473
dee91e82
DE
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;
36586728
TT
5480
5481 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5482
5483 if (use_existing_cu && this_cu->cu != NULL)
5484 {
5485 cu = this_cu->cu;
42e7ad6c
DE
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;
dee91e82
DE
5494 }
5495 else
5496 {
5497 /* If !use_existing_cu, this_cu->cu must be NULL. */
5498 gdb_assert (this_cu->cu == NULL);
8d749320 5499 cu = XNEW (struct dwarf2_cu);
dee91e82 5500 init_one_comp_unit (cu, this_cu);
dee91e82 5501 /* If an error occurs while loading, release our storage. */
365156ad 5502 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5503 }
dee91e82 5504
b0c7bfa9 5505 /* Get the header. */
42e7ad6c
DE
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 {
3019eac3 5513 if (this_cu->is_debug_types)
dee91e82
DE
5514 {
5515 ULONGEST signature;
42e7ad6c 5516 cu_offset type_offset_in_tu;
dee91e82 5517
4bdcc0c1
DE
5518 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5519 abbrev_section, info_ptr,
42e7ad6c
DE
5520 &signature,
5521 &type_offset_in_tu);
dee91e82 5522
42e7ad6c
DE
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);
dee91e82
DE
5529 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5530
42e7ad6c
DE
5531 /* LENGTH has not been set yet for type units if we're
5532 using .gdb_index. */
1ce1cefd 5533 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
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;
dee91e82
DE
5538 }
5539 else
5540 {
4bdcc0c1
DE
5541 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5542 abbrev_section,
5543 info_ptr, 0);
dee91e82
DE
5544
5545 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5546 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5547 }
5548 }
10b3939b 5549
6caca83c 5550 /* Skip dummy compilation units. */
dee91e82 5551 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5552 || peek_abbrev_code (abfd, info_ptr) == 0)
5553 {
dee91e82 5554 do_cleanups (cleanups);
21b2bd31 5555 return;
6caca83c
CC
5556 }
5557
433df2d4
DE
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
42e7ad6c
DE
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. */
f4dc4d17
DE
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)
dee91e82 5571 {
4bdcc0c1 5572 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5573 make_cleanup (dwarf2_free_abbrev_table, cu);
5574 }
42e7ad6c
DE
5575 else if (rereading_dwo_cu)
5576 {
5577 dwarf2_free_abbrev_table (cu);
5578 dwarf2_read_abbrevs (cu, abbrev_section);
5579 }
af703f96 5580
dee91e82 5581 /* Read the top level CU/TU die. */
3019eac3 5582 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5583 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5584
b0c7bfa9
DE
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). */
3019eac3
DE
5589 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5590 if (attr)
5591 {
3019eac3 5592 struct dwo_unit *dwo_unit;
b0c7bfa9 5593 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5594
5595 if (has_children)
6a506a2d
DE
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 }
b0c7bfa9 5602 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5603 if (dwo_unit != NULL)
3019eac3 5604 {
6a506a2d
DE
5605 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5606 abbrev_table != NULL,
a2ce51a0 5607 comp_unit_die, NULL,
6a506a2d
DE
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. */
3019eac3
DE
5624 }
5625 }
5626
b0c7bfa9 5627 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5628 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5629
b0c7bfa9 5630 /* Done, clean up. */
365156ad 5631 if (free_cu_cleanup != NULL)
348e048f 5632 {
365156ad
TT
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);
dee91e82 5638
365156ad
TT
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);
dee91e82 5642
365156ad
TT
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);
348e048f 5649 }
365156ad
TT
5650
5651 do_cleanups (cleanups);
dee91e82
DE
5652}
5653
33e80786
DE
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).
dee91e82
DE
5657
5658 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5659 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
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.
3019eac3
DE
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. */
dee91e82
DE
5669
5670static void
5671init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5672 struct dwo_file *dwo_file,
dee91e82
DE
5673 die_reader_func_ftype *die_reader_func,
5674 void *data)
5675{
5676 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5677 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5678 bfd *abfd = get_section_bfd_owner (section);
33e80786 5679 struct dwarf2_section_info *abbrev_section;
dee91e82 5680 struct dwarf2_cu cu;
d521ce57 5681 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5682 struct die_reader_specs reader;
5683 struct cleanup *cleanups;
5684 struct die_info *comp_unit_die;
5685 int has_children;
5686
b4f54984 5687 if (dwarf_die_debug)
09406207
DE
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
dee91e82
DE
5692 gdb_assert (this_cu->cu == NULL);
5693
33e80786
DE
5694 abbrev_section = (dwo_file != NULL
5695 ? &dwo_file->sections.abbrev
5696 : get_abbrev_section_for_cu (this_cu));
5697
dee91e82
DE
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;
4bdcc0c1
DE
5706 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5707 abbrev_section, info_ptr,
3019eac3 5708 this_cu->is_debug_types);
dee91e82 5709
1ce1cefd 5710 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5711
5712 /* Skip dummy compilation units. */
5713 if (info_ptr >= begin_info_ptr + this_cu->length
5714 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5715 {
dee91e82 5716 do_cleanups (cleanups);
21b2bd31 5717 return;
93311388 5718 }
72bf9492 5719
dee91e82
DE
5720 dwarf2_read_abbrevs (&cu, abbrev_section);
5721 make_cleanup (dwarf2_free_abbrev_table, &cu);
5722
3019eac3 5723 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
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
3019eac3
DE
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.
dee91e82
DE
5734
5735 THIS_CU->cu is always freed when done.
3019eac3
DE
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. */
dee91e82
DE
5739
5740static void
5741init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5742 die_reader_func_ftype *die_reader_func,
5743 void *data)
5744{
33e80786 5745 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5746}
0018ea6f
DE
5747\f
5748/* Type Unit Groups.
dee91e82 5749
0018ea6f
DE
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. */
ff013f42 5755
f4dc4d17
DE
5756static hashval_t
5757hash_type_unit_group (const void *item)
5758{
9a3c8263
SM
5759 const struct type_unit_group *tu_group
5760 = (const struct type_unit_group *) item;
f4dc4d17 5761
094b34ac 5762 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5763}
348e048f
DE
5764
5765static int
f4dc4d17 5766eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5767{
9a3c8263
SM
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;
348e048f 5770
094b34ac 5771 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5772}
348e048f 5773
f4dc4d17
DE
5774/* Allocate a hash table for type unit groups. */
5775
5776static htab_t
5777allocate_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}
dee91e82 5787
f4dc4d17
DE
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
dee91e82 5793
094b34ac 5794/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5795 Create the type_unit_group object used to hold one or more TUs. */
5796
5797static struct type_unit_group *
094b34ac 5798create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5799{
5800 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5801 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5802 struct type_unit_group *tu_group;
f4dc4d17
DE
5803
5804 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5805 struct type_unit_group);
094b34ac 5806 per_cu = &tu_group->per_cu;
f4dc4d17 5807 per_cu->objfile = objfile;
f4dc4d17 5808
094b34ac
DE
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);
094b34ac
DE
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;
f4dc4d17 5829
094b34ac
DE
5830 xfree (name);
5831 }
f4dc4d17 5832
094b34ac
DE
5833 tu_group->hash.dwo_unit = cu->dwo_unit;
5834 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5835
5836 return tu_group;
5837}
5838
094b34ac
DE
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. */
f4dc4d17
DE
5841
5842static struct type_unit_group *
ff39bb5e 5843get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
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
094b34ac
DE
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;
f4dc4d17
DE
5879 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5880 &type_unit_group_for_lookup, INSERT);
5881 if (*slot != NULL)
5882 {
9a3c8263 5883 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
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;
094b34ac 5891 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5892 *slot = tu_group;
5893 ++tu_stats->nr_symtabs;
5894 }
5895
5896 return tu_group;
5897}
0018ea6f
DE
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
5906static struct partial_symtab *
5907create_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
18a94d75 5912 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
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
b93601f3
TT
5926/* The DATA object passed to process_psymtab_comp_unit_reader has this
5927 type. */
5928
5929struct 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
0018ea6f
DE
5941/* die_reader_func for process_psymtab_comp_unit. */
5942
5943static void
5944process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5945 const gdb_byte *info_ptr,
0018ea6f
DE
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;
3e29f34a 5952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 5953 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
5954 CORE_ADDR baseaddr;
5955 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5956 struct partial_symtab *pst;
3a2b436a 5957 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 5958 const char *filename;
9a3c8263
SM
5959 struct process_psymtab_comp_unit_data *info
5960 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 5961
b93601f3 5962 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
5963 return;
5964
5965 gdb_assert (! per_cu->is_debug_types);
5966
b93601f3 5967 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
5968
5969 cu->list_in_scope = &file_symbols;
5970
5971 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
5972 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
5973 if (filename == NULL)
0018ea6f 5974 filename = "";
0018ea6f
DE
5975
5976 pst = create_partial_symtab (per_cu, filename);
5977
5978 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 5979 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
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'. */
3a2b436a
JK
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)
0018ea6f
DE
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,
3e29f34a
MR
5993 gdbarch_adjust_dwarf2_addr (gdbarch,
5994 best_lowpc + baseaddr),
5995 gdbarch_adjust_dwarf2_addr (gdbarch,
5996 best_highpc + baseaddr) - 1,
5997 pst);
0018ea6f
DE
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,
e385593e 6013 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
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. */
e385593e 6022 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6023 {
6024 best_lowpc = lowpc;
6025 best_highpc = highpc;
6026 }
6027 }
3e29f34a
MR
6028 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6029 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6030
8763cede 6031 end_psymtab_common (objfile, pst);
0018ea6f
DE
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;
8d749320
SM
6042 pst->dependencies =
6043 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
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
b4f54984 6057 if (dwarf_read_debug)
0018ea6f
DE
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
6075static void
6076process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6077 int want_partial_unit,
6078 enum language pretend_language)
0018ea6f 6079{
b93601f3
TT
6080 struct process_psymtab_comp_unit_data info;
6081
0018ea6f
DE
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);
b93601f3
TT
6091 info.want_partial_unit = want_partial_unit;
6092 info.pretend_language = pretend_language;
0018ea6f
DE
6093 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6094 process_psymtab_comp_unit_reader,
b93601f3 6095 &info);
0018ea6f
DE
6096
6097 /* Age out any secondary CUs. */
6098 age_cached_comp_units ();
6099}
f4dc4d17
DE
6100
6101/* Reader function for build_type_psymtabs. */
6102
6103static void
6104build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6105 const gdb_byte *info_ptr,
f4dc4d17
DE
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;
0186c6a7 6113 struct signatured_type *sig_type;
f4dc4d17
DE
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);
0186c6a7
DE
6121 gdb_assert (per_cu->is_debug_types);
6122 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6123
6124 if (! has_children)
6125 return;
6126
6127 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6128 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6129
0186c6a7 6130 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
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
8763cede 6143 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6144}
6145
73051182
DE
6146/* Struct used to sort TUs by their abbreviation table offset. */
6147
6148struct 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
6156static int
6157sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6158{
9a3c8263
SM
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;
73051182
DE
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
6187static void
6188build_type_psymtabs_1 (void)
6189{
73051182
DE
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;
73051182
DE
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
b4f54984 6222 if (dwarf_read_debug)
73051182
DE
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
73051182 6272 do_cleanups (cleanups);
6aa5f3a6 6273}
73051182 6274
6aa5f3a6
DE
6275/* Print collected type unit statistics. */
6276
6277static void
6278print_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);
73051182
DE
6295}
6296
f4dc4d17
DE
6297/* Traversal function for build_type_psymtabs. */
6298
6299static int
6300build_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;
094b34ac 6304 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6305 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6306 int len = VEC_length (sig_type_ptr, tu_group->tus);
6307 struct signatured_type *iter;
f4dc4d17
DE
6308 int i;
6309
6310 gdb_assert (len > 0);
0186c6a7 6311 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6312
6313 pst->number_of_dependencies = len;
8d749320
SM
6314 pst->dependencies =
6315 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6316 for (i = 0;
0186c6a7 6317 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6318 ++i)
6319 {
0186c6a7
DE
6320 gdb_assert (iter->per_cu.is_debug_types);
6321 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6322 iter->type_unit_group = tu_group;
f4dc4d17
DE
6323 }
6324
0186c6a7 6325 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
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
6333static void
6334build_type_psymtabs (struct objfile *objfile)
6335{
0e50663e 6336 if (! create_all_type_units (objfile))
348e048f
DE
6337 return;
6338
73051182 6339 build_type_psymtabs_1 ();
6aa5f3a6 6340}
f4dc4d17 6341
6aa5f3a6
DE
6342/* Traversal function for process_skeletonless_type_unit.
6343 Read a TU in a DWO file and build partial symbols for it. */
6344
6345static int
6346process_skeletonless_type_unit (void **slot, void *info)
6347{
6348 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6349 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
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
6383static int
6384process_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
6401static void
6402process_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 }
348e048f
DE
6412}
6413
60606b2c
TT
6414/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6415
6416static void
6417psymtabs_addrmap_cleanup (void *o)
6418{
9a3c8263 6419 struct objfile *objfile = (struct objfile *) o;
ec61707d 6420
60606b2c
TT
6421 objfile->psymtabs_addrmap = NULL;
6422}
6423
95554aad
TT
6424/* Compute the 'user' field for each psymtab in OBJFILE. */
6425
6426static void
6427set_partial_user (struct objfile *objfile)
6428{
6429 int i;
6430
6431 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6432 {
8832e7e3 6433 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6434 struct partial_symtab *pst = per_cu->v.psymtab;
6435 int j;
6436
36586728
TT
6437 if (pst == NULL)
6438 continue;
6439
95554aad
TT
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
93311388
DE
6449/* Build the partial symbol table by doing a quick pass through the
6450 .debug_info and .debug_abbrev sections. */
72bf9492 6451
93311388 6452static void
c67a9c90 6453dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6454{
60606b2c
TT
6455 struct cleanup *back_to, *addrmap_cleanup;
6456 struct obstack temp_obstack;
21b2bd31 6457 int i;
93311388 6458
b4f54984 6459 if (dwarf_read_debug)
45cfd468
DE
6460 {
6461 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6462 objfile_name (objfile));
45cfd468
DE
6463 }
6464
98bfdba5
PA
6465 dwarf2_per_objfile->reading_partial_symbols = 1;
6466
be391dca 6467 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6468
93311388
DE
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);
72bf9492 6472
348e048f
DE
6473 build_type_psymtabs (objfile);
6474
93311388 6475 create_all_comp_units (objfile);
c906108c 6476
60606b2c
TT
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);
72bf9492 6483
21b2bd31 6484 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6485 {
8832e7e3 6486 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6487
b93601f3 6488 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6489 }
ff013f42 6490
6aa5f3a6
DE
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
b4f54984 6501 if (dwarf_read_debug)
6aa5f3a6
DE
6502 print_tu_stats ();
6503
95554aad
TT
6504 set_partial_user (objfile);
6505
ff013f42
JK
6506 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6507 &objfile->objfile_obstack);
60606b2c 6508 discard_cleanups (addrmap_cleanup);
ff013f42 6509
ae038cb0 6510 do_cleanups (back_to);
45cfd468 6511
b4f54984 6512 if (dwarf_read_debug)
45cfd468 6513 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6514 objfile_name (objfile));
ae038cb0
DJ
6515}
6516
3019eac3 6517/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6518
6519static void
dee91e82 6520load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6521 const gdb_byte *info_ptr,
dee91e82
DE
6522 struct die_info *comp_unit_die,
6523 int has_children,
6524 void *data)
ae038cb0 6525{
dee91e82 6526 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6527
95554aad 6528 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6529
ae038cb0
DJ
6530 /* Check if comp unit has_children.
6531 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6532 If not, there's no more debug_info for this comp unit. */
d85a05f0 6533 if (has_children)
dee91e82
DE
6534 load_partial_dies (reader, info_ptr, 0);
6535}
98bfdba5 6536
dee91e82
DE
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. */
c5b7e1cb 6539
dee91e82
DE
6540static void
6541load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6542{
f4dc4d17
DE
6543 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6544 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6545}
6546
ae038cb0 6547static void
36586728
TT
6548read_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)
ae038cb0 6554{
d521ce57 6555 const gdb_byte *info_ptr;
a32a8923 6556 bfd *abfd = get_section_bfd_owner (section);
be391dca 6557
b4f54984 6558 if (dwarf_read_debug)
bf6af496 6559 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6560 get_section_name (section),
6561 get_section_file_name (section));
bf6af496 6562
36586728 6563 dwarf2_read_section (objfile, section);
ae038cb0 6564
36586728 6565 info_ptr = section->buffer;
6e70227d 6566
36586728 6567 while (info_ptr < section->buffer + section->size)
ae038cb0 6568 {
c764a876 6569 unsigned int length, initial_length_size;
ae038cb0 6570 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6571 sect_offset offset;
ae038cb0 6572
36586728 6573 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6574
6575 /* Read just enough information to find out where the next
6576 compilation unit is. */
36586728 6577 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6578
6579 /* Save the compilation unit for later lookup. */
8d749320 6580 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
ae038cb0
DJ
6581 memset (this_cu, 0, sizeof (*this_cu));
6582 this_cu->offset = offset;
c764a876 6583 this_cu->length = length + initial_length_size;
36586728 6584 this_cu->is_dwz = is_dwz;
9291a0cd 6585 this_cu->objfile = objfile;
8a0459fd 6586 this_cu->section = section;
ae038cb0 6587
36586728 6588 if (*n_comp_units == *n_allocated)
ae038cb0 6589 {
36586728 6590 *n_allocated *= 2;
224c3ddb
SM
6591 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6592 *all_comp_units, *n_allocated);
ae038cb0 6593 }
36586728
TT
6594 (*all_comp_units)[*n_comp_units] = this_cu;
6595 ++*n_comp_units;
ae038cb0
DJ
6596
6597 info_ptr = info_ptr + this_cu->length;
6598 }
36586728
TT
6599}
6600
6601/* Create a list of all compilation units in OBJFILE.
6602 This is only done for -readnow and building partial symtabs. */
6603
6604static void
6605create_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;
4db1a1dc 6610 struct dwz_file *dwz;
36586728
TT
6611
6612 n_comp_units = 0;
6613 n_allocated = 10;
8d749320 6614 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728
TT
6615
6616 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6617 &n_allocated, &n_comp_units, &all_comp_units);
6618
4db1a1dc
TT
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);
ae038cb0 6624
8d749320
SM
6625 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6626 struct dwarf2_per_cu_data *,
6627 n_comp_units);
ae038cb0
DJ
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;
c906108c
SS
6632}
6633
5734ee8b 6634/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6635 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6636 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
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. */
c906108c 6639
72bf9492
DJ
6640static void
6641scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6642 CORE_ADDR *highpc, int set_addrmap,
6643 struct dwarf2_cu *cu)
c906108c 6644{
72bf9492 6645 struct partial_die_info *pdi;
c906108c 6646
91c24f0a
DC
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. */
c906108c 6650
72bf9492 6651 pdi = first_die;
91c24f0a 6652
72bf9492
DJ
6653 while (pdi != NULL)
6654 {
6655 fixup_partial_die (pdi, cu);
c906108c 6656
f55ee35c 6657 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6658 children, so we need to look at them. Ditto for anonymous
6659 enums. */
933c6fe4 6660
72bf9492 6661 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6662 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6663 || pdi->tag == DW_TAG_imported_unit)
c906108c 6664 {
72bf9492 6665 switch (pdi->tag)
c906108c
SS
6666 {
6667 case DW_TAG_subprogram:
cdc07690 6668 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6669 break;
72929c62 6670 case DW_TAG_constant:
c906108c
SS
6671 case DW_TAG_variable:
6672 case DW_TAG_typedef:
91c24f0a 6673 case DW_TAG_union_type:
72bf9492 6674 if (!pdi->is_declaration)
63d06c5c 6675 {
72bf9492 6676 add_partial_symbol (pdi, cu);
63d06c5c
DC
6677 }
6678 break;
c906108c 6679 case DW_TAG_class_type:
680b30c7 6680 case DW_TAG_interface_type:
c906108c 6681 case DW_TAG_structure_type:
72bf9492 6682 if (!pdi->is_declaration)
c906108c 6683 {
72bf9492 6684 add_partial_symbol (pdi, cu);
c906108c 6685 }
e98c9e7c
TT
6686 if (cu->language == language_rust && pdi->has_children)
6687 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6688 set_addrmap, cu);
c906108c 6689 break;
91c24f0a 6690 case DW_TAG_enumeration_type:
72bf9492
DJ
6691 if (!pdi->is_declaration)
6692 add_partial_enumeration (pdi, cu);
c906108c
SS
6693 break;
6694 case DW_TAG_base_type:
a02abb62 6695 case DW_TAG_subrange_type:
c906108c 6696 /* File scope base type definitions are added to the partial
c5aa993b 6697 symbol table. */
72bf9492 6698 add_partial_symbol (pdi, cu);
c906108c 6699 break;
d9fa45fe 6700 case DW_TAG_namespace:
cdc07690 6701 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6702 break;
5d7cb8df 6703 case DW_TAG_module:
cdc07690 6704 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6705 break;
95554aad
TT
6706 case DW_TAG_imported_unit:
6707 {
6708 struct dwarf2_per_cu_data *per_cu;
6709
f4dc4d17
DE
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]"),
4262abfb 6715 objfile_name (cu->objfile));
f4dc4d17
DE
6716 }
6717
95554aad 6718 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6719 pdi->is_dwz,
95554aad
TT
6720 cu->objfile);
6721
6722 /* Go read the partial unit, if needed. */
6723 if (per_cu->v.psymtab == NULL)
b93601f3 6724 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6725
f4dc4d17 6726 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6727 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6728 }
6729 break;
74921315
KS
6730 case DW_TAG_imported_declaration:
6731 add_partial_symbol (pdi, cu);
6732 break;
c906108c
SS
6733 default:
6734 break;
6735 }
6736 }
6737
72bf9492
DJ
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.
91c24f0a 6745
72bf9492 6746 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 6747 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
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.
91c24f0a 6751
72bf9492
DJ
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. */
15d034d0 6767static const char *
72bf9492
DJ
6768partial_die_parent_scope (struct partial_die_info *pdi,
6769 struct dwarf2_cu *cu)
6770{
15d034d0 6771 const char *grandparent_scope;
72bf9492 6772 struct partial_die_info *parent, *real_pdi;
91c24f0a 6773
72bf9492
DJ
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;
72bf9492 6778 while (real_pdi->has_specification)
36586728
TT
6779 real_pdi = find_partial_die (real_pdi->spec_offset,
6780 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
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
10b3939b 6791 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6792
acebe513
UW
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
6e70227d 6797 && parent->tag == DW_TAG_namespace
acebe513
UW
6798 && strcmp (parent->name, "::") == 0
6799 && grandparent_scope == NULL)
6800 {
6801 parent->scope = NULL;
6802 parent->scope_set = 1;
6803 return NULL;
6804 }
6805
9c6c53f7
SA
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
f55ee35c 6810 || parent->tag == DW_TAG_module
72bf9492
DJ
6811 || parent->tag == DW_TAG_structure_type
6812 || parent->tag == DW_TAG_class_type
680b30c7 6813 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6814 || parent->tag == DW_TAG_union_type
6815 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6816 {
6817 if (grandparent_scope == NULL)
6818 parent->scope = parent->name;
6819 else
3e43a32a
MS
6820 parent->scope = typename_concat (&cu->comp_unit_obstack,
6821 grandparent_scope,
f55ee35c 6822 parent->name, 0, cu);
72bf9492 6823 }
72bf9492
DJ
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,
e2e0b3e5 6830 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6831 parent->tag, pdi->offset.sect_off);
72bf9492 6832 parent->scope = grandparent_scope;
c906108c
SS
6833 }
6834
72bf9492
DJ
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. */
4568ecf9 6841
72bf9492
DJ
6842static char *
6843partial_die_full_name (struct partial_die_info *pdi,
6844 struct dwarf2_cu *cu)
6845{
15d034d0 6846 const char *parent_scope;
72bf9492 6847
98bfdba5
PA
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
b64f50a1 6862 /* DW_FORM_ref_addr is using section offset. */
b4069958 6863 attr.name = (enum dwarf_attribute) 0;
98bfdba5 6864 attr.form = DW_FORM_ref_addr;
4568ecf9 6865 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6866 die = follow_die_ref (NULL, &attr, &ref_cu);
6867
6868 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6869 }
6870 }
6871
72bf9492
DJ
6872 parent_scope = partial_die_parent_scope (pdi, cu);
6873 if (parent_scope == NULL)
6874 return NULL;
6875 else
f55ee35c 6876 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6877}
6878
6879static void
72bf9492 6880add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6881{
e7c27a73 6882 struct objfile *objfile = cu->objfile;
3e29f34a 6883 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 6884 CORE_ADDR addr = 0;
15d034d0 6885 const char *actual_name = NULL;
e142c38c 6886 CORE_ADDR baseaddr;
15d034d0 6887 char *built_actual_name;
e142c38c
DJ
6888
6889 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6890
15d034d0
TT
6891 built_actual_name = partial_die_full_name (pdi, cu);
6892 if (built_actual_name != NULL)
6893 actual_name = built_actual_name;
63d06c5c 6894
72bf9492
DJ
6895 if (actual_name == NULL)
6896 actual_name = pdi->name;
6897
c906108c
SS
6898 switch (pdi->tag)
6899 {
6900 case DW_TAG_subprogram:
3e29f34a 6901 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 6902 if (pdi->is_external || cu->language == language_ada)
c906108c 6903 {
2cfa0c8d
JB
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. */
f47fb265 6908 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6909 built_actual_name != NULL,
f47fb265
MS
6910 VAR_DOMAIN, LOC_BLOCK,
6911 &objfile->global_psymbols,
1762568f 6912 addr, cu->language, objfile);
c906108c
SS
6913 }
6914 else
6915 {
f47fb265 6916 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6917 built_actual_name != NULL,
f47fb265
MS
6918 VAR_DOMAIN, LOC_BLOCK,
6919 &objfile->static_psymbols,
1762568f 6920 addr, cu->language, objfile);
c906108c 6921 }
0c1b455e
TT
6922
6923 if (pdi->main_subprogram && actual_name != NULL)
6924 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 6925 break;
72929c62
JB
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;
f47fb265 6934 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6935 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 6936 list, 0, cu->language, objfile);
72929c62
JB
6937 }
6938 break;
c906108c 6939 case DW_TAG_variable:
95554aad
TT
6940 if (pdi->d.locdesc)
6941 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6942
95554aad 6943 if (pdi->d.locdesc
caac4577
JG
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)
c906108c
SS
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
95554aad 6967 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6968 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6969 built_actual_name != NULL,
f47fb265
MS
6970 VAR_DOMAIN, LOC_STATIC,
6971 &objfile->global_psymbols,
1762568f 6972 addr + baseaddr,
f47fb265 6973 cu->language, objfile);
c906108c
SS
6974 }
6975 else
6976 {
ff908ebf
AW
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)
decbce07 6982 {
15d034d0 6983 xfree (built_actual_name);
decbce07
MS
6984 return;
6985 }
ff908ebf 6986
f47fb265 6987 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6988 built_actual_name != NULL,
f47fb265
MS
6989 VAR_DOMAIN, LOC_STATIC,
6990 &objfile->static_psymbols,
ff908ebf 6991 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 6992 cu->language, objfile);
c906108c
SS
6993 }
6994 break;
6995 case DW_TAG_typedef:
6996 case DW_TAG_base_type:
a02abb62 6997 case DW_TAG_subrange_type:
38d518c9 6998 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6999 built_actual_name != NULL,
176620f1 7000 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7001 &objfile->static_psymbols,
1762568f 7002 0, cu->language, objfile);
c906108c 7003 break;
74921315 7004 case DW_TAG_imported_declaration:
72bf9492
DJ
7005 case DW_TAG_namespace:
7006 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7007 built_actual_name != NULL,
72bf9492
DJ
7008 VAR_DOMAIN, LOC_TYPEDEF,
7009 &objfile->global_psymbols,
1762568f 7010 0, cu->language, objfile);
72bf9492 7011 break;
530e8392
KB
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,
1762568f 7017 0, cu->language, objfile);
530e8392 7018 break;
c906108c 7019 case DW_TAG_class_type:
680b30c7 7020 case DW_TAG_interface_type:
c906108c
SS
7021 case DW_TAG_structure_type:
7022 case DW_TAG_union_type:
7023 case DW_TAG_enumeration_type:
fa4028e9
JB
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)
decbce07 7030 {
15d034d0 7031 xfree (built_actual_name);
decbce07
MS
7032 return;
7033 }
fa4028e9 7034
63d06c5c
DC
7035 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7036 static vs. global. */
38d518c9 7037 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7038 built_actual_name != NULL,
176620f1 7039 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7040 cu->language == language_cplus
63d06c5c
DC
7041 ? &objfile->global_psymbols
7042 : &objfile->static_psymbols,
1762568f 7043 0, cu->language, objfile);
c906108c 7044
c906108c
SS
7045 break;
7046 case DW_TAG_enumerator:
38d518c9 7047 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7048 built_actual_name != NULL,
176620f1 7049 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7050 cu->language == language_cplus
f6fe98ef
DJ
7051 ? &objfile->global_psymbols
7052 : &objfile->static_psymbols,
1762568f 7053 0, cu->language, objfile);
c906108c
SS
7054 break;
7055 default:
7056 break;
7057 }
5c4e30ca 7058
15d034d0 7059 xfree (built_actual_name);
c906108c
SS
7060}
7061
5c4e30ca
DC
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. */
91c24f0a 7065
72bf9492
DJ
7066static void
7067add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7068 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7069 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7070{
72bf9492 7071 /* Add a symbol for the namespace. */
e7c27a73 7072
72bf9492 7073 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7074
7075 /* Now scan partial symbols in that namespace. */
7076
91c24f0a 7077 if (pdi->has_children)
cdc07690 7078 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7079}
7080
5d7cb8df
JK
7081/* Read a partial die corresponding to a Fortran module. */
7082
7083static void
7084add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7085 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7086{
530e8392
KB
7087 /* Add a symbol for the namespace. */
7088
7089 add_partial_symbol (pdi, cu);
7090
f55ee35c 7091 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7092
7093 if (pdi->has_children)
cdc07690 7094 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7095}
7096
bc30ff58
JB
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
cdc07690 7100 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7101 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7102 and highest PC values found in PDI.
6e70227d 7103
cdc07690
YQ
7104 PDI may also be a lexical block, in which case we simply search
7105 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7106 Again, this is only performed when the CU language allows this
7107 type of definitions. */
7108
7109static void
7110add_partial_subprogram (struct partial_die_info *pdi,
7111 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7112 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
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;
cdc07690 7122 if (set_addrmap)
5734ee8b 7123 {
5734ee8b 7124 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7126 CORE_ADDR baseaddr;
7127 CORE_ADDR highpc;
7128 CORE_ADDR lowpc;
5734ee8b
DJ
7129
7130 baseaddr = ANOFFSET (objfile->section_offsets,
7131 SECT_OFF_TEXT (objfile));
3e29f34a
MR
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,
9291a0cd 7137 cu->per_cu->v.psymtab);
5734ee8b 7138 }
481860b3
GB
7139 }
7140
7141 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7142 {
bc30ff58 7143 if (!pdi->is_declaration)
e8d05480
JB
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);
bc30ff58
JB
7149 }
7150 }
6e70227d 7151
bc30ff58
JB
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)
cdc07690 7163 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7164 pdi = pdi->die_sibling;
7165 }
7166 }
7167}
7168
91c24f0a
DC
7169/* Read a partial die corresponding to an enumeration type. */
7170
72bf9492
DJ
7171static void
7172add_partial_enumeration (struct partial_die_info *enum_pdi,
7173 struct dwarf2_cu *cu)
91c24f0a 7174{
72bf9492 7175 struct partial_die_info *pdi;
91c24f0a
DC
7176
7177 if (enum_pdi->name != NULL)
72bf9492
DJ
7178 add_partial_symbol (enum_pdi, cu);
7179
7180 pdi = enum_pdi->die_child;
7181 while (pdi)
91c24f0a 7182 {
72bf9492 7183 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7184 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7185 else
72bf9492
DJ
7186 add_partial_symbol (pdi, cu);
7187 pdi = pdi->die_sibling;
91c24f0a 7188 }
91c24f0a
DC
7189}
7190
6caca83c
CC
7191/* Return the initial uleb128 in the die at INFO_PTR. */
7192
7193static unsigned int
d521ce57 7194peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7195{
7196 unsigned int bytes_read;
7197
7198 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7199}
7200
4bb7a0a7
DJ
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
7206static struct abbrev_info *
d521ce57 7207peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7208 struct dwarf2_cu *cu)
4bb7a0a7
DJ
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
433df2d4 7219 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7220 if (!abbrev)
7221 {
422b9917
DE
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));
4bb7a0a7
DJ
7226 }
7227
7228 return abbrev;
7229}
7230
93311388
DE
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
4bb7a0a7
DJ
7233 DIE. Any children of the skipped DIEs will also be skipped. */
7234
d521ce57
TT
7235static const gdb_byte *
7236skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7237{
dee91e82 7238 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
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
dee91e82 7248 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7249 }
7250}
7251
93311388
DE
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
4bb7a0a7
DJ
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
d521ce57
TT
7258static const gdb_byte *
7259skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7260 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7261{
7262 unsigned int bytes_read;
7263 struct attribute attr;
dee91e82
DE
7264 bfd *abfd = reader->abfd;
7265 struct dwarf2_cu *cu = reader->cu;
d521ce57 7266 const gdb_byte *buffer = reader->buffer;
f664829e 7267 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
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 {
dee91e82 7275 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7276 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7277 complaint (&symfile_complaints,
7278 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7279 else
b9502d3f
WN
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"));
22869d73
KS
7287 else if (sibling_ptr > reader->buffer_end)
7288 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7289 else
7290 return sibling_ptr;
7291 }
4bb7a0a7
DJ
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 {
4bb7a0a7 7299 case DW_FORM_ref_addr:
ae411497
TT
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;
36586728
TT
7307 case DW_FORM_GNU_ref_alt:
7308 info_ptr += cu->header.offset_size;
7309 break;
ae411497 7310 case DW_FORM_addr:
4bb7a0a7
DJ
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;
2dc7f7b3
TT
7318 case DW_FORM_flag_present:
7319 break;
4bb7a0a7
DJ
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:
55f1336d 7330 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7331 info_ptr += 8;
7332 break;
7333 case DW_FORM_string:
9b1c24c8 7334 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7335 info_ptr += bytes_read;
7336 break;
2dc7f7b3 7337 case DW_FORM_sec_offset:
4bb7a0a7 7338 case DW_FORM_strp:
36586728 7339 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7340 info_ptr += cu->header.offset_size;
7341 break;
2dc7f7b3 7342 case DW_FORM_exprloc:
4bb7a0a7
DJ
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:
3019eac3
DE
7359 case DW_FORM_GNU_addr_index:
7360 case DW_FORM_GNU_str_index:
d521ce57 7361 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
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:
3e43a32a
MS
7371 error (_("Dwarf Error: Cannot handle %s "
7372 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7373 dwarf_form_name (form),
7374 bfd_get_filename (abfd));
7375 }
7376 }
7377
7378 if (abbrev->has_children)
dee91e82 7379 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7380 else
7381 return info_ptr;
7382}
7383
93311388 7384/* Locate ORIG_PDI's sibling.
dee91e82 7385 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7386
d521ce57 7387static const gdb_byte *
dee91e82
DE
7388locate_pdi_sibling (const struct die_reader_specs *reader,
7389 struct partial_die_info *orig_pdi,
d521ce57 7390 const gdb_byte *info_ptr)
91c24f0a
DC
7391{
7392 /* Do we know the sibling already? */
72bf9492 7393
91c24f0a
DC
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
4bb7a0a7 7402 /* Skip the children the long way. */
91c24f0a 7403
dee91e82 7404 return skip_children (reader, info_ptr);
91c24f0a
DC
7405}
7406
257e7a09 7407/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7408 not NULL. */
c906108c
SS
7409
7410static void
257e7a09
YQ
7411dwarf2_read_symtab (struct partial_symtab *self,
7412 struct objfile *objfile)
c906108c 7413{
257e7a09 7414 if (self->readin)
c906108c 7415 {
442e4d9c 7416 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7417 self->filename);
442e4d9c
YQ
7418 }
7419 else
7420 {
7421 if (info_verbose)
c906108c 7422 {
442e4d9c 7423 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7424 self->filename);
442e4d9c 7425 gdb_flush (gdb_stdout);
c906108c 7426 }
c906108c 7427
442e4d9c 7428 /* Restore our global data. */
9a3c8263
SM
7429 dwarf2_per_objfile
7430 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7431 dwarf2_objfile_data_key);
10b3939b 7432
442e4d9c
YQ
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
9a3c8263
SM
7440 = ((struct dwarf2_per_objfile *)
7441 objfile_data (objfile->separate_debug_objfile_backlink,
7442 dwarf2_objfile_data_key));
9a619af0 7443
442e4d9c
YQ
7444 dwarf2_per_objfile->has_section_at_zero
7445 = dpo_backlink->has_section_at_zero;
7446 }
b2ab525c 7447
442e4d9c 7448 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7449
257e7a09 7450 psymtab_to_symtab_1 (self);
c906108c 7451
442e4d9c
YQ
7452 /* Finish up the debug error message. */
7453 if (info_verbose)
7454 printf_filtered (_("done.\n"));
c906108c 7455 }
95554aad
TT
7456
7457 process_cu_includes ();
c906108c 7458}
9cdd5dbd
DE
7459\f
7460/* Reading in full CUs. */
c906108c 7461
10b3939b
DJ
7462/* Add PER_CU to the queue. */
7463
7464static void
95554aad
TT
7465queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7466 enum language pretend_language)
10b3939b
DJ
7467{
7468 struct dwarf2_queue_item *item;
7469
7470 per_cu->queued = 1;
8d749320 7471 item = XNEW (struct dwarf2_queue_item);
10b3939b 7472 item->per_cu = per_cu;
95554aad 7473 item->pretend_language = pretend_language;
10b3939b
DJ
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
89e63ee4
DE
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.
0907af0c 7487 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
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. */
0907af0c
DE
7492
7493static int
89e63ee4 7494maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
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. */
89e63ee4
DE
7510 if (dependent_cu != NULL)
7511 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
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
10b3939b
DJ
7531/* Process the queue. */
7532
7533static void
a0f42c21 7534process_queue (void)
10b3939b
DJ
7535{
7536 struct dwarf2_queue_item *item, *next_item;
7537
b4f54984 7538 if (dwarf_read_debug)
45cfd468
DE
7539 {
7540 fprintf_unfiltered (gdb_stdlog,
7541 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7542 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7543 }
7544
03dd20cc
DJ
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. */
10b3939b
DJ
7547 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7548 {
cc12ce38
DE
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)
f4dc4d17
DE
7554 {
7555 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7556 unsigned int debug_print_threshold;
247f5c4f 7557 char buf[100];
f4dc4d17 7558
247f5c4f 7559 if (per_cu->is_debug_types)
f4dc4d17 7560 {
247f5c4f
DE
7561 struct signatured_type *sig_type =
7562 (struct signatured_type *) per_cu;
7563
7564 sprintf (buf, "TU %s at offset 0x%x",
73be47f5
DE
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;
f4dc4d17 7570 }
247f5c4f 7571 else
73be47f5
DE
7572 {
7573 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7574 debug_print_threshold = 1;
7575 }
247f5c4f 7576
b4f54984 7577 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7578 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
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
b4f54984 7585 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7586 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7587 }
10b3939b
DJ
7588
7589 item->per_cu->queued = 0;
7590 next_item = item->next;
7591 xfree (item);
7592 }
7593
7594 dwarf2_queue_tail = NULL;
45cfd468 7595
b4f54984 7596 if (dwarf_read_debug)
45cfd468
DE
7597 {
7598 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7599 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7600 }
10b3939b
DJ
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
7607static void
7608dwarf2_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)
dee91e82 7620 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
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
c906108c 7634static void
fba45db2 7635psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7636{
10b3939b 7637 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7638 int i;
7639
95554aad
TT
7640 if (pst->readin)
7641 return;
7642
aaa75496 7643 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7644 if (!pst->dependencies[i]->readin
7645 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7646 {
7647 /* Inform about additional files that need to be read in. */
7648 if (info_verbose)
7649 {
a3f17187 7650 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
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);
0963b4bd 7656 wrap_here (""); /* Flush output. */
aaa75496
JB
7657 gdb_flush (gdb_stdout);
7658 }
7659 psymtab_to_symtab_1 (pst->dependencies[i]);
7660 }
7661
9a3c8263 7662 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7663
7664 if (per_cu == NULL)
aaa75496
JB
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 }
c906108c 7671
a0f42c21 7672 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7673}
7674
dee91e82
DE
7675/* Trivial hash function for die_info: the hash value of a DIE
7676 is its offset in .debug_info for this objfile. */
10b3939b 7677
dee91e82
DE
7678static hashval_t
7679die_hash (const void *item)
10b3939b 7680{
9a3c8263 7681 const struct die_info *die = (const struct die_info *) item;
6502dd73 7682
dee91e82
DE
7683 return die->offset.sect_off;
7684}
63d06c5c 7685
dee91e82
DE
7686/* Trivial comparison function for die_info structures: two DIEs
7687 are equal if they have the same offset. */
98bfdba5 7688
dee91e82
DE
7689static int
7690die_eq (const void *item_lhs, const void *item_rhs)
7691{
9a3c8263
SM
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;
c906108c 7694
dee91e82
DE
7695 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7696}
c906108c 7697
dee91e82
DE
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. */
c906108c 7701
dee91e82
DE
7702static void
7703load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7704 const gdb_byte *info_ptr,
dee91e82
DE
7705 struct die_info *comp_unit_die,
7706 int has_children,
7707 void *data)
7708{
7709 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7710 enum language *language_ptr = (enum language *) data;
6caca83c 7711
dee91e82
DE
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);
e142c38c 7721
dee91e82
DE
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. */
10b3939b
DJ
7727
7728 /* We try not to read any attributes in this function, because not
9cdd5dbd 7729 all CUs needed for references have been loaded yet, and symbol
10b3939b 7730 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
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. */
95554aad 7734 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7735}
10b3939b 7736
dee91e82 7737/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7738
dee91e82 7739static void
95554aad
TT
7740load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7741 enum language pretend_language)
dee91e82 7742{
3019eac3 7743 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7744
f4dc4d17
DE
7745 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7746 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7747}
7748
3da10d80
KS
7749/* Add a DIE to the delayed physname list. */
7750
7751static void
7752add_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
7767static void
7768free_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
7784static void
7785compute_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 {
1d06ead6 7791 const char *physname;
3da10d80
KS
7792 struct fn_fieldlist *fn_flp
7793 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7794 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
7795 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7796 = physname ? physname : "";
3da10d80
KS
7797 }
7798}
7799
a766d390
DE
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
7811static void
7812fixup_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"),
08be3fe3
DE
7838 (symbol_symtab (sym) != NULL
7839 ? symtab_to_filename_for_display
7840 (symbol_symtab (sym))
4262abfb 7841 : objfile_name (cu->objfile)),
a766d390
DE
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;
34a68019 7852 const char *saved_package_name
224c3ddb
SM
7853 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
7854 package_name,
7855 strlen (package_name));
19f392bc
UW
7856 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7857 saved_package_name);
a766d390
DE
7858 struct symbol *sym;
7859
7860 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7861
e623cf5d 7862 sym = allocate_symbol (objfile);
f85f34ed 7863 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7864 SYMBOL_SET_NAMES (sym, saved_package_name,
7865 strlen (saved_package_name), 0, objfile);
a766d390
DE
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;
f1e6e072 7869 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7870 SYMBOL_TYPE (sym) = type;
7871
7872 add_symbol_to_list (sym, &global_symbols);
7873
7874 xfree (package_name);
7875 }
7876}
7877
95554aad
TT
7878/* Return the symtab for PER_CU. This works properly regardless of
7879 whether we're using the index or psymtabs. */
7880
43f3e411
DE
7881static struct compunit_symtab *
7882get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
7883{
7884 return (dwarf2_per_objfile->using_index
43f3e411
DE
7885 ? per_cu->v.quick->compunit_symtab
7886 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
7887}
7888
7889/* A helper function for computing the list of all symbol tables
7890 included by PER_CU. */
7891
7892static void
43f3e411 7893recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 7894 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 7895 struct dwarf2_per_cu_data *per_cu,
43f3e411 7896 struct compunit_symtab *immediate_parent)
95554aad
TT
7897{
7898 void **slot;
7899 int ix;
43f3e411 7900 struct compunit_symtab *cust;
95554aad
TT
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. */
43f3e411
DE
7912 cust = get_compunit_symtab (per_cu);
7913 if (cust != NULL)
ec94af83
DE
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 {
43f3e411 7919 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
7920 if (*slot == NULL)
7921 {
43f3e411
DE
7922 *slot = cust;
7923 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7924 if (cust->user == NULL)
7925 cust->user = immediate_parent;
ec94af83
DE
7926 }
7927 }
7928 else
f9125b6c 7929 {
43f3e411
DE
7930 VEC_safe_push (compunit_symtab_ptr, *result, cust);
7931 if (cust->user == NULL)
7932 cust->user = immediate_parent;
f9125b6c 7933 }
ec94af83 7934 }
95554aad
TT
7935
7936 for (ix = 0;
796a7ff8 7937 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7938 ++ix)
ec94af83
DE
7939 {
7940 recursively_compute_inclusions (result, all_children,
43f3e411 7941 all_type_symtabs, iter, cust);
ec94af83 7942 }
95554aad
TT
7943}
7944
43f3e411 7945/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
7946 PER_CU. */
7947
7948static void
43f3e411 7949compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 7950{
f4dc4d17
DE
7951 gdb_assert (! per_cu->is_debug_types);
7952
796a7ff8 7953 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7954 {
7955 int ix, len;
ec94af83 7956 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
7957 struct compunit_symtab *compunit_symtab_iter;
7958 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 7959 htab_t all_children, all_type_symtabs;
43f3e411 7960 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
7961
7962 /* If we don't have a symtab, we can just skip this case. */
43f3e411 7963 if (cust == NULL)
95554aad
TT
7964 return;
7965
7966 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7967 NULL, xcalloc, xfree);
ec94af83
DE
7968 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7969 NULL, xcalloc, xfree);
95554aad
TT
7970
7971 for (ix = 0;
796a7ff8 7972 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7973 ix, per_cu_iter);
95554aad 7974 ++ix)
ec94af83
DE
7975 {
7976 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 7977 all_type_symtabs, per_cu_iter,
43f3e411 7978 cust);
ec94af83 7979 }
95554aad 7980
ec94af83 7981 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
7982 len = VEC_length (compunit_symtab_ptr, result_symtabs);
7983 cust->includes
8d749320
SM
7984 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
7985 struct compunit_symtab *, len + 1);
95554aad 7986 for (ix = 0;
43f3e411
DE
7987 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
7988 compunit_symtab_iter);
95554aad 7989 ++ix)
43f3e411
DE
7990 cust->includes[ix] = compunit_symtab_iter;
7991 cust->includes[len] = NULL;
95554aad 7992
43f3e411 7993 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 7994 htab_delete (all_children);
ec94af83 7995 htab_delete (all_type_symtabs);
95554aad
TT
7996 }
7997}
7998
7999/* Compute the 'includes' field for the symtabs of all the CUs we just
8000 read. */
8001
8002static void
8003process_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)
f4dc4d17
DE
8012 {
8013 if (! iter->is_debug_types)
43f3e411 8014 compute_compunit_symtab_includes (iter);
f4dc4d17 8015 }
95554aad
TT
8016
8017 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8018}
8019
9cdd5dbd 8020/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8021 already been loaded into memory. */
8022
8023static void
95554aad
TT
8024process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8025 enum language pretend_language)
10b3939b 8026{
10b3939b 8027 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8028 struct objfile *objfile = per_cu->objfile;
3e29f34a 8029 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8030 CORE_ADDR lowpc, highpc;
43f3e411 8031 struct compunit_symtab *cust;
3da10d80 8032 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8033 CORE_ADDR baseaddr;
4359dff1 8034 struct block *static_block;
3e29f34a 8035 CORE_ADDR addr;
10b3939b
DJ
8036
8037 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8038
10b3939b
DJ
8039 buildsym_init ();
8040 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8041 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8042
8043 cu->list_in_scope = &file_symbols;
c906108c 8044
95554aad
TT
8045 cu->language = pretend_language;
8046 cu->language_defn = language_def (cu->language);
8047
c906108c 8048 /* Do line number decoding in read_file_scope () */
10b3939b 8049 process_die (cu->dies, cu);
c906108c 8050
a766d390
DE
8051 /* For now fudge the Go package. */
8052 if (cu->language == language_go)
8053 fixup_go_packaging (cu);
8054
3da10d80
KS
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
fae299cd
DC
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. */
10b3939b 8064 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8065
3e29f34a
MR
8066 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8067 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
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
43f3e411
DE
8076 cust = end_symtab_from_static_block (static_block,
8077 SECT_OFF_TEXT (objfile), 0);
c906108c 8078
43f3e411 8079 if (cust != NULL)
c906108c 8080 {
df15bd07 8081 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8082
8be455d7
JK
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. */
43f3e411 8086 if (!(cu->language == language_c
40e3ad0e 8087 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8088 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
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
ab260dad
JK
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).
8be455d7
JK
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 */
ab260dad 8102 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8103 cust->locations_valid = 1;
e0d00bc7
JK
8104
8105 if (gcc_4_minor >= 5)
43f3e411 8106 cust->epilogue_unwind_valid = 1;
96408a79 8107
43f3e411 8108 cust->call_site_htab = cu->call_site_htab;
c906108c 8109 }
9291a0cd
TT
8110
8111 if (dwarf2_per_objfile->using_index)
43f3e411 8112 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8113 else
8114 {
8115 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8116 pst->compunit_symtab = cust;
9291a0cd
TT
8117 pst->readin = 1;
8118 }
c906108c 8119
95554aad
TT
8120 /* Push it for inclusion processing later. */
8121 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8122
c906108c 8123 do_cleanups (back_to);
f4dc4d17 8124}
45cfd468 8125
f4dc4d17
DE
8126/* Generate full symbol information for type unit PER_CU, whose DIEs have
8127 already been loaded into memory. */
8128
8129static void
8130process_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;
43f3e411 8135 struct compunit_symtab *cust;
f4dc4d17 8136 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8137 struct signatured_type *sig_type;
8138
8139 gdb_assert (per_cu->is_debug_types);
8140 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
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
094b34ac
DE
8166 of it with end_expandable_symtab. Otherwise, complete the addition of
8167 this TU's symbols to the existing symtab. */
43f3e411 8168 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8169 {
43f3e411
DE
8170 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8171 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8172
43f3e411 8173 if (cust != NULL)
f4dc4d17
DE
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. */
43f3e411
DE
8179 if (!(cu->language == language_c
8180 && COMPUNIT_FILETABS (cust)->language != language_c))
8181 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8182 }
8183 }
8184 else
8185 {
0ab9ce85 8186 augment_type_symtab ();
43f3e411 8187 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8188 }
8189
8190 if (dwarf2_per_objfile->using_index)
43f3e411 8191 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8192 else
8193 {
8194 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8195 pst->compunit_symtab = cust;
f4dc4d17 8196 pst->readin = 1;
45cfd468 8197 }
f4dc4d17
DE
8198
8199 do_cleanups (back_to);
c906108c
SS
8200}
8201
95554aad
TT
8202/* Process an imported unit DIE. */
8203
8204static void
8205process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8206{
8207 struct attribute *attr;
8208
f4dc4d17
DE
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]"),
4262abfb 8214 objfile_name (cu->objfile));
f4dc4d17
DE
8215 }
8216
95554aad
TT
8217 attr = dwarf2_attr (die, DW_AT_import, cu);
8218 if (attr != NULL)
8219 {
8220 struct dwarf2_per_cu_data *per_cu;
95554aad 8221 sect_offset offset;
36586728 8222 int is_dwz;
95554aad
TT
8223
8224 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
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);
95554aad 8227
69d751e3 8228 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8229 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8230 load_full_comp_unit (per_cu, cu->language);
8231
796a7ff8 8232 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8233 per_cu);
8234 }
8235}
8236
adde2bff
DE
8237/* Reset the in_process bit of a die. */
8238
8239static void
8240reset_die_in_process (void *arg)
8241{
9a3c8263 8242 struct die_info *die = (struct die_info *) arg;
8c3cb9fa 8243
adde2bff
DE
8244 die->in_process = 0;
8245}
8246
c906108c
SS
8247/* Process a die and its children. */
8248
8249static void
e7c27a73 8250process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8251{
adde2bff
DE
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
c906108c
SS
8260 switch (die->tag)
8261 {
8262 case DW_TAG_padding:
8263 break;
8264 case DW_TAG_compile_unit:
95554aad 8265 case DW_TAG_partial_unit:
e7c27a73 8266 read_file_scope (die, cu);
c906108c 8267 break;
348e048f
DE
8268 case DW_TAG_type_unit:
8269 read_type_unit_scope (die, cu);
8270 break;
c906108c 8271 case DW_TAG_subprogram:
c906108c 8272 case DW_TAG_inlined_subroutine:
edb3359d 8273 read_func_scope (die, cu);
c906108c
SS
8274 break;
8275 case DW_TAG_lexical_block:
14898363
L
8276 case DW_TAG_try_block:
8277 case DW_TAG_catch_block:
e7c27a73 8278 read_lexical_block_scope (die, cu);
c906108c 8279 break;
96408a79
SA
8280 case DW_TAG_GNU_call_site:
8281 read_call_site_scope (die, cu);
8282 break;
c906108c 8283 case DW_TAG_class_type:
680b30c7 8284 case DW_TAG_interface_type:
c906108c
SS
8285 case DW_TAG_structure_type:
8286 case DW_TAG_union_type:
134d01f1 8287 process_structure_scope (die, cu);
c906108c
SS
8288 break;
8289 case DW_TAG_enumeration_type:
134d01f1 8290 process_enumeration_scope (die, cu);
c906108c 8291 break;
134d01f1 8292
f792889a
DJ
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. */
c906108c 8296 case DW_TAG_subroutine_type:
72019c9c 8297 case DW_TAG_set_type:
c906108c 8298 case DW_TAG_array_type:
c906108c 8299 case DW_TAG_pointer_type:
c906108c 8300 case DW_TAG_ptr_to_member_type:
c906108c 8301 case DW_TAG_reference_type:
c906108c 8302 case DW_TAG_string_type:
c906108c 8303 break;
134d01f1 8304
c906108c 8305 case DW_TAG_base_type:
a02abb62 8306 case DW_TAG_subrange_type:
cb249c71 8307 case DW_TAG_typedef:
134d01f1
DJ
8308 /* Add a typedef symbol for the type definition, if it has a
8309 DW_AT_name. */
f792889a 8310 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8311 break;
c906108c 8312 case DW_TAG_common_block:
e7c27a73 8313 read_common_block (die, cu);
c906108c
SS
8314 break;
8315 case DW_TAG_common_inclusion:
8316 break;
d9fa45fe 8317 case DW_TAG_namespace:
4d4ec4e5 8318 cu->processing_has_namespace_info = 1;
e7c27a73 8319 read_namespace (die, cu);
d9fa45fe 8320 break;
5d7cb8df 8321 case DW_TAG_module:
4d4ec4e5 8322 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8323 read_module (die, cu);
8324 break;
d9fa45fe 8325 case DW_TAG_imported_declaration:
74921315
KS
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. */
d9fa45fe 8330 case DW_TAG_imported_module:
4d4ec4e5 8331 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
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);
d9fa45fe 8337 break;
95554aad
TT
8338
8339 case DW_TAG_imported_unit:
8340 process_imported_unit_die (die, cu);
8341 break;
8342
c906108c 8343 default:
e7c27a73 8344 new_symbol (die, NULL, cu);
c906108c
SS
8345 break;
8346 }
adde2bff
DE
8347
8348 do_cleanups (in_process);
c906108c 8349}
ca69b9e6
DE
8350\f
8351/* DWARF name computation. */
c906108c 8352
94af9270
KS
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
8357static int
8358die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8359{
1c809c68
TT
8360 struct attribute *attr;
8361
94af9270
KS
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:
08a76f8a 8373 case DW_TAG_inlined_subroutine:
94af9270 8374 case DW_TAG_member:
74921315 8375 case DW_TAG_imported_declaration:
94af9270
KS
8376 return 1;
8377
8378 case DW_TAG_variable:
c2b0a229 8379 case DW_TAG_constant:
94af9270
KS
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;
9a619af0 8388
94af9270
KS
8389 return die_needs_namespace (die_specification (die, &spec_cu),
8390 spec_cu);
8391 }
8392
1c809c68 8393 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8394 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8395 && die->parent->tag != DW_TAG_module)
1c809c68
TT
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
1054b214
TT
8402 || die->parent->tag == DW_TAG_catch_block
8403 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8404 return 0;
8405 return 1;
94af9270
KS
8406
8407 default:
8408 return 0;
8409 }
8410}
8411
8412/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8413 compute the physname for the object, which include a method's:
9c37b5ae 8414 - formal parameters (C++),
a766d390 8415 - receiver type (Go),
a766d390
DE
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.
94af9270 8420
af6b7be1
JB
8421 For Ada, return the DIE's linkage name rather than the fully qualified
8422 name. PHYSNAME is ignored..
8423
94af9270
KS
8424 The result is allocated on the objfile_obstack and canonicalized. */
8425
8426static const char *
15d034d0
TT
8427dwarf2_compute_name (const char *name,
8428 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8429 int physname)
8430{
bb5ed363
DE
8431 struct objfile *objfile = cu->objfile;
8432
94af9270
KS
8433 if (name == NULL)
8434 name = dwarf2_name (die, cu);
8435
2ee7123e
DE
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. */
f55ee35c
JK
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. */
2ee7123e 8451 const char *linkage_name;
f55ee35c 8452
2ee7123e
DE
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;
f55ee35c
JK
8458 }
8459
94af9270
KS
8460 /* These are the only languages we know how to qualify names in. */
8461 if (name != NULL
9c37b5ae 8462 && (cu->language == language_cplus
c44af4eb
TT
8463 || cu->language == language_fortran || cu->language == language_d
8464 || cu->language == language_rust))
94af9270
KS
8465 {
8466 if (die_needs_namespace (die, cu))
8467 {
8468 long length;
0d5cff50 8469 const char *prefix;
34a68019 8470 const char *canonical_name = NULL;
94af9270 8471
d7e74731
PA
8472 string_file buf;
8473
94af9270 8474 prefix = determine_prefix (die, cu);
94af9270
KS
8475 if (*prefix != '\0')
8476 {
f55ee35c
JK
8477 char *prefixed_name = typename_concat (NULL, prefix, name,
8478 physname, cu);
9a619af0 8479
d7e74731 8480 buf.puts (prefixed_name);
94af9270
KS
8481 xfree (prefixed_name);
8482 }
8483 else
d7e74731 8484 buf.puts (name);
94af9270 8485
98bfdba5
PA
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;
12df843f 8518 LONGEST value;
d521ce57 8519 const gdb_byte *bytes;
98bfdba5
PA
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 {
d7e74731 8529 buf.puts ("<");
98bfdba5
PA
8530 first = 0;
8531 }
8532 else
d7e74731 8533 buf.puts (", ");
98bfdba5
PA
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"));
d7e74731 8540 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8541 continue;
8542 }
8543 type = die_type (child, cu);
8544
8545 if (child->tag == DW_TAG_template_type_param)
8546 {
d7e74731 8547 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8548 continue;
8549 }
8550
8551 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8552 if (attr == NULL)
8553 {
8554 complaint (&symfile_complaints,
3e43a32a
MS
8555 _("template parameter missing "
8556 "DW_AT_const_value"));
d7e74731 8557 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
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. */
d7e74731 8568 c_printchar (value, type, &buf);
98bfdba5
PA
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
3e43a32a
MS
8587 /* Specify decimal so that we do not depend on
8588 the radix. */
98bfdba5
PA
8589 get_formatted_print_options (&opts, 'd');
8590 opts.raw = 1;
d7e74731 8591 value_print (v, &buf, &opts);
98bfdba5
PA
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). */
d7e74731
PA
8603 if (!buf.empty () && buf.string ().back () == '>')
8604 buf.puts (" >");
98bfdba5 8605 else
d7e74731 8606 buf.puts (">");
98bfdba5
PA
8607 }
8608 }
8609
9c37b5ae 8610 /* For C++ methods, append formal parameter type
94af9270 8611 information, if PHYSNAME. */
6e70227d 8612
94af9270 8613 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8614 && cu->language == language_cplus)
94af9270
KS
8615 {
8616 struct type *type = read_type_die (die, cu);
8617
d7e74731 8618 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8619 &type_print_raw_options);
94af9270 8620
9c37b5ae 8621 if (cu->language == language_cplus)
94af9270 8622 {
60430eff
DJ
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. */
94af9270
KS
8628 if (TYPE_NFIELDS (type) > 0
8629 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8630 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8631 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8632 0))))
d7e74731 8633 buf.puts (" const");
94af9270
KS
8634 }
8635 }
8636
d7e74731 8637 const std::string &intermediate_name = buf.string ();
94af9270
KS
8638
8639 if (cu->language == language_cplus)
34a68019 8640 canonical_name
322a8516 8641 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
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. */
322a8516 8647 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8648 name = ((const char *)
8649 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8650 intermediate_name.c_str (),
8651 intermediate_name.length ()));
34a68019
TT
8652 else
8653 name = canonical_name;
94af9270
KS
8654 }
8655 }
8656
8657 return name;
8658}
8659
0114d602
DJ
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
34a68019 8662 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8663 not have a name. NAME may either be from a previous call to
8664 dwarf2_name or NULL.
8665
9c37b5ae 8666 The output string will be canonicalized (if C++). */
0114d602
DJ
8667
8668static const char *
15d034d0 8669dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8670{
94af9270
KS
8671 return dwarf2_compute_name (name, die, cu, 0);
8672}
0114d602 8673
94af9270
KS
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.
0114d602 8678
9c37b5ae 8679 The output string will be canonicalized (if C++). */
0114d602 8680
94af9270 8681static const char *
15d034d0 8682dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8683{
bb5ed363 8684 struct objfile *objfile = cu->objfile;
900e11f9
JK
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
7d45c7c3
KB
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);
900e11f9 8699
e98c9e7c
TT
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
900e11f9
JK
8706 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8707 has computed. */
7d45c7c3 8708 if (mangled != NULL)
900e11f9
JK
8709 {
8710 char *demangled;
8711
900e11f9
JK
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
a766d390
DE
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 {
8de20a37 8730 demangled = gdb_demangle (mangled,
9c37b5ae 8731 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 8732 }
900e11f9
JK
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]"),
4262abfb
JK
8758 physname, canon, mangled, die->offset.sect_off,
8759 objfile_name (objfile));
900e11f9
JK
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)
224c3ddb
SM
8777 retval = ((const char *)
8778 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8779 retval, strlen (retval)));
900e11f9
JK
8780
8781 do_cleanups (back_to);
8782 return retval;
0114d602
DJ
8783}
8784
74921315
KS
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
8790static int
8791read_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
22cee43f
PMR
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
8854static struct using_direct **
8855using_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
27aa8d6a
SW
8863/* Read the import statement specified by the given die and record it. */
8864
8865static void
8866read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8867{
bb5ed363 8868 struct objfile *objfile = cu->objfile;
27aa8d6a 8869 struct attribute *import_attr;
32019081 8870 struct die_info *imported_die, *child_die;
de4affc9 8871 struct dwarf2_cu *imported_cu;
27aa8d6a 8872 const char *imported_name;
794684b6 8873 const char *imported_name_prefix;
13387711
SW
8874 const char *canonical_name;
8875 const char *import_alias;
8876 const char *imported_declaration = NULL;
794684b6 8877 const char *import_prefix;
32019081
JK
8878 VEC (const_char_ptr) *excludes = NULL;
8879 struct cleanup *cleanups;
13387711 8880
27aa8d6a
SW
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
de4affc9
CC
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);
27aa8d6a
SW
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
82856980
SW
8929 /* Figure out the local name after import. */
8930 import_alias = dwarf2_name (die, cu);
27aa8d6a 8931
794684b6
SW
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. */
de4affc9 8937 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8938
f55ee35c
JK
8939 if (imported_die->tag != DW_TAG_namespace
8940 && imported_die->tag != DW_TAG_module)
794684b6 8941 {
13387711
SW
8942 imported_declaration = imported_name;
8943 canonical_name = imported_name_prefix;
794684b6 8944 }
13387711 8945 else if (strlen (imported_name_prefix) > 0)
12aaed36 8946 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
8947 imported_name_prefix,
8948 (cu->language == language_d ? "." : "::"),
8949 imported_name, (char *) NULL);
13387711
SW
8950 else
8951 canonical_name = imported_name;
794684b6 8952
32019081
JK
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]"),
4262abfb 8969 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
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]"),
4262abfb 8990 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
8991 continue;
8992 }
8993
8994 VEC_safe_push (const_char_ptr, excludes, imported_name);
8995
8996 process_die (child_die, cu);
8997 }
8998
22cee43f
PMR
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);
32019081
JK
9007
9008 do_cleanups (cleanups);
27aa8d6a
SW
9009}
9010
f4dc4d17 9011/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 9012
cb1df416
DJ
9013static void
9014free_cu_line_header (void *arg)
9015{
9a3c8263 9016 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
cb1df416
DJ
9017
9018 free_line_header (cu->line_header);
9019 cu->line_header = NULL;
9020}
9021
1b80a9fa
JK
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
9026static int
9027producer_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
9291a0cd
TT
9035static void
9036find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 9037 const char **name, const char **comp_dir)
9291a0cd 9038{
9291a0cd
TT
9039 /* Find the filename. Do not use dwarf2_name here, since the filename
9040 is not a source language identifier. */
7d45c7c3
KB
9041 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9042 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9043
7d45c7c3
KB
9044 if (*comp_dir == NULL
9045 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9046 && IS_ABSOLUTE_PATH (*name))
9291a0cd 9047 {
15d034d0
TT
9048 char *d = ldirname (*name);
9049
9050 *comp_dir = d;
9051 if (d != NULL)
9052 make_cleanup (xfree, d);
9291a0cd
TT
9053 }
9054 if (*comp_dir != NULL)
9055 {
9056 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9057 directory, get rid of it. */
e6a959d6 9058 const char *cp = strchr (*comp_dir, ':');
9291a0cd
TT
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
f4dc4d17
DE
9068/* Handle DW_AT_stmt_list for a compilation unit.
9069 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9070 COMP_DIR is the compilation directory. LOWPC is passed to
9071 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9072
9073static void
9074handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9075 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9076{
527f3840 9077 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9078 struct attribute *attr;
527f3840
JK
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;
2ab95328 9085
f4dc4d17
DE
9086 gdb_assert (! cu->per_cu->is_debug_types);
9087
2ab95328 9088 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
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)
2ab95328 9102 {
527f3840
JK
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 }
2ab95328 9111
527f3840
JK
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)
dee91e82 9125 {
527f3840 9126 gdb_assert (*slot != NULL);
9a3c8263 9127 cu->line_header = (struct line_header *) *slot;
527f3840 9128 return;
dee91e82 9129 }
2ab95328 9130 }
527f3840
JK
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);
2ab95328
TT
9166}
9167
95554aad 9168/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9169
c906108c 9170static void
e7c27a73 9171read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9172{
dee91e82 9173 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9174 struct gdbarch *gdbarch = get_objfile_arch (objfile);
debd256d 9175 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 9176 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9177 CORE_ADDR highpc = ((CORE_ADDR) 0);
9178 struct attribute *attr;
15d034d0
TT
9179 const char *name = NULL;
9180 const char *comp_dir = NULL;
c906108c 9181 struct die_info *child_die;
e142c38c 9182 CORE_ADDR baseaddr;
6e70227d 9183
e142c38c 9184 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9185
fae299cd 9186 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9187
9188 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9189 from finish_block. */
2acceee2 9190 if (lowpc == ((CORE_ADDR) -1))
c906108c 9191 lowpc = highpc;
3e29f34a 9192 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9193
9291a0cd 9194 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 9195
95554aad 9196 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9197
f4b8a18d
KW
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
3019eac3
DE
9204 /* Similar hack for Go. */
9205 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9206 set_cu_language (DW_LANG_Go, cu);
9207
f4dc4d17 9208 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
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. */
c3b7b696 9213 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
3019eac3
DE
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
43f3e411 9237 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
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
43f3e411 9246 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9247 }
9248 }
9249
9250 do_cleanups (back_to);
9251}
9252
f4dc4d17
DE
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. */
3019eac3
DE
9258
9259static void
f4dc4d17 9260setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9261{
f4dc4d17
DE
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;
3019eac3 9266 struct attribute *attr;
f4dc4d17 9267 unsigned int i, line_offset;
0186c6a7 9268 struct signatured_type *sig_type;
3019eac3 9269
f4dc4d17 9270 gdb_assert (per_cu->is_debug_types);
0186c6a7 9271 sig_type = (struct signatured_type *) per_cu;
3019eac3 9272
f4dc4d17 9273 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9274
f4dc4d17 9275 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9276 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
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;
f4dc4d17
DE
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. */
43f3e411 9285 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
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)
3019eac3 9291 {
f4dc4d17
DE
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);
0ab9ce85 9302 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9303 }
f4dc4d17 9304 return;
3019eac3
DE
9305 }
9306
f4dc4d17
DE
9307 cu->line_header = lh;
9308 make_cleanup (free_cu_line_header, cu);
3019eac3 9309
f4dc4d17
DE
9310 if (first_time)
9311 {
43f3e411 9312 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9313
1fd60fc0
DE
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
f4dc4d17
DE
9319 tu_group->num_symtabs = lh->num_file_names;
9320 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 9321
f4dc4d17
DE
9322 for (i = 0; i < lh->num_file_names; ++i)
9323 {
d521ce57 9324 const char *dir = NULL;
f4dc4d17 9325 struct file_entry *fe = &lh->file_names[i];
3019eac3 9326
afa6c9ab 9327 if (fe->dir_index && lh->include_dirs != NULL)
f4dc4d17 9328 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 9329 dwarf2_start_subfile (fe->name, dir);
3019eac3 9330
f4dc4d17
DE
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
43f3e411 9335 simple mapping from lh->file_names to subfiles, plus
f4dc4d17 9336 lh->file_names may contain dups. */
43f3e411
DE
9337 current_subfile->symtab
9338 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9339 }
9340
9341 fe->symtab = current_subfile->symtab;
9342 tu_group->symtabs[i] = fe->symtab;
9343 }
9344 }
9345 else
3019eac3 9346 {
0ab9ce85 9347 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17
DE
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 }
3019eac3
DE
9355 }
9356
f4dc4d17
DE
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}
3019eac3 9363
f4dc4d17
DE
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. */
3019eac3 9368
f4dc4d17
DE
9369static void
9370read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9371{
9372 struct die_info *child_die;
3019eac3 9373
f4dc4d17
DE
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 }
3019eac3
DE
9390}
9391\f
80626a55
DE
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. */
3019eac3
DE
9400
9401static hashval_t
9402hash_dwo_file (const void *item)
9403{
9a3c8263 9404 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9405 hashval_t hash;
3019eac3 9406
a2ce51a0
DE
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;
3019eac3
DE
9411}
9412
9413static int
9414eq_dwo_file (const void *item_lhs, const void *item_rhs)
9415{
9a3c8263
SM
9416 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9417 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9418
a2ce51a0
DE
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;
3019eac3
DE
9424}
9425
9426/* Allocate a hash table for DWO files. */
9427
9428static htab_t
9429allocate_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
80626a55
DE
9442/* Lookup DWO file DWO_NAME. */
9443
9444static void **
0ac5b59e 9445lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
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));
0ac5b59e
DE
9454 find_entry.dwo_name = dwo_name;
9455 find_entry.comp_dir = comp_dir;
80626a55
DE
9456 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9457
9458 return slot;
9459}
9460
3019eac3
DE
9461static hashval_t
9462hash_dwo_unit (const void *item)
9463{
9a3c8263 9464 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9465
9466 /* This drops the top 32 bits of the id, but is ok for a hash. */
9467 return dwo_unit->signature;
9468}
9469
9470static int
9471eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9472{
9a3c8263
SM
9473 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9474 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
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
9486static htab_t
9487allocate_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
80626a55 9500/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9501
19c3d4c9 9502struct create_dwo_cu_data
3019eac3
DE
9503{
9504 struct dwo_file *dwo_file;
19c3d4c9 9505 struct dwo_unit dwo_unit;
3019eac3
DE
9506};
9507
19c3d4c9 9508/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9509
9510static void
19c3d4c9
DE
9511create_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)
3019eac3
DE
9516{
9517 struct dwarf2_cu *cu = reader->cu;
3019eac3 9518 sect_offset offset = cu->per_cu->offset;
8a0459fd 9519 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9520 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9521 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9522 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9523 struct attribute *attr;
3019eac3
DE
9524
9525 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9526 if (attr == NULL)
9527 {
19c3d4c9
DE
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);
3019eac3
DE
9532 return;
9533 }
9534
3019eac3
DE
9535 dwo_unit->dwo_file = dwo_file;
9536 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9537 dwo_unit->section = section;
3019eac3
DE
9538 dwo_unit->offset = offset;
9539 dwo_unit->length = cu->per_cu->length;
9540
b4f54984 9541 if (dwarf_read_debug)
4031ecc5
DE
9542 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9543 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
9544}
9545
19c3d4c9
DE
9546/* Create the dwo_unit for the lone CU in DWO_FILE.
9547 Note: This function processes DWO files only, not DWP files. */
3019eac3 9548
19c3d4c9
DE
9549static struct dwo_unit *
9550create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
9551{
9552 struct objfile *objfile = dwarf2_per_objfile->objfile;
9553 struct dwarf2_section_info *section = &dwo_file->sections.info;
d521ce57 9554 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
9555 struct create_dwo_cu_data create_dwo_cu_data;
9556 struct dwo_unit *dwo_unit;
3019eac3
DE
9557
9558 dwarf2_read_section (objfile, section);
9559 info_ptr = section->buffer;
9560
9561 if (info_ptr == NULL)
9562 return NULL;
9563
b4f54984 9564 if (dwarf_read_debug)
19c3d4c9
DE
9565 {
9566 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
9567 get_section_name (section),
9568 get_section_file_name (section));
19c3d4c9 9569 }
3019eac3 9570
19c3d4c9
DE
9571 create_dwo_cu_data.dwo_file = dwo_file;
9572 dwo_unit = NULL;
3019eac3
DE
9573
9574 end_ptr = info_ptr + section->size;
9575 while (info_ptr < end_ptr)
9576 {
9577 struct dwarf2_per_cu_data per_cu;
9578
19c3d4c9
DE
9579 memset (&create_dwo_cu_data.dwo_unit, 0,
9580 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
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;
8a0459fd 9585 per_cu.section = section;
3019eac3 9586
33e80786 9587 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
19c3d4c9
DE
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]"),
4262abfb 9600 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
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 }
3019eac3
DE
9607
9608 info_ptr += per_cu.length;
9609 }
9610
19c3d4c9 9611 return dwo_unit;
3019eac3
DE
9612}
9613
80626a55
DE
9614/* DWP file .debug_{cu,tu}_index section format:
9615 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9616
d2415c6c
DE
9617 DWP Version 1:
9618
80626a55
DE
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
d2415c6c
DE
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
80626a55 9631
d2415c6c 9632 Numbers are recorded using the byte order of the application binary.
80626a55 9633
d2415c6c
DE
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.)
80626a55 9638
d2415c6c
DE
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.
80626a55 9645
73869dc2
DE
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
d2415c6c
DE
9729 Given a 64-bit compilation unit signature or a type signature S, an entry
9730 in the hash table is located as follows:
80626a55 9731
d2415c6c
DE
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.
80626a55 9734
d2415c6c 9735 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9736
d2415c6c
DE
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.
80626a55 9740
d2415c6c 9741 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9742
d2415c6c 9743 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 9744 to stop at an unused slot or find the match. */
80626a55
DE
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
9751static struct dwp_hash_table *
9752create_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;
948f8e3d 9756 const gdb_byte *index_ptr, *index_end;
80626a55 9757 struct dwarf2_section_info *index;
73869dc2 9758 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
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);
73869dc2
DE
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;
80626a55
DE
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
73869dc2 9785 if (version != 1 && version != 2)
80626a55 9786 {
21aa081e 9787 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9788 " [in module %s]"),
21aa081e 9789 pulongest (version), dwp_file->name);
80626a55
DE
9790 }
9791 if (nr_slots != (nr_slots & -nr_slots))
9792 {
21aa081e 9793 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9794 " is not power of 2 [in module %s]"),
21aa081e 9795 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9796 }
9797
9798 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
9799 htab->version = version;
9800 htab->nr_columns = nr_columns;
80626a55
DE
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;
73869dc2
DE
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 }
80626a55
DE
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
73869dc2 9907 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
9908
9909 The result is non-zero for success, or zero if an error was found. */
9910
9911static int
73869dc2
DE
9912locate_v1_virtual_dwo_sections (asection *sectp,
9913 struct virtual_v1_dwo_sections *sections)
80626a55
DE
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. */
049412e3 9920 if (sections->abbrev.s.section != NULL)
80626a55 9921 return 0;
049412e3 9922 sections->abbrev.s.section = sectp;
80626a55
DE
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. */
049412e3 9929 if (sections->info_or_types.s.section != NULL)
80626a55 9930 return 0;
049412e3 9931 sections->info_or_types.s.section = sectp;
80626a55
DE
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. */
049412e3 9937 if (sections->line.s.section != NULL)
80626a55 9938 return 0;
049412e3 9939 sections->line.s.section = sectp;
80626a55
DE
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. */
049412e3 9945 if (sections->loc.s.section != NULL)
80626a55 9946 return 0;
049412e3 9947 sections->loc.s.section = sectp;
80626a55
DE
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. */
049412e3 9953 if (sections->macinfo.s.section != NULL)
80626a55 9954 return 0;
049412e3 9955 sections->macinfo.s.section = sectp;
80626a55
DE
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. */
049412e3 9961 if (sections->macro.s.section != NULL)
80626a55 9962 return 0;
049412e3 9963 sections->macro.s.section = sectp;
80626a55
DE
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. */
049412e3 9969 if (sections->str_offsets.s.section != NULL)
80626a55 9970 return 0;
049412e3 9971 sections->str_offsets.s.section = sectp;
80626a55
DE
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
73869dc2
DE
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. */
80626a55
DE
9987
9988static struct dwo_unit *
73869dc2
DE
9989create_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)
80626a55
DE
9993{
9994 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
9995 const struct dwp_hash_table *dwp_htab =
9996 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
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;
73869dc2 10001 struct virtual_v1_dwo_sections sections;
80626a55
DE
10002 void **dwo_file_slot;
10003 char *virtual_dwo_name;
80626a55
DE
10004 struct cleanup *cleanups;
10005 int i;
10006
73869dc2
DE
10007 gdb_assert (dwp_file->version == 1);
10008
b4f54984 10009 if (dwarf_read_debug)
80626a55 10010 {
73869dc2 10011 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10012 kind,
73869dc2 10013 pulongest (unit_index), hex_string (signature),
80626a55
DE
10014 dwp_file->name);
10015 }
10016
19ac8c2e 10017 /* Fetch the sections of this DWO unit.
80626a55
DE
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
73869dc2 10021#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10022 (1 /* .debug_info or .debug_types */ \
10023 + 1 /* .debug_abbrev */ \
10024 + 1 /* .debug_line */ \
10025 + 1 /* .debug_loc */ \
10026 + 1 /* .debug_str_offsets */ \
19ac8c2e 10027 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10028 + 1 /* trailing zero */)
10029
10030 memset (&sections, 0, sizeof (sections));
10031 cleanups = make_cleanup (null_cleanup, 0);
10032
73869dc2 10033 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10034 {
10035 asection *sectp;
10036 uint32_t section_nr =
10037 read_4_bytes (dbfd,
73869dc2
DE
10038 dwp_htab->section_pool.v1.indices
10039 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
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];
73869dc2 10051 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
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
a32a8923
DE
10060 || dwarf2_section_empty_p (&sections.info_or_types)
10061 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10062 {
10063 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10064 " [in module %s]"),
10065 dwp_file->name);
10066 }
73869dc2 10067 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
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.
c766f7ec 10078 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
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
80626a55
DE
10081 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10082
2792b94d
PM
10083 virtual_dwo_name =
10084 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
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));
80626a55
DE
10089 make_cleanup (xfree, virtual_dwo_name);
10090 /* Can we use an existing virtual DWO file? */
0ac5b59e 10091 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10092 /* Create one if necessary. */
10093 if (*dwo_file_slot == NULL)
10094 {
b4f54984 10095 if (dwarf_read_debug)
80626a55
DE
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);
224c3ddb
SM
10101 dwo_file->dwo_name
10102 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10103 virtual_dwo_name,
10104 strlen (virtual_dwo_name));
0ac5b59e 10105 dwo_file->comp_dir = comp_dir;
80626a55
DE
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;
57d63ce2 10114 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
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
57d63ce2
DE
10119 for it, invalidating all copies of pointers into the previous
10120 contents. */
80626a55
DE
10121 *dwo_file_slot = dwo_file;
10122 }
10123 else
10124 {
b4f54984 10125 if (dwarf_read_debug)
80626a55
DE
10126 {
10127 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10128 virtual_dwo_name);
10129 }
9a3c8263 10130 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
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;
8d749320
SM
10137 dwo_unit->section =
10138 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10139 *dwo_unit->section = sections.info_or_types;
57d63ce2 10140 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10141
10142 return dwo_unit;
10143}
10144
73869dc2
DE
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
10150static struct dwarf2_section_info
10151create_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
10193static struct dwo_unit *
10194create_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;
73869dc2
DE
10209 struct cleanup *cleanups;
10210 int i;
10211
10212 gdb_assert (dwp_file->version == 2);
10213
b4f54984 10214 if (dwarf_read_debug)
73869dc2
DE
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 {
b4f54984 10296 if (dwarf_read_debug)
73869dc2
DE
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);
224c3ddb
SM
10302 dwo_file->dwo_name
10303 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10304 virtual_dwo_name,
10305 strlen (virtual_dwo_name));
73869dc2
DE
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 {
b4f54984 10339 if (dwarf_read_debug)
73869dc2
DE
10340 {
10341 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10342 virtual_dwo_name);
10343 }
9a3c8263 10344 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
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;
8d749320
SM
10351 dwo_unit->section =
10352 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
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
57d63ce2
DE
10363/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10364 Returns NULL if the signature isn't found. */
80626a55
DE
10365
10366static struct dwo_unit *
57d63ce2
DE
10367lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10368 ULONGEST signature, int is_debug_types)
80626a55 10369{
57d63ce2
DE
10370 const struct dwp_hash_table *dwp_htab =
10371 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10372 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10373 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10374 uint32_t hash = signature & mask;
10375 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10376 unsigned int i;
10377 void **slot;
870f88f7 10378 struct dwo_unit find_dwo_cu;
80626a55
DE
10379
10380 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10381 find_dwo_cu.signature = signature;
19ac8c2e
DE
10382 slot = htab_find_slot (is_debug_types
10383 ? dwp_file->loaded_tus
10384 : dwp_file->loaded_cus,
10385 &find_dwo_cu, INSERT);
80626a55
DE
10386
10387 if (*slot != NULL)
9a3c8263 10388 return (struct dwo_unit *) *slot;
80626a55
DE
10389
10390 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10391 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10392 {
10393 ULONGEST signature_in_table;
10394
10395 signature_in_table =
57d63ce2 10396 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10397 if (signature_in_table == signature)
10398 {
57d63ce2
DE
10399 uint32_t unit_index =
10400 read_4_bytes (dbfd,
10401 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10402
73869dc2
DE
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 }
9a3c8263 10415 return (struct dwo_unit *) *slot;
80626a55
DE
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
ab5088bf 10427/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
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.
80626a55 10431 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10432 SEARCH_CWD is true if the current directory is to be searched.
10433 It will be searched before debug-file-directory.
13aaf454
DE
10434 If successful, the file is added to the bfd include table of the
10435 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10436 If unable to find/open the file, return NULL.
3019eac3
DE
10437 NOTE: This function is derived from symfile_bfd_open. */
10438
192b62ce 10439static gdb_bfd_ref_ptr
6ac97d4c 10440try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10441{
80626a55 10442 int desc, flags;
3019eac3 10443 char *absolute_name;
9c02c129
DE
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
6ac97d4c
DE
10450 if (search_cwd)
10451 {
10452 if (*debug_file_directory != '\0')
10453 search_path = concat (".", dirname_separator_string,
b36cec19 10454 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10455 else
10456 search_path = xstrdup (".");
10457 }
9c02c129 10458 else
6ac97d4c 10459 search_path = xstrdup (debug_file_directory);
3019eac3 10460
492c0ab7 10461 flags = OPF_RETURN_REALPATH;
80626a55
DE
10462 if (is_dwp)
10463 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10464 desc = openp (search_path, flags, file_name,
3019eac3 10465 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10466 xfree (search_path);
3019eac3
DE
10467 if (desc < 0)
10468 return NULL;
10469
192b62ce 10470 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10471 xfree (absolute_name);
9c02c129
DE
10472 if (sym_bfd == NULL)
10473 return NULL;
192b62ce 10474 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10475
192b62ce
TT
10476 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10477 return NULL;
3019eac3 10478
13aaf454
DE
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. */
192b62ce 10483 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10484
3019eac3
DE
10485 return sym_bfd;
10486}
10487
ab5088bf 10488/* Try to open DWO file FILE_NAME.
3019eac3
DE
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
192b62ce 10495static gdb_bfd_ref_ptr
ab5088bf 10496open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10497{
80626a55 10498 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10499 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10500
10501 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10502
10503 if (comp_dir != NULL)
10504 {
b36cec19
PA
10505 char *path_to_try = concat (comp_dir, SLASH_STRING,
10506 file_name, (char *) NULL);
3019eac3
DE
10507
10508 /* NOTE: If comp_dir is a relative path, this will also try the
10509 search path, which seems useful. */
192b62ce
TT
10510 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10511 1 /*search_cwd*/));
3019eac3
DE
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
6ac97d4c 10523 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10524}
10525
80626a55
DE
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
10529static void
10530dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10531{
9a3c8263 10532 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10533 const struct dwop_section_names *names = &dwop_section_names;
10534
10535 if (section_is_p (sectp->name, &names->abbrev_dwo))
10536 {
049412e3 10537 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10538 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10539 }
10540 else if (section_is_p (sectp->name, &names->info_dwo))
10541 {
049412e3 10542 dwo_sections->info.s.section = sectp;
80626a55
DE
10543 dwo_sections->info.size = bfd_get_section_size (sectp);
10544 }
10545 else if (section_is_p (sectp->name, &names->line_dwo))
10546 {
049412e3 10547 dwo_sections->line.s.section = sectp;
80626a55
DE
10548 dwo_sections->line.size = bfd_get_section_size (sectp);
10549 }
10550 else if (section_is_p (sectp->name, &names->loc_dwo))
10551 {
049412e3 10552 dwo_sections->loc.s.section = sectp;
80626a55
DE
10553 dwo_sections->loc.size = bfd_get_section_size (sectp);
10554 }
10555 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10556 {
049412e3 10557 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10558 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10559 }
10560 else if (section_is_p (sectp->name, &names->macro_dwo))
10561 {
049412e3 10562 dwo_sections->macro.s.section = sectp;
80626a55
DE
10563 dwo_sections->macro.size = bfd_get_section_size (sectp);
10564 }
10565 else if (section_is_p (sectp->name, &names->str_dwo))
10566 {
049412e3 10567 dwo_sections->str.s.section = sectp;
80626a55
DE
10568 dwo_sections->str.size = bfd_get_section_size (sectp);
10569 }
10570 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10571 {
049412e3 10572 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
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));
049412e3 10580 type_section.s.section = sectp;
80626a55
DE
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
ab5088bf 10587/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10588 by PER_CU. This is for the non-DWP case.
80626a55 10589 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10590
10591static struct dwo_file *
0ac5b59e
DE
10592open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10593 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10594{
10595 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10596 struct dwo_file *dwo_file;
3019eac3
DE
10597 struct cleanup *cleanups;
10598
192b62ce 10599 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10600 if (dbfd == NULL)
10601 {
b4f54984 10602 if (dwarf_read_debug)
80626a55
DE
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);
0ac5b59e
DE
10607 dwo_file->dwo_name = dwo_name;
10608 dwo_file->comp_dir = comp_dir;
192b62ce 10609 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10610
10611 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10612
192b62ce
TT
10613 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10614 &dwo_file->sections);
3019eac3 10615
19c3d4c9 10616 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
10617
10618 dwo_file->tus = create_debug_types_hash_table (dwo_file,
10619 dwo_file->sections.types);
10620
10621 discard_cleanups (cleanups);
10622
b4f54984 10623 if (dwarf_read_debug)
80626a55
DE
10624 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10625
3019eac3
DE
10626 return dwo_file;
10627}
10628
80626a55 10629/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10630 size of each of the DWP debugging sections common to version 1 and 2 that
10631 we are interested in. */
3019eac3 10632
80626a55 10633static void
73869dc2
DE
10634dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10635 void *dwp_file_ptr)
3019eac3 10636{
9a3c8263 10637 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10638 const struct dwop_section_names *names = &dwop_section_names;
10639 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10640
80626a55 10641 /* Record the ELF section number for later lookup: this is what the
73869dc2 10642 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10643 gdb_assert (elf_section_nr < dwp_file->num_sections);
10644 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10645
80626a55
DE
10646 /* Look for specific sections that we need. */
10647 if (section_is_p (sectp->name, &names->str_dwo))
10648 {
049412e3 10649 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10650 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10651 }
10652 else if (section_is_p (sectp->name, &names->cu_index))
10653 {
049412e3 10654 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
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 {
049412e3 10659 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10660 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10661 }
10662}
3019eac3 10663
73869dc2
DE
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
10669static void
10670dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10671{
9a3c8263 10672 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
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 {
049412e3 10684 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
10685 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10686 }
10687 else if (section_is_p (sectp->name, &names->info_dwo))
10688 {
049412e3 10689 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
10690 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10691 }
10692 else if (section_is_p (sectp->name, &names->line_dwo))
10693 {
049412e3 10694 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
10695 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10696 }
10697 else if (section_is_p (sectp->name, &names->loc_dwo))
10698 {
049412e3 10699 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
10700 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10701 }
10702 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10703 {
049412e3 10704 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
10705 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10706 }
10707 else if (section_is_p (sectp->name, &names->macro_dwo))
10708 {
049412e3 10709 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
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 {
049412e3 10714 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
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 {
049412e3 10719 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
10720 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10721 }
10722}
10723
80626a55 10724/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 10725
80626a55
DE
10726static hashval_t
10727hash_dwp_loaded_cutus (const void *item)
10728{
9a3c8263 10729 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 10730
80626a55
DE
10731 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10732 return dwo_unit->signature;
3019eac3
DE
10733}
10734
80626a55 10735/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 10736
80626a55
DE
10737static int
10738eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 10739{
9a3c8263
SM
10740 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10741 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 10742
80626a55
DE
10743 return dua->signature == dub->signature;
10744}
3019eac3 10745
80626a55 10746/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 10747
80626a55
DE
10748static htab_t
10749allocate_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}
3019eac3 10759
ab5088bf
DE
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
192b62ce 10766static gdb_bfd_ref_ptr
ab5088bf
DE
10767open_dwp_file (const char *file_name)
10768{
192b62ce
TT
10769 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10770 1 /*search_cwd*/));
6ac97d4c
DE
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;
ab5088bf
DE
10792}
10793
80626a55
DE
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. */
a766d390 10797
80626a55 10798static struct dwp_file *
ab5088bf 10799open_and_init_dwp_file (void)
80626a55
DE
10800{
10801 struct objfile *objfile = dwarf2_per_objfile->objfile;
10802 struct dwp_file *dwp_file;
10803 char *dwp_name;
6c447423 10804 struct cleanup *cleanups = make_cleanup (null_cleanup, 0);
80626a55 10805
82bf32bc
JK
10806 /* Try to find first .dwp for the binary file before any symbolic links
10807 resolving. */
6c447423
DE
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);
80626a55 10824
192b62ce 10825 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name));
82bf32bc
JK
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
80626a55
DE
10835 if (dbfd == NULL)
10836 {
b4f54984 10837 if (dwarf_read_debug)
80626a55
DE
10838 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10839 do_cleanups (cleanups);
10840 return NULL;
3019eac3 10841 }
80626a55 10842 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
10843 dwp_file->name = bfd_get_filename (dbfd.get ());
10844 dwp_file->dbfd = dbfd.release ();
80626a55 10845 do_cleanups (cleanups);
c906108c 10846
80626a55 10847 /* +1: section 0 is unused */
192b62ce 10848 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
10849 dwp_file->elf_sections =
10850 OBSTACK_CALLOC (&objfile->objfile_obstack,
10851 dwp_file->num_sections, asection *);
10852
192b62ce
TT
10853 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
10854 dwp_file);
80626a55
DE
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
73869dc2
DE
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
fbcbc3fd 10864 pretty bizarre. We use pulongest here because that's the established
4d65956b 10865 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
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);
73869dc2
DE
10870 }
10871 dwp_file->version = dwp_file->cus->version;
10872
10873 if (dwp_file->version == 2)
192b62ce
TT
10874 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
10875 dwp_file);
73869dc2 10876
19ac8c2e
DE
10877 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10878 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 10879
b4f54984 10880 if (dwarf_read_debug)
80626a55
DE
10881 {
10882 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10883 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
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));
80626a55
DE
10887 }
10888
10889 return dwp_file;
3019eac3 10890}
c906108c 10891
ab5088bf
DE
10892/* Wrapper around open_and_init_dwp_file, only open it once. */
10893
10894static struct dwp_file *
10895get_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
80626a55
DE
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.
3019eac3 10908 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
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
1c658ad5 10918 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 10919 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 10920
3019eac3 10921static struct dwo_unit *
80626a55
DE
10922lookup_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)
3019eac3
DE
10925{
10926 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
10927 const char *kind = is_debug_types ? "TU" : "CU";
10928 void **dwo_file_slot;
3019eac3 10929 struct dwo_file *dwo_file;
80626a55 10930 struct dwp_file *dwp_file;
cb1df416 10931
6a506a2d
DE
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. */
cf2c3c16 10936
ab5088bf 10937 dwp_file = get_dwp_file ();
80626a55 10938 if (dwp_file != NULL)
cf2c3c16 10939 {
80626a55
DE
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 =
57d63ce2
DE
10946 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
10947 signature, is_debug_types);
80626a55
DE
10948
10949 if (dwo_cutu != NULL)
10950 {
b4f54984 10951 if (dwarf_read_debug)
80626a55
DE
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 }
6a506a2d 10962 else
80626a55 10963 {
6a506a2d 10964 /* No DWP file, look for the DWO file. */
80626a55 10965
6a506a2d
DE
10966 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
10967 if (*dwo_file_slot == NULL)
80626a55 10968 {
6a506a2d
DE
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);
19c3d4c9 10971 }
6a506a2d 10972 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 10973 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 10974
6a506a2d 10975 if (dwo_file != NULL)
19c3d4c9 10976 {
6a506a2d
DE
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;
9a3c8263
SM
10985 dwo_cutu
10986 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d
DE
10987 }
10988 else if (!is_debug_types && dwo_file->cu)
80626a55 10989 {
6a506a2d
DE
10990 if (signature == dwo_file->cu->signature)
10991 dwo_cutu = dwo_file->cu;
10992 }
10993
10994 if (dwo_cutu != NULL)
10995 {
b4f54984 10996 if (dwarf_read_debug)
6a506a2d
DE
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;
80626a55
DE
11003 }
11004 }
2e276125 11005 }
9cdd5dbd 11006
80626a55
DE
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
b4f54984 11011 if (dwarf_read_debug)
80626a55
DE
11012 {
11013 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11014 kind, dwo_name, hex_string (signature));
11015 }
3019eac3 11016
6656a72d
DE
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). */
43942612
DE
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 }
3019eac3 11038 return NULL;
5fb290d7
DJ
11039}
11040
80626a55
DE
11041/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11042 See lookup_dwo_cutu_unit for details. */
11043
11044static struct dwo_unit *
11045lookup_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
11055static struct dwo_unit *
11056lookup_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
89e63ee4
DE
11062/* Traversal function for queue_and_load_all_dwo_tus. */
11063
11064static int
11065queue_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
11093static void
11094queue_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
3019eac3
DE
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. */
348e048f
DE
11114
11115static void
3019eac3 11116free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11117{
348e048f 11118
5c6fa7ab 11119 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11120 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11121
3019eac3
DE
11122 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11123}
348e048f 11124
3019eac3 11125/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11126
3019eac3
DE
11127static void
11128free_dwo_file_cleanup (void *arg)
11129{
11130 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11131 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11132
3019eac3
DE
11133 free_dwo_file (dwo_file, objfile);
11134}
348e048f 11135
3019eac3 11136/* Traversal function for free_dwo_files. */
2ab95328 11137
3019eac3
DE
11138static int
11139free_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;
348e048f 11143
3019eac3 11144 free_dwo_file (dwo_file, objfile);
348e048f 11145
3019eac3
DE
11146 return 1;
11147}
348e048f 11148
3019eac3 11149/* Free all resources associated with DWO_FILES. */
348e048f 11150
3019eac3
DE
11151static void
11152free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11153{
11154 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11155}
3019eac3
DE
11156\f
11157/* Read in various DIEs. */
348e048f 11158
d389af10
JK
11159/* qsort helper for inherit_abstract_dies. */
11160
11161static int
11162unsigned_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).
3e43a32a
MS
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. */
d389af10
JK
11174
11175static void
11176inherit_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. */
b64f50a1
JK
11181 sect_offset *offsets;
11182 sect_offset *offsets_end, *offsetp;
d389af10
JK
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;
cd02d79d
PA
11189 struct dwarf2_cu *origin_cu;
11190 struct pending **origin_previous_list_in_scope;
d389af10
JK
11191
11192 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11193 if (!attr)
11194 return;
11195
cd02d79d
PA
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
edb3359d
DJ
11207 if (die->tag != origin_die->tag
11208 && !(die->tag == DW_TAG_inlined_subroutine
11209 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11210 complaint (&symfile_complaints,
11211 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 11212 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
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 }
8d749320 11221 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11222 cleanups = make_cleanup (xfree, offsets);
11223
11224 offsets_end = offsets;
3ea89b92
PMR
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
c38f313d
DJ
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). */
3ea89b92
PMR
11245 child_origin_die = child_die;
11246 child_origin_cu = cu;
c38f313d
DJ
11247 while (1)
11248 {
cd02d79d
PA
11249 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11250 child_origin_cu);
c38f313d
DJ
11251 if (attr == NULL)
11252 break;
cd02d79d
PA
11253 child_origin_die = follow_die_ref (child_origin_die, attr,
11254 &child_origin_cu);
c38f313d
DJ
11255 }
11256
d389af10
JK
11257 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11258 counterpart may exist. */
c38f313d 11259 if (child_origin_die != child_die)
d389af10 11260 {
edb3359d
DJ
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))
d389af10
JK
11264 complaint (&symfile_complaints,
11265 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11266 "different tags"), child_die->offset.sect_off,
11267 child_origin_die->offset.sect_off);
c38f313d
DJ
11268 if (child_origin_die->parent != origin_die)
11269 complaint (&symfile_complaints,
11270 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11271 "different parents"), child_die->offset.sect_off,
11272 child_origin_die->offset.sect_off);
c38f313d
DJ
11273 else
11274 *offsets_end++ = child_origin_die->offset;
d389af10 11275 }
d389af10
JK
11276 }
11277 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11278 unsigned_int_compar);
11279 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 11280 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
11281 complaint (&symfile_complaints,
11282 _("Multiple children of DIE 0x%x refer "
11283 "to DIE 0x%x as their abstract origin"),
b64f50a1 11284 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
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? */
b64f50a1
JK
11291 while (offsetp < offsets_end
11292 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 11293 offsetp++;
b64f50a1
JK
11294 if (offsetp >= offsets_end
11295 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10 11296 {
adde2bff
DE
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);
d389af10
JK
11303 }
11304 origin_child_die = sibling_die (origin_child_die);
11305 }
cd02d79d 11306 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11307
11308 do_cleanups (cleanups);
11309}
11310
c906108c 11311static void
e7c27a73 11312read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11313{
e7c27a73 11314 struct objfile *objfile = cu->objfile;
3e29f34a 11315 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11316 struct context_stack *newobj;
c906108c
SS
11317 CORE_ADDR lowpc;
11318 CORE_ADDR highpc;
11319 struct die_info *child_die;
edb3359d 11320 struct attribute *attr, *call_line, *call_file;
15d034d0 11321 const char *name;
e142c38c 11322 CORE_ADDR baseaddr;
801e3a5b 11323 struct block *block;
edb3359d 11324 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11325 VEC (symbolp) *template_args = NULL;
11326 struct template_symbol *templ_func = NULL;
edb3359d
DJ
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 }
c906108c 11341
e142c38c
DJ
11342 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11343
94af9270 11344 name = dwarf2_name (die, cu);
c906108c 11345
e8d05480
JB
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,
b64f50a1
JK
11351 _("missing name for subprogram DIE at %d"),
11352 die->offset.sect_off);
e8d05480
JB
11353 return;
11354 }
11355
11356 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11357 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11358 <= PC_BOUNDS_INVALID)
e8d05480 11359 {
ae4d0c03
PM
11360 attr = dwarf2_attr (die, DW_AT_external, cu);
11361 if (!attr || !DW_UNSND (attr))
11362 complaint (&symfile_complaints,
3e43a32a
MS
11363 _("cannot get low and high bounds "
11364 "for subprogram DIE at %d"),
b64f50a1 11365 die->offset.sect_off);
e8d05480
JB
11366 return;
11367 }
c906108c 11368
3e29f34a
MR
11369 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11370 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11371
34eaf542
TT
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 {
e623cf5d 11379 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11380 templ_func->base.is_cplus_template_function = 1;
11381 break;
11382 }
11383 }
11384
fe978cb0
PA
11385 newobj = push_context (0, lowpc);
11386 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11387 (struct symbol *) templ_func);
4c2df51b 11388
4cecd739
DJ
11389 /* If there is a location expression for DW_AT_frame_base, record
11390 it. */
e142c38c 11391 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11392 if (attr)
fe978cb0 11393 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11394
63e43d3a
PMR
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 {
224c3ddb
SM
11400 newobj->static_link
11401 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11402 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11403 }
11404
e142c38c 11405 cu->list_in_scope = &local_symbols;
c906108c 11406
639d11d3 11407 if (die->child != NULL)
c906108c 11408 {
639d11d3 11409 child_die = die->child;
c906108c
SS
11410 while (child_die && child_die->tag)
11411 {
34eaf542
TT
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
f1078f66
DJ
11417 if (arg != NULL)
11418 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11419 }
11420 else
11421 process_die (child_die, cu);
c906108c
SS
11422 child_die = sibling_die (child_die);
11423 }
11424 }
11425
d389af10
JK
11426 inherit_abstract_dies (die, cu);
11427
4a811a97
UW
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
fe978cb0 11453 newobj = pop_context ();
c906108c 11454 /* Make a block for the local symbols within. */
fe978cb0 11455 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11456 newobj->static_link, lowpc, highpc);
801e3a5b 11457
df8a16a1 11458 /* For C++, set the block's scope. */
45280282
IB
11459 if ((cu->language == language_cplus
11460 || cu->language == language_fortran
c44af4eb
TT
11461 || cu->language == language_d
11462 || cu->language == language_rust)
4d4ec4e5 11463 && cu->processing_has_namespace_info)
195a3f6c
TT
11464 block_set_scope (block, determine_prefix (die, cu),
11465 &objfile->objfile_obstack);
df8a16a1 11466
801e3a5b
JB
11467 /* If we have address ranges, record them. */
11468 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11469
fe978cb0 11470 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11471
34eaf542
TT
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
8d749320
SM
11479 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11480 templ_func->n_template_arguments);
34eaf542
TT
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
208d8187
JB
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. */
fe978cb0 11491 local_symbols = newobj->locals;
22cee43f 11492 local_using_directives = newobj->local_using_directives;
208d8187 11493
921e78cf
JB
11494 /* If we've finished processing a top-level function, subsequent
11495 symbols go in the file symbol list. */
11496 if (outermost_context_p ())
e142c38c 11497 cu->list_in_scope = &file_symbols;
c906108c
SS
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
11503static void
e7c27a73 11504read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11505{
e7c27a73 11506 struct objfile *objfile = cu->objfile;
3e29f34a 11507 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11508 struct context_stack *newobj;
c906108c
SS
11509 CORE_ADDR lowpc, highpc;
11510 struct die_info *child_die;
e142c38c
DJ
11511 CORE_ADDR baseaddr;
11512
11513 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11514
11515 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11516 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11517 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11518 be nasty. Might be easier to properly extend generic blocks to
af34e669 11519 describe ranges. */
e385593e
JK
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 }
3e29f34a
MR
11534 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11535 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11536
11537 push_context (0, lowpc);
639d11d3 11538 if (die->child != NULL)
c906108c 11539 {
639d11d3 11540 child_die = die->child;
c906108c
SS
11541 while (child_die && child_die->tag)
11542 {
e7c27a73 11543 process_die (child_die, cu);
c906108c
SS
11544 child_die = sibling_die (child_die);
11545 }
11546 }
3ea89b92 11547 inherit_abstract_dies (die, cu);
fe978cb0 11548 newobj = pop_context ();
c906108c 11549
22cee43f 11550 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11551 {
801e3a5b 11552 struct block *block
63e43d3a 11553 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11554 newobj->start_addr, highpc);
801e3a5b
JB
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);
c906108c 11567 }
fe978cb0 11568 local_symbols = newobj->locals;
22cee43f 11569 local_using_directives = newobj->local_using_directives;
c906108c
SS
11570}
11571
96408a79
SA
11572/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
11573
11574static void
11575read_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]"),
4262abfb 11594 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11595 return;
11596 }
31aa7e4e 11597 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11598 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
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]"),
4262abfb
JK
11611 paddress (gdbarch, pc), die->offset.sect_off,
11612 objfile_name (objfile));
96408a79
SA
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]"),
4262abfb
JK
11627 child_die->tag, child_die->offset.sect_off,
11628 objfile_name (objfile));
96408a79
SA
11629 continue;
11630 }
11631
11632 nparams++;
11633 }
11634
224c3ddb
SM
11635 call_site
11636 = ((struct call_site *)
11637 obstack_alloc (&objfile->objfile_obstack,
11638 sizeof (*call_site)
11639 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
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]"),
4262abfb 11687 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
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
8d749320 11701 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
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 }
7771576e 11708 else if (attr_form_is_ref (attr))
96408a79 11709 {
96408a79
SA
11710 struct dwarf2_cu *target_cu = cu;
11711 struct die_info *target_die;
11712
ac9ec31b 11713 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
11714 gdb_assert (target_cu->objfile == objfile);
11715 if (die_is_declaration (target_die, target_cu))
11716 {
7d45c7c3 11717 const char *target_physname;
9112db09
JK
11718
11719 /* Prefer the mangled name; otherwise compute the demangled one. */
7d45c7c3
KB
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)
9112db09 11728 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
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]"),
4262abfb 11733 die->offset.sect_off, objfile_name (objfile));
96408a79 11734 else
7d455152 11735 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
11736 }
11737 else
11738 {
11739 CORE_ADDR lowpc;
11740
11741 /* DW_AT_entry_pc should be preferred. */
3a2b436a 11742 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 11743 <= PC_BOUNDS_INVALID)
96408a79
SA
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]"),
4262abfb 11747 die->offset.sect_off, objfile_name (objfile));
96408a79 11748 else
3e29f34a
MR
11749 {
11750 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11751 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11752 }
96408a79
SA
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]"),
4262abfb 11759 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
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 {
96408a79 11767 struct call_site_parameter *parameter;
1788b2d3 11768 struct attribute *loc, *origin;
96408a79
SA
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
1788b2d3
JK
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. */
96408a79 11782
24c5c679 11783 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 11784 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 11785 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
11786 {
11787 sect_offset offset;
11788
11789 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11790 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
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]"),
4262abfb 11800 child_die->offset.sect_off, objfile_name (objfile));
d76b7dbc
JK
11801 continue;
11802 }
1788b2d3
JK
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))
96408a79
SA
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]"),
4262abfb 11811 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11812 continue;
11813 }
24c5c679 11814 else
96408a79 11815 {
24c5c679
JK
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]"),
4262abfb 11831 child_die->offset.sect_off, objfile_name (objfile));
24c5c679
JK
11832 continue;
11833 }
96408a79
SA
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]"),
4262abfb 11842 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
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]"),
4262abfb 11860 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
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
43039443 11870/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
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'. */
43039443
JK
11873
11874static int
11875dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
11876 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11877 struct partial_symtab *ranges_pst)
43039443
JK
11878{
11879 struct objfile *objfile = cu->objfile;
3e29f34a 11880 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
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;
d521ce57 11889 const gdb_byte *buffer;
43039443
JK
11890 int low_set;
11891 CORE_ADDR low = 0;
11892 CORE_ADDR high = 0;
ff013f42 11893 CORE_ADDR baseaddr;
43039443 11894
d00adf39
DE
11895 found_base = cu->base_known;
11896 base = cu->base_address;
43039443 11897
be391dca 11898 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 11899 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
11900 {
11901 complaint (&symfile_complaints,
11902 _("Offset %d out of bounds for DW_AT_ranges attribute"),
11903 offset);
11904 return 0;
11905 }
dce234bc 11906 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 11907
43039443
JK
11908 low_set = 0;
11909
e7030f15 11910 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 11911
43039443
JK
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 {
28d2bfb9
AB
11932 /* If we found the largest possible address, then we already
11933 have the base address in range_end. */
11934 base = range_end;
43039443
JK
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
9277c30c
UW
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
43039443
JK
11960 range_beginning += base;
11961 range_end += base;
11962
01093045
DE
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"
4262abfb 11970 " [in module %s]"), objfile_name (objfile));
01093045
DE
11971 continue;
11972 }
11973
9277c30c 11974 if (ranges_pst != NULL)
3e29f34a
MR
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 }
ff013f42 11986
43039443
JK
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
3a2b436a
JK
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
e385593e 12020 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12021
3a2b436a 12022static enum pc_bounds_kind
af34e669 12023dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12024 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12025 struct partial_symtab *pst)
c906108c
SS
12026{
12027 struct attribute *attr;
91da1414 12028 struct attribute *attr_high;
af34e669
DJ
12029 CORE_ADDR low = 0;
12030 CORE_ADDR high = 0;
e385593e 12031 enum pc_bounds_kind ret;
c906108c 12032
91da1414
MW
12033 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12034 if (attr_high)
af34e669 12035 {
e142c38c 12036 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12037 if (attr)
91da1414 12038 {
31aa7e4e
JB
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;
91da1414 12043 }
af34e669
DJ
12044 else
12045 /* Found high w/o low attribute. */
e385593e 12046 return PC_BOUNDS_INVALID;
af34e669
DJ
12047
12048 /* Found consecutive range of addresses. */
3a2b436a 12049 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12050 }
c906108c 12051 else
af34e669 12052 {
e142c38c 12053 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12054 if (attr != NULL)
12055 {
ab435259
DE
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));
2e3cf129 12064
af34e669 12065 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12066 .debug_ranges section. */
2e3cf129 12067 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12068 return PC_BOUNDS_INVALID;
43039443 12069 /* Found discontinuous range of addresses. */
3a2b436a 12070 ret = PC_BOUNDS_RANGES;
af34e669 12071 }
e385593e
JK
12072 else
12073 return PC_BOUNDS_NOT_PRESENT;
af34e669 12074 }
c906108c 12075
9373cf26
JK
12076 /* read_partial_die has also the strict LOW < HIGH requirement. */
12077 if (high <= low)
e385593e 12078 return PC_BOUNDS_INVALID;
c906108c
SS
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. */
72dca2f5 12088 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12089 return PC_BOUNDS_INVALID;
c906108c
SS
12090
12091 *lowpc = low;
96408a79
SA
12092 if (highpc)
12093 *highpc = high;
af34e669 12094 return ret;
c906108c
SS
12095}
12096
b084d499
JB
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
12102static void
12103dwarf2_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
e385593e 12110 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12111 {
325fac50
PA
12112 *lowpc = std::min (*lowpc, low);
12113 *highpc = std::max (*highpc, high);
b084d499
JB
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;
6e70227d 12120
b084d499
JB
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
fae299cd
DC
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
12138static void
12139get_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
3a2b436a 12147 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12148 >= PC_BOUNDS_RANGES)
fae299cd
DC
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:
b084d499 12161 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12162 break;
12163 case DW_TAG_namespace:
f55ee35c 12164 case DW_TAG_module:
fae299cd
DC
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 {
325fac50
PA
12177 best_low = std::min (best_low, current_low);
12178 best_high = std::max (best_high, current_high);
fae299cd
DC
12179 }
12180 break;
12181 default:
0963b4bd 12182 /* Ignore. */
fae299cd
DC
12183 break;
12184 }
12185
12186 child = sibling_die (child);
12187 }
12188 }
12189
12190 *lowpc = best_low;
12191 *highpc = best_high;
12192}
12193
801e3a5b
JB
12194/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12195 in DIE. */
380bca97 12196
801e3a5b
JB
12197static void
12198dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12199 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12200{
bb5ed363 12201 struct objfile *objfile = cu->objfile;
3e29f34a 12202 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12203 struct attribute *attr;
91da1414 12204 struct attribute *attr_high;
801e3a5b 12205
91da1414
MW
12206 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12207 if (attr_high)
801e3a5b 12208 {
801e3a5b
JB
12209 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12210 if (attr)
12211 {
31aa7e4e
JB
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;
9a619af0 12217
3e29f34a
MR
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);
801e3a5b
JB
12221 }
12222 }
12223
12224 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12225 if (attr)
12226 {
bb5ed363 12227 bfd *obfd = objfile->obfd;
ab435259
DE
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;
801e3a5b
JB
12232
12233 /* The value of the DW_AT_ranges attribute is the offset of the
12234 address range list in the .debug_ranges section. */
ab435259
DE
12235 unsigned long offset = (DW_UNSND (attr)
12236 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12237 const gdb_byte *buffer;
801e3a5b
JB
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. */
d00adf39
DE
12253 CORE_ADDR base = cu->base_address;
12254 int base_known = cu->base_known;
801e3a5b 12255
d62bfeaf 12256 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12257 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
12258 {
12259 complaint (&symfile_complaints,
12260 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
12261 offset);
12262 return;
12263 }
d62bfeaf 12264 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
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,
3e43a32a
MS
12293 _("Invalid .debug_ranges data "
12294 "(no base address)"));
801e3a5b
JB
12295 return;
12296 }
12297
9277c30c
UW
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
01093045
DE
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"
4262abfb 12320 " [in module %s]"), objfile_name (objfile));
01093045
DE
12321 continue;
12322 }
12323
3e29f34a
MR
12324 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12325 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
01093045 12326 record_block_range (block, start, end - 1);
801e3a5b
JB
12327 }
12328 }
12329 }
12330}
12331
685b1105
JK
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. */
60d5a603 12334
685b1105
JK
12335static void
12336check_producer (struct dwarf2_cu *cu)
60d5a603 12337{
38360086 12338 int major, minor;
60d5a603
JK
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. */
60d5a603 12350 }
b1ffba5a 12351 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12352 {
38360086
MW
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);
685b1105 12355 }
61012eef 12356 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12357 cu->producer_is_icc = 1;
12358 else
12359 {
12360 /* For other non-GCC compilers, expect their behavior is DWARF version
12361 compliant. */
60d5a603
JK
12362 }
12363
ba919b58 12364 cu->checked_producer = 1;
685b1105 12365}
ba919b58 12366
685b1105
JK
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
12371static int
12372producer_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;
60d5a603
JK
12378}
12379
12380/* Return the default accessibility type if it is not overriden by
12381 DW_AT_accessibility. */
12382
12383static enum dwarf_access_attribute
12384dwarf2_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
74ac6d43
TT
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
12414static int
12415handle_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
c906108c
SS
12444/* Add an aggregate field to the field list. */
12445
12446static void
107d2387 12447dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12448 struct dwarf2_cu *cu)
6e70227d 12449{
e7c27a73 12450 struct objfile *objfile = cu->objfile;
5e2b427d 12451 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12452 struct nextfield *new_field;
12453 struct attribute *attr;
12454 struct field *fp;
15d034d0 12455 const char *fieldname = "";
c906108c
SS
12456
12457 /* Allocate a new field list entry and link it in. */
8d749320 12458 new_field = XNEW (struct nextfield);
b8c9b27d 12459 make_cleanup (xfree, new_field);
c906108c 12460 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
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 }
c906108c
SS
12472 fip->nfields++;
12473
e142c38c 12474 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12475 if (attr)
12476 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12477 else
12478 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12479 if (new_field->accessibility != DW_ACCESS_public)
12480 fip->non_public_fields = 1;
60d5a603 12481
e142c38c 12482 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12483 if (attr)
12484 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12485 else
12486 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12487
12488 fp = &new_field->field;
a9a9bd0f 12489
e142c38c 12490 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12491 {
74ac6d43
TT
12492 LONGEST offset;
12493
a9a9bd0f 12494 /* Data member other than a C++ static data member. */
6e70227d 12495
c906108c 12496 /* Get type of field. */
e7c27a73 12497 fp->type = die_type (die, cu);
c906108c 12498
d6a843b5 12499 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12500
c906108c 12501 /* Get bit size of field (zero if none). */
e142c38c 12502 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
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. */
74ac6d43
TT
12513 if (handle_data_member_location (die, cu, &offset))
12514 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12515 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12516 if (attr)
12517 {
5e2b427d 12518 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12519 {
12520 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
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. */
f41f5e61 12525 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12526 }
12527 else
12528 {
12529 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
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. */
c906108c
SS
12535 int anonymous_size;
12536 int bit_offset = DW_UNSND (attr);
12537
e142c38c 12538 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
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 }
f41f5e61
PA
12554 SET_FIELD_BITPOS (*fp,
12555 (FIELD_BITPOS (*fp)
12556 + anonymous_size * bits_per_byte
12557 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
12558 }
12559 }
da5b30da
AA
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)));
c906108c
SS
12564
12565 /* Get name of field. */
39cbfefa
DJ
12566 fieldname = dwarf2_name (die, cu);
12567 if (fieldname == NULL)
12568 fieldname = "";
d8151005
DJ
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;
c906108c
SS
12573
12574 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 12575 pointer or virtual base class pointer) to private. */
e142c38c 12576 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 12577 {
d48cc9dd 12578 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
12579 new_field->accessibility = DW_ACCESS_private;
12580 fip->non_public_fields = 1;
12581 }
12582 }
a9a9bd0f 12583 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 12584 {
a9a9bd0f
DC
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. */
6e70227d 12591
ff355380 12592 const char *physname;
c906108c 12593
a9a9bd0f 12594 /* Get name of field. */
39cbfefa
DJ
12595 fieldname = dwarf2_name (die, cu);
12596 if (fieldname == NULL)
c906108c
SS
12597 return;
12598
254e6b9e 12599 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
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))
254e6b9e
DE
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
2df3850c 12612 /* Get physical name. */
ff355380 12613 physname = dwarf2_physname (fieldname, die, cu);
c906108c 12614
d8151005
DJ
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 : "");
e7c27a73 12618 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 12619 FIELD_NAME (*fp) = fieldname;
c906108c
SS
12620 }
12621 else if (die->tag == DW_TAG_inheritance)
12622 {
74ac6d43 12623 LONGEST offset;
d4b96c9a 12624
74ac6d43
TT
12625 /* C++ base class field. */
12626 if (handle_data_member_location (die, cu, &offset))
12627 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 12628 FIELD_BITSIZE (*fp) = 0;
e7c27a73 12629 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
12630 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12631 fip->nbaseclasses++;
12632 }
12633}
12634
98751a41
JK
12635/* Add a typedef defined in the scope of the FIP's class. */
12636
12637static void
12638dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12639 struct dwarf2_cu *cu)
6e70227d 12640{
98751a41 12641 struct typedef_field_list *new_field;
98751a41 12642 struct typedef_field *fp;
98751a41
JK
12643
12644 /* Allocate a new field list entry and link it in. */
8d749320 12645 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
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
c906108c
SS
12664/* Create the vector of fields, and attach it to the type. */
12665
12666static void
fba45db2 12667dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12668 struct dwarf2_cu *cu)
c906108c
SS
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
b4ba55a1 12679 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
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
774b6a14
TT
12691 TYPE_FIELD_IGNORE_BITS (type) =
12692 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12693 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
12694 }
12695
12696 /* If the type has baseclasses, allocate and clear a bit vector for
12697 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 12698 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
12699 {
12700 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 12701 unsigned char *pointer;
c906108c
SS
12702
12703 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 12704 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 12705 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
12706 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12707 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12708 }
12709
3e43a32a
MS
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. */
c906108c
SS
12713 while (nfields-- > 0)
12714 {
7d0ccb61
DJ
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)
c906108c 12730 {
c5aa993b 12731 case DW_ACCESS_private:
b4ba55a1
JB
12732 if (cu->language != language_ada)
12733 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 12734 break;
c906108c 12735
c5aa993b 12736 case DW_ACCESS_protected:
b4ba55a1
JB
12737 if (cu->language != language_ada)
12738 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 12739 break;
c906108c 12740
c5aa993b
JM
12741 case DW_ACCESS_public:
12742 break;
c906108c 12743
c5aa993b
JM
12744 default:
12745 /* Unknown accessibility. Complain and treat it as public. */
12746 {
e2e0b3e5 12747 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 12748 fieldp->accessibility);
c5aa993b
JM
12749 }
12750 break;
c906108c
SS
12751 }
12752 if (nfields < fip->nbaseclasses)
12753 {
7d0ccb61 12754 switch (fieldp->virtuality)
c906108c 12755 {
c5aa993b
JM
12756 case DW_VIRTUALITY_virtual:
12757 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 12758 if (cu->language == language_ada)
a73c6dcd 12759 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
12760 SET_TYPE_FIELD_VIRTUAL (type, nfields);
12761 break;
c906108c
SS
12762 }
12763 }
c906108c
SS
12764 }
12765}
12766
7d27a96d
TT
12767/* Return true if this member function is a constructor, false
12768 otherwise. */
12769
12770static int
12771dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12772{
12773 const char *fieldname;
fe978cb0 12774 const char *type_name;
7d27a96d
TT
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);
fe978cb0
PA
12786 type_name = dwarf2_name (die->parent, cu);
12787 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
12788 return 0;
12789
12790 len = strlen (fieldname);
fe978cb0
PA
12791 return (strncmp (fieldname, type_name, len) == 0
12792 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
12793}
12794
c906108c
SS
12795/* Add a member function to the proper fieldlist. */
12796
12797static void
107d2387 12798dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 12799 struct type *type, struct dwarf2_cu *cu)
c906108c 12800{
e7c27a73 12801 struct objfile *objfile = cu->objfile;
c906108c
SS
12802 struct attribute *attr;
12803 struct fnfieldlist *flp;
12804 int i;
12805 struct fn_field *fnp;
15d034d0 12806 const char *fieldname;
c906108c 12807 struct nextfnfield *new_fnfield;
f792889a 12808 struct type *this_type;
60d5a603 12809 enum dwarf_access_attribute accessibility;
c906108c 12810
b4ba55a1 12811 if (cu->language == language_ada)
a73c6dcd 12812 error (_("unexpected member function in Ada type"));
b4ba55a1 12813
2df3850c 12814 /* Get name of member function. */
39cbfefa
DJ
12815 fieldname = dwarf2_name (die, cu);
12816 if (fieldname == NULL)
2df3850c 12817 return;
c906108c 12818
c906108c
SS
12819 /* Look up member function name in fieldlist. */
12820 for (i = 0; i < fip->nfnfields; i++)
12821 {
27bfe10e 12822 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
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)
c5aa993b 12836 * sizeof (struct fnfieldlist));
c906108c 12837 if (fip->nfnfields == 0)
c13c43fd 12838 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
12839 }
12840 flp = &fip->fnfieldlists[fip->nfnfields];
12841 flp->name = fieldname;
12842 flp->length = 0;
12843 flp->head = NULL;
3da10d80 12844 i = fip->nfnfields++;
c906108c
SS
12845 }
12846
12847 /* Create a new member function field and chain it to the field list
0963b4bd 12848 entry. */
8d749320 12849 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 12850 make_cleanup (xfree, new_fnfield);
c906108c
SS
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;
3da10d80
KS
12858
12859 /* Delay processing of the physname until later. */
9c37b5ae 12860 if (cu->language == language_cplus)
3da10d80
KS
12861 {
12862 add_to_method_list (type, i, flp->length - 1, fieldname,
12863 die, cu);
12864 }
12865 else
12866 {
1d06ead6 12867 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
12868 fnp->physname = physname ? physname : "";
12869 }
12870
c906108c 12871 fnp->type = alloc_type (objfile);
f792889a
DJ
12872 this_type = read_type_die (die, cu);
12873 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 12874 {
f792889a 12875 int nparams = TYPE_NFIELDS (this_type);
c906108c 12876
f792889a 12877 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
12878 of the method itself (TYPE_CODE_METHOD). */
12879 smash_to_method_type (fnp->type, type,
f792889a
DJ
12880 TYPE_TARGET_TYPE (this_type),
12881 TYPE_FIELDS (this_type),
12882 TYPE_NFIELDS (this_type),
12883 TYPE_VARARGS (this_type));
c906108c
SS
12884
12885 /* Handle static member functions.
c5aa993b 12886 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
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. */
f792889a 12891 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
12892 fnp->voffset = VOFFSET_STATIC;
12893 }
12894 else
e2e0b3e5 12895 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 12896 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
12897
12898 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 12899 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 12900 fnp->fcontext = die_containing_type (die, cu);
c906108c 12901
3e43a32a
MS
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. */
c906108c
SS
12904
12905 /* Get accessibility. */
e142c38c 12906 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 12907 if (attr)
aead7601 12908 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
12909 else
12910 accessibility = dwarf2_default_access_attribute (die, cu);
12911 switch (accessibility)
c906108c 12912 {
60d5a603
JK
12913 case DW_ACCESS_private:
12914 fnp->is_private = 1;
12915 break;
12916 case DW_ACCESS_protected:
12917 fnp->is_protected = 1;
12918 break;
c906108c
SS
12919 }
12920
b02dede2 12921 /* Check for artificial methods. */
e142c38c 12922 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
12923 if (attr && DW_UNSND (attr) != 0)
12924 fnp->is_artificial = 1;
12925
7d27a96d
TT
12926 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12927
0d564a31 12928 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
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
0d564a31
DJ
12932 to the object address. */
12933
e142c38c 12934 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 12935 if (attr)
8e19ed76 12936 {
aec5aa8b 12937 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 12938 {
aec5aa8b
TT
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 {
aec5aa8b
TT
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)
7e993ebf
KS
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 }
aec5aa8b 12978 }
3690dd37 12979 else if (attr_form_is_section_offset (attr))
8e19ed76 12980 {
4d3c2250 12981 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
12982 }
12983 else
12984 {
4d3c2250
KB
12985 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12986 fieldname);
8e19ed76 12987 }
0d564a31 12988 }
d48cc9dd
DJ
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,
3e43a32a
MS
12996 _("Member function \"%s\" (offset %d) is virtual "
12997 "but the vtable offset is not specified"),
b64f50a1 12998 fieldname, die->offset.sect_off);
9655fd1a 12999 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13000 TYPE_CPLUS_DYNAMIC (type) = 1;
13001 }
13002 }
c906108c
SS
13003}
13004
13005/* Create the vector of member function fields, and attach it to the type. */
13006
13007static void
fba45db2 13008dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13009 struct dwarf2_cu *cu)
c906108c
SS
13010{
13011 struct fnfieldlist *flp;
c906108c
SS
13012 int i;
13013
b4ba55a1 13014 if (cu->language == language_ada)
a73c6dcd 13015 error (_("unexpected member functions in Ada type"));
b4ba55a1 13016
c906108c
SS
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)
c5aa993b 13032 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13033 }
13034
13035 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13036}
13037
1168df01
JB
13038/* Returns non-zero if NAME is the name of a vtable member in CU's
13039 language, zero otherwise. */
13040static int
13041is_vtable_name (const char *name, struct dwarf2_cu *cu)
13042{
13043 static const char vptr[] = "_vptr";
987504bb 13044 static const char vtable[] = "vtable";
1168df01 13045
9c37b5ae
TT
13046 /* Look for the C++ form of the vtable. */
13047 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13048 return 1;
13049
13050 return 0;
13051}
13052
c0dd20ea 13053/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13054 functions, with the ABI-specified layout. If TYPE describes
13055 such a structure, smash it into a member function type.
61049d3b
DJ
13056
13057 GCC shouldn't do this; it should just output pointer to member DIEs.
13058 This is GCC PR debug/28767. */
c0dd20ea 13059
0b92b5bb
TT
13060static void
13061quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13062{
09e2d7c7 13063 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13064
13065 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13066 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13067 return;
c0dd20ea
DJ
13068
13069 /* Check for __pfn and __delta members. */
0b92b5bb
TT
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;
c0dd20ea
DJ
13075
13076 /* Find the type of the method. */
0b92b5bb 13077 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13078 if (pfn_type == NULL
13079 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13080 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13081 return;
c0dd20ea
DJ
13082
13083 /* Look for the "this" argument. */
13084 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13085 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13086 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13087 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13088 return;
c0dd20ea 13089
09e2d7c7 13090 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13091 new_type = alloc_type (objfile);
09e2d7c7 13092 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13093 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13094 TYPE_VARARGS (pfn_type));
0b92b5bb 13095 smash_to_methodptr_type (type, new_type);
c0dd20ea 13096}
1168df01 13097
685b1105
JK
13098/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13099 (icc). */
13100
13101static int
13102producer_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
c906108c 13110/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
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
83655187
DE
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).
c906108c 13116
c767944b
DJ
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
c906108c 13119 structure or union. This gets the type entered into our set of
83655187 13120 user defined types. */
c906108c 13121
f792889a 13122static struct type *
134d01f1 13123read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13124{
e7c27a73 13125 struct objfile *objfile = cu->objfile;
c906108c
SS
13126 struct type *type;
13127 struct attribute *attr;
15d034d0 13128 const char *name;
c906108c 13129
348e048f
DE
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. */
45e58e77 13133 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13134 if (attr)
13135 {
ac9ec31b 13136 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13137
ac9ec31b 13138 /* The type's CU may not be the same as CU.
02142a6c 13139 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13140 return set_die_type (die, type, cu);
13141 }
13142
c0dd20ea 13143 type = alloc_type (objfile);
c906108c 13144 INIT_CPLUS_SPECIFIC (type);
93311388 13145
39cbfefa
DJ
13146 name = dwarf2_name (die, cu);
13147 if (name != NULL)
c906108c 13148 {
987504bb 13149 if (cu->language == language_cplus
c44af4eb
TT
13150 || cu->language == language_d
13151 || cu->language == language_rust)
63d06c5c 13152 {
15d034d0 13153 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
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;
94af9270
KS
13161 if (die->tag == DW_TAG_structure_type
13162 || die->tag == DW_TAG_class_type)
13163 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13164 }
13165 else
13166 {
d8151005
DJ
13167 /* The name is already allocated along with this objfile, so
13168 we don't need to duplicate it for the type. */
7d455152 13169 TYPE_TAG_NAME (type) = name;
94af9270
KS
13170 if (die->tag == DW_TAG_class_type)
13171 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13172 }
c906108c
SS
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 {
4753d33b 13185 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13186 }
13187
0cc2414c
TT
13188 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13189 TYPE_DECLARED_CLASS (type) = 1;
13190
e142c38c 13191 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13192 if (attr)
13193 {
155bfbd3
JB
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 }
c906108c
SS
13208 }
13209 else
13210 {
13211 TYPE_LENGTH (type) = 0;
13212 }
13213
422b1cb0 13214 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13215 {
13216 /* ICC does not output the required DW_AT_declaration
13217 on incomplete types, but gives them a size of zero. */
422b1cb0 13218 TYPE_STUB (type) = 1;
685b1105
JK
13219 }
13220 else
13221 TYPE_STUB_SUPPORTED (type) = 1;
13222
dc718098 13223 if (die_is_declaration (die, cu))
876cecd0 13224 TYPE_STUB (type) = 1;
a6c727b2
DJ
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;
dc718098 13230
c906108c
SS
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
0963b4bd 13233 type within the structure itself. */
1c379e20 13234 set_die_type (die, type, cu);
c906108c 13235
7e314c57
JK
13236 /* set_die_type should be already done. */
13237 set_descriptive_type (type, die, cu);
13238
c767944b
DJ
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
13245static void
13246process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13247{
13248 struct objfile *objfile = cu->objfile;
ca040673 13249 struct die_info *child_die;
c767944b
DJ
13250 struct type *type;
13251
13252 type = get_die_type (die, cu);
13253 if (type == NULL)
13254 type = read_structure_type (die, cu);
13255
e142c38c 13256 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13257 {
13258 struct field_info fi;
34eaf542 13259 VEC (symbolp) *template_args = NULL;
c767944b 13260 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13261
13262 memset (&fi, 0, sizeof (struct field_info));
13263
639d11d3 13264 child_die = die->child;
c906108c
SS
13265
13266 while (child_die && child_die->tag)
13267 {
a9a9bd0f
DC
13268 if (child_die->tag == DW_TAG_member
13269 || child_die->tag == DW_TAG_variable)
c906108c 13270 {
a9a9bd0f
DC
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. */
e7c27a73 13276 dwarf2_add_field (&fi, child_die, cu);
c906108c 13277 }
8713b1b1 13278 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13279 {
e98c9e7c
TT
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 }
c906108c
SS
13290 }
13291 else if (child_die->tag == DW_TAG_inheritance)
13292 {
13293 /* C++ base class field. */
e7c27a73 13294 dwarf2_add_field (&fi, child_die, cu);
c906108c 13295 }
98751a41
JK
13296 else if (child_die->tag == DW_TAG_typedef)
13297 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
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
f1078f66
DJ
13303 if (arg != NULL)
13304 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13305 }
13306
c906108c
SS
13307 child_die = sibling_die (child_die);
13308 }
13309
34eaf542
TT
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)
8d749320
SM
13317 = XOBNEWVEC (&objfile->objfile_obstack,
13318 struct symbol *,
13319 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
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
c906108c
SS
13327 /* Attach fields and member functions to the type. */
13328 if (fi.nfields)
e7c27a73 13329 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13330 if (fi.nfnfields)
13331 {
e7c27a73 13332 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13333
c5aa993b 13334 /* Get the type which refers to the base class (possibly this
c906108c 13335 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13336 class from the DW_AT_containing_type attribute. This use of
13337 DW_AT_containing_type is a GNU extension. */
c906108c 13338
e142c38c 13339 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13340 {
e7c27a73 13341 struct type *t = die_containing_type (die, cu);
c906108c 13342
ae6ae975 13343 set_type_vptr_basetype (type, t);
c906108c
SS
13344 if (type == t)
13345 {
c906108c
SS
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 {
0d5cff50 13353 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13354
1168df01 13355 if (is_vtable_name (fieldname, cu))
c906108c 13356 {
ae6ae975 13357 set_type_vptr_fieldno (type, i);
c906108c
SS
13358 break;
13359 }
13360 }
13361
13362 /* Complain if virtual function table field not found. */
13363 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13364 complaint (&symfile_complaints,
3e43a32a
MS
13365 _("virtual function table pointer "
13366 "not found when defining class '%s'"),
4d3c2250
KB
13367 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13368 "");
c906108c
SS
13369 }
13370 else
13371 {
ae6ae975 13372 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13373 }
13374 }
f6235d4c 13375 else if (cu->producer
61012eef 13376 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
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 {
ae6ae975
DE
13390 set_type_vptr_fieldno (type, i);
13391 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13392 break;
13393 }
13394 }
13395 }
c906108c 13396 }
98751a41
JK
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
a0d7a4ff 13404 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13405 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13406 = ((struct typedef_field *)
13407 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
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;
6e70227d 13414
98751a41
JK
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 }
c767944b
DJ
13421
13422 do_cleanups (back_to);
c906108c 13423 }
63d06c5c 13424
bb5ed363 13425 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13426
90aeadfc
DC
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. */
134d01f1 13433
ca040673
DE
13434 child_die = die->child;
13435
90aeadfc
DC
13436 while (child_die != NULL && child_die->tag)
13437 {
13438 if (child_die->tag == DW_TAG_member
13439 || child_die->tag == DW_TAG_variable
34eaf542
TT
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)
134d01f1 13443 {
90aeadfc 13444 /* Do nothing. */
134d01f1 13445 }
90aeadfc
DC
13446 else
13447 process_die (child_die, cu);
134d01f1 13448
90aeadfc 13449 child_die = sibling_die (child_die);
134d01f1
DJ
13450 }
13451
fa4028e9
JB
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))
c767944b 13457 new_symbol (die, type, cu);
134d01f1
DJ
13458}
13459
55426c9d
JB
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
13463static void
13464update_enumeration_type_from_children (struct die_info *die,
13465 struct type *type,
13466 struct dwarf2_cu *cu)
13467{
13468 struct obstack obstack;
60f7655a 13469 struct die_info *child_die;
55426c9d
JB
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
60f7655a
DE
13478 for (child_die = die->child;
13479 child_die != NULL && child_die->tag;
13480 child_die = sibling_die (child_die))
55426c9d
JB
13481 {
13482 struct attribute *attr;
13483 LONGEST value;
13484 const gdb_byte *bytes;
13485 struct dwarf2_locexpr_baton *baton;
13486 const char *name;
60f7655a 13487
55426c9d
JB
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;
55426c9d
JB
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
134d01f1
DJ
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. */
c906108c 13527
f792889a 13528static struct type *
134d01f1 13529read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13530{
e7c27a73 13531 struct objfile *objfile = cu->objfile;
c906108c 13532 struct type *type;
c906108c 13533 struct attribute *attr;
0114d602 13534 const char *name;
134d01f1 13535
348e048f
DE
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. */
45e58e77 13539 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13540 if (attr)
13541 {
ac9ec31b 13542 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13543
ac9ec31b 13544 /* The type's CU may not be the same as CU.
02142a6c 13545 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13546 return set_die_type (die, type, cu);
13547 }
13548
c906108c
SS
13549 type = alloc_type (objfile);
13550
13551 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 13552 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 13553 if (name != NULL)
7d455152 13554 TYPE_TAG_NAME (type) = name;
c906108c 13555
0626fc76
TT
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
e142c38c 13564 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13565 if (attr)
13566 {
13567 TYPE_LENGTH (type) = DW_UNSND (attr);
13568 }
13569 else
13570 {
13571 TYPE_LENGTH (type) = 0;
13572 }
13573
137033e9
JB
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. */
02eb380e 13579 if (die_is_declaration (die, cu))
876cecd0 13580 TYPE_STUB (type) = 1;
02eb380e 13581
0626fc76
TT
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. */
55426c9d
JB
13585 update_enumeration_type_from_children (die, type, cu);
13586
0626fc76
TT
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
3d567982
TT
13601 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13602
f792889a 13603 return set_die_type (die, type, cu);
134d01f1
DJ
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
13612static void
13613process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13614{
f792889a 13615 struct type *this_type;
134d01f1 13616
f792889a
DJ
13617 this_type = get_die_type (die, cu);
13618 if (this_type == NULL)
13619 this_type = read_enumeration_type (die, cu);
9dc481d3 13620
639d11d3 13621 if (die->child != NULL)
c906108c 13622 {
9dc481d3
DE
13623 struct die_info *child_die;
13624 struct symbol *sym;
13625 struct field *fields = NULL;
13626 int num_fields = 0;
15d034d0 13627 const char *name;
9dc481d3 13628
639d11d3 13629 child_die = die->child;
c906108c
SS
13630 while (child_die && child_die->tag)
13631 {
13632 if (child_die->tag != DW_TAG_enumerator)
13633 {
e7c27a73 13634 process_die (child_die, cu);
c906108c
SS
13635 }
13636 else
13637 {
39cbfefa
DJ
13638 name = dwarf2_name (child_die, cu);
13639 if (name)
c906108c 13640 {
f792889a 13641 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
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)
c5aa993b 13648 * sizeof (struct field));
c906108c
SS
13649 }
13650
3567439c 13651 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 13652 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 13653 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
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 {
f792889a
DJ
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,
c906108c 13669 sizeof (struct field) * num_fields);
b8c9b27d 13670 xfree (fields);
c906108c 13671 }
c906108c 13672 }
134d01f1 13673
6c83ed52
TT
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. */
3019eac3 13682 if (cu->per_cu->is_debug_types
6c83ed52
TT
13683 && die_is_declaration (die, cu))
13684 {
52dc124a 13685 struct signatured_type *sig_type;
6c83ed52 13686
c0f78cd4 13687 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
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)
6c83ed52
TT
13690 return;
13691 }
13692
f792889a 13693 new_symbol (die, this_type, cu);
c906108c
SS
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
f792889a 13700static struct type *
e7c27a73 13701read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13702{
e7c27a73 13703 struct objfile *objfile = cu->objfile;
c906108c 13704 struct die_info *child_die;
7e314c57 13705 struct type *type;
c906108c
SS
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;
15d034d0 13711 const char *name;
dc53a7ad 13712 unsigned int bit_stride = 0;
c906108c 13713
e7c27a73 13714 element_type = die_type (die, cu);
c906108c 13715
7e314c57
JK
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
dc53a7ad
JB
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
c906108c
SS
13729 /* Irix 6.2 native cc creates array types without children for
13730 arrays with unspecified length. */
639d11d3 13731 if (die->child == NULL)
c906108c 13732 {
46bf5051 13733 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 13734 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
13735 type = create_array_type_with_stride (NULL, element_type, range_type,
13736 bit_stride);
f792889a 13737 return set_die_type (die, type, cu);
c906108c
SS
13738 }
13739
13740 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 13741 child_die = die->child;
c906108c
SS
13742 while (child_die && child_die->tag)
13743 {
13744 if (child_die->tag == DW_TAG_subrange_type)
13745 {
f792889a 13746 struct type *child_type = read_type_die (child_die, cu);
9a619af0 13747
f792889a 13748 if (child_type != NULL)
a02abb62 13749 {
0963b4bd
MS
13750 /* The range type was succesfully read. Save it for the
13751 array type creation. */
a02abb62
JB
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 }
f792889a 13760 range_types[ndim++] = child_type;
a02abb62 13761 }
c906108c
SS
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. */
7ca2d3a3 13768
c906108c 13769 type = element_type;
7ca2d3a3
DL
13770
13771 if (read_array_order (die, cu) == DW_ORD_col_major)
13772 {
13773 int i = 0;
9a619af0 13774
7ca2d3a3 13775 while (i < ndim)
dc53a7ad
JB
13776 type = create_array_type_with_stride (NULL, type, range_types[i++],
13777 bit_stride);
7ca2d3a3
DL
13778 }
13779 else
13780 {
13781 while (ndim-- > 0)
dc53a7ad
JB
13782 type = create_array_type_with_stride (NULL, type, range_types[ndim],
13783 bit_stride);
7ca2d3a3 13784 }
c906108c 13785
f5f8a009
EZ
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. */
e142c38c 13792 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 13793 if (attr)
ea37ba09 13794 make_vector_type (type);
f5f8a009 13795
dbc98a8b
KW
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
3e43a32a
MS
13805 complaint (&symfile_complaints,
13806 _("DW_AT_byte_size for array type smaller "
13807 "than the total size of elements"));
dbc98a8b
KW
13808 }
13809
39cbfefa
DJ
13810 name = dwarf2_name (die, cu);
13811 if (name)
13812 TYPE_NAME (type) = name;
6e70227d 13813
0963b4bd 13814 /* Install the type in the die. */
7e314c57
JK
13815 set_die_type (die, type, cu);
13816
13817 /* set_die_type should be already done. */
b4ba55a1
JB
13818 set_descriptive_type (type, die, cu);
13819
c906108c
SS
13820 do_cleanups (back_to);
13821
7e314c57 13822 return type;
c906108c
SS
13823}
13824
7ca2d3a3 13825static enum dwarf_array_dim_ordering
6e70227d 13826read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
13827{
13828 struct attribute *attr;
13829
13830 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13831
aead7601
SM
13832 if (attr)
13833 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 13834
0963b4bd
MS
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.
7ca2d3a3 13838
0963b4bd
MS
13839 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13840 version checking. */
7ca2d3a3 13841
905e0470
PM
13842 if (cu->language == language_fortran
13843 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
13844 {
13845 return DW_ORD_row_major;
13846 }
13847
6e70227d 13848 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
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
72019c9c 13858/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 13859 the DIE's type field. */
72019c9c 13860
f792889a 13861static struct type *
72019c9c
GM
13862read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13863{
7e314c57
JK
13864 struct type *domain_type, *set_type;
13865 struct attribute *attr;
f792889a 13866
7e314c57
JK
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);
d09039dd
PM
13877 if (attr)
13878 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 13879
f792889a 13880 return set_die_type (die, set_type, cu);
72019c9c 13881}
7ca2d3a3 13882
0971de02
TT
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
13892static void
13893mark_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
8d749320 13911 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
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
224c3ddb 13925 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
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
0971de02 13950 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 13951 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
13952}
13953
4357ac6c
TT
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. */
c906108c
SS
13959
13960static void
e7c27a73 13961read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13962{
0971de02
TT
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
639d11d3 13986 if (die->child != NULL)
c906108c 13987 {
4357ac6c
TT
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;
74ac6d43 13993
4357ac6c
TT
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 *));
224c3ddb
SM
14001 common_block
14002 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14003 size);
4357ac6c
TT
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. */
e7c27a73 14013 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
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]"),
4262abfb
JK
14032 child_die->offset.sect_off,
14033 objfile_name (cu->objfile));
0971de02
TT
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 }
c906108c 14048 }
4357ac6c
TT
14049
14050 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14051 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14052 }
14053}
14054
0114d602 14055/* Create a type for a C++ namespace. */
d9fa45fe 14056
0114d602
DJ
14057static struct type *
14058read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14059{
e7c27a73 14060 struct objfile *objfile = cu->objfile;
0114d602 14061 const char *previous_prefix, *name;
9219021c 14062 int is_anonymous;
0114d602
DJ
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;
9a619af0 14070
0114d602
DJ
14071 ext_die = dwarf2_extension (die, &ext_cu);
14072 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14073
14074 /* EXT_CU may not be the same as CU.
02142a6c 14075 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14076 return set_die_type (die, type, cu);
14077 }
9219021c 14078
e142c38c 14079 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14080
14081 /* Now build the name of the current namespace. */
14082
0114d602
DJ
14083 previous_prefix = determine_prefix (die, cu);
14084 if (previous_prefix[0] != '\0')
14085 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14086 previous_prefix, name, 0, cu);
0114d602
DJ
14087
14088 /* Create the type. */
19f392bc 14089 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14090 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14091
60531b24 14092 return set_die_type (die, type, cu);
0114d602
DJ
14093}
14094
22cee43f 14095/* Read a namespace scope. */
0114d602
DJ
14096
14097static void
14098read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14099{
14100 struct objfile *objfile = cu->objfile;
0114d602 14101 int is_anonymous;
9219021c 14102
5c4e30ca
DC
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. */
9219021c 14106
f2f0e013 14107 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14108 {
14109 struct type *type;
14110
0114d602 14111 type = read_type_die (die, cu);
e7c27a73 14112 new_symbol (die, type, cu);
5c4e30ca 14113
e8e80198 14114 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14115 if (is_anonymous)
0114d602
DJ
14116 {
14117 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14118
22cee43f
PMR
14119 add_using_directive (using_directives (cu->language),
14120 previous_prefix, TYPE_NAME (type), NULL,
14121 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14122 }
5c4e30ca 14123 }
9219021c 14124
639d11d3 14125 if (die->child != NULL)
d9fa45fe 14126 {
639d11d3 14127 struct die_info *child_die = die->child;
6e70227d 14128
d9fa45fe
DC
14129 while (child_die && child_die->tag)
14130 {
e7c27a73 14131 process_die (child_die, cu);
d9fa45fe
DC
14132 child_die = sibling_die (child_die);
14133 }
14134 }
38d518c9
EZ
14135}
14136
f55ee35c
JK
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
14141static struct type *
14142read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14143{
14144 struct objfile *objfile = cu->objfile;
15d034d0 14145 const char *module_name;
f55ee35c
JK
14146 struct type *type;
14147
14148 module_name = dwarf2_name (die, cu);
14149 if (!module_name)
3e43a32a
MS
14150 complaint (&symfile_complaints,
14151 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 14152 die->offset.sect_off);
19f392bc 14153 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
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
5d7cb8df
JK
14161/* Read a Fortran module. */
14162
14163static void
14164read_module (struct die_info *die, struct dwarf2_cu *cu)
14165{
14166 struct die_info *child_die = die->child;
530e8392
KB
14167 struct type *type;
14168
14169 type = read_type_die (die, cu);
14170 new_symbol (die, type, cu);
5d7cb8df 14171
5d7cb8df
JK
14172 while (child_die && child_die->tag)
14173 {
14174 process_die (child_die, cu);
14175 child_die = sibling_die (child_die);
14176 }
14177}
14178
38d518c9
EZ
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
14183static const char *
e142c38c 14184namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
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;
f2f0e013 14193 current_die = dwarf2_extension (die, &cu))
38d518c9 14194 {
96553a0c
DE
14195 /* We don't use dwarf2_name here so that we can detect the absence
14196 of a name -> anonymous namespace. */
7d45c7c3 14197 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14198
38d518c9
EZ
14199 if (name != NULL)
14200 break;
14201 }
14202
14203 /* Is it an anonymous namespace? */
14204
14205 *is_anonymous = (name == NULL);
14206 if (*is_anonymous)
2b1dbab0 14207 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14208
14209 return name;
d9fa45fe
DC
14210}
14211
c906108c
SS
14212/* Extract all information from a DW_TAG_pointer_type DIE and add to
14213 the user defined type vector. */
14214
f792889a 14215static struct type *
e7c27a73 14216read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14217{
5e2b427d 14218 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14219 struct comp_unit_head *cu_header = &cu->header;
c906108c 14220 struct type *type;
8b2dbe47
KB
14221 struct attribute *attr_byte_size;
14222 struct attribute *attr_address_class;
14223 int byte_size, addr_class;
7e314c57
JK
14224 struct type *target_type;
14225
14226 target_type = die_type (die, cu);
c906108c 14227
7e314c57
JK
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);
8b2dbe47 14234
e142c38c 14235 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14236 if (attr_byte_size)
14237 byte_size = DW_UNSND (attr_byte_size);
c906108c 14238 else
8b2dbe47
KB
14239 byte_size = cu_header->addr_size;
14240
e142c38c 14241 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
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)
c906108c 14251 {
5e2b427d 14252 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14253 {
14254 int type_flags;
14255
849957d9 14256 type_flags = gdbarch_address_class_type_flags
5e2b427d 14257 (gdbarch, byte_size, addr_class);
876cecd0
TT
14258 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14259 == 0);
8b2dbe47
KB
14260 type = make_type_with_address_space (type, type_flags);
14261 }
14262 else if (TYPE_LENGTH (type) != byte_size)
14263 {
3e43a32a
MS
14264 complaint (&symfile_complaints,
14265 _("invalid pointer size %d"), byte_size);
8b2dbe47 14266 }
6e70227d 14267 else
9a619af0
MS
14268 {
14269 /* Should we also complain about unhandled address classes? */
14270 }
c906108c 14271 }
8b2dbe47
KB
14272
14273 TYPE_LENGTH (type) = byte_size;
f792889a 14274 return set_die_type (die, type, cu);
c906108c
SS
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
f792889a 14280static struct type *
e7c27a73 14281read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14282{
14283 struct type *type;
14284 struct type *to_type;
14285 struct type *domain;
14286
e7c27a73
DJ
14287 to_type = die_type (die, cu);
14288 domain = die_containing_type (die, cu);
0d5de010 14289
7e314c57
JK
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
0d5de010
DJ
14295 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14296 type = lookup_methodptr_type (to_type);
7078baeb
TT
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 }
0d5de010
DJ
14306 else
14307 type = lookup_memberptr_type (to_type, domain);
c906108c 14308
f792889a 14309 return set_die_type (die, type, cu);
c906108c
SS
14310}
14311
14312/* Extract all information from a DW_TAG_reference_type DIE and add to
14313 the user defined type vector. */
14314
f792889a 14315static struct type *
e7c27a73 14316read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14317{
e7c27a73 14318 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14319 struct type *type, *target_type;
c906108c
SS
14320 struct attribute *attr;
14321
7e314c57
JK
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);
e142c38c 14330 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14331 if (attr)
14332 {
14333 TYPE_LENGTH (type) = DW_UNSND (attr);
14334 }
14335 else
14336 {
107d2387 14337 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14338 }
f792889a 14339 return set_die_type (die, type, cu);
c906108c
SS
14340}
14341
cf363f18
MW
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
14348static struct type *
14349add_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
f792889a 14372static struct type *
e7c27a73 14373read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14374{
f792889a 14375 struct type *base_type, *cv_type;
c906108c 14376
e7c27a73 14377 base_type = die_type (die, cu);
7e314c57
JK
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
2f608a3a
KW
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)
cf363f18 14387 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14388
f792889a
DJ
14389 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14390 return set_die_type (die, cv_type, cu);
c906108c
SS
14391}
14392
f792889a 14393static struct type *
e7c27a73 14394read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14395{
f792889a 14396 struct type *base_type, *cv_type;
c906108c 14397
e7c27a73 14398 base_type = die_type (die, cu);
7e314c57
JK
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
cf363f18
MW
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
f792889a
DJ
14411 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14412 return set_die_type (die, cv_type, cu);
c906108c
SS
14413}
14414
06d66ee9
TT
14415/* Handle DW_TAG_restrict_type. */
14416
14417static struct type *
14418read_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
a2c2acaf
MW
14433/* Handle DW_TAG_atomic_type. */
14434
14435static struct type *
14436read_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
c906108c
SS
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
f792889a 14456static struct type *
e7c27a73 14457read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14458{
e7c27a73 14459 struct objfile *objfile = cu->objfile;
3b7538c0 14460 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14461 struct type *type, *range_type, *index_type, *char_type;
14462 struct attribute *attr;
14463 unsigned int length;
14464
e142c38c 14465 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14466 if (attr)
14467 {
14468 length = DW_UNSND (attr);
14469 }
14470 else
14471 {
0963b4bd 14472 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14473 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14474 if (attr)
14475 {
14476 length = DW_UNSND (attr);
14477 }
14478 else
14479 {
14480 length = 1;
14481 }
c906108c 14482 }
6ccb9162 14483
46bf5051 14484 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14485 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14486 char_type = language_string_char_type (cu->language_defn, gdbarch);
14487 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14488
f792889a 14489 return set_die_type (die, type, cu);
c906108c
SS
14490}
14491
4d804846
JB
14492/* Assuming that DIE corresponds to a function, returns nonzero
14493 if the function is prototyped. */
14494
14495static int
14496prototyped_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
c906108c
SS
14524/* Handle DIES due to C code like:
14525
14526 struct foo
c5aa993b
JM
14527 {
14528 int (*funcp)(int a, long l);
14529 int b;
14530 };
c906108c 14531
0963b4bd 14532 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 14533
f792889a 14534static struct type *
e7c27a73 14535read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14536{
bb5ed363 14537 struct objfile *objfile = cu->objfile;
0963b4bd
MS
14538 struct type *type; /* Type that this function returns. */
14539 struct type *ftype; /* Function that returns above type. */
c906108c
SS
14540 struct attribute *attr;
14541
e7c27a73 14542 type = die_type (die, cu);
7e314c57
JK
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
0c8b41f1 14549 ftype = lookup_function_type (type);
c906108c 14550
4d804846 14551 if (prototyped_function_p (die, cu))
a6c727b2 14552 TYPE_PROTOTYPED (ftype) = 1;
c906108c 14553
c055b101
CV
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);
54fcddd0
UW
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;
76c10ea2 14564
743649fd
MW
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
76c10ea2
GM
14571 /* We need to add the subroutine type to the die immediately so
14572 we don't infinitely recurse when dealing with parameters
0963b4bd 14573 declared as the same subroutine type. */
76c10ea2 14574 set_die_type (die, ftype, cu);
6e70227d 14575
639d11d3 14576 if (die->child != NULL)
c906108c 14577 {
bb5ed363 14578 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 14579 struct die_info *child_die;
8072405b 14580 int nparams, iparams;
c906108c
SS
14581
14582 /* Count the number of parameters.
14583 FIXME: GDB currently ignores vararg functions, but knows about
14584 vararg member functions. */
8072405b 14585 nparams = 0;
639d11d3 14586 child_die = die->child;
c906108c
SS
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)
876cecd0 14592 TYPE_VARARGS (ftype) = 1;
c906108c
SS
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 *)
ae5a43e0 14599 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 14600
8072405b
JK
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;
639d11d3 14607 child_die = die->child;
c906108c
SS
14608 while (child_die && child_die->tag)
14609 {
14610 if (child_die->tag == DW_TAG_formal_parameter)
14611 {
3ce3b1ba
PA
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. */
e142c38c 14623 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
14624 if (attr)
14625 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14626 else
9c37b5ae 14627 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
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;
c906108c
SS
14662 iparams++;
14663 }
14664 child_die = sibling_die (child_die);
14665 }
14666 }
14667
76c10ea2 14668 return ftype;
c906108c
SS
14669}
14670
f792889a 14671static struct type *
e7c27a73 14672read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14673{
e7c27a73 14674 struct objfile *objfile = cu->objfile;
0114d602 14675 const char *name = NULL;
3c8e0968 14676 struct type *this_type, *target_type;
c906108c 14677
94af9270 14678 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
14679 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14680 TYPE_TARGET_STUB (this_type) = 1;
f792889a 14681 set_die_type (die, this_type, cu);
3c8e0968
DE
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]"),
4262abfb 14692 die->offset.sect_off, objfile_name (objfile));
3c8e0968
DE
14693 TYPE_TARGET_TYPE (this_type) = NULL;
14694 }
f792889a 14695 return this_type;
c906108c
SS
14696}
14697
9b790ce7
UW
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
14702static struct type *
14703dwarf2_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
c906108c
SS
14719/* Find a representation of a given base type and install
14720 it in the TYPE field of the die. */
14721
f792889a 14722static struct type *
e7c27a73 14723read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14724{
e7c27a73 14725 struct objfile *objfile = cu->objfile;
c906108c
SS
14726 struct type *type;
14727 struct attribute *attr;
19f392bc 14728 int encoding = 0, bits = 0;
15d034d0 14729 const char *name;
c906108c 14730
e142c38c 14731 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
14732 if (attr)
14733 {
14734 encoding = DW_UNSND (attr);
14735 }
e142c38c 14736 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14737 if (attr)
14738 {
19f392bc 14739 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 14740 }
39cbfefa 14741 name = dwarf2_name (die, cu);
6ccb9162 14742 if (!name)
c906108c 14743 {
6ccb9162
UW
14744 complaint (&symfile_complaints,
14745 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 14746 }
6ccb9162
UW
14747
14748 switch (encoding)
c906108c 14749 {
6ccb9162
UW
14750 case DW_ATE_address:
14751 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
14752 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
14753 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
14754 break;
14755 case DW_ATE_boolean:
19f392bc 14756 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
14757 break;
14758 case DW_ATE_complex_float:
9b790ce7 14759 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 14760 type = init_complex_type (objfile, name, type);
6ccb9162
UW
14761 break;
14762 case DW_ATE_decimal_float:
19f392bc 14763 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
14764 break;
14765 case DW_ATE_float:
9b790ce7 14766 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
14767 break;
14768 case DW_ATE_signed:
19f392bc 14769 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
14770 break;
14771 case DW_ATE_unsigned:
3b2b8fea
TT
14772 if (cu->language == language_fortran
14773 && name
61012eef 14774 && startswith (name, "character("))
19f392bc
UW
14775 type = init_character_type (objfile, bits, 1, name);
14776 else
14777 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
14778 break;
14779 case DW_ATE_signed_char:
6e70227d 14780 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
14781 || cu->language == language_pascal
14782 || cu->language == language_fortran)
19f392bc
UW
14783 type = init_character_type (objfile, bits, 0, name);
14784 else
14785 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
14786 break;
14787 case DW_ATE_unsigned_char:
868a0084 14788 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 14789 || cu->language == language_pascal
c44af4eb
TT
14790 || cu->language == language_fortran
14791 || cu->language == language_rust)
19f392bc
UW
14792 type = init_character_type (objfile, bits, 1, name);
14793 else
14794 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 14795 break;
75079b2b
TT
14796 case DW_ATE_UTF:
14797 /* We just treat this as an integer and then recognize the
14798 type by name elsewhere. */
19f392bc 14799 type = init_integer_type (objfile, bits, 0, name);
75079b2b
TT
14800 break;
14801
6ccb9162
UW
14802 default:
14803 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
14804 dwarf_type_encoding_name (encoding));
19f392bc
UW
14805 type = init_type (objfile, TYPE_CODE_ERROR,
14806 bits / TARGET_CHAR_BIT, name);
6ccb9162 14807 break;
c906108c 14808 }
6ccb9162 14809
0114d602 14810 if (name && strcmp (name, "char") == 0)
876cecd0 14811 TYPE_NOSIGN (type) = 1;
0114d602 14812
f792889a 14813 return set_die_type (die, type, cu);
c906108c
SS
14814}
14815
80180f79
SA
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
14820static int
14821attr_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 {
8d749320 14832 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
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);
df25ebbd
JB
14849 if (target_attr == NULL)
14850 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
14851 target_cu);
80180f79
SA
14852 if (target_attr == NULL)
14853 return 0;
14854
df25ebbd 14855 switch (target_attr->name)
80180f79 14856 {
df25ebbd
JB
14857 case DW_AT_location:
14858 if (attr_form_is_section_offset (target_attr))
14859 {
8d749320 14860 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
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 {
8d749320 14869 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
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
8d749320 14893 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
14894 baton->referenced_type = read_type_die (target_die->parent,
14895 target_cu);
df25ebbd
JB
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 }
80180f79
SA
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
a02abb62
JB
14919/* Read the given DW_AT_subrange DIE. */
14920
f792889a 14921static struct type *
a02abb62
JB
14922read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
14923{
4c9ad8c2 14924 struct type *base_type, *orig_base_type;
a02abb62
JB
14925 struct type *range_type;
14926 struct attribute *attr;
729efb13 14927 struct dynamic_prop low, high;
4fae6e18 14928 int low_default_is_valid;
c451ebe5 14929 int high_bound_is_count = 0;
15d034d0 14930 const char *name;
43bbcdc2 14931 LONGEST negative_mask;
e77813c8 14932
4c9ad8c2
TT
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);
a02abb62 14939
7e314c57
JK
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
729efb13
SA
14945 low.kind = PROP_CONST;
14946 high.kind = PROP_CONST;
14947 high.data.const_val = 0;
14948
4fae6e18
JK
14949 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
14950 omitting DW_AT_lower_bound. */
14951 switch (cu->language)
6e70227d 14952 {
4fae6e18
JK
14953 case language_c:
14954 case language_cplus:
729efb13 14955 low.data.const_val = 0;
4fae6e18
JK
14956 low_default_is_valid = 1;
14957 break;
14958 case language_fortran:
729efb13 14959 low.data.const_val = 1;
4fae6e18
JK
14960 low_default_is_valid = 1;
14961 break;
14962 case language_d:
4fae6e18 14963 case language_objc:
c44af4eb 14964 case language_rust:
729efb13 14965 low.data.const_val = 0;
4fae6e18
JK
14966 low_default_is_valid = (cu->header.version >= 4);
14967 break;
14968 case language_ada:
14969 case language_m2:
14970 case language_pascal:
729efb13 14971 low.data.const_val = 1;
4fae6e18
JK
14972 low_default_is_valid = (cu->header.version >= 4);
14973 break;
14974 default:
729efb13 14975 low.data.const_val = 0;
4fae6e18
JK
14976 low_default_is_valid = 0;
14977 break;
a02abb62
JB
14978 }
14979
e142c38c 14980 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 14981 if (attr)
11c1ba78 14982 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
14983 else if (!low_default_is_valid)
14984 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
14985 "- DIE at 0x%x [in module %s]"),
4262abfb 14986 die->offset.sect_off, objfile_name (cu->objfile));
a02abb62 14987
e142c38c 14988 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 14989 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
14990 {
14991 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 14992 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 14993 {
c451ebe5
SA
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;
c2ff108b 14999 }
e77813c8
PM
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,
0963b4bd 15013 high bound or count are not yet handled by this code. */
e77813c8
PM
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 }
a02abb62 15039
dbb9c2b1
JB
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. */
6e70227d 15047 negative_mask =
66c6502d 15048 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
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;
43bbcdc2 15055
729efb13 15056 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15057
c451ebe5
SA
15058 if (high_bound_is_count)
15059 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15060
c2ff108b
JK
15061 /* Ada expects an empty array on no boundary attributes. */
15062 if (attr == NULL && cu->language != language_ada)
729efb13 15063 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15064
39cbfefa
DJ
15065 name = dwarf2_name (die, cu);
15066 if (name)
15067 TYPE_NAME (range_type) = name;
6e70227d 15068
e142c38c 15069 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15070 if (attr)
15071 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15072
7e314c57
JK
15073 set_die_type (die, range_type, cu);
15074
15075 /* set_die_type should be already done. */
b4ba55a1
JB
15076 set_descriptive_type (range_type, die, cu);
15077
7e314c57 15078 return range_type;
a02abb62 15079}
6e70227d 15080
f792889a 15081static struct type *
81a17f79
JB
15082read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15083{
15084 struct type *type;
81a17f79 15085
81a17f79
JB
15086 /* For now, we only support the C meaning of an unspecified type: void. */
15087
19f392bc 15088 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15089 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15090
f792889a 15091 return set_die_type (die, type, cu);
81a17f79 15092}
a02abb62 15093
639d11d3
DC
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
15100static struct die_info *
dee91e82 15101read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15102 const gdb_byte *info_ptr,
15103 const gdb_byte **new_info_ptr,
dee91e82 15104 struct die_info *parent)
639d11d3
DC
15105{
15106 struct die_info *die;
d521ce57 15107 const gdb_byte *cur_ptr;
639d11d3
DC
15108 int has_children;
15109
bf6af496 15110 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15111 if (die == NULL)
15112 {
15113 *new_info_ptr = cur_ptr;
15114 return NULL;
15115 }
93311388 15116 store_in_ref_table (die, reader->cu);
639d11d3
DC
15117
15118 if (has_children)
bf6af496 15119 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
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
15135static struct die_info *
bf6af496 15136read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15137 const gdb_byte *info_ptr,
15138 const gdb_byte **new_info_ptr,
bf6af496 15139 struct die_info *parent)
639d11d3
DC
15140{
15141 struct die_info *first_die, *last_sibling;
d521ce57 15142 const gdb_byte *cur_ptr;
639d11d3 15143
c906108c 15144 cur_ptr = info_ptr;
639d11d3
DC
15145 first_die = last_sibling = NULL;
15146
15147 while (1)
c906108c 15148 {
639d11d3 15149 struct die_info *die
dee91e82 15150 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15151
1d325ec1 15152 if (die == NULL)
c906108c 15153 {
639d11d3
DC
15154 *new_info_ptr = cur_ptr;
15155 return first_die;
c906108c 15156 }
1d325ec1
DJ
15157
15158 if (!first_die)
15159 first_die = die;
c906108c 15160 else
1d325ec1
DJ
15161 last_sibling->sibling = die;
15162
15163 last_sibling = die;
c906108c 15164 }
c906108c
SS
15165}
15166
bf6af496
DE
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
15172static struct die_info *
15173read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15174 const gdb_byte *info_ptr,
15175 const gdb_byte **new_info_ptr,
bf6af496
DE
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
b4f54984 15181 if (dwarf_die_debug)
bf6af496
DE
15182 {
15183 fprintf_unfiltered (gdb_stdlog,
15184 "Read die from %s@0x%x of %s:\n",
a32a8923 15185 get_section_name (reader->die_section),
bf6af496
DE
15186 (unsigned) (info_ptr - reader->die_section->buffer),
15187 bfd_get_filename (reader->abfd));
b4f54984 15188 dump_die (die, dwarf_die_debug);
bf6af496
DE
15189 }
15190
15191 return die;
15192}
15193
3019eac3
DE
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. */
93311388 15201
d521ce57 15202static const gdb_byte *
3019eac3 15203read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15204 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15205 int *has_children, int num_extra_attrs)
93311388 15206{
b64f50a1
JK
15207 unsigned int abbrev_number, bytes_read, i;
15208 sect_offset offset;
93311388
DE
15209 struct abbrev_info *abbrev;
15210 struct die_info *die;
15211 struct dwarf2_cu *cu = reader->cu;
15212 bfd *abfd = reader->abfd;
15213
b64f50a1 15214 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
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
433df2d4 15224 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15225 if (!abbrev)
348e048f
DE
15226 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15227 abbrev_number,
15228 bfd_get_filename (abfd));
15229
3019eac3 15230 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
15231 die->offset = offset;
15232 die->tag = abbrev->tag;
15233 die->abbrev = abbrev_number;
15234
3019eac3
DE
15235 /* Make the result usable.
15236 The caller needs to update num_attrs after adding the extra
15237 attributes. */
93311388
DE
15238 die->num_attrs = abbrev->num_attrs;
15239
15240 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15241 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15242 info_ptr);
93311388
DE
15243
15244 *diep = die;
15245 *has_children = abbrev->has_children;
15246 return info_ptr;
15247}
15248
3019eac3
DE
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
d521ce57 15254static const gdb_byte *
3019eac3 15255read_full_die (const struct die_reader_specs *reader,
d521ce57 15256 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15257 int *has_children)
15258{
d521ce57 15259 const gdb_byte *result;
bf6af496
DE
15260
15261 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15262
b4f54984 15263 if (dwarf_die_debug)
bf6af496
DE
15264 {
15265 fprintf_unfiltered (gdb_stdlog,
15266 "Read die from %s@0x%x of %s:\n",
a32a8923 15267 get_section_name (reader->die_section),
bf6af496
DE
15268 (unsigned) (info_ptr - reader->die_section->buffer),
15269 bfd_get_filename (reader->abfd));
b4f54984 15270 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15271 }
15272
15273 return result;
3019eac3 15274}
433df2d4
DE
15275\f
15276/* Abbreviation tables.
3019eac3 15277
433df2d4 15278 In DWARF version 2, the description of the debugging information is
c906108c
SS
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
433df2d4
DE
15281 in a hash table. */
15282
15283/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15284
15285static struct abbrev_info *
15286abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15287{
15288 struct abbrev_info *abbrev;
15289
8d749320 15290 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15291 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15292
433df2d4
DE
15293 return abbrev;
15294}
15295
15296/* Add an abbreviation to the table. */
c906108c
SS
15297
15298static void
433df2d4
DE
15299abbrev_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}
dee91e82 15309
433df2d4
DE
15310/* Look up an abbrev in the table.
15311 Returns NULL if the abbrev is not found. */
15312
15313static struct abbrev_info *
15314abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15315 unsigned int abbrev_number)
c906108c 15316{
433df2d4
DE
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
15334static struct abbrev_table *
15335abbrev_table_read_table (struct dwarf2_section_info *section,
15336 sect_offset offset)
15337{
15338 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15339 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15340 struct abbrev_table *abbrev_table;
d521ce57 15341 const gdb_byte *abbrev_ptr;
c906108c
SS
15342 struct abbrev_info *cur_abbrev;
15343 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15344 unsigned int abbrev_form;
f3dd6933
DJ
15345 struct attr_abbrev *cur_attrs;
15346 unsigned int allocated_attrs;
c906108c 15347
70ba0933 15348 abbrev_table = XNEW (struct abbrev_table);
f4dc4d17 15349 abbrev_table->offset = offset;
433df2d4 15350 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15351 abbrev_table->abbrevs =
15352 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15353 ABBREV_HASH_SIZE);
433df2d4
DE
15354 memset (abbrev_table->abbrevs, 0,
15355 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15356
433df2d4
DE
15357 dwarf2_read_section (objfile, section);
15358 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
15359 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15360 abbrev_ptr += bytes_read;
15361
f3dd6933 15362 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15363 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15364
0963b4bd 15365 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15366 while (abbrev_number)
15367 {
433df2d4 15368 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15369
15370 /* read in abbrev header */
15371 cur_abbrev->number = abbrev_number;
aead7601
SM
15372 cur_abbrev->tag
15373 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
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 {
f3dd6933 15385 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15386 {
f3dd6933
DJ
15387 allocated_attrs += ATTR_ALLOC_CHUNK;
15388 cur_attrs
224c3ddb 15389 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15390 }
ae038cb0 15391
aead7601
SM
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;
c906108c
SS
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
8d749320
SM
15402 cur_abbrev->attrs =
15403 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15404 cur_abbrev->num_attrs);
f3dd6933
DJ
15405 memcpy (cur_abbrev->attrs, cur_attrs,
15406 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15407
433df2d4 15408 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15409
15410 /* Get next abbreviation.
15411 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
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. */
433df2d4 15417 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15418 break;
15419 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15420 abbrev_ptr += bytes_read;
433df2d4 15421 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15422 break;
15423 }
f3dd6933
DJ
15424
15425 xfree (cur_attrs);
433df2d4 15426 return abbrev_table;
c906108c
SS
15427}
15428
433df2d4 15429/* Free the resources held by ABBREV_TABLE. */
c906108c 15430
c906108c 15431static void
433df2d4 15432abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15433{
433df2d4
DE
15434 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15435 xfree (abbrev_table);
c906108c
SS
15436}
15437
f4dc4d17
DE
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
73051182 15441 build_type_psymtabs_1. */
f4dc4d17
DE
15442
15443static void
15444abbrev_table_free_cleanup (void *table_ptr)
15445{
9a3c8263 15446 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15447
15448 if (*abbrev_table_ptr != NULL)
15449 abbrev_table_free (*abbrev_table_ptr);
15450 *abbrev_table_ptr = NULL;
15451}
15452
433df2d4
DE
15453/* Read the abbrev table for CU from ABBREV_SECTION. */
15454
15455static void
15456dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15457 struct dwarf2_section_info *abbrev_section)
c906108c 15458{
433df2d4
DE
15459 cu->abbrev_table =
15460 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15461}
c906108c 15462
433df2d4 15463/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15464
433df2d4
DE
15465static void
15466dwarf2_free_abbrev_table (void *ptr_to_cu)
15467{
9a3c8263 15468 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15469
a2ce51a0
DE
15470 if (cu->abbrev_table != NULL)
15471 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
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
72bf9492
DJ
15477/* Returns nonzero if TAG represents a type that we might generate a partial
15478 symbol for. */
15479
15480static int
15481is_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:
680b30c7 15497 case DW_TAG_interface_type:
72bf9492
DJ
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
15511static struct partial_die_info *
dee91e82 15512load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15513 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15514{
dee91e82 15515 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15516 struct objfile *objfile = cu->objfile;
72bf9492
DJ
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;
5afb4e99 15521 unsigned int load_all = 0;
72bf9492
DJ
15522 int nesting_level = 1;
15523
15524 parent_die = NULL;
15525 last_die = NULL;
15526
7adf1e79
DE
15527 gdb_assert (cu->per_cu != NULL);
15528 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
15529 load_all = 1;
15530
72bf9492
DJ
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
8d749320 15540 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
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
98bfdba5
PA
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. */
dee91e82 15577 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15578 continue;
15579 }
15580 }
15581
0d99eb77 15582 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
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 {
dee91e82 15589 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15590 continue;
15591 }
15592
5afb4e99
DJ
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)
72929c62 15599 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
15600 && abbrev->tag != DW_TAG_enumerator
15601 && abbrev->tag != DW_TAG_subprogram
bc30ff58 15602 && abbrev->tag != DW_TAG_lexical_block
72bf9492 15603 && abbrev->tag != DW_TAG_variable
5afb4e99 15604 && abbrev->tag != DW_TAG_namespace
f55ee35c 15605 && abbrev->tag != DW_TAG_module
95554aad 15606 && abbrev->tag != DW_TAG_member
74921315
KS
15607 && abbrev->tag != DW_TAG_imported_unit
15608 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
15609 {
15610 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15611 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
15612 continue;
15613 }
15614
dee91e82
DE
15615 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15616 info_ptr);
72bf9492
DJ
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
d8228535 15637 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
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)
04a679b8 15642 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15643 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 15644 &objfile->static_psymbols,
1762568f 15645 0, cu->language, objfile);
dee91e82 15646 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15647 continue;
15648 }
15649
d8228535
JK
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]"),
4262abfb 15662 part_die->offset.sect_off, objfile_name (objfile));
d8228535 15663
72bf9492
DJ
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)
3e43a32a
MS
15675 complaint (&symfile_complaints,
15676 _("malformed enumerator DIE ignored"));
72bf9492 15677 else if (building_psymtab)
04a679b8 15678 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15679 VAR_DOMAIN, LOC_CONST,
9c37b5ae 15680 cu->language == language_cplus
bb5ed363
DE
15681 ? &objfile->global_psymbols
15682 : &objfile->static_psymbols,
1762568f 15683 0, cu->language, objfile);
72bf9492 15684
dee91e82 15685 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
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
5afb4e99
DJ
15722 wasted time in find_partial_die, when we reread the compilation
15723 unit with load_all_dies set. */
72bf9492 15724
5afb4e99 15725 if (load_all
72929c62 15726 || abbrev->tag == DW_TAG_constant
5afb4e99 15727 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
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,
b64f50a1 15735 part_die->offset.sect_off, INSERT);
72bf9492
DJ
15736 *slot = part_die;
15737 }
15738
8d749320 15739 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
15740
15741 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 15742 we have no reason to follow the children of structures; for other
98bfdba5
PA
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).
bc30ff58
JB
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. */
72bf9492 15753 if (last_die->has_children
5afb4e99
DJ
15754 && (load_all
15755 || last_die->tag == DW_TAG_namespace
f55ee35c 15756 || last_die->tag == DW_TAG_module
72bf9492 15757 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
15758 || (cu->language == language_cplus
15759 && last_die->tag == DW_TAG_subprogram
15760 && (last_die->name == NULL
15761 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
15762 || (cu->language != language_c
15763 && (last_die->tag == DW_TAG_class_type
680b30c7 15764 || last_die->tag == DW_TAG_interface_type
72bf9492 15765 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
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))))
72bf9492
DJ
15770 {
15771 nesting_level++;
15772 parent_die = last_die;
15773 continue;
15774 }
15775
15776 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15777 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
15778
15779 /* Back to the top, do it again. */
15780 }
15781}
15782
c906108c
SS
15783/* Read a minimal amount of information into the minimal die structure. */
15784
d521ce57 15785static const gdb_byte *
dee91e82
DE
15786read_partial_die (const struct die_reader_specs *reader,
15787 struct partial_die_info *part_die,
15788 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 15789 const gdb_byte *info_ptr)
c906108c 15790{
dee91e82 15791 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15792 struct objfile *objfile = cu->objfile;
d521ce57 15793 const gdb_byte *buffer = reader->buffer;
fa238c03 15794 unsigned int i;
c906108c 15795 struct attribute attr;
c5aa993b 15796 int has_low_pc_attr = 0;
c906108c 15797 int has_high_pc_attr = 0;
91da1414 15798 int high_pc_relative = 0;
c906108c 15799
72bf9492 15800 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 15801
b64f50a1 15802 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
15803
15804 info_ptr += abbrev_len;
15805
15806 if (abbrev == NULL)
15807 return info_ptr;
15808
c906108c
SS
15809 part_die->tag = abbrev->tag;
15810 part_die->has_children = abbrev->has_children;
c906108c
SS
15811
15812 for (i = 0; i < abbrev->num_attrs; ++i)
15813 {
dee91e82 15814 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
15815
15816 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 15817 partial symbol table. */
c906108c
SS
15818 switch (attr.name)
15819 {
15820 case DW_AT_name:
71c25dea
TT
15821 switch (part_die->tag)
15822 {
15823 case DW_TAG_compile_unit:
95554aad 15824 case DW_TAG_partial_unit:
348e048f 15825 case DW_TAG_type_unit:
71c25dea
TT
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,
34a68019 15837 &objfile->per_bfd->storage_obstack);
71c25dea
TT
15838 break;
15839 }
c906108c 15840 break;
31ef98ae 15841 case DW_AT_linkage_name:
c906108c 15842 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
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. */
94af9270
KS
15846 if (cu->language == language_ada)
15847 part_die->name = DW_STRING (&attr);
abc72ce4 15848 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
15849 break;
15850 case DW_AT_low_pc:
15851 has_low_pc_attr = 1;
31aa7e4e 15852 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
15853 break;
15854 case DW_AT_high_pc:
15855 has_high_pc_attr = 1;
31aa7e4e
JB
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;
c906108c
SS
15859 break;
15860 case DW_AT_location:
0963b4bd 15861 /* Support the .debug_loc offsets. */
8e19ed76
PS
15862 if (attr_form_is_block (&attr))
15863 {
95554aad 15864 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 15865 }
3690dd37 15866 else if (attr_form_is_section_offset (&attr))
8e19ed76 15867 {
4d3c2250 15868 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15869 }
15870 else
15871 {
4d3c2250
KB
15872 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15873 "partial symbol information");
8e19ed76 15874 }
c906108c 15875 break;
c906108c
SS
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:
72bf9492
DJ
15887 case DW_AT_extension:
15888 part_die->has_specification = 1;
c764a876 15889 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
15890 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15891 || cu->per_cu->is_dwz);
c906108c
SS
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)
3e43a32a
MS
15897 complaint (&symfile_complaints,
15898 _("ignoring absolute DW_AT_sibling"));
c906108c 15899 else
b9502d3f
WN
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"));
22869d73
KS
15907 else if (sibling_ptr > reader->buffer_end)
15908 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
15909 else
15910 part_die->sibling = sibling_ptr;
15911 }
c906108c 15912 break;
fa4028e9
JB
15913 case DW_AT_byte_size:
15914 part_die->has_byte_size = 1;
15915 break;
ff908ebf
AW
15916 case DW_AT_const_value:
15917 part_die->has_const_value = 1;
15918 break;
68511cec
CES
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
0c1b455e
TT
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. */
68511cec 15934 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
15935 && cu->language == language_fortran)
15936 part_die->main_subprogram = 1;
68511cec 15937 break;
481860b3
GB
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;
95554aad
TT
15943
15944 case DW_AT_import:
15945 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
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 }
95554aad
TT
15951 break;
15952
0c1b455e
TT
15953 case DW_AT_main_subprogram:
15954 part_die->main_subprogram = DW_UNSND (&attr);
15955 break;
15956
c906108c
SS
15957 default:
15958 break;
15959 }
15960 }
15961
91da1414
MW
15962 if (high_pc_relative)
15963 part_die->highpc += part_die->lowpc;
15964
9373cf26
JK
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 {
bb5ed363 15977 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
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),
4262abfb 15983 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
15984 }
15985 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
15986 else if (part_die->lowpc >= part_die->highpc)
15987 {
bb5ed363 15988 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
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),
4262abfb 15995 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
15996 }
15997 else
15998 part_die->has_pc_info = 1;
15999 }
85cbf3d3 16000
c906108c
SS
16001 return info_ptr;
16002}
16003
72bf9492
DJ
16004/* Find a cached partial DIE at OFFSET in CU. */
16005
16006static struct partial_die_info *
b64f50a1 16007find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
16008{
16009 struct partial_die_info *lookup_die = NULL;
16010 struct partial_die_info part_die;
16011
16012 part_die.offset = offset;
9a3c8263
SM
16013 lookup_die = ((struct partial_die_info *)
16014 htab_find_with_hash (cu->partial_dies, &part_die,
16015 offset.sect_off));
72bf9492 16016
72bf9492
DJ
16017 return lookup_die;
16018}
16019
348e048f
DE
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
55f1336d 16023 DW_FORM_ref_sig8). */
72bf9492
DJ
16024
16025static struct partial_die_info *
36586728 16026find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16027{
bb5ed363 16028 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16029 struct dwarf2_per_cu_data *per_cu = NULL;
16030 struct partial_die_info *pd = NULL;
72bf9492 16031
36586728
TT
16032 if (offset_in_dwz == cu->per_cu->is_dwz
16033 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
16034 {
16035 pd = find_partial_die_in_comp_unit (offset, cu);
16036 if (pd != NULL)
16037 return pd;
0d99eb77
DE
16038 /* We missed recording what we needed.
16039 Load all dies and try again. */
16040 per_cu = cu->per_cu;
5afb4e99 16041 }
0d99eb77
DE
16042 else
16043 {
16044 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16045 if (cu->per_cu->is_debug_types)
0d99eb77
DE
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 }
36586728
TT
16052 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16053 objfile);
72bf9492 16054
0d99eb77
DE
16055 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16056 load_partial_comp_unit (per_cu);
ae038cb0 16057
0d99eb77
DE
16058 per_cu->cu->last_used = 0;
16059 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16060 }
5afb4e99 16061
dee91e82
DE
16062 /* If we didn't find it, and not all dies have been loaded,
16063 load them all and try again. */
16064
5afb4e99
DJ
16065 if (pd == NULL && per_cu->load_all_dies == 0)
16066 {
5afb4e99 16067 per_cu->load_all_dies = 1;
fd820528
DE
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. */
dee91e82 16075 load_partial_comp_unit (per_cu);
5afb4e99
DJ
16076
16077 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16078 }
16079
16080 if (pd == NULL)
16081 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16082 _("could not find partial DIE 0x%x "
16083 "in cache [from module %s]\n"),
b64f50a1 16084 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 16085 return pd;
72bf9492
DJ
16086}
16087
abc72ce4
DE
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
16092static void
16093guess_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)
36586728
TT
16111 real_pdi = find_partial_die (real_pdi->spec_offset,
16112 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
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
224c3ddb
SM
16130 = ((const char *)
16131 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16132 actual_class_name,
16133 strlen (actual_class_name)));
abc72ce4
DE
16134 xfree (actual_class_name);
16135 }
16136 break;
16137 }
16138 }
16139}
16140
72bf9492
DJ
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
16144static void
16145fixup_partial_die (struct partial_die_info *part_die,
16146 struct dwarf2_cu *cu)
16147{
abc72ce4
DE
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
72bf9492
DJ
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;
72bf9492 16160
36586728
TT
16161 spec_die = find_partial_die (part_die->spec_offset,
16162 part_die->spec_is_dwz, cu);
72bf9492 16163
10b3939b 16164 fixup_partial_die (spec_die, cu);
72bf9492
DJ
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. */
72bf9492
DJ
16177
16178 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16179 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16180
abc72ce4
DE
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
122d1940 16183 name. */
abc72ce4 16184 if (cu->language == language_cplus
8b70b953 16185 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
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
53832f31
TT
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
96408a79
SA
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)
53832f31
TT
16200 && part_die->linkage_name != NULL)
16201 {
16202 char *demangled;
16203
8de20a37 16204 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16205 if (demangled)
16206 {
96408a79
SA
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
34a68019 16217 part_die->name
224c3ddb
SM
16218 = ((const char *)
16219 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16220 base, strlen (base)));
53832f31
TT
16221 xfree (demangled);
16222 }
16223 }
16224
abc72ce4 16225 part_die->fixup_called = 1;
72bf9492
DJ
16226}
16227
a8329558 16228/* Read an attribute value described by an attribute form. */
c906108c 16229
d521ce57 16230static const gdb_byte *
dee91e82
DE
16231read_attribute_value (const struct die_reader_specs *reader,
16232 struct attribute *attr, unsigned form,
d521ce57 16233 const gdb_byte *info_ptr)
c906108c 16234{
dee91e82 16235 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16236 struct objfile *objfile = cu->objfile;
16237 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16238 bfd *abfd = reader->abfd;
e7c27a73 16239 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16240 unsigned int bytes_read;
16241 struct dwarf_block *blk;
16242
aead7601 16243 attr->form = (enum dwarf_form) form;
a8329558 16244 switch (form)
c906108c 16245 {
c906108c 16246 case DW_FORM_ref_addr:
ae411497 16247 if (cu->header.version == 2)
4568ecf9 16248 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16249 else
4568ecf9
DE
16250 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16251 &cu->header, &bytes_read);
ae411497
TT
16252 info_ptr += bytes_read;
16253 break;
36586728
TT
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;
ae411497 16258 case DW_FORM_addr:
e7c27a73 16259 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16260 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16261 info_ptr += bytes_read;
c906108c
SS
16262 break;
16263 case DW_FORM_block2:
7b5a2f43 16264 blk = dwarf_alloc_block (cu);
c906108c
SS
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:
7b5a2f43 16272 blk = dwarf_alloc_block (cu);
c906108c
SS
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;
2dc7f7b3
TT
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;
c906108c 16295 case DW_FORM_string:
9b1c24c8 16296 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16297 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16298 info_ptr += bytes_read;
16299 break;
4bdf3d34 16300 case DW_FORM_strp:
36586728
TT
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 }
4bdf3d34 16320 break;
2dc7f7b3 16321 case DW_FORM_exprloc:
c906108c 16322 case DW_FORM_block:
7b5a2f43 16323 blk = dwarf_alloc_block (cu);
c906108c
SS
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:
7b5a2f43 16331 blk = dwarf_alloc_block (cu);
c906108c
SS
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;
2dc7f7b3
TT
16346 case DW_FORM_flag_present:
16347 DW_UNSND (attr) = 1;
16348 break;
c906108c
SS
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:
4568ecf9
DE
16358 DW_UNSND (attr) = (cu->header.offset.sect_off
16359 + read_1_byte (abfd, info_ptr));
c906108c
SS
16360 info_ptr += 1;
16361 break;
16362 case DW_FORM_ref2:
4568ecf9
DE
16363 DW_UNSND (attr) = (cu->header.offset.sect_off
16364 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16365 info_ptr += 2;
16366 break;
16367 case DW_FORM_ref4:
4568ecf9
DE
16368 DW_UNSND (attr) = (cu->header.offset.sect_off
16369 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16370 info_ptr += 4;
16371 break;
613e1657 16372 case DW_FORM_ref8:
4568ecf9
DE
16373 DW_UNSND (attr) = (cu->header.offset.sect_off
16374 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16375 info_ptr += 8;
16376 break;
55f1336d 16377 case DW_FORM_ref_sig8:
ac9ec31b 16378 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16379 info_ptr += 8;
16380 break;
c906108c 16381 case DW_FORM_ref_udata:
4568ecf9
DE
16382 DW_UNSND (attr) = (cu->header.offset.sect_off
16383 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16384 info_ptr += bytes_read;
16385 break;
c906108c 16386 case DW_FORM_indirect:
a8329558
KW
16387 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16388 info_ptr += bytes_read;
dee91e82 16389 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 16390 break;
3019eac3
DE
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
342587c4 16416 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16417 DW_STRING_IS_CANONICAL (attr) = 0;
16418 info_ptr += bytes_read;
16419 }
16420 break;
c906108c 16421 default:
8a3fe4f8 16422 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16423 dwarf_form_name (form),
16424 bfd_get_filename (abfd));
c906108c 16425 }
28e94949 16426
36586728 16427 /* Super hack. */
7771576e 16428 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16429 attr->form = DW_FORM_GNU_ref_alt;
16430
28e94949
JB
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)
01c66ae6
JB
16440 {
16441 complaint
16442 (&symfile_complaints,
43bbcdc2
PH
16443 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16444 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16445 DW_UNSND (attr) = 0;
16446 }
28e94949 16447
c906108c
SS
16448 return info_ptr;
16449}
16450
a8329558
KW
16451/* Read an attribute described by an abbreviated attribute. */
16452
d521ce57 16453static const gdb_byte *
dee91e82
DE
16454read_attribute (const struct die_reader_specs *reader,
16455 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16456 const gdb_byte *info_ptr)
a8329558
KW
16457{
16458 attr->name = abbrev->name;
dee91e82 16459 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
16460}
16461
0963b4bd 16462/* Read dwarf information from a buffer. */
c906108c
SS
16463
16464static unsigned int
a1855c1d 16465read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16466{
fe1b8b76 16467 return bfd_get_8 (abfd, buf);
c906108c
SS
16468}
16469
16470static int
a1855c1d 16471read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16472{
fe1b8b76 16473 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16474}
16475
16476static unsigned int
a1855c1d 16477read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16478{
fe1b8b76 16479 return bfd_get_16 (abfd, buf);
c906108c
SS
16480}
16481
21ae7a4d 16482static int
a1855c1d 16483read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16484{
16485 return bfd_get_signed_16 (abfd, buf);
16486}
16487
c906108c 16488static unsigned int
a1855c1d 16489read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16490{
fe1b8b76 16491 return bfd_get_32 (abfd, buf);
c906108c
SS
16492}
16493
21ae7a4d 16494static int
a1855c1d 16495read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16496{
16497 return bfd_get_signed_32 (abfd, buf);
16498}
16499
93311388 16500static ULONGEST
a1855c1d 16501read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16502{
fe1b8b76 16503 return bfd_get_64 (abfd, buf);
c906108c
SS
16504}
16505
16506static CORE_ADDR
d521ce57 16507read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 16508 unsigned int *bytes_read)
c906108c 16509{
e7c27a73 16510 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16511 CORE_ADDR retval = 0;
16512
107d2387 16513 if (cu_header->signed_addr_p)
c906108c 16514 {
107d2387
AC
16515 switch (cu_header->addr_size)
16516 {
16517 case 2:
fe1b8b76 16518 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
16519 break;
16520 case 4:
fe1b8b76 16521 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
16522 break;
16523 case 8:
fe1b8b76 16524 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
16525 break;
16526 default:
8e65ff28 16527 internal_error (__FILE__, __LINE__,
e2e0b3e5 16528 _("read_address: bad switch, signed [in module %s]"),
659b0389 16529 bfd_get_filename (abfd));
107d2387
AC
16530 }
16531 }
16532 else
16533 {
16534 switch (cu_header->addr_size)
16535 {
16536 case 2:
fe1b8b76 16537 retval = bfd_get_16 (abfd, buf);
107d2387
AC
16538 break;
16539 case 4:
fe1b8b76 16540 retval = bfd_get_32 (abfd, buf);
107d2387
AC
16541 break;
16542 case 8:
fe1b8b76 16543 retval = bfd_get_64 (abfd, buf);
107d2387
AC
16544 break;
16545 default:
8e65ff28 16546 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
16547 _("read_address: bad switch, "
16548 "unsigned [in module %s]"),
659b0389 16549 bfd_get_filename (abfd));
107d2387 16550 }
c906108c 16551 }
64367e0a 16552
107d2387
AC
16553 *bytes_read = cu_header->addr_size;
16554 return retval;
c906108c
SS
16555}
16556
f7ef9339 16557/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
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
f7ef9339
KB
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
917c78fc
MK
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.
f7ef9339 16575
917c78fc
MK
16576 The value returned via bytes_read should be used to increment the
16577 relevant pointer after calling read_initial_length().
c764a876 16578
613e1657
KB
16579 [ Note: read_initial_length() and read_offset() are based on the
16580 document entitled "DWARF Debugging Information Format", revision
f7ef9339 16581 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
16582 from:
16583
f7ef9339 16584 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 16585
613e1657
KB
16586 This document is only a draft and is subject to change. (So beware.)
16587
f7ef9339 16588 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
16589 determined empirically by examining 64-bit ELF files produced by
16590 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
16591
16592 - Kevin, July 16, 2002
613e1657
KB
16593 ] */
16594
16595static LONGEST
d521ce57 16596read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 16597{
fe1b8b76 16598 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 16599
dd373385 16600 if (length == 0xffffffff)
613e1657 16601 {
fe1b8b76 16602 length = bfd_get_64 (abfd, buf + 4);
613e1657 16603 *bytes_read = 12;
613e1657 16604 }
dd373385 16605 else if (length == 0)
f7ef9339 16606 {
dd373385 16607 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 16608 length = bfd_get_64 (abfd, buf);
f7ef9339 16609 *bytes_read = 8;
f7ef9339 16610 }
613e1657
KB
16611 else
16612 {
16613 *bytes_read = 4;
613e1657
KB
16614 }
16615
c764a876
DE
16616 return length;
16617}
dd373385 16618
c764a876
DE
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. */
dd373385 16626
c764a876 16627static LONGEST
d521ce57 16628read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
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"));
dd373385 16642
c764a876 16643 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 16644 return length;
613e1657
KB
16645}
16646
16647/* Read an offset from the data stream. The size of the offset is
917c78fc 16648 given by cu_header->offset_size. */
613e1657
KB
16649
16650static LONGEST
d521ce57
TT
16651read_offset (bfd *abfd, const gdb_byte *buf,
16652 const struct comp_unit_head *cu_header,
891d2f0b 16653 unsigned int *bytes_read)
c764a876
DE
16654{
16655 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 16656
c764a876
DE
16657 *bytes_read = cu_header->offset_size;
16658 return offset;
16659}
16660
16661/* Read an offset from the data stream. */
16662
16663static LONGEST
d521ce57 16664read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
16665{
16666 LONGEST retval = 0;
16667
c764a876 16668 switch (offset_size)
613e1657
KB
16669 {
16670 case 4:
fe1b8b76 16671 retval = bfd_get_32 (abfd, buf);
613e1657
KB
16672 break;
16673 case 8:
fe1b8b76 16674 retval = bfd_get_64 (abfd, buf);
613e1657
KB
16675 break;
16676 default:
8e65ff28 16677 internal_error (__FILE__, __LINE__,
c764a876 16678 _("read_offset_1: bad switch [in module %s]"),
659b0389 16679 bfd_get_filename (abfd));
613e1657
KB
16680 }
16681
917c78fc 16682 return retval;
613e1657
KB
16683}
16684
d521ce57
TT
16685static const gdb_byte *
16686read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
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. */
4bdf3d34 16691 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 16692 return buf;
c906108c
SS
16693}
16694
d521ce57
TT
16695static const char *
16696read_direct_string (bfd *abfd, const gdb_byte *buf,
16697 unsigned int *bytes_read_ptr)
c906108c
SS
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. */
4bdf3d34 16702 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
16703 if (*buf == '\0')
16704 {
16705 *bytes_read_ptr = 1;
16706 return NULL;
16707 }
d521ce57
TT
16708 *bytes_read_ptr = strlen ((const char *) buf) + 1;
16709 return (const char *) buf;
4bdf3d34
JJ
16710}
16711
d521ce57 16712static const char *
cf2c3c16 16713read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 16714{
be391dca 16715 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 16716 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
16717 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
16718 bfd_get_filename (abfd));
dce234bc 16719 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
16720 error (_("DW_FORM_strp pointing outside of "
16721 ".debug_str section [in module %s]"),
16722 bfd_get_filename (abfd));
4bdf3d34 16723 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 16724 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 16725 return NULL;
d521ce57 16726 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
16727}
16728
36586728
TT
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
d521ce57 16734static const char *
36586728
TT
16735read_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;
d521ce57 16750 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
16751}
16752
d521ce57
TT
16753static const char *
16754read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
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
12df843f 16763static ULONGEST
d521ce57
TT
16764read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
16765 unsigned int *bytes_read_ptr)
c906108c 16766{
12df843f 16767 ULONGEST result;
ce5d95e1 16768 unsigned int num_read;
870f88f7 16769 int shift;
c906108c
SS
16770 unsigned char byte;
16771
16772 result = 0;
16773 shift = 0;
16774 num_read = 0;
c906108c
SS
16775 while (1)
16776 {
fe1b8b76 16777 byte = bfd_get_8 (abfd, buf);
c906108c
SS
16778 buf++;
16779 num_read++;
12df843f 16780 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
16781 if ((byte & 128) == 0)
16782 {
16783 break;
16784 }
16785 shift += 7;
16786 }
16787 *bytes_read_ptr = num_read;
16788 return result;
16789}
16790
12df843f 16791static LONGEST
d521ce57
TT
16792read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
16793 unsigned int *bytes_read_ptr)
c906108c 16794{
12df843f 16795 LONGEST result;
870f88f7 16796 int shift, num_read;
c906108c
SS
16797 unsigned char byte;
16798
16799 result = 0;
16800 shift = 0;
c906108c 16801 num_read = 0;
c906108c
SS
16802 while (1)
16803 {
fe1b8b76 16804 byte = bfd_get_8 (abfd, buf);
c906108c
SS
16805 buf++;
16806 num_read++;
12df843f 16807 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
16808 shift += 7;
16809 if ((byte & 128) == 0)
16810 {
16811 break;
16812 }
16813 }
77e0b926 16814 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 16815 result |= -(((LONGEST) 1) << shift);
c906108c
SS
16816 *bytes_read_ptr = num_read;
16817 return result;
16818}
16819
3019eac3
DE
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
16824static CORE_ADDR
16825read_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]"),
4262abfb 16834 objfile_name (objfile));
3019eac3
DE
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]"),
4262abfb 16838 objfile_name (objfile));
3019eac3
DE
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
16849static CORE_ADDR
16850read_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
16857static CORE_ADDR
d521ce57 16858read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
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
16870struct 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
16878static void
16879dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 16880 const gdb_byte *info_ptr,
3019eac3
DE
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
16898CORE_ADDR
16899dwarf2_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.
a1b64ce1 16922
3019eac3 16923 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 16924 For now we skip this optimization. */
3019eac3
DE
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
a1b64ce1
DE
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);
3019eac3
DE
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
57d63ce2
DE
16946/* Given a DW_FORM_GNU_str_index, fetch the string.
16947 This is only used by the Fission support. */
3019eac3 16948
d521ce57 16949static const char *
342587c4 16950read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
16951{
16952 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 16953 const char *objf_name = objfile_name (objfile);
3019eac3 16954 bfd *abfd = objfile->obfd;
342587c4 16955 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
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;
d521ce57 16959 const gdb_byte *info_ptr;
3019eac3 16960 ULONGEST str_offset;
57d63ce2 16961 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 16962
73869dc2
DE
16963 dwarf2_read_section (objfile, str_section);
16964 dwarf2_read_section (objfile, str_offsets_section);
16965 if (str_section->buffer == NULL)
57d63ce2 16966 error (_("%s used without .debug_str.dwo section"
3019eac3 16967 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 16968 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 16969 if (str_offsets_section->buffer == NULL)
57d63ce2 16970 error (_("%s used without .debug_str_offsets.dwo section"
3019eac3 16971 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 16972 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 16973 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 16974 error (_("%s pointing outside of .debug_str_offsets.dwo"
3019eac3 16975 " section in CU at offset 0x%lx [in module %s]"),
c5164cbc 16976 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 16977 info_ptr = (str_offsets_section->buffer
3019eac3
DE
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);
73869dc2 16983 if (str_offset >= str_section->size)
57d63ce2 16984 error (_("Offset from %s pointing outside of"
3019eac3 16985 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
c5164cbc 16986 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 16987 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
16988}
16989
3019eac3
DE
16990/* Return the length of an LEB128 number in BUF. */
16991
16992static int
16993leb128_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
c906108c 17006static void
e142c38c 17007set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17008{
17009 switch (lang)
17010 {
17011 case DW_LANG_C89:
76bee0cc 17012 case DW_LANG_C99:
0cfd832f 17013 case DW_LANG_C11:
c906108c 17014 case DW_LANG_C:
d1be3247 17015 case DW_LANG_UPC:
e142c38c 17016 cu->language = language_c;
c906108c 17017 break;
9c37b5ae 17018 case DW_LANG_Java:
c906108c 17019 case DW_LANG_C_plus_plus:
0cfd832f
MW
17020 case DW_LANG_C_plus_plus_11:
17021 case DW_LANG_C_plus_plus_14:
e142c38c 17022 cu->language = language_cplus;
c906108c 17023 break;
6aecb9c2
JB
17024 case DW_LANG_D:
17025 cu->language = language_d;
17026 break;
c906108c
SS
17027 case DW_LANG_Fortran77:
17028 case DW_LANG_Fortran90:
b21b22e0 17029 case DW_LANG_Fortran95:
f7de9aab
MW
17030 case DW_LANG_Fortran03:
17031 case DW_LANG_Fortran08:
e142c38c 17032 cu->language = language_fortran;
c906108c 17033 break;
a766d390
DE
17034 case DW_LANG_Go:
17035 cu->language = language_go;
17036 break;
c906108c 17037 case DW_LANG_Mips_Assembler:
e142c38c 17038 cu->language = language_asm;
c906108c
SS
17039 break;
17040 case DW_LANG_Ada83:
8aaf0b47 17041 case DW_LANG_Ada95:
bc5f45f8
JB
17042 cu->language = language_ada;
17043 break;
72019c9c
GM
17044 case DW_LANG_Modula2:
17045 cu->language = language_m2;
17046 break;
fe8e67fd
PM
17047 case DW_LANG_Pascal83:
17048 cu->language = language_pascal;
17049 break;
22566fbd
DJ
17050 case DW_LANG_ObjC:
17051 cu->language = language_objc;
17052 break;
c44af4eb
TT
17053 case DW_LANG_Rust:
17054 case DW_LANG_Rust_old:
17055 cu->language = language_rust;
17056 break;
c906108c
SS
17057 case DW_LANG_Cobol74:
17058 case DW_LANG_Cobol85:
c906108c 17059 default:
e142c38c 17060 cu->language = language_minimal;
c906108c
SS
17061 break;
17062 }
e142c38c 17063 cu->language_defn = language_def (cu->language);
c906108c
SS
17064}
17065
17066/* Return the named attribute or NULL if not there. */
17067
17068static struct attribute *
e142c38c 17069dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17070{
a48e046c 17071 for (;;)
c906108c 17072 {
a48e046c
TT
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;
c906108c 17087
f2f0e013 17088 die = follow_die_ref (die, spec, &cu);
f2f0e013 17089 }
c5aa993b 17090
c906108c
SS
17091 return NULL;
17092}
17093
348e048f
DE
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
17100static struct attribute *
45e58e77 17101dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
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
7d45c7c3
KB
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
17115static const char *
17116dwarf2_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
05cf31d1
JB
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
2dc7f7b3 17141 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17142
17143static int
17144dwarf2_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
3ca72b44 17151static int
e142c38c 17152die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17153{
05cf31d1
JB
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);
3ca72b44
AC
17163}
17164
63d06c5c 17165/* Return the die giving the specification for DIE, if there is
f2f0e013 17166 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17167 containing the return value on output. If there is no
17168 specification, but there is an abstract origin, that is
17169 returned. */
63d06c5c
DC
17170
17171static struct die_info *
f2f0e013 17172die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17173{
f2f0e013
DJ
17174 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17175 *spec_cu);
63d06c5c 17176
edb3359d
DJ
17177 if (spec_attr == NULL)
17178 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17179
63d06c5c
DC
17180 if (spec_attr == NULL)
17181 return NULL;
17182 else
f2f0e013 17183 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17184}
c906108c 17185
debd256d 17186/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
17187 refers to.
17188 NOTE: This is also used as a "cleanup" function. */
17189
debd256d
JB
17190static void
17191free_line_header (struct line_header *lh)
17192{
17193 if (lh->standard_opcode_lengths)
a8bc7b56 17194 xfree (lh->standard_opcode_lengths);
debd256d
JB
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)
a8bc7b56 17199 xfree (lh->file_names);
debd256d
JB
17200
17201 /* Similarly for the include directory names. */
17202 if (lh->include_dirs)
a8bc7b56 17203 xfree (lh->include_dirs);
debd256d 17204
a8bc7b56 17205 xfree (lh);
debd256d
JB
17206}
17207
527f3840
JK
17208/* Stub for free_line_header to match void * callback types. */
17209
17210static void
17211free_line_header_voidp (void *arg)
17212{
9a3c8263 17213 struct line_header *lh = (struct line_header *) arg;
527f3840
JK
17214
17215 free_line_header (lh);
17216}
17217
debd256d 17218/* Add an entry to LH's include directory table. */
ae2de4f8 17219
debd256d 17220static void
d521ce57 17221add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 17222{
27e0867f
DE
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
debd256d
JB
17227 /* Grow the array if necessary. */
17228 if (lh->include_dirs_size == 0)
c5aa993b 17229 {
debd256d 17230 lh->include_dirs_size = 1; /* for testing */
8d749320 17231 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
debd256d
JB
17232 }
17233 else if (lh->num_include_dirs >= lh->include_dirs_size)
17234 {
17235 lh->include_dirs_size *= 2;
8d749320
SM
17236 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17237 lh->include_dirs_size);
c5aa993b 17238 }
c906108c 17239
debd256d
JB
17240 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17241}
6e70227d 17242
debd256d 17243/* Add an entry to LH's file name table. */
ae2de4f8 17244
debd256d
JB
17245static void
17246add_file_name (struct line_header *lh,
d521ce57 17247 const char *name,
debd256d
JB
17248 unsigned int dir_index,
17249 unsigned int mod_time,
17250 unsigned int length)
17251{
17252 struct file_entry *fe;
17253
27e0867f
DE
17254 if (dwarf_line_debug >= 2)
17255 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17256 lh->num_file_names + 1, name);
17257
debd256d
JB
17258 /* Grow the array if necessary. */
17259 if (lh->file_names_size == 0)
17260 {
17261 lh->file_names_size = 1; /* for testing */
8d749320 17262 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
debd256d
JB
17263 }
17264 else if (lh->num_file_names >= lh->file_names_size)
17265 {
17266 lh->file_names_size *= 2;
224c3ddb
SM
17267 lh->file_names
17268 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
debd256d
JB
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;
aaa75496 17276 fe->included_p = 0;
cb1df416 17277 fe->symtab = NULL;
debd256d 17278}
6e70227d 17279
83769d0b 17280/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17281
17282static struct dwarf2_section_info *
17283get_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
debd256d 17303/* Read the statement program header starting at OFFSET in
3019eac3 17304 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17305 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17306 Returns NULL if there is a problem reading the header, e.g., if it
17307 has a version we don't understand.
debd256d
JB
17308
17309 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17310 the returned object point into the dwarf line section buffer,
17311 and must not be freed. */
ae2de4f8 17312
debd256d 17313static struct line_header *
3019eac3 17314dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
17315{
17316 struct cleanup *back_to;
17317 struct line_header *lh;
d521ce57 17318 const gdb_byte *line_ptr;
c764a876 17319 unsigned int bytes_read, offset_size;
debd256d 17320 int i;
d521ce57 17321 const char *cur_dir, *cur_file;
3019eac3
DE
17322 struct dwarf2_section_info *section;
17323 bfd *abfd;
17324
36586728 17325 section = get_debug_line_section (cu);
3019eac3
DE
17326 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17327 if (section->buffer == NULL)
debd256d 17328 {
3019eac3
DE
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"));
debd256d
JB
17333 return 0;
17334 }
17335
fceca515
DE
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. */
a32a8923 17338 abfd = get_section_bfd_owner (section);
fceca515 17339
a738430d
MK
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. */
3019eac3 17342 if (offset + 4 >= section->size)
debd256d 17343 {
4d3c2250 17344 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17345 return 0;
17346 }
17347
8d749320 17348 lh = XNEW (struct line_header);
debd256d
JB
17349 memset (lh, 0, sizeof (*lh));
17350 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17351 (void *) lh);
17352
527f3840
JK
17353 lh->offset.sect_off = offset;
17354 lh->offset_in_dwz = cu->per_cu->is_dwz;
17355
3019eac3 17356 line_ptr = section->buffer + offset;
debd256d 17357
a738430d 17358 /* Read in the header. */
6e70227d 17359 lh->total_length =
c764a876
DE
17360 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17361 &bytes_read, &offset_size);
debd256d 17362 line_ptr += bytes_read;
3019eac3 17363 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17364 {
4d3c2250 17365 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 17366 do_cleanups (back_to);
debd256d
JB
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;
cd366ee8
DE
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 }
c764a876
DE
17380 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17381 line_ptr += offset_size;
debd256d
JB
17382 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17383 line_ptr += 1;
2dc7f7b3
TT
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,
3e43a32a
MS
17396 _("invalid maximum_ops_per_instruction "
17397 "in `.debug_line' section"));
2dc7f7b3
TT
17398 }
17399
debd256d
JB
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;
8d749320 17408 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
debd256d
JB
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
a738430d 17417 /* Read directory table. */
9b1c24c8 17418 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
17419 {
17420 line_ptr += bytes_read;
17421 add_include_dir (lh, cur_dir);
17422 }
17423 line_ptr += bytes_read;
17424
a738430d 17425 /* Read file name table. */
9b1c24c8 17426 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
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;
6e70227d 17441 lh->statement_program_start = line_ptr;
debd256d 17442
3019eac3 17443 if (line_ptr > (section->buffer + section->size))
4d3c2250 17444 complaint (&symfile_complaints,
3e43a32a
MS
17445 _("line number info header doesn't "
17446 "fit in `.debug_line' section"));
debd256d
JB
17447
17448 discard_cleanups (back_to);
17449 return lh;
17450}
c906108c 17451
c6da4cef
DE
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.
1ed59174
JK
17457 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17458
17459 The function creates dangling cleanup registration. */
c6da4cef 17460
d521ce57 17461static const char *
c6da4cef
DE
17462psymtab_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];
d521ce57
TT
17467 const char *include_name = fe.name;
17468 const char *include_name_to_compare = include_name;
17469 const char *dir_name = NULL;
72b9f47f
TT
17470 const char *pst_filename;
17471 char *copied_name = NULL;
c6da4cef
DE
17472 int file_is_pst;
17473
afa6c9ab 17474 if (fe.dir_index && lh->include_dirs != NULL)
c6da4cef
DE
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"
5f52445b
YQ
17497 DW_AT_name = "./hello.c"
17498
17499 */
c6da4cef
DE
17500
17501 if (dir_name != NULL)
17502 {
d521ce57
TT
17503 char *tem = concat (dir_name, SLASH_STRING,
17504 include_name, (char *)NULL);
17505
17506 make_cleanup (xfree, tem);
17507 include_name = tem;
c6da4cef 17508 include_name_to_compare = include_name;
c6da4cef
DE
17509 }
17510 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
17511 {
d521ce57
TT
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;
c6da4cef
DE
17517 }
17518 }
17519
17520 pst_filename = pst->filename;
17521 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
17522 {
72b9f47f
TT
17523 copied_name = concat (pst->dirname, SLASH_STRING,
17524 pst_filename, (char *)NULL);
17525 pst_filename = copied_name;
c6da4cef
DE
17526 }
17527
1e3fad37 17528 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 17529
72b9f47f
TT
17530 if (copied_name != NULL)
17531 xfree (copied_name);
c6da4cef
DE
17532
17533 if (file_is_pst)
17534 return NULL;
17535 return include_name;
17536}
17537
d9b3de22
DE
17538/* State machine to track the state of the line number program. */
17539
17540typedef 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
17572typedef 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
c91513d8
PP
17586/* Ignore this record_line request. */
17587
17588static void
17589noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
17590{
17591 return;
17592}
17593
a05a36a5
DE
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
17624static int
17625dwarf_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
252a6764
DE
17641/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
17642 in the line table of subfile SUBFILE. */
17643
17644static void
d9b3de22
DE
17645dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
17646 unsigned int line, CORE_ADDR address,
17647 record_line_ftype p_record_line)
252a6764
DE
17648{
17649 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
17650
27e0867f
DE
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
d5962de5 17659 (*p_record_line) (subfile, line, addr);
252a6764
DE
17660}
17661
17662/* Subroutine of dwarf_decode_lines_1 to simplify it.
17663 Mark the end of a set of line number records.
d9b3de22 17664 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
17665 If SUBFILE is NULL the request is ignored. */
17666
17667static void
17668dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
17669 CORE_ADDR address, record_line_ftype p_record_line)
17670{
27e0867f
DE
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
d9b3de22
DE
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
17688static void
17689dwarf_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 {
e815d2d2 17721 if (state->last_subfile != current_subfile || end_sequence)
d9b3de22
DE
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
17746static void
17747init_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;
252a6764
DE
17772}
17773
924c2928
DE
17774/* Check address and if invalid nop-out the rest of the lines in this
17775 sequence. */
17776
17777static void
d9b3de22 17778check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
924c2928
DE
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));
d9b3de22
DE
17798 state->record_line = noop_record_line;
17799 /* Note: sm.record_line is left as noop_record_line
924c2928
DE
17800 until we see DW_LNE_end_sequence. */
17801 }
17802}
17803
f3f5162e 17804/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
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. */
debd256d 17808
c906108c 17809static void
43f3e411
DE
17810dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
17811 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 17812{
d521ce57
TT
17813 const gdb_byte *line_ptr, *extended_end;
17814 const gdb_byte *line_end;
a8c50c1f 17815 unsigned int bytes_read, extended_len;
699ca60a 17816 unsigned char op_code, extended_op;
e142c38c
DJ
17817 CORE_ADDR baseaddr;
17818 struct objfile *objfile = cu->objfile;
f3f5162e 17819 bfd *abfd = objfile->obfd;
fbf65064 17820 struct gdbarch *gdbarch = get_objfile_arch (objfile);
d9b3de22
DE
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;
e142c38c
DJ
17826
17827 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 17828
debd256d
JB
17829 line_ptr = lh->statement_program_start;
17830 line_end = lh->statement_program_end;
c906108c 17831
d9b3de22
DE
17832 reader_state.gdbarch = gdbarch;
17833 reader_state.line_header = lh;
17834 reader_state.record_lines_p = record_lines_p;
17835
c906108c
SS
17836 /* Read the statement sequences until there's nothing left. */
17837 while (line_ptr < line_end)
17838 {
d9b3de22
DE
17839 /* The DWARF line number program state machine. */
17840 lnp_state_machine state_machine;
c906108c 17841 int end_sequence = 0;
d9b3de22
DE
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)
c906108c 17847 {
aaa75496 17848 /* Start a subfile for the current file of the state machine. */
debd256d
JB
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. */
d9b3de22 17852 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
d521ce57 17853 const char *dir = NULL;
a738430d 17854
afa6c9ab 17855 if (fe->dir_index && lh->include_dirs != NULL)
debd256d 17856 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb 17857
4d663531 17858 dwarf2_start_subfile (fe->name, dir);
c906108c
SS
17859 }
17860
a738430d 17861 /* Decode the table. */
d9b3de22 17862 while (line_ptr < line_end && !end_sequence)
c906108c
SS
17863 {
17864 op_code = read_1_byte (abfd, line_ptr);
17865 line_ptr += 1;
9aa1fe7e 17866
debd256d 17867 if (op_code >= lh->opcode_base)
6e70227d 17868 {
8e07a239 17869 /* Special opcode. */
699ca60a 17870 unsigned char adj_opcode;
3e29f34a 17871 CORE_ADDR addr_adj;
a05a36a5 17872 int line_delta;
8e07a239 17873
debd256d 17874 adj_opcode = op_code - lh->opcode_base;
d9b3de22
DE
17875 addr_adj = (((state_machine.op_index
17876 + (adj_opcode / lh->line_range))
2dc7f7b3
TT
17877 / lh->maximum_ops_per_instruction)
17878 * lh->minimum_instruction_length);
d9b3de22
DE
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);
a05a36a5 17884 line_delta = lh->line_base + (adj_opcode % lh->line_range);
d9b3de22 17885 state_machine.line += line_delta;
a05a36a5 17886 if (line_delta != 0)
d9b3de22
DE
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;
9aa1fe7e
GK
17892 }
17893 else switch (op_code)
c906108c
SS
17894 {
17895 case DW_LNS_extended_op:
3e43a32a
MS
17896 extended_len = read_unsigned_leb128 (abfd, line_ptr,
17897 &bytes_read);
473b7be6 17898 line_ptr += bytes_read;
a8c50c1f 17899 extended_end = line_ptr + extended_len;
c906108c
SS
17900 extended_op = read_1_byte (abfd, line_ptr);
17901 line_ptr += 1;
17902 switch (extended_op)
17903 {
17904 case DW_LNE_end_sequence:
d9b3de22 17905 state_machine.record_line = record_line;
c906108c 17906 end_sequence = 1;
c906108c
SS
17907 break;
17908 case DW_LNE_set_address:
d9b3de22
DE
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 }
c906108c
SS
17921 break;
17922 case DW_LNE_define_file:
debd256d 17923 {
d521ce57 17924 const char *cur_file;
debd256d 17925 unsigned int dir_index, mod_time, length;
6e70227d 17926
3e43a32a
MS
17927 cur_file = read_direct_string (abfd, line_ptr,
17928 &bytes_read);
debd256d
JB
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 }
c906108c 17941 break;
d0c6ba3d
CC
17942 case DW_LNE_set_discriminator:
17943 /* The discriminator is not interesting to the debugger;
a05a36a5
DE
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. */
d9b3de22
DE
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;
a05a36a5 17952 line_ptr += bytes_read;
d0c6ba3d 17953 break;
c906108c 17954 default:
4d3c2250 17955 complaint (&symfile_complaints,
e2e0b3e5 17956 _("mangled .debug_line section"));
debd256d 17957 return;
c906108c 17958 }
a8c50c1f
DJ
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 }
c906108c
SS
17968 break;
17969 case DW_LNS_copy:
d9b3de22
DE
17970 dwarf_record_line (&reader_state, &state_machine, 0);
17971 state_machine.discriminator = 0;
c906108c
SS
17972 break;
17973 case DW_LNS_advance_pc:
2dc7f7b3
TT
17974 {
17975 CORE_ADDR adjust
17976 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3e29f34a 17977 CORE_ADDR addr_adj;
2dc7f7b3 17978
d9b3de22 17979 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
17980 / lh->maximum_ops_per_instruction)
17981 * lh->minimum_instruction_length);
d9b3de22
DE
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);
2dc7f7b3
TT
17986 line_ptr += bytes_read;
17987 }
c906108c
SS
17988 break;
17989 case DW_LNS_advance_line:
a05a36a5
DE
17990 {
17991 int line_delta
17992 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
17993
d9b3de22 17994 state_machine.line += line_delta;
a05a36a5 17995 if (line_delta != 0)
d9b3de22
DE
17996 state_machine.line_has_non_zero_discriminator
17997 = state_machine.discriminator != 0;
a05a36a5
DE
17998 line_ptr += bytes_read;
17999 }
c906108c
SS
18000 break;
18001 case DW_LNS_set_file:
d9b3de22
DE
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 }
c906108c
SS
18029 break;
18030 case DW_LNS_set_column:
0ad93d4f 18031 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18032 line_ptr += bytes_read;
18033 break;
18034 case DW_LNS_negate_stmt:
d9b3de22 18035 state_machine.is_stmt = (!state_machine.is_stmt);
c906108c
SS
18036 break;
18037 case DW_LNS_set_basic_block:
c906108c 18038 break;
c2c6d25f
JM
18039 /* Add to the address register of the state machine the
18040 address increment value corresponding to special opcode
a738430d
MK
18041 255. I.e., this value is scaled by the minimum
18042 instruction length since special opcode 255 would have
b021a221 18043 scaled the increment. */
c906108c 18044 case DW_LNS_const_add_pc:
2dc7f7b3
TT
18045 {
18046 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
3e29f34a 18047 CORE_ADDR addr_adj;
2dc7f7b3 18048
d9b3de22 18049 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
18050 / lh->maximum_ops_per_instruction)
18051 * lh->minimum_instruction_length);
d9b3de22
DE
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);
2dc7f7b3 18056 }
c906108c
SS
18057 break;
18058 case DW_LNS_fixed_advance_pc:
3e29f34a
MR
18059 {
18060 CORE_ADDR addr_adj;
18061
18062 addr_adj = read_2_bytes (abfd, line_ptr);
d9b3de22
DE
18063 state_machine.address
18064 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18065 state_machine.op_index = 0;
3e29f34a
MR
18066 line_ptr += 2;
18067 }
c906108c 18068 break;
9aa1fe7e 18069 default:
a738430d
MK
18070 {
18071 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18072 int i;
a738430d 18073
debd256d 18074 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18075 {
18076 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18077 line_ptr += bytes_read;
18078 }
18079 }
c906108c
SS
18080 }
18081 }
d9b3de22
DE
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);
c906108c 18089 }
f3f5162e
DE
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.
c3b7b696
YQ
18112 A good testcase for this is mb-inline.exp.
18113
527f3840
JK
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. */
f3f5162e
DE
18119
18120static void
18121dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18122 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18123 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18124{
18125 struct objfile *objfile = cu->objfile;
18126 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18127
527f3840
JK
18128 if (decode_mapping)
18129 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
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 {
d521ce57 18140 const char *include_name =
c6da4cef
DE
18141 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18142 if (include_name != NULL)
aaa75496
JB
18143 dwarf2_create_include_psymtab (include_name, pst, objfile);
18144 }
18145 }
cb1df416
DJ
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). */
43f3e411 18151 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18152 int i;
cb1df416
DJ
18153
18154 for (i = 0; i < lh->num_file_names; i++)
18155 {
d521ce57 18156 const char *dir = NULL;
f3f5162e 18157 struct file_entry *fe;
9a619af0 18158
cb1df416 18159 fe = &lh->file_names[i];
afa6c9ab 18160 if (fe->dir_index && lh->include_dirs != NULL)
cb1df416 18161 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 18162 dwarf2_start_subfile (fe->name, dir);
cb1df416 18163
cb1df416 18164 if (current_subfile->symtab == NULL)
43f3e411
DE
18165 {
18166 current_subfile->symtab
18167 = allocate_symtab (cust, current_subfile->name);
18168 }
cb1df416
DJ
18169 fe->symtab = current_subfile->symtab;
18170 }
18171 }
c906108c
SS
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
4d663531 18176 or NULL if not known.
c906108c
SS
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
c5aa993b 18184 DW_AT_name: /srcdir/list0.c
4d663531 18185 DW_AT_comp_dir: /compdir
357e46e7 18186 files.files[0].name: list0.h
c5aa993b 18187 files.files[0].dir: /srcdir
357e46e7 18188 files.files[1].name: list0.c
c5aa993b 18189 files.files[1].dir: /srcdir
c906108c
SS
18190
18191 The line number information for list0.c has to end up in a single
4f1520fb
FR
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. */
c906108c
SS
18196
18197static void
4d663531 18198dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18199{
d521ce57 18200 char *copy = NULL;
4f1520fb 18201
4d663531 18202 /* In order not to lose the line information directory,
4f1520fb
FR
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. */
c906108c 18208
d5166ae1 18209 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18210 {
18211 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18212 filename = copy;
18213 }
c906108c 18214
4d663531 18215 start_subfile (filename);
4f1520fb 18216
d521ce57
TT
18217 if (copy != NULL)
18218 xfree (copy);
c906108c
SS
18219}
18220
f4dc4d17
DE
18221/* Start a symtab for DWARF.
18222 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18223
43f3e411 18224static struct compunit_symtab *
f4dc4d17 18225dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18226 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18227{
43f3e411
DE
18228 struct compunit_symtab *cust
18229 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18230
f4dc4d17
DE
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
4d4ec4e5 18237 cu->processing_has_namespace_info = 0;
43f3e411
DE
18238
18239 return cust;
f4dc4d17
DE
18240}
18241
4c2df51b
DJ
18242static void
18243var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18244 struct dwarf2_cu *cu)
4c2df51b 18245{
e7c27a73
DJ
18246 struct objfile *objfile = cu->objfile;
18247 struct comp_unit_head *cu_header = &cu->header;
18248
4c2df51b
DJ
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 {
f1e6e072 18263 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18264 return;
18265 }
18266
18267 /* Handle one degenerate form of location expression specially, to
18268 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18269 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18270 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18271
18272 if (attr_form_is_block (attr)
3019eac3
DE
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])))))
4c2df51b 18278 {
891d2f0b 18279 unsigned int dummy;
4c2df51b 18280
3019eac3
DE
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);
f1e6e072 18287 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18288 fixup_symbol_section (sym, objfile);
18289 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18290 SYMBOL_SECTION (sym));
4c2df51b
DJ
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
f1e6e072 18301 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 18302
f1e6e072 18303 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 18304 cu->has_loclist = 1;
4c2df51b
DJ
18305}
18306
c906108c
SS
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
34eaf542
TT
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. */
c906108c
SS
18314
18315static struct symbol *
34eaf542
TT
18316new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18317 struct symbol *space)
c906108c 18318{
e7c27a73 18319 struct objfile *objfile = cu->objfile;
3e29f34a 18320 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 18321 struct symbol *sym = NULL;
15d034d0 18322 const char *name;
c906108c
SS
18323 struct attribute *attr = NULL;
18324 struct attribute *attr2 = NULL;
e142c38c 18325 CORE_ADDR baseaddr;
e37fd15a
SW
18326 struct pending **list_to_add = NULL;
18327
edb3359d 18328 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
18329
18330 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18331
94af9270 18332 name = dwarf2_name (die, cu);
c906108c
SS
18333 if (name)
18334 {
94af9270 18335 const char *linkagename;
34eaf542 18336 int suppress_add = 0;
94af9270 18337
34eaf542
TT
18338 if (space)
18339 sym = space;
18340 else
e623cf5d 18341 sym = allocate_symbol (objfile);
c906108c 18342 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
18343
18344 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 18345 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
18346 linkagename = dwarf2_physname (name, die, cu);
18347 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 18348
f55ee35c
JK
18349 /* Fortran does not have mangling standard and the mangling does differ
18350 between gfortran, iFort etc. */
18351 if (cu->language == language_fortran
b250c185 18352 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 18353 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 18354 dwarf2_full_name (name, die, cu),
29df156d 18355 NULL);
f55ee35c 18356
c906108c 18357 /* Default assumptions.
c5aa993b 18358 Use the passed type or decode it from the die. */
176620f1 18359 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 18360 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
18361 if (type != NULL)
18362 SYMBOL_TYPE (sym) = type;
18363 else
e7c27a73 18364 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
18365 attr = dwarf2_attr (die,
18366 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18367 cu);
c906108c
SS
18368 if (attr)
18369 {
18370 SYMBOL_LINE (sym) = DW_UNSND (attr);
18371 }
cb1df416 18372
edb3359d
DJ
18373 attr = dwarf2_attr (die,
18374 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18375 cu);
cb1df416
DJ
18376 if (attr)
18377 {
18378 int file_index = DW_UNSND (attr);
9a619af0 18379
cb1df416
DJ
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"));
1c3d648d 18384 else if (file_index > 0)
cb1df416
DJ
18385 {
18386 struct file_entry *fe;
9a619af0 18387
cb1df416 18388 fe = &cu->line_header->file_names[file_index - 1];
08be3fe3 18389 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
18390 }
18391 }
18392
c906108c
SS
18393 switch (die->tag)
18394 {
18395 case DW_TAG_label:
e142c38c 18396 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 18397 if (attr)
3e29f34a
MR
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 }
0f5238ed
TT
18405 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18406 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 18407 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 18408 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
18409 break;
18410 case DW_TAG_subprogram:
18411 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18412 finish_block. */
f1e6e072 18413 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 18414 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
18415 if ((attr2 && (DW_UNSND (attr2) != 0))
18416 || cu->language == language_ada)
c906108c 18417 {
2cfa0c8d
JB
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. */
e37fd15a 18424 list_to_add = &global_symbols;
c906108c
SS
18425 }
18426 else
18427 {
e37fd15a 18428 list_to_add = cu->list_in_scope;
c906108c
SS
18429 }
18430 break;
edb3359d
DJ
18431 case DW_TAG_inlined_subroutine:
18432 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18433 finish_block. */
f1e6e072 18434 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 18435 SYMBOL_INLINED (sym) = 1;
481860b3 18436 list_to_add = cu->list_in_scope;
edb3359d 18437 break;
34eaf542
TT
18438 case DW_TAG_template_value_param:
18439 suppress_add = 1;
18440 /* Fall through. */
72929c62 18441 case DW_TAG_constant:
c906108c 18442 case DW_TAG_variable:
254e6b9e 18443 case DW_TAG_member:
0963b4bd
MS
18444 /* Compilation with minimal debug info may result in
18445 variables with missing type entries. Change the
18446 misleading `void' type to something sensible. */
c906108c 18447 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 18448 SYMBOL_TYPE (sym)
46bf5051 18449 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 18450
e142c38c 18451 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
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 {
3863f96c
DE
18456 /* dwarf2_add_field uses die_is_declaration,
18457 so we do the same. */
254e6b9e
DE
18458 gdb_assert (die_is_declaration (die, cu));
18459 gdb_assert (attr);
18460 }
c906108c
SS
18461 if (attr)
18462 {
e7c27a73 18463 dwarf2_const_value (attr, sym, cu);
e142c38c 18464 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 18465 if (!suppress_add)
34eaf542
TT
18466 {
18467 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 18468 list_to_add = &global_symbols;
34eaf542 18469 else
e37fd15a 18470 list_to_add = cu->list_in_scope;
34eaf542 18471 }
c906108c
SS
18472 break;
18473 }
e142c38c 18474 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
18475 if (attr)
18476 {
e7c27a73 18477 var_decode_location (attr, sym, cu);
e142c38c 18478 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
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
caac4577
JG
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))
1c809c68 18496 {
f55ee35c
JK
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
28586665 18508 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 18509 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 18510
1c809c68
TT
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);
1c809c68 18515 }
c906108c 18516 else
e37fd15a 18517 list_to_add = cu->list_in_scope;
c906108c
SS
18518 }
18519 else
18520 {
18521 /* We do not know the address of this symbol.
c5aa993b
JM
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. */
e142c38c 18527 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
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)
c906108c 18541 {
0fe7935b
DJ
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
f1e6e072 18547 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 18548 }
442ddf59
JK
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);
e37fd15a
SW
18553 if (!suppress_add)
18554 list_to_add = cu->list_in_scope;
442ddf59 18555 }
c906108c
SS
18556 }
18557 break;
18558 case DW_TAG_formal_parameter:
edb3359d
DJ
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;
e142c38c 18567 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
18568 if (attr)
18569 {
e7c27a73 18570 var_decode_location (attr, sym, cu);
c906108c 18571 }
e142c38c 18572 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
18573 if (attr)
18574 {
e7c27a73 18575 dwarf2_const_value (attr, sym, cu);
c906108c 18576 }
f346a30d 18577
e37fd15a 18578 list_to_add = cu->list_in_scope;
c906108c
SS
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;
34eaf542
TT
18585 case DW_TAG_template_type_param:
18586 suppress_add = 1;
18587 /* Fall through. */
c906108c 18588 case DW_TAG_class_type:
680b30c7 18589 case DW_TAG_interface_type:
c906108c
SS
18590 case DW_TAG_structure_type:
18591 case DW_TAG_union_type:
72019c9c 18592 case DW_TAG_set_type:
c906108c 18593 case DW_TAG_enumeration_type:
f1e6e072 18594 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 18595 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 18596
63d06c5c 18597 {
9c37b5ae 18598 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
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
e37fd15a 18606 if (!suppress_add)
34eaf542 18607 {
34eaf542 18608 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 18609 && cu->language == language_cplus
34eaf542 18610 ? &global_symbols : cu->list_in_scope);
63d06c5c 18611
64382290 18612 /* The semantics of C++ state that "struct foo {
9c37b5ae 18613 ... }" also defines a typedef for "foo". */
64382290 18614 if (cu->language == language_cplus
45280282 18615 || cu->language == language_ada
c44af4eb
TT
18616 || cu->language == language_d
18617 || cu->language == language_rust)
64382290
TT
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 }
63d06c5c
DC
18625 }
18626 }
c906108c
SS
18627 break;
18628 case DW_TAG_typedef:
f1e6e072 18629 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 18630 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 18631 list_to_add = cu->list_in_scope;
63d06c5c 18632 break;
c906108c 18633 case DW_TAG_base_type:
a02abb62 18634 case DW_TAG_subrange_type:
f1e6e072 18635 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 18636 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 18637 list_to_add = cu->list_in_scope;
c906108c
SS
18638 break;
18639 case DW_TAG_enumerator:
e142c38c 18640 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
18641 if (attr)
18642 {
e7c27a73 18643 dwarf2_const_value (attr, sym, cu);
c906108c 18644 }
63d06c5c
DC
18645 {
18646 /* NOTE: carlton/2003-11-10: See comment above in the
18647 DW_TAG_class_type, etc. block. */
18648
e142c38c 18649 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 18650 && cu->language == language_cplus
e142c38c 18651 ? &global_symbols : cu->list_in_scope);
63d06c5c 18652 }
c906108c 18653 break;
74921315 18654 case DW_TAG_imported_declaration:
5c4e30ca 18655 case DW_TAG_namespace:
f1e6e072 18656 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 18657 list_to_add = &global_symbols;
5c4e30ca 18658 break;
530e8392
KB
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;
4357ac6c 18664 case DW_TAG_common_block:
f1e6e072 18665 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
18666 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
18667 add_symbol_to_list (sym, cu->list_in_scope);
18668 break;
c906108c
SS
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
0963b4bd 18673 this point. */
e2e0b3e5 18674 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 18675 dwarf_tag_name (die->tag));
c906108c
SS
18676 break;
18677 }
df8a16a1 18678
e37fd15a
SW
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
df8a16a1
DJ
18689 /* For the benefit of old versions of GCC, check for anonymous
18690 namespaces based on the demangled name. */
4d4ec4e5 18691 if (!cu->processing_has_namespace_info
94af9270 18692 && cu->language == language_cplus)
a10964d1 18693 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
18694 }
18695 return (sym);
18696}
18697
34eaf542
TT
18698/* A wrapper for new_symbol_full that always allocates a new symbol. */
18699
18700static struct symbol *
18701new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18702{
18703 return new_symbol_full (die, type, cu, NULL);
18704}
18705
98bfdba5
PA
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. */
c906108c 18713
98bfdba5 18714static gdb_byte *
ff39bb5e 18715dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 18716 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 18717{
e7c27a73 18718 struct objfile *objfile = cu->objfile;
e17a4113
UW
18719 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
18720 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
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 {
224c3ddb 18732 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
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
18746static void
ff39bb5e 18747dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
18748 const char *name, struct obstack *obstack,
18749 struct dwarf2_cu *cu,
d521ce57 18750 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
18751 struct dwarf2_locexpr_baton **baton)
18752{
18753 struct objfile *objfile = cu->objfile;
18754 struct comp_unit_head *cu_header = &cu->header;
c906108c 18755 struct dwarf_block *blk;
98bfdba5
PA
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;
c906108c
SS
18762
18763 switch (attr->form)
18764 {
18765 case DW_FORM_addr:
3019eac3 18766 case DW_FORM_GNU_addr_index:
ac56253d 18767 {
ac56253d
TT
18768 gdb_byte *data;
18769
98bfdba5
PA
18770 if (TYPE_LENGTH (type) != cu_header->addr_size)
18771 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 18772 cu_header->addr_size,
98bfdba5 18773 TYPE_LENGTH (type));
ac56253d
TT
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. */
8d749320 18777 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
18778 (*baton)->per_cu = cu->per_cu;
18779 gdb_assert ((*baton)->per_cu);
ac56253d 18780
98bfdba5 18781 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 18782 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 18783 (*baton)->data = data;
ac56253d
TT
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;
ac56253d 18789 }
c906108c 18790 break;
4ac36638 18791 case DW_FORM_string:
93b5768b 18792 case DW_FORM_strp:
3019eac3 18793 case DW_FORM_GNU_str_index:
36586728 18794 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
18795 /* DW_STRING is already allocated on the objfile obstack, point
18796 directly to it. */
d521ce57 18797 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 18798 break;
c906108c
SS
18799 case DW_FORM_block1:
18800 case DW_FORM_block2:
18801 case DW_FORM_block4:
18802 case DW_FORM_block:
2dc7f7b3 18803 case DW_FORM_exprloc:
c906108c 18804 blk = DW_BLOCK (attr);
98bfdba5
PA
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;
c906108c 18809 break;
2df3850c
JM
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:
3aef2284 18817 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 18818 break;
c906108c 18819 case DW_FORM_data2:
3aef2284 18820 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 18821 break;
c906108c 18822 case DW_FORM_data4:
3aef2284 18823 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 18824 break;
c906108c 18825 case DW_FORM_data8:
3aef2284 18826 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
18827 break;
18828
c906108c 18829 case DW_FORM_sdata:
98bfdba5 18830 *value = DW_SND (attr);
2df3850c
JM
18831 break;
18832
c906108c 18833 case DW_FORM_udata:
98bfdba5 18834 *value = DW_UNSND (attr);
c906108c 18835 break;
2df3850c 18836
c906108c 18837 default:
4d3c2250 18838 complaint (&symfile_complaints,
e2e0b3e5 18839 _("unsupported const value attribute form: '%s'"),
4d3c2250 18840 dwarf_form_name (attr->form));
98bfdba5 18841 *value = 0;
c906108c
SS
18842 break;
18843 }
18844}
18845
2df3850c 18846
98bfdba5
PA
18847/* Copy constant value from an attribute to a symbol. */
18848
2df3850c 18849static void
ff39bb5e 18850dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 18851 struct dwarf2_cu *cu)
2df3850c 18852{
98bfdba5 18853 struct objfile *objfile = cu->objfile;
12df843f 18854 LONGEST value;
d521ce57 18855 const gdb_byte *bytes;
98bfdba5 18856 struct dwarf2_locexpr_baton *baton;
2df3850c 18857
98bfdba5
PA
18858 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
18859 SYMBOL_PRINT_NAME (sym),
18860 &objfile->objfile_obstack, cu,
18861 &value, &bytes, &baton);
2df3850c 18862
98bfdba5
PA
18863 if (baton != NULL)
18864 {
98bfdba5 18865 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 18866 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
18867 }
18868 else if (bytes != NULL)
18869 {
18870 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 18871 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
18872 }
18873 else
18874 {
18875 SYMBOL_VALUE (sym) = value;
f1e6e072 18876 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 18877 }
2df3850c
JM
18878}
18879
c906108c
SS
18880/* Return the type of the die in question using its DW_AT_type attribute. */
18881
18882static struct type *
e7c27a73 18883die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18884{
c906108c 18885 struct attribute *type_attr;
c906108c 18886
e142c38c 18887 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
18888 if (!type_attr)
18889 {
18890 /* A missing DW_AT_type represents a void type. */
46bf5051 18891 return objfile_type (cu->objfile)->builtin_void;
c906108c 18892 }
348e048f 18893
673bfd45 18894 return lookup_die_type (die, type_attr, cu);
c906108c
SS
18895}
18896
b4ba55a1
JB
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
18901static int
18902need_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
b4ba55a1
JB
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
18919static struct type *
18920die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
18921{
b4ba55a1 18922 struct attribute *type_attr;
b4ba55a1
JB
18923
18924 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
18925 if (!type_attr)
18926 return NULL;
18927
673bfd45 18928 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
18929}
18930
18931/* If DIE has a descriptive_type attribute, then set the TYPE's
18932 descriptive type accordingly. */
18933
18934static void
18935set_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
c906108c
SS
18947/* Return the containing type of the die in question using its
18948 DW_AT_containing_type attribute. */
18949
18950static struct type *
e7c27a73 18951die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18952{
c906108c 18953 struct attribute *type_attr;
c906108c 18954
e142c38c 18955 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
18956 if (!type_attr)
18957 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 18958 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 18959
673bfd45 18960 return lookup_die_type (die, type_attr, cu);
c906108c
SS
18961}
18962
ac9ec31b
DE
18963/* Return an error marker type to use for the ill formed type in DIE/CU. */
18964
18965static struct type *
18966build_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>"),
4262abfb 18972 objfile_name (objfile),
ac9ec31b
DE
18973 cu->header.offset.sect_off,
18974 die->offset.sect_off);
224c3ddb
SM
18975 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
18976 message, strlen (message));
ac9ec31b
DE
18977 xfree (message);
18978
19f392bc 18979 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
18980}
18981
673bfd45 18982/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
18983 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
18984 DW_AT_containing_type.
673bfd45
DE
18985 If there is no type substitute an error marker. */
18986
c906108c 18987static struct type *
ff39bb5e 18988lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 18989 struct dwarf2_cu *cu)
c906108c 18990{
bb5ed363 18991 struct objfile *objfile = cu->objfile;
f792889a
DJ
18992 struct type *this_type;
18993
ac9ec31b
DE
18994 gdb_assert (attr->name == DW_AT_type
18995 || attr->name == DW_AT_GNAT_descriptive_type
18996 || attr->name == DW_AT_containing_type);
18997
673bfd45
DE
18998 /* First see if we have it cached. */
18999
36586728
TT
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 }
7771576e 19008 else if (attr_form_is_ref (attr))
673bfd45 19009 {
b64f50a1 19010 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
19011
19012 this_type = get_die_type_at_offset (offset, cu->per_cu);
19013 }
55f1336d 19014 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19015 {
ac9ec31b 19016 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19017
ac9ec31b 19018 return get_signatured_type (die, signature, cu);
673bfd45
DE
19019 }
19020 else
19021 {
ac9ec31b
DE
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,
4262abfb 19026 objfile_name (objfile));
ac9ec31b 19027 return build_error_marker_type (cu, die);
673bfd45
DE
19028 }
19029
19030 /* If not cached we need to read it in. */
19031
19032 if (this_type == NULL)
19033 {
ac9ec31b 19034 struct die_info *type_die = NULL;
673bfd45
DE
19035 struct dwarf2_cu *type_cu = cu;
19036
7771576e 19037 if (attr_form_is_ref (attr))
ac9ec31b
DE
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
3019eac3
DE
19042 from an inter-CU reference and the type's CU got expanded before
19043 ours. */
ac9ec31b 19044 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19045 }
19046
19047 /* If we still don't have a type use an error marker. */
19048
19049 if (this_type == NULL)
ac9ec31b 19050 return build_error_marker_type (cu, die);
673bfd45 19051
f792889a 19052 return this_type;
c906108c
SS
19053}
19054
673bfd45
DE
19055/* Return the type in DIE, CU.
19056 Returns NULL for invalid types.
19057
02142a6c 19058 This first does a lookup in die_type_hash,
673bfd45
DE
19059 and only reads the die in if necessary.
19060
19061 NOTE: This can be called when reading in partial or full symbols. */
19062
f792889a 19063static struct type *
e7c27a73 19064read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19065{
f792889a
DJ
19066 struct type *this_type;
19067
19068 this_type = get_die_type (die, cu);
19069 if (this_type)
19070 return this_type;
19071
673bfd45
DE
19072 return read_type_die_1 (die, cu);
19073}
19074
19075/* Read the type in DIE, CU.
19076 Returns NULL for invalid types. */
19077
19078static struct type *
19079read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19080{
19081 struct type *this_type = NULL;
19082
c906108c
SS
19083 switch (die->tag)
19084 {
19085 case DW_TAG_class_type:
680b30c7 19086 case DW_TAG_interface_type:
c906108c
SS
19087 case DW_TAG_structure_type:
19088 case DW_TAG_union_type:
f792889a 19089 this_type = read_structure_type (die, cu);
c906108c
SS
19090 break;
19091 case DW_TAG_enumeration_type:
f792889a 19092 this_type = read_enumeration_type (die, cu);
c906108c
SS
19093 break;
19094 case DW_TAG_subprogram:
19095 case DW_TAG_subroutine_type:
edb3359d 19096 case DW_TAG_inlined_subroutine:
f792889a 19097 this_type = read_subroutine_type (die, cu);
c906108c
SS
19098 break;
19099 case DW_TAG_array_type:
f792889a 19100 this_type = read_array_type (die, cu);
c906108c 19101 break;
72019c9c 19102 case DW_TAG_set_type:
f792889a 19103 this_type = read_set_type (die, cu);
72019c9c 19104 break;
c906108c 19105 case DW_TAG_pointer_type:
f792889a 19106 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19107 break;
19108 case DW_TAG_ptr_to_member_type:
f792889a 19109 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19110 break;
19111 case DW_TAG_reference_type:
f792889a 19112 this_type = read_tag_reference_type (die, cu);
c906108c
SS
19113 break;
19114 case DW_TAG_const_type:
f792889a 19115 this_type = read_tag_const_type (die, cu);
c906108c
SS
19116 break;
19117 case DW_TAG_volatile_type:
f792889a 19118 this_type = read_tag_volatile_type (die, cu);
c906108c 19119 break;
06d66ee9
TT
19120 case DW_TAG_restrict_type:
19121 this_type = read_tag_restrict_type (die, cu);
19122 break;
c906108c 19123 case DW_TAG_string_type:
f792889a 19124 this_type = read_tag_string_type (die, cu);
c906108c
SS
19125 break;
19126 case DW_TAG_typedef:
f792889a 19127 this_type = read_typedef (die, cu);
c906108c 19128 break;
a02abb62 19129 case DW_TAG_subrange_type:
f792889a 19130 this_type = read_subrange_type (die, cu);
a02abb62 19131 break;
c906108c 19132 case DW_TAG_base_type:
f792889a 19133 this_type = read_base_type (die, cu);
c906108c 19134 break;
81a17f79 19135 case DW_TAG_unspecified_type:
f792889a 19136 this_type = read_unspecified_type (die, cu);
81a17f79 19137 break;
0114d602
DJ
19138 case DW_TAG_namespace:
19139 this_type = read_namespace_type (die, cu);
19140 break;
f55ee35c
JK
19141 case DW_TAG_module:
19142 this_type = read_module_type (die, cu);
19143 break;
a2c2acaf
MW
19144 case DW_TAG_atomic_type:
19145 this_type = read_tag_atomic_type (die, cu);
19146 break;
c906108c 19147 default:
3e43a32a
MS
19148 complaint (&symfile_complaints,
19149 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19150 dwarf_tag_name (die->tag));
c906108c
SS
19151 break;
19152 }
63d06c5c 19153
f792889a 19154 return this_type;
63d06c5c
DC
19155}
19156
abc72ce4
DE
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
19165static char *
19166guess_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 {
7d45c7c3 19186 const char *linkage_name;
abc72ce4 19187
7d45c7c3
KB
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)
abc72ce4
DE
19193 {
19194 char *actual_name
19195 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19196 linkage_name);
abc72ce4
DE
19197 char *name = NULL;
19198
19199 if (actual_name != NULL)
19200 {
15d034d0 19201 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
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
3e43a32a
MS
19213 && actual_name[actual_name_len
19214 - die_name_len - 1] == ':')
224c3ddb
SM
19215 name = (char *) obstack_copy0 (
19216 &cu->objfile->per_bfd->storage_obstack,
19217 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19218 }
19219 }
19220 xfree (actual_name);
19221 return name;
19222 }
19223 }
19224 }
19225
19226 return NULL;
19227}
19228
96408a79
SA
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
19233static char *
19234anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19235{
19236 struct attribute *attr;
e6a959d6 19237 const char *base;
96408a79
SA
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
7d45c7c3 19243 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
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
224c3ddb
SM
19260 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19261 DW_STRING (attr),
19262 &base[-1] - DW_STRING (attr));
96408a79
SA
19263}
19264
fdde2d81 19265/* Return the name of the namespace/class that DIE is defined within,
0114d602 19266 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19267
0114d602
DJ
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". */
fdde2d81 19279
0d5cff50 19280static const char *
e142c38c 19281determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19282{
0114d602
DJ
19283 struct die_info *parent, *spec_die;
19284 struct dwarf2_cu *spec_cu;
19285 struct type *parent_type;
96408a79 19286 char *retval;
63d06c5c 19287
9c37b5ae 19288 if (cu->language != language_cplus
c44af4eb
TT
19289 && cu->language != language_fortran && cu->language != language_d
19290 && cu->language != language_rust)
0114d602
DJ
19291 return "";
19292
96408a79
SA
19293 retval = anonymous_struct_prefix (die, cu);
19294 if (retval)
19295 return retval;
19296
0114d602
DJ
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
63d06c5c 19322 {
0114d602
DJ
19323 parent = spec_die->parent;
19324 cu = spec_cu;
63d06c5c 19325 }
0114d602
DJ
19326
19327 if (parent == NULL)
19328 return "";
98bfdba5
PA
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 }
63d06c5c 19371 else
0114d602
DJ
19372 switch (parent->tag)
19373 {
63d06c5c 19374 case DW_TAG_namespace:
0114d602 19375 parent_type = read_type_die (parent, cu);
acebe513
UW
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 "";
0114d602
DJ
19382 /* We give a name to even anonymous namespaces. */
19383 return TYPE_TAG_NAME (parent_type);
63d06c5c 19384 case DW_TAG_class_type:
680b30c7 19385 case DW_TAG_interface_type:
63d06c5c 19386 case DW_TAG_structure_type:
0114d602 19387 case DW_TAG_union_type:
f55ee35c 19388 case DW_TAG_module:
0114d602
DJ
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 "";
abc72ce4 19397 case DW_TAG_compile_unit:
95554aad 19398 case DW_TAG_partial_unit:
abc72ce4
DE
19399 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19400 if (cu->language == language_cplus
8b70b953 19401 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
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 "";
3d567982
TT
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. */
63d06c5c 19421 default:
8176b9b8 19422 return determine_prefix (parent, cu);
63d06c5c 19423 }
63d06c5c
DC
19424}
19425
3e43a32a
MS
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. */
987504bb 19431
f55ee35c 19432#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
19433
19434static char *
f55ee35c
JK
19435typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19436 int physname, struct dwarf2_cu *cu)
63d06c5c 19437{
f55ee35c 19438 const char *lead = "";
5c315b68 19439 const char *sep;
63d06c5c 19440
3e43a32a
MS
19441 if (suffix == NULL || suffix[0] == '\0'
19442 || prefix == NULL || prefix[0] == '\0')
987504bb 19443 sep = "";
45280282
IB
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 }
f55ee35c
JK
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 }
987504bb
JJ
19464 else
19465 sep = "::";
63d06c5c 19466
6dd47d34
DE
19467 if (prefix == NULL)
19468 prefix = "";
19469 if (suffix == NULL)
19470 suffix = "";
19471
987504bb
JJ
19472 if (obs == NULL)
19473 {
3e43a32a 19474 char *retval
224c3ddb
SM
19475 = ((char *)
19476 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 19477
f55ee35c
JK
19478 strcpy (retval, lead);
19479 strcat (retval, prefix);
6dd47d34
DE
19480 strcat (retval, sep);
19481 strcat (retval, suffix);
63d06c5c
DC
19482 return retval;
19483 }
987504bb
JJ
19484 else
19485 {
19486 /* We have an obstack. */
f55ee35c 19487 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 19488 }
63d06c5c
DC
19489}
19490
c906108c
SS
19491/* Return sibling of die, NULL if no sibling. */
19492
f9aca02d 19493static struct die_info *
fba45db2 19494sibling_die (struct die_info *die)
c906108c 19495{
639d11d3 19496 return die->sibling;
c906108c
SS
19497}
19498
71c25dea
TT
19499/* Get name of a die, return NULL if not found. */
19500
15d034d0
TT
19501static const char *
19502dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
19503 struct obstack *obstack)
19504{
19505 if (name && cu->language == language_cplus)
19506 {
2f408ecb 19507 std::string canon_name = cp_canonicalize_string (name);
71c25dea 19508
2f408ecb 19509 if (!canon_name.empty ())
71c25dea 19510 {
2f408ecb
PA
19511 if (canon_name != name)
19512 name = (const char *) obstack_copy0 (obstack,
19513 canon_name.c_str (),
19514 canon_name.length ());
71c25dea
TT
19515 }
19516 }
19517
19518 return name;
c906108c
SS
19519}
19520
96553a0c
DE
19521/* Get name of a die, return NULL if not found.
19522 Anonymous namespaces are converted to their magic string. */
9219021c 19523
15d034d0 19524static const char *
e142c38c 19525dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
19526{
19527 struct attribute *attr;
19528
e142c38c 19529 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 19530 if ((!attr || !DW_STRING (attr))
96553a0c 19531 && die->tag != DW_TAG_namespace
53832f31
TT
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)
71c25dea
TT
19536 return NULL;
19537
19538 switch (die->tag)
19539 {
19540 case DW_TAG_compile_unit:
95554aad 19541 case DW_TAG_partial_unit:
71c25dea
TT
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);
907af001 19549
96553a0c
DE
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
907af001
UW
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. */
53832f31 19563 if (attr && DW_STRING (attr)
61012eef
GB
19564 && (startswith (DW_STRING (attr), "._")
19565 || startswith (DW_STRING (attr), "<anonymous")))
907af001 19566 return NULL;
53832f31
TT
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 {
df5c6c50 19572 char *demangled = NULL;
53832f31
TT
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
df5c6c50
JK
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))
8de20a37 19584 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
19585
19586 if (demangled)
19587 {
e6a959d6 19588 const char *base;
96408a79 19589
53832f31 19590 /* FIXME: we already did this for the partial symbol... */
34a68019 19591 DW_STRING (attr)
224c3ddb
SM
19592 = ((const char *)
19593 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19594 demangled, strlen (demangled)));
53832f31
TT
19595 DW_STRING_IS_CANONICAL (attr) = 1;
19596 xfree (demangled);
96408a79
SA
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);
53832f31
TT
19605 }
19606 }
907af001
UW
19607 break;
19608
71c25dea 19609 default:
907af001
UW
19610 break;
19611 }
19612
19613 if (!DW_STRING_IS_CANONICAL (attr))
19614 {
19615 DW_STRING (attr)
19616 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 19617 &cu->objfile->per_bfd->storage_obstack);
907af001 19618 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 19619 }
907af001 19620 return DW_STRING (attr);
9219021c
DC
19621}
19622
19623/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
19624 is none. *EXT_CU is the CU containing DIE on input, and the CU
19625 containing the return value on output. */
9219021c
DC
19626
19627static struct die_info *
f2f0e013 19628dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
19629{
19630 struct attribute *attr;
9219021c 19631
f2f0e013 19632 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
19633 if (attr == NULL)
19634 return NULL;
19635
f2f0e013 19636 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
19637}
19638
c906108c
SS
19639/* Convert a DIE tag into its string name. */
19640
f39c6ffd 19641static const char *
aa1ee363 19642dwarf_tag_name (unsigned tag)
c906108c 19643{
f39c6ffd
TT
19644 const char *name = get_DW_TAG_name (tag);
19645
19646 if (name == NULL)
19647 return "DW_TAG_<unknown>";
19648
19649 return name;
c906108c
SS
19650}
19651
19652/* Convert a DWARF attribute code into its string name. */
19653
f39c6ffd 19654static const char *
aa1ee363 19655dwarf_attr_name (unsigned attr)
c906108c 19656{
f39c6ffd
TT
19657 const char *name;
19658
c764a876 19659#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
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";
c764a876 19665#endif
f39c6ffd
TT
19666
19667 name = get_DW_AT_name (attr);
19668
19669 if (name == NULL)
19670 return "DW_AT_<unknown>";
19671
19672 return name;
c906108c
SS
19673}
19674
19675/* Convert a DWARF value form code into its string name. */
19676
f39c6ffd 19677static const char *
aa1ee363 19678dwarf_form_name (unsigned form)
c906108c 19679{
f39c6ffd
TT
19680 const char *name = get_DW_FORM_name (form);
19681
19682 if (name == NULL)
19683 return "DW_FORM_<unknown>";
19684
19685 return name;
c906108c
SS
19686}
19687
19688static char *
fba45db2 19689dwarf_bool_name (unsigned mybool)
c906108c
SS
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
f39c6ffd 19699static const char *
aa1ee363 19700dwarf_type_encoding_name (unsigned enc)
c906108c 19701{
f39c6ffd 19702 const char *name = get_DW_ATE_name (enc);
c906108c 19703
f39c6ffd
TT
19704 if (name == NULL)
19705 return "DW_ATE_<unknown>";
c906108c 19706
f39c6ffd 19707 return name;
c906108c 19708}
c906108c 19709
f9aca02d 19710static void
d97bc12b 19711dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
19712{
19713 unsigned int i;
19714
d97bc12b
DE
19715 print_spaces (indent, f);
19716 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 19717 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
19718
19719 if (die->parent != NULL)
19720 {
19721 print_spaces (indent, f);
19722 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 19723 die->parent->offset.sect_off);
d97bc12b
DE
19724 }
19725
19726 print_spaces (indent, f);
19727 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 19728 dwarf_bool_name (die->child != NULL));
c906108c 19729
d97bc12b
DE
19730 print_spaces (indent, f);
19731 fprintf_unfiltered (f, " attributes:\n");
19732
c906108c
SS
19733 for (i = 0; i < die->num_attrs; ++i)
19734 {
d97bc12b
DE
19735 print_spaces (indent, f);
19736 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
19737 dwarf_attr_name (die->attrs[i].name),
19738 dwarf_form_name (die->attrs[i].form));
d97bc12b 19739
c906108c
SS
19740 switch (die->attrs[i].form)
19741 {
c906108c 19742 case DW_FORM_addr:
3019eac3 19743 case DW_FORM_GNU_addr_index:
d97bc12b 19744 fprintf_unfiltered (f, "address: ");
5af949e3 19745 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
19746 break;
19747 case DW_FORM_block2:
19748 case DW_FORM_block4:
19749 case DW_FORM_block:
19750 case DW_FORM_block1:
56eb65bd
SP
19751 fprintf_unfiltered (f, "block: size %s",
19752 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 19753 break;
2dc7f7b3 19754 case DW_FORM_exprloc:
56eb65bd
SP
19755 fprintf_unfiltered (f, "expression: size %s",
19756 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 19757 break;
4568ecf9
DE
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;
36586728
TT
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;
10b3939b
DJ
19766 case DW_FORM_ref1:
19767 case DW_FORM_ref2:
19768 case DW_FORM_ref4:
4568ecf9
DE
19769 case DW_FORM_ref8:
19770 case DW_FORM_ref_udata:
d97bc12b 19771 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 19772 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 19773 break;
c906108c
SS
19774 case DW_FORM_data1:
19775 case DW_FORM_data2:
19776 case DW_FORM_data4:
ce5d95e1 19777 case DW_FORM_data8:
c906108c
SS
19778 case DW_FORM_udata:
19779 case DW_FORM_sdata:
43bbcdc2
PH
19780 fprintf_unfiltered (f, "constant: %s",
19781 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 19782 break;
2dc7f7b3
TT
19783 case DW_FORM_sec_offset:
19784 fprintf_unfiltered (f, "section offset: %s",
19785 pulongest (DW_UNSND (&die->attrs[i])));
19786 break;
55f1336d 19787 case DW_FORM_ref_sig8:
ac9ec31b
DE
19788 fprintf_unfiltered (f, "signature: %s",
19789 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 19790 break;
c906108c 19791 case DW_FORM_string:
4bdf3d34 19792 case DW_FORM_strp:
3019eac3 19793 case DW_FORM_GNU_str_index:
36586728 19794 case DW_FORM_GNU_strp_alt:
8285870a 19795 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 19796 DW_STRING (&die->attrs[i])
8285870a
JK
19797 ? DW_STRING (&die->attrs[i]) : "",
19798 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
19799 break;
19800 case DW_FORM_flag:
19801 if (DW_UNSND (&die->attrs[i]))
d97bc12b 19802 fprintf_unfiltered (f, "flag: TRUE");
c906108c 19803 else
d97bc12b 19804 fprintf_unfiltered (f, "flag: FALSE");
c906108c 19805 break;
2dc7f7b3
TT
19806 case DW_FORM_flag_present:
19807 fprintf_unfiltered (f, "flag: TRUE");
19808 break;
a8329558 19809 case DW_FORM_indirect:
0963b4bd
MS
19810 /* The reader will have reduced the indirect form to
19811 the "base form" so this form should not occur. */
3e43a32a
MS
19812 fprintf_unfiltered (f,
19813 "unexpected attribute form: DW_FORM_indirect");
a8329558 19814 break;
c906108c 19815 default:
d97bc12b 19816 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 19817 die->attrs[i].form);
d97bc12b 19818 break;
c906108c 19819 }
d97bc12b 19820 fprintf_unfiltered (f, "\n");
c906108c
SS
19821 }
19822}
19823
f9aca02d 19824static void
d97bc12b 19825dump_die_for_error (struct die_info *die)
c906108c 19826{
d97bc12b
DE
19827 dump_die_shallow (gdb_stderr, 0, die);
19828}
19829
19830static void
19831dump_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)
c906108c 19843 {
d97bc12b
DE
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 {
3e43a32a
MS
19853 fprintf_unfiltered (f,
19854 " [not printed, max nesting level reached]\n");
d97bc12b
DE
19855 }
19856 }
19857
19858 if (die->sibling != NULL && level > 0)
19859 {
19860 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
19861 }
19862}
19863
d97bc12b
DE
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
19867void
19868dump_die (struct die_info *die, int max_level)
19869{
19870 dump_die_1 (gdb_stdlog, 0, max_level, die);
19871}
19872
f9aca02d 19873static void
51545339 19874store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19875{
51545339 19876 void **slot;
c906108c 19877
b64f50a1
JK
19878 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
19879 INSERT);
51545339
DJ
19880
19881 *slot = die;
c906108c
SS
19882}
19883
b64f50a1
JK
19884/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
19885 required kind. */
19886
19887static sect_offset
ff39bb5e 19888dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 19889{
4568ecf9 19890 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 19891
7771576e 19892 if (attr_form_is_ref (attr))
b64f50a1 19893 return retval;
93311388 19894
b64f50a1 19895 retval.sect_off = 0;
93311388
DE
19896 complaint (&symfile_complaints,
19897 _("unsupported die ref attribute form: '%s'"),
19898 dwarf_form_name (attr->form));
b64f50a1 19899 return retval;
c906108c
SS
19900}
19901
43bbcdc2
PH
19902/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
19903 * the value held by the attribute is not constant. */
a02abb62 19904
43bbcdc2 19905static LONGEST
ff39bb5e 19906dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
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 {
3e43a32a
MS
19918 complaint (&symfile_complaints,
19919 _("Attribute value is not a constant (%s)"),
a02abb62
JB
19920 dwarf_form_name (attr->form));
19921 return default_value;
19922 }
19923}
19924
348e048f
DE
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
19929static struct die_info *
ff39bb5e 19930follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
19931 struct dwarf2_cu **ref_cu)
19932{
19933 struct die_info *die;
19934
7771576e 19935 if (attr_form_is_ref (attr))
348e048f 19936 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 19937 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
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]"),
4262abfb 19943 objfile_name ((*ref_cu)->objfile));
348e048f
DE
19944 }
19945
19946 return die;
03dd20cc
DJ
19947}
19948
5c631832 19949/* Follow reference OFFSET.
673bfd45
DE
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. */
f504f079 19953
f9aca02d 19954static struct die_info *
36586728
TT
19955follow_die_offset (sect_offset offset, int offset_in_dwz,
19956 struct dwarf2_cu **ref_cu)
c906108c 19957{
10b3939b 19958 struct die_info temp_die;
f2f0e013 19959 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 19960
348e048f
DE
19961 gdb_assert (cu->per_cu != NULL);
19962
98bfdba5
PA
19963 target_cu = cu;
19964
3019eac3 19965 if (cu->per_cu->is_debug_types)
348e048f
DE
19966 {
19967 /* .debug_types CUs cannot reference anything outside their CU.
19968 If they need to, they have to reference a signatured type via
55f1336d 19969 DW_FORM_ref_sig8. */
348e048f 19970 if (! offset_in_cu_p (&cu->header, offset))
5c631832 19971 return NULL;
348e048f 19972 }
36586728
TT
19973 else if (offset_in_dwz != cu->per_cu->is_dwz
19974 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
19975 {
19976 struct dwarf2_per_cu_data *per_cu;
9a619af0 19977
36586728
TT
19978 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
19979 cu->objfile);
03dd20cc
DJ
19980
19981 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
19982 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
19983 load_full_comp_unit (per_cu, cu->language);
03dd20cc 19984
10b3939b
DJ
19985 target_cu = per_cu->cu;
19986 }
98bfdba5
PA
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);
95554aad 19991 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 19992 }
c906108c 19993
f2f0e013 19994 *ref_cu = target_cu;
51545339 19995 temp_die.offset = offset;
9a3c8263
SM
19996 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
19997 &temp_die, offset.sect_off);
5c631832 19998}
10b3939b 19999
5c631832
JK
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
20004static struct die_info *
ff39bb5e 20005follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20006 struct dwarf2_cu **ref_cu)
20007{
b64f50a1 20008 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20009 struct dwarf2_cu *cu = *ref_cu;
20010 struct die_info *die;
20011
36586728
TT
20012 die = follow_die_offset (offset,
20013 (attr->form == DW_FORM_GNU_ref_alt
20014 || cu->per_cu->is_dwz),
20015 ref_cu);
5c631832
JK
20016 if (!die)
20017 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20018 "at 0x%x [in module %s]"),
4262abfb
JK
20019 offset.sect_off, src_die->offset.sect_off,
20020 objfile_name (cu->objfile));
348e048f 20021
5c631832
JK
20022 return die;
20023}
20024
d83e736b
JK
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. */
5c631832
JK
20028
20029struct dwarf2_locexpr_baton
8b9737bf
TT
20030dwarf2_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)
5c631832 20034{
918dd910 20035 struct dwarf2_cu *cu;
5c631832
JK
20036 struct die_info *die;
20037 struct attribute *attr;
20038 struct dwarf2_locexpr_baton retval;
20039
8cf6f0b1
TT
20040 dw2_setup (per_cu->objfile);
20041
918dd910
JK
20042 if (per_cu->cu == NULL)
20043 load_cu (per_cu);
20044 cu = per_cu->cu;
cc12ce38
DE
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 }
918dd910 20052
36586728 20053 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
20054 if (!die)
20055 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20056 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20057
20058 attr = dwarf2_attr (die, DW_AT_location, cu);
20059 if (!attr)
20060 {
e103e986
JK
20061 /* DWARF: "If there is no such attribute, then there is no effect.".
20062 DATA is ignored if SIZE is 0. */
5c631832 20063
e103e986 20064 retval.data = NULL;
5c631832
JK
20065 retval.size = 0;
20066 }
8cf6f0b1
TT
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 }
5c631832
JK
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"),
4262abfb 20084 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20085
20086 retval.data = DW_BLOCK (attr)->data;
20087 retval.size = DW_BLOCK (attr)->size;
20088 }
20089 retval.per_cu = cu->per_cu;
918dd910 20090
918dd910
JK
20091 age_cached_comp_units ();
20092
5c631832 20093 return retval;
348e048f
DE
20094}
20095
8b9737bf
TT
20096/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20097 offset. */
20098
20099struct dwarf2_locexpr_baton
20100dwarf2_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
b6807d98
TT
20110/* Write a constant of a given type as target-ordered bytes into
20111 OBSTACK. */
20112
20113static const gdb_byte *
20114write_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);
224c3ddb 20123 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
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
20134const gdb_byte *
20135dwarf2_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;
cc12ce38
DE
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 }
b6807d98
TT
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"),
4262abfb 20164 offset.sect_off, objfile_name (per_cu->objfile));
b6807d98
TT
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;
224c3ddb 20182 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
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
8a9b8146
TT
20261/* Return the type of the DIE at DIE_OFFSET in the CU named by
20262 PER_CU. */
20263
20264struct type *
b64f50a1 20265dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
20266 struct dwarf2_per_cu_data *per_cu)
20267{
b64f50a1
JK
20268 sect_offset die_offset_sect;
20269
8a9b8146 20270 dw2_setup (per_cu->objfile);
b64f50a1
JK
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);
8a9b8146
TT
20274}
20275
ac9ec31b 20276/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 20277 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
20278 On exit *REF_CU is the CU of the result.
20279 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
20280
20281static struct die_info *
ac9ec31b
DE
20282follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20283 struct dwarf2_cu **ref_cu)
348e048f 20284{
348e048f 20285 struct die_info temp_die;
348e048f
DE
20286 struct dwarf2_cu *sig_cu;
20287 struct die_info *die;
20288
ac9ec31b
DE
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. */
348e048f
DE
20292
20293 /* If necessary, add it to the queue and load its DIEs. */
20294
95554aad 20295 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 20296 read_signatured_type (sig_type);
348e048f 20297
348e048f 20298 sig_cu = sig_type->per_cu.cu;
69d751e3 20299 gdb_assert (sig_cu != NULL);
3019eac3
DE
20300 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20301 temp_die.offset = sig_type->type_offset_in_section;
9a3c8263
SM
20302 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20303 temp_die.offset.sect_off);
348e048f
DE
20304 if (die)
20305 {
796a7ff8
DE
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
348e048f
DE
20316 *ref_cu = sig_cu;
20317 return die;
20318 }
20319
ac9ec31b
DE
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
20329static struct die_info *
ff39bb5e 20330follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
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
a2ce51a0 20339 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
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,
4262abfb 20347 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
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,
4262abfb 20357 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
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
20366static struct type *
20367get_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
a2ce51a0 20375 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
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,
4262abfb 20384 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
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,
4262abfb 20406 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
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,
4262abfb 20416 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
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
20427static struct type *
ff39bb5e 20428get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 20429 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
20430{
20431 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 20432 if (attr_form_is_ref (attr))
ac9ec31b
DE
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,
4262abfb 20449 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20450 return build_error_marker_type (cu, die);
20451 }
348e048f
DE
20452}
20453
e5fe5e75 20454/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
20455
20456static void
e5fe5e75 20457load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 20458{
52dc124a 20459 struct signatured_type *sig_type;
348e048f 20460
f4dc4d17
DE
20461 /* Caller is responsible for ensuring type_unit_groups don't get here. */
20462 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
20463
6721b2ec
DE
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;
348e048f 20468
6721b2ec 20469 gdb_assert (per_cu->cu == NULL);
348e048f 20470
52dc124a 20471 read_signatured_type (sig_type);
348e048f 20472
6721b2ec 20473 gdb_assert (per_cu->cu != NULL);
348e048f
DE
20474}
20475
dee91e82
DE
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. */
348e048f
DE
20479
20480static void
dee91e82 20481read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 20482 const gdb_byte *info_ptr,
dee91e82
DE
20483 struct die_info *comp_unit_die,
20484 int has_children,
20485 void *data)
348e048f 20486{
dee91e82 20487 struct dwarf2_cu *cu = reader->cu;
348e048f 20488
dee91e82
DE
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);
348e048f 20498
dee91e82
DE
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. */
348e048f
DE
20504
20505 /* We try not to read any attributes in this function, because not
9cdd5dbd 20506 all CUs needed for references have been loaded yet, and symbol
348e048f 20507 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
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. */
95554aad 20511 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 20512}
348e048f 20513
3019eac3
DE
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. */
dee91e82
DE
20517
20518static void
20519read_signatured_type (struct signatured_type *sig_type)
20520{
20521 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 20522
3019eac3 20523 gdb_assert (per_cu->is_debug_types);
dee91e82 20524 gdb_assert (per_cu->cu == NULL);
348e048f 20525
f4dc4d17
DE
20526 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
20527 read_signatured_type_reader, NULL);
7ee85ab1 20528 sig_type->per_cu.tu_read = 1;
c906108c
SS
20529}
20530
c906108c
SS
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
4cecd739
DJ
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).
c906108c
SS
20543
20544 A location description containing no operations indicates that the
4cecd739 20545 object is optimized out. The return value is 0 for that case.
6b992462
DJ
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
21ae7a4d
JK
20548 complaint.
20549
20550 Note that stack[0] is unused except as a default error return. */
c906108c
SS
20551
20552static CORE_ADDR
e7c27a73 20553decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 20554{
e7c27a73 20555 struct objfile *objfile = cu->objfile;
56eb65bd
SP
20556 size_t i;
20557 size_t size = blk->size;
d521ce57 20558 const gdb_byte *data = blk->data;
21ae7a4d
JK
20559 CORE_ADDR stack[64];
20560 int stacki;
20561 unsigned int bytes_read, unsnd;
20562 gdb_byte op;
c906108c 20563
21ae7a4d
JK
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;
f1bea926 20608
21ae7a4d
JK
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;
c906108c 20645
21ae7a4d
JK
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;
c906108c 20653
21ae7a4d
JK
20654 case DW_OP_addr:
20655 stack[++stacki] = read_address (objfile->obfd, &data[i],
20656 cu, &bytes_read);
20657 i += bytes_read;
20658 break;
d53d4ac5 20659
21ae7a4d
JK
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;
d53d4ac5 20679
21ae7a4d
JK
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
585861ea
JK
20690 case DW_OP_const8u:
20691 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
20692 i += 8;
20693 break;
20694
21ae7a4d
JK
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:
4aa4e28b 20737 case DW_OP_form_tls_address:
21ae7a4d
JK
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
585861ea
JK
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. */
21ae7a4d
JK
20746 if (i < size)
20747 dwarf2_complex_location_expr_complaint ();
585861ea 20748 stack[stacki]++;
21ae7a4d
JK
20749 break;
20750
20751 case DW_OP_GNU_uninit:
20752 break;
20753
3019eac3 20754 case DW_OP_GNU_addr_index:
49f6c839 20755 case DW_OP_GNU_const_index:
3019eac3
DE
20756 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
20757 &bytes_read);
20758 i += bytes_read;
20759 break;
20760
21ae7a4d
JK
20761 default:
20762 {
f39c6ffd 20763 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
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]);
d53d4ac5 20774 }
3c6e0cb3 20775
21ae7a4d
JK
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]);
c906108c
SS
20793}
20794
20795/* memory allocation interface */
20796
c906108c 20797static struct dwarf_block *
7b5a2f43 20798dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 20799{
8d749320 20800 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
20801}
20802
c906108c 20803static struct die_info *
b60c80d6 20804dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
20805{
20806 struct die_info *die;
b60c80d6
DJ
20807 size_t size = sizeof (struct die_info);
20808
20809 if (num_attrs > 1)
20810 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 20811
b60c80d6 20812 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
20813 memset (die, 0, sizeof (struct die_info));
20814 return (die);
20815}
2e276125
JB
20816
20817\f
20818/* Macro support. */
20819
233d95b5
JK
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
2e276125 20822 responsible for freeing it. */
233d95b5 20823
2e276125 20824static char *
233d95b5 20825file_file_name (int file, struct line_header *lh)
2e276125 20826{
6a83a1e6
EZ
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];
6e70227d 20832
afa6c9ab
SL
20833 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
20834 || lh->include_dirs == NULL)
6a83a1e6 20835 return xstrdup (fe->name);
233d95b5 20836 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
b36cec19 20837 fe->name, (char *) NULL);
6a83a1e6 20838 }
2e276125
JB
20839 else
20840 {
6a83a1e6
EZ
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];
9a619af0 20845
8c042590
PM
20846 xsnprintf (fake_name, sizeof (fake_name),
20847 "<bad macro file number %d>", file);
2e276125 20848
6e70227d 20849 complaint (&symfile_complaints,
6a83a1e6
EZ
20850 _("bad file number in macro information (%d)"),
20851 file);
2e276125 20852
6a83a1e6 20853 return xstrdup (fake_name);
2e276125
JB
20854 }
20855}
20856
233d95b5
JK
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. */
20861static char *
20862file_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;
b36cec19
PA
20872 return reconcat (relative, comp_dir, SLASH_STRING,
20873 relative, (char *) NULL);
233d95b5
JK
20874 }
20875 else
20876 return file_file_name (file, lh);
20877}
20878
2e276125
JB
20879
20880static struct macro_source_file *
20881macro_start_file (int file, int line,
20882 struct macro_source_file *current_file,
43f3e411 20883 struct line_header *lh)
2e276125 20884{
233d95b5
JK
20885 /* File name relative to the compilation directory of this source file. */
20886 char *file_name = file_file_name (file, lh);
2e276125 20887
2e276125 20888 if (! current_file)
abc9d0dc 20889 {
fc474241
DE
20890 /* Note: We don't create a macro table for this compilation unit
20891 at all until we actually get a filename. */
43f3e411 20892 struct macro_table *macro_table = get_macro_table ();
fc474241 20893
abc9d0dc
TT
20894 /* If we have no current file, then this must be the start_file
20895 directive for the compilation unit's main source file. */
fc474241
DE
20896 current_file = macro_set_main (macro_table, file_name);
20897 macro_define_special (macro_table);
abc9d0dc 20898 }
2e276125 20899 else
233d95b5 20900 current_file = macro_include (current_file, line, file_name);
2e276125 20901
233d95b5 20902 xfree (file_name);
6e70227d 20903
2e276125
JB
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. */
20910static char *
20911copy_string (const char *buf, int len)
20912{
224c3ddb 20913 char *s = (char *) xmalloc (len + 1);
9a619af0 20914
2e276125
JB
20915 memcpy (s, buf, len);
20916 s[len] = '\0';
2e276125
JB
20917 return s;
20918}
20919
20920
20921static const char *
20922consume_improper_spaces (const char *p, const char *body)
20923{
20924 if (*p == ' ')
20925 {
4d3c2250 20926 complaint (&symfile_complaints,
3e43a32a
MS
20927 _("macro definition contains spaces "
20928 "in formal argument list:\n`%s'"),
4d3c2250 20929 body);
2e276125
JB
20930
20931 while (*p == ' ')
20932 p++;
20933 }
20934
20935 return p;
20936}
20937
20938
20939static void
20940parse_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
6e70227d 20961 the space when the macro's definition is the empty string.
2e276125
JB
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 {
4d3c2250 20987 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
20988 replacement = body + name_len;
20989 }
6e70227d 20990
2e276125
JB
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;
8d749320 21001 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
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)
4d3c2250 21017 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21018 else
21019 {
21020 /* Make sure argv has room for the new argument. */
21021 if (argc >= argv_size)
21022 {
21023 argv_size *= 2;
224c3ddb 21024 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
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,
6e70227d 21048 argc, (const char **) argv,
2e276125
JB
21049 p + 1);
21050 else if (*p == '\0')
21051 {
21052 /* Complain, but do define it. */
4d3c2250 21053 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21054 macro_define_function (file, line, name,
6e70227d 21055 argc, (const char **) argv,
2e276125
JB
21056 p);
21057 }
21058 else
21059 /* Just complain. */
4d3c2250 21060 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21061 }
21062 else
21063 /* Just complain. */
4d3c2250 21064 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
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
4d3c2250 21076 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21077}
21078
cf2c3c16
TT
21079/* Skip some bytes from BYTES according to the form given in FORM.
21080 Returns the new pointer. */
2e276125 21081
d521ce57
TT
21082static const gdb_byte *
21083skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21084 enum dwarf_form form,
21085 unsigned int offset_size,
21086 struct dwarf2_section_info *section)
2e276125 21087{
cf2c3c16 21088 unsigned int bytes_read;
2e276125 21089
cf2c3c16 21090 switch (form)
2e276125 21091 {
cf2c3c16
TT
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:
36586728 21116 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
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:
3019eac3
DE
21137 case DW_FORM_GNU_addr_index:
21138 case DW_FORM_GNU_str_index:
d521ce57 21139 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21140 if (bytes == NULL)
21141 {
21142 dwarf2_section_buffer_overflow_complaint (section);
21143 return NULL;
21144 }
cf2c3c16
TT
21145 break;
21146
21147 default:
21148 {
21149 complain:
21150 complaint (&symfile_complaints,
21151 _("invalid form 0x%x in `%s'"),
a32a8923 21152 form, get_section_name (section));
cf2c3c16
TT
21153 return NULL;
21154 }
2e276125
JB
21155 }
21156
cf2c3c16
TT
21157 return bytes;
21158}
757a13d0 21159
cf2c3c16
TT
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. */
757a13d0 21163
d521ce57 21164static const gdb_byte *
cf2c3c16 21165skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21166 const gdb_byte **opcode_definitions,
21167 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
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;
d521ce57 21174 const gdb_byte *defn;
2e276125 21175
cf2c3c16 21176 if (opcode_definitions[opcode] == NULL)
2e276125 21177 {
cf2c3c16
TT
21178 complaint (&symfile_complaints,
21179 _("unrecognized DW_MACFINO opcode 0x%x"),
21180 opcode);
21181 return NULL;
21182 }
2e276125 21183
cf2c3c16
TT
21184 defn = opcode_definitions[opcode];
21185 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21186 defn += bytes_read;
2e276125 21187
cf2c3c16
TT
21188 for (i = 0; i < arg; ++i)
21189 {
aead7601
SM
21190 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21191 (enum dwarf_form) defn[i], offset_size,
f664829e 21192 section);
cf2c3c16
TT
21193 if (mac_ptr == NULL)
21194 {
21195 /* skip_form_bytes already issued the complaint. */
21196 return NULL;
21197 }
21198 }
757a13d0 21199
cf2c3c16
TT
21200 return mac_ptr;
21201}
757a13d0 21202
cf2c3c16
TT
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. */
757a13d0 21207
d521ce57
TT
21208static const gdb_byte *
21209dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21210 bfd *abfd,
d521ce57 21211 const gdb_byte *mac_ptr,
cf2c3c16
TT
21212 unsigned int *offset_size,
21213 int section_is_gnu)
21214{
21215 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21216
cf2c3c16
TT
21217 if (section_is_gnu)
21218 {
21219 unsigned int version, flags;
757a13d0 21220
cf2c3c16
TT
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;
757a13d0 21230
cf2c3c16
TT
21231 flags = read_1_byte (abfd, mac_ptr);
21232 ++mac_ptr;
21233 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21234
cf2c3c16
TT
21235 if ((flags & 2) != 0)
21236 /* We don't need the line table offset. */
21237 mac_ptr += *offset_size;
757a13d0 21238
cf2c3c16
TT
21239 /* Vendor opcode descriptions. */
21240 if ((flags & 4) != 0)
21241 {
21242 unsigned int i, count;
757a13d0 21243
cf2c3c16
TT
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 }
757a13d0 21258 }
cf2c3c16 21259 }
757a13d0 21260
cf2c3c16
TT
21261 return mac_ptr;
21262}
757a13d0 21263
cf2c3c16 21264/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 21265 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
21266
21267static void
d521ce57
TT
21268dwarf_decode_macro_bytes (bfd *abfd,
21269 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21270 struct macro_source_file *current_file,
43f3e411 21271 struct line_header *lh,
cf2c3c16 21272 struct dwarf2_section_info *section,
36586728 21273 int section_is_gnu, int section_is_dwz,
cf2c3c16 21274 unsigned int offset_size,
8fc3fc34 21275 htab_t include_hash)
cf2c3c16 21276{
4d663531 21277 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
21278 enum dwarf_macro_record_type macinfo_type;
21279 int at_commandline;
d521ce57 21280 const gdb_byte *opcode_definitions[256];
757a13d0 21281
cf2c3c16
TT
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 }
757a13d0
JK
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 {
f664829e 21304 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
21305 break;
21306 }
21307
aead7601 21308 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
21309 mac_ptr++;
21310
cf2c3c16
TT
21311 /* Note that we rely on the fact that the corresponding GNU and
21312 DWARF constants are the same. */
757a13d0
JK
21313 switch (macinfo_type)
21314 {
21315 /* A zero macinfo type indicates the end of the macro
21316 information. */
21317 case 0:
21318 break;
2e276125 21319
cf2c3c16
TT
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:
36586728
TT
21324 case DW_MACRO_GNU_define_indirect_alt:
21325 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 21326 {
891d2f0b 21327 unsigned int bytes_read;
2e276125 21328 int line;
d521ce57 21329 const char *body;
cf2c3c16 21330 int is_define;
2e276125 21331
cf2c3c16
TT
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;
2e276125 21347
36586728 21348 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
21349 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
21350 || section_is_dwz)
36586728
TT
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);
cf2c3c16
TT
21358 }
21359
21360 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
21361 || macinfo_type == DW_MACRO_GNU_define_indirect
21362 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 21363 if (! current_file)
757a13d0
JK
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"),
cf2c3c16
TT
21369 is_define ? _("definition") : _("undefinition"),
21370 line, body);
757a13d0
JK
21371 break;
21372 }
3e43a32a
MS
21373 if ((line == 0 && !at_commandline)
21374 || (line != 0 && at_commandline))
4d3c2250 21375 complaint (&symfile_complaints,
757a13d0
JK
21376 _("debug info gives %s macro %s with %s line %d: %s"),
21377 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 21378 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
21379 line == 0 ? _("zero") : _("non-zero"), line, body);
21380
cf2c3c16 21381 if (is_define)
757a13d0 21382 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
21383 else
21384 {
21385 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
21386 || macinfo_type == DW_MACRO_GNU_undef_indirect
21387 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
21388 macro_undef (current_file, line, body);
21389 }
2e276125
JB
21390 }
21391 break;
21392
cf2c3c16 21393 case DW_MACRO_GNU_start_file:
2e276125 21394 {
891d2f0b 21395 unsigned int bytes_read;
2e276125
JB
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
3e43a32a
MS
21403 if ((line == 0 && !at_commandline)
21404 || (line != 0 && at_commandline))
757a13d0
JK
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 {
cf2c3c16
TT
21413 /* This DW_MACRO_GNU_start_file was executed in the
21414 pass one. */
757a13d0
JK
21415 at_commandline = 0;
21416 }
21417 else
43f3e411 21418 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
21419 }
21420 break;
21421
cf2c3c16 21422 case DW_MACRO_GNU_end_file:
2e276125 21423 if (! current_file)
4d3c2250 21424 complaint (&symfile_complaints,
3e43a32a
MS
21425 _("macro debug info has an unmatched "
21426 "`close_file' directive"));
2e276125
JB
21427 else
21428 {
21429 current_file = current_file->included_by;
21430 if (! current_file)
21431 {
cf2c3c16 21432 enum dwarf_macro_record_type next_type;
2e276125
JB
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 {
f664829e 21442 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
21443 return;
21444 }
21445
21446 /* We don't increment mac_ptr here, so this is just
21447 a look-ahead. */
aead7601
SM
21448 next_type
21449 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21450 mac_ptr);
2e276125 21451 if (next_type != 0)
4d3c2250 21452 complaint (&symfile_complaints,
3e43a32a
MS
21453 _("no terminating 0-type entry for "
21454 "macros in `.debug_macinfo' section"));
2e276125
JB
21455
21456 return;
21457 }
21458 }
21459 break;
21460
cf2c3c16 21461 case DW_MACRO_GNU_transparent_include:
36586728 21462 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
21463 {
21464 LONGEST offset;
8fc3fc34 21465 void **slot;
a036ba48
TT
21466 bfd *include_bfd = abfd;
21467 struct dwarf2_section_info *include_section = section;
d521ce57 21468 const gdb_byte *include_mac_end = mac_end;
a036ba48 21469 int is_dwz = section_is_dwz;
d521ce57 21470 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
21471
21472 offset = read_offset_1 (abfd, mac_ptr, offset_size);
21473 mac_ptr += offset_size;
21474
a036ba48
TT
21475 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
21476 {
21477 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21478
4d663531 21479 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 21480
a036ba48 21481 include_section = &dwz->macro;
a32a8923 21482 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
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
8fc3fc34
TT
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 {
d521ce57 21500 *slot = (void *) new_mac_ptr;
36586728 21501
a036ba48 21502 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 21503 include_mac_end, current_file, lh,
36586728 21504 section, section_is_gnu, is_dwz,
4d663531 21505 offset_size, include_hash);
8fc3fc34 21506
d521ce57 21507 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 21508 }
cf2c3c16
TT
21509 }
21510 break;
21511
2e276125 21512 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
21513 if (!section_is_gnu)
21514 {
21515 unsigned int bytes_read;
2e276125 21516
ac298888
TT
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);
cf2c3c16
TT
21520 mac_ptr += bytes_read;
21521 read_direct_string (abfd, mac_ptr, &bytes_read);
21522 mac_ptr += bytes_read;
2e276125 21523
cf2c3c16
TT
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,
f664829e 21531 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
21532 section);
21533 if (mac_ptr == NULL)
21534 return;
21535 break;
2e276125 21536 }
757a13d0 21537 } while (macinfo_type != 0);
2e276125 21538}
8e19ed76 21539
cf2c3c16 21540static void
09262596 21541dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 21542 int section_is_gnu)
cf2c3c16 21543{
bb5ed363 21544 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
21545 struct line_header *lh = cu->line_header;
21546 bfd *abfd;
d521ce57 21547 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
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;
d521ce57 21551 const gdb_byte *opcode_definitions[256];
8fc3fc34 21552 struct cleanup *cleanup;
8fc3fc34 21553 void **slot;
09262596
DE
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 }
cf2c3c16 21583
bb5ed363 21584 dwarf2_read_section (objfile, section);
cf2c3c16
TT
21585 if (section->buffer == NULL)
21586 {
fceca515 21587 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
21588 return;
21589 }
a32a8923 21590 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
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
aead7601 21625 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
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
43f3e411 21660 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
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:
f7a35f02
TT
21670 case DW_MACRO_GNU_define_indirect_alt:
21671 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
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:
f7a35f02 21682 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
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,
f664829e 21704 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
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
fc4007c9
TT
21718 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
21719 htab_eq_pointer,
21720 NULL, xcalloc, xfree));
8fc3fc34 21721 mac_ptr = section->buffer + offset;
fc4007c9 21722 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 21723 *slot = (void *) mac_ptr;
8fc3fc34 21724 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 21725 current_file, lh, section,
fc4007c9
TT
21726 section_is_gnu, 0, offset_size,
21727 include_hash.get ());
cf2c3c16
TT
21728}
21729
8e19ed76 21730/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 21731 if so return true else false. */
380bca97 21732
8e19ed76 21733static int
6e5a29e1 21734attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
21735{
21736 return (attr == NULL ? 0 :
21737 attr->form == DW_FORM_block1
21738 || attr->form == DW_FORM_block2
21739 || attr->form == DW_FORM_block4
2dc7f7b3
TT
21740 || attr->form == DW_FORM_block
21741 || attr->form == DW_FORM_exprloc);
8e19ed76 21742}
4c2df51b 21743
c6a0999f
JB
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. */
380bca97 21752
3690dd37 21753static int
6e5a29e1 21754attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
21755{
21756 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
21757 || attr->form == DW_FORM_data8
21758 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
21759}
21760
3690dd37
JB
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. */
380bca97 21773
3690dd37 21774static int
6e5a29e1 21775attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
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
7771576e
SA
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
21795static int
6e5a29e1 21796attr_form_is_ref (const struct attribute *attr)
7771576e
SA
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
3019eac3
DE
21813/* Return the .debug_loc section to use for CU.
21814 For DWO files use .debug_loc.dwo. */
21815
21816static struct dwarf2_section_info *
21817cu_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
8cf6f0b1
TT
21824/* A helper function that fills in a dwarf2_loclist_baton. */
21825
21826static void
21827fill_in_loclist_baton (struct dwarf2_cu *cu,
21828 struct dwarf2_loclist_baton *baton,
ff39bb5e 21829 const struct attribute *attr)
8cf6f0b1 21830{
3019eac3
DE
21831 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21832
21833 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
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. */
3019eac3
DE
21839 baton->size = section->size - DW_UNSND (attr);
21840 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 21841 baton->base_address = cu->base_address;
f664829e 21842 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
21843}
21844
4c2df51b 21845static void
ff39bb5e 21846dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 21847 struct dwarf2_cu *cu, int is_block)
4c2df51b 21848{
bb5ed363 21849 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 21850 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 21851
3690dd37 21852 if (attr_form_is_section_offset (attr)
3019eac3 21853 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
21854 the section. If so, fall through to the complaint in the
21855 other branch. */
3019eac3 21856 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 21857 {
0d53c4c4 21858 struct dwarf2_loclist_baton *baton;
4c2df51b 21859
8d749320 21860 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 21861
8cf6f0b1 21862 fill_in_loclist_baton (cu, baton, attr);
be391dca 21863
d00adf39 21864 if (cu->base_known == 0)
0d53c4c4 21865 complaint (&symfile_complaints,
3e43a32a
MS
21866 _("Location list used without "
21867 "specifying the CU base address."));
4c2df51b 21868
f1e6e072
TT
21869 SYMBOL_ACLASS_INDEX (sym) = (is_block
21870 ? dwarf2_loclist_block_index
21871 : dwarf2_loclist_index);
0d53c4c4
DJ
21872 SYMBOL_LOCATION_BATON (sym) = baton;
21873 }
21874 else
21875 {
21876 struct dwarf2_locexpr_baton *baton;
21877
8d749320 21878 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
21879 baton->per_cu = cu->per_cu;
21880 gdb_assert (baton->per_cu);
0d53c4c4
DJ
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
6502dd73
DJ
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. */
0d53c4c4
DJ
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;
0d53c4c4 21897 }
6e70227d 21898
f1e6e072
TT
21899 SYMBOL_ACLASS_INDEX (sym) = (is_block
21900 ? dwarf2_locexpr_block_index
21901 : dwarf2_locexpr_index);
0d53c4c4
DJ
21902 SYMBOL_LOCATION_BATON (sym) = baton;
21903 }
4c2df51b 21904}
6502dd73 21905
9aa1f1e3
TT
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. */
ae0d2f24
UW
21909
21910struct objfile *
21911dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
21912{
9291a0cd 21913 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
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
96408a79
SA
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
21927static const struct comp_unit_head *
21928per_cu_header_read_in (struct comp_unit_head *cu_headerp,
21929 struct dwarf2_per_cu_data *per_cu)
21930{
d521ce57 21931 const gdb_byte *info_ptr;
96408a79
SA
21932
21933 if (per_cu->cu)
21934 return &per_cu->cu->header;
21935
8a0459fd 21936 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
21937
21938 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 21939 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
21940
21941 return cu_headerp;
21942}
21943
ae0d2f24
UW
21944/* Return the address size given in the compilation unit header for CU. */
21945
98714339 21946int
ae0d2f24
UW
21947dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
21948{
96408a79
SA
21949 struct comp_unit_head cu_header_local;
21950 const struct comp_unit_head *cu_headerp;
c471e790 21951
96408a79
SA
21952 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
21953
21954 return cu_headerp->addr_size;
ae0d2f24
UW
21955}
21956
9eae7c52
TT
21957/* Return the offset size given in the compilation unit header for CU. */
21958
21959int
21960dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
21961{
96408a79
SA
21962 struct comp_unit_head cu_header_local;
21963 const struct comp_unit_head *cu_headerp;
9c6c53f7 21964
96408a79
SA
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
21972int
21973dwarf2_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;
181cebd4
JK
21984}
21985
9aa1f1e3
TT
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
21991CORE_ADDR
21992dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
21993{
bb3fa9d0 21994 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
21995
21996 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21997}
21998
348e048f
DE
21999/* Locate the .debug_info compilation unit from CU's objfile which contains
22000 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22001
22002static struct dwarf2_per_cu_data *
b64f50a1 22003dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 22004 unsigned int offset_in_dwz,
ae038cb0
DJ
22005 struct objfile *objfile)
22006{
22007 struct dwarf2_per_cu_data *this_cu;
22008 int low, high;
36586728 22009 const sect_offset *cu_off;
ae038cb0 22010
ae038cb0
DJ
22011 low = 0;
22012 high = dwarf2_per_objfile->n_comp_units - 1;
22013 while (high > low)
22014 {
36586728 22015 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22016 int mid = low + (high - low) / 2;
9a619af0 22017
36586728
TT
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))
ae038cb0
DJ
22023 high = mid;
22024 else
22025 low = mid + 1;
22026 }
22027 gdb_assert (low == high);
36586728
TT
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)
ae038cb0 22031 {
36586728 22032 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
22033 error (_("Dwarf Error: could not find partial DIE containing "
22034 "offset 0x%lx [in module %s]"),
b64f50a1 22035 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 22036
b64f50a1
JK
22037 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22038 <= offset.sect_off);
ae038cb0
DJ
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
b64f50a1
JK
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);
ae038cb0
DJ
22048 return this_cu;
22049 }
22050}
22051
23745b47 22052/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22053
9816fde3 22054static void
23745b47 22055init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22056{
9816fde3 22057 memset (cu, 0, sizeof (*cu));
23745b47
DE
22058 per_cu->cu = cu;
22059 cu->per_cu = per_cu;
22060 cu->objfile = per_cu->objfile;
93311388 22061 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22062}
22063
22064/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22065
22066static void
95554aad
TT
22067prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22068 enum language pretend_language)
9816fde3
JK
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
9cded63f 22077 {
95554aad 22078 cu->language = pretend_language;
9cded63f
TT
22079 cu->language_defn = language_def (cu->language);
22080 }
dee91e82 22081
7d45c7c3 22082 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22083}
22084
ae038cb0
DJ
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;
93311388
DE
22087 the caller is responsible for that.
22088 NOTE: DATA is a void * because this function is also used as a
22089 cleanup routine. */
ae038cb0
DJ
22090
22091static void
68dc6402 22092free_heap_comp_unit (void *data)
ae038cb0 22093{
9a3c8263 22094 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22095
23745b47
DE
22096 gdb_assert (cu->per_cu != NULL);
22097 cu->per_cu->cu = NULL;
ae038cb0
DJ
22098 cu->per_cu = NULL;
22099
22100 obstack_free (&cu->comp_unit_obstack, NULL);
22101
22102 xfree (cu);
22103}
22104
72bf9492 22105/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22106 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22107 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22108
22109static void
22110free_stack_comp_unit (void *data)
22111{
9a3c8263 22112 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22113
23745b47
DE
22114 gdb_assert (cu->per_cu != NULL);
22115 cu->per_cu->cu = NULL;
22116 cu->per_cu = NULL;
22117
72bf9492
DJ
22118 obstack_free (&cu->comp_unit_obstack, NULL);
22119 cu->partial_dies = NULL;
ae038cb0
DJ
22120}
22121
22122/* Free all cached compilation units. */
22123
22124static void
22125free_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
68dc6402 22137 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
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
22147static void
22148age_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 ++;
b4f54984 22157 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
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 {
68dc6402 22172 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
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
22184static void
dee91e82 22185free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
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
dee91e82 22197 if (per_cu == target_per_cu)
ae038cb0 22198 {
68dc6402 22199 free_heap_comp_unit (per_cu->cu);
dee91e82 22200 per_cu->cu = NULL;
ae038cb0
DJ
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
fe3e1990
DJ
22211/* Release all extra memory associated with OBJFILE. */
22212
22213void
22214dwarf2_free_objfile (struct objfile *objfile)
22215{
9a3c8263
SM
22216 dwarf2_per_objfile
22217 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22218 dwarf2_objfile_data_key);
fe3e1990
DJ
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
7b9f3c50
DE
22226 if (dwarf2_per_objfile->quick_file_names_table)
22227 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 22228
527f3840
JK
22229 if (dwarf2_per_objfile->line_header_hash)
22230 htab_delete (dwarf2_per_objfile->line_header_hash);
22231
fe3e1990
DJ
22232 /* Everything else should be on the objfile obstack. */
22233}
22234
dee91e82
DE
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
3019eac3 22240 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
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. */
1c379e20 22248
dee91e82 22249struct dwarf2_per_cu_offset_and_type
1c379e20 22250{
dee91e82 22251 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 22252 sect_offset offset;
1c379e20
DJ
22253 struct type *type;
22254};
22255
dee91e82 22256/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22257
22258static hashval_t
dee91e82 22259per_cu_offset_and_type_hash (const void *item)
1c379e20 22260{
9a3c8263
SM
22261 const struct dwarf2_per_cu_offset_and_type *ofs
22262 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22263
dee91e82 22264 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
22265}
22266
dee91e82 22267/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22268
22269static int
dee91e82 22270per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22271{
9a3c8263
SM
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;
9a619af0 22276
dee91e82
DE
22277 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22278 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
22279}
22280
22281/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
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.
e71ec853 22295 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
22296 possible before fetching more types to complete the current type.
22297 * Make the type as complete as possible before fetching more types. */
1c379e20 22298
f792889a 22299static struct type *
1c379e20
DJ
22300set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22301{
dee91e82 22302 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 22303 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
22304 struct attribute *attr;
22305 struct dynamic_prop prop;
1c379e20 22306
b4ba55a1
JB
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
09e2d7c7
DE
22317 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22318 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22319 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
22320 && !HAVE_GNAT_AUX_INFO (type))
22321 INIT_GNAT_SPECIFIC (type);
22322
3f2f83dd
KB
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
3cdcd0ce
JB
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))
93a8e227 22356 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 22357
dee91e82 22358 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22359 {
dee91e82
DE
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);
f792889a 22368 }
1c379e20 22369
dee91e82 22370 ofs.per_cu = cu->per_cu;
1c379e20
DJ
22371 ofs.offset = die->offset;
22372 ofs.type = type;
dee91e82
DE
22373 slot = (struct dwarf2_per_cu_offset_and_type **)
22374 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
22375 if (*slot)
22376 complaint (&symfile_complaints,
22377 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 22378 die->offset.sect_off);
8d749320
SM
22379 *slot = XOBNEW (&objfile->objfile_obstack,
22380 struct dwarf2_per_cu_offset_and_type);
1c379e20 22381 **slot = ofs;
f792889a 22382 return type;
1c379e20
DJ
22383}
22384
02142a6c
DE
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. */
1c379e20
DJ
22387
22388static struct type *
b64f50a1 22389get_die_type_at_offset (sect_offset offset,
673bfd45 22390 struct dwarf2_per_cu_data *per_cu)
1c379e20 22391{
dee91e82 22392 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 22393
dee91e82 22394 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22395 return NULL;
1c379e20 22396
dee91e82 22397 ofs.per_cu = per_cu;
673bfd45 22398 ofs.offset = offset;
9a3c8263
SM
22399 slot = ((struct dwarf2_per_cu_offset_and_type *)
22400 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
22401 if (slot)
22402 return slot->type;
22403 else
22404 return NULL;
22405}
22406
02142a6c 22407/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
22408 or return NULL if DIE does not have a saved type. */
22409
22410static struct type *
22411get_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
10b3939b
DJ
22416/* Add a dependence relationship from CU to REF_PER_CU. */
22417
22418static void
22419dwarf2_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}
1c379e20 22435
f504f079
DE
22436/* Subroutine of dwarf2_mark to pass to htab_traverse.
22437 Set the mark field in every compilation unit in the
ae038cb0
DJ
22438 cache that we must keep because we are keeping CU. */
22439
10b3939b
DJ
22440static int
22441dwarf2_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;
d07ed419
JK
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
10b3939b
DJ
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
f504f079
DE
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
ae038cb0
DJ
22466static void
22467dwarf2_mark (struct dwarf2_cu *cu)
22468{
22469 if (cu->mark)
22470 return;
22471 cu->mark = 1;
10b3939b
DJ
22472 if (cu->dependencies != NULL)
22473 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
22474}
22475
22476static void
22477dwarf2_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 }
72bf9492
DJ
22484}
22485
72bf9492
DJ
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
22489static hashval_t
22490partial_die_hash (const void *item)
22491{
9a3c8263
SM
22492 const struct partial_die_info *part_die
22493 = (const struct partial_die_info *) item;
9a619af0 22494
b64f50a1 22495 return part_die->offset.sect_off;
72bf9492
DJ
22496}
22497
22498/* Trivial comparison function for partial_die_info structures: two DIEs
22499 are equal if they have the same offset. */
22500
22501static int
22502partial_die_eq (const void *item_lhs, const void *item_rhs)
22503{
9a3c8263
SM
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;
9a619af0 22508
b64f50a1 22509 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
22510}
22511
b4f54984
DE
22512static struct cmd_list_element *set_dwarf_cmdlist;
22513static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
22514
22515static void
b4f54984 22516set_dwarf_cmd (char *args, int from_tty)
ae038cb0 22517{
b4f54984 22518 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 22519 gdb_stdout);
ae038cb0
DJ
22520}
22521
22522static void
b4f54984 22523show_dwarf_cmd (char *args, int from_tty)
6e70227d 22524{
b4f54984 22525 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
22526}
22527
4bf44c1c 22528/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
22529
22530static void
c1bd65d0 22531dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 22532{
9a3c8263 22533 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 22534 int ix;
8b70b953 22535
626f2d1c
TT
22536 /* Make sure we don't accidentally use dwarf2_per_objfile while
22537 cleaning up. */
22538 dwarf2_per_objfile = NULL;
22539
59b0c7c1
JB
22540 for (ix = 0; ix < data->n_comp_units; ++ix)
22541 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 22542
59b0c7c1 22543 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 22544 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
22545 data->all_type_units[ix]->per_cu.imported_symtabs);
22546 xfree (data->all_type_units);
95554aad 22547
8b70b953 22548 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
22549
22550 if (data->dwo_files)
22551 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
22552 if (data->dwp_file)
22553 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
22554
22555 if (data->dwz_file && data->dwz_file->dwz_bfd)
22556 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
22557}
22558
22559\f
ae2de4f8 22560/* The "save gdb-index" command. */
9291a0cd
TT
22561
22562/* The contents of the hash table we create when building the string
22563 table. */
22564struct strtab_entry
22565{
22566 offset_type offset;
22567 const char *str;
22568};
22569
559a7a62
JK
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. */
b89be57b 22574
9291a0cd
TT
22575static hashval_t
22576hash_strtab_entry (const void *e)
22577{
9a3c8263 22578 const struct strtab_entry *entry = (const struct strtab_entry *) e;
559a7a62 22579 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
22580}
22581
22582/* Equality function for a strtab_entry. */
b89be57b 22583
9291a0cd
TT
22584static int
22585eq_strtab_entry (const void *a, const void *b)
22586{
9a3c8263
SM
22587 const struct strtab_entry *ea = (const struct strtab_entry *) a;
22588 const struct strtab_entry *eb = (const struct strtab_entry *) b;
9291a0cd
TT
22589 return !strcmp (ea->str, eb->str);
22590}
22591
22592/* Create a strtab_entry hash table. */
b89be57b 22593
9291a0cd
TT
22594static htab_t
22595create_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. */
b89be57b 22603
9291a0cd
TT
22604static offset_type
22605add_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)
9a3c8263 22614 result = (struct strtab_entry *) *slot;
9291a0cd
TT
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. */
22627struct 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. */
22639struct 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. */
b89be57b 22647
9291a0cd
TT
22648static hashval_t
22649hash_symtab_entry (const void *e)
22650{
9a3c8263
SM
22651 const struct symtab_index_entry *entry
22652 = (const struct symtab_index_entry *) e;
9291a0cd
TT
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. */
b89be57b 22660
9291a0cd
TT
22661static int
22662eq_symtab_entry (const void *a, const void *b)
22663{
9a3c8263
SM
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;
9291a0cd
TT
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. */
b89be57b 22675
9291a0cd
TT
22676static void
22677delete_symtab_entry (void *p)
22678{
9a3c8263 22679 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
9291a0cd
TT
22680 VEC_free (offset_type, entry->cu_indices);
22681 xfree (entry);
22682}
22683
22684/* Create a hash table holding symtab_index_entry objects. */
b89be57b 22685
9291a0cd 22686static htab_t
3876f04e 22687create_symbol_hash_table (void)
9291a0cd
TT
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. */
b89be57b 22694
9291a0cd
TT
22695static struct mapped_symtab *
22696create_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. */
b89be57b 22706
9291a0cd
TT
22707static void
22708cleanup_mapped_symtab (void *p)
22709{
9a3c8263 22710 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
9291a0cd
TT
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
559a7a62
JK
22718 the slot.
22719
22720 Function is used only during write_hash_table so no index format backward
22721 compatibility is needed. */
b89be57b 22722
9291a0cd
TT
22723static struct symtab_index_entry **
22724find_slot (struct mapped_symtab *symtab, const char *name)
22725{
559a7a62 22726 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
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. */
b89be57b 22740
9291a0cd
TT
22741static void
22742hash_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
156942c7
DE
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). */
b89be57b 22767
9291a0cd
TT
22768static void
22769add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 22770 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
22771 offset_type cu_index)
22772{
22773 struct symtab_index_entry **slot;
156942c7 22774 offset_type cu_index_and_attrs;
9291a0cd
TT
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;
156942c7 22785 /* index_offset is set later. */
9291a0cd
TT
22786 (*slot)->cu_indices = NULL;
22787 }
156942c7
DE
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
22806static int
22807offset_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
22817static void
22818uniquify_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 }
9291a0cd
TT
22855}
22856
22857/* Add a vector of indices to the constant pool. */
b89be57b 22858
9291a0cd 22859static offset_type
3876f04e 22860add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
22861 struct symtab_index_entry *entry)
22862{
22863 void **slot;
22864
3876f04e 22865 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
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 {
9a3c8263
SM
22887 struct symtab_index_entry *old_entry
22888 = (struct symtab_index_entry *) *slot;
9291a0cd
TT
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. */
b89be57b 22897
9291a0cd
TT
22898static void
22899write_hash_table (struct mapped_symtab *symtab,
22900 struct obstack *output, struct obstack *cpool)
22901{
22902 offset_type i;
3876f04e 22903 htab_t symbol_hash_table;
9291a0cd
TT
22904 htab_t str_table;
22905
3876f04e 22906 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 22907 str_table = create_strtab ();
3876f04e 22908
9291a0cd
TT
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])
3876f04e 22914 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
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);
3876f04e 22943 htab_delete (symbol_hash_table);
9291a0cd
TT
22944}
22945
0a5429f6
DE
22946/* Struct to map psymtab to CU index in the index file. */
22947struct psymtab_cu_index_map
22948{
22949 struct partial_symtab *psymtab;
22950 unsigned int cu_index;
22951};
22952
22953static hashval_t
22954hash_psymtab_cu_index (const void *item)
22955{
9a3c8263
SM
22956 const struct psymtab_cu_index_map *map
22957 = (const struct psymtab_cu_index_map *) item;
0a5429f6
DE
22958
22959 return htab_hash_pointer (map->psymtab);
22960}
22961
22962static int
22963eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
22964{
9a3c8263
SM
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;
0a5429f6
DE
22969
22970 return lhs->psymtab == rhs->psymtab;
22971}
22972
22973/* Helper struct for building the address table. */
22974struct 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;
0963b4bd 22986 /* Start address of the CU. */
0a5429f6
DE
22987 CORE_ADDR previous_cu_start;
22988};
22989
22990/* Write an address entry to OBSTACK. */
b89be57b 22991
9291a0cd 22992static void
0a5429f6
DE
22993add_address_entry (struct objfile *objfile, struct obstack *obstack,
22994 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 22995{
0a5429f6 22996 offset_type cu_index_to_write;
948f8e3d 22997 gdb_byte addr[8];
9291a0cd
TT
22998 CORE_ADDR baseaddr;
22999
23000 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23001
0a5429f6
DE
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
23012static int
23013add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23014{
9a3c8263
SM
23015 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23016 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
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;
9a3c8263
SM
23028 map = ((struct psymtab_cu_index_map *)
23029 htab_find (data->cu_index_htab, &find_map));
0a5429f6
DE
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
23044static void
23045write_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);
9291a0cd
TT
23071}
23072
156942c7
DE
23073/* Return the symbol kind of PSYM. */
23074
23075static gdb_index_symbol_kind
23076symbol_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
9291a0cd 23111/* Add a list of partial symbols to SYMTAB. */
b89be57b 23112
9291a0cd
TT
23113static void
23114write_psymbols (struct mapped_symtab *symtab,
987d643c 23115 htab_t psyms_seen,
9291a0cd
TT
23116 struct partial_symbol **psymp,
23117 int count,
987d643c
TT
23118 offset_type cu_index,
23119 int is_static)
9291a0cd
TT
23120{
23121 for (; count-- > 0; ++psymp)
23122 {
156942c7
DE
23123 struct partial_symbol *psym = *psymp;
23124 void **slot;
987d643c 23125
156942c7 23126 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23127 error (_("Ada is not currently supported by the index"));
987d643c 23128
987d643c 23129 /* Only add a given psymbol once. */
156942c7 23130 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
23131 if (!*slot)
23132 {
156942c7
DE
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);
987d643c 23138 }
9291a0cd
TT
23139 }
23140}
23141
23142/* Write the contents of an ("unfinished") obstack to FILE. Throw an
23143 exception if there is an error. */
b89be57b 23144
9291a0cd
TT
23145static void
23146write_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
1fd400ff
TT
23154/* A helper struct used when iterating over debug_types. */
23155struct signatured_type_index_data
23156{
23157 struct objfile *objfile;
23158 struct mapped_symtab *symtab;
23159 struct obstack *types_list;
987d643c 23160 htab_t psyms_seen;
1fd400ff
TT
23161 int cu_index;
23162};
23163
23164/* A helper function that writes a single signatured_type to an
23165 obstack. */
b89be57b 23166
1fd400ff
TT
23167static int
23168write_one_signatured_type (void **slot, void *d)
23169{
9a3c8263
SM
23170 struct signatured_type_index_data *info
23171 = (struct signatured_type_index_data *) d;
1fd400ff 23172 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23173 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23174 gdb_byte val[8];
23175
23176 write_psymbols (info->symtab,
987d643c 23177 info->psyms_seen,
3e43a32a
MS
23178 info->objfile->global_psymbols.list
23179 + psymtab->globals_offset,
987d643c
TT
23180 psymtab->n_global_syms, info->cu_index,
23181 0);
1fd400ff 23182 write_psymbols (info->symtab,
987d643c 23183 info->psyms_seen,
3e43a32a
MS
23184 info->objfile->static_psymbols.list
23185 + psymtab->statics_offset,
987d643c
TT
23186 psymtab->n_static_syms, info->cu_index,
23187 1);
1fd400ff 23188
b64f50a1
JK
23189 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23190 entry->per_cu.offset.sect_off);
1fd400ff 23191 obstack_grow (info->types_list, val, 8);
3019eac3
DE
23192 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23193 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
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
95554aad
TT
23203/* Recurse into all "included" dependencies and write their symbols as
23204 if they appeared in this psymtab. */
23205
23206static void
23207recursively_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
9291a0cd 23232/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23233
9291a0cd
TT
23234static void
23235write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23236{
23237 struct cleanup *cleanup;
bef155c3 23238 char *filename;
1fd400ff
TT
23239 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23240 struct obstack cu_list, types_cu_list;
9291a0cd
TT
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;
0a5429f6 23246 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 23247
9291a0cd
TT
23248 if (dwarf2_per_objfile->using_index)
23249 error (_("Cannot use an index to create the index"));
23250
8b70b953
TT
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
260b681b
DE
23254 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23255 return;
23256
4262abfb
JK
23257 if (stat (objfile_name (objfile), &st) < 0)
23258 perror_with_name (objfile_name (objfile));
9291a0cd 23259
4262abfb 23260 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
23261 INDEX_SUFFIX, (char *) NULL);
23262 cleanup = make_cleanup (xfree, filename);
23263
614c279d 23264 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
23265 if (!out_file)
23266 error (_("Can't open `%s' for writing"), filename);
23267
bef155c3 23268 gdb::unlinker unlink_file (filename);
9291a0cd
TT
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
1fd400ff
TT
23279 obstack_init (&types_cu_list);
23280 make_cleanup_obstack_free (&types_cu_list);
23281
fc4007c9
TT
23282 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23283 htab_eq_pointer,
23284 NULL, xcalloc, xfree));
987d643c 23285
0a5429f6
DE
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. */
fc4007c9
TT
23290 htab_up cu_index_htab (htab_create_alloc (100,
23291 hash_psymtab_cu_index,
23292 eq_psymtab_cu_index,
23293 NULL, xcalloc, xfree));
8d749320
SM
23294 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23295 dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23296 make_cleanup (xfree, psymtab_cu_index_map);
23297
23298 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23299 work here. Also, the debug_types entries do not appear in
23300 all_comp_units, but only in their own hash table. */
9291a0cd
TT
23301 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23302 {
3e43a32a
MS
23303 struct dwarf2_per_cu_data *per_cu
23304 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23305 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23306 gdb_byte val[8];
0a5429f6
DE
23307 struct psymtab_cu_index_map *map;
23308 void **slot;
9291a0cd 23309
92fac807
JK
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
95554aad 23316 if (psymtab->user == NULL)
fc4007c9
TT
23317 recursively_write_psymbols (objfile, psymtab, symtab,
23318 psyms_seen.get (), i);
9291a0cd 23319
0a5429f6
DE
23320 map = &psymtab_cu_index_map[i];
23321 map->psymtab = psymtab;
23322 map->cu_index = i;
fc4007c9 23323 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
0a5429f6
DE
23324 gdb_assert (slot != NULL);
23325 gdb_assert (*slot == NULL);
23326 *slot = map;
9291a0cd 23327
b64f50a1
JK
23328 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23329 per_cu->offset.sect_off);
9291a0cd 23330 obstack_grow (&cu_list, val, 8);
e254ef6a 23331 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23332 obstack_grow (&cu_list, val, 8);
23333 }
23334
0a5429f6 23335 /* Dump the address map. */
fc4007c9 23336 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
0a5429f6 23337
1fd400ff
TT
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;
fc4007c9 23346 sig_data.psyms_seen = psyms_seen.get ();
1fd400ff
TT
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
156942c7
DE
23352 /* Now that we've processed all symbols we can shrink their cu_indices
23353 lists. */
23354 uniquify_cu_indices (symtab);
23355
9291a0cd
TT
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);
1fd400ff 23364 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
23365 total_len = size_of_contents;
23366
23367 /* The version number. */
796a7ff8 23368 val = MAYBE_SWAP (8);
9291a0cd
TT
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
1fd400ff
TT
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
9291a0cd
TT
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);
1fd400ff 23400 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
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
bef155c3
TT
23407 /* We want to keep the file. */
23408 unlink_file.keep ();
9291a0cd
TT
23409
23410 do_cleanups (cleanup);
23411}
23412
90476074
TT
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. */
11570e71 23417
9291a0cd
TT
23418static void
23419save_gdb_index_command (char *arg, int from_tty)
23420{
23421 struct objfile *objfile;
23422
23423 if (!arg || !*arg)
96d19272 23424 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
23425
23426 ALL_OBJFILES (objfile)
23427 {
23428 struct stat st;
23429
23430 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 23431 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
23432 continue;
23433
9a3c8263
SM
23434 dwarf2_per_objfile
23435 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23436 dwarf2_objfile_data_key);
9291a0cd
TT
23437 if (dwarf2_per_objfile)
23438 {
9291a0cd 23439
492d29ea 23440 TRY
9291a0cd
TT
23441 {
23442 write_psymtabs_to_index (objfile, arg);
23443 }
492d29ea
PA
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
9291a0cd
TT
23451 }
23452 }
dce234bc
PP
23453}
23454
9291a0cd
TT
23455\f
23456
b4f54984 23457int dwarf_always_disassemble;
9eae7c52
TT
23458
23459static void
b4f54984
DE
23460show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23461 struct cmd_list_element *c, const char *value)
9eae7c52 23462{
3e43a32a
MS
23463 fprintf_filtered (file,
23464 _("Whether to always disassemble "
23465 "DWARF expressions is %s.\n"),
9eae7c52
TT
23466 value);
23467}
23468
900e11f9
JK
23469static void
23470show_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
6502dd73
DJ
23478void _initialize_dwarf2_read (void);
23479
23480void
23481_initialize_dwarf2_read (void)
23482{
96d19272
JK
23483 struct cmd_list_element *c;
23484
dce234bc 23485 dwarf2_objfile_data_key
c1bd65d0 23486 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 23487
b4f54984
DE
23488 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23489Set DWARF specific variables.\n\
23490Configure DWARF variables such as the cache size"),
23491 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
23492 0/*allow-unknown*/, &maintenance_set_cmdlist);
23493
b4f54984
DE
23494 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23495Show DWARF specific variables\n\
23496Show DWARF variables such as the cache size"),
23497 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
23498 0/*allow-unknown*/, &maintenance_show_cmdlist);
23499
23500 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
23501 &dwarf_max_cache_age, _("\
23502Set the upper bound on the age of cached DWARF compilation units."), _("\
23503Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
23504A higher limit means that cached compilation units will be stored\n\
23505in memory longer, and more total memory will be used. Zero disables\n\
23506caching, which can slow down startup."),
2c5b56ce 23507 NULL,
b4f54984
DE
23508 show_dwarf_max_cache_age,
23509 &set_dwarf_cmdlist,
23510 &show_dwarf_cmdlist);
d97bc12b 23511
9eae7c52 23512 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 23513 &dwarf_always_disassemble, _("\
9eae7c52
TT
23514Set whether `info address' always disassembles DWARF expressions."), _("\
23515Show whether `info address' always disassembles DWARF expressions."), _("\
23516When enabled, DWARF expressions are always printed in an assembly-like\n\
23517syntax. When disabled, expressions will be printed in a more\n\
23518conversational style, when possible."),
23519 NULL,
b4f54984
DE
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, _("\
23525Set debugging of the DWARF reader."), _("\
23526Show debugging of the DWARF reader."), _("\
23527When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
23528reading and symtab expansion. A value of 1 (one) provides basic\n\
23529information. A value greater than 1 provides more verbose information."),
45cfd468
DE
23530 NULL,
23531 NULL,
23532 &setdebuglist, &showdebuglist);
23533
b4f54984
DE
23534 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23535Set debugging of the DWARF DIE reader."), _("\
23536Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
23537When enabled (non-zero), DIEs are dumped after they are read in.\n\
23538The value is the maximum depth to print."),
ccce17b0
YQ
23539 NULL,
23540 NULL,
23541 &setdebuglist, &showdebuglist);
9291a0cd 23542
27e0867f
DE
23543 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23544Set debugging of the dwarf line reader."), _("\
23545Show debugging of the dwarf line reader."), _("\
23546When enabled (non-zero), line number entries are dumped as they are read in.\n\
23547A value of 1 (one) provides basic information.\n\
23548A value greater than 1 provides more verbose information."),
23549 NULL,
23550 NULL,
23551 &setdebuglist, &showdebuglist);
23552
900e11f9
JK
23553 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23554Set cross-checking of \"physname\" code against demangler."), _("\
23555Show cross-checking of \"physname\" code against demangler."), _("\
23556When enabled, GDB's internal \"physname\" code is checked against\n\
23557the demangler."),
23558 NULL, show_check_physname,
23559 &setdebuglist, &showdebuglist);
23560
e615022a
DE
23561 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23562 no_class, &use_deprecated_index_sections, _("\
23563Set whether to use deprecated gdb_index sections."), _("\
23564Show whether to use deprecated gdb_index sections."), _("\
23565When enabled, deprecated .gdb_index sections are used anyway.\n\
23566Normally they are ignored either because of a missing feature or\n\
23567performance issue.\n\
23568Warning: This option must be enabled before gdb reads the file."),
23569 NULL,
23570 NULL,
23571 &setlist, &showlist);
23572
96d19272 23573 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 23574 _("\
fc1a9d6e 23575Save a gdb-index file.\n\
11570e71 23576Usage: save gdb-index DIRECTORY"),
96d19272
JK
23577 &save_cmdlist);
23578 set_cmd_completer (c, filename_completer);
f1e6e072
TT
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);
6502dd73 23589}
This page took 6.070977 seconds and 4 git commands to generate.