* dwarf2read.c (read_cutu_die_from_dwo): New function.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 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
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
4c2df51b 71
c906108c
SS
72#include <fcntl.h>
73#include "gdb_string.h"
4bdf3d34 74#include "gdb_assert.h"
c906108c 75#include <sys/types.h>
d8151005 76
34eaf542
TT
77typedef struct symbol *symbolp;
78DEF_VEC_P (symbolp);
79
45cfd468
DE
80/* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82static int dwarf2_read_debug = 0;
83
d97bc12b 84/* When non-zero, dump DIEs after they are read in. */
ccce17b0 85static unsigned int dwarf2_die_debug = 0;
d97bc12b 86
900e11f9
JK
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
481860b3 90/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 91static int use_deprecated_index_sections = 0;
481860b3 92
6502dd73
DJ
93static const struct objfile_data *dwarf2_objfile_data_key;
94
f1e6e072
TT
95/* The "aclass" indices for various kinds of computed DWARF symbols. */
96
97static int dwarf2_locexpr_index;
98static int dwarf2_loclist_index;
99static int dwarf2_locexpr_block_index;
100static int dwarf2_loclist_block_index;
101
dce234bc
PP
102struct dwarf2_section_info
103{
104 asection *asection;
105 gdb_byte *buffer;
106 bfd_size_type size;
be391dca
TT
107 /* True if we have tried to read this section. */
108 int readin;
dce234bc
PP
109};
110
8b70b953
TT
111typedef struct dwarf2_section_info dwarf2_section_info_def;
112DEF_VEC_O (dwarf2_section_info_def);
113
9291a0cd
TT
114/* All offsets in the index are of this type. It must be
115 architecture-independent. */
116typedef uint32_t offset_type;
117
118DEF_VEC_I (offset_type);
119
156942c7
DE
120/* Ensure only legit values are used. */
121#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
122 do { \
123 gdb_assert ((unsigned int) (value) <= 1); \
124 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
125 } while (0)
126
127/* Ensure only legit values are used. */
128#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
129 do { \
130 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
133 } while (0)
134
135/* Ensure we don't use more than the alloted nuber of bits for the CU. */
136#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
137 do { \
138 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
140 } while (0)
141
9291a0cd
TT
142/* A description of the mapped index. The file format is described in
143 a comment by the code that writes the index. */
144struct mapped_index
145{
559a7a62
JK
146 /* Index data format version. */
147 int version;
148
9291a0cd
TT
149 /* The total length of the buffer. */
150 off_t total_size;
b11b1f88 151
9291a0cd
TT
152 /* A pointer to the address table data. */
153 const gdb_byte *address_table;
b11b1f88 154
9291a0cd
TT
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
b11b1f88 157
3876f04e
DE
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
b11b1f88 160
9291a0cd 161 /* Size in slots, each slot is 2 offset_types. */
3876f04e 162 offset_type symbol_table_slots;
b11b1f88 163
9291a0cd
TT
164 /* A pointer to the constant pool. */
165 const char *constant_pool;
166};
167
95554aad
TT
168typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169DEF_VEC_P (dwarf2_per_cu_ptr);
170
9cdd5dbd
DE
171/* Collection of data recorded per objfile.
172 This hangs off of dwarf2_objfile_data_key. */
173
6502dd73
DJ
174struct dwarf2_per_objfile
175{
dce234bc
PP
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
dce234bc
PP
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
cf2c3c16 181 struct dwarf2_section_info macro;
dce234bc
PP
182 struct dwarf2_section_info str;
183 struct dwarf2_section_info ranges;
3019eac3 184 struct dwarf2_section_info addr;
dce234bc
PP
185 struct dwarf2_section_info frame;
186 struct dwarf2_section_info eh_frame;
9291a0cd 187 struct dwarf2_section_info gdb_index;
ae038cb0 188
8b70b953
TT
189 VEC (dwarf2_section_info_def) *types;
190
be391dca
TT
191 /* Back link. */
192 struct objfile *objfile;
193
d467dd73 194 /* Table of all the compilation units. This is used to locate
10b3939b 195 the target compilation unit of a particular reference. */
ae038cb0
DJ
196 struct dwarf2_per_cu_data **all_comp_units;
197
198 /* The number of compilation units in ALL_COMP_UNITS. */
199 int n_comp_units;
200
1fd400ff 201 /* The number of .debug_types-related CUs. */
d467dd73 202 int n_type_units;
1fd400ff 203
d467dd73 204 /* The .debug_types-related CUs (TUs). */
b4dd5633 205 struct signatured_type **all_type_units;
1fd400ff 206
f4dc4d17
DE
207 /* The number of entries in all_type_unit_groups. */
208 int n_type_unit_groups;
209
210 /* Table of type unit groups.
211 This exists to make it easy to iterate over all CUs and TU groups. */
212 struct type_unit_group **all_type_unit_groups;
213
214 /* Table of struct type_unit_group objects.
215 The hash key is the DW_AT_stmt_list value. */
216 htab_t type_unit_groups;
72dca2f5 217
348e048f
DE
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
221
f4dc4d17
DE
222 /* Type unit statistics, to see how well the scaling improvements
223 are doing. */
224 struct tu_stats
225 {
226 int nr_uniq_abbrev_tables;
227 int nr_symtabs;
228 int nr_symtab_sharers;
229 int nr_stmt_less_type_units;
230 } tu_stats;
231
232 /* A chain of compilation units that are currently read in, so that
233 they can be freed later. */
234 struct dwarf2_per_cu_data *read_in_chain;
235
3019eac3
DE
236 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237 This is NULL if the table hasn't been allocated yet. */
238 htab_t dwo_files;
239
80626a55
DE
240 /* Non-zero if we've check for whether there is a DWP file. */
241 int dwp_checked;
242
243 /* The DWP file if there is one, or NULL. */
244 struct dwp_file *dwp_file;
245
36586728
TT
246 /* The shared '.dwz' file, if one exists. This is used when the
247 original data was compressed using 'dwz -m'. */
248 struct dwz_file *dwz_file;
249
72dca2f5
FR
250 /* A flag indicating wether this objfile has a section loaded at a
251 VMA of 0. */
252 int has_section_at_zero;
9291a0cd 253
ae2de4f8
DE
254 /* True if we are using the mapped index,
255 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
256 unsigned char using_index;
257
ae2de4f8 258 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 259 struct mapped_index *index_table;
98bfdba5 260
7b9f3c50 261 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
262 TUs typically share line table entries with a CU, so we maintain a
263 separate table of all line table entries to support the sharing.
264 Note that while there can be way more TUs than CUs, we've already
265 sorted all the TUs into "type unit groups", grouped by their
266 DW_AT_stmt_list value. Therefore the only sharing done here is with a
267 CU and its associated TU group if there is one. */
7b9f3c50
DE
268 htab_t quick_file_names_table;
269
98bfdba5
PA
270 /* Set during partial symbol reading, to prevent queueing of full
271 symbols. */
272 int reading_partial_symbols;
673bfd45 273
dee91e82 274 /* Table mapping type DIEs to their struct type *.
673bfd45 275 This is NULL if not allocated yet.
dee91e82
DE
276 The mapping is done via (CU/TU signature + DIE offset) -> type. */
277 htab_t die_type_hash;
95554aad
TT
278
279 /* The CUs we recently read. */
280 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
281};
282
283static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 284
251d32d9 285/* Default names of the debugging sections. */
c906108c 286
233a11ab
CS
287/* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
9cdd5dbd
DE
290static const struct dwarf2_debug_sections dwarf2_elf_names =
291{
251d32d9
TG
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 297 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_ranges", ".zdebug_ranges" },
300 { ".debug_types", ".zdebug_types" },
3019eac3 301 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
24d3216f
TT
304 { ".gdb_index", ".zgdb_index" },
305 23
251d32d9 306};
c906108c 307
80626a55 308/* List of DWO/DWP sections. */
3019eac3 309
80626a55 310static const struct dwop_section_names
3019eac3
DE
311{
312 struct dwarf2_section_names abbrev_dwo;
313 struct dwarf2_section_names info_dwo;
314 struct dwarf2_section_names line_dwo;
315 struct dwarf2_section_names loc_dwo;
09262596
DE
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
3019eac3
DE
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
80626a55
DE
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
3019eac3 323}
80626a55 324dwop_section_names =
3019eac3
DE
325{
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
330 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
332 { ".debug_str.dwo", ".zdebug_str.dwo" },
333 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
335 { ".debug_cu_index", ".zdebug_cu_index" },
336 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
337};
338
c906108c
SS
339/* local data types */
340
107d2387
AC
341/* The data in a compilation unit header, after target2host
342 translation, looks like this. */
c906108c 343struct comp_unit_head
a738430d 344{
c764a876 345 unsigned int length;
a738430d 346 short version;
a738430d
MK
347 unsigned char addr_size;
348 unsigned char signed_addr_p;
b64f50a1 349 sect_offset abbrev_offset;
57349743 350
a738430d
MK
351 /* Size of file offsets; either 4 or 8. */
352 unsigned int offset_size;
57349743 353
a738430d
MK
354 /* Size of the length field; either 4 or 12. */
355 unsigned int initial_length_size;
57349743 356
a738430d
MK
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
b64f50a1 359 sect_offset offset;
57349743 360
d00adf39
DE
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
b64f50a1 363 cu_offset first_die_offset;
a738430d 364};
c906108c 365
3da10d80
KS
366/* Type used for delaying computation of method physnames.
367 See comments for compute_delayed_physnames. */
368struct delayed_method_info
369{
370 /* The type to which the method is attached, i.e., its parent class. */
371 struct type *type;
372
373 /* The index of the method in the type's function fieldlists. */
374 int fnfield_index;
375
376 /* The index of the method in the fieldlist. */
377 int index;
378
379 /* The name of the DIE. */
380 const char *name;
381
382 /* The DIE associated with this method. */
383 struct die_info *die;
384};
385
386typedef struct delayed_method_info delayed_method_info;
387DEF_VEC_O (delayed_method_info);
388
e7c27a73
DJ
389/* Internal state when decoding a particular compilation unit. */
390struct dwarf2_cu
391{
392 /* The objfile containing this compilation unit. */
393 struct objfile *objfile;
394
d00adf39 395 /* The header of the compilation unit. */
e7c27a73 396 struct comp_unit_head header;
e142c38c 397
d00adf39
DE
398 /* Base address of this compilation unit. */
399 CORE_ADDR base_address;
400
401 /* Non-zero if base_address has been set. */
402 int base_known;
403
e142c38c
DJ
404 /* The language we are debugging. */
405 enum language language;
406 const struct language_defn *language_defn;
407
b0f35d58
DL
408 const char *producer;
409
e142c38c
DJ
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope;
420
433df2d4
DE
421 /* The abbrev table for this CU.
422 Normally this points to the abbrev table in the objfile.
423 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
424 struct abbrev_table *abbrev_table;
72bf9492 425
b64f50a1
JK
426 /* Hash table holding all the loaded partial DIEs
427 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
428 htab_t partial_dies;
429
430 /* Storage for things with the same lifetime as this read-in compilation
431 unit, including partial DIEs. */
432 struct obstack comp_unit_obstack;
433
ae038cb0
DJ
434 /* When multiple dwarf2_cu structures are living in memory, this field
435 chains them all together, so that they can be released efficiently.
436 We will probably also want a generation counter so that most-recently-used
437 compilation units are cached... */
438 struct dwarf2_per_cu_data *read_in_chain;
439
440 /* Backchain to our per_cu entry if the tree has been built. */
441 struct dwarf2_per_cu_data *per_cu;
442
443 /* How many compilation units ago was this CU last referenced? */
444 int last_used;
445
b64f50a1
JK
446 /* A hash table of DIE cu_offset for following references with
447 die_info->offset.sect_off as hash. */
51545339 448 htab_t die_hash;
10b3939b
DJ
449
450 /* Full DIEs if read in. */
451 struct die_info *dies;
452
453 /* A set of pointers to dwarf2_per_cu_data objects for compilation
454 units referenced by this one. Only set during full symbol processing;
455 partial symbol tables do not have dependencies. */
456 htab_t dependencies;
457
cb1df416
DJ
458 /* Header data from the line table, during full symbol processing. */
459 struct line_header *line_header;
460
3da10d80
KS
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 VEC (delayed_method_info) *method_list;
464
96408a79
SA
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab;
467
034e5797
DE
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
3019eac3
DE
477 struct dwo_unit *dwo_unit;
478
479 /* The DW_AT_addr_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE. */
482 ULONGEST addr_base;
483
2e3cf129
DE
484 /* The DW_AT_ranges_base attribute if present, zero otherwise
485 (zero is a valid value though).
486 Note this value comes from the stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_ranges_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
494 ULONGEST ranges_base;
495
ae038cb0
DJ
496 /* Mark used when releasing cached dies. */
497 unsigned int mark : 1;
498
8be455d7
JK
499 /* This CU references .debug_loc. See the symtab->locations_valid field.
500 This test is imperfect as there may exist optimized debug code not using
501 any location list and still facing inlining issues if handled as
502 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 503 unsigned int has_loclist : 1;
ba919b58 504
1b80a9fa
JK
505 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
506 if all the producer_is_* fields are valid. This information is cached
507 because profiling CU expansion showed excessive time spent in
508 producer_is_gxx_lt_4_6. */
ba919b58
TT
509 unsigned int checked_producer : 1;
510 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 511 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 512 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
513
514 /* When set, the file that we're processing is known to have
515 debugging info for C++ namespaces. GCC 3.3.x did not produce
516 this information, but later versions do. */
517
518 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
519};
520
10b3939b
DJ
521/* Persistent data held for a compilation unit, even when not
522 processing it. We put a pointer to this structure in the
28dee7f5 523 read_symtab_private field of the psymtab. */
10b3939b 524
ae038cb0
DJ
525struct dwarf2_per_cu_data
526{
36586728 527 /* The start offset and length of this compilation unit.
45452591 528 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
529 initial_length_size.
530 If the DIE refers to a DWO file, this is always of the original die,
531 not the DWO file. */
b64f50a1 532 sect_offset offset;
36586728 533 unsigned int length;
ae038cb0
DJ
534
535 /* Flag indicating this compilation unit will be read in before
536 any of the current compilation units are processed. */
c764a876 537 unsigned int queued : 1;
ae038cb0 538
0d99eb77
DE
539 /* This flag will be set when reading partial DIEs if we need to load
540 absolutely all DIEs for this compilation unit, instead of just the ones
541 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
542 hash table and don't find it. */
543 unsigned int load_all_dies : 1;
544
3019eac3
DE
545 /* Non-zero if this CU is from .debug_types. */
546 unsigned int is_debug_types : 1;
547
36586728
TT
548 /* Non-zero if this CU is from the .dwz file. */
549 unsigned int is_dwz : 1;
550
3019eac3
DE
551 /* The section this CU/TU lives in.
552 If the DIE refers to a DWO file, this is always the original die,
553 not the DWO file. */
8a0459fd 554 struct dwarf2_section_info *section;
348e048f 555
17ea53c3
JK
556 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
557 of the CU cache it gets reset to NULL again. */
ae038cb0 558 struct dwarf2_cu *cu;
1c379e20 559
9cdd5dbd
DE
560 /* The corresponding objfile.
561 Normally we can get the objfile from dwarf2_per_objfile.
562 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
563 struct objfile *objfile;
564
565 /* When using partial symbol tables, the 'psymtab' field is active.
566 Otherwise the 'quick' field is active. */
567 union
568 {
569 /* The partial symbol table associated with this compilation unit,
95554aad 570 or NULL for unread partial units. */
9291a0cd
TT
571 struct partial_symtab *psymtab;
572
573 /* Data needed by the "quick" functions. */
574 struct dwarf2_per_cu_quick_data *quick;
575 } v;
95554aad 576
796a7ff8
DE
577 /* The CUs we import using DW_TAG_imported_unit. This is filled in
578 while reading psymtabs, used to compute the psymtab dependencies,
579 and then cleared. Then it is filled in again while reading full
580 symbols, and only deleted when the objfile is destroyed.
581
582 This is also used to work around a difference between the way gold
583 generates .gdb_index version <=7 and the way gdb does. Arguably this
584 is a gold bug. For symbols coming from TUs, gold records in the index
585 the CU that includes the TU instead of the TU itself. This breaks
586 dw2_lookup_symbol: It assumes that if the index says symbol X lives
587 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
588 will find X. Alas TUs live in their own symtab, so after expanding CU Y
589 we need to look in TU Z to find X. Fortunately, this is akin to
590 DW_TAG_imported_unit, so we just use the same mechanism: For
591 .gdb_index version <=7 this also records the TUs that the CU referred
592 to. Concurrently with this change gdb was modified to emit version 8
593 indices so we only pay a price for gold generated indices. */
594 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
595
596 /* Type units are grouped by their DW_AT_stmt_list entry so that they
597 can share them. If this is a TU, this points to the containing
598 symtab. */
599 struct type_unit_group *type_unit_group;
ae038cb0
DJ
600};
601
348e048f
DE
602/* Entry in the signatured_types hash table. */
603
604struct signatured_type
605{
42e7ad6c
DE
606 /* The "per_cu" object of this type.
607 N.B.: This is the first member so that it's easy to convert pointers
608 between them. */
609 struct dwarf2_per_cu_data per_cu;
610
3019eac3 611 /* The type's signature. */
348e048f
DE
612 ULONGEST signature;
613
3019eac3 614 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
615 If this TU is a DWO stub and the definition lives in a DWO file
616 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
617 cu_offset type_offset_in_tu;
618
619 /* Offset in the section of the type's DIE.
620 If the definition lives in a DWO file, this is the offset in the
621 .debug_types.dwo section.
622 The value is zero until the actual value is known.
623 Zero is otherwise not a valid section offset. */
624 sect_offset type_offset_in_section;
348e048f
DE
625};
626
094b34ac
DE
627/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
628 This includes type_unit_group and quick_file_names. */
629
630struct stmt_list_hash
631{
632 /* The DWO unit this table is from or NULL if there is none. */
633 struct dwo_unit *dwo_unit;
634
635 /* Offset in .debug_line or .debug_line.dwo. */
636 sect_offset line_offset;
637};
638
f4dc4d17
DE
639/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
640 an object of this type. */
641
642struct type_unit_group
643{
644 /* dwarf2read.c's main "handle" on the symtab.
645 To simplify things we create an artificial CU that "includes" all the
646 type units using this stmt_list so that the rest of the code still has
647 a "per_cu" handle on the symtab.
648 This PER_CU is recognized by having no section. */
8a0459fd 649#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
650 struct dwarf2_per_cu_data per_cu;
651
652 union
653 {
654 /* The TUs that share this DW_AT_stmt_list entry.
655 This is added to while parsing type units to build partial symtabs,
656 and is deleted afterwards and not used again. */
657 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 658
094b34ac
DE
659 /* When reading the line table in "quick" functions, we need a real TU.
660 Any will do, we know they all share the same DW_AT_stmt_list entry.
661 For simplicity's sake, we pick the first one. */
662 struct dwarf2_per_cu_data *first_tu;
663 } t;
f4dc4d17
DE
664
665 /* The primary symtab.
094b34ac
DE
666 Type units in a group needn't all be defined in the same source file,
667 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
668 struct symtab *primary_symtab;
669
094b34ac
DE
670 /* The data used to construct the hash key. */
671 struct stmt_list_hash hash;
f4dc4d17
DE
672
673 /* The number of symtabs from the line header.
674 The value here must match line_header.num_file_names. */
675 unsigned int num_symtabs;
676
677 /* The symbol tables for this TU (obtained from the files listed in
678 DW_AT_stmt_list).
679 WARNING: The order of entries here must match the order of entries
680 in the line header. After the first TU using this type_unit_group, the
681 line header for the subsequent TUs is recreated from this. This is done
682 because we need to use the same symtabs for each TU using the same
683 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
684 there's no guarantee the line header doesn't have duplicate entries. */
685 struct symtab **symtabs;
686};
687
80626a55 688/* These sections are what may appear in a DWO file. */
3019eac3
DE
689
690struct dwo_sections
691{
692 struct dwarf2_section_info abbrev;
3019eac3
DE
693 struct dwarf2_section_info line;
694 struct dwarf2_section_info loc;
09262596
DE
695 struct dwarf2_section_info macinfo;
696 struct dwarf2_section_info macro;
3019eac3
DE
697 struct dwarf2_section_info str;
698 struct dwarf2_section_info str_offsets;
80626a55
DE
699 /* In the case of a virtual DWO file, these two are unused. */
700 struct dwarf2_section_info info;
3019eac3
DE
701 VEC (dwarf2_section_info_def) *types;
702};
703
c88ee1f0 704/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
705
706struct dwo_unit
707{
708 /* Backlink to the containing struct dwo_file. */
709 struct dwo_file *dwo_file;
710
711 /* The "id" that distinguishes this CU/TU.
712 .debug_info calls this "dwo_id", .debug_types calls this "signature".
713 Since signatures came first, we stick with it for consistency. */
714 ULONGEST signature;
715
716 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 717 struct dwarf2_section_info *section;
3019eac3
DE
718
719 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
720 sect_offset offset;
721 unsigned int length;
722
723 /* For types, offset in the type's DIE of the type defined by this TU. */
724 cu_offset type_offset_in_tu;
725};
726
80626a55
DE
727/* Data for one DWO file.
728 This includes virtual DWO files that have been packaged into a
729 DWP file. */
3019eac3
DE
730
731struct dwo_file
732{
80626a55
DE
733 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
734 For virtual DWO files the name is constructed from the section offsets
735 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
736 from related CU+TUs. */
737 const char *name;
3019eac3 738
80626a55
DE
739 /* The bfd, when the file is open. Otherwise this is NULL.
740 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
741 bfd *dbfd;
3019eac3
DE
742
743 /* Section info for this file. */
744 struct dwo_sections sections;
745
746 /* Table of CUs in the file.
747 Each element is a struct dwo_unit. */
748 htab_t cus;
749
750 /* Table of TUs in the file.
751 Each element is a struct dwo_unit. */
752 htab_t tus;
753};
754
80626a55
DE
755/* These sections are what may appear in a DWP file. */
756
757struct dwp_sections
758{
759 struct dwarf2_section_info str;
760 struct dwarf2_section_info cu_index;
761 struct dwarf2_section_info tu_index;
762 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
763 by section number. We don't need to record them here. */
764};
765
766/* These sections are what may appear in a virtual DWO file. */
767
768struct virtual_dwo_sections
769{
770 struct dwarf2_section_info abbrev;
771 struct dwarf2_section_info line;
772 struct dwarf2_section_info loc;
773 struct dwarf2_section_info macinfo;
774 struct dwarf2_section_info macro;
775 struct dwarf2_section_info str_offsets;
776 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 777 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
778 struct dwarf2_section_info info_or_types;
779};
780
781/* Contents of DWP hash tables. */
782
783struct dwp_hash_table
784{
785 uint32_t nr_units, nr_slots;
786 const gdb_byte *hash_table, *unit_table, *section_pool;
787};
788
789/* Data for one DWP file. */
790
791struct dwp_file
792{
793 /* Name of the file. */
794 const char *name;
795
796 /* The bfd, when the file is open. Otherwise this is NULL. */
797 bfd *dbfd;
798
799 /* Section info for this file. */
800 struct dwp_sections sections;
801
802 /* Table of CUs in the file. */
803 const struct dwp_hash_table *cus;
804
805 /* Table of TUs in the file. */
806 const struct dwp_hash_table *tus;
807
808 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
809 htab_t loaded_cutus;
810
811 /* Table to map ELF section numbers to their sections. */
812 unsigned int num_sections;
813 asection **elf_sections;
814};
815
36586728
TT
816/* This represents a '.dwz' file. */
817
818struct dwz_file
819{
820 /* A dwz file can only contain a few sections. */
821 struct dwarf2_section_info abbrev;
822 struct dwarf2_section_info info;
823 struct dwarf2_section_info str;
824 struct dwarf2_section_info line;
825 struct dwarf2_section_info macro;
2ec9a5e0 826 struct dwarf2_section_info gdb_index;
36586728
TT
827
828 /* The dwz's BFD. */
829 bfd *dwz_bfd;
830};
831
0963b4bd
MS
832/* Struct used to pass misc. parameters to read_die_and_children, et
833 al. which are used for both .debug_info and .debug_types dies.
834 All parameters here are unchanging for the life of the call. This
dee91e82 835 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
836
837struct die_reader_specs
838{
dee91e82 839 /* die_section->asection->owner. */
93311388
DE
840 bfd* abfd;
841
842 /* The CU of the DIE we are parsing. */
843 struct dwarf2_cu *cu;
844
80626a55 845 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
846 struct dwo_file *dwo_file;
847
dee91e82 848 /* The section the die comes from.
3019eac3 849 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
850 struct dwarf2_section_info *die_section;
851
852 /* die_section->buffer. */
853 gdb_byte *buffer;
f664829e
DE
854
855 /* The end of the buffer. */
856 const gdb_byte *buffer_end;
93311388
DE
857};
858
fd820528 859/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
860typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
861 gdb_byte *info_ptr,
862 struct die_info *comp_unit_die,
863 int has_children,
864 void *data);
865
debd256d
JB
866/* The line number information for a compilation unit (found in the
867 .debug_line section) begins with a "statement program header",
868 which contains the following information. */
869struct line_header
870{
871 unsigned int total_length;
872 unsigned short version;
873 unsigned int header_length;
874 unsigned char minimum_instruction_length;
2dc7f7b3 875 unsigned char maximum_ops_per_instruction;
debd256d
JB
876 unsigned char default_is_stmt;
877 int line_base;
878 unsigned char line_range;
879 unsigned char opcode_base;
880
881 /* standard_opcode_lengths[i] is the number of operands for the
882 standard opcode whose value is i. This means that
883 standard_opcode_lengths[0] is unused, and the last meaningful
884 element is standard_opcode_lengths[opcode_base - 1]. */
885 unsigned char *standard_opcode_lengths;
886
887 /* The include_directories table. NOTE! These strings are not
888 allocated with xmalloc; instead, they are pointers into
889 debug_line_buffer. If you try to free them, `free' will get
890 indigestion. */
891 unsigned int num_include_dirs, include_dirs_size;
892 char **include_dirs;
893
894 /* The file_names table. NOTE! These strings are not allocated
895 with xmalloc; instead, they are pointers into debug_line_buffer.
896 Don't try to free them directly. */
897 unsigned int num_file_names, file_names_size;
898 struct file_entry
c906108c 899 {
debd256d
JB
900 char *name;
901 unsigned int dir_index;
902 unsigned int mod_time;
903 unsigned int length;
aaa75496 904 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 905 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
906 } *file_names;
907
908 /* The start and end of the statement program following this
6502dd73 909 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 910 gdb_byte *statement_program_start, *statement_program_end;
debd256d 911};
c906108c
SS
912
913/* When we construct a partial symbol table entry we only
0963b4bd 914 need this much information. */
c906108c
SS
915struct partial_die_info
916 {
72bf9492 917 /* Offset of this DIE. */
b64f50a1 918 sect_offset offset;
72bf9492
DJ
919
920 /* DWARF-2 tag for this DIE. */
921 ENUM_BITFIELD(dwarf_tag) tag : 16;
922
72bf9492
DJ
923 /* Assorted flags describing the data found in this DIE. */
924 unsigned int has_children : 1;
925 unsigned int is_external : 1;
926 unsigned int is_declaration : 1;
927 unsigned int has_type : 1;
928 unsigned int has_specification : 1;
929 unsigned int has_pc_info : 1;
481860b3 930 unsigned int may_be_inlined : 1;
72bf9492
DJ
931
932 /* Flag set if the SCOPE field of this structure has been
933 computed. */
934 unsigned int scope_set : 1;
935
fa4028e9
JB
936 /* Flag set if the DIE has a byte_size attribute. */
937 unsigned int has_byte_size : 1;
938
98bfdba5
PA
939 /* Flag set if any of the DIE's children are template arguments. */
940 unsigned int has_template_arguments : 1;
941
abc72ce4
DE
942 /* Flag set if fixup_partial_die has been called on this die. */
943 unsigned int fixup_called : 1;
944
36586728
TT
945 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
946 unsigned int is_dwz : 1;
947
948 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
949 unsigned int spec_is_dwz : 1;
950
72bf9492 951 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 952 sometimes a default name for unnamed DIEs. */
15d034d0 953 const char *name;
72bf9492 954
abc72ce4
DE
955 /* The linkage name, if present. */
956 const char *linkage_name;
957
72bf9492
DJ
958 /* The scope to prepend to our children. This is generally
959 allocated on the comp_unit_obstack, so will disappear
960 when this compilation unit leaves the cache. */
15d034d0 961 const char *scope;
72bf9492 962
95554aad
TT
963 /* Some data associated with the partial DIE. The tag determines
964 which field is live. */
965 union
966 {
967 /* The location description associated with this DIE, if any. */
968 struct dwarf_block *locdesc;
969 /* The offset of an import, for DW_TAG_imported_unit. */
970 sect_offset offset;
971 } d;
72bf9492
DJ
972
973 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
974 CORE_ADDR lowpc;
975 CORE_ADDR highpc;
72bf9492 976
93311388 977 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 978 DW_AT_sibling, if any. */
abc72ce4
DE
979 /* NOTE: This member isn't strictly necessary, read_partial_die could
980 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 981 gdb_byte *sibling;
72bf9492
DJ
982
983 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
984 DW_AT_specification (or DW_AT_abstract_origin or
985 DW_AT_extension). */
b64f50a1 986 sect_offset spec_offset;
72bf9492
DJ
987
988 /* Pointers to this DIE's parent, first child, and next sibling,
989 if any. */
990 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
991 };
992
0963b4bd 993/* This data structure holds the information of an abbrev. */
c906108c
SS
994struct abbrev_info
995 {
996 unsigned int number; /* number identifying abbrev */
997 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
998 unsigned short has_children; /* boolean */
999 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1000 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1001 struct abbrev_info *next; /* next in chain */
1002 };
1003
1004struct attr_abbrev
1005 {
9d25dd43
DE
1006 ENUM_BITFIELD(dwarf_attribute) name : 16;
1007 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1008 };
1009
433df2d4
DE
1010/* Size of abbrev_table.abbrev_hash_table. */
1011#define ABBREV_HASH_SIZE 121
1012
1013/* Top level data structure to contain an abbreviation table. */
1014
1015struct abbrev_table
1016{
f4dc4d17
DE
1017 /* Where the abbrev table came from.
1018 This is used as a sanity check when the table is used. */
433df2d4
DE
1019 sect_offset offset;
1020
1021 /* Storage for the abbrev table. */
1022 struct obstack abbrev_obstack;
1023
1024 /* Hash table of abbrevs.
1025 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1026 It could be statically allocated, but the previous code didn't so we
1027 don't either. */
1028 struct abbrev_info **abbrevs;
1029};
1030
0963b4bd 1031/* Attributes have a name and a value. */
b60c80d6
DJ
1032struct attribute
1033 {
9d25dd43 1034 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1035 ENUM_BITFIELD(dwarf_form) form : 15;
1036
1037 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1038 field should be in u.str (existing only for DW_STRING) but it is kept
1039 here for better struct attribute alignment. */
1040 unsigned int string_is_canonical : 1;
1041
b60c80d6
DJ
1042 union
1043 {
15d034d0 1044 const char *str;
b60c80d6 1045 struct dwarf_block *blk;
43bbcdc2
PH
1046 ULONGEST unsnd;
1047 LONGEST snd;
b60c80d6 1048 CORE_ADDR addr;
348e048f 1049 struct signatured_type *signatured_type;
b60c80d6
DJ
1050 }
1051 u;
1052 };
1053
0963b4bd 1054/* This data structure holds a complete die structure. */
c906108c
SS
1055struct die_info
1056 {
76815b17
DE
1057 /* DWARF-2 tag for this DIE. */
1058 ENUM_BITFIELD(dwarf_tag) tag : 16;
1059
1060 /* Number of attributes */
98bfdba5
PA
1061 unsigned char num_attrs;
1062
1063 /* True if we're presently building the full type name for the
1064 type derived from this DIE. */
1065 unsigned char building_fullname : 1;
76815b17
DE
1066
1067 /* Abbrev number */
1068 unsigned int abbrev;
1069
93311388 1070 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1071 sect_offset offset;
78ba4af6
JB
1072
1073 /* The dies in a compilation unit form an n-ary tree. PARENT
1074 points to this die's parent; CHILD points to the first child of
1075 this node; and all the children of a given node are chained
4950bc1c 1076 together via their SIBLING fields. */
639d11d3
DC
1077 struct die_info *child; /* Its first child, if any. */
1078 struct die_info *sibling; /* Its next sibling, if any. */
1079 struct die_info *parent; /* Its parent, if any. */
c906108c 1080
b60c80d6
DJ
1081 /* An array of attributes, with NUM_ATTRS elements. There may be
1082 zero, but it's not common and zero-sized arrays are not
1083 sufficiently portable C. */
1084 struct attribute attrs[1];
c906108c
SS
1085 };
1086
0963b4bd 1087/* Get at parts of an attribute structure. */
c906108c
SS
1088
1089#define DW_STRING(attr) ((attr)->u.str)
8285870a 1090#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1091#define DW_UNSND(attr) ((attr)->u.unsnd)
1092#define DW_BLOCK(attr) ((attr)->u.blk)
1093#define DW_SND(attr) ((attr)->u.snd)
1094#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1095#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1096
0963b4bd 1097/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1098struct dwarf_block
1099 {
56eb65bd 1100 size_t size;
1d6edc3c
JK
1101
1102 /* Valid only if SIZE is not zero. */
fe1b8b76 1103 gdb_byte *data;
c906108c
SS
1104 };
1105
c906108c
SS
1106#ifndef ATTR_ALLOC_CHUNK
1107#define ATTR_ALLOC_CHUNK 4
1108#endif
1109
c906108c
SS
1110/* Allocate fields for structs, unions and enums in this size. */
1111#ifndef DW_FIELD_ALLOC_CHUNK
1112#define DW_FIELD_ALLOC_CHUNK 4
1113#endif
1114
c906108c
SS
1115/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1116 but this would require a corresponding change in unpack_field_as_long
1117 and friends. */
1118static int bits_per_byte = 8;
1119
1120/* The routines that read and process dies for a C struct or C++ class
1121 pass lists of data member fields and lists of member function fields
1122 in an instance of a field_info structure, as defined below. */
1123struct field_info
c5aa993b 1124 {
0963b4bd 1125 /* List of data member and baseclasses fields. */
c5aa993b
JM
1126 struct nextfield
1127 {
1128 struct nextfield *next;
1129 int accessibility;
1130 int virtuality;
1131 struct field field;
1132 }
7d0ccb61 1133 *fields, *baseclasses;
c906108c 1134
7d0ccb61 1135 /* Number of fields (including baseclasses). */
c5aa993b 1136 int nfields;
c906108c 1137
c5aa993b
JM
1138 /* Number of baseclasses. */
1139 int nbaseclasses;
c906108c 1140
c5aa993b
JM
1141 /* Set if the accesibility of one of the fields is not public. */
1142 int non_public_fields;
c906108c 1143
c5aa993b
JM
1144 /* Member function fields array, entries are allocated in the order they
1145 are encountered in the object file. */
1146 struct nextfnfield
1147 {
1148 struct nextfnfield *next;
1149 struct fn_field fnfield;
1150 }
1151 *fnfields;
c906108c 1152
c5aa993b
JM
1153 /* Member function fieldlist array, contains name of possibly overloaded
1154 member function, number of overloaded member functions and a pointer
1155 to the head of the member function field chain. */
1156 struct fnfieldlist
1157 {
15d034d0 1158 const char *name;
c5aa993b
JM
1159 int length;
1160 struct nextfnfield *head;
1161 }
1162 *fnfieldlists;
c906108c 1163
c5aa993b
JM
1164 /* Number of entries in the fnfieldlists array. */
1165 int nfnfields;
98751a41
JK
1166
1167 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1168 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1169 struct typedef_field_list
1170 {
1171 struct typedef_field field;
1172 struct typedef_field_list *next;
1173 }
1174 *typedef_field_list;
1175 unsigned typedef_field_list_count;
c5aa993b 1176 };
c906108c 1177
10b3939b
DJ
1178/* One item on the queue of compilation units to read in full symbols
1179 for. */
1180struct dwarf2_queue_item
1181{
1182 struct dwarf2_per_cu_data *per_cu;
95554aad 1183 enum language pretend_language;
10b3939b
DJ
1184 struct dwarf2_queue_item *next;
1185};
1186
1187/* The current queue. */
1188static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1189
ae038cb0
DJ
1190/* Loaded secondary compilation units are kept in memory until they
1191 have not been referenced for the processing of this many
1192 compilation units. Set this to zero to disable caching. Cache
1193 sizes of up to at least twenty will improve startup time for
1194 typical inter-CU-reference binaries, at an obvious memory cost. */
1195static int dwarf2_max_cache_age = 5;
920d2a44
AC
1196static void
1197show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1198 struct cmd_list_element *c, const char *value)
1199{
3e43a32a
MS
1200 fprintf_filtered (file, _("The upper bound on the age of cached "
1201 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1202 value);
1203}
1204
ae038cb0 1205
0963b4bd 1206/* Various complaints about symbol reading that don't abort the process. */
c906108c 1207
4d3c2250
KB
1208static void
1209dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1210{
4d3c2250 1211 complaint (&symfile_complaints,
e2e0b3e5 1212 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1213}
1214
25e43795
DJ
1215static void
1216dwarf2_debug_line_missing_file_complaint (void)
1217{
1218 complaint (&symfile_complaints,
1219 _(".debug_line section has line data without a file"));
1220}
1221
59205f5a
JB
1222static void
1223dwarf2_debug_line_missing_end_sequence_complaint (void)
1224{
1225 complaint (&symfile_complaints,
3e43a32a
MS
1226 _(".debug_line section has line "
1227 "program sequence without an end"));
59205f5a
JB
1228}
1229
4d3c2250
KB
1230static void
1231dwarf2_complex_location_expr_complaint (void)
2e276125 1232{
e2e0b3e5 1233 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1234}
1235
4d3c2250
KB
1236static void
1237dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1238 int arg3)
2e276125 1239{
4d3c2250 1240 complaint (&symfile_complaints,
3e43a32a
MS
1241 _("const value length mismatch for '%s', got %d, expected %d"),
1242 arg1, arg2, arg3);
4d3c2250
KB
1243}
1244
1245static void
f664829e 1246dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1247{
4d3c2250 1248 complaint (&symfile_complaints,
f664829e
DE
1249 _("debug info runs off end of %s section"
1250 " [in module %s]"),
1251 section->asection->name,
1252 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1253}
1254
1255static void
1256dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1257{
4d3c2250 1258 complaint (&symfile_complaints,
3e43a32a
MS
1259 _("macro debug info contains a "
1260 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1261 arg1);
1262}
1263
1264static void
1265dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1266{
4d3c2250 1267 complaint (&symfile_complaints,
3e43a32a
MS
1268 _("invalid attribute class or form for '%s' in '%s'"),
1269 arg1, arg2);
4d3c2250 1270}
c906108c 1271
c906108c
SS
1272/* local function prototypes */
1273
4efb68b1 1274static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1275
aaa75496
JB
1276static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1277 struct objfile *);
1278
918dd910
JK
1279static void dwarf2_find_base_address (struct die_info *die,
1280 struct dwarf2_cu *cu);
1281
0018ea6f
DE
1282static struct partial_symtab *create_partial_symtab
1283 (struct dwarf2_per_cu_data *per_cu, const char *name);
1284
c67a9c90 1285static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1286
72bf9492
DJ
1287static void scan_partial_symbols (struct partial_die_info *,
1288 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1289 int, struct dwarf2_cu *);
c906108c 1290
72bf9492
DJ
1291static void add_partial_symbol (struct partial_die_info *,
1292 struct dwarf2_cu *);
63d06c5c 1293
72bf9492
DJ
1294static void add_partial_namespace (struct partial_die_info *pdi,
1295 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1296 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1297
5d7cb8df
JK
1298static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1299 CORE_ADDR *highpc, int need_pc,
1300 struct dwarf2_cu *cu);
1301
72bf9492
DJ
1302static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1303 struct dwarf2_cu *cu);
91c24f0a 1304
bc30ff58
JB
1305static void add_partial_subprogram (struct partial_die_info *pdi,
1306 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1307 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1308
257e7a09
YQ
1309static void dwarf2_read_symtab (struct partial_symtab *,
1310 struct objfile *);
c906108c 1311
a14ed312 1312static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1313
433df2d4
DE
1314static struct abbrev_info *abbrev_table_lookup_abbrev
1315 (const struct abbrev_table *, unsigned int);
1316
1317static struct abbrev_table *abbrev_table_read_table
1318 (struct dwarf2_section_info *, sect_offset);
1319
1320static void abbrev_table_free (struct abbrev_table *);
1321
f4dc4d17
DE
1322static void abbrev_table_free_cleanup (void *);
1323
dee91e82
DE
1324static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1325 struct dwarf2_section_info *);
c906108c 1326
f3dd6933 1327static void dwarf2_free_abbrev_table (void *);
c906108c 1328
6caca83c
CC
1329static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1330
dee91e82
DE
1331static struct partial_die_info *load_partial_dies
1332 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1333
dee91e82
DE
1334static gdb_byte *read_partial_die (const struct die_reader_specs *,
1335 struct partial_die_info *,
1336 struct abbrev_info *,
1337 unsigned int,
1338 gdb_byte *);
c906108c 1339
36586728 1340static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1341 struct dwarf2_cu *);
72bf9492
DJ
1342
1343static void fixup_partial_die (struct partial_die_info *,
1344 struct dwarf2_cu *);
1345
dee91e82
DE
1346static gdb_byte *read_attribute (const struct die_reader_specs *,
1347 struct attribute *, struct attr_abbrev *,
1348 gdb_byte *);
a8329558 1349
a1855c1d 1350static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1351
a1855c1d 1352static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1353
a1855c1d 1354static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1355
a1855c1d 1356static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1357
a1855c1d 1358static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1359
fe1b8b76 1360static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1361 unsigned int *);
c906108c 1362
c764a876
DE
1363static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1364
1365static LONGEST read_checked_initial_length_and_offset
1366 (bfd *, gdb_byte *, const struct comp_unit_head *,
1367 unsigned int *, unsigned int *);
613e1657 1368
fe1b8b76 1369static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1370 unsigned int *);
1371
1372static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1373
f4dc4d17
DE
1374static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1375 sect_offset);
1376
fe1b8b76 1377static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1378
9b1c24c8 1379static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1380
fe1b8b76
JB
1381static char *read_indirect_string (bfd *, gdb_byte *,
1382 const struct comp_unit_head *,
1383 unsigned int *);
4bdf3d34 1384
36586728
TT
1385static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1386
12df843f 1387static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1388
12df843f 1389static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1390
3019eac3
DE
1391static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1392 unsigned int *);
1393
1394static char *read_str_index (const struct die_reader_specs *reader,
1395 struct dwarf2_cu *cu, ULONGEST str_index);
1396
e142c38c 1397static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1398
e142c38c
DJ
1399static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1400 struct dwarf2_cu *);
c906108c 1401
348e048f 1402static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1403 unsigned int);
348e048f 1404
05cf31d1
JB
1405static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1406 struct dwarf2_cu *cu);
1407
e142c38c 1408static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1409
e142c38c 1410static struct die_info *die_specification (struct die_info *die,
f2f0e013 1411 struct dwarf2_cu **);
63d06c5c 1412
debd256d
JB
1413static void free_line_header (struct line_header *lh);
1414
aaa75496
JB
1415static void add_file_name (struct line_header *, char *, unsigned int,
1416 unsigned int, unsigned int);
1417
3019eac3
DE
1418static struct line_header *dwarf_decode_line_header (unsigned int offset,
1419 struct dwarf2_cu *cu);
debd256d 1420
f3f5162e
DE
1421static void dwarf_decode_lines (struct line_header *, const char *,
1422 struct dwarf2_cu *, struct partial_symtab *,
1423 int);
c906108c 1424
72b9f47f 1425static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1426
f4dc4d17 1427static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1428 const char *, const char *, CORE_ADDR);
f4dc4d17 1429
a14ed312 1430static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1431 struct dwarf2_cu *);
c906108c 1432
34eaf542
TT
1433static struct symbol *new_symbol_full (struct die_info *, struct type *,
1434 struct dwarf2_cu *, struct symbol *);
1435
a14ed312 1436static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1437 struct dwarf2_cu *);
c906108c 1438
98bfdba5
PA
1439static void dwarf2_const_value_attr (struct attribute *attr,
1440 struct type *type,
1441 const char *name,
1442 struct obstack *obstack,
12df843f 1443 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1444 gdb_byte **bytes,
1445 struct dwarf2_locexpr_baton **baton);
2df3850c 1446
e7c27a73 1447static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1448
b4ba55a1
JB
1449static int need_gnat_info (struct dwarf2_cu *);
1450
3e43a32a
MS
1451static struct type *die_descriptive_type (struct die_info *,
1452 struct dwarf2_cu *);
b4ba55a1
JB
1453
1454static void set_descriptive_type (struct type *, struct die_info *,
1455 struct dwarf2_cu *);
1456
e7c27a73
DJ
1457static struct type *die_containing_type (struct die_info *,
1458 struct dwarf2_cu *);
c906108c 1459
673bfd45
DE
1460static struct type *lookup_die_type (struct die_info *, struct attribute *,
1461 struct dwarf2_cu *);
c906108c 1462
f792889a 1463static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1464
673bfd45
DE
1465static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1466
0d5cff50 1467static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1468
6e70227d 1469static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1470 const char *suffix, int physname,
1471 struct dwarf2_cu *cu);
63d06c5c 1472
e7c27a73 1473static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1474
348e048f
DE
1475static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1476
e7c27a73 1477static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1478
e7c27a73 1479static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1480
96408a79
SA
1481static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1482
ff013f42
JK
1483static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1484 struct dwarf2_cu *, struct partial_symtab *);
1485
a14ed312 1486static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1487 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1488 struct partial_symtab *);
c906108c 1489
fae299cd
DC
1490static void get_scope_pc_bounds (struct die_info *,
1491 CORE_ADDR *, CORE_ADDR *,
1492 struct dwarf2_cu *);
1493
801e3a5b
JB
1494static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1495 CORE_ADDR, struct dwarf2_cu *);
1496
a14ed312 1497static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1498 struct dwarf2_cu *);
c906108c 1499
a14ed312 1500static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1501 struct type *, struct dwarf2_cu *);
c906108c 1502
a14ed312 1503static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1504 struct die_info *, struct type *,
e7c27a73 1505 struct dwarf2_cu *);
c906108c 1506
a14ed312 1507static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1508 struct type *,
1509 struct dwarf2_cu *);
c906108c 1510
134d01f1 1511static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1512
e7c27a73 1513static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1514
e7c27a73 1515static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1516
5d7cb8df
JK
1517static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1518
27aa8d6a
SW
1519static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1520
f55ee35c
JK
1521static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
38d518c9 1524static const char *namespace_name (struct die_info *die,
e142c38c 1525 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1526
134d01f1 1527static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1528
e7c27a73 1529static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1530
6e70227d 1531static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1532 struct dwarf2_cu *);
1533
dee91e82 1534static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1535 gdb_byte *info_ptr,
fe1b8b76 1536 gdb_byte **new_info_ptr,
639d11d3
DC
1537 struct die_info *parent);
1538
dee91e82 1539static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1540 gdb_byte *info_ptr,
fe1b8b76 1541 gdb_byte **new_info_ptr,
639d11d3
DC
1542 struct die_info *parent);
1543
3019eac3
DE
1544static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, gdb_byte *, int *, int);
1546
dee91e82
DE
1547static gdb_byte *read_full_die (const struct die_reader_specs *,
1548 struct die_info **, gdb_byte *, int *);
93311388 1549
e7c27a73 1550static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1551
15d034d0
TT
1552static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1553 struct obstack *);
71c25dea 1554
15d034d0 1555static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1556
15d034d0 1557static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1558 struct die_info *die,
1559 struct dwarf2_cu *cu);
1560
ca69b9e6
DE
1561static const char *dwarf2_physname (const char *name, struct die_info *die,
1562 struct dwarf2_cu *cu);
1563
e142c38c 1564static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1565 struct dwarf2_cu **);
9219021c 1566
f39c6ffd 1567static const char *dwarf_tag_name (unsigned int);
c906108c 1568
f39c6ffd 1569static const char *dwarf_attr_name (unsigned int);
c906108c 1570
f39c6ffd 1571static const char *dwarf_form_name (unsigned int);
c906108c 1572
a14ed312 1573static char *dwarf_bool_name (unsigned int);
c906108c 1574
f39c6ffd 1575static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1576
f9aca02d 1577static struct die_info *sibling_die (struct die_info *);
c906108c 1578
d97bc12b
DE
1579static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1580
1581static void dump_die_for_error (struct die_info *);
1582
1583static void dump_die_1 (struct ui_file *, int level, int max_level,
1584 struct die_info *);
c906108c 1585
d97bc12b 1586/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1587
51545339 1588static void store_in_ref_table (struct die_info *,
10b3939b 1589 struct dwarf2_cu *);
c906108c 1590
93311388
DE
1591static int is_ref_attr (struct attribute *);
1592
b64f50a1 1593static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1594
43bbcdc2 1595static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1596
348e048f
DE
1597static struct die_info *follow_die_ref_or_sig (struct die_info *,
1598 struct attribute *,
1599 struct dwarf2_cu **);
1600
10b3939b
DJ
1601static struct die_info *follow_die_ref (struct die_info *,
1602 struct attribute *,
f2f0e013 1603 struct dwarf2_cu **);
c906108c 1604
348e048f
DE
1605static struct die_info *follow_die_sig (struct die_info *,
1606 struct attribute *,
1607 struct dwarf2_cu **);
1608
6c83ed52
TT
1609static struct signatured_type *lookup_signatured_type_at_offset
1610 (struct objfile *objfile,
b64f50a1 1611 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1612
e5fe5e75 1613static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1614
52dc124a 1615static void read_signatured_type (struct signatured_type *);
348e048f 1616
f4dc4d17 1617static struct type_unit_group *get_type_unit_group
094b34ac 1618 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1619
1620static void build_type_unit_groups (die_reader_func_ftype *, void *);
1621
c906108c
SS
1622/* memory allocation interface */
1623
7b5a2f43 1624static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1625
b60c80d6 1626static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1627
09262596 1628static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1629 const char *, int);
2e276125 1630
8e19ed76
PS
1631static int attr_form_is_block (struct attribute *);
1632
3690dd37
JB
1633static int attr_form_is_section_offset (struct attribute *);
1634
1635static int attr_form_is_constant (struct attribute *);
1636
8cf6f0b1
TT
1637static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1638 struct dwarf2_loclist_baton *baton,
1639 struct attribute *attr);
1640
93e7bd98
DJ
1641static void dwarf2_symbol_mark_computed (struct attribute *attr,
1642 struct symbol *sym,
f1e6e072
TT
1643 struct dwarf2_cu *cu,
1644 int is_block);
4c2df51b 1645
dee91e82
DE
1646static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1647 gdb_byte *info_ptr,
1648 struct abbrev_info *abbrev);
4bb7a0a7 1649
72bf9492
DJ
1650static void free_stack_comp_unit (void *);
1651
72bf9492
DJ
1652static hashval_t partial_die_hash (const void *item);
1653
1654static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1655
ae038cb0 1656static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1657 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1658
9816fde3 1659static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1660 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1661
1662static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1663 struct die_info *comp_unit_die,
1664 enum language pretend_language);
93311388 1665
68dc6402 1666static void free_heap_comp_unit (void *);
ae038cb0
DJ
1667
1668static void free_cached_comp_units (void *);
1669
1670static void age_cached_comp_units (void);
1671
dee91e82 1672static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1673
f792889a
DJ
1674static struct type *set_die_type (struct die_info *, struct type *,
1675 struct dwarf2_cu *);
1c379e20 1676
ae038cb0
DJ
1677static void create_all_comp_units (struct objfile *);
1678
0e50663e 1679static int create_all_type_units (struct objfile *);
1fd400ff 1680
95554aad
TT
1681static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1682 enum language);
10b3939b 1683
95554aad
TT
1684static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1685 enum language);
10b3939b 1686
f4dc4d17
DE
1687static void process_full_type_unit (struct dwarf2_per_cu_data *,
1688 enum language);
1689
10b3939b
DJ
1690static void dwarf2_add_dependence (struct dwarf2_cu *,
1691 struct dwarf2_per_cu_data *);
1692
ae038cb0
DJ
1693static void dwarf2_mark (struct dwarf2_cu *);
1694
1695static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1696
b64f50a1 1697static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1698 struct dwarf2_per_cu_data *per_cu);
1699
f792889a 1700static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1701
9291a0cd
TT
1702static void dwarf2_release_queue (void *dummy);
1703
95554aad
TT
1704static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1705 enum language pretend_language);
1706
1707static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1708 struct dwarf2_per_cu_data *per_cu,
1709 enum language pretend_language);
9291a0cd 1710
a0f42c21 1711static void process_queue (void);
9291a0cd
TT
1712
1713static void find_file_and_directory (struct die_info *die,
1714 struct dwarf2_cu *cu,
15d034d0 1715 const char **name, const char **comp_dir);
9291a0cd
TT
1716
1717static char *file_full_name (int file, struct line_header *lh,
1718 const char *comp_dir);
1719
36586728
TT
1720static gdb_byte *read_and_check_comp_unit_head
1721 (struct comp_unit_head *header,
1722 struct dwarf2_section_info *section,
1723 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1724 int is_debug_types_section);
1725
fd820528 1726static void init_cutu_and_read_dies
f4dc4d17
DE
1727 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1728 int use_existing_cu, int keep,
3019eac3
DE
1729 die_reader_func_ftype *die_reader_func, void *data);
1730
dee91e82
DE
1731static void init_cutu_and_read_dies_simple
1732 (struct dwarf2_per_cu_data *this_cu,
1733 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1734
673bfd45 1735static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1736
3019eac3
DE
1737static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1738
1739static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1740 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1741
1742static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1743 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1744
1745static void free_dwo_file_cleanup (void *);
1746
95554aad
TT
1747static void process_cu_includes (void);
1748
1b80a9fa
JK
1749static void check_producer (struct dwarf2_cu *cu);
1750
9291a0cd
TT
1751#if WORDS_BIGENDIAN
1752
1753/* Convert VALUE between big- and little-endian. */
1754static offset_type
1755byte_swap (offset_type value)
1756{
1757 offset_type result;
1758
1759 result = (value & 0xff) << 24;
1760 result |= (value & 0xff00) << 8;
1761 result |= (value & 0xff0000) >> 8;
1762 result |= (value & 0xff000000) >> 24;
1763 return result;
1764}
1765
1766#define MAYBE_SWAP(V) byte_swap (V)
1767
1768#else
1769#define MAYBE_SWAP(V) (V)
1770#endif /* WORDS_BIGENDIAN */
1771
1772/* The suffix for an index file. */
1773#define INDEX_SUFFIX ".gdb-index"
1774
c906108c 1775/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1776 information and return true if we have enough to do something.
1777 NAMES points to the dwarf2 section names, or is NULL if the standard
1778 ELF names are used. */
c906108c
SS
1779
1780int
251d32d9
TG
1781dwarf2_has_info (struct objfile *objfile,
1782 const struct dwarf2_debug_sections *names)
c906108c 1783{
be391dca
TT
1784 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1785 if (!dwarf2_per_objfile)
1786 {
1787 /* Initialize per-objfile state. */
1788 struct dwarf2_per_objfile *data
1789 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1790
be391dca
TT
1791 memset (data, 0, sizeof (*data));
1792 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1793 dwarf2_per_objfile = data;
6502dd73 1794
251d32d9
TG
1795 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1796 (void *) names);
be391dca
TT
1797 dwarf2_per_objfile->objfile = objfile;
1798 }
1799 return (dwarf2_per_objfile->info.asection != NULL
1800 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1801}
1802
251d32d9
TG
1803/* When loading sections, we look either for uncompressed section or for
1804 compressed section names. */
233a11ab
CS
1805
1806static int
251d32d9
TG
1807section_is_p (const char *section_name,
1808 const struct dwarf2_section_names *names)
233a11ab 1809{
251d32d9
TG
1810 if (names->normal != NULL
1811 && strcmp (section_name, names->normal) == 0)
1812 return 1;
1813 if (names->compressed != NULL
1814 && strcmp (section_name, names->compressed) == 0)
1815 return 1;
1816 return 0;
233a11ab
CS
1817}
1818
c906108c
SS
1819/* This function is mapped across the sections and remembers the
1820 offset and size of each of the debugging sections we are interested
1821 in. */
1822
1823static void
251d32d9 1824dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1825{
251d32d9 1826 const struct dwarf2_debug_sections *names;
dc7650b8 1827 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1828
1829 if (vnames == NULL)
1830 names = &dwarf2_elf_names;
1831 else
1832 names = (const struct dwarf2_debug_sections *) vnames;
1833
dc7650b8
JK
1834 if ((aflag & SEC_HAS_CONTENTS) == 0)
1835 {
1836 }
1837 else if (section_is_p (sectp->name, &names->info))
c906108c 1838 {
dce234bc
PP
1839 dwarf2_per_objfile->info.asection = sectp;
1840 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1841 }
251d32d9 1842 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1843 {
dce234bc
PP
1844 dwarf2_per_objfile->abbrev.asection = sectp;
1845 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1846 }
251d32d9 1847 else if (section_is_p (sectp->name, &names->line))
c906108c 1848 {
dce234bc
PP
1849 dwarf2_per_objfile->line.asection = sectp;
1850 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1851 }
251d32d9 1852 else if (section_is_p (sectp->name, &names->loc))
c906108c 1853 {
dce234bc
PP
1854 dwarf2_per_objfile->loc.asection = sectp;
1855 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1856 }
251d32d9 1857 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1858 {
dce234bc
PP
1859 dwarf2_per_objfile->macinfo.asection = sectp;
1860 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1861 }
cf2c3c16
TT
1862 else if (section_is_p (sectp->name, &names->macro))
1863 {
1864 dwarf2_per_objfile->macro.asection = sectp;
1865 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1866 }
251d32d9 1867 else if (section_is_p (sectp->name, &names->str))
c906108c 1868 {
dce234bc
PP
1869 dwarf2_per_objfile->str.asection = sectp;
1870 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1871 }
3019eac3
DE
1872 else if (section_is_p (sectp->name, &names->addr))
1873 {
1874 dwarf2_per_objfile->addr.asection = sectp;
1875 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1876 }
251d32d9 1877 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1878 {
dce234bc
PP
1879 dwarf2_per_objfile->frame.asection = sectp;
1880 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1881 }
251d32d9 1882 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1883 {
dc7650b8
JK
1884 dwarf2_per_objfile->eh_frame.asection = sectp;
1885 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1886 }
251d32d9 1887 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1888 {
dce234bc
PP
1889 dwarf2_per_objfile->ranges.asection = sectp;
1890 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1891 }
251d32d9 1892 else if (section_is_p (sectp->name, &names->types))
348e048f 1893 {
8b70b953
TT
1894 struct dwarf2_section_info type_section;
1895
1896 memset (&type_section, 0, sizeof (type_section));
1897 type_section.asection = sectp;
1898 type_section.size = bfd_get_section_size (sectp);
1899
1900 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1901 &type_section);
348e048f 1902 }
251d32d9 1903 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1904 {
1905 dwarf2_per_objfile->gdb_index.asection = sectp;
1906 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1907 }
dce234bc 1908
72dca2f5
FR
1909 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1910 && bfd_section_vma (abfd, sectp) == 0)
1911 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1912}
1913
fceca515
DE
1914/* A helper function that decides whether a section is empty,
1915 or not present. */
9e0ac564
TT
1916
1917static int
1918dwarf2_section_empty_p (struct dwarf2_section_info *info)
1919{
1920 return info->asection == NULL || info->size == 0;
1921}
1922
3019eac3
DE
1923/* Read the contents of the section INFO.
1924 OBJFILE is the main object file, but not necessarily the file where
1925 the section comes from. E.g., for DWO files INFO->asection->owner
1926 is the bfd of the DWO file.
dce234bc 1927 If the section is compressed, uncompress it before returning. */
c906108c 1928
dce234bc
PP
1929static void
1930dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1931{
dce234bc 1932 asection *sectp = info->asection;
3019eac3 1933 bfd *abfd;
dce234bc
PP
1934 gdb_byte *buf, *retbuf;
1935 unsigned char header[4];
c906108c 1936
be391dca
TT
1937 if (info->readin)
1938 return;
dce234bc 1939 info->buffer = NULL;
be391dca 1940 info->readin = 1;
188dd5d6 1941
9e0ac564 1942 if (dwarf2_section_empty_p (info))
dce234bc 1943 return;
c906108c 1944
3019eac3
DE
1945 abfd = sectp->owner;
1946
4bf44c1c
TT
1947 /* If the section has relocations, we must read it ourselves.
1948 Otherwise we attach it to the BFD. */
1949 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1950 {
4bf44c1c 1951 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1952
4bf44c1c
TT
1953 /* We have to cast away const here for historical reasons.
1954 Fixing dwarf2read to be const-correct would be quite nice. */
1955 info->buffer = (gdb_byte *) bytes;
1956 return;
dce234bc 1957 }
dce234bc 1958
4bf44c1c
TT
1959 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1960 info->buffer = buf;
dce234bc
PP
1961
1962 /* When debugging .o files, we may need to apply relocations; see
1963 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1964 We never compress sections in .o files, so we only need to
1965 try this when the section is not compressed. */
ac8035ab 1966 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1967 if (retbuf != NULL)
1968 {
1969 info->buffer = retbuf;
1970 return;
1971 }
1972
1973 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1974 || bfd_bread (buf, info->size, abfd) != info->size)
1975 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1976 bfd_get_filename (abfd));
1977}
1978
9e0ac564
TT
1979/* A helper function that returns the size of a section in a safe way.
1980 If you are positive that the section has been read before using the
1981 size, then it is safe to refer to the dwarf2_section_info object's
1982 "size" field directly. In other cases, you must call this
1983 function, because for compressed sections the size field is not set
1984 correctly until the section has been read. */
1985
1986static bfd_size_type
1987dwarf2_section_size (struct objfile *objfile,
1988 struct dwarf2_section_info *info)
1989{
1990 if (!info->readin)
1991 dwarf2_read_section (objfile, info);
1992 return info->size;
1993}
1994
dce234bc 1995/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1996 SECTION_NAME. */
af34e669 1997
dce234bc 1998void
3017a003
TG
1999dwarf2_get_section_info (struct objfile *objfile,
2000 enum dwarf2_section_enum sect,
dce234bc
PP
2001 asection **sectp, gdb_byte **bufp,
2002 bfd_size_type *sizep)
2003{
2004 struct dwarf2_per_objfile *data
2005 = objfile_data (objfile, dwarf2_objfile_data_key);
2006 struct dwarf2_section_info *info;
a3b2a86b
TT
2007
2008 /* We may see an objfile without any DWARF, in which case we just
2009 return nothing. */
2010 if (data == NULL)
2011 {
2012 *sectp = NULL;
2013 *bufp = NULL;
2014 *sizep = 0;
2015 return;
2016 }
3017a003
TG
2017 switch (sect)
2018 {
2019 case DWARF2_DEBUG_FRAME:
2020 info = &data->frame;
2021 break;
2022 case DWARF2_EH_FRAME:
2023 info = &data->eh_frame;
2024 break;
2025 default:
2026 gdb_assert_not_reached ("unexpected section");
2027 }
dce234bc 2028
9e0ac564 2029 dwarf2_read_section (objfile, info);
dce234bc
PP
2030
2031 *sectp = info->asection;
2032 *bufp = info->buffer;
2033 *sizep = info->size;
2034}
2035
36586728
TT
2036/* A helper function to find the sections for a .dwz file. */
2037
2038static void
2039locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2040{
2041 struct dwz_file *dwz_file = arg;
2042
2043 /* Note that we only support the standard ELF names, because .dwz
2044 is ELF-only (at the time of writing). */
2045 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2046 {
2047 dwz_file->abbrev.asection = sectp;
2048 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2049 }
2050 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2051 {
2052 dwz_file->info.asection = sectp;
2053 dwz_file->info.size = bfd_get_section_size (sectp);
2054 }
2055 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2056 {
2057 dwz_file->str.asection = sectp;
2058 dwz_file->str.size = bfd_get_section_size (sectp);
2059 }
2060 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2061 {
2062 dwz_file->line.asection = sectp;
2063 dwz_file->line.size = bfd_get_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2066 {
2067 dwz_file->macro.asection = sectp;
2068 dwz_file->macro.size = bfd_get_section_size (sectp);
2069 }
2ec9a5e0
TT
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2071 {
2072 dwz_file->gdb_index.asection = sectp;
2073 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2074 }
36586728
TT
2075}
2076
2077/* Open the separate '.dwz' debug file, if needed. Error if the file
2078 cannot be found. */
2079
2080static struct dwz_file *
2081dwarf2_get_dwz_file (void)
2082{
2083 bfd *abfd, *dwz_bfd;
2084 asection *section;
2085 gdb_byte *data;
2086 struct cleanup *cleanup;
2087 const char *filename;
2088 struct dwz_file *result;
2089
2090 if (dwarf2_per_objfile->dwz_file != NULL)
2091 return dwarf2_per_objfile->dwz_file;
2092
2093 abfd = dwarf2_per_objfile->objfile->obfd;
2094 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2095 if (section == NULL)
2096 error (_("could not find '.gnu_debugaltlink' section"));
2097 if (!bfd_malloc_and_get_section (abfd, section, &data))
2098 error (_("could not read '.gnu_debugaltlink' section: %s"),
2099 bfd_errmsg (bfd_get_error ()));
2100 cleanup = make_cleanup (xfree, data);
2101
2102 filename = data;
2103 if (!IS_ABSOLUTE_PATH (filename))
2104 {
2105 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2106 char *rel;
2107
2108 make_cleanup (xfree, abs);
2109 abs = ldirname (abs);
2110 make_cleanup (xfree, abs);
2111
2112 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2113 make_cleanup (xfree, rel);
2114 filename = rel;
2115 }
2116
2117 /* The format is just a NUL-terminated file name, followed by the
2118 build-id. For now, though, we ignore the build-id. */
2119 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2120 if (dwz_bfd == NULL)
2121 error (_("could not read '%s': %s"), filename,
2122 bfd_errmsg (bfd_get_error ()));
2123
2124 if (!bfd_check_format (dwz_bfd, bfd_object))
2125 {
2126 gdb_bfd_unref (dwz_bfd);
2127 error (_("file '%s' was not usable: %s"), filename,
2128 bfd_errmsg (bfd_get_error ()));
2129 }
2130
2131 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2132 struct dwz_file);
2133 result->dwz_bfd = dwz_bfd;
2134
2135 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2136
2137 do_cleanups (cleanup);
2138
8d2cc612 2139 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2140 return result;
2141}
9291a0cd 2142\f
7b9f3c50
DE
2143/* DWARF quick_symbols_functions support. */
2144
2145/* TUs can share .debug_line entries, and there can be a lot more TUs than
2146 unique line tables, so we maintain a separate table of all .debug_line
2147 derived entries to support the sharing.
2148 All the quick functions need is the list of file names. We discard the
2149 line_header when we're done and don't need to record it here. */
2150struct quick_file_names
2151{
094b34ac
DE
2152 /* The data used to construct the hash key. */
2153 struct stmt_list_hash hash;
7b9f3c50
DE
2154
2155 /* The number of entries in file_names, real_names. */
2156 unsigned int num_file_names;
2157
2158 /* The file names from the line table, after being run through
2159 file_full_name. */
2160 const char **file_names;
2161
2162 /* The file names from the line table after being run through
2163 gdb_realpath. These are computed lazily. */
2164 const char **real_names;
2165};
2166
2167/* When using the index (and thus not using psymtabs), each CU has an
2168 object of this type. This is used to hold information needed by
2169 the various "quick" methods. */
2170struct dwarf2_per_cu_quick_data
2171{
2172 /* The file table. This can be NULL if there was no file table
2173 or it's currently not read in.
2174 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2175 struct quick_file_names *file_names;
2176
2177 /* The corresponding symbol table. This is NULL if symbols for this
2178 CU have not yet been read. */
2179 struct symtab *symtab;
2180
2181 /* A temporary mark bit used when iterating over all CUs in
2182 expand_symtabs_matching. */
2183 unsigned int mark : 1;
2184
2185 /* True if we've tried to read the file table and found there isn't one.
2186 There will be no point in trying to read it again next time. */
2187 unsigned int no_file_data : 1;
2188};
2189
094b34ac
DE
2190/* Utility hash function for a stmt_list_hash. */
2191
2192static hashval_t
2193hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2194{
2195 hashval_t v = 0;
2196
2197 if (stmt_list_hash->dwo_unit != NULL)
2198 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2199 v += stmt_list_hash->line_offset.sect_off;
2200 return v;
2201}
2202
2203/* Utility equality function for a stmt_list_hash. */
2204
2205static int
2206eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2207 const struct stmt_list_hash *rhs)
2208{
2209 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2210 return 0;
2211 if (lhs->dwo_unit != NULL
2212 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2213 return 0;
2214
2215 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2216}
2217
7b9f3c50
DE
2218/* Hash function for a quick_file_names. */
2219
2220static hashval_t
2221hash_file_name_entry (const void *e)
2222{
2223 const struct quick_file_names *file_data = e;
2224
094b34ac 2225 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2226}
2227
2228/* Equality function for a quick_file_names. */
2229
2230static int
2231eq_file_name_entry (const void *a, const void *b)
2232{
2233 const struct quick_file_names *ea = a;
2234 const struct quick_file_names *eb = b;
2235
094b34ac 2236 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2237}
2238
2239/* Delete function for a quick_file_names. */
2240
2241static void
2242delete_file_name_entry (void *e)
2243{
2244 struct quick_file_names *file_data = e;
2245 int i;
2246
2247 for (i = 0; i < file_data->num_file_names; ++i)
2248 {
2249 xfree ((void*) file_data->file_names[i]);
2250 if (file_data->real_names)
2251 xfree ((void*) file_data->real_names[i]);
2252 }
2253
2254 /* The space for the struct itself lives on objfile_obstack,
2255 so we don't free it here. */
2256}
2257
2258/* Create a quick_file_names hash table. */
2259
2260static htab_t
2261create_quick_file_names_table (unsigned int nr_initial_entries)
2262{
2263 return htab_create_alloc (nr_initial_entries,
2264 hash_file_name_entry, eq_file_name_entry,
2265 delete_file_name_entry, xcalloc, xfree);
2266}
9291a0cd 2267
918dd910
JK
2268/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2269 have to be created afterwards. You should call age_cached_comp_units after
2270 processing PER_CU->CU. dw2_setup must have been already called. */
2271
2272static void
2273load_cu (struct dwarf2_per_cu_data *per_cu)
2274{
3019eac3 2275 if (per_cu->is_debug_types)
e5fe5e75 2276 load_full_type_unit (per_cu);
918dd910 2277 else
95554aad 2278 load_full_comp_unit (per_cu, language_minimal);
918dd910 2279
918dd910 2280 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2281
2282 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2283}
2284
a0f42c21 2285/* Read in the symbols for PER_CU. */
2fdf6df6 2286
9291a0cd 2287static void
a0f42c21 2288dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2289{
2290 struct cleanup *back_to;
2291
f4dc4d17
DE
2292 /* Skip type_unit_groups, reading the type units they contain
2293 is handled elsewhere. */
2294 if (IS_TYPE_UNIT_GROUP (per_cu))
2295 return;
2296
9291a0cd
TT
2297 back_to = make_cleanup (dwarf2_release_queue, NULL);
2298
95554aad
TT
2299 if (dwarf2_per_objfile->using_index
2300 ? per_cu->v.quick->symtab == NULL
2301 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2302 {
2303 queue_comp_unit (per_cu, language_minimal);
2304 load_cu (per_cu);
2305 }
9291a0cd 2306
a0f42c21 2307 process_queue ();
9291a0cd
TT
2308
2309 /* Age the cache, releasing compilation units that have not
2310 been used recently. */
2311 age_cached_comp_units ();
2312
2313 do_cleanups (back_to);
2314}
2315
2316/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2317 the objfile from which this CU came. Returns the resulting symbol
2318 table. */
2fdf6df6 2319
9291a0cd 2320static struct symtab *
a0f42c21 2321dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2322{
95554aad 2323 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2324 if (!per_cu->v.quick->symtab)
2325 {
2326 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2327 increment_reading_symtab ();
a0f42c21 2328 dw2_do_instantiate_symtab (per_cu);
95554aad 2329 process_cu_includes ();
9291a0cd
TT
2330 do_cleanups (back_to);
2331 }
2332 return per_cu->v.quick->symtab;
2333}
2334
f4dc4d17
DE
2335/* Return the CU given its index.
2336
2337 This is intended for loops like:
2338
2339 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2340 + dwarf2_per_objfile->n_type_units); ++i)
2341 {
2342 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2343
2344 ...;
2345 }
2346*/
2fdf6df6 2347
1fd400ff
TT
2348static struct dwarf2_per_cu_data *
2349dw2_get_cu (int index)
2350{
2351 if (index >= dwarf2_per_objfile->n_comp_units)
2352 {
f4dc4d17 2353 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2354 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2355 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2356 }
2357
2358 return dwarf2_per_objfile->all_comp_units[index];
2359}
2360
2361/* Return the primary CU given its index.
2362 The difference between this function and dw2_get_cu is in the handling
2363 of type units (TUs). Here we return the type_unit_group object.
2364
2365 This is intended for loops like:
2366
2367 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2368 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2369 {
2370 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2371
2372 ...;
2373 }
2374*/
2375
2376static struct dwarf2_per_cu_data *
2377dw2_get_primary_cu (int index)
2378{
2379 if (index >= dwarf2_per_objfile->n_comp_units)
2380 {
1fd400ff 2381 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2382 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2383 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2384 }
f4dc4d17 2385
1fd400ff
TT
2386 return dwarf2_per_objfile->all_comp_units[index];
2387}
2388
2ec9a5e0
TT
2389/* A helper for create_cus_from_index that handles a given list of
2390 CUs. */
2fdf6df6 2391
74a0d9f6 2392static void
2ec9a5e0
TT
2393create_cus_from_index_list (struct objfile *objfile,
2394 const gdb_byte *cu_list, offset_type n_elements,
2395 struct dwarf2_section_info *section,
2396 int is_dwz,
2397 int base_offset)
9291a0cd
TT
2398{
2399 offset_type i;
9291a0cd 2400
2ec9a5e0 2401 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2402 {
2403 struct dwarf2_per_cu_data *the_cu;
2404 ULONGEST offset, length;
2405
74a0d9f6
JK
2406 gdb_static_assert (sizeof (ULONGEST) >= 8);
2407 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2408 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2409 cu_list += 2 * 8;
2410
2411 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2412 struct dwarf2_per_cu_data);
b64f50a1 2413 the_cu->offset.sect_off = offset;
9291a0cd
TT
2414 the_cu->length = length;
2415 the_cu->objfile = objfile;
8a0459fd 2416 the_cu->section = section;
9291a0cd
TT
2417 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2418 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2419 the_cu->is_dwz = is_dwz;
2420 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2421 }
9291a0cd
TT
2422}
2423
2ec9a5e0 2424/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2425 the CU objects for this objfile. */
2ec9a5e0 2426
74a0d9f6 2427static void
2ec9a5e0
TT
2428create_cus_from_index (struct objfile *objfile,
2429 const gdb_byte *cu_list, offset_type cu_list_elements,
2430 const gdb_byte *dwz_list, offset_type dwz_elements)
2431{
2432 struct dwz_file *dwz;
2433
2434 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2435 dwarf2_per_objfile->all_comp_units
2436 = obstack_alloc (&objfile->objfile_obstack,
2437 dwarf2_per_objfile->n_comp_units
2438 * sizeof (struct dwarf2_per_cu_data *));
2439
74a0d9f6
JK
2440 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2441 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2442
2443 if (dwz_elements == 0)
74a0d9f6 2444 return;
2ec9a5e0
TT
2445
2446 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2447 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2448 cu_list_elements / 2);
2ec9a5e0
TT
2449}
2450
1fd400ff 2451/* Create the signatured type hash table from the index. */
673bfd45 2452
74a0d9f6 2453static void
673bfd45 2454create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2455 struct dwarf2_section_info *section,
673bfd45
DE
2456 const gdb_byte *bytes,
2457 offset_type elements)
1fd400ff
TT
2458{
2459 offset_type i;
673bfd45 2460 htab_t sig_types_hash;
1fd400ff 2461
d467dd73
DE
2462 dwarf2_per_objfile->n_type_units = elements / 3;
2463 dwarf2_per_objfile->all_type_units
1fd400ff 2464 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2465 dwarf2_per_objfile->n_type_units
b4dd5633 2466 * sizeof (struct signatured_type *));
1fd400ff 2467
673bfd45 2468 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2469
2470 for (i = 0; i < elements; i += 3)
2471 {
52dc124a
DE
2472 struct signatured_type *sig_type;
2473 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2474 void **slot;
2475
74a0d9f6
JK
2476 gdb_static_assert (sizeof (ULONGEST) >= 8);
2477 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2478 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2479 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2480 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2481 bytes += 3 * 8;
2482
52dc124a 2483 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2484 struct signatured_type);
52dc124a 2485 sig_type->signature = signature;
3019eac3
DE
2486 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2487 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2488 sig_type->per_cu.section = section;
52dc124a
DE
2489 sig_type->per_cu.offset.sect_off = offset;
2490 sig_type->per_cu.objfile = objfile;
2491 sig_type->per_cu.v.quick
1fd400ff
TT
2492 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2493 struct dwarf2_per_cu_quick_data);
2494
52dc124a
DE
2495 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2496 *slot = sig_type;
1fd400ff 2497
b4dd5633 2498 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2499 }
2500
673bfd45 2501 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2502}
2503
9291a0cd
TT
2504/* Read the address map data from the mapped index, and use it to
2505 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2506
9291a0cd
TT
2507static void
2508create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2509{
2510 const gdb_byte *iter, *end;
2511 struct obstack temp_obstack;
2512 struct addrmap *mutable_map;
2513 struct cleanup *cleanup;
2514 CORE_ADDR baseaddr;
2515
2516 obstack_init (&temp_obstack);
2517 cleanup = make_cleanup_obstack_free (&temp_obstack);
2518 mutable_map = addrmap_create_mutable (&temp_obstack);
2519
2520 iter = index->address_table;
2521 end = iter + index->address_table_size;
2522
2523 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2524
2525 while (iter < end)
2526 {
2527 ULONGEST hi, lo, cu_index;
2528 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2529 iter += 8;
2530 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2531 iter += 8;
2532 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2533 iter += 4;
f652bce2
DE
2534
2535 if (cu_index < dwarf2_per_objfile->n_comp_units)
2536 {
2537 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2538 dw2_get_cu (cu_index));
2539 }
2540 else
2541 {
2542 complaint (&symfile_complaints,
2543 _(".gdb_index address table has invalid CU number %u"),
2544 (unsigned) cu_index);
2545 }
9291a0cd
TT
2546 }
2547
2548 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2549 &objfile->objfile_obstack);
2550 do_cleanups (cleanup);
2551}
2552
59d7bcaf
JK
2553/* The hash function for strings in the mapped index. This is the same as
2554 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2555 implementation. This is necessary because the hash function is tied to the
2556 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2557 SYMBOL_HASH_NEXT.
2558
2559 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2560
9291a0cd 2561static hashval_t
559a7a62 2562mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2563{
2564 const unsigned char *str = (const unsigned char *) p;
2565 hashval_t r = 0;
2566 unsigned char c;
2567
2568 while ((c = *str++) != 0)
559a7a62
JK
2569 {
2570 if (index_version >= 5)
2571 c = tolower (c);
2572 r = r * 67 + c - 113;
2573 }
9291a0cd
TT
2574
2575 return r;
2576}
2577
2578/* Find a slot in the mapped index INDEX for the object named NAME.
2579 If NAME is found, set *VEC_OUT to point to the CU vector in the
2580 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2581
9291a0cd
TT
2582static int
2583find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2584 offset_type **vec_out)
2585{
0cf03b49
JK
2586 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2587 offset_type hash;
9291a0cd 2588 offset_type slot, step;
559a7a62 2589 int (*cmp) (const char *, const char *);
9291a0cd 2590
0cf03b49
JK
2591 if (current_language->la_language == language_cplus
2592 || current_language->la_language == language_java
2593 || current_language->la_language == language_fortran)
2594 {
2595 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2596 not contain any. */
2597 const char *paren = strchr (name, '(');
2598
2599 if (paren)
2600 {
2601 char *dup;
2602
2603 dup = xmalloc (paren - name + 1);
2604 memcpy (dup, name, paren - name);
2605 dup[paren - name] = 0;
2606
2607 make_cleanup (xfree, dup);
2608 name = dup;
2609 }
2610 }
2611
559a7a62 2612 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2613 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2614 simulate our NAME being searched is also lowercased. */
2615 hash = mapped_index_string_hash ((index->version == 4
2616 && case_sensitivity == case_sensitive_off
2617 ? 5 : index->version),
2618 name);
2619
3876f04e
DE
2620 slot = hash & (index->symbol_table_slots - 1);
2621 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2622 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2623
2624 for (;;)
2625 {
2626 /* Convert a slot number to an offset into the table. */
2627 offset_type i = 2 * slot;
2628 const char *str;
3876f04e 2629 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2630 {
2631 do_cleanups (back_to);
2632 return 0;
2633 }
9291a0cd 2634
3876f04e 2635 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2636 if (!cmp (name, str))
9291a0cd
TT
2637 {
2638 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2639 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2640 do_cleanups (back_to);
9291a0cd
TT
2641 return 1;
2642 }
2643
3876f04e 2644 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2645 }
2646}
2647
2ec9a5e0
TT
2648/* A helper function that reads the .gdb_index from SECTION and fills
2649 in MAP. FILENAME is the name of the file containing the section;
2650 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2651 ok to use deprecated sections.
2652
2653 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2654 out parameters that are filled in with information about the CU and
2655 TU lists in the section.
2656
2657 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2658
9291a0cd 2659static int
2ec9a5e0
TT
2660read_index_from_section (struct objfile *objfile,
2661 const char *filename,
2662 int deprecated_ok,
2663 struct dwarf2_section_info *section,
2664 struct mapped_index *map,
2665 const gdb_byte **cu_list,
2666 offset_type *cu_list_elements,
2667 const gdb_byte **types_list,
2668 offset_type *types_list_elements)
9291a0cd 2669{
9291a0cd 2670 char *addr;
2ec9a5e0 2671 offset_type version;
b3b272e1 2672 offset_type *metadata;
1fd400ff 2673 int i;
9291a0cd 2674
2ec9a5e0 2675 if (dwarf2_section_empty_p (section))
9291a0cd 2676 return 0;
82430852
JK
2677
2678 /* Older elfutils strip versions could keep the section in the main
2679 executable while splitting it for the separate debug info file. */
2ec9a5e0 2680 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2681 return 0;
2682
2ec9a5e0 2683 dwarf2_read_section (objfile, section);
9291a0cd 2684
2ec9a5e0 2685 addr = section->buffer;
9291a0cd 2686 /* Version check. */
1fd400ff 2687 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2688 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2689 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2690 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2691 indices. */
831adc1f 2692 if (version < 4)
481860b3
GB
2693 {
2694 static int warning_printed = 0;
2695 if (!warning_printed)
2696 {
2697 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2698 filename);
481860b3
GB
2699 warning_printed = 1;
2700 }
2701 return 0;
2702 }
2703 /* Index version 4 uses a different hash function than index version
2704 5 and later.
2705
2706 Versions earlier than 6 did not emit psymbols for inlined
2707 functions. Using these files will cause GDB not to be able to
2708 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2709 indices unless the user has done
2710 "set use-deprecated-index-sections on". */
2ec9a5e0 2711 if (version < 6 && !deprecated_ok)
481860b3
GB
2712 {
2713 static int warning_printed = 0;
2714 if (!warning_printed)
2715 {
e615022a
DE
2716 warning (_("\
2717Skipping deprecated .gdb_index section in %s.\n\
2718Do \"set use-deprecated-index-sections on\" before the file is read\n\
2719to use the section anyway."),
2ec9a5e0 2720 filename);
481860b3
GB
2721 warning_printed = 1;
2722 }
2723 return 0;
2724 }
796a7ff8
DE
2725 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2726 of the TU (for symbols coming from TUs). It's just a performance bug, and
2727 we can't distinguish gdb-generated indices from gold-generated ones, so
2728 nothing to do here. */
2729
481860b3 2730 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2731 longer backward compatible. */
796a7ff8 2732 if (version > 8)
594e8718 2733 return 0;
9291a0cd 2734
559a7a62 2735 map->version = version;
2ec9a5e0 2736 map->total_size = section->size;
9291a0cd
TT
2737
2738 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2739
2740 i = 0;
2ec9a5e0
TT
2741 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2742 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2743 / 8);
1fd400ff
TT
2744 ++i;
2745
2ec9a5e0
TT
2746 *types_list = addr + MAYBE_SWAP (metadata[i]);
2747 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2748 - MAYBE_SWAP (metadata[i]))
2749 / 8);
987d643c 2750 ++i;
1fd400ff
TT
2751
2752 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2753 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2754 - MAYBE_SWAP (metadata[i]));
2755 ++i;
2756
3876f04e
DE
2757 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2758 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2759 - MAYBE_SWAP (metadata[i]))
2760 / (2 * sizeof (offset_type)));
1fd400ff 2761 ++i;
9291a0cd 2762
1fd400ff
TT
2763 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2764
2ec9a5e0
TT
2765 return 1;
2766}
2767
2768
2769/* Read the index file. If everything went ok, initialize the "quick"
2770 elements of all the CUs and return 1. Otherwise, return 0. */
2771
2772static int
2773dwarf2_read_index (struct objfile *objfile)
2774{
2775 struct mapped_index local_map, *map;
2776 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2777 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2778
2779 if (!read_index_from_section (objfile, objfile->name,
2780 use_deprecated_index_sections,
2781 &dwarf2_per_objfile->gdb_index, &local_map,
2782 &cu_list, &cu_list_elements,
2783 &types_list, &types_list_elements))
2784 return 0;
2785
0fefef59 2786 /* Don't use the index if it's empty. */
2ec9a5e0 2787 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2788 return 0;
2789
2ec9a5e0
TT
2790 /* If there is a .dwz file, read it so we can get its CU list as
2791 well. */
2792 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2793 {
2794 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2795 struct mapped_index dwz_map;
2796 const gdb_byte *dwz_types_ignore;
2797 offset_type dwz_types_elements_ignore;
2798
2799 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2800 1,
2801 &dwz->gdb_index, &dwz_map,
2802 &dwz_list, &dwz_list_elements,
2803 &dwz_types_ignore,
2804 &dwz_types_elements_ignore))
2805 {
2806 warning (_("could not read '.gdb_index' section from %s; skipping"),
2807 bfd_get_filename (dwz->dwz_bfd));
2808 return 0;
2809 }
2810 }
2811
74a0d9f6
JK
2812 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2813 dwz_list_elements);
1fd400ff 2814
8b70b953
TT
2815 if (types_list_elements)
2816 {
2817 struct dwarf2_section_info *section;
2818
2819 /* We can only handle a single .debug_types when we have an
2820 index. */
2821 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2822 return 0;
2823
2824 section = VEC_index (dwarf2_section_info_def,
2825 dwarf2_per_objfile->types, 0);
2826
74a0d9f6
JK
2827 create_signatured_type_table_from_index (objfile, section, types_list,
2828 types_list_elements);
8b70b953 2829 }
9291a0cd 2830
2ec9a5e0
TT
2831 create_addrmap_from_index (objfile, &local_map);
2832
2833 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2834 *map = local_map;
9291a0cd
TT
2835
2836 dwarf2_per_objfile->index_table = map;
2837 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2838 dwarf2_per_objfile->quick_file_names_table =
2839 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2840
2841 return 1;
2842}
2843
2844/* A helper for the "quick" functions which sets the global
2845 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2846
9291a0cd
TT
2847static void
2848dw2_setup (struct objfile *objfile)
2849{
2850 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2851 gdb_assert (dwarf2_per_objfile);
2852}
2853
dee91e82 2854/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2855
dee91e82
DE
2856static void
2857dw2_get_file_names_reader (const struct die_reader_specs *reader,
2858 gdb_byte *info_ptr,
2859 struct die_info *comp_unit_die,
2860 int has_children,
2861 void *data)
9291a0cd 2862{
dee91e82
DE
2863 struct dwarf2_cu *cu = reader->cu;
2864 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2865 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2866 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2867 struct line_header *lh;
9291a0cd 2868 struct attribute *attr;
dee91e82 2869 int i;
15d034d0 2870 const char *name, *comp_dir;
7b9f3c50
DE
2871 void **slot;
2872 struct quick_file_names *qfn;
2873 unsigned int line_offset;
9291a0cd 2874
07261596
TT
2875 /* Our callers never want to match partial units -- instead they
2876 will match the enclosing full CU. */
2877 if (comp_unit_die->tag == DW_TAG_partial_unit)
2878 {
2879 this_cu->v.quick->no_file_data = 1;
2880 return;
2881 }
2882
094b34ac
DE
2883 /* If we're reading the line header for TUs, store it in the "per_cu"
2884 for tu_group. */
2885 if (this_cu->is_debug_types)
2886 {
2887 struct type_unit_group *tu_group = data;
2888
2889 gdb_assert (tu_group != NULL);
2890 lh_cu = &tu_group->per_cu;
2891 }
2892 else
2893 lh_cu = this_cu;
2894
7b9f3c50
DE
2895 lh = NULL;
2896 slot = NULL;
2897 line_offset = 0;
dee91e82
DE
2898
2899 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2900 if (attr)
2901 {
7b9f3c50
DE
2902 struct quick_file_names find_entry;
2903
2904 line_offset = DW_UNSND (attr);
2905
2906 /* We may have already read in this line header (TU line header sharing).
2907 If we have we're done. */
094b34ac
DE
2908 find_entry.hash.dwo_unit = cu->dwo_unit;
2909 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2910 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2911 &find_entry, INSERT);
2912 if (*slot != NULL)
2913 {
094b34ac 2914 lh_cu->v.quick->file_names = *slot;
dee91e82 2915 return;
7b9f3c50
DE
2916 }
2917
3019eac3 2918 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2919 }
2920 if (lh == NULL)
2921 {
094b34ac 2922 lh_cu->v.quick->no_file_data = 1;
dee91e82 2923 return;
9291a0cd
TT
2924 }
2925
7b9f3c50 2926 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2927 qfn->hash.dwo_unit = cu->dwo_unit;
2928 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2929 gdb_assert (slot != NULL);
2930 *slot = qfn;
9291a0cd 2931
dee91e82 2932 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2933
7b9f3c50
DE
2934 qfn->num_file_names = lh->num_file_names;
2935 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2936 lh->num_file_names * sizeof (char *));
9291a0cd 2937 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2938 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2939 qfn->real_names = NULL;
9291a0cd 2940
7b9f3c50 2941 free_line_header (lh);
7b9f3c50 2942
094b34ac 2943 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2944}
2945
2946/* A helper for the "quick" functions which attempts to read the line
2947 table for THIS_CU. */
2948
2949static struct quick_file_names *
e4a48d9d 2950dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2951{
f4dc4d17
DE
2952 /* For TUs this should only be called on the parent group. */
2953 if (this_cu->is_debug_types)
2954 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2955
dee91e82
DE
2956 if (this_cu->v.quick->file_names != NULL)
2957 return this_cu->v.quick->file_names;
2958 /* If we know there is no line data, no point in looking again. */
2959 if (this_cu->v.quick->no_file_data)
2960 return NULL;
2961
3019eac3
DE
2962 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2963 in the stub for CUs, there's is no need to lookup the DWO file.
2964 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2965 DWO file. */
2966 if (this_cu->is_debug_types)
094b34ac 2967 {
796a7ff8 2968 struct type_unit_group *tu_group = this_cu->type_unit_group;
094b34ac
DE
2969
2970 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2971 dw2_get_file_names_reader, tu_group);
2972 }
3019eac3
DE
2973 else
2974 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2975
2976 if (this_cu->v.quick->no_file_data)
2977 return NULL;
2978 return this_cu->v.quick->file_names;
9291a0cd
TT
2979}
2980
2981/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2982 real path for a given file name from the line table. */
2fdf6df6 2983
9291a0cd 2984static const char *
7b9f3c50
DE
2985dw2_get_real_path (struct objfile *objfile,
2986 struct quick_file_names *qfn, int index)
9291a0cd 2987{
7b9f3c50
DE
2988 if (qfn->real_names == NULL)
2989 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2990 qfn->num_file_names, sizeof (char *));
9291a0cd 2991
7b9f3c50
DE
2992 if (qfn->real_names[index] == NULL)
2993 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2994
7b9f3c50 2995 return qfn->real_names[index];
9291a0cd
TT
2996}
2997
2998static struct symtab *
2999dw2_find_last_source_symtab (struct objfile *objfile)
3000{
3001 int index;
ae2de4f8 3002
9291a0cd
TT
3003 dw2_setup (objfile);
3004 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3005 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3006}
3007
7b9f3c50
DE
3008/* Traversal function for dw2_forget_cached_source_info. */
3009
3010static int
3011dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3012{
7b9f3c50 3013 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3014
7b9f3c50 3015 if (file_data->real_names)
9291a0cd 3016 {
7b9f3c50 3017 int i;
9291a0cd 3018
7b9f3c50 3019 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3020 {
7b9f3c50
DE
3021 xfree ((void*) file_data->real_names[i]);
3022 file_data->real_names[i] = NULL;
9291a0cd
TT
3023 }
3024 }
7b9f3c50
DE
3025
3026 return 1;
3027}
3028
3029static void
3030dw2_forget_cached_source_info (struct objfile *objfile)
3031{
3032 dw2_setup (objfile);
3033
3034 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3035 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3036}
3037
f8eba3c6
TT
3038/* Helper function for dw2_map_symtabs_matching_filename that expands
3039 the symtabs and calls the iterator. */
3040
3041static int
3042dw2_map_expand_apply (struct objfile *objfile,
3043 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3044 const char *name, const char *real_path,
f8eba3c6
TT
3045 int (*callback) (struct symtab *, void *),
3046 void *data)
3047{
3048 struct symtab *last_made = objfile->symtabs;
3049
3050 /* Don't visit already-expanded CUs. */
3051 if (per_cu->v.quick->symtab)
3052 return 0;
3053
3054 /* This may expand more than one symtab, and we want to iterate over
3055 all of them. */
a0f42c21 3056 dw2_instantiate_symtab (per_cu);
f8eba3c6 3057
f5b95b50 3058 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3059 objfile->symtabs, last_made);
3060}
3061
3062/* Implementation of the map_symtabs_matching_filename method. */
3063
9291a0cd 3064static int
f8eba3c6 3065dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3066 const char *real_path,
f8eba3c6
TT
3067 int (*callback) (struct symtab *, void *),
3068 void *data)
9291a0cd
TT
3069{
3070 int i;
c011a4f4 3071 const char *name_basename = lbasename (name);
9291a0cd
TT
3072
3073 dw2_setup (objfile);
ae2de4f8 3074
848e3e78
DE
3075 /* The rule is CUs specify all the files, including those used by
3076 any TU, so there's no need to scan TUs here. */
f4dc4d17 3077
848e3e78 3078 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3079 {
3080 int j;
f4dc4d17 3081 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3082 struct quick_file_names *file_data;
9291a0cd 3083
3d7bb9d9 3084 /* We only need to look at symtabs not already expanded. */
e254ef6a 3085 if (per_cu->v.quick->symtab)
9291a0cd
TT
3086 continue;
3087
e4a48d9d 3088 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3089 if (file_data == NULL)
9291a0cd
TT
3090 continue;
3091
7b9f3c50 3092 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3093 {
7b9f3c50 3094 const char *this_name = file_data->file_names[j];
da235a7c 3095 const char *this_real_name;
9291a0cd 3096
af529f8f 3097 if (compare_filenames_for_search (this_name, name))
9291a0cd 3098 {
f5b95b50 3099 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3100 callback, data))
3101 return 1;
288e77a7 3102 continue;
4aac40c8 3103 }
9291a0cd 3104
c011a4f4
DE
3105 /* Before we invoke realpath, which can get expensive when many
3106 files are involved, do a quick comparison of the basenames. */
3107 if (! basenames_may_differ
3108 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3109 continue;
3110
da235a7c
JK
3111 this_real_name = dw2_get_real_path (objfile, file_data, j);
3112 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3113 {
da235a7c
JK
3114 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3115 callback, data))
3116 return 1;
288e77a7 3117 continue;
da235a7c 3118 }
9291a0cd 3119
da235a7c
JK
3120 if (real_path != NULL)
3121 {
af529f8f
JK
3122 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3123 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3124 if (this_real_name != NULL
af529f8f 3125 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3126 {
f5b95b50 3127 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3128 callback, data))
3129 return 1;
288e77a7 3130 continue;
9291a0cd
TT
3131 }
3132 }
3133 }
3134 }
3135
9291a0cd
TT
3136 return 0;
3137}
3138
da51c347
DE
3139/* Struct used to manage iterating over all CUs looking for a symbol. */
3140
3141struct dw2_symtab_iterator
9291a0cd 3142{
da51c347
DE
3143 /* The internalized form of .gdb_index. */
3144 struct mapped_index *index;
3145 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3146 int want_specific_block;
3147 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3148 Unused if !WANT_SPECIFIC_BLOCK. */
3149 int block_index;
3150 /* The kind of symbol we're looking for. */
3151 domain_enum domain;
3152 /* The list of CUs from the index entry of the symbol,
3153 or NULL if not found. */
3154 offset_type *vec;
3155 /* The next element in VEC to look at. */
3156 int next;
3157 /* The number of elements in VEC, or zero if there is no match. */
3158 int length;
3159};
9291a0cd 3160
da51c347
DE
3161/* Initialize the index symtab iterator ITER.
3162 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3163 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3164
9291a0cd 3165static void
da51c347
DE
3166dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3167 struct mapped_index *index,
3168 int want_specific_block,
3169 int block_index,
3170 domain_enum domain,
3171 const char *name)
3172{
3173 iter->index = index;
3174 iter->want_specific_block = want_specific_block;
3175 iter->block_index = block_index;
3176 iter->domain = domain;
3177 iter->next = 0;
3178
3179 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3180 iter->length = MAYBE_SWAP (*iter->vec);
3181 else
3182 {
3183 iter->vec = NULL;
3184 iter->length = 0;
3185 }
3186}
3187
3188/* Return the next matching CU or NULL if there are no more. */
3189
3190static struct dwarf2_per_cu_data *
3191dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3192{
3193 for ( ; iter->next < iter->length; ++iter->next)
3194 {
3195 offset_type cu_index_and_attrs =
3196 MAYBE_SWAP (iter->vec[iter->next + 1]);
3197 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3198 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3199 int want_static = iter->block_index != GLOBAL_BLOCK;
3200 /* This value is only valid for index versions >= 7. */
3201 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3202 gdb_index_symbol_kind symbol_kind =
3203 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3204 /* Only check the symbol attributes if they're present.
3205 Indices prior to version 7 don't record them,
3206 and indices >= 7 may elide them for certain symbols
3207 (gold does this). */
3208 int attrs_valid =
3209 (iter->index->version >= 7
3210 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3211
3212 /* Skip if already read in. */
3213 if (per_cu->v.quick->symtab)
3214 continue;
3215
3216 if (attrs_valid
3217 && iter->want_specific_block
3218 && want_static != is_static)
3219 continue;
3220
3221 /* Only check the symbol's kind if it has one. */
3222 if (attrs_valid)
3223 {
3224 switch (iter->domain)
3225 {
3226 case VAR_DOMAIN:
3227 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3228 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3229 /* Some types are also in VAR_DOMAIN. */
3230 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3231 continue;
3232 break;
3233 case STRUCT_DOMAIN:
3234 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3235 continue;
3236 break;
3237 case LABEL_DOMAIN:
3238 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3239 continue;
3240 break;
3241 default:
3242 break;
3243 }
3244 }
3245
3246 ++iter->next;
3247 return per_cu;
3248 }
3249
3250 return NULL;
3251}
3252
3253static struct symtab *
3254dw2_lookup_symbol (struct objfile *objfile, int block_index,
3255 const char *name, domain_enum domain)
9291a0cd 3256{
da51c347 3257 struct symtab *stab_best = NULL;
156942c7
DE
3258 struct mapped_index *index;
3259
9291a0cd
TT
3260 dw2_setup (objfile);
3261
156942c7
DE
3262 index = dwarf2_per_objfile->index_table;
3263
da51c347 3264 /* index is NULL if OBJF_READNOW. */
156942c7 3265 if (index)
9291a0cd 3266 {
da51c347
DE
3267 struct dw2_symtab_iterator iter;
3268 struct dwarf2_per_cu_data *per_cu;
3269
3270 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3271
da51c347 3272 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3273 {
da51c347
DE
3274 struct symbol *sym = NULL;
3275 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3276
3277 /* Some caution must be observed with overloaded functions
3278 and methods, since the index will not contain any overload
3279 information (but NAME might contain it). */
3280 if (stab->primary)
9291a0cd 3281 {
da51c347
DE
3282 struct blockvector *bv = BLOCKVECTOR (stab);
3283 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3284
da51c347
DE
3285 sym = lookup_block_symbol (block, name, domain);
3286 }
1fd400ff 3287
da51c347
DE
3288 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3289 {
3290 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3291 return stab;
3292
3293 stab_best = stab;
9291a0cd 3294 }
da51c347
DE
3295
3296 /* Keep looking through other CUs. */
9291a0cd
TT
3297 }
3298 }
9291a0cd 3299
da51c347 3300 return stab_best;
9291a0cd
TT
3301}
3302
3303static void
3304dw2_print_stats (struct objfile *objfile)
3305{
e4a48d9d 3306 int i, total, count;
9291a0cd
TT
3307
3308 dw2_setup (objfile);
e4a48d9d 3309 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3310 count = 0;
e4a48d9d 3311 for (i = 0; i < total; ++i)
9291a0cd 3312 {
e254ef6a 3313 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3314
e254ef6a 3315 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3316 ++count;
3317 }
e4a48d9d 3318 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3319 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3320}
3321
3322static void
3323dw2_dump (struct objfile *objfile)
3324{
3325 /* Nothing worth printing. */
3326}
3327
3328static void
3329dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3330 struct section_offsets *delta)
3331{
3332 /* There's nothing to relocate here. */
3333}
3334
3335static void
3336dw2_expand_symtabs_for_function (struct objfile *objfile,
3337 const char *func_name)
3338{
da51c347
DE
3339 struct mapped_index *index;
3340
3341 dw2_setup (objfile);
3342
3343 index = dwarf2_per_objfile->index_table;
3344
3345 /* index is NULL if OBJF_READNOW. */
3346 if (index)
3347 {
3348 struct dw2_symtab_iterator iter;
3349 struct dwarf2_per_cu_data *per_cu;
3350
3351 /* Note: It doesn't matter what we pass for block_index here. */
3352 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3353 func_name);
3354
3355 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3356 dw2_instantiate_symtab (per_cu);
3357 }
9291a0cd
TT
3358}
3359
3360static void
3361dw2_expand_all_symtabs (struct objfile *objfile)
3362{
3363 int i;
3364
3365 dw2_setup (objfile);
1fd400ff
TT
3366
3367 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3368 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3369 {
e254ef6a 3370 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3371
a0f42c21 3372 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3373 }
3374}
3375
3376static void
652a8996
JK
3377dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3378 const char *fullname)
9291a0cd
TT
3379{
3380 int i;
3381
3382 dw2_setup (objfile);
d4637a04
DE
3383
3384 /* We don't need to consider type units here.
3385 This is only called for examining code, e.g. expand_line_sal.
3386 There can be an order of magnitude (or more) more type units
3387 than comp units, and we avoid them if we can. */
3388
3389 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3390 {
3391 int j;
e254ef6a 3392 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3393 struct quick_file_names *file_data;
9291a0cd 3394
3d7bb9d9 3395 /* We only need to look at symtabs not already expanded. */
e254ef6a 3396 if (per_cu->v.quick->symtab)
9291a0cd
TT
3397 continue;
3398
e4a48d9d 3399 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3400 if (file_data == NULL)
9291a0cd
TT
3401 continue;
3402
7b9f3c50 3403 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3404 {
652a8996
JK
3405 const char *this_fullname = file_data->file_names[j];
3406
3407 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3408 {
a0f42c21 3409 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3410 break;
3411 }
3412 }
3413 }
3414}
3415
356d9f9d
TT
3416/* A helper function for dw2_find_symbol_file that finds the primary
3417 file name for a given CU. This is a die_reader_func. */
3418
3419static void
3420dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3421 gdb_byte *info_ptr,
3422 struct die_info *comp_unit_die,
3423 int has_children,
3424 void *data)
3425{
3426 const char **result_ptr = data;
3427 struct dwarf2_cu *cu = reader->cu;
3428 struct attribute *attr;
3429
3430 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3431 if (attr == NULL)
3432 *result_ptr = NULL;
3433 else
3434 *result_ptr = DW_STRING (attr);
3435}
3436
dd786858 3437static const char *
9291a0cd
TT
3438dw2_find_symbol_file (struct objfile *objfile, const char *name)
3439{
e254ef6a 3440 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3441 offset_type *vec;
356d9f9d 3442 const char *filename;
9291a0cd
TT
3443
3444 dw2_setup (objfile);
3445
ae2de4f8 3446 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3447 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3448 {
3449 struct symtab *s;
3450
d790cf0a
DE
3451 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3452 {
3453 struct blockvector *bv = BLOCKVECTOR (s);
3454 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3455 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3456
3457 if (sym)
652a8996
JK
3458 {
3459 /* Only file extension of returned filename is recognized. */
3460 return SYMBOL_SYMTAB (sym)->filename;
3461 }
d790cf0a 3462 }
96408a79
SA
3463 return NULL;
3464 }
9291a0cd
TT
3465
3466 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3467 name, &vec))
3468 return NULL;
3469
3470 /* Note that this just looks at the very first one named NAME -- but
3471 actually we are looking for a function. find_main_filename
3472 should be rewritten so that it doesn't require a custom hook. It
3473 could just use the ordinary symbol tables. */
3474 /* vec[0] is the length, which must always be >0. */
156942c7 3475 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3476
356d9f9d 3477 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3478 {
3479 /* Only file extension of returned filename is recognized. */
3480 return per_cu->v.quick->symtab->filename;
3481 }
356d9f9d 3482
f4dc4d17
DE
3483 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3484 dw2_get_primary_filename_reader, &filename);
9291a0cd 3485
652a8996 3486 /* Only file extension of returned filename is recognized. */
356d9f9d 3487 return filename;
9291a0cd
TT
3488}
3489
3490static void
40658b94
PH
3491dw2_map_matching_symbols (const char * name, domain_enum namespace,
3492 struct objfile *objfile, int global,
3493 int (*callback) (struct block *,
3494 struct symbol *, void *),
2edb89d3
JK
3495 void *data, symbol_compare_ftype *match,
3496 symbol_compare_ftype *ordered_compare)
9291a0cd 3497{
40658b94 3498 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3499 current language is Ada for a non-Ada objfile using GNU index. As Ada
3500 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3501}
3502
3503static void
f8eba3c6
TT
3504dw2_expand_symtabs_matching
3505 (struct objfile *objfile,
fbd9ab74 3506 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3507 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3508 enum search_domain kind,
3509 void *data)
9291a0cd
TT
3510{
3511 int i;
3512 offset_type iter;
4b5246aa 3513 struct mapped_index *index;
9291a0cd
TT
3514
3515 dw2_setup (objfile);
ae2de4f8
DE
3516
3517 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3518 if (!dwarf2_per_objfile->index_table)
3519 return;
4b5246aa 3520 index = dwarf2_per_objfile->index_table;
9291a0cd 3521
7b08b9eb 3522 if (file_matcher != NULL)
24c79950
TT
3523 {
3524 struct cleanup *cleanup;
3525 htab_t visited_found, visited_not_found;
3526
3527 visited_found = htab_create_alloc (10,
3528 htab_hash_pointer, htab_eq_pointer,
3529 NULL, xcalloc, xfree);
3530 cleanup = make_cleanup_htab_delete (visited_found);
3531 visited_not_found = htab_create_alloc (10,
3532 htab_hash_pointer, htab_eq_pointer,
3533 NULL, xcalloc, xfree);
3534 make_cleanup_htab_delete (visited_not_found);
3535
848e3e78
DE
3536 /* The rule is CUs specify all the files, including those used by
3537 any TU, so there's no need to scan TUs here. */
3538
3539 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3540 {
3541 int j;
f4dc4d17 3542 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3543 struct quick_file_names *file_data;
3544 void **slot;
7b08b9eb 3545
24c79950 3546 per_cu->v.quick->mark = 0;
3d7bb9d9 3547
24c79950
TT
3548 /* We only need to look at symtabs not already expanded. */
3549 if (per_cu->v.quick->symtab)
3550 continue;
7b08b9eb 3551
e4a48d9d 3552 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3553 if (file_data == NULL)
3554 continue;
7b08b9eb 3555
24c79950
TT
3556 if (htab_find (visited_not_found, file_data) != NULL)
3557 continue;
3558 else if (htab_find (visited_found, file_data) != NULL)
3559 {
3560 per_cu->v.quick->mark = 1;
3561 continue;
3562 }
3563
3564 for (j = 0; j < file_data->num_file_names; ++j)
3565 {
da235a7c
JK
3566 const char *this_real_name;
3567
fbd9ab74 3568 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3569 {
3570 per_cu->v.quick->mark = 1;
3571 break;
3572 }
da235a7c
JK
3573
3574 /* Before we invoke realpath, which can get expensive when many
3575 files are involved, do a quick comparison of the basenames. */
3576 if (!basenames_may_differ
3577 && !file_matcher (lbasename (file_data->file_names[j]),
3578 data, 1))
3579 continue;
3580
3581 this_real_name = dw2_get_real_path (objfile, file_data, j);
3582 if (file_matcher (this_real_name, data, 0))
3583 {
3584 per_cu->v.quick->mark = 1;
3585 break;
3586 }
24c79950
TT
3587 }
3588
3589 slot = htab_find_slot (per_cu->v.quick->mark
3590 ? visited_found
3591 : visited_not_found,
3592 file_data, INSERT);
3593 *slot = file_data;
3594 }
3595
3596 do_cleanups (cleanup);
3597 }
9291a0cd 3598
3876f04e 3599 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3600 {
3601 offset_type idx = 2 * iter;
3602 const char *name;
3603 offset_type *vec, vec_len, vec_idx;
3604
3876f04e 3605 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3606 continue;
3607
3876f04e 3608 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3609
e078317b 3610 if (! (*name_matcher) (name, data))
9291a0cd
TT
3611 continue;
3612
3613 /* The name was matched, now expand corresponding CUs that were
3614 marked. */
4b5246aa 3615 vec = (offset_type *) (index->constant_pool
3876f04e 3616 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3617 vec_len = MAYBE_SWAP (vec[0]);
3618 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3619 {
e254ef6a 3620 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3621 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3622 gdb_index_symbol_kind symbol_kind =
3623 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3624 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3625
3626 /* Don't crash on bad data. */
3627 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3628 + dwarf2_per_objfile->n_type_units))
156942c7 3629 continue;
1fd400ff 3630
156942c7
DE
3631 /* Only check the symbol's kind if it has one.
3632 Indices prior to version 7 don't record it. */
3633 if (index->version >= 7)
3634 {
3635 switch (kind)
3636 {
3637 case VARIABLES_DOMAIN:
3638 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3639 continue;
3640 break;
3641 case FUNCTIONS_DOMAIN:
3642 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3643 continue;
3644 break;
3645 case TYPES_DOMAIN:
3646 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3647 continue;
3648 break;
3649 default:
3650 break;
3651 }
3652 }
3653
3654 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3655 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3656 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3657 }
3658 }
3659}
3660
9703b513
TT
3661/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3662 symtab. */
3663
3664static struct symtab *
3665recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3666{
3667 int i;
3668
3669 if (BLOCKVECTOR (symtab) != NULL
3670 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3671 return symtab;
3672
a3ec0bb1
DE
3673 if (symtab->includes == NULL)
3674 return NULL;
3675
9703b513
TT
3676 for (i = 0; symtab->includes[i]; ++i)
3677 {
a3ec0bb1 3678 struct symtab *s = symtab->includes[i];
9703b513
TT
3679
3680 s = recursively_find_pc_sect_symtab (s, pc);
3681 if (s != NULL)
3682 return s;
3683 }
3684
3685 return NULL;
3686}
3687
9291a0cd
TT
3688static struct symtab *
3689dw2_find_pc_sect_symtab (struct objfile *objfile,
3690 struct minimal_symbol *msymbol,
3691 CORE_ADDR pc,
3692 struct obj_section *section,
3693 int warn_if_readin)
3694{
3695 struct dwarf2_per_cu_data *data;
9703b513 3696 struct symtab *result;
9291a0cd
TT
3697
3698 dw2_setup (objfile);
3699
3700 if (!objfile->psymtabs_addrmap)
3701 return NULL;
3702
3703 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3704 if (!data)
3705 return NULL;
3706
3707 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3708 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3709 paddress (get_objfile_arch (objfile), pc));
3710
9703b513
TT
3711 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3712 gdb_assert (result != NULL);
3713 return result;
9291a0cd
TT
3714}
3715
9291a0cd 3716static void
44b13c5a 3717dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3718 void *data, int need_fullname)
9291a0cd
TT
3719{
3720 int i;
24c79950
TT
3721 struct cleanup *cleanup;
3722 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3723 NULL, xcalloc, xfree);
9291a0cd 3724
24c79950 3725 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3726 dw2_setup (objfile);
ae2de4f8 3727
848e3e78
DE
3728 /* The rule is CUs specify all the files, including those used by
3729 any TU, so there's no need to scan TUs here.
3730 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3731
848e3e78 3732 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3733 {
3734 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3735
3736 if (per_cu->v.quick->symtab)
3737 {
3738 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3739 INSERT);
3740
3741 *slot = per_cu->v.quick->file_names;
3742 }
3743 }
3744
848e3e78 3745 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3746 {
3747 int j;
f4dc4d17 3748 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3749 struct quick_file_names *file_data;
24c79950 3750 void **slot;
9291a0cd 3751
3d7bb9d9 3752 /* We only need to look at symtabs not already expanded. */
e254ef6a 3753 if (per_cu->v.quick->symtab)
9291a0cd
TT
3754 continue;
3755
e4a48d9d 3756 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3757 if (file_data == NULL)
9291a0cd
TT
3758 continue;
3759
24c79950
TT
3760 slot = htab_find_slot (visited, file_data, INSERT);
3761 if (*slot)
3762 {
3763 /* Already visited. */
3764 continue;
3765 }
3766 *slot = file_data;
3767
7b9f3c50 3768 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3769 {
74e2f255
DE
3770 const char *this_real_name;
3771
3772 if (need_fullname)
3773 this_real_name = dw2_get_real_path (objfile, file_data, j);
3774 else
3775 this_real_name = NULL;
7b9f3c50 3776 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3777 }
3778 }
24c79950
TT
3779
3780 do_cleanups (cleanup);
9291a0cd
TT
3781}
3782
3783static int
3784dw2_has_symbols (struct objfile *objfile)
3785{
3786 return 1;
3787}
3788
3789const struct quick_symbol_functions dwarf2_gdb_index_functions =
3790{
3791 dw2_has_symbols,
3792 dw2_find_last_source_symtab,
3793 dw2_forget_cached_source_info,
f8eba3c6 3794 dw2_map_symtabs_matching_filename,
9291a0cd 3795 dw2_lookup_symbol,
9291a0cd
TT
3796 dw2_print_stats,
3797 dw2_dump,
3798 dw2_relocate,
3799 dw2_expand_symtabs_for_function,
3800 dw2_expand_all_symtabs,
652a8996 3801 dw2_expand_symtabs_with_fullname,
9291a0cd 3802 dw2_find_symbol_file,
40658b94 3803 dw2_map_matching_symbols,
9291a0cd
TT
3804 dw2_expand_symtabs_matching,
3805 dw2_find_pc_sect_symtab,
9291a0cd
TT
3806 dw2_map_symbol_filenames
3807};
3808
3809/* Initialize for reading DWARF for this objfile. Return 0 if this
3810 file will use psymtabs, or 1 if using the GNU index. */
3811
3812int
3813dwarf2_initialize_objfile (struct objfile *objfile)
3814{
3815 /* If we're about to read full symbols, don't bother with the
3816 indices. In this case we also don't care if some other debug
3817 format is making psymtabs, because they are all about to be
3818 expanded anyway. */
3819 if ((objfile->flags & OBJF_READNOW))
3820 {
3821 int i;
3822
3823 dwarf2_per_objfile->using_index = 1;
3824 create_all_comp_units (objfile);
0e50663e 3825 create_all_type_units (objfile);
7b9f3c50
DE
3826 dwarf2_per_objfile->quick_file_names_table =
3827 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3828
1fd400ff 3829 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3830 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3831 {
e254ef6a 3832 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3833
e254ef6a
DE
3834 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3835 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3836 }
3837
3838 /* Return 1 so that gdb sees the "quick" functions. However,
3839 these functions will be no-ops because we will have expanded
3840 all symtabs. */
3841 return 1;
3842 }
3843
3844 if (dwarf2_read_index (objfile))
3845 return 1;
3846
9291a0cd
TT
3847 return 0;
3848}
3849
3850\f
3851
dce234bc
PP
3852/* Build a partial symbol table. */
3853
3854void
f29dff0a 3855dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3856{
c9bf0622
TT
3857 volatile struct gdb_exception except;
3858
f29dff0a 3859 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3860 {
3861 init_psymbol_list (objfile, 1024);
3862 }
3863
c9bf0622
TT
3864 TRY_CATCH (except, RETURN_MASK_ERROR)
3865 {
3866 /* This isn't really ideal: all the data we allocate on the
3867 objfile's obstack is still uselessly kept around. However,
3868 freeing it seems unsafe. */
3869 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3870
3871 dwarf2_build_psymtabs_hard (objfile);
3872 discard_cleanups (cleanups);
3873 }
3874 if (except.reason < 0)
3875 exception_print (gdb_stderr, except);
c906108c 3876}
c906108c 3877
1ce1cefd
DE
3878/* Return the total length of the CU described by HEADER. */
3879
3880static unsigned int
3881get_cu_length (const struct comp_unit_head *header)
3882{
3883 return header->initial_length_size + header->length;
3884}
3885
45452591
DE
3886/* Return TRUE if OFFSET is within CU_HEADER. */
3887
3888static inline int
b64f50a1 3889offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3890{
b64f50a1 3891 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3892 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3893
b64f50a1 3894 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3895}
3896
3b80fe9b
DE
3897/* Find the base address of the compilation unit for range lists and
3898 location lists. It will normally be specified by DW_AT_low_pc.
3899 In DWARF-3 draft 4, the base address could be overridden by
3900 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3901 compilation units with discontinuous ranges. */
3902
3903static void
3904dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3905{
3906 struct attribute *attr;
3907
3908 cu->base_known = 0;
3909 cu->base_address = 0;
3910
3911 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3912 if (attr)
3913 {
3914 cu->base_address = DW_ADDR (attr);
3915 cu->base_known = 1;
3916 }
3917 else
3918 {
3919 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3920 if (attr)
3921 {
3922 cu->base_address = DW_ADDR (attr);
3923 cu->base_known = 1;
3924 }
3925 }
3926}
3927
93311388
DE
3928/* Read in the comp unit header information from the debug_info at info_ptr.
3929 NOTE: This leaves members offset, first_die_offset to be filled in
3930 by the caller. */
107d2387 3931
fe1b8b76 3932static gdb_byte *
107d2387 3933read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3934 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3935{
3936 int signed_addr;
891d2f0b 3937 unsigned int bytes_read;
c764a876
DE
3938
3939 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3940 cu_header->initial_length_size = bytes_read;
3941 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3942 info_ptr += bytes_read;
107d2387
AC
3943 cu_header->version = read_2_bytes (abfd, info_ptr);
3944 info_ptr += 2;
b64f50a1
JK
3945 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3946 &bytes_read);
613e1657 3947 info_ptr += bytes_read;
107d2387
AC
3948 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3949 info_ptr += 1;
3950 signed_addr = bfd_get_sign_extend_vma (abfd);
3951 if (signed_addr < 0)
8e65ff28 3952 internal_error (__FILE__, __LINE__,
e2e0b3e5 3953 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3954 cu_header->signed_addr_p = signed_addr;
c764a876 3955
107d2387
AC
3956 return info_ptr;
3957}
3958
36586728
TT
3959/* Helper function that returns the proper abbrev section for
3960 THIS_CU. */
3961
3962static struct dwarf2_section_info *
3963get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3964{
3965 struct dwarf2_section_info *abbrev;
3966
3967 if (this_cu->is_dwz)
3968 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3969 else
3970 abbrev = &dwarf2_per_objfile->abbrev;
3971
3972 return abbrev;
3973}
3974
9ff913ba
DE
3975/* Subroutine of read_and_check_comp_unit_head and
3976 read_and_check_type_unit_head to simplify them.
3977 Perform various error checking on the header. */
3978
3979static void
3980error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3981 struct dwarf2_section_info *section,
3982 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3983{
3984 bfd *abfd = section->asection->owner;
3985 const char *filename = bfd_get_filename (abfd);
3986
3987 if (header->version != 2 && header->version != 3 && header->version != 4)
3988 error (_("Dwarf Error: wrong version in compilation unit header "
3989 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3990 filename);
3991
b64f50a1 3992 if (header->abbrev_offset.sect_off
36586728 3993 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3994 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3995 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3996 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3997 filename);
3998
3999 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4000 avoid potential 32-bit overflow. */
1ce1cefd 4001 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4002 > section->size)
4003 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4004 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4005 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4006 filename);
4007}
4008
4009/* Read in a CU/TU header and perform some basic error checking.
4010 The contents of the header are stored in HEADER.
4011 The result is a pointer to the start of the first DIE. */
adabb602 4012
fe1b8b76 4013static gdb_byte *
9ff913ba
DE
4014read_and_check_comp_unit_head (struct comp_unit_head *header,
4015 struct dwarf2_section_info *section,
4bdcc0c1 4016 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
4017 gdb_byte *info_ptr,
4018 int is_debug_types_section)
72bf9492 4019{
fe1b8b76 4020 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4021 bfd *abfd = section->asection->owner;
72bf9492 4022
b64f50a1 4023 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4024
72bf9492
DJ
4025 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4026
460c1c54
CC
4027 /* If we're reading a type unit, skip over the signature and
4028 type_offset fields. */
b0df02fd 4029 if (is_debug_types_section)
460c1c54
CC
4030 info_ptr += 8 /*signature*/ + header->offset_size;
4031
b64f50a1 4032 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4033
4bdcc0c1 4034 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4035
4036 return info_ptr;
4037}
4038
348e048f
DE
4039/* Read in the types comp unit header information from .debug_types entry at
4040 types_ptr. The result is a pointer to one past the end of the header. */
4041
4042static gdb_byte *
9ff913ba
DE
4043read_and_check_type_unit_head (struct comp_unit_head *header,
4044 struct dwarf2_section_info *section,
4bdcc0c1 4045 struct dwarf2_section_info *abbrev_section,
9ff913ba 4046 gdb_byte *info_ptr,
dee91e82
DE
4047 ULONGEST *signature,
4048 cu_offset *type_offset_in_tu)
348e048f 4049{
9ff913ba
DE
4050 gdb_byte *beg_of_comp_unit = info_ptr;
4051 bfd *abfd = section->asection->owner;
348e048f 4052
b64f50a1 4053 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4054
9ff913ba 4055 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4056
9ff913ba
DE
4057 /* If we're reading a type unit, skip over the signature and
4058 type_offset fields. */
4059 if (signature != NULL)
4060 *signature = read_8_bytes (abfd, info_ptr);
4061 info_ptr += 8;
dee91e82
DE
4062 if (type_offset_in_tu != NULL)
4063 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4064 header->offset_size);
9ff913ba
DE
4065 info_ptr += header->offset_size;
4066
b64f50a1 4067 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4068
4bdcc0c1 4069 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4070
4071 return info_ptr;
348e048f
DE
4072}
4073
f4dc4d17
DE
4074/* Fetch the abbreviation table offset from a comp or type unit header. */
4075
4076static sect_offset
4077read_abbrev_offset (struct dwarf2_section_info *section,
4078 sect_offset offset)
4079{
4080 bfd *abfd = section->asection->owner;
4081 gdb_byte *info_ptr;
4082 unsigned int length, initial_length_size, offset_size;
4083 sect_offset abbrev_offset;
4084
4085 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4086 info_ptr = section->buffer + offset.sect_off;
4087 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4088 offset_size = initial_length_size == 4 ? 4 : 8;
4089 info_ptr += initial_length_size + 2 /*version*/;
4090 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4091 return abbrev_offset;
4092}
4093
aaa75496
JB
4094/* Allocate a new partial symtab for file named NAME and mark this new
4095 partial symtab as being an include of PST. */
4096
4097static void
4098dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4099 struct objfile *objfile)
4100{
4101 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4102
fbd9ab74
JK
4103 if (!IS_ABSOLUTE_PATH (subpst->filename))
4104 {
4105 /* It shares objfile->objfile_obstack. */
4106 subpst->dirname = pst->dirname;
4107 }
4108
aaa75496
JB
4109 subpst->section_offsets = pst->section_offsets;
4110 subpst->textlow = 0;
4111 subpst->texthigh = 0;
4112
4113 subpst->dependencies = (struct partial_symtab **)
4114 obstack_alloc (&objfile->objfile_obstack,
4115 sizeof (struct partial_symtab *));
4116 subpst->dependencies[0] = pst;
4117 subpst->number_of_dependencies = 1;
4118
4119 subpst->globals_offset = 0;
4120 subpst->n_global_syms = 0;
4121 subpst->statics_offset = 0;
4122 subpst->n_static_syms = 0;
4123 subpst->symtab = NULL;
4124 subpst->read_symtab = pst->read_symtab;
4125 subpst->readin = 0;
4126
4127 /* No private part is necessary for include psymtabs. This property
4128 can be used to differentiate between such include psymtabs and
10b3939b 4129 the regular ones. */
58a9656e 4130 subpst->read_symtab_private = NULL;
aaa75496
JB
4131}
4132
4133/* Read the Line Number Program data and extract the list of files
4134 included by the source file represented by PST. Build an include
d85a05f0 4135 partial symtab for each of these included files. */
aaa75496
JB
4136
4137static void
4138dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4139 struct die_info *die,
4140 struct partial_symtab *pst)
aaa75496 4141{
d85a05f0
DJ
4142 struct line_header *lh = NULL;
4143 struct attribute *attr;
aaa75496 4144
d85a05f0
DJ
4145 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4146 if (attr)
3019eac3 4147 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4148 if (lh == NULL)
4149 return; /* No linetable, so no includes. */
4150
c6da4cef 4151 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4152 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4153
4154 free_line_header (lh);
4155}
4156
348e048f 4157static hashval_t
52dc124a 4158hash_signatured_type (const void *item)
348e048f 4159{
52dc124a 4160 const struct signatured_type *sig_type = item;
9a619af0 4161
348e048f 4162 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4163 return sig_type->signature;
348e048f
DE
4164}
4165
4166static int
52dc124a 4167eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4168{
4169 const struct signatured_type *lhs = item_lhs;
4170 const struct signatured_type *rhs = item_rhs;
9a619af0 4171
348e048f
DE
4172 return lhs->signature == rhs->signature;
4173}
4174
1fd400ff
TT
4175/* Allocate a hash table for signatured types. */
4176
4177static htab_t
673bfd45 4178allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4179{
4180 return htab_create_alloc_ex (41,
52dc124a
DE
4181 hash_signatured_type,
4182 eq_signatured_type,
1fd400ff
TT
4183 NULL,
4184 &objfile->objfile_obstack,
4185 hashtab_obstack_allocate,
4186 dummy_obstack_deallocate);
4187}
4188
d467dd73 4189/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4190
4191static int
d467dd73 4192add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4193{
4194 struct signatured_type *sigt = *slot;
b4dd5633 4195 struct signatured_type ***datap = datum;
1fd400ff 4196
b4dd5633 4197 **datap = sigt;
1fd400ff
TT
4198 ++*datap;
4199
4200 return 1;
4201}
4202
c88ee1f0
DE
4203/* Create the hash table of all entries in the .debug_types
4204 (or .debug_types.dwo) section(s).
4205 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4206 otherwise it is NULL.
4207
4208 The result is a pointer to the hash table or NULL if there are no types.
4209
4210 Note: This function processes DWO files only, not DWP files. */
348e048f 4211
3019eac3
DE
4212static htab_t
4213create_debug_types_hash_table (struct dwo_file *dwo_file,
4214 VEC (dwarf2_section_info_def) *types)
348e048f 4215{
3019eac3 4216 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4217 htab_t types_htab = NULL;
8b70b953
TT
4218 int ix;
4219 struct dwarf2_section_info *section;
4bdcc0c1 4220 struct dwarf2_section_info *abbrev_section;
348e048f 4221
3019eac3
DE
4222 if (VEC_empty (dwarf2_section_info_def, types))
4223 return NULL;
348e048f 4224
4bdcc0c1
DE
4225 abbrev_section = (dwo_file != NULL
4226 ? &dwo_file->sections.abbrev
4227 : &dwarf2_per_objfile->abbrev);
4228
09406207
DE
4229 if (dwarf2_read_debug)
4230 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4231 dwo_file ? ".dwo" : "",
4232 bfd_get_filename (abbrev_section->asection->owner));
4233
8b70b953 4234 for (ix = 0;
3019eac3 4235 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4236 ++ix)
4237 {
3019eac3 4238 bfd *abfd;
8b70b953 4239 gdb_byte *info_ptr, *end_ptr;
36586728 4240 struct dwarf2_section_info *abbrev_section;
348e048f 4241
8b70b953
TT
4242 dwarf2_read_section (objfile, section);
4243 info_ptr = section->buffer;
348e048f 4244
8b70b953
TT
4245 if (info_ptr == NULL)
4246 continue;
348e048f 4247
3019eac3
DE
4248 /* We can't set abfd until now because the section may be empty or
4249 not present, in which case section->asection will be NULL. */
4250 abfd = section->asection->owner;
4251
36586728
TT
4252 if (dwo_file)
4253 abbrev_section = &dwo_file->sections.abbrev;
4254 else
4255 abbrev_section = &dwarf2_per_objfile->abbrev;
4256
dee91e82
DE
4257 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4258 because we don't need to read any dies: the signature is in the
4259 header. */
8b70b953
TT
4260
4261 end_ptr = info_ptr + section->size;
4262 while (info_ptr < end_ptr)
4263 {
b64f50a1 4264 sect_offset offset;
3019eac3 4265 cu_offset type_offset_in_tu;
8b70b953 4266 ULONGEST signature;
52dc124a 4267 struct signatured_type *sig_type;
3019eac3 4268 struct dwo_unit *dwo_tu;
8b70b953
TT
4269 void **slot;
4270 gdb_byte *ptr = info_ptr;
9ff913ba 4271 struct comp_unit_head header;
dee91e82 4272 unsigned int length;
348e048f 4273
b64f50a1 4274 offset.sect_off = ptr - section->buffer;
348e048f 4275
8b70b953 4276 /* We need to read the type's signature in order to build the hash
9ff913ba 4277 table, but we don't need anything else just yet. */
348e048f 4278
4bdcc0c1
DE
4279 ptr = read_and_check_type_unit_head (&header, section,
4280 abbrev_section, ptr,
3019eac3 4281 &signature, &type_offset_in_tu);
6caca83c 4282
1ce1cefd 4283 length = get_cu_length (&header);
dee91e82 4284
6caca83c 4285 /* Skip dummy type units. */
dee91e82
DE
4286 if (ptr >= info_ptr + length
4287 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4288 {
1ce1cefd 4289 info_ptr += length;
6caca83c
CC
4290 continue;
4291 }
8b70b953 4292
0349ea22
DE
4293 if (types_htab == NULL)
4294 {
4295 if (dwo_file)
4296 types_htab = allocate_dwo_unit_table (objfile);
4297 else
4298 types_htab = allocate_signatured_type_table (objfile);
4299 }
4300
3019eac3
DE
4301 if (dwo_file)
4302 {
4303 sig_type = NULL;
4304 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4305 struct dwo_unit);
4306 dwo_tu->dwo_file = dwo_file;
4307 dwo_tu->signature = signature;
4308 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4309 dwo_tu->section = section;
3019eac3
DE
4310 dwo_tu->offset = offset;
4311 dwo_tu->length = length;
4312 }
4313 else
4314 {
4315 /* N.B.: type_offset is not usable if this type uses a DWO file.
4316 The real type_offset is in the DWO file. */
4317 dwo_tu = NULL;
4318 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4319 struct signatured_type);
4320 sig_type->signature = signature;
4321 sig_type->type_offset_in_tu = type_offset_in_tu;
4322 sig_type->per_cu.objfile = objfile;
4323 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4324 sig_type->per_cu.section = section;
3019eac3
DE
4325 sig_type->per_cu.offset = offset;
4326 sig_type->per_cu.length = length;
4327 }
8b70b953 4328
3019eac3
DE
4329 slot = htab_find_slot (types_htab,
4330 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4331 INSERT);
8b70b953
TT
4332 gdb_assert (slot != NULL);
4333 if (*slot != NULL)
4334 {
3019eac3
DE
4335 sect_offset dup_offset;
4336
4337 if (dwo_file)
4338 {
4339 const struct dwo_unit *dup_tu = *slot;
4340
4341 dup_offset = dup_tu->offset;
4342 }
4343 else
4344 {
4345 const struct signatured_type *dup_tu = *slot;
4346
4347 dup_offset = dup_tu->per_cu.offset;
4348 }
b3c8eb43 4349
8b70b953 4350 complaint (&symfile_complaints,
c88ee1f0
DE
4351 _("debug type entry at offset 0x%x is duplicate to"
4352 " the entry at offset 0x%x, signature 0x%s"),
3019eac3 4353 offset.sect_off, dup_offset.sect_off,
8b70b953 4354 phex (signature, sizeof (signature)));
8b70b953 4355 }
3019eac3 4356 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4357
09406207 4358 if (dwarf2_read_debug)
8b70b953 4359 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4360 offset.sect_off,
4361 phex (signature, sizeof (signature)));
348e048f 4362
dee91e82 4363 info_ptr += length;
8b70b953 4364 }
348e048f
DE
4365 }
4366
3019eac3
DE
4367 return types_htab;
4368}
4369
4370/* Create the hash table of all entries in the .debug_types section,
4371 and initialize all_type_units.
4372 The result is zero if there is an error (e.g. missing .debug_types section),
4373 otherwise non-zero. */
4374
4375static int
4376create_all_type_units (struct objfile *objfile)
4377{
4378 htab_t types_htab;
b4dd5633 4379 struct signatured_type **iter;
3019eac3
DE
4380
4381 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4382 if (types_htab == NULL)
4383 {
4384 dwarf2_per_objfile->signatured_types = NULL;
4385 return 0;
4386 }
4387
348e048f
DE
4388 dwarf2_per_objfile->signatured_types = types_htab;
4389
d467dd73
DE
4390 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4391 dwarf2_per_objfile->all_type_units
1fd400ff 4392 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4393 dwarf2_per_objfile->n_type_units
b4dd5633 4394 * sizeof (struct signatured_type *));
d467dd73
DE
4395 iter = &dwarf2_per_objfile->all_type_units[0];
4396 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4397 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4398 == dwarf2_per_objfile->n_type_units);
1fd400ff 4399
348e048f
DE
4400 return 1;
4401}
4402
380bca97 4403/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4404 Returns NULL if signature SIG is not present in the table.
4405 It is up to the caller to complain about this. */
348e048f
DE
4406
4407static struct signatured_type *
e319fa28 4408lookup_signatured_type (ULONGEST sig)
348e048f
DE
4409{
4410 struct signatured_type find_entry, *entry;
4411
4412 if (dwarf2_per_objfile->signatured_types == NULL)
5a8b3f62 4413 return NULL;
348e048f
DE
4414 find_entry.signature = sig;
4415 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4416 return entry;
4417}
42e7ad6c
DE
4418\f
4419/* Low level DIE reading support. */
348e048f 4420
d85a05f0
DJ
4421/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4422
4423static void
4424init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4425 struct dwarf2_cu *cu,
3019eac3
DE
4426 struct dwarf2_section_info *section,
4427 struct dwo_file *dwo_file)
d85a05f0 4428{
fceca515 4429 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4430 reader->abfd = section->asection->owner;
d85a05f0 4431 reader->cu = cu;
3019eac3 4432 reader->dwo_file = dwo_file;
dee91e82
DE
4433 reader->die_section = section;
4434 reader->buffer = section->buffer;
f664829e 4435 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4436}
4437
b0c7bfa9
DE
4438/* Subroutine of init_cutu_and_read_dies to simplify it.
4439 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4440 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4441 already.
4442
4443 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4444 from it to the DIE in the DWO. If NULL we are skipping the stub.
4445 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4446 are filled in with the info of the DIE from the DWO file.
4447 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4448 provided an abbrev table to use.
4449 The result is non-zero if a valid (non-dummy) DIE was found. */
4450
4451static int
4452read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4453 struct dwo_unit *dwo_unit,
4454 int abbrev_table_provided,
4455 struct die_info *stub_comp_unit_die,
4456 struct die_reader_specs *result_reader,
4457 gdb_byte **result_info_ptr,
4458 struct die_info **result_comp_unit_die,
4459 int *result_has_children)
4460{
4461 struct objfile *objfile = dwarf2_per_objfile->objfile;
4462 struct dwarf2_cu *cu = this_cu->cu;
4463 struct dwarf2_section_info *section;
4464 bfd *abfd;
4465 gdb_byte *begin_info_ptr, *info_ptr;
4466 const char *comp_dir_string;
4467 ULONGEST signature; /* Or dwo_id. */
4468 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4469 int i,num_extra_attrs;
4470 struct dwarf2_section_info *dwo_abbrev_section;
4471 struct attribute *attr;
4472 struct die_info *comp_unit_die;
4473
4474 /* These attributes aren't processed until later:
4475 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4476 However, the attribute is found in the stub which we won't have later.
4477 In order to not impose this complication on the rest of the code,
4478 we read them here and copy them to the DWO CU/TU die. */
4479
4480 stmt_list = NULL;
4481 low_pc = NULL;
4482 high_pc = NULL;
4483 ranges = NULL;
4484 comp_dir = NULL;
4485
4486 if (stub_comp_unit_die != NULL)
4487 {
4488 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4489 DWO file. */
4490 if (! this_cu->is_debug_types)
4491 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4492 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4493 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4494 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4495 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4496
4497 /* There should be a DW_AT_addr_base attribute here (if needed).
4498 We need the value before we can process DW_FORM_GNU_addr_index. */
4499 cu->addr_base = 0;
4500 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4501 if (attr)
4502 cu->addr_base = DW_UNSND (attr);
4503
4504 /* There should be a DW_AT_ranges_base attribute here (if needed).
4505 We need the value before we can process DW_AT_ranges. */
4506 cu->ranges_base = 0;
4507 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4508 if (attr)
4509 cu->ranges_base = DW_UNSND (attr);
4510 }
4511
4512 /* Set up for reading the DWO CU/TU. */
4513 cu->dwo_unit = dwo_unit;
4514 section = dwo_unit->section;
4515 dwarf2_read_section (objfile, section);
4516 abfd = section->asection->owner;
4517 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4518 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4519 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4520
4521 if (this_cu->is_debug_types)
4522 {
4523 ULONGEST header_signature;
4524 cu_offset type_offset_in_tu;
4525 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4526
4527 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4528 dwo_abbrev_section,
4529 info_ptr,
4530 &header_signature,
4531 &type_offset_in_tu);
4532 gdb_assert (sig_type->signature == header_signature);
4533 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4534 /* For DWOs coming from DWP files, we don't know the CU length
4535 nor the type's offset in the TU until now. */
4536 dwo_unit->length = get_cu_length (&cu->header);
4537 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4538
4539 /* Establish the type offset that can be used to lookup the type.
4540 For DWO files, we don't know it until now. */
4541 sig_type->type_offset_in_section.sect_off =
4542 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4543 }
4544 else
4545 {
4546 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4547 dwo_abbrev_section,
4548 info_ptr, 0);
4549 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4550 /* For DWOs coming from DWP files, we don't know the CU length
4551 until now. */
4552 dwo_unit->length = get_cu_length (&cu->header);
4553 }
4554
4555 /* Replace the CU's original abbrev table with the DWO's. */
4556 if (abbrev_table_provided)
4557 {
4558 /* Don't free the provided abbrev table, the caller of
4559 init_cutu_and_read_dies owns it. */
4560 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4561 make_cleanup (dwarf2_free_abbrev_table, cu);
4562 }
4563 else
4564 {
4565 dwarf2_free_abbrev_table (cu);
4566 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4567 }
4568
4569 /* Read in the die, but leave space to copy over the attributes
4570 from the stub. This has the benefit of simplifying the rest of
4571 the code - all the work to maintain the illusion of a single
4572 DW_TAG_{compile,type}_unit DIE is done here. */
4573 num_extra_attrs = ((stmt_list != NULL)
4574 + (low_pc != NULL)
4575 + (high_pc != NULL)
4576 + (ranges != NULL)
4577 + (comp_dir != NULL));
4578 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4579 result_has_children, num_extra_attrs);
4580
4581 /* Copy over the attributes from the stub to the DIE we just read in. */
4582 comp_unit_die = *result_comp_unit_die;
4583 i = comp_unit_die->num_attrs;
4584 if (stmt_list != NULL)
4585 comp_unit_die->attrs[i++] = *stmt_list;
4586 if (low_pc != NULL)
4587 comp_unit_die->attrs[i++] = *low_pc;
4588 if (high_pc != NULL)
4589 comp_unit_die->attrs[i++] = *high_pc;
4590 if (ranges != NULL)
4591 comp_unit_die->attrs[i++] = *ranges;
4592 if (comp_dir != NULL)
4593 comp_unit_die->attrs[i++] = *comp_dir;
4594 comp_unit_die->num_attrs += num_extra_attrs;
4595
4596 /* Skip dummy compilation units. */
4597 if (info_ptr >= begin_info_ptr + dwo_unit->length
4598 || peek_abbrev_code (abfd, info_ptr) == 0)
4599 return 0;
4600
4601 *result_info_ptr = info_ptr;
4602 return 1;
4603}
4604
4605/* Subroutine of init_cutu_and_read_dies to simplify it.
4606 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4607 If the specified DWO unit cannot be found an error is thrown. */
4608
4609static struct dwo_unit *
4610lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4611 struct die_info *comp_unit_die)
4612{
4613 struct dwarf2_cu *cu = this_cu->cu;
4614 struct attribute *attr;
4615 ULONGEST signature;
4616 struct dwo_unit *dwo_unit;
4617 const char *comp_dir, *dwo_name;
4618
4619 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4620 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4621 gdb_assert (attr != NULL);
4622 dwo_name = DW_STRING (attr);
4623 comp_dir = NULL;
4624 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4625 if (attr)
4626 comp_dir = DW_STRING (attr);
4627
4628 if (this_cu->is_debug_types)
4629 {
4630 struct signatured_type *sig_type;
4631
4632 /* Since this_cu is the first member of struct signatured_type,
4633 we can go from a pointer to one to a pointer to the other. */
4634 sig_type = (struct signatured_type *) this_cu;
4635 signature = sig_type->signature;
4636 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4637 }
4638 else
4639 {
4640 struct attribute *attr;
4641
4642 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4643 if (! attr)
4644 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4645 " [in module %s]"),
4646 dwo_name, this_cu->objfile->name);
4647 signature = DW_UNSND (attr);
4648 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4649 signature);
4650 }
4651
4652 if (dwo_unit == NULL)
4653 {
4654 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4655 " with ID %s [in module %s]"),
4656 this_cu->offset.sect_off,
4657 phex (signature, sizeof (signature)),
4658 this_cu->objfile->name);
4659 }
4660
4661 return dwo_unit;
4662}
4663
fd820528 4664/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4665 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4666
f4dc4d17
DE
4667 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4668 Otherwise the table specified in the comp unit header is read in and used.
4669 This is an optimization for when we already have the abbrev table.
4670
dee91e82
DE
4671 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4672 Otherwise, a new CU is allocated with xmalloc.
4673
4674 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4675 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4676
4677 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4678 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4679
70221824 4680static void
fd820528 4681init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4682 struct abbrev_table *abbrev_table,
fd820528
DE
4683 int use_existing_cu, int keep,
4684 die_reader_func_ftype *die_reader_func,
4685 void *data)
c906108c 4686{
dee91e82 4687 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4688 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4689 bfd *abfd = section->asection->owner;
dee91e82
DE
4690 struct dwarf2_cu *cu;
4691 gdb_byte *begin_info_ptr, *info_ptr;
4692 struct die_reader_specs reader;
d85a05f0 4693 struct die_info *comp_unit_die;
dee91e82 4694 int has_children;
d85a05f0 4695 struct attribute *attr;
dee91e82
DE
4696 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4697 struct signatured_type *sig_type = NULL;
4bdcc0c1 4698 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4699 /* Non-zero if CU currently points to a DWO file and we need to
4700 reread it. When this happens we need to reread the skeleton die
4701 before we can reread the DWO file. */
4702 int rereading_dwo_cu = 0;
c906108c 4703
09406207
DE
4704 if (dwarf2_die_debug)
4705 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4706 this_cu->is_debug_types ? "type" : "comp",
4707 this_cu->offset.sect_off);
4708
dee91e82
DE
4709 if (use_existing_cu)
4710 gdb_assert (keep);
23745b47 4711
dee91e82
DE
4712 cleanups = make_cleanup (null_cleanup, NULL);
4713
4714 /* This is cheap if the section is already read in. */
4715 dwarf2_read_section (objfile, section);
4716
4717 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4718
4719 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4720
4721 if (use_existing_cu && this_cu->cu != NULL)
4722 {
4723 cu = this_cu->cu;
42e7ad6c
DE
4724
4725 /* If this CU is from a DWO file we need to start over, we need to
4726 refetch the attributes from the skeleton CU.
4727 This could be optimized by retrieving those attributes from when we
4728 were here the first time: the previous comp_unit_die was stored in
4729 comp_unit_obstack. But there's no data yet that we need this
4730 optimization. */
4731 if (cu->dwo_unit != NULL)
4732 rereading_dwo_cu = 1;
dee91e82
DE
4733 }
4734 else
4735 {
4736 /* If !use_existing_cu, this_cu->cu must be NULL. */
4737 gdb_assert (this_cu->cu == NULL);
4738
4739 cu = xmalloc (sizeof (*cu));
4740 init_one_comp_unit (cu, this_cu);
4741
4742 /* If an error occurs while loading, release our storage. */
4743 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4744 }
dee91e82 4745
b0c7bfa9 4746 /* Get the header. */
42e7ad6c
DE
4747 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4748 {
4749 /* We already have the header, there's no need to read it in again. */
4750 info_ptr += cu->header.first_die_offset.cu_off;
4751 }
4752 else
4753 {
3019eac3 4754 if (this_cu->is_debug_types)
dee91e82
DE
4755 {
4756 ULONGEST signature;
42e7ad6c 4757 cu_offset type_offset_in_tu;
dee91e82 4758
4bdcc0c1
DE
4759 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4760 abbrev_section, info_ptr,
42e7ad6c
DE
4761 &signature,
4762 &type_offset_in_tu);
dee91e82 4763
42e7ad6c
DE
4764 /* Since per_cu is the first member of struct signatured_type,
4765 we can go from a pointer to one to a pointer to the other. */
4766 sig_type = (struct signatured_type *) this_cu;
4767 gdb_assert (sig_type->signature == signature);
4768 gdb_assert (sig_type->type_offset_in_tu.cu_off
4769 == type_offset_in_tu.cu_off);
dee91e82
DE
4770 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4771
42e7ad6c
DE
4772 /* LENGTH has not been set yet for type units if we're
4773 using .gdb_index. */
1ce1cefd 4774 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4775
4776 /* Establish the type offset that can be used to lookup the type. */
4777 sig_type->type_offset_in_section.sect_off =
4778 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4779 }
4780 else
4781 {
4bdcc0c1
DE
4782 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4783 abbrev_section,
4784 info_ptr, 0);
dee91e82
DE
4785
4786 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4787 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4788 }
4789 }
10b3939b 4790
6caca83c 4791 /* Skip dummy compilation units. */
dee91e82 4792 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4793 || peek_abbrev_code (abfd, info_ptr) == 0)
4794 {
dee91e82 4795 do_cleanups (cleanups);
21b2bd31 4796 return;
6caca83c
CC
4797 }
4798
433df2d4
DE
4799 /* If we don't have them yet, read the abbrevs for this compilation unit.
4800 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4801 done. Note that it's important that if the CU had an abbrev table
4802 on entry we don't free it when we're done: Somewhere up the call stack
4803 it may be in use. */
f4dc4d17
DE
4804 if (abbrev_table != NULL)
4805 {
4806 gdb_assert (cu->abbrev_table == NULL);
4807 gdb_assert (cu->header.abbrev_offset.sect_off
4808 == abbrev_table->offset.sect_off);
4809 cu->abbrev_table = abbrev_table;
4810 }
4811 else if (cu->abbrev_table == NULL)
dee91e82 4812 {
4bdcc0c1 4813 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4814 make_cleanup (dwarf2_free_abbrev_table, cu);
4815 }
42e7ad6c
DE
4816 else if (rereading_dwo_cu)
4817 {
4818 dwarf2_free_abbrev_table (cu);
4819 dwarf2_read_abbrevs (cu, abbrev_section);
4820 }
af703f96 4821
dee91e82 4822 /* Read the top level CU/TU die. */
3019eac3 4823 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4824 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4825
b0c7bfa9
DE
4826 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4827 from the DWO file.
4828 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4829 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
4830 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4831 if (attr)
4832 {
3019eac3 4833 struct dwo_unit *dwo_unit;
b0c7bfa9 4834 struct die_info *dwo_comp_unit_die;
3019eac3
DE
4835
4836 if (has_children)
4837 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4838 " has children (offset 0x%x) [in module %s]"),
4839 this_cu->offset.sect_off, bfd_get_filename (abfd));
b0c7bfa9
DE
4840 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
4841 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4842 abbrev_table != NULL,
4843 comp_unit_die,
4844 &reader, &info_ptr,
4845 &dwo_comp_unit_die, &has_children) == 0)
3019eac3 4846 {
b0c7bfa9 4847 /* Dummy die. */
3019eac3
DE
4848 do_cleanups (cleanups);
4849 return;
4850 }
b0c7bfa9 4851 comp_unit_die = dwo_comp_unit_die;
3019eac3
DE
4852 }
4853
b0c7bfa9 4854 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
4855 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4856
b0c7bfa9 4857 /* Done, clean up. */
dee91e82 4858 if (free_cu_cleanup != NULL)
348e048f 4859 {
dee91e82
DE
4860 if (keep)
4861 {
4862 /* We've successfully allocated this compilation unit. Let our
4863 caller clean it up when finished with it. */
4864 discard_cleanups (free_cu_cleanup);
4865
4866 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4867 So we have to manually free the abbrev table. */
4868 dwarf2_free_abbrev_table (cu);
4869
4870 /* Link this CU into read_in_chain. */
4871 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4872 dwarf2_per_objfile->read_in_chain = this_cu;
4873 }
4874 else
4875 do_cleanups (free_cu_cleanup);
348e048f 4876 }
dee91e82
DE
4877
4878 do_cleanups (cleanups);
4879}
4880
3019eac3
DE
4881/* Read CU/TU THIS_CU in section SECTION,
4882 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4883 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4884 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4885
4886 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4887 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4888
4889 We fill in THIS_CU->length.
4890
4891 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4892 linker) then DIE_READER_FUNC will not get called.
4893
4894 THIS_CU->cu is always freed when done.
3019eac3
DE
4895 This is done in order to not leave THIS_CU->cu in a state where we have
4896 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4897
4898static void
4899init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4900 struct dwarf2_section_info *abbrev_section,
3019eac3 4901 struct dwo_file *dwo_file,
dee91e82
DE
4902 die_reader_func_ftype *die_reader_func,
4903 void *data)
4904{
4905 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4906 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4907 bfd *abfd = section->asection->owner;
dee91e82
DE
4908 struct dwarf2_cu cu;
4909 gdb_byte *begin_info_ptr, *info_ptr;
4910 struct die_reader_specs reader;
4911 struct cleanup *cleanups;
4912 struct die_info *comp_unit_die;
4913 int has_children;
4914
09406207
DE
4915 if (dwarf2_die_debug)
4916 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4917 this_cu->is_debug_types ? "type" : "comp",
4918 this_cu->offset.sect_off);
4919
dee91e82
DE
4920 gdb_assert (this_cu->cu == NULL);
4921
dee91e82
DE
4922 /* This is cheap if the section is already read in. */
4923 dwarf2_read_section (objfile, section);
4924
4925 init_one_comp_unit (&cu, this_cu);
4926
4927 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4928
4929 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4930 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4931 abbrev_section, info_ptr,
3019eac3 4932 this_cu->is_debug_types);
dee91e82 4933
1ce1cefd 4934 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4935
4936 /* Skip dummy compilation units. */
4937 if (info_ptr >= begin_info_ptr + this_cu->length
4938 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4939 {
dee91e82 4940 do_cleanups (cleanups);
21b2bd31 4941 return;
93311388 4942 }
72bf9492 4943
dee91e82
DE
4944 dwarf2_read_abbrevs (&cu, abbrev_section);
4945 make_cleanup (dwarf2_free_abbrev_table, &cu);
4946
3019eac3 4947 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4948 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4949
4950 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4951
4952 do_cleanups (cleanups);
4953}
4954
3019eac3
DE
4955/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4956 does not lookup the specified DWO file.
4957 This cannot be used to read DWO files.
dee91e82
DE
4958
4959 THIS_CU->cu is always freed when done.
3019eac3
DE
4960 This is done in order to not leave THIS_CU->cu in a state where we have
4961 to care whether it refers to the "main" CU or the DWO CU.
4962 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4963
4964static void
4965init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4966 die_reader_func_ftype *die_reader_func,
4967 void *data)
4968{
4969 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4970 get_abbrev_section_for_cu (this_cu),
3019eac3 4971 NULL,
dee91e82
DE
4972 die_reader_func, data);
4973}
0018ea6f
DE
4974\f
4975/* Type Unit Groups.
dee91e82 4976
0018ea6f
DE
4977 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4978 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4979 so that all types coming from the same compilation (.o file) are grouped
4980 together. A future step could be to put the types in the same symtab as
4981 the CU the types ultimately came from. */
ff013f42 4982
f4dc4d17
DE
4983static hashval_t
4984hash_type_unit_group (const void *item)
4985{
094b34ac 4986 const struct type_unit_group *tu_group = item;
f4dc4d17 4987
094b34ac 4988 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4989}
348e048f
DE
4990
4991static int
f4dc4d17 4992eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4993{
f4dc4d17
DE
4994 const struct type_unit_group *lhs = item_lhs;
4995 const struct type_unit_group *rhs = item_rhs;
348e048f 4996
094b34ac 4997 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4998}
348e048f 4999
f4dc4d17
DE
5000/* Allocate a hash table for type unit groups. */
5001
5002static htab_t
5003allocate_type_unit_groups_table (void)
5004{
5005 return htab_create_alloc_ex (3,
5006 hash_type_unit_group,
5007 eq_type_unit_group,
5008 NULL,
5009 &dwarf2_per_objfile->objfile->objfile_obstack,
5010 hashtab_obstack_allocate,
5011 dummy_obstack_deallocate);
5012}
dee91e82 5013
f4dc4d17
DE
5014/* Type units that don't have DW_AT_stmt_list are grouped into their own
5015 partial symtabs. We combine several TUs per psymtab to not let the size
5016 of any one psymtab grow too big. */
5017#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5018#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5019
094b34ac 5020/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5021 Create the type_unit_group object used to hold one or more TUs. */
5022
5023static struct type_unit_group *
094b34ac 5024create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5025{
5026 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5027 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5028 struct type_unit_group *tu_group;
f4dc4d17
DE
5029
5030 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5031 struct type_unit_group);
094b34ac 5032 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5033 per_cu->objfile = objfile;
5034 per_cu->is_debug_types = 1;
796a7ff8 5035 per_cu->type_unit_group = tu_group;
f4dc4d17 5036
094b34ac
DE
5037 if (dwarf2_per_objfile->using_index)
5038 {
5039 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5040 struct dwarf2_per_cu_quick_data);
5041 tu_group->t.first_tu = cu->per_cu;
5042 }
5043 else
5044 {
5045 unsigned int line_offset = line_offset_struct.sect_off;
5046 struct partial_symtab *pst;
5047 char *name;
5048
5049 /* Give the symtab a useful name for debug purposes. */
5050 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5051 name = xstrprintf ("<type_units_%d>",
5052 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5053 else
5054 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5055
5056 pst = create_partial_symtab (per_cu, name);
5057 pst->anonymous = 1;
f4dc4d17 5058
094b34ac
DE
5059 xfree (name);
5060 }
f4dc4d17 5061
094b34ac
DE
5062 tu_group->hash.dwo_unit = cu->dwo_unit;
5063 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5064
5065 return tu_group;
5066}
5067
094b34ac
DE
5068/* Look up the type_unit_group for type unit CU, and create it if necessary.
5069 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5070
5071static struct type_unit_group *
094b34ac 5072get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5073{
5074 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5075 struct type_unit_group *tu_group;
5076 void **slot;
5077 unsigned int line_offset;
5078 struct type_unit_group type_unit_group_for_lookup;
5079
5080 if (dwarf2_per_objfile->type_unit_groups == NULL)
5081 {
5082 dwarf2_per_objfile->type_unit_groups =
5083 allocate_type_unit_groups_table ();
5084 }
5085
5086 /* Do we need to create a new group, or can we use an existing one? */
5087
5088 if (stmt_list)
5089 {
5090 line_offset = DW_UNSND (stmt_list);
5091 ++tu_stats->nr_symtab_sharers;
5092 }
5093 else
5094 {
5095 /* Ugh, no stmt_list. Rare, but we have to handle it.
5096 We can do various things here like create one group per TU or
5097 spread them over multiple groups to split up the expansion work.
5098 To avoid worst case scenarios (too many groups or too large groups)
5099 we, umm, group them in bunches. */
5100 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5101 | (tu_stats->nr_stmt_less_type_units
5102 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5103 ++tu_stats->nr_stmt_less_type_units;
5104 }
5105
094b34ac
DE
5106 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5107 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5108 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5109 &type_unit_group_for_lookup, INSERT);
5110 if (*slot != NULL)
5111 {
5112 tu_group = *slot;
5113 gdb_assert (tu_group != NULL);
5114 }
5115 else
5116 {
5117 sect_offset line_offset_struct;
5118
5119 line_offset_struct.sect_off = line_offset;
094b34ac 5120 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5121 *slot = tu_group;
5122 ++tu_stats->nr_symtabs;
5123 }
5124
5125 return tu_group;
5126}
5127
5128/* Struct used to sort TUs by their abbreviation table offset. */
5129
5130struct tu_abbrev_offset
5131{
5132 struct signatured_type *sig_type;
5133 sect_offset abbrev_offset;
5134};
5135
5136/* Helper routine for build_type_unit_groups, passed to qsort. */
5137
5138static int
5139sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5140{
5141 const struct tu_abbrev_offset * const *a = ap;
5142 const struct tu_abbrev_offset * const *b = bp;
5143 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5144 unsigned int boff = (*b)->abbrev_offset.sect_off;
5145
5146 return (aoff > boff) - (aoff < boff);
5147}
5148
5149/* A helper function to add a type_unit_group to a table. */
5150
5151static int
5152add_type_unit_group_to_table (void **slot, void *datum)
5153{
5154 struct type_unit_group *tu_group = *slot;
5155 struct type_unit_group ***datap = datum;
5156
5157 **datap = tu_group;
5158 ++*datap;
5159
5160 return 1;
5161}
5162
5163/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5164 each one passing FUNC,DATA.
5165
5166 The efficiency is because we sort TUs by the abbrev table they use and
5167 only read each abbrev table once. In one program there are 200K TUs
5168 sharing 8K abbrev tables.
5169
5170 The main purpose of this function is to support building the
5171 dwarf2_per_objfile->type_unit_groups table.
5172 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5173 can collapse the search space by grouping them by stmt_list.
5174 The savings can be significant, in the same program from above the 200K TUs
5175 share 8K stmt_list tables.
5176
5177 FUNC is expected to call get_type_unit_group, which will create the
5178 struct type_unit_group if necessary and add it to
5179 dwarf2_per_objfile->type_unit_groups. */
5180
5181static void
5182build_type_unit_groups (die_reader_func_ftype *func, void *data)
5183{
5184 struct objfile *objfile = dwarf2_per_objfile->objfile;
5185 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5186 struct cleanup *cleanups;
5187 struct abbrev_table *abbrev_table;
5188 sect_offset abbrev_offset;
5189 struct tu_abbrev_offset *sorted_by_abbrev;
5190 struct type_unit_group **iter;
5191 int i;
5192
5193 /* It's up to the caller to not call us multiple times. */
5194 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5195
5196 if (dwarf2_per_objfile->n_type_units == 0)
5197 return;
5198
5199 /* TUs typically share abbrev tables, and there can be way more TUs than
5200 abbrev tables. Sort by abbrev table to reduce the number of times we
5201 read each abbrev table in.
5202 Alternatives are to punt or to maintain a cache of abbrev tables.
5203 This is simpler and efficient enough for now.
5204
5205 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5206 symtab to use). Typically TUs with the same abbrev offset have the same
5207 stmt_list value too so in practice this should work well.
5208
5209 The basic algorithm here is:
5210
5211 sort TUs by abbrev table
5212 for each TU with same abbrev table:
5213 read abbrev table if first user
5214 read TU top level DIE
5215 [IWBN if DWO skeletons had DW_AT_stmt_list]
5216 call FUNC */
5217
5218 if (dwarf2_read_debug)
5219 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5220
5221 /* Sort in a separate table to maintain the order of all_type_units
5222 for .gdb_index: TU indices directly index all_type_units. */
5223 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5224 dwarf2_per_objfile->n_type_units);
5225 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5226 {
5227 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5228
5229 sorted_by_abbrev[i].sig_type = sig_type;
5230 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5231 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5232 sig_type->per_cu.offset);
5233 }
5234 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5235 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5236 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5237
094b34ac
DE
5238 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5239 called any number of times, so we don't reset tu_stats here. */
5240
f4dc4d17
DE
5241 abbrev_offset.sect_off = ~(unsigned) 0;
5242 abbrev_table = NULL;
5243 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5244
5245 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5246 {
5247 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5248
5249 /* Switch to the next abbrev table if necessary. */
5250 if (abbrev_table == NULL
5251 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5252 {
5253 if (abbrev_table != NULL)
5254 {
5255 abbrev_table_free (abbrev_table);
5256 /* Reset to NULL in case abbrev_table_read_table throws
5257 an error: abbrev_table_free_cleanup will get called. */
5258 abbrev_table = NULL;
5259 }
5260 abbrev_offset = tu->abbrev_offset;
5261 abbrev_table =
5262 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5263 abbrev_offset);
5264 ++tu_stats->nr_uniq_abbrev_tables;
5265 }
5266
5267 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5268 func, data);
5269 }
5270
5271 /* Create a vector of pointers to primary type units to make it easy to
5272 iterate over them and CUs. See dw2_get_primary_cu. */
5273 dwarf2_per_objfile->n_type_unit_groups =
5274 htab_elements (dwarf2_per_objfile->type_unit_groups);
5275 dwarf2_per_objfile->all_type_unit_groups =
5276 obstack_alloc (&objfile->objfile_obstack,
5277 dwarf2_per_objfile->n_type_unit_groups
5278 * sizeof (struct type_unit_group *));
5279 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5280 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5281 add_type_unit_group_to_table, &iter);
5282 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5283 == dwarf2_per_objfile->n_type_unit_groups);
5284
5285 do_cleanups (cleanups);
5286
5287 if (dwarf2_read_debug)
5288 {
5289 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5290 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5291 dwarf2_per_objfile->n_type_units);
5292 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5293 tu_stats->nr_uniq_abbrev_tables);
5294 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5295 tu_stats->nr_symtabs);
5296 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5297 tu_stats->nr_symtab_sharers);
5298 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5299 tu_stats->nr_stmt_less_type_units);
5300 }
5301}
0018ea6f
DE
5302\f
5303/* Partial symbol tables. */
5304
5305/* Create a psymtab named NAME and assign it to PER_CU.
5306
5307 The caller must fill in the following details:
5308 dirname, textlow, texthigh. */
5309
5310static struct partial_symtab *
5311create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5312{
5313 struct objfile *objfile = per_cu->objfile;
5314 struct partial_symtab *pst;
5315
5316 pst = start_psymtab_common (objfile, objfile->section_offsets,
5317 name, 0,
5318 objfile->global_psymbols.next,
5319 objfile->static_psymbols.next);
5320
5321 pst->psymtabs_addrmap_supported = 1;
5322
5323 /* This is the glue that links PST into GDB's symbol API. */
5324 pst->read_symtab_private = per_cu;
5325 pst->read_symtab = dwarf2_read_symtab;
5326 per_cu->v.psymtab = pst;
5327
5328 return pst;
5329}
5330
5331/* die_reader_func for process_psymtab_comp_unit. */
5332
5333static void
5334process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5335 gdb_byte *info_ptr,
5336 struct die_info *comp_unit_die,
5337 int has_children,
5338 void *data)
5339{
5340 struct dwarf2_cu *cu = reader->cu;
5341 struct objfile *objfile = cu->objfile;
5342 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5343 struct attribute *attr;
5344 CORE_ADDR baseaddr;
5345 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5346 struct partial_symtab *pst;
5347 int has_pc_info;
5348 const char *filename;
5349 int *want_partial_unit_ptr = data;
5350
5351 if (comp_unit_die->tag == DW_TAG_partial_unit
5352 && (want_partial_unit_ptr == NULL
5353 || !*want_partial_unit_ptr))
5354 return;
5355
5356 gdb_assert (! per_cu->is_debug_types);
5357
5358 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5359
5360 cu->list_in_scope = &file_symbols;
5361
5362 /* Allocate a new partial symbol table structure. */
5363 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5364 if (attr == NULL || !DW_STRING (attr))
5365 filename = "";
5366 else
5367 filename = DW_STRING (attr);
5368
5369 pst = create_partial_symtab (per_cu, filename);
5370
5371 /* This must be done before calling dwarf2_build_include_psymtabs. */
5372 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5373 if (attr != NULL)
5374 pst->dirname = DW_STRING (attr);
5375
5376 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5377
5378 dwarf2_find_base_address (comp_unit_die, cu);
5379
5380 /* Possibly set the default values of LOWPC and HIGHPC from
5381 `DW_AT_ranges'. */
5382 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5383 &best_highpc, cu, pst);
5384 if (has_pc_info == 1 && best_lowpc < best_highpc)
5385 /* Store the contiguous range if it is not empty; it can be empty for
5386 CUs with no code. */
5387 addrmap_set_empty (objfile->psymtabs_addrmap,
5388 best_lowpc + baseaddr,
5389 best_highpc + baseaddr - 1, pst);
5390
5391 /* Check if comp unit has_children.
5392 If so, read the rest of the partial symbols from this comp unit.
5393 If not, there's no more debug_info for this comp unit. */
5394 if (has_children)
5395 {
5396 struct partial_die_info *first_die;
5397 CORE_ADDR lowpc, highpc;
5398
5399 lowpc = ((CORE_ADDR) -1);
5400 highpc = ((CORE_ADDR) 0);
5401
5402 first_die = load_partial_dies (reader, info_ptr, 1);
5403
5404 scan_partial_symbols (first_die, &lowpc, &highpc,
5405 ! has_pc_info, cu);
5406
5407 /* If we didn't find a lowpc, set it to highpc to avoid
5408 complaints from `maint check'. */
5409 if (lowpc == ((CORE_ADDR) -1))
5410 lowpc = highpc;
5411
5412 /* If the compilation unit didn't have an explicit address range,
5413 then use the information extracted from its child dies. */
5414 if (! has_pc_info)
5415 {
5416 best_lowpc = lowpc;
5417 best_highpc = highpc;
5418 }
5419 }
5420 pst->textlow = best_lowpc + baseaddr;
5421 pst->texthigh = best_highpc + baseaddr;
5422
5423 pst->n_global_syms = objfile->global_psymbols.next -
5424 (objfile->global_psymbols.list + pst->globals_offset);
5425 pst->n_static_syms = objfile->static_psymbols.next -
5426 (objfile->static_psymbols.list + pst->statics_offset);
5427 sort_pst_symbols (objfile, pst);
5428
5429 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5430 {
5431 int i;
5432 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5433 struct dwarf2_per_cu_data *iter;
5434
5435 /* Fill in 'dependencies' here; we fill in 'users' in a
5436 post-pass. */
5437 pst->number_of_dependencies = len;
5438 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5439 len * sizeof (struct symtab *));
5440 for (i = 0;
5441 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5442 i, iter);
5443 ++i)
5444 pst->dependencies[i] = iter->v.psymtab;
5445
5446 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5447 }
5448
5449 /* Get the list of files included in the current compilation unit,
5450 and build a psymtab for each of them. */
5451 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5452
5453 if (dwarf2_read_debug)
5454 {
5455 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5456
5457 fprintf_unfiltered (gdb_stdlog,
5458 "Psymtab for %s unit @0x%x: %s - %s"
5459 ", %d global, %d static syms\n",
5460 per_cu->is_debug_types ? "type" : "comp",
5461 per_cu->offset.sect_off,
5462 paddress (gdbarch, pst->textlow),
5463 paddress (gdbarch, pst->texthigh),
5464 pst->n_global_syms, pst->n_static_syms);
5465 }
5466}
5467
5468/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5469 Process compilation unit THIS_CU for a psymtab. */
5470
5471static void
5472process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5473 int want_partial_unit)
5474{
5475 /* If this compilation unit was already read in, free the
5476 cached copy in order to read it in again. This is
5477 necessary because we skipped some symbols when we first
5478 read in the compilation unit (see load_partial_dies).
5479 This problem could be avoided, but the benefit is unclear. */
5480 if (this_cu->cu != NULL)
5481 free_one_cached_comp_unit (this_cu);
5482
5483 gdb_assert (! this_cu->is_debug_types);
5484 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5485 process_psymtab_comp_unit_reader,
5486 &want_partial_unit);
5487
5488 /* Age out any secondary CUs. */
5489 age_cached_comp_units ();
5490}
f4dc4d17
DE
5491
5492/* Reader function for build_type_psymtabs. */
5493
5494static void
5495build_type_psymtabs_reader (const struct die_reader_specs *reader,
5496 gdb_byte *info_ptr,
5497 struct die_info *type_unit_die,
5498 int has_children,
5499 void *data)
5500{
5501 struct objfile *objfile = dwarf2_per_objfile->objfile;
5502 struct dwarf2_cu *cu = reader->cu;
5503 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5504 struct type_unit_group *tu_group;
5505 struct attribute *attr;
5506 struct partial_die_info *first_die;
5507 CORE_ADDR lowpc, highpc;
5508 struct partial_symtab *pst;
5509
5510 gdb_assert (data == NULL);
5511
5512 if (! has_children)
5513 return;
5514
5515 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5516 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5517
094b34ac 5518 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5519
5520 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5521 cu->list_in_scope = &file_symbols;
5522 pst = create_partial_symtab (per_cu, "");
5523 pst->anonymous = 1;
5524
5525 first_die = load_partial_dies (reader, info_ptr, 1);
5526
5527 lowpc = (CORE_ADDR) -1;
5528 highpc = (CORE_ADDR) 0;
5529 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5530
5531 pst->n_global_syms = objfile->global_psymbols.next -
5532 (objfile->global_psymbols.list + pst->globals_offset);
5533 pst->n_static_syms = objfile->static_psymbols.next -
5534 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5535 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5536}
5537
5538/* Traversal function for build_type_psymtabs. */
5539
5540static int
5541build_type_psymtab_dependencies (void **slot, void *info)
5542{
5543 struct objfile *objfile = dwarf2_per_objfile->objfile;
5544 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5545 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5546 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5547 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5548 struct dwarf2_per_cu_data *iter;
5549 int i;
5550
5551 gdb_assert (len > 0);
5552
5553 pst->number_of_dependencies = len;
5554 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5555 len * sizeof (struct psymtab *));
5556 for (i = 0;
094b34ac 5557 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5558 ++i)
5559 {
5560 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 5561 iter->type_unit_group = tu_group;
f4dc4d17
DE
5562 }
5563
094b34ac 5564 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5565
5566 return 1;
5567}
5568
5569/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5570 Build partial symbol tables for the .debug_types comp-units. */
5571
5572static void
5573build_type_psymtabs (struct objfile *objfile)
5574{
0e50663e 5575 if (! create_all_type_units (objfile))
348e048f
DE
5576 return;
5577
f4dc4d17
DE
5578 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5579
5580 /* Now that all TUs have been processed we can fill in the dependencies. */
5581 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5582 build_type_psymtab_dependencies, NULL);
348e048f
DE
5583}
5584
60606b2c
TT
5585/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5586
5587static void
5588psymtabs_addrmap_cleanup (void *o)
5589{
5590 struct objfile *objfile = o;
ec61707d 5591
60606b2c
TT
5592 objfile->psymtabs_addrmap = NULL;
5593}
5594
95554aad
TT
5595/* Compute the 'user' field for each psymtab in OBJFILE. */
5596
5597static void
5598set_partial_user (struct objfile *objfile)
5599{
5600 int i;
5601
5602 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5603 {
5604 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5605 struct partial_symtab *pst = per_cu->v.psymtab;
5606 int j;
5607
36586728
TT
5608 if (pst == NULL)
5609 continue;
5610
95554aad
TT
5611 for (j = 0; j < pst->number_of_dependencies; ++j)
5612 {
5613 /* Set the 'user' field only if it is not already set. */
5614 if (pst->dependencies[j]->user == NULL)
5615 pst->dependencies[j]->user = pst;
5616 }
5617 }
5618}
5619
93311388
DE
5620/* Build the partial symbol table by doing a quick pass through the
5621 .debug_info and .debug_abbrev sections. */
72bf9492 5622
93311388 5623static void
c67a9c90 5624dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5625{
60606b2c
TT
5626 struct cleanup *back_to, *addrmap_cleanup;
5627 struct obstack temp_obstack;
21b2bd31 5628 int i;
93311388 5629
45cfd468
DE
5630 if (dwarf2_read_debug)
5631 {
5632 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5633 objfile->name);
5634 }
5635
98bfdba5
PA
5636 dwarf2_per_objfile->reading_partial_symbols = 1;
5637
be391dca 5638 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5639
93311388
DE
5640 /* Any cached compilation units will be linked by the per-objfile
5641 read_in_chain. Make sure to free them when we're done. */
5642 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5643
348e048f
DE
5644 build_type_psymtabs (objfile);
5645
93311388 5646 create_all_comp_units (objfile);
c906108c 5647
60606b2c
TT
5648 /* Create a temporary address map on a temporary obstack. We later
5649 copy this to the final obstack. */
5650 obstack_init (&temp_obstack);
5651 make_cleanup_obstack_free (&temp_obstack);
5652 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5653 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5654
21b2bd31 5655 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5656 {
21b2bd31 5657 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5658
95554aad 5659 process_psymtab_comp_unit (per_cu, 0);
c906108c 5660 }
ff013f42 5661
95554aad
TT
5662 set_partial_user (objfile);
5663
ff013f42
JK
5664 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5665 &objfile->objfile_obstack);
60606b2c 5666 discard_cleanups (addrmap_cleanup);
ff013f42 5667
ae038cb0 5668 do_cleanups (back_to);
45cfd468
DE
5669
5670 if (dwarf2_read_debug)
5671 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5672 objfile->name);
ae038cb0
DJ
5673}
5674
3019eac3 5675/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5676
5677static void
dee91e82
DE
5678load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5679 gdb_byte *info_ptr,
5680 struct die_info *comp_unit_die,
5681 int has_children,
5682 void *data)
ae038cb0 5683{
dee91e82 5684 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5685
95554aad 5686 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5687
ae038cb0
DJ
5688 /* Check if comp unit has_children.
5689 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5690 If not, there's no more debug_info for this comp unit. */
d85a05f0 5691 if (has_children)
dee91e82
DE
5692 load_partial_dies (reader, info_ptr, 0);
5693}
98bfdba5 5694
dee91e82
DE
5695/* Load the partial DIEs for a secondary CU into memory.
5696 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5697
dee91e82
DE
5698static void
5699load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5700{
f4dc4d17
DE
5701 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5702 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5703}
5704
ae038cb0 5705static void
36586728
TT
5706read_comp_units_from_section (struct objfile *objfile,
5707 struct dwarf2_section_info *section,
5708 unsigned int is_dwz,
5709 int *n_allocated,
5710 int *n_comp_units,
5711 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5712{
be391dca 5713 gdb_byte *info_ptr;
36586728 5714 bfd *abfd = section->asection->owner;
be391dca 5715
36586728 5716 dwarf2_read_section (objfile, section);
ae038cb0 5717
36586728 5718 info_ptr = section->buffer;
6e70227d 5719
36586728 5720 while (info_ptr < section->buffer + section->size)
ae038cb0 5721 {
c764a876 5722 unsigned int length, initial_length_size;
ae038cb0 5723 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5724 sect_offset offset;
ae038cb0 5725
36586728 5726 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5727
5728 /* Read just enough information to find out where the next
5729 compilation unit is. */
36586728 5730 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5731
5732 /* Save the compilation unit for later lookup. */
5733 this_cu = obstack_alloc (&objfile->objfile_obstack,
5734 sizeof (struct dwarf2_per_cu_data));
5735 memset (this_cu, 0, sizeof (*this_cu));
5736 this_cu->offset = offset;
c764a876 5737 this_cu->length = length + initial_length_size;
36586728 5738 this_cu->is_dwz = is_dwz;
9291a0cd 5739 this_cu->objfile = objfile;
8a0459fd 5740 this_cu->section = section;
ae038cb0 5741
36586728 5742 if (*n_comp_units == *n_allocated)
ae038cb0 5743 {
36586728
TT
5744 *n_allocated *= 2;
5745 *all_comp_units = xrealloc (*all_comp_units,
5746 *n_allocated
5747 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5748 }
36586728
TT
5749 (*all_comp_units)[*n_comp_units] = this_cu;
5750 ++*n_comp_units;
ae038cb0
DJ
5751
5752 info_ptr = info_ptr + this_cu->length;
5753 }
36586728
TT
5754}
5755
5756/* Create a list of all compilation units in OBJFILE.
5757 This is only done for -readnow and building partial symtabs. */
5758
5759static void
5760create_all_comp_units (struct objfile *objfile)
5761{
5762 int n_allocated;
5763 int n_comp_units;
5764 struct dwarf2_per_cu_data **all_comp_units;
5765
5766 n_comp_units = 0;
5767 n_allocated = 10;
5768 all_comp_units = xmalloc (n_allocated
5769 * sizeof (struct dwarf2_per_cu_data *));
5770
5771 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5772 &n_allocated, &n_comp_units, &all_comp_units);
5773
5774 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5775 {
5776 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5777
5778 read_comp_units_from_section (objfile, &dwz->info, 1,
5779 &n_allocated, &n_comp_units,
5780 &all_comp_units);
5781 }
ae038cb0
DJ
5782
5783 dwarf2_per_objfile->all_comp_units
5784 = obstack_alloc (&objfile->objfile_obstack,
5785 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5786 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5787 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5788 xfree (all_comp_units);
5789 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5790}
5791
5734ee8b
DJ
5792/* Process all loaded DIEs for compilation unit CU, starting at
5793 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5794 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5795 DW_AT_ranges). If NEED_PC is set, then this function will set
5796 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5797 and record the covered ranges in the addrmap. */
c906108c 5798
72bf9492
DJ
5799static void
5800scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5801 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5802{
72bf9492 5803 struct partial_die_info *pdi;
c906108c 5804
91c24f0a
DC
5805 /* Now, march along the PDI's, descending into ones which have
5806 interesting children but skipping the children of the other ones,
5807 until we reach the end of the compilation unit. */
c906108c 5808
72bf9492 5809 pdi = first_die;
91c24f0a 5810
72bf9492
DJ
5811 while (pdi != NULL)
5812 {
5813 fixup_partial_die (pdi, cu);
c906108c 5814
f55ee35c 5815 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5816 children, so we need to look at them. Ditto for anonymous
5817 enums. */
933c6fe4 5818
72bf9492 5819 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5820 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5821 || pdi->tag == DW_TAG_imported_unit)
c906108c 5822 {
72bf9492 5823 switch (pdi->tag)
c906108c
SS
5824 {
5825 case DW_TAG_subprogram:
5734ee8b 5826 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5827 break;
72929c62 5828 case DW_TAG_constant:
c906108c
SS
5829 case DW_TAG_variable:
5830 case DW_TAG_typedef:
91c24f0a 5831 case DW_TAG_union_type:
72bf9492 5832 if (!pdi->is_declaration)
63d06c5c 5833 {
72bf9492 5834 add_partial_symbol (pdi, cu);
63d06c5c
DC
5835 }
5836 break;
c906108c 5837 case DW_TAG_class_type:
680b30c7 5838 case DW_TAG_interface_type:
c906108c 5839 case DW_TAG_structure_type:
72bf9492 5840 if (!pdi->is_declaration)
c906108c 5841 {
72bf9492 5842 add_partial_symbol (pdi, cu);
c906108c
SS
5843 }
5844 break;
91c24f0a 5845 case DW_TAG_enumeration_type:
72bf9492
DJ
5846 if (!pdi->is_declaration)
5847 add_partial_enumeration (pdi, cu);
c906108c
SS
5848 break;
5849 case DW_TAG_base_type:
a02abb62 5850 case DW_TAG_subrange_type:
c906108c 5851 /* File scope base type definitions are added to the partial
c5aa993b 5852 symbol table. */
72bf9492 5853 add_partial_symbol (pdi, cu);
c906108c 5854 break;
d9fa45fe 5855 case DW_TAG_namespace:
5734ee8b 5856 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5857 break;
5d7cb8df
JK
5858 case DW_TAG_module:
5859 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5860 break;
95554aad
TT
5861 case DW_TAG_imported_unit:
5862 {
5863 struct dwarf2_per_cu_data *per_cu;
5864
f4dc4d17
DE
5865 /* For now we don't handle imported units in type units. */
5866 if (cu->per_cu->is_debug_types)
5867 {
5868 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5869 " supported in type units [in module %s]"),
5870 cu->objfile->name);
5871 }
5872
95554aad 5873 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5874 pdi->is_dwz,
95554aad
TT
5875 cu->objfile);
5876
5877 /* Go read the partial unit, if needed. */
5878 if (per_cu->v.psymtab == NULL)
5879 process_psymtab_comp_unit (per_cu, 1);
5880
f4dc4d17 5881 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5882 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5883 }
5884 break;
c906108c
SS
5885 default:
5886 break;
5887 }
5888 }
5889
72bf9492
DJ
5890 /* If the die has a sibling, skip to the sibling. */
5891
5892 pdi = pdi->die_sibling;
5893 }
5894}
5895
5896/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5897
72bf9492 5898 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5899 name is concatenated with "::" and the partial DIE's name. For
5900 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5901 Enumerators are an exception; they use the scope of their parent
5902 enumeration type, i.e. the name of the enumeration type is not
5903 prepended to the enumerator.
91c24f0a 5904
72bf9492
DJ
5905 There are two complexities. One is DW_AT_specification; in this
5906 case "parent" means the parent of the target of the specification,
5907 instead of the direct parent of the DIE. The other is compilers
5908 which do not emit DW_TAG_namespace; in this case we try to guess
5909 the fully qualified name of structure types from their members'
5910 linkage names. This must be done using the DIE's children rather
5911 than the children of any DW_AT_specification target. We only need
5912 to do this for structures at the top level, i.e. if the target of
5913 any DW_AT_specification (if any; otherwise the DIE itself) does not
5914 have a parent. */
5915
5916/* Compute the scope prefix associated with PDI's parent, in
5917 compilation unit CU. The result will be allocated on CU's
5918 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5919 field. NULL is returned if no prefix is necessary. */
15d034d0 5920static const char *
72bf9492
DJ
5921partial_die_parent_scope (struct partial_die_info *pdi,
5922 struct dwarf2_cu *cu)
5923{
15d034d0 5924 const char *grandparent_scope;
72bf9492 5925 struct partial_die_info *parent, *real_pdi;
91c24f0a 5926
72bf9492
DJ
5927 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5928 then this means the parent of the specification DIE. */
5929
5930 real_pdi = pdi;
72bf9492 5931 while (real_pdi->has_specification)
36586728
TT
5932 real_pdi = find_partial_die (real_pdi->spec_offset,
5933 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5934
5935 parent = real_pdi->die_parent;
5936 if (parent == NULL)
5937 return NULL;
5938
5939 if (parent->scope_set)
5940 return parent->scope;
5941
5942 fixup_partial_die (parent, cu);
5943
10b3939b 5944 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5945
acebe513
UW
5946 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5947 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5948 Work around this problem here. */
5949 if (cu->language == language_cplus
6e70227d 5950 && parent->tag == DW_TAG_namespace
acebe513
UW
5951 && strcmp (parent->name, "::") == 0
5952 && grandparent_scope == NULL)
5953 {
5954 parent->scope = NULL;
5955 parent->scope_set = 1;
5956 return NULL;
5957 }
5958
9c6c53f7
SA
5959 if (pdi->tag == DW_TAG_enumerator)
5960 /* Enumerators should not get the name of the enumeration as a prefix. */
5961 parent->scope = grandparent_scope;
5962 else if (parent->tag == DW_TAG_namespace
f55ee35c 5963 || parent->tag == DW_TAG_module
72bf9492
DJ
5964 || parent->tag == DW_TAG_structure_type
5965 || parent->tag == DW_TAG_class_type
680b30c7 5966 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5967 || parent->tag == DW_TAG_union_type
5968 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5969 {
5970 if (grandparent_scope == NULL)
5971 parent->scope = parent->name;
5972 else
3e43a32a
MS
5973 parent->scope = typename_concat (&cu->comp_unit_obstack,
5974 grandparent_scope,
f55ee35c 5975 parent->name, 0, cu);
72bf9492 5976 }
72bf9492
DJ
5977 else
5978 {
5979 /* FIXME drow/2004-04-01: What should we be doing with
5980 function-local names? For partial symbols, we should probably be
5981 ignoring them. */
5982 complaint (&symfile_complaints,
e2e0b3e5 5983 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5984 parent->tag, pdi->offset.sect_off);
72bf9492 5985 parent->scope = grandparent_scope;
c906108c
SS
5986 }
5987
72bf9492
DJ
5988 parent->scope_set = 1;
5989 return parent->scope;
5990}
5991
5992/* Return the fully scoped name associated with PDI, from compilation unit
5993 CU. The result will be allocated with malloc. */
4568ecf9 5994
72bf9492
DJ
5995static char *
5996partial_die_full_name (struct partial_die_info *pdi,
5997 struct dwarf2_cu *cu)
5998{
15d034d0 5999 const char *parent_scope;
72bf9492 6000
98bfdba5
PA
6001 /* If this is a template instantiation, we can not work out the
6002 template arguments from partial DIEs. So, unfortunately, we have
6003 to go through the full DIEs. At least any work we do building
6004 types here will be reused if full symbols are loaded later. */
6005 if (pdi->has_template_arguments)
6006 {
6007 fixup_partial_die (pdi, cu);
6008
6009 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6010 {
6011 struct die_info *die;
6012 struct attribute attr;
6013 struct dwarf2_cu *ref_cu = cu;
6014
b64f50a1 6015 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6016 attr.name = 0;
6017 attr.form = DW_FORM_ref_addr;
4568ecf9 6018 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6019 die = follow_die_ref (NULL, &attr, &ref_cu);
6020
6021 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6022 }
6023 }
6024
72bf9492
DJ
6025 parent_scope = partial_die_parent_scope (pdi, cu);
6026 if (parent_scope == NULL)
6027 return NULL;
6028 else
f55ee35c 6029 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6030}
6031
6032static void
72bf9492 6033add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6034{
e7c27a73 6035 struct objfile *objfile = cu->objfile;
c906108c 6036 CORE_ADDR addr = 0;
15d034d0 6037 const char *actual_name = NULL;
e142c38c 6038 CORE_ADDR baseaddr;
15d034d0 6039 char *built_actual_name;
e142c38c
DJ
6040
6041 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6042
15d034d0
TT
6043 built_actual_name = partial_die_full_name (pdi, cu);
6044 if (built_actual_name != NULL)
6045 actual_name = built_actual_name;
63d06c5c 6046
72bf9492
DJ
6047 if (actual_name == NULL)
6048 actual_name = pdi->name;
6049
c906108c
SS
6050 switch (pdi->tag)
6051 {
6052 case DW_TAG_subprogram:
2cfa0c8d 6053 if (pdi->is_external || cu->language == language_ada)
c906108c 6054 {
2cfa0c8d
JB
6055 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6056 of the global scope. But in Ada, we want to be able to access
6057 nested procedures globally. So all Ada subprograms are stored
6058 in the global scope. */
f47fb265 6059 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6060 mst_text, objfile); */
f47fb265 6061 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6062 built_actual_name != NULL,
f47fb265
MS
6063 VAR_DOMAIN, LOC_BLOCK,
6064 &objfile->global_psymbols,
6065 0, pdi->lowpc + baseaddr,
6066 cu->language, objfile);
c906108c
SS
6067 }
6068 else
6069 {
f47fb265 6070 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6071 mst_file_text, objfile); */
f47fb265 6072 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6073 built_actual_name != NULL,
f47fb265
MS
6074 VAR_DOMAIN, LOC_BLOCK,
6075 &objfile->static_psymbols,
6076 0, pdi->lowpc + baseaddr,
6077 cu->language, objfile);
c906108c
SS
6078 }
6079 break;
72929c62
JB
6080 case DW_TAG_constant:
6081 {
6082 struct psymbol_allocation_list *list;
6083
6084 if (pdi->is_external)
6085 list = &objfile->global_psymbols;
6086 else
6087 list = &objfile->static_psymbols;
f47fb265 6088 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6089 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6090 list, 0, 0, cu->language, objfile);
72929c62
JB
6091 }
6092 break;
c906108c 6093 case DW_TAG_variable:
95554aad
TT
6094 if (pdi->d.locdesc)
6095 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6096
95554aad 6097 if (pdi->d.locdesc
caac4577
JG
6098 && addr == 0
6099 && !dwarf2_per_objfile->has_section_at_zero)
6100 {
6101 /* A global or static variable may also have been stripped
6102 out by the linker if unused, in which case its address
6103 will be nullified; do not add such variables into partial
6104 symbol table then. */
6105 }
6106 else if (pdi->is_external)
c906108c
SS
6107 {
6108 /* Global Variable.
6109 Don't enter into the minimal symbol tables as there is
6110 a minimal symbol table entry from the ELF symbols already.
6111 Enter into partial symbol table if it has a location
6112 descriptor or a type.
6113 If the location descriptor is missing, new_symbol will create
6114 a LOC_UNRESOLVED symbol, the address of the variable will then
6115 be determined from the minimal symbol table whenever the variable
6116 is referenced.
6117 The address for the partial symbol table entry is not
6118 used by GDB, but it comes in handy for debugging partial symbol
6119 table building. */
6120
95554aad 6121 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6122 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6123 built_actual_name != NULL,
f47fb265
MS
6124 VAR_DOMAIN, LOC_STATIC,
6125 &objfile->global_psymbols,
6126 0, addr + baseaddr,
6127 cu->language, objfile);
c906108c
SS
6128 }
6129 else
6130 {
0963b4bd 6131 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6132 if (pdi->d.locdesc == NULL)
decbce07 6133 {
15d034d0 6134 xfree (built_actual_name);
decbce07
MS
6135 return;
6136 }
f47fb265 6137 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6138 mst_file_data, objfile); */
f47fb265 6139 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6140 built_actual_name != NULL,
f47fb265
MS
6141 VAR_DOMAIN, LOC_STATIC,
6142 &objfile->static_psymbols,
6143 0, addr + baseaddr,
6144 cu->language, objfile);
c906108c
SS
6145 }
6146 break;
6147 case DW_TAG_typedef:
6148 case DW_TAG_base_type:
a02abb62 6149 case DW_TAG_subrange_type:
38d518c9 6150 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6151 built_actual_name != NULL,
176620f1 6152 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6153 &objfile->static_psymbols,
e142c38c 6154 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6155 break;
72bf9492
DJ
6156 case DW_TAG_namespace:
6157 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6158 built_actual_name != NULL,
72bf9492
DJ
6159 VAR_DOMAIN, LOC_TYPEDEF,
6160 &objfile->global_psymbols,
6161 0, (CORE_ADDR) 0, cu->language, objfile);
6162 break;
c906108c 6163 case DW_TAG_class_type:
680b30c7 6164 case DW_TAG_interface_type:
c906108c
SS
6165 case DW_TAG_structure_type:
6166 case DW_TAG_union_type:
6167 case DW_TAG_enumeration_type:
fa4028e9
JB
6168 /* Skip external references. The DWARF standard says in the section
6169 about "Structure, Union, and Class Type Entries": "An incomplete
6170 structure, union or class type is represented by a structure,
6171 union or class entry that does not have a byte size attribute
6172 and that has a DW_AT_declaration attribute." */
6173 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6174 {
15d034d0 6175 xfree (built_actual_name);
decbce07
MS
6176 return;
6177 }
fa4028e9 6178
63d06c5c
DC
6179 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6180 static vs. global. */
38d518c9 6181 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6182 built_actual_name != NULL,
176620f1 6183 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6184 (cu->language == language_cplus
6185 || cu->language == language_java)
63d06c5c
DC
6186 ? &objfile->global_psymbols
6187 : &objfile->static_psymbols,
e142c38c 6188 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6189
c906108c
SS
6190 break;
6191 case DW_TAG_enumerator:
38d518c9 6192 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6193 built_actual_name != NULL,
176620f1 6194 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6195 (cu->language == language_cplus
6196 || cu->language == language_java)
f6fe98ef
DJ
6197 ? &objfile->global_psymbols
6198 : &objfile->static_psymbols,
e142c38c 6199 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6200 break;
6201 default:
6202 break;
6203 }
5c4e30ca 6204
15d034d0 6205 xfree (built_actual_name);
c906108c
SS
6206}
6207
5c4e30ca
DC
6208/* Read a partial die corresponding to a namespace; also, add a symbol
6209 corresponding to that namespace to the symbol table. NAMESPACE is
6210 the name of the enclosing namespace. */
91c24f0a 6211
72bf9492
DJ
6212static void
6213add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6214 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6215 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6216{
72bf9492 6217 /* Add a symbol for the namespace. */
e7c27a73 6218
72bf9492 6219 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6220
6221 /* Now scan partial symbols in that namespace. */
6222
91c24f0a 6223 if (pdi->has_children)
5734ee8b 6224 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6225}
6226
5d7cb8df
JK
6227/* Read a partial die corresponding to a Fortran module. */
6228
6229static void
6230add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6231 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6232{
f55ee35c 6233 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6234
6235 if (pdi->has_children)
6236 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6237}
6238
bc30ff58
JB
6239/* Read a partial die corresponding to a subprogram and create a partial
6240 symbol for that subprogram. When the CU language allows it, this
6241 routine also defines a partial symbol for each nested subprogram
6242 that this subprogram contains.
6e70227d 6243
bc30ff58
JB
6244 DIE my also be a lexical block, in which case we simply search
6245 recursively for suprograms defined inside that lexical block.
6246 Again, this is only performed when the CU language allows this
6247 type of definitions. */
6248
6249static void
6250add_partial_subprogram (struct partial_die_info *pdi,
6251 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6252 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6253{
6254 if (pdi->tag == DW_TAG_subprogram)
6255 {
6256 if (pdi->has_pc_info)
6257 {
6258 if (pdi->lowpc < *lowpc)
6259 *lowpc = pdi->lowpc;
6260 if (pdi->highpc > *highpc)
6261 *highpc = pdi->highpc;
5734ee8b
DJ
6262 if (need_pc)
6263 {
6264 CORE_ADDR baseaddr;
6265 struct objfile *objfile = cu->objfile;
6266
6267 baseaddr = ANOFFSET (objfile->section_offsets,
6268 SECT_OFF_TEXT (objfile));
6269 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6270 pdi->lowpc + baseaddr,
6271 pdi->highpc - 1 + baseaddr,
9291a0cd 6272 cu->per_cu->v.psymtab);
5734ee8b 6273 }
481860b3
GB
6274 }
6275
6276 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6277 {
bc30ff58 6278 if (!pdi->is_declaration)
e8d05480
JB
6279 /* Ignore subprogram DIEs that do not have a name, they are
6280 illegal. Do not emit a complaint at this point, we will
6281 do so when we convert this psymtab into a symtab. */
6282 if (pdi->name)
6283 add_partial_symbol (pdi, cu);
bc30ff58
JB
6284 }
6285 }
6e70227d 6286
bc30ff58
JB
6287 if (! pdi->has_children)
6288 return;
6289
6290 if (cu->language == language_ada)
6291 {
6292 pdi = pdi->die_child;
6293 while (pdi != NULL)
6294 {
6295 fixup_partial_die (pdi, cu);
6296 if (pdi->tag == DW_TAG_subprogram
6297 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6298 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6299 pdi = pdi->die_sibling;
6300 }
6301 }
6302}
6303
91c24f0a
DC
6304/* Read a partial die corresponding to an enumeration type. */
6305
72bf9492
DJ
6306static void
6307add_partial_enumeration (struct partial_die_info *enum_pdi,
6308 struct dwarf2_cu *cu)
91c24f0a 6309{
72bf9492 6310 struct partial_die_info *pdi;
91c24f0a
DC
6311
6312 if (enum_pdi->name != NULL)
72bf9492
DJ
6313 add_partial_symbol (enum_pdi, cu);
6314
6315 pdi = enum_pdi->die_child;
6316 while (pdi)
91c24f0a 6317 {
72bf9492 6318 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6319 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6320 else
72bf9492
DJ
6321 add_partial_symbol (pdi, cu);
6322 pdi = pdi->die_sibling;
91c24f0a 6323 }
91c24f0a
DC
6324}
6325
6caca83c
CC
6326/* Return the initial uleb128 in the die at INFO_PTR. */
6327
6328static unsigned int
6329peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6330{
6331 unsigned int bytes_read;
6332
6333 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6334}
6335
4bb7a0a7
DJ
6336/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6337 Return the corresponding abbrev, or NULL if the number is zero (indicating
6338 an empty DIE). In either case *BYTES_READ will be set to the length of
6339 the initial number. */
6340
6341static struct abbrev_info *
fe1b8b76 6342peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6343 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6344{
6345 bfd *abfd = cu->objfile->obfd;
6346 unsigned int abbrev_number;
6347 struct abbrev_info *abbrev;
6348
6349 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6350
6351 if (abbrev_number == 0)
6352 return NULL;
6353
433df2d4 6354 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6355 if (!abbrev)
6356 {
3e43a32a
MS
6357 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6358 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6359 }
6360
6361 return abbrev;
6362}
6363
93311388
DE
6364/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6365 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6366 DIE. Any children of the skipped DIEs will also be skipped. */
6367
fe1b8b76 6368static gdb_byte *
dee91e82 6369skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6370{
dee91e82 6371 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6372 struct abbrev_info *abbrev;
6373 unsigned int bytes_read;
6374
6375 while (1)
6376 {
6377 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6378 if (abbrev == NULL)
6379 return info_ptr + bytes_read;
6380 else
dee91e82 6381 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6382 }
6383}
6384
93311388
DE
6385/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6386 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6387 abbrev corresponding to that skipped uleb128 should be passed in
6388 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6389 children. */
6390
fe1b8b76 6391static gdb_byte *
dee91e82
DE
6392skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6393 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6394{
6395 unsigned int bytes_read;
6396 struct attribute attr;
dee91e82
DE
6397 bfd *abfd = reader->abfd;
6398 struct dwarf2_cu *cu = reader->cu;
6399 gdb_byte *buffer = reader->buffer;
f664829e
DE
6400 const gdb_byte *buffer_end = reader->buffer_end;
6401 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6402 unsigned int form, i;
6403
6404 for (i = 0; i < abbrev->num_attrs; i++)
6405 {
6406 /* The only abbrev we care about is DW_AT_sibling. */
6407 if (abbrev->attrs[i].name == DW_AT_sibling)
6408 {
dee91e82 6409 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6410 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6411 complaint (&symfile_complaints,
6412 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6413 else
b64f50a1 6414 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6415 }
6416
6417 /* If it isn't DW_AT_sibling, skip this attribute. */
6418 form = abbrev->attrs[i].form;
6419 skip_attribute:
6420 switch (form)
6421 {
4bb7a0a7 6422 case DW_FORM_ref_addr:
ae411497
TT
6423 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6424 and later it is offset sized. */
6425 if (cu->header.version == 2)
6426 info_ptr += cu->header.addr_size;
6427 else
6428 info_ptr += cu->header.offset_size;
6429 break;
36586728
TT
6430 case DW_FORM_GNU_ref_alt:
6431 info_ptr += cu->header.offset_size;
6432 break;
ae411497 6433 case DW_FORM_addr:
4bb7a0a7
DJ
6434 info_ptr += cu->header.addr_size;
6435 break;
6436 case DW_FORM_data1:
6437 case DW_FORM_ref1:
6438 case DW_FORM_flag:
6439 info_ptr += 1;
6440 break;
2dc7f7b3
TT
6441 case DW_FORM_flag_present:
6442 break;
4bb7a0a7
DJ
6443 case DW_FORM_data2:
6444 case DW_FORM_ref2:
6445 info_ptr += 2;
6446 break;
6447 case DW_FORM_data4:
6448 case DW_FORM_ref4:
6449 info_ptr += 4;
6450 break;
6451 case DW_FORM_data8:
6452 case DW_FORM_ref8:
55f1336d 6453 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6454 info_ptr += 8;
6455 break;
6456 case DW_FORM_string:
9b1c24c8 6457 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6458 info_ptr += bytes_read;
6459 break;
2dc7f7b3 6460 case DW_FORM_sec_offset:
4bb7a0a7 6461 case DW_FORM_strp:
36586728 6462 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6463 info_ptr += cu->header.offset_size;
6464 break;
2dc7f7b3 6465 case DW_FORM_exprloc:
4bb7a0a7
DJ
6466 case DW_FORM_block:
6467 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6468 info_ptr += bytes_read;
6469 break;
6470 case DW_FORM_block1:
6471 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6472 break;
6473 case DW_FORM_block2:
6474 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6475 break;
6476 case DW_FORM_block4:
6477 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6478 break;
6479 case DW_FORM_sdata:
6480 case DW_FORM_udata:
6481 case DW_FORM_ref_udata:
3019eac3
DE
6482 case DW_FORM_GNU_addr_index:
6483 case DW_FORM_GNU_str_index:
f664829e 6484 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6485 break;
6486 case DW_FORM_indirect:
6487 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6488 info_ptr += bytes_read;
6489 /* We need to continue parsing from here, so just go back to
6490 the top. */
6491 goto skip_attribute;
6492
6493 default:
3e43a32a
MS
6494 error (_("Dwarf Error: Cannot handle %s "
6495 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6496 dwarf_form_name (form),
6497 bfd_get_filename (abfd));
6498 }
6499 }
6500
6501 if (abbrev->has_children)
dee91e82 6502 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6503 else
6504 return info_ptr;
6505}
6506
93311388 6507/* Locate ORIG_PDI's sibling.
dee91e82 6508 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6509
fe1b8b76 6510static gdb_byte *
dee91e82
DE
6511locate_pdi_sibling (const struct die_reader_specs *reader,
6512 struct partial_die_info *orig_pdi,
6513 gdb_byte *info_ptr)
91c24f0a
DC
6514{
6515 /* Do we know the sibling already? */
72bf9492 6516
91c24f0a
DC
6517 if (orig_pdi->sibling)
6518 return orig_pdi->sibling;
6519
6520 /* Are there any children to deal with? */
6521
6522 if (!orig_pdi->has_children)
6523 return info_ptr;
6524
4bb7a0a7 6525 /* Skip the children the long way. */
91c24f0a 6526
dee91e82 6527 return skip_children (reader, info_ptr);
91c24f0a
DC
6528}
6529
257e7a09 6530/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6531 not NULL. */
c906108c
SS
6532
6533static void
257e7a09
YQ
6534dwarf2_read_symtab (struct partial_symtab *self,
6535 struct objfile *objfile)
c906108c 6536{
257e7a09 6537 if (self->readin)
c906108c 6538 {
442e4d9c 6539 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6540 self->filename);
442e4d9c
YQ
6541 }
6542 else
6543 {
6544 if (info_verbose)
c906108c 6545 {
442e4d9c 6546 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6547 self->filename);
442e4d9c 6548 gdb_flush (gdb_stdout);
c906108c 6549 }
c906108c 6550
442e4d9c
YQ
6551 /* Restore our global data. */
6552 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6553
442e4d9c
YQ
6554 /* If this psymtab is constructed from a debug-only objfile, the
6555 has_section_at_zero flag will not necessarily be correct. We
6556 can get the correct value for this flag by looking at the data
6557 associated with the (presumably stripped) associated objfile. */
6558 if (objfile->separate_debug_objfile_backlink)
6559 {
6560 struct dwarf2_per_objfile *dpo_backlink
6561 = objfile_data (objfile->separate_debug_objfile_backlink,
6562 dwarf2_objfile_data_key);
9a619af0 6563
442e4d9c
YQ
6564 dwarf2_per_objfile->has_section_at_zero
6565 = dpo_backlink->has_section_at_zero;
6566 }
b2ab525c 6567
442e4d9c 6568 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6569
257e7a09 6570 psymtab_to_symtab_1 (self);
c906108c 6571
442e4d9c
YQ
6572 /* Finish up the debug error message. */
6573 if (info_verbose)
6574 printf_filtered (_("done.\n"));
c906108c 6575 }
95554aad
TT
6576
6577 process_cu_includes ();
c906108c 6578}
9cdd5dbd
DE
6579\f
6580/* Reading in full CUs. */
c906108c 6581
10b3939b
DJ
6582/* Add PER_CU to the queue. */
6583
6584static void
95554aad
TT
6585queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6586 enum language pretend_language)
10b3939b
DJ
6587{
6588 struct dwarf2_queue_item *item;
6589
6590 per_cu->queued = 1;
6591 item = xmalloc (sizeof (*item));
6592 item->per_cu = per_cu;
95554aad 6593 item->pretend_language = pretend_language;
10b3939b
DJ
6594 item->next = NULL;
6595
6596 if (dwarf2_queue == NULL)
6597 dwarf2_queue = item;
6598 else
6599 dwarf2_queue_tail->next = item;
6600
6601 dwarf2_queue_tail = item;
6602}
6603
0907af0c
DE
6604/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6605 unit and add it to our queue.
6606 The result is non-zero if PER_CU was queued, otherwise the result is zero
6607 meaning either PER_CU is already queued or it is already loaded. */
6608
6609static int
6610maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6611 struct dwarf2_per_cu_data *per_cu,
6612 enum language pretend_language)
6613{
6614 /* We may arrive here during partial symbol reading, if we need full
6615 DIEs to process an unusual case (e.g. template arguments). Do
6616 not queue PER_CU, just tell our caller to load its DIEs. */
6617 if (dwarf2_per_objfile->reading_partial_symbols)
6618 {
6619 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6620 return 1;
6621 return 0;
6622 }
6623
6624 /* Mark the dependence relation so that we don't flush PER_CU
6625 too early. */
6626 dwarf2_add_dependence (this_cu, per_cu);
6627
6628 /* If it's already on the queue, we have nothing to do. */
6629 if (per_cu->queued)
6630 return 0;
6631
6632 /* If the compilation unit is already loaded, just mark it as
6633 used. */
6634 if (per_cu->cu != NULL)
6635 {
6636 per_cu->cu->last_used = 0;
6637 return 0;
6638 }
6639
6640 /* Add it to the queue. */
6641 queue_comp_unit (per_cu, pretend_language);
6642
6643 return 1;
6644}
6645
10b3939b
DJ
6646/* Process the queue. */
6647
6648static void
a0f42c21 6649process_queue (void)
10b3939b
DJ
6650{
6651 struct dwarf2_queue_item *item, *next_item;
6652
45cfd468
DE
6653 if (dwarf2_read_debug)
6654 {
6655 fprintf_unfiltered (gdb_stdlog,
6656 "Expanding one or more symtabs of objfile %s ...\n",
6657 dwarf2_per_objfile->objfile->name);
6658 }
6659
03dd20cc
DJ
6660 /* The queue starts out with one item, but following a DIE reference
6661 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6662 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6663 {
9291a0cd
TT
6664 if (dwarf2_per_objfile->using_index
6665 ? !item->per_cu->v.quick->symtab
6666 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6667 {
6668 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6669
6670 if (dwarf2_read_debug)
6671 {
6672 fprintf_unfiltered (gdb_stdlog,
6673 "Expanding symtab of %s at offset 0x%x\n",
6674 per_cu->is_debug_types ? "TU" : "CU",
6675 per_cu->offset.sect_off);
6676 }
6677
6678 if (per_cu->is_debug_types)
6679 process_full_type_unit (per_cu, item->pretend_language);
6680 else
6681 process_full_comp_unit (per_cu, item->pretend_language);
6682
6683 if (dwarf2_read_debug)
6684 {
6685 fprintf_unfiltered (gdb_stdlog,
6686 "Done expanding %s at offset 0x%x\n",
6687 per_cu->is_debug_types ? "TU" : "CU",
6688 per_cu->offset.sect_off);
6689 }
6690 }
10b3939b
DJ
6691
6692 item->per_cu->queued = 0;
6693 next_item = item->next;
6694 xfree (item);
6695 }
6696
6697 dwarf2_queue_tail = NULL;
45cfd468
DE
6698
6699 if (dwarf2_read_debug)
6700 {
6701 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6702 dwarf2_per_objfile->objfile->name);
6703 }
10b3939b
DJ
6704}
6705
6706/* Free all allocated queue entries. This function only releases anything if
6707 an error was thrown; if the queue was processed then it would have been
6708 freed as we went along. */
6709
6710static void
6711dwarf2_release_queue (void *dummy)
6712{
6713 struct dwarf2_queue_item *item, *last;
6714
6715 item = dwarf2_queue;
6716 while (item)
6717 {
6718 /* Anything still marked queued is likely to be in an
6719 inconsistent state, so discard it. */
6720 if (item->per_cu->queued)
6721 {
6722 if (item->per_cu->cu != NULL)
dee91e82 6723 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6724 item->per_cu->queued = 0;
6725 }
6726
6727 last = item;
6728 item = item->next;
6729 xfree (last);
6730 }
6731
6732 dwarf2_queue = dwarf2_queue_tail = NULL;
6733}
6734
6735/* Read in full symbols for PST, and anything it depends on. */
6736
c906108c 6737static void
fba45db2 6738psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6739{
10b3939b 6740 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6741 int i;
6742
95554aad
TT
6743 if (pst->readin)
6744 return;
6745
aaa75496 6746 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6747 if (!pst->dependencies[i]->readin
6748 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6749 {
6750 /* Inform about additional files that need to be read in. */
6751 if (info_verbose)
6752 {
a3f17187 6753 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6754 fputs_filtered (" ", gdb_stdout);
6755 wrap_here ("");
6756 fputs_filtered ("and ", gdb_stdout);
6757 wrap_here ("");
6758 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6759 wrap_here (""); /* Flush output. */
aaa75496
JB
6760 gdb_flush (gdb_stdout);
6761 }
6762 psymtab_to_symtab_1 (pst->dependencies[i]);
6763 }
6764
e38df1d0 6765 per_cu = pst->read_symtab_private;
10b3939b
DJ
6766
6767 if (per_cu == NULL)
aaa75496
JB
6768 {
6769 /* It's an include file, no symbols to read for it.
6770 Everything is in the parent symtab. */
6771 pst->readin = 1;
6772 return;
6773 }
c906108c 6774
a0f42c21 6775 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6776}
6777
dee91e82
DE
6778/* Trivial hash function for die_info: the hash value of a DIE
6779 is its offset in .debug_info for this objfile. */
10b3939b 6780
dee91e82
DE
6781static hashval_t
6782die_hash (const void *item)
10b3939b 6783{
dee91e82 6784 const struct die_info *die = item;
6502dd73 6785
dee91e82
DE
6786 return die->offset.sect_off;
6787}
63d06c5c 6788
dee91e82
DE
6789/* Trivial comparison function for die_info structures: two DIEs
6790 are equal if they have the same offset. */
98bfdba5 6791
dee91e82
DE
6792static int
6793die_eq (const void *item_lhs, const void *item_rhs)
6794{
6795 const struct die_info *die_lhs = item_lhs;
6796 const struct die_info *die_rhs = item_rhs;
c906108c 6797
dee91e82
DE
6798 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6799}
c906108c 6800
dee91e82
DE
6801/* die_reader_func for load_full_comp_unit.
6802 This is identical to read_signatured_type_reader,
6803 but is kept separate for now. */
c906108c 6804
dee91e82
DE
6805static void
6806load_full_comp_unit_reader (const struct die_reader_specs *reader,
6807 gdb_byte *info_ptr,
6808 struct die_info *comp_unit_die,
6809 int has_children,
6810 void *data)
6811{
6812 struct dwarf2_cu *cu = reader->cu;
95554aad 6813 enum language *language_ptr = data;
6caca83c 6814
dee91e82
DE
6815 gdb_assert (cu->die_hash == NULL);
6816 cu->die_hash =
6817 htab_create_alloc_ex (cu->header.length / 12,
6818 die_hash,
6819 die_eq,
6820 NULL,
6821 &cu->comp_unit_obstack,
6822 hashtab_obstack_allocate,
6823 dummy_obstack_deallocate);
e142c38c 6824
dee91e82
DE
6825 if (has_children)
6826 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6827 &info_ptr, comp_unit_die);
6828 cu->dies = comp_unit_die;
6829 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6830
6831 /* We try not to read any attributes in this function, because not
9cdd5dbd 6832 all CUs needed for references have been loaded yet, and symbol
10b3939b 6833 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6834 or we won't be able to build types correctly.
6835 Similarly, if we do not read the producer, we can not apply
6836 producer-specific interpretation. */
95554aad 6837 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6838}
10b3939b 6839
dee91e82 6840/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6841
dee91e82 6842static void
95554aad
TT
6843load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6844 enum language pretend_language)
dee91e82 6845{
3019eac3 6846 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6847
f4dc4d17
DE
6848 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6849 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6850}
6851
3da10d80
KS
6852/* Add a DIE to the delayed physname list. */
6853
6854static void
6855add_to_method_list (struct type *type, int fnfield_index, int index,
6856 const char *name, struct die_info *die,
6857 struct dwarf2_cu *cu)
6858{
6859 struct delayed_method_info mi;
6860 mi.type = type;
6861 mi.fnfield_index = fnfield_index;
6862 mi.index = index;
6863 mi.name = name;
6864 mi.die = die;
6865 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6866}
6867
6868/* A cleanup for freeing the delayed method list. */
6869
6870static void
6871free_delayed_list (void *ptr)
6872{
6873 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6874 if (cu->method_list != NULL)
6875 {
6876 VEC_free (delayed_method_info, cu->method_list);
6877 cu->method_list = NULL;
6878 }
6879}
6880
6881/* Compute the physnames of any methods on the CU's method list.
6882
6883 The computation of method physnames is delayed in order to avoid the
6884 (bad) condition that one of the method's formal parameters is of an as yet
6885 incomplete type. */
6886
6887static void
6888compute_delayed_physnames (struct dwarf2_cu *cu)
6889{
6890 int i;
6891 struct delayed_method_info *mi;
6892 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6893 {
1d06ead6 6894 const char *physname;
3da10d80
KS
6895 struct fn_fieldlist *fn_flp
6896 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6897 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6898 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6899 }
6900}
6901
a766d390
DE
6902/* Go objects should be embedded in a DW_TAG_module DIE,
6903 and it's not clear if/how imported objects will appear.
6904 To keep Go support simple until that's worked out,
6905 go back through what we've read and create something usable.
6906 We could do this while processing each DIE, and feels kinda cleaner,
6907 but that way is more invasive.
6908 This is to, for example, allow the user to type "p var" or "b main"
6909 without having to specify the package name, and allow lookups
6910 of module.object to work in contexts that use the expression
6911 parser. */
6912
6913static void
6914fixup_go_packaging (struct dwarf2_cu *cu)
6915{
6916 char *package_name = NULL;
6917 struct pending *list;
6918 int i;
6919
6920 for (list = global_symbols; list != NULL; list = list->next)
6921 {
6922 for (i = 0; i < list->nsyms; ++i)
6923 {
6924 struct symbol *sym = list->symbol[i];
6925
6926 if (SYMBOL_LANGUAGE (sym) == language_go
6927 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6928 {
6929 char *this_package_name = go_symbol_package_name (sym);
6930
6931 if (this_package_name == NULL)
6932 continue;
6933 if (package_name == NULL)
6934 package_name = this_package_name;
6935 else
6936 {
6937 if (strcmp (package_name, this_package_name) != 0)
6938 complaint (&symfile_complaints,
6939 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6940 (SYMBOL_SYMTAB (sym)
05cba821 6941 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6942 : cu->objfile->name),
6943 this_package_name, package_name);
6944 xfree (this_package_name);
6945 }
6946 }
6947 }
6948 }
6949
6950 if (package_name != NULL)
6951 {
6952 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6953 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6954 package_name,
6955 strlen (package_name));
a766d390 6956 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6957 saved_package_name, objfile);
a766d390
DE
6958 struct symbol *sym;
6959
6960 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6961
6962 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6963 SYMBOL_SET_LANGUAGE (sym, language_go);
86f62fd7
TT
6964 SYMBOL_SET_NAMES (sym, saved_package_name,
6965 strlen (saved_package_name), 0, objfile);
a766d390
DE
6966 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6967 e.g., "main" finds the "main" module and not C's main(). */
6968 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6969 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6970 SYMBOL_TYPE (sym) = type;
6971
6972 add_symbol_to_list (sym, &global_symbols);
6973
6974 xfree (package_name);
6975 }
6976}
6977
95554aad
TT
6978/* Return the symtab for PER_CU. This works properly regardless of
6979 whether we're using the index or psymtabs. */
6980
6981static struct symtab *
6982get_symtab (struct dwarf2_per_cu_data *per_cu)
6983{
6984 return (dwarf2_per_objfile->using_index
6985 ? per_cu->v.quick->symtab
6986 : per_cu->v.psymtab->symtab);
6987}
6988
6989/* A helper function for computing the list of all symbol tables
6990 included by PER_CU. */
6991
6992static void
6993recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6994 htab_t all_children,
6995 struct dwarf2_per_cu_data *per_cu)
6996{
6997 void **slot;
6998 int ix;
6999 struct dwarf2_per_cu_data *iter;
7000
7001 slot = htab_find_slot (all_children, per_cu, INSERT);
7002 if (*slot != NULL)
7003 {
7004 /* This inclusion and its children have been processed. */
7005 return;
7006 }
7007
7008 *slot = per_cu;
7009 /* Only add a CU if it has a symbol table. */
7010 if (get_symtab (per_cu) != NULL)
7011 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7012
7013 for (ix = 0;
796a7ff8 7014 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
7015 ++ix)
7016 recursively_compute_inclusions (result, all_children, iter);
7017}
7018
7019/* Compute the symtab 'includes' fields for the symtab related to
7020 PER_CU. */
7021
7022static void
7023compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7024{
f4dc4d17
DE
7025 gdb_assert (! per_cu->is_debug_types);
7026
796a7ff8 7027 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7028 {
7029 int ix, len;
7030 struct dwarf2_per_cu_data *iter;
7031 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7032 htab_t all_children;
7033 struct symtab *symtab = get_symtab (per_cu);
7034
7035 /* If we don't have a symtab, we can just skip this case. */
7036 if (symtab == NULL)
7037 return;
7038
7039 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7040 NULL, xcalloc, xfree);
7041
7042 for (ix = 0;
796a7ff8 7043 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7044 ix, iter);
7045 ++ix)
7046 recursively_compute_inclusions (&result_children, all_children, iter);
7047
796a7ff8
DE
7048 /* Now we have a transitive closure of all the included CUs, and
7049 for .gdb_index version 7 the included TUs, so we can convert it
7050 to a list of symtabs. */
95554aad
TT
7051 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7052 symtab->includes
7053 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7054 (len + 1) * sizeof (struct symtab *));
7055 for (ix = 0;
7056 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7057 ++ix)
7058 symtab->includes[ix] = get_symtab (iter);
7059 symtab->includes[len] = NULL;
7060
7061 VEC_free (dwarf2_per_cu_ptr, result_children);
7062 htab_delete (all_children);
7063 }
7064}
7065
7066/* Compute the 'includes' field for the symtabs of all the CUs we just
7067 read. */
7068
7069static void
7070process_cu_includes (void)
7071{
7072 int ix;
7073 struct dwarf2_per_cu_data *iter;
7074
7075 for (ix = 0;
7076 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7077 ix, iter);
7078 ++ix)
f4dc4d17
DE
7079 {
7080 if (! iter->is_debug_types)
7081 compute_symtab_includes (iter);
7082 }
95554aad
TT
7083
7084 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7085}
7086
9cdd5dbd 7087/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7088 already been loaded into memory. */
7089
7090static void
95554aad
TT
7091process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7092 enum language pretend_language)
10b3939b 7093{
10b3939b 7094 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7095 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7096 CORE_ADDR lowpc, highpc;
7097 struct symtab *symtab;
3da10d80 7098 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7099 CORE_ADDR baseaddr;
4359dff1 7100 struct block *static_block;
10b3939b
DJ
7101
7102 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7103
10b3939b
DJ
7104 buildsym_init ();
7105 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7106 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7107
7108 cu->list_in_scope = &file_symbols;
c906108c 7109
95554aad
TT
7110 cu->language = pretend_language;
7111 cu->language_defn = language_def (cu->language);
7112
c906108c 7113 /* Do line number decoding in read_file_scope () */
10b3939b 7114 process_die (cu->dies, cu);
c906108c 7115
a766d390
DE
7116 /* For now fudge the Go package. */
7117 if (cu->language == language_go)
7118 fixup_go_packaging (cu);
7119
3da10d80
KS
7120 /* Now that we have processed all the DIEs in the CU, all the types
7121 should be complete, and it should now be safe to compute all of the
7122 physnames. */
7123 compute_delayed_physnames (cu);
7124 do_cleanups (delayed_list_cleanup);
7125
fae299cd
DC
7126 /* Some compilers don't define a DW_AT_high_pc attribute for the
7127 compilation unit. If the DW_AT_high_pc is missing, synthesize
7128 it, by scanning the DIE's below the compilation unit. */
10b3939b 7129 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7130
36586728
TT
7131 static_block
7132 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7133 per_cu->imported_symtabs != NULL);
4359dff1
JK
7134
7135 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7136 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7137 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7138 addrmap to help ensure it has an accurate map of pc values belonging to
7139 this comp unit. */
7140 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7141
7142 symtab = end_symtab_from_static_block (static_block, objfile,
7143 SECT_OFF_TEXT (objfile), 0);
c906108c 7144
8be455d7 7145 if (symtab != NULL)
c906108c 7146 {
df15bd07 7147 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7148
8be455d7
JK
7149 /* Set symtab language to language from DW_AT_language. If the
7150 compilation is from a C file generated by language preprocessors, do
7151 not set the language if it was already deduced by start_subfile. */
7152 if (!(cu->language == language_c && symtab->language != language_c))
7153 symtab->language = cu->language;
7154
7155 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7156 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7157 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7158 there were bugs in prologue debug info, fixed later in GCC-4.5
7159 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7160
7161 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7162 needed, it would be wrong due to missing DW_AT_producer there.
7163
7164 Still one can confuse GDB by using non-standard GCC compilation
7165 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7166 */
ab260dad 7167 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7168 symtab->locations_valid = 1;
e0d00bc7
JK
7169
7170 if (gcc_4_minor >= 5)
7171 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7172
7173 symtab->call_site_htab = cu->call_site_htab;
c906108c 7174 }
9291a0cd
TT
7175
7176 if (dwarf2_per_objfile->using_index)
7177 per_cu->v.quick->symtab = symtab;
7178 else
7179 {
7180 struct partial_symtab *pst = per_cu->v.psymtab;
7181 pst->symtab = symtab;
7182 pst->readin = 1;
7183 }
c906108c 7184
95554aad
TT
7185 /* Push it for inclusion processing later. */
7186 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7187
c906108c 7188 do_cleanups (back_to);
f4dc4d17 7189}
45cfd468 7190
f4dc4d17
DE
7191/* Generate full symbol information for type unit PER_CU, whose DIEs have
7192 already been loaded into memory. */
7193
7194static void
7195process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7196 enum language pretend_language)
7197{
7198 struct dwarf2_cu *cu = per_cu->cu;
7199 struct objfile *objfile = per_cu->objfile;
7200 struct symtab *symtab;
7201 struct cleanup *back_to, *delayed_list_cleanup;
7202
7203 buildsym_init ();
7204 back_to = make_cleanup (really_free_pendings, NULL);
7205 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7206
7207 cu->list_in_scope = &file_symbols;
7208
7209 cu->language = pretend_language;
7210 cu->language_defn = language_def (cu->language);
7211
7212 /* The symbol tables are set up in read_type_unit_scope. */
7213 process_die (cu->dies, cu);
7214
7215 /* For now fudge the Go package. */
7216 if (cu->language == language_go)
7217 fixup_go_packaging (cu);
7218
7219 /* Now that we have processed all the DIEs in the CU, all the types
7220 should be complete, and it should now be safe to compute all of the
7221 physnames. */
7222 compute_delayed_physnames (cu);
7223 do_cleanups (delayed_list_cleanup);
7224
7225 /* TUs share symbol tables.
7226 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7227 of it with end_expandable_symtab. Otherwise, complete the addition of
7228 this TU's symbols to the existing symtab. */
796a7ff8 7229 if (per_cu->type_unit_group->primary_symtab == NULL)
45cfd468 7230 {
f4dc4d17 7231 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
796a7ff8 7232 per_cu->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7233
7234 if (symtab != NULL)
7235 {
7236 /* Set symtab language to language from DW_AT_language. If the
7237 compilation is from a C file generated by language preprocessors,
7238 do not set the language if it was already deduced by
7239 start_subfile. */
7240 if (!(cu->language == language_c && symtab->language != language_c))
7241 symtab->language = cu->language;
7242 }
7243 }
7244 else
7245 {
7246 augment_type_symtab (objfile,
796a7ff8
DE
7247 per_cu->type_unit_group->primary_symtab);
7248 symtab = per_cu->type_unit_group->primary_symtab;
f4dc4d17
DE
7249 }
7250
7251 if (dwarf2_per_objfile->using_index)
7252 per_cu->v.quick->symtab = symtab;
7253 else
7254 {
7255 struct partial_symtab *pst = per_cu->v.psymtab;
7256 pst->symtab = symtab;
7257 pst->readin = 1;
45cfd468 7258 }
f4dc4d17
DE
7259
7260 do_cleanups (back_to);
c906108c
SS
7261}
7262
95554aad
TT
7263/* Process an imported unit DIE. */
7264
7265static void
7266process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7267{
7268 struct attribute *attr;
7269
f4dc4d17
DE
7270 /* For now we don't handle imported units in type units. */
7271 if (cu->per_cu->is_debug_types)
7272 {
7273 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7274 " supported in type units [in module %s]"),
7275 cu->objfile->name);
7276 }
7277
95554aad
TT
7278 attr = dwarf2_attr (die, DW_AT_import, cu);
7279 if (attr != NULL)
7280 {
7281 struct dwarf2_per_cu_data *per_cu;
7282 struct symtab *imported_symtab;
7283 sect_offset offset;
36586728 7284 int is_dwz;
95554aad
TT
7285
7286 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7287 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7288 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7289
7290 /* Queue the unit, if needed. */
7291 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7292 load_full_comp_unit (per_cu, cu->language);
7293
796a7ff8 7294 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7295 per_cu);
7296 }
7297}
7298
c906108c
SS
7299/* Process a die and its children. */
7300
7301static void
e7c27a73 7302process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7303{
7304 switch (die->tag)
7305 {
7306 case DW_TAG_padding:
7307 break;
7308 case DW_TAG_compile_unit:
95554aad 7309 case DW_TAG_partial_unit:
e7c27a73 7310 read_file_scope (die, cu);
c906108c 7311 break;
348e048f
DE
7312 case DW_TAG_type_unit:
7313 read_type_unit_scope (die, cu);
7314 break;
c906108c 7315 case DW_TAG_subprogram:
c906108c 7316 case DW_TAG_inlined_subroutine:
edb3359d 7317 read_func_scope (die, cu);
c906108c
SS
7318 break;
7319 case DW_TAG_lexical_block:
14898363
L
7320 case DW_TAG_try_block:
7321 case DW_TAG_catch_block:
e7c27a73 7322 read_lexical_block_scope (die, cu);
c906108c 7323 break;
96408a79
SA
7324 case DW_TAG_GNU_call_site:
7325 read_call_site_scope (die, cu);
7326 break;
c906108c 7327 case DW_TAG_class_type:
680b30c7 7328 case DW_TAG_interface_type:
c906108c
SS
7329 case DW_TAG_structure_type:
7330 case DW_TAG_union_type:
134d01f1 7331 process_structure_scope (die, cu);
c906108c
SS
7332 break;
7333 case DW_TAG_enumeration_type:
134d01f1 7334 process_enumeration_scope (die, cu);
c906108c 7335 break;
134d01f1 7336
f792889a
DJ
7337 /* These dies have a type, but processing them does not create
7338 a symbol or recurse to process the children. Therefore we can
7339 read them on-demand through read_type_die. */
c906108c 7340 case DW_TAG_subroutine_type:
72019c9c 7341 case DW_TAG_set_type:
c906108c 7342 case DW_TAG_array_type:
c906108c 7343 case DW_TAG_pointer_type:
c906108c 7344 case DW_TAG_ptr_to_member_type:
c906108c 7345 case DW_TAG_reference_type:
c906108c 7346 case DW_TAG_string_type:
c906108c 7347 break;
134d01f1 7348
c906108c 7349 case DW_TAG_base_type:
a02abb62 7350 case DW_TAG_subrange_type:
cb249c71 7351 case DW_TAG_typedef:
134d01f1
DJ
7352 /* Add a typedef symbol for the type definition, if it has a
7353 DW_AT_name. */
f792889a 7354 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7355 break;
c906108c 7356 case DW_TAG_common_block:
e7c27a73 7357 read_common_block (die, cu);
c906108c
SS
7358 break;
7359 case DW_TAG_common_inclusion:
7360 break;
d9fa45fe 7361 case DW_TAG_namespace:
4d4ec4e5 7362 cu->processing_has_namespace_info = 1;
e7c27a73 7363 read_namespace (die, cu);
d9fa45fe 7364 break;
5d7cb8df 7365 case DW_TAG_module:
4d4ec4e5 7366 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7367 read_module (die, cu);
7368 break;
d9fa45fe
DC
7369 case DW_TAG_imported_declaration:
7370 case DW_TAG_imported_module:
4d4ec4e5 7371 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7372 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7373 || cu->language != language_fortran))
7374 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7375 dwarf_tag_name (die->tag));
7376 read_import_statement (die, cu);
d9fa45fe 7377 break;
95554aad
TT
7378
7379 case DW_TAG_imported_unit:
7380 process_imported_unit_die (die, cu);
7381 break;
7382
c906108c 7383 default:
e7c27a73 7384 new_symbol (die, NULL, cu);
c906108c
SS
7385 break;
7386 }
7387}
ca69b9e6
DE
7388\f
7389/* DWARF name computation. */
c906108c 7390
94af9270
KS
7391/* A helper function for dwarf2_compute_name which determines whether DIE
7392 needs to have the name of the scope prepended to the name listed in the
7393 die. */
7394
7395static int
7396die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7397{
1c809c68
TT
7398 struct attribute *attr;
7399
94af9270
KS
7400 switch (die->tag)
7401 {
7402 case DW_TAG_namespace:
7403 case DW_TAG_typedef:
7404 case DW_TAG_class_type:
7405 case DW_TAG_interface_type:
7406 case DW_TAG_structure_type:
7407 case DW_TAG_union_type:
7408 case DW_TAG_enumeration_type:
7409 case DW_TAG_enumerator:
7410 case DW_TAG_subprogram:
7411 case DW_TAG_member:
7412 return 1;
7413
7414 case DW_TAG_variable:
c2b0a229 7415 case DW_TAG_constant:
94af9270
KS
7416 /* We only need to prefix "globally" visible variables. These include
7417 any variable marked with DW_AT_external or any variable that
7418 lives in a namespace. [Variables in anonymous namespaces
7419 require prefixing, but they are not DW_AT_external.] */
7420
7421 if (dwarf2_attr (die, DW_AT_specification, cu))
7422 {
7423 struct dwarf2_cu *spec_cu = cu;
9a619af0 7424
94af9270
KS
7425 return die_needs_namespace (die_specification (die, &spec_cu),
7426 spec_cu);
7427 }
7428
1c809c68 7429 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7430 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7431 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7432 return 0;
7433 /* A variable in a lexical block of some kind does not need a
7434 namespace, even though in C++ such variables may be external
7435 and have a mangled name. */
7436 if (die->parent->tag == DW_TAG_lexical_block
7437 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7438 || die->parent->tag == DW_TAG_catch_block
7439 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7440 return 0;
7441 return 1;
94af9270
KS
7442
7443 default:
7444 return 0;
7445 }
7446}
7447
98bfdba5
PA
7448/* Retrieve the last character from a mem_file. */
7449
7450static void
7451do_ui_file_peek_last (void *object, const char *buffer, long length)
7452{
7453 char *last_char_p = (char *) object;
7454
7455 if (length > 0)
7456 *last_char_p = buffer[length - 1];
7457}
7458
94af9270 7459/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7460 compute the physname for the object, which include a method's:
7461 - formal parameters (C++/Java),
7462 - receiver type (Go),
7463 - return type (Java).
7464
7465 The term "physname" is a bit confusing.
7466 For C++, for example, it is the demangled name.
7467 For Go, for example, it's the mangled name.
94af9270 7468
af6b7be1
JB
7469 For Ada, return the DIE's linkage name rather than the fully qualified
7470 name. PHYSNAME is ignored..
7471
94af9270
KS
7472 The result is allocated on the objfile_obstack and canonicalized. */
7473
7474static const char *
15d034d0
TT
7475dwarf2_compute_name (const char *name,
7476 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7477 int physname)
7478{
bb5ed363
DE
7479 struct objfile *objfile = cu->objfile;
7480
94af9270
KS
7481 if (name == NULL)
7482 name = dwarf2_name (die, cu);
7483
f55ee35c
JK
7484 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7485 compute it by typename_concat inside GDB. */
7486 if (cu->language == language_ada
7487 || (cu->language == language_fortran && physname))
7488 {
7489 /* For Ada unit, we prefer the linkage name over the name, as
7490 the former contains the exported name, which the user expects
7491 to be able to reference. Ideally, we want the user to be able
7492 to reference this entity using either natural or linkage name,
7493 but we haven't started looking at this enhancement yet. */
7494 struct attribute *attr;
7495
7496 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7497 if (attr == NULL)
7498 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7499 if (attr && DW_STRING (attr))
7500 return DW_STRING (attr);
7501 }
7502
94af9270
KS
7503 /* These are the only languages we know how to qualify names in. */
7504 if (name != NULL
f55ee35c
JK
7505 && (cu->language == language_cplus || cu->language == language_java
7506 || cu->language == language_fortran))
94af9270
KS
7507 {
7508 if (die_needs_namespace (die, cu))
7509 {
7510 long length;
0d5cff50 7511 const char *prefix;
94af9270
KS
7512 struct ui_file *buf;
7513
7514 prefix = determine_prefix (die, cu);
7515 buf = mem_fileopen ();
7516 if (*prefix != '\0')
7517 {
f55ee35c
JK
7518 char *prefixed_name = typename_concat (NULL, prefix, name,
7519 physname, cu);
9a619af0 7520
94af9270
KS
7521 fputs_unfiltered (prefixed_name, buf);
7522 xfree (prefixed_name);
7523 }
7524 else
62d5b8da 7525 fputs_unfiltered (name, buf);
94af9270 7526
98bfdba5
PA
7527 /* Template parameters may be specified in the DIE's DW_AT_name, or
7528 as children with DW_TAG_template_type_param or
7529 DW_TAG_value_type_param. If the latter, add them to the name
7530 here. If the name already has template parameters, then
7531 skip this step; some versions of GCC emit both, and
7532 it is more efficient to use the pre-computed name.
7533
7534 Something to keep in mind about this process: it is very
7535 unlikely, or in some cases downright impossible, to produce
7536 something that will match the mangled name of a function.
7537 If the definition of the function has the same debug info,
7538 we should be able to match up with it anyway. But fallbacks
7539 using the minimal symbol, for instance to find a method
7540 implemented in a stripped copy of libstdc++, will not work.
7541 If we do not have debug info for the definition, we will have to
7542 match them up some other way.
7543
7544 When we do name matching there is a related problem with function
7545 templates; two instantiated function templates are allowed to
7546 differ only by their return types, which we do not add here. */
7547
7548 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7549 {
7550 struct attribute *attr;
7551 struct die_info *child;
7552 int first = 1;
7553
7554 die->building_fullname = 1;
7555
7556 for (child = die->child; child != NULL; child = child->sibling)
7557 {
7558 struct type *type;
12df843f 7559 LONGEST value;
98bfdba5
PA
7560 gdb_byte *bytes;
7561 struct dwarf2_locexpr_baton *baton;
7562 struct value *v;
7563
7564 if (child->tag != DW_TAG_template_type_param
7565 && child->tag != DW_TAG_template_value_param)
7566 continue;
7567
7568 if (first)
7569 {
7570 fputs_unfiltered ("<", buf);
7571 first = 0;
7572 }
7573 else
7574 fputs_unfiltered (", ", buf);
7575
7576 attr = dwarf2_attr (child, DW_AT_type, cu);
7577 if (attr == NULL)
7578 {
7579 complaint (&symfile_complaints,
7580 _("template parameter missing DW_AT_type"));
7581 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7582 continue;
7583 }
7584 type = die_type (child, cu);
7585
7586 if (child->tag == DW_TAG_template_type_param)
7587 {
79d43c61 7588 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7589 continue;
7590 }
7591
7592 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7593 if (attr == NULL)
7594 {
7595 complaint (&symfile_complaints,
3e43a32a
MS
7596 _("template parameter missing "
7597 "DW_AT_const_value"));
98bfdba5
PA
7598 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7599 continue;
7600 }
7601
7602 dwarf2_const_value_attr (attr, type, name,
7603 &cu->comp_unit_obstack, cu,
7604 &value, &bytes, &baton);
7605
7606 if (TYPE_NOSIGN (type))
7607 /* GDB prints characters as NUMBER 'CHAR'. If that's
7608 changed, this can use value_print instead. */
7609 c_printchar (value, type, buf);
7610 else
7611 {
7612 struct value_print_options opts;
7613
7614 if (baton != NULL)
7615 v = dwarf2_evaluate_loc_desc (type, NULL,
7616 baton->data,
7617 baton->size,
7618 baton->per_cu);
7619 else if (bytes != NULL)
7620 {
7621 v = allocate_value (type);
7622 memcpy (value_contents_writeable (v), bytes,
7623 TYPE_LENGTH (type));
7624 }
7625 else
7626 v = value_from_longest (type, value);
7627
3e43a32a
MS
7628 /* Specify decimal so that we do not depend on
7629 the radix. */
98bfdba5
PA
7630 get_formatted_print_options (&opts, 'd');
7631 opts.raw = 1;
7632 value_print (v, buf, &opts);
7633 release_value (v);
7634 value_free (v);
7635 }
7636 }
7637
7638 die->building_fullname = 0;
7639
7640 if (!first)
7641 {
7642 /* Close the argument list, with a space if necessary
7643 (nested templates). */
7644 char last_char = '\0';
7645 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7646 if (last_char == '>')
7647 fputs_unfiltered (" >", buf);
7648 else
7649 fputs_unfiltered (">", buf);
7650 }
7651 }
7652
94af9270
KS
7653 /* For Java and C++ methods, append formal parameter type
7654 information, if PHYSNAME. */
6e70227d 7655
94af9270
KS
7656 if (physname && die->tag == DW_TAG_subprogram
7657 && (cu->language == language_cplus
7658 || cu->language == language_java))
7659 {
7660 struct type *type = read_type_die (die, cu);
7661
79d43c61
TT
7662 c_type_print_args (type, buf, 1, cu->language,
7663 &type_print_raw_options);
94af9270
KS
7664
7665 if (cu->language == language_java)
7666 {
7667 /* For java, we must append the return type to method
0963b4bd 7668 names. */
94af9270
KS
7669 if (die->tag == DW_TAG_subprogram)
7670 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7671 0, 0, &type_print_raw_options);
94af9270
KS
7672 }
7673 else if (cu->language == language_cplus)
7674 {
60430eff
DJ
7675 /* Assume that an artificial first parameter is
7676 "this", but do not crash if it is not. RealView
7677 marks unnamed (and thus unused) parameters as
7678 artificial; there is no way to differentiate
7679 the two cases. */
94af9270
KS
7680 if (TYPE_NFIELDS (type) > 0
7681 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7682 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7683 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7684 0))))
94af9270
KS
7685 fputs_unfiltered (" const", buf);
7686 }
7687 }
7688
bb5ed363 7689 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7690 &length);
7691 ui_file_delete (buf);
7692
7693 if (cu->language == language_cplus)
7694 {
15d034d0 7695 const char *cname
94af9270 7696 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7697 &objfile->objfile_obstack);
9a619af0 7698
94af9270
KS
7699 if (cname != NULL)
7700 name = cname;
7701 }
7702 }
7703 }
7704
7705 return name;
7706}
7707
0114d602
DJ
7708/* Return the fully qualified name of DIE, based on its DW_AT_name.
7709 If scope qualifiers are appropriate they will be added. The result
7710 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7711 not have a name. NAME may either be from a previous call to
7712 dwarf2_name or NULL.
7713
0963b4bd 7714 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7715
7716static const char *
15d034d0 7717dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7718{
94af9270
KS
7719 return dwarf2_compute_name (name, die, cu, 0);
7720}
0114d602 7721
94af9270
KS
7722/* Construct a physname for the given DIE in CU. NAME may either be
7723 from a previous call to dwarf2_name or NULL. The result will be
7724 allocated on the objfile_objstack or NULL if the DIE does not have a
7725 name.
0114d602 7726
94af9270 7727 The output string will be canonicalized (if C++/Java). */
0114d602 7728
94af9270 7729static const char *
15d034d0 7730dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7731{
bb5ed363 7732 struct objfile *objfile = cu->objfile;
900e11f9
JK
7733 struct attribute *attr;
7734 const char *retval, *mangled = NULL, *canon = NULL;
7735 struct cleanup *back_to;
7736 int need_copy = 1;
7737
7738 /* In this case dwarf2_compute_name is just a shortcut not building anything
7739 on its own. */
7740 if (!die_needs_namespace (die, cu))
7741 return dwarf2_compute_name (name, die, cu, 1);
7742
7743 back_to = make_cleanup (null_cleanup, NULL);
7744
7745 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7746 if (!attr)
7747 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7748
7749 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7750 has computed. */
7751 if (attr && DW_STRING (attr))
7752 {
7753 char *demangled;
7754
7755 mangled = DW_STRING (attr);
7756
7757 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7758 type. It is easier for GDB users to search for such functions as
7759 `name(params)' than `long name(params)'. In such case the minimal
7760 symbol names do not match the full symbol names but for template
7761 functions there is never a need to look up their definition from their
7762 declaration so the only disadvantage remains the minimal symbol
7763 variant `long name(params)' does not have the proper inferior type.
7764 */
7765
a766d390
DE
7766 if (cu->language == language_go)
7767 {
7768 /* This is a lie, but we already lie to the caller new_symbol_full.
7769 new_symbol_full assumes we return the mangled name.
7770 This just undoes that lie until things are cleaned up. */
7771 demangled = NULL;
7772 }
7773 else
7774 {
7775 demangled = cplus_demangle (mangled,
7776 (DMGL_PARAMS | DMGL_ANSI
7777 | (cu->language == language_java
7778 ? DMGL_JAVA | DMGL_RET_POSTFIX
7779 : DMGL_RET_DROP)));
7780 }
900e11f9
JK
7781 if (demangled)
7782 {
7783 make_cleanup (xfree, demangled);
7784 canon = demangled;
7785 }
7786 else
7787 {
7788 canon = mangled;
7789 need_copy = 0;
7790 }
7791 }
7792
7793 if (canon == NULL || check_physname)
7794 {
7795 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7796
7797 if (canon != NULL && strcmp (physname, canon) != 0)
7798 {
7799 /* It may not mean a bug in GDB. The compiler could also
7800 compute DW_AT_linkage_name incorrectly. But in such case
7801 GDB would need to be bug-to-bug compatible. */
7802
7803 complaint (&symfile_complaints,
7804 _("Computed physname <%s> does not match demangled <%s> "
7805 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7806 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7807
7808 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7809 is available here - over computed PHYSNAME. It is safer
7810 against both buggy GDB and buggy compilers. */
7811
7812 retval = canon;
7813 }
7814 else
7815 {
7816 retval = physname;
7817 need_copy = 0;
7818 }
7819 }
7820 else
7821 retval = canon;
7822
7823 if (need_copy)
10f0c4bb 7824 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7825
7826 do_cleanups (back_to);
7827 return retval;
0114d602
DJ
7828}
7829
27aa8d6a
SW
7830/* Read the import statement specified by the given die and record it. */
7831
7832static void
7833read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7834{
bb5ed363 7835 struct objfile *objfile = cu->objfile;
27aa8d6a 7836 struct attribute *import_attr;
32019081 7837 struct die_info *imported_die, *child_die;
de4affc9 7838 struct dwarf2_cu *imported_cu;
27aa8d6a 7839 const char *imported_name;
794684b6 7840 const char *imported_name_prefix;
13387711
SW
7841 const char *canonical_name;
7842 const char *import_alias;
7843 const char *imported_declaration = NULL;
794684b6 7844 const char *import_prefix;
32019081
JK
7845 VEC (const_char_ptr) *excludes = NULL;
7846 struct cleanup *cleanups;
13387711 7847
27aa8d6a
SW
7848 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7849 if (import_attr == NULL)
7850 {
7851 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7852 dwarf_tag_name (die->tag));
7853 return;
7854 }
7855
de4affc9
CC
7856 imported_cu = cu;
7857 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7858 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7859 if (imported_name == NULL)
7860 {
7861 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7862
7863 The import in the following code:
7864 namespace A
7865 {
7866 typedef int B;
7867 }
7868
7869 int main ()
7870 {
7871 using A::B;
7872 B b;
7873 return b;
7874 }
7875
7876 ...
7877 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7878 <52> DW_AT_decl_file : 1
7879 <53> DW_AT_decl_line : 6
7880 <54> DW_AT_import : <0x75>
7881 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7882 <59> DW_AT_name : B
7883 <5b> DW_AT_decl_file : 1
7884 <5c> DW_AT_decl_line : 2
7885 <5d> DW_AT_type : <0x6e>
7886 ...
7887 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7888 <76> DW_AT_byte_size : 4
7889 <77> DW_AT_encoding : 5 (signed)
7890
7891 imports the wrong die ( 0x75 instead of 0x58 ).
7892 This case will be ignored until the gcc bug is fixed. */
7893 return;
7894 }
7895
82856980
SW
7896 /* Figure out the local name after import. */
7897 import_alias = dwarf2_name (die, cu);
27aa8d6a 7898
794684b6
SW
7899 /* Figure out where the statement is being imported to. */
7900 import_prefix = determine_prefix (die, cu);
7901
7902 /* Figure out what the scope of the imported die is and prepend it
7903 to the name of the imported die. */
de4affc9 7904 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7905
f55ee35c
JK
7906 if (imported_die->tag != DW_TAG_namespace
7907 && imported_die->tag != DW_TAG_module)
794684b6 7908 {
13387711
SW
7909 imported_declaration = imported_name;
7910 canonical_name = imported_name_prefix;
794684b6 7911 }
13387711 7912 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7913 canonical_name = obconcat (&objfile->objfile_obstack,
7914 imported_name_prefix, "::", imported_name,
7915 (char *) NULL);
13387711
SW
7916 else
7917 canonical_name = imported_name;
794684b6 7918
32019081
JK
7919 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7920
7921 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7922 for (child_die = die->child; child_die && child_die->tag;
7923 child_die = sibling_die (child_die))
7924 {
7925 /* DWARF-4: A Fortran use statement with a “rename list” may be
7926 represented by an imported module entry with an import attribute
7927 referring to the module and owned entries corresponding to those
7928 entities that are renamed as part of being imported. */
7929
7930 if (child_die->tag != DW_TAG_imported_declaration)
7931 {
7932 complaint (&symfile_complaints,
7933 _("child DW_TAG_imported_declaration expected "
7934 "- DIE at 0x%x [in module %s]"),
b64f50a1 7935 child_die->offset.sect_off, objfile->name);
32019081
JK
7936 continue;
7937 }
7938
7939 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7940 if (import_attr == NULL)
7941 {
7942 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7943 dwarf_tag_name (child_die->tag));
7944 continue;
7945 }
7946
7947 imported_cu = cu;
7948 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7949 &imported_cu);
7950 imported_name = dwarf2_name (imported_die, imported_cu);
7951 if (imported_name == NULL)
7952 {
7953 complaint (&symfile_complaints,
7954 _("child DW_TAG_imported_declaration has unknown "
7955 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7956 child_die->offset.sect_off, objfile->name);
32019081
JK
7957 continue;
7958 }
7959
7960 VEC_safe_push (const_char_ptr, excludes, imported_name);
7961
7962 process_die (child_die, cu);
7963 }
7964
c0cc3a76
SW
7965 cp_add_using_directive (import_prefix,
7966 canonical_name,
7967 import_alias,
13387711 7968 imported_declaration,
32019081 7969 excludes,
12aaed36 7970 0,
bb5ed363 7971 &objfile->objfile_obstack);
32019081
JK
7972
7973 do_cleanups (cleanups);
27aa8d6a
SW
7974}
7975
f4dc4d17 7976/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7977
cb1df416
DJ
7978static void
7979free_cu_line_header (void *arg)
7980{
7981 struct dwarf2_cu *cu = arg;
7982
7983 free_line_header (cu->line_header);
7984 cu->line_header = NULL;
7985}
7986
1b80a9fa
JK
7987/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7988 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7989 this, it was first present in GCC release 4.3.0. */
7990
7991static int
7992producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7993{
7994 if (!cu->checked_producer)
7995 check_producer (cu);
7996
7997 return cu->producer_is_gcc_lt_4_3;
7998}
7999
9291a0cd
TT
8000static void
8001find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8002 const char **name, const char **comp_dir)
9291a0cd
TT
8003{
8004 struct attribute *attr;
8005
8006 *name = NULL;
8007 *comp_dir = NULL;
8008
8009 /* Find the filename. Do not use dwarf2_name here, since the filename
8010 is not a source language identifier. */
8011 attr = dwarf2_attr (die, DW_AT_name, cu);
8012 if (attr)
8013 {
8014 *name = DW_STRING (attr);
8015 }
8016
8017 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8018 if (attr)
8019 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8020 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8021 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8022 {
15d034d0
TT
8023 char *d = ldirname (*name);
8024
8025 *comp_dir = d;
8026 if (d != NULL)
8027 make_cleanup (xfree, d);
9291a0cd
TT
8028 }
8029 if (*comp_dir != NULL)
8030 {
8031 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8032 directory, get rid of it. */
8033 char *cp = strchr (*comp_dir, ':');
8034
8035 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8036 *comp_dir = cp + 1;
8037 }
8038
8039 if (*name == NULL)
8040 *name = "<unknown>";
8041}
8042
f4dc4d17
DE
8043/* Handle DW_AT_stmt_list for a compilation unit.
8044 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8045 COMP_DIR is the compilation directory.
8046 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8047
8048static void
8049handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 8050 const char *comp_dir)
2ab95328
TT
8051{
8052 struct attribute *attr;
2ab95328 8053
f4dc4d17
DE
8054 gdb_assert (! cu->per_cu->is_debug_types);
8055
2ab95328
TT
8056 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8057 if (attr)
8058 {
8059 unsigned int line_offset = DW_UNSND (attr);
8060 struct line_header *line_header
3019eac3 8061 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8062
8063 if (line_header)
dee91e82
DE
8064 {
8065 cu->line_header = line_header;
8066 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8067 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8068 }
2ab95328
TT
8069 }
8070}
8071
95554aad 8072/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8073
c906108c 8074static void
e7c27a73 8075read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8076{
dee91e82 8077 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8078 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8079 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8080 CORE_ADDR highpc = ((CORE_ADDR) 0);
8081 struct attribute *attr;
15d034d0
TT
8082 const char *name = NULL;
8083 const char *comp_dir = NULL;
c906108c
SS
8084 struct die_info *child_die;
8085 bfd *abfd = objfile->obfd;
e142c38c 8086 CORE_ADDR baseaddr;
6e70227d 8087
e142c38c 8088 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8089
fae299cd 8090 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8091
8092 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8093 from finish_block. */
2acceee2 8094 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8095 lowpc = highpc;
8096 lowpc += baseaddr;
8097 highpc += baseaddr;
8098
9291a0cd 8099 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8100
95554aad 8101 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8102
f4b8a18d
KW
8103 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8104 standardised yet. As a workaround for the language detection we fall
8105 back to the DW_AT_producer string. */
8106 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8107 cu->language = language_opencl;
8108
3019eac3
DE
8109 /* Similar hack for Go. */
8110 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8111 set_cu_language (DW_LANG_Go, cu);
8112
f4dc4d17 8113 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8114
8115 /* Decode line number information if present. We do this before
8116 processing child DIEs, so that the line header table is available
8117 for DW_AT_decl_file. */
f4dc4d17 8118 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8119
8120 /* Process all dies in compilation unit. */
8121 if (die->child != NULL)
8122 {
8123 child_die = die->child;
8124 while (child_die && child_die->tag)
8125 {
8126 process_die (child_die, cu);
8127 child_die = sibling_die (child_die);
8128 }
8129 }
8130
8131 /* Decode macro information, if present. Dwarf 2 macro information
8132 refers to information in the line number info statement program
8133 header, so we can only read it if we've read the header
8134 successfully. */
8135 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8136 if (attr && cu->line_header)
8137 {
8138 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8139 complaint (&symfile_complaints,
8140 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8141
09262596 8142 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8143 }
8144 else
8145 {
8146 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8147 if (attr && cu->line_header)
8148 {
8149 unsigned int macro_offset = DW_UNSND (attr);
8150
09262596 8151 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8152 }
8153 }
8154
8155 do_cleanups (back_to);
8156}
8157
f4dc4d17
DE
8158/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8159 Create the set of symtabs used by this TU, or if this TU is sharing
8160 symtabs with another TU and the symtabs have already been created
8161 then restore those symtabs in the line header.
8162 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8163
8164static void
f4dc4d17 8165setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8166{
f4dc4d17
DE
8167 struct objfile *objfile = dwarf2_per_objfile->objfile;
8168 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8169 struct type_unit_group *tu_group;
8170 int first_time;
8171 struct line_header *lh;
3019eac3 8172 struct attribute *attr;
f4dc4d17 8173 unsigned int i, line_offset;
3019eac3 8174
f4dc4d17 8175 gdb_assert (per_cu->is_debug_types);
3019eac3 8176
f4dc4d17 8177 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8178
f4dc4d17
DE
8179 /* If we're using .gdb_index (includes -readnow) then
8180 per_cu->s.type_unit_group may not have been set up yet. */
796a7ff8
DE
8181 if (per_cu->type_unit_group == NULL)
8182 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8183 tu_group = per_cu->type_unit_group;
f4dc4d17
DE
8184
8185 /* If we've already processed this stmt_list there's no real need to
8186 do it again, we could fake it and just recreate the part we need
8187 (file name,index -> symtab mapping). If data shows this optimization
8188 is useful we can do it then. */
8189 first_time = tu_group->primary_symtab == NULL;
8190
8191 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8192 debug info. */
8193 lh = NULL;
8194 if (attr != NULL)
3019eac3 8195 {
f4dc4d17
DE
8196 line_offset = DW_UNSND (attr);
8197 lh = dwarf_decode_line_header (line_offset, cu);
8198 }
8199 if (lh == NULL)
8200 {
8201 if (first_time)
8202 dwarf2_start_symtab (cu, "", NULL, 0);
8203 else
8204 {
8205 gdb_assert (tu_group->symtabs == NULL);
8206 restart_symtab (0);
8207 }
8208 /* Note: The primary symtab will get allocated at the end. */
8209 return;
3019eac3
DE
8210 }
8211
f4dc4d17
DE
8212 cu->line_header = lh;
8213 make_cleanup (free_cu_line_header, cu);
3019eac3 8214
f4dc4d17
DE
8215 if (first_time)
8216 {
8217 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8218
f4dc4d17
DE
8219 tu_group->num_symtabs = lh->num_file_names;
8220 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8221
f4dc4d17
DE
8222 for (i = 0; i < lh->num_file_names; ++i)
8223 {
8224 char *dir = NULL;
8225 struct file_entry *fe = &lh->file_names[i];
3019eac3 8226
f4dc4d17
DE
8227 if (fe->dir_index)
8228 dir = lh->include_dirs[fe->dir_index - 1];
8229 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8230
f4dc4d17
DE
8231 /* Note: We don't have to watch for the main subfile here, type units
8232 don't have DW_AT_name. */
3019eac3 8233
f4dc4d17
DE
8234 if (current_subfile->symtab == NULL)
8235 {
8236 /* NOTE: start_subfile will recognize when it's been passed
8237 a file it has already seen. So we can't assume there's a
8238 simple mapping from lh->file_names to subfiles,
8239 lh->file_names may contain dups. */
8240 current_subfile->symtab = allocate_symtab (current_subfile->name,
8241 objfile);
8242 }
8243
8244 fe->symtab = current_subfile->symtab;
8245 tu_group->symtabs[i] = fe->symtab;
8246 }
8247 }
8248 else
3019eac3 8249 {
f4dc4d17
DE
8250 restart_symtab (0);
8251
8252 for (i = 0; i < lh->num_file_names; ++i)
8253 {
8254 struct file_entry *fe = &lh->file_names[i];
8255
8256 fe->symtab = tu_group->symtabs[i];
8257 }
3019eac3
DE
8258 }
8259
f4dc4d17
DE
8260 /* The main symtab is allocated last. Type units don't have DW_AT_name
8261 so they don't have a "real" (so to speak) symtab anyway.
8262 There is later code that will assign the main symtab to all symbols
8263 that don't have one. We need to handle the case of a symbol with a
8264 missing symtab (DW_AT_decl_file) anyway. */
8265}
3019eac3 8266
f4dc4d17
DE
8267/* Process DW_TAG_type_unit.
8268 For TUs we want to skip the first top level sibling if it's not the
8269 actual type being defined by this TU. In this case the first top
8270 level sibling is there to provide context only. */
3019eac3 8271
f4dc4d17
DE
8272static void
8273read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8274{
8275 struct die_info *child_die;
3019eac3 8276
f4dc4d17
DE
8277 prepare_one_comp_unit (cu, die, language_minimal);
8278
8279 /* Initialize (or reinitialize) the machinery for building symtabs.
8280 We do this before processing child DIEs, so that the line header table
8281 is available for DW_AT_decl_file. */
8282 setup_type_unit_groups (die, cu);
8283
8284 if (die->child != NULL)
8285 {
8286 child_die = die->child;
8287 while (child_die && child_die->tag)
8288 {
8289 process_die (child_die, cu);
8290 child_die = sibling_die (child_die);
8291 }
8292 }
3019eac3
DE
8293}
8294\f
80626a55
DE
8295/* DWO/DWP files.
8296
8297 http://gcc.gnu.org/wiki/DebugFission
8298 http://gcc.gnu.org/wiki/DebugFissionDWP
8299
8300 To simplify handling of both DWO files ("object" files with the DWARF info)
8301 and DWP files (a file with the DWOs packaged up into one file), we treat
8302 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8303
8304static hashval_t
8305hash_dwo_file (const void *item)
8306{
8307 const struct dwo_file *dwo_file = item;
8308
80626a55 8309 return htab_hash_string (dwo_file->name);
3019eac3
DE
8310}
8311
8312static int
8313eq_dwo_file (const void *item_lhs, const void *item_rhs)
8314{
8315 const struct dwo_file *lhs = item_lhs;
8316 const struct dwo_file *rhs = item_rhs;
8317
80626a55 8318 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8319}
8320
8321/* Allocate a hash table for DWO files. */
8322
8323static htab_t
8324allocate_dwo_file_hash_table (void)
8325{
8326 struct objfile *objfile = dwarf2_per_objfile->objfile;
8327
8328 return htab_create_alloc_ex (41,
8329 hash_dwo_file,
8330 eq_dwo_file,
8331 NULL,
8332 &objfile->objfile_obstack,
8333 hashtab_obstack_allocate,
8334 dummy_obstack_deallocate);
8335}
8336
80626a55
DE
8337/* Lookup DWO file DWO_NAME. */
8338
8339static void **
8340lookup_dwo_file_slot (const char *dwo_name)
8341{
8342 struct dwo_file find_entry;
8343 void **slot;
8344
8345 if (dwarf2_per_objfile->dwo_files == NULL)
8346 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8347
8348 memset (&find_entry, 0, sizeof (find_entry));
8349 find_entry.name = dwo_name;
8350 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8351
8352 return slot;
8353}
8354
3019eac3
DE
8355static hashval_t
8356hash_dwo_unit (const void *item)
8357{
8358 const struct dwo_unit *dwo_unit = item;
8359
8360 /* This drops the top 32 bits of the id, but is ok for a hash. */
8361 return dwo_unit->signature;
8362}
8363
8364static int
8365eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8366{
8367 const struct dwo_unit *lhs = item_lhs;
8368 const struct dwo_unit *rhs = item_rhs;
8369
8370 /* The signature is assumed to be unique within the DWO file.
8371 So while object file CU dwo_id's always have the value zero,
8372 that's OK, assuming each object file DWO file has only one CU,
8373 and that's the rule for now. */
8374 return lhs->signature == rhs->signature;
8375}
8376
8377/* Allocate a hash table for DWO CUs,TUs.
8378 There is one of these tables for each of CUs,TUs for each DWO file. */
8379
8380static htab_t
8381allocate_dwo_unit_table (struct objfile *objfile)
8382{
8383 /* Start out with a pretty small number.
8384 Generally DWO files contain only one CU and maybe some TUs. */
8385 return htab_create_alloc_ex (3,
8386 hash_dwo_unit,
8387 eq_dwo_unit,
8388 NULL,
8389 &objfile->objfile_obstack,
8390 hashtab_obstack_allocate,
8391 dummy_obstack_deallocate);
8392}
8393
80626a55 8394/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8395
8396struct create_dwo_info_table_data
8397{
8398 struct dwo_file *dwo_file;
8399 htab_t cu_htab;
8400};
8401
80626a55 8402/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8403
8404static void
80626a55
DE
8405create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8406 gdb_byte *info_ptr,
8407 struct die_info *comp_unit_die,
8408 int has_children,
8409 void *datap)
3019eac3
DE
8410{
8411 struct dwarf2_cu *cu = reader->cu;
8412 struct objfile *objfile = dwarf2_per_objfile->objfile;
8413 sect_offset offset = cu->per_cu->offset;
8a0459fd 8414 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3
DE
8415 struct create_dwo_info_table_data *data = datap;
8416 struct dwo_file *dwo_file = data->dwo_file;
8417 htab_t cu_htab = data->cu_htab;
8418 void **slot;
8419 struct attribute *attr;
8420 struct dwo_unit *dwo_unit;
8421
8422 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8423 if (attr == NULL)
8424 {
8425 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8426 " its dwo_id [in module %s]"),
80626a55 8427 offset.sect_off, dwo_file->name);
3019eac3
DE
8428 return;
8429 }
8430
8431 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8432 dwo_unit->dwo_file = dwo_file;
8433 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8434 dwo_unit->section = section;
3019eac3
DE
8435 dwo_unit->offset = offset;
8436 dwo_unit->length = cu->per_cu->length;
8437
8438 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8439 gdb_assert (slot != NULL);
8440 if (*slot != NULL)
8441 {
8442 const struct dwo_unit *dup_dwo_unit = *slot;
8443
8444 complaint (&symfile_complaints,
8445 _("debug entry at offset 0x%x is duplicate to the entry at"
8446 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8447 offset.sect_off, dup_dwo_unit->offset.sect_off,
8448 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8449 dwo_file->name);
3019eac3
DE
8450 }
8451 else
8452 *slot = dwo_unit;
8453
09406207 8454 if (dwarf2_read_debug)
3019eac3
DE
8455 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8456 offset.sect_off,
8457 phex (dwo_unit->signature,
8458 sizeof (dwo_unit->signature)));
8459}
8460
80626a55
DE
8461/* Create a hash table to map DWO IDs to their CU entry in
8462 .debug_info.dwo in DWO_FILE.
c88ee1f0
DE
8463 Note: This function processes DWO files only, not DWP files.
8464 Note: A DWO file generally contains one CU, but we don't assume this. */
3019eac3
DE
8465
8466static htab_t
80626a55 8467create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8468{
8469 struct objfile *objfile = dwarf2_per_objfile->objfile;
8470 struct dwarf2_section_info *section = &dwo_file->sections.info;
8471 bfd *abfd;
8472 htab_t cu_htab;
8473 gdb_byte *info_ptr, *end_ptr;
8474 struct create_dwo_info_table_data create_dwo_info_table_data;
8475
8476 dwarf2_read_section (objfile, section);
8477 info_ptr = section->buffer;
8478
8479 if (info_ptr == NULL)
8480 return NULL;
8481
8482 /* We can't set abfd until now because the section may be empty or
8483 not present, in which case section->asection will be NULL. */
8484 abfd = section->asection->owner;
8485
09406207 8486 if (dwarf2_read_debug)
3019eac3
DE
8487 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8488 bfd_get_filename (abfd));
8489
8490 cu_htab = allocate_dwo_unit_table (objfile);
8491
8492 create_dwo_info_table_data.dwo_file = dwo_file;
8493 create_dwo_info_table_data.cu_htab = cu_htab;
8494
8495 end_ptr = info_ptr + section->size;
8496 while (info_ptr < end_ptr)
8497 {
8498 struct dwarf2_per_cu_data per_cu;
8499
8500 memset (&per_cu, 0, sizeof (per_cu));
8501 per_cu.objfile = objfile;
8502 per_cu.is_debug_types = 0;
8503 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8504 per_cu.section = section;
3019eac3
DE
8505
8506 init_cutu_and_read_dies_no_follow (&per_cu,
8507 &dwo_file->sections.abbrev,
8508 dwo_file,
80626a55 8509 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8510 &create_dwo_info_table_data);
8511
8512 info_ptr += per_cu.length;
8513 }
8514
8515 return cu_htab;
8516}
8517
80626a55
DE
8518/* DWP file .debug_{cu,tu}_index section format:
8519 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8520
8521 Both index sections have the same format, and serve to map a 64-bit
8522 signature to a set of section numbers. Each section begins with a header,
8523 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8524 indexes, and a pool of 32-bit section numbers. The index sections will be
8525 aligned at 8-byte boundaries in the file.
8526
8527 The index section header contains two unsigned 32-bit values (using the
8528 byte order of the application binary):
8529
8530 N, the number of compilation units or type units in the index
8531 M, the number of slots in the hash table
8532
8533 (We assume that N and M will not exceed 2^32 - 1.)
8534
8535 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8536
8537 The hash table begins at offset 8 in the section, and consists of an array
8538 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8539 order of the application binary). Unused slots in the hash table are 0.
8540 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8541
8542 The parallel table begins immediately after the hash table
8543 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8544 array of 32-bit indexes (using the byte order of the application binary),
8545 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8546 table contains a 32-bit index into the pool of section numbers. For unused
8547 hash table slots, the corresponding entry in the parallel table will be 0.
8548
8549 Given a 64-bit compilation unit signature or a type signature S, an entry
8550 in the hash table is located as follows:
8551
8552 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8553 the low-order k bits all set to 1.
8554
8555 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8556
8557 3) If the hash table entry at index H matches the signature, use that
8558 entry. If the hash table entry at index H is unused (all zeroes),
8559 terminate the search: the signature is not present in the table.
8560
8561 4) Let H = (H + H') modulo M. Repeat at Step 3.
8562
8563 Because M > N and H' and M are relatively prime, the search is guaranteed
8564 to stop at an unused slot or find the match.
8565
8566 The pool of section numbers begins immediately following the hash table
8567 (at offset 8 + 12 * M from the beginning of the section). The pool of
8568 section numbers consists of an array of 32-bit words (using the byte order
8569 of the application binary). Each item in the array is indexed starting
8570 from 0. The hash table entry provides the index of the first section
8571 number in the set. Additional section numbers in the set follow, and the
8572 set is terminated by a 0 entry (section number 0 is not used in ELF).
8573
8574 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8575 section must be the first entry in the set, and the .debug_abbrev.dwo must
8576 be the second entry. Other members of the set may follow in any order. */
8577
8578/* Create a hash table to map DWO IDs to their CU/TU entry in
8579 .debug_{info,types}.dwo in DWP_FILE.
8580 Returns NULL if there isn't one.
8581 Note: This function processes DWP files only, not DWO files. */
8582
8583static struct dwp_hash_table *
8584create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8585{
8586 struct objfile *objfile = dwarf2_per_objfile->objfile;
8587 bfd *dbfd = dwp_file->dbfd;
8588 char *index_ptr, *index_end;
8589 struct dwarf2_section_info *index;
8590 uint32_t version, nr_units, nr_slots;
8591 struct dwp_hash_table *htab;
8592
8593 if (is_debug_types)
8594 index = &dwp_file->sections.tu_index;
8595 else
8596 index = &dwp_file->sections.cu_index;
8597
8598 if (dwarf2_section_empty_p (index))
8599 return NULL;
8600 dwarf2_read_section (objfile, index);
8601
8602 index_ptr = index->buffer;
8603 index_end = index_ptr + index->size;
8604
8605 version = read_4_bytes (dbfd, index_ptr);
8606 index_ptr += 8; /* Skip the unused word. */
8607 nr_units = read_4_bytes (dbfd, index_ptr);
8608 index_ptr += 4;
8609 nr_slots = read_4_bytes (dbfd, index_ptr);
8610 index_ptr += 4;
8611
8612 if (version != 1)
8613 {
8614 error (_("Dwarf Error: unsupported DWP file version (%u)"
8615 " [in module %s]"),
8616 version, dwp_file->name);
8617 }
8618 if (nr_slots != (nr_slots & -nr_slots))
8619 {
8620 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8621 " is not power of 2 [in module %s]"),
8622 nr_slots, dwp_file->name);
8623 }
8624
8625 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8626 htab->nr_units = nr_units;
8627 htab->nr_slots = nr_slots;
8628 htab->hash_table = index_ptr;
8629 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8630 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8631
8632 return htab;
8633}
8634
8635/* Update SECTIONS with the data from SECTP.
8636
8637 This function is like the other "locate" section routines that are
8638 passed to bfd_map_over_sections, but in this context the sections to
8639 read comes from the DWP hash table, not the full ELF section table.
8640
8641 The result is non-zero for success, or zero if an error was found. */
8642
8643static int
8644locate_virtual_dwo_sections (asection *sectp,
8645 struct virtual_dwo_sections *sections)
8646{
8647 const struct dwop_section_names *names = &dwop_section_names;
8648
8649 if (section_is_p (sectp->name, &names->abbrev_dwo))
8650 {
8651 /* There can be only one. */
8652 if (sections->abbrev.asection != NULL)
8653 return 0;
8654 sections->abbrev.asection = sectp;
8655 sections->abbrev.size = bfd_get_section_size (sectp);
8656 }
8657 else if (section_is_p (sectp->name, &names->info_dwo)
8658 || section_is_p (sectp->name, &names->types_dwo))
8659 {
8660 /* There can be only one. */
8661 if (sections->info_or_types.asection != NULL)
8662 return 0;
8663 sections->info_or_types.asection = sectp;
8664 sections->info_or_types.size = bfd_get_section_size (sectp);
8665 }
8666 else if (section_is_p (sectp->name, &names->line_dwo))
8667 {
8668 /* There can be only one. */
8669 if (sections->line.asection != NULL)
8670 return 0;
8671 sections->line.asection = sectp;
8672 sections->line.size = bfd_get_section_size (sectp);
8673 }
8674 else if (section_is_p (sectp->name, &names->loc_dwo))
8675 {
8676 /* There can be only one. */
8677 if (sections->loc.asection != NULL)
8678 return 0;
8679 sections->loc.asection = sectp;
8680 sections->loc.size = bfd_get_section_size (sectp);
8681 }
8682 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8683 {
8684 /* There can be only one. */
8685 if (sections->macinfo.asection != NULL)
8686 return 0;
8687 sections->macinfo.asection = sectp;
8688 sections->macinfo.size = bfd_get_section_size (sectp);
8689 }
8690 else if (section_is_p (sectp->name, &names->macro_dwo))
8691 {
8692 /* There can be only one. */
8693 if (sections->macro.asection != NULL)
8694 return 0;
8695 sections->macro.asection = sectp;
8696 sections->macro.size = bfd_get_section_size (sectp);
8697 }
8698 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8699 {
8700 /* There can be only one. */
8701 if (sections->str_offsets.asection != NULL)
8702 return 0;
8703 sections->str_offsets.asection = sectp;
8704 sections->str_offsets.size = bfd_get_section_size (sectp);
8705 }
8706 else
8707 {
8708 /* No other kind of section is valid. */
8709 return 0;
8710 }
8711
8712 return 1;
8713}
8714
8715/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8716 HTAB is the hash table from the DWP file.
8717 SECTION_INDEX is the index of the DWO in HTAB. */
8718
8719static struct dwo_unit *
8720create_dwo_in_dwp (struct dwp_file *dwp_file,
8721 const struct dwp_hash_table *htab,
8722 uint32_t section_index,
8723 ULONGEST signature, int is_debug_types)
8724{
8725 struct objfile *objfile = dwarf2_per_objfile->objfile;
8726 bfd *dbfd = dwp_file->dbfd;
8727 const char *kind = is_debug_types ? "TU" : "CU";
8728 struct dwo_file *dwo_file;
8729 struct dwo_unit *dwo_unit;
8730 struct virtual_dwo_sections sections;
8731 void **dwo_file_slot;
8732 char *virtual_dwo_name;
8733 struct dwarf2_section_info *cutu;
8734 struct cleanup *cleanups;
8735 int i;
8736
8737 if (dwarf2_read_debug)
8738 {
8739 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8740 kind,
8741 section_index, phex (signature, sizeof (signature)),
8742 dwp_file->name);
8743 }
8744
8745 /* Fetch the sections of this DWO.
8746 Put a limit on the number of sections we look for so that bad data
8747 doesn't cause us to loop forever. */
8748
8749#define MAX_NR_DWO_SECTIONS \
8750 (1 /* .debug_info or .debug_types */ \
8751 + 1 /* .debug_abbrev */ \
8752 + 1 /* .debug_line */ \
8753 + 1 /* .debug_loc */ \
8754 + 1 /* .debug_str_offsets */ \
8755 + 1 /* .debug_macro */ \
8756 + 1 /* .debug_macinfo */ \
8757 + 1 /* trailing zero */)
8758
8759 memset (&sections, 0, sizeof (sections));
8760 cleanups = make_cleanup (null_cleanup, 0);
8761
8762 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8763 {
8764 asection *sectp;
8765 uint32_t section_nr =
8766 read_4_bytes (dbfd,
8767 htab->section_pool
8768 + (section_index + i) * sizeof (uint32_t));
8769
8770 if (section_nr == 0)
8771 break;
8772 if (section_nr >= dwp_file->num_sections)
8773 {
8774 error (_("Dwarf Error: bad DWP hash table, section number too large"
8775 " [in module %s]"),
8776 dwp_file->name);
8777 }
8778
8779 sectp = dwp_file->elf_sections[section_nr];
8780 if (! locate_virtual_dwo_sections (sectp, &sections))
8781 {
8782 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8783 " [in module %s]"),
8784 dwp_file->name);
8785 }
8786 }
8787
8788 if (i < 2
8789 || sections.info_or_types.asection == NULL
8790 || sections.abbrev.asection == NULL)
8791 {
8792 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8793 " [in module %s]"),
8794 dwp_file->name);
8795 }
8796 if (i == MAX_NR_DWO_SECTIONS)
8797 {
8798 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8799 " [in module %s]"),
8800 dwp_file->name);
8801 }
8802
8803 /* It's easier for the rest of the code if we fake a struct dwo_file and
8804 have dwo_unit "live" in that. At least for now.
8805
8806 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8807 However, for each CU + set of TUs that came from the same original DWO
8808 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8809 (fewer struct dwo_file objects to allocated). Remember that for really
8810 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8811
2792b94d
PM
8812 virtual_dwo_name =
8813 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8814 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8815 sections.line.asection ? sections.line.asection->id : 0,
8816 sections.loc.asection ? sections.loc.asection->id : 0,
8817 (sections.str_offsets.asection
8818 ? sections.str_offsets.asection->id
8819 : 0));
80626a55
DE
8820 make_cleanup (xfree, virtual_dwo_name);
8821 /* Can we use an existing virtual DWO file? */
8822 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8823 /* Create one if necessary. */
8824 if (*dwo_file_slot == NULL)
8825 {
8826 if (dwarf2_read_debug)
8827 {
8828 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8829 virtual_dwo_name);
8830 }
8831 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8832 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8833 virtual_dwo_name,
8834 strlen (virtual_dwo_name));
8835 dwo_file->sections.abbrev = sections.abbrev;
8836 dwo_file->sections.line = sections.line;
8837 dwo_file->sections.loc = sections.loc;
8838 dwo_file->sections.macinfo = sections.macinfo;
8839 dwo_file->sections.macro = sections.macro;
8840 dwo_file->sections.str_offsets = sections.str_offsets;
8841 /* The "str" section is global to the entire DWP file. */
8842 dwo_file->sections.str = dwp_file->sections.str;
8843 /* The info or types section is assigned later to dwo_unit,
8844 there's no need to record it in dwo_file.
8845 Also, we can't simply record type sections in dwo_file because
8846 we record a pointer into the vector in dwo_unit. As we collect more
8847 types we'll grow the vector and eventually have to reallocate space
8848 for it, invalidating all the pointers into the current copy. */
8849 *dwo_file_slot = dwo_file;
8850 }
8851 else
8852 {
8853 if (dwarf2_read_debug)
8854 {
8855 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8856 virtual_dwo_name);
8857 }
8858 dwo_file = *dwo_file_slot;
8859 }
8860 do_cleanups (cleanups);
8861
8862 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8863 dwo_unit->dwo_file = dwo_file;
8864 dwo_unit->signature = signature;
8a0459fd
DE
8865 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8866 sizeof (struct dwarf2_section_info));
8867 *dwo_unit->section = sections.info_or_types;
80626a55
DE
8868 /* offset, length, type_offset_in_tu are set later. */
8869
8870 return dwo_unit;
8871}
8872
8873/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8874
8875static struct dwo_unit *
8876lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8877 const struct dwp_hash_table *htab,
8878 ULONGEST signature, int is_debug_types)
8879{
8880 bfd *dbfd = dwp_file->dbfd;
8881 uint32_t mask = htab->nr_slots - 1;
8882 uint32_t hash = signature & mask;
8883 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8884 unsigned int i;
8885 void **slot;
8886 struct dwo_unit find_dwo_cu, *dwo_cu;
8887
8888 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8889 find_dwo_cu.signature = signature;
8890 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8891
8892 if (*slot != NULL)
8893 return *slot;
8894
8895 /* Use a for loop so that we don't loop forever on bad debug info. */
8896 for (i = 0; i < htab->nr_slots; ++i)
8897 {
8898 ULONGEST signature_in_table;
8899
8900 signature_in_table =
8901 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8902 if (signature_in_table == signature)
8903 {
8904 uint32_t section_index =
8905 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8906
8907 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8908 signature, is_debug_types);
8909 return *slot;
8910 }
8911 if (signature_in_table == 0)
8912 return NULL;
8913 hash = (hash + hash2) & mask;
8914 }
8915
8916 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8917 " [in module %s]"),
8918 dwp_file->name);
8919}
8920
8921/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8922 Open the file specified by FILE_NAME and hand it off to BFD for
8923 preliminary analysis. Return a newly initialized bfd *, which
8924 includes a canonicalized copy of FILE_NAME.
80626a55 8925 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8926 In case of trouble, return NULL.
8927 NOTE: This function is derived from symfile_bfd_open. */
8928
8929static bfd *
80626a55 8930try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8931{
8932 bfd *sym_bfd;
80626a55 8933 int desc, flags;
3019eac3 8934 char *absolute_name;
3019eac3 8935
80626a55
DE
8936 flags = OPF_TRY_CWD_FIRST;
8937 if (is_dwp)
8938 flags |= OPF_SEARCH_IN_PATH;
8939 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8940 O_RDONLY | O_BINARY, &absolute_name);
8941 if (desc < 0)
8942 return NULL;
8943
bb397797 8944 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8945 if (!sym_bfd)
8946 {
3019eac3
DE
8947 xfree (absolute_name);
8948 return NULL;
8949 }
a4453b7e 8950 xfree (absolute_name);
3019eac3
DE
8951 bfd_set_cacheable (sym_bfd, 1);
8952
8953 if (!bfd_check_format (sym_bfd, bfd_object))
8954 {
cbb099e8 8955 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8956 return NULL;
8957 }
8958
3019eac3
DE
8959 return sym_bfd;
8960}
8961
80626a55 8962/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8963 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8964 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8965 The result is the bfd handle of the file.
8966 If there is a problem finding or opening the file, return NULL.
8967 Upon success, the canonicalized path of the file is stored in the bfd,
8968 same as symfile_bfd_open. */
8969
8970static bfd *
80626a55 8971open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8972{
8973 bfd *abfd;
3019eac3 8974
80626a55
DE
8975 if (IS_ABSOLUTE_PATH (file_name))
8976 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8977
8978 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8979
8980 if (comp_dir != NULL)
8981 {
80626a55 8982 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8983
8984 /* NOTE: If comp_dir is a relative path, this will also try the
8985 search path, which seems useful. */
80626a55 8986 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8987 xfree (path_to_try);
8988 if (abfd != NULL)
8989 return abfd;
8990 }
8991
8992 /* That didn't work, try debug-file-directory, which, despite its name,
8993 is a list of paths. */
8994
8995 if (*debug_file_directory == '\0')
8996 return NULL;
8997
80626a55 8998 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8999}
9000
80626a55
DE
9001/* This function is mapped across the sections and remembers the offset and
9002 size of each of the DWO debugging sections we are interested in. */
9003
9004static void
9005dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9006{
9007 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9008 const struct dwop_section_names *names = &dwop_section_names;
9009
9010 if (section_is_p (sectp->name, &names->abbrev_dwo))
9011 {
9012 dwo_sections->abbrev.asection = sectp;
9013 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9014 }
9015 else if (section_is_p (sectp->name, &names->info_dwo))
9016 {
9017 dwo_sections->info.asection = sectp;
9018 dwo_sections->info.size = bfd_get_section_size (sectp);
9019 }
9020 else if (section_is_p (sectp->name, &names->line_dwo))
9021 {
9022 dwo_sections->line.asection = sectp;
9023 dwo_sections->line.size = bfd_get_section_size (sectp);
9024 }
9025 else if (section_is_p (sectp->name, &names->loc_dwo))
9026 {
9027 dwo_sections->loc.asection = sectp;
9028 dwo_sections->loc.size = bfd_get_section_size (sectp);
9029 }
9030 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9031 {
9032 dwo_sections->macinfo.asection = sectp;
9033 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9034 }
9035 else if (section_is_p (sectp->name, &names->macro_dwo))
9036 {
9037 dwo_sections->macro.asection = sectp;
9038 dwo_sections->macro.size = bfd_get_section_size (sectp);
9039 }
9040 else if (section_is_p (sectp->name, &names->str_dwo))
9041 {
9042 dwo_sections->str.asection = sectp;
9043 dwo_sections->str.size = bfd_get_section_size (sectp);
9044 }
9045 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9046 {
9047 dwo_sections->str_offsets.asection = sectp;
9048 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9049 }
9050 else if (section_is_p (sectp->name, &names->types_dwo))
9051 {
9052 struct dwarf2_section_info type_section;
9053
9054 memset (&type_section, 0, sizeof (type_section));
9055 type_section.asection = sectp;
9056 type_section.size = bfd_get_section_size (sectp);
9057 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9058 &type_section);
9059 }
9060}
9061
9062/* Initialize the use of the DWO file specified by DWO_NAME.
9063 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9064
9065static struct dwo_file *
80626a55 9066open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
9067{
9068 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9069 struct dwo_file *dwo_file;
9070 bfd *dbfd;
3019eac3
DE
9071 struct cleanup *cleanups;
9072
80626a55
DE
9073 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
9074 if (dbfd == NULL)
9075 {
9076 if (dwarf2_read_debug)
9077 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9078 return NULL;
9079 }
9080 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9081 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
9082 dwo_name, strlen (dwo_name));
9083 dwo_file->dbfd = dbfd;
3019eac3
DE
9084
9085 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9086
80626a55 9087 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9088
80626a55 9089 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
9090
9091 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9092 dwo_file->sections.types);
9093
9094 discard_cleanups (cleanups);
9095
80626a55
DE
9096 if (dwarf2_read_debug)
9097 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9098
3019eac3
DE
9099 return dwo_file;
9100}
9101
80626a55
DE
9102/* This function is mapped across the sections and remembers the offset and
9103 size of each of the DWP debugging sections we are interested in. */
3019eac3 9104
80626a55
DE
9105static void
9106dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9107{
80626a55
DE
9108 struct dwp_file *dwp_file = dwp_file_ptr;
9109 const struct dwop_section_names *names = &dwop_section_names;
9110 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9111
80626a55
DE
9112 /* Record the ELF section number for later lookup: this is what the
9113 .debug_cu_index,.debug_tu_index tables use. */
9114 gdb_assert (elf_section_nr < dwp_file->num_sections);
9115 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9116
80626a55
DE
9117 /* Look for specific sections that we need. */
9118 if (section_is_p (sectp->name, &names->str_dwo))
9119 {
9120 dwp_file->sections.str.asection = sectp;
9121 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9122 }
9123 else if (section_is_p (sectp->name, &names->cu_index))
9124 {
9125 dwp_file->sections.cu_index.asection = sectp;
9126 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9127 }
9128 else if (section_is_p (sectp->name, &names->tu_index))
9129 {
9130 dwp_file->sections.tu_index.asection = sectp;
9131 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9132 }
9133}
3019eac3 9134
80626a55 9135/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9136
80626a55
DE
9137static hashval_t
9138hash_dwp_loaded_cutus (const void *item)
9139{
9140 const struct dwo_unit *dwo_unit = item;
3019eac3 9141
80626a55
DE
9142 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9143 return dwo_unit->signature;
3019eac3
DE
9144}
9145
80626a55 9146/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9147
80626a55
DE
9148static int
9149eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9150{
80626a55
DE
9151 const struct dwo_unit *dua = a;
9152 const struct dwo_unit *dub = b;
3019eac3 9153
80626a55
DE
9154 return dua->signature == dub->signature;
9155}
3019eac3 9156
80626a55 9157/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9158
80626a55
DE
9159static htab_t
9160allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9161{
9162 return htab_create_alloc_ex (3,
9163 hash_dwp_loaded_cutus,
9164 eq_dwp_loaded_cutus,
9165 NULL,
9166 &objfile->objfile_obstack,
9167 hashtab_obstack_allocate,
9168 dummy_obstack_deallocate);
9169}
3019eac3 9170
80626a55
DE
9171/* Initialize the use of the DWP file for the current objfile.
9172 By convention the name of the DWP file is ${objfile}.dwp.
9173 The result is NULL if it can't be found. */
a766d390 9174
80626a55
DE
9175static struct dwp_file *
9176open_and_init_dwp_file (const char *comp_dir)
9177{
9178 struct objfile *objfile = dwarf2_per_objfile->objfile;
9179 struct dwp_file *dwp_file;
9180 char *dwp_name;
9181 bfd *dbfd;
9182 struct cleanup *cleanups;
9183
2792b94d 9184 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9185 cleanups = make_cleanup (xfree, dwp_name);
9186
9187 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9188 if (dbfd == NULL)
9189 {
9190 if (dwarf2_read_debug)
9191 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9192 do_cleanups (cleanups);
9193 return NULL;
3019eac3 9194 }
80626a55
DE
9195 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9196 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9197 dwp_name, strlen (dwp_name));
9198 dwp_file->dbfd = dbfd;
9199 do_cleanups (cleanups);
c906108c 9200
80626a55 9201 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9202
80626a55
DE
9203 /* +1: section 0 is unused */
9204 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9205 dwp_file->elf_sections =
9206 OBSTACK_CALLOC (&objfile->objfile_obstack,
9207 dwp_file->num_sections, asection *);
9208
9209 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9210
9211 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9212
9213 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9214
9215 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9216
9217 discard_cleanups (cleanups);
9218
9219 if (dwarf2_read_debug)
9220 {
9221 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9222 fprintf_unfiltered (gdb_stdlog,
9223 " %u CUs, %u TUs\n",
9224 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9225 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9226 }
9227
9228 return dwp_file;
3019eac3 9229}
c906108c 9230
80626a55
DE
9231/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9232 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9233 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9234 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9235 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9236
9237 This is called, for example, when wanting to read a variable with a
9238 complex location. Therefore we don't want to do file i/o for every call.
9239 Therefore we don't want to look for a DWO file on every call.
9240 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9241 then we check if we've already seen DWO_NAME, and only THEN do we check
9242 for a DWO file.
9243
1c658ad5 9244 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9245 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9246
3019eac3 9247static struct dwo_unit *
80626a55
DE
9248lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9249 const char *dwo_name, const char *comp_dir,
9250 ULONGEST signature, int is_debug_types)
3019eac3
DE
9251{
9252 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9253 const char *kind = is_debug_types ? "TU" : "CU";
9254 void **dwo_file_slot;
3019eac3 9255 struct dwo_file *dwo_file;
80626a55 9256 struct dwp_file *dwp_file;
cb1df416 9257
80626a55 9258 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9259
80626a55
DE
9260 if (! dwarf2_per_objfile->dwp_checked)
9261 {
9262 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9263 dwarf2_per_objfile->dwp_checked = 1;
9264 }
9265 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9266
80626a55 9267 if (dwp_file != NULL)
cf2c3c16 9268 {
80626a55
DE
9269 const struct dwp_hash_table *dwp_htab =
9270 is_debug_types ? dwp_file->tus : dwp_file->cus;
9271
9272 if (dwp_htab != NULL)
9273 {
9274 struct dwo_unit *dwo_cutu =
9275 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9276
9277 if (dwo_cutu != NULL)
9278 {
9279 if (dwarf2_read_debug)
9280 {
9281 fprintf_unfiltered (gdb_stdlog,
9282 "Virtual DWO %s %s found: @%s\n",
9283 kind, hex_string (signature),
9284 host_address_to_string (dwo_cutu));
9285 }
9286 return dwo_cutu;
9287 }
9288 }
9289 }
9290
9291 /* Have we already seen DWO_NAME? */
9292
9293 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9294 if (*dwo_file_slot == NULL)
9295 {
9296 /* Read in the file and build a table of the DWOs it contains. */
9297 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9298 }
9299 /* NOTE: This will be NULL if unable to open the file. */
9300 dwo_file = *dwo_file_slot;
9301
9302 if (dwo_file != NULL)
9303 {
9304 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9305
9306 if (htab != NULL)
9307 {
9308 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9309
80626a55
DE
9310 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9311 find_dwo_cutu.signature = signature;
9312 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9313
80626a55
DE
9314 if (dwo_cutu != NULL)
9315 {
9316 if (dwarf2_read_debug)
9317 {
9318 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9319 kind, dwo_name, hex_string (signature),
9320 host_address_to_string (dwo_cutu));
9321 }
9322 return dwo_cutu;
9323 }
9324 }
2e276125 9325 }
9cdd5dbd 9326
80626a55
DE
9327 /* We didn't find it. This could mean a dwo_id mismatch, or
9328 someone deleted the DWO/DWP file, or the search path isn't set up
9329 correctly to find the file. */
9330
9331 if (dwarf2_read_debug)
9332 {
9333 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9334 kind, dwo_name, hex_string (signature));
9335 }
3019eac3
DE
9336
9337 complaint (&symfile_complaints,
80626a55 9338 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9339 " [in module %s]"),
80626a55 9340 this_unit->offset.sect_off, objfile->name);
3019eac3 9341 return NULL;
5fb290d7
DJ
9342}
9343
80626a55
DE
9344/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9345 See lookup_dwo_cutu_unit for details. */
9346
9347static struct dwo_unit *
9348lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9349 const char *dwo_name, const char *comp_dir,
9350 ULONGEST signature)
9351{
9352 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9353}
9354
9355/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9356 See lookup_dwo_cutu_unit for details. */
9357
9358static struct dwo_unit *
9359lookup_dwo_type_unit (struct signatured_type *this_tu,
9360 const char *dwo_name, const char *comp_dir)
9361{
9362 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9363}
9364
3019eac3
DE
9365/* Free all resources associated with DWO_FILE.
9366 Close the DWO file and munmap the sections.
9367 All memory should be on the objfile obstack. */
348e048f
DE
9368
9369static void
3019eac3 9370free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9371{
3019eac3
DE
9372 int ix;
9373 struct dwarf2_section_info *section;
348e048f 9374
80626a55 9375 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9376
3019eac3
DE
9377 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9378}
348e048f 9379
3019eac3 9380/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9381
3019eac3
DE
9382static void
9383free_dwo_file_cleanup (void *arg)
9384{
9385 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9386 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9387
3019eac3
DE
9388 free_dwo_file (dwo_file, objfile);
9389}
348e048f 9390
3019eac3 9391/* Traversal function for free_dwo_files. */
2ab95328 9392
3019eac3
DE
9393static int
9394free_dwo_file_from_slot (void **slot, void *info)
9395{
9396 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9397 struct objfile *objfile = (struct objfile *) info;
348e048f 9398
3019eac3 9399 free_dwo_file (dwo_file, objfile);
348e048f 9400
3019eac3
DE
9401 return 1;
9402}
348e048f 9403
3019eac3 9404/* Free all resources associated with DWO_FILES. */
348e048f 9405
3019eac3
DE
9406static void
9407free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9408{
9409 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9410}
3019eac3
DE
9411\f
9412/* Read in various DIEs. */
348e048f 9413
d389af10
JK
9414/* qsort helper for inherit_abstract_dies. */
9415
9416static int
9417unsigned_int_compar (const void *ap, const void *bp)
9418{
9419 unsigned int a = *(unsigned int *) ap;
9420 unsigned int b = *(unsigned int *) bp;
9421
9422 return (a > b) - (b > a);
9423}
9424
9425/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9426 Inherit only the children of the DW_AT_abstract_origin DIE not being
9427 already referenced by DW_AT_abstract_origin from the children of the
9428 current DIE. */
d389af10
JK
9429
9430static void
9431inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9432{
9433 struct die_info *child_die;
9434 unsigned die_children_count;
9435 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9436 sect_offset *offsets;
9437 sect_offset *offsets_end, *offsetp;
d389af10
JK
9438 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9439 struct die_info *origin_die;
9440 /* Iterator of the ORIGIN_DIE children. */
9441 struct die_info *origin_child_die;
9442 struct cleanup *cleanups;
9443 struct attribute *attr;
cd02d79d
PA
9444 struct dwarf2_cu *origin_cu;
9445 struct pending **origin_previous_list_in_scope;
d389af10
JK
9446
9447 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9448 if (!attr)
9449 return;
9450
cd02d79d
PA
9451 /* Note that following die references may follow to a die in a
9452 different cu. */
9453
9454 origin_cu = cu;
9455 origin_die = follow_die_ref (die, attr, &origin_cu);
9456
9457 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9458 symbols in. */
9459 origin_previous_list_in_scope = origin_cu->list_in_scope;
9460 origin_cu->list_in_scope = cu->list_in_scope;
9461
edb3359d
DJ
9462 if (die->tag != origin_die->tag
9463 && !(die->tag == DW_TAG_inlined_subroutine
9464 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9465 complaint (&symfile_complaints,
9466 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9467 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9468
9469 child_die = die->child;
9470 die_children_count = 0;
9471 while (child_die && child_die->tag)
9472 {
9473 child_die = sibling_die (child_die);
9474 die_children_count++;
9475 }
9476 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9477 cleanups = make_cleanup (xfree, offsets);
9478
9479 offsets_end = offsets;
9480 child_die = die->child;
9481 while (child_die && child_die->tag)
9482 {
c38f313d
DJ
9483 /* For each CHILD_DIE, find the corresponding child of
9484 ORIGIN_DIE. If there is more than one layer of
9485 DW_AT_abstract_origin, follow them all; there shouldn't be,
9486 but GCC versions at least through 4.4 generate this (GCC PR
9487 40573). */
9488 struct die_info *child_origin_die = child_die;
cd02d79d 9489 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9490
c38f313d
DJ
9491 while (1)
9492 {
cd02d79d
PA
9493 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9494 child_origin_cu);
c38f313d
DJ
9495 if (attr == NULL)
9496 break;
cd02d79d
PA
9497 child_origin_die = follow_die_ref (child_origin_die, attr,
9498 &child_origin_cu);
c38f313d
DJ
9499 }
9500
d389af10
JK
9501 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9502 counterpart may exist. */
c38f313d 9503 if (child_origin_die != child_die)
d389af10 9504 {
edb3359d
DJ
9505 if (child_die->tag != child_origin_die->tag
9506 && !(child_die->tag == DW_TAG_inlined_subroutine
9507 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9508 complaint (&symfile_complaints,
9509 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9510 "different tags"), child_die->offset.sect_off,
9511 child_origin_die->offset.sect_off);
c38f313d
DJ
9512 if (child_origin_die->parent != origin_die)
9513 complaint (&symfile_complaints,
9514 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9515 "different parents"), child_die->offset.sect_off,
9516 child_origin_die->offset.sect_off);
c38f313d
DJ
9517 else
9518 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9519 }
9520 child_die = sibling_die (child_die);
9521 }
9522 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9523 unsigned_int_compar);
9524 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9525 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9526 complaint (&symfile_complaints,
9527 _("Multiple children of DIE 0x%x refer "
9528 "to DIE 0x%x as their abstract origin"),
b64f50a1 9529 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9530
9531 offsetp = offsets;
9532 origin_child_die = origin_die->child;
9533 while (origin_child_die && origin_child_die->tag)
9534 {
9535 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9536 while (offsetp < offsets_end
9537 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9538 offsetp++;
b64f50a1
JK
9539 if (offsetp >= offsets_end
9540 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9541 {
9542 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9543 process_die (origin_child_die, origin_cu);
d389af10
JK
9544 }
9545 origin_child_die = sibling_die (origin_child_die);
9546 }
cd02d79d 9547 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9548
9549 do_cleanups (cleanups);
9550}
9551
c906108c 9552static void
e7c27a73 9553read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9554{
e7c27a73 9555 struct objfile *objfile = cu->objfile;
52f0bd74 9556 struct context_stack *new;
c906108c
SS
9557 CORE_ADDR lowpc;
9558 CORE_ADDR highpc;
9559 struct die_info *child_die;
edb3359d 9560 struct attribute *attr, *call_line, *call_file;
15d034d0 9561 const char *name;
e142c38c 9562 CORE_ADDR baseaddr;
801e3a5b 9563 struct block *block;
edb3359d 9564 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9565 VEC (symbolp) *template_args = NULL;
9566 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9567
9568 if (inlined_func)
9569 {
9570 /* If we do not have call site information, we can't show the
9571 caller of this inlined function. That's too confusing, so
9572 only use the scope for local variables. */
9573 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9574 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9575 if (call_line == NULL || call_file == NULL)
9576 {
9577 read_lexical_block_scope (die, cu);
9578 return;
9579 }
9580 }
c906108c 9581
e142c38c
DJ
9582 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9583
94af9270 9584 name = dwarf2_name (die, cu);
c906108c 9585
e8d05480
JB
9586 /* Ignore functions with missing or empty names. These are actually
9587 illegal according to the DWARF standard. */
9588 if (name == NULL)
9589 {
9590 complaint (&symfile_complaints,
b64f50a1
JK
9591 _("missing name for subprogram DIE at %d"),
9592 die->offset.sect_off);
e8d05480
JB
9593 return;
9594 }
9595
9596 /* Ignore functions with missing or invalid low and high pc attributes. */
9597 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9598 {
ae4d0c03
PM
9599 attr = dwarf2_attr (die, DW_AT_external, cu);
9600 if (!attr || !DW_UNSND (attr))
9601 complaint (&symfile_complaints,
3e43a32a
MS
9602 _("cannot get low and high bounds "
9603 "for subprogram DIE at %d"),
b64f50a1 9604 die->offset.sect_off);
e8d05480
JB
9605 return;
9606 }
c906108c
SS
9607
9608 lowpc += baseaddr;
9609 highpc += baseaddr;
9610
34eaf542
TT
9611 /* If we have any template arguments, then we must allocate a
9612 different sort of symbol. */
9613 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9614 {
9615 if (child_die->tag == DW_TAG_template_type_param
9616 || child_die->tag == DW_TAG_template_value_param)
9617 {
9618 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9619 struct template_symbol);
9620 templ_func->base.is_cplus_template_function = 1;
9621 break;
9622 }
9623 }
9624
c906108c 9625 new = push_context (0, lowpc);
34eaf542
TT
9626 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9627 (struct symbol *) templ_func);
4c2df51b 9628
4cecd739
DJ
9629 /* If there is a location expression for DW_AT_frame_base, record
9630 it. */
e142c38c 9631 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9632 if (attr)
f1e6e072 9633 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9634
e142c38c 9635 cu->list_in_scope = &local_symbols;
c906108c 9636
639d11d3 9637 if (die->child != NULL)
c906108c 9638 {
639d11d3 9639 child_die = die->child;
c906108c
SS
9640 while (child_die && child_die->tag)
9641 {
34eaf542
TT
9642 if (child_die->tag == DW_TAG_template_type_param
9643 || child_die->tag == DW_TAG_template_value_param)
9644 {
9645 struct symbol *arg = new_symbol (child_die, NULL, cu);
9646
f1078f66
DJ
9647 if (arg != NULL)
9648 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9649 }
9650 else
9651 process_die (child_die, cu);
c906108c
SS
9652 child_die = sibling_die (child_die);
9653 }
9654 }
9655
d389af10
JK
9656 inherit_abstract_dies (die, cu);
9657
4a811a97
UW
9658 /* If we have a DW_AT_specification, we might need to import using
9659 directives from the context of the specification DIE. See the
9660 comment in determine_prefix. */
9661 if (cu->language == language_cplus
9662 && dwarf2_attr (die, DW_AT_specification, cu))
9663 {
9664 struct dwarf2_cu *spec_cu = cu;
9665 struct die_info *spec_die = die_specification (die, &spec_cu);
9666
9667 while (spec_die)
9668 {
9669 child_die = spec_die->child;
9670 while (child_die && child_die->tag)
9671 {
9672 if (child_die->tag == DW_TAG_imported_module)
9673 process_die (child_die, spec_cu);
9674 child_die = sibling_die (child_die);
9675 }
9676
9677 /* In some cases, GCC generates specification DIEs that
9678 themselves contain DW_AT_specification attributes. */
9679 spec_die = die_specification (spec_die, &spec_cu);
9680 }
9681 }
9682
c906108c
SS
9683 new = pop_context ();
9684 /* Make a block for the local symbols within. */
801e3a5b
JB
9685 block = finish_block (new->name, &local_symbols, new->old_blocks,
9686 lowpc, highpc, objfile);
9687
df8a16a1 9688 /* For C++, set the block's scope. */
195a3f6c 9689 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9690 && cu->processing_has_namespace_info)
195a3f6c
TT
9691 block_set_scope (block, determine_prefix (die, cu),
9692 &objfile->objfile_obstack);
df8a16a1 9693
801e3a5b
JB
9694 /* If we have address ranges, record them. */
9695 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9696
34eaf542
TT
9697 /* Attach template arguments to function. */
9698 if (! VEC_empty (symbolp, template_args))
9699 {
9700 gdb_assert (templ_func != NULL);
9701
9702 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9703 templ_func->template_arguments
9704 = obstack_alloc (&objfile->objfile_obstack,
9705 (templ_func->n_template_arguments
9706 * sizeof (struct symbol *)));
9707 memcpy (templ_func->template_arguments,
9708 VEC_address (symbolp, template_args),
9709 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9710 VEC_free (symbolp, template_args);
9711 }
9712
208d8187
JB
9713 /* In C++, we can have functions nested inside functions (e.g., when
9714 a function declares a class that has methods). This means that
9715 when we finish processing a function scope, we may need to go
9716 back to building a containing block's symbol lists. */
9717 local_symbols = new->locals;
27aa8d6a 9718 using_directives = new->using_directives;
208d8187 9719
921e78cf
JB
9720 /* If we've finished processing a top-level function, subsequent
9721 symbols go in the file symbol list. */
9722 if (outermost_context_p ())
e142c38c 9723 cu->list_in_scope = &file_symbols;
c906108c
SS
9724}
9725
9726/* Process all the DIES contained within a lexical block scope. Start
9727 a new scope, process the dies, and then close the scope. */
9728
9729static void
e7c27a73 9730read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9731{
e7c27a73 9732 struct objfile *objfile = cu->objfile;
52f0bd74 9733 struct context_stack *new;
c906108c
SS
9734 CORE_ADDR lowpc, highpc;
9735 struct die_info *child_die;
e142c38c
DJ
9736 CORE_ADDR baseaddr;
9737
9738 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9739
9740 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9741 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9742 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9743 be nasty. Might be easier to properly extend generic blocks to
af34e669 9744 describe ranges. */
d85a05f0 9745 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9746 return;
9747 lowpc += baseaddr;
9748 highpc += baseaddr;
9749
9750 push_context (0, lowpc);
639d11d3 9751 if (die->child != NULL)
c906108c 9752 {
639d11d3 9753 child_die = die->child;
c906108c
SS
9754 while (child_die && child_die->tag)
9755 {
e7c27a73 9756 process_die (child_die, cu);
c906108c
SS
9757 child_die = sibling_die (child_die);
9758 }
9759 }
9760 new = pop_context ();
9761
8540c487 9762 if (local_symbols != NULL || using_directives != NULL)
c906108c 9763 {
801e3a5b
JB
9764 struct block *block
9765 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9766 highpc, objfile);
9767
9768 /* Note that recording ranges after traversing children, as we
9769 do here, means that recording a parent's ranges entails
9770 walking across all its children's ranges as they appear in
9771 the address map, which is quadratic behavior.
9772
9773 It would be nicer to record the parent's ranges before
9774 traversing its children, simply overriding whatever you find
9775 there. But since we don't even decide whether to create a
9776 block until after we've traversed its children, that's hard
9777 to do. */
9778 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9779 }
9780 local_symbols = new->locals;
27aa8d6a 9781 using_directives = new->using_directives;
c906108c
SS
9782}
9783
96408a79
SA
9784/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9785
9786static void
9787read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9788{
9789 struct objfile *objfile = cu->objfile;
9790 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9791 CORE_ADDR pc, baseaddr;
9792 struct attribute *attr;
9793 struct call_site *call_site, call_site_local;
9794 void **slot;
9795 int nparams;
9796 struct die_info *child_die;
9797
9798 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9799
9800 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9801 if (!attr)
9802 {
9803 complaint (&symfile_complaints,
9804 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9805 "DIE 0x%x [in module %s]"),
b64f50a1 9806 die->offset.sect_off, objfile->name);
96408a79
SA
9807 return;
9808 }
9809 pc = DW_ADDR (attr) + baseaddr;
9810
9811 if (cu->call_site_htab == NULL)
9812 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9813 NULL, &objfile->objfile_obstack,
9814 hashtab_obstack_allocate, NULL);
9815 call_site_local.pc = pc;
9816 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9817 if (*slot != NULL)
9818 {
9819 complaint (&symfile_complaints,
9820 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9821 "DIE 0x%x [in module %s]"),
b64f50a1 9822 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9823 return;
9824 }
9825
9826 /* Count parameters at the caller. */
9827
9828 nparams = 0;
9829 for (child_die = die->child; child_die && child_die->tag;
9830 child_die = sibling_die (child_die))
9831 {
9832 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9833 {
9834 complaint (&symfile_complaints,
9835 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9836 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9837 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9838 continue;
9839 }
9840
9841 nparams++;
9842 }
9843
9844 call_site = obstack_alloc (&objfile->objfile_obstack,
9845 (sizeof (*call_site)
9846 + (sizeof (*call_site->parameter)
9847 * (nparams - 1))));
9848 *slot = call_site;
9849 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9850 call_site->pc = pc;
9851
9852 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9853 {
9854 struct die_info *func_die;
9855
9856 /* Skip also over DW_TAG_inlined_subroutine. */
9857 for (func_die = die->parent;
9858 func_die && func_die->tag != DW_TAG_subprogram
9859 && func_die->tag != DW_TAG_subroutine_type;
9860 func_die = func_die->parent);
9861
9862 /* DW_AT_GNU_all_call_sites is a superset
9863 of DW_AT_GNU_all_tail_call_sites. */
9864 if (func_die
9865 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9866 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9867 {
9868 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9869 not complete. But keep CALL_SITE for look ups via call_site_htab,
9870 both the initial caller containing the real return address PC and
9871 the final callee containing the current PC of a chain of tail
9872 calls do not need to have the tail call list complete. But any
9873 function candidate for a virtual tail call frame searched via
9874 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9875 determined unambiguously. */
9876 }
9877 else
9878 {
9879 struct type *func_type = NULL;
9880
9881 if (func_die)
9882 func_type = get_die_type (func_die, cu);
9883 if (func_type != NULL)
9884 {
9885 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9886
9887 /* Enlist this call site to the function. */
9888 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9889 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9890 }
9891 else
9892 complaint (&symfile_complaints,
9893 _("Cannot find function owning DW_TAG_GNU_call_site "
9894 "DIE 0x%x [in module %s]"),
b64f50a1 9895 die->offset.sect_off, objfile->name);
96408a79
SA
9896 }
9897 }
9898
9899 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9900 if (attr == NULL)
9901 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9902 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9903 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9904 /* Keep NULL DWARF_BLOCK. */;
9905 else if (attr_form_is_block (attr))
9906 {
9907 struct dwarf2_locexpr_baton *dlbaton;
9908
9909 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9910 dlbaton->data = DW_BLOCK (attr)->data;
9911 dlbaton->size = DW_BLOCK (attr)->size;
9912 dlbaton->per_cu = cu->per_cu;
9913
9914 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9915 }
9916 else if (is_ref_attr (attr))
9917 {
96408a79
SA
9918 struct dwarf2_cu *target_cu = cu;
9919 struct die_info *target_die;
9920
9921 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9922 gdb_assert (target_cu->objfile == objfile);
9923 if (die_is_declaration (target_die, target_cu))
9924 {
9112db09
JK
9925 const char *target_physname = NULL;
9926 struct attribute *target_attr;
9927
9928 /* Prefer the mangled name; otherwise compute the demangled one. */
9929 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9930 if (target_attr == NULL)
9931 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9932 target_cu);
9933 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9934 target_physname = DW_STRING (target_attr);
9935 else
9936 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9937 if (target_physname == NULL)
9938 complaint (&symfile_complaints,
9939 _("DW_AT_GNU_call_site_target target DIE has invalid "
9940 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9941 die->offset.sect_off, objfile->name);
96408a79 9942 else
7d455152 9943 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9944 }
9945 else
9946 {
9947 CORE_ADDR lowpc;
9948
9949 /* DW_AT_entry_pc should be preferred. */
9950 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9951 complaint (&symfile_complaints,
9952 _("DW_AT_GNU_call_site_target target DIE has invalid "
9953 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9954 die->offset.sect_off, objfile->name);
96408a79
SA
9955 else
9956 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9957 }
9958 }
9959 else
9960 complaint (&symfile_complaints,
9961 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9962 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9963 die->offset.sect_off, objfile->name);
96408a79
SA
9964
9965 call_site->per_cu = cu->per_cu;
9966
9967 for (child_die = die->child;
9968 child_die && child_die->tag;
9969 child_die = sibling_die (child_die))
9970 {
96408a79 9971 struct call_site_parameter *parameter;
1788b2d3 9972 struct attribute *loc, *origin;
96408a79
SA
9973
9974 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9975 {
9976 /* Already printed the complaint above. */
9977 continue;
9978 }
9979
9980 gdb_assert (call_site->parameter_count < nparams);
9981 parameter = &call_site->parameter[call_site->parameter_count];
9982
1788b2d3
JK
9983 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9984 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9985 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9986
24c5c679 9987 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9988 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9989 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9990 {
9991 sect_offset offset;
9992
9993 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9994 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9995 if (!offset_in_cu_p (&cu->header, offset))
9996 {
9997 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9998 binding can be done only inside one CU. Such referenced DIE
9999 therefore cannot be even moved to DW_TAG_partial_unit. */
10000 complaint (&symfile_complaints,
10001 _("DW_AT_abstract_origin offset is not in CU for "
10002 "DW_TAG_GNU_call_site child DIE 0x%x "
10003 "[in module %s]"),
10004 child_die->offset.sect_off, objfile->name);
10005 continue;
10006 }
1788b2d3
JK
10007 parameter->u.param_offset.cu_off = (offset.sect_off
10008 - cu->header.offset.sect_off);
10009 }
10010 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10011 {
10012 complaint (&symfile_complaints,
10013 _("No DW_FORM_block* DW_AT_location for "
10014 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10015 child_die->offset.sect_off, objfile->name);
96408a79
SA
10016 continue;
10017 }
24c5c679 10018 else
96408a79 10019 {
24c5c679
JK
10020 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10021 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10022 if (parameter->u.dwarf_reg != -1)
10023 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10024 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10025 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10026 &parameter->u.fb_offset))
10027 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10028 else
10029 {
10030 complaint (&symfile_complaints,
10031 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10032 "for DW_FORM_block* DW_AT_location is supported for "
10033 "DW_TAG_GNU_call_site child DIE 0x%x "
10034 "[in module %s]"),
10035 child_die->offset.sect_off, objfile->name);
10036 continue;
10037 }
96408a79
SA
10038 }
10039
10040 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10041 if (!attr_form_is_block (attr))
10042 {
10043 complaint (&symfile_complaints,
10044 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10045 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10046 child_die->offset.sect_off, objfile->name);
96408a79
SA
10047 continue;
10048 }
10049 parameter->value = DW_BLOCK (attr)->data;
10050 parameter->value_size = DW_BLOCK (attr)->size;
10051
10052 /* Parameters are not pre-cleared by memset above. */
10053 parameter->data_value = NULL;
10054 parameter->data_value_size = 0;
10055 call_site->parameter_count++;
10056
10057 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10058 if (attr)
10059 {
10060 if (!attr_form_is_block (attr))
10061 complaint (&symfile_complaints,
10062 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10063 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10064 child_die->offset.sect_off, objfile->name);
96408a79
SA
10065 else
10066 {
10067 parameter->data_value = DW_BLOCK (attr)->data;
10068 parameter->data_value_size = DW_BLOCK (attr)->size;
10069 }
10070 }
10071 }
10072}
10073
43039443 10074/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10075 Return 1 if the attributes are present and valid, otherwise, return 0.
10076 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10077
10078static int
10079dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10080 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10081 struct partial_symtab *ranges_pst)
43039443
JK
10082{
10083 struct objfile *objfile = cu->objfile;
10084 struct comp_unit_head *cu_header = &cu->header;
10085 bfd *obfd = objfile->obfd;
10086 unsigned int addr_size = cu_header->addr_size;
10087 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10088 /* Base address selection entry. */
10089 CORE_ADDR base;
10090 int found_base;
10091 unsigned int dummy;
10092 gdb_byte *buffer;
10093 CORE_ADDR marker;
10094 int low_set;
10095 CORE_ADDR low = 0;
10096 CORE_ADDR high = 0;
ff013f42 10097 CORE_ADDR baseaddr;
43039443 10098
d00adf39
DE
10099 found_base = cu->base_known;
10100 base = cu->base_address;
43039443 10101
be391dca 10102 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10103 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10104 {
10105 complaint (&symfile_complaints,
10106 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10107 offset);
10108 return 0;
10109 }
dce234bc 10110 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10111
10112 /* Read in the largest possible address. */
10113 marker = read_address (obfd, buffer, cu, &dummy);
10114 if ((marker & mask) == mask)
10115 {
10116 /* If we found the largest possible address, then
10117 read the base address. */
10118 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10119 buffer += 2 * addr_size;
10120 offset += 2 * addr_size;
10121 found_base = 1;
10122 }
10123
10124 low_set = 0;
10125
e7030f15 10126 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10127
43039443
JK
10128 while (1)
10129 {
10130 CORE_ADDR range_beginning, range_end;
10131
10132 range_beginning = read_address (obfd, buffer, cu, &dummy);
10133 buffer += addr_size;
10134 range_end = read_address (obfd, buffer, cu, &dummy);
10135 buffer += addr_size;
10136 offset += 2 * addr_size;
10137
10138 /* An end of list marker is a pair of zero addresses. */
10139 if (range_beginning == 0 && range_end == 0)
10140 /* Found the end of list entry. */
10141 break;
10142
10143 /* Each base address selection entry is a pair of 2 values.
10144 The first is the largest possible address, the second is
10145 the base address. Check for a base address here. */
10146 if ((range_beginning & mask) == mask)
10147 {
10148 /* If we found the largest possible address, then
10149 read the base address. */
10150 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10151 found_base = 1;
10152 continue;
10153 }
10154
10155 if (!found_base)
10156 {
10157 /* We have no valid base address for the ranges
10158 data. */
10159 complaint (&symfile_complaints,
10160 _("Invalid .debug_ranges data (no base address)"));
10161 return 0;
10162 }
10163
9277c30c
UW
10164 if (range_beginning > range_end)
10165 {
10166 /* Inverted range entries are invalid. */
10167 complaint (&symfile_complaints,
10168 _("Invalid .debug_ranges data (inverted range)"));
10169 return 0;
10170 }
10171
10172 /* Empty range entries have no effect. */
10173 if (range_beginning == range_end)
10174 continue;
10175
43039443
JK
10176 range_beginning += base;
10177 range_end += base;
10178
01093045
DE
10179 /* A not-uncommon case of bad debug info.
10180 Don't pollute the addrmap with bad data. */
10181 if (range_beginning + baseaddr == 0
10182 && !dwarf2_per_objfile->has_section_at_zero)
10183 {
10184 complaint (&symfile_complaints,
10185 _(".debug_ranges entry has start address of zero"
10186 " [in module %s]"), objfile->name);
10187 continue;
10188 }
10189
9277c30c 10190 if (ranges_pst != NULL)
ff013f42 10191 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10192 range_beginning + baseaddr,
10193 range_end - 1 + baseaddr,
ff013f42
JK
10194 ranges_pst);
10195
43039443
JK
10196 /* FIXME: This is recording everything as a low-high
10197 segment of consecutive addresses. We should have a
10198 data structure for discontiguous block ranges
10199 instead. */
10200 if (! low_set)
10201 {
10202 low = range_beginning;
10203 high = range_end;
10204 low_set = 1;
10205 }
10206 else
10207 {
10208 if (range_beginning < low)
10209 low = range_beginning;
10210 if (range_end > high)
10211 high = range_end;
10212 }
10213 }
10214
10215 if (! low_set)
10216 /* If the first entry is an end-of-list marker, the range
10217 describes an empty scope, i.e. no instructions. */
10218 return 0;
10219
10220 if (low_return)
10221 *low_return = low;
10222 if (high_return)
10223 *high_return = high;
10224 return 1;
10225}
10226
af34e669
DJ
10227/* Get low and high pc attributes from a die. Return 1 if the attributes
10228 are present and valid, otherwise, return 0. Return -1 if the range is
10229 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10230
c906108c 10231static int
af34e669 10232dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10233 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10234 struct partial_symtab *pst)
c906108c
SS
10235{
10236 struct attribute *attr;
91da1414 10237 struct attribute *attr_high;
af34e669
DJ
10238 CORE_ADDR low = 0;
10239 CORE_ADDR high = 0;
10240 int ret = 0;
c906108c 10241
91da1414
MW
10242 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10243 if (attr_high)
af34e669 10244 {
e142c38c 10245 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10246 if (attr)
91da1414
MW
10247 {
10248 low = DW_ADDR (attr);
3019eac3
DE
10249 if (attr_high->form == DW_FORM_addr
10250 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10251 high = DW_ADDR (attr_high);
10252 else
10253 high = low + DW_UNSND (attr_high);
10254 }
af34e669
DJ
10255 else
10256 /* Found high w/o low attribute. */
10257 return 0;
10258
10259 /* Found consecutive range of addresses. */
10260 ret = 1;
10261 }
c906108c 10262 else
af34e669 10263 {
e142c38c 10264 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10265 if (attr != NULL)
10266 {
ab435259
DE
10267 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10268 We take advantage of the fact that DW_AT_ranges does not appear
10269 in DW_TAG_compile_unit of DWO files. */
10270 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10271 unsigned int ranges_offset = (DW_UNSND (attr)
10272 + (need_ranges_base
10273 ? cu->ranges_base
10274 : 0));
2e3cf129 10275
af34e669 10276 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10277 .debug_ranges section. */
2e3cf129 10278 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10279 return 0;
43039443 10280 /* Found discontinuous range of addresses. */
af34e669
DJ
10281 ret = -1;
10282 }
10283 }
c906108c 10284
9373cf26
JK
10285 /* read_partial_die has also the strict LOW < HIGH requirement. */
10286 if (high <= low)
c906108c
SS
10287 return 0;
10288
10289 /* When using the GNU linker, .gnu.linkonce. sections are used to
10290 eliminate duplicate copies of functions and vtables and such.
10291 The linker will arbitrarily choose one and discard the others.
10292 The AT_*_pc values for such functions refer to local labels in
10293 these sections. If the section from that file was discarded, the
10294 labels are not in the output, so the relocs get a value of 0.
10295 If this is a discarded function, mark the pc bounds as invalid,
10296 so that GDB will ignore it. */
72dca2f5 10297 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10298 return 0;
10299
10300 *lowpc = low;
96408a79
SA
10301 if (highpc)
10302 *highpc = high;
af34e669 10303 return ret;
c906108c
SS
10304}
10305
b084d499
JB
10306/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10307 its low and high PC addresses. Do nothing if these addresses could not
10308 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10309 and HIGHPC to the high address if greater than HIGHPC. */
10310
10311static void
10312dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10313 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10314 struct dwarf2_cu *cu)
10315{
10316 CORE_ADDR low, high;
10317 struct die_info *child = die->child;
10318
d85a05f0 10319 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10320 {
10321 *lowpc = min (*lowpc, low);
10322 *highpc = max (*highpc, high);
10323 }
10324
10325 /* If the language does not allow nested subprograms (either inside
10326 subprograms or lexical blocks), we're done. */
10327 if (cu->language != language_ada)
10328 return;
6e70227d 10329
b084d499
JB
10330 /* Check all the children of the given DIE. If it contains nested
10331 subprograms, then check their pc bounds. Likewise, we need to
10332 check lexical blocks as well, as they may also contain subprogram
10333 definitions. */
10334 while (child && child->tag)
10335 {
10336 if (child->tag == DW_TAG_subprogram
10337 || child->tag == DW_TAG_lexical_block)
10338 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10339 child = sibling_die (child);
10340 }
10341}
10342
fae299cd
DC
10343/* Get the low and high pc's represented by the scope DIE, and store
10344 them in *LOWPC and *HIGHPC. If the correct values can't be
10345 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10346
10347static void
10348get_scope_pc_bounds (struct die_info *die,
10349 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10350 struct dwarf2_cu *cu)
10351{
10352 CORE_ADDR best_low = (CORE_ADDR) -1;
10353 CORE_ADDR best_high = (CORE_ADDR) 0;
10354 CORE_ADDR current_low, current_high;
10355
d85a05f0 10356 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10357 {
10358 best_low = current_low;
10359 best_high = current_high;
10360 }
10361 else
10362 {
10363 struct die_info *child = die->child;
10364
10365 while (child && child->tag)
10366 {
10367 switch (child->tag) {
10368 case DW_TAG_subprogram:
b084d499 10369 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10370 break;
10371 case DW_TAG_namespace:
f55ee35c 10372 case DW_TAG_module:
fae299cd
DC
10373 /* FIXME: carlton/2004-01-16: Should we do this for
10374 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10375 that current GCC's always emit the DIEs corresponding
10376 to definitions of methods of classes as children of a
10377 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10378 the DIEs giving the declarations, which could be
10379 anywhere). But I don't see any reason why the
10380 standards says that they have to be there. */
10381 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10382
10383 if (current_low != ((CORE_ADDR) -1))
10384 {
10385 best_low = min (best_low, current_low);
10386 best_high = max (best_high, current_high);
10387 }
10388 break;
10389 default:
0963b4bd 10390 /* Ignore. */
fae299cd
DC
10391 break;
10392 }
10393
10394 child = sibling_die (child);
10395 }
10396 }
10397
10398 *lowpc = best_low;
10399 *highpc = best_high;
10400}
10401
801e3a5b
JB
10402/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10403 in DIE. */
380bca97 10404
801e3a5b
JB
10405static void
10406dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10407 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10408{
bb5ed363 10409 struct objfile *objfile = cu->objfile;
801e3a5b 10410 struct attribute *attr;
91da1414 10411 struct attribute *attr_high;
801e3a5b 10412
91da1414
MW
10413 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10414 if (attr_high)
801e3a5b 10415 {
801e3a5b
JB
10416 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10417 if (attr)
10418 {
10419 CORE_ADDR low = DW_ADDR (attr);
91da1414 10420 CORE_ADDR high;
3019eac3
DE
10421 if (attr_high->form == DW_FORM_addr
10422 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10423 high = DW_ADDR (attr_high);
10424 else
10425 high = low + DW_UNSND (attr_high);
9a619af0 10426
801e3a5b
JB
10427 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10428 }
10429 }
10430
10431 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10432 if (attr)
10433 {
bb5ed363 10434 bfd *obfd = objfile->obfd;
ab435259
DE
10435 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10436 We take advantage of the fact that DW_AT_ranges does not appear
10437 in DW_TAG_compile_unit of DWO files. */
10438 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10439
10440 /* The value of the DW_AT_ranges attribute is the offset of the
10441 address range list in the .debug_ranges section. */
ab435259
DE
10442 unsigned long offset = (DW_UNSND (attr)
10443 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10444 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10445
10446 /* For some target architectures, but not others, the
10447 read_address function sign-extends the addresses it returns.
10448 To recognize base address selection entries, we need a
10449 mask. */
10450 unsigned int addr_size = cu->header.addr_size;
10451 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10452
10453 /* The base address, to which the next pair is relative. Note
10454 that this 'base' is a DWARF concept: most entries in a range
10455 list are relative, to reduce the number of relocs against the
10456 debugging information. This is separate from this function's
10457 'baseaddr' argument, which GDB uses to relocate debugging
10458 information from a shared library based on the address at
10459 which the library was loaded. */
d00adf39
DE
10460 CORE_ADDR base = cu->base_address;
10461 int base_known = cu->base_known;
801e3a5b 10462
be391dca 10463 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10464 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10465 {
10466 complaint (&symfile_complaints,
10467 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10468 offset);
10469 return;
10470 }
10471
10472 for (;;)
10473 {
10474 unsigned int bytes_read;
10475 CORE_ADDR start, end;
10476
10477 start = read_address (obfd, buffer, cu, &bytes_read);
10478 buffer += bytes_read;
10479 end = read_address (obfd, buffer, cu, &bytes_read);
10480 buffer += bytes_read;
10481
10482 /* Did we find the end of the range list? */
10483 if (start == 0 && end == 0)
10484 break;
10485
10486 /* Did we find a base address selection entry? */
10487 else if ((start & base_select_mask) == base_select_mask)
10488 {
10489 base = end;
10490 base_known = 1;
10491 }
10492
10493 /* We found an ordinary address range. */
10494 else
10495 {
10496 if (!base_known)
10497 {
10498 complaint (&symfile_complaints,
3e43a32a
MS
10499 _("Invalid .debug_ranges data "
10500 "(no base address)"));
801e3a5b
JB
10501 return;
10502 }
10503
9277c30c
UW
10504 if (start > end)
10505 {
10506 /* Inverted range entries are invalid. */
10507 complaint (&symfile_complaints,
10508 _("Invalid .debug_ranges data "
10509 "(inverted range)"));
10510 return;
10511 }
10512
10513 /* Empty range entries have no effect. */
10514 if (start == end)
10515 continue;
10516
01093045
DE
10517 start += base + baseaddr;
10518 end += base + baseaddr;
10519
10520 /* A not-uncommon case of bad debug info.
10521 Don't pollute the addrmap with bad data. */
10522 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10523 {
10524 complaint (&symfile_complaints,
10525 _(".debug_ranges entry has start address of zero"
10526 " [in module %s]"), objfile->name);
10527 continue;
10528 }
10529
10530 record_block_range (block, start, end - 1);
801e3a5b
JB
10531 }
10532 }
10533 }
10534}
10535
685b1105
JK
10536/* Check whether the producer field indicates either of GCC < 4.6, or the
10537 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10538
685b1105
JK
10539static void
10540check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10541{
10542 const char *cs;
10543 int major, minor, release;
10544
10545 if (cu->producer == NULL)
10546 {
10547 /* For unknown compilers expect their behavior is DWARF version
10548 compliant.
10549
10550 GCC started to support .debug_types sections by -gdwarf-4 since
10551 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10552 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10553 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10554 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10555 }
685b1105 10556 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10557 {
685b1105
JK
10558 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10559
ba919b58
TT
10560 cs = &cu->producer[strlen ("GNU ")];
10561 while (*cs && !isdigit (*cs))
10562 cs++;
10563 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10564 {
10565 /* Not recognized as GCC. */
10566 }
10567 else
1b80a9fa
JK
10568 {
10569 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10570 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10571 }
685b1105
JK
10572 }
10573 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10574 cu->producer_is_icc = 1;
10575 else
10576 {
10577 /* For other non-GCC compilers, expect their behavior is DWARF version
10578 compliant. */
60d5a603
JK
10579 }
10580
ba919b58 10581 cu->checked_producer = 1;
685b1105 10582}
ba919b58 10583
685b1105
JK
10584/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10585 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10586 during 4.6.0 experimental. */
10587
10588static int
10589producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10590{
10591 if (!cu->checked_producer)
10592 check_producer (cu);
10593
10594 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10595}
10596
10597/* Return the default accessibility type if it is not overriden by
10598 DW_AT_accessibility. */
10599
10600static enum dwarf_access_attribute
10601dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10602{
10603 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10604 {
10605 /* The default DWARF 2 accessibility for members is public, the default
10606 accessibility for inheritance is private. */
10607
10608 if (die->tag != DW_TAG_inheritance)
10609 return DW_ACCESS_public;
10610 else
10611 return DW_ACCESS_private;
10612 }
10613 else
10614 {
10615 /* DWARF 3+ defines the default accessibility a different way. The same
10616 rules apply now for DW_TAG_inheritance as for the members and it only
10617 depends on the container kind. */
10618
10619 if (die->parent->tag == DW_TAG_class_type)
10620 return DW_ACCESS_private;
10621 else
10622 return DW_ACCESS_public;
10623 }
10624}
10625
74ac6d43
TT
10626/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10627 offset. If the attribute was not found return 0, otherwise return
10628 1. If it was found but could not properly be handled, set *OFFSET
10629 to 0. */
10630
10631static int
10632handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10633 LONGEST *offset)
10634{
10635 struct attribute *attr;
10636
10637 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10638 if (attr != NULL)
10639 {
10640 *offset = 0;
10641
10642 /* Note that we do not check for a section offset first here.
10643 This is because DW_AT_data_member_location is new in DWARF 4,
10644 so if we see it, we can assume that a constant form is really
10645 a constant and not a section offset. */
10646 if (attr_form_is_constant (attr))
10647 *offset = dwarf2_get_attr_constant_value (attr, 0);
10648 else if (attr_form_is_section_offset (attr))
10649 dwarf2_complex_location_expr_complaint ();
10650 else if (attr_form_is_block (attr))
10651 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10652 else
10653 dwarf2_complex_location_expr_complaint ();
10654
10655 return 1;
10656 }
10657
10658 return 0;
10659}
10660
c906108c
SS
10661/* Add an aggregate field to the field list. */
10662
10663static void
107d2387 10664dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10665 struct dwarf2_cu *cu)
6e70227d 10666{
e7c27a73 10667 struct objfile *objfile = cu->objfile;
5e2b427d 10668 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10669 struct nextfield *new_field;
10670 struct attribute *attr;
10671 struct field *fp;
15d034d0 10672 const char *fieldname = "";
c906108c
SS
10673
10674 /* Allocate a new field list entry and link it in. */
10675 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10676 make_cleanup (xfree, new_field);
c906108c 10677 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10678
10679 if (die->tag == DW_TAG_inheritance)
10680 {
10681 new_field->next = fip->baseclasses;
10682 fip->baseclasses = new_field;
10683 }
10684 else
10685 {
10686 new_field->next = fip->fields;
10687 fip->fields = new_field;
10688 }
c906108c
SS
10689 fip->nfields++;
10690
e142c38c 10691 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10692 if (attr)
10693 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10694 else
10695 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10696 if (new_field->accessibility != DW_ACCESS_public)
10697 fip->non_public_fields = 1;
60d5a603 10698
e142c38c 10699 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10700 if (attr)
10701 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10702 else
10703 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10704
10705 fp = &new_field->field;
a9a9bd0f 10706
e142c38c 10707 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10708 {
74ac6d43
TT
10709 LONGEST offset;
10710
a9a9bd0f 10711 /* Data member other than a C++ static data member. */
6e70227d 10712
c906108c 10713 /* Get type of field. */
e7c27a73 10714 fp->type = die_type (die, cu);
c906108c 10715
d6a843b5 10716 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10717
c906108c 10718 /* Get bit size of field (zero if none). */
e142c38c 10719 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10720 if (attr)
10721 {
10722 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10723 }
10724 else
10725 {
10726 FIELD_BITSIZE (*fp) = 0;
10727 }
10728
10729 /* Get bit offset of field. */
74ac6d43
TT
10730 if (handle_data_member_location (die, cu, &offset))
10731 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10732 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10733 if (attr)
10734 {
5e2b427d 10735 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10736 {
10737 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10738 additional bit offset from the MSB of the containing
10739 anonymous object to the MSB of the field. We don't
10740 have to do anything special since we don't need to
10741 know the size of the anonymous object. */
f41f5e61 10742 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10743 }
10744 else
10745 {
10746 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10747 MSB of the anonymous object, subtract off the number of
10748 bits from the MSB of the field to the MSB of the
10749 object, and then subtract off the number of bits of
10750 the field itself. The result is the bit offset of
10751 the LSB of the field. */
c906108c
SS
10752 int anonymous_size;
10753 int bit_offset = DW_UNSND (attr);
10754
e142c38c 10755 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10756 if (attr)
10757 {
10758 /* The size of the anonymous object containing
10759 the bit field is explicit, so use the
10760 indicated size (in bytes). */
10761 anonymous_size = DW_UNSND (attr);
10762 }
10763 else
10764 {
10765 /* The size of the anonymous object containing
10766 the bit field must be inferred from the type
10767 attribute of the data member containing the
10768 bit field. */
10769 anonymous_size = TYPE_LENGTH (fp->type);
10770 }
f41f5e61
PA
10771 SET_FIELD_BITPOS (*fp,
10772 (FIELD_BITPOS (*fp)
10773 + anonymous_size * bits_per_byte
10774 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10775 }
10776 }
10777
10778 /* Get name of field. */
39cbfefa
DJ
10779 fieldname = dwarf2_name (die, cu);
10780 if (fieldname == NULL)
10781 fieldname = "";
d8151005
DJ
10782
10783 /* The name is already allocated along with this objfile, so we don't
10784 need to duplicate it for the type. */
10785 fp->name = fieldname;
c906108c
SS
10786
10787 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10788 pointer or virtual base class pointer) to private. */
e142c38c 10789 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10790 {
d48cc9dd 10791 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10792 new_field->accessibility = DW_ACCESS_private;
10793 fip->non_public_fields = 1;
10794 }
10795 }
a9a9bd0f 10796 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10797 {
a9a9bd0f
DC
10798 /* C++ static member. */
10799
10800 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10801 is a declaration, but all versions of G++ as of this writing
10802 (so through at least 3.2.1) incorrectly generate
10803 DW_TAG_variable tags. */
6e70227d 10804
ff355380 10805 const char *physname;
c906108c 10806
a9a9bd0f 10807 /* Get name of field. */
39cbfefa
DJ
10808 fieldname = dwarf2_name (die, cu);
10809 if (fieldname == NULL)
c906108c
SS
10810 return;
10811
254e6b9e 10812 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10813 if (attr
10814 /* Only create a symbol if this is an external value.
10815 new_symbol checks this and puts the value in the global symbol
10816 table, which we want. If it is not external, new_symbol
10817 will try to put the value in cu->list_in_scope which is wrong. */
10818 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10819 {
10820 /* A static const member, not much different than an enum as far as
10821 we're concerned, except that we can support more types. */
10822 new_symbol (die, NULL, cu);
10823 }
10824
2df3850c 10825 /* Get physical name. */
ff355380 10826 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10827
d8151005
DJ
10828 /* The name is already allocated along with this objfile, so we don't
10829 need to duplicate it for the type. */
10830 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10831 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10832 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10833 }
10834 else if (die->tag == DW_TAG_inheritance)
10835 {
74ac6d43 10836 LONGEST offset;
d4b96c9a 10837
74ac6d43
TT
10838 /* C++ base class field. */
10839 if (handle_data_member_location (die, cu, &offset))
10840 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10841 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10842 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10843 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10844 fip->nbaseclasses++;
10845 }
10846}
10847
98751a41
JK
10848/* Add a typedef defined in the scope of the FIP's class. */
10849
10850static void
10851dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10852 struct dwarf2_cu *cu)
6e70227d 10853{
98751a41 10854 struct objfile *objfile = cu->objfile;
98751a41
JK
10855 struct typedef_field_list *new_field;
10856 struct attribute *attr;
10857 struct typedef_field *fp;
10858 char *fieldname = "";
10859
10860 /* Allocate a new field list entry and link it in. */
10861 new_field = xzalloc (sizeof (*new_field));
10862 make_cleanup (xfree, new_field);
10863
10864 gdb_assert (die->tag == DW_TAG_typedef);
10865
10866 fp = &new_field->field;
10867
10868 /* Get name of field. */
10869 fp->name = dwarf2_name (die, cu);
10870 if (fp->name == NULL)
10871 return;
10872
10873 fp->type = read_type_die (die, cu);
10874
10875 new_field->next = fip->typedef_field_list;
10876 fip->typedef_field_list = new_field;
10877 fip->typedef_field_list_count++;
10878}
10879
c906108c
SS
10880/* Create the vector of fields, and attach it to the type. */
10881
10882static void
fba45db2 10883dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10884 struct dwarf2_cu *cu)
c906108c
SS
10885{
10886 int nfields = fip->nfields;
10887
10888 /* Record the field count, allocate space for the array of fields,
10889 and create blank accessibility bitfields if necessary. */
10890 TYPE_NFIELDS (type) = nfields;
10891 TYPE_FIELDS (type) = (struct field *)
10892 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10893 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10894
b4ba55a1 10895 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10896 {
10897 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10898
10899 TYPE_FIELD_PRIVATE_BITS (type) =
10900 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10901 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10902
10903 TYPE_FIELD_PROTECTED_BITS (type) =
10904 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10905 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10906
774b6a14
TT
10907 TYPE_FIELD_IGNORE_BITS (type) =
10908 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10909 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10910 }
10911
10912 /* If the type has baseclasses, allocate and clear a bit vector for
10913 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10914 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10915 {
10916 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10917 unsigned char *pointer;
c906108c
SS
10918
10919 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10920 pointer = TYPE_ALLOC (type, num_bytes);
10921 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10922 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10923 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10924 }
10925
3e43a32a
MS
10926 /* Copy the saved-up fields into the field vector. Start from the head of
10927 the list, adding to the tail of the field array, so that they end up in
10928 the same order in the array in which they were added to the list. */
c906108c
SS
10929 while (nfields-- > 0)
10930 {
7d0ccb61
DJ
10931 struct nextfield *fieldp;
10932
10933 if (fip->fields)
10934 {
10935 fieldp = fip->fields;
10936 fip->fields = fieldp->next;
10937 }
10938 else
10939 {
10940 fieldp = fip->baseclasses;
10941 fip->baseclasses = fieldp->next;
10942 }
10943
10944 TYPE_FIELD (type, nfields) = fieldp->field;
10945 switch (fieldp->accessibility)
c906108c 10946 {
c5aa993b 10947 case DW_ACCESS_private:
b4ba55a1
JB
10948 if (cu->language != language_ada)
10949 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10950 break;
c906108c 10951
c5aa993b 10952 case DW_ACCESS_protected:
b4ba55a1
JB
10953 if (cu->language != language_ada)
10954 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10955 break;
c906108c 10956
c5aa993b
JM
10957 case DW_ACCESS_public:
10958 break;
c906108c 10959
c5aa993b
JM
10960 default:
10961 /* Unknown accessibility. Complain and treat it as public. */
10962 {
e2e0b3e5 10963 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10964 fieldp->accessibility);
c5aa993b
JM
10965 }
10966 break;
c906108c
SS
10967 }
10968 if (nfields < fip->nbaseclasses)
10969 {
7d0ccb61 10970 switch (fieldp->virtuality)
c906108c 10971 {
c5aa993b
JM
10972 case DW_VIRTUALITY_virtual:
10973 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10974 if (cu->language == language_ada)
a73c6dcd 10975 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10976 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10977 break;
c906108c
SS
10978 }
10979 }
c906108c
SS
10980 }
10981}
10982
7d27a96d
TT
10983/* Return true if this member function is a constructor, false
10984 otherwise. */
10985
10986static int
10987dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10988{
10989 const char *fieldname;
10990 const char *typename;
10991 int len;
10992
10993 if (die->parent == NULL)
10994 return 0;
10995
10996 if (die->parent->tag != DW_TAG_structure_type
10997 && die->parent->tag != DW_TAG_union_type
10998 && die->parent->tag != DW_TAG_class_type)
10999 return 0;
11000
11001 fieldname = dwarf2_name (die, cu);
11002 typename = dwarf2_name (die->parent, cu);
11003 if (fieldname == NULL || typename == NULL)
11004 return 0;
11005
11006 len = strlen (fieldname);
11007 return (strncmp (fieldname, typename, len) == 0
11008 && (typename[len] == '\0' || typename[len] == '<'));
11009}
11010
c906108c
SS
11011/* Add a member function to the proper fieldlist. */
11012
11013static void
107d2387 11014dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11015 struct type *type, struct dwarf2_cu *cu)
c906108c 11016{
e7c27a73 11017 struct objfile *objfile = cu->objfile;
c906108c
SS
11018 struct attribute *attr;
11019 struct fnfieldlist *flp;
11020 int i;
11021 struct fn_field *fnp;
15d034d0 11022 const char *fieldname;
c906108c 11023 struct nextfnfield *new_fnfield;
f792889a 11024 struct type *this_type;
60d5a603 11025 enum dwarf_access_attribute accessibility;
c906108c 11026
b4ba55a1 11027 if (cu->language == language_ada)
a73c6dcd 11028 error (_("unexpected member function in Ada type"));
b4ba55a1 11029
2df3850c 11030 /* Get name of member function. */
39cbfefa
DJ
11031 fieldname = dwarf2_name (die, cu);
11032 if (fieldname == NULL)
2df3850c 11033 return;
c906108c 11034
c906108c
SS
11035 /* Look up member function name in fieldlist. */
11036 for (i = 0; i < fip->nfnfields; i++)
11037 {
27bfe10e 11038 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11039 break;
11040 }
11041
11042 /* Create new list element if necessary. */
11043 if (i < fip->nfnfields)
11044 flp = &fip->fnfieldlists[i];
11045 else
11046 {
11047 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11048 {
11049 fip->fnfieldlists = (struct fnfieldlist *)
11050 xrealloc (fip->fnfieldlists,
11051 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11052 * sizeof (struct fnfieldlist));
c906108c 11053 if (fip->nfnfields == 0)
c13c43fd 11054 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11055 }
11056 flp = &fip->fnfieldlists[fip->nfnfields];
11057 flp->name = fieldname;
11058 flp->length = 0;
11059 flp->head = NULL;
3da10d80 11060 i = fip->nfnfields++;
c906108c
SS
11061 }
11062
11063 /* Create a new member function field and chain it to the field list
0963b4bd 11064 entry. */
c906108c 11065 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11066 make_cleanup (xfree, new_fnfield);
c906108c
SS
11067 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11068 new_fnfield->next = flp->head;
11069 flp->head = new_fnfield;
11070 flp->length++;
11071
11072 /* Fill in the member function field info. */
11073 fnp = &new_fnfield->fnfield;
3da10d80
KS
11074
11075 /* Delay processing of the physname until later. */
11076 if (cu->language == language_cplus || cu->language == language_java)
11077 {
11078 add_to_method_list (type, i, flp->length - 1, fieldname,
11079 die, cu);
11080 }
11081 else
11082 {
1d06ead6 11083 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11084 fnp->physname = physname ? physname : "";
11085 }
11086
c906108c 11087 fnp->type = alloc_type (objfile);
f792889a
DJ
11088 this_type = read_type_die (die, cu);
11089 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11090 {
f792889a 11091 int nparams = TYPE_NFIELDS (this_type);
c906108c 11092
f792889a 11093 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11094 of the method itself (TYPE_CODE_METHOD). */
11095 smash_to_method_type (fnp->type, type,
f792889a
DJ
11096 TYPE_TARGET_TYPE (this_type),
11097 TYPE_FIELDS (this_type),
11098 TYPE_NFIELDS (this_type),
11099 TYPE_VARARGS (this_type));
c906108c
SS
11100
11101 /* Handle static member functions.
c5aa993b 11102 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11103 member functions. G++ helps GDB by marking the first
11104 parameter for non-static member functions (which is the this
11105 pointer) as artificial. We obtain this information from
11106 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11107 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11108 fnp->voffset = VOFFSET_STATIC;
11109 }
11110 else
e2e0b3e5 11111 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11112 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11113
11114 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11115 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11116 fnp->fcontext = die_containing_type (die, cu);
c906108c 11117
3e43a32a
MS
11118 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11119 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11120
11121 /* Get accessibility. */
e142c38c 11122 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11123 if (attr)
60d5a603
JK
11124 accessibility = DW_UNSND (attr);
11125 else
11126 accessibility = dwarf2_default_access_attribute (die, cu);
11127 switch (accessibility)
c906108c 11128 {
60d5a603
JK
11129 case DW_ACCESS_private:
11130 fnp->is_private = 1;
11131 break;
11132 case DW_ACCESS_protected:
11133 fnp->is_protected = 1;
11134 break;
c906108c
SS
11135 }
11136
b02dede2 11137 /* Check for artificial methods. */
e142c38c 11138 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11139 if (attr && DW_UNSND (attr) != 0)
11140 fnp->is_artificial = 1;
11141
7d27a96d
TT
11142 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11143
0d564a31 11144 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11145 function. For older versions of GCC, this is an offset in the
11146 appropriate virtual table, as specified by DW_AT_containing_type.
11147 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11148 to the object address. */
11149
e142c38c 11150 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11151 if (attr)
8e19ed76 11152 {
aec5aa8b 11153 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11154 {
aec5aa8b
TT
11155 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11156 {
11157 /* Old-style GCC. */
11158 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11159 }
11160 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11161 || (DW_BLOCK (attr)->size > 1
11162 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11163 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11164 {
11165 struct dwarf_block blk;
11166 int offset;
11167
11168 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11169 ? 1 : 2);
11170 blk.size = DW_BLOCK (attr)->size - offset;
11171 blk.data = DW_BLOCK (attr)->data + offset;
11172 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11173 if ((fnp->voffset % cu->header.addr_size) != 0)
11174 dwarf2_complex_location_expr_complaint ();
11175 else
11176 fnp->voffset /= cu->header.addr_size;
11177 fnp->voffset += 2;
11178 }
11179 else
11180 dwarf2_complex_location_expr_complaint ();
11181
11182 if (!fnp->fcontext)
11183 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11184 }
3690dd37 11185 else if (attr_form_is_section_offset (attr))
8e19ed76 11186 {
4d3c2250 11187 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11188 }
11189 else
11190 {
4d3c2250
KB
11191 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11192 fieldname);
8e19ed76 11193 }
0d564a31 11194 }
d48cc9dd
DJ
11195 else
11196 {
11197 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11198 if (attr && DW_UNSND (attr))
11199 {
11200 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11201 complaint (&symfile_complaints,
3e43a32a
MS
11202 _("Member function \"%s\" (offset %d) is virtual "
11203 "but the vtable offset is not specified"),
b64f50a1 11204 fieldname, die->offset.sect_off);
9655fd1a 11205 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11206 TYPE_CPLUS_DYNAMIC (type) = 1;
11207 }
11208 }
c906108c
SS
11209}
11210
11211/* Create the vector of member function fields, and attach it to the type. */
11212
11213static void
fba45db2 11214dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11215 struct dwarf2_cu *cu)
c906108c
SS
11216{
11217 struct fnfieldlist *flp;
c906108c
SS
11218 int i;
11219
b4ba55a1 11220 if (cu->language == language_ada)
a73c6dcd 11221 error (_("unexpected member functions in Ada type"));
b4ba55a1 11222
c906108c
SS
11223 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11224 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11225 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11226
11227 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11228 {
11229 struct nextfnfield *nfp = flp->head;
11230 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11231 int k;
11232
11233 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11234 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11235 fn_flp->fn_fields = (struct fn_field *)
11236 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11237 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11238 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11239 }
11240
11241 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11242}
11243
1168df01
JB
11244/* Returns non-zero if NAME is the name of a vtable member in CU's
11245 language, zero otherwise. */
11246static int
11247is_vtable_name (const char *name, struct dwarf2_cu *cu)
11248{
11249 static const char vptr[] = "_vptr";
987504bb 11250 static const char vtable[] = "vtable";
1168df01 11251
987504bb
JJ
11252 /* Look for the C++ and Java forms of the vtable. */
11253 if ((cu->language == language_java
11254 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11255 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11256 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11257 return 1;
11258
11259 return 0;
11260}
11261
c0dd20ea 11262/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11263 functions, with the ABI-specified layout. If TYPE describes
11264 such a structure, smash it into a member function type.
61049d3b
DJ
11265
11266 GCC shouldn't do this; it should just output pointer to member DIEs.
11267 This is GCC PR debug/28767. */
c0dd20ea 11268
0b92b5bb
TT
11269static void
11270quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11271{
0b92b5bb 11272 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11273
11274 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11275 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11276 return;
c0dd20ea
DJ
11277
11278 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11279 if (TYPE_FIELD_NAME (type, 0) == NULL
11280 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11281 || TYPE_FIELD_NAME (type, 1) == NULL
11282 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11283 return;
c0dd20ea
DJ
11284
11285 /* Find the type of the method. */
0b92b5bb 11286 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11287 if (pfn_type == NULL
11288 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11289 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11290 return;
c0dd20ea
DJ
11291
11292 /* Look for the "this" argument. */
11293 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11294 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11295 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11296 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11297 return;
c0dd20ea
DJ
11298
11299 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11300 new_type = alloc_type (objfile);
11301 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11302 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11303 TYPE_VARARGS (pfn_type));
0b92b5bb 11304 smash_to_methodptr_type (type, new_type);
c0dd20ea 11305}
1168df01 11306
685b1105
JK
11307/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11308 (icc). */
11309
11310static int
11311producer_is_icc (struct dwarf2_cu *cu)
11312{
11313 if (!cu->checked_producer)
11314 check_producer (cu);
11315
11316 return cu->producer_is_icc;
11317}
11318
c906108c 11319/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11320 (definition) to create a type for the structure or union. Fill in
11321 the type's name and general properties; the members will not be
11322 processed until process_structure_type.
c906108c 11323
c767944b
DJ
11324 NOTE: we need to call these functions regardless of whether or not the
11325 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11326 structure or union. This gets the type entered into our set of
11327 user defined types.
11328
11329 However, if the structure is incomplete (an opaque struct/union)
11330 then suppress creating a symbol table entry for it since gdb only
11331 wants to find the one with the complete definition. Note that if
11332 it is complete, we just call new_symbol, which does it's own
11333 checking about whether the struct/union is anonymous or not (and
11334 suppresses creating a symbol table entry itself). */
11335
f792889a 11336static struct type *
134d01f1 11337read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11338{
e7c27a73 11339 struct objfile *objfile = cu->objfile;
c906108c
SS
11340 struct type *type;
11341 struct attribute *attr;
15d034d0 11342 const char *name;
c906108c 11343
348e048f
DE
11344 /* If the definition of this type lives in .debug_types, read that type.
11345 Don't follow DW_AT_specification though, that will take us back up
11346 the chain and we want to go down. */
45e58e77 11347 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11348 if (attr)
11349 {
11350 struct dwarf2_cu *type_cu = cu;
11351 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11352
348e048f
DE
11353 /* We could just recurse on read_structure_type, but we need to call
11354 get_die_type to ensure only one type for this DIE is created.
11355 This is important, for example, because for c++ classes we need
11356 TYPE_NAME set which is only done by new_symbol. Blech. */
11357 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11358
11359 /* TYPE_CU may not be the same as CU.
11360 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11361 return set_die_type (die, type, cu);
11362 }
11363
c0dd20ea 11364 type = alloc_type (objfile);
c906108c 11365 INIT_CPLUS_SPECIFIC (type);
93311388 11366
39cbfefa
DJ
11367 name = dwarf2_name (die, cu);
11368 if (name != NULL)
c906108c 11369 {
987504bb
JJ
11370 if (cu->language == language_cplus
11371 || cu->language == language_java)
63d06c5c 11372 {
15d034d0 11373 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11374
11375 /* dwarf2_full_name might have already finished building the DIE's
11376 type. If so, there is no need to continue. */
11377 if (get_die_type (die, cu) != NULL)
11378 return get_die_type (die, cu);
11379
11380 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11381 if (die->tag == DW_TAG_structure_type
11382 || die->tag == DW_TAG_class_type)
11383 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11384 }
11385 else
11386 {
d8151005
DJ
11387 /* The name is already allocated along with this objfile, so
11388 we don't need to duplicate it for the type. */
7d455152 11389 TYPE_TAG_NAME (type) = name;
94af9270
KS
11390 if (die->tag == DW_TAG_class_type)
11391 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11392 }
c906108c
SS
11393 }
11394
11395 if (die->tag == DW_TAG_structure_type)
11396 {
11397 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11398 }
11399 else if (die->tag == DW_TAG_union_type)
11400 {
11401 TYPE_CODE (type) = TYPE_CODE_UNION;
11402 }
11403 else
11404 {
c906108c
SS
11405 TYPE_CODE (type) = TYPE_CODE_CLASS;
11406 }
11407
0cc2414c
TT
11408 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11409 TYPE_DECLARED_CLASS (type) = 1;
11410
e142c38c 11411 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11412 if (attr)
11413 {
11414 TYPE_LENGTH (type) = DW_UNSND (attr);
11415 }
11416 else
11417 {
11418 TYPE_LENGTH (type) = 0;
11419 }
11420
685b1105
JK
11421 if (producer_is_icc (cu))
11422 {
11423 /* ICC does not output the required DW_AT_declaration
11424 on incomplete types, but gives them a size of zero. */
11425 }
11426 else
11427 TYPE_STUB_SUPPORTED (type) = 1;
11428
dc718098 11429 if (die_is_declaration (die, cu))
876cecd0 11430 TYPE_STUB (type) = 1;
a6c727b2
DJ
11431 else if (attr == NULL && die->child == NULL
11432 && producer_is_realview (cu->producer))
11433 /* RealView does not output the required DW_AT_declaration
11434 on incomplete types. */
11435 TYPE_STUB (type) = 1;
dc718098 11436
c906108c
SS
11437 /* We need to add the type field to the die immediately so we don't
11438 infinitely recurse when dealing with pointers to the structure
0963b4bd 11439 type within the structure itself. */
1c379e20 11440 set_die_type (die, type, cu);
c906108c 11441
7e314c57
JK
11442 /* set_die_type should be already done. */
11443 set_descriptive_type (type, die, cu);
11444
c767944b
DJ
11445 return type;
11446}
11447
11448/* Finish creating a structure or union type, including filling in
11449 its members and creating a symbol for it. */
11450
11451static void
11452process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11453{
11454 struct objfile *objfile = cu->objfile;
11455 struct die_info *child_die = die->child;
11456 struct type *type;
11457
11458 type = get_die_type (die, cu);
11459 if (type == NULL)
11460 type = read_structure_type (die, cu);
11461
e142c38c 11462 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11463 {
11464 struct field_info fi;
11465 struct die_info *child_die;
34eaf542 11466 VEC (symbolp) *template_args = NULL;
c767944b 11467 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11468
11469 memset (&fi, 0, sizeof (struct field_info));
11470
639d11d3 11471 child_die = die->child;
c906108c
SS
11472
11473 while (child_die && child_die->tag)
11474 {
a9a9bd0f
DC
11475 if (child_die->tag == DW_TAG_member
11476 || child_die->tag == DW_TAG_variable)
c906108c 11477 {
a9a9bd0f
DC
11478 /* NOTE: carlton/2002-11-05: A C++ static data member
11479 should be a DW_TAG_member that is a declaration, but
11480 all versions of G++ as of this writing (so through at
11481 least 3.2.1) incorrectly generate DW_TAG_variable
11482 tags for them instead. */
e7c27a73 11483 dwarf2_add_field (&fi, child_die, cu);
c906108c 11484 }
8713b1b1 11485 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11486 {
0963b4bd 11487 /* C++ member function. */
e7c27a73 11488 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11489 }
11490 else if (child_die->tag == DW_TAG_inheritance)
11491 {
11492 /* C++ base class field. */
e7c27a73 11493 dwarf2_add_field (&fi, child_die, cu);
c906108c 11494 }
98751a41
JK
11495 else if (child_die->tag == DW_TAG_typedef)
11496 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11497 else if (child_die->tag == DW_TAG_template_type_param
11498 || child_die->tag == DW_TAG_template_value_param)
11499 {
11500 struct symbol *arg = new_symbol (child_die, NULL, cu);
11501
f1078f66
DJ
11502 if (arg != NULL)
11503 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11504 }
11505
c906108c
SS
11506 child_die = sibling_die (child_die);
11507 }
11508
34eaf542
TT
11509 /* Attach template arguments to type. */
11510 if (! VEC_empty (symbolp, template_args))
11511 {
11512 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11513 TYPE_N_TEMPLATE_ARGUMENTS (type)
11514 = VEC_length (symbolp, template_args);
11515 TYPE_TEMPLATE_ARGUMENTS (type)
11516 = obstack_alloc (&objfile->objfile_obstack,
11517 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11518 * sizeof (struct symbol *)));
11519 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11520 VEC_address (symbolp, template_args),
11521 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11522 * sizeof (struct symbol *)));
11523 VEC_free (symbolp, template_args);
11524 }
11525
c906108c
SS
11526 /* Attach fields and member functions to the type. */
11527 if (fi.nfields)
e7c27a73 11528 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11529 if (fi.nfnfields)
11530 {
e7c27a73 11531 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11532
c5aa993b 11533 /* Get the type which refers to the base class (possibly this
c906108c 11534 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11535 class from the DW_AT_containing_type attribute. This use of
11536 DW_AT_containing_type is a GNU extension. */
c906108c 11537
e142c38c 11538 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11539 {
e7c27a73 11540 struct type *t = die_containing_type (die, cu);
c906108c
SS
11541
11542 TYPE_VPTR_BASETYPE (type) = t;
11543 if (type == t)
11544 {
c906108c
SS
11545 int i;
11546
11547 /* Our own class provides vtbl ptr. */
11548 for (i = TYPE_NFIELDS (t) - 1;
11549 i >= TYPE_N_BASECLASSES (t);
11550 --i)
11551 {
0d5cff50 11552 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11553
1168df01 11554 if (is_vtable_name (fieldname, cu))
c906108c
SS
11555 {
11556 TYPE_VPTR_FIELDNO (type) = i;
11557 break;
11558 }
11559 }
11560
11561 /* Complain if virtual function table field not found. */
11562 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11563 complaint (&symfile_complaints,
3e43a32a
MS
11564 _("virtual function table pointer "
11565 "not found when defining class '%s'"),
4d3c2250
KB
11566 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11567 "");
c906108c
SS
11568 }
11569 else
11570 {
11571 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11572 }
11573 }
f6235d4c
EZ
11574 else if (cu->producer
11575 && strncmp (cu->producer,
11576 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11577 {
11578 /* The IBM XLC compiler does not provide direct indication
11579 of the containing type, but the vtable pointer is
11580 always named __vfp. */
11581
11582 int i;
11583
11584 for (i = TYPE_NFIELDS (type) - 1;
11585 i >= TYPE_N_BASECLASSES (type);
11586 --i)
11587 {
11588 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11589 {
11590 TYPE_VPTR_FIELDNO (type) = i;
11591 TYPE_VPTR_BASETYPE (type) = type;
11592 break;
11593 }
11594 }
11595 }
c906108c 11596 }
98751a41
JK
11597
11598 /* Copy fi.typedef_field_list linked list elements content into the
11599 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11600 if (fi.typedef_field_list)
11601 {
11602 int i = fi.typedef_field_list_count;
11603
a0d7a4ff 11604 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11605 TYPE_TYPEDEF_FIELD_ARRAY (type)
11606 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11607 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11608
11609 /* Reverse the list order to keep the debug info elements order. */
11610 while (--i >= 0)
11611 {
11612 struct typedef_field *dest, *src;
6e70227d 11613
98751a41
JK
11614 dest = &TYPE_TYPEDEF_FIELD (type, i);
11615 src = &fi.typedef_field_list->field;
11616 fi.typedef_field_list = fi.typedef_field_list->next;
11617 *dest = *src;
11618 }
11619 }
c767944b
DJ
11620
11621 do_cleanups (back_to);
eb2a6f42
TT
11622
11623 if (HAVE_CPLUS_STRUCT (type))
11624 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11625 }
63d06c5c 11626
bb5ed363 11627 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11628
90aeadfc
DC
11629 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11630 snapshots) has been known to create a die giving a declaration
11631 for a class that has, as a child, a die giving a definition for a
11632 nested class. So we have to process our children even if the
11633 current die is a declaration. Normally, of course, a declaration
11634 won't have any children at all. */
134d01f1 11635
90aeadfc
DC
11636 while (child_die != NULL && child_die->tag)
11637 {
11638 if (child_die->tag == DW_TAG_member
11639 || child_die->tag == DW_TAG_variable
34eaf542
TT
11640 || child_die->tag == DW_TAG_inheritance
11641 || child_die->tag == DW_TAG_template_value_param
11642 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11643 {
90aeadfc 11644 /* Do nothing. */
134d01f1 11645 }
90aeadfc
DC
11646 else
11647 process_die (child_die, cu);
134d01f1 11648
90aeadfc 11649 child_die = sibling_die (child_die);
134d01f1
DJ
11650 }
11651
fa4028e9
JB
11652 /* Do not consider external references. According to the DWARF standard,
11653 these DIEs are identified by the fact that they have no byte_size
11654 attribute, and a declaration attribute. */
11655 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11656 || !die_is_declaration (die, cu))
c767944b 11657 new_symbol (die, type, cu);
134d01f1
DJ
11658}
11659
11660/* Given a DW_AT_enumeration_type die, set its type. We do not
11661 complete the type's fields yet, or create any symbols. */
c906108c 11662
f792889a 11663static struct type *
134d01f1 11664read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11665{
e7c27a73 11666 struct objfile *objfile = cu->objfile;
c906108c 11667 struct type *type;
c906108c 11668 struct attribute *attr;
0114d602 11669 const char *name;
134d01f1 11670
348e048f
DE
11671 /* If the definition of this type lives in .debug_types, read that type.
11672 Don't follow DW_AT_specification though, that will take us back up
11673 the chain and we want to go down. */
45e58e77 11674 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11675 if (attr)
11676 {
11677 struct dwarf2_cu *type_cu = cu;
11678 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11679
348e048f 11680 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11681
11682 /* TYPE_CU may not be the same as CU.
11683 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11684 return set_die_type (die, type, cu);
11685 }
11686
c906108c
SS
11687 type = alloc_type (objfile);
11688
11689 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11690 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11691 if (name != NULL)
7d455152 11692 TYPE_TAG_NAME (type) = name;
c906108c 11693
e142c38c 11694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11695 if (attr)
11696 {
11697 TYPE_LENGTH (type) = DW_UNSND (attr);
11698 }
11699 else
11700 {
11701 TYPE_LENGTH (type) = 0;
11702 }
11703
137033e9
JB
11704 /* The enumeration DIE can be incomplete. In Ada, any type can be
11705 declared as private in the package spec, and then defined only
11706 inside the package body. Such types are known as Taft Amendment
11707 Types. When another package uses such a type, an incomplete DIE
11708 may be generated by the compiler. */
02eb380e 11709 if (die_is_declaration (die, cu))
876cecd0 11710 TYPE_STUB (type) = 1;
02eb380e 11711
f792889a 11712 return set_die_type (die, type, cu);
134d01f1
DJ
11713}
11714
11715/* Given a pointer to a die which begins an enumeration, process all
11716 the dies that define the members of the enumeration, and create the
11717 symbol for the enumeration type.
11718
11719 NOTE: We reverse the order of the element list. */
11720
11721static void
11722process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11723{
f792889a 11724 struct type *this_type;
134d01f1 11725
f792889a
DJ
11726 this_type = get_die_type (die, cu);
11727 if (this_type == NULL)
11728 this_type = read_enumeration_type (die, cu);
9dc481d3 11729
639d11d3 11730 if (die->child != NULL)
c906108c 11731 {
9dc481d3
DE
11732 struct die_info *child_die;
11733 struct symbol *sym;
11734 struct field *fields = NULL;
11735 int num_fields = 0;
11736 int unsigned_enum = 1;
15d034d0 11737 const char *name;
cafec441
TT
11738 int flag_enum = 1;
11739 ULONGEST mask = 0;
9dc481d3 11740
639d11d3 11741 child_die = die->child;
c906108c
SS
11742 while (child_die && child_die->tag)
11743 {
11744 if (child_die->tag != DW_TAG_enumerator)
11745 {
e7c27a73 11746 process_die (child_die, cu);
c906108c
SS
11747 }
11748 else
11749 {
39cbfefa
DJ
11750 name = dwarf2_name (child_die, cu);
11751 if (name)
c906108c 11752 {
f792889a 11753 sym = new_symbol (child_die, this_type, cu);
c906108c 11754 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11755 {
11756 unsigned_enum = 0;
11757 flag_enum = 0;
11758 }
11759 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11760 flag_enum = 0;
11761 else
11762 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11763
11764 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11765 {
11766 fields = (struct field *)
11767 xrealloc (fields,
11768 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11769 * sizeof (struct field));
c906108c
SS
11770 }
11771
3567439c 11772 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11773 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11774 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11775 FIELD_BITSIZE (fields[num_fields]) = 0;
11776
11777 num_fields++;
11778 }
11779 }
11780
11781 child_die = sibling_die (child_die);
11782 }
11783
11784 if (num_fields)
11785 {
f792889a
DJ
11786 TYPE_NFIELDS (this_type) = num_fields;
11787 TYPE_FIELDS (this_type) = (struct field *)
11788 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11789 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11790 sizeof (struct field) * num_fields);
b8c9b27d 11791 xfree (fields);
c906108c
SS
11792 }
11793 if (unsigned_enum)
876cecd0 11794 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11795 if (flag_enum)
11796 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11797 }
134d01f1 11798
6c83ed52
TT
11799 /* If we are reading an enum from a .debug_types unit, and the enum
11800 is a declaration, and the enum is not the signatured type in the
11801 unit, then we do not want to add a symbol for it. Adding a
11802 symbol would in some cases obscure the true definition of the
11803 enum, giving users an incomplete type when the definition is
11804 actually available. Note that we do not want to do this for all
11805 enums which are just declarations, because C++0x allows forward
11806 enum declarations. */
3019eac3 11807 if (cu->per_cu->is_debug_types
6c83ed52
TT
11808 && die_is_declaration (die, cu))
11809 {
52dc124a 11810 struct signatured_type *sig_type;
6c83ed52 11811
52dc124a 11812 sig_type
6c83ed52 11813 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8a0459fd 11814 cu->per_cu->section,
6c83ed52 11815 cu->per_cu->offset);
3019eac3
DE
11816 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11817 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11818 return;
11819 }
11820
f792889a 11821 new_symbol (die, this_type, cu);
c906108c
SS
11822}
11823
11824/* Extract all information from a DW_TAG_array_type DIE and put it in
11825 the DIE's type field. For now, this only handles one dimensional
11826 arrays. */
11827
f792889a 11828static struct type *
e7c27a73 11829read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11830{
e7c27a73 11831 struct objfile *objfile = cu->objfile;
c906108c 11832 struct die_info *child_die;
7e314c57 11833 struct type *type;
c906108c
SS
11834 struct type *element_type, *range_type, *index_type;
11835 struct type **range_types = NULL;
11836 struct attribute *attr;
11837 int ndim = 0;
11838 struct cleanup *back_to;
15d034d0 11839 const char *name;
c906108c 11840
e7c27a73 11841 element_type = die_type (die, cu);
c906108c 11842
7e314c57
JK
11843 /* The die_type call above may have already set the type for this DIE. */
11844 type = get_die_type (die, cu);
11845 if (type)
11846 return type;
11847
c906108c
SS
11848 /* Irix 6.2 native cc creates array types without children for
11849 arrays with unspecified length. */
639d11d3 11850 if (die->child == NULL)
c906108c 11851 {
46bf5051 11852 index_type = objfile_type (objfile)->builtin_int;
c906108c 11853 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11854 type = create_array_type (NULL, element_type, range_type);
11855 return set_die_type (die, type, cu);
c906108c
SS
11856 }
11857
11858 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11859 child_die = die->child;
c906108c
SS
11860 while (child_die && child_die->tag)
11861 {
11862 if (child_die->tag == DW_TAG_subrange_type)
11863 {
f792889a 11864 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11865
f792889a 11866 if (child_type != NULL)
a02abb62 11867 {
0963b4bd
MS
11868 /* The range type was succesfully read. Save it for the
11869 array type creation. */
a02abb62
JB
11870 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11871 {
11872 range_types = (struct type **)
11873 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11874 * sizeof (struct type *));
11875 if (ndim == 0)
11876 make_cleanup (free_current_contents, &range_types);
11877 }
f792889a 11878 range_types[ndim++] = child_type;
a02abb62 11879 }
c906108c
SS
11880 }
11881 child_die = sibling_die (child_die);
11882 }
11883
11884 /* Dwarf2 dimensions are output from left to right, create the
11885 necessary array types in backwards order. */
7ca2d3a3 11886
c906108c 11887 type = element_type;
7ca2d3a3
DL
11888
11889 if (read_array_order (die, cu) == DW_ORD_col_major)
11890 {
11891 int i = 0;
9a619af0 11892
7ca2d3a3
DL
11893 while (i < ndim)
11894 type = create_array_type (NULL, type, range_types[i++]);
11895 }
11896 else
11897 {
11898 while (ndim-- > 0)
11899 type = create_array_type (NULL, type, range_types[ndim]);
11900 }
c906108c 11901
f5f8a009
EZ
11902 /* Understand Dwarf2 support for vector types (like they occur on
11903 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11904 array type. This is not part of the Dwarf2/3 standard yet, but a
11905 custom vendor extension. The main difference between a regular
11906 array and the vector variant is that vectors are passed by value
11907 to functions. */
e142c38c 11908 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11909 if (attr)
ea37ba09 11910 make_vector_type (type);
f5f8a009 11911
dbc98a8b
KW
11912 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11913 implementation may choose to implement triple vectors using this
11914 attribute. */
11915 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11916 if (attr)
11917 {
11918 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11919 TYPE_LENGTH (type) = DW_UNSND (attr);
11920 else
3e43a32a
MS
11921 complaint (&symfile_complaints,
11922 _("DW_AT_byte_size for array type smaller "
11923 "than the total size of elements"));
dbc98a8b
KW
11924 }
11925
39cbfefa
DJ
11926 name = dwarf2_name (die, cu);
11927 if (name)
11928 TYPE_NAME (type) = name;
6e70227d 11929
0963b4bd 11930 /* Install the type in the die. */
7e314c57
JK
11931 set_die_type (die, type, cu);
11932
11933 /* set_die_type should be already done. */
b4ba55a1
JB
11934 set_descriptive_type (type, die, cu);
11935
c906108c
SS
11936 do_cleanups (back_to);
11937
7e314c57 11938 return type;
c906108c
SS
11939}
11940
7ca2d3a3 11941static enum dwarf_array_dim_ordering
6e70227d 11942read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11943{
11944 struct attribute *attr;
11945
11946 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11947
11948 if (attr) return DW_SND (attr);
11949
0963b4bd
MS
11950 /* GNU F77 is a special case, as at 08/2004 array type info is the
11951 opposite order to the dwarf2 specification, but data is still
11952 laid out as per normal fortran.
7ca2d3a3 11953
0963b4bd
MS
11954 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11955 version checking. */
7ca2d3a3 11956
905e0470
PM
11957 if (cu->language == language_fortran
11958 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11959 {
11960 return DW_ORD_row_major;
11961 }
11962
6e70227d 11963 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11964 {
11965 case array_column_major:
11966 return DW_ORD_col_major;
11967 case array_row_major:
11968 default:
11969 return DW_ORD_row_major;
11970 };
11971}
11972
72019c9c 11973/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11974 the DIE's type field. */
72019c9c 11975
f792889a 11976static struct type *
72019c9c
GM
11977read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11978{
7e314c57
JK
11979 struct type *domain_type, *set_type;
11980 struct attribute *attr;
f792889a 11981
7e314c57
JK
11982 domain_type = die_type (die, cu);
11983
11984 /* The die_type call above may have already set the type for this DIE. */
11985 set_type = get_die_type (die, cu);
11986 if (set_type)
11987 return set_type;
11988
11989 set_type = create_set_type (NULL, domain_type);
11990
11991 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11992 if (attr)
11993 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11994
f792889a 11995 return set_die_type (die, set_type, cu);
72019c9c 11996}
7ca2d3a3 11997
0971de02
TT
11998/* A helper for read_common_block that creates a locexpr baton.
11999 SYM is the symbol which we are marking as computed.
12000 COMMON_DIE is the DIE for the common block.
12001 COMMON_LOC is the location expression attribute for the common
12002 block itself.
12003 MEMBER_LOC is the location expression attribute for the particular
12004 member of the common block that we are processing.
12005 CU is the CU from which the above come. */
12006
12007static void
12008mark_common_block_symbol_computed (struct symbol *sym,
12009 struct die_info *common_die,
12010 struct attribute *common_loc,
12011 struct attribute *member_loc,
12012 struct dwarf2_cu *cu)
12013{
12014 struct objfile *objfile = dwarf2_per_objfile->objfile;
12015 struct dwarf2_locexpr_baton *baton;
12016 gdb_byte *ptr;
12017 unsigned int cu_off;
12018 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12019 LONGEST offset = 0;
12020
12021 gdb_assert (common_loc && member_loc);
12022 gdb_assert (attr_form_is_block (common_loc));
12023 gdb_assert (attr_form_is_block (member_loc)
12024 || attr_form_is_constant (member_loc));
12025
12026 baton = obstack_alloc (&objfile->objfile_obstack,
12027 sizeof (struct dwarf2_locexpr_baton));
12028 baton->per_cu = cu->per_cu;
12029 gdb_assert (baton->per_cu);
12030
12031 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12032
12033 if (attr_form_is_constant (member_loc))
12034 {
12035 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12036 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12037 }
12038 else
12039 baton->size += DW_BLOCK (member_loc)->size;
12040
12041 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12042 baton->data = ptr;
12043
12044 *ptr++ = DW_OP_call4;
12045 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12046 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12047 ptr += 4;
12048
12049 if (attr_form_is_constant (member_loc))
12050 {
12051 *ptr++ = DW_OP_addr;
12052 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12053 ptr += cu->header.addr_size;
12054 }
12055 else
12056 {
12057 /* We have to copy the data here, because DW_OP_call4 will only
12058 use a DW_AT_location attribute. */
12059 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12060 ptr += DW_BLOCK (member_loc)->size;
12061 }
12062
12063 *ptr++ = DW_OP_plus;
12064 gdb_assert (ptr - baton->data == baton->size);
12065
0971de02 12066 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12067 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12068}
12069
4357ac6c
TT
12070/* Create appropriate locally-scoped variables for all the
12071 DW_TAG_common_block entries. Also create a struct common_block
12072 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12073 is used to sepate the common blocks name namespace from regular
12074 variable names. */
c906108c
SS
12075
12076static void
e7c27a73 12077read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12078{
0971de02
TT
12079 struct attribute *attr;
12080
12081 attr = dwarf2_attr (die, DW_AT_location, cu);
12082 if (attr)
12083 {
12084 /* Support the .debug_loc offsets. */
12085 if (attr_form_is_block (attr))
12086 {
12087 /* Ok. */
12088 }
12089 else if (attr_form_is_section_offset (attr))
12090 {
12091 dwarf2_complex_location_expr_complaint ();
12092 attr = NULL;
12093 }
12094 else
12095 {
12096 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12097 "common block member");
12098 attr = NULL;
12099 }
12100 }
12101
639d11d3 12102 if (die->child != NULL)
c906108c 12103 {
4357ac6c
TT
12104 struct objfile *objfile = cu->objfile;
12105 struct die_info *child_die;
12106 size_t n_entries = 0, size;
12107 struct common_block *common_block;
12108 struct symbol *sym;
74ac6d43 12109
4357ac6c
TT
12110 for (child_die = die->child;
12111 child_die && child_die->tag;
12112 child_die = sibling_die (child_die))
12113 ++n_entries;
12114
12115 size = (sizeof (struct common_block)
12116 + (n_entries - 1) * sizeof (struct symbol *));
12117 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12118 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12119 common_block->n_entries = 0;
12120
12121 for (child_die = die->child;
12122 child_die && child_die->tag;
12123 child_die = sibling_die (child_die))
12124 {
12125 /* Create the symbol in the DW_TAG_common_block block in the current
12126 symbol scope. */
e7c27a73 12127 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12128 if (sym != NULL)
12129 {
12130 struct attribute *member_loc;
12131
12132 common_block->contents[common_block->n_entries++] = sym;
12133
12134 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12135 cu);
12136 if (member_loc)
12137 {
12138 /* GDB has handled this for a long time, but it is
12139 not specified by DWARF. It seems to have been
12140 emitted by gfortran at least as recently as:
12141 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12142 complaint (&symfile_complaints,
12143 _("Variable in common block has "
12144 "DW_AT_data_member_location "
12145 "- DIE at 0x%x [in module %s]"),
12146 child_die->offset.sect_off, cu->objfile->name);
12147
12148 if (attr_form_is_section_offset (member_loc))
12149 dwarf2_complex_location_expr_complaint ();
12150 else if (attr_form_is_constant (member_loc)
12151 || attr_form_is_block (member_loc))
12152 {
12153 if (attr)
12154 mark_common_block_symbol_computed (sym, die, attr,
12155 member_loc, cu);
12156 }
12157 else
12158 dwarf2_complex_location_expr_complaint ();
12159 }
12160 }
c906108c 12161 }
4357ac6c
TT
12162
12163 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12164 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12165 }
12166}
12167
0114d602 12168/* Create a type for a C++ namespace. */
d9fa45fe 12169
0114d602
DJ
12170static struct type *
12171read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12172{
e7c27a73 12173 struct objfile *objfile = cu->objfile;
0114d602 12174 const char *previous_prefix, *name;
9219021c 12175 int is_anonymous;
0114d602
DJ
12176 struct type *type;
12177
12178 /* For extensions, reuse the type of the original namespace. */
12179 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12180 {
12181 struct die_info *ext_die;
12182 struct dwarf2_cu *ext_cu = cu;
9a619af0 12183
0114d602
DJ
12184 ext_die = dwarf2_extension (die, &ext_cu);
12185 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12186
12187 /* EXT_CU may not be the same as CU.
12188 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12189 return set_die_type (die, type, cu);
12190 }
9219021c 12191
e142c38c 12192 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12193
12194 /* Now build the name of the current namespace. */
12195
0114d602
DJ
12196 previous_prefix = determine_prefix (die, cu);
12197 if (previous_prefix[0] != '\0')
12198 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12199 previous_prefix, name, 0, cu);
0114d602
DJ
12200
12201 /* Create the type. */
12202 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12203 objfile);
abee88f2 12204 TYPE_NAME (type) = name;
0114d602
DJ
12205 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12206
60531b24 12207 return set_die_type (die, type, cu);
0114d602
DJ
12208}
12209
12210/* Read a C++ namespace. */
12211
12212static void
12213read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12214{
12215 struct objfile *objfile = cu->objfile;
0114d602 12216 int is_anonymous;
9219021c 12217
5c4e30ca
DC
12218 /* Add a symbol associated to this if we haven't seen the namespace
12219 before. Also, add a using directive if it's an anonymous
12220 namespace. */
9219021c 12221
f2f0e013 12222 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12223 {
12224 struct type *type;
12225
0114d602 12226 type = read_type_die (die, cu);
e7c27a73 12227 new_symbol (die, type, cu);
5c4e30ca 12228
e8e80198 12229 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12230 if (is_anonymous)
0114d602
DJ
12231 {
12232 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12233
c0cc3a76 12234 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12235 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12236 }
5c4e30ca 12237 }
9219021c 12238
639d11d3 12239 if (die->child != NULL)
d9fa45fe 12240 {
639d11d3 12241 struct die_info *child_die = die->child;
6e70227d 12242
d9fa45fe
DC
12243 while (child_die && child_die->tag)
12244 {
e7c27a73 12245 process_die (child_die, cu);
d9fa45fe
DC
12246 child_die = sibling_die (child_die);
12247 }
12248 }
38d518c9
EZ
12249}
12250
f55ee35c
JK
12251/* Read a Fortran module as type. This DIE can be only a declaration used for
12252 imported module. Still we need that type as local Fortran "use ... only"
12253 declaration imports depend on the created type in determine_prefix. */
12254
12255static struct type *
12256read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12257{
12258 struct objfile *objfile = cu->objfile;
15d034d0 12259 const char *module_name;
f55ee35c
JK
12260 struct type *type;
12261
12262 module_name = dwarf2_name (die, cu);
12263 if (!module_name)
3e43a32a
MS
12264 complaint (&symfile_complaints,
12265 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12266 die->offset.sect_off);
f55ee35c
JK
12267 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12268
12269 /* determine_prefix uses TYPE_TAG_NAME. */
12270 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12271
12272 return set_die_type (die, type, cu);
12273}
12274
5d7cb8df
JK
12275/* Read a Fortran module. */
12276
12277static void
12278read_module (struct die_info *die, struct dwarf2_cu *cu)
12279{
12280 struct die_info *child_die = die->child;
12281
5d7cb8df
JK
12282 while (child_die && child_die->tag)
12283 {
12284 process_die (child_die, cu);
12285 child_die = sibling_die (child_die);
12286 }
12287}
12288
38d518c9
EZ
12289/* Return the name of the namespace represented by DIE. Set
12290 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12291 namespace. */
12292
12293static const char *
e142c38c 12294namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12295{
12296 struct die_info *current_die;
12297 const char *name = NULL;
12298
12299 /* Loop through the extensions until we find a name. */
12300
12301 for (current_die = die;
12302 current_die != NULL;
f2f0e013 12303 current_die = dwarf2_extension (die, &cu))
38d518c9 12304 {
e142c38c 12305 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12306 if (name != NULL)
12307 break;
12308 }
12309
12310 /* Is it an anonymous namespace? */
12311
12312 *is_anonymous = (name == NULL);
12313 if (*is_anonymous)
2b1dbab0 12314 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12315
12316 return name;
d9fa45fe
DC
12317}
12318
c906108c
SS
12319/* Extract all information from a DW_TAG_pointer_type DIE and add to
12320 the user defined type vector. */
12321
f792889a 12322static struct type *
e7c27a73 12323read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12324{
5e2b427d 12325 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12326 struct comp_unit_head *cu_header = &cu->header;
c906108c 12327 struct type *type;
8b2dbe47
KB
12328 struct attribute *attr_byte_size;
12329 struct attribute *attr_address_class;
12330 int byte_size, addr_class;
7e314c57
JK
12331 struct type *target_type;
12332
12333 target_type = die_type (die, cu);
c906108c 12334
7e314c57
JK
12335 /* The die_type call above may have already set the type for this DIE. */
12336 type = get_die_type (die, cu);
12337 if (type)
12338 return type;
12339
12340 type = lookup_pointer_type (target_type);
8b2dbe47 12341
e142c38c 12342 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12343 if (attr_byte_size)
12344 byte_size = DW_UNSND (attr_byte_size);
c906108c 12345 else
8b2dbe47
KB
12346 byte_size = cu_header->addr_size;
12347
e142c38c 12348 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12349 if (attr_address_class)
12350 addr_class = DW_UNSND (attr_address_class);
12351 else
12352 addr_class = DW_ADDR_none;
12353
12354 /* If the pointer size or address class is different than the
12355 default, create a type variant marked as such and set the
12356 length accordingly. */
12357 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12358 {
5e2b427d 12359 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12360 {
12361 int type_flags;
12362
849957d9 12363 type_flags = gdbarch_address_class_type_flags
5e2b427d 12364 (gdbarch, byte_size, addr_class);
876cecd0
TT
12365 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12366 == 0);
8b2dbe47
KB
12367 type = make_type_with_address_space (type, type_flags);
12368 }
12369 else if (TYPE_LENGTH (type) != byte_size)
12370 {
3e43a32a
MS
12371 complaint (&symfile_complaints,
12372 _("invalid pointer size %d"), byte_size);
8b2dbe47 12373 }
6e70227d 12374 else
9a619af0
MS
12375 {
12376 /* Should we also complain about unhandled address classes? */
12377 }
c906108c 12378 }
8b2dbe47
KB
12379
12380 TYPE_LENGTH (type) = byte_size;
f792889a 12381 return set_die_type (die, type, cu);
c906108c
SS
12382}
12383
12384/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12385 the user defined type vector. */
12386
f792889a 12387static struct type *
e7c27a73 12388read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12389{
12390 struct type *type;
12391 struct type *to_type;
12392 struct type *domain;
12393
e7c27a73
DJ
12394 to_type = die_type (die, cu);
12395 domain = die_containing_type (die, cu);
0d5de010 12396
7e314c57
JK
12397 /* The calls above may have already set the type for this DIE. */
12398 type = get_die_type (die, cu);
12399 if (type)
12400 return type;
12401
0d5de010
DJ
12402 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12403 type = lookup_methodptr_type (to_type);
7078baeb
TT
12404 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12405 {
12406 struct type *new_type = alloc_type (cu->objfile);
12407
12408 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12409 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12410 TYPE_VARARGS (to_type));
12411 type = lookup_methodptr_type (new_type);
12412 }
0d5de010
DJ
12413 else
12414 type = lookup_memberptr_type (to_type, domain);
c906108c 12415
f792889a 12416 return set_die_type (die, type, cu);
c906108c
SS
12417}
12418
12419/* Extract all information from a DW_TAG_reference_type DIE and add to
12420 the user defined type vector. */
12421
f792889a 12422static struct type *
e7c27a73 12423read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12424{
e7c27a73 12425 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12426 struct type *type, *target_type;
c906108c
SS
12427 struct attribute *attr;
12428
7e314c57
JK
12429 target_type = die_type (die, cu);
12430
12431 /* The die_type call above may have already set the type for this DIE. */
12432 type = get_die_type (die, cu);
12433 if (type)
12434 return type;
12435
12436 type = lookup_reference_type (target_type);
e142c38c 12437 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12438 if (attr)
12439 {
12440 TYPE_LENGTH (type) = DW_UNSND (attr);
12441 }
12442 else
12443 {
107d2387 12444 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12445 }
f792889a 12446 return set_die_type (die, type, cu);
c906108c
SS
12447}
12448
f792889a 12449static struct type *
e7c27a73 12450read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12451{
f792889a 12452 struct type *base_type, *cv_type;
c906108c 12453
e7c27a73 12454 base_type = die_type (die, cu);
7e314c57
JK
12455
12456 /* The die_type call above may have already set the type for this DIE. */
12457 cv_type = get_die_type (die, cu);
12458 if (cv_type)
12459 return cv_type;
12460
2f608a3a
KW
12461 /* In case the const qualifier is applied to an array type, the element type
12462 is so qualified, not the array type (section 6.7.3 of C99). */
12463 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12464 {
12465 struct type *el_type, *inner_array;
12466
12467 base_type = copy_type (base_type);
12468 inner_array = base_type;
12469
12470 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12471 {
12472 TYPE_TARGET_TYPE (inner_array) =
12473 copy_type (TYPE_TARGET_TYPE (inner_array));
12474 inner_array = TYPE_TARGET_TYPE (inner_array);
12475 }
12476
12477 el_type = TYPE_TARGET_TYPE (inner_array);
12478 TYPE_TARGET_TYPE (inner_array) =
12479 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12480
12481 return set_die_type (die, base_type, cu);
12482 }
12483
f792889a
DJ
12484 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12485 return set_die_type (die, cv_type, cu);
c906108c
SS
12486}
12487
f792889a 12488static struct type *
e7c27a73 12489read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12490{
f792889a 12491 struct type *base_type, *cv_type;
c906108c 12492
e7c27a73 12493 base_type = die_type (die, cu);
7e314c57
JK
12494
12495 /* The die_type call above may have already set the type for this DIE. */
12496 cv_type = get_die_type (die, cu);
12497 if (cv_type)
12498 return cv_type;
12499
f792889a
DJ
12500 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12501 return set_die_type (die, cv_type, cu);
c906108c
SS
12502}
12503
06d66ee9
TT
12504/* Handle DW_TAG_restrict_type. */
12505
12506static struct type *
12507read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12508{
12509 struct type *base_type, *cv_type;
12510
12511 base_type = die_type (die, cu);
12512
12513 /* The die_type call above may have already set the type for this DIE. */
12514 cv_type = get_die_type (die, cu);
12515 if (cv_type)
12516 return cv_type;
12517
12518 cv_type = make_restrict_type (base_type);
12519 return set_die_type (die, cv_type, cu);
12520}
12521
c906108c
SS
12522/* Extract all information from a DW_TAG_string_type DIE and add to
12523 the user defined type vector. It isn't really a user defined type,
12524 but it behaves like one, with other DIE's using an AT_user_def_type
12525 attribute to reference it. */
12526
f792889a 12527static struct type *
e7c27a73 12528read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12529{
e7c27a73 12530 struct objfile *objfile = cu->objfile;
3b7538c0 12531 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12532 struct type *type, *range_type, *index_type, *char_type;
12533 struct attribute *attr;
12534 unsigned int length;
12535
e142c38c 12536 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12537 if (attr)
12538 {
12539 length = DW_UNSND (attr);
12540 }
12541 else
12542 {
0963b4bd 12543 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12544 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12545 if (attr)
12546 {
12547 length = DW_UNSND (attr);
12548 }
12549 else
12550 {
12551 length = 1;
12552 }
c906108c 12553 }
6ccb9162 12554
46bf5051 12555 index_type = objfile_type (objfile)->builtin_int;
c906108c 12556 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12557 char_type = language_string_char_type (cu->language_defn, gdbarch);
12558 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12559
f792889a 12560 return set_die_type (die, type, cu);
c906108c
SS
12561}
12562
12563/* Handle DIES due to C code like:
12564
12565 struct foo
c5aa993b
JM
12566 {
12567 int (*funcp)(int a, long l);
12568 int b;
12569 };
c906108c 12570
0963b4bd 12571 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12572
f792889a 12573static struct type *
e7c27a73 12574read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12575{
bb5ed363 12576 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12577 struct type *type; /* Type that this function returns. */
12578 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12579 struct attribute *attr;
12580
e7c27a73 12581 type = die_type (die, cu);
7e314c57
JK
12582
12583 /* The die_type call above may have already set the type for this DIE. */
12584 ftype = get_die_type (die, cu);
12585 if (ftype)
12586 return ftype;
12587
0c8b41f1 12588 ftype = lookup_function_type (type);
c906108c 12589
5b8101ae 12590 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12591 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12592 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12593 || cu->language == language_cplus
5b8101ae
PM
12594 || cu->language == language_java
12595 || cu->language == language_pascal)
876cecd0 12596 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12597 else if (producer_is_realview (cu->producer))
12598 /* RealView does not emit DW_AT_prototyped. We can not
12599 distinguish prototyped and unprototyped functions; default to
12600 prototyped, since that is more common in modern code (and
12601 RealView warns about unprototyped functions). */
12602 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12603
c055b101
CV
12604 /* Store the calling convention in the type if it's available in
12605 the subroutine die. Otherwise set the calling convention to
12606 the default value DW_CC_normal. */
12607 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12608 if (attr)
12609 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12610 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12611 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12612 else
12613 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12614
12615 /* We need to add the subroutine type to the die immediately so
12616 we don't infinitely recurse when dealing with parameters
0963b4bd 12617 declared as the same subroutine type. */
76c10ea2 12618 set_die_type (die, ftype, cu);
6e70227d 12619
639d11d3 12620 if (die->child != NULL)
c906108c 12621 {
bb5ed363 12622 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12623 struct die_info *child_die;
8072405b 12624 int nparams, iparams;
c906108c
SS
12625
12626 /* Count the number of parameters.
12627 FIXME: GDB currently ignores vararg functions, but knows about
12628 vararg member functions. */
8072405b 12629 nparams = 0;
639d11d3 12630 child_die = die->child;
c906108c
SS
12631 while (child_die && child_die->tag)
12632 {
12633 if (child_die->tag == DW_TAG_formal_parameter)
12634 nparams++;
12635 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12636 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12637 child_die = sibling_die (child_die);
12638 }
12639
12640 /* Allocate storage for parameters and fill them in. */
12641 TYPE_NFIELDS (ftype) = nparams;
12642 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12643 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12644
8072405b
JK
12645 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12646 even if we error out during the parameters reading below. */
12647 for (iparams = 0; iparams < nparams; iparams++)
12648 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12649
12650 iparams = 0;
639d11d3 12651 child_die = die->child;
c906108c
SS
12652 while (child_die && child_die->tag)
12653 {
12654 if (child_die->tag == DW_TAG_formal_parameter)
12655 {
3ce3b1ba
PA
12656 struct type *arg_type;
12657
12658 /* DWARF version 2 has no clean way to discern C++
12659 static and non-static member functions. G++ helps
12660 GDB by marking the first parameter for non-static
12661 member functions (which is the this pointer) as
12662 artificial. We pass this information to
12663 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12664
12665 DWARF version 3 added DW_AT_object_pointer, which GCC
12666 4.5 does not yet generate. */
e142c38c 12667 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12668 if (attr)
12669 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12670 else
418835cc
KS
12671 {
12672 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12673
12674 /* GCC/43521: In java, the formal parameter
12675 "this" is sometimes not marked with DW_AT_artificial. */
12676 if (cu->language == language_java)
12677 {
12678 const char *name = dwarf2_name (child_die, cu);
9a619af0 12679
418835cc
KS
12680 if (name && !strcmp (name, "this"))
12681 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12682 }
12683 }
3ce3b1ba
PA
12684 arg_type = die_type (child_die, cu);
12685
12686 /* RealView does not mark THIS as const, which the testsuite
12687 expects. GCC marks THIS as const in method definitions,
12688 but not in the class specifications (GCC PR 43053). */
12689 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12690 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12691 {
12692 int is_this = 0;
12693 struct dwarf2_cu *arg_cu = cu;
12694 const char *name = dwarf2_name (child_die, cu);
12695
12696 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12697 if (attr)
12698 {
12699 /* If the compiler emits this, use it. */
12700 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12701 is_this = 1;
12702 }
12703 else if (name && strcmp (name, "this") == 0)
12704 /* Function definitions will have the argument names. */
12705 is_this = 1;
12706 else if (name == NULL && iparams == 0)
12707 /* Declarations may not have the names, so like
12708 elsewhere in GDB, assume an artificial first
12709 argument is "this". */
12710 is_this = 1;
12711
12712 if (is_this)
12713 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12714 arg_type, 0);
12715 }
12716
12717 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12718 iparams++;
12719 }
12720 child_die = sibling_die (child_die);
12721 }
12722 }
12723
76c10ea2 12724 return ftype;
c906108c
SS
12725}
12726
f792889a 12727static struct type *
e7c27a73 12728read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12729{
e7c27a73 12730 struct objfile *objfile = cu->objfile;
0114d602 12731 const char *name = NULL;
3c8e0968 12732 struct type *this_type, *target_type;
c906108c 12733
94af9270 12734 name = dwarf2_full_name (NULL, die, cu);
f792889a 12735 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12736 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12737 TYPE_NAME (this_type) = name;
f792889a 12738 set_die_type (die, this_type, cu);
3c8e0968
DE
12739 target_type = die_type (die, cu);
12740 if (target_type != this_type)
12741 TYPE_TARGET_TYPE (this_type) = target_type;
12742 else
12743 {
12744 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12745 spec and cause infinite loops in GDB. */
12746 complaint (&symfile_complaints,
12747 _("Self-referential DW_TAG_typedef "
12748 "- DIE at 0x%x [in module %s]"),
b64f50a1 12749 die->offset.sect_off, objfile->name);
3c8e0968
DE
12750 TYPE_TARGET_TYPE (this_type) = NULL;
12751 }
f792889a 12752 return this_type;
c906108c
SS
12753}
12754
12755/* Find a representation of a given base type and install
12756 it in the TYPE field of the die. */
12757
f792889a 12758static struct type *
e7c27a73 12759read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12760{
e7c27a73 12761 struct objfile *objfile = cu->objfile;
c906108c
SS
12762 struct type *type;
12763 struct attribute *attr;
12764 int encoding = 0, size = 0;
15d034d0 12765 const char *name;
6ccb9162
UW
12766 enum type_code code = TYPE_CODE_INT;
12767 int type_flags = 0;
12768 struct type *target_type = NULL;
c906108c 12769
e142c38c 12770 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12771 if (attr)
12772 {
12773 encoding = DW_UNSND (attr);
12774 }
e142c38c 12775 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12776 if (attr)
12777 {
12778 size = DW_UNSND (attr);
12779 }
39cbfefa 12780 name = dwarf2_name (die, cu);
6ccb9162 12781 if (!name)
c906108c 12782 {
6ccb9162
UW
12783 complaint (&symfile_complaints,
12784 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12785 }
6ccb9162
UW
12786
12787 switch (encoding)
c906108c 12788 {
6ccb9162
UW
12789 case DW_ATE_address:
12790 /* Turn DW_ATE_address into a void * pointer. */
12791 code = TYPE_CODE_PTR;
12792 type_flags |= TYPE_FLAG_UNSIGNED;
12793 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12794 break;
12795 case DW_ATE_boolean:
12796 code = TYPE_CODE_BOOL;
12797 type_flags |= TYPE_FLAG_UNSIGNED;
12798 break;
12799 case DW_ATE_complex_float:
12800 code = TYPE_CODE_COMPLEX;
12801 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12802 break;
12803 case DW_ATE_decimal_float:
12804 code = TYPE_CODE_DECFLOAT;
12805 break;
12806 case DW_ATE_float:
12807 code = TYPE_CODE_FLT;
12808 break;
12809 case DW_ATE_signed:
12810 break;
12811 case DW_ATE_unsigned:
12812 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12813 if (cu->language == language_fortran
12814 && name
12815 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12816 code = TYPE_CODE_CHAR;
6ccb9162
UW
12817 break;
12818 case DW_ATE_signed_char:
6e70227d 12819 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12820 || cu->language == language_pascal
12821 || cu->language == language_fortran)
6ccb9162
UW
12822 code = TYPE_CODE_CHAR;
12823 break;
12824 case DW_ATE_unsigned_char:
868a0084 12825 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12826 || cu->language == language_pascal
12827 || cu->language == language_fortran)
6ccb9162
UW
12828 code = TYPE_CODE_CHAR;
12829 type_flags |= TYPE_FLAG_UNSIGNED;
12830 break;
75079b2b
TT
12831 case DW_ATE_UTF:
12832 /* We just treat this as an integer and then recognize the
12833 type by name elsewhere. */
12834 break;
12835
6ccb9162
UW
12836 default:
12837 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12838 dwarf_type_encoding_name (encoding));
12839 break;
c906108c 12840 }
6ccb9162 12841
0114d602
DJ
12842 type = init_type (code, size, type_flags, NULL, objfile);
12843 TYPE_NAME (type) = name;
6ccb9162
UW
12844 TYPE_TARGET_TYPE (type) = target_type;
12845
0114d602 12846 if (name && strcmp (name, "char") == 0)
876cecd0 12847 TYPE_NOSIGN (type) = 1;
0114d602 12848
f792889a 12849 return set_die_type (die, type, cu);
c906108c
SS
12850}
12851
a02abb62
JB
12852/* Read the given DW_AT_subrange DIE. */
12853
f792889a 12854static struct type *
a02abb62
JB
12855read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12856{
4c9ad8c2 12857 struct type *base_type, *orig_base_type;
a02abb62
JB
12858 struct type *range_type;
12859 struct attribute *attr;
4fae6e18
JK
12860 LONGEST low, high;
12861 int low_default_is_valid;
15d034d0 12862 const char *name;
43bbcdc2 12863 LONGEST negative_mask;
e77813c8 12864
4c9ad8c2
TT
12865 orig_base_type = die_type (die, cu);
12866 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12867 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12868 creating the range type, but we use the result of check_typedef
12869 when examining properties of the type. */
12870 base_type = check_typedef (orig_base_type);
a02abb62 12871
7e314c57
JK
12872 /* The die_type call above may have already set the type for this DIE. */
12873 range_type = get_die_type (die, cu);
12874 if (range_type)
12875 return range_type;
12876
4fae6e18
JK
12877 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12878 omitting DW_AT_lower_bound. */
12879 switch (cu->language)
6e70227d 12880 {
4fae6e18
JK
12881 case language_c:
12882 case language_cplus:
12883 low = 0;
12884 low_default_is_valid = 1;
12885 break;
12886 case language_fortran:
12887 low = 1;
12888 low_default_is_valid = 1;
12889 break;
12890 case language_d:
12891 case language_java:
12892 case language_objc:
12893 low = 0;
12894 low_default_is_valid = (cu->header.version >= 4);
12895 break;
12896 case language_ada:
12897 case language_m2:
12898 case language_pascal:
a02abb62 12899 low = 1;
4fae6e18
JK
12900 low_default_is_valid = (cu->header.version >= 4);
12901 break;
12902 default:
12903 low = 0;
12904 low_default_is_valid = 0;
12905 break;
a02abb62
JB
12906 }
12907
dd5e6932
DJ
12908 /* FIXME: For variable sized arrays either of these could be
12909 a variable rather than a constant value. We'll allow it,
12910 but we don't know how to handle it. */
e142c38c 12911 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12912 if (attr)
4fae6e18
JK
12913 low = dwarf2_get_attr_constant_value (attr, low);
12914 else if (!low_default_is_valid)
12915 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12916 "- DIE at 0x%x [in module %s]"),
12917 die->offset.sect_off, cu->objfile->name);
a02abb62 12918
e142c38c 12919 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12920 if (attr)
6e70227d 12921 {
d48323d8 12922 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12923 {
12924 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12925 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12926 FIXME: GDB does not yet know how to handle dynamic
12927 arrays properly, treat them as arrays with unspecified
12928 length for now.
12929
12930 FIXME: jimb/2003-09-22: GDB does not really know
12931 how to handle arrays of unspecified length
12932 either; we just represent them as zero-length
12933 arrays. Choose an appropriate upper bound given
12934 the lower bound we've computed above. */
12935 high = low - 1;
12936 }
12937 else
12938 high = dwarf2_get_attr_constant_value (attr, 1);
12939 }
e77813c8
PM
12940 else
12941 {
12942 attr = dwarf2_attr (die, DW_AT_count, cu);
12943 if (attr)
12944 {
12945 int count = dwarf2_get_attr_constant_value (attr, 1);
12946 high = low + count - 1;
12947 }
c2ff108b
JK
12948 else
12949 {
12950 /* Unspecified array length. */
12951 high = low - 1;
12952 }
e77813c8
PM
12953 }
12954
12955 /* Dwarf-2 specifications explicitly allows to create subrange types
12956 without specifying a base type.
12957 In that case, the base type must be set to the type of
12958 the lower bound, upper bound or count, in that order, if any of these
12959 three attributes references an object that has a type.
12960 If no base type is found, the Dwarf-2 specifications say that
12961 a signed integer type of size equal to the size of an address should
12962 be used.
12963 For the following C code: `extern char gdb_int [];'
12964 GCC produces an empty range DIE.
12965 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12966 high bound or count are not yet handled by this code. */
e77813c8
PM
12967 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12968 {
12969 struct objfile *objfile = cu->objfile;
12970 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12971 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12972 struct type *int_type = objfile_type (objfile)->builtin_int;
12973
12974 /* Test "int", "long int", and "long long int" objfile types,
12975 and select the first one having a size above or equal to the
12976 architecture address size. */
12977 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12978 base_type = int_type;
12979 else
12980 {
12981 int_type = objfile_type (objfile)->builtin_long;
12982 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12983 base_type = int_type;
12984 else
12985 {
12986 int_type = objfile_type (objfile)->builtin_long_long;
12987 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12988 base_type = int_type;
12989 }
12990 }
12991 }
a02abb62 12992
6e70227d 12993 negative_mask =
43bbcdc2
PH
12994 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12995 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12996 low |= negative_mask;
12997 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12998 high |= negative_mask;
12999
4c9ad8c2 13000 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13001
bbb0eef6
JK
13002 /* Mark arrays with dynamic length at least as an array of unspecified
13003 length. GDB could check the boundary but before it gets implemented at
13004 least allow accessing the array elements. */
d48323d8 13005 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13006 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13007
c2ff108b
JK
13008 /* Ada expects an empty array on no boundary attributes. */
13009 if (attr == NULL && cu->language != language_ada)
13010 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13011
39cbfefa
DJ
13012 name = dwarf2_name (die, cu);
13013 if (name)
13014 TYPE_NAME (range_type) = name;
6e70227d 13015
e142c38c 13016 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13017 if (attr)
13018 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13019
7e314c57
JK
13020 set_die_type (die, range_type, cu);
13021
13022 /* set_die_type should be already done. */
b4ba55a1
JB
13023 set_descriptive_type (range_type, die, cu);
13024
7e314c57 13025 return range_type;
a02abb62 13026}
6e70227d 13027
f792889a 13028static struct type *
81a17f79
JB
13029read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13030{
13031 struct type *type;
81a17f79 13032
81a17f79
JB
13033 /* For now, we only support the C meaning of an unspecified type: void. */
13034
0114d602
DJ
13035 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13036 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13037
f792889a 13038 return set_die_type (die, type, cu);
81a17f79 13039}
a02abb62 13040
639d11d3
DC
13041/* Read a single die and all its descendents. Set the die's sibling
13042 field to NULL; set other fields in the die correctly, and set all
13043 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13044 location of the info_ptr after reading all of those dies. PARENT
13045 is the parent of the die in question. */
13046
13047static struct die_info *
dee91e82
DE
13048read_die_and_children (const struct die_reader_specs *reader,
13049 gdb_byte *info_ptr,
13050 gdb_byte **new_info_ptr,
13051 struct die_info *parent)
639d11d3
DC
13052{
13053 struct die_info *die;
fe1b8b76 13054 gdb_byte *cur_ptr;
639d11d3
DC
13055 int has_children;
13056
93311388 13057 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
13058 if (die == NULL)
13059 {
13060 *new_info_ptr = cur_ptr;
13061 return NULL;
13062 }
93311388 13063 store_in_ref_table (die, reader->cu);
639d11d3
DC
13064
13065 if (has_children)
348e048f 13066 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13067 else
13068 {
13069 die->child = NULL;
13070 *new_info_ptr = cur_ptr;
13071 }
13072
13073 die->sibling = NULL;
13074 die->parent = parent;
13075 return die;
13076}
13077
13078/* Read a die, all of its descendents, and all of its siblings; set
13079 all of the fields of all of the dies correctly. Arguments are as
13080 in read_die_and_children. */
13081
13082static struct die_info *
93311388
DE
13083read_die_and_siblings (const struct die_reader_specs *reader,
13084 gdb_byte *info_ptr,
fe1b8b76 13085 gdb_byte **new_info_ptr,
639d11d3
DC
13086 struct die_info *parent)
13087{
13088 struct die_info *first_die, *last_sibling;
fe1b8b76 13089 gdb_byte *cur_ptr;
639d11d3 13090
c906108c 13091 cur_ptr = info_ptr;
639d11d3
DC
13092 first_die = last_sibling = NULL;
13093
13094 while (1)
c906108c 13095 {
639d11d3 13096 struct die_info *die
dee91e82 13097 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13098
1d325ec1 13099 if (die == NULL)
c906108c 13100 {
639d11d3
DC
13101 *new_info_ptr = cur_ptr;
13102 return first_die;
c906108c 13103 }
1d325ec1
DJ
13104
13105 if (!first_die)
13106 first_die = die;
c906108c 13107 else
1d325ec1
DJ
13108 last_sibling->sibling = die;
13109
13110 last_sibling = die;
c906108c 13111 }
c906108c
SS
13112}
13113
3019eac3
DE
13114/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13115 attributes.
13116 The caller is responsible for filling in the extra attributes
13117 and updating (*DIEP)->num_attrs.
13118 Set DIEP to point to a newly allocated die with its information,
13119 except for its child, sibling, and parent fields.
13120 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
13121
13122static gdb_byte *
3019eac3
DE
13123read_full_die_1 (const struct die_reader_specs *reader,
13124 struct die_info **diep, gdb_byte *info_ptr,
13125 int *has_children, int num_extra_attrs)
93311388 13126{
b64f50a1
JK
13127 unsigned int abbrev_number, bytes_read, i;
13128 sect_offset offset;
93311388
DE
13129 struct abbrev_info *abbrev;
13130 struct die_info *die;
13131 struct dwarf2_cu *cu = reader->cu;
13132 bfd *abfd = reader->abfd;
13133
b64f50a1 13134 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13135 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13136 info_ptr += bytes_read;
13137 if (!abbrev_number)
13138 {
13139 *diep = NULL;
13140 *has_children = 0;
13141 return info_ptr;
13142 }
13143
433df2d4 13144 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13145 if (!abbrev)
348e048f
DE
13146 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13147 abbrev_number,
13148 bfd_get_filename (abfd));
13149
3019eac3 13150 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13151 die->offset = offset;
13152 die->tag = abbrev->tag;
13153 die->abbrev = abbrev_number;
13154
3019eac3
DE
13155 /* Make the result usable.
13156 The caller needs to update num_attrs after adding the extra
13157 attributes. */
93311388
DE
13158 die->num_attrs = abbrev->num_attrs;
13159
13160 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13161 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13162 info_ptr);
93311388
DE
13163
13164 *diep = die;
13165 *has_children = abbrev->has_children;
13166 return info_ptr;
13167}
13168
3019eac3
DE
13169/* Read a die and all its attributes.
13170 Set DIEP to point to a newly allocated die with its information,
13171 except for its child, sibling, and parent fields.
13172 Set HAS_CHILDREN to tell whether the die has children or not. */
13173
13174static gdb_byte *
13175read_full_die (const struct die_reader_specs *reader,
13176 struct die_info **diep, gdb_byte *info_ptr,
13177 int *has_children)
13178{
13179 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13180}
433df2d4
DE
13181\f
13182/* Abbreviation tables.
3019eac3 13183
433df2d4 13184 In DWARF version 2, the description of the debugging information is
c906108c
SS
13185 stored in a separate .debug_abbrev section. Before we read any
13186 dies from a section we read in all abbreviations and install them
433df2d4
DE
13187 in a hash table. */
13188
13189/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13190
13191static struct abbrev_info *
13192abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13193{
13194 struct abbrev_info *abbrev;
13195
13196 abbrev = (struct abbrev_info *)
13197 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13198 memset (abbrev, 0, sizeof (struct abbrev_info));
13199 return abbrev;
13200}
13201
13202/* Add an abbreviation to the table. */
c906108c
SS
13203
13204static void
433df2d4
DE
13205abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13206 unsigned int abbrev_number,
13207 struct abbrev_info *abbrev)
13208{
13209 unsigned int hash_number;
13210
13211 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13212 abbrev->next = abbrev_table->abbrevs[hash_number];
13213 abbrev_table->abbrevs[hash_number] = abbrev;
13214}
dee91e82 13215
433df2d4
DE
13216/* Look up an abbrev in the table.
13217 Returns NULL if the abbrev is not found. */
13218
13219static struct abbrev_info *
13220abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13221 unsigned int abbrev_number)
c906108c 13222{
433df2d4
DE
13223 unsigned int hash_number;
13224 struct abbrev_info *abbrev;
13225
13226 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13227 abbrev = abbrev_table->abbrevs[hash_number];
13228
13229 while (abbrev)
13230 {
13231 if (abbrev->number == abbrev_number)
13232 return abbrev;
13233 abbrev = abbrev->next;
13234 }
13235 return NULL;
13236}
13237
13238/* Read in an abbrev table. */
13239
13240static struct abbrev_table *
13241abbrev_table_read_table (struct dwarf2_section_info *section,
13242 sect_offset offset)
13243{
13244 struct objfile *objfile = dwarf2_per_objfile->objfile;
13245 bfd *abfd = section->asection->owner;
13246 struct abbrev_table *abbrev_table;
fe1b8b76 13247 gdb_byte *abbrev_ptr;
c906108c
SS
13248 struct abbrev_info *cur_abbrev;
13249 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13250 unsigned int abbrev_form;
f3dd6933
DJ
13251 struct attr_abbrev *cur_attrs;
13252 unsigned int allocated_attrs;
c906108c 13253
433df2d4 13254 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13255 abbrev_table->offset = offset;
433df2d4
DE
13256 obstack_init (&abbrev_table->abbrev_obstack);
13257 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13258 (ABBREV_HASH_SIZE
13259 * sizeof (struct abbrev_info *)));
13260 memset (abbrev_table->abbrevs, 0,
13261 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13262
433df2d4
DE
13263 dwarf2_read_section (objfile, section);
13264 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13265 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13266 abbrev_ptr += bytes_read;
13267
f3dd6933
DJ
13268 allocated_attrs = ATTR_ALLOC_CHUNK;
13269 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13270
0963b4bd 13271 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13272 while (abbrev_number)
13273 {
433df2d4 13274 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13275
13276 /* read in abbrev header */
13277 cur_abbrev->number = abbrev_number;
13278 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13279 abbrev_ptr += bytes_read;
13280 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13281 abbrev_ptr += 1;
13282
13283 /* now read in declarations */
13284 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13285 abbrev_ptr += bytes_read;
13286 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13287 abbrev_ptr += bytes_read;
13288 while (abbrev_name)
13289 {
f3dd6933 13290 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13291 {
f3dd6933
DJ
13292 allocated_attrs += ATTR_ALLOC_CHUNK;
13293 cur_attrs
13294 = xrealloc (cur_attrs, (allocated_attrs
13295 * sizeof (struct attr_abbrev)));
c906108c 13296 }
ae038cb0 13297
f3dd6933
DJ
13298 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13299 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13300 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13301 abbrev_ptr += bytes_read;
13302 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13303 abbrev_ptr += bytes_read;
13304 }
13305
433df2d4 13306 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13307 (cur_abbrev->num_attrs
13308 * sizeof (struct attr_abbrev)));
13309 memcpy (cur_abbrev->attrs, cur_attrs,
13310 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13311
433df2d4 13312 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13313
13314 /* Get next abbreviation.
13315 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13316 always properly terminated with an abbrev number of 0.
13317 Exit loop if we encounter an abbreviation which we have
13318 already read (which means we are about to read the abbreviations
13319 for the next compile unit) or if the end of the abbreviation
13320 table is reached. */
433df2d4 13321 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13322 break;
13323 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13324 abbrev_ptr += bytes_read;
433df2d4 13325 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13326 break;
13327 }
f3dd6933
DJ
13328
13329 xfree (cur_attrs);
433df2d4 13330 return abbrev_table;
c906108c
SS
13331}
13332
433df2d4 13333/* Free the resources held by ABBREV_TABLE. */
c906108c 13334
c906108c 13335static void
433df2d4 13336abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13337{
433df2d4
DE
13338 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13339 xfree (abbrev_table);
c906108c
SS
13340}
13341
f4dc4d17
DE
13342/* Same as abbrev_table_free but as a cleanup.
13343 We pass in a pointer to the pointer to the table so that we can
13344 set the pointer to NULL when we're done. It also simplifies
13345 build_type_unit_groups. */
13346
13347static void
13348abbrev_table_free_cleanup (void *table_ptr)
13349{
13350 struct abbrev_table **abbrev_table_ptr = table_ptr;
13351
13352 if (*abbrev_table_ptr != NULL)
13353 abbrev_table_free (*abbrev_table_ptr);
13354 *abbrev_table_ptr = NULL;
13355}
13356
433df2d4
DE
13357/* Read the abbrev table for CU from ABBREV_SECTION. */
13358
13359static void
13360dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13361 struct dwarf2_section_info *abbrev_section)
c906108c 13362{
433df2d4
DE
13363 cu->abbrev_table =
13364 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13365}
c906108c 13366
433df2d4 13367/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13368
433df2d4
DE
13369static void
13370dwarf2_free_abbrev_table (void *ptr_to_cu)
13371{
13372 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13373
433df2d4
DE
13374 abbrev_table_free (cu->abbrev_table);
13375 /* Set this to NULL so that we SEGV if we try to read it later,
13376 and also because free_comp_unit verifies this is NULL. */
13377 cu->abbrev_table = NULL;
13378}
13379\f
72bf9492
DJ
13380/* Returns nonzero if TAG represents a type that we might generate a partial
13381 symbol for. */
13382
13383static int
13384is_type_tag_for_partial (int tag)
13385{
13386 switch (tag)
13387 {
13388#if 0
13389 /* Some types that would be reasonable to generate partial symbols for,
13390 that we don't at present. */
13391 case DW_TAG_array_type:
13392 case DW_TAG_file_type:
13393 case DW_TAG_ptr_to_member_type:
13394 case DW_TAG_set_type:
13395 case DW_TAG_string_type:
13396 case DW_TAG_subroutine_type:
13397#endif
13398 case DW_TAG_base_type:
13399 case DW_TAG_class_type:
680b30c7 13400 case DW_TAG_interface_type:
72bf9492
DJ
13401 case DW_TAG_enumeration_type:
13402 case DW_TAG_structure_type:
13403 case DW_TAG_subrange_type:
13404 case DW_TAG_typedef:
13405 case DW_TAG_union_type:
13406 return 1;
13407 default:
13408 return 0;
13409 }
13410}
13411
13412/* Load all DIEs that are interesting for partial symbols into memory. */
13413
13414static struct partial_die_info *
dee91e82
DE
13415load_partial_dies (const struct die_reader_specs *reader,
13416 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13417{
dee91e82 13418 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13419 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13420 struct partial_die_info *part_die;
13421 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13422 struct abbrev_info *abbrev;
13423 unsigned int bytes_read;
5afb4e99 13424 unsigned int load_all = 0;
72bf9492
DJ
13425 int nesting_level = 1;
13426
13427 parent_die = NULL;
13428 last_die = NULL;
13429
7adf1e79
DE
13430 gdb_assert (cu->per_cu != NULL);
13431 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13432 load_all = 1;
13433
72bf9492
DJ
13434 cu->partial_dies
13435 = htab_create_alloc_ex (cu->header.length / 12,
13436 partial_die_hash,
13437 partial_die_eq,
13438 NULL,
13439 &cu->comp_unit_obstack,
13440 hashtab_obstack_allocate,
13441 dummy_obstack_deallocate);
13442
13443 part_die = obstack_alloc (&cu->comp_unit_obstack,
13444 sizeof (struct partial_die_info));
13445
13446 while (1)
13447 {
13448 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13449
13450 /* A NULL abbrev means the end of a series of children. */
13451 if (abbrev == NULL)
13452 {
13453 if (--nesting_level == 0)
13454 {
13455 /* PART_DIE was probably the last thing allocated on the
13456 comp_unit_obstack, so we could call obstack_free
13457 here. We don't do that because the waste is small,
13458 and will be cleaned up when we're done with this
13459 compilation unit. This way, we're also more robust
13460 against other users of the comp_unit_obstack. */
13461 return first_die;
13462 }
13463 info_ptr += bytes_read;
13464 last_die = parent_die;
13465 parent_die = parent_die->die_parent;
13466 continue;
13467 }
13468
98bfdba5
PA
13469 /* Check for template arguments. We never save these; if
13470 they're seen, we just mark the parent, and go on our way. */
13471 if (parent_die != NULL
13472 && cu->language == language_cplus
13473 && (abbrev->tag == DW_TAG_template_type_param
13474 || abbrev->tag == DW_TAG_template_value_param))
13475 {
13476 parent_die->has_template_arguments = 1;
13477
13478 if (!load_all)
13479 {
13480 /* We don't need a partial DIE for the template argument. */
dee91e82 13481 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13482 continue;
13483 }
13484 }
13485
0d99eb77 13486 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13487 Skip their other children. */
13488 if (!load_all
13489 && cu->language == language_cplus
13490 && parent_die != NULL
13491 && parent_die->tag == DW_TAG_subprogram)
13492 {
dee91e82 13493 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13494 continue;
13495 }
13496
5afb4e99
DJ
13497 /* Check whether this DIE is interesting enough to save. Normally
13498 we would not be interested in members here, but there may be
13499 later variables referencing them via DW_AT_specification (for
13500 static members). */
13501 if (!load_all
13502 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13503 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13504 && abbrev->tag != DW_TAG_enumerator
13505 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13506 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13507 && abbrev->tag != DW_TAG_variable
5afb4e99 13508 && abbrev->tag != DW_TAG_namespace
f55ee35c 13509 && abbrev->tag != DW_TAG_module
95554aad
TT
13510 && abbrev->tag != DW_TAG_member
13511 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13512 {
13513 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13514 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13515 continue;
13516 }
13517
dee91e82
DE
13518 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13519 info_ptr);
72bf9492
DJ
13520
13521 /* This two-pass algorithm for processing partial symbols has a
13522 high cost in cache pressure. Thus, handle some simple cases
13523 here which cover the majority of C partial symbols. DIEs
13524 which neither have specification tags in them, nor could have
13525 specification tags elsewhere pointing at them, can simply be
13526 processed and discarded.
13527
13528 This segment is also optional; scan_partial_symbols and
13529 add_partial_symbol will handle these DIEs if we chain
13530 them in normally. When compilers which do not emit large
13531 quantities of duplicate debug information are more common,
13532 this code can probably be removed. */
13533
13534 /* Any complete simple types at the top level (pretty much all
13535 of them, for a language without namespaces), can be processed
13536 directly. */
13537 if (parent_die == NULL
13538 && part_die->has_specification == 0
13539 && part_die->is_declaration == 0
d8228535 13540 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13541 || part_die->tag == DW_TAG_base_type
13542 || part_die->tag == DW_TAG_subrange_type))
13543 {
13544 if (building_psymtab && part_die->name != NULL)
04a679b8 13545 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13546 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13547 &objfile->static_psymbols,
13548 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13549 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13550 continue;
13551 }
13552
d8228535
JK
13553 /* The exception for DW_TAG_typedef with has_children above is
13554 a workaround of GCC PR debug/47510. In the case of this complaint
13555 type_name_no_tag_or_error will error on such types later.
13556
13557 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13558 it could not find the child DIEs referenced later, this is checked
13559 above. In correct DWARF DW_TAG_typedef should have no children. */
13560
13561 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13562 complaint (&symfile_complaints,
13563 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13564 "- DIE at 0x%x [in module %s]"),
b64f50a1 13565 part_die->offset.sect_off, objfile->name);
d8228535 13566
72bf9492
DJ
13567 /* If we're at the second level, and we're an enumerator, and
13568 our parent has no specification (meaning possibly lives in a
13569 namespace elsewhere), then we can add the partial symbol now
13570 instead of queueing it. */
13571 if (part_die->tag == DW_TAG_enumerator
13572 && parent_die != NULL
13573 && parent_die->die_parent == NULL
13574 && parent_die->tag == DW_TAG_enumeration_type
13575 && parent_die->has_specification == 0)
13576 {
13577 if (part_die->name == NULL)
3e43a32a
MS
13578 complaint (&symfile_complaints,
13579 _("malformed enumerator DIE ignored"));
72bf9492 13580 else if (building_psymtab)
04a679b8 13581 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13582 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13583 (cu->language == language_cplus
13584 || cu->language == language_java)
bb5ed363
DE
13585 ? &objfile->global_psymbols
13586 : &objfile->static_psymbols,
13587 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13588
dee91e82 13589 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13590 continue;
13591 }
13592
13593 /* We'll save this DIE so link it in. */
13594 part_die->die_parent = parent_die;
13595 part_die->die_sibling = NULL;
13596 part_die->die_child = NULL;
13597
13598 if (last_die && last_die == parent_die)
13599 last_die->die_child = part_die;
13600 else if (last_die)
13601 last_die->die_sibling = part_die;
13602
13603 last_die = part_die;
13604
13605 if (first_die == NULL)
13606 first_die = part_die;
13607
13608 /* Maybe add the DIE to the hash table. Not all DIEs that we
13609 find interesting need to be in the hash table, because we
13610 also have the parent/sibling/child chains; only those that we
13611 might refer to by offset later during partial symbol reading.
13612
13613 For now this means things that might have be the target of a
13614 DW_AT_specification, DW_AT_abstract_origin, or
13615 DW_AT_extension. DW_AT_extension will refer only to
13616 namespaces; DW_AT_abstract_origin refers to functions (and
13617 many things under the function DIE, but we do not recurse
13618 into function DIEs during partial symbol reading) and
13619 possibly variables as well; DW_AT_specification refers to
13620 declarations. Declarations ought to have the DW_AT_declaration
13621 flag. It happens that GCC forgets to put it in sometimes, but
13622 only for functions, not for types.
13623
13624 Adding more things than necessary to the hash table is harmless
13625 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13626 wasted time in find_partial_die, when we reread the compilation
13627 unit with load_all_dies set. */
72bf9492 13628
5afb4e99 13629 if (load_all
72929c62 13630 || abbrev->tag == DW_TAG_constant
5afb4e99 13631 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13632 || abbrev->tag == DW_TAG_variable
13633 || abbrev->tag == DW_TAG_namespace
13634 || part_die->is_declaration)
13635 {
13636 void **slot;
13637
13638 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13639 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13640 *slot = part_die;
13641 }
13642
13643 part_die = obstack_alloc (&cu->comp_unit_obstack,
13644 sizeof (struct partial_die_info));
13645
13646 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13647 we have no reason to follow the children of structures; for other
98bfdba5
PA
13648 languages we have to, so that we can get at method physnames
13649 to infer fully qualified class names, for DW_AT_specification,
13650 and for C++ template arguments. For C++, we also look one level
13651 inside functions to find template arguments (if the name of the
13652 function does not already contain the template arguments).
bc30ff58
JB
13653
13654 For Ada, we need to scan the children of subprograms and lexical
13655 blocks as well because Ada allows the definition of nested
13656 entities that could be interesting for the debugger, such as
13657 nested subprograms for instance. */
72bf9492 13658 if (last_die->has_children
5afb4e99
DJ
13659 && (load_all
13660 || last_die->tag == DW_TAG_namespace
f55ee35c 13661 || last_die->tag == DW_TAG_module
72bf9492 13662 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13663 || (cu->language == language_cplus
13664 && last_die->tag == DW_TAG_subprogram
13665 && (last_die->name == NULL
13666 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13667 || (cu->language != language_c
13668 && (last_die->tag == DW_TAG_class_type
680b30c7 13669 || last_die->tag == DW_TAG_interface_type
72bf9492 13670 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13671 || last_die->tag == DW_TAG_union_type))
13672 || (cu->language == language_ada
13673 && (last_die->tag == DW_TAG_subprogram
13674 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13675 {
13676 nesting_level++;
13677 parent_die = last_die;
13678 continue;
13679 }
13680
13681 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13682 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13683
13684 /* Back to the top, do it again. */
13685 }
13686}
13687
c906108c
SS
13688/* Read a minimal amount of information into the minimal die structure. */
13689
fe1b8b76 13690static gdb_byte *
dee91e82
DE
13691read_partial_die (const struct die_reader_specs *reader,
13692 struct partial_die_info *part_die,
13693 struct abbrev_info *abbrev, unsigned int abbrev_len,
13694 gdb_byte *info_ptr)
c906108c 13695{
dee91e82 13696 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13697 struct objfile *objfile = cu->objfile;
dee91e82 13698 gdb_byte *buffer = reader->buffer;
fa238c03 13699 unsigned int i;
c906108c 13700 struct attribute attr;
c5aa993b 13701 int has_low_pc_attr = 0;
c906108c 13702 int has_high_pc_attr = 0;
91da1414 13703 int high_pc_relative = 0;
c906108c 13704
72bf9492 13705 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13706
b64f50a1 13707 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13708
13709 info_ptr += abbrev_len;
13710
13711 if (abbrev == NULL)
13712 return info_ptr;
13713
c906108c
SS
13714 part_die->tag = abbrev->tag;
13715 part_die->has_children = abbrev->has_children;
c906108c
SS
13716
13717 for (i = 0; i < abbrev->num_attrs; ++i)
13718 {
dee91e82 13719 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13720
13721 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13722 partial symbol table. */
c906108c
SS
13723 switch (attr.name)
13724 {
13725 case DW_AT_name:
71c25dea
TT
13726 switch (part_die->tag)
13727 {
13728 case DW_TAG_compile_unit:
95554aad 13729 case DW_TAG_partial_unit:
348e048f 13730 case DW_TAG_type_unit:
71c25dea
TT
13731 /* Compilation units have a DW_AT_name that is a filename, not
13732 a source language identifier. */
13733 case DW_TAG_enumeration_type:
13734 case DW_TAG_enumerator:
13735 /* These tags always have simple identifiers already; no need
13736 to canonicalize them. */
13737 part_die->name = DW_STRING (&attr);
13738 break;
13739 default:
13740 part_die->name
13741 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13742 &objfile->objfile_obstack);
71c25dea
TT
13743 break;
13744 }
c906108c 13745 break;
31ef98ae 13746 case DW_AT_linkage_name:
c906108c 13747 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13748 /* Note that both forms of linkage name might appear. We
13749 assume they will be the same, and we only store the last
13750 one we see. */
94af9270
KS
13751 if (cu->language == language_ada)
13752 part_die->name = DW_STRING (&attr);
abc72ce4 13753 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13754 break;
13755 case DW_AT_low_pc:
13756 has_low_pc_attr = 1;
13757 part_die->lowpc = DW_ADDR (&attr);
13758 break;
13759 case DW_AT_high_pc:
13760 has_high_pc_attr = 1;
3019eac3
DE
13761 if (attr.form == DW_FORM_addr
13762 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13763 part_die->highpc = DW_ADDR (&attr);
13764 else
13765 {
13766 high_pc_relative = 1;
13767 part_die->highpc = DW_UNSND (&attr);
13768 }
c906108c
SS
13769 break;
13770 case DW_AT_location:
0963b4bd 13771 /* Support the .debug_loc offsets. */
8e19ed76
PS
13772 if (attr_form_is_block (&attr))
13773 {
95554aad 13774 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13775 }
3690dd37 13776 else if (attr_form_is_section_offset (&attr))
8e19ed76 13777 {
4d3c2250 13778 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13779 }
13780 else
13781 {
4d3c2250
KB
13782 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13783 "partial symbol information");
8e19ed76 13784 }
c906108c 13785 break;
c906108c
SS
13786 case DW_AT_external:
13787 part_die->is_external = DW_UNSND (&attr);
13788 break;
13789 case DW_AT_declaration:
13790 part_die->is_declaration = DW_UNSND (&attr);
13791 break;
13792 case DW_AT_type:
13793 part_die->has_type = 1;
13794 break;
13795 case DW_AT_abstract_origin:
13796 case DW_AT_specification:
72bf9492
DJ
13797 case DW_AT_extension:
13798 part_die->has_specification = 1;
c764a876 13799 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13800 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13801 || cu->per_cu->is_dwz);
c906108c
SS
13802 break;
13803 case DW_AT_sibling:
13804 /* Ignore absolute siblings, they might point outside of
13805 the current compile unit. */
13806 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13807 complaint (&symfile_complaints,
13808 _("ignoring absolute DW_AT_sibling"));
c906108c 13809 else
b64f50a1 13810 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13811 break;
fa4028e9
JB
13812 case DW_AT_byte_size:
13813 part_die->has_byte_size = 1;
13814 break;
68511cec
CES
13815 case DW_AT_calling_convention:
13816 /* DWARF doesn't provide a way to identify a program's source-level
13817 entry point. DW_AT_calling_convention attributes are only meant
13818 to describe functions' calling conventions.
13819
13820 However, because it's a necessary piece of information in
13821 Fortran, and because DW_CC_program is the only piece of debugging
13822 information whose definition refers to a 'main program' at all,
13823 several compilers have begun marking Fortran main programs with
13824 DW_CC_program --- even when those functions use the standard
13825 calling conventions.
13826
13827 So until DWARF specifies a way to provide this information and
13828 compilers pick up the new representation, we'll support this
13829 practice. */
13830 if (DW_UNSND (&attr) == DW_CC_program
13831 && cu->language == language_fortran)
01f8c46d
JK
13832 {
13833 set_main_name (part_die->name);
13834
13835 /* As this DIE has a static linkage the name would be difficult
13836 to look up later. */
13837 language_of_main = language_fortran;
13838 }
68511cec 13839 break;
481860b3
GB
13840 case DW_AT_inline:
13841 if (DW_UNSND (&attr) == DW_INL_inlined
13842 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13843 part_die->may_be_inlined = 1;
13844 break;
95554aad
TT
13845
13846 case DW_AT_import:
13847 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13848 {
13849 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13850 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13851 || cu->per_cu->is_dwz);
13852 }
95554aad
TT
13853 break;
13854
c906108c
SS
13855 default:
13856 break;
13857 }
13858 }
13859
91da1414
MW
13860 if (high_pc_relative)
13861 part_die->highpc += part_die->lowpc;
13862
9373cf26
JK
13863 if (has_low_pc_attr && has_high_pc_attr)
13864 {
13865 /* When using the GNU linker, .gnu.linkonce. sections are used to
13866 eliminate duplicate copies of functions and vtables and such.
13867 The linker will arbitrarily choose one and discard the others.
13868 The AT_*_pc values for such functions refer to local labels in
13869 these sections. If the section from that file was discarded, the
13870 labels are not in the output, so the relocs get a value of 0.
13871 If this is a discarded function, mark the pc bounds as invalid,
13872 so that GDB will ignore it. */
13873 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13874 {
bb5ed363 13875 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13876
13877 complaint (&symfile_complaints,
13878 _("DW_AT_low_pc %s is zero "
13879 "for DIE at 0x%x [in module %s]"),
13880 paddress (gdbarch, part_die->lowpc),
b64f50a1 13881 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13882 }
13883 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13884 else if (part_die->lowpc >= part_die->highpc)
13885 {
bb5ed363 13886 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13887
13888 complaint (&symfile_complaints,
13889 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13890 "for DIE at 0x%x [in module %s]"),
13891 paddress (gdbarch, part_die->lowpc),
13892 paddress (gdbarch, part_die->highpc),
b64f50a1 13893 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13894 }
13895 else
13896 part_die->has_pc_info = 1;
13897 }
85cbf3d3 13898
c906108c
SS
13899 return info_ptr;
13900}
13901
72bf9492
DJ
13902/* Find a cached partial DIE at OFFSET in CU. */
13903
13904static struct partial_die_info *
b64f50a1 13905find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13906{
13907 struct partial_die_info *lookup_die = NULL;
13908 struct partial_die_info part_die;
13909
13910 part_die.offset = offset;
b64f50a1
JK
13911 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13912 offset.sect_off);
72bf9492 13913
72bf9492
DJ
13914 return lookup_die;
13915}
13916
348e048f
DE
13917/* Find a partial DIE at OFFSET, which may or may not be in CU,
13918 except in the case of .debug_types DIEs which do not reference
13919 outside their CU (they do however referencing other types via
55f1336d 13920 DW_FORM_ref_sig8). */
72bf9492
DJ
13921
13922static struct partial_die_info *
36586728 13923find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13924{
bb5ed363 13925 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13926 struct dwarf2_per_cu_data *per_cu = NULL;
13927 struct partial_die_info *pd = NULL;
72bf9492 13928
36586728
TT
13929 if (offset_in_dwz == cu->per_cu->is_dwz
13930 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13931 {
13932 pd = find_partial_die_in_comp_unit (offset, cu);
13933 if (pd != NULL)
13934 return pd;
0d99eb77
DE
13935 /* We missed recording what we needed.
13936 Load all dies and try again. */
13937 per_cu = cu->per_cu;
5afb4e99 13938 }
0d99eb77
DE
13939 else
13940 {
13941 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13942 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13943 {
13944 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13945 " external reference to offset 0x%lx [in module %s].\n"),
13946 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13947 bfd_get_filename (objfile->obfd));
13948 }
36586728
TT
13949 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13950 objfile);
72bf9492 13951
0d99eb77
DE
13952 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13953 load_partial_comp_unit (per_cu);
ae038cb0 13954
0d99eb77
DE
13955 per_cu->cu->last_used = 0;
13956 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13957 }
5afb4e99 13958
dee91e82
DE
13959 /* If we didn't find it, and not all dies have been loaded,
13960 load them all and try again. */
13961
5afb4e99
DJ
13962 if (pd == NULL && per_cu->load_all_dies == 0)
13963 {
5afb4e99 13964 per_cu->load_all_dies = 1;
fd820528
DE
13965
13966 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13967 THIS_CU->cu may already be in use. So we can't just free it and
13968 replace its DIEs with the ones we read in. Instead, we leave those
13969 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13970 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13971 set. */
dee91e82 13972 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13973
13974 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13975 }
13976
13977 if (pd == NULL)
13978 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13979 _("could not find partial DIE 0x%x "
13980 "in cache [from module %s]\n"),
b64f50a1 13981 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13982 return pd;
72bf9492
DJ
13983}
13984
abc72ce4
DE
13985/* See if we can figure out if the class lives in a namespace. We do
13986 this by looking for a member function; its demangled name will
13987 contain namespace info, if there is any. */
13988
13989static void
13990guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13991 struct dwarf2_cu *cu)
13992{
13993 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13994 what template types look like, because the demangler
13995 frequently doesn't give the same name as the debug info. We
13996 could fix this by only using the demangled name to get the
13997 prefix (but see comment in read_structure_type). */
13998
13999 struct partial_die_info *real_pdi;
14000 struct partial_die_info *child_pdi;
14001
14002 /* If this DIE (this DIE's specification, if any) has a parent, then
14003 we should not do this. We'll prepend the parent's fully qualified
14004 name when we create the partial symbol. */
14005
14006 real_pdi = struct_pdi;
14007 while (real_pdi->has_specification)
36586728
TT
14008 real_pdi = find_partial_die (real_pdi->spec_offset,
14009 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14010
14011 if (real_pdi->die_parent != NULL)
14012 return;
14013
14014 for (child_pdi = struct_pdi->die_child;
14015 child_pdi != NULL;
14016 child_pdi = child_pdi->die_sibling)
14017 {
14018 if (child_pdi->tag == DW_TAG_subprogram
14019 && child_pdi->linkage_name != NULL)
14020 {
14021 char *actual_class_name
14022 = language_class_name_from_physname (cu->language_defn,
14023 child_pdi->linkage_name);
14024 if (actual_class_name != NULL)
14025 {
14026 struct_pdi->name
10f0c4bb
TT
14027 = obstack_copy0 (&cu->objfile->objfile_obstack,
14028 actual_class_name,
14029 strlen (actual_class_name));
abc72ce4
DE
14030 xfree (actual_class_name);
14031 }
14032 break;
14033 }
14034 }
14035}
14036
72bf9492
DJ
14037/* Adjust PART_DIE before generating a symbol for it. This function
14038 may set the is_external flag or change the DIE's name. */
14039
14040static void
14041fixup_partial_die (struct partial_die_info *part_die,
14042 struct dwarf2_cu *cu)
14043{
abc72ce4
DE
14044 /* Once we've fixed up a die, there's no point in doing so again.
14045 This also avoids a memory leak if we were to call
14046 guess_partial_die_structure_name multiple times. */
14047 if (part_die->fixup_called)
14048 return;
14049
72bf9492
DJ
14050 /* If we found a reference attribute and the DIE has no name, try
14051 to find a name in the referred to DIE. */
14052
14053 if (part_die->name == NULL && part_die->has_specification)
14054 {
14055 struct partial_die_info *spec_die;
72bf9492 14056
36586728
TT
14057 spec_die = find_partial_die (part_die->spec_offset,
14058 part_die->spec_is_dwz, cu);
72bf9492 14059
10b3939b 14060 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14061
14062 if (spec_die->name)
14063 {
14064 part_die->name = spec_die->name;
14065
14066 /* Copy DW_AT_external attribute if it is set. */
14067 if (spec_die->is_external)
14068 part_die->is_external = spec_die->is_external;
14069 }
14070 }
14071
14072 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14073
14074 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14075 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14076
abc72ce4
DE
14077 /* If there is no parent die to provide a namespace, and there are
14078 children, see if we can determine the namespace from their linkage
122d1940 14079 name. */
abc72ce4 14080 if (cu->language == language_cplus
8b70b953 14081 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14082 && part_die->die_parent == NULL
14083 && part_die->has_children
14084 && (part_die->tag == DW_TAG_class_type
14085 || part_die->tag == DW_TAG_structure_type
14086 || part_die->tag == DW_TAG_union_type))
14087 guess_partial_die_structure_name (part_die, cu);
14088
53832f31
TT
14089 /* GCC might emit a nameless struct or union that has a linkage
14090 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14091 if (part_die->name == NULL
96408a79
SA
14092 && (part_die->tag == DW_TAG_class_type
14093 || part_die->tag == DW_TAG_interface_type
14094 || part_die->tag == DW_TAG_structure_type
14095 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14096 && part_die->linkage_name != NULL)
14097 {
14098 char *demangled;
14099
14100 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14101 if (demangled)
14102 {
96408a79
SA
14103 const char *base;
14104
14105 /* Strip any leading namespaces/classes, keep only the base name.
14106 DW_AT_name for named DIEs does not contain the prefixes. */
14107 base = strrchr (demangled, ':');
14108 if (base && base > demangled && base[-1] == ':')
14109 base++;
14110 else
14111 base = demangled;
14112
10f0c4bb
TT
14113 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14114 base, strlen (base));
53832f31
TT
14115 xfree (demangled);
14116 }
14117 }
14118
abc72ce4 14119 part_die->fixup_called = 1;
72bf9492
DJ
14120}
14121
a8329558 14122/* Read an attribute value described by an attribute form. */
c906108c 14123
fe1b8b76 14124static gdb_byte *
dee91e82
DE
14125read_attribute_value (const struct die_reader_specs *reader,
14126 struct attribute *attr, unsigned form,
14127 gdb_byte *info_ptr)
c906108c 14128{
dee91e82
DE
14129 struct dwarf2_cu *cu = reader->cu;
14130 bfd *abfd = reader->abfd;
e7c27a73 14131 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14132 unsigned int bytes_read;
14133 struct dwarf_block *blk;
14134
a8329558
KW
14135 attr->form = form;
14136 switch (form)
c906108c 14137 {
c906108c 14138 case DW_FORM_ref_addr:
ae411497 14139 if (cu->header.version == 2)
4568ecf9 14140 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14141 else
4568ecf9
DE
14142 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14143 &cu->header, &bytes_read);
ae411497
TT
14144 info_ptr += bytes_read;
14145 break;
36586728
TT
14146 case DW_FORM_GNU_ref_alt:
14147 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14148 info_ptr += bytes_read;
14149 break;
ae411497 14150 case DW_FORM_addr:
e7c27a73 14151 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14152 info_ptr += bytes_read;
c906108c
SS
14153 break;
14154 case DW_FORM_block2:
7b5a2f43 14155 blk = dwarf_alloc_block (cu);
c906108c
SS
14156 blk->size = read_2_bytes (abfd, info_ptr);
14157 info_ptr += 2;
14158 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14159 info_ptr += blk->size;
14160 DW_BLOCK (attr) = blk;
14161 break;
14162 case DW_FORM_block4:
7b5a2f43 14163 blk = dwarf_alloc_block (cu);
c906108c
SS
14164 blk->size = read_4_bytes (abfd, info_ptr);
14165 info_ptr += 4;
14166 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14167 info_ptr += blk->size;
14168 DW_BLOCK (attr) = blk;
14169 break;
14170 case DW_FORM_data2:
14171 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14172 info_ptr += 2;
14173 break;
14174 case DW_FORM_data4:
14175 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14176 info_ptr += 4;
14177 break;
14178 case DW_FORM_data8:
14179 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14180 info_ptr += 8;
14181 break;
2dc7f7b3
TT
14182 case DW_FORM_sec_offset:
14183 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14184 info_ptr += bytes_read;
14185 break;
c906108c 14186 case DW_FORM_string:
9b1c24c8 14187 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14188 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14189 info_ptr += bytes_read;
14190 break;
4bdf3d34 14191 case DW_FORM_strp:
36586728
TT
14192 if (!cu->per_cu->is_dwz)
14193 {
14194 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14195 &bytes_read);
14196 DW_STRING_IS_CANONICAL (attr) = 0;
14197 info_ptr += bytes_read;
14198 break;
14199 }
14200 /* FALLTHROUGH */
14201 case DW_FORM_GNU_strp_alt:
14202 {
14203 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14204 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14205 &bytes_read);
14206
14207 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14208 DW_STRING_IS_CANONICAL (attr) = 0;
14209 info_ptr += bytes_read;
14210 }
4bdf3d34 14211 break;
2dc7f7b3 14212 case DW_FORM_exprloc:
c906108c 14213 case DW_FORM_block:
7b5a2f43 14214 blk = dwarf_alloc_block (cu);
c906108c
SS
14215 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14216 info_ptr += bytes_read;
14217 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14218 info_ptr += blk->size;
14219 DW_BLOCK (attr) = blk;
14220 break;
14221 case DW_FORM_block1:
7b5a2f43 14222 blk = dwarf_alloc_block (cu);
c906108c
SS
14223 blk->size = read_1_byte (abfd, info_ptr);
14224 info_ptr += 1;
14225 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14226 info_ptr += blk->size;
14227 DW_BLOCK (attr) = blk;
14228 break;
14229 case DW_FORM_data1:
14230 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14231 info_ptr += 1;
14232 break;
14233 case DW_FORM_flag:
14234 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14235 info_ptr += 1;
14236 break;
2dc7f7b3
TT
14237 case DW_FORM_flag_present:
14238 DW_UNSND (attr) = 1;
14239 break;
c906108c
SS
14240 case DW_FORM_sdata:
14241 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14242 info_ptr += bytes_read;
14243 break;
14244 case DW_FORM_udata:
14245 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14246 info_ptr += bytes_read;
14247 break;
14248 case DW_FORM_ref1:
4568ecf9
DE
14249 DW_UNSND (attr) = (cu->header.offset.sect_off
14250 + read_1_byte (abfd, info_ptr));
c906108c
SS
14251 info_ptr += 1;
14252 break;
14253 case DW_FORM_ref2:
4568ecf9
DE
14254 DW_UNSND (attr) = (cu->header.offset.sect_off
14255 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14256 info_ptr += 2;
14257 break;
14258 case DW_FORM_ref4:
4568ecf9
DE
14259 DW_UNSND (attr) = (cu->header.offset.sect_off
14260 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14261 info_ptr += 4;
14262 break;
613e1657 14263 case DW_FORM_ref8:
4568ecf9
DE
14264 DW_UNSND (attr) = (cu->header.offset.sect_off
14265 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14266 info_ptr += 8;
14267 break;
55f1336d 14268 case DW_FORM_ref_sig8:
348e048f
DE
14269 /* Convert the signature to something we can record in DW_UNSND
14270 for later lookup.
14271 NOTE: This is NULL if the type wasn't found. */
14272 DW_SIGNATURED_TYPE (attr) =
e319fa28 14273 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14274 info_ptr += 8;
14275 break;
c906108c 14276 case DW_FORM_ref_udata:
4568ecf9
DE
14277 DW_UNSND (attr) = (cu->header.offset.sect_off
14278 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14279 info_ptr += bytes_read;
14280 break;
c906108c 14281 case DW_FORM_indirect:
a8329558
KW
14282 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14283 info_ptr += bytes_read;
dee91e82 14284 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14285 break;
3019eac3
DE
14286 case DW_FORM_GNU_addr_index:
14287 if (reader->dwo_file == NULL)
14288 {
14289 /* For now flag a hard error.
14290 Later we can turn this into a complaint. */
14291 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14292 dwarf_form_name (form),
14293 bfd_get_filename (abfd));
14294 }
14295 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14296 info_ptr += bytes_read;
14297 break;
14298 case DW_FORM_GNU_str_index:
14299 if (reader->dwo_file == NULL)
14300 {
14301 /* For now flag a hard error.
14302 Later we can turn this into a complaint if warranted. */
14303 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14304 dwarf_form_name (form),
14305 bfd_get_filename (abfd));
14306 }
14307 {
14308 ULONGEST str_index =
14309 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14310
14311 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14312 DW_STRING_IS_CANONICAL (attr) = 0;
14313 info_ptr += bytes_read;
14314 }
14315 break;
c906108c 14316 default:
8a3fe4f8 14317 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14318 dwarf_form_name (form),
14319 bfd_get_filename (abfd));
c906108c 14320 }
28e94949 14321
36586728
TT
14322 /* Super hack. */
14323 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14324 attr->form = DW_FORM_GNU_ref_alt;
14325
28e94949
JB
14326 /* We have seen instances where the compiler tried to emit a byte
14327 size attribute of -1 which ended up being encoded as an unsigned
14328 0xffffffff. Although 0xffffffff is technically a valid size value,
14329 an object of this size seems pretty unlikely so we can relatively
14330 safely treat these cases as if the size attribute was invalid and
14331 treat them as zero by default. */
14332 if (attr->name == DW_AT_byte_size
14333 && form == DW_FORM_data4
14334 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14335 {
14336 complaint
14337 (&symfile_complaints,
43bbcdc2
PH
14338 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14339 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14340 DW_UNSND (attr) = 0;
14341 }
28e94949 14342
c906108c
SS
14343 return info_ptr;
14344}
14345
a8329558
KW
14346/* Read an attribute described by an abbreviated attribute. */
14347
fe1b8b76 14348static gdb_byte *
dee91e82
DE
14349read_attribute (const struct die_reader_specs *reader,
14350 struct attribute *attr, struct attr_abbrev *abbrev,
14351 gdb_byte *info_ptr)
a8329558
KW
14352{
14353 attr->name = abbrev->name;
dee91e82 14354 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14355}
14356
0963b4bd 14357/* Read dwarf information from a buffer. */
c906108c
SS
14358
14359static unsigned int
a1855c1d 14360read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14361{
fe1b8b76 14362 return bfd_get_8 (abfd, buf);
c906108c
SS
14363}
14364
14365static int
a1855c1d 14366read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14367{
fe1b8b76 14368 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14369}
14370
14371static unsigned int
a1855c1d 14372read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14373{
fe1b8b76 14374 return bfd_get_16 (abfd, buf);
c906108c
SS
14375}
14376
21ae7a4d 14377static int
a1855c1d 14378read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14379{
14380 return bfd_get_signed_16 (abfd, buf);
14381}
14382
c906108c 14383static unsigned int
a1855c1d 14384read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14385{
fe1b8b76 14386 return bfd_get_32 (abfd, buf);
c906108c
SS
14387}
14388
21ae7a4d 14389static int
a1855c1d 14390read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14391{
14392 return bfd_get_signed_32 (abfd, buf);
14393}
14394
93311388 14395static ULONGEST
a1855c1d 14396read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14397{
fe1b8b76 14398 return bfd_get_64 (abfd, buf);
c906108c
SS
14399}
14400
14401static CORE_ADDR
fe1b8b76 14402read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14403 unsigned int *bytes_read)
c906108c 14404{
e7c27a73 14405 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14406 CORE_ADDR retval = 0;
14407
107d2387 14408 if (cu_header->signed_addr_p)
c906108c 14409 {
107d2387
AC
14410 switch (cu_header->addr_size)
14411 {
14412 case 2:
fe1b8b76 14413 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14414 break;
14415 case 4:
fe1b8b76 14416 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14417 break;
14418 case 8:
fe1b8b76 14419 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14420 break;
14421 default:
8e65ff28 14422 internal_error (__FILE__, __LINE__,
e2e0b3e5 14423 _("read_address: bad switch, signed [in module %s]"),
659b0389 14424 bfd_get_filename (abfd));
107d2387
AC
14425 }
14426 }
14427 else
14428 {
14429 switch (cu_header->addr_size)
14430 {
14431 case 2:
fe1b8b76 14432 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14433 break;
14434 case 4:
fe1b8b76 14435 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14436 break;
14437 case 8:
fe1b8b76 14438 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14439 break;
14440 default:
8e65ff28 14441 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14442 _("read_address: bad switch, "
14443 "unsigned [in module %s]"),
659b0389 14444 bfd_get_filename (abfd));
107d2387 14445 }
c906108c 14446 }
64367e0a 14447
107d2387
AC
14448 *bytes_read = cu_header->addr_size;
14449 return retval;
c906108c
SS
14450}
14451
f7ef9339 14452/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14453 specification allows the initial length to take up either 4 bytes
14454 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14455 bytes describe the length and all offsets will be 8 bytes in length
14456 instead of 4.
14457
f7ef9339
KB
14458 An older, non-standard 64-bit format is also handled by this
14459 function. The older format in question stores the initial length
14460 as an 8-byte quantity without an escape value. Lengths greater
14461 than 2^32 aren't very common which means that the initial 4 bytes
14462 is almost always zero. Since a length value of zero doesn't make
14463 sense for the 32-bit format, this initial zero can be considered to
14464 be an escape value which indicates the presence of the older 64-bit
14465 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14466 greater than 4GB. If it becomes necessary to handle lengths
14467 somewhat larger than 4GB, we could allow other small values (such
14468 as the non-sensical values of 1, 2, and 3) to also be used as
14469 escape values indicating the presence of the old format.
f7ef9339 14470
917c78fc
MK
14471 The value returned via bytes_read should be used to increment the
14472 relevant pointer after calling read_initial_length().
c764a876 14473
613e1657
KB
14474 [ Note: read_initial_length() and read_offset() are based on the
14475 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14476 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14477 from:
14478
f7ef9339 14479 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14480
613e1657
KB
14481 This document is only a draft and is subject to change. (So beware.)
14482
f7ef9339 14483 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14484 determined empirically by examining 64-bit ELF files produced by
14485 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14486
14487 - Kevin, July 16, 2002
613e1657
KB
14488 ] */
14489
14490static LONGEST
c764a876 14491read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14492{
fe1b8b76 14493 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14494
dd373385 14495 if (length == 0xffffffff)
613e1657 14496 {
fe1b8b76 14497 length = bfd_get_64 (abfd, buf + 4);
613e1657 14498 *bytes_read = 12;
613e1657 14499 }
dd373385 14500 else if (length == 0)
f7ef9339 14501 {
dd373385 14502 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14503 length = bfd_get_64 (abfd, buf);
f7ef9339 14504 *bytes_read = 8;
f7ef9339 14505 }
613e1657
KB
14506 else
14507 {
14508 *bytes_read = 4;
613e1657
KB
14509 }
14510
c764a876
DE
14511 return length;
14512}
dd373385 14513
c764a876
DE
14514/* Cover function for read_initial_length.
14515 Returns the length of the object at BUF, and stores the size of the
14516 initial length in *BYTES_READ and stores the size that offsets will be in
14517 *OFFSET_SIZE.
14518 If the initial length size is not equivalent to that specified in
14519 CU_HEADER then issue a complaint.
14520 This is useful when reading non-comp-unit headers. */
dd373385 14521
c764a876
DE
14522static LONGEST
14523read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14524 const struct comp_unit_head *cu_header,
14525 unsigned int *bytes_read,
14526 unsigned int *offset_size)
14527{
14528 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14529
14530 gdb_assert (cu_header->initial_length_size == 4
14531 || cu_header->initial_length_size == 8
14532 || cu_header->initial_length_size == 12);
14533
14534 if (cu_header->initial_length_size != *bytes_read)
14535 complaint (&symfile_complaints,
14536 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14537
c764a876 14538 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14539 return length;
613e1657
KB
14540}
14541
14542/* Read an offset from the data stream. The size of the offset is
917c78fc 14543 given by cu_header->offset_size. */
613e1657
KB
14544
14545static LONGEST
fe1b8b76 14546read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14547 unsigned int *bytes_read)
c764a876
DE
14548{
14549 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14550
c764a876
DE
14551 *bytes_read = cu_header->offset_size;
14552 return offset;
14553}
14554
14555/* Read an offset from the data stream. */
14556
14557static LONGEST
14558read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14559{
14560 LONGEST retval = 0;
14561
c764a876 14562 switch (offset_size)
613e1657
KB
14563 {
14564 case 4:
fe1b8b76 14565 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14566 break;
14567 case 8:
fe1b8b76 14568 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14569 break;
14570 default:
8e65ff28 14571 internal_error (__FILE__, __LINE__,
c764a876 14572 _("read_offset_1: bad switch [in module %s]"),
659b0389 14573 bfd_get_filename (abfd));
613e1657
KB
14574 }
14575
917c78fc 14576 return retval;
613e1657
KB
14577}
14578
fe1b8b76
JB
14579static gdb_byte *
14580read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14581{
14582 /* If the size of a host char is 8 bits, we can return a pointer
14583 to the buffer, otherwise we have to copy the data to a buffer
14584 allocated on the temporary obstack. */
4bdf3d34 14585 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14586 return buf;
c906108c
SS
14587}
14588
14589static char *
9b1c24c8 14590read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14591{
14592 /* If the size of a host char is 8 bits, we can return a pointer
14593 to the string, otherwise we have to copy the string to a buffer
14594 allocated on the temporary obstack. */
4bdf3d34 14595 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14596 if (*buf == '\0')
14597 {
14598 *bytes_read_ptr = 1;
14599 return NULL;
14600 }
fe1b8b76
JB
14601 *bytes_read_ptr = strlen ((char *) buf) + 1;
14602 return (char *) buf;
4bdf3d34
JJ
14603}
14604
14605static char *
cf2c3c16 14606read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14607{
be391dca 14608 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14609 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14610 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14611 bfd_get_filename (abfd));
dce234bc 14612 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14613 error (_("DW_FORM_strp pointing outside of "
14614 ".debug_str section [in module %s]"),
14615 bfd_get_filename (abfd));
4bdf3d34 14616 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14617 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14618 return NULL;
dce234bc 14619 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14620}
14621
36586728
TT
14622/* Read a string at offset STR_OFFSET in the .debug_str section from
14623 the .dwz file DWZ. Throw an error if the offset is too large. If
14624 the string consists of a single NUL byte, return NULL; otherwise
14625 return a pointer to the string. */
14626
14627static char *
14628read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14629{
14630 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14631
14632 if (dwz->str.buffer == NULL)
14633 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14634 "section [in module %s]"),
14635 bfd_get_filename (dwz->dwz_bfd));
14636 if (str_offset >= dwz->str.size)
14637 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14638 ".debug_str section [in module %s]"),
14639 bfd_get_filename (dwz->dwz_bfd));
14640 gdb_assert (HOST_CHAR_BIT == 8);
14641 if (dwz->str.buffer[str_offset] == '\0')
14642 return NULL;
14643 return (char *) (dwz->str.buffer + str_offset);
14644}
14645
cf2c3c16
TT
14646static char *
14647read_indirect_string (bfd *abfd, gdb_byte *buf,
14648 const struct comp_unit_head *cu_header,
14649 unsigned int *bytes_read_ptr)
14650{
14651 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14652
14653 return read_indirect_string_at_offset (abfd, str_offset);
14654}
14655
12df843f 14656static ULONGEST
fe1b8b76 14657read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14658{
12df843f 14659 ULONGEST result;
ce5d95e1 14660 unsigned int num_read;
c906108c
SS
14661 int i, shift;
14662 unsigned char byte;
14663
14664 result = 0;
14665 shift = 0;
14666 num_read = 0;
14667 i = 0;
14668 while (1)
14669 {
fe1b8b76 14670 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14671 buf++;
14672 num_read++;
12df843f 14673 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14674 if ((byte & 128) == 0)
14675 {
14676 break;
14677 }
14678 shift += 7;
14679 }
14680 *bytes_read_ptr = num_read;
14681 return result;
14682}
14683
12df843f 14684static LONGEST
fe1b8b76 14685read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14686{
12df843f 14687 LONGEST result;
77e0b926 14688 int i, shift, num_read;
c906108c
SS
14689 unsigned char byte;
14690
14691 result = 0;
14692 shift = 0;
c906108c
SS
14693 num_read = 0;
14694 i = 0;
14695 while (1)
14696 {
fe1b8b76 14697 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14698 buf++;
14699 num_read++;
12df843f 14700 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14701 shift += 7;
14702 if ((byte & 128) == 0)
14703 {
14704 break;
14705 }
14706 }
77e0b926 14707 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14708 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14709 *bytes_read_ptr = num_read;
14710 return result;
14711}
14712
3019eac3
DE
14713/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14714 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14715 ADDR_SIZE is the size of addresses from the CU header. */
14716
14717static CORE_ADDR
14718read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14719{
14720 struct objfile *objfile = dwarf2_per_objfile->objfile;
14721 bfd *abfd = objfile->obfd;
14722 const gdb_byte *info_ptr;
14723
14724 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14725 if (dwarf2_per_objfile->addr.buffer == NULL)
14726 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14727 objfile->name);
14728 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14729 error (_("DW_FORM_addr_index pointing outside of "
14730 ".debug_addr section [in module %s]"),
14731 objfile->name);
14732 info_ptr = (dwarf2_per_objfile->addr.buffer
14733 + addr_base + addr_index * addr_size);
14734 if (addr_size == 4)
14735 return bfd_get_32 (abfd, info_ptr);
14736 else
14737 return bfd_get_64 (abfd, info_ptr);
14738}
14739
14740/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14741
14742static CORE_ADDR
14743read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14744{
14745 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14746}
14747
14748/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14749
14750static CORE_ADDR
14751read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14752 unsigned int *bytes_read)
14753{
14754 bfd *abfd = cu->objfile->obfd;
14755 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14756
14757 return read_addr_index (cu, addr_index);
14758}
14759
14760/* Data structure to pass results from dwarf2_read_addr_index_reader
14761 back to dwarf2_read_addr_index. */
14762
14763struct dwarf2_read_addr_index_data
14764{
14765 ULONGEST addr_base;
14766 int addr_size;
14767};
14768
14769/* die_reader_func for dwarf2_read_addr_index. */
14770
14771static void
14772dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14773 gdb_byte *info_ptr,
14774 struct die_info *comp_unit_die,
14775 int has_children,
14776 void *data)
14777{
14778 struct dwarf2_cu *cu = reader->cu;
14779 struct dwarf2_read_addr_index_data *aidata =
14780 (struct dwarf2_read_addr_index_data *) data;
14781
14782 aidata->addr_base = cu->addr_base;
14783 aidata->addr_size = cu->header.addr_size;
14784}
14785
14786/* Given an index in .debug_addr, fetch the value.
14787 NOTE: This can be called during dwarf expression evaluation,
14788 long after the debug information has been read, and thus per_cu->cu
14789 may no longer exist. */
14790
14791CORE_ADDR
14792dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14793 unsigned int addr_index)
14794{
14795 struct objfile *objfile = per_cu->objfile;
14796 struct dwarf2_cu *cu = per_cu->cu;
14797 ULONGEST addr_base;
14798 int addr_size;
14799
14800 /* This is intended to be called from outside this file. */
14801 dw2_setup (objfile);
14802
14803 /* We need addr_base and addr_size.
14804 If we don't have PER_CU->cu, we have to get it.
14805 Nasty, but the alternative is storing the needed info in PER_CU,
14806 which at this point doesn't seem justified: it's not clear how frequently
14807 it would get used and it would increase the size of every PER_CU.
14808 Entry points like dwarf2_per_cu_addr_size do a similar thing
14809 so we're not in uncharted territory here.
14810 Alas we need to be a bit more complicated as addr_base is contained
14811 in the DIE.
14812
14813 We don't need to read the entire CU(/TU).
14814 We just need the header and top level die.
a1b64ce1 14815
3019eac3 14816 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14817 For now we skip this optimization. */
3019eac3
DE
14818
14819 if (cu != NULL)
14820 {
14821 addr_base = cu->addr_base;
14822 addr_size = cu->header.addr_size;
14823 }
14824 else
14825 {
14826 struct dwarf2_read_addr_index_data aidata;
14827
a1b64ce1
DE
14828 /* Note: We can't use init_cutu_and_read_dies_simple here,
14829 we need addr_base. */
14830 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14831 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14832 addr_base = aidata.addr_base;
14833 addr_size = aidata.addr_size;
14834 }
14835
14836 return read_addr_index_1 (addr_index, addr_base, addr_size);
14837}
14838
14839/* Given a DW_AT_str_index, fetch the string. */
14840
14841static char *
14842read_str_index (const struct die_reader_specs *reader,
14843 struct dwarf2_cu *cu, ULONGEST str_index)
14844{
14845 struct objfile *objfile = dwarf2_per_objfile->objfile;
14846 const char *dwo_name = objfile->name;
14847 bfd *abfd = objfile->obfd;
14848 struct dwo_sections *sections = &reader->dwo_file->sections;
14849 gdb_byte *info_ptr;
14850 ULONGEST str_offset;
14851
14852 dwarf2_read_section (objfile, &sections->str);
14853 dwarf2_read_section (objfile, &sections->str_offsets);
14854 if (sections->str.buffer == NULL)
14855 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14856 " in CU at offset 0x%lx [in module %s]"),
14857 (long) cu->header.offset.sect_off, dwo_name);
14858 if (sections->str_offsets.buffer == NULL)
14859 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14860 " in CU at offset 0x%lx [in module %s]"),
14861 (long) cu->header.offset.sect_off, dwo_name);
14862 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14863 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14864 " section in CU at offset 0x%lx [in module %s]"),
14865 (long) cu->header.offset.sect_off, dwo_name);
14866 info_ptr = (sections->str_offsets.buffer
14867 + str_index * cu->header.offset_size);
14868 if (cu->header.offset_size == 4)
14869 str_offset = bfd_get_32 (abfd, info_ptr);
14870 else
14871 str_offset = bfd_get_64 (abfd, info_ptr);
14872 if (str_offset >= sections->str.size)
14873 error (_("Offset from DW_FORM_str_index pointing outside of"
14874 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14875 (long) cu->header.offset.sect_off, dwo_name);
14876 return (char *) (sections->str.buffer + str_offset);
14877}
14878
3019eac3
DE
14879/* Return the length of an LEB128 number in BUF. */
14880
14881static int
14882leb128_size (const gdb_byte *buf)
14883{
14884 const gdb_byte *begin = buf;
14885 gdb_byte byte;
14886
14887 while (1)
14888 {
14889 byte = *buf++;
14890 if ((byte & 128) == 0)
14891 return buf - begin;
14892 }
14893}
14894
c906108c 14895static void
e142c38c 14896set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14897{
14898 switch (lang)
14899 {
14900 case DW_LANG_C89:
76bee0cc 14901 case DW_LANG_C99:
c906108c 14902 case DW_LANG_C:
e142c38c 14903 cu->language = language_c;
c906108c
SS
14904 break;
14905 case DW_LANG_C_plus_plus:
e142c38c 14906 cu->language = language_cplus;
c906108c 14907 break;
6aecb9c2
JB
14908 case DW_LANG_D:
14909 cu->language = language_d;
14910 break;
c906108c
SS
14911 case DW_LANG_Fortran77:
14912 case DW_LANG_Fortran90:
b21b22e0 14913 case DW_LANG_Fortran95:
e142c38c 14914 cu->language = language_fortran;
c906108c 14915 break;
a766d390
DE
14916 case DW_LANG_Go:
14917 cu->language = language_go;
14918 break;
c906108c 14919 case DW_LANG_Mips_Assembler:
e142c38c 14920 cu->language = language_asm;
c906108c 14921 break;
bebd888e 14922 case DW_LANG_Java:
e142c38c 14923 cu->language = language_java;
bebd888e 14924 break;
c906108c 14925 case DW_LANG_Ada83:
8aaf0b47 14926 case DW_LANG_Ada95:
bc5f45f8
JB
14927 cu->language = language_ada;
14928 break;
72019c9c
GM
14929 case DW_LANG_Modula2:
14930 cu->language = language_m2;
14931 break;
fe8e67fd
PM
14932 case DW_LANG_Pascal83:
14933 cu->language = language_pascal;
14934 break;
22566fbd
DJ
14935 case DW_LANG_ObjC:
14936 cu->language = language_objc;
14937 break;
c906108c
SS
14938 case DW_LANG_Cobol74:
14939 case DW_LANG_Cobol85:
c906108c 14940 default:
e142c38c 14941 cu->language = language_minimal;
c906108c
SS
14942 break;
14943 }
e142c38c 14944 cu->language_defn = language_def (cu->language);
c906108c
SS
14945}
14946
14947/* Return the named attribute or NULL if not there. */
14948
14949static struct attribute *
e142c38c 14950dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14951{
a48e046c 14952 for (;;)
c906108c 14953 {
a48e046c
TT
14954 unsigned int i;
14955 struct attribute *spec = NULL;
14956
14957 for (i = 0; i < die->num_attrs; ++i)
14958 {
14959 if (die->attrs[i].name == name)
14960 return &die->attrs[i];
14961 if (die->attrs[i].name == DW_AT_specification
14962 || die->attrs[i].name == DW_AT_abstract_origin)
14963 spec = &die->attrs[i];
14964 }
14965
14966 if (!spec)
14967 break;
c906108c 14968
f2f0e013 14969 die = follow_die_ref (die, spec, &cu);
f2f0e013 14970 }
c5aa993b 14971
c906108c
SS
14972 return NULL;
14973}
14974
348e048f
DE
14975/* Return the named attribute or NULL if not there,
14976 but do not follow DW_AT_specification, etc.
14977 This is for use in contexts where we're reading .debug_types dies.
14978 Following DW_AT_specification, DW_AT_abstract_origin will take us
14979 back up the chain, and we want to go down. */
14980
14981static struct attribute *
45e58e77 14982dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14983{
14984 unsigned int i;
14985
14986 for (i = 0; i < die->num_attrs; ++i)
14987 if (die->attrs[i].name == name)
14988 return &die->attrs[i];
14989
14990 return NULL;
14991}
14992
05cf31d1
JB
14993/* Return non-zero iff the attribute NAME is defined for the given DIE,
14994 and holds a non-zero value. This function should only be used for
2dc7f7b3 14995 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14996
14997static int
14998dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14999{
15000 struct attribute *attr = dwarf2_attr (die, name, cu);
15001
15002 return (attr && DW_UNSND (attr));
15003}
15004
3ca72b44 15005static int
e142c38c 15006die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15007{
05cf31d1
JB
15008 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15009 which value is non-zero. However, we have to be careful with
15010 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15011 (via dwarf2_flag_true_p) follows this attribute. So we may
15012 end up accidently finding a declaration attribute that belongs
15013 to a different DIE referenced by the specification attribute,
15014 even though the given DIE does not have a declaration attribute. */
15015 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15016 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15017}
15018
63d06c5c 15019/* Return the die giving the specification for DIE, if there is
f2f0e013 15020 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15021 containing the return value on output. If there is no
15022 specification, but there is an abstract origin, that is
15023 returned. */
63d06c5c
DC
15024
15025static struct die_info *
f2f0e013 15026die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15027{
f2f0e013
DJ
15028 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15029 *spec_cu);
63d06c5c 15030
edb3359d
DJ
15031 if (spec_attr == NULL)
15032 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15033
63d06c5c
DC
15034 if (spec_attr == NULL)
15035 return NULL;
15036 else
f2f0e013 15037 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15038}
c906108c 15039
debd256d 15040/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15041 refers to.
15042 NOTE: This is also used as a "cleanup" function. */
15043
debd256d
JB
15044static void
15045free_line_header (struct line_header *lh)
15046{
15047 if (lh->standard_opcode_lengths)
a8bc7b56 15048 xfree (lh->standard_opcode_lengths);
debd256d
JB
15049
15050 /* Remember that all the lh->file_names[i].name pointers are
15051 pointers into debug_line_buffer, and don't need to be freed. */
15052 if (lh->file_names)
a8bc7b56 15053 xfree (lh->file_names);
debd256d
JB
15054
15055 /* Similarly for the include directory names. */
15056 if (lh->include_dirs)
a8bc7b56 15057 xfree (lh->include_dirs);
debd256d 15058
a8bc7b56 15059 xfree (lh);
debd256d
JB
15060}
15061
debd256d 15062/* Add an entry to LH's include directory table. */
ae2de4f8 15063
debd256d
JB
15064static void
15065add_include_dir (struct line_header *lh, char *include_dir)
c906108c 15066{
debd256d
JB
15067 /* Grow the array if necessary. */
15068 if (lh->include_dirs_size == 0)
c5aa993b 15069 {
debd256d
JB
15070 lh->include_dirs_size = 1; /* for testing */
15071 lh->include_dirs = xmalloc (lh->include_dirs_size
15072 * sizeof (*lh->include_dirs));
15073 }
15074 else if (lh->num_include_dirs >= lh->include_dirs_size)
15075 {
15076 lh->include_dirs_size *= 2;
15077 lh->include_dirs = xrealloc (lh->include_dirs,
15078 (lh->include_dirs_size
15079 * sizeof (*lh->include_dirs)));
c5aa993b 15080 }
c906108c 15081
debd256d
JB
15082 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15083}
6e70227d 15084
debd256d 15085/* Add an entry to LH's file name table. */
ae2de4f8 15086
debd256d
JB
15087static void
15088add_file_name (struct line_header *lh,
15089 char *name,
15090 unsigned int dir_index,
15091 unsigned int mod_time,
15092 unsigned int length)
15093{
15094 struct file_entry *fe;
15095
15096 /* Grow the array if necessary. */
15097 if (lh->file_names_size == 0)
15098 {
15099 lh->file_names_size = 1; /* for testing */
15100 lh->file_names = xmalloc (lh->file_names_size
15101 * sizeof (*lh->file_names));
15102 }
15103 else if (lh->num_file_names >= lh->file_names_size)
15104 {
15105 lh->file_names_size *= 2;
15106 lh->file_names = xrealloc (lh->file_names,
15107 (lh->file_names_size
15108 * sizeof (*lh->file_names)));
15109 }
15110
15111 fe = &lh->file_names[lh->num_file_names++];
15112 fe->name = name;
15113 fe->dir_index = dir_index;
15114 fe->mod_time = mod_time;
15115 fe->length = length;
aaa75496 15116 fe->included_p = 0;
cb1df416 15117 fe->symtab = NULL;
debd256d 15118}
6e70227d 15119
36586728
TT
15120/* A convenience function to find the proper .debug_line section for a
15121 CU. */
15122
15123static struct dwarf2_section_info *
15124get_debug_line_section (struct dwarf2_cu *cu)
15125{
15126 struct dwarf2_section_info *section;
15127
15128 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15129 DWO file. */
15130 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15131 section = &cu->dwo_unit->dwo_file->sections.line;
15132 else if (cu->per_cu->is_dwz)
15133 {
15134 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15135
15136 section = &dwz->line;
15137 }
15138 else
15139 section = &dwarf2_per_objfile->line;
15140
15141 return section;
15142}
15143
debd256d 15144/* Read the statement program header starting at OFFSET in
3019eac3 15145 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15146 to a struct line_header, allocated using xmalloc.
debd256d
JB
15147
15148 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15149 the returned object point into the dwarf line section buffer,
15150 and must not be freed. */
ae2de4f8 15151
debd256d 15152static struct line_header *
3019eac3 15153dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15154{
15155 struct cleanup *back_to;
15156 struct line_header *lh;
fe1b8b76 15157 gdb_byte *line_ptr;
c764a876 15158 unsigned int bytes_read, offset_size;
debd256d
JB
15159 int i;
15160 char *cur_dir, *cur_file;
3019eac3
DE
15161 struct dwarf2_section_info *section;
15162 bfd *abfd;
15163
36586728 15164 section = get_debug_line_section (cu);
3019eac3
DE
15165 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15166 if (section->buffer == NULL)
debd256d 15167 {
3019eac3
DE
15168 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15169 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15170 else
15171 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15172 return 0;
15173 }
15174
fceca515
DE
15175 /* We can't do this until we know the section is non-empty.
15176 Only then do we know we have such a section. */
15177 abfd = section->asection->owner;
15178
a738430d
MK
15179 /* Make sure that at least there's room for the total_length field.
15180 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15181 if (offset + 4 >= section->size)
debd256d 15182 {
4d3c2250 15183 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15184 return 0;
15185 }
15186
15187 lh = xmalloc (sizeof (*lh));
15188 memset (lh, 0, sizeof (*lh));
15189 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15190 (void *) lh);
15191
3019eac3 15192 line_ptr = section->buffer + offset;
debd256d 15193
a738430d 15194 /* Read in the header. */
6e70227d 15195 lh->total_length =
c764a876
DE
15196 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15197 &bytes_read, &offset_size);
debd256d 15198 line_ptr += bytes_read;
3019eac3 15199 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15200 {
4d3c2250 15201 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15202 return 0;
15203 }
15204 lh->statement_program_end = line_ptr + lh->total_length;
15205 lh->version = read_2_bytes (abfd, line_ptr);
15206 line_ptr += 2;
c764a876
DE
15207 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15208 line_ptr += offset_size;
debd256d
JB
15209 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15210 line_ptr += 1;
2dc7f7b3
TT
15211 if (lh->version >= 4)
15212 {
15213 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15214 line_ptr += 1;
15215 }
15216 else
15217 lh->maximum_ops_per_instruction = 1;
15218
15219 if (lh->maximum_ops_per_instruction == 0)
15220 {
15221 lh->maximum_ops_per_instruction = 1;
15222 complaint (&symfile_complaints,
3e43a32a
MS
15223 _("invalid maximum_ops_per_instruction "
15224 "in `.debug_line' section"));
2dc7f7b3
TT
15225 }
15226
debd256d
JB
15227 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15228 line_ptr += 1;
15229 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15230 line_ptr += 1;
15231 lh->line_range = read_1_byte (abfd, line_ptr);
15232 line_ptr += 1;
15233 lh->opcode_base = read_1_byte (abfd, line_ptr);
15234 line_ptr += 1;
15235 lh->standard_opcode_lengths
fe1b8b76 15236 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15237
15238 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15239 for (i = 1; i < lh->opcode_base; ++i)
15240 {
15241 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15242 line_ptr += 1;
15243 }
15244
a738430d 15245 /* Read directory table. */
9b1c24c8 15246 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15247 {
15248 line_ptr += bytes_read;
15249 add_include_dir (lh, cur_dir);
15250 }
15251 line_ptr += bytes_read;
15252
a738430d 15253 /* Read file name table. */
9b1c24c8 15254 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15255 {
15256 unsigned int dir_index, mod_time, length;
15257
15258 line_ptr += bytes_read;
15259 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15260 line_ptr += bytes_read;
15261 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15262 line_ptr += bytes_read;
15263 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15264 line_ptr += bytes_read;
15265
15266 add_file_name (lh, cur_file, dir_index, mod_time, length);
15267 }
15268 line_ptr += bytes_read;
6e70227d 15269 lh->statement_program_start = line_ptr;
debd256d 15270
3019eac3 15271 if (line_ptr > (section->buffer + section->size))
4d3c2250 15272 complaint (&symfile_complaints,
3e43a32a
MS
15273 _("line number info header doesn't "
15274 "fit in `.debug_line' section"));
debd256d
JB
15275
15276 discard_cleanups (back_to);
15277 return lh;
15278}
c906108c 15279
c6da4cef
DE
15280/* Subroutine of dwarf_decode_lines to simplify it.
15281 Return the file name of the psymtab for included file FILE_INDEX
15282 in line header LH of PST.
15283 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15284 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15285 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15286
15287 The function creates dangling cleanup registration. */
c6da4cef
DE
15288
15289static char *
15290psymtab_include_file_name (const struct line_header *lh, int file_index,
15291 const struct partial_symtab *pst,
15292 const char *comp_dir)
15293{
15294 const struct file_entry fe = lh->file_names [file_index];
15295 char *include_name = fe.name;
15296 char *include_name_to_compare = include_name;
15297 char *dir_name = NULL;
72b9f47f
TT
15298 const char *pst_filename;
15299 char *copied_name = NULL;
c6da4cef
DE
15300 int file_is_pst;
15301
15302 if (fe.dir_index)
15303 dir_name = lh->include_dirs[fe.dir_index - 1];
15304
15305 if (!IS_ABSOLUTE_PATH (include_name)
15306 && (dir_name != NULL || comp_dir != NULL))
15307 {
15308 /* Avoid creating a duplicate psymtab for PST.
15309 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15310 Before we do the comparison, however, we need to account
15311 for DIR_NAME and COMP_DIR.
15312 First prepend dir_name (if non-NULL). If we still don't
15313 have an absolute path prepend comp_dir (if non-NULL).
15314 However, the directory we record in the include-file's
15315 psymtab does not contain COMP_DIR (to match the
15316 corresponding symtab(s)).
15317
15318 Example:
15319
15320 bash$ cd /tmp
15321 bash$ gcc -g ./hello.c
15322 include_name = "hello.c"
15323 dir_name = "."
15324 DW_AT_comp_dir = comp_dir = "/tmp"
15325 DW_AT_name = "./hello.c" */
15326
15327 if (dir_name != NULL)
15328 {
15329 include_name = concat (dir_name, SLASH_STRING,
15330 include_name, (char *)NULL);
15331 include_name_to_compare = include_name;
15332 make_cleanup (xfree, include_name);
15333 }
15334 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15335 {
15336 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15337 include_name, (char *)NULL);
15338 }
15339 }
15340
15341 pst_filename = pst->filename;
15342 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15343 {
72b9f47f
TT
15344 copied_name = concat (pst->dirname, SLASH_STRING,
15345 pst_filename, (char *)NULL);
15346 pst_filename = copied_name;
c6da4cef
DE
15347 }
15348
1e3fad37 15349 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15350
15351 if (include_name_to_compare != include_name)
15352 xfree (include_name_to_compare);
72b9f47f
TT
15353 if (copied_name != NULL)
15354 xfree (copied_name);
c6da4cef
DE
15355
15356 if (file_is_pst)
15357 return NULL;
15358 return include_name;
15359}
15360
c91513d8
PP
15361/* Ignore this record_line request. */
15362
15363static void
15364noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15365{
15366 return;
15367}
15368
f3f5162e
DE
15369/* Subroutine of dwarf_decode_lines to simplify it.
15370 Process the line number information in LH. */
debd256d 15371
c906108c 15372static void
f3f5162e
DE
15373dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15374 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15375{
a8c50c1f 15376 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15377 gdb_byte *line_end;
a8c50c1f 15378 unsigned int bytes_read, extended_len;
c906108c 15379 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15380 CORE_ADDR baseaddr;
15381 struct objfile *objfile = cu->objfile;
f3f5162e 15382 bfd *abfd = objfile->obfd;
fbf65064 15383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15384 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15385 struct subfile *last_subfile = NULL;
c91513d8
PP
15386 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15387 = record_line;
e142c38c
DJ
15388
15389 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15390
debd256d
JB
15391 line_ptr = lh->statement_program_start;
15392 line_end = lh->statement_program_end;
c906108c
SS
15393
15394 /* Read the statement sequences until there's nothing left. */
15395 while (line_ptr < line_end)
15396 {
15397 /* state machine registers */
15398 CORE_ADDR address = 0;
15399 unsigned int file = 1;
15400 unsigned int line = 1;
15401 unsigned int column = 0;
debd256d 15402 int is_stmt = lh->default_is_stmt;
c906108c
SS
15403 int basic_block = 0;
15404 int end_sequence = 0;
fbf65064 15405 CORE_ADDR addr;
2dc7f7b3 15406 unsigned char op_index = 0;
c906108c 15407
aaa75496 15408 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15409 {
aaa75496 15410 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15411 /* lh->include_dirs and lh->file_names are 0-based, but the
15412 directory and file name numbers in the statement program
15413 are 1-based. */
15414 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15415 char *dir = NULL;
a738430d 15416
debd256d
JB
15417 if (fe->dir_index)
15418 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15419
15420 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15421 }
15422
a738430d 15423 /* Decode the table. */
c5aa993b 15424 while (!end_sequence)
c906108c
SS
15425 {
15426 op_code = read_1_byte (abfd, line_ptr);
15427 line_ptr += 1;
59205f5a
JB
15428 if (line_ptr > line_end)
15429 {
15430 dwarf2_debug_line_missing_end_sequence_complaint ();
15431 break;
15432 }
9aa1fe7e 15433
debd256d 15434 if (op_code >= lh->opcode_base)
6e70227d 15435 {
a738430d 15436 /* Special operand. */
debd256d 15437 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15438 address += (((op_index + (adj_opcode / lh->line_range))
15439 / lh->maximum_ops_per_instruction)
15440 * lh->minimum_instruction_length);
15441 op_index = ((op_index + (adj_opcode / lh->line_range))
15442 % lh->maximum_ops_per_instruction);
debd256d 15443 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15444 if (lh->num_file_names < file || file == 0)
25e43795 15445 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15446 /* For now we ignore lines not starting on an
15447 instruction boundary. */
15448 else if (op_index == 0)
25e43795
DJ
15449 {
15450 lh->file_names[file - 1].included_p = 1;
ca5f395d 15451 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15452 {
15453 if (last_subfile != current_subfile)
15454 {
15455 addr = gdbarch_addr_bits_remove (gdbarch, address);
15456 if (last_subfile)
c91513d8 15457 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15458 last_subfile = current_subfile;
15459 }
25e43795 15460 /* Append row to matrix using current values. */
7019d805 15461 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15462 (*p_record_line) (current_subfile, line, addr);
366da635 15463 }
25e43795 15464 }
ca5f395d 15465 basic_block = 0;
9aa1fe7e
GK
15466 }
15467 else switch (op_code)
c906108c
SS
15468 {
15469 case DW_LNS_extended_op:
3e43a32a
MS
15470 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15471 &bytes_read);
473b7be6 15472 line_ptr += bytes_read;
a8c50c1f 15473 extended_end = line_ptr + extended_len;
c906108c
SS
15474 extended_op = read_1_byte (abfd, line_ptr);
15475 line_ptr += 1;
15476 switch (extended_op)
15477 {
15478 case DW_LNE_end_sequence:
c91513d8 15479 p_record_line = record_line;
c906108c 15480 end_sequence = 1;
c906108c
SS
15481 break;
15482 case DW_LNE_set_address:
e7c27a73 15483 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15484
15485 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15486 {
15487 /* This line table is for a function which has been
15488 GCd by the linker. Ignore it. PR gdb/12528 */
15489
15490 long line_offset
36586728 15491 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15492
15493 complaint (&symfile_complaints,
15494 _(".debug_line address at offset 0x%lx is 0 "
15495 "[in module %s]"),
bb5ed363 15496 line_offset, objfile->name);
c91513d8
PP
15497 p_record_line = noop_record_line;
15498 }
15499
2dc7f7b3 15500 op_index = 0;
107d2387
AC
15501 line_ptr += bytes_read;
15502 address += baseaddr;
c906108c
SS
15503 break;
15504 case DW_LNE_define_file:
debd256d
JB
15505 {
15506 char *cur_file;
15507 unsigned int dir_index, mod_time, length;
6e70227d 15508
3e43a32a
MS
15509 cur_file = read_direct_string (abfd, line_ptr,
15510 &bytes_read);
debd256d
JB
15511 line_ptr += bytes_read;
15512 dir_index =
15513 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15514 line_ptr += bytes_read;
15515 mod_time =
15516 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15517 line_ptr += bytes_read;
15518 length =
15519 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15520 line_ptr += bytes_read;
15521 add_file_name (lh, cur_file, dir_index, mod_time, length);
15522 }
c906108c 15523 break;
d0c6ba3d
CC
15524 case DW_LNE_set_discriminator:
15525 /* The discriminator is not interesting to the debugger;
15526 just ignore it. */
15527 line_ptr = extended_end;
15528 break;
c906108c 15529 default:
4d3c2250 15530 complaint (&symfile_complaints,
e2e0b3e5 15531 _("mangled .debug_line section"));
debd256d 15532 return;
c906108c 15533 }
a8c50c1f
DJ
15534 /* Make sure that we parsed the extended op correctly. If e.g.
15535 we expected a different address size than the producer used,
15536 we may have read the wrong number of bytes. */
15537 if (line_ptr != extended_end)
15538 {
15539 complaint (&symfile_complaints,
15540 _("mangled .debug_line section"));
15541 return;
15542 }
c906108c
SS
15543 break;
15544 case DW_LNS_copy:
59205f5a 15545 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15546 dwarf2_debug_line_missing_file_complaint ();
15547 else
366da635 15548 {
25e43795 15549 lh->file_names[file - 1].included_p = 1;
ca5f395d 15550 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15551 {
15552 if (last_subfile != current_subfile)
15553 {
15554 addr = gdbarch_addr_bits_remove (gdbarch, address);
15555 if (last_subfile)
c91513d8 15556 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15557 last_subfile = current_subfile;
15558 }
7019d805 15559 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15560 (*p_record_line) (current_subfile, line, addr);
fbf65064 15561 }
366da635 15562 }
c906108c
SS
15563 basic_block = 0;
15564 break;
15565 case DW_LNS_advance_pc:
2dc7f7b3
TT
15566 {
15567 CORE_ADDR adjust
15568 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15569
15570 address += (((op_index + adjust)
15571 / lh->maximum_ops_per_instruction)
15572 * lh->minimum_instruction_length);
15573 op_index = ((op_index + adjust)
15574 % lh->maximum_ops_per_instruction);
15575 line_ptr += bytes_read;
15576 }
c906108c
SS
15577 break;
15578 case DW_LNS_advance_line:
15579 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15580 line_ptr += bytes_read;
15581 break;
15582 case DW_LNS_set_file:
debd256d 15583 {
a738430d
MK
15584 /* The arrays lh->include_dirs and lh->file_names are
15585 0-based, but the directory and file name numbers in
15586 the statement program are 1-based. */
debd256d 15587 struct file_entry *fe;
4f1520fb 15588 char *dir = NULL;
a738430d 15589
debd256d
JB
15590 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15591 line_ptr += bytes_read;
59205f5a 15592 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15593 dwarf2_debug_line_missing_file_complaint ();
15594 else
15595 {
15596 fe = &lh->file_names[file - 1];
15597 if (fe->dir_index)
15598 dir = lh->include_dirs[fe->dir_index - 1];
15599 if (!decode_for_pst_p)
15600 {
15601 last_subfile = current_subfile;
15602 dwarf2_start_subfile (fe->name, dir, comp_dir);
15603 }
15604 }
debd256d 15605 }
c906108c
SS
15606 break;
15607 case DW_LNS_set_column:
15608 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15609 line_ptr += bytes_read;
15610 break;
15611 case DW_LNS_negate_stmt:
15612 is_stmt = (!is_stmt);
15613 break;
15614 case DW_LNS_set_basic_block:
15615 basic_block = 1;
15616 break;
c2c6d25f
JM
15617 /* Add to the address register of the state machine the
15618 address increment value corresponding to special opcode
a738430d
MK
15619 255. I.e., this value is scaled by the minimum
15620 instruction length since special opcode 255 would have
b021a221 15621 scaled the increment. */
c906108c 15622 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15623 {
15624 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15625
15626 address += (((op_index + adjust)
15627 / lh->maximum_ops_per_instruction)
15628 * lh->minimum_instruction_length);
15629 op_index = ((op_index + adjust)
15630 % lh->maximum_ops_per_instruction);
15631 }
c906108c
SS
15632 break;
15633 case DW_LNS_fixed_advance_pc:
15634 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15635 op_index = 0;
c906108c
SS
15636 line_ptr += 2;
15637 break;
9aa1fe7e 15638 default:
a738430d
MK
15639 {
15640 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15641 int i;
a738430d 15642
debd256d 15643 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15644 {
15645 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15646 line_ptr += bytes_read;
15647 }
15648 }
c906108c
SS
15649 }
15650 }
59205f5a
JB
15651 if (lh->num_file_names < file || file == 0)
15652 dwarf2_debug_line_missing_file_complaint ();
15653 else
15654 {
15655 lh->file_names[file - 1].included_p = 1;
15656 if (!decode_for_pst_p)
fbf65064
UW
15657 {
15658 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15659 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15660 }
59205f5a 15661 }
c906108c 15662 }
f3f5162e
DE
15663}
15664
15665/* Decode the Line Number Program (LNP) for the given line_header
15666 structure and CU. The actual information extracted and the type
15667 of structures created from the LNP depends on the value of PST.
15668
15669 1. If PST is NULL, then this procedure uses the data from the program
15670 to create all necessary symbol tables, and their linetables.
15671
15672 2. If PST is not NULL, this procedure reads the program to determine
15673 the list of files included by the unit represented by PST, and
15674 builds all the associated partial symbol tables.
15675
15676 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15677 It is used for relative paths in the line table.
15678 NOTE: When processing partial symtabs (pst != NULL),
15679 comp_dir == pst->dirname.
15680
15681 NOTE: It is important that psymtabs have the same file name (via strcmp)
15682 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15683 symtab we don't use it in the name of the psymtabs we create.
15684 E.g. expand_line_sal requires this when finding psymtabs to expand.
15685 A good testcase for this is mb-inline.exp. */
15686
15687static void
15688dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15689 struct dwarf2_cu *cu, struct partial_symtab *pst,
15690 int want_line_info)
15691{
15692 struct objfile *objfile = cu->objfile;
15693 const int decode_for_pst_p = (pst != NULL);
15694 struct subfile *first_subfile = current_subfile;
15695
15696 if (want_line_info)
15697 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15698
15699 if (decode_for_pst_p)
15700 {
15701 int file_index;
15702
15703 /* Now that we're done scanning the Line Header Program, we can
15704 create the psymtab of each included file. */
15705 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15706 if (lh->file_names[file_index].included_p == 1)
15707 {
c6da4cef
DE
15708 char *include_name =
15709 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15710 if (include_name != NULL)
aaa75496
JB
15711 dwarf2_create_include_psymtab (include_name, pst, objfile);
15712 }
15713 }
cb1df416
DJ
15714 else
15715 {
15716 /* Make sure a symtab is created for every file, even files
15717 which contain only variables (i.e. no code with associated
15718 line numbers). */
cb1df416 15719 int i;
cb1df416
DJ
15720
15721 for (i = 0; i < lh->num_file_names; i++)
15722 {
15723 char *dir = NULL;
f3f5162e 15724 struct file_entry *fe;
9a619af0 15725
cb1df416
DJ
15726 fe = &lh->file_names[i];
15727 if (fe->dir_index)
15728 dir = lh->include_dirs[fe->dir_index - 1];
15729 dwarf2_start_subfile (fe->name, dir, comp_dir);
15730
15731 /* Skip the main file; we don't need it, and it must be
15732 allocated last, so that it will show up before the
15733 non-primary symtabs in the objfile's symtab list. */
15734 if (current_subfile == first_subfile)
15735 continue;
15736
15737 if (current_subfile->symtab == NULL)
15738 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15739 objfile);
cb1df416
DJ
15740 fe->symtab = current_subfile->symtab;
15741 }
15742 }
c906108c
SS
15743}
15744
15745/* Start a subfile for DWARF. FILENAME is the name of the file and
15746 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15747 or NULL if not known. COMP_DIR is the compilation directory for the
15748 linetable's compilation unit or NULL if not known.
c906108c
SS
15749 This routine tries to keep line numbers from identical absolute and
15750 relative file names in a common subfile.
15751
15752 Using the `list' example from the GDB testsuite, which resides in
15753 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15754 of /srcdir/list0.c yields the following debugging information for list0.c:
15755
c5aa993b
JM
15756 DW_AT_name: /srcdir/list0.c
15757 DW_AT_comp_dir: /compdir
357e46e7 15758 files.files[0].name: list0.h
c5aa993b 15759 files.files[0].dir: /srcdir
357e46e7 15760 files.files[1].name: list0.c
c5aa993b 15761 files.files[1].dir: /srcdir
c906108c
SS
15762
15763 The line number information for list0.c has to end up in a single
4f1520fb
FR
15764 subfile, so that `break /srcdir/list0.c:1' works as expected.
15765 start_subfile will ensure that this happens provided that we pass the
15766 concatenation of files.files[1].dir and files.files[1].name as the
15767 subfile's name. */
c906108c
SS
15768
15769static void
3e43a32a
MS
15770dwarf2_start_subfile (char *filename, const char *dirname,
15771 const char *comp_dir)
c906108c 15772{
4f1520fb
FR
15773 char *fullname;
15774
15775 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15776 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15777 second argument to start_subfile. To be consistent, we do the
15778 same here. In order not to lose the line information directory,
15779 we concatenate it to the filename when it makes sense.
15780 Note that the Dwarf3 standard says (speaking of filenames in line
15781 information): ``The directory index is ignored for file names
15782 that represent full path names''. Thus ignoring dirname in the
15783 `else' branch below isn't an issue. */
c906108c 15784
d5166ae1 15785 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15786 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15787 else
15788 fullname = filename;
c906108c 15789
4f1520fb
FR
15790 start_subfile (fullname, comp_dir);
15791
15792 if (fullname != filename)
15793 xfree (fullname);
c906108c
SS
15794}
15795
f4dc4d17
DE
15796/* Start a symtab for DWARF.
15797 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15798
15799static void
15800dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15801 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15802{
15803 start_symtab (name, comp_dir, low_pc);
15804 record_debugformat ("DWARF 2");
15805 record_producer (cu->producer);
15806
15807 /* We assume that we're processing GCC output. */
15808 processing_gcc_compilation = 2;
15809
4d4ec4e5 15810 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15811}
15812
4c2df51b
DJ
15813static void
15814var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15815 struct dwarf2_cu *cu)
4c2df51b 15816{
e7c27a73
DJ
15817 struct objfile *objfile = cu->objfile;
15818 struct comp_unit_head *cu_header = &cu->header;
15819
4c2df51b
DJ
15820 /* NOTE drow/2003-01-30: There used to be a comment and some special
15821 code here to turn a symbol with DW_AT_external and a
15822 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15823 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15824 with some versions of binutils) where shared libraries could have
15825 relocations against symbols in their debug information - the
15826 minimal symbol would have the right address, but the debug info
15827 would not. It's no longer necessary, because we will explicitly
15828 apply relocations when we read in the debug information now. */
15829
15830 /* A DW_AT_location attribute with no contents indicates that a
15831 variable has been optimized away. */
15832 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15833 {
f1e6e072 15834 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15835 return;
15836 }
15837
15838 /* Handle one degenerate form of location expression specially, to
15839 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15840 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15841 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15842
15843 if (attr_form_is_block (attr)
3019eac3
DE
15844 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15845 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15846 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15847 && (DW_BLOCK (attr)->size
15848 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15849 {
891d2f0b 15850 unsigned int dummy;
4c2df51b 15851
3019eac3
DE
15852 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15853 SYMBOL_VALUE_ADDRESS (sym) =
15854 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15855 else
15856 SYMBOL_VALUE_ADDRESS (sym) =
15857 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15858 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15859 fixup_symbol_section (sym, objfile);
15860 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15861 SYMBOL_SECTION (sym));
4c2df51b
DJ
15862 return;
15863 }
15864
15865 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15866 expression evaluator, and use LOC_COMPUTED only when necessary
15867 (i.e. when the value of a register or memory location is
15868 referenced, or a thread-local block, etc.). Then again, it might
15869 not be worthwhile. I'm assuming that it isn't unless performance
15870 or memory numbers show me otherwise. */
15871
f1e6e072 15872 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15873
f1e6e072 15874 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15875 cu->has_loclist = 1;
4c2df51b
DJ
15876}
15877
c906108c
SS
15878/* Given a pointer to a DWARF information entry, figure out if we need
15879 to make a symbol table entry for it, and if so, create a new entry
15880 and return a pointer to it.
15881 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15882 used the passed type.
15883 If SPACE is not NULL, use it to hold the new symbol. If it is
15884 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15885
15886static struct symbol *
34eaf542
TT
15887new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15888 struct symbol *space)
c906108c 15889{
e7c27a73 15890 struct objfile *objfile = cu->objfile;
c906108c 15891 struct symbol *sym = NULL;
15d034d0 15892 const char *name;
c906108c
SS
15893 struct attribute *attr = NULL;
15894 struct attribute *attr2 = NULL;
e142c38c 15895 CORE_ADDR baseaddr;
e37fd15a
SW
15896 struct pending **list_to_add = NULL;
15897
edb3359d 15898 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15899
15900 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15901
94af9270 15902 name = dwarf2_name (die, cu);
c906108c
SS
15903 if (name)
15904 {
94af9270 15905 const char *linkagename;
34eaf542 15906 int suppress_add = 0;
94af9270 15907
34eaf542
TT
15908 if (space)
15909 sym = space;
15910 else
15911 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15912 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15913
15914 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15915 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15916 linkagename = dwarf2_physname (name, die, cu);
15917 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15918
f55ee35c
JK
15919 /* Fortran does not have mangling standard and the mangling does differ
15920 between gfortran, iFort etc. */
15921 if (cu->language == language_fortran
b250c185 15922 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15923 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15924 dwarf2_full_name (name, die, cu),
29df156d 15925 NULL);
f55ee35c 15926
c906108c 15927 /* Default assumptions.
c5aa993b 15928 Use the passed type or decode it from the die. */
176620f1 15929 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 15930 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15931 if (type != NULL)
15932 SYMBOL_TYPE (sym) = type;
15933 else
e7c27a73 15934 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15935 attr = dwarf2_attr (die,
15936 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15937 cu);
c906108c
SS
15938 if (attr)
15939 {
15940 SYMBOL_LINE (sym) = DW_UNSND (attr);
15941 }
cb1df416 15942
edb3359d
DJ
15943 attr = dwarf2_attr (die,
15944 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15945 cu);
cb1df416
DJ
15946 if (attr)
15947 {
15948 int file_index = DW_UNSND (attr);
9a619af0 15949
cb1df416
DJ
15950 if (cu->line_header == NULL
15951 || file_index > cu->line_header->num_file_names)
15952 complaint (&symfile_complaints,
15953 _("file index out of range"));
1c3d648d 15954 else if (file_index > 0)
cb1df416
DJ
15955 {
15956 struct file_entry *fe;
9a619af0 15957
cb1df416
DJ
15958 fe = &cu->line_header->file_names[file_index - 1];
15959 SYMBOL_SYMTAB (sym) = fe->symtab;
15960 }
15961 }
15962
c906108c
SS
15963 switch (die->tag)
15964 {
15965 case DW_TAG_label:
e142c38c 15966 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15967 if (attr)
15968 {
15969 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15970 }
0f5238ed
TT
15971 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15972 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 15973 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 15974 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15975 break;
15976 case DW_TAG_subprogram:
15977 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15978 finish_block. */
f1e6e072 15979 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 15980 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15981 if ((attr2 && (DW_UNSND (attr2) != 0))
15982 || cu->language == language_ada)
c906108c 15983 {
2cfa0c8d
JB
15984 /* Subprograms marked external are stored as a global symbol.
15985 Ada subprograms, whether marked external or not, are always
15986 stored as a global symbol, because we want to be able to
15987 access them globally. For instance, we want to be able
15988 to break on a nested subprogram without having to
15989 specify the context. */
e37fd15a 15990 list_to_add = &global_symbols;
c906108c
SS
15991 }
15992 else
15993 {
e37fd15a 15994 list_to_add = cu->list_in_scope;
c906108c
SS
15995 }
15996 break;
edb3359d
DJ
15997 case DW_TAG_inlined_subroutine:
15998 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15999 finish_block. */
f1e6e072 16000 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16001 SYMBOL_INLINED (sym) = 1;
481860b3 16002 list_to_add = cu->list_in_scope;
edb3359d 16003 break;
34eaf542
TT
16004 case DW_TAG_template_value_param:
16005 suppress_add = 1;
16006 /* Fall through. */
72929c62 16007 case DW_TAG_constant:
c906108c 16008 case DW_TAG_variable:
254e6b9e 16009 case DW_TAG_member:
0963b4bd
MS
16010 /* Compilation with minimal debug info may result in
16011 variables with missing type entries. Change the
16012 misleading `void' type to something sensible. */
c906108c 16013 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16014 SYMBOL_TYPE (sym)
46bf5051 16015 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16016
e142c38c 16017 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16018 /* In the case of DW_TAG_member, we should only be called for
16019 static const members. */
16020 if (die->tag == DW_TAG_member)
16021 {
3863f96c
DE
16022 /* dwarf2_add_field uses die_is_declaration,
16023 so we do the same. */
254e6b9e
DE
16024 gdb_assert (die_is_declaration (die, cu));
16025 gdb_assert (attr);
16026 }
c906108c
SS
16027 if (attr)
16028 {
e7c27a73 16029 dwarf2_const_value (attr, sym, cu);
e142c38c 16030 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16031 if (!suppress_add)
34eaf542
TT
16032 {
16033 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16034 list_to_add = &global_symbols;
34eaf542 16035 else
e37fd15a 16036 list_to_add = cu->list_in_scope;
34eaf542 16037 }
c906108c
SS
16038 break;
16039 }
e142c38c 16040 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16041 if (attr)
16042 {
e7c27a73 16043 var_decode_location (attr, sym, cu);
e142c38c 16044 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16045
16046 /* Fortran explicitly imports any global symbols to the local
16047 scope by DW_TAG_common_block. */
16048 if (cu->language == language_fortran && die->parent
16049 && die->parent->tag == DW_TAG_common_block)
16050 attr2 = NULL;
16051
caac4577
JG
16052 if (SYMBOL_CLASS (sym) == LOC_STATIC
16053 && SYMBOL_VALUE_ADDRESS (sym) == 0
16054 && !dwarf2_per_objfile->has_section_at_zero)
16055 {
16056 /* When a static variable is eliminated by the linker,
16057 the corresponding debug information is not stripped
16058 out, but the variable address is set to null;
16059 do not add such variables into symbol table. */
16060 }
16061 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16062 {
f55ee35c
JK
16063 /* Workaround gfortran PR debug/40040 - it uses
16064 DW_AT_location for variables in -fPIC libraries which may
16065 get overriden by other libraries/executable and get
16066 a different address. Resolve it by the minimal symbol
16067 which may come from inferior's executable using copy
16068 relocation. Make this workaround only for gfortran as for
16069 other compilers GDB cannot guess the minimal symbol
16070 Fortran mangling kind. */
16071 if (cu->language == language_fortran && die->parent
16072 && die->parent->tag == DW_TAG_module
16073 && cu->producer
16074 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16075 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16076
1c809c68
TT
16077 /* A variable with DW_AT_external is never static,
16078 but it may be block-scoped. */
16079 list_to_add = (cu->list_in_scope == &file_symbols
16080 ? &global_symbols : cu->list_in_scope);
1c809c68 16081 }
c906108c 16082 else
e37fd15a 16083 list_to_add = cu->list_in_scope;
c906108c
SS
16084 }
16085 else
16086 {
16087 /* We do not know the address of this symbol.
c5aa993b
JM
16088 If it is an external symbol and we have type information
16089 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16090 The address of the variable will then be determined from
16091 the minimal symbol table whenever the variable is
16092 referenced. */
e142c38c 16093 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16094
16095 /* Fortran explicitly imports any global symbols to the local
16096 scope by DW_TAG_common_block. */
16097 if (cu->language == language_fortran && die->parent
16098 && die->parent->tag == DW_TAG_common_block)
16099 {
16100 /* SYMBOL_CLASS doesn't matter here because
16101 read_common_block is going to reset it. */
16102 if (!suppress_add)
16103 list_to_add = cu->list_in_scope;
16104 }
16105 else if (attr2 && (DW_UNSND (attr2) != 0)
16106 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16107 {
0fe7935b
DJ
16108 /* A variable with DW_AT_external is never static, but it
16109 may be block-scoped. */
16110 list_to_add = (cu->list_in_scope == &file_symbols
16111 ? &global_symbols : cu->list_in_scope);
16112
f1e6e072 16113 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16114 }
442ddf59
JK
16115 else if (!die_is_declaration (die, cu))
16116 {
16117 /* Use the default LOC_OPTIMIZED_OUT class. */
16118 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16119 if (!suppress_add)
16120 list_to_add = cu->list_in_scope;
442ddf59 16121 }
c906108c
SS
16122 }
16123 break;
16124 case DW_TAG_formal_parameter:
edb3359d
DJ
16125 /* If we are inside a function, mark this as an argument. If
16126 not, we might be looking at an argument to an inlined function
16127 when we do not have enough information to show inlined frames;
16128 pretend it's a local variable in that case so that the user can
16129 still see it. */
16130 if (context_stack_depth > 0
16131 && context_stack[context_stack_depth - 1].name != NULL)
16132 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16133 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16134 if (attr)
16135 {
e7c27a73 16136 var_decode_location (attr, sym, cu);
c906108c 16137 }
e142c38c 16138 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16139 if (attr)
16140 {
e7c27a73 16141 dwarf2_const_value (attr, sym, cu);
c906108c 16142 }
f346a30d 16143
e37fd15a 16144 list_to_add = cu->list_in_scope;
c906108c
SS
16145 break;
16146 case DW_TAG_unspecified_parameters:
16147 /* From varargs functions; gdb doesn't seem to have any
16148 interest in this information, so just ignore it for now.
16149 (FIXME?) */
16150 break;
34eaf542
TT
16151 case DW_TAG_template_type_param:
16152 suppress_add = 1;
16153 /* Fall through. */
c906108c 16154 case DW_TAG_class_type:
680b30c7 16155 case DW_TAG_interface_type:
c906108c
SS
16156 case DW_TAG_structure_type:
16157 case DW_TAG_union_type:
72019c9c 16158 case DW_TAG_set_type:
c906108c 16159 case DW_TAG_enumeration_type:
f1e6e072 16160 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16161 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16162
63d06c5c 16163 {
987504bb 16164 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16165 really ever be static objects: otherwise, if you try
16166 to, say, break of a class's method and you're in a file
16167 which doesn't mention that class, it won't work unless
16168 the check for all static symbols in lookup_symbol_aux
16169 saves you. See the OtherFileClass tests in
16170 gdb.c++/namespace.exp. */
16171
e37fd15a 16172 if (!suppress_add)
34eaf542 16173 {
34eaf542
TT
16174 list_to_add = (cu->list_in_scope == &file_symbols
16175 && (cu->language == language_cplus
16176 || cu->language == language_java)
16177 ? &global_symbols : cu->list_in_scope);
63d06c5c 16178
64382290
TT
16179 /* The semantics of C++ state that "struct foo {
16180 ... }" also defines a typedef for "foo". A Java
16181 class declaration also defines a typedef for the
16182 class. */
16183 if (cu->language == language_cplus
16184 || cu->language == language_java
16185 || cu->language == language_ada)
16186 {
16187 /* The symbol's name is already allocated along
16188 with this objfile, so we don't need to
16189 duplicate it for the type. */
16190 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16191 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16192 }
63d06c5c
DC
16193 }
16194 }
c906108c
SS
16195 break;
16196 case DW_TAG_typedef:
f1e6e072 16197 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16198 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16199 list_to_add = cu->list_in_scope;
63d06c5c 16200 break;
c906108c 16201 case DW_TAG_base_type:
a02abb62 16202 case DW_TAG_subrange_type:
f1e6e072 16203 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16204 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16205 list_to_add = cu->list_in_scope;
c906108c
SS
16206 break;
16207 case DW_TAG_enumerator:
e142c38c 16208 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16209 if (attr)
16210 {
e7c27a73 16211 dwarf2_const_value (attr, sym, cu);
c906108c 16212 }
63d06c5c
DC
16213 {
16214 /* NOTE: carlton/2003-11-10: See comment above in the
16215 DW_TAG_class_type, etc. block. */
16216
e142c38c 16217 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16218 && (cu->language == language_cplus
16219 || cu->language == language_java)
e142c38c 16220 ? &global_symbols : cu->list_in_scope);
63d06c5c 16221 }
c906108c 16222 break;
5c4e30ca 16223 case DW_TAG_namespace:
f1e6e072 16224 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16225 list_to_add = &global_symbols;
5c4e30ca 16226 break;
4357ac6c 16227 case DW_TAG_common_block:
f1e6e072 16228 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16229 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16230 add_symbol_to_list (sym, cu->list_in_scope);
16231 break;
c906108c
SS
16232 default:
16233 /* Not a tag we recognize. Hopefully we aren't processing
16234 trash data, but since we must specifically ignore things
16235 we don't recognize, there is nothing else we should do at
0963b4bd 16236 this point. */
e2e0b3e5 16237 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16238 dwarf_tag_name (die->tag));
c906108c
SS
16239 break;
16240 }
df8a16a1 16241
e37fd15a
SW
16242 if (suppress_add)
16243 {
16244 sym->hash_next = objfile->template_symbols;
16245 objfile->template_symbols = sym;
16246 list_to_add = NULL;
16247 }
16248
16249 if (list_to_add != NULL)
16250 add_symbol_to_list (sym, list_to_add);
16251
df8a16a1
DJ
16252 /* For the benefit of old versions of GCC, check for anonymous
16253 namespaces based on the demangled name. */
4d4ec4e5 16254 if (!cu->processing_has_namespace_info
94af9270 16255 && cu->language == language_cplus)
a10964d1 16256 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16257 }
16258 return (sym);
16259}
16260
34eaf542
TT
16261/* A wrapper for new_symbol_full that always allocates a new symbol. */
16262
16263static struct symbol *
16264new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16265{
16266 return new_symbol_full (die, type, cu, NULL);
16267}
16268
98bfdba5
PA
16269/* Given an attr with a DW_FORM_dataN value in host byte order,
16270 zero-extend it as appropriate for the symbol's type. The DWARF
16271 standard (v4) is not entirely clear about the meaning of using
16272 DW_FORM_dataN for a constant with a signed type, where the type is
16273 wider than the data. The conclusion of a discussion on the DWARF
16274 list was that this is unspecified. We choose to always zero-extend
16275 because that is the interpretation long in use by GCC. */
c906108c 16276
98bfdba5
PA
16277static gdb_byte *
16278dwarf2_const_value_data (struct attribute *attr, struct type *type,
16279 const char *name, struct obstack *obstack,
12df843f 16280 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16281{
e7c27a73 16282 struct objfile *objfile = cu->objfile;
e17a4113
UW
16283 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16284 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16285 LONGEST l = DW_UNSND (attr);
16286
16287 if (bits < sizeof (*value) * 8)
16288 {
16289 l &= ((LONGEST) 1 << bits) - 1;
16290 *value = l;
16291 }
16292 else if (bits == sizeof (*value) * 8)
16293 *value = l;
16294 else
16295 {
16296 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16297 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16298 return bytes;
16299 }
16300
16301 return NULL;
16302}
16303
16304/* Read a constant value from an attribute. Either set *VALUE, or if
16305 the value does not fit in *VALUE, set *BYTES - either already
16306 allocated on the objfile obstack, or newly allocated on OBSTACK,
16307 or, set *BATON, if we translated the constant to a location
16308 expression. */
16309
16310static void
16311dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16312 const char *name, struct obstack *obstack,
16313 struct dwarf2_cu *cu,
12df843f 16314 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16315 struct dwarf2_locexpr_baton **baton)
16316{
16317 struct objfile *objfile = cu->objfile;
16318 struct comp_unit_head *cu_header = &cu->header;
c906108c 16319 struct dwarf_block *blk;
98bfdba5
PA
16320 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16321 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16322
16323 *value = 0;
16324 *bytes = NULL;
16325 *baton = NULL;
c906108c
SS
16326
16327 switch (attr->form)
16328 {
16329 case DW_FORM_addr:
3019eac3 16330 case DW_FORM_GNU_addr_index:
ac56253d 16331 {
ac56253d
TT
16332 gdb_byte *data;
16333
98bfdba5
PA
16334 if (TYPE_LENGTH (type) != cu_header->addr_size)
16335 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16336 cu_header->addr_size,
98bfdba5 16337 TYPE_LENGTH (type));
ac56253d
TT
16338 /* Symbols of this form are reasonably rare, so we just
16339 piggyback on the existing location code rather than writing
16340 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16341 *baton = obstack_alloc (&objfile->objfile_obstack,
16342 sizeof (struct dwarf2_locexpr_baton));
16343 (*baton)->per_cu = cu->per_cu;
16344 gdb_assert ((*baton)->per_cu);
ac56253d 16345
98bfdba5
PA
16346 (*baton)->size = 2 + cu_header->addr_size;
16347 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16348 (*baton)->data = data;
ac56253d
TT
16349
16350 data[0] = DW_OP_addr;
16351 store_unsigned_integer (&data[1], cu_header->addr_size,
16352 byte_order, DW_ADDR (attr));
16353 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16354 }
c906108c 16355 break;
4ac36638 16356 case DW_FORM_string:
93b5768b 16357 case DW_FORM_strp:
3019eac3 16358 case DW_FORM_GNU_str_index:
36586728 16359 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16360 /* DW_STRING is already allocated on the objfile obstack, point
16361 directly to it. */
16362 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16363 break;
c906108c
SS
16364 case DW_FORM_block1:
16365 case DW_FORM_block2:
16366 case DW_FORM_block4:
16367 case DW_FORM_block:
2dc7f7b3 16368 case DW_FORM_exprloc:
c906108c 16369 blk = DW_BLOCK (attr);
98bfdba5
PA
16370 if (TYPE_LENGTH (type) != blk->size)
16371 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16372 TYPE_LENGTH (type));
16373 *bytes = blk->data;
c906108c 16374 break;
2df3850c
JM
16375
16376 /* The DW_AT_const_value attributes are supposed to carry the
16377 symbol's value "represented as it would be on the target
16378 architecture." By the time we get here, it's already been
16379 converted to host endianness, so we just need to sign- or
16380 zero-extend it as appropriate. */
16381 case DW_FORM_data1:
3e43a32a
MS
16382 *bytes = dwarf2_const_value_data (attr, type, name,
16383 obstack, cu, value, 8);
2df3850c 16384 break;
c906108c 16385 case DW_FORM_data2:
3e43a32a
MS
16386 *bytes = dwarf2_const_value_data (attr, type, name,
16387 obstack, cu, value, 16);
2df3850c 16388 break;
c906108c 16389 case DW_FORM_data4:
3e43a32a
MS
16390 *bytes = dwarf2_const_value_data (attr, type, name,
16391 obstack, cu, value, 32);
2df3850c 16392 break;
c906108c 16393 case DW_FORM_data8:
3e43a32a
MS
16394 *bytes = dwarf2_const_value_data (attr, type, name,
16395 obstack, cu, value, 64);
2df3850c
JM
16396 break;
16397
c906108c 16398 case DW_FORM_sdata:
98bfdba5 16399 *value = DW_SND (attr);
2df3850c
JM
16400 break;
16401
c906108c 16402 case DW_FORM_udata:
98bfdba5 16403 *value = DW_UNSND (attr);
c906108c 16404 break;
2df3850c 16405
c906108c 16406 default:
4d3c2250 16407 complaint (&symfile_complaints,
e2e0b3e5 16408 _("unsupported const value attribute form: '%s'"),
4d3c2250 16409 dwarf_form_name (attr->form));
98bfdba5 16410 *value = 0;
c906108c
SS
16411 break;
16412 }
16413}
16414
2df3850c 16415
98bfdba5
PA
16416/* Copy constant value from an attribute to a symbol. */
16417
2df3850c 16418static void
98bfdba5
PA
16419dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16420 struct dwarf2_cu *cu)
2df3850c 16421{
98bfdba5
PA
16422 struct objfile *objfile = cu->objfile;
16423 struct comp_unit_head *cu_header = &cu->header;
12df843f 16424 LONGEST value;
98bfdba5
PA
16425 gdb_byte *bytes;
16426 struct dwarf2_locexpr_baton *baton;
2df3850c 16427
98bfdba5
PA
16428 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16429 SYMBOL_PRINT_NAME (sym),
16430 &objfile->objfile_obstack, cu,
16431 &value, &bytes, &baton);
2df3850c 16432
98bfdba5
PA
16433 if (baton != NULL)
16434 {
98bfdba5 16435 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16436 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16437 }
16438 else if (bytes != NULL)
16439 {
16440 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16441 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16442 }
16443 else
16444 {
16445 SYMBOL_VALUE (sym) = value;
f1e6e072 16446 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16447 }
2df3850c
JM
16448}
16449
c906108c
SS
16450/* Return the type of the die in question using its DW_AT_type attribute. */
16451
16452static struct type *
e7c27a73 16453die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16454{
c906108c 16455 struct attribute *type_attr;
c906108c 16456
e142c38c 16457 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16458 if (!type_attr)
16459 {
16460 /* A missing DW_AT_type represents a void type. */
46bf5051 16461 return objfile_type (cu->objfile)->builtin_void;
c906108c 16462 }
348e048f 16463
673bfd45 16464 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16465}
16466
b4ba55a1
JB
16467/* True iff CU's producer generates GNAT Ada auxiliary information
16468 that allows to find parallel types through that information instead
16469 of having to do expensive parallel lookups by type name. */
16470
16471static int
16472need_gnat_info (struct dwarf2_cu *cu)
16473{
16474 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16475 of GNAT produces this auxiliary information, without any indication
16476 that it is produced. Part of enhancing the FSF version of GNAT
16477 to produce that information will be to put in place an indicator
16478 that we can use in order to determine whether the descriptive type
16479 info is available or not. One suggestion that has been made is
16480 to use a new attribute, attached to the CU die. For now, assume
16481 that the descriptive type info is not available. */
16482 return 0;
16483}
16484
b4ba55a1
JB
16485/* Return the auxiliary type of the die in question using its
16486 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16487 attribute is not present. */
16488
16489static struct type *
16490die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16491{
b4ba55a1 16492 struct attribute *type_attr;
b4ba55a1
JB
16493
16494 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16495 if (!type_attr)
16496 return NULL;
16497
673bfd45 16498 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16499}
16500
16501/* If DIE has a descriptive_type attribute, then set the TYPE's
16502 descriptive type accordingly. */
16503
16504static void
16505set_descriptive_type (struct type *type, struct die_info *die,
16506 struct dwarf2_cu *cu)
16507{
16508 struct type *descriptive_type = die_descriptive_type (die, cu);
16509
16510 if (descriptive_type)
16511 {
16512 ALLOCATE_GNAT_AUX_TYPE (type);
16513 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16514 }
16515}
16516
c906108c
SS
16517/* Return the containing type of the die in question using its
16518 DW_AT_containing_type attribute. */
16519
16520static struct type *
e7c27a73 16521die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16522{
c906108c 16523 struct attribute *type_attr;
c906108c 16524
e142c38c 16525 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16526 if (!type_attr)
16527 error (_("Dwarf Error: Problem turning containing type into gdb type "
16528 "[in module %s]"), cu->objfile->name);
16529
673bfd45 16530 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16531}
16532
673bfd45
DE
16533/* Look up the type of DIE in CU using its type attribute ATTR.
16534 If there is no type substitute an error marker. */
16535
c906108c 16536static struct type *
673bfd45
DE
16537lookup_die_type (struct die_info *die, struct attribute *attr,
16538 struct dwarf2_cu *cu)
c906108c 16539{
bb5ed363 16540 struct objfile *objfile = cu->objfile;
f792889a
DJ
16541 struct type *this_type;
16542
673bfd45
DE
16543 /* First see if we have it cached. */
16544
36586728
TT
16545 if (attr->form == DW_FORM_GNU_ref_alt)
16546 {
16547 struct dwarf2_per_cu_data *per_cu;
16548 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16549
16550 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16551 this_type = get_die_type_at_offset (offset, per_cu);
16552 }
16553 else if (is_ref_attr (attr))
673bfd45 16554 {
b64f50a1 16555 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16556
16557 this_type = get_die_type_at_offset (offset, cu->per_cu);
16558 }
55f1336d 16559 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16560 {
16561 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16562
16563 /* sig_type will be NULL if the signatured type is missing from
16564 the debug info. */
16565 if (sig_type == NULL)
16566 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16567 "at 0x%x [in module %s]"),
b64f50a1 16568 die->offset.sect_off, objfile->name);
673bfd45 16569
3019eac3
DE
16570 gdb_assert (sig_type->per_cu.is_debug_types);
16571 /* If we haven't filled in type_offset_in_section yet, then we
16572 haven't read the type in yet. */
16573 this_type = NULL;
16574 if (sig_type->type_offset_in_section.sect_off != 0)
16575 {
16576 this_type =
16577 get_die_type_at_offset (sig_type->type_offset_in_section,
16578 &sig_type->per_cu);
16579 }
673bfd45
DE
16580 }
16581 else
16582 {
16583 dump_die_for_error (die);
16584 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16585 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16586 }
16587
16588 /* If not cached we need to read it in. */
16589
16590 if (this_type == NULL)
16591 {
16592 struct die_info *type_die;
16593 struct dwarf2_cu *type_cu = cu;
16594
16595 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16596 /* If we found the type now, it's probably because the type came
16597 from an inter-CU reference and the type's CU got expanded before
16598 ours. */
16599 this_type = get_die_type (type_die, type_cu);
16600 if (this_type == NULL)
16601 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16602 }
16603
16604 /* If we still don't have a type use an error marker. */
16605
16606 if (this_type == NULL)
c906108c 16607 {
b00fdb78
TT
16608 char *message, *saved;
16609
16610 /* read_type_die already issued a complaint. */
16611 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16612 objfile->name,
b64f50a1
JK
16613 cu->header.offset.sect_off,
16614 die->offset.sect_off);
bb5ed363 16615 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16616 message, strlen (message));
16617 xfree (message);
16618
bb5ed363 16619 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16620 }
673bfd45 16621
f792889a 16622 return this_type;
c906108c
SS
16623}
16624
673bfd45
DE
16625/* Return the type in DIE, CU.
16626 Returns NULL for invalid types.
16627
16628 This first does a lookup in the appropriate type_hash table,
16629 and only reads the die in if necessary.
16630
16631 NOTE: This can be called when reading in partial or full symbols. */
16632
f792889a 16633static struct type *
e7c27a73 16634read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16635{
f792889a
DJ
16636 struct type *this_type;
16637
16638 this_type = get_die_type (die, cu);
16639 if (this_type)
16640 return this_type;
16641
673bfd45
DE
16642 return read_type_die_1 (die, cu);
16643}
16644
16645/* Read the type in DIE, CU.
16646 Returns NULL for invalid types. */
16647
16648static struct type *
16649read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16650{
16651 struct type *this_type = NULL;
16652
c906108c
SS
16653 switch (die->tag)
16654 {
16655 case DW_TAG_class_type:
680b30c7 16656 case DW_TAG_interface_type:
c906108c
SS
16657 case DW_TAG_structure_type:
16658 case DW_TAG_union_type:
f792889a 16659 this_type = read_structure_type (die, cu);
c906108c
SS
16660 break;
16661 case DW_TAG_enumeration_type:
f792889a 16662 this_type = read_enumeration_type (die, cu);
c906108c
SS
16663 break;
16664 case DW_TAG_subprogram:
16665 case DW_TAG_subroutine_type:
edb3359d 16666 case DW_TAG_inlined_subroutine:
f792889a 16667 this_type = read_subroutine_type (die, cu);
c906108c
SS
16668 break;
16669 case DW_TAG_array_type:
f792889a 16670 this_type = read_array_type (die, cu);
c906108c 16671 break;
72019c9c 16672 case DW_TAG_set_type:
f792889a 16673 this_type = read_set_type (die, cu);
72019c9c 16674 break;
c906108c 16675 case DW_TAG_pointer_type:
f792889a 16676 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16677 break;
16678 case DW_TAG_ptr_to_member_type:
f792889a 16679 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16680 break;
16681 case DW_TAG_reference_type:
f792889a 16682 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16683 break;
16684 case DW_TAG_const_type:
f792889a 16685 this_type = read_tag_const_type (die, cu);
c906108c
SS
16686 break;
16687 case DW_TAG_volatile_type:
f792889a 16688 this_type = read_tag_volatile_type (die, cu);
c906108c 16689 break;
06d66ee9
TT
16690 case DW_TAG_restrict_type:
16691 this_type = read_tag_restrict_type (die, cu);
16692 break;
c906108c 16693 case DW_TAG_string_type:
f792889a 16694 this_type = read_tag_string_type (die, cu);
c906108c
SS
16695 break;
16696 case DW_TAG_typedef:
f792889a 16697 this_type = read_typedef (die, cu);
c906108c 16698 break;
a02abb62 16699 case DW_TAG_subrange_type:
f792889a 16700 this_type = read_subrange_type (die, cu);
a02abb62 16701 break;
c906108c 16702 case DW_TAG_base_type:
f792889a 16703 this_type = read_base_type (die, cu);
c906108c 16704 break;
81a17f79 16705 case DW_TAG_unspecified_type:
f792889a 16706 this_type = read_unspecified_type (die, cu);
81a17f79 16707 break;
0114d602
DJ
16708 case DW_TAG_namespace:
16709 this_type = read_namespace_type (die, cu);
16710 break;
f55ee35c
JK
16711 case DW_TAG_module:
16712 this_type = read_module_type (die, cu);
16713 break;
c906108c 16714 default:
3e43a32a
MS
16715 complaint (&symfile_complaints,
16716 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16717 dwarf_tag_name (die->tag));
c906108c
SS
16718 break;
16719 }
63d06c5c 16720
f792889a 16721 return this_type;
63d06c5c
DC
16722}
16723
abc72ce4
DE
16724/* See if we can figure out if the class lives in a namespace. We do
16725 this by looking for a member function; its demangled name will
16726 contain namespace info, if there is any.
16727 Return the computed name or NULL.
16728 Space for the result is allocated on the objfile's obstack.
16729 This is the full-die version of guess_partial_die_structure_name.
16730 In this case we know DIE has no useful parent. */
16731
16732static char *
16733guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16734{
16735 struct die_info *spec_die;
16736 struct dwarf2_cu *spec_cu;
16737 struct die_info *child;
16738
16739 spec_cu = cu;
16740 spec_die = die_specification (die, &spec_cu);
16741 if (spec_die != NULL)
16742 {
16743 die = spec_die;
16744 cu = spec_cu;
16745 }
16746
16747 for (child = die->child;
16748 child != NULL;
16749 child = child->sibling)
16750 {
16751 if (child->tag == DW_TAG_subprogram)
16752 {
16753 struct attribute *attr;
16754
16755 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16756 if (attr == NULL)
16757 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16758 if (attr != NULL)
16759 {
16760 char *actual_name
16761 = language_class_name_from_physname (cu->language_defn,
16762 DW_STRING (attr));
16763 char *name = NULL;
16764
16765 if (actual_name != NULL)
16766 {
15d034d0 16767 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16768
16769 if (die_name != NULL
16770 && strcmp (die_name, actual_name) != 0)
16771 {
16772 /* Strip off the class name from the full name.
16773 We want the prefix. */
16774 int die_name_len = strlen (die_name);
16775 int actual_name_len = strlen (actual_name);
16776
16777 /* Test for '::' as a sanity check. */
16778 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16779 && actual_name[actual_name_len
16780 - die_name_len - 1] == ':')
abc72ce4 16781 name =
10f0c4bb
TT
16782 obstack_copy0 (&cu->objfile->objfile_obstack,
16783 actual_name,
16784 actual_name_len - die_name_len - 2);
abc72ce4
DE
16785 }
16786 }
16787 xfree (actual_name);
16788 return name;
16789 }
16790 }
16791 }
16792
16793 return NULL;
16794}
16795
96408a79
SA
16796/* GCC might emit a nameless typedef that has a linkage name. Determine the
16797 prefix part in such case. See
16798 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16799
16800static char *
16801anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16802{
16803 struct attribute *attr;
16804 char *base;
16805
16806 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16807 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16808 return NULL;
16809
16810 attr = dwarf2_attr (die, DW_AT_name, cu);
16811 if (attr != NULL && DW_STRING (attr) != NULL)
16812 return NULL;
16813
16814 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16815 if (attr == NULL)
16816 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16817 if (attr == NULL || DW_STRING (attr) == NULL)
16818 return NULL;
16819
16820 /* dwarf2_name had to be already called. */
16821 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16822
16823 /* Strip the base name, keep any leading namespaces/classes. */
16824 base = strrchr (DW_STRING (attr), ':');
16825 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16826 return "";
16827
10f0c4bb
TT
16828 return obstack_copy0 (&cu->objfile->objfile_obstack,
16829 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16830}
16831
fdde2d81 16832/* Return the name of the namespace/class that DIE is defined within,
0114d602 16833 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16834
0114d602
DJ
16835 For example, if we're within the method foo() in the following
16836 code:
16837
16838 namespace N {
16839 class C {
16840 void foo () {
16841 }
16842 };
16843 }
16844
16845 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16846
0d5cff50 16847static const char *
e142c38c 16848determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16849{
0114d602
DJ
16850 struct die_info *parent, *spec_die;
16851 struct dwarf2_cu *spec_cu;
16852 struct type *parent_type;
96408a79 16853 char *retval;
63d06c5c 16854
f55ee35c
JK
16855 if (cu->language != language_cplus && cu->language != language_java
16856 && cu->language != language_fortran)
0114d602
DJ
16857 return "";
16858
96408a79
SA
16859 retval = anonymous_struct_prefix (die, cu);
16860 if (retval)
16861 return retval;
16862
0114d602
DJ
16863 /* We have to be careful in the presence of DW_AT_specification.
16864 For example, with GCC 3.4, given the code
16865
16866 namespace N {
16867 void foo() {
16868 // Definition of N::foo.
16869 }
16870 }
16871
16872 then we'll have a tree of DIEs like this:
16873
16874 1: DW_TAG_compile_unit
16875 2: DW_TAG_namespace // N
16876 3: DW_TAG_subprogram // declaration of N::foo
16877 4: DW_TAG_subprogram // definition of N::foo
16878 DW_AT_specification // refers to die #3
16879
16880 Thus, when processing die #4, we have to pretend that we're in
16881 the context of its DW_AT_specification, namely the contex of die
16882 #3. */
16883 spec_cu = cu;
16884 spec_die = die_specification (die, &spec_cu);
16885 if (spec_die == NULL)
16886 parent = die->parent;
16887 else
63d06c5c 16888 {
0114d602
DJ
16889 parent = spec_die->parent;
16890 cu = spec_cu;
63d06c5c 16891 }
0114d602
DJ
16892
16893 if (parent == NULL)
16894 return "";
98bfdba5
PA
16895 else if (parent->building_fullname)
16896 {
16897 const char *name;
16898 const char *parent_name;
16899
16900 /* It has been seen on RealView 2.2 built binaries,
16901 DW_TAG_template_type_param types actually _defined_ as
16902 children of the parent class:
16903
16904 enum E {};
16905 template class <class Enum> Class{};
16906 Class<enum E> class_e;
16907
16908 1: DW_TAG_class_type (Class)
16909 2: DW_TAG_enumeration_type (E)
16910 3: DW_TAG_enumerator (enum1:0)
16911 3: DW_TAG_enumerator (enum2:1)
16912 ...
16913 2: DW_TAG_template_type_param
16914 DW_AT_type DW_FORM_ref_udata (E)
16915
16916 Besides being broken debug info, it can put GDB into an
16917 infinite loop. Consider:
16918
16919 When we're building the full name for Class<E>, we'll start
16920 at Class, and go look over its template type parameters,
16921 finding E. We'll then try to build the full name of E, and
16922 reach here. We're now trying to build the full name of E,
16923 and look over the parent DIE for containing scope. In the
16924 broken case, if we followed the parent DIE of E, we'd again
16925 find Class, and once again go look at its template type
16926 arguments, etc., etc. Simply don't consider such parent die
16927 as source-level parent of this die (it can't be, the language
16928 doesn't allow it), and break the loop here. */
16929 name = dwarf2_name (die, cu);
16930 parent_name = dwarf2_name (parent, cu);
16931 complaint (&symfile_complaints,
16932 _("template param type '%s' defined within parent '%s'"),
16933 name ? name : "<unknown>",
16934 parent_name ? parent_name : "<unknown>");
16935 return "";
16936 }
63d06c5c 16937 else
0114d602
DJ
16938 switch (parent->tag)
16939 {
63d06c5c 16940 case DW_TAG_namespace:
0114d602 16941 parent_type = read_type_die (parent, cu);
acebe513
UW
16942 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16943 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16944 Work around this problem here. */
16945 if (cu->language == language_cplus
16946 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16947 return "";
0114d602
DJ
16948 /* We give a name to even anonymous namespaces. */
16949 return TYPE_TAG_NAME (parent_type);
63d06c5c 16950 case DW_TAG_class_type:
680b30c7 16951 case DW_TAG_interface_type:
63d06c5c 16952 case DW_TAG_structure_type:
0114d602 16953 case DW_TAG_union_type:
f55ee35c 16954 case DW_TAG_module:
0114d602
DJ
16955 parent_type = read_type_die (parent, cu);
16956 if (TYPE_TAG_NAME (parent_type) != NULL)
16957 return TYPE_TAG_NAME (parent_type);
16958 else
16959 /* An anonymous structure is only allowed non-static data
16960 members; no typedefs, no member functions, et cetera.
16961 So it does not need a prefix. */
16962 return "";
abc72ce4 16963 case DW_TAG_compile_unit:
95554aad 16964 case DW_TAG_partial_unit:
abc72ce4
DE
16965 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16966 if (cu->language == language_cplus
8b70b953 16967 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16968 && die->child != NULL
16969 && (die->tag == DW_TAG_class_type
16970 || die->tag == DW_TAG_structure_type
16971 || die->tag == DW_TAG_union_type))
16972 {
16973 char *name = guess_full_die_structure_name (die, cu);
16974 if (name != NULL)
16975 return name;
16976 }
16977 return "";
63d06c5c 16978 default:
8176b9b8 16979 return determine_prefix (parent, cu);
63d06c5c 16980 }
63d06c5c
DC
16981}
16982
3e43a32a
MS
16983/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16984 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16985 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16986 an obconcat, otherwise allocate storage for the result. The CU argument is
16987 used to determine the language and hence, the appropriate separator. */
987504bb 16988
f55ee35c 16989#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16990
16991static char *
f55ee35c
JK
16992typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16993 int physname, struct dwarf2_cu *cu)
63d06c5c 16994{
f55ee35c 16995 const char *lead = "";
5c315b68 16996 const char *sep;
63d06c5c 16997
3e43a32a
MS
16998 if (suffix == NULL || suffix[0] == '\0'
16999 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17000 sep = "";
17001 else if (cu->language == language_java)
17002 sep = ".";
f55ee35c
JK
17003 else if (cu->language == language_fortran && physname)
17004 {
17005 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17006 DW_AT_MIPS_linkage_name is preferred and used instead. */
17007
17008 lead = "__";
17009 sep = "_MOD_";
17010 }
987504bb
JJ
17011 else
17012 sep = "::";
63d06c5c 17013
6dd47d34
DE
17014 if (prefix == NULL)
17015 prefix = "";
17016 if (suffix == NULL)
17017 suffix = "";
17018
987504bb
JJ
17019 if (obs == NULL)
17020 {
3e43a32a
MS
17021 char *retval
17022 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17023
f55ee35c
JK
17024 strcpy (retval, lead);
17025 strcat (retval, prefix);
6dd47d34
DE
17026 strcat (retval, sep);
17027 strcat (retval, suffix);
63d06c5c
DC
17028 return retval;
17029 }
987504bb
JJ
17030 else
17031 {
17032 /* We have an obstack. */
f55ee35c 17033 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17034 }
63d06c5c
DC
17035}
17036
c906108c
SS
17037/* Return sibling of die, NULL if no sibling. */
17038
f9aca02d 17039static struct die_info *
fba45db2 17040sibling_die (struct die_info *die)
c906108c 17041{
639d11d3 17042 return die->sibling;
c906108c
SS
17043}
17044
71c25dea
TT
17045/* Get name of a die, return NULL if not found. */
17046
15d034d0
TT
17047static const char *
17048dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17049 struct obstack *obstack)
17050{
17051 if (name && cu->language == language_cplus)
17052 {
17053 char *canon_name = cp_canonicalize_string (name);
17054
17055 if (canon_name != NULL)
17056 {
17057 if (strcmp (canon_name, name) != 0)
10f0c4bb 17058 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17059 xfree (canon_name);
17060 }
17061 }
17062
17063 return name;
c906108c
SS
17064}
17065
9219021c
DC
17066/* Get name of a die, return NULL if not found. */
17067
15d034d0 17068static const char *
e142c38c 17069dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17070{
17071 struct attribute *attr;
17072
e142c38c 17073 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17074 if ((!attr || !DW_STRING (attr))
17075 && die->tag != DW_TAG_class_type
17076 && die->tag != DW_TAG_interface_type
17077 && die->tag != DW_TAG_structure_type
17078 && die->tag != DW_TAG_union_type)
71c25dea
TT
17079 return NULL;
17080
17081 switch (die->tag)
17082 {
17083 case DW_TAG_compile_unit:
95554aad 17084 case DW_TAG_partial_unit:
71c25dea
TT
17085 /* Compilation units have a DW_AT_name that is a filename, not
17086 a source language identifier. */
17087 case DW_TAG_enumeration_type:
17088 case DW_TAG_enumerator:
17089 /* These tags always have simple identifiers already; no need
17090 to canonicalize them. */
17091 return DW_STRING (attr);
907af001 17092
418835cc
KS
17093 case DW_TAG_subprogram:
17094 /* Java constructors will all be named "<init>", so return
17095 the class name when we see this special case. */
17096 if (cu->language == language_java
17097 && DW_STRING (attr) != NULL
17098 && strcmp (DW_STRING (attr), "<init>") == 0)
17099 {
17100 struct dwarf2_cu *spec_cu = cu;
17101 struct die_info *spec_die;
17102
17103 /* GCJ will output '<init>' for Java constructor names.
17104 For this special case, return the name of the parent class. */
17105
17106 /* GCJ may output suprogram DIEs with AT_specification set.
17107 If so, use the name of the specified DIE. */
17108 spec_die = die_specification (die, &spec_cu);
17109 if (spec_die != NULL)
17110 return dwarf2_name (spec_die, spec_cu);
17111
17112 do
17113 {
17114 die = die->parent;
17115 if (die->tag == DW_TAG_class_type)
17116 return dwarf2_name (die, cu);
17117 }
95554aad
TT
17118 while (die->tag != DW_TAG_compile_unit
17119 && die->tag != DW_TAG_partial_unit);
418835cc 17120 }
907af001
UW
17121 break;
17122
17123 case DW_TAG_class_type:
17124 case DW_TAG_interface_type:
17125 case DW_TAG_structure_type:
17126 case DW_TAG_union_type:
17127 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17128 structures or unions. These were of the form "._%d" in GCC 4.1,
17129 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17130 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17131 if (attr && DW_STRING (attr)
17132 && (strncmp (DW_STRING (attr), "._", 2) == 0
17133 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17134 return NULL;
53832f31
TT
17135
17136 /* GCC might emit a nameless typedef that has a linkage name. See
17137 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17138 if (!attr || DW_STRING (attr) == NULL)
17139 {
df5c6c50 17140 char *demangled = NULL;
53832f31
TT
17141
17142 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17143 if (attr == NULL)
17144 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17145
17146 if (attr == NULL || DW_STRING (attr) == NULL)
17147 return NULL;
17148
df5c6c50
JK
17149 /* Avoid demangling DW_STRING (attr) the second time on a second
17150 call for the same DIE. */
17151 if (!DW_STRING_IS_CANONICAL (attr))
17152 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17153
17154 if (demangled)
17155 {
96408a79
SA
17156 char *base;
17157
53832f31 17158 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17159 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17160 demangled, strlen (demangled));
53832f31
TT
17161 DW_STRING_IS_CANONICAL (attr) = 1;
17162 xfree (demangled);
96408a79
SA
17163
17164 /* Strip any leading namespaces/classes, keep only the base name.
17165 DW_AT_name for named DIEs does not contain the prefixes. */
17166 base = strrchr (DW_STRING (attr), ':');
17167 if (base && base > DW_STRING (attr) && base[-1] == ':')
17168 return &base[1];
17169 else
17170 return DW_STRING (attr);
53832f31
TT
17171 }
17172 }
907af001
UW
17173 break;
17174
71c25dea 17175 default:
907af001
UW
17176 break;
17177 }
17178
17179 if (!DW_STRING_IS_CANONICAL (attr))
17180 {
17181 DW_STRING (attr)
17182 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17183 &cu->objfile->objfile_obstack);
17184 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17185 }
907af001 17186 return DW_STRING (attr);
9219021c
DC
17187}
17188
17189/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17190 is none. *EXT_CU is the CU containing DIE on input, and the CU
17191 containing the return value on output. */
9219021c
DC
17192
17193static struct die_info *
f2f0e013 17194dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17195{
17196 struct attribute *attr;
9219021c 17197
f2f0e013 17198 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17199 if (attr == NULL)
17200 return NULL;
17201
f2f0e013 17202 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17203}
17204
c906108c
SS
17205/* Convert a DIE tag into its string name. */
17206
f39c6ffd 17207static const char *
aa1ee363 17208dwarf_tag_name (unsigned tag)
c906108c 17209{
f39c6ffd
TT
17210 const char *name = get_DW_TAG_name (tag);
17211
17212 if (name == NULL)
17213 return "DW_TAG_<unknown>";
17214
17215 return name;
c906108c
SS
17216}
17217
17218/* Convert a DWARF attribute code into its string name. */
17219
f39c6ffd 17220static const char *
aa1ee363 17221dwarf_attr_name (unsigned attr)
c906108c 17222{
f39c6ffd
TT
17223 const char *name;
17224
c764a876 17225#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17226 if (attr == DW_AT_MIPS_fde)
17227 return "DW_AT_MIPS_fde";
17228#else
17229 if (attr == DW_AT_HP_block_index)
17230 return "DW_AT_HP_block_index";
c764a876 17231#endif
f39c6ffd
TT
17232
17233 name = get_DW_AT_name (attr);
17234
17235 if (name == NULL)
17236 return "DW_AT_<unknown>";
17237
17238 return name;
c906108c
SS
17239}
17240
17241/* Convert a DWARF value form code into its string name. */
17242
f39c6ffd 17243static const char *
aa1ee363 17244dwarf_form_name (unsigned form)
c906108c 17245{
f39c6ffd
TT
17246 const char *name = get_DW_FORM_name (form);
17247
17248 if (name == NULL)
17249 return "DW_FORM_<unknown>";
17250
17251 return name;
c906108c
SS
17252}
17253
17254static char *
fba45db2 17255dwarf_bool_name (unsigned mybool)
c906108c
SS
17256{
17257 if (mybool)
17258 return "TRUE";
17259 else
17260 return "FALSE";
17261}
17262
17263/* Convert a DWARF type code into its string name. */
17264
f39c6ffd 17265static const char *
aa1ee363 17266dwarf_type_encoding_name (unsigned enc)
c906108c 17267{
f39c6ffd 17268 const char *name = get_DW_ATE_name (enc);
c906108c 17269
f39c6ffd
TT
17270 if (name == NULL)
17271 return "DW_ATE_<unknown>";
c906108c 17272
f39c6ffd 17273 return name;
c906108c 17274}
c906108c 17275
f9aca02d 17276static void
d97bc12b 17277dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17278{
17279 unsigned int i;
17280
d97bc12b
DE
17281 print_spaces (indent, f);
17282 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17283 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17284
17285 if (die->parent != NULL)
17286 {
17287 print_spaces (indent, f);
17288 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17289 die->parent->offset.sect_off);
d97bc12b
DE
17290 }
17291
17292 print_spaces (indent, f);
17293 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17294 dwarf_bool_name (die->child != NULL));
c906108c 17295
d97bc12b
DE
17296 print_spaces (indent, f);
17297 fprintf_unfiltered (f, " attributes:\n");
17298
c906108c
SS
17299 for (i = 0; i < die->num_attrs; ++i)
17300 {
d97bc12b
DE
17301 print_spaces (indent, f);
17302 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17303 dwarf_attr_name (die->attrs[i].name),
17304 dwarf_form_name (die->attrs[i].form));
d97bc12b 17305
c906108c
SS
17306 switch (die->attrs[i].form)
17307 {
c906108c 17308 case DW_FORM_addr:
3019eac3 17309 case DW_FORM_GNU_addr_index:
d97bc12b 17310 fprintf_unfiltered (f, "address: ");
5af949e3 17311 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17312 break;
17313 case DW_FORM_block2:
17314 case DW_FORM_block4:
17315 case DW_FORM_block:
17316 case DW_FORM_block1:
56eb65bd
SP
17317 fprintf_unfiltered (f, "block: size %s",
17318 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17319 break;
2dc7f7b3 17320 case DW_FORM_exprloc:
56eb65bd
SP
17321 fprintf_unfiltered (f, "expression: size %s",
17322 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17323 break;
4568ecf9
DE
17324 case DW_FORM_ref_addr:
17325 fprintf_unfiltered (f, "ref address: ");
17326 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17327 break;
36586728
TT
17328 case DW_FORM_GNU_ref_alt:
17329 fprintf_unfiltered (f, "alt ref address: ");
17330 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17331 break;
10b3939b
DJ
17332 case DW_FORM_ref1:
17333 case DW_FORM_ref2:
17334 case DW_FORM_ref4:
4568ecf9
DE
17335 case DW_FORM_ref8:
17336 case DW_FORM_ref_udata:
d97bc12b 17337 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17338 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17339 break;
c906108c
SS
17340 case DW_FORM_data1:
17341 case DW_FORM_data2:
17342 case DW_FORM_data4:
ce5d95e1 17343 case DW_FORM_data8:
c906108c
SS
17344 case DW_FORM_udata:
17345 case DW_FORM_sdata:
43bbcdc2
PH
17346 fprintf_unfiltered (f, "constant: %s",
17347 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17348 break;
2dc7f7b3
TT
17349 case DW_FORM_sec_offset:
17350 fprintf_unfiltered (f, "section offset: %s",
17351 pulongest (DW_UNSND (&die->attrs[i])));
17352 break;
55f1336d 17353 case DW_FORM_ref_sig8:
348e048f 17354 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
e4a48d9d
DE
17355 {
17356 struct signatured_type *sig_type =
17357 DW_SIGNATURED_TYPE (&die->attrs[i]);
17358
17359 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17360 hex_string (sig_type->signature),
17361 sig_type->per_cu.offset.sect_off);
17362 }
348e048f 17363 else
e4a48d9d 17364 fprintf_unfiltered (f, "signatured type, unknown");
348e048f 17365 break;
c906108c 17366 case DW_FORM_string:
4bdf3d34 17367 case DW_FORM_strp:
3019eac3 17368 case DW_FORM_GNU_str_index:
36586728 17369 case DW_FORM_GNU_strp_alt:
8285870a 17370 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17371 DW_STRING (&die->attrs[i])
8285870a
JK
17372 ? DW_STRING (&die->attrs[i]) : "",
17373 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17374 break;
17375 case DW_FORM_flag:
17376 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17377 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17378 else
d97bc12b 17379 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17380 break;
2dc7f7b3
TT
17381 case DW_FORM_flag_present:
17382 fprintf_unfiltered (f, "flag: TRUE");
17383 break;
a8329558 17384 case DW_FORM_indirect:
0963b4bd
MS
17385 /* The reader will have reduced the indirect form to
17386 the "base form" so this form should not occur. */
3e43a32a
MS
17387 fprintf_unfiltered (f,
17388 "unexpected attribute form: DW_FORM_indirect");
a8329558 17389 break;
c906108c 17390 default:
d97bc12b 17391 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17392 die->attrs[i].form);
d97bc12b 17393 break;
c906108c 17394 }
d97bc12b 17395 fprintf_unfiltered (f, "\n");
c906108c
SS
17396 }
17397}
17398
f9aca02d 17399static void
d97bc12b 17400dump_die_for_error (struct die_info *die)
c906108c 17401{
d97bc12b
DE
17402 dump_die_shallow (gdb_stderr, 0, die);
17403}
17404
17405static void
17406dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17407{
17408 int indent = level * 4;
17409
17410 gdb_assert (die != NULL);
17411
17412 if (level >= max_level)
17413 return;
17414
17415 dump_die_shallow (f, indent, die);
17416
17417 if (die->child != NULL)
c906108c 17418 {
d97bc12b
DE
17419 print_spaces (indent, f);
17420 fprintf_unfiltered (f, " Children:");
17421 if (level + 1 < max_level)
17422 {
17423 fprintf_unfiltered (f, "\n");
17424 dump_die_1 (f, level + 1, max_level, die->child);
17425 }
17426 else
17427 {
3e43a32a
MS
17428 fprintf_unfiltered (f,
17429 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17430 }
17431 }
17432
17433 if (die->sibling != NULL && level > 0)
17434 {
17435 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17436 }
17437}
17438
d97bc12b
DE
17439/* This is called from the pdie macro in gdbinit.in.
17440 It's not static so gcc will keep a copy callable from gdb. */
17441
17442void
17443dump_die (struct die_info *die, int max_level)
17444{
17445 dump_die_1 (gdb_stdlog, 0, max_level, die);
17446}
17447
f9aca02d 17448static void
51545339 17449store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17450{
51545339 17451 void **slot;
c906108c 17452
b64f50a1
JK
17453 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17454 INSERT);
51545339
DJ
17455
17456 *slot = die;
c906108c
SS
17457}
17458
b64f50a1
JK
17459/* DW_ADDR is always stored already as sect_offset; despite for the forms
17460 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17461
93311388
DE
17462static int
17463is_ref_attr (struct attribute *attr)
c906108c 17464{
c906108c
SS
17465 switch (attr->form)
17466 {
17467 case DW_FORM_ref_addr:
c906108c
SS
17468 case DW_FORM_ref1:
17469 case DW_FORM_ref2:
17470 case DW_FORM_ref4:
613e1657 17471 case DW_FORM_ref8:
c906108c 17472 case DW_FORM_ref_udata:
36586728 17473 case DW_FORM_GNU_ref_alt:
93311388 17474 return 1;
c906108c 17475 default:
93311388 17476 return 0;
c906108c 17477 }
93311388
DE
17478}
17479
b64f50a1
JK
17480/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17481 required kind. */
17482
17483static sect_offset
93311388
DE
17484dwarf2_get_ref_die_offset (struct attribute *attr)
17485{
4568ecf9 17486 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17487
93311388 17488 if (is_ref_attr (attr))
b64f50a1 17489 return retval;
93311388 17490
b64f50a1 17491 retval.sect_off = 0;
93311388
DE
17492 complaint (&symfile_complaints,
17493 _("unsupported die ref attribute form: '%s'"),
17494 dwarf_form_name (attr->form));
b64f50a1 17495 return retval;
c906108c
SS
17496}
17497
43bbcdc2
PH
17498/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17499 * the value held by the attribute is not constant. */
a02abb62 17500
43bbcdc2 17501static LONGEST
a02abb62
JB
17502dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17503{
17504 if (attr->form == DW_FORM_sdata)
17505 return DW_SND (attr);
17506 else if (attr->form == DW_FORM_udata
17507 || attr->form == DW_FORM_data1
17508 || attr->form == DW_FORM_data2
17509 || attr->form == DW_FORM_data4
17510 || attr->form == DW_FORM_data8)
17511 return DW_UNSND (attr);
17512 else
17513 {
3e43a32a
MS
17514 complaint (&symfile_complaints,
17515 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17516 dwarf_form_name (attr->form));
17517 return default_value;
17518 }
17519}
17520
348e048f
DE
17521/* Follow reference or signature attribute ATTR of SRC_DIE.
17522 On entry *REF_CU is the CU of SRC_DIE.
17523 On exit *REF_CU is the CU of the result. */
17524
17525static struct die_info *
17526follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17527 struct dwarf2_cu **ref_cu)
17528{
17529 struct die_info *die;
17530
17531 if (is_ref_attr (attr))
17532 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17533 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17534 die = follow_die_sig (src_die, attr, ref_cu);
17535 else
17536 {
17537 dump_die_for_error (src_die);
17538 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17539 (*ref_cu)->objfile->name);
17540 }
17541
17542 return die;
03dd20cc
DJ
17543}
17544
5c631832 17545/* Follow reference OFFSET.
673bfd45
DE
17546 On entry *REF_CU is the CU of the source die referencing OFFSET.
17547 On exit *REF_CU is the CU of the result.
17548 Returns NULL if OFFSET is invalid. */
f504f079 17549
f9aca02d 17550static struct die_info *
36586728
TT
17551follow_die_offset (sect_offset offset, int offset_in_dwz,
17552 struct dwarf2_cu **ref_cu)
c906108c 17553{
10b3939b 17554 struct die_info temp_die;
f2f0e013 17555 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17556
348e048f
DE
17557 gdb_assert (cu->per_cu != NULL);
17558
98bfdba5
PA
17559 target_cu = cu;
17560
3019eac3 17561 if (cu->per_cu->is_debug_types)
348e048f
DE
17562 {
17563 /* .debug_types CUs cannot reference anything outside their CU.
17564 If they need to, they have to reference a signatured type via
55f1336d 17565 DW_FORM_ref_sig8. */
348e048f 17566 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17567 return NULL;
348e048f 17568 }
36586728
TT
17569 else if (offset_in_dwz != cu->per_cu->is_dwz
17570 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17571 {
17572 struct dwarf2_per_cu_data *per_cu;
9a619af0 17573
36586728
TT
17574 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17575 cu->objfile);
03dd20cc
DJ
17576
17577 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17578 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17579 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17580
10b3939b
DJ
17581 target_cu = per_cu->cu;
17582 }
98bfdba5
PA
17583 else if (cu->dies == NULL)
17584 {
17585 /* We're loading full DIEs during partial symbol reading. */
17586 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17587 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17588 }
c906108c 17589
f2f0e013 17590 *ref_cu = target_cu;
51545339 17591 temp_die.offset = offset;
b64f50a1 17592 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17593}
10b3939b 17594
5c631832
JK
17595/* Follow reference attribute ATTR of SRC_DIE.
17596 On entry *REF_CU is the CU of SRC_DIE.
17597 On exit *REF_CU is the CU of the result. */
17598
17599static struct die_info *
17600follow_die_ref (struct die_info *src_die, struct attribute *attr,
17601 struct dwarf2_cu **ref_cu)
17602{
b64f50a1 17603 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17604 struct dwarf2_cu *cu = *ref_cu;
17605 struct die_info *die;
17606
36586728
TT
17607 die = follow_die_offset (offset,
17608 (attr->form == DW_FORM_GNU_ref_alt
17609 || cu->per_cu->is_dwz),
17610 ref_cu);
5c631832
JK
17611 if (!die)
17612 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17613 "at 0x%x [in module %s]"),
b64f50a1 17614 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17615
5c631832
JK
17616 return die;
17617}
17618
d83e736b
JK
17619/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17620 Returned value is intended for DW_OP_call*. Returned
17621 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17622
17623struct dwarf2_locexpr_baton
8b9737bf
TT
17624dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17625 struct dwarf2_per_cu_data *per_cu,
17626 CORE_ADDR (*get_frame_pc) (void *baton),
17627 void *baton)
5c631832 17628{
918dd910 17629 struct dwarf2_cu *cu;
5c631832
JK
17630 struct die_info *die;
17631 struct attribute *attr;
17632 struct dwarf2_locexpr_baton retval;
17633
8cf6f0b1
TT
17634 dw2_setup (per_cu->objfile);
17635
918dd910
JK
17636 if (per_cu->cu == NULL)
17637 load_cu (per_cu);
17638 cu = per_cu->cu;
17639
36586728 17640 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17641 if (!die)
17642 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17643 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17644
17645 attr = dwarf2_attr (die, DW_AT_location, cu);
17646 if (!attr)
17647 {
e103e986
JK
17648 /* DWARF: "If there is no such attribute, then there is no effect.".
17649 DATA is ignored if SIZE is 0. */
5c631832 17650
e103e986 17651 retval.data = NULL;
5c631832
JK
17652 retval.size = 0;
17653 }
8cf6f0b1
TT
17654 else if (attr_form_is_section_offset (attr))
17655 {
17656 struct dwarf2_loclist_baton loclist_baton;
17657 CORE_ADDR pc = (*get_frame_pc) (baton);
17658 size_t size;
17659
17660 fill_in_loclist_baton (cu, &loclist_baton, attr);
17661
17662 retval.data = dwarf2_find_location_expression (&loclist_baton,
17663 &size, pc);
17664 retval.size = size;
17665 }
5c631832
JK
17666 else
17667 {
17668 if (!attr_form_is_block (attr))
17669 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17670 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17671 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17672
17673 retval.data = DW_BLOCK (attr)->data;
17674 retval.size = DW_BLOCK (attr)->size;
17675 }
17676 retval.per_cu = cu->per_cu;
918dd910 17677
918dd910
JK
17678 age_cached_comp_units ();
17679
5c631832 17680 return retval;
348e048f
DE
17681}
17682
8b9737bf
TT
17683/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17684 offset. */
17685
17686struct dwarf2_locexpr_baton
17687dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17688 struct dwarf2_per_cu_data *per_cu,
17689 CORE_ADDR (*get_frame_pc) (void *baton),
17690 void *baton)
17691{
17692 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17693
17694 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17695}
17696
8a9b8146
TT
17697/* Return the type of the DIE at DIE_OFFSET in the CU named by
17698 PER_CU. */
17699
17700struct type *
b64f50a1 17701dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17702 struct dwarf2_per_cu_data *per_cu)
17703{
b64f50a1
JK
17704 sect_offset die_offset_sect;
17705
8a9b8146 17706 dw2_setup (per_cu->objfile);
b64f50a1
JK
17707
17708 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17709 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17710}
17711
348e048f
DE
17712/* Follow the signature attribute ATTR in SRC_DIE.
17713 On entry *REF_CU is the CU of SRC_DIE.
17714 On exit *REF_CU is the CU of the result. */
17715
17716static struct die_info *
17717follow_die_sig (struct die_info *src_die, struct attribute *attr,
17718 struct dwarf2_cu **ref_cu)
17719{
17720 struct objfile *objfile = (*ref_cu)->objfile;
17721 struct die_info temp_die;
17722 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17723 struct dwarf2_cu *sig_cu;
17724 struct die_info *die;
17725
17726 /* sig_type will be NULL if the signatured type is missing from
17727 the debug info. */
17728 if (sig_type == NULL)
17729 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17730 "at 0x%x [in module %s]"),
b64f50a1 17731 src_die->offset.sect_off, objfile->name);
348e048f
DE
17732
17733 /* If necessary, add it to the queue and load its DIEs. */
17734
95554aad 17735 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17736 read_signatured_type (sig_type);
348e048f
DE
17737
17738 gdb_assert (sig_type->per_cu.cu != NULL);
17739
17740 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17741 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17742 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17743 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17744 temp_die.offset.sect_off);
348e048f
DE
17745 if (die)
17746 {
796a7ff8
DE
17747 /* For .gdb_index version 7 keep track of included TUs.
17748 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17749 if (dwarf2_per_objfile->index_table != NULL
17750 && dwarf2_per_objfile->index_table->version <= 7)
17751 {
17752 VEC_safe_push (dwarf2_per_cu_ptr,
17753 (*ref_cu)->per_cu->imported_symtabs,
17754 sig_cu->per_cu);
17755 }
17756
348e048f
DE
17757 *ref_cu = sig_cu;
17758 return die;
17759 }
17760
3e43a32a
MS
17761 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17762 "from DIE at 0x%x [in module %s]"),
b64f50a1 17763 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17764}
17765
17766/* Given an offset of a signatured type, return its signatured_type. */
17767
17768static struct signatured_type *
8b70b953
TT
17769lookup_signatured_type_at_offset (struct objfile *objfile,
17770 struct dwarf2_section_info *section,
b64f50a1 17771 sect_offset offset)
348e048f 17772{
b64f50a1 17773 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17774 unsigned int length, initial_length_size;
17775 unsigned int sig_offset;
52dc124a 17776 struct signatured_type find_entry, *sig_type;
348e048f
DE
17777
17778 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17779 sig_offset = (initial_length_size
17780 + 2 /*version*/
17781 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17782 + 1 /*address_size*/);
17783 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17784 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17785
17786 /* This is only used to lookup previously recorded types.
17787 If we didn't find it, it's our bug. */
52dc124a
DE
17788 gdb_assert (sig_type != NULL);
17789 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17790
52dc124a 17791 return sig_type;
348e048f
DE
17792}
17793
e5fe5e75 17794/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17795
17796static void
e5fe5e75 17797load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17798{
52dc124a 17799 struct signatured_type *sig_type;
348e048f 17800
f4dc4d17
DE
17801 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17802 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17803
6721b2ec
DE
17804 /* We have the per_cu, but we need the signatured_type.
17805 Fortunately this is an easy translation. */
17806 gdb_assert (per_cu->is_debug_types);
17807 sig_type = (struct signatured_type *) per_cu;
348e048f 17808
6721b2ec 17809 gdb_assert (per_cu->cu == NULL);
348e048f 17810
52dc124a 17811 read_signatured_type (sig_type);
348e048f 17812
6721b2ec 17813 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17814}
17815
dee91e82
DE
17816/* die_reader_func for read_signatured_type.
17817 This is identical to load_full_comp_unit_reader,
17818 but is kept separate for now. */
348e048f
DE
17819
17820static void
dee91e82
DE
17821read_signatured_type_reader (const struct die_reader_specs *reader,
17822 gdb_byte *info_ptr,
17823 struct die_info *comp_unit_die,
17824 int has_children,
17825 void *data)
348e048f 17826{
dee91e82 17827 struct dwarf2_cu *cu = reader->cu;
348e048f 17828
dee91e82
DE
17829 gdb_assert (cu->die_hash == NULL);
17830 cu->die_hash =
17831 htab_create_alloc_ex (cu->header.length / 12,
17832 die_hash,
17833 die_eq,
17834 NULL,
17835 &cu->comp_unit_obstack,
17836 hashtab_obstack_allocate,
17837 dummy_obstack_deallocate);
348e048f 17838
dee91e82
DE
17839 if (has_children)
17840 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17841 &info_ptr, comp_unit_die);
17842 cu->dies = comp_unit_die;
17843 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17844
17845 /* We try not to read any attributes in this function, because not
9cdd5dbd 17846 all CUs needed for references have been loaded yet, and symbol
348e048f 17847 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17848 or we won't be able to build types correctly.
17849 Similarly, if we do not read the producer, we can not apply
17850 producer-specific interpretation. */
95554aad 17851 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17852}
348e048f 17853
3019eac3
DE
17854/* Read in a signatured type and build its CU and DIEs.
17855 If the type is a stub for the real type in a DWO file,
17856 read in the real type from the DWO file as well. */
dee91e82
DE
17857
17858static void
17859read_signatured_type (struct signatured_type *sig_type)
17860{
17861 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17862
3019eac3 17863 gdb_assert (per_cu->is_debug_types);
dee91e82 17864 gdb_assert (per_cu->cu == NULL);
348e048f 17865
f4dc4d17
DE
17866 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17867 read_signatured_type_reader, NULL);
c906108c
SS
17868}
17869
c906108c
SS
17870/* Decode simple location descriptions.
17871 Given a pointer to a dwarf block that defines a location, compute
17872 the location and return the value.
17873
4cecd739
DJ
17874 NOTE drow/2003-11-18: This function is called in two situations
17875 now: for the address of static or global variables (partial symbols
17876 only) and for offsets into structures which are expected to be
17877 (more or less) constant. The partial symbol case should go away,
17878 and only the constant case should remain. That will let this
17879 function complain more accurately. A few special modes are allowed
17880 without complaint for global variables (for instance, global
17881 register values and thread-local values).
c906108c
SS
17882
17883 A location description containing no operations indicates that the
4cecd739 17884 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17885 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17886 callers will only want a very basic result and this can become a
21ae7a4d
JK
17887 complaint.
17888
17889 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17890
17891static CORE_ADDR
e7c27a73 17892decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17893{
e7c27a73 17894 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17895 size_t i;
17896 size_t size = blk->size;
21ae7a4d
JK
17897 gdb_byte *data = blk->data;
17898 CORE_ADDR stack[64];
17899 int stacki;
17900 unsigned int bytes_read, unsnd;
17901 gdb_byte op;
c906108c 17902
21ae7a4d
JK
17903 i = 0;
17904 stacki = 0;
17905 stack[stacki] = 0;
17906 stack[++stacki] = 0;
17907
17908 while (i < size)
17909 {
17910 op = data[i++];
17911 switch (op)
17912 {
17913 case DW_OP_lit0:
17914 case DW_OP_lit1:
17915 case DW_OP_lit2:
17916 case DW_OP_lit3:
17917 case DW_OP_lit4:
17918 case DW_OP_lit5:
17919 case DW_OP_lit6:
17920 case DW_OP_lit7:
17921 case DW_OP_lit8:
17922 case DW_OP_lit9:
17923 case DW_OP_lit10:
17924 case DW_OP_lit11:
17925 case DW_OP_lit12:
17926 case DW_OP_lit13:
17927 case DW_OP_lit14:
17928 case DW_OP_lit15:
17929 case DW_OP_lit16:
17930 case DW_OP_lit17:
17931 case DW_OP_lit18:
17932 case DW_OP_lit19:
17933 case DW_OP_lit20:
17934 case DW_OP_lit21:
17935 case DW_OP_lit22:
17936 case DW_OP_lit23:
17937 case DW_OP_lit24:
17938 case DW_OP_lit25:
17939 case DW_OP_lit26:
17940 case DW_OP_lit27:
17941 case DW_OP_lit28:
17942 case DW_OP_lit29:
17943 case DW_OP_lit30:
17944 case DW_OP_lit31:
17945 stack[++stacki] = op - DW_OP_lit0;
17946 break;
f1bea926 17947
21ae7a4d
JK
17948 case DW_OP_reg0:
17949 case DW_OP_reg1:
17950 case DW_OP_reg2:
17951 case DW_OP_reg3:
17952 case DW_OP_reg4:
17953 case DW_OP_reg5:
17954 case DW_OP_reg6:
17955 case DW_OP_reg7:
17956 case DW_OP_reg8:
17957 case DW_OP_reg9:
17958 case DW_OP_reg10:
17959 case DW_OP_reg11:
17960 case DW_OP_reg12:
17961 case DW_OP_reg13:
17962 case DW_OP_reg14:
17963 case DW_OP_reg15:
17964 case DW_OP_reg16:
17965 case DW_OP_reg17:
17966 case DW_OP_reg18:
17967 case DW_OP_reg19:
17968 case DW_OP_reg20:
17969 case DW_OP_reg21:
17970 case DW_OP_reg22:
17971 case DW_OP_reg23:
17972 case DW_OP_reg24:
17973 case DW_OP_reg25:
17974 case DW_OP_reg26:
17975 case DW_OP_reg27:
17976 case DW_OP_reg28:
17977 case DW_OP_reg29:
17978 case DW_OP_reg30:
17979 case DW_OP_reg31:
17980 stack[++stacki] = op - DW_OP_reg0;
17981 if (i < size)
17982 dwarf2_complex_location_expr_complaint ();
17983 break;
c906108c 17984
21ae7a4d
JK
17985 case DW_OP_regx:
17986 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17987 i += bytes_read;
17988 stack[++stacki] = unsnd;
17989 if (i < size)
17990 dwarf2_complex_location_expr_complaint ();
17991 break;
c906108c 17992
21ae7a4d
JK
17993 case DW_OP_addr:
17994 stack[++stacki] = read_address (objfile->obfd, &data[i],
17995 cu, &bytes_read);
17996 i += bytes_read;
17997 break;
d53d4ac5 17998
21ae7a4d
JK
17999 case DW_OP_const1u:
18000 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18001 i += 1;
18002 break;
18003
18004 case DW_OP_const1s:
18005 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18006 i += 1;
18007 break;
18008
18009 case DW_OP_const2u:
18010 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18011 i += 2;
18012 break;
18013
18014 case DW_OP_const2s:
18015 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18016 i += 2;
18017 break;
d53d4ac5 18018
21ae7a4d
JK
18019 case DW_OP_const4u:
18020 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18021 i += 4;
18022 break;
18023
18024 case DW_OP_const4s:
18025 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18026 i += 4;
18027 break;
18028
585861ea
JK
18029 case DW_OP_const8u:
18030 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18031 i += 8;
18032 break;
18033
21ae7a4d
JK
18034 case DW_OP_constu:
18035 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18036 &bytes_read);
18037 i += bytes_read;
18038 break;
18039
18040 case DW_OP_consts:
18041 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18042 i += bytes_read;
18043 break;
18044
18045 case DW_OP_dup:
18046 stack[stacki + 1] = stack[stacki];
18047 stacki++;
18048 break;
18049
18050 case DW_OP_plus:
18051 stack[stacki - 1] += stack[stacki];
18052 stacki--;
18053 break;
18054
18055 case DW_OP_plus_uconst:
18056 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18057 &bytes_read);
18058 i += bytes_read;
18059 break;
18060
18061 case DW_OP_minus:
18062 stack[stacki - 1] -= stack[stacki];
18063 stacki--;
18064 break;
18065
18066 case DW_OP_deref:
18067 /* If we're not the last op, then we definitely can't encode
18068 this using GDB's address_class enum. This is valid for partial
18069 global symbols, although the variable's address will be bogus
18070 in the psymtab. */
18071 if (i < size)
18072 dwarf2_complex_location_expr_complaint ();
18073 break;
18074
18075 case DW_OP_GNU_push_tls_address:
18076 /* The top of the stack has the offset from the beginning
18077 of the thread control block at which the variable is located. */
18078 /* Nothing should follow this operator, so the top of stack would
18079 be returned. */
18080 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18081 address will be bogus in the psymtab. Make it always at least
18082 non-zero to not look as a variable garbage collected by linker
18083 which have DW_OP_addr 0. */
21ae7a4d
JK
18084 if (i < size)
18085 dwarf2_complex_location_expr_complaint ();
585861ea 18086 stack[stacki]++;
21ae7a4d
JK
18087 break;
18088
18089 case DW_OP_GNU_uninit:
18090 break;
18091
3019eac3 18092 case DW_OP_GNU_addr_index:
49f6c839 18093 case DW_OP_GNU_const_index:
3019eac3
DE
18094 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18095 &bytes_read);
18096 i += bytes_read;
18097 break;
18098
21ae7a4d
JK
18099 default:
18100 {
f39c6ffd 18101 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18102
18103 if (name)
18104 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18105 name);
18106 else
18107 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18108 op);
18109 }
18110
18111 return (stack[stacki]);
d53d4ac5 18112 }
3c6e0cb3 18113
21ae7a4d
JK
18114 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18115 outside of the allocated space. Also enforce minimum>0. */
18116 if (stacki >= ARRAY_SIZE (stack) - 1)
18117 {
18118 complaint (&symfile_complaints,
18119 _("location description stack overflow"));
18120 return 0;
18121 }
18122
18123 if (stacki <= 0)
18124 {
18125 complaint (&symfile_complaints,
18126 _("location description stack underflow"));
18127 return 0;
18128 }
18129 }
18130 return (stack[stacki]);
c906108c
SS
18131}
18132
18133/* memory allocation interface */
18134
c906108c 18135static struct dwarf_block *
7b5a2f43 18136dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18137{
18138 struct dwarf_block *blk;
18139
18140 blk = (struct dwarf_block *)
7b5a2f43 18141 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18142 return (blk);
18143}
18144
c906108c 18145static struct die_info *
b60c80d6 18146dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18147{
18148 struct die_info *die;
b60c80d6
DJ
18149 size_t size = sizeof (struct die_info);
18150
18151 if (num_attrs > 1)
18152 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18153
b60c80d6 18154 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18155 memset (die, 0, sizeof (struct die_info));
18156 return (die);
18157}
2e276125
JB
18158
18159\f
18160/* Macro support. */
18161
233d95b5
JK
18162/* Return file name relative to the compilation directory of file number I in
18163 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18164 responsible for freeing it. */
233d95b5 18165
2e276125 18166static char *
233d95b5 18167file_file_name (int file, struct line_header *lh)
2e276125 18168{
6a83a1e6
EZ
18169 /* Is the file number a valid index into the line header's file name
18170 table? Remember that file numbers start with one, not zero. */
18171 if (1 <= file && file <= lh->num_file_names)
18172 {
18173 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18174
233d95b5 18175 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18176 return xstrdup (fe->name);
233d95b5
JK
18177 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18178 fe->name, NULL);
6a83a1e6 18179 }
2e276125
JB
18180 else
18181 {
6a83a1e6
EZ
18182 /* The compiler produced a bogus file number. We can at least
18183 record the macro definitions made in the file, even if we
18184 won't be able to find the file by name. */
18185 char fake_name[80];
9a619af0 18186
8c042590
PM
18187 xsnprintf (fake_name, sizeof (fake_name),
18188 "<bad macro file number %d>", file);
2e276125 18189
6e70227d 18190 complaint (&symfile_complaints,
6a83a1e6
EZ
18191 _("bad file number in macro information (%d)"),
18192 file);
2e276125 18193
6a83a1e6 18194 return xstrdup (fake_name);
2e276125
JB
18195 }
18196}
18197
233d95b5
JK
18198/* Return the full name of file number I in *LH's file name table.
18199 Use COMP_DIR as the name of the current directory of the
18200 compilation. The result is allocated using xmalloc; the caller is
18201 responsible for freeing it. */
18202static char *
18203file_full_name (int file, struct line_header *lh, const char *comp_dir)
18204{
18205 /* Is the file number a valid index into the line header's file name
18206 table? Remember that file numbers start with one, not zero. */
18207 if (1 <= file && file <= lh->num_file_names)
18208 {
18209 char *relative = file_file_name (file, lh);
18210
18211 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18212 return relative;
18213 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18214 }
18215 else
18216 return file_file_name (file, lh);
18217}
18218
2e276125
JB
18219
18220static struct macro_source_file *
18221macro_start_file (int file, int line,
18222 struct macro_source_file *current_file,
18223 const char *comp_dir,
18224 struct line_header *lh, struct objfile *objfile)
18225{
233d95b5
JK
18226 /* File name relative to the compilation directory of this source file. */
18227 char *file_name = file_file_name (file, lh);
2e276125
JB
18228
18229 /* We don't create a macro table for this compilation unit
18230 at all until we actually get a filename. */
18231 if (! pending_macros)
6532ff36 18232 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18233 objfile->per_bfd->macro_cache,
18234 comp_dir);
2e276125
JB
18235
18236 if (! current_file)
abc9d0dc
TT
18237 {
18238 /* If we have no current file, then this must be the start_file
18239 directive for the compilation unit's main source file. */
233d95b5 18240 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18241 macro_define_special (pending_macros);
18242 }
2e276125 18243 else
233d95b5 18244 current_file = macro_include (current_file, line, file_name);
2e276125 18245
233d95b5 18246 xfree (file_name);
6e70227d 18247
2e276125
JB
18248 return current_file;
18249}
18250
18251
18252/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18253 followed by a null byte. */
18254static char *
18255copy_string (const char *buf, int len)
18256{
18257 char *s = xmalloc (len + 1);
9a619af0 18258
2e276125
JB
18259 memcpy (s, buf, len);
18260 s[len] = '\0';
2e276125
JB
18261 return s;
18262}
18263
18264
18265static const char *
18266consume_improper_spaces (const char *p, const char *body)
18267{
18268 if (*p == ' ')
18269 {
4d3c2250 18270 complaint (&symfile_complaints,
3e43a32a
MS
18271 _("macro definition contains spaces "
18272 "in formal argument list:\n`%s'"),
4d3c2250 18273 body);
2e276125
JB
18274
18275 while (*p == ' ')
18276 p++;
18277 }
18278
18279 return p;
18280}
18281
18282
18283static void
18284parse_macro_definition (struct macro_source_file *file, int line,
18285 const char *body)
18286{
18287 const char *p;
18288
18289 /* The body string takes one of two forms. For object-like macro
18290 definitions, it should be:
18291
18292 <macro name> " " <definition>
18293
18294 For function-like macro definitions, it should be:
18295
18296 <macro name> "() " <definition>
18297 or
18298 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18299
18300 Spaces may appear only where explicitly indicated, and in the
18301 <definition>.
18302
18303 The Dwarf 2 spec says that an object-like macro's name is always
18304 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18305 the space when the macro's definition is the empty string.
2e276125
JB
18306
18307 The Dwarf 2 spec says that there should be no spaces between the
18308 formal arguments in a function-like macro's formal argument list,
18309 but versions of GCC around March 2002 include spaces after the
18310 commas. */
18311
18312
18313 /* Find the extent of the macro name. The macro name is terminated
18314 by either a space or null character (for an object-like macro) or
18315 an opening paren (for a function-like macro). */
18316 for (p = body; *p; p++)
18317 if (*p == ' ' || *p == '(')
18318 break;
18319
18320 if (*p == ' ' || *p == '\0')
18321 {
18322 /* It's an object-like macro. */
18323 int name_len = p - body;
18324 char *name = copy_string (body, name_len);
18325 const char *replacement;
18326
18327 if (*p == ' ')
18328 replacement = body + name_len + 1;
18329 else
18330 {
4d3c2250 18331 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18332 replacement = body + name_len;
18333 }
6e70227d 18334
2e276125
JB
18335 macro_define_object (file, line, name, replacement);
18336
18337 xfree (name);
18338 }
18339 else if (*p == '(')
18340 {
18341 /* It's a function-like macro. */
18342 char *name = copy_string (body, p - body);
18343 int argc = 0;
18344 int argv_size = 1;
18345 char **argv = xmalloc (argv_size * sizeof (*argv));
18346
18347 p++;
18348
18349 p = consume_improper_spaces (p, body);
18350
18351 /* Parse the formal argument list. */
18352 while (*p && *p != ')')
18353 {
18354 /* Find the extent of the current argument name. */
18355 const char *arg_start = p;
18356
18357 while (*p && *p != ',' && *p != ')' && *p != ' ')
18358 p++;
18359
18360 if (! *p || p == arg_start)
4d3c2250 18361 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18362 else
18363 {
18364 /* Make sure argv has room for the new argument. */
18365 if (argc >= argv_size)
18366 {
18367 argv_size *= 2;
18368 argv = xrealloc (argv, argv_size * sizeof (*argv));
18369 }
18370
18371 argv[argc++] = copy_string (arg_start, p - arg_start);
18372 }
18373
18374 p = consume_improper_spaces (p, body);
18375
18376 /* Consume the comma, if present. */
18377 if (*p == ',')
18378 {
18379 p++;
18380
18381 p = consume_improper_spaces (p, body);
18382 }
18383 }
18384
18385 if (*p == ')')
18386 {
18387 p++;
18388
18389 if (*p == ' ')
18390 /* Perfectly formed definition, no complaints. */
18391 macro_define_function (file, line, name,
6e70227d 18392 argc, (const char **) argv,
2e276125
JB
18393 p + 1);
18394 else if (*p == '\0')
18395 {
18396 /* Complain, but do define it. */
4d3c2250 18397 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18398 macro_define_function (file, line, name,
6e70227d 18399 argc, (const char **) argv,
2e276125
JB
18400 p);
18401 }
18402 else
18403 /* Just complain. */
4d3c2250 18404 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18405 }
18406 else
18407 /* Just complain. */
4d3c2250 18408 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18409
18410 xfree (name);
18411 {
18412 int i;
18413
18414 for (i = 0; i < argc; i++)
18415 xfree (argv[i]);
18416 }
18417 xfree (argv);
18418 }
18419 else
4d3c2250 18420 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18421}
18422
cf2c3c16
TT
18423/* Skip some bytes from BYTES according to the form given in FORM.
18424 Returns the new pointer. */
2e276125 18425
cf2c3c16 18426static gdb_byte *
f664829e 18427skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18428 enum dwarf_form form,
18429 unsigned int offset_size,
18430 struct dwarf2_section_info *section)
2e276125 18431{
cf2c3c16 18432 unsigned int bytes_read;
2e276125 18433
cf2c3c16 18434 switch (form)
2e276125 18435 {
cf2c3c16
TT
18436 case DW_FORM_data1:
18437 case DW_FORM_flag:
18438 ++bytes;
18439 break;
18440
18441 case DW_FORM_data2:
18442 bytes += 2;
18443 break;
18444
18445 case DW_FORM_data4:
18446 bytes += 4;
18447 break;
18448
18449 case DW_FORM_data8:
18450 bytes += 8;
18451 break;
18452
18453 case DW_FORM_string:
18454 read_direct_string (abfd, bytes, &bytes_read);
18455 bytes += bytes_read;
18456 break;
18457
18458 case DW_FORM_sec_offset:
18459 case DW_FORM_strp:
36586728 18460 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18461 bytes += offset_size;
18462 break;
18463
18464 case DW_FORM_block:
18465 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18466 bytes += bytes_read;
18467 break;
18468
18469 case DW_FORM_block1:
18470 bytes += 1 + read_1_byte (abfd, bytes);
18471 break;
18472 case DW_FORM_block2:
18473 bytes += 2 + read_2_bytes (abfd, bytes);
18474 break;
18475 case DW_FORM_block4:
18476 bytes += 4 + read_4_bytes (abfd, bytes);
18477 break;
18478
18479 case DW_FORM_sdata:
18480 case DW_FORM_udata:
3019eac3
DE
18481 case DW_FORM_GNU_addr_index:
18482 case DW_FORM_GNU_str_index:
f664829e
DE
18483 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18484 if (bytes == NULL)
18485 {
18486 dwarf2_section_buffer_overflow_complaint (section);
18487 return NULL;
18488 }
cf2c3c16
TT
18489 break;
18490
18491 default:
18492 {
18493 complain:
18494 complaint (&symfile_complaints,
18495 _("invalid form 0x%x in `%s'"),
18496 form,
18497 section->asection->name);
18498 return NULL;
18499 }
2e276125
JB
18500 }
18501
cf2c3c16
TT
18502 return bytes;
18503}
757a13d0 18504
cf2c3c16
TT
18505/* A helper for dwarf_decode_macros that handles skipping an unknown
18506 opcode. Returns an updated pointer to the macro data buffer; or,
18507 on error, issues a complaint and returns NULL. */
757a13d0 18508
cf2c3c16
TT
18509static gdb_byte *
18510skip_unknown_opcode (unsigned int opcode,
18511 gdb_byte **opcode_definitions,
f664829e 18512 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18513 bfd *abfd,
18514 unsigned int offset_size,
18515 struct dwarf2_section_info *section)
18516{
18517 unsigned int bytes_read, i;
18518 unsigned long arg;
18519 gdb_byte *defn;
2e276125 18520
cf2c3c16 18521 if (opcode_definitions[opcode] == NULL)
2e276125 18522 {
cf2c3c16
TT
18523 complaint (&symfile_complaints,
18524 _("unrecognized DW_MACFINO opcode 0x%x"),
18525 opcode);
18526 return NULL;
18527 }
2e276125 18528
cf2c3c16
TT
18529 defn = opcode_definitions[opcode];
18530 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18531 defn += bytes_read;
2e276125 18532
cf2c3c16
TT
18533 for (i = 0; i < arg; ++i)
18534 {
f664829e
DE
18535 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18536 section);
cf2c3c16
TT
18537 if (mac_ptr == NULL)
18538 {
18539 /* skip_form_bytes already issued the complaint. */
18540 return NULL;
18541 }
18542 }
757a13d0 18543
cf2c3c16
TT
18544 return mac_ptr;
18545}
757a13d0 18546
cf2c3c16
TT
18547/* A helper function which parses the header of a macro section.
18548 If the macro section is the extended (for now called "GNU") type,
18549 then this updates *OFFSET_SIZE. Returns a pointer to just after
18550 the header, or issues a complaint and returns NULL on error. */
757a13d0 18551
cf2c3c16
TT
18552static gdb_byte *
18553dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18554 bfd *abfd,
18555 gdb_byte *mac_ptr,
18556 unsigned int *offset_size,
18557 int section_is_gnu)
18558{
18559 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18560
cf2c3c16
TT
18561 if (section_is_gnu)
18562 {
18563 unsigned int version, flags;
757a13d0 18564
cf2c3c16
TT
18565 version = read_2_bytes (abfd, mac_ptr);
18566 if (version != 4)
18567 {
18568 complaint (&symfile_complaints,
18569 _("unrecognized version `%d' in .debug_macro section"),
18570 version);
18571 return NULL;
18572 }
18573 mac_ptr += 2;
757a13d0 18574
cf2c3c16
TT
18575 flags = read_1_byte (abfd, mac_ptr);
18576 ++mac_ptr;
18577 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18578
cf2c3c16
TT
18579 if ((flags & 2) != 0)
18580 /* We don't need the line table offset. */
18581 mac_ptr += *offset_size;
757a13d0 18582
cf2c3c16
TT
18583 /* Vendor opcode descriptions. */
18584 if ((flags & 4) != 0)
18585 {
18586 unsigned int i, count;
757a13d0 18587
cf2c3c16
TT
18588 count = read_1_byte (abfd, mac_ptr);
18589 ++mac_ptr;
18590 for (i = 0; i < count; ++i)
18591 {
18592 unsigned int opcode, bytes_read;
18593 unsigned long arg;
18594
18595 opcode = read_1_byte (abfd, mac_ptr);
18596 ++mac_ptr;
18597 opcode_definitions[opcode] = mac_ptr;
18598 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18599 mac_ptr += bytes_read;
18600 mac_ptr += arg;
18601 }
757a13d0 18602 }
cf2c3c16 18603 }
757a13d0 18604
cf2c3c16
TT
18605 return mac_ptr;
18606}
757a13d0 18607
cf2c3c16 18608/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18609 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18610
18611static void
18612dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18613 struct macro_source_file *current_file,
15d034d0 18614 struct line_header *lh, const char *comp_dir,
cf2c3c16 18615 struct dwarf2_section_info *section,
36586728 18616 int section_is_gnu, int section_is_dwz,
cf2c3c16 18617 unsigned int offset_size,
8fc3fc34
TT
18618 struct objfile *objfile,
18619 htab_t include_hash)
cf2c3c16
TT
18620{
18621 enum dwarf_macro_record_type macinfo_type;
18622 int at_commandline;
18623 gdb_byte *opcode_definitions[256];
757a13d0 18624
cf2c3c16
TT
18625 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18626 &offset_size, section_is_gnu);
18627 if (mac_ptr == NULL)
18628 {
18629 /* We already issued a complaint. */
18630 return;
18631 }
757a13d0
JK
18632
18633 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18634 GDB is still reading the definitions from command line. First
18635 DW_MACINFO_start_file will need to be ignored as it was already executed
18636 to create CURRENT_FILE for the main source holding also the command line
18637 definitions. On first met DW_MACINFO_start_file this flag is reset to
18638 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18639
18640 at_commandline = 1;
18641
18642 do
18643 {
18644 /* Do we at least have room for a macinfo type byte? */
18645 if (mac_ptr >= mac_end)
18646 {
f664829e 18647 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18648 break;
18649 }
18650
18651 macinfo_type = read_1_byte (abfd, mac_ptr);
18652 mac_ptr++;
18653
cf2c3c16
TT
18654 /* Note that we rely on the fact that the corresponding GNU and
18655 DWARF constants are the same. */
757a13d0
JK
18656 switch (macinfo_type)
18657 {
18658 /* A zero macinfo type indicates the end of the macro
18659 information. */
18660 case 0:
18661 break;
2e276125 18662
cf2c3c16
TT
18663 case DW_MACRO_GNU_define:
18664 case DW_MACRO_GNU_undef:
18665 case DW_MACRO_GNU_define_indirect:
18666 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18667 case DW_MACRO_GNU_define_indirect_alt:
18668 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18669 {
891d2f0b 18670 unsigned int bytes_read;
2e276125
JB
18671 int line;
18672 char *body;
cf2c3c16 18673 int is_define;
2e276125 18674
cf2c3c16
TT
18675 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18676 mac_ptr += bytes_read;
18677
18678 if (macinfo_type == DW_MACRO_GNU_define
18679 || macinfo_type == DW_MACRO_GNU_undef)
18680 {
18681 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18682 mac_ptr += bytes_read;
18683 }
18684 else
18685 {
18686 LONGEST str_offset;
18687
18688 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18689 mac_ptr += offset_size;
2e276125 18690
36586728 18691 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18692 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18693 || section_is_dwz)
36586728
TT
18694 {
18695 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18696
18697 body = read_indirect_string_from_dwz (dwz, str_offset);
18698 }
18699 else
18700 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18701 }
18702
18703 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18704 || macinfo_type == DW_MACRO_GNU_define_indirect
18705 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18706 if (! current_file)
757a13d0
JK
18707 {
18708 /* DWARF violation as no main source is present. */
18709 complaint (&symfile_complaints,
18710 _("debug info with no main source gives macro %s "
18711 "on line %d: %s"),
cf2c3c16
TT
18712 is_define ? _("definition") : _("undefinition"),
18713 line, body);
757a13d0
JK
18714 break;
18715 }
3e43a32a
MS
18716 if ((line == 0 && !at_commandline)
18717 || (line != 0 && at_commandline))
4d3c2250 18718 complaint (&symfile_complaints,
757a13d0
JK
18719 _("debug info gives %s macro %s with %s line %d: %s"),
18720 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18721 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18722 line == 0 ? _("zero") : _("non-zero"), line, body);
18723
cf2c3c16 18724 if (is_define)
757a13d0 18725 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18726 else
18727 {
18728 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18729 || macinfo_type == DW_MACRO_GNU_undef_indirect
18730 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18731 macro_undef (current_file, line, body);
18732 }
2e276125
JB
18733 }
18734 break;
18735
cf2c3c16 18736 case DW_MACRO_GNU_start_file:
2e276125 18737 {
891d2f0b 18738 unsigned int bytes_read;
2e276125
JB
18739 int line, file;
18740
18741 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18742 mac_ptr += bytes_read;
18743 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18744 mac_ptr += bytes_read;
18745
3e43a32a
MS
18746 if ((line == 0 && !at_commandline)
18747 || (line != 0 && at_commandline))
757a13d0
JK
18748 complaint (&symfile_complaints,
18749 _("debug info gives source %d included "
18750 "from %s at %s line %d"),
18751 file, at_commandline ? _("command-line") : _("file"),
18752 line == 0 ? _("zero") : _("non-zero"), line);
18753
18754 if (at_commandline)
18755 {
cf2c3c16
TT
18756 /* This DW_MACRO_GNU_start_file was executed in the
18757 pass one. */
757a13d0
JK
18758 at_commandline = 0;
18759 }
18760 else
18761 current_file = macro_start_file (file, line,
18762 current_file, comp_dir,
cf2c3c16 18763 lh, objfile);
2e276125
JB
18764 }
18765 break;
18766
cf2c3c16 18767 case DW_MACRO_GNU_end_file:
2e276125 18768 if (! current_file)
4d3c2250 18769 complaint (&symfile_complaints,
3e43a32a
MS
18770 _("macro debug info has an unmatched "
18771 "`close_file' directive"));
2e276125
JB
18772 else
18773 {
18774 current_file = current_file->included_by;
18775 if (! current_file)
18776 {
cf2c3c16 18777 enum dwarf_macro_record_type next_type;
2e276125
JB
18778
18779 /* GCC circa March 2002 doesn't produce the zero
18780 type byte marking the end of the compilation
18781 unit. Complain if it's not there, but exit no
18782 matter what. */
18783
18784 /* Do we at least have room for a macinfo type byte? */
18785 if (mac_ptr >= mac_end)
18786 {
f664829e 18787 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18788 return;
18789 }
18790
18791 /* We don't increment mac_ptr here, so this is just
18792 a look-ahead. */
18793 next_type = read_1_byte (abfd, mac_ptr);
18794 if (next_type != 0)
4d3c2250 18795 complaint (&symfile_complaints,
3e43a32a
MS
18796 _("no terminating 0-type entry for "
18797 "macros in `.debug_macinfo' section"));
2e276125
JB
18798
18799 return;
18800 }
18801 }
18802 break;
18803
cf2c3c16 18804 case DW_MACRO_GNU_transparent_include:
36586728 18805 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18806 {
18807 LONGEST offset;
8fc3fc34 18808 void **slot;
a036ba48
TT
18809 bfd *include_bfd = abfd;
18810 struct dwarf2_section_info *include_section = section;
18811 struct dwarf2_section_info alt_section;
18812 gdb_byte *include_mac_end = mac_end;
18813 int is_dwz = section_is_dwz;
18814 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18815
18816 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18817 mac_ptr += offset_size;
18818
a036ba48
TT
18819 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18820 {
18821 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18822
18823 dwarf2_read_section (dwarf2_per_objfile->objfile,
18824 &dwz->macro);
18825
18826 include_bfd = dwz->macro.asection->owner;
18827 include_section = &dwz->macro;
18828 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18829 is_dwz = 1;
18830 }
18831
18832 new_mac_ptr = include_section->buffer + offset;
18833 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18834
8fc3fc34
TT
18835 if (*slot != NULL)
18836 {
18837 /* This has actually happened; see
18838 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18839 complaint (&symfile_complaints,
18840 _("recursive DW_MACRO_GNU_transparent_include in "
18841 ".debug_macro section"));
18842 }
18843 else
18844 {
a036ba48 18845 *slot = new_mac_ptr;
36586728 18846
a036ba48 18847 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18848 include_mac_end, current_file,
8fc3fc34 18849 lh, comp_dir,
36586728 18850 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18851 offset_size, objfile, include_hash);
18852
a036ba48 18853 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18854 }
cf2c3c16
TT
18855 }
18856 break;
18857
2e276125 18858 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18859 if (!section_is_gnu)
18860 {
18861 unsigned int bytes_read;
18862 int constant;
2e276125 18863
cf2c3c16
TT
18864 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18865 mac_ptr += bytes_read;
18866 read_direct_string (abfd, mac_ptr, &bytes_read);
18867 mac_ptr += bytes_read;
2e276125 18868
cf2c3c16
TT
18869 /* We don't recognize any vendor extensions. */
18870 break;
18871 }
18872 /* FALLTHROUGH */
18873
18874 default:
18875 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18876 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18877 section);
18878 if (mac_ptr == NULL)
18879 return;
18880 break;
2e276125 18881 }
757a13d0 18882 } while (macinfo_type != 0);
2e276125 18883}
8e19ed76 18884
cf2c3c16 18885static void
09262596 18886dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18887 const char *comp_dir, int section_is_gnu)
cf2c3c16 18888{
bb5ed363 18889 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18890 struct line_header *lh = cu->line_header;
18891 bfd *abfd;
cf2c3c16
TT
18892 gdb_byte *mac_ptr, *mac_end;
18893 struct macro_source_file *current_file = 0;
18894 enum dwarf_macro_record_type macinfo_type;
18895 unsigned int offset_size = cu->header.offset_size;
18896 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18897 struct cleanup *cleanup;
18898 htab_t include_hash;
18899 void **slot;
09262596
DE
18900 struct dwarf2_section_info *section;
18901 const char *section_name;
18902
18903 if (cu->dwo_unit != NULL)
18904 {
18905 if (section_is_gnu)
18906 {
18907 section = &cu->dwo_unit->dwo_file->sections.macro;
18908 section_name = ".debug_macro.dwo";
18909 }
18910 else
18911 {
18912 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18913 section_name = ".debug_macinfo.dwo";
18914 }
18915 }
18916 else
18917 {
18918 if (section_is_gnu)
18919 {
18920 section = &dwarf2_per_objfile->macro;
18921 section_name = ".debug_macro";
18922 }
18923 else
18924 {
18925 section = &dwarf2_per_objfile->macinfo;
18926 section_name = ".debug_macinfo";
18927 }
18928 }
cf2c3c16 18929
bb5ed363 18930 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18931 if (section->buffer == NULL)
18932 {
fceca515 18933 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18934 return;
18935 }
09262596 18936 abfd = section->asection->owner;
cf2c3c16
TT
18937
18938 /* First pass: Find the name of the base filename.
18939 This filename is needed in order to process all macros whose definition
18940 (or undefinition) comes from the command line. These macros are defined
18941 before the first DW_MACINFO_start_file entry, and yet still need to be
18942 associated to the base file.
18943
18944 To determine the base file name, we scan the macro definitions until we
18945 reach the first DW_MACINFO_start_file entry. We then initialize
18946 CURRENT_FILE accordingly so that any macro definition found before the
18947 first DW_MACINFO_start_file can still be associated to the base file. */
18948
18949 mac_ptr = section->buffer + offset;
18950 mac_end = section->buffer + section->size;
18951
18952 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18953 &offset_size, section_is_gnu);
18954 if (mac_ptr == NULL)
18955 {
18956 /* We already issued a complaint. */
18957 return;
18958 }
18959
18960 do
18961 {
18962 /* Do we at least have room for a macinfo type byte? */
18963 if (mac_ptr >= mac_end)
18964 {
18965 /* Complaint is printed during the second pass as GDB will probably
18966 stop the first pass earlier upon finding
18967 DW_MACINFO_start_file. */
18968 break;
18969 }
18970
18971 macinfo_type = read_1_byte (abfd, mac_ptr);
18972 mac_ptr++;
18973
18974 /* Note that we rely on the fact that the corresponding GNU and
18975 DWARF constants are the same. */
18976 switch (macinfo_type)
18977 {
18978 /* A zero macinfo type indicates the end of the macro
18979 information. */
18980 case 0:
18981 break;
18982
18983 case DW_MACRO_GNU_define:
18984 case DW_MACRO_GNU_undef:
18985 /* Only skip the data by MAC_PTR. */
18986 {
18987 unsigned int bytes_read;
18988
18989 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18990 mac_ptr += bytes_read;
18991 read_direct_string (abfd, mac_ptr, &bytes_read);
18992 mac_ptr += bytes_read;
18993 }
18994 break;
18995
18996 case DW_MACRO_GNU_start_file:
18997 {
18998 unsigned int bytes_read;
18999 int line, file;
19000
19001 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19002 mac_ptr += bytes_read;
19003 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19004 mac_ptr += bytes_read;
19005
19006 current_file = macro_start_file (file, line, current_file,
bb5ed363 19007 comp_dir, lh, objfile);
cf2c3c16
TT
19008 }
19009 break;
19010
19011 case DW_MACRO_GNU_end_file:
19012 /* No data to skip by MAC_PTR. */
19013 break;
19014
19015 case DW_MACRO_GNU_define_indirect:
19016 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19017 case DW_MACRO_GNU_define_indirect_alt:
19018 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19019 {
19020 unsigned int bytes_read;
19021
19022 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19023 mac_ptr += bytes_read;
19024 mac_ptr += offset_size;
19025 }
19026 break;
19027
19028 case DW_MACRO_GNU_transparent_include:
f7a35f02 19029 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19030 /* Note that, according to the spec, a transparent include
19031 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19032 skip this opcode. */
19033 mac_ptr += offset_size;
19034 break;
19035
19036 case DW_MACINFO_vendor_ext:
19037 /* Only skip the data by MAC_PTR. */
19038 if (!section_is_gnu)
19039 {
19040 unsigned int bytes_read;
19041
19042 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19043 mac_ptr += bytes_read;
19044 read_direct_string (abfd, mac_ptr, &bytes_read);
19045 mac_ptr += bytes_read;
19046 }
19047 /* FALLTHROUGH */
19048
19049 default:
19050 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19051 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19052 section);
19053 if (mac_ptr == NULL)
19054 return;
19055 break;
19056 }
19057 } while (macinfo_type != 0 && current_file == NULL);
19058
19059 /* Second pass: Process all entries.
19060
19061 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19062 command-line macro definitions/undefinitions. This flag is unset when we
19063 reach the first DW_MACINFO_start_file entry. */
19064
8fc3fc34
TT
19065 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19066 NULL, xcalloc, xfree);
19067 cleanup = make_cleanup_htab_delete (include_hash);
19068 mac_ptr = section->buffer + offset;
19069 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19070 *slot = mac_ptr;
19071 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19072 current_file, lh, comp_dir, section,
19073 section_is_gnu, 0,
8fc3fc34
TT
19074 offset_size, objfile, include_hash);
19075 do_cleanups (cleanup);
cf2c3c16
TT
19076}
19077
8e19ed76 19078/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19079 if so return true else false. */
380bca97 19080
8e19ed76
PS
19081static int
19082attr_form_is_block (struct attribute *attr)
19083{
19084 return (attr == NULL ? 0 :
19085 attr->form == DW_FORM_block1
19086 || attr->form == DW_FORM_block2
19087 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19088 || attr->form == DW_FORM_block
19089 || attr->form == DW_FORM_exprloc);
8e19ed76 19090}
4c2df51b 19091
c6a0999f
JB
19092/* Return non-zero if ATTR's value is a section offset --- classes
19093 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19094 You may use DW_UNSND (attr) to retrieve such offsets.
19095
19096 Section 7.5.4, "Attribute Encodings", explains that no attribute
19097 may have a value that belongs to more than one of these classes; it
19098 would be ambiguous if we did, because we use the same forms for all
19099 of them. */
380bca97 19100
3690dd37
JB
19101static int
19102attr_form_is_section_offset (struct attribute *attr)
19103{
19104 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19105 || attr->form == DW_FORM_data8
19106 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19107}
19108
3690dd37
JB
19109/* Return non-zero if ATTR's value falls in the 'constant' class, or
19110 zero otherwise. When this function returns true, you can apply
19111 dwarf2_get_attr_constant_value to it.
19112
19113 However, note that for some attributes you must check
19114 attr_form_is_section_offset before using this test. DW_FORM_data4
19115 and DW_FORM_data8 are members of both the constant class, and of
19116 the classes that contain offsets into other debug sections
19117 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19118 that, if an attribute's can be either a constant or one of the
19119 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19120 taken as section offsets, not constants. */
380bca97 19121
3690dd37
JB
19122static int
19123attr_form_is_constant (struct attribute *attr)
19124{
19125 switch (attr->form)
19126 {
19127 case DW_FORM_sdata:
19128 case DW_FORM_udata:
19129 case DW_FORM_data1:
19130 case DW_FORM_data2:
19131 case DW_FORM_data4:
19132 case DW_FORM_data8:
19133 return 1;
19134 default:
19135 return 0;
19136 }
19137}
19138
3019eac3
DE
19139/* Return the .debug_loc section to use for CU.
19140 For DWO files use .debug_loc.dwo. */
19141
19142static struct dwarf2_section_info *
19143cu_debug_loc_section (struct dwarf2_cu *cu)
19144{
19145 if (cu->dwo_unit)
19146 return &cu->dwo_unit->dwo_file->sections.loc;
19147 return &dwarf2_per_objfile->loc;
19148}
19149
8cf6f0b1
TT
19150/* A helper function that fills in a dwarf2_loclist_baton. */
19151
19152static void
19153fill_in_loclist_baton (struct dwarf2_cu *cu,
19154 struct dwarf2_loclist_baton *baton,
19155 struct attribute *attr)
19156{
3019eac3
DE
19157 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19158
19159 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19160
19161 baton->per_cu = cu->per_cu;
19162 gdb_assert (baton->per_cu);
19163 /* We don't know how long the location list is, but make sure we
19164 don't run off the edge of the section. */
3019eac3
DE
19165 baton->size = section->size - DW_UNSND (attr);
19166 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19167 baton->base_address = cu->base_address;
f664829e 19168 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19169}
19170
4c2df51b
DJ
19171static void
19172dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19173 struct dwarf2_cu *cu, int is_block)
4c2df51b 19174{
bb5ed363 19175 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19176 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19177
3690dd37 19178 if (attr_form_is_section_offset (attr)
3019eac3 19179 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19180 the section. If so, fall through to the complaint in the
19181 other branch. */
3019eac3 19182 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19183 {
0d53c4c4 19184 struct dwarf2_loclist_baton *baton;
4c2df51b 19185
bb5ed363 19186 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19187 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19188
8cf6f0b1 19189 fill_in_loclist_baton (cu, baton, attr);
be391dca 19190
d00adf39 19191 if (cu->base_known == 0)
0d53c4c4 19192 complaint (&symfile_complaints,
3e43a32a
MS
19193 _("Location list used without "
19194 "specifying the CU base address."));
4c2df51b 19195
f1e6e072
TT
19196 SYMBOL_ACLASS_INDEX (sym) = (is_block
19197 ? dwarf2_loclist_block_index
19198 : dwarf2_loclist_index);
0d53c4c4
DJ
19199 SYMBOL_LOCATION_BATON (sym) = baton;
19200 }
19201 else
19202 {
19203 struct dwarf2_locexpr_baton *baton;
19204
bb5ed363 19205 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19206 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19207 baton->per_cu = cu->per_cu;
19208 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19209
19210 if (attr_form_is_block (attr))
19211 {
19212 /* Note that we're just copying the block's data pointer
19213 here, not the actual data. We're still pointing into the
6502dd73
DJ
19214 info_buffer for SYM's objfile; right now we never release
19215 that buffer, but when we do clean up properly this may
19216 need to change. */
0d53c4c4
DJ
19217 baton->size = DW_BLOCK (attr)->size;
19218 baton->data = DW_BLOCK (attr)->data;
19219 }
19220 else
19221 {
19222 dwarf2_invalid_attrib_class_complaint ("location description",
19223 SYMBOL_NATURAL_NAME (sym));
19224 baton->size = 0;
0d53c4c4 19225 }
6e70227d 19226
f1e6e072
TT
19227 SYMBOL_ACLASS_INDEX (sym) = (is_block
19228 ? dwarf2_locexpr_block_index
19229 : dwarf2_locexpr_index);
0d53c4c4
DJ
19230 SYMBOL_LOCATION_BATON (sym) = baton;
19231 }
4c2df51b 19232}
6502dd73 19233
9aa1f1e3
TT
19234/* Return the OBJFILE associated with the compilation unit CU. If CU
19235 came from a separate debuginfo file, then the master objfile is
19236 returned. */
ae0d2f24
UW
19237
19238struct objfile *
19239dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19240{
9291a0cd 19241 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19242
19243 /* Return the master objfile, so that we can report and look up the
19244 correct file containing this variable. */
19245 if (objfile->separate_debug_objfile_backlink)
19246 objfile = objfile->separate_debug_objfile_backlink;
19247
19248 return objfile;
19249}
19250
96408a79
SA
19251/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19252 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19253 CU_HEADERP first. */
19254
19255static const struct comp_unit_head *
19256per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19257 struct dwarf2_per_cu_data *per_cu)
19258{
96408a79
SA
19259 gdb_byte *info_ptr;
19260
19261 if (per_cu->cu)
19262 return &per_cu->cu->header;
19263
8a0459fd 19264 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19265
19266 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19267 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19268
19269 return cu_headerp;
19270}
19271
ae0d2f24
UW
19272/* Return the address size given in the compilation unit header for CU. */
19273
98714339 19274int
ae0d2f24
UW
19275dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19276{
96408a79
SA
19277 struct comp_unit_head cu_header_local;
19278 const struct comp_unit_head *cu_headerp;
c471e790 19279
96408a79
SA
19280 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19281
19282 return cu_headerp->addr_size;
ae0d2f24
UW
19283}
19284
9eae7c52
TT
19285/* Return the offset size given in the compilation unit header for CU. */
19286
19287int
19288dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19289{
96408a79
SA
19290 struct comp_unit_head cu_header_local;
19291 const struct comp_unit_head *cu_headerp;
9c6c53f7 19292
96408a79
SA
19293 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19294
19295 return cu_headerp->offset_size;
19296}
19297
19298/* See its dwarf2loc.h declaration. */
19299
19300int
19301dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19302{
19303 struct comp_unit_head cu_header_local;
19304 const struct comp_unit_head *cu_headerp;
19305
19306 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19307
19308 if (cu_headerp->version == 2)
19309 return cu_headerp->addr_size;
19310 else
19311 return cu_headerp->offset_size;
181cebd4
JK
19312}
19313
9aa1f1e3
TT
19314/* Return the text offset of the CU. The returned offset comes from
19315 this CU's objfile. If this objfile came from a separate debuginfo
19316 file, then the offset may be different from the corresponding
19317 offset in the parent objfile. */
19318
19319CORE_ADDR
19320dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19321{
bb3fa9d0 19322 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19323
19324 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19325}
19326
348e048f
DE
19327/* Locate the .debug_info compilation unit from CU's objfile which contains
19328 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19329
19330static struct dwarf2_per_cu_data *
b64f50a1 19331dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19332 unsigned int offset_in_dwz,
ae038cb0
DJ
19333 struct objfile *objfile)
19334{
19335 struct dwarf2_per_cu_data *this_cu;
19336 int low, high;
36586728 19337 const sect_offset *cu_off;
ae038cb0 19338
ae038cb0
DJ
19339 low = 0;
19340 high = dwarf2_per_objfile->n_comp_units - 1;
19341 while (high > low)
19342 {
36586728 19343 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19344 int mid = low + (high - low) / 2;
9a619af0 19345
36586728
TT
19346 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19347 cu_off = &mid_cu->offset;
19348 if (mid_cu->is_dwz > offset_in_dwz
19349 || (mid_cu->is_dwz == offset_in_dwz
19350 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19351 high = mid;
19352 else
19353 low = mid + 1;
19354 }
19355 gdb_assert (low == high);
36586728
TT
19356 this_cu = dwarf2_per_objfile->all_comp_units[low];
19357 cu_off = &this_cu->offset;
19358 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19359 {
36586728 19360 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19361 error (_("Dwarf Error: could not find partial DIE containing "
19362 "offset 0x%lx [in module %s]"),
b64f50a1 19363 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19364
b64f50a1
JK
19365 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19366 <= offset.sect_off);
ae038cb0
DJ
19367 return dwarf2_per_objfile->all_comp_units[low-1];
19368 }
19369 else
19370 {
19371 this_cu = dwarf2_per_objfile->all_comp_units[low];
19372 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19373 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19374 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19375 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19376 return this_cu;
19377 }
19378}
19379
23745b47 19380/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19381
9816fde3 19382static void
23745b47 19383init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19384{
9816fde3 19385 memset (cu, 0, sizeof (*cu));
23745b47
DE
19386 per_cu->cu = cu;
19387 cu->per_cu = per_cu;
19388 cu->objfile = per_cu->objfile;
93311388 19389 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19390}
19391
19392/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19393
19394static void
95554aad
TT
19395prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19396 enum language pretend_language)
9816fde3
JK
19397{
19398 struct attribute *attr;
19399
19400 /* Set the language we're debugging. */
19401 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19402 if (attr)
19403 set_cu_language (DW_UNSND (attr), cu);
19404 else
9cded63f 19405 {
95554aad 19406 cu->language = pretend_language;
9cded63f
TT
19407 cu->language_defn = language_def (cu->language);
19408 }
dee91e82
DE
19409
19410 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19411 if (attr)
19412 cu->producer = DW_STRING (attr);
93311388
DE
19413}
19414
ae038cb0
DJ
19415/* Release one cached compilation unit, CU. We unlink it from the tree
19416 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19417 the caller is responsible for that.
19418 NOTE: DATA is a void * because this function is also used as a
19419 cleanup routine. */
ae038cb0
DJ
19420
19421static void
68dc6402 19422free_heap_comp_unit (void *data)
ae038cb0
DJ
19423{
19424 struct dwarf2_cu *cu = data;
19425
23745b47
DE
19426 gdb_assert (cu->per_cu != NULL);
19427 cu->per_cu->cu = NULL;
ae038cb0
DJ
19428 cu->per_cu = NULL;
19429
19430 obstack_free (&cu->comp_unit_obstack, NULL);
19431
19432 xfree (cu);
19433}
19434
72bf9492 19435/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19436 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19437 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19438
19439static void
19440free_stack_comp_unit (void *data)
19441{
19442 struct dwarf2_cu *cu = data;
19443
23745b47
DE
19444 gdb_assert (cu->per_cu != NULL);
19445 cu->per_cu->cu = NULL;
19446 cu->per_cu = NULL;
19447
72bf9492
DJ
19448 obstack_free (&cu->comp_unit_obstack, NULL);
19449 cu->partial_dies = NULL;
ae038cb0
DJ
19450}
19451
19452/* Free all cached compilation units. */
19453
19454static void
19455free_cached_comp_units (void *data)
19456{
19457 struct dwarf2_per_cu_data *per_cu, **last_chain;
19458
19459 per_cu = dwarf2_per_objfile->read_in_chain;
19460 last_chain = &dwarf2_per_objfile->read_in_chain;
19461 while (per_cu != NULL)
19462 {
19463 struct dwarf2_per_cu_data *next_cu;
19464
19465 next_cu = per_cu->cu->read_in_chain;
19466
68dc6402 19467 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19468 *last_chain = next_cu;
19469
19470 per_cu = next_cu;
19471 }
19472}
19473
19474/* Increase the age counter on each cached compilation unit, and free
19475 any that are too old. */
19476
19477static void
19478age_cached_comp_units (void)
19479{
19480 struct dwarf2_per_cu_data *per_cu, **last_chain;
19481
19482 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19483 per_cu = dwarf2_per_objfile->read_in_chain;
19484 while (per_cu != NULL)
19485 {
19486 per_cu->cu->last_used ++;
19487 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19488 dwarf2_mark (per_cu->cu);
19489 per_cu = per_cu->cu->read_in_chain;
19490 }
19491
19492 per_cu = dwarf2_per_objfile->read_in_chain;
19493 last_chain = &dwarf2_per_objfile->read_in_chain;
19494 while (per_cu != NULL)
19495 {
19496 struct dwarf2_per_cu_data *next_cu;
19497
19498 next_cu = per_cu->cu->read_in_chain;
19499
19500 if (!per_cu->cu->mark)
19501 {
68dc6402 19502 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19503 *last_chain = next_cu;
19504 }
19505 else
19506 last_chain = &per_cu->cu->read_in_chain;
19507
19508 per_cu = next_cu;
19509 }
19510}
19511
19512/* Remove a single compilation unit from the cache. */
19513
19514static void
dee91e82 19515free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19516{
19517 struct dwarf2_per_cu_data *per_cu, **last_chain;
19518
19519 per_cu = dwarf2_per_objfile->read_in_chain;
19520 last_chain = &dwarf2_per_objfile->read_in_chain;
19521 while (per_cu != NULL)
19522 {
19523 struct dwarf2_per_cu_data *next_cu;
19524
19525 next_cu = per_cu->cu->read_in_chain;
19526
dee91e82 19527 if (per_cu == target_per_cu)
ae038cb0 19528 {
68dc6402 19529 free_heap_comp_unit (per_cu->cu);
dee91e82 19530 per_cu->cu = NULL;
ae038cb0
DJ
19531 *last_chain = next_cu;
19532 break;
19533 }
19534 else
19535 last_chain = &per_cu->cu->read_in_chain;
19536
19537 per_cu = next_cu;
19538 }
19539}
19540
fe3e1990
DJ
19541/* Release all extra memory associated with OBJFILE. */
19542
19543void
19544dwarf2_free_objfile (struct objfile *objfile)
19545{
19546 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19547
19548 if (dwarf2_per_objfile == NULL)
19549 return;
19550
19551 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19552 free_cached_comp_units (NULL);
19553
7b9f3c50
DE
19554 if (dwarf2_per_objfile->quick_file_names_table)
19555 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19556
fe3e1990
DJ
19557 /* Everything else should be on the objfile obstack. */
19558}
19559
dee91e82
DE
19560/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19561 We store these in a hash table separate from the DIEs, and preserve them
19562 when the DIEs are flushed out of cache.
19563
19564 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 19565 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
19566 or the type may come from a DWO file. Furthermore, while it's more logical
19567 to use per_cu->section+offset, with Fission the section with the data is in
19568 the DWO file but we don't know that section at the point we need it.
19569 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19570 because we can enter the lookup routine, get_die_type_at_offset, from
19571 outside this file, and thus won't necessarily have PER_CU->cu.
19572 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 19573
dee91e82 19574struct dwarf2_per_cu_offset_and_type
1c379e20 19575{
dee91e82 19576 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19577 sect_offset offset;
1c379e20
DJ
19578 struct type *type;
19579};
19580
dee91e82 19581/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19582
19583static hashval_t
dee91e82 19584per_cu_offset_and_type_hash (const void *item)
1c379e20 19585{
dee91e82 19586 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19587
dee91e82 19588 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19589}
19590
dee91e82 19591/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19592
19593static int
dee91e82 19594per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19595{
dee91e82
DE
19596 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19597 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19598
dee91e82
DE
19599 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19600 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19601}
19602
19603/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19604 table if necessary. For convenience, return TYPE.
19605
19606 The DIEs reading must have careful ordering to:
19607 * Not cause infite loops trying to read in DIEs as a prerequisite for
19608 reading current DIE.
19609 * Not trying to dereference contents of still incompletely read in types
19610 while reading in other DIEs.
19611 * Enable referencing still incompletely read in types just by a pointer to
19612 the type without accessing its fields.
19613
19614 Therefore caller should follow these rules:
19615 * Try to fetch any prerequisite types we may need to build this DIE type
19616 before building the type and calling set_die_type.
e71ec853 19617 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19618 possible before fetching more types to complete the current type.
19619 * Make the type as complete as possible before fetching more types. */
1c379e20 19620
f792889a 19621static struct type *
1c379e20
DJ
19622set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19623{
dee91e82 19624 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19625 struct objfile *objfile = cu->objfile;
1c379e20 19626
b4ba55a1
JB
19627 /* For Ada types, make sure that the gnat-specific data is always
19628 initialized (if not already set). There are a few types where
19629 we should not be doing so, because the type-specific area is
19630 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19631 where the type-specific area is used to store the floatformat).
19632 But this is not a problem, because the gnat-specific information
19633 is actually not needed for these types. */
19634 if (need_gnat_info (cu)
19635 && TYPE_CODE (type) != TYPE_CODE_FUNC
19636 && TYPE_CODE (type) != TYPE_CODE_FLT
19637 && !HAVE_GNAT_AUX_INFO (type))
19638 INIT_GNAT_SPECIFIC (type);
19639
dee91e82 19640 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19641 {
dee91e82
DE
19642 dwarf2_per_objfile->die_type_hash =
19643 htab_create_alloc_ex (127,
19644 per_cu_offset_and_type_hash,
19645 per_cu_offset_and_type_eq,
19646 NULL,
19647 &objfile->objfile_obstack,
19648 hashtab_obstack_allocate,
19649 dummy_obstack_deallocate);
f792889a 19650 }
1c379e20 19651
dee91e82 19652 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19653 ofs.offset = die->offset;
19654 ofs.type = type;
dee91e82
DE
19655 slot = (struct dwarf2_per_cu_offset_and_type **)
19656 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19657 if (*slot)
19658 complaint (&symfile_complaints,
19659 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19660 die->offset.sect_off);
673bfd45 19661 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19662 **slot = ofs;
f792889a 19663 return type;
1c379e20
DJ
19664}
19665
380bca97 19666/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19667 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19668
19669static struct type *
b64f50a1 19670get_die_type_at_offset (sect_offset offset,
673bfd45 19671 struct dwarf2_per_cu_data *per_cu)
1c379e20 19672{
dee91e82 19673 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19674
dee91e82 19675 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19676 return NULL;
1c379e20 19677
dee91e82 19678 ofs.per_cu = per_cu;
673bfd45 19679 ofs.offset = offset;
dee91e82 19680 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19681 if (slot)
19682 return slot->type;
19683 else
19684 return NULL;
19685}
19686
673bfd45
DE
19687/* Look up the type for DIE in the appropriate type_hash table,
19688 or return NULL if DIE does not have a saved type. */
19689
19690static struct type *
19691get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19692{
19693 return get_die_type_at_offset (die->offset, cu->per_cu);
19694}
19695
10b3939b
DJ
19696/* Add a dependence relationship from CU to REF_PER_CU. */
19697
19698static void
19699dwarf2_add_dependence (struct dwarf2_cu *cu,
19700 struct dwarf2_per_cu_data *ref_per_cu)
19701{
19702 void **slot;
19703
19704 if (cu->dependencies == NULL)
19705 cu->dependencies
19706 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19707 NULL, &cu->comp_unit_obstack,
19708 hashtab_obstack_allocate,
19709 dummy_obstack_deallocate);
19710
19711 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19712 if (*slot == NULL)
19713 *slot = ref_per_cu;
19714}
1c379e20 19715
f504f079
DE
19716/* Subroutine of dwarf2_mark to pass to htab_traverse.
19717 Set the mark field in every compilation unit in the
ae038cb0
DJ
19718 cache that we must keep because we are keeping CU. */
19719
10b3939b
DJ
19720static int
19721dwarf2_mark_helper (void **slot, void *data)
19722{
19723 struct dwarf2_per_cu_data *per_cu;
19724
19725 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19726
19727 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19728 reading of the chain. As such dependencies remain valid it is not much
19729 useful to track and undo them during QUIT cleanups. */
19730 if (per_cu->cu == NULL)
19731 return 1;
19732
10b3939b
DJ
19733 if (per_cu->cu->mark)
19734 return 1;
19735 per_cu->cu->mark = 1;
19736
19737 if (per_cu->cu->dependencies != NULL)
19738 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19739
19740 return 1;
19741}
19742
f504f079
DE
19743/* Set the mark field in CU and in every other compilation unit in the
19744 cache that we must keep because we are keeping CU. */
19745
ae038cb0
DJ
19746static void
19747dwarf2_mark (struct dwarf2_cu *cu)
19748{
19749 if (cu->mark)
19750 return;
19751 cu->mark = 1;
10b3939b
DJ
19752 if (cu->dependencies != NULL)
19753 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19754}
19755
19756static void
19757dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19758{
19759 while (per_cu)
19760 {
19761 per_cu->cu->mark = 0;
19762 per_cu = per_cu->cu->read_in_chain;
19763 }
72bf9492
DJ
19764}
19765
72bf9492
DJ
19766/* Trivial hash function for partial_die_info: the hash value of a DIE
19767 is its offset in .debug_info for this objfile. */
19768
19769static hashval_t
19770partial_die_hash (const void *item)
19771{
19772 const struct partial_die_info *part_die = item;
9a619af0 19773
b64f50a1 19774 return part_die->offset.sect_off;
72bf9492
DJ
19775}
19776
19777/* Trivial comparison function for partial_die_info structures: two DIEs
19778 are equal if they have the same offset. */
19779
19780static int
19781partial_die_eq (const void *item_lhs, const void *item_rhs)
19782{
19783 const struct partial_die_info *part_die_lhs = item_lhs;
19784 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19785
b64f50a1 19786 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19787}
19788
ae038cb0
DJ
19789static struct cmd_list_element *set_dwarf2_cmdlist;
19790static struct cmd_list_element *show_dwarf2_cmdlist;
19791
19792static void
19793set_dwarf2_cmd (char *args, int from_tty)
19794{
19795 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19796}
19797
19798static void
19799show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19800{
ae038cb0
DJ
19801 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19802}
19803
4bf44c1c 19804/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19805
19806static void
c1bd65d0 19807dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19808{
19809 struct dwarf2_per_objfile *data = d;
8b70b953 19810 int ix;
8b70b953 19811
95554aad
TT
19812 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19813 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19814 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19815
19816 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19817 VEC_free (dwarf2_per_cu_ptr,
19818 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19819
8b70b953 19820 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19821
19822 if (data->dwo_files)
19823 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19824
19825 if (data->dwz_file && data->dwz_file->dwz_bfd)
19826 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19827}
19828
19829\f
ae2de4f8 19830/* The "save gdb-index" command. */
9291a0cd
TT
19831
19832/* The contents of the hash table we create when building the string
19833 table. */
19834struct strtab_entry
19835{
19836 offset_type offset;
19837 const char *str;
19838};
19839
559a7a62
JK
19840/* Hash function for a strtab_entry.
19841
19842 Function is used only during write_hash_table so no index format backward
19843 compatibility is needed. */
b89be57b 19844
9291a0cd
TT
19845static hashval_t
19846hash_strtab_entry (const void *e)
19847{
19848 const struct strtab_entry *entry = e;
559a7a62 19849 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19850}
19851
19852/* Equality function for a strtab_entry. */
b89be57b 19853
9291a0cd
TT
19854static int
19855eq_strtab_entry (const void *a, const void *b)
19856{
19857 const struct strtab_entry *ea = a;
19858 const struct strtab_entry *eb = b;
19859 return !strcmp (ea->str, eb->str);
19860}
19861
19862/* Create a strtab_entry hash table. */
b89be57b 19863
9291a0cd
TT
19864static htab_t
19865create_strtab (void)
19866{
19867 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19868 xfree, xcalloc, xfree);
19869}
19870
19871/* Add a string to the constant pool. Return the string's offset in
19872 host order. */
b89be57b 19873
9291a0cd
TT
19874static offset_type
19875add_string (htab_t table, struct obstack *cpool, const char *str)
19876{
19877 void **slot;
19878 struct strtab_entry entry;
19879 struct strtab_entry *result;
19880
19881 entry.str = str;
19882 slot = htab_find_slot (table, &entry, INSERT);
19883 if (*slot)
19884 result = *slot;
19885 else
19886 {
19887 result = XNEW (struct strtab_entry);
19888 result->offset = obstack_object_size (cpool);
19889 result->str = str;
19890 obstack_grow_str0 (cpool, str);
19891 *slot = result;
19892 }
19893 return result->offset;
19894}
19895
19896/* An entry in the symbol table. */
19897struct symtab_index_entry
19898{
19899 /* The name of the symbol. */
19900 const char *name;
19901 /* The offset of the name in the constant pool. */
19902 offset_type index_offset;
19903 /* A sorted vector of the indices of all the CUs that hold an object
19904 of this name. */
19905 VEC (offset_type) *cu_indices;
19906};
19907
19908/* The symbol table. This is a power-of-2-sized hash table. */
19909struct mapped_symtab
19910{
19911 offset_type n_elements;
19912 offset_type size;
19913 struct symtab_index_entry **data;
19914};
19915
19916/* Hash function for a symtab_index_entry. */
b89be57b 19917
9291a0cd
TT
19918static hashval_t
19919hash_symtab_entry (const void *e)
19920{
19921 const struct symtab_index_entry *entry = e;
19922 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19923 sizeof (offset_type) * VEC_length (offset_type,
19924 entry->cu_indices),
19925 0);
19926}
19927
19928/* Equality function for a symtab_index_entry. */
b89be57b 19929
9291a0cd
TT
19930static int
19931eq_symtab_entry (const void *a, const void *b)
19932{
19933 const struct symtab_index_entry *ea = a;
19934 const struct symtab_index_entry *eb = b;
19935 int len = VEC_length (offset_type, ea->cu_indices);
19936 if (len != VEC_length (offset_type, eb->cu_indices))
19937 return 0;
19938 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19939 VEC_address (offset_type, eb->cu_indices),
19940 sizeof (offset_type) * len);
19941}
19942
19943/* Destroy a symtab_index_entry. */
b89be57b 19944
9291a0cd
TT
19945static void
19946delete_symtab_entry (void *p)
19947{
19948 struct symtab_index_entry *entry = p;
19949 VEC_free (offset_type, entry->cu_indices);
19950 xfree (entry);
19951}
19952
19953/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19954
9291a0cd 19955static htab_t
3876f04e 19956create_symbol_hash_table (void)
9291a0cd
TT
19957{
19958 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19959 delete_symtab_entry, xcalloc, xfree);
19960}
19961
19962/* Create a new mapped symtab object. */
b89be57b 19963
9291a0cd
TT
19964static struct mapped_symtab *
19965create_mapped_symtab (void)
19966{
19967 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19968 symtab->n_elements = 0;
19969 symtab->size = 1024;
19970 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19971 return symtab;
19972}
19973
19974/* Destroy a mapped_symtab. */
b89be57b 19975
9291a0cd
TT
19976static void
19977cleanup_mapped_symtab (void *p)
19978{
19979 struct mapped_symtab *symtab = p;
19980 /* The contents of the array are freed when the other hash table is
19981 destroyed. */
19982 xfree (symtab->data);
19983 xfree (symtab);
19984}
19985
19986/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19987 the slot.
19988
19989 Function is used only during write_hash_table so no index format backward
19990 compatibility is needed. */
b89be57b 19991
9291a0cd
TT
19992static struct symtab_index_entry **
19993find_slot (struct mapped_symtab *symtab, const char *name)
19994{
559a7a62 19995 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19996
19997 index = hash & (symtab->size - 1);
19998 step = ((hash * 17) & (symtab->size - 1)) | 1;
19999
20000 for (;;)
20001 {
20002 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20003 return &symtab->data[index];
20004 index = (index + step) & (symtab->size - 1);
20005 }
20006}
20007
20008/* Expand SYMTAB's hash table. */
b89be57b 20009
9291a0cd
TT
20010static void
20011hash_expand (struct mapped_symtab *symtab)
20012{
20013 offset_type old_size = symtab->size;
20014 offset_type i;
20015 struct symtab_index_entry **old_entries = symtab->data;
20016
20017 symtab->size *= 2;
20018 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20019
20020 for (i = 0; i < old_size; ++i)
20021 {
20022 if (old_entries[i])
20023 {
20024 struct symtab_index_entry **slot = find_slot (symtab,
20025 old_entries[i]->name);
20026 *slot = old_entries[i];
20027 }
20028 }
20029
20030 xfree (old_entries);
20031}
20032
156942c7
DE
20033/* Add an entry to SYMTAB. NAME is the name of the symbol.
20034 CU_INDEX is the index of the CU in which the symbol appears.
20035 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20036
9291a0cd
TT
20037static void
20038add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20039 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20040 offset_type cu_index)
20041{
20042 struct symtab_index_entry **slot;
156942c7 20043 offset_type cu_index_and_attrs;
9291a0cd
TT
20044
20045 ++symtab->n_elements;
20046 if (4 * symtab->n_elements / 3 >= symtab->size)
20047 hash_expand (symtab);
20048
20049 slot = find_slot (symtab, name);
20050 if (!*slot)
20051 {
20052 *slot = XNEW (struct symtab_index_entry);
20053 (*slot)->name = name;
156942c7 20054 /* index_offset is set later. */
9291a0cd
TT
20055 (*slot)->cu_indices = NULL;
20056 }
156942c7
DE
20057
20058 cu_index_and_attrs = 0;
20059 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20060 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20061 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20062
20063 /* We don't want to record an index value twice as we want to avoid the
20064 duplication.
20065 We process all global symbols and then all static symbols
20066 (which would allow us to avoid the duplication by only having to check
20067 the last entry pushed), but a symbol could have multiple kinds in one CU.
20068 To keep things simple we don't worry about the duplication here and
20069 sort and uniqufy the list after we've processed all symbols. */
20070 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20071}
20072
20073/* qsort helper routine for uniquify_cu_indices. */
20074
20075static int
20076offset_type_compare (const void *ap, const void *bp)
20077{
20078 offset_type a = *(offset_type *) ap;
20079 offset_type b = *(offset_type *) bp;
20080
20081 return (a > b) - (b > a);
20082}
20083
20084/* Sort and remove duplicates of all symbols' cu_indices lists. */
20085
20086static void
20087uniquify_cu_indices (struct mapped_symtab *symtab)
20088{
20089 int i;
20090
20091 for (i = 0; i < symtab->size; ++i)
20092 {
20093 struct symtab_index_entry *entry = symtab->data[i];
20094
20095 if (entry
20096 && entry->cu_indices != NULL)
20097 {
20098 unsigned int next_to_insert, next_to_check;
20099 offset_type last_value;
20100
20101 qsort (VEC_address (offset_type, entry->cu_indices),
20102 VEC_length (offset_type, entry->cu_indices),
20103 sizeof (offset_type), offset_type_compare);
20104
20105 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20106 next_to_insert = 1;
20107 for (next_to_check = 1;
20108 next_to_check < VEC_length (offset_type, entry->cu_indices);
20109 ++next_to_check)
20110 {
20111 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20112 != last_value)
20113 {
20114 last_value = VEC_index (offset_type, entry->cu_indices,
20115 next_to_check);
20116 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20117 last_value);
20118 ++next_to_insert;
20119 }
20120 }
20121 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20122 }
20123 }
9291a0cd
TT
20124}
20125
20126/* Add a vector of indices to the constant pool. */
b89be57b 20127
9291a0cd 20128static offset_type
3876f04e 20129add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20130 struct symtab_index_entry *entry)
20131{
20132 void **slot;
20133
3876f04e 20134 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20135 if (!*slot)
20136 {
20137 offset_type len = VEC_length (offset_type, entry->cu_indices);
20138 offset_type val = MAYBE_SWAP (len);
20139 offset_type iter;
20140 int i;
20141
20142 *slot = entry;
20143 entry->index_offset = obstack_object_size (cpool);
20144
20145 obstack_grow (cpool, &val, sizeof (val));
20146 for (i = 0;
20147 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20148 ++i)
20149 {
20150 val = MAYBE_SWAP (iter);
20151 obstack_grow (cpool, &val, sizeof (val));
20152 }
20153 }
20154 else
20155 {
20156 struct symtab_index_entry *old_entry = *slot;
20157 entry->index_offset = old_entry->index_offset;
20158 entry = old_entry;
20159 }
20160 return entry->index_offset;
20161}
20162
20163/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20164 constant pool entries going into the obstack CPOOL. */
b89be57b 20165
9291a0cd
TT
20166static void
20167write_hash_table (struct mapped_symtab *symtab,
20168 struct obstack *output, struct obstack *cpool)
20169{
20170 offset_type i;
3876f04e 20171 htab_t symbol_hash_table;
9291a0cd
TT
20172 htab_t str_table;
20173
3876f04e 20174 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20175 str_table = create_strtab ();
3876f04e 20176
9291a0cd
TT
20177 /* We add all the index vectors to the constant pool first, to
20178 ensure alignment is ok. */
20179 for (i = 0; i < symtab->size; ++i)
20180 {
20181 if (symtab->data[i])
3876f04e 20182 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20183 }
20184
20185 /* Now write out the hash table. */
20186 for (i = 0; i < symtab->size; ++i)
20187 {
20188 offset_type str_off, vec_off;
20189
20190 if (symtab->data[i])
20191 {
20192 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20193 vec_off = symtab->data[i]->index_offset;
20194 }
20195 else
20196 {
20197 /* While 0 is a valid constant pool index, it is not valid
20198 to have 0 for both offsets. */
20199 str_off = 0;
20200 vec_off = 0;
20201 }
20202
20203 str_off = MAYBE_SWAP (str_off);
20204 vec_off = MAYBE_SWAP (vec_off);
20205
20206 obstack_grow (output, &str_off, sizeof (str_off));
20207 obstack_grow (output, &vec_off, sizeof (vec_off));
20208 }
20209
20210 htab_delete (str_table);
3876f04e 20211 htab_delete (symbol_hash_table);
9291a0cd
TT
20212}
20213
0a5429f6
DE
20214/* Struct to map psymtab to CU index in the index file. */
20215struct psymtab_cu_index_map
20216{
20217 struct partial_symtab *psymtab;
20218 unsigned int cu_index;
20219};
20220
20221static hashval_t
20222hash_psymtab_cu_index (const void *item)
20223{
20224 const struct psymtab_cu_index_map *map = item;
20225
20226 return htab_hash_pointer (map->psymtab);
20227}
20228
20229static int
20230eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20231{
20232 const struct psymtab_cu_index_map *lhs = item_lhs;
20233 const struct psymtab_cu_index_map *rhs = item_rhs;
20234
20235 return lhs->psymtab == rhs->psymtab;
20236}
20237
20238/* Helper struct for building the address table. */
20239struct addrmap_index_data
20240{
20241 struct objfile *objfile;
20242 struct obstack *addr_obstack;
20243 htab_t cu_index_htab;
20244
20245 /* Non-zero if the previous_* fields are valid.
20246 We can't write an entry until we see the next entry (since it is only then
20247 that we know the end of the entry). */
20248 int previous_valid;
20249 /* Index of the CU in the table of all CUs in the index file. */
20250 unsigned int previous_cu_index;
0963b4bd 20251 /* Start address of the CU. */
0a5429f6
DE
20252 CORE_ADDR previous_cu_start;
20253};
20254
20255/* Write an address entry to OBSTACK. */
b89be57b 20256
9291a0cd 20257static void
0a5429f6
DE
20258add_address_entry (struct objfile *objfile, struct obstack *obstack,
20259 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20260{
0a5429f6 20261 offset_type cu_index_to_write;
9291a0cd
TT
20262 char addr[8];
20263 CORE_ADDR baseaddr;
20264
20265 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20266
0a5429f6
DE
20267 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20268 obstack_grow (obstack, addr, 8);
20269 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20270 obstack_grow (obstack, addr, 8);
20271 cu_index_to_write = MAYBE_SWAP (cu_index);
20272 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20273}
20274
20275/* Worker function for traversing an addrmap to build the address table. */
20276
20277static int
20278add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20279{
20280 struct addrmap_index_data *data = datap;
20281 struct partial_symtab *pst = obj;
0a5429f6
DE
20282
20283 if (data->previous_valid)
20284 add_address_entry (data->objfile, data->addr_obstack,
20285 data->previous_cu_start, start_addr,
20286 data->previous_cu_index);
20287
20288 data->previous_cu_start = start_addr;
20289 if (pst != NULL)
20290 {
20291 struct psymtab_cu_index_map find_map, *map;
20292 find_map.psymtab = pst;
20293 map = htab_find (data->cu_index_htab, &find_map);
20294 gdb_assert (map != NULL);
20295 data->previous_cu_index = map->cu_index;
20296 data->previous_valid = 1;
20297 }
20298 else
20299 data->previous_valid = 0;
20300
20301 return 0;
20302}
20303
20304/* Write OBJFILE's address map to OBSTACK.
20305 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20306 in the index file. */
20307
20308static void
20309write_address_map (struct objfile *objfile, struct obstack *obstack,
20310 htab_t cu_index_htab)
20311{
20312 struct addrmap_index_data addrmap_index_data;
20313
20314 /* When writing the address table, we have to cope with the fact that
20315 the addrmap iterator only provides the start of a region; we have to
20316 wait until the next invocation to get the start of the next region. */
20317
20318 addrmap_index_data.objfile = objfile;
20319 addrmap_index_data.addr_obstack = obstack;
20320 addrmap_index_data.cu_index_htab = cu_index_htab;
20321 addrmap_index_data.previous_valid = 0;
20322
20323 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20324 &addrmap_index_data);
20325
20326 /* It's highly unlikely the last entry (end address = 0xff...ff)
20327 is valid, but we should still handle it.
20328 The end address is recorded as the start of the next region, but that
20329 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20330 anyway. */
20331 if (addrmap_index_data.previous_valid)
20332 add_address_entry (objfile, obstack,
20333 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20334 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20335}
20336
156942c7
DE
20337/* Return the symbol kind of PSYM. */
20338
20339static gdb_index_symbol_kind
20340symbol_kind (struct partial_symbol *psym)
20341{
20342 domain_enum domain = PSYMBOL_DOMAIN (psym);
20343 enum address_class aclass = PSYMBOL_CLASS (psym);
20344
20345 switch (domain)
20346 {
20347 case VAR_DOMAIN:
20348 switch (aclass)
20349 {
20350 case LOC_BLOCK:
20351 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20352 case LOC_TYPEDEF:
20353 return GDB_INDEX_SYMBOL_KIND_TYPE;
20354 case LOC_COMPUTED:
20355 case LOC_CONST_BYTES:
20356 case LOC_OPTIMIZED_OUT:
20357 case LOC_STATIC:
20358 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20359 case LOC_CONST:
20360 /* Note: It's currently impossible to recognize psyms as enum values
20361 short of reading the type info. For now punt. */
20362 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20363 default:
20364 /* There are other LOC_FOO values that one might want to classify
20365 as variables, but dwarf2read.c doesn't currently use them. */
20366 return GDB_INDEX_SYMBOL_KIND_OTHER;
20367 }
20368 case STRUCT_DOMAIN:
20369 return GDB_INDEX_SYMBOL_KIND_TYPE;
20370 default:
20371 return GDB_INDEX_SYMBOL_KIND_OTHER;
20372 }
20373}
20374
9291a0cd 20375/* Add a list of partial symbols to SYMTAB. */
b89be57b 20376
9291a0cd
TT
20377static void
20378write_psymbols (struct mapped_symtab *symtab,
987d643c 20379 htab_t psyms_seen,
9291a0cd
TT
20380 struct partial_symbol **psymp,
20381 int count,
987d643c
TT
20382 offset_type cu_index,
20383 int is_static)
9291a0cd
TT
20384{
20385 for (; count-- > 0; ++psymp)
20386 {
156942c7
DE
20387 struct partial_symbol *psym = *psymp;
20388 void **slot;
987d643c 20389
156942c7 20390 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20391 error (_("Ada is not currently supported by the index"));
987d643c 20392
987d643c 20393 /* Only add a given psymbol once. */
156942c7 20394 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20395 if (!*slot)
20396 {
156942c7
DE
20397 gdb_index_symbol_kind kind = symbol_kind (psym);
20398
20399 *slot = psym;
20400 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20401 is_static, kind, cu_index);
987d643c 20402 }
9291a0cd
TT
20403 }
20404}
20405
20406/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20407 exception if there is an error. */
b89be57b 20408
9291a0cd
TT
20409static void
20410write_obstack (FILE *file, struct obstack *obstack)
20411{
20412 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20413 file)
20414 != obstack_object_size (obstack))
20415 error (_("couldn't data write to file"));
20416}
20417
20418/* Unlink a file if the argument is not NULL. */
b89be57b 20419
9291a0cd
TT
20420static void
20421unlink_if_set (void *p)
20422{
20423 char **filename = p;
20424 if (*filename)
20425 unlink (*filename);
20426}
20427
1fd400ff
TT
20428/* A helper struct used when iterating over debug_types. */
20429struct signatured_type_index_data
20430{
20431 struct objfile *objfile;
20432 struct mapped_symtab *symtab;
20433 struct obstack *types_list;
987d643c 20434 htab_t psyms_seen;
1fd400ff
TT
20435 int cu_index;
20436};
20437
20438/* A helper function that writes a single signatured_type to an
20439 obstack. */
b89be57b 20440
1fd400ff
TT
20441static int
20442write_one_signatured_type (void **slot, void *d)
20443{
20444 struct signatured_type_index_data *info = d;
20445 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20446 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20447 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20448 gdb_byte val[8];
20449
20450 write_psymbols (info->symtab,
987d643c 20451 info->psyms_seen,
3e43a32a
MS
20452 info->objfile->global_psymbols.list
20453 + psymtab->globals_offset,
987d643c
TT
20454 psymtab->n_global_syms, info->cu_index,
20455 0);
1fd400ff 20456 write_psymbols (info->symtab,
987d643c 20457 info->psyms_seen,
3e43a32a
MS
20458 info->objfile->static_psymbols.list
20459 + psymtab->statics_offset,
987d643c
TT
20460 psymtab->n_static_syms, info->cu_index,
20461 1);
1fd400ff 20462
b64f50a1
JK
20463 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20464 entry->per_cu.offset.sect_off);
1fd400ff 20465 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20466 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20467 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20468 obstack_grow (info->types_list, val, 8);
20469 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20470 obstack_grow (info->types_list, val, 8);
20471
20472 ++info->cu_index;
20473
20474 return 1;
20475}
20476
95554aad
TT
20477/* Recurse into all "included" dependencies and write their symbols as
20478 if they appeared in this psymtab. */
20479
20480static void
20481recursively_write_psymbols (struct objfile *objfile,
20482 struct partial_symtab *psymtab,
20483 struct mapped_symtab *symtab,
20484 htab_t psyms_seen,
20485 offset_type cu_index)
20486{
20487 int i;
20488
20489 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20490 if (psymtab->dependencies[i]->user != NULL)
20491 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20492 symtab, psyms_seen, cu_index);
20493
20494 write_psymbols (symtab,
20495 psyms_seen,
20496 objfile->global_psymbols.list + psymtab->globals_offset,
20497 psymtab->n_global_syms, cu_index,
20498 0);
20499 write_psymbols (symtab,
20500 psyms_seen,
20501 objfile->static_psymbols.list + psymtab->statics_offset,
20502 psymtab->n_static_syms, cu_index,
20503 1);
20504}
20505
9291a0cd 20506/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20507
9291a0cd
TT
20508static void
20509write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20510{
20511 struct cleanup *cleanup;
20512 char *filename, *cleanup_filename;
1fd400ff
TT
20513 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20514 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20515 int i;
20516 FILE *out_file;
20517 struct mapped_symtab *symtab;
20518 offset_type val, size_of_contents, total_len;
20519 struct stat st;
987d643c 20520 htab_t psyms_seen;
0a5429f6
DE
20521 htab_t cu_index_htab;
20522 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20523
b4f2f049 20524 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20525 return;
b4f2f049 20526
9291a0cd
TT
20527 if (dwarf2_per_objfile->using_index)
20528 error (_("Cannot use an index to create the index"));
20529
8b70b953
TT
20530 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20531 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20532
9291a0cd 20533 if (stat (objfile->name, &st) < 0)
7e17e088 20534 perror_with_name (objfile->name);
9291a0cd
TT
20535
20536 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20537 INDEX_SUFFIX, (char *) NULL);
20538 cleanup = make_cleanup (xfree, filename);
20539
20540 out_file = fopen (filename, "wb");
20541 if (!out_file)
20542 error (_("Can't open `%s' for writing"), filename);
20543
20544 cleanup_filename = filename;
20545 make_cleanup (unlink_if_set, &cleanup_filename);
20546
20547 symtab = create_mapped_symtab ();
20548 make_cleanup (cleanup_mapped_symtab, symtab);
20549
20550 obstack_init (&addr_obstack);
20551 make_cleanup_obstack_free (&addr_obstack);
20552
20553 obstack_init (&cu_list);
20554 make_cleanup_obstack_free (&cu_list);
20555
1fd400ff
TT
20556 obstack_init (&types_cu_list);
20557 make_cleanup_obstack_free (&types_cu_list);
20558
987d643c
TT
20559 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20560 NULL, xcalloc, xfree);
96408a79 20561 make_cleanup_htab_delete (psyms_seen);
987d643c 20562
0a5429f6
DE
20563 /* While we're scanning CU's create a table that maps a psymtab pointer
20564 (which is what addrmap records) to its index (which is what is recorded
20565 in the index file). This will later be needed to write the address
20566 table. */
20567 cu_index_htab = htab_create_alloc (100,
20568 hash_psymtab_cu_index,
20569 eq_psymtab_cu_index,
20570 NULL, xcalloc, xfree);
96408a79 20571 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20572 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20573 xmalloc (sizeof (struct psymtab_cu_index_map)
20574 * dwarf2_per_objfile->n_comp_units);
20575 make_cleanup (xfree, psymtab_cu_index_map);
20576
20577 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20578 work here. Also, the debug_types entries do not appear in
20579 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20580 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20581 {
3e43a32a
MS
20582 struct dwarf2_per_cu_data *per_cu
20583 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20584 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20585 gdb_byte val[8];
0a5429f6
DE
20586 struct psymtab_cu_index_map *map;
20587 void **slot;
9291a0cd 20588
95554aad
TT
20589 if (psymtab->user == NULL)
20590 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20591
0a5429f6
DE
20592 map = &psymtab_cu_index_map[i];
20593 map->psymtab = psymtab;
20594 map->cu_index = i;
20595 slot = htab_find_slot (cu_index_htab, map, INSERT);
20596 gdb_assert (slot != NULL);
20597 gdb_assert (*slot == NULL);
20598 *slot = map;
9291a0cd 20599
b64f50a1
JK
20600 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20601 per_cu->offset.sect_off);
9291a0cd 20602 obstack_grow (&cu_list, val, 8);
e254ef6a 20603 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20604 obstack_grow (&cu_list, val, 8);
20605 }
20606
0a5429f6
DE
20607 /* Dump the address map. */
20608 write_address_map (objfile, &addr_obstack, cu_index_htab);
20609
1fd400ff
TT
20610 /* Write out the .debug_type entries, if any. */
20611 if (dwarf2_per_objfile->signatured_types)
20612 {
20613 struct signatured_type_index_data sig_data;
20614
20615 sig_data.objfile = objfile;
20616 sig_data.symtab = symtab;
20617 sig_data.types_list = &types_cu_list;
987d643c 20618 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20619 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20620 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20621 write_one_signatured_type, &sig_data);
20622 }
20623
156942c7
DE
20624 /* Now that we've processed all symbols we can shrink their cu_indices
20625 lists. */
20626 uniquify_cu_indices (symtab);
20627
9291a0cd
TT
20628 obstack_init (&constant_pool);
20629 make_cleanup_obstack_free (&constant_pool);
20630 obstack_init (&symtab_obstack);
20631 make_cleanup_obstack_free (&symtab_obstack);
20632 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20633
20634 obstack_init (&contents);
20635 make_cleanup_obstack_free (&contents);
1fd400ff 20636 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20637 total_len = size_of_contents;
20638
20639 /* The version number. */
796a7ff8 20640 val = MAYBE_SWAP (8);
9291a0cd
TT
20641 obstack_grow (&contents, &val, sizeof (val));
20642
20643 /* The offset of the CU list from the start of the file. */
20644 val = MAYBE_SWAP (total_len);
20645 obstack_grow (&contents, &val, sizeof (val));
20646 total_len += obstack_object_size (&cu_list);
20647
1fd400ff
TT
20648 /* The offset of the types CU list from the start of the file. */
20649 val = MAYBE_SWAP (total_len);
20650 obstack_grow (&contents, &val, sizeof (val));
20651 total_len += obstack_object_size (&types_cu_list);
20652
9291a0cd
TT
20653 /* The offset of the address table from the start of the file. */
20654 val = MAYBE_SWAP (total_len);
20655 obstack_grow (&contents, &val, sizeof (val));
20656 total_len += obstack_object_size (&addr_obstack);
20657
20658 /* The offset of the symbol table from the start of the file. */
20659 val = MAYBE_SWAP (total_len);
20660 obstack_grow (&contents, &val, sizeof (val));
20661 total_len += obstack_object_size (&symtab_obstack);
20662
20663 /* The offset of the constant pool from the start of the file. */
20664 val = MAYBE_SWAP (total_len);
20665 obstack_grow (&contents, &val, sizeof (val));
20666 total_len += obstack_object_size (&constant_pool);
20667
20668 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20669
20670 write_obstack (out_file, &contents);
20671 write_obstack (out_file, &cu_list);
1fd400ff 20672 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20673 write_obstack (out_file, &addr_obstack);
20674 write_obstack (out_file, &symtab_obstack);
20675 write_obstack (out_file, &constant_pool);
20676
20677 fclose (out_file);
20678
20679 /* We want to keep the file, so we set cleanup_filename to NULL
20680 here. See unlink_if_set. */
20681 cleanup_filename = NULL;
20682
20683 do_cleanups (cleanup);
20684}
20685
90476074
TT
20686/* Implementation of the `save gdb-index' command.
20687
20688 Note that the file format used by this command is documented in the
20689 GDB manual. Any changes here must be documented there. */
11570e71 20690
9291a0cd
TT
20691static void
20692save_gdb_index_command (char *arg, int from_tty)
20693{
20694 struct objfile *objfile;
20695
20696 if (!arg || !*arg)
96d19272 20697 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20698
20699 ALL_OBJFILES (objfile)
20700 {
20701 struct stat st;
20702
20703 /* If the objfile does not correspond to an actual file, skip it. */
20704 if (stat (objfile->name, &st) < 0)
20705 continue;
20706
20707 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20708 if (dwarf2_per_objfile)
20709 {
20710 volatile struct gdb_exception except;
20711
20712 TRY_CATCH (except, RETURN_MASK_ERROR)
20713 {
20714 write_psymtabs_to_index (objfile, arg);
20715 }
20716 if (except.reason < 0)
20717 exception_fprintf (gdb_stderr, except,
20718 _("Error while writing index for `%s': "),
20719 objfile->name);
20720 }
20721 }
dce234bc
PP
20722}
20723
9291a0cd
TT
20724\f
20725
9eae7c52
TT
20726int dwarf2_always_disassemble;
20727
20728static void
20729show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20730 struct cmd_list_element *c, const char *value)
20731{
3e43a32a
MS
20732 fprintf_filtered (file,
20733 _("Whether to always disassemble "
20734 "DWARF expressions is %s.\n"),
9eae7c52
TT
20735 value);
20736}
20737
900e11f9
JK
20738static void
20739show_check_physname (struct ui_file *file, int from_tty,
20740 struct cmd_list_element *c, const char *value)
20741{
20742 fprintf_filtered (file,
20743 _("Whether to check \"physname\" is %s.\n"),
20744 value);
20745}
20746
6502dd73
DJ
20747void _initialize_dwarf2_read (void);
20748
20749void
20750_initialize_dwarf2_read (void)
20751{
96d19272
JK
20752 struct cmd_list_element *c;
20753
dce234bc 20754 dwarf2_objfile_data_key
c1bd65d0 20755 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20756
1bedd215
AC
20757 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20758Set DWARF 2 specific variables.\n\
20759Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20760 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20761 0/*allow-unknown*/, &maintenance_set_cmdlist);
20762
1bedd215
AC
20763 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20764Show DWARF 2 specific variables\n\
20765Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20766 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20767 0/*allow-unknown*/, &maintenance_show_cmdlist);
20768
20769 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20770 &dwarf2_max_cache_age, _("\
20771Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20772Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20773A higher limit means that cached compilation units will be stored\n\
20774in memory longer, and more total memory will be used. Zero disables\n\
20775caching, which can slow down startup."),
2c5b56ce 20776 NULL,
920d2a44 20777 show_dwarf2_max_cache_age,
2c5b56ce 20778 &set_dwarf2_cmdlist,
ae038cb0 20779 &show_dwarf2_cmdlist);
d97bc12b 20780
9eae7c52
TT
20781 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20782 &dwarf2_always_disassemble, _("\
20783Set whether `info address' always disassembles DWARF expressions."), _("\
20784Show whether `info address' always disassembles DWARF expressions."), _("\
20785When enabled, DWARF expressions are always printed in an assembly-like\n\
20786syntax. When disabled, expressions will be printed in a more\n\
20787conversational style, when possible."),
20788 NULL,
20789 show_dwarf2_always_disassemble,
20790 &set_dwarf2_cmdlist,
20791 &show_dwarf2_cmdlist);
20792
45cfd468
DE
20793 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20794Set debugging of the dwarf2 reader."), _("\
20795Show debugging of the dwarf2 reader."), _("\
20796When enabled, debugging messages are printed during dwarf2 reading\n\
20797and symtab expansion."),
20798 NULL,
20799 NULL,
20800 &setdebuglist, &showdebuglist);
20801
ccce17b0 20802 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20803Set debugging of the dwarf2 DIE reader."), _("\
20804Show debugging of the dwarf2 DIE reader."), _("\
20805When enabled (non-zero), DIEs are dumped after they are read in.\n\
20806The value is the maximum depth to print."),
ccce17b0
YQ
20807 NULL,
20808 NULL,
20809 &setdebuglist, &showdebuglist);
9291a0cd 20810
900e11f9
JK
20811 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20812Set cross-checking of \"physname\" code against demangler."), _("\
20813Show cross-checking of \"physname\" code against demangler."), _("\
20814When enabled, GDB's internal \"physname\" code is checked against\n\
20815the demangler."),
20816 NULL, show_check_physname,
20817 &setdebuglist, &showdebuglist);
20818
e615022a
DE
20819 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20820 no_class, &use_deprecated_index_sections, _("\
20821Set whether to use deprecated gdb_index sections."), _("\
20822Show whether to use deprecated gdb_index sections."), _("\
20823When enabled, deprecated .gdb_index sections are used anyway.\n\
20824Normally they are ignored either because of a missing feature or\n\
20825performance issue.\n\
20826Warning: This option must be enabled before gdb reads the file."),
20827 NULL,
20828 NULL,
20829 &setlist, &showlist);
20830
96d19272 20831 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20832 _("\
fc1a9d6e 20833Save a gdb-index file.\n\
11570e71 20834Usage: save gdb-index DIRECTORY"),
96d19272
JK
20835 &save_cmdlist);
20836 set_cmd_completer (c, filename_completer);
f1e6e072
TT
20837
20838 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20839 &dwarf2_locexpr_funcs);
20840 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20841 &dwarf2_loclist_funcs);
20842
20843 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20844 &dwarf2_block_frame_base_locexpr_funcs);
20845 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20846 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 20847}
This page took 2.963041 seconds and 4 git commands to generate.