NEWS/manual: Missing documentation for new commands in 7.6.
[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
0186c6a7
DE
545 /* Non-zero if this CU is from .debug_types.
546 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
547 this is non-zero. */
3019eac3
DE
548 unsigned int is_debug_types : 1;
549
36586728
TT
550 /* Non-zero if this CU is from the .dwz file. */
551 unsigned int is_dwz : 1;
552
3019eac3
DE
553 /* The section this CU/TU lives in.
554 If the DIE refers to a DWO file, this is always the original die,
555 not the DWO file. */
8a0459fd 556 struct dwarf2_section_info *section;
348e048f 557
17ea53c3
JK
558 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
559 of the CU cache it gets reset to NULL again. */
ae038cb0 560 struct dwarf2_cu *cu;
1c379e20 561
9cdd5dbd
DE
562 /* The corresponding objfile.
563 Normally we can get the objfile from dwarf2_per_objfile.
564 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
565 struct objfile *objfile;
566
567 /* When using partial symbol tables, the 'psymtab' field is active.
568 Otherwise the 'quick' field is active. */
569 union
570 {
571 /* The partial symbol table associated with this compilation unit,
95554aad 572 or NULL for unread partial units. */
9291a0cd
TT
573 struct partial_symtab *psymtab;
574
575 /* Data needed by the "quick" functions. */
576 struct dwarf2_per_cu_quick_data *quick;
577 } v;
95554aad 578
796a7ff8
DE
579 /* The CUs we import using DW_TAG_imported_unit. This is filled in
580 while reading psymtabs, used to compute the psymtab dependencies,
581 and then cleared. Then it is filled in again while reading full
582 symbols, and only deleted when the objfile is destroyed.
583
584 This is also used to work around a difference between the way gold
585 generates .gdb_index version <=7 and the way gdb does. Arguably this
586 is a gold bug. For symbols coming from TUs, gold records in the index
587 the CU that includes the TU instead of the TU itself. This breaks
588 dw2_lookup_symbol: It assumes that if the index says symbol X lives
589 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
590 will find X. Alas TUs live in their own symtab, so after expanding CU Y
591 we need to look in TU Z to find X. Fortunately, this is akin to
592 DW_TAG_imported_unit, so we just use the same mechanism: For
593 .gdb_index version <=7 this also records the TUs that the CU referred
594 to. Concurrently with this change gdb was modified to emit version 8
595 indices so we only pay a price for gold generated indices. */
596 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
597};
598
348e048f
DE
599/* Entry in the signatured_types hash table. */
600
601struct signatured_type
602{
42e7ad6c
DE
603 /* The "per_cu" object of this type.
604 N.B.: This is the first member so that it's easy to convert pointers
605 between them. */
606 struct dwarf2_per_cu_data per_cu;
607
3019eac3 608 /* The type's signature. */
348e048f
DE
609 ULONGEST signature;
610
3019eac3 611 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
612 If this TU is a DWO stub and the definition lives in a DWO file
613 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
614 cu_offset type_offset_in_tu;
615
616 /* Offset in the section of the type's DIE.
617 If the definition lives in a DWO file, this is the offset in the
618 .debug_types.dwo section.
619 The value is zero until the actual value is known.
620 Zero is otherwise not a valid section offset. */
621 sect_offset type_offset_in_section;
0186c6a7
DE
622
623 /* Type units are grouped by their DW_AT_stmt_list entry so that they
624 can share them. This points to the containing symtab. */
625 struct type_unit_group *type_unit_group;
348e048f
DE
626};
627
0186c6a7
DE
628typedef struct signatured_type *sig_type_ptr;
629DEF_VEC_P (sig_type_ptr);
630
094b34ac
DE
631/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
632 This includes type_unit_group and quick_file_names. */
633
634struct stmt_list_hash
635{
636 /* The DWO unit this table is from or NULL if there is none. */
637 struct dwo_unit *dwo_unit;
638
639 /* Offset in .debug_line or .debug_line.dwo. */
640 sect_offset line_offset;
641};
642
f4dc4d17
DE
643/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
644 an object of this type. */
645
646struct type_unit_group
647{
0186c6a7 648 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
649 To simplify things we create an artificial CU that "includes" all the
650 type units using this stmt_list so that the rest of the code still has
651 a "per_cu" handle on the symtab.
652 This PER_CU is recognized by having no section. */
8a0459fd 653#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
654 struct dwarf2_per_cu_data per_cu;
655
0186c6a7
DE
656 /* The TUs that share this DW_AT_stmt_list entry.
657 This is added to while parsing type units to build partial symtabs,
658 and is deleted afterwards and not used again. */
659 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
660
661 /* The primary symtab.
094b34ac
DE
662 Type units in a group needn't all be defined in the same source file,
663 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
664 struct symtab *primary_symtab;
665
094b34ac
DE
666 /* The data used to construct the hash key. */
667 struct stmt_list_hash hash;
f4dc4d17
DE
668
669 /* The number of symtabs from the line header.
670 The value here must match line_header.num_file_names. */
671 unsigned int num_symtabs;
672
673 /* The symbol tables for this TU (obtained from the files listed in
674 DW_AT_stmt_list).
675 WARNING: The order of entries here must match the order of entries
676 in the line header. After the first TU using this type_unit_group, the
677 line header for the subsequent TUs is recreated from this. This is done
678 because we need to use the same symtabs for each TU using the same
679 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
680 there's no guarantee the line header doesn't have duplicate entries. */
681 struct symtab **symtabs;
682};
683
80626a55 684/* These sections are what may appear in a DWO file. */
3019eac3
DE
685
686struct dwo_sections
687{
688 struct dwarf2_section_info abbrev;
3019eac3
DE
689 struct dwarf2_section_info line;
690 struct dwarf2_section_info loc;
09262596
DE
691 struct dwarf2_section_info macinfo;
692 struct dwarf2_section_info macro;
3019eac3
DE
693 struct dwarf2_section_info str;
694 struct dwarf2_section_info str_offsets;
80626a55
DE
695 /* In the case of a virtual DWO file, these two are unused. */
696 struct dwarf2_section_info info;
3019eac3
DE
697 VEC (dwarf2_section_info_def) *types;
698};
699
c88ee1f0 700/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
701
702struct dwo_unit
703{
704 /* Backlink to the containing struct dwo_file. */
705 struct dwo_file *dwo_file;
706
707 /* The "id" that distinguishes this CU/TU.
708 .debug_info calls this "dwo_id", .debug_types calls this "signature".
709 Since signatures came first, we stick with it for consistency. */
710 ULONGEST signature;
711
712 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 713 struct dwarf2_section_info *section;
3019eac3
DE
714
715 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
716 sect_offset offset;
717 unsigned int length;
718
719 /* For types, offset in the type's DIE of the type defined by this TU. */
720 cu_offset type_offset_in_tu;
721};
722
80626a55
DE
723/* Data for one DWO file.
724 This includes virtual DWO files that have been packaged into a
725 DWP file. */
3019eac3
DE
726
727struct dwo_file
728{
80626a55
DE
729 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
730 For virtual DWO files the name is constructed from the section offsets
731 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
732 from related CU+TUs. */
733 const char *name;
3019eac3 734
80626a55
DE
735 /* The bfd, when the file is open. Otherwise this is NULL.
736 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
737 bfd *dbfd;
3019eac3
DE
738
739 /* Section info for this file. */
740 struct dwo_sections sections;
741
742 /* Table of CUs in the file.
743 Each element is a struct dwo_unit. */
744 htab_t cus;
745
746 /* Table of TUs in the file.
747 Each element is a struct dwo_unit. */
748 htab_t tus;
749};
750
80626a55
DE
751/* These sections are what may appear in a DWP file. */
752
753struct dwp_sections
754{
755 struct dwarf2_section_info str;
756 struct dwarf2_section_info cu_index;
757 struct dwarf2_section_info tu_index;
758 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
759 by section number. We don't need to record them here. */
760};
761
762/* These sections are what may appear in a virtual DWO file. */
763
764struct virtual_dwo_sections
765{
766 struct dwarf2_section_info abbrev;
767 struct dwarf2_section_info line;
768 struct dwarf2_section_info loc;
769 struct dwarf2_section_info macinfo;
770 struct dwarf2_section_info macro;
771 struct dwarf2_section_info str_offsets;
772 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 773 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
774 struct dwarf2_section_info info_or_types;
775};
776
777/* Contents of DWP hash tables. */
778
779struct dwp_hash_table
780{
781 uint32_t nr_units, nr_slots;
782 const gdb_byte *hash_table, *unit_table, *section_pool;
783};
784
785/* Data for one DWP file. */
786
787struct dwp_file
788{
789 /* Name of the file. */
790 const char *name;
791
792 /* The bfd, when the file is open. Otherwise this is NULL. */
793 bfd *dbfd;
794
795 /* Section info for this file. */
796 struct dwp_sections sections;
797
798 /* Table of CUs in the file. */
799 const struct dwp_hash_table *cus;
800
801 /* Table of TUs in the file. */
802 const struct dwp_hash_table *tus;
803
804 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
805 htab_t loaded_cutus;
806
807 /* Table to map ELF section numbers to their sections. */
808 unsigned int num_sections;
809 asection **elf_sections;
810};
811
36586728
TT
812/* This represents a '.dwz' file. */
813
814struct dwz_file
815{
816 /* A dwz file can only contain a few sections. */
817 struct dwarf2_section_info abbrev;
818 struct dwarf2_section_info info;
819 struct dwarf2_section_info str;
820 struct dwarf2_section_info line;
821 struct dwarf2_section_info macro;
2ec9a5e0 822 struct dwarf2_section_info gdb_index;
36586728
TT
823
824 /* The dwz's BFD. */
825 bfd *dwz_bfd;
826};
827
0963b4bd
MS
828/* Struct used to pass misc. parameters to read_die_and_children, et
829 al. which are used for both .debug_info and .debug_types dies.
830 All parameters here are unchanging for the life of the call. This
dee91e82 831 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
832
833struct die_reader_specs
834{
dee91e82 835 /* die_section->asection->owner. */
93311388
DE
836 bfd* abfd;
837
838 /* The CU of the DIE we are parsing. */
839 struct dwarf2_cu *cu;
840
80626a55 841 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
842 struct dwo_file *dwo_file;
843
dee91e82 844 /* The section the die comes from.
3019eac3 845 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
846 struct dwarf2_section_info *die_section;
847
848 /* die_section->buffer. */
849 gdb_byte *buffer;
f664829e
DE
850
851 /* The end of the buffer. */
852 const gdb_byte *buffer_end;
93311388
DE
853};
854
fd820528 855/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
856typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
857 gdb_byte *info_ptr,
858 struct die_info *comp_unit_die,
859 int has_children,
860 void *data);
861
debd256d
JB
862/* The line number information for a compilation unit (found in the
863 .debug_line section) begins with a "statement program header",
864 which contains the following information. */
865struct line_header
866{
867 unsigned int total_length;
868 unsigned short version;
869 unsigned int header_length;
870 unsigned char minimum_instruction_length;
2dc7f7b3 871 unsigned char maximum_ops_per_instruction;
debd256d
JB
872 unsigned char default_is_stmt;
873 int line_base;
874 unsigned char line_range;
875 unsigned char opcode_base;
876
877 /* standard_opcode_lengths[i] is the number of operands for the
878 standard opcode whose value is i. This means that
879 standard_opcode_lengths[0] is unused, and the last meaningful
880 element is standard_opcode_lengths[opcode_base - 1]. */
881 unsigned char *standard_opcode_lengths;
882
883 /* The include_directories table. NOTE! These strings are not
884 allocated with xmalloc; instead, they are pointers into
885 debug_line_buffer. If you try to free them, `free' will get
886 indigestion. */
887 unsigned int num_include_dirs, include_dirs_size;
888 char **include_dirs;
889
890 /* The file_names table. NOTE! These strings are not allocated
891 with xmalloc; instead, they are pointers into debug_line_buffer.
892 Don't try to free them directly. */
893 unsigned int num_file_names, file_names_size;
894 struct file_entry
c906108c 895 {
debd256d
JB
896 char *name;
897 unsigned int dir_index;
898 unsigned int mod_time;
899 unsigned int length;
aaa75496 900 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 901 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
902 } *file_names;
903
904 /* The start and end of the statement program following this
6502dd73 905 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 906 gdb_byte *statement_program_start, *statement_program_end;
debd256d 907};
c906108c
SS
908
909/* When we construct a partial symbol table entry we only
0963b4bd 910 need this much information. */
c906108c
SS
911struct partial_die_info
912 {
72bf9492 913 /* Offset of this DIE. */
b64f50a1 914 sect_offset offset;
72bf9492
DJ
915
916 /* DWARF-2 tag for this DIE. */
917 ENUM_BITFIELD(dwarf_tag) tag : 16;
918
72bf9492
DJ
919 /* Assorted flags describing the data found in this DIE. */
920 unsigned int has_children : 1;
921 unsigned int is_external : 1;
922 unsigned int is_declaration : 1;
923 unsigned int has_type : 1;
924 unsigned int has_specification : 1;
925 unsigned int has_pc_info : 1;
481860b3 926 unsigned int may_be_inlined : 1;
72bf9492
DJ
927
928 /* Flag set if the SCOPE field of this structure has been
929 computed. */
930 unsigned int scope_set : 1;
931
fa4028e9
JB
932 /* Flag set if the DIE has a byte_size attribute. */
933 unsigned int has_byte_size : 1;
934
98bfdba5
PA
935 /* Flag set if any of the DIE's children are template arguments. */
936 unsigned int has_template_arguments : 1;
937
abc72ce4
DE
938 /* Flag set if fixup_partial_die has been called on this die. */
939 unsigned int fixup_called : 1;
940
36586728
TT
941 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
942 unsigned int is_dwz : 1;
943
944 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
945 unsigned int spec_is_dwz : 1;
946
72bf9492 947 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 948 sometimes a default name for unnamed DIEs. */
15d034d0 949 const char *name;
72bf9492 950
abc72ce4
DE
951 /* The linkage name, if present. */
952 const char *linkage_name;
953
72bf9492
DJ
954 /* The scope to prepend to our children. This is generally
955 allocated on the comp_unit_obstack, so will disappear
956 when this compilation unit leaves the cache. */
15d034d0 957 const char *scope;
72bf9492 958
95554aad
TT
959 /* Some data associated with the partial DIE. The tag determines
960 which field is live. */
961 union
962 {
963 /* The location description associated with this DIE, if any. */
964 struct dwarf_block *locdesc;
965 /* The offset of an import, for DW_TAG_imported_unit. */
966 sect_offset offset;
967 } d;
72bf9492
DJ
968
969 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
970 CORE_ADDR lowpc;
971 CORE_ADDR highpc;
72bf9492 972
93311388 973 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 974 DW_AT_sibling, if any. */
abc72ce4
DE
975 /* NOTE: This member isn't strictly necessary, read_partial_die could
976 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 977 gdb_byte *sibling;
72bf9492
DJ
978
979 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
980 DW_AT_specification (or DW_AT_abstract_origin or
981 DW_AT_extension). */
b64f50a1 982 sect_offset spec_offset;
72bf9492
DJ
983
984 /* Pointers to this DIE's parent, first child, and next sibling,
985 if any. */
986 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
987 };
988
0963b4bd 989/* This data structure holds the information of an abbrev. */
c906108c
SS
990struct abbrev_info
991 {
992 unsigned int number; /* number identifying abbrev */
993 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
994 unsigned short has_children; /* boolean */
995 unsigned short num_attrs; /* number of attributes */
c906108c
SS
996 struct attr_abbrev *attrs; /* an array of attribute descriptions */
997 struct abbrev_info *next; /* next in chain */
998 };
999
1000struct attr_abbrev
1001 {
9d25dd43
DE
1002 ENUM_BITFIELD(dwarf_attribute) name : 16;
1003 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1004 };
1005
433df2d4
DE
1006/* Size of abbrev_table.abbrev_hash_table. */
1007#define ABBREV_HASH_SIZE 121
1008
1009/* Top level data structure to contain an abbreviation table. */
1010
1011struct abbrev_table
1012{
f4dc4d17
DE
1013 /* Where the abbrev table came from.
1014 This is used as a sanity check when the table is used. */
433df2d4
DE
1015 sect_offset offset;
1016
1017 /* Storage for the abbrev table. */
1018 struct obstack abbrev_obstack;
1019
1020 /* Hash table of abbrevs.
1021 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1022 It could be statically allocated, but the previous code didn't so we
1023 don't either. */
1024 struct abbrev_info **abbrevs;
1025};
1026
0963b4bd 1027/* Attributes have a name and a value. */
b60c80d6
DJ
1028struct attribute
1029 {
9d25dd43 1030 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1031 ENUM_BITFIELD(dwarf_form) form : 15;
1032
1033 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1034 field should be in u.str (existing only for DW_STRING) but it is kept
1035 here for better struct attribute alignment. */
1036 unsigned int string_is_canonical : 1;
1037
b60c80d6
DJ
1038 union
1039 {
15d034d0 1040 const char *str;
b60c80d6 1041 struct dwarf_block *blk;
43bbcdc2
PH
1042 ULONGEST unsnd;
1043 LONGEST snd;
b60c80d6 1044 CORE_ADDR addr;
348e048f 1045 struct signatured_type *signatured_type;
b60c80d6
DJ
1046 }
1047 u;
1048 };
1049
0963b4bd 1050/* This data structure holds a complete die structure. */
c906108c
SS
1051struct die_info
1052 {
76815b17
DE
1053 /* DWARF-2 tag for this DIE. */
1054 ENUM_BITFIELD(dwarf_tag) tag : 16;
1055
1056 /* Number of attributes */
98bfdba5
PA
1057 unsigned char num_attrs;
1058
1059 /* True if we're presently building the full type name for the
1060 type derived from this DIE. */
1061 unsigned char building_fullname : 1;
76815b17
DE
1062
1063 /* Abbrev number */
1064 unsigned int abbrev;
1065
93311388 1066 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1067 sect_offset offset;
78ba4af6
JB
1068
1069 /* The dies in a compilation unit form an n-ary tree. PARENT
1070 points to this die's parent; CHILD points to the first child of
1071 this node; and all the children of a given node are chained
4950bc1c 1072 together via their SIBLING fields. */
639d11d3
DC
1073 struct die_info *child; /* Its first child, if any. */
1074 struct die_info *sibling; /* Its next sibling, if any. */
1075 struct die_info *parent; /* Its parent, if any. */
c906108c 1076
b60c80d6
DJ
1077 /* An array of attributes, with NUM_ATTRS elements. There may be
1078 zero, but it's not common and zero-sized arrays are not
1079 sufficiently portable C. */
1080 struct attribute attrs[1];
c906108c
SS
1081 };
1082
0963b4bd 1083/* Get at parts of an attribute structure. */
c906108c
SS
1084
1085#define DW_STRING(attr) ((attr)->u.str)
8285870a 1086#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1087#define DW_UNSND(attr) ((attr)->u.unsnd)
1088#define DW_BLOCK(attr) ((attr)->u.blk)
1089#define DW_SND(attr) ((attr)->u.snd)
1090#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1091#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1092
0963b4bd 1093/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1094struct dwarf_block
1095 {
56eb65bd 1096 size_t size;
1d6edc3c
JK
1097
1098 /* Valid only if SIZE is not zero. */
fe1b8b76 1099 gdb_byte *data;
c906108c
SS
1100 };
1101
c906108c
SS
1102#ifndef ATTR_ALLOC_CHUNK
1103#define ATTR_ALLOC_CHUNK 4
1104#endif
1105
c906108c
SS
1106/* Allocate fields for structs, unions and enums in this size. */
1107#ifndef DW_FIELD_ALLOC_CHUNK
1108#define DW_FIELD_ALLOC_CHUNK 4
1109#endif
1110
c906108c
SS
1111/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1112 but this would require a corresponding change in unpack_field_as_long
1113 and friends. */
1114static int bits_per_byte = 8;
1115
1116/* The routines that read and process dies for a C struct or C++ class
1117 pass lists of data member fields and lists of member function fields
1118 in an instance of a field_info structure, as defined below. */
1119struct field_info
c5aa993b 1120 {
0963b4bd 1121 /* List of data member and baseclasses fields. */
c5aa993b
JM
1122 struct nextfield
1123 {
1124 struct nextfield *next;
1125 int accessibility;
1126 int virtuality;
1127 struct field field;
1128 }
7d0ccb61 1129 *fields, *baseclasses;
c906108c 1130
7d0ccb61 1131 /* Number of fields (including baseclasses). */
c5aa993b 1132 int nfields;
c906108c 1133
c5aa993b
JM
1134 /* Number of baseclasses. */
1135 int nbaseclasses;
c906108c 1136
c5aa993b
JM
1137 /* Set if the accesibility of one of the fields is not public. */
1138 int non_public_fields;
c906108c 1139
c5aa993b
JM
1140 /* Member function fields array, entries are allocated in the order they
1141 are encountered in the object file. */
1142 struct nextfnfield
1143 {
1144 struct nextfnfield *next;
1145 struct fn_field fnfield;
1146 }
1147 *fnfields;
c906108c 1148
c5aa993b
JM
1149 /* Member function fieldlist array, contains name of possibly overloaded
1150 member function, number of overloaded member functions and a pointer
1151 to the head of the member function field chain. */
1152 struct fnfieldlist
1153 {
15d034d0 1154 const char *name;
c5aa993b
JM
1155 int length;
1156 struct nextfnfield *head;
1157 }
1158 *fnfieldlists;
c906108c 1159
c5aa993b
JM
1160 /* Number of entries in the fnfieldlists array. */
1161 int nfnfields;
98751a41
JK
1162
1163 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1164 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1165 struct typedef_field_list
1166 {
1167 struct typedef_field field;
1168 struct typedef_field_list *next;
1169 }
1170 *typedef_field_list;
1171 unsigned typedef_field_list_count;
c5aa993b 1172 };
c906108c 1173
10b3939b
DJ
1174/* One item on the queue of compilation units to read in full symbols
1175 for. */
1176struct dwarf2_queue_item
1177{
1178 struct dwarf2_per_cu_data *per_cu;
95554aad 1179 enum language pretend_language;
10b3939b
DJ
1180 struct dwarf2_queue_item *next;
1181};
1182
1183/* The current queue. */
1184static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1185
ae038cb0
DJ
1186/* Loaded secondary compilation units are kept in memory until they
1187 have not been referenced for the processing of this many
1188 compilation units. Set this to zero to disable caching. Cache
1189 sizes of up to at least twenty will improve startup time for
1190 typical inter-CU-reference binaries, at an obvious memory cost. */
1191static int dwarf2_max_cache_age = 5;
920d2a44
AC
1192static void
1193show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1194 struct cmd_list_element *c, const char *value)
1195{
3e43a32a
MS
1196 fprintf_filtered (file, _("The upper bound on the age of cached "
1197 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1198 value);
1199}
1200
ae038cb0 1201
0963b4bd 1202/* Various complaints about symbol reading that don't abort the process. */
c906108c 1203
4d3c2250
KB
1204static void
1205dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1206{
4d3c2250 1207 complaint (&symfile_complaints,
e2e0b3e5 1208 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1209}
1210
25e43795
DJ
1211static void
1212dwarf2_debug_line_missing_file_complaint (void)
1213{
1214 complaint (&symfile_complaints,
1215 _(".debug_line section has line data without a file"));
1216}
1217
59205f5a
JB
1218static void
1219dwarf2_debug_line_missing_end_sequence_complaint (void)
1220{
1221 complaint (&symfile_complaints,
3e43a32a
MS
1222 _(".debug_line section has line "
1223 "program sequence without an end"));
59205f5a
JB
1224}
1225
4d3c2250
KB
1226static void
1227dwarf2_complex_location_expr_complaint (void)
2e276125 1228{
e2e0b3e5 1229 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1230}
1231
4d3c2250
KB
1232static void
1233dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1234 int arg3)
2e276125 1235{
4d3c2250 1236 complaint (&symfile_complaints,
3e43a32a
MS
1237 _("const value length mismatch for '%s', got %d, expected %d"),
1238 arg1, arg2, arg3);
4d3c2250
KB
1239}
1240
1241static void
f664829e 1242dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1243{
4d3c2250 1244 complaint (&symfile_complaints,
f664829e
DE
1245 _("debug info runs off end of %s section"
1246 " [in module %s]"),
1247 section->asection->name,
1248 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1249}
1250
1251static void
1252dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1253{
4d3c2250 1254 complaint (&symfile_complaints,
3e43a32a
MS
1255 _("macro debug info contains a "
1256 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1257 arg1);
1258}
1259
1260static void
1261dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1262{
4d3c2250 1263 complaint (&symfile_complaints,
3e43a32a
MS
1264 _("invalid attribute class or form for '%s' in '%s'"),
1265 arg1, arg2);
4d3c2250 1266}
c906108c 1267
c906108c
SS
1268/* local function prototypes */
1269
4efb68b1 1270static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1271
aaa75496
JB
1272static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1273 struct objfile *);
1274
918dd910
JK
1275static void dwarf2_find_base_address (struct die_info *die,
1276 struct dwarf2_cu *cu);
1277
0018ea6f
DE
1278static struct partial_symtab *create_partial_symtab
1279 (struct dwarf2_per_cu_data *per_cu, const char *name);
1280
c67a9c90 1281static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1282
72bf9492
DJ
1283static void scan_partial_symbols (struct partial_die_info *,
1284 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1285 int, struct dwarf2_cu *);
c906108c 1286
72bf9492
DJ
1287static void add_partial_symbol (struct partial_die_info *,
1288 struct dwarf2_cu *);
63d06c5c 1289
72bf9492
DJ
1290static void add_partial_namespace (struct partial_die_info *pdi,
1291 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1292 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1293
5d7cb8df
JK
1294static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1295 CORE_ADDR *highpc, int need_pc,
1296 struct dwarf2_cu *cu);
1297
72bf9492
DJ
1298static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1299 struct dwarf2_cu *cu);
91c24f0a 1300
bc30ff58
JB
1301static void add_partial_subprogram (struct partial_die_info *pdi,
1302 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1303 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1304
257e7a09
YQ
1305static void dwarf2_read_symtab (struct partial_symtab *,
1306 struct objfile *);
c906108c 1307
a14ed312 1308static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1309
433df2d4
DE
1310static struct abbrev_info *abbrev_table_lookup_abbrev
1311 (const struct abbrev_table *, unsigned int);
1312
1313static struct abbrev_table *abbrev_table_read_table
1314 (struct dwarf2_section_info *, sect_offset);
1315
1316static void abbrev_table_free (struct abbrev_table *);
1317
f4dc4d17
DE
1318static void abbrev_table_free_cleanup (void *);
1319
dee91e82
DE
1320static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1321 struct dwarf2_section_info *);
c906108c 1322
f3dd6933 1323static void dwarf2_free_abbrev_table (void *);
c906108c 1324
6caca83c
CC
1325static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1326
dee91e82
DE
1327static struct partial_die_info *load_partial_dies
1328 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1329
dee91e82
DE
1330static gdb_byte *read_partial_die (const struct die_reader_specs *,
1331 struct partial_die_info *,
1332 struct abbrev_info *,
1333 unsigned int,
1334 gdb_byte *);
c906108c 1335
36586728 1336static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1337 struct dwarf2_cu *);
72bf9492
DJ
1338
1339static void fixup_partial_die (struct partial_die_info *,
1340 struct dwarf2_cu *);
1341
dee91e82
DE
1342static gdb_byte *read_attribute (const struct die_reader_specs *,
1343 struct attribute *, struct attr_abbrev *,
1344 gdb_byte *);
a8329558 1345
a1855c1d 1346static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1347
a1855c1d 1348static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1349
a1855c1d 1350static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1351
a1855c1d 1352static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1353
a1855c1d 1354static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1355
fe1b8b76 1356static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1357 unsigned int *);
c906108c 1358
c764a876
DE
1359static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1360
1361static LONGEST read_checked_initial_length_and_offset
1362 (bfd *, gdb_byte *, const struct comp_unit_head *,
1363 unsigned int *, unsigned int *);
613e1657 1364
fe1b8b76 1365static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1366 unsigned int *);
1367
1368static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1369
f4dc4d17
DE
1370static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1371 sect_offset);
1372
fe1b8b76 1373static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1374
9b1c24c8 1375static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1376
fe1b8b76
JB
1377static char *read_indirect_string (bfd *, gdb_byte *,
1378 const struct comp_unit_head *,
1379 unsigned int *);
4bdf3d34 1380
36586728
TT
1381static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1382
12df843f 1383static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1384
12df843f 1385static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1386
3019eac3
DE
1387static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1388 unsigned int *);
1389
1390static char *read_str_index (const struct die_reader_specs *reader,
1391 struct dwarf2_cu *cu, ULONGEST str_index);
1392
e142c38c 1393static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1394
e142c38c
DJ
1395static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1396 struct dwarf2_cu *);
c906108c 1397
348e048f 1398static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1399 unsigned int);
348e048f 1400
05cf31d1
JB
1401static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1402 struct dwarf2_cu *cu);
1403
e142c38c 1404static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1405
e142c38c 1406static struct die_info *die_specification (struct die_info *die,
f2f0e013 1407 struct dwarf2_cu **);
63d06c5c 1408
debd256d
JB
1409static void free_line_header (struct line_header *lh);
1410
aaa75496
JB
1411static void add_file_name (struct line_header *, char *, unsigned int,
1412 unsigned int, unsigned int);
1413
3019eac3
DE
1414static struct line_header *dwarf_decode_line_header (unsigned int offset,
1415 struct dwarf2_cu *cu);
debd256d 1416
f3f5162e
DE
1417static void dwarf_decode_lines (struct line_header *, const char *,
1418 struct dwarf2_cu *, struct partial_symtab *,
1419 int);
c906108c 1420
72b9f47f 1421static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1422
f4dc4d17 1423static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1424 const char *, const char *, CORE_ADDR);
f4dc4d17 1425
a14ed312 1426static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1427 struct dwarf2_cu *);
c906108c 1428
34eaf542
TT
1429static struct symbol *new_symbol_full (struct die_info *, struct type *,
1430 struct dwarf2_cu *, struct symbol *);
1431
a14ed312 1432static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1433 struct dwarf2_cu *);
c906108c 1434
98bfdba5
PA
1435static void dwarf2_const_value_attr (struct attribute *attr,
1436 struct type *type,
1437 const char *name,
1438 struct obstack *obstack,
12df843f 1439 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1440 gdb_byte **bytes,
1441 struct dwarf2_locexpr_baton **baton);
2df3850c 1442
e7c27a73 1443static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1444
b4ba55a1
JB
1445static int need_gnat_info (struct dwarf2_cu *);
1446
3e43a32a
MS
1447static struct type *die_descriptive_type (struct die_info *,
1448 struct dwarf2_cu *);
b4ba55a1
JB
1449
1450static void set_descriptive_type (struct type *, struct die_info *,
1451 struct dwarf2_cu *);
1452
e7c27a73
DJ
1453static struct type *die_containing_type (struct die_info *,
1454 struct dwarf2_cu *);
c906108c 1455
673bfd45
DE
1456static struct type *lookup_die_type (struct die_info *, struct attribute *,
1457 struct dwarf2_cu *);
c906108c 1458
f792889a 1459static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1460
673bfd45
DE
1461static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1462
0d5cff50 1463static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1464
6e70227d 1465static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1466 const char *suffix, int physname,
1467 struct dwarf2_cu *cu);
63d06c5c 1468
e7c27a73 1469static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1470
348e048f
DE
1471static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1472
e7c27a73 1473static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1474
e7c27a73 1475static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1476
96408a79
SA
1477static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1478
ff013f42
JK
1479static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1480 struct dwarf2_cu *, struct partial_symtab *);
1481
a14ed312 1482static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1483 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1484 struct partial_symtab *);
c906108c 1485
fae299cd
DC
1486static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
801e3a5b
JB
1490static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
a14ed312 1493static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1494 struct dwarf2_cu *);
c906108c 1495
a14ed312 1496static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1497 struct type *, struct dwarf2_cu *);
c906108c 1498
a14ed312 1499static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1500 struct die_info *, struct type *,
e7c27a73 1501 struct dwarf2_cu *);
c906108c 1502
a14ed312 1503static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1504 struct type *,
1505 struct dwarf2_cu *);
c906108c 1506
134d01f1 1507static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1508
e7c27a73 1509static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1510
e7c27a73 1511static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1512
5d7cb8df
JK
1513static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
27aa8d6a
SW
1515static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1516
f55ee35c
JK
1517static struct type *read_module_type (struct die_info *die,
1518 struct dwarf2_cu *cu);
1519
38d518c9 1520static const char *namespace_name (struct die_info *die,
e142c38c 1521 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1522
134d01f1 1523static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1524
e7c27a73 1525static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1526
6e70227d 1527static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1528 struct dwarf2_cu *);
1529
dee91e82 1530static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1531 gdb_byte *info_ptr,
fe1b8b76 1532 gdb_byte **new_info_ptr,
639d11d3
DC
1533 struct die_info *parent);
1534
dee91e82 1535static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1536 gdb_byte *info_ptr,
fe1b8b76 1537 gdb_byte **new_info_ptr,
639d11d3
DC
1538 struct die_info *parent);
1539
3019eac3
DE
1540static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1541 struct die_info **, gdb_byte *, int *, int);
1542
dee91e82
DE
1543static gdb_byte *read_full_die (const struct die_reader_specs *,
1544 struct die_info **, gdb_byte *, int *);
93311388 1545
e7c27a73 1546static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1547
15d034d0
TT
1548static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1549 struct obstack *);
71c25dea 1550
15d034d0 1551static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1552
15d034d0 1553static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1554 struct die_info *die,
1555 struct dwarf2_cu *cu);
1556
ca69b9e6
DE
1557static const char *dwarf2_physname (const char *name, struct die_info *die,
1558 struct dwarf2_cu *cu);
1559
e142c38c 1560static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1561 struct dwarf2_cu **);
9219021c 1562
f39c6ffd 1563static const char *dwarf_tag_name (unsigned int);
c906108c 1564
f39c6ffd 1565static const char *dwarf_attr_name (unsigned int);
c906108c 1566
f39c6ffd 1567static const char *dwarf_form_name (unsigned int);
c906108c 1568
a14ed312 1569static char *dwarf_bool_name (unsigned int);
c906108c 1570
f39c6ffd 1571static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1572
f9aca02d 1573static struct die_info *sibling_die (struct die_info *);
c906108c 1574
d97bc12b
DE
1575static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1576
1577static void dump_die_for_error (struct die_info *);
1578
1579static void dump_die_1 (struct ui_file *, int level, int max_level,
1580 struct die_info *);
c906108c 1581
d97bc12b 1582/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1583
51545339 1584static void store_in_ref_table (struct die_info *,
10b3939b 1585 struct dwarf2_cu *);
c906108c 1586
93311388
DE
1587static int is_ref_attr (struct attribute *);
1588
b64f50a1 1589static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1590
43bbcdc2 1591static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1592
348e048f
DE
1593static struct die_info *follow_die_ref_or_sig (struct die_info *,
1594 struct attribute *,
1595 struct dwarf2_cu **);
1596
10b3939b
DJ
1597static struct die_info *follow_die_ref (struct die_info *,
1598 struct attribute *,
f2f0e013 1599 struct dwarf2_cu **);
c906108c 1600
348e048f
DE
1601static struct die_info *follow_die_sig (struct die_info *,
1602 struct attribute *,
1603 struct dwarf2_cu **);
1604
e5fe5e75 1605static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1606
52dc124a 1607static void read_signatured_type (struct signatured_type *);
348e048f 1608
f4dc4d17 1609static struct type_unit_group *get_type_unit_group
094b34ac 1610 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1611
1612static void build_type_unit_groups (die_reader_func_ftype *, void *);
1613
c906108c
SS
1614/* memory allocation interface */
1615
7b5a2f43 1616static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1617
b60c80d6 1618static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1619
09262596 1620static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1621 const char *, int);
2e276125 1622
8e19ed76
PS
1623static int attr_form_is_block (struct attribute *);
1624
3690dd37
JB
1625static int attr_form_is_section_offset (struct attribute *);
1626
1627static int attr_form_is_constant (struct attribute *);
1628
8cf6f0b1
TT
1629static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1630 struct dwarf2_loclist_baton *baton,
1631 struct attribute *attr);
1632
93e7bd98
DJ
1633static void dwarf2_symbol_mark_computed (struct attribute *attr,
1634 struct symbol *sym,
f1e6e072
TT
1635 struct dwarf2_cu *cu,
1636 int is_block);
4c2df51b 1637
dee91e82
DE
1638static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1639 gdb_byte *info_ptr,
1640 struct abbrev_info *abbrev);
4bb7a0a7 1641
72bf9492
DJ
1642static void free_stack_comp_unit (void *);
1643
72bf9492
DJ
1644static hashval_t partial_die_hash (const void *item);
1645
1646static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1647
ae038cb0 1648static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1649 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1650
9816fde3 1651static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1652 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1653
1654static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1655 struct die_info *comp_unit_die,
1656 enum language pretend_language);
93311388 1657
68dc6402 1658static void free_heap_comp_unit (void *);
ae038cb0
DJ
1659
1660static void free_cached_comp_units (void *);
1661
1662static void age_cached_comp_units (void);
1663
dee91e82 1664static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1665
f792889a
DJ
1666static struct type *set_die_type (struct die_info *, struct type *,
1667 struct dwarf2_cu *);
1c379e20 1668
ae038cb0
DJ
1669static void create_all_comp_units (struct objfile *);
1670
0e50663e 1671static int create_all_type_units (struct objfile *);
1fd400ff 1672
95554aad
TT
1673static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1674 enum language);
10b3939b 1675
95554aad
TT
1676static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1677 enum language);
10b3939b 1678
f4dc4d17
DE
1679static void process_full_type_unit (struct dwarf2_per_cu_data *,
1680 enum language);
1681
10b3939b
DJ
1682static void dwarf2_add_dependence (struct dwarf2_cu *,
1683 struct dwarf2_per_cu_data *);
1684
ae038cb0
DJ
1685static void dwarf2_mark (struct dwarf2_cu *);
1686
1687static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1688
b64f50a1 1689static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1690 struct dwarf2_per_cu_data *per_cu);
1691
f792889a 1692static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1693
9291a0cd
TT
1694static void dwarf2_release_queue (void *dummy);
1695
95554aad
TT
1696static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1697 enum language pretend_language);
1698
1699static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1700 struct dwarf2_per_cu_data *per_cu,
1701 enum language pretend_language);
9291a0cd 1702
a0f42c21 1703static void process_queue (void);
9291a0cd
TT
1704
1705static void find_file_and_directory (struct die_info *die,
1706 struct dwarf2_cu *cu,
15d034d0 1707 const char **name, const char **comp_dir);
9291a0cd
TT
1708
1709static char *file_full_name (int file, struct line_header *lh,
1710 const char *comp_dir);
1711
36586728
TT
1712static gdb_byte *read_and_check_comp_unit_head
1713 (struct comp_unit_head *header,
1714 struct dwarf2_section_info *section,
1715 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1716 int is_debug_types_section);
1717
fd820528 1718static void init_cutu_and_read_dies
f4dc4d17
DE
1719 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1720 int use_existing_cu, int keep,
3019eac3
DE
1721 die_reader_func_ftype *die_reader_func, void *data);
1722
dee91e82
DE
1723static void init_cutu_and_read_dies_simple
1724 (struct dwarf2_per_cu_data *this_cu,
1725 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1726
673bfd45 1727static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1728
3019eac3
DE
1729static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1730
1731static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1732 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1733
1734static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1735 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1736
1737static void free_dwo_file_cleanup (void *);
1738
95554aad
TT
1739static void process_cu_includes (void);
1740
1b80a9fa
JK
1741static void check_producer (struct dwarf2_cu *cu);
1742
9291a0cd
TT
1743#if WORDS_BIGENDIAN
1744
1745/* Convert VALUE between big- and little-endian. */
1746static offset_type
1747byte_swap (offset_type value)
1748{
1749 offset_type result;
1750
1751 result = (value & 0xff) << 24;
1752 result |= (value & 0xff00) << 8;
1753 result |= (value & 0xff0000) >> 8;
1754 result |= (value & 0xff000000) >> 24;
1755 return result;
1756}
1757
1758#define MAYBE_SWAP(V) byte_swap (V)
1759
1760#else
1761#define MAYBE_SWAP(V) (V)
1762#endif /* WORDS_BIGENDIAN */
1763
1764/* The suffix for an index file. */
1765#define INDEX_SUFFIX ".gdb-index"
1766
c906108c 1767/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1768 information and return true if we have enough to do something.
1769 NAMES points to the dwarf2 section names, or is NULL if the standard
1770 ELF names are used. */
c906108c
SS
1771
1772int
251d32d9
TG
1773dwarf2_has_info (struct objfile *objfile,
1774 const struct dwarf2_debug_sections *names)
c906108c 1775{
be391dca
TT
1776 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1777 if (!dwarf2_per_objfile)
1778 {
1779 /* Initialize per-objfile state. */
1780 struct dwarf2_per_objfile *data
1781 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1782
be391dca
TT
1783 memset (data, 0, sizeof (*data));
1784 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1785 dwarf2_per_objfile = data;
6502dd73 1786
251d32d9
TG
1787 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1788 (void *) names);
be391dca
TT
1789 dwarf2_per_objfile->objfile = objfile;
1790 }
1791 return (dwarf2_per_objfile->info.asection != NULL
1792 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1793}
1794
251d32d9
TG
1795/* When loading sections, we look either for uncompressed section or for
1796 compressed section names. */
233a11ab
CS
1797
1798static int
251d32d9
TG
1799section_is_p (const char *section_name,
1800 const struct dwarf2_section_names *names)
233a11ab 1801{
251d32d9
TG
1802 if (names->normal != NULL
1803 && strcmp (section_name, names->normal) == 0)
1804 return 1;
1805 if (names->compressed != NULL
1806 && strcmp (section_name, names->compressed) == 0)
1807 return 1;
1808 return 0;
233a11ab
CS
1809}
1810
c906108c
SS
1811/* This function is mapped across the sections and remembers the
1812 offset and size of each of the debugging sections we are interested
1813 in. */
1814
1815static void
251d32d9 1816dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1817{
251d32d9 1818 const struct dwarf2_debug_sections *names;
dc7650b8 1819 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1820
1821 if (vnames == NULL)
1822 names = &dwarf2_elf_names;
1823 else
1824 names = (const struct dwarf2_debug_sections *) vnames;
1825
dc7650b8
JK
1826 if ((aflag & SEC_HAS_CONTENTS) == 0)
1827 {
1828 }
1829 else if (section_is_p (sectp->name, &names->info))
c906108c 1830 {
dce234bc
PP
1831 dwarf2_per_objfile->info.asection = sectp;
1832 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1833 }
251d32d9 1834 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1835 {
dce234bc
PP
1836 dwarf2_per_objfile->abbrev.asection = sectp;
1837 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1838 }
251d32d9 1839 else if (section_is_p (sectp->name, &names->line))
c906108c 1840 {
dce234bc
PP
1841 dwarf2_per_objfile->line.asection = sectp;
1842 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1843 }
251d32d9 1844 else if (section_is_p (sectp->name, &names->loc))
c906108c 1845 {
dce234bc
PP
1846 dwarf2_per_objfile->loc.asection = sectp;
1847 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1848 }
251d32d9 1849 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1850 {
dce234bc
PP
1851 dwarf2_per_objfile->macinfo.asection = sectp;
1852 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1853 }
cf2c3c16
TT
1854 else if (section_is_p (sectp->name, &names->macro))
1855 {
1856 dwarf2_per_objfile->macro.asection = sectp;
1857 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1858 }
251d32d9 1859 else if (section_is_p (sectp->name, &names->str))
c906108c 1860 {
dce234bc
PP
1861 dwarf2_per_objfile->str.asection = sectp;
1862 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1863 }
3019eac3
DE
1864 else if (section_is_p (sectp->name, &names->addr))
1865 {
1866 dwarf2_per_objfile->addr.asection = sectp;
1867 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1868 }
251d32d9 1869 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1870 {
dce234bc
PP
1871 dwarf2_per_objfile->frame.asection = sectp;
1872 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1873 }
251d32d9 1874 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1875 {
dc7650b8
JK
1876 dwarf2_per_objfile->eh_frame.asection = sectp;
1877 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1878 }
251d32d9 1879 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1880 {
dce234bc
PP
1881 dwarf2_per_objfile->ranges.asection = sectp;
1882 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1883 }
251d32d9 1884 else if (section_is_p (sectp->name, &names->types))
348e048f 1885 {
8b70b953
TT
1886 struct dwarf2_section_info type_section;
1887
1888 memset (&type_section, 0, sizeof (type_section));
1889 type_section.asection = sectp;
1890 type_section.size = bfd_get_section_size (sectp);
1891
1892 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1893 &type_section);
348e048f 1894 }
251d32d9 1895 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1896 {
1897 dwarf2_per_objfile->gdb_index.asection = sectp;
1898 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1899 }
dce234bc 1900
72dca2f5
FR
1901 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1902 && bfd_section_vma (abfd, sectp) == 0)
1903 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1904}
1905
fceca515
DE
1906/* A helper function that decides whether a section is empty,
1907 or not present. */
9e0ac564
TT
1908
1909static int
1910dwarf2_section_empty_p (struct dwarf2_section_info *info)
1911{
1912 return info->asection == NULL || info->size == 0;
1913}
1914
3019eac3
DE
1915/* Read the contents of the section INFO.
1916 OBJFILE is the main object file, but not necessarily the file where
1917 the section comes from. E.g., for DWO files INFO->asection->owner
1918 is the bfd of the DWO file.
dce234bc 1919 If the section is compressed, uncompress it before returning. */
c906108c 1920
dce234bc
PP
1921static void
1922dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1923{
dce234bc 1924 asection *sectp = info->asection;
3019eac3 1925 bfd *abfd;
dce234bc
PP
1926 gdb_byte *buf, *retbuf;
1927 unsigned char header[4];
c906108c 1928
be391dca
TT
1929 if (info->readin)
1930 return;
dce234bc 1931 info->buffer = NULL;
be391dca 1932 info->readin = 1;
188dd5d6 1933
9e0ac564 1934 if (dwarf2_section_empty_p (info))
dce234bc 1935 return;
c906108c 1936
3019eac3
DE
1937 abfd = sectp->owner;
1938
4bf44c1c
TT
1939 /* If the section has relocations, we must read it ourselves.
1940 Otherwise we attach it to the BFD. */
1941 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1942 {
4bf44c1c 1943 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1944
4bf44c1c
TT
1945 /* We have to cast away const here for historical reasons.
1946 Fixing dwarf2read to be const-correct would be quite nice. */
1947 info->buffer = (gdb_byte *) bytes;
1948 return;
dce234bc 1949 }
dce234bc 1950
4bf44c1c
TT
1951 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1952 info->buffer = buf;
dce234bc
PP
1953
1954 /* When debugging .o files, we may need to apply relocations; see
1955 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1956 We never compress sections in .o files, so we only need to
1957 try this when the section is not compressed. */
ac8035ab 1958 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1959 if (retbuf != NULL)
1960 {
1961 info->buffer = retbuf;
1962 return;
1963 }
1964
1965 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1966 || bfd_bread (buf, info->size, abfd) != info->size)
1967 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1968 bfd_get_filename (abfd));
1969}
1970
9e0ac564
TT
1971/* A helper function that returns the size of a section in a safe way.
1972 If you are positive that the section has been read before using the
1973 size, then it is safe to refer to the dwarf2_section_info object's
1974 "size" field directly. In other cases, you must call this
1975 function, because for compressed sections the size field is not set
1976 correctly until the section has been read. */
1977
1978static bfd_size_type
1979dwarf2_section_size (struct objfile *objfile,
1980 struct dwarf2_section_info *info)
1981{
1982 if (!info->readin)
1983 dwarf2_read_section (objfile, info);
1984 return info->size;
1985}
1986
dce234bc 1987/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1988 SECTION_NAME. */
af34e669 1989
dce234bc 1990void
3017a003
TG
1991dwarf2_get_section_info (struct objfile *objfile,
1992 enum dwarf2_section_enum sect,
dce234bc
PP
1993 asection **sectp, gdb_byte **bufp,
1994 bfd_size_type *sizep)
1995{
1996 struct dwarf2_per_objfile *data
1997 = objfile_data (objfile, dwarf2_objfile_data_key);
1998 struct dwarf2_section_info *info;
a3b2a86b
TT
1999
2000 /* We may see an objfile without any DWARF, in which case we just
2001 return nothing. */
2002 if (data == NULL)
2003 {
2004 *sectp = NULL;
2005 *bufp = NULL;
2006 *sizep = 0;
2007 return;
2008 }
3017a003
TG
2009 switch (sect)
2010 {
2011 case DWARF2_DEBUG_FRAME:
2012 info = &data->frame;
2013 break;
2014 case DWARF2_EH_FRAME:
2015 info = &data->eh_frame;
2016 break;
2017 default:
2018 gdb_assert_not_reached ("unexpected section");
2019 }
dce234bc 2020
9e0ac564 2021 dwarf2_read_section (objfile, info);
dce234bc
PP
2022
2023 *sectp = info->asection;
2024 *bufp = info->buffer;
2025 *sizep = info->size;
2026}
2027
36586728
TT
2028/* A helper function to find the sections for a .dwz file. */
2029
2030static void
2031locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2032{
2033 struct dwz_file *dwz_file = arg;
2034
2035 /* Note that we only support the standard ELF names, because .dwz
2036 is ELF-only (at the time of writing). */
2037 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2038 {
2039 dwz_file->abbrev.asection = sectp;
2040 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2041 }
2042 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2043 {
2044 dwz_file->info.asection = sectp;
2045 dwz_file->info.size = bfd_get_section_size (sectp);
2046 }
2047 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2048 {
2049 dwz_file->str.asection = sectp;
2050 dwz_file->str.size = bfd_get_section_size (sectp);
2051 }
2052 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2053 {
2054 dwz_file->line.asection = sectp;
2055 dwz_file->line.size = bfd_get_section_size (sectp);
2056 }
2057 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2058 {
2059 dwz_file->macro.asection = sectp;
2060 dwz_file->macro.size = bfd_get_section_size (sectp);
2061 }
2ec9a5e0
TT
2062 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2063 {
2064 dwz_file->gdb_index.asection = sectp;
2065 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2066 }
36586728
TT
2067}
2068
2069/* Open the separate '.dwz' debug file, if needed. Error if the file
2070 cannot be found. */
2071
2072static struct dwz_file *
2073dwarf2_get_dwz_file (void)
2074{
2075 bfd *abfd, *dwz_bfd;
2076 asection *section;
2077 gdb_byte *data;
2078 struct cleanup *cleanup;
2079 const char *filename;
2080 struct dwz_file *result;
2081
2082 if (dwarf2_per_objfile->dwz_file != NULL)
2083 return dwarf2_per_objfile->dwz_file;
2084
2085 abfd = dwarf2_per_objfile->objfile->obfd;
2086 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2087 if (section == NULL)
2088 error (_("could not find '.gnu_debugaltlink' section"));
2089 if (!bfd_malloc_and_get_section (abfd, section, &data))
2090 error (_("could not read '.gnu_debugaltlink' section: %s"),
2091 bfd_errmsg (bfd_get_error ()));
2092 cleanup = make_cleanup (xfree, data);
2093
2094 filename = data;
2095 if (!IS_ABSOLUTE_PATH (filename))
2096 {
2097 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2098 char *rel;
2099
2100 make_cleanup (xfree, abs);
2101 abs = ldirname (abs);
2102 make_cleanup (xfree, abs);
2103
2104 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2105 make_cleanup (xfree, rel);
2106 filename = rel;
2107 }
2108
2109 /* The format is just a NUL-terminated file name, followed by the
2110 build-id. For now, though, we ignore the build-id. */
2111 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2112 if (dwz_bfd == NULL)
2113 error (_("could not read '%s': %s"), filename,
2114 bfd_errmsg (bfd_get_error ()));
2115
2116 if (!bfd_check_format (dwz_bfd, bfd_object))
2117 {
2118 gdb_bfd_unref (dwz_bfd);
2119 error (_("file '%s' was not usable: %s"), filename,
2120 bfd_errmsg (bfd_get_error ()));
2121 }
2122
2123 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2124 struct dwz_file);
2125 result->dwz_bfd = dwz_bfd;
2126
2127 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2128
2129 do_cleanups (cleanup);
2130
8d2cc612 2131 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2132 return result;
2133}
9291a0cd 2134\f
7b9f3c50
DE
2135/* DWARF quick_symbols_functions support. */
2136
2137/* TUs can share .debug_line entries, and there can be a lot more TUs than
2138 unique line tables, so we maintain a separate table of all .debug_line
2139 derived entries to support the sharing.
2140 All the quick functions need is the list of file names. We discard the
2141 line_header when we're done and don't need to record it here. */
2142struct quick_file_names
2143{
094b34ac
DE
2144 /* The data used to construct the hash key. */
2145 struct stmt_list_hash hash;
7b9f3c50
DE
2146
2147 /* The number of entries in file_names, real_names. */
2148 unsigned int num_file_names;
2149
2150 /* The file names from the line table, after being run through
2151 file_full_name. */
2152 const char **file_names;
2153
2154 /* The file names from the line table after being run through
2155 gdb_realpath. These are computed lazily. */
2156 const char **real_names;
2157};
2158
2159/* When using the index (and thus not using psymtabs), each CU has an
2160 object of this type. This is used to hold information needed by
2161 the various "quick" methods. */
2162struct dwarf2_per_cu_quick_data
2163{
2164 /* The file table. This can be NULL if there was no file table
2165 or it's currently not read in.
2166 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2167 struct quick_file_names *file_names;
2168
2169 /* The corresponding symbol table. This is NULL if symbols for this
2170 CU have not yet been read. */
2171 struct symtab *symtab;
2172
2173 /* A temporary mark bit used when iterating over all CUs in
2174 expand_symtabs_matching. */
2175 unsigned int mark : 1;
2176
2177 /* True if we've tried to read the file table and found there isn't one.
2178 There will be no point in trying to read it again next time. */
2179 unsigned int no_file_data : 1;
2180};
2181
094b34ac
DE
2182/* Utility hash function for a stmt_list_hash. */
2183
2184static hashval_t
2185hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2186{
2187 hashval_t v = 0;
2188
2189 if (stmt_list_hash->dwo_unit != NULL)
2190 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2191 v += stmt_list_hash->line_offset.sect_off;
2192 return v;
2193}
2194
2195/* Utility equality function for a stmt_list_hash. */
2196
2197static int
2198eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2199 const struct stmt_list_hash *rhs)
2200{
2201 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2202 return 0;
2203 if (lhs->dwo_unit != NULL
2204 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2205 return 0;
2206
2207 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2208}
2209
7b9f3c50
DE
2210/* Hash function for a quick_file_names. */
2211
2212static hashval_t
2213hash_file_name_entry (const void *e)
2214{
2215 const struct quick_file_names *file_data = e;
2216
094b34ac 2217 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2218}
2219
2220/* Equality function for a quick_file_names. */
2221
2222static int
2223eq_file_name_entry (const void *a, const void *b)
2224{
2225 const struct quick_file_names *ea = a;
2226 const struct quick_file_names *eb = b;
2227
094b34ac 2228 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2229}
2230
2231/* Delete function for a quick_file_names. */
2232
2233static void
2234delete_file_name_entry (void *e)
2235{
2236 struct quick_file_names *file_data = e;
2237 int i;
2238
2239 for (i = 0; i < file_data->num_file_names; ++i)
2240 {
2241 xfree ((void*) file_data->file_names[i]);
2242 if (file_data->real_names)
2243 xfree ((void*) file_data->real_names[i]);
2244 }
2245
2246 /* The space for the struct itself lives on objfile_obstack,
2247 so we don't free it here. */
2248}
2249
2250/* Create a quick_file_names hash table. */
2251
2252static htab_t
2253create_quick_file_names_table (unsigned int nr_initial_entries)
2254{
2255 return htab_create_alloc (nr_initial_entries,
2256 hash_file_name_entry, eq_file_name_entry,
2257 delete_file_name_entry, xcalloc, xfree);
2258}
9291a0cd 2259
918dd910
JK
2260/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2261 have to be created afterwards. You should call age_cached_comp_units after
2262 processing PER_CU->CU. dw2_setup must have been already called. */
2263
2264static void
2265load_cu (struct dwarf2_per_cu_data *per_cu)
2266{
3019eac3 2267 if (per_cu->is_debug_types)
e5fe5e75 2268 load_full_type_unit (per_cu);
918dd910 2269 else
95554aad 2270 load_full_comp_unit (per_cu, language_minimal);
918dd910 2271
918dd910 2272 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2273
2274 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2275}
2276
a0f42c21 2277/* Read in the symbols for PER_CU. */
2fdf6df6 2278
9291a0cd 2279static void
a0f42c21 2280dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2281{
2282 struct cleanup *back_to;
2283
f4dc4d17
DE
2284 /* Skip type_unit_groups, reading the type units they contain
2285 is handled elsewhere. */
2286 if (IS_TYPE_UNIT_GROUP (per_cu))
2287 return;
2288
9291a0cd
TT
2289 back_to = make_cleanup (dwarf2_release_queue, NULL);
2290
95554aad
TT
2291 if (dwarf2_per_objfile->using_index
2292 ? per_cu->v.quick->symtab == NULL
2293 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2294 {
2295 queue_comp_unit (per_cu, language_minimal);
2296 load_cu (per_cu);
2297 }
9291a0cd 2298
a0f42c21 2299 process_queue ();
9291a0cd
TT
2300
2301 /* Age the cache, releasing compilation units that have not
2302 been used recently. */
2303 age_cached_comp_units ();
2304
2305 do_cleanups (back_to);
2306}
2307
2308/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2309 the objfile from which this CU came. Returns the resulting symbol
2310 table. */
2fdf6df6 2311
9291a0cd 2312static struct symtab *
a0f42c21 2313dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2314{
95554aad 2315 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2316 if (!per_cu->v.quick->symtab)
2317 {
2318 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2319 increment_reading_symtab ();
a0f42c21 2320 dw2_do_instantiate_symtab (per_cu);
95554aad 2321 process_cu_includes ();
9291a0cd
TT
2322 do_cleanups (back_to);
2323 }
2324 return per_cu->v.quick->symtab;
2325}
2326
f4dc4d17
DE
2327/* Return the CU given its index.
2328
2329 This is intended for loops like:
2330
2331 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2332 + dwarf2_per_objfile->n_type_units); ++i)
2333 {
2334 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2335
2336 ...;
2337 }
2338*/
2fdf6df6 2339
1fd400ff
TT
2340static struct dwarf2_per_cu_data *
2341dw2_get_cu (int index)
2342{
2343 if (index >= dwarf2_per_objfile->n_comp_units)
2344 {
f4dc4d17 2345 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2346 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2347 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2348 }
2349
2350 return dwarf2_per_objfile->all_comp_units[index];
2351}
2352
2353/* Return the primary CU given its index.
2354 The difference between this function and dw2_get_cu is in the handling
2355 of type units (TUs). Here we return the type_unit_group object.
2356
2357 This is intended for loops like:
2358
2359 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2360 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2361 {
2362 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2363
2364 ...;
2365 }
2366*/
2367
2368static struct dwarf2_per_cu_data *
2369dw2_get_primary_cu (int index)
2370{
2371 if (index >= dwarf2_per_objfile->n_comp_units)
2372 {
1fd400ff 2373 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2374 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2375 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2376 }
f4dc4d17 2377
1fd400ff
TT
2378 return dwarf2_per_objfile->all_comp_units[index];
2379}
2380
2ec9a5e0
TT
2381/* A helper for create_cus_from_index that handles a given list of
2382 CUs. */
2fdf6df6 2383
74a0d9f6 2384static void
2ec9a5e0
TT
2385create_cus_from_index_list (struct objfile *objfile,
2386 const gdb_byte *cu_list, offset_type n_elements,
2387 struct dwarf2_section_info *section,
2388 int is_dwz,
2389 int base_offset)
9291a0cd
TT
2390{
2391 offset_type i;
9291a0cd 2392
2ec9a5e0 2393 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2394 {
2395 struct dwarf2_per_cu_data *the_cu;
2396 ULONGEST offset, length;
2397
74a0d9f6
JK
2398 gdb_static_assert (sizeof (ULONGEST) >= 8);
2399 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2400 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2401 cu_list += 2 * 8;
2402
2403 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2404 struct dwarf2_per_cu_data);
b64f50a1 2405 the_cu->offset.sect_off = offset;
9291a0cd
TT
2406 the_cu->length = length;
2407 the_cu->objfile = objfile;
8a0459fd 2408 the_cu->section = section;
9291a0cd
TT
2409 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2410 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2411 the_cu->is_dwz = is_dwz;
2412 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2413 }
9291a0cd
TT
2414}
2415
2ec9a5e0 2416/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2417 the CU objects for this objfile. */
2ec9a5e0 2418
74a0d9f6 2419static void
2ec9a5e0
TT
2420create_cus_from_index (struct objfile *objfile,
2421 const gdb_byte *cu_list, offset_type cu_list_elements,
2422 const gdb_byte *dwz_list, offset_type dwz_elements)
2423{
2424 struct dwz_file *dwz;
2425
2426 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2427 dwarf2_per_objfile->all_comp_units
2428 = obstack_alloc (&objfile->objfile_obstack,
2429 dwarf2_per_objfile->n_comp_units
2430 * sizeof (struct dwarf2_per_cu_data *));
2431
74a0d9f6
JK
2432 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2433 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2434
2435 if (dwz_elements == 0)
74a0d9f6 2436 return;
2ec9a5e0
TT
2437
2438 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2439 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2440 cu_list_elements / 2);
2ec9a5e0
TT
2441}
2442
1fd400ff 2443/* Create the signatured type hash table from the index. */
673bfd45 2444
74a0d9f6 2445static void
673bfd45 2446create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2447 struct dwarf2_section_info *section,
673bfd45
DE
2448 const gdb_byte *bytes,
2449 offset_type elements)
1fd400ff
TT
2450{
2451 offset_type i;
673bfd45 2452 htab_t sig_types_hash;
1fd400ff 2453
d467dd73
DE
2454 dwarf2_per_objfile->n_type_units = elements / 3;
2455 dwarf2_per_objfile->all_type_units
1fd400ff 2456 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2457 dwarf2_per_objfile->n_type_units
b4dd5633 2458 * sizeof (struct signatured_type *));
1fd400ff 2459
673bfd45 2460 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2461
2462 for (i = 0; i < elements; i += 3)
2463 {
52dc124a
DE
2464 struct signatured_type *sig_type;
2465 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2466 void **slot;
2467
74a0d9f6
JK
2468 gdb_static_assert (sizeof (ULONGEST) >= 8);
2469 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2470 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2471 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2472 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2473 bytes += 3 * 8;
2474
52dc124a 2475 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2476 struct signatured_type);
52dc124a 2477 sig_type->signature = signature;
3019eac3
DE
2478 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2479 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2480 sig_type->per_cu.section = section;
52dc124a
DE
2481 sig_type->per_cu.offset.sect_off = offset;
2482 sig_type->per_cu.objfile = objfile;
2483 sig_type->per_cu.v.quick
1fd400ff
TT
2484 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2485 struct dwarf2_per_cu_quick_data);
2486
52dc124a
DE
2487 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2488 *slot = sig_type;
1fd400ff 2489
b4dd5633 2490 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2491 }
2492
673bfd45 2493 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2494}
2495
9291a0cd
TT
2496/* Read the address map data from the mapped index, and use it to
2497 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2498
9291a0cd
TT
2499static void
2500create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2501{
2502 const gdb_byte *iter, *end;
2503 struct obstack temp_obstack;
2504 struct addrmap *mutable_map;
2505 struct cleanup *cleanup;
2506 CORE_ADDR baseaddr;
2507
2508 obstack_init (&temp_obstack);
2509 cleanup = make_cleanup_obstack_free (&temp_obstack);
2510 mutable_map = addrmap_create_mutable (&temp_obstack);
2511
2512 iter = index->address_table;
2513 end = iter + index->address_table_size;
2514
2515 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2516
2517 while (iter < end)
2518 {
2519 ULONGEST hi, lo, cu_index;
2520 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2521 iter += 8;
2522 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2523 iter += 8;
2524 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2525 iter += 4;
f652bce2
DE
2526
2527 if (cu_index < dwarf2_per_objfile->n_comp_units)
2528 {
2529 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2530 dw2_get_cu (cu_index));
2531 }
2532 else
2533 {
2534 complaint (&symfile_complaints,
2535 _(".gdb_index address table has invalid CU number %u"),
2536 (unsigned) cu_index);
2537 }
9291a0cd
TT
2538 }
2539
2540 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2541 &objfile->objfile_obstack);
2542 do_cleanups (cleanup);
2543}
2544
59d7bcaf
JK
2545/* The hash function for strings in the mapped index. This is the same as
2546 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2547 implementation. This is necessary because the hash function is tied to the
2548 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2549 SYMBOL_HASH_NEXT.
2550
2551 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2552
9291a0cd 2553static hashval_t
559a7a62 2554mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2555{
2556 const unsigned char *str = (const unsigned char *) p;
2557 hashval_t r = 0;
2558 unsigned char c;
2559
2560 while ((c = *str++) != 0)
559a7a62
JK
2561 {
2562 if (index_version >= 5)
2563 c = tolower (c);
2564 r = r * 67 + c - 113;
2565 }
9291a0cd
TT
2566
2567 return r;
2568}
2569
2570/* Find a slot in the mapped index INDEX for the object named NAME.
2571 If NAME is found, set *VEC_OUT to point to the CU vector in the
2572 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2573
9291a0cd
TT
2574static int
2575find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2576 offset_type **vec_out)
2577{
0cf03b49
JK
2578 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2579 offset_type hash;
9291a0cd 2580 offset_type slot, step;
559a7a62 2581 int (*cmp) (const char *, const char *);
9291a0cd 2582
0cf03b49
JK
2583 if (current_language->la_language == language_cplus
2584 || current_language->la_language == language_java
2585 || current_language->la_language == language_fortran)
2586 {
2587 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2588 not contain any. */
2589 const char *paren = strchr (name, '(');
2590
2591 if (paren)
2592 {
2593 char *dup;
2594
2595 dup = xmalloc (paren - name + 1);
2596 memcpy (dup, name, paren - name);
2597 dup[paren - name] = 0;
2598
2599 make_cleanup (xfree, dup);
2600 name = dup;
2601 }
2602 }
2603
559a7a62 2604 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2605 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2606 simulate our NAME being searched is also lowercased. */
2607 hash = mapped_index_string_hash ((index->version == 4
2608 && case_sensitivity == case_sensitive_off
2609 ? 5 : index->version),
2610 name);
2611
3876f04e
DE
2612 slot = hash & (index->symbol_table_slots - 1);
2613 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2614 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2615
2616 for (;;)
2617 {
2618 /* Convert a slot number to an offset into the table. */
2619 offset_type i = 2 * slot;
2620 const char *str;
3876f04e 2621 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2622 {
2623 do_cleanups (back_to);
2624 return 0;
2625 }
9291a0cd 2626
3876f04e 2627 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2628 if (!cmp (name, str))
9291a0cd
TT
2629 {
2630 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2631 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2632 do_cleanups (back_to);
9291a0cd
TT
2633 return 1;
2634 }
2635
3876f04e 2636 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2637 }
2638}
2639
2ec9a5e0
TT
2640/* A helper function that reads the .gdb_index from SECTION and fills
2641 in MAP. FILENAME is the name of the file containing the section;
2642 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2643 ok to use deprecated sections.
2644
2645 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2646 out parameters that are filled in with information about the CU and
2647 TU lists in the section.
2648
2649 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2650
9291a0cd 2651static int
2ec9a5e0
TT
2652read_index_from_section (struct objfile *objfile,
2653 const char *filename,
2654 int deprecated_ok,
2655 struct dwarf2_section_info *section,
2656 struct mapped_index *map,
2657 const gdb_byte **cu_list,
2658 offset_type *cu_list_elements,
2659 const gdb_byte **types_list,
2660 offset_type *types_list_elements)
9291a0cd 2661{
9291a0cd 2662 char *addr;
2ec9a5e0 2663 offset_type version;
b3b272e1 2664 offset_type *metadata;
1fd400ff 2665 int i;
9291a0cd 2666
2ec9a5e0 2667 if (dwarf2_section_empty_p (section))
9291a0cd 2668 return 0;
82430852
JK
2669
2670 /* Older elfutils strip versions could keep the section in the main
2671 executable while splitting it for the separate debug info file. */
2ec9a5e0 2672 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2673 return 0;
2674
2ec9a5e0 2675 dwarf2_read_section (objfile, section);
9291a0cd 2676
2ec9a5e0 2677 addr = section->buffer;
9291a0cd 2678 /* Version check. */
1fd400ff 2679 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2680 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2681 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2682 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2683 indices. */
831adc1f 2684 if (version < 4)
481860b3
GB
2685 {
2686 static int warning_printed = 0;
2687 if (!warning_printed)
2688 {
2689 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2690 filename);
481860b3
GB
2691 warning_printed = 1;
2692 }
2693 return 0;
2694 }
2695 /* Index version 4 uses a different hash function than index version
2696 5 and later.
2697
2698 Versions earlier than 6 did not emit psymbols for inlined
2699 functions. Using these files will cause GDB not to be able to
2700 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2701 indices unless the user has done
2702 "set use-deprecated-index-sections on". */
2ec9a5e0 2703 if (version < 6 && !deprecated_ok)
481860b3
GB
2704 {
2705 static int warning_printed = 0;
2706 if (!warning_printed)
2707 {
e615022a
DE
2708 warning (_("\
2709Skipping deprecated .gdb_index section in %s.\n\
2710Do \"set use-deprecated-index-sections on\" before the file is read\n\
2711to use the section anyway."),
2ec9a5e0 2712 filename);
481860b3
GB
2713 warning_printed = 1;
2714 }
2715 return 0;
2716 }
796a7ff8
DE
2717 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2718 of the TU (for symbols coming from TUs). It's just a performance bug, and
2719 we can't distinguish gdb-generated indices from gold-generated ones, so
2720 nothing to do here. */
2721
481860b3 2722 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2723 longer backward compatible. */
796a7ff8 2724 if (version > 8)
594e8718 2725 return 0;
9291a0cd 2726
559a7a62 2727 map->version = version;
2ec9a5e0 2728 map->total_size = section->size;
9291a0cd
TT
2729
2730 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2731
2732 i = 0;
2ec9a5e0
TT
2733 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2734 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2735 / 8);
1fd400ff
TT
2736 ++i;
2737
2ec9a5e0
TT
2738 *types_list = addr + MAYBE_SWAP (metadata[i]);
2739 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2740 - MAYBE_SWAP (metadata[i]))
2741 / 8);
987d643c 2742 ++i;
1fd400ff
TT
2743
2744 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2745 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2746 - MAYBE_SWAP (metadata[i]));
2747 ++i;
2748
3876f04e
DE
2749 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2750 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2751 - MAYBE_SWAP (metadata[i]))
2752 / (2 * sizeof (offset_type)));
1fd400ff 2753 ++i;
9291a0cd 2754
1fd400ff
TT
2755 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2756
2ec9a5e0
TT
2757 return 1;
2758}
2759
2760
2761/* Read the index file. If everything went ok, initialize the "quick"
2762 elements of all the CUs and return 1. Otherwise, return 0. */
2763
2764static int
2765dwarf2_read_index (struct objfile *objfile)
2766{
2767 struct mapped_index local_map, *map;
2768 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2769 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2770
2771 if (!read_index_from_section (objfile, objfile->name,
2772 use_deprecated_index_sections,
2773 &dwarf2_per_objfile->gdb_index, &local_map,
2774 &cu_list, &cu_list_elements,
2775 &types_list, &types_list_elements))
2776 return 0;
2777
0fefef59 2778 /* Don't use the index if it's empty. */
2ec9a5e0 2779 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2780 return 0;
2781
2ec9a5e0
TT
2782 /* If there is a .dwz file, read it so we can get its CU list as
2783 well. */
2784 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2785 {
2786 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2787 struct mapped_index dwz_map;
2788 const gdb_byte *dwz_types_ignore;
2789 offset_type dwz_types_elements_ignore;
2790
2791 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2792 1,
2793 &dwz->gdb_index, &dwz_map,
2794 &dwz_list, &dwz_list_elements,
2795 &dwz_types_ignore,
2796 &dwz_types_elements_ignore))
2797 {
2798 warning (_("could not read '.gdb_index' section from %s; skipping"),
2799 bfd_get_filename (dwz->dwz_bfd));
2800 return 0;
2801 }
2802 }
2803
74a0d9f6
JK
2804 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2805 dwz_list_elements);
1fd400ff 2806
8b70b953
TT
2807 if (types_list_elements)
2808 {
2809 struct dwarf2_section_info *section;
2810
2811 /* We can only handle a single .debug_types when we have an
2812 index. */
2813 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2814 return 0;
2815
2816 section = VEC_index (dwarf2_section_info_def,
2817 dwarf2_per_objfile->types, 0);
2818
74a0d9f6
JK
2819 create_signatured_type_table_from_index (objfile, section, types_list,
2820 types_list_elements);
8b70b953 2821 }
9291a0cd 2822
2ec9a5e0
TT
2823 create_addrmap_from_index (objfile, &local_map);
2824
2825 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2826 *map = local_map;
9291a0cd
TT
2827
2828 dwarf2_per_objfile->index_table = map;
2829 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2830 dwarf2_per_objfile->quick_file_names_table =
2831 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2832
2833 return 1;
2834}
2835
2836/* A helper for the "quick" functions which sets the global
2837 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2838
9291a0cd
TT
2839static void
2840dw2_setup (struct objfile *objfile)
2841{
2842 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2843 gdb_assert (dwarf2_per_objfile);
2844}
2845
dee91e82 2846/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2847
dee91e82
DE
2848static void
2849dw2_get_file_names_reader (const struct die_reader_specs *reader,
2850 gdb_byte *info_ptr,
2851 struct die_info *comp_unit_die,
2852 int has_children,
2853 void *data)
9291a0cd 2854{
dee91e82
DE
2855 struct dwarf2_cu *cu = reader->cu;
2856 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2857 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2858 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2859 struct line_header *lh;
9291a0cd 2860 struct attribute *attr;
dee91e82 2861 int i;
15d034d0 2862 const char *name, *comp_dir;
7b9f3c50
DE
2863 void **slot;
2864 struct quick_file_names *qfn;
2865 unsigned int line_offset;
9291a0cd 2866
0186c6a7
DE
2867 gdb_assert (! this_cu->is_debug_types);
2868
07261596
TT
2869 /* Our callers never want to match partial units -- instead they
2870 will match the enclosing full CU. */
2871 if (comp_unit_die->tag == DW_TAG_partial_unit)
2872 {
2873 this_cu->v.quick->no_file_data = 1;
2874 return;
2875 }
2876
0186c6a7 2877 lh_cu = this_cu;
7b9f3c50
DE
2878 lh = NULL;
2879 slot = NULL;
2880 line_offset = 0;
dee91e82
DE
2881
2882 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2883 if (attr)
2884 {
7b9f3c50
DE
2885 struct quick_file_names find_entry;
2886
2887 line_offset = DW_UNSND (attr);
2888
2889 /* We may have already read in this line header (TU line header sharing).
2890 If we have we're done. */
094b34ac
DE
2891 find_entry.hash.dwo_unit = cu->dwo_unit;
2892 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2893 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2894 &find_entry, INSERT);
2895 if (*slot != NULL)
2896 {
094b34ac 2897 lh_cu->v.quick->file_names = *slot;
dee91e82 2898 return;
7b9f3c50
DE
2899 }
2900
3019eac3 2901 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2902 }
2903 if (lh == NULL)
2904 {
094b34ac 2905 lh_cu->v.quick->no_file_data = 1;
dee91e82 2906 return;
9291a0cd
TT
2907 }
2908
7b9f3c50 2909 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2910 qfn->hash.dwo_unit = cu->dwo_unit;
2911 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2912 gdb_assert (slot != NULL);
2913 *slot = qfn;
9291a0cd 2914
dee91e82 2915 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2916
7b9f3c50
DE
2917 qfn->num_file_names = lh->num_file_names;
2918 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2919 lh->num_file_names * sizeof (char *));
9291a0cd 2920 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2921 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2922 qfn->real_names = NULL;
9291a0cd 2923
7b9f3c50 2924 free_line_header (lh);
7b9f3c50 2925
094b34ac 2926 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2927}
2928
2929/* A helper for the "quick" functions which attempts to read the line
2930 table for THIS_CU. */
2931
2932static struct quick_file_names *
e4a48d9d 2933dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2934{
0186c6a7
DE
2935 /* This should never be called for TUs. */
2936 gdb_assert (! this_cu->is_debug_types);
2937 /* Nor type unit groups. */
2938 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2939
dee91e82
DE
2940 if (this_cu->v.quick->file_names != NULL)
2941 return this_cu->v.quick->file_names;
2942 /* If we know there is no line data, no point in looking again. */
2943 if (this_cu->v.quick->no_file_data)
2944 return NULL;
2945
0186c6a7 2946 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2947
2948 if (this_cu->v.quick->no_file_data)
2949 return NULL;
2950 return this_cu->v.quick->file_names;
9291a0cd
TT
2951}
2952
2953/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2954 real path for a given file name from the line table. */
2fdf6df6 2955
9291a0cd 2956static const char *
7b9f3c50
DE
2957dw2_get_real_path (struct objfile *objfile,
2958 struct quick_file_names *qfn, int index)
9291a0cd 2959{
7b9f3c50
DE
2960 if (qfn->real_names == NULL)
2961 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2962 qfn->num_file_names, sizeof (char *));
9291a0cd 2963
7b9f3c50
DE
2964 if (qfn->real_names[index] == NULL)
2965 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2966
7b9f3c50 2967 return qfn->real_names[index];
9291a0cd
TT
2968}
2969
2970static struct symtab *
2971dw2_find_last_source_symtab (struct objfile *objfile)
2972{
2973 int index;
ae2de4f8 2974
9291a0cd
TT
2975 dw2_setup (objfile);
2976 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2977 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2978}
2979
7b9f3c50
DE
2980/* Traversal function for dw2_forget_cached_source_info. */
2981
2982static int
2983dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2984{
7b9f3c50 2985 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2986
7b9f3c50 2987 if (file_data->real_names)
9291a0cd 2988 {
7b9f3c50 2989 int i;
9291a0cd 2990
7b9f3c50 2991 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2992 {
7b9f3c50
DE
2993 xfree ((void*) file_data->real_names[i]);
2994 file_data->real_names[i] = NULL;
9291a0cd
TT
2995 }
2996 }
7b9f3c50
DE
2997
2998 return 1;
2999}
3000
3001static void
3002dw2_forget_cached_source_info (struct objfile *objfile)
3003{
3004 dw2_setup (objfile);
3005
3006 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3007 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3008}
3009
f8eba3c6
TT
3010/* Helper function for dw2_map_symtabs_matching_filename that expands
3011 the symtabs and calls the iterator. */
3012
3013static int
3014dw2_map_expand_apply (struct objfile *objfile,
3015 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3016 const char *name, const char *real_path,
f8eba3c6
TT
3017 int (*callback) (struct symtab *, void *),
3018 void *data)
3019{
3020 struct symtab *last_made = objfile->symtabs;
3021
3022 /* Don't visit already-expanded CUs. */
3023 if (per_cu->v.quick->symtab)
3024 return 0;
3025
3026 /* This may expand more than one symtab, and we want to iterate over
3027 all of them. */
a0f42c21 3028 dw2_instantiate_symtab (per_cu);
f8eba3c6 3029
f5b95b50 3030 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3031 objfile->symtabs, last_made);
3032}
3033
3034/* Implementation of the map_symtabs_matching_filename method. */
3035
9291a0cd 3036static int
f8eba3c6 3037dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3038 const char *real_path,
f8eba3c6
TT
3039 int (*callback) (struct symtab *, void *),
3040 void *data)
9291a0cd
TT
3041{
3042 int i;
c011a4f4 3043 const char *name_basename = lbasename (name);
9291a0cd
TT
3044
3045 dw2_setup (objfile);
ae2de4f8 3046
848e3e78
DE
3047 /* The rule is CUs specify all the files, including those used by
3048 any TU, so there's no need to scan TUs here. */
f4dc4d17 3049
848e3e78 3050 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3051 {
3052 int j;
f4dc4d17 3053 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3054 struct quick_file_names *file_data;
9291a0cd 3055
3d7bb9d9 3056 /* We only need to look at symtabs not already expanded. */
e254ef6a 3057 if (per_cu->v.quick->symtab)
9291a0cd
TT
3058 continue;
3059
e4a48d9d 3060 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3061 if (file_data == NULL)
9291a0cd
TT
3062 continue;
3063
7b9f3c50 3064 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3065 {
7b9f3c50 3066 const char *this_name = file_data->file_names[j];
da235a7c 3067 const char *this_real_name;
9291a0cd 3068
af529f8f 3069 if (compare_filenames_for_search (this_name, name))
9291a0cd 3070 {
f5b95b50 3071 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3072 callback, data))
3073 return 1;
288e77a7 3074 continue;
4aac40c8 3075 }
9291a0cd 3076
c011a4f4
DE
3077 /* Before we invoke realpath, which can get expensive when many
3078 files are involved, do a quick comparison of the basenames. */
3079 if (! basenames_may_differ
3080 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3081 continue;
3082
da235a7c
JK
3083 this_real_name = dw2_get_real_path (objfile, file_data, j);
3084 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3085 {
da235a7c
JK
3086 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3087 callback, data))
3088 return 1;
288e77a7 3089 continue;
da235a7c 3090 }
9291a0cd 3091
da235a7c
JK
3092 if (real_path != NULL)
3093 {
af529f8f
JK
3094 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3095 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3096 if (this_real_name != NULL
af529f8f 3097 && FILENAME_CMP (real_path, this_real_name) == 0)
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;
9291a0cd
TT
3103 }
3104 }
3105 }
3106 }
3107
9291a0cd
TT
3108 return 0;
3109}
3110
da51c347
DE
3111/* Struct used to manage iterating over all CUs looking for a symbol. */
3112
3113struct dw2_symtab_iterator
9291a0cd 3114{
da51c347
DE
3115 /* The internalized form of .gdb_index. */
3116 struct mapped_index *index;
3117 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3118 int want_specific_block;
3119 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3120 Unused if !WANT_SPECIFIC_BLOCK. */
3121 int block_index;
3122 /* The kind of symbol we're looking for. */
3123 domain_enum domain;
3124 /* The list of CUs from the index entry of the symbol,
3125 or NULL if not found. */
3126 offset_type *vec;
3127 /* The next element in VEC to look at. */
3128 int next;
3129 /* The number of elements in VEC, or zero if there is no match. */
3130 int length;
3131};
9291a0cd 3132
da51c347
DE
3133/* Initialize the index symtab iterator ITER.
3134 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3135 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3136
9291a0cd 3137static void
da51c347
DE
3138dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3139 struct mapped_index *index,
3140 int want_specific_block,
3141 int block_index,
3142 domain_enum domain,
3143 const char *name)
3144{
3145 iter->index = index;
3146 iter->want_specific_block = want_specific_block;
3147 iter->block_index = block_index;
3148 iter->domain = domain;
3149 iter->next = 0;
3150
3151 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3152 iter->length = MAYBE_SWAP (*iter->vec);
3153 else
3154 {
3155 iter->vec = NULL;
3156 iter->length = 0;
3157 }
3158}
3159
3160/* Return the next matching CU or NULL if there are no more. */
3161
3162static struct dwarf2_per_cu_data *
3163dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3164{
3165 for ( ; iter->next < iter->length; ++iter->next)
3166 {
3167 offset_type cu_index_and_attrs =
3168 MAYBE_SWAP (iter->vec[iter->next + 1]);
3169 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3170 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3171 int want_static = iter->block_index != GLOBAL_BLOCK;
3172 /* This value is only valid for index versions >= 7. */
3173 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3174 gdb_index_symbol_kind symbol_kind =
3175 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3176 /* Only check the symbol attributes if they're present.
3177 Indices prior to version 7 don't record them,
3178 and indices >= 7 may elide them for certain symbols
3179 (gold does this). */
3180 int attrs_valid =
3181 (iter->index->version >= 7
3182 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3183
3184 /* Skip if already read in. */
3185 if (per_cu->v.quick->symtab)
3186 continue;
3187
3188 if (attrs_valid
3189 && iter->want_specific_block
3190 && want_static != is_static)
3191 continue;
3192
3193 /* Only check the symbol's kind if it has one. */
3194 if (attrs_valid)
3195 {
3196 switch (iter->domain)
3197 {
3198 case VAR_DOMAIN:
3199 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3200 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3201 /* Some types are also in VAR_DOMAIN. */
3202 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3203 continue;
3204 break;
3205 case STRUCT_DOMAIN:
3206 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3207 continue;
3208 break;
3209 case LABEL_DOMAIN:
3210 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3211 continue;
3212 break;
3213 default:
3214 break;
3215 }
3216 }
3217
3218 ++iter->next;
3219 return per_cu;
3220 }
3221
3222 return NULL;
3223}
3224
3225static struct symtab *
3226dw2_lookup_symbol (struct objfile *objfile, int block_index,
3227 const char *name, domain_enum domain)
9291a0cd 3228{
da51c347 3229 struct symtab *stab_best = NULL;
156942c7
DE
3230 struct mapped_index *index;
3231
9291a0cd
TT
3232 dw2_setup (objfile);
3233
156942c7
DE
3234 index = dwarf2_per_objfile->index_table;
3235
da51c347 3236 /* index is NULL if OBJF_READNOW. */
156942c7 3237 if (index)
9291a0cd 3238 {
da51c347
DE
3239 struct dw2_symtab_iterator iter;
3240 struct dwarf2_per_cu_data *per_cu;
3241
3242 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3243
da51c347 3244 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3245 {
da51c347
DE
3246 struct symbol *sym = NULL;
3247 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3248
3249 /* Some caution must be observed with overloaded functions
3250 and methods, since the index will not contain any overload
3251 information (but NAME might contain it). */
3252 if (stab->primary)
9291a0cd 3253 {
da51c347
DE
3254 struct blockvector *bv = BLOCKVECTOR (stab);
3255 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3256
da51c347
DE
3257 sym = lookup_block_symbol (block, name, domain);
3258 }
1fd400ff 3259
da51c347
DE
3260 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3261 {
3262 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3263 return stab;
3264
3265 stab_best = stab;
9291a0cd 3266 }
da51c347
DE
3267
3268 /* Keep looking through other CUs. */
9291a0cd
TT
3269 }
3270 }
9291a0cd 3271
da51c347 3272 return stab_best;
9291a0cd
TT
3273}
3274
3275static void
3276dw2_print_stats (struct objfile *objfile)
3277{
e4a48d9d 3278 int i, total, count;
9291a0cd
TT
3279
3280 dw2_setup (objfile);
e4a48d9d 3281 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3282 count = 0;
e4a48d9d 3283 for (i = 0; i < total; ++i)
9291a0cd 3284 {
e254ef6a 3285 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3286
e254ef6a 3287 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3288 ++count;
3289 }
e4a48d9d 3290 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3291 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3292}
3293
3294static void
3295dw2_dump (struct objfile *objfile)
3296{
3297 /* Nothing worth printing. */
3298}
3299
3300static void
3301dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3302 struct section_offsets *delta)
3303{
3304 /* There's nothing to relocate here. */
3305}
3306
3307static void
3308dw2_expand_symtabs_for_function (struct objfile *objfile,
3309 const char *func_name)
3310{
da51c347
DE
3311 struct mapped_index *index;
3312
3313 dw2_setup (objfile);
3314
3315 index = dwarf2_per_objfile->index_table;
3316
3317 /* index is NULL if OBJF_READNOW. */
3318 if (index)
3319 {
3320 struct dw2_symtab_iterator iter;
3321 struct dwarf2_per_cu_data *per_cu;
3322
3323 /* Note: It doesn't matter what we pass for block_index here. */
3324 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3325 func_name);
3326
3327 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3328 dw2_instantiate_symtab (per_cu);
3329 }
9291a0cd
TT
3330}
3331
3332static void
3333dw2_expand_all_symtabs (struct objfile *objfile)
3334{
3335 int i;
3336
3337 dw2_setup (objfile);
1fd400ff
TT
3338
3339 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3340 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3341 {
e254ef6a 3342 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3343
a0f42c21 3344 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3345 }
3346}
3347
3348static void
652a8996
JK
3349dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3350 const char *fullname)
9291a0cd
TT
3351{
3352 int i;
3353
3354 dw2_setup (objfile);
d4637a04
DE
3355
3356 /* We don't need to consider type units here.
3357 This is only called for examining code, e.g. expand_line_sal.
3358 There can be an order of magnitude (or more) more type units
3359 than comp units, and we avoid them if we can. */
3360
3361 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3362 {
3363 int j;
e254ef6a 3364 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3365 struct quick_file_names *file_data;
9291a0cd 3366
3d7bb9d9 3367 /* We only need to look at symtabs not already expanded. */
e254ef6a 3368 if (per_cu->v.quick->symtab)
9291a0cd
TT
3369 continue;
3370
e4a48d9d 3371 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3372 if (file_data == NULL)
9291a0cd
TT
3373 continue;
3374
7b9f3c50 3375 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3376 {
652a8996
JK
3377 const char *this_fullname = file_data->file_names[j];
3378
3379 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3380 {
a0f42c21 3381 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3382 break;
3383 }
3384 }
3385 }
3386}
3387
356d9f9d
TT
3388/* A helper function for dw2_find_symbol_file that finds the primary
3389 file name for a given CU. This is a die_reader_func. */
3390
3391static void
3392dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3393 gdb_byte *info_ptr,
3394 struct die_info *comp_unit_die,
3395 int has_children,
3396 void *data)
3397{
3398 const char **result_ptr = data;
3399 struct dwarf2_cu *cu = reader->cu;
3400 struct attribute *attr;
3401
3402 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3403 if (attr == NULL)
3404 *result_ptr = NULL;
3405 else
3406 *result_ptr = DW_STRING (attr);
3407}
3408
dd786858 3409static const char *
9291a0cd
TT
3410dw2_find_symbol_file (struct objfile *objfile, const char *name)
3411{
e254ef6a 3412 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3413 offset_type *vec;
356d9f9d 3414 const char *filename;
9291a0cd
TT
3415
3416 dw2_setup (objfile);
3417
ae2de4f8 3418 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3419 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3420 {
3421 struct symtab *s;
3422
d790cf0a
DE
3423 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3424 {
3425 struct blockvector *bv = BLOCKVECTOR (s);
3426 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3427 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3428
3429 if (sym)
652a8996
JK
3430 {
3431 /* Only file extension of returned filename is recognized. */
3432 return SYMBOL_SYMTAB (sym)->filename;
3433 }
d790cf0a 3434 }
96408a79
SA
3435 return NULL;
3436 }
9291a0cd
TT
3437
3438 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3439 name, &vec))
3440 return NULL;
3441
3442 /* Note that this just looks at the very first one named NAME -- but
3443 actually we are looking for a function. find_main_filename
3444 should be rewritten so that it doesn't require a custom hook. It
3445 could just use the ordinary symbol tables. */
3446 /* vec[0] is the length, which must always be >0. */
156942c7 3447 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3448
356d9f9d 3449 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3450 {
3451 /* Only file extension of returned filename is recognized. */
3452 return per_cu->v.quick->symtab->filename;
3453 }
356d9f9d 3454
f4dc4d17
DE
3455 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3456 dw2_get_primary_filename_reader, &filename);
9291a0cd 3457
652a8996 3458 /* Only file extension of returned filename is recognized. */
356d9f9d 3459 return filename;
9291a0cd
TT
3460}
3461
3462static void
40658b94
PH
3463dw2_map_matching_symbols (const char * name, domain_enum namespace,
3464 struct objfile *objfile, int global,
3465 int (*callback) (struct block *,
3466 struct symbol *, void *),
2edb89d3
JK
3467 void *data, symbol_compare_ftype *match,
3468 symbol_compare_ftype *ordered_compare)
9291a0cd 3469{
40658b94 3470 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3471 current language is Ada for a non-Ada objfile using GNU index. As Ada
3472 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3473}
3474
3475static void
f8eba3c6
TT
3476dw2_expand_symtabs_matching
3477 (struct objfile *objfile,
fbd9ab74 3478 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3479 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3480 enum search_domain kind,
3481 void *data)
9291a0cd
TT
3482{
3483 int i;
3484 offset_type iter;
4b5246aa 3485 struct mapped_index *index;
9291a0cd
TT
3486
3487 dw2_setup (objfile);
ae2de4f8
DE
3488
3489 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3490 if (!dwarf2_per_objfile->index_table)
3491 return;
4b5246aa 3492 index = dwarf2_per_objfile->index_table;
9291a0cd 3493
7b08b9eb 3494 if (file_matcher != NULL)
24c79950
TT
3495 {
3496 struct cleanup *cleanup;
3497 htab_t visited_found, visited_not_found;
3498
3499 visited_found = htab_create_alloc (10,
3500 htab_hash_pointer, htab_eq_pointer,
3501 NULL, xcalloc, xfree);
3502 cleanup = make_cleanup_htab_delete (visited_found);
3503 visited_not_found = htab_create_alloc (10,
3504 htab_hash_pointer, htab_eq_pointer,
3505 NULL, xcalloc, xfree);
3506 make_cleanup_htab_delete (visited_not_found);
3507
848e3e78
DE
3508 /* The rule is CUs specify all the files, including those used by
3509 any TU, so there's no need to scan TUs here. */
3510
3511 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3512 {
3513 int j;
f4dc4d17 3514 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3515 struct quick_file_names *file_data;
3516 void **slot;
7b08b9eb 3517
24c79950 3518 per_cu->v.quick->mark = 0;
3d7bb9d9 3519
24c79950
TT
3520 /* We only need to look at symtabs not already expanded. */
3521 if (per_cu->v.quick->symtab)
3522 continue;
7b08b9eb 3523
e4a48d9d 3524 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3525 if (file_data == NULL)
3526 continue;
7b08b9eb 3527
24c79950
TT
3528 if (htab_find (visited_not_found, file_data) != NULL)
3529 continue;
3530 else if (htab_find (visited_found, file_data) != NULL)
3531 {
3532 per_cu->v.quick->mark = 1;
3533 continue;
3534 }
3535
3536 for (j = 0; j < file_data->num_file_names; ++j)
3537 {
da235a7c
JK
3538 const char *this_real_name;
3539
fbd9ab74 3540 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3541 {
3542 per_cu->v.quick->mark = 1;
3543 break;
3544 }
da235a7c
JK
3545
3546 /* Before we invoke realpath, which can get expensive when many
3547 files are involved, do a quick comparison of the basenames. */
3548 if (!basenames_may_differ
3549 && !file_matcher (lbasename (file_data->file_names[j]),
3550 data, 1))
3551 continue;
3552
3553 this_real_name = dw2_get_real_path (objfile, file_data, j);
3554 if (file_matcher (this_real_name, data, 0))
3555 {
3556 per_cu->v.quick->mark = 1;
3557 break;
3558 }
24c79950
TT
3559 }
3560
3561 slot = htab_find_slot (per_cu->v.quick->mark
3562 ? visited_found
3563 : visited_not_found,
3564 file_data, INSERT);
3565 *slot = file_data;
3566 }
3567
3568 do_cleanups (cleanup);
3569 }
9291a0cd 3570
3876f04e 3571 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3572 {
3573 offset_type idx = 2 * iter;
3574 const char *name;
3575 offset_type *vec, vec_len, vec_idx;
3576
3876f04e 3577 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3578 continue;
3579
3876f04e 3580 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3581
e078317b 3582 if (! (*name_matcher) (name, data))
9291a0cd
TT
3583 continue;
3584
3585 /* The name was matched, now expand corresponding CUs that were
3586 marked. */
4b5246aa 3587 vec = (offset_type *) (index->constant_pool
3876f04e 3588 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3589 vec_len = MAYBE_SWAP (vec[0]);
3590 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3591 {
e254ef6a 3592 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3593 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3594 gdb_index_symbol_kind symbol_kind =
3595 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3596 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3597
3598 /* Don't crash on bad data. */
3599 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3600 + dwarf2_per_objfile->n_type_units))
156942c7 3601 continue;
1fd400ff 3602
156942c7
DE
3603 /* Only check the symbol's kind if it has one.
3604 Indices prior to version 7 don't record it. */
3605 if (index->version >= 7)
3606 {
3607 switch (kind)
3608 {
3609 case VARIABLES_DOMAIN:
3610 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3611 continue;
3612 break;
3613 case FUNCTIONS_DOMAIN:
3614 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3615 continue;
3616 break;
3617 case TYPES_DOMAIN:
3618 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3619 continue;
3620 break;
3621 default:
3622 break;
3623 }
3624 }
3625
3626 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3627 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3628 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3629 }
3630 }
3631}
3632
9703b513
TT
3633/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3634 symtab. */
3635
3636static struct symtab *
3637recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3638{
3639 int i;
3640
3641 if (BLOCKVECTOR (symtab) != NULL
3642 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3643 return symtab;
3644
a3ec0bb1
DE
3645 if (symtab->includes == NULL)
3646 return NULL;
3647
9703b513
TT
3648 for (i = 0; symtab->includes[i]; ++i)
3649 {
a3ec0bb1 3650 struct symtab *s = symtab->includes[i];
9703b513
TT
3651
3652 s = recursively_find_pc_sect_symtab (s, pc);
3653 if (s != NULL)
3654 return s;
3655 }
3656
3657 return NULL;
3658}
3659
9291a0cd
TT
3660static struct symtab *
3661dw2_find_pc_sect_symtab (struct objfile *objfile,
3662 struct minimal_symbol *msymbol,
3663 CORE_ADDR pc,
3664 struct obj_section *section,
3665 int warn_if_readin)
3666{
3667 struct dwarf2_per_cu_data *data;
9703b513 3668 struct symtab *result;
9291a0cd
TT
3669
3670 dw2_setup (objfile);
3671
3672 if (!objfile->psymtabs_addrmap)
3673 return NULL;
3674
3675 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3676 if (!data)
3677 return NULL;
3678
3679 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3680 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3681 paddress (get_objfile_arch (objfile), pc));
3682
9703b513
TT
3683 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3684 gdb_assert (result != NULL);
3685 return result;
9291a0cd
TT
3686}
3687
9291a0cd 3688static void
44b13c5a 3689dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3690 void *data, int need_fullname)
9291a0cd
TT
3691{
3692 int i;
24c79950
TT
3693 struct cleanup *cleanup;
3694 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3695 NULL, xcalloc, xfree);
9291a0cd 3696
24c79950 3697 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3698 dw2_setup (objfile);
ae2de4f8 3699
848e3e78
DE
3700 /* The rule is CUs specify all the files, including those used by
3701 any TU, so there's no need to scan TUs here.
3702 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3703
848e3e78 3704 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3705 {
3706 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3707
3708 if (per_cu->v.quick->symtab)
3709 {
3710 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3711 INSERT);
3712
3713 *slot = per_cu->v.quick->file_names;
3714 }
3715 }
3716
848e3e78 3717 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3718 {
3719 int j;
f4dc4d17 3720 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3721 struct quick_file_names *file_data;
24c79950 3722 void **slot;
9291a0cd 3723
3d7bb9d9 3724 /* We only need to look at symtabs not already expanded. */
e254ef6a 3725 if (per_cu->v.quick->symtab)
9291a0cd
TT
3726 continue;
3727
e4a48d9d 3728 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3729 if (file_data == NULL)
9291a0cd
TT
3730 continue;
3731
24c79950
TT
3732 slot = htab_find_slot (visited, file_data, INSERT);
3733 if (*slot)
3734 {
3735 /* Already visited. */
3736 continue;
3737 }
3738 *slot = file_data;
3739
7b9f3c50 3740 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3741 {
74e2f255
DE
3742 const char *this_real_name;
3743
3744 if (need_fullname)
3745 this_real_name = dw2_get_real_path (objfile, file_data, j);
3746 else
3747 this_real_name = NULL;
7b9f3c50 3748 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3749 }
3750 }
24c79950
TT
3751
3752 do_cleanups (cleanup);
9291a0cd
TT
3753}
3754
3755static int
3756dw2_has_symbols (struct objfile *objfile)
3757{
3758 return 1;
3759}
3760
3761const struct quick_symbol_functions dwarf2_gdb_index_functions =
3762{
3763 dw2_has_symbols,
3764 dw2_find_last_source_symtab,
3765 dw2_forget_cached_source_info,
f8eba3c6 3766 dw2_map_symtabs_matching_filename,
9291a0cd 3767 dw2_lookup_symbol,
9291a0cd
TT
3768 dw2_print_stats,
3769 dw2_dump,
3770 dw2_relocate,
3771 dw2_expand_symtabs_for_function,
3772 dw2_expand_all_symtabs,
652a8996 3773 dw2_expand_symtabs_with_fullname,
9291a0cd 3774 dw2_find_symbol_file,
40658b94 3775 dw2_map_matching_symbols,
9291a0cd
TT
3776 dw2_expand_symtabs_matching,
3777 dw2_find_pc_sect_symtab,
9291a0cd
TT
3778 dw2_map_symbol_filenames
3779};
3780
3781/* Initialize for reading DWARF for this objfile. Return 0 if this
3782 file will use psymtabs, or 1 if using the GNU index. */
3783
3784int
3785dwarf2_initialize_objfile (struct objfile *objfile)
3786{
3787 /* If we're about to read full symbols, don't bother with the
3788 indices. In this case we also don't care if some other debug
3789 format is making psymtabs, because they are all about to be
3790 expanded anyway. */
3791 if ((objfile->flags & OBJF_READNOW))
3792 {
3793 int i;
3794
3795 dwarf2_per_objfile->using_index = 1;
3796 create_all_comp_units (objfile);
0e50663e 3797 create_all_type_units (objfile);
7b9f3c50
DE
3798 dwarf2_per_objfile->quick_file_names_table =
3799 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3800
1fd400ff 3801 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3802 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3803 {
e254ef6a 3804 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3805
e254ef6a
DE
3806 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3807 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3808 }
3809
3810 /* Return 1 so that gdb sees the "quick" functions. However,
3811 these functions will be no-ops because we will have expanded
3812 all symtabs. */
3813 return 1;
3814 }
3815
3816 if (dwarf2_read_index (objfile))
3817 return 1;
3818
9291a0cd
TT
3819 return 0;
3820}
3821
3822\f
3823
dce234bc
PP
3824/* Build a partial symbol table. */
3825
3826void
f29dff0a 3827dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3828{
c9bf0622
TT
3829 volatile struct gdb_exception except;
3830
f29dff0a 3831 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3832 {
3833 init_psymbol_list (objfile, 1024);
3834 }
3835
c9bf0622
TT
3836 TRY_CATCH (except, RETURN_MASK_ERROR)
3837 {
3838 /* This isn't really ideal: all the data we allocate on the
3839 objfile's obstack is still uselessly kept around. However,
3840 freeing it seems unsafe. */
3841 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3842
3843 dwarf2_build_psymtabs_hard (objfile);
3844 discard_cleanups (cleanups);
3845 }
3846 if (except.reason < 0)
3847 exception_print (gdb_stderr, except);
c906108c 3848}
c906108c 3849
1ce1cefd
DE
3850/* Return the total length of the CU described by HEADER. */
3851
3852static unsigned int
3853get_cu_length (const struct comp_unit_head *header)
3854{
3855 return header->initial_length_size + header->length;
3856}
3857
45452591
DE
3858/* Return TRUE if OFFSET is within CU_HEADER. */
3859
3860static inline int
b64f50a1 3861offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3862{
b64f50a1 3863 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3864 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3865
b64f50a1 3866 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3867}
3868
3b80fe9b
DE
3869/* Find the base address of the compilation unit for range lists and
3870 location lists. It will normally be specified by DW_AT_low_pc.
3871 In DWARF-3 draft 4, the base address could be overridden by
3872 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3873 compilation units with discontinuous ranges. */
3874
3875static void
3876dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3877{
3878 struct attribute *attr;
3879
3880 cu->base_known = 0;
3881 cu->base_address = 0;
3882
3883 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3884 if (attr)
3885 {
3886 cu->base_address = DW_ADDR (attr);
3887 cu->base_known = 1;
3888 }
3889 else
3890 {
3891 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3892 if (attr)
3893 {
3894 cu->base_address = DW_ADDR (attr);
3895 cu->base_known = 1;
3896 }
3897 }
3898}
3899
93311388
DE
3900/* Read in the comp unit header information from the debug_info at info_ptr.
3901 NOTE: This leaves members offset, first_die_offset to be filled in
3902 by the caller. */
107d2387 3903
fe1b8b76 3904static gdb_byte *
107d2387 3905read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3906 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3907{
3908 int signed_addr;
891d2f0b 3909 unsigned int bytes_read;
c764a876
DE
3910
3911 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3912 cu_header->initial_length_size = bytes_read;
3913 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3914 info_ptr += bytes_read;
107d2387
AC
3915 cu_header->version = read_2_bytes (abfd, info_ptr);
3916 info_ptr += 2;
b64f50a1
JK
3917 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3918 &bytes_read);
613e1657 3919 info_ptr += bytes_read;
107d2387
AC
3920 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3921 info_ptr += 1;
3922 signed_addr = bfd_get_sign_extend_vma (abfd);
3923 if (signed_addr < 0)
8e65ff28 3924 internal_error (__FILE__, __LINE__,
e2e0b3e5 3925 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3926 cu_header->signed_addr_p = signed_addr;
c764a876 3927
107d2387
AC
3928 return info_ptr;
3929}
3930
36586728
TT
3931/* Helper function that returns the proper abbrev section for
3932 THIS_CU. */
3933
3934static struct dwarf2_section_info *
3935get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3936{
3937 struct dwarf2_section_info *abbrev;
3938
3939 if (this_cu->is_dwz)
3940 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3941 else
3942 abbrev = &dwarf2_per_objfile->abbrev;
3943
3944 return abbrev;
3945}
3946
9ff913ba
DE
3947/* Subroutine of read_and_check_comp_unit_head and
3948 read_and_check_type_unit_head to simplify them.
3949 Perform various error checking on the header. */
3950
3951static void
3952error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3953 struct dwarf2_section_info *section,
3954 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3955{
3956 bfd *abfd = section->asection->owner;
3957 const char *filename = bfd_get_filename (abfd);
3958
3959 if (header->version != 2 && header->version != 3 && header->version != 4)
3960 error (_("Dwarf Error: wrong version in compilation unit header "
3961 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3962 filename);
3963
b64f50a1 3964 if (header->abbrev_offset.sect_off
36586728 3965 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3966 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3967 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3968 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3969 filename);
3970
3971 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3972 avoid potential 32-bit overflow. */
1ce1cefd 3973 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3974 > section->size)
3975 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3976 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3977 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3978 filename);
3979}
3980
3981/* Read in a CU/TU header and perform some basic error checking.
3982 The contents of the header are stored in HEADER.
3983 The result is a pointer to the start of the first DIE. */
adabb602 3984
fe1b8b76 3985static gdb_byte *
9ff913ba
DE
3986read_and_check_comp_unit_head (struct comp_unit_head *header,
3987 struct dwarf2_section_info *section,
4bdcc0c1 3988 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3989 gdb_byte *info_ptr,
3990 int is_debug_types_section)
72bf9492 3991{
fe1b8b76 3992 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3993 bfd *abfd = section->asection->owner;
72bf9492 3994
b64f50a1 3995 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3996
72bf9492
DJ
3997 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3998
460c1c54
CC
3999 /* If we're reading a type unit, skip over the signature and
4000 type_offset fields. */
b0df02fd 4001 if (is_debug_types_section)
460c1c54
CC
4002 info_ptr += 8 /*signature*/ + header->offset_size;
4003
b64f50a1 4004 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4005
4bdcc0c1 4006 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4007
4008 return info_ptr;
4009}
4010
348e048f
DE
4011/* Read in the types comp unit header information from .debug_types entry at
4012 types_ptr. The result is a pointer to one past the end of the header. */
4013
4014static gdb_byte *
9ff913ba
DE
4015read_and_check_type_unit_head (struct comp_unit_head *header,
4016 struct dwarf2_section_info *section,
4bdcc0c1 4017 struct dwarf2_section_info *abbrev_section,
9ff913ba 4018 gdb_byte *info_ptr,
dee91e82
DE
4019 ULONGEST *signature,
4020 cu_offset *type_offset_in_tu)
348e048f 4021{
9ff913ba
DE
4022 gdb_byte *beg_of_comp_unit = info_ptr;
4023 bfd *abfd = section->asection->owner;
348e048f 4024
b64f50a1 4025 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4026
9ff913ba 4027 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4028
9ff913ba
DE
4029 /* If we're reading a type unit, skip over the signature and
4030 type_offset fields. */
4031 if (signature != NULL)
4032 *signature = read_8_bytes (abfd, info_ptr);
4033 info_ptr += 8;
dee91e82
DE
4034 if (type_offset_in_tu != NULL)
4035 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4036 header->offset_size);
9ff913ba
DE
4037 info_ptr += header->offset_size;
4038
b64f50a1 4039 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4040
4bdcc0c1 4041 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4042
4043 return info_ptr;
348e048f
DE
4044}
4045
f4dc4d17
DE
4046/* Fetch the abbreviation table offset from a comp or type unit header. */
4047
4048static sect_offset
4049read_abbrev_offset (struct dwarf2_section_info *section,
4050 sect_offset offset)
4051{
4052 bfd *abfd = section->asection->owner;
4053 gdb_byte *info_ptr;
4054 unsigned int length, initial_length_size, offset_size;
4055 sect_offset abbrev_offset;
4056
4057 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4058 info_ptr = section->buffer + offset.sect_off;
4059 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4060 offset_size = initial_length_size == 4 ? 4 : 8;
4061 info_ptr += initial_length_size + 2 /*version*/;
4062 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4063 return abbrev_offset;
4064}
4065
aaa75496
JB
4066/* Allocate a new partial symtab for file named NAME and mark this new
4067 partial symtab as being an include of PST. */
4068
4069static void
4070dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4071 struct objfile *objfile)
4072{
4073 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4074
fbd9ab74
JK
4075 if (!IS_ABSOLUTE_PATH (subpst->filename))
4076 {
4077 /* It shares objfile->objfile_obstack. */
4078 subpst->dirname = pst->dirname;
4079 }
4080
aaa75496
JB
4081 subpst->section_offsets = pst->section_offsets;
4082 subpst->textlow = 0;
4083 subpst->texthigh = 0;
4084
4085 subpst->dependencies = (struct partial_symtab **)
4086 obstack_alloc (&objfile->objfile_obstack,
4087 sizeof (struct partial_symtab *));
4088 subpst->dependencies[0] = pst;
4089 subpst->number_of_dependencies = 1;
4090
4091 subpst->globals_offset = 0;
4092 subpst->n_global_syms = 0;
4093 subpst->statics_offset = 0;
4094 subpst->n_static_syms = 0;
4095 subpst->symtab = NULL;
4096 subpst->read_symtab = pst->read_symtab;
4097 subpst->readin = 0;
4098
4099 /* No private part is necessary for include psymtabs. This property
4100 can be used to differentiate between such include psymtabs and
10b3939b 4101 the regular ones. */
58a9656e 4102 subpst->read_symtab_private = NULL;
aaa75496
JB
4103}
4104
4105/* Read the Line Number Program data and extract the list of files
4106 included by the source file represented by PST. Build an include
d85a05f0 4107 partial symtab for each of these included files. */
aaa75496
JB
4108
4109static void
4110dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4111 struct die_info *die,
4112 struct partial_symtab *pst)
aaa75496 4113{
d85a05f0
DJ
4114 struct line_header *lh = NULL;
4115 struct attribute *attr;
aaa75496 4116
d85a05f0
DJ
4117 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4118 if (attr)
3019eac3 4119 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4120 if (lh == NULL)
4121 return; /* No linetable, so no includes. */
4122
c6da4cef 4123 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4124 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4125
4126 free_line_header (lh);
4127}
4128
348e048f 4129static hashval_t
52dc124a 4130hash_signatured_type (const void *item)
348e048f 4131{
52dc124a 4132 const struct signatured_type *sig_type = item;
9a619af0 4133
348e048f 4134 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4135 return sig_type->signature;
348e048f
DE
4136}
4137
4138static int
52dc124a 4139eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4140{
4141 const struct signatured_type *lhs = item_lhs;
4142 const struct signatured_type *rhs = item_rhs;
9a619af0 4143
348e048f
DE
4144 return lhs->signature == rhs->signature;
4145}
4146
1fd400ff
TT
4147/* Allocate a hash table for signatured types. */
4148
4149static htab_t
673bfd45 4150allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4151{
4152 return htab_create_alloc_ex (41,
52dc124a
DE
4153 hash_signatured_type,
4154 eq_signatured_type,
1fd400ff
TT
4155 NULL,
4156 &objfile->objfile_obstack,
4157 hashtab_obstack_allocate,
4158 dummy_obstack_deallocate);
4159}
4160
d467dd73 4161/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4162
4163static int
d467dd73 4164add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4165{
4166 struct signatured_type *sigt = *slot;
b4dd5633 4167 struct signatured_type ***datap = datum;
1fd400ff 4168
b4dd5633 4169 **datap = sigt;
1fd400ff
TT
4170 ++*datap;
4171
4172 return 1;
4173}
4174
c88ee1f0
DE
4175/* Create the hash table of all entries in the .debug_types
4176 (or .debug_types.dwo) section(s).
4177 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4178 otherwise it is NULL.
4179
4180 The result is a pointer to the hash table or NULL if there are no types.
4181
4182 Note: This function processes DWO files only, not DWP files. */
348e048f 4183
3019eac3
DE
4184static htab_t
4185create_debug_types_hash_table (struct dwo_file *dwo_file,
4186 VEC (dwarf2_section_info_def) *types)
348e048f 4187{
3019eac3 4188 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4189 htab_t types_htab = NULL;
8b70b953
TT
4190 int ix;
4191 struct dwarf2_section_info *section;
4bdcc0c1 4192 struct dwarf2_section_info *abbrev_section;
348e048f 4193
3019eac3
DE
4194 if (VEC_empty (dwarf2_section_info_def, types))
4195 return NULL;
348e048f 4196
4bdcc0c1
DE
4197 abbrev_section = (dwo_file != NULL
4198 ? &dwo_file->sections.abbrev
4199 : &dwarf2_per_objfile->abbrev);
4200
09406207
DE
4201 if (dwarf2_read_debug)
4202 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4203 dwo_file ? ".dwo" : "",
4204 bfd_get_filename (abbrev_section->asection->owner));
4205
8b70b953 4206 for (ix = 0;
3019eac3 4207 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4208 ++ix)
4209 {
3019eac3 4210 bfd *abfd;
8b70b953 4211 gdb_byte *info_ptr, *end_ptr;
36586728 4212 struct dwarf2_section_info *abbrev_section;
348e048f 4213
8b70b953
TT
4214 dwarf2_read_section (objfile, section);
4215 info_ptr = section->buffer;
348e048f 4216
8b70b953
TT
4217 if (info_ptr == NULL)
4218 continue;
348e048f 4219
3019eac3
DE
4220 /* We can't set abfd until now because the section may be empty or
4221 not present, in which case section->asection will be NULL. */
4222 abfd = section->asection->owner;
4223
36586728
TT
4224 if (dwo_file)
4225 abbrev_section = &dwo_file->sections.abbrev;
4226 else
4227 abbrev_section = &dwarf2_per_objfile->abbrev;
4228
dee91e82
DE
4229 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4230 because we don't need to read any dies: the signature is in the
4231 header. */
8b70b953
TT
4232
4233 end_ptr = info_ptr + section->size;
4234 while (info_ptr < end_ptr)
4235 {
b64f50a1 4236 sect_offset offset;
3019eac3 4237 cu_offset type_offset_in_tu;
8b70b953 4238 ULONGEST signature;
52dc124a 4239 struct signatured_type *sig_type;
3019eac3 4240 struct dwo_unit *dwo_tu;
8b70b953
TT
4241 void **slot;
4242 gdb_byte *ptr = info_ptr;
9ff913ba 4243 struct comp_unit_head header;
dee91e82 4244 unsigned int length;
348e048f 4245
b64f50a1 4246 offset.sect_off = ptr - section->buffer;
348e048f 4247
8b70b953 4248 /* We need to read the type's signature in order to build the hash
9ff913ba 4249 table, but we don't need anything else just yet. */
348e048f 4250
4bdcc0c1
DE
4251 ptr = read_and_check_type_unit_head (&header, section,
4252 abbrev_section, ptr,
3019eac3 4253 &signature, &type_offset_in_tu);
6caca83c 4254
1ce1cefd 4255 length = get_cu_length (&header);
dee91e82 4256
6caca83c 4257 /* Skip dummy type units. */
dee91e82
DE
4258 if (ptr >= info_ptr + length
4259 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4260 {
1ce1cefd 4261 info_ptr += length;
6caca83c
CC
4262 continue;
4263 }
8b70b953 4264
0349ea22
DE
4265 if (types_htab == NULL)
4266 {
4267 if (dwo_file)
4268 types_htab = allocate_dwo_unit_table (objfile);
4269 else
4270 types_htab = allocate_signatured_type_table (objfile);
4271 }
4272
3019eac3
DE
4273 if (dwo_file)
4274 {
4275 sig_type = NULL;
4276 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4277 struct dwo_unit);
4278 dwo_tu->dwo_file = dwo_file;
4279 dwo_tu->signature = signature;
4280 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4281 dwo_tu->section = section;
3019eac3
DE
4282 dwo_tu->offset = offset;
4283 dwo_tu->length = length;
4284 }
4285 else
4286 {
4287 /* N.B.: type_offset is not usable if this type uses a DWO file.
4288 The real type_offset is in the DWO file. */
4289 dwo_tu = NULL;
4290 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4291 struct signatured_type);
4292 sig_type->signature = signature;
4293 sig_type->type_offset_in_tu = type_offset_in_tu;
4294 sig_type->per_cu.objfile = objfile;
4295 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4296 sig_type->per_cu.section = section;
3019eac3
DE
4297 sig_type->per_cu.offset = offset;
4298 sig_type->per_cu.length = length;
4299 }
8b70b953 4300
3019eac3
DE
4301 slot = htab_find_slot (types_htab,
4302 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4303 INSERT);
8b70b953
TT
4304 gdb_assert (slot != NULL);
4305 if (*slot != NULL)
4306 {
3019eac3
DE
4307 sect_offset dup_offset;
4308
4309 if (dwo_file)
4310 {
4311 const struct dwo_unit *dup_tu = *slot;
4312
4313 dup_offset = dup_tu->offset;
4314 }
4315 else
4316 {
4317 const struct signatured_type *dup_tu = *slot;
4318
4319 dup_offset = dup_tu->per_cu.offset;
4320 }
b3c8eb43 4321
8b70b953 4322 complaint (&symfile_complaints,
c88ee1f0
DE
4323 _("debug type entry at offset 0x%x is duplicate to"
4324 " the entry at offset 0x%x, signature 0x%s"),
3019eac3 4325 offset.sect_off, dup_offset.sect_off,
8b70b953 4326 phex (signature, sizeof (signature)));
8b70b953 4327 }
3019eac3 4328 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4329
09406207 4330 if (dwarf2_read_debug)
8b70b953 4331 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4332 offset.sect_off,
4333 phex (signature, sizeof (signature)));
348e048f 4334
dee91e82 4335 info_ptr += length;
8b70b953 4336 }
348e048f
DE
4337 }
4338
3019eac3
DE
4339 return types_htab;
4340}
4341
4342/* Create the hash table of all entries in the .debug_types section,
4343 and initialize all_type_units.
4344 The result is zero if there is an error (e.g. missing .debug_types section),
4345 otherwise non-zero. */
4346
4347static int
4348create_all_type_units (struct objfile *objfile)
4349{
4350 htab_t types_htab;
b4dd5633 4351 struct signatured_type **iter;
3019eac3
DE
4352
4353 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4354 if (types_htab == NULL)
4355 {
4356 dwarf2_per_objfile->signatured_types = NULL;
4357 return 0;
4358 }
4359
348e048f
DE
4360 dwarf2_per_objfile->signatured_types = types_htab;
4361
d467dd73
DE
4362 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4363 dwarf2_per_objfile->all_type_units
1fd400ff 4364 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4365 dwarf2_per_objfile->n_type_units
b4dd5633 4366 * sizeof (struct signatured_type *));
d467dd73
DE
4367 iter = &dwarf2_per_objfile->all_type_units[0];
4368 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4369 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4370 == dwarf2_per_objfile->n_type_units);
1fd400ff 4371
348e048f
DE
4372 return 1;
4373}
4374
380bca97 4375/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4376 Returns NULL if signature SIG is not present in the table.
4377 It is up to the caller to complain about this. */
348e048f
DE
4378
4379static struct signatured_type *
e319fa28 4380lookup_signatured_type (ULONGEST sig)
348e048f
DE
4381{
4382 struct signatured_type find_entry, *entry;
4383
4384 if (dwarf2_per_objfile->signatured_types == NULL)
5a8b3f62 4385 return NULL;
348e048f
DE
4386 find_entry.signature = sig;
4387 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4388 return entry;
4389}
42e7ad6c
DE
4390\f
4391/* Low level DIE reading support. */
348e048f 4392
d85a05f0
DJ
4393/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4394
4395static void
4396init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4397 struct dwarf2_cu *cu,
3019eac3
DE
4398 struct dwarf2_section_info *section,
4399 struct dwo_file *dwo_file)
d85a05f0 4400{
fceca515 4401 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4402 reader->abfd = section->asection->owner;
d85a05f0 4403 reader->cu = cu;
3019eac3 4404 reader->dwo_file = dwo_file;
dee91e82
DE
4405 reader->die_section = section;
4406 reader->buffer = section->buffer;
f664829e 4407 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4408}
4409
b0c7bfa9
DE
4410/* Subroutine of init_cutu_and_read_dies to simplify it.
4411 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4412 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4413 already.
4414
4415 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4416 from it to the DIE in the DWO. If NULL we are skipping the stub.
4417 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4418 are filled in with the info of the DIE from the DWO file.
4419 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4420 provided an abbrev table to use.
4421 The result is non-zero if a valid (non-dummy) DIE was found. */
4422
4423static int
4424read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4425 struct dwo_unit *dwo_unit,
4426 int abbrev_table_provided,
4427 struct die_info *stub_comp_unit_die,
4428 struct die_reader_specs *result_reader,
4429 gdb_byte **result_info_ptr,
4430 struct die_info **result_comp_unit_die,
4431 int *result_has_children)
4432{
4433 struct objfile *objfile = dwarf2_per_objfile->objfile;
4434 struct dwarf2_cu *cu = this_cu->cu;
4435 struct dwarf2_section_info *section;
4436 bfd *abfd;
4437 gdb_byte *begin_info_ptr, *info_ptr;
4438 const char *comp_dir_string;
4439 ULONGEST signature; /* Or dwo_id. */
4440 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4441 int i,num_extra_attrs;
4442 struct dwarf2_section_info *dwo_abbrev_section;
4443 struct attribute *attr;
4444 struct die_info *comp_unit_die;
4445
4446 /* These attributes aren't processed until later:
4447 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4448 However, the attribute is found in the stub which we won't have later.
4449 In order to not impose this complication on the rest of the code,
4450 we read them here and copy them to the DWO CU/TU die. */
4451
4452 stmt_list = NULL;
4453 low_pc = NULL;
4454 high_pc = NULL;
4455 ranges = NULL;
4456 comp_dir = NULL;
4457
4458 if (stub_comp_unit_die != NULL)
4459 {
4460 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4461 DWO file. */
4462 if (! this_cu->is_debug_types)
4463 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4464 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4465 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4466 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4467 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4468
4469 /* There should be a DW_AT_addr_base attribute here (if needed).
4470 We need the value before we can process DW_FORM_GNU_addr_index. */
4471 cu->addr_base = 0;
4472 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4473 if (attr)
4474 cu->addr_base = DW_UNSND (attr);
4475
4476 /* There should be a DW_AT_ranges_base attribute here (if needed).
4477 We need the value before we can process DW_AT_ranges. */
4478 cu->ranges_base = 0;
4479 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4480 if (attr)
4481 cu->ranges_base = DW_UNSND (attr);
4482 }
4483
4484 /* Set up for reading the DWO CU/TU. */
4485 cu->dwo_unit = dwo_unit;
4486 section = dwo_unit->section;
4487 dwarf2_read_section (objfile, section);
4488 abfd = section->asection->owner;
4489 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4490 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4491 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4492
4493 if (this_cu->is_debug_types)
4494 {
4495 ULONGEST header_signature;
4496 cu_offset type_offset_in_tu;
4497 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4498
4499 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4500 dwo_abbrev_section,
4501 info_ptr,
4502 &header_signature,
4503 &type_offset_in_tu);
4504 gdb_assert (sig_type->signature == header_signature);
4505 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4506 /* For DWOs coming from DWP files, we don't know the CU length
4507 nor the type's offset in the TU until now. */
4508 dwo_unit->length = get_cu_length (&cu->header);
4509 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4510
4511 /* Establish the type offset that can be used to lookup the type.
4512 For DWO files, we don't know it until now. */
4513 sig_type->type_offset_in_section.sect_off =
4514 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4515 }
4516 else
4517 {
4518 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4519 dwo_abbrev_section,
4520 info_ptr, 0);
4521 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4522 /* For DWOs coming from DWP files, we don't know the CU length
4523 until now. */
4524 dwo_unit->length = get_cu_length (&cu->header);
4525 }
4526
4527 /* Replace the CU's original abbrev table with the DWO's. */
4528 if (abbrev_table_provided)
4529 {
4530 /* Don't free the provided abbrev table, the caller of
4531 init_cutu_and_read_dies owns it. */
4532 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4533 make_cleanup (dwarf2_free_abbrev_table, cu);
4534 }
4535 else
4536 {
4537 dwarf2_free_abbrev_table (cu);
4538 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4539 }
4540
4541 /* Read in the die, but leave space to copy over the attributes
4542 from the stub. This has the benefit of simplifying the rest of
4543 the code - all the work to maintain the illusion of a single
4544 DW_TAG_{compile,type}_unit DIE is done here. */
4545 num_extra_attrs = ((stmt_list != NULL)
4546 + (low_pc != NULL)
4547 + (high_pc != NULL)
4548 + (ranges != NULL)
4549 + (comp_dir != NULL));
4550 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4551 result_has_children, num_extra_attrs);
4552
4553 /* Copy over the attributes from the stub to the DIE we just read in. */
4554 comp_unit_die = *result_comp_unit_die;
4555 i = comp_unit_die->num_attrs;
4556 if (stmt_list != NULL)
4557 comp_unit_die->attrs[i++] = *stmt_list;
4558 if (low_pc != NULL)
4559 comp_unit_die->attrs[i++] = *low_pc;
4560 if (high_pc != NULL)
4561 comp_unit_die->attrs[i++] = *high_pc;
4562 if (ranges != NULL)
4563 comp_unit_die->attrs[i++] = *ranges;
4564 if (comp_dir != NULL)
4565 comp_unit_die->attrs[i++] = *comp_dir;
4566 comp_unit_die->num_attrs += num_extra_attrs;
4567
4568 /* Skip dummy compilation units. */
4569 if (info_ptr >= begin_info_ptr + dwo_unit->length
4570 || peek_abbrev_code (abfd, info_ptr) == 0)
4571 return 0;
4572
4573 *result_info_ptr = info_ptr;
4574 return 1;
4575}
4576
4577/* Subroutine of init_cutu_and_read_dies to simplify it.
4578 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4579 If the specified DWO unit cannot be found an error is thrown. */
4580
4581static struct dwo_unit *
4582lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4583 struct die_info *comp_unit_die)
4584{
4585 struct dwarf2_cu *cu = this_cu->cu;
4586 struct attribute *attr;
4587 ULONGEST signature;
4588 struct dwo_unit *dwo_unit;
4589 const char *comp_dir, *dwo_name;
4590
4591 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4592 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4593 gdb_assert (attr != NULL);
4594 dwo_name = DW_STRING (attr);
4595 comp_dir = NULL;
4596 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4597 if (attr)
4598 comp_dir = DW_STRING (attr);
4599
4600 if (this_cu->is_debug_types)
4601 {
4602 struct signatured_type *sig_type;
4603
4604 /* Since this_cu is the first member of struct signatured_type,
4605 we can go from a pointer to one to a pointer to the other. */
4606 sig_type = (struct signatured_type *) this_cu;
4607 signature = sig_type->signature;
4608 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4609 }
4610 else
4611 {
4612 struct attribute *attr;
4613
4614 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4615 if (! attr)
4616 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4617 " [in module %s]"),
4618 dwo_name, this_cu->objfile->name);
4619 signature = DW_UNSND (attr);
4620 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4621 signature);
4622 }
4623
4624 if (dwo_unit == NULL)
4625 {
4626 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4627 " with ID %s [in module %s]"),
4628 this_cu->offset.sect_off,
4629 phex (signature, sizeof (signature)),
4630 this_cu->objfile->name);
4631 }
4632
4633 return dwo_unit;
4634}
4635
fd820528 4636/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4637 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4638
f4dc4d17
DE
4639 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4640 Otherwise the table specified in the comp unit header is read in and used.
4641 This is an optimization for when we already have the abbrev table.
4642
dee91e82
DE
4643 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4644 Otherwise, a new CU is allocated with xmalloc.
4645
4646 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4647 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4648
4649 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4650 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4651
70221824 4652static void
fd820528 4653init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4654 struct abbrev_table *abbrev_table,
fd820528
DE
4655 int use_existing_cu, int keep,
4656 die_reader_func_ftype *die_reader_func,
4657 void *data)
c906108c 4658{
dee91e82 4659 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4660 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4661 bfd *abfd = section->asection->owner;
dee91e82
DE
4662 struct dwarf2_cu *cu;
4663 gdb_byte *begin_info_ptr, *info_ptr;
4664 struct die_reader_specs reader;
d85a05f0 4665 struct die_info *comp_unit_die;
dee91e82 4666 int has_children;
d85a05f0 4667 struct attribute *attr;
dee91e82
DE
4668 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4669 struct signatured_type *sig_type = NULL;
4bdcc0c1 4670 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4671 /* Non-zero if CU currently points to a DWO file and we need to
4672 reread it. When this happens we need to reread the skeleton die
4673 before we can reread the DWO file. */
4674 int rereading_dwo_cu = 0;
c906108c 4675
09406207
DE
4676 if (dwarf2_die_debug)
4677 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4678 this_cu->is_debug_types ? "type" : "comp",
4679 this_cu->offset.sect_off);
4680
dee91e82
DE
4681 if (use_existing_cu)
4682 gdb_assert (keep);
23745b47 4683
dee91e82
DE
4684 cleanups = make_cleanup (null_cleanup, NULL);
4685
4686 /* This is cheap if the section is already read in. */
4687 dwarf2_read_section (objfile, section);
4688
4689 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4690
4691 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4692
4693 if (use_existing_cu && this_cu->cu != NULL)
4694 {
4695 cu = this_cu->cu;
42e7ad6c
DE
4696
4697 /* If this CU is from a DWO file we need to start over, we need to
4698 refetch the attributes from the skeleton CU.
4699 This could be optimized by retrieving those attributes from when we
4700 were here the first time: the previous comp_unit_die was stored in
4701 comp_unit_obstack. But there's no data yet that we need this
4702 optimization. */
4703 if (cu->dwo_unit != NULL)
4704 rereading_dwo_cu = 1;
dee91e82
DE
4705 }
4706 else
4707 {
4708 /* If !use_existing_cu, this_cu->cu must be NULL. */
4709 gdb_assert (this_cu->cu == NULL);
4710
4711 cu = xmalloc (sizeof (*cu));
4712 init_one_comp_unit (cu, this_cu);
4713
4714 /* If an error occurs while loading, release our storage. */
4715 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4716 }
dee91e82 4717
b0c7bfa9 4718 /* Get the header. */
42e7ad6c
DE
4719 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4720 {
4721 /* We already have the header, there's no need to read it in again. */
4722 info_ptr += cu->header.first_die_offset.cu_off;
4723 }
4724 else
4725 {
3019eac3 4726 if (this_cu->is_debug_types)
dee91e82
DE
4727 {
4728 ULONGEST signature;
42e7ad6c 4729 cu_offset type_offset_in_tu;
dee91e82 4730
4bdcc0c1
DE
4731 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4732 abbrev_section, info_ptr,
42e7ad6c
DE
4733 &signature,
4734 &type_offset_in_tu);
dee91e82 4735
42e7ad6c
DE
4736 /* Since per_cu is the first member of struct signatured_type,
4737 we can go from a pointer to one to a pointer to the other. */
4738 sig_type = (struct signatured_type *) this_cu;
4739 gdb_assert (sig_type->signature == signature);
4740 gdb_assert (sig_type->type_offset_in_tu.cu_off
4741 == type_offset_in_tu.cu_off);
dee91e82
DE
4742 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4743
42e7ad6c
DE
4744 /* LENGTH has not been set yet for type units if we're
4745 using .gdb_index. */
1ce1cefd 4746 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4747
4748 /* Establish the type offset that can be used to lookup the type. */
4749 sig_type->type_offset_in_section.sect_off =
4750 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4751 }
4752 else
4753 {
4bdcc0c1
DE
4754 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4755 abbrev_section,
4756 info_ptr, 0);
dee91e82
DE
4757
4758 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4759 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4760 }
4761 }
10b3939b 4762
6caca83c 4763 /* Skip dummy compilation units. */
dee91e82 4764 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4765 || peek_abbrev_code (abfd, info_ptr) == 0)
4766 {
dee91e82 4767 do_cleanups (cleanups);
21b2bd31 4768 return;
6caca83c
CC
4769 }
4770
433df2d4
DE
4771 /* If we don't have them yet, read the abbrevs for this compilation unit.
4772 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4773 done. Note that it's important that if the CU had an abbrev table
4774 on entry we don't free it when we're done: Somewhere up the call stack
4775 it may be in use. */
f4dc4d17
DE
4776 if (abbrev_table != NULL)
4777 {
4778 gdb_assert (cu->abbrev_table == NULL);
4779 gdb_assert (cu->header.abbrev_offset.sect_off
4780 == abbrev_table->offset.sect_off);
4781 cu->abbrev_table = abbrev_table;
4782 }
4783 else if (cu->abbrev_table == NULL)
dee91e82 4784 {
4bdcc0c1 4785 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4786 make_cleanup (dwarf2_free_abbrev_table, cu);
4787 }
42e7ad6c
DE
4788 else if (rereading_dwo_cu)
4789 {
4790 dwarf2_free_abbrev_table (cu);
4791 dwarf2_read_abbrevs (cu, abbrev_section);
4792 }
af703f96 4793
dee91e82 4794 /* Read the top level CU/TU die. */
3019eac3 4795 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4796 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4797
b0c7bfa9
DE
4798 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4799 from the DWO file.
4800 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4801 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
4802 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4803 if (attr)
4804 {
3019eac3 4805 struct dwo_unit *dwo_unit;
b0c7bfa9 4806 struct die_info *dwo_comp_unit_die;
3019eac3
DE
4807
4808 if (has_children)
4809 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4810 " has children (offset 0x%x) [in module %s]"),
4811 this_cu->offset.sect_off, bfd_get_filename (abfd));
b0c7bfa9
DE
4812 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
4813 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4814 abbrev_table != NULL,
4815 comp_unit_die,
4816 &reader, &info_ptr,
4817 &dwo_comp_unit_die, &has_children) == 0)
3019eac3 4818 {
b0c7bfa9 4819 /* Dummy die. */
3019eac3
DE
4820 do_cleanups (cleanups);
4821 return;
4822 }
b0c7bfa9 4823 comp_unit_die = dwo_comp_unit_die;
3019eac3
DE
4824 }
4825
b0c7bfa9 4826 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
4827 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4828
b0c7bfa9 4829 /* Done, clean up. */
dee91e82 4830 if (free_cu_cleanup != NULL)
348e048f 4831 {
dee91e82
DE
4832 if (keep)
4833 {
4834 /* We've successfully allocated this compilation unit. Let our
4835 caller clean it up when finished with it. */
4836 discard_cleanups (free_cu_cleanup);
4837
4838 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4839 So we have to manually free the abbrev table. */
4840 dwarf2_free_abbrev_table (cu);
4841
4842 /* Link this CU into read_in_chain. */
4843 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4844 dwarf2_per_objfile->read_in_chain = this_cu;
4845 }
4846 else
4847 do_cleanups (free_cu_cleanup);
348e048f 4848 }
dee91e82
DE
4849
4850 do_cleanups (cleanups);
4851}
4852
3019eac3
DE
4853/* Read CU/TU THIS_CU in section SECTION,
4854 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4855 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4856 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4857
4858 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4859 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4860
4861 We fill in THIS_CU->length.
4862
4863 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4864 linker) then DIE_READER_FUNC will not get called.
4865
4866 THIS_CU->cu is always freed when done.
3019eac3
DE
4867 This is done in order to not leave THIS_CU->cu in a state where we have
4868 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4869
4870static void
4871init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4872 struct dwarf2_section_info *abbrev_section,
3019eac3 4873 struct dwo_file *dwo_file,
dee91e82
DE
4874 die_reader_func_ftype *die_reader_func,
4875 void *data)
4876{
4877 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4878 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4879 bfd *abfd = section->asection->owner;
dee91e82
DE
4880 struct dwarf2_cu cu;
4881 gdb_byte *begin_info_ptr, *info_ptr;
4882 struct die_reader_specs reader;
4883 struct cleanup *cleanups;
4884 struct die_info *comp_unit_die;
4885 int has_children;
4886
09406207
DE
4887 if (dwarf2_die_debug)
4888 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4889 this_cu->is_debug_types ? "type" : "comp",
4890 this_cu->offset.sect_off);
4891
dee91e82
DE
4892 gdb_assert (this_cu->cu == NULL);
4893
dee91e82
DE
4894 /* This is cheap if the section is already read in. */
4895 dwarf2_read_section (objfile, section);
4896
4897 init_one_comp_unit (&cu, this_cu);
4898
4899 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4900
4901 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4902 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4903 abbrev_section, info_ptr,
3019eac3 4904 this_cu->is_debug_types);
dee91e82 4905
1ce1cefd 4906 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4907
4908 /* Skip dummy compilation units. */
4909 if (info_ptr >= begin_info_ptr + this_cu->length
4910 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4911 {
dee91e82 4912 do_cleanups (cleanups);
21b2bd31 4913 return;
93311388 4914 }
72bf9492 4915
dee91e82
DE
4916 dwarf2_read_abbrevs (&cu, abbrev_section);
4917 make_cleanup (dwarf2_free_abbrev_table, &cu);
4918
3019eac3 4919 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4920 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4921
4922 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4923
4924 do_cleanups (cleanups);
4925}
4926
3019eac3
DE
4927/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4928 does not lookup the specified DWO file.
4929 This cannot be used to read DWO files.
dee91e82
DE
4930
4931 THIS_CU->cu is always freed when done.
3019eac3
DE
4932 This is done in order to not leave THIS_CU->cu in a state where we have
4933 to care whether it refers to the "main" CU or the DWO CU.
4934 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4935
4936static void
4937init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4938 die_reader_func_ftype *die_reader_func,
4939 void *data)
4940{
4941 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4942 get_abbrev_section_for_cu (this_cu),
3019eac3 4943 NULL,
dee91e82
DE
4944 die_reader_func, data);
4945}
0018ea6f
DE
4946\f
4947/* Type Unit Groups.
dee91e82 4948
0018ea6f
DE
4949 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4950 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4951 so that all types coming from the same compilation (.o file) are grouped
4952 together. A future step could be to put the types in the same symtab as
4953 the CU the types ultimately came from. */
ff013f42 4954
f4dc4d17
DE
4955static hashval_t
4956hash_type_unit_group (const void *item)
4957{
094b34ac 4958 const struct type_unit_group *tu_group = item;
f4dc4d17 4959
094b34ac 4960 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4961}
348e048f
DE
4962
4963static int
f4dc4d17 4964eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4965{
f4dc4d17
DE
4966 const struct type_unit_group *lhs = item_lhs;
4967 const struct type_unit_group *rhs = item_rhs;
348e048f 4968
094b34ac 4969 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4970}
348e048f 4971
f4dc4d17
DE
4972/* Allocate a hash table for type unit groups. */
4973
4974static htab_t
4975allocate_type_unit_groups_table (void)
4976{
4977 return htab_create_alloc_ex (3,
4978 hash_type_unit_group,
4979 eq_type_unit_group,
4980 NULL,
4981 &dwarf2_per_objfile->objfile->objfile_obstack,
4982 hashtab_obstack_allocate,
4983 dummy_obstack_deallocate);
4984}
dee91e82 4985
f4dc4d17
DE
4986/* Type units that don't have DW_AT_stmt_list are grouped into their own
4987 partial symtabs. We combine several TUs per psymtab to not let the size
4988 of any one psymtab grow too big. */
4989#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4990#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 4991
094b34ac 4992/* Helper routine for get_type_unit_group.
f4dc4d17
DE
4993 Create the type_unit_group object used to hold one or more TUs. */
4994
4995static struct type_unit_group *
094b34ac 4996create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
4997{
4998 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 4999 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5000 struct type_unit_group *tu_group;
f4dc4d17
DE
5001
5002 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5003 struct type_unit_group);
094b34ac 5004 per_cu = &tu_group->per_cu;
f4dc4d17 5005 per_cu->objfile = objfile;
f4dc4d17 5006
094b34ac
DE
5007 if (dwarf2_per_objfile->using_index)
5008 {
5009 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5010 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5011 }
5012 else
5013 {
5014 unsigned int line_offset = line_offset_struct.sect_off;
5015 struct partial_symtab *pst;
5016 char *name;
5017
5018 /* Give the symtab a useful name for debug purposes. */
5019 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5020 name = xstrprintf ("<type_units_%d>",
5021 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5022 else
5023 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5024
5025 pst = create_partial_symtab (per_cu, name);
5026 pst->anonymous = 1;
f4dc4d17 5027
094b34ac
DE
5028 xfree (name);
5029 }
f4dc4d17 5030
094b34ac
DE
5031 tu_group->hash.dwo_unit = cu->dwo_unit;
5032 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5033
5034 return tu_group;
5035}
5036
094b34ac
DE
5037/* Look up the type_unit_group for type unit CU, and create it if necessary.
5038 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5039
5040static struct type_unit_group *
094b34ac 5041get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5042{
5043 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5044 struct type_unit_group *tu_group;
5045 void **slot;
5046 unsigned int line_offset;
5047 struct type_unit_group type_unit_group_for_lookup;
5048
5049 if (dwarf2_per_objfile->type_unit_groups == NULL)
5050 {
5051 dwarf2_per_objfile->type_unit_groups =
5052 allocate_type_unit_groups_table ();
5053 }
5054
5055 /* Do we need to create a new group, or can we use an existing one? */
5056
5057 if (stmt_list)
5058 {
5059 line_offset = DW_UNSND (stmt_list);
5060 ++tu_stats->nr_symtab_sharers;
5061 }
5062 else
5063 {
5064 /* Ugh, no stmt_list. Rare, but we have to handle it.
5065 We can do various things here like create one group per TU or
5066 spread them over multiple groups to split up the expansion work.
5067 To avoid worst case scenarios (too many groups or too large groups)
5068 we, umm, group them in bunches. */
5069 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5070 | (tu_stats->nr_stmt_less_type_units
5071 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5072 ++tu_stats->nr_stmt_less_type_units;
5073 }
5074
094b34ac
DE
5075 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5076 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5077 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5078 &type_unit_group_for_lookup, INSERT);
5079 if (*slot != NULL)
5080 {
5081 tu_group = *slot;
5082 gdb_assert (tu_group != NULL);
5083 }
5084 else
5085 {
5086 sect_offset line_offset_struct;
5087
5088 line_offset_struct.sect_off = line_offset;
094b34ac 5089 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5090 *slot = tu_group;
5091 ++tu_stats->nr_symtabs;
5092 }
5093
5094 return tu_group;
5095}
5096
5097/* Struct used to sort TUs by their abbreviation table offset. */
5098
5099struct tu_abbrev_offset
5100{
5101 struct signatured_type *sig_type;
5102 sect_offset abbrev_offset;
5103};
5104
5105/* Helper routine for build_type_unit_groups, passed to qsort. */
5106
5107static int
5108sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5109{
5110 const struct tu_abbrev_offset * const *a = ap;
5111 const struct tu_abbrev_offset * const *b = bp;
5112 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5113 unsigned int boff = (*b)->abbrev_offset.sect_off;
5114
5115 return (aoff > boff) - (aoff < boff);
5116}
5117
5118/* A helper function to add a type_unit_group to a table. */
5119
5120static int
5121add_type_unit_group_to_table (void **slot, void *datum)
5122{
5123 struct type_unit_group *tu_group = *slot;
5124 struct type_unit_group ***datap = datum;
5125
5126 **datap = tu_group;
5127 ++*datap;
5128
5129 return 1;
5130}
5131
5132/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5133 each one passing FUNC,DATA.
5134
5135 The efficiency is because we sort TUs by the abbrev table they use and
5136 only read each abbrev table once. In one program there are 200K TUs
5137 sharing 8K abbrev tables.
5138
5139 The main purpose of this function is to support building the
5140 dwarf2_per_objfile->type_unit_groups table.
5141 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5142 can collapse the search space by grouping them by stmt_list.
5143 The savings can be significant, in the same program from above the 200K TUs
5144 share 8K stmt_list tables.
5145
5146 FUNC is expected to call get_type_unit_group, which will create the
5147 struct type_unit_group if necessary and add it to
5148 dwarf2_per_objfile->type_unit_groups. */
5149
5150static void
5151build_type_unit_groups (die_reader_func_ftype *func, void *data)
5152{
5153 struct objfile *objfile = dwarf2_per_objfile->objfile;
5154 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5155 struct cleanup *cleanups;
5156 struct abbrev_table *abbrev_table;
5157 sect_offset abbrev_offset;
5158 struct tu_abbrev_offset *sorted_by_abbrev;
5159 struct type_unit_group **iter;
5160 int i;
5161
5162 /* It's up to the caller to not call us multiple times. */
5163 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5164
5165 if (dwarf2_per_objfile->n_type_units == 0)
5166 return;
5167
5168 /* TUs typically share abbrev tables, and there can be way more TUs than
5169 abbrev tables. Sort by abbrev table to reduce the number of times we
5170 read each abbrev table in.
5171 Alternatives are to punt or to maintain a cache of abbrev tables.
5172 This is simpler and efficient enough for now.
5173
5174 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5175 symtab to use). Typically TUs with the same abbrev offset have the same
5176 stmt_list value too so in practice this should work well.
5177
5178 The basic algorithm here is:
5179
5180 sort TUs by abbrev table
5181 for each TU with same abbrev table:
5182 read abbrev table if first user
5183 read TU top level DIE
5184 [IWBN if DWO skeletons had DW_AT_stmt_list]
5185 call FUNC */
5186
5187 if (dwarf2_read_debug)
5188 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5189
5190 /* Sort in a separate table to maintain the order of all_type_units
5191 for .gdb_index: TU indices directly index all_type_units. */
5192 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5193 dwarf2_per_objfile->n_type_units);
5194 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5195 {
5196 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5197
5198 sorted_by_abbrev[i].sig_type = sig_type;
5199 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5200 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5201 sig_type->per_cu.offset);
5202 }
5203 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5204 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5205 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5206
094b34ac
DE
5207 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5208 called any number of times, so we don't reset tu_stats here. */
5209
f4dc4d17
DE
5210 abbrev_offset.sect_off = ~(unsigned) 0;
5211 abbrev_table = NULL;
5212 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5213
5214 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5215 {
5216 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5217
5218 /* Switch to the next abbrev table if necessary. */
5219 if (abbrev_table == NULL
5220 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5221 {
5222 if (abbrev_table != NULL)
5223 {
5224 abbrev_table_free (abbrev_table);
5225 /* Reset to NULL in case abbrev_table_read_table throws
5226 an error: abbrev_table_free_cleanup will get called. */
5227 abbrev_table = NULL;
5228 }
5229 abbrev_offset = tu->abbrev_offset;
5230 abbrev_table =
5231 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5232 abbrev_offset);
5233 ++tu_stats->nr_uniq_abbrev_tables;
5234 }
5235
5236 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5237 func, data);
5238 }
5239
5240 /* Create a vector of pointers to primary type units to make it easy to
5241 iterate over them and CUs. See dw2_get_primary_cu. */
5242 dwarf2_per_objfile->n_type_unit_groups =
5243 htab_elements (dwarf2_per_objfile->type_unit_groups);
5244 dwarf2_per_objfile->all_type_unit_groups =
5245 obstack_alloc (&objfile->objfile_obstack,
5246 dwarf2_per_objfile->n_type_unit_groups
5247 * sizeof (struct type_unit_group *));
5248 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5249 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5250 add_type_unit_group_to_table, &iter);
5251 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5252 == dwarf2_per_objfile->n_type_unit_groups);
5253
5254 do_cleanups (cleanups);
5255
5256 if (dwarf2_read_debug)
5257 {
5258 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5259 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5260 dwarf2_per_objfile->n_type_units);
5261 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5262 tu_stats->nr_uniq_abbrev_tables);
5263 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5264 tu_stats->nr_symtabs);
5265 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5266 tu_stats->nr_symtab_sharers);
5267 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5268 tu_stats->nr_stmt_less_type_units);
5269 }
5270}
0018ea6f
DE
5271\f
5272/* Partial symbol tables. */
5273
5274/* Create a psymtab named NAME and assign it to PER_CU.
5275
5276 The caller must fill in the following details:
5277 dirname, textlow, texthigh. */
5278
5279static struct partial_symtab *
5280create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5281{
5282 struct objfile *objfile = per_cu->objfile;
5283 struct partial_symtab *pst;
5284
5285 pst = start_psymtab_common (objfile, objfile->section_offsets,
5286 name, 0,
5287 objfile->global_psymbols.next,
5288 objfile->static_psymbols.next);
5289
5290 pst->psymtabs_addrmap_supported = 1;
5291
5292 /* This is the glue that links PST into GDB's symbol API. */
5293 pst->read_symtab_private = per_cu;
5294 pst->read_symtab = dwarf2_read_symtab;
5295 per_cu->v.psymtab = pst;
5296
5297 return pst;
5298}
5299
5300/* die_reader_func for process_psymtab_comp_unit. */
5301
5302static void
5303process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5304 gdb_byte *info_ptr,
5305 struct die_info *comp_unit_die,
5306 int has_children,
5307 void *data)
5308{
5309 struct dwarf2_cu *cu = reader->cu;
5310 struct objfile *objfile = cu->objfile;
5311 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5312 struct attribute *attr;
5313 CORE_ADDR baseaddr;
5314 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5315 struct partial_symtab *pst;
5316 int has_pc_info;
5317 const char *filename;
5318 int *want_partial_unit_ptr = data;
5319
5320 if (comp_unit_die->tag == DW_TAG_partial_unit
5321 && (want_partial_unit_ptr == NULL
5322 || !*want_partial_unit_ptr))
5323 return;
5324
5325 gdb_assert (! per_cu->is_debug_types);
5326
5327 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5328
5329 cu->list_in_scope = &file_symbols;
5330
5331 /* Allocate a new partial symbol table structure. */
5332 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5333 if (attr == NULL || !DW_STRING (attr))
5334 filename = "";
5335 else
5336 filename = DW_STRING (attr);
5337
5338 pst = create_partial_symtab (per_cu, filename);
5339
5340 /* This must be done before calling dwarf2_build_include_psymtabs. */
5341 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5342 if (attr != NULL)
5343 pst->dirname = DW_STRING (attr);
5344
5345 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5346
5347 dwarf2_find_base_address (comp_unit_die, cu);
5348
5349 /* Possibly set the default values of LOWPC and HIGHPC from
5350 `DW_AT_ranges'. */
5351 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5352 &best_highpc, cu, pst);
5353 if (has_pc_info == 1 && best_lowpc < best_highpc)
5354 /* Store the contiguous range if it is not empty; it can be empty for
5355 CUs with no code. */
5356 addrmap_set_empty (objfile->psymtabs_addrmap,
5357 best_lowpc + baseaddr,
5358 best_highpc + baseaddr - 1, pst);
5359
5360 /* Check if comp unit has_children.
5361 If so, read the rest of the partial symbols from this comp unit.
5362 If not, there's no more debug_info for this comp unit. */
5363 if (has_children)
5364 {
5365 struct partial_die_info *first_die;
5366 CORE_ADDR lowpc, highpc;
5367
5368 lowpc = ((CORE_ADDR) -1);
5369 highpc = ((CORE_ADDR) 0);
5370
5371 first_die = load_partial_dies (reader, info_ptr, 1);
5372
5373 scan_partial_symbols (first_die, &lowpc, &highpc,
5374 ! has_pc_info, cu);
5375
5376 /* If we didn't find a lowpc, set it to highpc to avoid
5377 complaints from `maint check'. */
5378 if (lowpc == ((CORE_ADDR) -1))
5379 lowpc = highpc;
5380
5381 /* If the compilation unit didn't have an explicit address range,
5382 then use the information extracted from its child dies. */
5383 if (! has_pc_info)
5384 {
5385 best_lowpc = lowpc;
5386 best_highpc = highpc;
5387 }
5388 }
5389 pst->textlow = best_lowpc + baseaddr;
5390 pst->texthigh = best_highpc + baseaddr;
5391
5392 pst->n_global_syms = objfile->global_psymbols.next -
5393 (objfile->global_psymbols.list + pst->globals_offset);
5394 pst->n_static_syms = objfile->static_psymbols.next -
5395 (objfile->static_psymbols.list + pst->statics_offset);
5396 sort_pst_symbols (objfile, pst);
5397
5398 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5399 {
5400 int i;
5401 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5402 struct dwarf2_per_cu_data *iter;
5403
5404 /* Fill in 'dependencies' here; we fill in 'users' in a
5405 post-pass. */
5406 pst->number_of_dependencies = len;
5407 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5408 len * sizeof (struct symtab *));
5409 for (i = 0;
5410 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5411 i, iter);
5412 ++i)
5413 pst->dependencies[i] = iter->v.psymtab;
5414
5415 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5416 }
5417
5418 /* Get the list of files included in the current compilation unit,
5419 and build a psymtab for each of them. */
5420 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5421
5422 if (dwarf2_read_debug)
5423 {
5424 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5425
5426 fprintf_unfiltered (gdb_stdlog,
5427 "Psymtab for %s unit @0x%x: %s - %s"
5428 ", %d global, %d static syms\n",
5429 per_cu->is_debug_types ? "type" : "comp",
5430 per_cu->offset.sect_off,
5431 paddress (gdbarch, pst->textlow),
5432 paddress (gdbarch, pst->texthigh),
5433 pst->n_global_syms, pst->n_static_syms);
5434 }
5435}
5436
5437/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5438 Process compilation unit THIS_CU for a psymtab. */
5439
5440static void
5441process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5442 int want_partial_unit)
5443{
5444 /* If this compilation unit was already read in, free the
5445 cached copy in order to read it in again. This is
5446 necessary because we skipped some symbols when we first
5447 read in the compilation unit (see load_partial_dies).
5448 This problem could be avoided, but the benefit is unclear. */
5449 if (this_cu->cu != NULL)
5450 free_one_cached_comp_unit (this_cu);
5451
5452 gdb_assert (! this_cu->is_debug_types);
5453 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5454 process_psymtab_comp_unit_reader,
5455 &want_partial_unit);
5456
5457 /* Age out any secondary CUs. */
5458 age_cached_comp_units ();
5459}
f4dc4d17
DE
5460
5461/* Reader function for build_type_psymtabs. */
5462
5463static void
5464build_type_psymtabs_reader (const struct die_reader_specs *reader,
5465 gdb_byte *info_ptr,
5466 struct die_info *type_unit_die,
5467 int has_children,
5468 void *data)
5469{
5470 struct objfile *objfile = dwarf2_per_objfile->objfile;
5471 struct dwarf2_cu *cu = reader->cu;
5472 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5473 struct signatured_type *sig_type;
f4dc4d17
DE
5474 struct type_unit_group *tu_group;
5475 struct attribute *attr;
5476 struct partial_die_info *first_die;
5477 CORE_ADDR lowpc, highpc;
5478 struct partial_symtab *pst;
5479
5480 gdb_assert (data == NULL);
0186c6a7
DE
5481 gdb_assert (per_cu->is_debug_types);
5482 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5483
5484 if (! has_children)
5485 return;
5486
5487 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5488 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5489
0186c6a7 5490 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5491
5492 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5493 cu->list_in_scope = &file_symbols;
5494 pst = create_partial_symtab (per_cu, "");
5495 pst->anonymous = 1;
5496
5497 first_die = load_partial_dies (reader, info_ptr, 1);
5498
5499 lowpc = (CORE_ADDR) -1;
5500 highpc = (CORE_ADDR) 0;
5501 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5502
5503 pst->n_global_syms = objfile->global_psymbols.next -
5504 (objfile->global_psymbols.list + pst->globals_offset);
5505 pst->n_static_syms = objfile->static_psymbols.next -
5506 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5507 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5508}
5509
5510/* Traversal function for build_type_psymtabs. */
5511
5512static int
5513build_type_psymtab_dependencies (void **slot, void *info)
5514{
5515 struct objfile *objfile = dwarf2_per_objfile->objfile;
5516 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5517 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5518 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5519 int len = VEC_length (sig_type_ptr, tu_group->tus);
5520 struct signatured_type *iter;
f4dc4d17
DE
5521 int i;
5522
5523 gdb_assert (len > 0);
0186c6a7 5524 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5525
5526 pst->number_of_dependencies = len;
5527 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5528 len * sizeof (struct psymtab *));
5529 for (i = 0;
0186c6a7 5530 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5531 ++i)
5532 {
0186c6a7
DE
5533 gdb_assert (iter->per_cu.is_debug_types);
5534 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5535 iter->type_unit_group = tu_group;
f4dc4d17
DE
5536 }
5537
0186c6a7 5538 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5539
5540 return 1;
5541}
5542
5543/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5544 Build partial symbol tables for the .debug_types comp-units. */
5545
5546static void
5547build_type_psymtabs (struct objfile *objfile)
5548{
0e50663e 5549 if (! create_all_type_units (objfile))
348e048f
DE
5550 return;
5551
f4dc4d17
DE
5552 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5553
5554 /* Now that all TUs have been processed we can fill in the dependencies. */
5555 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5556 build_type_psymtab_dependencies, NULL);
348e048f
DE
5557}
5558
60606b2c
TT
5559/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5560
5561static void
5562psymtabs_addrmap_cleanup (void *o)
5563{
5564 struct objfile *objfile = o;
ec61707d 5565
60606b2c
TT
5566 objfile->psymtabs_addrmap = NULL;
5567}
5568
95554aad
TT
5569/* Compute the 'user' field for each psymtab in OBJFILE. */
5570
5571static void
5572set_partial_user (struct objfile *objfile)
5573{
5574 int i;
5575
5576 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5577 {
5578 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5579 struct partial_symtab *pst = per_cu->v.psymtab;
5580 int j;
5581
36586728
TT
5582 if (pst == NULL)
5583 continue;
5584
95554aad
TT
5585 for (j = 0; j < pst->number_of_dependencies; ++j)
5586 {
5587 /* Set the 'user' field only if it is not already set. */
5588 if (pst->dependencies[j]->user == NULL)
5589 pst->dependencies[j]->user = pst;
5590 }
5591 }
5592}
5593
93311388
DE
5594/* Build the partial symbol table by doing a quick pass through the
5595 .debug_info and .debug_abbrev sections. */
72bf9492 5596
93311388 5597static void
c67a9c90 5598dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5599{
60606b2c
TT
5600 struct cleanup *back_to, *addrmap_cleanup;
5601 struct obstack temp_obstack;
21b2bd31 5602 int i;
93311388 5603
45cfd468
DE
5604 if (dwarf2_read_debug)
5605 {
5606 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5607 objfile->name);
5608 }
5609
98bfdba5
PA
5610 dwarf2_per_objfile->reading_partial_symbols = 1;
5611
be391dca 5612 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5613
93311388
DE
5614 /* Any cached compilation units will be linked by the per-objfile
5615 read_in_chain. Make sure to free them when we're done. */
5616 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5617
348e048f
DE
5618 build_type_psymtabs (objfile);
5619
93311388 5620 create_all_comp_units (objfile);
c906108c 5621
60606b2c
TT
5622 /* Create a temporary address map on a temporary obstack. We later
5623 copy this to the final obstack. */
5624 obstack_init (&temp_obstack);
5625 make_cleanup_obstack_free (&temp_obstack);
5626 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5627 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5628
21b2bd31 5629 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5630 {
21b2bd31 5631 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5632
95554aad 5633 process_psymtab_comp_unit (per_cu, 0);
c906108c 5634 }
ff013f42 5635
95554aad
TT
5636 set_partial_user (objfile);
5637
ff013f42
JK
5638 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5639 &objfile->objfile_obstack);
60606b2c 5640 discard_cleanups (addrmap_cleanup);
ff013f42 5641
ae038cb0 5642 do_cleanups (back_to);
45cfd468
DE
5643
5644 if (dwarf2_read_debug)
5645 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5646 objfile->name);
ae038cb0
DJ
5647}
5648
3019eac3 5649/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5650
5651static void
dee91e82
DE
5652load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5653 gdb_byte *info_ptr,
5654 struct die_info *comp_unit_die,
5655 int has_children,
5656 void *data)
ae038cb0 5657{
dee91e82 5658 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5659
95554aad 5660 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5661
ae038cb0
DJ
5662 /* Check if comp unit has_children.
5663 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5664 If not, there's no more debug_info for this comp unit. */
d85a05f0 5665 if (has_children)
dee91e82
DE
5666 load_partial_dies (reader, info_ptr, 0);
5667}
98bfdba5 5668
dee91e82
DE
5669/* Load the partial DIEs for a secondary CU into memory.
5670 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5671
dee91e82
DE
5672static void
5673load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5674{
f4dc4d17
DE
5675 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5676 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5677}
5678
ae038cb0 5679static void
36586728
TT
5680read_comp_units_from_section (struct objfile *objfile,
5681 struct dwarf2_section_info *section,
5682 unsigned int is_dwz,
5683 int *n_allocated,
5684 int *n_comp_units,
5685 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5686{
be391dca 5687 gdb_byte *info_ptr;
36586728 5688 bfd *abfd = section->asection->owner;
be391dca 5689
36586728 5690 dwarf2_read_section (objfile, section);
ae038cb0 5691
36586728 5692 info_ptr = section->buffer;
6e70227d 5693
36586728 5694 while (info_ptr < section->buffer + section->size)
ae038cb0 5695 {
c764a876 5696 unsigned int length, initial_length_size;
ae038cb0 5697 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5698 sect_offset offset;
ae038cb0 5699
36586728 5700 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5701
5702 /* Read just enough information to find out where the next
5703 compilation unit is. */
36586728 5704 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5705
5706 /* Save the compilation unit for later lookup. */
5707 this_cu = obstack_alloc (&objfile->objfile_obstack,
5708 sizeof (struct dwarf2_per_cu_data));
5709 memset (this_cu, 0, sizeof (*this_cu));
5710 this_cu->offset = offset;
c764a876 5711 this_cu->length = length + initial_length_size;
36586728 5712 this_cu->is_dwz = is_dwz;
9291a0cd 5713 this_cu->objfile = objfile;
8a0459fd 5714 this_cu->section = section;
ae038cb0 5715
36586728 5716 if (*n_comp_units == *n_allocated)
ae038cb0 5717 {
36586728
TT
5718 *n_allocated *= 2;
5719 *all_comp_units = xrealloc (*all_comp_units,
5720 *n_allocated
5721 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5722 }
36586728
TT
5723 (*all_comp_units)[*n_comp_units] = this_cu;
5724 ++*n_comp_units;
ae038cb0
DJ
5725
5726 info_ptr = info_ptr + this_cu->length;
5727 }
36586728
TT
5728}
5729
5730/* Create a list of all compilation units in OBJFILE.
5731 This is only done for -readnow and building partial symtabs. */
5732
5733static void
5734create_all_comp_units (struct objfile *objfile)
5735{
5736 int n_allocated;
5737 int n_comp_units;
5738 struct dwarf2_per_cu_data **all_comp_units;
5739
5740 n_comp_units = 0;
5741 n_allocated = 10;
5742 all_comp_units = xmalloc (n_allocated
5743 * sizeof (struct dwarf2_per_cu_data *));
5744
5745 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5746 &n_allocated, &n_comp_units, &all_comp_units);
5747
5748 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5749 {
5750 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5751
5752 read_comp_units_from_section (objfile, &dwz->info, 1,
5753 &n_allocated, &n_comp_units,
5754 &all_comp_units);
5755 }
ae038cb0
DJ
5756
5757 dwarf2_per_objfile->all_comp_units
5758 = obstack_alloc (&objfile->objfile_obstack,
5759 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5760 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5761 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5762 xfree (all_comp_units);
5763 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5764}
5765
5734ee8b
DJ
5766/* Process all loaded DIEs for compilation unit CU, starting at
5767 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5768 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5769 DW_AT_ranges). If NEED_PC is set, then this function will set
5770 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5771 and record the covered ranges in the addrmap. */
c906108c 5772
72bf9492
DJ
5773static void
5774scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5775 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5776{
72bf9492 5777 struct partial_die_info *pdi;
c906108c 5778
91c24f0a
DC
5779 /* Now, march along the PDI's, descending into ones which have
5780 interesting children but skipping the children of the other ones,
5781 until we reach the end of the compilation unit. */
c906108c 5782
72bf9492 5783 pdi = first_die;
91c24f0a 5784
72bf9492
DJ
5785 while (pdi != NULL)
5786 {
5787 fixup_partial_die (pdi, cu);
c906108c 5788
f55ee35c 5789 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5790 children, so we need to look at them. Ditto for anonymous
5791 enums. */
933c6fe4 5792
72bf9492 5793 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5794 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5795 || pdi->tag == DW_TAG_imported_unit)
c906108c 5796 {
72bf9492 5797 switch (pdi->tag)
c906108c
SS
5798 {
5799 case DW_TAG_subprogram:
5734ee8b 5800 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5801 break;
72929c62 5802 case DW_TAG_constant:
c906108c
SS
5803 case DW_TAG_variable:
5804 case DW_TAG_typedef:
91c24f0a 5805 case DW_TAG_union_type:
72bf9492 5806 if (!pdi->is_declaration)
63d06c5c 5807 {
72bf9492 5808 add_partial_symbol (pdi, cu);
63d06c5c
DC
5809 }
5810 break;
c906108c 5811 case DW_TAG_class_type:
680b30c7 5812 case DW_TAG_interface_type:
c906108c 5813 case DW_TAG_structure_type:
72bf9492 5814 if (!pdi->is_declaration)
c906108c 5815 {
72bf9492 5816 add_partial_symbol (pdi, cu);
c906108c
SS
5817 }
5818 break;
91c24f0a 5819 case DW_TAG_enumeration_type:
72bf9492
DJ
5820 if (!pdi->is_declaration)
5821 add_partial_enumeration (pdi, cu);
c906108c
SS
5822 break;
5823 case DW_TAG_base_type:
a02abb62 5824 case DW_TAG_subrange_type:
c906108c 5825 /* File scope base type definitions are added to the partial
c5aa993b 5826 symbol table. */
72bf9492 5827 add_partial_symbol (pdi, cu);
c906108c 5828 break;
d9fa45fe 5829 case DW_TAG_namespace:
5734ee8b 5830 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5831 break;
5d7cb8df
JK
5832 case DW_TAG_module:
5833 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5834 break;
95554aad
TT
5835 case DW_TAG_imported_unit:
5836 {
5837 struct dwarf2_per_cu_data *per_cu;
5838
f4dc4d17
DE
5839 /* For now we don't handle imported units in type units. */
5840 if (cu->per_cu->is_debug_types)
5841 {
5842 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5843 " supported in type units [in module %s]"),
5844 cu->objfile->name);
5845 }
5846
95554aad 5847 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5848 pdi->is_dwz,
95554aad
TT
5849 cu->objfile);
5850
5851 /* Go read the partial unit, if needed. */
5852 if (per_cu->v.psymtab == NULL)
5853 process_psymtab_comp_unit (per_cu, 1);
5854
f4dc4d17 5855 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5856 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5857 }
5858 break;
c906108c
SS
5859 default:
5860 break;
5861 }
5862 }
5863
72bf9492
DJ
5864 /* If the die has a sibling, skip to the sibling. */
5865
5866 pdi = pdi->die_sibling;
5867 }
5868}
5869
5870/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5871
72bf9492 5872 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5873 name is concatenated with "::" and the partial DIE's name. For
5874 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5875 Enumerators are an exception; they use the scope of their parent
5876 enumeration type, i.e. the name of the enumeration type is not
5877 prepended to the enumerator.
91c24f0a 5878
72bf9492
DJ
5879 There are two complexities. One is DW_AT_specification; in this
5880 case "parent" means the parent of the target of the specification,
5881 instead of the direct parent of the DIE. The other is compilers
5882 which do not emit DW_TAG_namespace; in this case we try to guess
5883 the fully qualified name of structure types from their members'
5884 linkage names. This must be done using the DIE's children rather
5885 than the children of any DW_AT_specification target. We only need
5886 to do this for structures at the top level, i.e. if the target of
5887 any DW_AT_specification (if any; otherwise the DIE itself) does not
5888 have a parent. */
5889
5890/* Compute the scope prefix associated with PDI's parent, in
5891 compilation unit CU. The result will be allocated on CU's
5892 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5893 field. NULL is returned if no prefix is necessary. */
15d034d0 5894static const char *
72bf9492
DJ
5895partial_die_parent_scope (struct partial_die_info *pdi,
5896 struct dwarf2_cu *cu)
5897{
15d034d0 5898 const char *grandparent_scope;
72bf9492 5899 struct partial_die_info *parent, *real_pdi;
91c24f0a 5900
72bf9492
DJ
5901 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5902 then this means the parent of the specification DIE. */
5903
5904 real_pdi = pdi;
72bf9492 5905 while (real_pdi->has_specification)
36586728
TT
5906 real_pdi = find_partial_die (real_pdi->spec_offset,
5907 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5908
5909 parent = real_pdi->die_parent;
5910 if (parent == NULL)
5911 return NULL;
5912
5913 if (parent->scope_set)
5914 return parent->scope;
5915
5916 fixup_partial_die (parent, cu);
5917
10b3939b 5918 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5919
acebe513
UW
5920 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5921 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5922 Work around this problem here. */
5923 if (cu->language == language_cplus
6e70227d 5924 && parent->tag == DW_TAG_namespace
acebe513
UW
5925 && strcmp (parent->name, "::") == 0
5926 && grandparent_scope == NULL)
5927 {
5928 parent->scope = NULL;
5929 parent->scope_set = 1;
5930 return NULL;
5931 }
5932
9c6c53f7
SA
5933 if (pdi->tag == DW_TAG_enumerator)
5934 /* Enumerators should not get the name of the enumeration as a prefix. */
5935 parent->scope = grandparent_scope;
5936 else if (parent->tag == DW_TAG_namespace
f55ee35c 5937 || parent->tag == DW_TAG_module
72bf9492
DJ
5938 || parent->tag == DW_TAG_structure_type
5939 || parent->tag == DW_TAG_class_type
680b30c7 5940 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5941 || parent->tag == DW_TAG_union_type
5942 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5943 {
5944 if (grandparent_scope == NULL)
5945 parent->scope = parent->name;
5946 else
3e43a32a
MS
5947 parent->scope = typename_concat (&cu->comp_unit_obstack,
5948 grandparent_scope,
f55ee35c 5949 parent->name, 0, cu);
72bf9492 5950 }
72bf9492
DJ
5951 else
5952 {
5953 /* FIXME drow/2004-04-01: What should we be doing with
5954 function-local names? For partial symbols, we should probably be
5955 ignoring them. */
5956 complaint (&symfile_complaints,
e2e0b3e5 5957 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5958 parent->tag, pdi->offset.sect_off);
72bf9492 5959 parent->scope = grandparent_scope;
c906108c
SS
5960 }
5961
72bf9492
DJ
5962 parent->scope_set = 1;
5963 return parent->scope;
5964}
5965
5966/* Return the fully scoped name associated with PDI, from compilation unit
5967 CU. The result will be allocated with malloc. */
4568ecf9 5968
72bf9492
DJ
5969static char *
5970partial_die_full_name (struct partial_die_info *pdi,
5971 struct dwarf2_cu *cu)
5972{
15d034d0 5973 const char *parent_scope;
72bf9492 5974
98bfdba5
PA
5975 /* If this is a template instantiation, we can not work out the
5976 template arguments from partial DIEs. So, unfortunately, we have
5977 to go through the full DIEs. At least any work we do building
5978 types here will be reused if full symbols are loaded later. */
5979 if (pdi->has_template_arguments)
5980 {
5981 fixup_partial_die (pdi, cu);
5982
5983 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5984 {
5985 struct die_info *die;
5986 struct attribute attr;
5987 struct dwarf2_cu *ref_cu = cu;
5988
b64f50a1 5989 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5990 attr.name = 0;
5991 attr.form = DW_FORM_ref_addr;
4568ecf9 5992 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5993 die = follow_die_ref (NULL, &attr, &ref_cu);
5994
5995 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5996 }
5997 }
5998
72bf9492
DJ
5999 parent_scope = partial_die_parent_scope (pdi, cu);
6000 if (parent_scope == NULL)
6001 return NULL;
6002 else
f55ee35c 6003 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6004}
6005
6006static void
72bf9492 6007add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6008{
e7c27a73 6009 struct objfile *objfile = cu->objfile;
c906108c 6010 CORE_ADDR addr = 0;
15d034d0 6011 const char *actual_name = NULL;
e142c38c 6012 CORE_ADDR baseaddr;
15d034d0 6013 char *built_actual_name;
e142c38c
DJ
6014
6015 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6016
15d034d0
TT
6017 built_actual_name = partial_die_full_name (pdi, cu);
6018 if (built_actual_name != NULL)
6019 actual_name = built_actual_name;
63d06c5c 6020
72bf9492
DJ
6021 if (actual_name == NULL)
6022 actual_name = pdi->name;
6023
c906108c
SS
6024 switch (pdi->tag)
6025 {
6026 case DW_TAG_subprogram:
2cfa0c8d 6027 if (pdi->is_external || cu->language == language_ada)
c906108c 6028 {
2cfa0c8d
JB
6029 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6030 of the global scope. But in Ada, we want to be able to access
6031 nested procedures globally. So all Ada subprograms are stored
6032 in the global scope. */
f47fb265 6033 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6034 mst_text, objfile); */
f47fb265 6035 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6036 built_actual_name != NULL,
f47fb265
MS
6037 VAR_DOMAIN, LOC_BLOCK,
6038 &objfile->global_psymbols,
6039 0, pdi->lowpc + baseaddr,
6040 cu->language, objfile);
c906108c
SS
6041 }
6042 else
6043 {
f47fb265 6044 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6045 mst_file_text, objfile); */
f47fb265 6046 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6047 built_actual_name != NULL,
f47fb265
MS
6048 VAR_DOMAIN, LOC_BLOCK,
6049 &objfile->static_psymbols,
6050 0, pdi->lowpc + baseaddr,
6051 cu->language, objfile);
c906108c
SS
6052 }
6053 break;
72929c62
JB
6054 case DW_TAG_constant:
6055 {
6056 struct psymbol_allocation_list *list;
6057
6058 if (pdi->is_external)
6059 list = &objfile->global_psymbols;
6060 else
6061 list = &objfile->static_psymbols;
f47fb265 6062 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6063 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6064 list, 0, 0, cu->language, objfile);
72929c62
JB
6065 }
6066 break;
c906108c 6067 case DW_TAG_variable:
95554aad
TT
6068 if (pdi->d.locdesc)
6069 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6070
95554aad 6071 if (pdi->d.locdesc
caac4577
JG
6072 && addr == 0
6073 && !dwarf2_per_objfile->has_section_at_zero)
6074 {
6075 /* A global or static variable may also have been stripped
6076 out by the linker if unused, in which case its address
6077 will be nullified; do not add such variables into partial
6078 symbol table then. */
6079 }
6080 else if (pdi->is_external)
c906108c
SS
6081 {
6082 /* Global Variable.
6083 Don't enter into the minimal symbol tables as there is
6084 a minimal symbol table entry from the ELF symbols already.
6085 Enter into partial symbol table if it has a location
6086 descriptor or a type.
6087 If the location descriptor is missing, new_symbol will create
6088 a LOC_UNRESOLVED symbol, the address of the variable will then
6089 be determined from the minimal symbol table whenever the variable
6090 is referenced.
6091 The address for the partial symbol table entry is not
6092 used by GDB, but it comes in handy for debugging partial symbol
6093 table building. */
6094
95554aad 6095 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6096 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6097 built_actual_name != NULL,
f47fb265
MS
6098 VAR_DOMAIN, LOC_STATIC,
6099 &objfile->global_psymbols,
6100 0, addr + baseaddr,
6101 cu->language, objfile);
c906108c
SS
6102 }
6103 else
6104 {
0963b4bd 6105 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6106 if (pdi->d.locdesc == NULL)
decbce07 6107 {
15d034d0 6108 xfree (built_actual_name);
decbce07
MS
6109 return;
6110 }
f47fb265 6111 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6112 mst_file_data, objfile); */
f47fb265 6113 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6114 built_actual_name != NULL,
f47fb265
MS
6115 VAR_DOMAIN, LOC_STATIC,
6116 &objfile->static_psymbols,
6117 0, addr + baseaddr,
6118 cu->language, objfile);
c906108c
SS
6119 }
6120 break;
6121 case DW_TAG_typedef:
6122 case DW_TAG_base_type:
a02abb62 6123 case DW_TAG_subrange_type:
38d518c9 6124 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6125 built_actual_name != NULL,
176620f1 6126 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6127 &objfile->static_psymbols,
e142c38c 6128 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6129 break;
72bf9492
DJ
6130 case DW_TAG_namespace:
6131 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6132 built_actual_name != NULL,
72bf9492
DJ
6133 VAR_DOMAIN, LOC_TYPEDEF,
6134 &objfile->global_psymbols,
6135 0, (CORE_ADDR) 0, cu->language, objfile);
6136 break;
c906108c 6137 case DW_TAG_class_type:
680b30c7 6138 case DW_TAG_interface_type:
c906108c
SS
6139 case DW_TAG_structure_type:
6140 case DW_TAG_union_type:
6141 case DW_TAG_enumeration_type:
fa4028e9
JB
6142 /* Skip external references. The DWARF standard says in the section
6143 about "Structure, Union, and Class Type Entries": "An incomplete
6144 structure, union or class type is represented by a structure,
6145 union or class entry that does not have a byte size attribute
6146 and that has a DW_AT_declaration attribute." */
6147 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6148 {
15d034d0 6149 xfree (built_actual_name);
decbce07
MS
6150 return;
6151 }
fa4028e9 6152
63d06c5c
DC
6153 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6154 static vs. global. */
38d518c9 6155 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6156 built_actual_name != NULL,
176620f1 6157 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6158 (cu->language == language_cplus
6159 || cu->language == language_java)
63d06c5c
DC
6160 ? &objfile->global_psymbols
6161 : &objfile->static_psymbols,
e142c38c 6162 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6163
c906108c
SS
6164 break;
6165 case DW_TAG_enumerator:
38d518c9 6166 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6167 built_actual_name != NULL,
176620f1 6168 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6169 (cu->language == language_cplus
6170 || cu->language == language_java)
f6fe98ef
DJ
6171 ? &objfile->global_psymbols
6172 : &objfile->static_psymbols,
e142c38c 6173 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6174 break;
6175 default:
6176 break;
6177 }
5c4e30ca 6178
15d034d0 6179 xfree (built_actual_name);
c906108c
SS
6180}
6181
5c4e30ca
DC
6182/* Read a partial die corresponding to a namespace; also, add a symbol
6183 corresponding to that namespace to the symbol table. NAMESPACE is
6184 the name of the enclosing namespace. */
91c24f0a 6185
72bf9492
DJ
6186static void
6187add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6188 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6189 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6190{
72bf9492 6191 /* Add a symbol for the namespace. */
e7c27a73 6192
72bf9492 6193 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6194
6195 /* Now scan partial symbols in that namespace. */
6196
91c24f0a 6197 if (pdi->has_children)
5734ee8b 6198 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6199}
6200
5d7cb8df
JK
6201/* Read a partial die corresponding to a Fortran module. */
6202
6203static void
6204add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6205 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6206{
f55ee35c 6207 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6208
6209 if (pdi->has_children)
6210 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6211}
6212
bc30ff58
JB
6213/* Read a partial die corresponding to a subprogram and create a partial
6214 symbol for that subprogram. When the CU language allows it, this
6215 routine also defines a partial symbol for each nested subprogram
6216 that this subprogram contains.
6e70227d 6217
bc30ff58
JB
6218 DIE my also be a lexical block, in which case we simply search
6219 recursively for suprograms defined inside that lexical block.
6220 Again, this is only performed when the CU language allows this
6221 type of definitions. */
6222
6223static void
6224add_partial_subprogram (struct partial_die_info *pdi,
6225 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6226 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6227{
6228 if (pdi->tag == DW_TAG_subprogram)
6229 {
6230 if (pdi->has_pc_info)
6231 {
6232 if (pdi->lowpc < *lowpc)
6233 *lowpc = pdi->lowpc;
6234 if (pdi->highpc > *highpc)
6235 *highpc = pdi->highpc;
5734ee8b
DJ
6236 if (need_pc)
6237 {
6238 CORE_ADDR baseaddr;
6239 struct objfile *objfile = cu->objfile;
6240
6241 baseaddr = ANOFFSET (objfile->section_offsets,
6242 SECT_OFF_TEXT (objfile));
6243 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6244 pdi->lowpc + baseaddr,
6245 pdi->highpc - 1 + baseaddr,
9291a0cd 6246 cu->per_cu->v.psymtab);
5734ee8b 6247 }
481860b3
GB
6248 }
6249
6250 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6251 {
bc30ff58 6252 if (!pdi->is_declaration)
e8d05480
JB
6253 /* Ignore subprogram DIEs that do not have a name, they are
6254 illegal. Do not emit a complaint at this point, we will
6255 do so when we convert this psymtab into a symtab. */
6256 if (pdi->name)
6257 add_partial_symbol (pdi, cu);
bc30ff58
JB
6258 }
6259 }
6e70227d 6260
bc30ff58
JB
6261 if (! pdi->has_children)
6262 return;
6263
6264 if (cu->language == language_ada)
6265 {
6266 pdi = pdi->die_child;
6267 while (pdi != NULL)
6268 {
6269 fixup_partial_die (pdi, cu);
6270 if (pdi->tag == DW_TAG_subprogram
6271 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6272 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6273 pdi = pdi->die_sibling;
6274 }
6275 }
6276}
6277
91c24f0a
DC
6278/* Read a partial die corresponding to an enumeration type. */
6279
72bf9492
DJ
6280static void
6281add_partial_enumeration (struct partial_die_info *enum_pdi,
6282 struct dwarf2_cu *cu)
91c24f0a 6283{
72bf9492 6284 struct partial_die_info *pdi;
91c24f0a
DC
6285
6286 if (enum_pdi->name != NULL)
72bf9492
DJ
6287 add_partial_symbol (enum_pdi, cu);
6288
6289 pdi = enum_pdi->die_child;
6290 while (pdi)
91c24f0a 6291 {
72bf9492 6292 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6293 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6294 else
72bf9492
DJ
6295 add_partial_symbol (pdi, cu);
6296 pdi = pdi->die_sibling;
91c24f0a 6297 }
91c24f0a
DC
6298}
6299
6caca83c
CC
6300/* Return the initial uleb128 in the die at INFO_PTR. */
6301
6302static unsigned int
6303peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6304{
6305 unsigned int bytes_read;
6306
6307 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6308}
6309
4bb7a0a7
DJ
6310/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6311 Return the corresponding abbrev, or NULL if the number is zero (indicating
6312 an empty DIE). In either case *BYTES_READ will be set to the length of
6313 the initial number. */
6314
6315static struct abbrev_info *
fe1b8b76 6316peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6317 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6318{
6319 bfd *abfd = cu->objfile->obfd;
6320 unsigned int abbrev_number;
6321 struct abbrev_info *abbrev;
6322
6323 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6324
6325 if (abbrev_number == 0)
6326 return NULL;
6327
433df2d4 6328 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6329 if (!abbrev)
6330 {
3e43a32a
MS
6331 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6332 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6333 }
6334
6335 return abbrev;
6336}
6337
93311388
DE
6338/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6339 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6340 DIE. Any children of the skipped DIEs will also be skipped. */
6341
fe1b8b76 6342static gdb_byte *
dee91e82 6343skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6344{
dee91e82 6345 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6346 struct abbrev_info *abbrev;
6347 unsigned int bytes_read;
6348
6349 while (1)
6350 {
6351 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6352 if (abbrev == NULL)
6353 return info_ptr + bytes_read;
6354 else
dee91e82 6355 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6356 }
6357}
6358
93311388
DE
6359/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6360 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6361 abbrev corresponding to that skipped uleb128 should be passed in
6362 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6363 children. */
6364
fe1b8b76 6365static gdb_byte *
dee91e82
DE
6366skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6367 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6368{
6369 unsigned int bytes_read;
6370 struct attribute attr;
dee91e82
DE
6371 bfd *abfd = reader->abfd;
6372 struct dwarf2_cu *cu = reader->cu;
6373 gdb_byte *buffer = reader->buffer;
f664829e
DE
6374 const gdb_byte *buffer_end = reader->buffer_end;
6375 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6376 unsigned int form, i;
6377
6378 for (i = 0; i < abbrev->num_attrs; i++)
6379 {
6380 /* The only abbrev we care about is DW_AT_sibling. */
6381 if (abbrev->attrs[i].name == DW_AT_sibling)
6382 {
dee91e82 6383 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6384 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6385 complaint (&symfile_complaints,
6386 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6387 else
b64f50a1 6388 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6389 }
6390
6391 /* If it isn't DW_AT_sibling, skip this attribute. */
6392 form = abbrev->attrs[i].form;
6393 skip_attribute:
6394 switch (form)
6395 {
4bb7a0a7 6396 case DW_FORM_ref_addr:
ae411497
TT
6397 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6398 and later it is offset sized. */
6399 if (cu->header.version == 2)
6400 info_ptr += cu->header.addr_size;
6401 else
6402 info_ptr += cu->header.offset_size;
6403 break;
36586728
TT
6404 case DW_FORM_GNU_ref_alt:
6405 info_ptr += cu->header.offset_size;
6406 break;
ae411497 6407 case DW_FORM_addr:
4bb7a0a7
DJ
6408 info_ptr += cu->header.addr_size;
6409 break;
6410 case DW_FORM_data1:
6411 case DW_FORM_ref1:
6412 case DW_FORM_flag:
6413 info_ptr += 1;
6414 break;
2dc7f7b3
TT
6415 case DW_FORM_flag_present:
6416 break;
4bb7a0a7
DJ
6417 case DW_FORM_data2:
6418 case DW_FORM_ref2:
6419 info_ptr += 2;
6420 break;
6421 case DW_FORM_data4:
6422 case DW_FORM_ref4:
6423 info_ptr += 4;
6424 break;
6425 case DW_FORM_data8:
6426 case DW_FORM_ref8:
55f1336d 6427 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6428 info_ptr += 8;
6429 break;
6430 case DW_FORM_string:
9b1c24c8 6431 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6432 info_ptr += bytes_read;
6433 break;
2dc7f7b3 6434 case DW_FORM_sec_offset:
4bb7a0a7 6435 case DW_FORM_strp:
36586728 6436 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6437 info_ptr += cu->header.offset_size;
6438 break;
2dc7f7b3 6439 case DW_FORM_exprloc:
4bb7a0a7
DJ
6440 case DW_FORM_block:
6441 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6442 info_ptr += bytes_read;
6443 break;
6444 case DW_FORM_block1:
6445 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6446 break;
6447 case DW_FORM_block2:
6448 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6449 break;
6450 case DW_FORM_block4:
6451 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6452 break;
6453 case DW_FORM_sdata:
6454 case DW_FORM_udata:
6455 case DW_FORM_ref_udata:
3019eac3
DE
6456 case DW_FORM_GNU_addr_index:
6457 case DW_FORM_GNU_str_index:
f664829e 6458 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6459 break;
6460 case DW_FORM_indirect:
6461 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6462 info_ptr += bytes_read;
6463 /* We need to continue parsing from here, so just go back to
6464 the top. */
6465 goto skip_attribute;
6466
6467 default:
3e43a32a
MS
6468 error (_("Dwarf Error: Cannot handle %s "
6469 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6470 dwarf_form_name (form),
6471 bfd_get_filename (abfd));
6472 }
6473 }
6474
6475 if (abbrev->has_children)
dee91e82 6476 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6477 else
6478 return info_ptr;
6479}
6480
93311388 6481/* Locate ORIG_PDI's sibling.
dee91e82 6482 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6483
fe1b8b76 6484static gdb_byte *
dee91e82
DE
6485locate_pdi_sibling (const struct die_reader_specs *reader,
6486 struct partial_die_info *orig_pdi,
6487 gdb_byte *info_ptr)
91c24f0a
DC
6488{
6489 /* Do we know the sibling already? */
72bf9492 6490
91c24f0a
DC
6491 if (orig_pdi->sibling)
6492 return orig_pdi->sibling;
6493
6494 /* Are there any children to deal with? */
6495
6496 if (!orig_pdi->has_children)
6497 return info_ptr;
6498
4bb7a0a7 6499 /* Skip the children the long way. */
91c24f0a 6500
dee91e82 6501 return skip_children (reader, info_ptr);
91c24f0a
DC
6502}
6503
257e7a09 6504/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6505 not NULL. */
c906108c
SS
6506
6507static void
257e7a09
YQ
6508dwarf2_read_symtab (struct partial_symtab *self,
6509 struct objfile *objfile)
c906108c 6510{
257e7a09 6511 if (self->readin)
c906108c 6512 {
442e4d9c 6513 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6514 self->filename);
442e4d9c
YQ
6515 }
6516 else
6517 {
6518 if (info_verbose)
c906108c 6519 {
442e4d9c 6520 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6521 self->filename);
442e4d9c 6522 gdb_flush (gdb_stdout);
c906108c 6523 }
c906108c 6524
442e4d9c
YQ
6525 /* Restore our global data. */
6526 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6527
442e4d9c
YQ
6528 /* If this psymtab is constructed from a debug-only objfile, the
6529 has_section_at_zero flag will not necessarily be correct. We
6530 can get the correct value for this flag by looking at the data
6531 associated with the (presumably stripped) associated objfile. */
6532 if (objfile->separate_debug_objfile_backlink)
6533 {
6534 struct dwarf2_per_objfile *dpo_backlink
6535 = objfile_data (objfile->separate_debug_objfile_backlink,
6536 dwarf2_objfile_data_key);
9a619af0 6537
442e4d9c
YQ
6538 dwarf2_per_objfile->has_section_at_zero
6539 = dpo_backlink->has_section_at_zero;
6540 }
b2ab525c 6541
442e4d9c 6542 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6543
257e7a09 6544 psymtab_to_symtab_1 (self);
c906108c 6545
442e4d9c
YQ
6546 /* Finish up the debug error message. */
6547 if (info_verbose)
6548 printf_filtered (_("done.\n"));
c906108c 6549 }
95554aad
TT
6550
6551 process_cu_includes ();
c906108c 6552}
9cdd5dbd
DE
6553\f
6554/* Reading in full CUs. */
c906108c 6555
10b3939b
DJ
6556/* Add PER_CU to the queue. */
6557
6558static void
95554aad
TT
6559queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6560 enum language pretend_language)
10b3939b
DJ
6561{
6562 struct dwarf2_queue_item *item;
6563
6564 per_cu->queued = 1;
6565 item = xmalloc (sizeof (*item));
6566 item->per_cu = per_cu;
95554aad 6567 item->pretend_language = pretend_language;
10b3939b
DJ
6568 item->next = NULL;
6569
6570 if (dwarf2_queue == NULL)
6571 dwarf2_queue = item;
6572 else
6573 dwarf2_queue_tail->next = item;
6574
6575 dwarf2_queue_tail = item;
6576}
6577
0907af0c
DE
6578/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6579 unit and add it to our queue.
6580 The result is non-zero if PER_CU was queued, otherwise the result is zero
6581 meaning either PER_CU is already queued or it is already loaded. */
6582
6583static int
6584maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6585 struct dwarf2_per_cu_data *per_cu,
6586 enum language pretend_language)
6587{
6588 /* We may arrive here during partial symbol reading, if we need full
6589 DIEs to process an unusual case (e.g. template arguments). Do
6590 not queue PER_CU, just tell our caller to load its DIEs. */
6591 if (dwarf2_per_objfile->reading_partial_symbols)
6592 {
6593 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6594 return 1;
6595 return 0;
6596 }
6597
6598 /* Mark the dependence relation so that we don't flush PER_CU
6599 too early. */
6600 dwarf2_add_dependence (this_cu, per_cu);
6601
6602 /* If it's already on the queue, we have nothing to do. */
6603 if (per_cu->queued)
6604 return 0;
6605
6606 /* If the compilation unit is already loaded, just mark it as
6607 used. */
6608 if (per_cu->cu != NULL)
6609 {
6610 per_cu->cu->last_used = 0;
6611 return 0;
6612 }
6613
6614 /* Add it to the queue. */
6615 queue_comp_unit (per_cu, pretend_language);
6616
6617 return 1;
6618}
6619
10b3939b
DJ
6620/* Process the queue. */
6621
6622static void
a0f42c21 6623process_queue (void)
10b3939b
DJ
6624{
6625 struct dwarf2_queue_item *item, *next_item;
6626
45cfd468
DE
6627 if (dwarf2_read_debug)
6628 {
6629 fprintf_unfiltered (gdb_stdlog,
6630 "Expanding one or more symtabs of objfile %s ...\n",
6631 dwarf2_per_objfile->objfile->name);
6632 }
6633
03dd20cc
DJ
6634 /* The queue starts out with one item, but following a DIE reference
6635 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6636 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6637 {
9291a0cd
TT
6638 if (dwarf2_per_objfile->using_index
6639 ? !item->per_cu->v.quick->symtab
6640 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6641 {
6642 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6643
6644 if (dwarf2_read_debug)
6645 {
6646 fprintf_unfiltered (gdb_stdlog,
6647 "Expanding symtab of %s at offset 0x%x\n",
6648 per_cu->is_debug_types ? "TU" : "CU",
6649 per_cu->offset.sect_off);
6650 }
6651
6652 if (per_cu->is_debug_types)
6653 process_full_type_unit (per_cu, item->pretend_language);
6654 else
6655 process_full_comp_unit (per_cu, item->pretend_language);
6656
6657 if (dwarf2_read_debug)
6658 {
6659 fprintf_unfiltered (gdb_stdlog,
6660 "Done expanding %s at offset 0x%x\n",
6661 per_cu->is_debug_types ? "TU" : "CU",
6662 per_cu->offset.sect_off);
6663 }
6664 }
10b3939b
DJ
6665
6666 item->per_cu->queued = 0;
6667 next_item = item->next;
6668 xfree (item);
6669 }
6670
6671 dwarf2_queue_tail = NULL;
45cfd468
DE
6672
6673 if (dwarf2_read_debug)
6674 {
6675 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6676 dwarf2_per_objfile->objfile->name);
6677 }
10b3939b
DJ
6678}
6679
6680/* Free all allocated queue entries. This function only releases anything if
6681 an error was thrown; if the queue was processed then it would have been
6682 freed as we went along. */
6683
6684static void
6685dwarf2_release_queue (void *dummy)
6686{
6687 struct dwarf2_queue_item *item, *last;
6688
6689 item = dwarf2_queue;
6690 while (item)
6691 {
6692 /* Anything still marked queued is likely to be in an
6693 inconsistent state, so discard it. */
6694 if (item->per_cu->queued)
6695 {
6696 if (item->per_cu->cu != NULL)
dee91e82 6697 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6698 item->per_cu->queued = 0;
6699 }
6700
6701 last = item;
6702 item = item->next;
6703 xfree (last);
6704 }
6705
6706 dwarf2_queue = dwarf2_queue_tail = NULL;
6707}
6708
6709/* Read in full symbols for PST, and anything it depends on. */
6710
c906108c 6711static void
fba45db2 6712psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6713{
10b3939b 6714 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6715 int i;
6716
95554aad
TT
6717 if (pst->readin)
6718 return;
6719
aaa75496 6720 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6721 if (!pst->dependencies[i]->readin
6722 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6723 {
6724 /* Inform about additional files that need to be read in. */
6725 if (info_verbose)
6726 {
a3f17187 6727 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6728 fputs_filtered (" ", gdb_stdout);
6729 wrap_here ("");
6730 fputs_filtered ("and ", gdb_stdout);
6731 wrap_here ("");
6732 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6733 wrap_here (""); /* Flush output. */
aaa75496
JB
6734 gdb_flush (gdb_stdout);
6735 }
6736 psymtab_to_symtab_1 (pst->dependencies[i]);
6737 }
6738
e38df1d0 6739 per_cu = pst->read_symtab_private;
10b3939b
DJ
6740
6741 if (per_cu == NULL)
aaa75496
JB
6742 {
6743 /* It's an include file, no symbols to read for it.
6744 Everything is in the parent symtab. */
6745 pst->readin = 1;
6746 return;
6747 }
c906108c 6748
a0f42c21 6749 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6750}
6751
dee91e82
DE
6752/* Trivial hash function for die_info: the hash value of a DIE
6753 is its offset in .debug_info for this objfile. */
10b3939b 6754
dee91e82
DE
6755static hashval_t
6756die_hash (const void *item)
10b3939b 6757{
dee91e82 6758 const struct die_info *die = item;
6502dd73 6759
dee91e82
DE
6760 return die->offset.sect_off;
6761}
63d06c5c 6762
dee91e82
DE
6763/* Trivial comparison function for die_info structures: two DIEs
6764 are equal if they have the same offset. */
98bfdba5 6765
dee91e82
DE
6766static int
6767die_eq (const void *item_lhs, const void *item_rhs)
6768{
6769 const struct die_info *die_lhs = item_lhs;
6770 const struct die_info *die_rhs = item_rhs;
c906108c 6771
dee91e82
DE
6772 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6773}
c906108c 6774
dee91e82
DE
6775/* die_reader_func for load_full_comp_unit.
6776 This is identical to read_signatured_type_reader,
6777 but is kept separate for now. */
c906108c 6778
dee91e82
DE
6779static void
6780load_full_comp_unit_reader (const struct die_reader_specs *reader,
6781 gdb_byte *info_ptr,
6782 struct die_info *comp_unit_die,
6783 int has_children,
6784 void *data)
6785{
6786 struct dwarf2_cu *cu = reader->cu;
95554aad 6787 enum language *language_ptr = data;
6caca83c 6788
dee91e82
DE
6789 gdb_assert (cu->die_hash == NULL);
6790 cu->die_hash =
6791 htab_create_alloc_ex (cu->header.length / 12,
6792 die_hash,
6793 die_eq,
6794 NULL,
6795 &cu->comp_unit_obstack,
6796 hashtab_obstack_allocate,
6797 dummy_obstack_deallocate);
e142c38c 6798
dee91e82
DE
6799 if (has_children)
6800 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6801 &info_ptr, comp_unit_die);
6802 cu->dies = comp_unit_die;
6803 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6804
6805 /* We try not to read any attributes in this function, because not
9cdd5dbd 6806 all CUs needed for references have been loaded yet, and symbol
10b3939b 6807 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6808 or we won't be able to build types correctly.
6809 Similarly, if we do not read the producer, we can not apply
6810 producer-specific interpretation. */
95554aad 6811 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6812}
10b3939b 6813
dee91e82 6814/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6815
dee91e82 6816static void
95554aad
TT
6817load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6818 enum language pretend_language)
dee91e82 6819{
3019eac3 6820 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6821
f4dc4d17
DE
6822 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6823 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6824}
6825
3da10d80
KS
6826/* Add a DIE to the delayed physname list. */
6827
6828static void
6829add_to_method_list (struct type *type, int fnfield_index, int index,
6830 const char *name, struct die_info *die,
6831 struct dwarf2_cu *cu)
6832{
6833 struct delayed_method_info mi;
6834 mi.type = type;
6835 mi.fnfield_index = fnfield_index;
6836 mi.index = index;
6837 mi.name = name;
6838 mi.die = die;
6839 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6840}
6841
6842/* A cleanup for freeing the delayed method list. */
6843
6844static void
6845free_delayed_list (void *ptr)
6846{
6847 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6848 if (cu->method_list != NULL)
6849 {
6850 VEC_free (delayed_method_info, cu->method_list);
6851 cu->method_list = NULL;
6852 }
6853}
6854
6855/* Compute the physnames of any methods on the CU's method list.
6856
6857 The computation of method physnames is delayed in order to avoid the
6858 (bad) condition that one of the method's formal parameters is of an as yet
6859 incomplete type. */
6860
6861static void
6862compute_delayed_physnames (struct dwarf2_cu *cu)
6863{
6864 int i;
6865 struct delayed_method_info *mi;
6866 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6867 {
1d06ead6 6868 const char *physname;
3da10d80
KS
6869 struct fn_fieldlist *fn_flp
6870 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6871 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6872 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6873 }
6874}
6875
a766d390
DE
6876/* Go objects should be embedded in a DW_TAG_module DIE,
6877 and it's not clear if/how imported objects will appear.
6878 To keep Go support simple until that's worked out,
6879 go back through what we've read and create something usable.
6880 We could do this while processing each DIE, and feels kinda cleaner,
6881 but that way is more invasive.
6882 This is to, for example, allow the user to type "p var" or "b main"
6883 without having to specify the package name, and allow lookups
6884 of module.object to work in contexts that use the expression
6885 parser. */
6886
6887static void
6888fixup_go_packaging (struct dwarf2_cu *cu)
6889{
6890 char *package_name = NULL;
6891 struct pending *list;
6892 int i;
6893
6894 for (list = global_symbols; list != NULL; list = list->next)
6895 {
6896 for (i = 0; i < list->nsyms; ++i)
6897 {
6898 struct symbol *sym = list->symbol[i];
6899
6900 if (SYMBOL_LANGUAGE (sym) == language_go
6901 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6902 {
6903 char *this_package_name = go_symbol_package_name (sym);
6904
6905 if (this_package_name == NULL)
6906 continue;
6907 if (package_name == NULL)
6908 package_name = this_package_name;
6909 else
6910 {
6911 if (strcmp (package_name, this_package_name) != 0)
6912 complaint (&symfile_complaints,
6913 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6914 (SYMBOL_SYMTAB (sym)
05cba821 6915 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6916 : cu->objfile->name),
6917 this_package_name, package_name);
6918 xfree (this_package_name);
6919 }
6920 }
6921 }
6922 }
6923
6924 if (package_name != NULL)
6925 {
6926 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6927 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6928 package_name,
6929 strlen (package_name));
a766d390 6930 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6931 saved_package_name, objfile);
a766d390
DE
6932 struct symbol *sym;
6933
6934 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6935
6936 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6937 SYMBOL_SET_LANGUAGE (sym, language_go);
86f62fd7
TT
6938 SYMBOL_SET_NAMES (sym, saved_package_name,
6939 strlen (saved_package_name), 0, objfile);
a766d390
DE
6940 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6941 e.g., "main" finds the "main" module and not C's main(). */
6942 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6943 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6944 SYMBOL_TYPE (sym) = type;
6945
6946 add_symbol_to_list (sym, &global_symbols);
6947
6948 xfree (package_name);
6949 }
6950}
6951
95554aad
TT
6952/* Return the symtab for PER_CU. This works properly regardless of
6953 whether we're using the index or psymtabs. */
6954
6955static struct symtab *
6956get_symtab (struct dwarf2_per_cu_data *per_cu)
6957{
6958 return (dwarf2_per_objfile->using_index
6959 ? per_cu->v.quick->symtab
6960 : per_cu->v.psymtab->symtab);
6961}
6962
6963/* A helper function for computing the list of all symbol tables
6964 included by PER_CU. */
6965
6966static void
6967recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6968 htab_t all_children,
6969 struct dwarf2_per_cu_data *per_cu)
6970{
6971 void **slot;
6972 int ix;
6973 struct dwarf2_per_cu_data *iter;
6974
6975 slot = htab_find_slot (all_children, per_cu, INSERT);
6976 if (*slot != NULL)
6977 {
6978 /* This inclusion and its children have been processed. */
6979 return;
6980 }
6981
6982 *slot = per_cu;
6983 /* Only add a CU if it has a symbol table. */
6984 if (get_symtab (per_cu) != NULL)
6985 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6986
6987 for (ix = 0;
796a7ff8 6988 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
6989 ++ix)
6990 recursively_compute_inclusions (result, all_children, iter);
6991}
6992
6993/* Compute the symtab 'includes' fields for the symtab related to
6994 PER_CU. */
6995
6996static void
6997compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6998{
f4dc4d17
DE
6999 gdb_assert (! per_cu->is_debug_types);
7000
796a7ff8 7001 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7002 {
7003 int ix, len;
7004 struct dwarf2_per_cu_data *iter;
7005 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7006 htab_t all_children;
7007 struct symtab *symtab = get_symtab (per_cu);
7008
7009 /* If we don't have a symtab, we can just skip this case. */
7010 if (symtab == NULL)
7011 return;
7012
7013 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7014 NULL, xcalloc, xfree);
7015
7016 for (ix = 0;
796a7ff8 7017 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7018 ix, iter);
7019 ++ix)
7020 recursively_compute_inclusions (&result_children, all_children, iter);
7021
796a7ff8
DE
7022 /* Now we have a transitive closure of all the included CUs, and
7023 for .gdb_index version 7 the included TUs, so we can convert it
7024 to a list of symtabs. */
95554aad
TT
7025 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7026 symtab->includes
7027 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7028 (len + 1) * sizeof (struct symtab *));
7029 for (ix = 0;
7030 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7031 ++ix)
7032 symtab->includes[ix] = get_symtab (iter);
7033 symtab->includes[len] = NULL;
7034
7035 VEC_free (dwarf2_per_cu_ptr, result_children);
7036 htab_delete (all_children);
7037 }
7038}
7039
7040/* Compute the 'includes' field for the symtabs of all the CUs we just
7041 read. */
7042
7043static void
7044process_cu_includes (void)
7045{
7046 int ix;
7047 struct dwarf2_per_cu_data *iter;
7048
7049 for (ix = 0;
7050 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7051 ix, iter);
7052 ++ix)
f4dc4d17
DE
7053 {
7054 if (! iter->is_debug_types)
7055 compute_symtab_includes (iter);
7056 }
95554aad
TT
7057
7058 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7059}
7060
9cdd5dbd 7061/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7062 already been loaded into memory. */
7063
7064static void
95554aad
TT
7065process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7066 enum language pretend_language)
10b3939b 7067{
10b3939b 7068 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7069 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7070 CORE_ADDR lowpc, highpc;
7071 struct symtab *symtab;
3da10d80 7072 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7073 CORE_ADDR baseaddr;
4359dff1 7074 struct block *static_block;
10b3939b
DJ
7075
7076 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7077
10b3939b
DJ
7078 buildsym_init ();
7079 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7080 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7081
7082 cu->list_in_scope = &file_symbols;
c906108c 7083
95554aad
TT
7084 cu->language = pretend_language;
7085 cu->language_defn = language_def (cu->language);
7086
c906108c 7087 /* Do line number decoding in read_file_scope () */
10b3939b 7088 process_die (cu->dies, cu);
c906108c 7089
a766d390
DE
7090 /* For now fudge the Go package. */
7091 if (cu->language == language_go)
7092 fixup_go_packaging (cu);
7093
3da10d80
KS
7094 /* Now that we have processed all the DIEs in the CU, all the types
7095 should be complete, and it should now be safe to compute all of the
7096 physnames. */
7097 compute_delayed_physnames (cu);
7098 do_cleanups (delayed_list_cleanup);
7099
fae299cd
DC
7100 /* Some compilers don't define a DW_AT_high_pc attribute for the
7101 compilation unit. If the DW_AT_high_pc is missing, synthesize
7102 it, by scanning the DIE's below the compilation unit. */
10b3939b 7103 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7104
36586728
TT
7105 static_block
7106 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7107 per_cu->imported_symtabs != NULL);
4359dff1
JK
7108
7109 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7110 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7111 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7112 addrmap to help ensure it has an accurate map of pc values belonging to
7113 this comp unit. */
7114 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7115
7116 symtab = end_symtab_from_static_block (static_block, objfile,
7117 SECT_OFF_TEXT (objfile), 0);
c906108c 7118
8be455d7 7119 if (symtab != NULL)
c906108c 7120 {
df15bd07 7121 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7122
8be455d7
JK
7123 /* Set symtab language to language from DW_AT_language. If the
7124 compilation is from a C file generated by language preprocessors, do
7125 not set the language if it was already deduced by start_subfile. */
7126 if (!(cu->language == language_c && symtab->language != language_c))
7127 symtab->language = cu->language;
7128
7129 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7130 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7131 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7132 there were bugs in prologue debug info, fixed later in GCC-4.5
7133 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7134
7135 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7136 needed, it would be wrong due to missing DW_AT_producer there.
7137
7138 Still one can confuse GDB by using non-standard GCC compilation
7139 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7140 */
ab260dad 7141 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7142 symtab->locations_valid = 1;
e0d00bc7
JK
7143
7144 if (gcc_4_minor >= 5)
7145 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7146
7147 symtab->call_site_htab = cu->call_site_htab;
c906108c 7148 }
9291a0cd
TT
7149
7150 if (dwarf2_per_objfile->using_index)
7151 per_cu->v.quick->symtab = symtab;
7152 else
7153 {
7154 struct partial_symtab *pst = per_cu->v.psymtab;
7155 pst->symtab = symtab;
7156 pst->readin = 1;
7157 }
c906108c 7158
95554aad
TT
7159 /* Push it for inclusion processing later. */
7160 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7161
c906108c 7162 do_cleanups (back_to);
f4dc4d17 7163}
45cfd468 7164
f4dc4d17
DE
7165/* Generate full symbol information for type unit PER_CU, whose DIEs have
7166 already been loaded into memory. */
7167
7168static void
7169process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7170 enum language pretend_language)
7171{
7172 struct dwarf2_cu *cu = per_cu->cu;
7173 struct objfile *objfile = per_cu->objfile;
7174 struct symtab *symtab;
7175 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7176 struct signatured_type *sig_type;
7177
7178 gdb_assert (per_cu->is_debug_types);
7179 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7180
7181 buildsym_init ();
7182 back_to = make_cleanup (really_free_pendings, NULL);
7183 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7184
7185 cu->list_in_scope = &file_symbols;
7186
7187 cu->language = pretend_language;
7188 cu->language_defn = language_def (cu->language);
7189
7190 /* The symbol tables are set up in read_type_unit_scope. */
7191 process_die (cu->dies, cu);
7192
7193 /* For now fudge the Go package. */
7194 if (cu->language == language_go)
7195 fixup_go_packaging (cu);
7196
7197 /* Now that we have processed all the DIEs in the CU, all the types
7198 should be complete, and it should now be safe to compute all of the
7199 physnames. */
7200 compute_delayed_physnames (cu);
7201 do_cleanups (delayed_list_cleanup);
7202
7203 /* TUs share symbol tables.
7204 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7205 of it with end_expandable_symtab. Otherwise, complete the addition of
7206 this TU's symbols to the existing symtab. */
0186c6a7 7207 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7208 {
f4dc4d17 7209 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7210 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7211
7212 if (symtab != NULL)
7213 {
7214 /* Set symtab language to language from DW_AT_language. If the
7215 compilation is from a C file generated by language preprocessors,
7216 do not set the language if it was already deduced by
7217 start_subfile. */
7218 if (!(cu->language == language_c && symtab->language != language_c))
7219 symtab->language = cu->language;
7220 }
7221 }
7222 else
7223 {
7224 augment_type_symtab (objfile,
0186c6a7
DE
7225 sig_type->type_unit_group->primary_symtab);
7226 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7227 }
7228
7229 if (dwarf2_per_objfile->using_index)
7230 per_cu->v.quick->symtab = symtab;
7231 else
7232 {
7233 struct partial_symtab *pst = per_cu->v.psymtab;
7234 pst->symtab = symtab;
7235 pst->readin = 1;
45cfd468 7236 }
f4dc4d17
DE
7237
7238 do_cleanups (back_to);
c906108c
SS
7239}
7240
95554aad
TT
7241/* Process an imported unit DIE. */
7242
7243static void
7244process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7245{
7246 struct attribute *attr;
7247
f4dc4d17
DE
7248 /* For now we don't handle imported units in type units. */
7249 if (cu->per_cu->is_debug_types)
7250 {
7251 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7252 " supported in type units [in module %s]"),
7253 cu->objfile->name);
7254 }
7255
95554aad
TT
7256 attr = dwarf2_attr (die, DW_AT_import, cu);
7257 if (attr != NULL)
7258 {
7259 struct dwarf2_per_cu_data *per_cu;
7260 struct symtab *imported_symtab;
7261 sect_offset offset;
36586728 7262 int is_dwz;
95554aad
TT
7263
7264 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7265 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7266 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7267
7268 /* Queue the unit, if needed. */
7269 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7270 load_full_comp_unit (per_cu, cu->language);
7271
796a7ff8 7272 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7273 per_cu);
7274 }
7275}
7276
c906108c
SS
7277/* Process a die and its children. */
7278
7279static void
e7c27a73 7280process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7281{
7282 switch (die->tag)
7283 {
7284 case DW_TAG_padding:
7285 break;
7286 case DW_TAG_compile_unit:
95554aad 7287 case DW_TAG_partial_unit:
e7c27a73 7288 read_file_scope (die, cu);
c906108c 7289 break;
348e048f
DE
7290 case DW_TAG_type_unit:
7291 read_type_unit_scope (die, cu);
7292 break;
c906108c 7293 case DW_TAG_subprogram:
c906108c 7294 case DW_TAG_inlined_subroutine:
edb3359d 7295 read_func_scope (die, cu);
c906108c
SS
7296 break;
7297 case DW_TAG_lexical_block:
14898363
L
7298 case DW_TAG_try_block:
7299 case DW_TAG_catch_block:
e7c27a73 7300 read_lexical_block_scope (die, cu);
c906108c 7301 break;
96408a79
SA
7302 case DW_TAG_GNU_call_site:
7303 read_call_site_scope (die, cu);
7304 break;
c906108c 7305 case DW_TAG_class_type:
680b30c7 7306 case DW_TAG_interface_type:
c906108c
SS
7307 case DW_TAG_structure_type:
7308 case DW_TAG_union_type:
134d01f1 7309 process_structure_scope (die, cu);
c906108c
SS
7310 break;
7311 case DW_TAG_enumeration_type:
134d01f1 7312 process_enumeration_scope (die, cu);
c906108c 7313 break;
134d01f1 7314
f792889a
DJ
7315 /* These dies have a type, but processing them does not create
7316 a symbol or recurse to process the children. Therefore we can
7317 read them on-demand through read_type_die. */
c906108c 7318 case DW_TAG_subroutine_type:
72019c9c 7319 case DW_TAG_set_type:
c906108c 7320 case DW_TAG_array_type:
c906108c 7321 case DW_TAG_pointer_type:
c906108c 7322 case DW_TAG_ptr_to_member_type:
c906108c 7323 case DW_TAG_reference_type:
c906108c 7324 case DW_TAG_string_type:
c906108c 7325 break;
134d01f1 7326
c906108c 7327 case DW_TAG_base_type:
a02abb62 7328 case DW_TAG_subrange_type:
cb249c71 7329 case DW_TAG_typedef:
134d01f1
DJ
7330 /* Add a typedef symbol for the type definition, if it has a
7331 DW_AT_name. */
f792889a 7332 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7333 break;
c906108c 7334 case DW_TAG_common_block:
e7c27a73 7335 read_common_block (die, cu);
c906108c
SS
7336 break;
7337 case DW_TAG_common_inclusion:
7338 break;
d9fa45fe 7339 case DW_TAG_namespace:
4d4ec4e5 7340 cu->processing_has_namespace_info = 1;
e7c27a73 7341 read_namespace (die, cu);
d9fa45fe 7342 break;
5d7cb8df 7343 case DW_TAG_module:
4d4ec4e5 7344 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7345 read_module (die, cu);
7346 break;
d9fa45fe
DC
7347 case DW_TAG_imported_declaration:
7348 case DW_TAG_imported_module:
4d4ec4e5 7349 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7350 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7351 || cu->language != language_fortran))
7352 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7353 dwarf_tag_name (die->tag));
7354 read_import_statement (die, cu);
d9fa45fe 7355 break;
95554aad
TT
7356
7357 case DW_TAG_imported_unit:
7358 process_imported_unit_die (die, cu);
7359 break;
7360
c906108c 7361 default:
e7c27a73 7362 new_symbol (die, NULL, cu);
c906108c
SS
7363 break;
7364 }
7365}
ca69b9e6
DE
7366\f
7367/* DWARF name computation. */
c906108c 7368
94af9270
KS
7369/* A helper function for dwarf2_compute_name which determines whether DIE
7370 needs to have the name of the scope prepended to the name listed in the
7371 die. */
7372
7373static int
7374die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7375{
1c809c68
TT
7376 struct attribute *attr;
7377
94af9270
KS
7378 switch (die->tag)
7379 {
7380 case DW_TAG_namespace:
7381 case DW_TAG_typedef:
7382 case DW_TAG_class_type:
7383 case DW_TAG_interface_type:
7384 case DW_TAG_structure_type:
7385 case DW_TAG_union_type:
7386 case DW_TAG_enumeration_type:
7387 case DW_TAG_enumerator:
7388 case DW_TAG_subprogram:
7389 case DW_TAG_member:
7390 return 1;
7391
7392 case DW_TAG_variable:
c2b0a229 7393 case DW_TAG_constant:
94af9270
KS
7394 /* We only need to prefix "globally" visible variables. These include
7395 any variable marked with DW_AT_external or any variable that
7396 lives in a namespace. [Variables in anonymous namespaces
7397 require prefixing, but they are not DW_AT_external.] */
7398
7399 if (dwarf2_attr (die, DW_AT_specification, cu))
7400 {
7401 struct dwarf2_cu *spec_cu = cu;
9a619af0 7402
94af9270
KS
7403 return die_needs_namespace (die_specification (die, &spec_cu),
7404 spec_cu);
7405 }
7406
1c809c68 7407 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7408 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7409 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7410 return 0;
7411 /* A variable in a lexical block of some kind does not need a
7412 namespace, even though in C++ such variables may be external
7413 and have a mangled name. */
7414 if (die->parent->tag == DW_TAG_lexical_block
7415 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7416 || die->parent->tag == DW_TAG_catch_block
7417 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7418 return 0;
7419 return 1;
94af9270
KS
7420
7421 default:
7422 return 0;
7423 }
7424}
7425
98bfdba5
PA
7426/* Retrieve the last character from a mem_file. */
7427
7428static void
7429do_ui_file_peek_last (void *object, const char *buffer, long length)
7430{
7431 char *last_char_p = (char *) object;
7432
7433 if (length > 0)
7434 *last_char_p = buffer[length - 1];
7435}
7436
94af9270 7437/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7438 compute the physname for the object, which include a method's:
7439 - formal parameters (C++/Java),
7440 - receiver type (Go),
7441 - return type (Java).
7442
7443 The term "physname" is a bit confusing.
7444 For C++, for example, it is the demangled name.
7445 For Go, for example, it's the mangled name.
94af9270 7446
af6b7be1
JB
7447 For Ada, return the DIE's linkage name rather than the fully qualified
7448 name. PHYSNAME is ignored..
7449
94af9270
KS
7450 The result is allocated on the objfile_obstack and canonicalized. */
7451
7452static const char *
15d034d0
TT
7453dwarf2_compute_name (const char *name,
7454 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7455 int physname)
7456{
bb5ed363
DE
7457 struct objfile *objfile = cu->objfile;
7458
94af9270
KS
7459 if (name == NULL)
7460 name = dwarf2_name (die, cu);
7461
f55ee35c
JK
7462 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7463 compute it by typename_concat inside GDB. */
7464 if (cu->language == language_ada
7465 || (cu->language == language_fortran && physname))
7466 {
7467 /* For Ada unit, we prefer the linkage name over the name, as
7468 the former contains the exported name, which the user expects
7469 to be able to reference. Ideally, we want the user to be able
7470 to reference this entity using either natural or linkage name,
7471 but we haven't started looking at this enhancement yet. */
7472 struct attribute *attr;
7473
7474 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7475 if (attr == NULL)
7476 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7477 if (attr && DW_STRING (attr))
7478 return DW_STRING (attr);
7479 }
7480
94af9270
KS
7481 /* These are the only languages we know how to qualify names in. */
7482 if (name != NULL
f55ee35c
JK
7483 && (cu->language == language_cplus || cu->language == language_java
7484 || cu->language == language_fortran))
94af9270
KS
7485 {
7486 if (die_needs_namespace (die, cu))
7487 {
7488 long length;
0d5cff50 7489 const char *prefix;
94af9270
KS
7490 struct ui_file *buf;
7491
7492 prefix = determine_prefix (die, cu);
7493 buf = mem_fileopen ();
7494 if (*prefix != '\0')
7495 {
f55ee35c
JK
7496 char *prefixed_name = typename_concat (NULL, prefix, name,
7497 physname, cu);
9a619af0 7498
94af9270
KS
7499 fputs_unfiltered (prefixed_name, buf);
7500 xfree (prefixed_name);
7501 }
7502 else
62d5b8da 7503 fputs_unfiltered (name, buf);
94af9270 7504
98bfdba5
PA
7505 /* Template parameters may be specified in the DIE's DW_AT_name, or
7506 as children with DW_TAG_template_type_param or
7507 DW_TAG_value_type_param. If the latter, add them to the name
7508 here. If the name already has template parameters, then
7509 skip this step; some versions of GCC emit both, and
7510 it is more efficient to use the pre-computed name.
7511
7512 Something to keep in mind about this process: it is very
7513 unlikely, or in some cases downright impossible, to produce
7514 something that will match the mangled name of a function.
7515 If the definition of the function has the same debug info,
7516 we should be able to match up with it anyway. But fallbacks
7517 using the minimal symbol, for instance to find a method
7518 implemented in a stripped copy of libstdc++, will not work.
7519 If we do not have debug info for the definition, we will have to
7520 match them up some other way.
7521
7522 When we do name matching there is a related problem with function
7523 templates; two instantiated function templates are allowed to
7524 differ only by their return types, which we do not add here. */
7525
7526 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7527 {
7528 struct attribute *attr;
7529 struct die_info *child;
7530 int first = 1;
7531
7532 die->building_fullname = 1;
7533
7534 for (child = die->child; child != NULL; child = child->sibling)
7535 {
7536 struct type *type;
12df843f 7537 LONGEST value;
98bfdba5
PA
7538 gdb_byte *bytes;
7539 struct dwarf2_locexpr_baton *baton;
7540 struct value *v;
7541
7542 if (child->tag != DW_TAG_template_type_param
7543 && child->tag != DW_TAG_template_value_param)
7544 continue;
7545
7546 if (first)
7547 {
7548 fputs_unfiltered ("<", buf);
7549 first = 0;
7550 }
7551 else
7552 fputs_unfiltered (", ", buf);
7553
7554 attr = dwarf2_attr (child, DW_AT_type, cu);
7555 if (attr == NULL)
7556 {
7557 complaint (&symfile_complaints,
7558 _("template parameter missing DW_AT_type"));
7559 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7560 continue;
7561 }
7562 type = die_type (child, cu);
7563
7564 if (child->tag == DW_TAG_template_type_param)
7565 {
79d43c61 7566 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7567 continue;
7568 }
7569
7570 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7571 if (attr == NULL)
7572 {
7573 complaint (&symfile_complaints,
3e43a32a
MS
7574 _("template parameter missing "
7575 "DW_AT_const_value"));
98bfdba5
PA
7576 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7577 continue;
7578 }
7579
7580 dwarf2_const_value_attr (attr, type, name,
7581 &cu->comp_unit_obstack, cu,
7582 &value, &bytes, &baton);
7583
7584 if (TYPE_NOSIGN (type))
7585 /* GDB prints characters as NUMBER 'CHAR'. If that's
7586 changed, this can use value_print instead. */
7587 c_printchar (value, type, buf);
7588 else
7589 {
7590 struct value_print_options opts;
7591
7592 if (baton != NULL)
7593 v = dwarf2_evaluate_loc_desc (type, NULL,
7594 baton->data,
7595 baton->size,
7596 baton->per_cu);
7597 else if (bytes != NULL)
7598 {
7599 v = allocate_value (type);
7600 memcpy (value_contents_writeable (v), bytes,
7601 TYPE_LENGTH (type));
7602 }
7603 else
7604 v = value_from_longest (type, value);
7605
3e43a32a
MS
7606 /* Specify decimal so that we do not depend on
7607 the radix. */
98bfdba5
PA
7608 get_formatted_print_options (&opts, 'd');
7609 opts.raw = 1;
7610 value_print (v, buf, &opts);
7611 release_value (v);
7612 value_free (v);
7613 }
7614 }
7615
7616 die->building_fullname = 0;
7617
7618 if (!first)
7619 {
7620 /* Close the argument list, with a space if necessary
7621 (nested templates). */
7622 char last_char = '\0';
7623 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7624 if (last_char == '>')
7625 fputs_unfiltered (" >", buf);
7626 else
7627 fputs_unfiltered (">", buf);
7628 }
7629 }
7630
94af9270
KS
7631 /* For Java and C++ methods, append formal parameter type
7632 information, if PHYSNAME. */
6e70227d 7633
94af9270
KS
7634 if (physname && die->tag == DW_TAG_subprogram
7635 && (cu->language == language_cplus
7636 || cu->language == language_java))
7637 {
7638 struct type *type = read_type_die (die, cu);
7639
79d43c61
TT
7640 c_type_print_args (type, buf, 1, cu->language,
7641 &type_print_raw_options);
94af9270
KS
7642
7643 if (cu->language == language_java)
7644 {
7645 /* For java, we must append the return type to method
0963b4bd 7646 names. */
94af9270
KS
7647 if (die->tag == DW_TAG_subprogram)
7648 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7649 0, 0, &type_print_raw_options);
94af9270
KS
7650 }
7651 else if (cu->language == language_cplus)
7652 {
60430eff
DJ
7653 /* Assume that an artificial first parameter is
7654 "this", but do not crash if it is not. RealView
7655 marks unnamed (and thus unused) parameters as
7656 artificial; there is no way to differentiate
7657 the two cases. */
94af9270
KS
7658 if (TYPE_NFIELDS (type) > 0
7659 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7660 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7661 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7662 0))))
94af9270
KS
7663 fputs_unfiltered (" const", buf);
7664 }
7665 }
7666
bb5ed363 7667 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7668 &length);
7669 ui_file_delete (buf);
7670
7671 if (cu->language == language_cplus)
7672 {
15d034d0 7673 const char *cname
94af9270 7674 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7675 &objfile->objfile_obstack);
9a619af0 7676
94af9270
KS
7677 if (cname != NULL)
7678 name = cname;
7679 }
7680 }
7681 }
7682
7683 return name;
7684}
7685
0114d602
DJ
7686/* Return the fully qualified name of DIE, based on its DW_AT_name.
7687 If scope qualifiers are appropriate they will be added. The result
7688 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7689 not have a name. NAME may either be from a previous call to
7690 dwarf2_name or NULL.
7691
0963b4bd 7692 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7693
7694static const char *
15d034d0 7695dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7696{
94af9270
KS
7697 return dwarf2_compute_name (name, die, cu, 0);
7698}
0114d602 7699
94af9270
KS
7700/* Construct a physname for the given DIE in CU. NAME may either be
7701 from a previous call to dwarf2_name or NULL. The result will be
7702 allocated on the objfile_objstack or NULL if the DIE does not have a
7703 name.
0114d602 7704
94af9270 7705 The output string will be canonicalized (if C++/Java). */
0114d602 7706
94af9270 7707static const char *
15d034d0 7708dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7709{
bb5ed363 7710 struct objfile *objfile = cu->objfile;
900e11f9
JK
7711 struct attribute *attr;
7712 const char *retval, *mangled = NULL, *canon = NULL;
7713 struct cleanup *back_to;
7714 int need_copy = 1;
7715
7716 /* In this case dwarf2_compute_name is just a shortcut not building anything
7717 on its own. */
7718 if (!die_needs_namespace (die, cu))
7719 return dwarf2_compute_name (name, die, cu, 1);
7720
7721 back_to = make_cleanup (null_cleanup, NULL);
7722
7723 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7724 if (!attr)
7725 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7726
7727 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7728 has computed. */
7729 if (attr && DW_STRING (attr))
7730 {
7731 char *demangled;
7732
7733 mangled = DW_STRING (attr);
7734
7735 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7736 type. It is easier for GDB users to search for such functions as
7737 `name(params)' than `long name(params)'. In such case the minimal
7738 symbol names do not match the full symbol names but for template
7739 functions there is never a need to look up their definition from their
7740 declaration so the only disadvantage remains the minimal symbol
7741 variant `long name(params)' does not have the proper inferior type.
7742 */
7743
a766d390
DE
7744 if (cu->language == language_go)
7745 {
7746 /* This is a lie, but we already lie to the caller new_symbol_full.
7747 new_symbol_full assumes we return the mangled name.
7748 This just undoes that lie until things are cleaned up. */
7749 demangled = NULL;
7750 }
7751 else
7752 {
7753 demangled = cplus_demangle (mangled,
7754 (DMGL_PARAMS | DMGL_ANSI
7755 | (cu->language == language_java
7756 ? DMGL_JAVA | DMGL_RET_POSTFIX
7757 : DMGL_RET_DROP)));
7758 }
900e11f9
JK
7759 if (demangled)
7760 {
7761 make_cleanup (xfree, demangled);
7762 canon = demangled;
7763 }
7764 else
7765 {
7766 canon = mangled;
7767 need_copy = 0;
7768 }
7769 }
7770
7771 if (canon == NULL || check_physname)
7772 {
7773 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7774
7775 if (canon != NULL && strcmp (physname, canon) != 0)
7776 {
7777 /* It may not mean a bug in GDB. The compiler could also
7778 compute DW_AT_linkage_name incorrectly. But in such case
7779 GDB would need to be bug-to-bug compatible. */
7780
7781 complaint (&symfile_complaints,
7782 _("Computed physname <%s> does not match demangled <%s> "
7783 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7784 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7785
7786 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7787 is available here - over computed PHYSNAME. It is safer
7788 against both buggy GDB and buggy compilers. */
7789
7790 retval = canon;
7791 }
7792 else
7793 {
7794 retval = physname;
7795 need_copy = 0;
7796 }
7797 }
7798 else
7799 retval = canon;
7800
7801 if (need_copy)
10f0c4bb 7802 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7803
7804 do_cleanups (back_to);
7805 return retval;
0114d602
DJ
7806}
7807
27aa8d6a
SW
7808/* Read the import statement specified by the given die and record it. */
7809
7810static void
7811read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7812{
bb5ed363 7813 struct objfile *objfile = cu->objfile;
27aa8d6a 7814 struct attribute *import_attr;
32019081 7815 struct die_info *imported_die, *child_die;
de4affc9 7816 struct dwarf2_cu *imported_cu;
27aa8d6a 7817 const char *imported_name;
794684b6 7818 const char *imported_name_prefix;
13387711
SW
7819 const char *canonical_name;
7820 const char *import_alias;
7821 const char *imported_declaration = NULL;
794684b6 7822 const char *import_prefix;
32019081
JK
7823 VEC (const_char_ptr) *excludes = NULL;
7824 struct cleanup *cleanups;
13387711 7825
27aa8d6a
SW
7826 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7827 if (import_attr == NULL)
7828 {
7829 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7830 dwarf_tag_name (die->tag));
7831 return;
7832 }
7833
de4affc9
CC
7834 imported_cu = cu;
7835 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7836 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7837 if (imported_name == NULL)
7838 {
7839 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7840
7841 The import in the following code:
7842 namespace A
7843 {
7844 typedef int B;
7845 }
7846
7847 int main ()
7848 {
7849 using A::B;
7850 B b;
7851 return b;
7852 }
7853
7854 ...
7855 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7856 <52> DW_AT_decl_file : 1
7857 <53> DW_AT_decl_line : 6
7858 <54> DW_AT_import : <0x75>
7859 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7860 <59> DW_AT_name : B
7861 <5b> DW_AT_decl_file : 1
7862 <5c> DW_AT_decl_line : 2
7863 <5d> DW_AT_type : <0x6e>
7864 ...
7865 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7866 <76> DW_AT_byte_size : 4
7867 <77> DW_AT_encoding : 5 (signed)
7868
7869 imports the wrong die ( 0x75 instead of 0x58 ).
7870 This case will be ignored until the gcc bug is fixed. */
7871 return;
7872 }
7873
82856980
SW
7874 /* Figure out the local name after import. */
7875 import_alias = dwarf2_name (die, cu);
27aa8d6a 7876
794684b6
SW
7877 /* Figure out where the statement is being imported to. */
7878 import_prefix = determine_prefix (die, cu);
7879
7880 /* Figure out what the scope of the imported die is and prepend it
7881 to the name of the imported die. */
de4affc9 7882 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7883
f55ee35c
JK
7884 if (imported_die->tag != DW_TAG_namespace
7885 && imported_die->tag != DW_TAG_module)
794684b6 7886 {
13387711
SW
7887 imported_declaration = imported_name;
7888 canonical_name = imported_name_prefix;
794684b6 7889 }
13387711 7890 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7891 canonical_name = obconcat (&objfile->objfile_obstack,
7892 imported_name_prefix, "::", imported_name,
7893 (char *) NULL);
13387711
SW
7894 else
7895 canonical_name = imported_name;
794684b6 7896
32019081
JK
7897 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7898
7899 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7900 for (child_die = die->child; child_die && child_die->tag;
7901 child_die = sibling_die (child_die))
7902 {
7903 /* DWARF-4: A Fortran use statement with a “rename list” may be
7904 represented by an imported module entry with an import attribute
7905 referring to the module and owned entries corresponding to those
7906 entities that are renamed as part of being imported. */
7907
7908 if (child_die->tag != DW_TAG_imported_declaration)
7909 {
7910 complaint (&symfile_complaints,
7911 _("child DW_TAG_imported_declaration expected "
7912 "- DIE at 0x%x [in module %s]"),
b64f50a1 7913 child_die->offset.sect_off, objfile->name);
32019081
JK
7914 continue;
7915 }
7916
7917 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7918 if (import_attr == NULL)
7919 {
7920 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7921 dwarf_tag_name (child_die->tag));
7922 continue;
7923 }
7924
7925 imported_cu = cu;
7926 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7927 &imported_cu);
7928 imported_name = dwarf2_name (imported_die, imported_cu);
7929 if (imported_name == NULL)
7930 {
7931 complaint (&symfile_complaints,
7932 _("child DW_TAG_imported_declaration has unknown "
7933 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7934 child_die->offset.sect_off, objfile->name);
32019081
JK
7935 continue;
7936 }
7937
7938 VEC_safe_push (const_char_ptr, excludes, imported_name);
7939
7940 process_die (child_die, cu);
7941 }
7942
c0cc3a76
SW
7943 cp_add_using_directive (import_prefix,
7944 canonical_name,
7945 import_alias,
13387711 7946 imported_declaration,
32019081 7947 excludes,
12aaed36 7948 0,
bb5ed363 7949 &objfile->objfile_obstack);
32019081
JK
7950
7951 do_cleanups (cleanups);
27aa8d6a
SW
7952}
7953
f4dc4d17 7954/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7955
cb1df416
DJ
7956static void
7957free_cu_line_header (void *arg)
7958{
7959 struct dwarf2_cu *cu = arg;
7960
7961 free_line_header (cu->line_header);
7962 cu->line_header = NULL;
7963}
7964
1b80a9fa
JK
7965/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7966 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7967 this, it was first present in GCC release 4.3.0. */
7968
7969static int
7970producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7971{
7972 if (!cu->checked_producer)
7973 check_producer (cu);
7974
7975 return cu->producer_is_gcc_lt_4_3;
7976}
7977
9291a0cd
TT
7978static void
7979find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7980 const char **name, const char **comp_dir)
9291a0cd
TT
7981{
7982 struct attribute *attr;
7983
7984 *name = NULL;
7985 *comp_dir = NULL;
7986
7987 /* Find the filename. Do not use dwarf2_name here, since the filename
7988 is not a source language identifier. */
7989 attr = dwarf2_attr (die, DW_AT_name, cu);
7990 if (attr)
7991 {
7992 *name = DW_STRING (attr);
7993 }
7994
7995 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7996 if (attr)
7997 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7998 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7999 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8000 {
15d034d0
TT
8001 char *d = ldirname (*name);
8002
8003 *comp_dir = d;
8004 if (d != NULL)
8005 make_cleanup (xfree, d);
9291a0cd
TT
8006 }
8007 if (*comp_dir != NULL)
8008 {
8009 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8010 directory, get rid of it. */
8011 char *cp = strchr (*comp_dir, ':');
8012
8013 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8014 *comp_dir = cp + 1;
8015 }
8016
8017 if (*name == NULL)
8018 *name = "<unknown>";
8019}
8020
f4dc4d17
DE
8021/* Handle DW_AT_stmt_list for a compilation unit.
8022 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8023 COMP_DIR is the compilation directory.
8024 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8025
8026static void
8027handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 8028 const char *comp_dir)
2ab95328
TT
8029{
8030 struct attribute *attr;
2ab95328 8031
f4dc4d17
DE
8032 gdb_assert (! cu->per_cu->is_debug_types);
8033
2ab95328
TT
8034 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8035 if (attr)
8036 {
8037 unsigned int line_offset = DW_UNSND (attr);
8038 struct line_header *line_header
3019eac3 8039 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8040
8041 if (line_header)
dee91e82
DE
8042 {
8043 cu->line_header = line_header;
8044 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8045 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8046 }
2ab95328
TT
8047 }
8048}
8049
95554aad 8050/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8051
c906108c 8052static void
e7c27a73 8053read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8054{
dee91e82 8055 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8056 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8057 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8058 CORE_ADDR highpc = ((CORE_ADDR) 0);
8059 struct attribute *attr;
15d034d0
TT
8060 const char *name = NULL;
8061 const char *comp_dir = NULL;
c906108c
SS
8062 struct die_info *child_die;
8063 bfd *abfd = objfile->obfd;
e142c38c 8064 CORE_ADDR baseaddr;
6e70227d 8065
e142c38c 8066 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8067
fae299cd 8068 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8069
8070 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8071 from finish_block. */
2acceee2 8072 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8073 lowpc = highpc;
8074 lowpc += baseaddr;
8075 highpc += baseaddr;
8076
9291a0cd 8077 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8078
95554aad 8079 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8080
f4b8a18d
KW
8081 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8082 standardised yet. As a workaround for the language detection we fall
8083 back to the DW_AT_producer string. */
8084 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8085 cu->language = language_opencl;
8086
3019eac3
DE
8087 /* Similar hack for Go. */
8088 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8089 set_cu_language (DW_LANG_Go, cu);
8090
f4dc4d17 8091 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8092
8093 /* Decode line number information if present. We do this before
8094 processing child DIEs, so that the line header table is available
8095 for DW_AT_decl_file. */
f4dc4d17 8096 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8097
8098 /* Process all dies in compilation unit. */
8099 if (die->child != NULL)
8100 {
8101 child_die = die->child;
8102 while (child_die && child_die->tag)
8103 {
8104 process_die (child_die, cu);
8105 child_die = sibling_die (child_die);
8106 }
8107 }
8108
8109 /* Decode macro information, if present. Dwarf 2 macro information
8110 refers to information in the line number info statement program
8111 header, so we can only read it if we've read the header
8112 successfully. */
8113 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8114 if (attr && cu->line_header)
8115 {
8116 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8117 complaint (&symfile_complaints,
8118 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8119
09262596 8120 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8121 }
8122 else
8123 {
8124 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8125 if (attr && cu->line_header)
8126 {
8127 unsigned int macro_offset = DW_UNSND (attr);
8128
09262596 8129 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8130 }
8131 }
8132
8133 do_cleanups (back_to);
8134}
8135
f4dc4d17
DE
8136/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8137 Create the set of symtabs used by this TU, or if this TU is sharing
8138 symtabs with another TU and the symtabs have already been created
8139 then restore those symtabs in the line header.
8140 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8141
8142static void
f4dc4d17 8143setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8144{
f4dc4d17
DE
8145 struct objfile *objfile = dwarf2_per_objfile->objfile;
8146 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8147 struct type_unit_group *tu_group;
8148 int first_time;
8149 struct line_header *lh;
3019eac3 8150 struct attribute *attr;
f4dc4d17 8151 unsigned int i, line_offset;
0186c6a7 8152 struct signatured_type *sig_type;
3019eac3 8153
f4dc4d17 8154 gdb_assert (per_cu->is_debug_types);
0186c6a7 8155 sig_type = (struct signatured_type *) per_cu;
3019eac3 8156
f4dc4d17 8157 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8158
f4dc4d17
DE
8159 /* If we're using .gdb_index (includes -readnow) then
8160 per_cu->s.type_unit_group may not have been set up yet. */
0186c6a7
DE
8161 if (sig_type->type_unit_group == NULL)
8162 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8163 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8164
8165 /* If we've already processed this stmt_list there's no real need to
8166 do it again, we could fake it and just recreate the part we need
8167 (file name,index -> symtab mapping). If data shows this optimization
8168 is useful we can do it then. */
8169 first_time = tu_group->primary_symtab == NULL;
8170
8171 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8172 debug info. */
8173 lh = NULL;
8174 if (attr != NULL)
3019eac3 8175 {
f4dc4d17
DE
8176 line_offset = DW_UNSND (attr);
8177 lh = dwarf_decode_line_header (line_offset, cu);
8178 }
8179 if (lh == NULL)
8180 {
8181 if (first_time)
8182 dwarf2_start_symtab (cu, "", NULL, 0);
8183 else
8184 {
8185 gdb_assert (tu_group->symtabs == NULL);
8186 restart_symtab (0);
8187 }
8188 /* Note: The primary symtab will get allocated at the end. */
8189 return;
3019eac3
DE
8190 }
8191
f4dc4d17
DE
8192 cu->line_header = lh;
8193 make_cleanup (free_cu_line_header, cu);
3019eac3 8194
f4dc4d17
DE
8195 if (first_time)
8196 {
8197 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8198
f4dc4d17
DE
8199 tu_group->num_symtabs = lh->num_file_names;
8200 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8201
f4dc4d17
DE
8202 for (i = 0; i < lh->num_file_names; ++i)
8203 {
8204 char *dir = NULL;
8205 struct file_entry *fe = &lh->file_names[i];
3019eac3 8206
f4dc4d17
DE
8207 if (fe->dir_index)
8208 dir = lh->include_dirs[fe->dir_index - 1];
8209 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8210
f4dc4d17
DE
8211 /* Note: We don't have to watch for the main subfile here, type units
8212 don't have DW_AT_name. */
3019eac3 8213
f4dc4d17
DE
8214 if (current_subfile->symtab == NULL)
8215 {
8216 /* NOTE: start_subfile will recognize when it's been passed
8217 a file it has already seen. So we can't assume there's a
8218 simple mapping from lh->file_names to subfiles,
8219 lh->file_names may contain dups. */
8220 current_subfile->symtab = allocate_symtab (current_subfile->name,
8221 objfile);
8222 }
8223
8224 fe->symtab = current_subfile->symtab;
8225 tu_group->symtabs[i] = fe->symtab;
8226 }
8227 }
8228 else
3019eac3 8229 {
f4dc4d17
DE
8230 restart_symtab (0);
8231
8232 for (i = 0; i < lh->num_file_names; ++i)
8233 {
8234 struct file_entry *fe = &lh->file_names[i];
8235
8236 fe->symtab = tu_group->symtabs[i];
8237 }
3019eac3
DE
8238 }
8239
f4dc4d17
DE
8240 /* The main symtab is allocated last. Type units don't have DW_AT_name
8241 so they don't have a "real" (so to speak) symtab anyway.
8242 There is later code that will assign the main symtab to all symbols
8243 that don't have one. We need to handle the case of a symbol with a
8244 missing symtab (DW_AT_decl_file) anyway. */
8245}
3019eac3 8246
f4dc4d17
DE
8247/* Process DW_TAG_type_unit.
8248 For TUs we want to skip the first top level sibling if it's not the
8249 actual type being defined by this TU. In this case the first top
8250 level sibling is there to provide context only. */
3019eac3 8251
f4dc4d17
DE
8252static void
8253read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8254{
8255 struct die_info *child_die;
3019eac3 8256
f4dc4d17
DE
8257 prepare_one_comp_unit (cu, die, language_minimal);
8258
8259 /* Initialize (or reinitialize) the machinery for building symtabs.
8260 We do this before processing child DIEs, so that the line header table
8261 is available for DW_AT_decl_file. */
8262 setup_type_unit_groups (die, cu);
8263
8264 if (die->child != NULL)
8265 {
8266 child_die = die->child;
8267 while (child_die && child_die->tag)
8268 {
8269 process_die (child_die, cu);
8270 child_die = sibling_die (child_die);
8271 }
8272 }
3019eac3
DE
8273}
8274\f
80626a55
DE
8275/* DWO/DWP files.
8276
8277 http://gcc.gnu.org/wiki/DebugFission
8278 http://gcc.gnu.org/wiki/DebugFissionDWP
8279
8280 To simplify handling of both DWO files ("object" files with the DWARF info)
8281 and DWP files (a file with the DWOs packaged up into one file), we treat
8282 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8283
8284static hashval_t
8285hash_dwo_file (const void *item)
8286{
8287 const struct dwo_file *dwo_file = item;
8288
80626a55 8289 return htab_hash_string (dwo_file->name);
3019eac3
DE
8290}
8291
8292static int
8293eq_dwo_file (const void *item_lhs, const void *item_rhs)
8294{
8295 const struct dwo_file *lhs = item_lhs;
8296 const struct dwo_file *rhs = item_rhs;
8297
80626a55 8298 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8299}
8300
8301/* Allocate a hash table for DWO files. */
8302
8303static htab_t
8304allocate_dwo_file_hash_table (void)
8305{
8306 struct objfile *objfile = dwarf2_per_objfile->objfile;
8307
8308 return htab_create_alloc_ex (41,
8309 hash_dwo_file,
8310 eq_dwo_file,
8311 NULL,
8312 &objfile->objfile_obstack,
8313 hashtab_obstack_allocate,
8314 dummy_obstack_deallocate);
8315}
8316
80626a55
DE
8317/* Lookup DWO file DWO_NAME. */
8318
8319static void **
8320lookup_dwo_file_slot (const char *dwo_name)
8321{
8322 struct dwo_file find_entry;
8323 void **slot;
8324
8325 if (dwarf2_per_objfile->dwo_files == NULL)
8326 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8327
8328 memset (&find_entry, 0, sizeof (find_entry));
8329 find_entry.name = dwo_name;
8330 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8331
8332 return slot;
8333}
8334
3019eac3
DE
8335static hashval_t
8336hash_dwo_unit (const void *item)
8337{
8338 const struct dwo_unit *dwo_unit = item;
8339
8340 /* This drops the top 32 bits of the id, but is ok for a hash. */
8341 return dwo_unit->signature;
8342}
8343
8344static int
8345eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8346{
8347 const struct dwo_unit *lhs = item_lhs;
8348 const struct dwo_unit *rhs = item_rhs;
8349
8350 /* The signature is assumed to be unique within the DWO file.
8351 So while object file CU dwo_id's always have the value zero,
8352 that's OK, assuming each object file DWO file has only one CU,
8353 and that's the rule for now. */
8354 return lhs->signature == rhs->signature;
8355}
8356
8357/* Allocate a hash table for DWO CUs,TUs.
8358 There is one of these tables for each of CUs,TUs for each DWO file. */
8359
8360static htab_t
8361allocate_dwo_unit_table (struct objfile *objfile)
8362{
8363 /* Start out with a pretty small number.
8364 Generally DWO files contain only one CU and maybe some TUs. */
8365 return htab_create_alloc_ex (3,
8366 hash_dwo_unit,
8367 eq_dwo_unit,
8368 NULL,
8369 &objfile->objfile_obstack,
8370 hashtab_obstack_allocate,
8371 dummy_obstack_deallocate);
8372}
8373
80626a55 8374/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8375
8376struct create_dwo_info_table_data
8377{
8378 struct dwo_file *dwo_file;
8379 htab_t cu_htab;
8380};
8381
80626a55 8382/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8383
8384static void
80626a55
DE
8385create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8386 gdb_byte *info_ptr,
8387 struct die_info *comp_unit_die,
8388 int has_children,
8389 void *datap)
3019eac3
DE
8390{
8391 struct dwarf2_cu *cu = reader->cu;
8392 struct objfile *objfile = dwarf2_per_objfile->objfile;
8393 sect_offset offset = cu->per_cu->offset;
8a0459fd 8394 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3
DE
8395 struct create_dwo_info_table_data *data = datap;
8396 struct dwo_file *dwo_file = data->dwo_file;
8397 htab_t cu_htab = data->cu_htab;
8398 void **slot;
8399 struct attribute *attr;
8400 struct dwo_unit *dwo_unit;
8401
8402 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8403 if (attr == NULL)
8404 {
8405 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8406 " its dwo_id [in module %s]"),
80626a55 8407 offset.sect_off, dwo_file->name);
3019eac3
DE
8408 return;
8409 }
8410
8411 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8412 dwo_unit->dwo_file = dwo_file;
8413 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8414 dwo_unit->section = section;
3019eac3
DE
8415 dwo_unit->offset = offset;
8416 dwo_unit->length = cu->per_cu->length;
8417
8418 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8419 gdb_assert (slot != NULL);
8420 if (*slot != NULL)
8421 {
8422 const struct dwo_unit *dup_dwo_unit = *slot;
8423
8424 complaint (&symfile_complaints,
8425 _("debug entry at offset 0x%x is duplicate to the entry at"
8426 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8427 offset.sect_off, dup_dwo_unit->offset.sect_off,
8428 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8429 dwo_file->name);
3019eac3
DE
8430 }
8431 else
8432 *slot = dwo_unit;
8433
09406207 8434 if (dwarf2_read_debug)
3019eac3
DE
8435 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8436 offset.sect_off,
8437 phex (dwo_unit->signature,
8438 sizeof (dwo_unit->signature)));
8439}
8440
80626a55
DE
8441/* Create a hash table to map DWO IDs to their CU entry in
8442 .debug_info.dwo in DWO_FILE.
c88ee1f0
DE
8443 Note: This function processes DWO files only, not DWP files.
8444 Note: A DWO file generally contains one CU, but we don't assume this. */
3019eac3
DE
8445
8446static htab_t
80626a55 8447create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8448{
8449 struct objfile *objfile = dwarf2_per_objfile->objfile;
8450 struct dwarf2_section_info *section = &dwo_file->sections.info;
8451 bfd *abfd;
8452 htab_t cu_htab;
8453 gdb_byte *info_ptr, *end_ptr;
8454 struct create_dwo_info_table_data create_dwo_info_table_data;
8455
8456 dwarf2_read_section (objfile, section);
8457 info_ptr = section->buffer;
8458
8459 if (info_ptr == NULL)
8460 return NULL;
8461
8462 /* We can't set abfd until now because the section may be empty or
8463 not present, in which case section->asection will be NULL. */
8464 abfd = section->asection->owner;
8465
09406207 8466 if (dwarf2_read_debug)
3019eac3
DE
8467 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8468 bfd_get_filename (abfd));
8469
8470 cu_htab = allocate_dwo_unit_table (objfile);
8471
8472 create_dwo_info_table_data.dwo_file = dwo_file;
8473 create_dwo_info_table_data.cu_htab = cu_htab;
8474
8475 end_ptr = info_ptr + section->size;
8476 while (info_ptr < end_ptr)
8477 {
8478 struct dwarf2_per_cu_data per_cu;
8479
8480 memset (&per_cu, 0, sizeof (per_cu));
8481 per_cu.objfile = objfile;
8482 per_cu.is_debug_types = 0;
8483 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8484 per_cu.section = section;
3019eac3
DE
8485
8486 init_cutu_and_read_dies_no_follow (&per_cu,
8487 &dwo_file->sections.abbrev,
8488 dwo_file,
80626a55 8489 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8490 &create_dwo_info_table_data);
8491
8492 info_ptr += per_cu.length;
8493 }
8494
8495 return cu_htab;
8496}
8497
80626a55
DE
8498/* DWP file .debug_{cu,tu}_index section format:
8499 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8500
8501 Both index sections have the same format, and serve to map a 64-bit
8502 signature to a set of section numbers. Each section begins with a header,
8503 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8504 indexes, and a pool of 32-bit section numbers. The index sections will be
8505 aligned at 8-byte boundaries in the file.
8506
8507 The index section header contains two unsigned 32-bit values (using the
8508 byte order of the application binary):
8509
8510 N, the number of compilation units or type units in the index
8511 M, the number of slots in the hash table
8512
8513 (We assume that N and M will not exceed 2^32 - 1.)
8514
8515 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8516
8517 The hash table begins at offset 8 in the section, and consists of an array
8518 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8519 order of the application binary). Unused slots in the hash table are 0.
8520 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8521
8522 The parallel table begins immediately after the hash table
8523 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8524 array of 32-bit indexes (using the byte order of the application binary),
8525 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8526 table contains a 32-bit index into the pool of section numbers. For unused
8527 hash table slots, the corresponding entry in the parallel table will be 0.
8528
8529 Given a 64-bit compilation unit signature or a type signature S, an entry
8530 in the hash table is located as follows:
8531
8532 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8533 the low-order k bits all set to 1.
8534
8535 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8536
8537 3) If the hash table entry at index H matches the signature, use that
8538 entry. If the hash table entry at index H is unused (all zeroes),
8539 terminate the search: the signature is not present in the table.
8540
8541 4) Let H = (H + H') modulo M. Repeat at Step 3.
8542
8543 Because M > N and H' and M are relatively prime, the search is guaranteed
8544 to stop at an unused slot or find the match.
8545
8546 The pool of section numbers begins immediately following the hash table
8547 (at offset 8 + 12 * M from the beginning of the section). The pool of
8548 section numbers consists of an array of 32-bit words (using the byte order
8549 of the application binary). Each item in the array is indexed starting
8550 from 0. The hash table entry provides the index of the first section
8551 number in the set. Additional section numbers in the set follow, and the
8552 set is terminated by a 0 entry (section number 0 is not used in ELF).
8553
8554 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8555 section must be the first entry in the set, and the .debug_abbrev.dwo must
8556 be the second entry. Other members of the set may follow in any order. */
8557
8558/* Create a hash table to map DWO IDs to their CU/TU entry in
8559 .debug_{info,types}.dwo in DWP_FILE.
8560 Returns NULL if there isn't one.
8561 Note: This function processes DWP files only, not DWO files. */
8562
8563static struct dwp_hash_table *
8564create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8565{
8566 struct objfile *objfile = dwarf2_per_objfile->objfile;
8567 bfd *dbfd = dwp_file->dbfd;
8568 char *index_ptr, *index_end;
8569 struct dwarf2_section_info *index;
8570 uint32_t version, nr_units, nr_slots;
8571 struct dwp_hash_table *htab;
8572
8573 if (is_debug_types)
8574 index = &dwp_file->sections.tu_index;
8575 else
8576 index = &dwp_file->sections.cu_index;
8577
8578 if (dwarf2_section_empty_p (index))
8579 return NULL;
8580 dwarf2_read_section (objfile, index);
8581
8582 index_ptr = index->buffer;
8583 index_end = index_ptr + index->size;
8584
8585 version = read_4_bytes (dbfd, index_ptr);
8586 index_ptr += 8; /* Skip the unused word. */
8587 nr_units = read_4_bytes (dbfd, index_ptr);
8588 index_ptr += 4;
8589 nr_slots = read_4_bytes (dbfd, index_ptr);
8590 index_ptr += 4;
8591
8592 if (version != 1)
8593 {
8594 error (_("Dwarf Error: unsupported DWP file version (%u)"
8595 " [in module %s]"),
8596 version, dwp_file->name);
8597 }
8598 if (nr_slots != (nr_slots & -nr_slots))
8599 {
8600 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8601 " is not power of 2 [in module %s]"),
8602 nr_slots, dwp_file->name);
8603 }
8604
8605 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8606 htab->nr_units = nr_units;
8607 htab->nr_slots = nr_slots;
8608 htab->hash_table = index_ptr;
8609 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8610 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8611
8612 return htab;
8613}
8614
8615/* Update SECTIONS with the data from SECTP.
8616
8617 This function is like the other "locate" section routines that are
8618 passed to bfd_map_over_sections, but in this context the sections to
8619 read comes from the DWP hash table, not the full ELF section table.
8620
8621 The result is non-zero for success, or zero if an error was found. */
8622
8623static int
8624locate_virtual_dwo_sections (asection *sectp,
8625 struct virtual_dwo_sections *sections)
8626{
8627 const struct dwop_section_names *names = &dwop_section_names;
8628
8629 if (section_is_p (sectp->name, &names->abbrev_dwo))
8630 {
8631 /* There can be only one. */
8632 if (sections->abbrev.asection != NULL)
8633 return 0;
8634 sections->abbrev.asection = sectp;
8635 sections->abbrev.size = bfd_get_section_size (sectp);
8636 }
8637 else if (section_is_p (sectp->name, &names->info_dwo)
8638 || section_is_p (sectp->name, &names->types_dwo))
8639 {
8640 /* There can be only one. */
8641 if (sections->info_or_types.asection != NULL)
8642 return 0;
8643 sections->info_or_types.asection = sectp;
8644 sections->info_or_types.size = bfd_get_section_size (sectp);
8645 }
8646 else if (section_is_p (sectp->name, &names->line_dwo))
8647 {
8648 /* There can be only one. */
8649 if (sections->line.asection != NULL)
8650 return 0;
8651 sections->line.asection = sectp;
8652 sections->line.size = bfd_get_section_size (sectp);
8653 }
8654 else if (section_is_p (sectp->name, &names->loc_dwo))
8655 {
8656 /* There can be only one. */
8657 if (sections->loc.asection != NULL)
8658 return 0;
8659 sections->loc.asection = sectp;
8660 sections->loc.size = bfd_get_section_size (sectp);
8661 }
8662 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8663 {
8664 /* There can be only one. */
8665 if (sections->macinfo.asection != NULL)
8666 return 0;
8667 sections->macinfo.asection = sectp;
8668 sections->macinfo.size = bfd_get_section_size (sectp);
8669 }
8670 else if (section_is_p (sectp->name, &names->macro_dwo))
8671 {
8672 /* There can be only one. */
8673 if (sections->macro.asection != NULL)
8674 return 0;
8675 sections->macro.asection = sectp;
8676 sections->macro.size = bfd_get_section_size (sectp);
8677 }
8678 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8679 {
8680 /* There can be only one. */
8681 if (sections->str_offsets.asection != NULL)
8682 return 0;
8683 sections->str_offsets.asection = sectp;
8684 sections->str_offsets.size = bfd_get_section_size (sectp);
8685 }
8686 else
8687 {
8688 /* No other kind of section is valid. */
8689 return 0;
8690 }
8691
8692 return 1;
8693}
8694
8695/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8696 HTAB is the hash table from the DWP file.
8697 SECTION_INDEX is the index of the DWO in HTAB. */
8698
8699static struct dwo_unit *
8700create_dwo_in_dwp (struct dwp_file *dwp_file,
8701 const struct dwp_hash_table *htab,
8702 uint32_t section_index,
8703 ULONGEST signature, int is_debug_types)
8704{
8705 struct objfile *objfile = dwarf2_per_objfile->objfile;
8706 bfd *dbfd = dwp_file->dbfd;
8707 const char *kind = is_debug_types ? "TU" : "CU";
8708 struct dwo_file *dwo_file;
8709 struct dwo_unit *dwo_unit;
8710 struct virtual_dwo_sections sections;
8711 void **dwo_file_slot;
8712 char *virtual_dwo_name;
8713 struct dwarf2_section_info *cutu;
8714 struct cleanup *cleanups;
8715 int i;
8716
8717 if (dwarf2_read_debug)
8718 {
8719 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8720 kind,
8721 section_index, phex (signature, sizeof (signature)),
8722 dwp_file->name);
8723 }
8724
8725 /* Fetch the sections of this DWO.
8726 Put a limit on the number of sections we look for so that bad data
8727 doesn't cause us to loop forever. */
8728
8729#define MAX_NR_DWO_SECTIONS \
8730 (1 /* .debug_info or .debug_types */ \
8731 + 1 /* .debug_abbrev */ \
8732 + 1 /* .debug_line */ \
8733 + 1 /* .debug_loc */ \
8734 + 1 /* .debug_str_offsets */ \
8735 + 1 /* .debug_macro */ \
8736 + 1 /* .debug_macinfo */ \
8737 + 1 /* trailing zero */)
8738
8739 memset (&sections, 0, sizeof (sections));
8740 cleanups = make_cleanup (null_cleanup, 0);
8741
8742 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8743 {
8744 asection *sectp;
8745 uint32_t section_nr =
8746 read_4_bytes (dbfd,
8747 htab->section_pool
8748 + (section_index + i) * sizeof (uint32_t));
8749
8750 if (section_nr == 0)
8751 break;
8752 if (section_nr >= dwp_file->num_sections)
8753 {
8754 error (_("Dwarf Error: bad DWP hash table, section number too large"
8755 " [in module %s]"),
8756 dwp_file->name);
8757 }
8758
8759 sectp = dwp_file->elf_sections[section_nr];
8760 if (! locate_virtual_dwo_sections (sectp, &sections))
8761 {
8762 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8763 " [in module %s]"),
8764 dwp_file->name);
8765 }
8766 }
8767
8768 if (i < 2
8769 || sections.info_or_types.asection == NULL
8770 || sections.abbrev.asection == NULL)
8771 {
8772 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8773 " [in module %s]"),
8774 dwp_file->name);
8775 }
8776 if (i == MAX_NR_DWO_SECTIONS)
8777 {
8778 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8779 " [in module %s]"),
8780 dwp_file->name);
8781 }
8782
8783 /* It's easier for the rest of the code if we fake a struct dwo_file and
8784 have dwo_unit "live" in that. At least for now.
8785
8786 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8787 However, for each CU + set of TUs that came from the same original DWO
8788 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8789 (fewer struct dwo_file objects to allocated). Remember that for really
8790 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8791
2792b94d
PM
8792 virtual_dwo_name =
8793 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8794 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8795 sections.line.asection ? sections.line.asection->id : 0,
8796 sections.loc.asection ? sections.loc.asection->id : 0,
8797 (sections.str_offsets.asection
8798 ? sections.str_offsets.asection->id
8799 : 0));
80626a55
DE
8800 make_cleanup (xfree, virtual_dwo_name);
8801 /* Can we use an existing virtual DWO file? */
8802 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8803 /* Create one if necessary. */
8804 if (*dwo_file_slot == NULL)
8805 {
8806 if (dwarf2_read_debug)
8807 {
8808 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8809 virtual_dwo_name);
8810 }
8811 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8812 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8813 virtual_dwo_name,
8814 strlen (virtual_dwo_name));
8815 dwo_file->sections.abbrev = sections.abbrev;
8816 dwo_file->sections.line = sections.line;
8817 dwo_file->sections.loc = sections.loc;
8818 dwo_file->sections.macinfo = sections.macinfo;
8819 dwo_file->sections.macro = sections.macro;
8820 dwo_file->sections.str_offsets = sections.str_offsets;
8821 /* The "str" section is global to the entire DWP file. */
8822 dwo_file->sections.str = dwp_file->sections.str;
8823 /* The info or types section is assigned later to dwo_unit,
8824 there's no need to record it in dwo_file.
8825 Also, we can't simply record type sections in dwo_file because
8826 we record a pointer into the vector in dwo_unit. As we collect more
8827 types we'll grow the vector and eventually have to reallocate space
8828 for it, invalidating all the pointers into the current copy. */
8829 *dwo_file_slot = dwo_file;
8830 }
8831 else
8832 {
8833 if (dwarf2_read_debug)
8834 {
8835 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8836 virtual_dwo_name);
8837 }
8838 dwo_file = *dwo_file_slot;
8839 }
8840 do_cleanups (cleanups);
8841
8842 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8843 dwo_unit->dwo_file = dwo_file;
8844 dwo_unit->signature = signature;
8a0459fd
DE
8845 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8846 sizeof (struct dwarf2_section_info));
8847 *dwo_unit->section = sections.info_or_types;
80626a55
DE
8848 /* offset, length, type_offset_in_tu are set later. */
8849
8850 return dwo_unit;
8851}
8852
8853/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8854
8855static struct dwo_unit *
8856lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8857 const struct dwp_hash_table *htab,
8858 ULONGEST signature, int is_debug_types)
8859{
8860 bfd *dbfd = dwp_file->dbfd;
8861 uint32_t mask = htab->nr_slots - 1;
8862 uint32_t hash = signature & mask;
8863 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8864 unsigned int i;
8865 void **slot;
8866 struct dwo_unit find_dwo_cu, *dwo_cu;
8867
8868 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8869 find_dwo_cu.signature = signature;
8870 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8871
8872 if (*slot != NULL)
8873 return *slot;
8874
8875 /* Use a for loop so that we don't loop forever on bad debug info. */
8876 for (i = 0; i < htab->nr_slots; ++i)
8877 {
8878 ULONGEST signature_in_table;
8879
8880 signature_in_table =
8881 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8882 if (signature_in_table == signature)
8883 {
8884 uint32_t section_index =
8885 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8886
8887 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8888 signature, is_debug_types);
8889 return *slot;
8890 }
8891 if (signature_in_table == 0)
8892 return NULL;
8893 hash = (hash + hash2) & mask;
8894 }
8895
8896 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8897 " [in module %s]"),
8898 dwp_file->name);
8899}
8900
ab5088bf 8901/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
8902 Open the file specified by FILE_NAME and hand it off to BFD for
8903 preliminary analysis. Return a newly initialized bfd *, which
8904 includes a canonicalized copy of FILE_NAME.
80626a55 8905 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8906 In case of trouble, return NULL.
8907 NOTE: This function is derived from symfile_bfd_open. */
8908
8909static bfd *
80626a55 8910try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8911{
8912 bfd *sym_bfd;
80626a55 8913 int desc, flags;
3019eac3 8914 char *absolute_name;
3019eac3 8915
80626a55
DE
8916 flags = OPF_TRY_CWD_FIRST;
8917 if (is_dwp)
8918 flags |= OPF_SEARCH_IN_PATH;
8919 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8920 O_RDONLY | O_BINARY, &absolute_name);
8921 if (desc < 0)
8922 return NULL;
8923
bb397797 8924 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8925 if (!sym_bfd)
8926 {
3019eac3
DE
8927 xfree (absolute_name);
8928 return NULL;
8929 }
a4453b7e 8930 xfree (absolute_name);
3019eac3
DE
8931 bfd_set_cacheable (sym_bfd, 1);
8932
8933 if (!bfd_check_format (sym_bfd, bfd_object))
8934 {
cbb099e8 8935 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8936 return NULL;
8937 }
8938
3019eac3
DE
8939 return sym_bfd;
8940}
8941
ab5088bf 8942/* Try to open DWO file FILE_NAME.
3019eac3
DE
8943 COMP_DIR is the DW_AT_comp_dir attribute.
8944 The result is the bfd handle of the file.
8945 If there is a problem finding or opening the file, return NULL.
8946 Upon success, the canonicalized path of the file is stored in the bfd,
8947 same as symfile_bfd_open. */
8948
8949static bfd *
ab5088bf 8950open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
8951{
8952 bfd *abfd;
3019eac3 8953
80626a55 8954 if (IS_ABSOLUTE_PATH (file_name))
ab5088bf 8955 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
8956
8957 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8958
8959 if (comp_dir != NULL)
8960 {
80626a55 8961 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8962
8963 /* NOTE: If comp_dir is a relative path, this will also try the
8964 search path, which seems useful. */
ab5088bf 8965 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
3019eac3
DE
8966 xfree (path_to_try);
8967 if (abfd != NULL)
8968 return abfd;
8969 }
8970
8971 /* That didn't work, try debug-file-directory, which, despite its name,
8972 is a list of paths. */
8973
8974 if (*debug_file_directory == '\0')
8975 return NULL;
8976
ab5088bf 8977 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
8978}
8979
80626a55
DE
8980/* This function is mapped across the sections and remembers the offset and
8981 size of each of the DWO debugging sections we are interested in. */
8982
8983static void
8984dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8985{
8986 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8987 const struct dwop_section_names *names = &dwop_section_names;
8988
8989 if (section_is_p (sectp->name, &names->abbrev_dwo))
8990 {
8991 dwo_sections->abbrev.asection = sectp;
8992 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8993 }
8994 else if (section_is_p (sectp->name, &names->info_dwo))
8995 {
8996 dwo_sections->info.asection = sectp;
8997 dwo_sections->info.size = bfd_get_section_size (sectp);
8998 }
8999 else if (section_is_p (sectp->name, &names->line_dwo))
9000 {
9001 dwo_sections->line.asection = sectp;
9002 dwo_sections->line.size = bfd_get_section_size (sectp);
9003 }
9004 else if (section_is_p (sectp->name, &names->loc_dwo))
9005 {
9006 dwo_sections->loc.asection = sectp;
9007 dwo_sections->loc.size = bfd_get_section_size (sectp);
9008 }
9009 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9010 {
9011 dwo_sections->macinfo.asection = sectp;
9012 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9013 }
9014 else if (section_is_p (sectp->name, &names->macro_dwo))
9015 {
9016 dwo_sections->macro.asection = sectp;
9017 dwo_sections->macro.size = bfd_get_section_size (sectp);
9018 }
9019 else if (section_is_p (sectp->name, &names->str_dwo))
9020 {
9021 dwo_sections->str.asection = sectp;
9022 dwo_sections->str.size = bfd_get_section_size (sectp);
9023 }
9024 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9025 {
9026 dwo_sections->str_offsets.asection = sectp;
9027 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9028 }
9029 else if (section_is_p (sectp->name, &names->types_dwo))
9030 {
9031 struct dwarf2_section_info type_section;
9032
9033 memset (&type_section, 0, sizeof (type_section));
9034 type_section.asection = sectp;
9035 type_section.size = bfd_get_section_size (sectp);
9036 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9037 &type_section);
9038 }
9039}
9040
ab5088bf
DE
9041/* Initialize the use of the DWO file specified by DWO_NAME and referenced
9042 by PER_CU.
80626a55 9043 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9044
9045static struct dwo_file *
80626a55 9046open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
9047{
9048 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9049 struct dwo_file *dwo_file;
9050 bfd *dbfd;
3019eac3
DE
9051 struct cleanup *cleanups;
9052
ab5088bf 9053 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9054 if (dbfd == NULL)
9055 {
9056 if (dwarf2_read_debug)
9057 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9058 return NULL;
9059 }
9060 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9061 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
9062 dwo_name, strlen (dwo_name));
9063 dwo_file->dbfd = dbfd;
3019eac3
DE
9064
9065 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9066
80626a55 9067 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9068
80626a55 9069 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
9070
9071 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9072 dwo_file->sections.types);
9073
9074 discard_cleanups (cleanups);
9075
80626a55
DE
9076 if (dwarf2_read_debug)
9077 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9078
3019eac3
DE
9079 return dwo_file;
9080}
9081
80626a55
DE
9082/* This function is mapped across the sections and remembers the offset and
9083 size of each of the DWP debugging sections we are interested in. */
3019eac3 9084
80626a55
DE
9085static void
9086dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9087{
80626a55
DE
9088 struct dwp_file *dwp_file = dwp_file_ptr;
9089 const struct dwop_section_names *names = &dwop_section_names;
9090 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9091
80626a55
DE
9092 /* Record the ELF section number for later lookup: this is what the
9093 .debug_cu_index,.debug_tu_index tables use. */
9094 gdb_assert (elf_section_nr < dwp_file->num_sections);
9095 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9096
80626a55
DE
9097 /* Look for specific sections that we need. */
9098 if (section_is_p (sectp->name, &names->str_dwo))
9099 {
9100 dwp_file->sections.str.asection = sectp;
9101 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9102 }
9103 else if (section_is_p (sectp->name, &names->cu_index))
9104 {
9105 dwp_file->sections.cu_index.asection = sectp;
9106 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9107 }
9108 else if (section_is_p (sectp->name, &names->tu_index))
9109 {
9110 dwp_file->sections.tu_index.asection = sectp;
9111 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9112 }
9113}
3019eac3 9114
80626a55 9115/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9116
80626a55
DE
9117static hashval_t
9118hash_dwp_loaded_cutus (const void *item)
9119{
9120 const struct dwo_unit *dwo_unit = item;
3019eac3 9121
80626a55
DE
9122 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9123 return dwo_unit->signature;
3019eac3
DE
9124}
9125
80626a55 9126/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9127
80626a55
DE
9128static int
9129eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9130{
80626a55
DE
9131 const struct dwo_unit *dua = a;
9132 const struct dwo_unit *dub = b;
3019eac3 9133
80626a55
DE
9134 return dua->signature == dub->signature;
9135}
3019eac3 9136
80626a55 9137/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9138
80626a55
DE
9139static htab_t
9140allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9141{
9142 return htab_create_alloc_ex (3,
9143 hash_dwp_loaded_cutus,
9144 eq_dwp_loaded_cutus,
9145 NULL,
9146 &objfile->objfile_obstack,
9147 hashtab_obstack_allocate,
9148 dummy_obstack_deallocate);
9149}
3019eac3 9150
ab5088bf
DE
9151/* Try to open DWP file FILE_NAME.
9152 The result is the bfd handle of the file.
9153 If there is a problem finding or opening the file, return NULL.
9154 Upon success, the canonicalized path of the file is stored in the bfd,
9155 same as symfile_bfd_open. */
9156
9157static bfd *
9158open_dwp_file (const char *file_name)
9159{
9160 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9161}
9162
80626a55
DE
9163/* Initialize the use of the DWP file for the current objfile.
9164 By convention the name of the DWP file is ${objfile}.dwp.
9165 The result is NULL if it can't be found. */
a766d390 9166
80626a55 9167static struct dwp_file *
ab5088bf 9168open_and_init_dwp_file (void)
80626a55
DE
9169{
9170 struct objfile *objfile = dwarf2_per_objfile->objfile;
9171 struct dwp_file *dwp_file;
9172 char *dwp_name;
9173 bfd *dbfd;
9174 struct cleanup *cleanups;
9175
2792b94d 9176 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9177 cleanups = make_cleanup (xfree, dwp_name);
9178
ab5088bf 9179 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9180 if (dbfd == NULL)
9181 {
9182 if (dwarf2_read_debug)
9183 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9184 do_cleanups (cleanups);
9185 return NULL;
3019eac3 9186 }
80626a55
DE
9187 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9188 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9189 dwp_name, strlen (dwp_name));
9190 dwp_file->dbfd = dbfd;
9191 do_cleanups (cleanups);
c906108c 9192
80626a55
DE
9193 /* +1: section 0 is unused */
9194 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9195 dwp_file->elf_sections =
9196 OBSTACK_CALLOC (&objfile->objfile_obstack,
9197 dwp_file->num_sections, asection *);
9198
9199 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9200
9201 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9202
9203 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9204
9205 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9206
80626a55
DE
9207 if (dwarf2_read_debug)
9208 {
9209 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9210 fprintf_unfiltered (gdb_stdlog,
9211 " %u CUs, %u TUs\n",
9212 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9213 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9214 }
9215
9216 return dwp_file;
3019eac3 9217}
c906108c 9218
ab5088bf
DE
9219/* Wrapper around open_and_init_dwp_file, only open it once. */
9220
9221static struct dwp_file *
9222get_dwp_file (void)
9223{
9224 if (! dwarf2_per_objfile->dwp_checked)
9225 {
9226 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9227 dwarf2_per_objfile->dwp_checked = 1;
9228 }
9229 return dwarf2_per_objfile->dwp_file;
9230}
9231
80626a55
DE
9232/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9233 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9234 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9235 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9236 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9237
9238 This is called, for example, when wanting to read a variable with a
9239 complex location. Therefore we don't want to do file i/o for every call.
9240 Therefore we don't want to look for a DWO file on every call.
9241 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9242 then we check if we've already seen DWO_NAME, and only THEN do we check
9243 for a DWO file.
9244
1c658ad5 9245 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9246 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9247
3019eac3 9248static struct dwo_unit *
80626a55
DE
9249lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9250 const char *dwo_name, const char *comp_dir,
9251 ULONGEST signature, int is_debug_types)
3019eac3
DE
9252{
9253 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9254 const char *kind = is_debug_types ? "TU" : "CU";
9255 void **dwo_file_slot;
3019eac3 9256 struct dwo_file *dwo_file;
80626a55 9257 struct dwp_file *dwp_file;
cb1df416 9258
80626a55 9259 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9260
ab5088bf 9261 dwp_file = get_dwp_file ();
80626a55 9262 if (dwp_file != NULL)
cf2c3c16 9263 {
80626a55
DE
9264 const struct dwp_hash_table *dwp_htab =
9265 is_debug_types ? dwp_file->tus : dwp_file->cus;
9266
9267 if (dwp_htab != NULL)
9268 {
9269 struct dwo_unit *dwo_cutu =
9270 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9271
9272 if (dwo_cutu != NULL)
9273 {
9274 if (dwarf2_read_debug)
9275 {
9276 fprintf_unfiltered (gdb_stdlog,
9277 "Virtual DWO %s %s found: @%s\n",
9278 kind, hex_string (signature),
9279 host_address_to_string (dwo_cutu));
9280 }
9281 return dwo_cutu;
9282 }
9283 }
9284 }
9285
9286 /* Have we already seen DWO_NAME? */
9287
9288 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9289 if (*dwo_file_slot == NULL)
9290 {
9291 /* Read in the file and build a table of the DWOs it contains. */
9292 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9293 }
9294 /* NOTE: This will be NULL if unable to open the file. */
9295 dwo_file = *dwo_file_slot;
9296
9297 if (dwo_file != NULL)
9298 {
9299 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9300
9301 if (htab != NULL)
9302 {
9303 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9304
80626a55
DE
9305 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9306 find_dwo_cutu.signature = signature;
9307 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9308
80626a55
DE
9309 if (dwo_cutu != NULL)
9310 {
9311 if (dwarf2_read_debug)
9312 {
9313 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9314 kind, dwo_name, hex_string (signature),
9315 host_address_to_string (dwo_cutu));
9316 }
9317 return dwo_cutu;
9318 }
9319 }
2e276125 9320 }
9cdd5dbd 9321
80626a55
DE
9322 /* We didn't find it. This could mean a dwo_id mismatch, or
9323 someone deleted the DWO/DWP file, or the search path isn't set up
9324 correctly to find the file. */
9325
9326 if (dwarf2_read_debug)
9327 {
9328 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9329 kind, dwo_name, hex_string (signature));
9330 }
3019eac3
DE
9331
9332 complaint (&symfile_complaints,
6296d8c1 9333 _("Could not find DWO %s referenced by CU at offset 0x%x"
3019eac3 9334 " [in module %s]"),
6296d8c1 9335 kind, this_unit->offset.sect_off, objfile->name);
3019eac3 9336 return NULL;
5fb290d7
DJ
9337}
9338
80626a55
DE
9339/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9340 See lookup_dwo_cutu_unit for details. */
9341
9342static struct dwo_unit *
9343lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9344 const char *dwo_name, const char *comp_dir,
9345 ULONGEST signature)
9346{
9347 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9348}
9349
9350/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9351 See lookup_dwo_cutu_unit for details. */
9352
9353static struct dwo_unit *
9354lookup_dwo_type_unit (struct signatured_type *this_tu,
9355 const char *dwo_name, const char *comp_dir)
9356{
9357 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9358}
9359
3019eac3
DE
9360/* Free all resources associated with DWO_FILE.
9361 Close the DWO file and munmap the sections.
9362 All memory should be on the objfile obstack. */
348e048f
DE
9363
9364static void
3019eac3 9365free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9366{
3019eac3
DE
9367 int ix;
9368 struct dwarf2_section_info *section;
348e048f 9369
5c6fa7ab 9370 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9371 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9372
3019eac3
DE
9373 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9374}
348e048f 9375
3019eac3 9376/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9377
3019eac3
DE
9378static void
9379free_dwo_file_cleanup (void *arg)
9380{
9381 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9382 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9383
3019eac3
DE
9384 free_dwo_file (dwo_file, objfile);
9385}
348e048f 9386
3019eac3 9387/* Traversal function for free_dwo_files. */
2ab95328 9388
3019eac3
DE
9389static int
9390free_dwo_file_from_slot (void **slot, void *info)
9391{
9392 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9393 struct objfile *objfile = (struct objfile *) info;
348e048f 9394
3019eac3 9395 free_dwo_file (dwo_file, objfile);
348e048f 9396
3019eac3
DE
9397 return 1;
9398}
348e048f 9399
3019eac3 9400/* Free all resources associated with DWO_FILES. */
348e048f 9401
3019eac3
DE
9402static void
9403free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9404{
9405 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9406}
3019eac3
DE
9407\f
9408/* Read in various DIEs. */
348e048f 9409
d389af10
JK
9410/* qsort helper for inherit_abstract_dies. */
9411
9412static int
9413unsigned_int_compar (const void *ap, const void *bp)
9414{
9415 unsigned int a = *(unsigned int *) ap;
9416 unsigned int b = *(unsigned int *) bp;
9417
9418 return (a > b) - (b > a);
9419}
9420
9421/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9422 Inherit only the children of the DW_AT_abstract_origin DIE not being
9423 already referenced by DW_AT_abstract_origin from the children of the
9424 current DIE. */
d389af10
JK
9425
9426static void
9427inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9428{
9429 struct die_info *child_die;
9430 unsigned die_children_count;
9431 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9432 sect_offset *offsets;
9433 sect_offset *offsets_end, *offsetp;
d389af10
JK
9434 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9435 struct die_info *origin_die;
9436 /* Iterator of the ORIGIN_DIE children. */
9437 struct die_info *origin_child_die;
9438 struct cleanup *cleanups;
9439 struct attribute *attr;
cd02d79d
PA
9440 struct dwarf2_cu *origin_cu;
9441 struct pending **origin_previous_list_in_scope;
d389af10
JK
9442
9443 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9444 if (!attr)
9445 return;
9446
cd02d79d
PA
9447 /* Note that following die references may follow to a die in a
9448 different cu. */
9449
9450 origin_cu = cu;
9451 origin_die = follow_die_ref (die, attr, &origin_cu);
9452
9453 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9454 symbols in. */
9455 origin_previous_list_in_scope = origin_cu->list_in_scope;
9456 origin_cu->list_in_scope = cu->list_in_scope;
9457
edb3359d
DJ
9458 if (die->tag != origin_die->tag
9459 && !(die->tag == DW_TAG_inlined_subroutine
9460 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9461 complaint (&symfile_complaints,
9462 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9463 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9464
9465 child_die = die->child;
9466 die_children_count = 0;
9467 while (child_die && child_die->tag)
9468 {
9469 child_die = sibling_die (child_die);
9470 die_children_count++;
9471 }
9472 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9473 cleanups = make_cleanup (xfree, offsets);
9474
9475 offsets_end = offsets;
9476 child_die = die->child;
9477 while (child_die && child_die->tag)
9478 {
c38f313d
DJ
9479 /* For each CHILD_DIE, find the corresponding child of
9480 ORIGIN_DIE. If there is more than one layer of
9481 DW_AT_abstract_origin, follow them all; there shouldn't be,
9482 but GCC versions at least through 4.4 generate this (GCC PR
9483 40573). */
9484 struct die_info *child_origin_die = child_die;
cd02d79d 9485 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9486
c38f313d
DJ
9487 while (1)
9488 {
cd02d79d
PA
9489 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9490 child_origin_cu);
c38f313d
DJ
9491 if (attr == NULL)
9492 break;
cd02d79d
PA
9493 child_origin_die = follow_die_ref (child_origin_die, attr,
9494 &child_origin_cu);
c38f313d
DJ
9495 }
9496
d389af10
JK
9497 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9498 counterpart may exist. */
c38f313d 9499 if (child_origin_die != child_die)
d389af10 9500 {
edb3359d
DJ
9501 if (child_die->tag != child_origin_die->tag
9502 && !(child_die->tag == DW_TAG_inlined_subroutine
9503 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9504 complaint (&symfile_complaints,
9505 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9506 "different tags"), child_die->offset.sect_off,
9507 child_origin_die->offset.sect_off);
c38f313d
DJ
9508 if (child_origin_die->parent != origin_die)
9509 complaint (&symfile_complaints,
9510 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9511 "different parents"), child_die->offset.sect_off,
9512 child_origin_die->offset.sect_off);
c38f313d
DJ
9513 else
9514 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9515 }
9516 child_die = sibling_die (child_die);
9517 }
9518 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9519 unsigned_int_compar);
9520 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9521 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9522 complaint (&symfile_complaints,
9523 _("Multiple children of DIE 0x%x refer "
9524 "to DIE 0x%x as their abstract origin"),
b64f50a1 9525 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9526
9527 offsetp = offsets;
9528 origin_child_die = origin_die->child;
9529 while (origin_child_die && origin_child_die->tag)
9530 {
9531 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9532 while (offsetp < offsets_end
9533 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9534 offsetp++;
b64f50a1
JK
9535 if (offsetp >= offsets_end
9536 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9537 {
9538 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9539 process_die (origin_child_die, origin_cu);
d389af10
JK
9540 }
9541 origin_child_die = sibling_die (origin_child_die);
9542 }
cd02d79d 9543 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9544
9545 do_cleanups (cleanups);
9546}
9547
c906108c 9548static void
e7c27a73 9549read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9550{
e7c27a73 9551 struct objfile *objfile = cu->objfile;
52f0bd74 9552 struct context_stack *new;
c906108c
SS
9553 CORE_ADDR lowpc;
9554 CORE_ADDR highpc;
9555 struct die_info *child_die;
edb3359d 9556 struct attribute *attr, *call_line, *call_file;
15d034d0 9557 const char *name;
e142c38c 9558 CORE_ADDR baseaddr;
801e3a5b 9559 struct block *block;
edb3359d 9560 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9561 VEC (symbolp) *template_args = NULL;
9562 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9563
9564 if (inlined_func)
9565 {
9566 /* If we do not have call site information, we can't show the
9567 caller of this inlined function. That's too confusing, so
9568 only use the scope for local variables. */
9569 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9570 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9571 if (call_line == NULL || call_file == NULL)
9572 {
9573 read_lexical_block_scope (die, cu);
9574 return;
9575 }
9576 }
c906108c 9577
e142c38c
DJ
9578 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9579
94af9270 9580 name = dwarf2_name (die, cu);
c906108c 9581
e8d05480
JB
9582 /* Ignore functions with missing or empty names. These are actually
9583 illegal according to the DWARF standard. */
9584 if (name == NULL)
9585 {
9586 complaint (&symfile_complaints,
b64f50a1
JK
9587 _("missing name for subprogram DIE at %d"),
9588 die->offset.sect_off);
e8d05480
JB
9589 return;
9590 }
9591
9592 /* Ignore functions with missing or invalid low and high pc attributes. */
9593 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9594 {
ae4d0c03
PM
9595 attr = dwarf2_attr (die, DW_AT_external, cu);
9596 if (!attr || !DW_UNSND (attr))
9597 complaint (&symfile_complaints,
3e43a32a
MS
9598 _("cannot get low and high bounds "
9599 "for subprogram DIE at %d"),
b64f50a1 9600 die->offset.sect_off);
e8d05480
JB
9601 return;
9602 }
c906108c
SS
9603
9604 lowpc += baseaddr;
9605 highpc += baseaddr;
9606
34eaf542
TT
9607 /* If we have any template arguments, then we must allocate a
9608 different sort of symbol. */
9609 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9610 {
9611 if (child_die->tag == DW_TAG_template_type_param
9612 || child_die->tag == DW_TAG_template_value_param)
9613 {
9614 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9615 struct template_symbol);
9616 templ_func->base.is_cplus_template_function = 1;
9617 break;
9618 }
9619 }
9620
c906108c 9621 new = push_context (0, lowpc);
34eaf542
TT
9622 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9623 (struct symbol *) templ_func);
4c2df51b 9624
4cecd739
DJ
9625 /* If there is a location expression for DW_AT_frame_base, record
9626 it. */
e142c38c 9627 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9628 if (attr)
f1e6e072 9629 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9630
e142c38c 9631 cu->list_in_scope = &local_symbols;
c906108c 9632
639d11d3 9633 if (die->child != NULL)
c906108c 9634 {
639d11d3 9635 child_die = die->child;
c906108c
SS
9636 while (child_die && child_die->tag)
9637 {
34eaf542
TT
9638 if (child_die->tag == DW_TAG_template_type_param
9639 || child_die->tag == DW_TAG_template_value_param)
9640 {
9641 struct symbol *arg = new_symbol (child_die, NULL, cu);
9642
f1078f66
DJ
9643 if (arg != NULL)
9644 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9645 }
9646 else
9647 process_die (child_die, cu);
c906108c
SS
9648 child_die = sibling_die (child_die);
9649 }
9650 }
9651
d389af10
JK
9652 inherit_abstract_dies (die, cu);
9653
4a811a97
UW
9654 /* If we have a DW_AT_specification, we might need to import using
9655 directives from the context of the specification DIE. See the
9656 comment in determine_prefix. */
9657 if (cu->language == language_cplus
9658 && dwarf2_attr (die, DW_AT_specification, cu))
9659 {
9660 struct dwarf2_cu *spec_cu = cu;
9661 struct die_info *spec_die = die_specification (die, &spec_cu);
9662
9663 while (spec_die)
9664 {
9665 child_die = spec_die->child;
9666 while (child_die && child_die->tag)
9667 {
9668 if (child_die->tag == DW_TAG_imported_module)
9669 process_die (child_die, spec_cu);
9670 child_die = sibling_die (child_die);
9671 }
9672
9673 /* In some cases, GCC generates specification DIEs that
9674 themselves contain DW_AT_specification attributes. */
9675 spec_die = die_specification (spec_die, &spec_cu);
9676 }
9677 }
9678
c906108c
SS
9679 new = pop_context ();
9680 /* Make a block for the local symbols within. */
801e3a5b
JB
9681 block = finish_block (new->name, &local_symbols, new->old_blocks,
9682 lowpc, highpc, objfile);
9683
df8a16a1 9684 /* For C++, set the block's scope. */
195a3f6c 9685 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9686 && cu->processing_has_namespace_info)
195a3f6c
TT
9687 block_set_scope (block, determine_prefix (die, cu),
9688 &objfile->objfile_obstack);
df8a16a1 9689
801e3a5b
JB
9690 /* If we have address ranges, record them. */
9691 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9692
34eaf542
TT
9693 /* Attach template arguments to function. */
9694 if (! VEC_empty (symbolp, template_args))
9695 {
9696 gdb_assert (templ_func != NULL);
9697
9698 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9699 templ_func->template_arguments
9700 = obstack_alloc (&objfile->objfile_obstack,
9701 (templ_func->n_template_arguments
9702 * sizeof (struct symbol *)));
9703 memcpy (templ_func->template_arguments,
9704 VEC_address (symbolp, template_args),
9705 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9706 VEC_free (symbolp, template_args);
9707 }
9708
208d8187
JB
9709 /* In C++, we can have functions nested inside functions (e.g., when
9710 a function declares a class that has methods). This means that
9711 when we finish processing a function scope, we may need to go
9712 back to building a containing block's symbol lists. */
9713 local_symbols = new->locals;
27aa8d6a 9714 using_directives = new->using_directives;
208d8187 9715
921e78cf
JB
9716 /* If we've finished processing a top-level function, subsequent
9717 symbols go in the file symbol list. */
9718 if (outermost_context_p ())
e142c38c 9719 cu->list_in_scope = &file_symbols;
c906108c
SS
9720}
9721
9722/* Process all the DIES contained within a lexical block scope. Start
9723 a new scope, process the dies, and then close the scope. */
9724
9725static void
e7c27a73 9726read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9727{
e7c27a73 9728 struct objfile *objfile = cu->objfile;
52f0bd74 9729 struct context_stack *new;
c906108c
SS
9730 CORE_ADDR lowpc, highpc;
9731 struct die_info *child_die;
e142c38c
DJ
9732 CORE_ADDR baseaddr;
9733
9734 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9735
9736 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9737 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9738 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9739 be nasty. Might be easier to properly extend generic blocks to
af34e669 9740 describe ranges. */
d85a05f0 9741 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9742 return;
9743 lowpc += baseaddr;
9744 highpc += baseaddr;
9745
9746 push_context (0, lowpc);
639d11d3 9747 if (die->child != NULL)
c906108c 9748 {
639d11d3 9749 child_die = die->child;
c906108c
SS
9750 while (child_die && child_die->tag)
9751 {
e7c27a73 9752 process_die (child_die, cu);
c906108c
SS
9753 child_die = sibling_die (child_die);
9754 }
9755 }
9756 new = pop_context ();
9757
8540c487 9758 if (local_symbols != NULL || using_directives != NULL)
c906108c 9759 {
801e3a5b
JB
9760 struct block *block
9761 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9762 highpc, objfile);
9763
9764 /* Note that recording ranges after traversing children, as we
9765 do here, means that recording a parent's ranges entails
9766 walking across all its children's ranges as they appear in
9767 the address map, which is quadratic behavior.
9768
9769 It would be nicer to record the parent's ranges before
9770 traversing its children, simply overriding whatever you find
9771 there. But since we don't even decide whether to create a
9772 block until after we've traversed its children, that's hard
9773 to do. */
9774 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9775 }
9776 local_symbols = new->locals;
27aa8d6a 9777 using_directives = new->using_directives;
c906108c
SS
9778}
9779
96408a79
SA
9780/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9781
9782static void
9783read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9784{
9785 struct objfile *objfile = cu->objfile;
9786 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9787 CORE_ADDR pc, baseaddr;
9788 struct attribute *attr;
9789 struct call_site *call_site, call_site_local;
9790 void **slot;
9791 int nparams;
9792 struct die_info *child_die;
9793
9794 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9795
9796 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9797 if (!attr)
9798 {
9799 complaint (&symfile_complaints,
9800 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9801 "DIE 0x%x [in module %s]"),
b64f50a1 9802 die->offset.sect_off, objfile->name);
96408a79
SA
9803 return;
9804 }
9805 pc = DW_ADDR (attr) + baseaddr;
9806
9807 if (cu->call_site_htab == NULL)
9808 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9809 NULL, &objfile->objfile_obstack,
9810 hashtab_obstack_allocate, NULL);
9811 call_site_local.pc = pc;
9812 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9813 if (*slot != NULL)
9814 {
9815 complaint (&symfile_complaints,
9816 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9817 "DIE 0x%x [in module %s]"),
b64f50a1 9818 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9819 return;
9820 }
9821
9822 /* Count parameters at the caller. */
9823
9824 nparams = 0;
9825 for (child_die = die->child; child_die && child_die->tag;
9826 child_die = sibling_die (child_die))
9827 {
9828 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9829 {
9830 complaint (&symfile_complaints,
9831 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9832 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9833 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9834 continue;
9835 }
9836
9837 nparams++;
9838 }
9839
9840 call_site = obstack_alloc (&objfile->objfile_obstack,
9841 (sizeof (*call_site)
9842 + (sizeof (*call_site->parameter)
9843 * (nparams - 1))));
9844 *slot = call_site;
9845 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9846 call_site->pc = pc;
9847
9848 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9849 {
9850 struct die_info *func_die;
9851
9852 /* Skip also over DW_TAG_inlined_subroutine. */
9853 for (func_die = die->parent;
9854 func_die && func_die->tag != DW_TAG_subprogram
9855 && func_die->tag != DW_TAG_subroutine_type;
9856 func_die = func_die->parent);
9857
9858 /* DW_AT_GNU_all_call_sites is a superset
9859 of DW_AT_GNU_all_tail_call_sites. */
9860 if (func_die
9861 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9862 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9863 {
9864 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9865 not complete. But keep CALL_SITE for look ups via call_site_htab,
9866 both the initial caller containing the real return address PC and
9867 the final callee containing the current PC of a chain of tail
9868 calls do not need to have the tail call list complete. But any
9869 function candidate for a virtual tail call frame searched via
9870 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9871 determined unambiguously. */
9872 }
9873 else
9874 {
9875 struct type *func_type = NULL;
9876
9877 if (func_die)
9878 func_type = get_die_type (func_die, cu);
9879 if (func_type != NULL)
9880 {
9881 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9882
9883 /* Enlist this call site to the function. */
9884 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9885 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9886 }
9887 else
9888 complaint (&symfile_complaints,
9889 _("Cannot find function owning DW_TAG_GNU_call_site "
9890 "DIE 0x%x [in module %s]"),
b64f50a1 9891 die->offset.sect_off, objfile->name);
96408a79
SA
9892 }
9893 }
9894
9895 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9896 if (attr == NULL)
9897 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9898 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9899 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9900 /* Keep NULL DWARF_BLOCK. */;
9901 else if (attr_form_is_block (attr))
9902 {
9903 struct dwarf2_locexpr_baton *dlbaton;
9904
9905 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9906 dlbaton->data = DW_BLOCK (attr)->data;
9907 dlbaton->size = DW_BLOCK (attr)->size;
9908 dlbaton->per_cu = cu->per_cu;
9909
9910 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9911 }
9912 else if (is_ref_attr (attr))
9913 {
96408a79
SA
9914 struct dwarf2_cu *target_cu = cu;
9915 struct die_info *target_die;
9916
9917 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9918 gdb_assert (target_cu->objfile == objfile);
9919 if (die_is_declaration (target_die, target_cu))
9920 {
9112db09
JK
9921 const char *target_physname = NULL;
9922 struct attribute *target_attr;
9923
9924 /* Prefer the mangled name; otherwise compute the demangled one. */
9925 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9926 if (target_attr == NULL)
9927 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9928 target_cu);
9929 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9930 target_physname = DW_STRING (target_attr);
9931 else
9932 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9933 if (target_physname == NULL)
9934 complaint (&symfile_complaints,
9935 _("DW_AT_GNU_call_site_target target DIE has invalid "
9936 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9937 die->offset.sect_off, objfile->name);
96408a79 9938 else
7d455152 9939 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9940 }
9941 else
9942 {
9943 CORE_ADDR lowpc;
9944
9945 /* DW_AT_entry_pc should be preferred. */
9946 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9947 complaint (&symfile_complaints,
9948 _("DW_AT_GNU_call_site_target target DIE has invalid "
9949 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9950 die->offset.sect_off, objfile->name);
96408a79
SA
9951 else
9952 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9953 }
9954 }
9955 else
9956 complaint (&symfile_complaints,
9957 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9958 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9959 die->offset.sect_off, objfile->name);
96408a79
SA
9960
9961 call_site->per_cu = cu->per_cu;
9962
9963 for (child_die = die->child;
9964 child_die && child_die->tag;
9965 child_die = sibling_die (child_die))
9966 {
96408a79 9967 struct call_site_parameter *parameter;
1788b2d3 9968 struct attribute *loc, *origin;
96408a79
SA
9969
9970 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9971 {
9972 /* Already printed the complaint above. */
9973 continue;
9974 }
9975
9976 gdb_assert (call_site->parameter_count < nparams);
9977 parameter = &call_site->parameter[call_site->parameter_count];
9978
1788b2d3
JK
9979 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9980 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9981 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9982
24c5c679 9983 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9984 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9985 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9986 {
9987 sect_offset offset;
9988
9989 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9990 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9991 if (!offset_in_cu_p (&cu->header, offset))
9992 {
9993 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9994 binding can be done only inside one CU. Such referenced DIE
9995 therefore cannot be even moved to DW_TAG_partial_unit. */
9996 complaint (&symfile_complaints,
9997 _("DW_AT_abstract_origin offset is not in CU for "
9998 "DW_TAG_GNU_call_site child DIE 0x%x "
9999 "[in module %s]"),
10000 child_die->offset.sect_off, objfile->name);
10001 continue;
10002 }
1788b2d3
JK
10003 parameter->u.param_offset.cu_off = (offset.sect_off
10004 - cu->header.offset.sect_off);
10005 }
10006 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10007 {
10008 complaint (&symfile_complaints,
10009 _("No DW_FORM_block* DW_AT_location for "
10010 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10011 child_die->offset.sect_off, objfile->name);
96408a79
SA
10012 continue;
10013 }
24c5c679 10014 else
96408a79 10015 {
24c5c679
JK
10016 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10017 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10018 if (parameter->u.dwarf_reg != -1)
10019 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10020 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10021 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10022 &parameter->u.fb_offset))
10023 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10024 else
10025 {
10026 complaint (&symfile_complaints,
10027 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10028 "for DW_FORM_block* DW_AT_location is supported for "
10029 "DW_TAG_GNU_call_site child DIE 0x%x "
10030 "[in module %s]"),
10031 child_die->offset.sect_off, objfile->name);
10032 continue;
10033 }
96408a79
SA
10034 }
10035
10036 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10037 if (!attr_form_is_block (attr))
10038 {
10039 complaint (&symfile_complaints,
10040 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10041 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10042 child_die->offset.sect_off, objfile->name);
96408a79
SA
10043 continue;
10044 }
10045 parameter->value = DW_BLOCK (attr)->data;
10046 parameter->value_size = DW_BLOCK (attr)->size;
10047
10048 /* Parameters are not pre-cleared by memset above. */
10049 parameter->data_value = NULL;
10050 parameter->data_value_size = 0;
10051 call_site->parameter_count++;
10052
10053 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10054 if (attr)
10055 {
10056 if (!attr_form_is_block (attr))
10057 complaint (&symfile_complaints,
10058 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10059 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10060 child_die->offset.sect_off, objfile->name);
96408a79
SA
10061 else
10062 {
10063 parameter->data_value = DW_BLOCK (attr)->data;
10064 parameter->data_value_size = DW_BLOCK (attr)->size;
10065 }
10066 }
10067 }
10068}
10069
43039443 10070/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10071 Return 1 if the attributes are present and valid, otherwise, return 0.
10072 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10073
10074static int
10075dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10076 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10077 struct partial_symtab *ranges_pst)
43039443
JK
10078{
10079 struct objfile *objfile = cu->objfile;
10080 struct comp_unit_head *cu_header = &cu->header;
10081 bfd *obfd = objfile->obfd;
10082 unsigned int addr_size = cu_header->addr_size;
10083 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10084 /* Base address selection entry. */
10085 CORE_ADDR base;
10086 int found_base;
10087 unsigned int dummy;
10088 gdb_byte *buffer;
10089 CORE_ADDR marker;
10090 int low_set;
10091 CORE_ADDR low = 0;
10092 CORE_ADDR high = 0;
ff013f42 10093 CORE_ADDR baseaddr;
43039443 10094
d00adf39
DE
10095 found_base = cu->base_known;
10096 base = cu->base_address;
43039443 10097
be391dca 10098 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10099 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10100 {
10101 complaint (&symfile_complaints,
10102 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10103 offset);
10104 return 0;
10105 }
dce234bc 10106 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10107
10108 /* Read in the largest possible address. */
10109 marker = read_address (obfd, buffer, cu, &dummy);
10110 if ((marker & mask) == mask)
10111 {
10112 /* If we found the largest possible address, then
10113 read the base address. */
10114 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10115 buffer += 2 * addr_size;
10116 offset += 2 * addr_size;
10117 found_base = 1;
10118 }
10119
10120 low_set = 0;
10121
e7030f15 10122 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10123
43039443
JK
10124 while (1)
10125 {
10126 CORE_ADDR range_beginning, range_end;
10127
10128 range_beginning = read_address (obfd, buffer, cu, &dummy);
10129 buffer += addr_size;
10130 range_end = read_address (obfd, buffer, cu, &dummy);
10131 buffer += addr_size;
10132 offset += 2 * addr_size;
10133
10134 /* An end of list marker is a pair of zero addresses. */
10135 if (range_beginning == 0 && range_end == 0)
10136 /* Found the end of list entry. */
10137 break;
10138
10139 /* Each base address selection entry is a pair of 2 values.
10140 The first is the largest possible address, the second is
10141 the base address. Check for a base address here. */
10142 if ((range_beginning & mask) == mask)
10143 {
10144 /* If we found the largest possible address, then
10145 read the base address. */
10146 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10147 found_base = 1;
10148 continue;
10149 }
10150
10151 if (!found_base)
10152 {
10153 /* We have no valid base address for the ranges
10154 data. */
10155 complaint (&symfile_complaints,
10156 _("Invalid .debug_ranges data (no base address)"));
10157 return 0;
10158 }
10159
9277c30c
UW
10160 if (range_beginning > range_end)
10161 {
10162 /* Inverted range entries are invalid. */
10163 complaint (&symfile_complaints,
10164 _("Invalid .debug_ranges data (inverted range)"));
10165 return 0;
10166 }
10167
10168 /* Empty range entries have no effect. */
10169 if (range_beginning == range_end)
10170 continue;
10171
43039443
JK
10172 range_beginning += base;
10173 range_end += base;
10174
01093045
DE
10175 /* A not-uncommon case of bad debug info.
10176 Don't pollute the addrmap with bad data. */
10177 if (range_beginning + baseaddr == 0
10178 && !dwarf2_per_objfile->has_section_at_zero)
10179 {
10180 complaint (&symfile_complaints,
10181 _(".debug_ranges entry has start address of zero"
10182 " [in module %s]"), objfile->name);
10183 continue;
10184 }
10185
9277c30c 10186 if (ranges_pst != NULL)
ff013f42 10187 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10188 range_beginning + baseaddr,
10189 range_end - 1 + baseaddr,
ff013f42
JK
10190 ranges_pst);
10191
43039443
JK
10192 /* FIXME: This is recording everything as a low-high
10193 segment of consecutive addresses. We should have a
10194 data structure for discontiguous block ranges
10195 instead. */
10196 if (! low_set)
10197 {
10198 low = range_beginning;
10199 high = range_end;
10200 low_set = 1;
10201 }
10202 else
10203 {
10204 if (range_beginning < low)
10205 low = range_beginning;
10206 if (range_end > high)
10207 high = range_end;
10208 }
10209 }
10210
10211 if (! low_set)
10212 /* If the first entry is an end-of-list marker, the range
10213 describes an empty scope, i.e. no instructions. */
10214 return 0;
10215
10216 if (low_return)
10217 *low_return = low;
10218 if (high_return)
10219 *high_return = high;
10220 return 1;
10221}
10222
af34e669
DJ
10223/* Get low and high pc attributes from a die. Return 1 if the attributes
10224 are present and valid, otherwise, return 0. Return -1 if the range is
10225 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10226
c906108c 10227static int
af34e669 10228dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10229 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10230 struct partial_symtab *pst)
c906108c
SS
10231{
10232 struct attribute *attr;
91da1414 10233 struct attribute *attr_high;
af34e669
DJ
10234 CORE_ADDR low = 0;
10235 CORE_ADDR high = 0;
10236 int ret = 0;
c906108c 10237
91da1414
MW
10238 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10239 if (attr_high)
af34e669 10240 {
e142c38c 10241 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10242 if (attr)
91da1414
MW
10243 {
10244 low = DW_ADDR (attr);
3019eac3
DE
10245 if (attr_high->form == DW_FORM_addr
10246 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10247 high = DW_ADDR (attr_high);
10248 else
10249 high = low + DW_UNSND (attr_high);
10250 }
af34e669
DJ
10251 else
10252 /* Found high w/o low attribute. */
10253 return 0;
10254
10255 /* Found consecutive range of addresses. */
10256 ret = 1;
10257 }
c906108c 10258 else
af34e669 10259 {
e142c38c 10260 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10261 if (attr != NULL)
10262 {
ab435259
DE
10263 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10264 We take advantage of the fact that DW_AT_ranges does not appear
10265 in DW_TAG_compile_unit of DWO files. */
10266 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10267 unsigned int ranges_offset = (DW_UNSND (attr)
10268 + (need_ranges_base
10269 ? cu->ranges_base
10270 : 0));
2e3cf129 10271
af34e669 10272 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10273 .debug_ranges section. */
2e3cf129 10274 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10275 return 0;
43039443 10276 /* Found discontinuous range of addresses. */
af34e669
DJ
10277 ret = -1;
10278 }
10279 }
c906108c 10280
9373cf26
JK
10281 /* read_partial_die has also the strict LOW < HIGH requirement. */
10282 if (high <= low)
c906108c
SS
10283 return 0;
10284
10285 /* When using the GNU linker, .gnu.linkonce. sections are used to
10286 eliminate duplicate copies of functions and vtables and such.
10287 The linker will arbitrarily choose one and discard the others.
10288 The AT_*_pc values for such functions refer to local labels in
10289 these sections. If the section from that file was discarded, the
10290 labels are not in the output, so the relocs get a value of 0.
10291 If this is a discarded function, mark the pc bounds as invalid,
10292 so that GDB will ignore it. */
72dca2f5 10293 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10294 return 0;
10295
10296 *lowpc = low;
96408a79
SA
10297 if (highpc)
10298 *highpc = high;
af34e669 10299 return ret;
c906108c
SS
10300}
10301
b084d499
JB
10302/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10303 its low and high PC addresses. Do nothing if these addresses could not
10304 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10305 and HIGHPC to the high address if greater than HIGHPC. */
10306
10307static void
10308dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10309 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10310 struct dwarf2_cu *cu)
10311{
10312 CORE_ADDR low, high;
10313 struct die_info *child = die->child;
10314
d85a05f0 10315 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10316 {
10317 *lowpc = min (*lowpc, low);
10318 *highpc = max (*highpc, high);
10319 }
10320
10321 /* If the language does not allow nested subprograms (either inside
10322 subprograms or lexical blocks), we're done. */
10323 if (cu->language != language_ada)
10324 return;
6e70227d 10325
b084d499
JB
10326 /* Check all the children of the given DIE. If it contains nested
10327 subprograms, then check their pc bounds. Likewise, we need to
10328 check lexical blocks as well, as they may also contain subprogram
10329 definitions. */
10330 while (child && child->tag)
10331 {
10332 if (child->tag == DW_TAG_subprogram
10333 || child->tag == DW_TAG_lexical_block)
10334 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10335 child = sibling_die (child);
10336 }
10337}
10338
fae299cd
DC
10339/* Get the low and high pc's represented by the scope DIE, and store
10340 them in *LOWPC and *HIGHPC. If the correct values can't be
10341 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10342
10343static void
10344get_scope_pc_bounds (struct die_info *die,
10345 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10346 struct dwarf2_cu *cu)
10347{
10348 CORE_ADDR best_low = (CORE_ADDR) -1;
10349 CORE_ADDR best_high = (CORE_ADDR) 0;
10350 CORE_ADDR current_low, current_high;
10351
d85a05f0 10352 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10353 {
10354 best_low = current_low;
10355 best_high = current_high;
10356 }
10357 else
10358 {
10359 struct die_info *child = die->child;
10360
10361 while (child && child->tag)
10362 {
10363 switch (child->tag) {
10364 case DW_TAG_subprogram:
b084d499 10365 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10366 break;
10367 case DW_TAG_namespace:
f55ee35c 10368 case DW_TAG_module:
fae299cd
DC
10369 /* FIXME: carlton/2004-01-16: Should we do this for
10370 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10371 that current GCC's always emit the DIEs corresponding
10372 to definitions of methods of classes as children of a
10373 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10374 the DIEs giving the declarations, which could be
10375 anywhere). But I don't see any reason why the
10376 standards says that they have to be there. */
10377 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10378
10379 if (current_low != ((CORE_ADDR) -1))
10380 {
10381 best_low = min (best_low, current_low);
10382 best_high = max (best_high, current_high);
10383 }
10384 break;
10385 default:
0963b4bd 10386 /* Ignore. */
fae299cd
DC
10387 break;
10388 }
10389
10390 child = sibling_die (child);
10391 }
10392 }
10393
10394 *lowpc = best_low;
10395 *highpc = best_high;
10396}
10397
801e3a5b
JB
10398/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10399 in DIE. */
380bca97 10400
801e3a5b
JB
10401static void
10402dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10403 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10404{
bb5ed363 10405 struct objfile *objfile = cu->objfile;
801e3a5b 10406 struct attribute *attr;
91da1414 10407 struct attribute *attr_high;
801e3a5b 10408
91da1414
MW
10409 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10410 if (attr_high)
801e3a5b 10411 {
801e3a5b
JB
10412 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10413 if (attr)
10414 {
10415 CORE_ADDR low = DW_ADDR (attr);
91da1414 10416 CORE_ADDR high;
3019eac3
DE
10417 if (attr_high->form == DW_FORM_addr
10418 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10419 high = DW_ADDR (attr_high);
10420 else
10421 high = low + DW_UNSND (attr_high);
9a619af0 10422
801e3a5b
JB
10423 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10424 }
10425 }
10426
10427 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10428 if (attr)
10429 {
bb5ed363 10430 bfd *obfd = objfile->obfd;
ab435259
DE
10431 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10432 We take advantage of the fact that DW_AT_ranges does not appear
10433 in DW_TAG_compile_unit of DWO files. */
10434 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10435
10436 /* The value of the DW_AT_ranges attribute is the offset of the
10437 address range list in the .debug_ranges section. */
ab435259
DE
10438 unsigned long offset = (DW_UNSND (attr)
10439 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10440 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10441
10442 /* For some target architectures, but not others, the
10443 read_address function sign-extends the addresses it returns.
10444 To recognize base address selection entries, we need a
10445 mask. */
10446 unsigned int addr_size = cu->header.addr_size;
10447 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10448
10449 /* The base address, to which the next pair is relative. Note
10450 that this 'base' is a DWARF concept: most entries in a range
10451 list are relative, to reduce the number of relocs against the
10452 debugging information. This is separate from this function's
10453 'baseaddr' argument, which GDB uses to relocate debugging
10454 information from a shared library based on the address at
10455 which the library was loaded. */
d00adf39
DE
10456 CORE_ADDR base = cu->base_address;
10457 int base_known = cu->base_known;
801e3a5b 10458
be391dca 10459 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10460 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10461 {
10462 complaint (&symfile_complaints,
10463 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10464 offset);
10465 return;
10466 }
10467
10468 for (;;)
10469 {
10470 unsigned int bytes_read;
10471 CORE_ADDR start, end;
10472
10473 start = read_address (obfd, buffer, cu, &bytes_read);
10474 buffer += bytes_read;
10475 end = read_address (obfd, buffer, cu, &bytes_read);
10476 buffer += bytes_read;
10477
10478 /* Did we find the end of the range list? */
10479 if (start == 0 && end == 0)
10480 break;
10481
10482 /* Did we find a base address selection entry? */
10483 else if ((start & base_select_mask) == base_select_mask)
10484 {
10485 base = end;
10486 base_known = 1;
10487 }
10488
10489 /* We found an ordinary address range. */
10490 else
10491 {
10492 if (!base_known)
10493 {
10494 complaint (&symfile_complaints,
3e43a32a
MS
10495 _("Invalid .debug_ranges data "
10496 "(no base address)"));
801e3a5b
JB
10497 return;
10498 }
10499
9277c30c
UW
10500 if (start > end)
10501 {
10502 /* Inverted range entries are invalid. */
10503 complaint (&symfile_complaints,
10504 _("Invalid .debug_ranges data "
10505 "(inverted range)"));
10506 return;
10507 }
10508
10509 /* Empty range entries have no effect. */
10510 if (start == end)
10511 continue;
10512
01093045
DE
10513 start += base + baseaddr;
10514 end += base + baseaddr;
10515
10516 /* A not-uncommon case of bad debug info.
10517 Don't pollute the addrmap with bad data. */
10518 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10519 {
10520 complaint (&symfile_complaints,
10521 _(".debug_ranges entry has start address of zero"
10522 " [in module %s]"), objfile->name);
10523 continue;
10524 }
10525
10526 record_block_range (block, start, end - 1);
801e3a5b
JB
10527 }
10528 }
10529 }
10530}
10531
685b1105
JK
10532/* Check whether the producer field indicates either of GCC < 4.6, or the
10533 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10534
685b1105
JK
10535static void
10536check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10537{
10538 const char *cs;
10539 int major, minor, release;
10540
10541 if (cu->producer == NULL)
10542 {
10543 /* For unknown compilers expect their behavior is DWARF version
10544 compliant.
10545
10546 GCC started to support .debug_types sections by -gdwarf-4 since
10547 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10548 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10549 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10550 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10551 }
685b1105 10552 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10553 {
685b1105
JK
10554 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10555
ba919b58
TT
10556 cs = &cu->producer[strlen ("GNU ")];
10557 while (*cs && !isdigit (*cs))
10558 cs++;
10559 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10560 {
10561 /* Not recognized as GCC. */
10562 }
10563 else
1b80a9fa
JK
10564 {
10565 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10566 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10567 }
685b1105
JK
10568 }
10569 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10570 cu->producer_is_icc = 1;
10571 else
10572 {
10573 /* For other non-GCC compilers, expect their behavior is DWARF version
10574 compliant. */
60d5a603
JK
10575 }
10576
ba919b58 10577 cu->checked_producer = 1;
685b1105 10578}
ba919b58 10579
685b1105
JK
10580/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10581 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10582 during 4.6.0 experimental. */
10583
10584static int
10585producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10586{
10587 if (!cu->checked_producer)
10588 check_producer (cu);
10589
10590 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10591}
10592
10593/* Return the default accessibility type if it is not overriden by
10594 DW_AT_accessibility. */
10595
10596static enum dwarf_access_attribute
10597dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10598{
10599 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10600 {
10601 /* The default DWARF 2 accessibility for members is public, the default
10602 accessibility for inheritance is private. */
10603
10604 if (die->tag != DW_TAG_inheritance)
10605 return DW_ACCESS_public;
10606 else
10607 return DW_ACCESS_private;
10608 }
10609 else
10610 {
10611 /* DWARF 3+ defines the default accessibility a different way. The same
10612 rules apply now for DW_TAG_inheritance as for the members and it only
10613 depends on the container kind. */
10614
10615 if (die->parent->tag == DW_TAG_class_type)
10616 return DW_ACCESS_private;
10617 else
10618 return DW_ACCESS_public;
10619 }
10620}
10621
74ac6d43
TT
10622/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10623 offset. If the attribute was not found return 0, otherwise return
10624 1. If it was found but could not properly be handled, set *OFFSET
10625 to 0. */
10626
10627static int
10628handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10629 LONGEST *offset)
10630{
10631 struct attribute *attr;
10632
10633 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10634 if (attr != NULL)
10635 {
10636 *offset = 0;
10637
10638 /* Note that we do not check for a section offset first here.
10639 This is because DW_AT_data_member_location is new in DWARF 4,
10640 so if we see it, we can assume that a constant form is really
10641 a constant and not a section offset. */
10642 if (attr_form_is_constant (attr))
10643 *offset = dwarf2_get_attr_constant_value (attr, 0);
10644 else if (attr_form_is_section_offset (attr))
10645 dwarf2_complex_location_expr_complaint ();
10646 else if (attr_form_is_block (attr))
10647 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10648 else
10649 dwarf2_complex_location_expr_complaint ();
10650
10651 return 1;
10652 }
10653
10654 return 0;
10655}
10656
c906108c
SS
10657/* Add an aggregate field to the field list. */
10658
10659static void
107d2387 10660dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10661 struct dwarf2_cu *cu)
6e70227d 10662{
e7c27a73 10663 struct objfile *objfile = cu->objfile;
5e2b427d 10664 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10665 struct nextfield *new_field;
10666 struct attribute *attr;
10667 struct field *fp;
15d034d0 10668 const char *fieldname = "";
c906108c
SS
10669
10670 /* Allocate a new field list entry and link it in. */
10671 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10672 make_cleanup (xfree, new_field);
c906108c 10673 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10674
10675 if (die->tag == DW_TAG_inheritance)
10676 {
10677 new_field->next = fip->baseclasses;
10678 fip->baseclasses = new_field;
10679 }
10680 else
10681 {
10682 new_field->next = fip->fields;
10683 fip->fields = new_field;
10684 }
c906108c
SS
10685 fip->nfields++;
10686
e142c38c 10687 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10688 if (attr)
10689 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10690 else
10691 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10692 if (new_field->accessibility != DW_ACCESS_public)
10693 fip->non_public_fields = 1;
60d5a603 10694
e142c38c 10695 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10696 if (attr)
10697 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10698 else
10699 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10700
10701 fp = &new_field->field;
a9a9bd0f 10702
e142c38c 10703 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10704 {
74ac6d43
TT
10705 LONGEST offset;
10706
a9a9bd0f 10707 /* Data member other than a C++ static data member. */
6e70227d 10708
c906108c 10709 /* Get type of field. */
e7c27a73 10710 fp->type = die_type (die, cu);
c906108c 10711
d6a843b5 10712 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10713
c906108c 10714 /* Get bit size of field (zero if none). */
e142c38c 10715 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10716 if (attr)
10717 {
10718 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10719 }
10720 else
10721 {
10722 FIELD_BITSIZE (*fp) = 0;
10723 }
10724
10725 /* Get bit offset of field. */
74ac6d43
TT
10726 if (handle_data_member_location (die, cu, &offset))
10727 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10728 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10729 if (attr)
10730 {
5e2b427d 10731 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10732 {
10733 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10734 additional bit offset from the MSB of the containing
10735 anonymous object to the MSB of the field. We don't
10736 have to do anything special since we don't need to
10737 know the size of the anonymous object. */
f41f5e61 10738 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10739 }
10740 else
10741 {
10742 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10743 MSB of the anonymous object, subtract off the number of
10744 bits from the MSB of the field to the MSB of the
10745 object, and then subtract off the number of bits of
10746 the field itself. The result is the bit offset of
10747 the LSB of the field. */
c906108c
SS
10748 int anonymous_size;
10749 int bit_offset = DW_UNSND (attr);
10750
e142c38c 10751 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10752 if (attr)
10753 {
10754 /* The size of the anonymous object containing
10755 the bit field is explicit, so use the
10756 indicated size (in bytes). */
10757 anonymous_size = DW_UNSND (attr);
10758 }
10759 else
10760 {
10761 /* The size of the anonymous object containing
10762 the bit field must be inferred from the type
10763 attribute of the data member containing the
10764 bit field. */
10765 anonymous_size = TYPE_LENGTH (fp->type);
10766 }
f41f5e61
PA
10767 SET_FIELD_BITPOS (*fp,
10768 (FIELD_BITPOS (*fp)
10769 + anonymous_size * bits_per_byte
10770 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10771 }
10772 }
10773
10774 /* Get name of field. */
39cbfefa
DJ
10775 fieldname = dwarf2_name (die, cu);
10776 if (fieldname == NULL)
10777 fieldname = "";
d8151005
DJ
10778
10779 /* The name is already allocated along with this objfile, so we don't
10780 need to duplicate it for the type. */
10781 fp->name = fieldname;
c906108c
SS
10782
10783 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10784 pointer or virtual base class pointer) to private. */
e142c38c 10785 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10786 {
d48cc9dd 10787 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10788 new_field->accessibility = DW_ACCESS_private;
10789 fip->non_public_fields = 1;
10790 }
10791 }
a9a9bd0f 10792 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10793 {
a9a9bd0f
DC
10794 /* C++ static member. */
10795
10796 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10797 is a declaration, but all versions of G++ as of this writing
10798 (so through at least 3.2.1) incorrectly generate
10799 DW_TAG_variable tags. */
6e70227d 10800
ff355380 10801 const char *physname;
c906108c 10802
a9a9bd0f 10803 /* Get name of field. */
39cbfefa
DJ
10804 fieldname = dwarf2_name (die, cu);
10805 if (fieldname == NULL)
c906108c
SS
10806 return;
10807
254e6b9e 10808 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10809 if (attr
10810 /* Only create a symbol if this is an external value.
10811 new_symbol checks this and puts the value in the global symbol
10812 table, which we want. If it is not external, new_symbol
10813 will try to put the value in cu->list_in_scope which is wrong. */
10814 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10815 {
10816 /* A static const member, not much different than an enum as far as
10817 we're concerned, except that we can support more types. */
10818 new_symbol (die, NULL, cu);
10819 }
10820
2df3850c 10821 /* Get physical name. */
ff355380 10822 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10823
d8151005
DJ
10824 /* The name is already allocated along with this objfile, so we don't
10825 need to duplicate it for the type. */
10826 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10827 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10828 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10829 }
10830 else if (die->tag == DW_TAG_inheritance)
10831 {
74ac6d43 10832 LONGEST offset;
d4b96c9a 10833
74ac6d43
TT
10834 /* C++ base class field. */
10835 if (handle_data_member_location (die, cu, &offset))
10836 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10837 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10838 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10839 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10840 fip->nbaseclasses++;
10841 }
10842}
10843
98751a41
JK
10844/* Add a typedef defined in the scope of the FIP's class. */
10845
10846static void
10847dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10848 struct dwarf2_cu *cu)
6e70227d 10849{
98751a41 10850 struct objfile *objfile = cu->objfile;
98751a41
JK
10851 struct typedef_field_list *new_field;
10852 struct attribute *attr;
10853 struct typedef_field *fp;
10854 char *fieldname = "";
10855
10856 /* Allocate a new field list entry and link it in. */
10857 new_field = xzalloc (sizeof (*new_field));
10858 make_cleanup (xfree, new_field);
10859
10860 gdb_assert (die->tag == DW_TAG_typedef);
10861
10862 fp = &new_field->field;
10863
10864 /* Get name of field. */
10865 fp->name = dwarf2_name (die, cu);
10866 if (fp->name == NULL)
10867 return;
10868
10869 fp->type = read_type_die (die, cu);
10870
10871 new_field->next = fip->typedef_field_list;
10872 fip->typedef_field_list = new_field;
10873 fip->typedef_field_list_count++;
10874}
10875
c906108c
SS
10876/* Create the vector of fields, and attach it to the type. */
10877
10878static void
fba45db2 10879dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10880 struct dwarf2_cu *cu)
c906108c
SS
10881{
10882 int nfields = fip->nfields;
10883
10884 /* Record the field count, allocate space for the array of fields,
10885 and create blank accessibility bitfields if necessary. */
10886 TYPE_NFIELDS (type) = nfields;
10887 TYPE_FIELDS (type) = (struct field *)
10888 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10889 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10890
b4ba55a1 10891 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10892 {
10893 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10894
10895 TYPE_FIELD_PRIVATE_BITS (type) =
10896 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10897 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10898
10899 TYPE_FIELD_PROTECTED_BITS (type) =
10900 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10901 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10902
774b6a14
TT
10903 TYPE_FIELD_IGNORE_BITS (type) =
10904 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10905 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10906 }
10907
10908 /* If the type has baseclasses, allocate and clear a bit vector for
10909 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10910 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10911 {
10912 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10913 unsigned char *pointer;
c906108c
SS
10914
10915 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10916 pointer = TYPE_ALLOC (type, num_bytes);
10917 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10918 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10919 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10920 }
10921
3e43a32a
MS
10922 /* Copy the saved-up fields into the field vector. Start from the head of
10923 the list, adding to the tail of the field array, so that they end up in
10924 the same order in the array in which they were added to the list. */
c906108c
SS
10925 while (nfields-- > 0)
10926 {
7d0ccb61
DJ
10927 struct nextfield *fieldp;
10928
10929 if (fip->fields)
10930 {
10931 fieldp = fip->fields;
10932 fip->fields = fieldp->next;
10933 }
10934 else
10935 {
10936 fieldp = fip->baseclasses;
10937 fip->baseclasses = fieldp->next;
10938 }
10939
10940 TYPE_FIELD (type, nfields) = fieldp->field;
10941 switch (fieldp->accessibility)
c906108c 10942 {
c5aa993b 10943 case DW_ACCESS_private:
b4ba55a1
JB
10944 if (cu->language != language_ada)
10945 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10946 break;
c906108c 10947
c5aa993b 10948 case DW_ACCESS_protected:
b4ba55a1
JB
10949 if (cu->language != language_ada)
10950 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10951 break;
c906108c 10952
c5aa993b
JM
10953 case DW_ACCESS_public:
10954 break;
c906108c 10955
c5aa993b
JM
10956 default:
10957 /* Unknown accessibility. Complain and treat it as public. */
10958 {
e2e0b3e5 10959 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10960 fieldp->accessibility);
c5aa993b
JM
10961 }
10962 break;
c906108c
SS
10963 }
10964 if (nfields < fip->nbaseclasses)
10965 {
7d0ccb61 10966 switch (fieldp->virtuality)
c906108c 10967 {
c5aa993b
JM
10968 case DW_VIRTUALITY_virtual:
10969 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10970 if (cu->language == language_ada)
a73c6dcd 10971 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10972 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10973 break;
c906108c
SS
10974 }
10975 }
c906108c
SS
10976 }
10977}
10978
7d27a96d
TT
10979/* Return true if this member function is a constructor, false
10980 otherwise. */
10981
10982static int
10983dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10984{
10985 const char *fieldname;
10986 const char *typename;
10987 int len;
10988
10989 if (die->parent == NULL)
10990 return 0;
10991
10992 if (die->parent->tag != DW_TAG_structure_type
10993 && die->parent->tag != DW_TAG_union_type
10994 && die->parent->tag != DW_TAG_class_type)
10995 return 0;
10996
10997 fieldname = dwarf2_name (die, cu);
10998 typename = dwarf2_name (die->parent, cu);
10999 if (fieldname == NULL || typename == NULL)
11000 return 0;
11001
11002 len = strlen (fieldname);
11003 return (strncmp (fieldname, typename, len) == 0
11004 && (typename[len] == '\0' || typename[len] == '<'));
11005}
11006
c906108c
SS
11007/* Add a member function to the proper fieldlist. */
11008
11009static void
107d2387 11010dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11011 struct type *type, struct dwarf2_cu *cu)
c906108c 11012{
e7c27a73 11013 struct objfile *objfile = cu->objfile;
c906108c
SS
11014 struct attribute *attr;
11015 struct fnfieldlist *flp;
11016 int i;
11017 struct fn_field *fnp;
15d034d0 11018 const char *fieldname;
c906108c 11019 struct nextfnfield *new_fnfield;
f792889a 11020 struct type *this_type;
60d5a603 11021 enum dwarf_access_attribute accessibility;
c906108c 11022
b4ba55a1 11023 if (cu->language == language_ada)
a73c6dcd 11024 error (_("unexpected member function in Ada type"));
b4ba55a1 11025
2df3850c 11026 /* Get name of member function. */
39cbfefa
DJ
11027 fieldname = dwarf2_name (die, cu);
11028 if (fieldname == NULL)
2df3850c 11029 return;
c906108c 11030
c906108c
SS
11031 /* Look up member function name in fieldlist. */
11032 for (i = 0; i < fip->nfnfields; i++)
11033 {
27bfe10e 11034 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11035 break;
11036 }
11037
11038 /* Create new list element if necessary. */
11039 if (i < fip->nfnfields)
11040 flp = &fip->fnfieldlists[i];
11041 else
11042 {
11043 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11044 {
11045 fip->fnfieldlists = (struct fnfieldlist *)
11046 xrealloc (fip->fnfieldlists,
11047 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11048 * sizeof (struct fnfieldlist));
c906108c 11049 if (fip->nfnfields == 0)
c13c43fd 11050 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11051 }
11052 flp = &fip->fnfieldlists[fip->nfnfields];
11053 flp->name = fieldname;
11054 flp->length = 0;
11055 flp->head = NULL;
3da10d80 11056 i = fip->nfnfields++;
c906108c
SS
11057 }
11058
11059 /* Create a new member function field and chain it to the field list
0963b4bd 11060 entry. */
c906108c 11061 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11062 make_cleanup (xfree, new_fnfield);
c906108c
SS
11063 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11064 new_fnfield->next = flp->head;
11065 flp->head = new_fnfield;
11066 flp->length++;
11067
11068 /* Fill in the member function field info. */
11069 fnp = &new_fnfield->fnfield;
3da10d80
KS
11070
11071 /* Delay processing of the physname until later. */
11072 if (cu->language == language_cplus || cu->language == language_java)
11073 {
11074 add_to_method_list (type, i, flp->length - 1, fieldname,
11075 die, cu);
11076 }
11077 else
11078 {
1d06ead6 11079 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11080 fnp->physname = physname ? physname : "";
11081 }
11082
c906108c 11083 fnp->type = alloc_type (objfile);
f792889a
DJ
11084 this_type = read_type_die (die, cu);
11085 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11086 {
f792889a 11087 int nparams = TYPE_NFIELDS (this_type);
c906108c 11088
f792889a 11089 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11090 of the method itself (TYPE_CODE_METHOD). */
11091 smash_to_method_type (fnp->type, type,
f792889a
DJ
11092 TYPE_TARGET_TYPE (this_type),
11093 TYPE_FIELDS (this_type),
11094 TYPE_NFIELDS (this_type),
11095 TYPE_VARARGS (this_type));
c906108c
SS
11096
11097 /* Handle static member functions.
c5aa993b 11098 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11099 member functions. G++ helps GDB by marking the first
11100 parameter for non-static member functions (which is the this
11101 pointer) as artificial. We obtain this information from
11102 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11103 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11104 fnp->voffset = VOFFSET_STATIC;
11105 }
11106 else
e2e0b3e5 11107 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11108 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11109
11110 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11111 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11112 fnp->fcontext = die_containing_type (die, cu);
c906108c 11113
3e43a32a
MS
11114 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11115 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11116
11117 /* Get accessibility. */
e142c38c 11118 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11119 if (attr)
60d5a603
JK
11120 accessibility = DW_UNSND (attr);
11121 else
11122 accessibility = dwarf2_default_access_attribute (die, cu);
11123 switch (accessibility)
c906108c 11124 {
60d5a603
JK
11125 case DW_ACCESS_private:
11126 fnp->is_private = 1;
11127 break;
11128 case DW_ACCESS_protected:
11129 fnp->is_protected = 1;
11130 break;
c906108c
SS
11131 }
11132
b02dede2 11133 /* Check for artificial methods. */
e142c38c 11134 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11135 if (attr && DW_UNSND (attr) != 0)
11136 fnp->is_artificial = 1;
11137
7d27a96d
TT
11138 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11139
0d564a31 11140 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11141 function. For older versions of GCC, this is an offset in the
11142 appropriate virtual table, as specified by DW_AT_containing_type.
11143 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11144 to the object address. */
11145
e142c38c 11146 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11147 if (attr)
8e19ed76 11148 {
aec5aa8b 11149 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11150 {
aec5aa8b
TT
11151 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11152 {
11153 /* Old-style GCC. */
11154 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11155 }
11156 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11157 || (DW_BLOCK (attr)->size > 1
11158 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11159 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11160 {
11161 struct dwarf_block blk;
11162 int offset;
11163
11164 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11165 ? 1 : 2);
11166 blk.size = DW_BLOCK (attr)->size - offset;
11167 blk.data = DW_BLOCK (attr)->data + offset;
11168 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11169 if ((fnp->voffset % cu->header.addr_size) != 0)
11170 dwarf2_complex_location_expr_complaint ();
11171 else
11172 fnp->voffset /= cu->header.addr_size;
11173 fnp->voffset += 2;
11174 }
11175 else
11176 dwarf2_complex_location_expr_complaint ();
11177
11178 if (!fnp->fcontext)
11179 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11180 }
3690dd37 11181 else if (attr_form_is_section_offset (attr))
8e19ed76 11182 {
4d3c2250 11183 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11184 }
11185 else
11186 {
4d3c2250
KB
11187 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11188 fieldname);
8e19ed76 11189 }
0d564a31 11190 }
d48cc9dd
DJ
11191 else
11192 {
11193 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11194 if (attr && DW_UNSND (attr))
11195 {
11196 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11197 complaint (&symfile_complaints,
3e43a32a
MS
11198 _("Member function \"%s\" (offset %d) is virtual "
11199 "but the vtable offset is not specified"),
b64f50a1 11200 fieldname, die->offset.sect_off);
9655fd1a 11201 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11202 TYPE_CPLUS_DYNAMIC (type) = 1;
11203 }
11204 }
c906108c
SS
11205}
11206
11207/* Create the vector of member function fields, and attach it to the type. */
11208
11209static void
fba45db2 11210dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11211 struct dwarf2_cu *cu)
c906108c
SS
11212{
11213 struct fnfieldlist *flp;
c906108c
SS
11214 int i;
11215
b4ba55a1 11216 if (cu->language == language_ada)
a73c6dcd 11217 error (_("unexpected member functions in Ada type"));
b4ba55a1 11218
c906108c
SS
11219 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11220 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11221 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11222
11223 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11224 {
11225 struct nextfnfield *nfp = flp->head;
11226 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11227 int k;
11228
11229 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11230 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11231 fn_flp->fn_fields = (struct fn_field *)
11232 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11233 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11234 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11235 }
11236
11237 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11238}
11239
1168df01
JB
11240/* Returns non-zero if NAME is the name of a vtable member in CU's
11241 language, zero otherwise. */
11242static int
11243is_vtable_name (const char *name, struct dwarf2_cu *cu)
11244{
11245 static const char vptr[] = "_vptr";
987504bb 11246 static const char vtable[] = "vtable";
1168df01 11247
987504bb
JJ
11248 /* Look for the C++ and Java forms of the vtable. */
11249 if ((cu->language == language_java
11250 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11251 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11252 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11253 return 1;
11254
11255 return 0;
11256}
11257
c0dd20ea 11258/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11259 functions, with the ABI-specified layout. If TYPE describes
11260 such a structure, smash it into a member function type.
61049d3b
DJ
11261
11262 GCC shouldn't do this; it should just output pointer to member DIEs.
11263 This is GCC PR debug/28767. */
c0dd20ea 11264
0b92b5bb
TT
11265static void
11266quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11267{
0b92b5bb 11268 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11269
11270 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11271 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11272 return;
c0dd20ea
DJ
11273
11274 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11275 if (TYPE_FIELD_NAME (type, 0) == NULL
11276 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11277 || TYPE_FIELD_NAME (type, 1) == NULL
11278 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11279 return;
c0dd20ea
DJ
11280
11281 /* Find the type of the method. */
0b92b5bb 11282 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11283 if (pfn_type == NULL
11284 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11285 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11286 return;
c0dd20ea
DJ
11287
11288 /* Look for the "this" argument. */
11289 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11290 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11291 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11292 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11293 return;
c0dd20ea
DJ
11294
11295 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11296 new_type = alloc_type (objfile);
11297 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11298 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11299 TYPE_VARARGS (pfn_type));
0b92b5bb 11300 smash_to_methodptr_type (type, new_type);
c0dd20ea 11301}
1168df01 11302
685b1105
JK
11303/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11304 (icc). */
11305
11306static int
11307producer_is_icc (struct dwarf2_cu *cu)
11308{
11309 if (!cu->checked_producer)
11310 check_producer (cu);
11311
11312 return cu->producer_is_icc;
11313}
11314
c906108c 11315/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11316 (definition) to create a type for the structure or union. Fill in
11317 the type's name and general properties; the members will not be
11318 processed until process_structure_type.
c906108c 11319
c767944b
DJ
11320 NOTE: we need to call these functions regardless of whether or not the
11321 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11322 structure or union. This gets the type entered into our set of
11323 user defined types.
11324
11325 However, if the structure is incomplete (an opaque struct/union)
11326 then suppress creating a symbol table entry for it since gdb only
11327 wants to find the one with the complete definition. Note that if
11328 it is complete, we just call new_symbol, which does it's own
11329 checking about whether the struct/union is anonymous or not (and
11330 suppresses creating a symbol table entry itself). */
11331
f792889a 11332static struct type *
134d01f1 11333read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11334{
e7c27a73 11335 struct objfile *objfile = cu->objfile;
c906108c
SS
11336 struct type *type;
11337 struct attribute *attr;
15d034d0 11338 const char *name;
c906108c 11339
348e048f
DE
11340 /* If the definition of this type lives in .debug_types, read that type.
11341 Don't follow DW_AT_specification though, that will take us back up
11342 the chain and we want to go down. */
45e58e77 11343 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11344 if (attr)
11345 {
11346 struct dwarf2_cu *type_cu = cu;
11347 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11348
348e048f
DE
11349 /* We could just recurse on read_structure_type, but we need to call
11350 get_die_type to ensure only one type for this DIE is created.
11351 This is important, for example, because for c++ classes we need
11352 TYPE_NAME set which is only done by new_symbol. Blech. */
11353 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11354
11355 /* TYPE_CU may not be the same as CU.
11356 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11357 return set_die_type (die, type, cu);
11358 }
11359
c0dd20ea 11360 type = alloc_type (objfile);
c906108c 11361 INIT_CPLUS_SPECIFIC (type);
93311388 11362
39cbfefa
DJ
11363 name = dwarf2_name (die, cu);
11364 if (name != NULL)
c906108c 11365 {
987504bb
JJ
11366 if (cu->language == language_cplus
11367 || cu->language == language_java)
63d06c5c 11368 {
15d034d0 11369 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11370
11371 /* dwarf2_full_name might have already finished building the DIE's
11372 type. If so, there is no need to continue. */
11373 if (get_die_type (die, cu) != NULL)
11374 return get_die_type (die, cu);
11375
11376 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11377 if (die->tag == DW_TAG_structure_type
11378 || die->tag == DW_TAG_class_type)
11379 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11380 }
11381 else
11382 {
d8151005
DJ
11383 /* The name is already allocated along with this objfile, so
11384 we don't need to duplicate it for the type. */
7d455152 11385 TYPE_TAG_NAME (type) = name;
94af9270
KS
11386 if (die->tag == DW_TAG_class_type)
11387 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11388 }
c906108c
SS
11389 }
11390
11391 if (die->tag == DW_TAG_structure_type)
11392 {
11393 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11394 }
11395 else if (die->tag == DW_TAG_union_type)
11396 {
11397 TYPE_CODE (type) = TYPE_CODE_UNION;
11398 }
11399 else
11400 {
c906108c
SS
11401 TYPE_CODE (type) = TYPE_CODE_CLASS;
11402 }
11403
0cc2414c
TT
11404 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11405 TYPE_DECLARED_CLASS (type) = 1;
11406
e142c38c 11407 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11408 if (attr)
11409 {
11410 TYPE_LENGTH (type) = DW_UNSND (attr);
11411 }
11412 else
11413 {
11414 TYPE_LENGTH (type) = 0;
11415 }
11416
685b1105
JK
11417 if (producer_is_icc (cu))
11418 {
11419 /* ICC does not output the required DW_AT_declaration
11420 on incomplete types, but gives them a size of zero. */
11421 }
11422 else
11423 TYPE_STUB_SUPPORTED (type) = 1;
11424
dc718098 11425 if (die_is_declaration (die, cu))
876cecd0 11426 TYPE_STUB (type) = 1;
a6c727b2
DJ
11427 else if (attr == NULL && die->child == NULL
11428 && producer_is_realview (cu->producer))
11429 /* RealView does not output the required DW_AT_declaration
11430 on incomplete types. */
11431 TYPE_STUB (type) = 1;
dc718098 11432
c906108c
SS
11433 /* We need to add the type field to the die immediately so we don't
11434 infinitely recurse when dealing with pointers to the structure
0963b4bd 11435 type within the structure itself. */
1c379e20 11436 set_die_type (die, type, cu);
c906108c 11437
7e314c57
JK
11438 /* set_die_type should be already done. */
11439 set_descriptive_type (type, die, cu);
11440
c767944b
DJ
11441 return type;
11442}
11443
11444/* Finish creating a structure or union type, including filling in
11445 its members and creating a symbol for it. */
11446
11447static void
11448process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11449{
11450 struct objfile *objfile = cu->objfile;
11451 struct die_info *child_die = die->child;
11452 struct type *type;
11453
11454 type = get_die_type (die, cu);
11455 if (type == NULL)
11456 type = read_structure_type (die, cu);
11457
e142c38c 11458 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11459 {
11460 struct field_info fi;
11461 struct die_info *child_die;
34eaf542 11462 VEC (symbolp) *template_args = NULL;
c767944b 11463 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11464
11465 memset (&fi, 0, sizeof (struct field_info));
11466
639d11d3 11467 child_die = die->child;
c906108c
SS
11468
11469 while (child_die && child_die->tag)
11470 {
a9a9bd0f
DC
11471 if (child_die->tag == DW_TAG_member
11472 || child_die->tag == DW_TAG_variable)
c906108c 11473 {
a9a9bd0f
DC
11474 /* NOTE: carlton/2002-11-05: A C++ static data member
11475 should be a DW_TAG_member that is a declaration, but
11476 all versions of G++ as of this writing (so through at
11477 least 3.2.1) incorrectly generate DW_TAG_variable
11478 tags for them instead. */
e7c27a73 11479 dwarf2_add_field (&fi, child_die, cu);
c906108c 11480 }
8713b1b1 11481 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11482 {
0963b4bd 11483 /* C++ member function. */
e7c27a73 11484 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11485 }
11486 else if (child_die->tag == DW_TAG_inheritance)
11487 {
11488 /* C++ base class field. */
e7c27a73 11489 dwarf2_add_field (&fi, child_die, cu);
c906108c 11490 }
98751a41
JK
11491 else if (child_die->tag == DW_TAG_typedef)
11492 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11493 else if (child_die->tag == DW_TAG_template_type_param
11494 || child_die->tag == DW_TAG_template_value_param)
11495 {
11496 struct symbol *arg = new_symbol (child_die, NULL, cu);
11497
f1078f66
DJ
11498 if (arg != NULL)
11499 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11500 }
11501
c906108c
SS
11502 child_die = sibling_die (child_die);
11503 }
11504
34eaf542
TT
11505 /* Attach template arguments to type. */
11506 if (! VEC_empty (symbolp, template_args))
11507 {
11508 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11509 TYPE_N_TEMPLATE_ARGUMENTS (type)
11510 = VEC_length (symbolp, template_args);
11511 TYPE_TEMPLATE_ARGUMENTS (type)
11512 = obstack_alloc (&objfile->objfile_obstack,
11513 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11514 * sizeof (struct symbol *)));
11515 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11516 VEC_address (symbolp, template_args),
11517 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11518 * sizeof (struct symbol *)));
11519 VEC_free (symbolp, template_args);
11520 }
11521
c906108c
SS
11522 /* Attach fields and member functions to the type. */
11523 if (fi.nfields)
e7c27a73 11524 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11525 if (fi.nfnfields)
11526 {
e7c27a73 11527 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11528
c5aa993b 11529 /* Get the type which refers to the base class (possibly this
c906108c 11530 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11531 class from the DW_AT_containing_type attribute. This use of
11532 DW_AT_containing_type is a GNU extension. */
c906108c 11533
e142c38c 11534 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11535 {
e7c27a73 11536 struct type *t = die_containing_type (die, cu);
c906108c
SS
11537
11538 TYPE_VPTR_BASETYPE (type) = t;
11539 if (type == t)
11540 {
c906108c
SS
11541 int i;
11542
11543 /* Our own class provides vtbl ptr. */
11544 for (i = TYPE_NFIELDS (t) - 1;
11545 i >= TYPE_N_BASECLASSES (t);
11546 --i)
11547 {
0d5cff50 11548 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11549
1168df01 11550 if (is_vtable_name (fieldname, cu))
c906108c
SS
11551 {
11552 TYPE_VPTR_FIELDNO (type) = i;
11553 break;
11554 }
11555 }
11556
11557 /* Complain if virtual function table field not found. */
11558 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11559 complaint (&symfile_complaints,
3e43a32a
MS
11560 _("virtual function table pointer "
11561 "not found when defining class '%s'"),
4d3c2250
KB
11562 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11563 "");
c906108c
SS
11564 }
11565 else
11566 {
11567 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11568 }
11569 }
f6235d4c
EZ
11570 else if (cu->producer
11571 && strncmp (cu->producer,
11572 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11573 {
11574 /* The IBM XLC compiler does not provide direct indication
11575 of the containing type, but the vtable pointer is
11576 always named __vfp. */
11577
11578 int i;
11579
11580 for (i = TYPE_NFIELDS (type) - 1;
11581 i >= TYPE_N_BASECLASSES (type);
11582 --i)
11583 {
11584 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11585 {
11586 TYPE_VPTR_FIELDNO (type) = i;
11587 TYPE_VPTR_BASETYPE (type) = type;
11588 break;
11589 }
11590 }
11591 }
c906108c 11592 }
98751a41
JK
11593
11594 /* Copy fi.typedef_field_list linked list elements content into the
11595 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11596 if (fi.typedef_field_list)
11597 {
11598 int i = fi.typedef_field_list_count;
11599
a0d7a4ff 11600 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11601 TYPE_TYPEDEF_FIELD_ARRAY (type)
11602 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11603 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11604
11605 /* Reverse the list order to keep the debug info elements order. */
11606 while (--i >= 0)
11607 {
11608 struct typedef_field *dest, *src;
6e70227d 11609
98751a41
JK
11610 dest = &TYPE_TYPEDEF_FIELD (type, i);
11611 src = &fi.typedef_field_list->field;
11612 fi.typedef_field_list = fi.typedef_field_list->next;
11613 *dest = *src;
11614 }
11615 }
c767944b
DJ
11616
11617 do_cleanups (back_to);
eb2a6f42
TT
11618
11619 if (HAVE_CPLUS_STRUCT (type))
11620 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11621 }
63d06c5c 11622
bb5ed363 11623 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11624
90aeadfc
DC
11625 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11626 snapshots) has been known to create a die giving a declaration
11627 for a class that has, as a child, a die giving a definition for a
11628 nested class. So we have to process our children even if the
11629 current die is a declaration. Normally, of course, a declaration
11630 won't have any children at all. */
134d01f1 11631
90aeadfc
DC
11632 while (child_die != NULL && child_die->tag)
11633 {
11634 if (child_die->tag == DW_TAG_member
11635 || child_die->tag == DW_TAG_variable
34eaf542
TT
11636 || child_die->tag == DW_TAG_inheritance
11637 || child_die->tag == DW_TAG_template_value_param
11638 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11639 {
90aeadfc 11640 /* Do nothing. */
134d01f1 11641 }
90aeadfc
DC
11642 else
11643 process_die (child_die, cu);
134d01f1 11644
90aeadfc 11645 child_die = sibling_die (child_die);
134d01f1
DJ
11646 }
11647
fa4028e9
JB
11648 /* Do not consider external references. According to the DWARF standard,
11649 these DIEs are identified by the fact that they have no byte_size
11650 attribute, and a declaration attribute. */
11651 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11652 || !die_is_declaration (die, cu))
c767944b 11653 new_symbol (die, type, cu);
134d01f1
DJ
11654}
11655
11656/* Given a DW_AT_enumeration_type die, set its type. We do not
11657 complete the type's fields yet, or create any symbols. */
c906108c 11658
f792889a 11659static struct type *
134d01f1 11660read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11661{
e7c27a73 11662 struct objfile *objfile = cu->objfile;
c906108c 11663 struct type *type;
c906108c 11664 struct attribute *attr;
0114d602 11665 const char *name;
134d01f1 11666
348e048f
DE
11667 /* If the definition of this type lives in .debug_types, read that type.
11668 Don't follow DW_AT_specification though, that will take us back up
11669 the chain and we want to go down. */
45e58e77 11670 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11671 if (attr)
11672 {
11673 struct dwarf2_cu *type_cu = cu;
11674 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11675
348e048f 11676 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11677
11678 /* TYPE_CU may not be the same as CU.
11679 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11680 return set_die_type (die, type, cu);
11681 }
11682
c906108c
SS
11683 type = alloc_type (objfile);
11684
11685 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11686 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11687 if (name != NULL)
7d455152 11688 TYPE_TAG_NAME (type) = name;
c906108c 11689
e142c38c 11690 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11691 if (attr)
11692 {
11693 TYPE_LENGTH (type) = DW_UNSND (attr);
11694 }
11695 else
11696 {
11697 TYPE_LENGTH (type) = 0;
11698 }
11699
137033e9
JB
11700 /* The enumeration DIE can be incomplete. In Ada, any type can be
11701 declared as private in the package spec, and then defined only
11702 inside the package body. Such types are known as Taft Amendment
11703 Types. When another package uses such a type, an incomplete DIE
11704 may be generated by the compiler. */
02eb380e 11705 if (die_is_declaration (die, cu))
876cecd0 11706 TYPE_STUB (type) = 1;
02eb380e 11707
f792889a 11708 return set_die_type (die, type, cu);
134d01f1
DJ
11709}
11710
11711/* Given a pointer to a die which begins an enumeration, process all
11712 the dies that define the members of the enumeration, and create the
11713 symbol for the enumeration type.
11714
11715 NOTE: We reverse the order of the element list. */
11716
11717static void
11718process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11719{
f792889a 11720 struct type *this_type;
134d01f1 11721
f792889a
DJ
11722 this_type = get_die_type (die, cu);
11723 if (this_type == NULL)
11724 this_type = read_enumeration_type (die, cu);
9dc481d3 11725
639d11d3 11726 if (die->child != NULL)
c906108c 11727 {
9dc481d3
DE
11728 struct die_info *child_die;
11729 struct symbol *sym;
11730 struct field *fields = NULL;
11731 int num_fields = 0;
11732 int unsigned_enum = 1;
15d034d0 11733 const char *name;
cafec441
TT
11734 int flag_enum = 1;
11735 ULONGEST mask = 0;
9dc481d3 11736
639d11d3 11737 child_die = die->child;
c906108c
SS
11738 while (child_die && child_die->tag)
11739 {
11740 if (child_die->tag != DW_TAG_enumerator)
11741 {
e7c27a73 11742 process_die (child_die, cu);
c906108c
SS
11743 }
11744 else
11745 {
39cbfefa
DJ
11746 name = dwarf2_name (child_die, cu);
11747 if (name)
c906108c 11748 {
f792889a 11749 sym = new_symbol (child_die, this_type, cu);
c906108c 11750 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11751 {
11752 unsigned_enum = 0;
11753 flag_enum = 0;
11754 }
11755 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11756 flag_enum = 0;
11757 else
11758 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11759
11760 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11761 {
11762 fields = (struct field *)
11763 xrealloc (fields,
11764 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11765 * sizeof (struct field));
c906108c
SS
11766 }
11767
3567439c 11768 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11769 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11770 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11771 FIELD_BITSIZE (fields[num_fields]) = 0;
11772
11773 num_fields++;
11774 }
11775 }
11776
11777 child_die = sibling_die (child_die);
11778 }
11779
11780 if (num_fields)
11781 {
f792889a
DJ
11782 TYPE_NFIELDS (this_type) = num_fields;
11783 TYPE_FIELDS (this_type) = (struct field *)
11784 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11785 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11786 sizeof (struct field) * num_fields);
b8c9b27d 11787 xfree (fields);
c906108c
SS
11788 }
11789 if (unsigned_enum)
876cecd0 11790 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11791 if (flag_enum)
11792 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11793 }
134d01f1 11794
6c83ed52
TT
11795 /* If we are reading an enum from a .debug_types unit, and the enum
11796 is a declaration, and the enum is not the signatured type in the
11797 unit, then we do not want to add a symbol for it. Adding a
11798 symbol would in some cases obscure the true definition of the
11799 enum, giving users an incomplete type when the definition is
11800 actually available. Note that we do not want to do this for all
11801 enums which are just declarations, because C++0x allows forward
11802 enum declarations. */
3019eac3 11803 if (cu->per_cu->is_debug_types
6c83ed52
TT
11804 && die_is_declaration (die, cu))
11805 {
52dc124a 11806 struct signatured_type *sig_type;
6c83ed52 11807
c0f78cd4 11808 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
11809 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11810 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11811 return;
11812 }
11813
f792889a 11814 new_symbol (die, this_type, cu);
c906108c
SS
11815}
11816
11817/* Extract all information from a DW_TAG_array_type DIE and put it in
11818 the DIE's type field. For now, this only handles one dimensional
11819 arrays. */
11820
f792889a 11821static struct type *
e7c27a73 11822read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11823{
e7c27a73 11824 struct objfile *objfile = cu->objfile;
c906108c 11825 struct die_info *child_die;
7e314c57 11826 struct type *type;
c906108c
SS
11827 struct type *element_type, *range_type, *index_type;
11828 struct type **range_types = NULL;
11829 struct attribute *attr;
11830 int ndim = 0;
11831 struct cleanup *back_to;
15d034d0 11832 const char *name;
c906108c 11833
e7c27a73 11834 element_type = die_type (die, cu);
c906108c 11835
7e314c57
JK
11836 /* The die_type call above may have already set the type for this DIE. */
11837 type = get_die_type (die, cu);
11838 if (type)
11839 return type;
11840
c906108c
SS
11841 /* Irix 6.2 native cc creates array types without children for
11842 arrays with unspecified length. */
639d11d3 11843 if (die->child == NULL)
c906108c 11844 {
46bf5051 11845 index_type = objfile_type (objfile)->builtin_int;
c906108c 11846 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11847 type = create_array_type (NULL, element_type, range_type);
11848 return set_die_type (die, type, cu);
c906108c
SS
11849 }
11850
11851 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11852 child_die = die->child;
c906108c
SS
11853 while (child_die && child_die->tag)
11854 {
11855 if (child_die->tag == DW_TAG_subrange_type)
11856 {
f792889a 11857 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11858
f792889a 11859 if (child_type != NULL)
a02abb62 11860 {
0963b4bd
MS
11861 /* The range type was succesfully read. Save it for the
11862 array type creation. */
a02abb62
JB
11863 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11864 {
11865 range_types = (struct type **)
11866 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11867 * sizeof (struct type *));
11868 if (ndim == 0)
11869 make_cleanup (free_current_contents, &range_types);
11870 }
f792889a 11871 range_types[ndim++] = child_type;
a02abb62 11872 }
c906108c
SS
11873 }
11874 child_die = sibling_die (child_die);
11875 }
11876
11877 /* Dwarf2 dimensions are output from left to right, create the
11878 necessary array types in backwards order. */
7ca2d3a3 11879
c906108c 11880 type = element_type;
7ca2d3a3
DL
11881
11882 if (read_array_order (die, cu) == DW_ORD_col_major)
11883 {
11884 int i = 0;
9a619af0 11885
7ca2d3a3
DL
11886 while (i < ndim)
11887 type = create_array_type (NULL, type, range_types[i++]);
11888 }
11889 else
11890 {
11891 while (ndim-- > 0)
11892 type = create_array_type (NULL, type, range_types[ndim]);
11893 }
c906108c 11894
f5f8a009
EZ
11895 /* Understand Dwarf2 support for vector types (like they occur on
11896 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11897 array type. This is not part of the Dwarf2/3 standard yet, but a
11898 custom vendor extension. The main difference between a regular
11899 array and the vector variant is that vectors are passed by value
11900 to functions. */
e142c38c 11901 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11902 if (attr)
ea37ba09 11903 make_vector_type (type);
f5f8a009 11904
dbc98a8b
KW
11905 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11906 implementation may choose to implement triple vectors using this
11907 attribute. */
11908 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11909 if (attr)
11910 {
11911 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11912 TYPE_LENGTH (type) = DW_UNSND (attr);
11913 else
3e43a32a
MS
11914 complaint (&symfile_complaints,
11915 _("DW_AT_byte_size for array type smaller "
11916 "than the total size of elements"));
dbc98a8b
KW
11917 }
11918
39cbfefa
DJ
11919 name = dwarf2_name (die, cu);
11920 if (name)
11921 TYPE_NAME (type) = name;
6e70227d 11922
0963b4bd 11923 /* Install the type in the die. */
7e314c57
JK
11924 set_die_type (die, type, cu);
11925
11926 /* set_die_type should be already done. */
b4ba55a1
JB
11927 set_descriptive_type (type, die, cu);
11928
c906108c
SS
11929 do_cleanups (back_to);
11930
7e314c57 11931 return type;
c906108c
SS
11932}
11933
7ca2d3a3 11934static enum dwarf_array_dim_ordering
6e70227d 11935read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11936{
11937 struct attribute *attr;
11938
11939 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11940
11941 if (attr) return DW_SND (attr);
11942
0963b4bd
MS
11943 /* GNU F77 is a special case, as at 08/2004 array type info is the
11944 opposite order to the dwarf2 specification, but data is still
11945 laid out as per normal fortran.
7ca2d3a3 11946
0963b4bd
MS
11947 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11948 version checking. */
7ca2d3a3 11949
905e0470
PM
11950 if (cu->language == language_fortran
11951 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11952 {
11953 return DW_ORD_row_major;
11954 }
11955
6e70227d 11956 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11957 {
11958 case array_column_major:
11959 return DW_ORD_col_major;
11960 case array_row_major:
11961 default:
11962 return DW_ORD_row_major;
11963 };
11964}
11965
72019c9c 11966/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11967 the DIE's type field. */
72019c9c 11968
f792889a 11969static struct type *
72019c9c
GM
11970read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11971{
7e314c57
JK
11972 struct type *domain_type, *set_type;
11973 struct attribute *attr;
f792889a 11974
7e314c57
JK
11975 domain_type = die_type (die, cu);
11976
11977 /* The die_type call above may have already set the type for this DIE. */
11978 set_type = get_die_type (die, cu);
11979 if (set_type)
11980 return set_type;
11981
11982 set_type = create_set_type (NULL, domain_type);
11983
11984 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11985 if (attr)
11986 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11987
f792889a 11988 return set_die_type (die, set_type, cu);
72019c9c 11989}
7ca2d3a3 11990
0971de02
TT
11991/* A helper for read_common_block that creates a locexpr baton.
11992 SYM is the symbol which we are marking as computed.
11993 COMMON_DIE is the DIE for the common block.
11994 COMMON_LOC is the location expression attribute for the common
11995 block itself.
11996 MEMBER_LOC is the location expression attribute for the particular
11997 member of the common block that we are processing.
11998 CU is the CU from which the above come. */
11999
12000static void
12001mark_common_block_symbol_computed (struct symbol *sym,
12002 struct die_info *common_die,
12003 struct attribute *common_loc,
12004 struct attribute *member_loc,
12005 struct dwarf2_cu *cu)
12006{
12007 struct objfile *objfile = dwarf2_per_objfile->objfile;
12008 struct dwarf2_locexpr_baton *baton;
12009 gdb_byte *ptr;
12010 unsigned int cu_off;
12011 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12012 LONGEST offset = 0;
12013
12014 gdb_assert (common_loc && member_loc);
12015 gdb_assert (attr_form_is_block (common_loc));
12016 gdb_assert (attr_form_is_block (member_loc)
12017 || attr_form_is_constant (member_loc));
12018
12019 baton = obstack_alloc (&objfile->objfile_obstack,
12020 sizeof (struct dwarf2_locexpr_baton));
12021 baton->per_cu = cu->per_cu;
12022 gdb_assert (baton->per_cu);
12023
12024 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12025
12026 if (attr_form_is_constant (member_loc))
12027 {
12028 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12029 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12030 }
12031 else
12032 baton->size += DW_BLOCK (member_loc)->size;
12033
12034 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12035 baton->data = ptr;
12036
12037 *ptr++ = DW_OP_call4;
12038 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12039 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12040 ptr += 4;
12041
12042 if (attr_form_is_constant (member_loc))
12043 {
12044 *ptr++ = DW_OP_addr;
12045 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12046 ptr += cu->header.addr_size;
12047 }
12048 else
12049 {
12050 /* We have to copy the data here, because DW_OP_call4 will only
12051 use a DW_AT_location attribute. */
12052 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12053 ptr += DW_BLOCK (member_loc)->size;
12054 }
12055
12056 *ptr++ = DW_OP_plus;
12057 gdb_assert (ptr - baton->data == baton->size);
12058
0971de02 12059 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12060 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12061}
12062
4357ac6c
TT
12063/* Create appropriate locally-scoped variables for all the
12064 DW_TAG_common_block entries. Also create a struct common_block
12065 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12066 is used to sepate the common blocks name namespace from regular
12067 variable names. */
c906108c
SS
12068
12069static void
e7c27a73 12070read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12071{
0971de02
TT
12072 struct attribute *attr;
12073
12074 attr = dwarf2_attr (die, DW_AT_location, cu);
12075 if (attr)
12076 {
12077 /* Support the .debug_loc offsets. */
12078 if (attr_form_is_block (attr))
12079 {
12080 /* Ok. */
12081 }
12082 else if (attr_form_is_section_offset (attr))
12083 {
12084 dwarf2_complex_location_expr_complaint ();
12085 attr = NULL;
12086 }
12087 else
12088 {
12089 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12090 "common block member");
12091 attr = NULL;
12092 }
12093 }
12094
639d11d3 12095 if (die->child != NULL)
c906108c 12096 {
4357ac6c
TT
12097 struct objfile *objfile = cu->objfile;
12098 struct die_info *child_die;
12099 size_t n_entries = 0, size;
12100 struct common_block *common_block;
12101 struct symbol *sym;
74ac6d43 12102
4357ac6c
TT
12103 for (child_die = die->child;
12104 child_die && child_die->tag;
12105 child_die = sibling_die (child_die))
12106 ++n_entries;
12107
12108 size = (sizeof (struct common_block)
12109 + (n_entries - 1) * sizeof (struct symbol *));
12110 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12111 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12112 common_block->n_entries = 0;
12113
12114 for (child_die = die->child;
12115 child_die && child_die->tag;
12116 child_die = sibling_die (child_die))
12117 {
12118 /* Create the symbol in the DW_TAG_common_block block in the current
12119 symbol scope. */
e7c27a73 12120 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12121 if (sym != NULL)
12122 {
12123 struct attribute *member_loc;
12124
12125 common_block->contents[common_block->n_entries++] = sym;
12126
12127 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12128 cu);
12129 if (member_loc)
12130 {
12131 /* GDB has handled this for a long time, but it is
12132 not specified by DWARF. It seems to have been
12133 emitted by gfortran at least as recently as:
12134 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12135 complaint (&symfile_complaints,
12136 _("Variable in common block has "
12137 "DW_AT_data_member_location "
12138 "- DIE at 0x%x [in module %s]"),
12139 child_die->offset.sect_off, cu->objfile->name);
12140
12141 if (attr_form_is_section_offset (member_loc))
12142 dwarf2_complex_location_expr_complaint ();
12143 else if (attr_form_is_constant (member_loc)
12144 || attr_form_is_block (member_loc))
12145 {
12146 if (attr)
12147 mark_common_block_symbol_computed (sym, die, attr,
12148 member_loc, cu);
12149 }
12150 else
12151 dwarf2_complex_location_expr_complaint ();
12152 }
12153 }
c906108c 12154 }
4357ac6c
TT
12155
12156 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12157 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12158 }
12159}
12160
0114d602 12161/* Create a type for a C++ namespace. */
d9fa45fe 12162
0114d602
DJ
12163static struct type *
12164read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12165{
e7c27a73 12166 struct objfile *objfile = cu->objfile;
0114d602 12167 const char *previous_prefix, *name;
9219021c 12168 int is_anonymous;
0114d602
DJ
12169 struct type *type;
12170
12171 /* For extensions, reuse the type of the original namespace. */
12172 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12173 {
12174 struct die_info *ext_die;
12175 struct dwarf2_cu *ext_cu = cu;
9a619af0 12176
0114d602
DJ
12177 ext_die = dwarf2_extension (die, &ext_cu);
12178 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12179
12180 /* EXT_CU may not be the same as CU.
12181 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12182 return set_die_type (die, type, cu);
12183 }
9219021c 12184
e142c38c 12185 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12186
12187 /* Now build the name of the current namespace. */
12188
0114d602
DJ
12189 previous_prefix = determine_prefix (die, cu);
12190 if (previous_prefix[0] != '\0')
12191 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12192 previous_prefix, name, 0, cu);
0114d602
DJ
12193
12194 /* Create the type. */
12195 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12196 objfile);
abee88f2 12197 TYPE_NAME (type) = name;
0114d602
DJ
12198 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12199
60531b24 12200 return set_die_type (die, type, cu);
0114d602
DJ
12201}
12202
12203/* Read a C++ namespace. */
12204
12205static void
12206read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12207{
12208 struct objfile *objfile = cu->objfile;
0114d602 12209 int is_anonymous;
9219021c 12210
5c4e30ca
DC
12211 /* Add a symbol associated to this if we haven't seen the namespace
12212 before. Also, add a using directive if it's an anonymous
12213 namespace. */
9219021c 12214
f2f0e013 12215 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12216 {
12217 struct type *type;
12218
0114d602 12219 type = read_type_die (die, cu);
e7c27a73 12220 new_symbol (die, type, cu);
5c4e30ca 12221
e8e80198 12222 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12223 if (is_anonymous)
0114d602
DJ
12224 {
12225 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12226
c0cc3a76 12227 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12228 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12229 }
5c4e30ca 12230 }
9219021c 12231
639d11d3 12232 if (die->child != NULL)
d9fa45fe 12233 {
639d11d3 12234 struct die_info *child_die = die->child;
6e70227d 12235
d9fa45fe
DC
12236 while (child_die && child_die->tag)
12237 {
e7c27a73 12238 process_die (child_die, cu);
d9fa45fe
DC
12239 child_die = sibling_die (child_die);
12240 }
12241 }
38d518c9
EZ
12242}
12243
f55ee35c
JK
12244/* Read a Fortran module as type. This DIE can be only a declaration used for
12245 imported module. Still we need that type as local Fortran "use ... only"
12246 declaration imports depend on the created type in determine_prefix. */
12247
12248static struct type *
12249read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12250{
12251 struct objfile *objfile = cu->objfile;
15d034d0 12252 const char *module_name;
f55ee35c
JK
12253 struct type *type;
12254
12255 module_name = dwarf2_name (die, cu);
12256 if (!module_name)
3e43a32a
MS
12257 complaint (&symfile_complaints,
12258 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12259 die->offset.sect_off);
f55ee35c
JK
12260 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12261
12262 /* determine_prefix uses TYPE_TAG_NAME. */
12263 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12264
12265 return set_die_type (die, type, cu);
12266}
12267
5d7cb8df
JK
12268/* Read a Fortran module. */
12269
12270static void
12271read_module (struct die_info *die, struct dwarf2_cu *cu)
12272{
12273 struct die_info *child_die = die->child;
12274
5d7cb8df
JK
12275 while (child_die && child_die->tag)
12276 {
12277 process_die (child_die, cu);
12278 child_die = sibling_die (child_die);
12279 }
12280}
12281
38d518c9
EZ
12282/* Return the name of the namespace represented by DIE. Set
12283 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12284 namespace. */
12285
12286static const char *
e142c38c 12287namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12288{
12289 struct die_info *current_die;
12290 const char *name = NULL;
12291
12292 /* Loop through the extensions until we find a name. */
12293
12294 for (current_die = die;
12295 current_die != NULL;
f2f0e013 12296 current_die = dwarf2_extension (die, &cu))
38d518c9 12297 {
e142c38c 12298 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12299 if (name != NULL)
12300 break;
12301 }
12302
12303 /* Is it an anonymous namespace? */
12304
12305 *is_anonymous = (name == NULL);
12306 if (*is_anonymous)
2b1dbab0 12307 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12308
12309 return name;
d9fa45fe
DC
12310}
12311
c906108c
SS
12312/* Extract all information from a DW_TAG_pointer_type DIE and add to
12313 the user defined type vector. */
12314
f792889a 12315static struct type *
e7c27a73 12316read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12317{
5e2b427d 12318 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12319 struct comp_unit_head *cu_header = &cu->header;
c906108c 12320 struct type *type;
8b2dbe47
KB
12321 struct attribute *attr_byte_size;
12322 struct attribute *attr_address_class;
12323 int byte_size, addr_class;
7e314c57
JK
12324 struct type *target_type;
12325
12326 target_type = die_type (die, cu);
c906108c 12327
7e314c57
JK
12328 /* The die_type call above may have already set the type for this DIE. */
12329 type = get_die_type (die, cu);
12330 if (type)
12331 return type;
12332
12333 type = lookup_pointer_type (target_type);
8b2dbe47 12334
e142c38c 12335 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12336 if (attr_byte_size)
12337 byte_size = DW_UNSND (attr_byte_size);
c906108c 12338 else
8b2dbe47
KB
12339 byte_size = cu_header->addr_size;
12340
e142c38c 12341 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12342 if (attr_address_class)
12343 addr_class = DW_UNSND (attr_address_class);
12344 else
12345 addr_class = DW_ADDR_none;
12346
12347 /* If the pointer size or address class is different than the
12348 default, create a type variant marked as such and set the
12349 length accordingly. */
12350 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12351 {
5e2b427d 12352 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12353 {
12354 int type_flags;
12355
849957d9 12356 type_flags = gdbarch_address_class_type_flags
5e2b427d 12357 (gdbarch, byte_size, addr_class);
876cecd0
TT
12358 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12359 == 0);
8b2dbe47
KB
12360 type = make_type_with_address_space (type, type_flags);
12361 }
12362 else if (TYPE_LENGTH (type) != byte_size)
12363 {
3e43a32a
MS
12364 complaint (&symfile_complaints,
12365 _("invalid pointer size %d"), byte_size);
8b2dbe47 12366 }
6e70227d 12367 else
9a619af0
MS
12368 {
12369 /* Should we also complain about unhandled address classes? */
12370 }
c906108c 12371 }
8b2dbe47
KB
12372
12373 TYPE_LENGTH (type) = byte_size;
f792889a 12374 return set_die_type (die, type, cu);
c906108c
SS
12375}
12376
12377/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12378 the user defined type vector. */
12379
f792889a 12380static struct type *
e7c27a73 12381read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12382{
12383 struct type *type;
12384 struct type *to_type;
12385 struct type *domain;
12386
e7c27a73
DJ
12387 to_type = die_type (die, cu);
12388 domain = die_containing_type (die, cu);
0d5de010 12389
7e314c57
JK
12390 /* The calls above may have already set the type for this DIE. */
12391 type = get_die_type (die, cu);
12392 if (type)
12393 return type;
12394
0d5de010
DJ
12395 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12396 type = lookup_methodptr_type (to_type);
7078baeb
TT
12397 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12398 {
12399 struct type *new_type = alloc_type (cu->objfile);
12400
12401 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12402 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12403 TYPE_VARARGS (to_type));
12404 type = lookup_methodptr_type (new_type);
12405 }
0d5de010
DJ
12406 else
12407 type = lookup_memberptr_type (to_type, domain);
c906108c 12408
f792889a 12409 return set_die_type (die, type, cu);
c906108c
SS
12410}
12411
12412/* Extract all information from a DW_TAG_reference_type DIE and add to
12413 the user defined type vector. */
12414
f792889a 12415static struct type *
e7c27a73 12416read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12417{
e7c27a73 12418 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12419 struct type *type, *target_type;
c906108c
SS
12420 struct attribute *attr;
12421
7e314c57
JK
12422 target_type = die_type (die, cu);
12423
12424 /* The die_type call above may have already set the type for this DIE. */
12425 type = get_die_type (die, cu);
12426 if (type)
12427 return type;
12428
12429 type = lookup_reference_type (target_type);
e142c38c 12430 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12431 if (attr)
12432 {
12433 TYPE_LENGTH (type) = DW_UNSND (attr);
12434 }
12435 else
12436 {
107d2387 12437 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12438 }
f792889a 12439 return set_die_type (die, type, cu);
c906108c
SS
12440}
12441
f792889a 12442static struct type *
e7c27a73 12443read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12444{
f792889a 12445 struct type *base_type, *cv_type;
c906108c 12446
e7c27a73 12447 base_type = die_type (die, cu);
7e314c57
JK
12448
12449 /* The die_type call above may have already set the type for this DIE. */
12450 cv_type = get_die_type (die, cu);
12451 if (cv_type)
12452 return cv_type;
12453
2f608a3a
KW
12454 /* In case the const qualifier is applied to an array type, the element type
12455 is so qualified, not the array type (section 6.7.3 of C99). */
12456 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12457 {
12458 struct type *el_type, *inner_array;
12459
12460 base_type = copy_type (base_type);
12461 inner_array = base_type;
12462
12463 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12464 {
12465 TYPE_TARGET_TYPE (inner_array) =
12466 copy_type (TYPE_TARGET_TYPE (inner_array));
12467 inner_array = TYPE_TARGET_TYPE (inner_array);
12468 }
12469
12470 el_type = TYPE_TARGET_TYPE (inner_array);
12471 TYPE_TARGET_TYPE (inner_array) =
12472 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12473
12474 return set_die_type (die, base_type, cu);
12475 }
12476
f792889a
DJ
12477 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12478 return set_die_type (die, cv_type, cu);
c906108c
SS
12479}
12480
f792889a 12481static struct type *
e7c27a73 12482read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12483{
f792889a 12484 struct type *base_type, *cv_type;
c906108c 12485
e7c27a73 12486 base_type = die_type (die, cu);
7e314c57
JK
12487
12488 /* The die_type call above may have already set the type for this DIE. */
12489 cv_type = get_die_type (die, cu);
12490 if (cv_type)
12491 return cv_type;
12492
f792889a
DJ
12493 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12494 return set_die_type (die, cv_type, cu);
c906108c
SS
12495}
12496
06d66ee9
TT
12497/* Handle DW_TAG_restrict_type. */
12498
12499static struct type *
12500read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12501{
12502 struct type *base_type, *cv_type;
12503
12504 base_type = die_type (die, cu);
12505
12506 /* The die_type call above may have already set the type for this DIE. */
12507 cv_type = get_die_type (die, cu);
12508 if (cv_type)
12509 return cv_type;
12510
12511 cv_type = make_restrict_type (base_type);
12512 return set_die_type (die, cv_type, cu);
12513}
12514
c906108c
SS
12515/* Extract all information from a DW_TAG_string_type DIE and add to
12516 the user defined type vector. It isn't really a user defined type,
12517 but it behaves like one, with other DIE's using an AT_user_def_type
12518 attribute to reference it. */
12519
f792889a 12520static struct type *
e7c27a73 12521read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12522{
e7c27a73 12523 struct objfile *objfile = cu->objfile;
3b7538c0 12524 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12525 struct type *type, *range_type, *index_type, *char_type;
12526 struct attribute *attr;
12527 unsigned int length;
12528
e142c38c 12529 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12530 if (attr)
12531 {
12532 length = DW_UNSND (attr);
12533 }
12534 else
12535 {
0963b4bd 12536 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12537 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12538 if (attr)
12539 {
12540 length = DW_UNSND (attr);
12541 }
12542 else
12543 {
12544 length = 1;
12545 }
c906108c 12546 }
6ccb9162 12547
46bf5051 12548 index_type = objfile_type (objfile)->builtin_int;
c906108c 12549 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12550 char_type = language_string_char_type (cu->language_defn, gdbarch);
12551 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12552
f792889a 12553 return set_die_type (die, type, cu);
c906108c
SS
12554}
12555
12556/* Handle DIES due to C code like:
12557
12558 struct foo
c5aa993b
JM
12559 {
12560 int (*funcp)(int a, long l);
12561 int b;
12562 };
c906108c 12563
0963b4bd 12564 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12565
f792889a 12566static struct type *
e7c27a73 12567read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12568{
bb5ed363 12569 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12570 struct type *type; /* Type that this function returns. */
12571 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12572 struct attribute *attr;
12573
e7c27a73 12574 type = die_type (die, cu);
7e314c57
JK
12575
12576 /* The die_type call above may have already set the type for this DIE. */
12577 ftype = get_die_type (die, cu);
12578 if (ftype)
12579 return ftype;
12580
0c8b41f1 12581 ftype = lookup_function_type (type);
c906108c 12582
5b8101ae 12583 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12584 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12585 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12586 || cu->language == language_cplus
5b8101ae
PM
12587 || cu->language == language_java
12588 || cu->language == language_pascal)
876cecd0 12589 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12590 else if (producer_is_realview (cu->producer))
12591 /* RealView does not emit DW_AT_prototyped. We can not
12592 distinguish prototyped and unprototyped functions; default to
12593 prototyped, since that is more common in modern code (and
12594 RealView warns about unprototyped functions). */
12595 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12596
c055b101
CV
12597 /* Store the calling convention in the type if it's available in
12598 the subroutine die. Otherwise set the calling convention to
12599 the default value DW_CC_normal. */
12600 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12601 if (attr)
12602 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12603 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12604 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12605 else
12606 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12607
12608 /* We need to add the subroutine type to the die immediately so
12609 we don't infinitely recurse when dealing with parameters
0963b4bd 12610 declared as the same subroutine type. */
76c10ea2 12611 set_die_type (die, ftype, cu);
6e70227d 12612
639d11d3 12613 if (die->child != NULL)
c906108c 12614 {
bb5ed363 12615 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12616 struct die_info *child_die;
8072405b 12617 int nparams, iparams;
c906108c
SS
12618
12619 /* Count the number of parameters.
12620 FIXME: GDB currently ignores vararg functions, but knows about
12621 vararg member functions. */
8072405b 12622 nparams = 0;
639d11d3 12623 child_die = die->child;
c906108c
SS
12624 while (child_die && child_die->tag)
12625 {
12626 if (child_die->tag == DW_TAG_formal_parameter)
12627 nparams++;
12628 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12629 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12630 child_die = sibling_die (child_die);
12631 }
12632
12633 /* Allocate storage for parameters and fill them in. */
12634 TYPE_NFIELDS (ftype) = nparams;
12635 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12636 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12637
8072405b
JK
12638 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12639 even if we error out during the parameters reading below. */
12640 for (iparams = 0; iparams < nparams; iparams++)
12641 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12642
12643 iparams = 0;
639d11d3 12644 child_die = die->child;
c906108c
SS
12645 while (child_die && child_die->tag)
12646 {
12647 if (child_die->tag == DW_TAG_formal_parameter)
12648 {
3ce3b1ba
PA
12649 struct type *arg_type;
12650
12651 /* DWARF version 2 has no clean way to discern C++
12652 static and non-static member functions. G++ helps
12653 GDB by marking the first parameter for non-static
12654 member functions (which is the this pointer) as
12655 artificial. We pass this information to
12656 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12657
12658 DWARF version 3 added DW_AT_object_pointer, which GCC
12659 4.5 does not yet generate. */
e142c38c 12660 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12661 if (attr)
12662 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12663 else
418835cc
KS
12664 {
12665 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12666
12667 /* GCC/43521: In java, the formal parameter
12668 "this" is sometimes not marked with DW_AT_artificial. */
12669 if (cu->language == language_java)
12670 {
12671 const char *name = dwarf2_name (child_die, cu);
9a619af0 12672
418835cc
KS
12673 if (name && !strcmp (name, "this"))
12674 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12675 }
12676 }
3ce3b1ba
PA
12677 arg_type = die_type (child_die, cu);
12678
12679 /* RealView does not mark THIS as const, which the testsuite
12680 expects. GCC marks THIS as const in method definitions,
12681 but not in the class specifications (GCC PR 43053). */
12682 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12683 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12684 {
12685 int is_this = 0;
12686 struct dwarf2_cu *arg_cu = cu;
12687 const char *name = dwarf2_name (child_die, cu);
12688
12689 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12690 if (attr)
12691 {
12692 /* If the compiler emits this, use it. */
12693 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12694 is_this = 1;
12695 }
12696 else if (name && strcmp (name, "this") == 0)
12697 /* Function definitions will have the argument names. */
12698 is_this = 1;
12699 else if (name == NULL && iparams == 0)
12700 /* Declarations may not have the names, so like
12701 elsewhere in GDB, assume an artificial first
12702 argument is "this". */
12703 is_this = 1;
12704
12705 if (is_this)
12706 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12707 arg_type, 0);
12708 }
12709
12710 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12711 iparams++;
12712 }
12713 child_die = sibling_die (child_die);
12714 }
12715 }
12716
76c10ea2 12717 return ftype;
c906108c
SS
12718}
12719
f792889a 12720static struct type *
e7c27a73 12721read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12722{
e7c27a73 12723 struct objfile *objfile = cu->objfile;
0114d602 12724 const char *name = NULL;
3c8e0968 12725 struct type *this_type, *target_type;
c906108c 12726
94af9270 12727 name = dwarf2_full_name (NULL, die, cu);
f792889a 12728 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12729 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12730 TYPE_NAME (this_type) = name;
f792889a 12731 set_die_type (die, this_type, cu);
3c8e0968
DE
12732 target_type = die_type (die, cu);
12733 if (target_type != this_type)
12734 TYPE_TARGET_TYPE (this_type) = target_type;
12735 else
12736 {
12737 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12738 spec and cause infinite loops in GDB. */
12739 complaint (&symfile_complaints,
12740 _("Self-referential DW_TAG_typedef "
12741 "- DIE at 0x%x [in module %s]"),
b64f50a1 12742 die->offset.sect_off, objfile->name);
3c8e0968
DE
12743 TYPE_TARGET_TYPE (this_type) = NULL;
12744 }
f792889a 12745 return this_type;
c906108c
SS
12746}
12747
12748/* Find a representation of a given base type and install
12749 it in the TYPE field of the die. */
12750
f792889a 12751static struct type *
e7c27a73 12752read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12753{
e7c27a73 12754 struct objfile *objfile = cu->objfile;
c906108c
SS
12755 struct type *type;
12756 struct attribute *attr;
12757 int encoding = 0, size = 0;
15d034d0 12758 const char *name;
6ccb9162
UW
12759 enum type_code code = TYPE_CODE_INT;
12760 int type_flags = 0;
12761 struct type *target_type = NULL;
c906108c 12762
e142c38c 12763 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12764 if (attr)
12765 {
12766 encoding = DW_UNSND (attr);
12767 }
e142c38c 12768 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12769 if (attr)
12770 {
12771 size = DW_UNSND (attr);
12772 }
39cbfefa 12773 name = dwarf2_name (die, cu);
6ccb9162 12774 if (!name)
c906108c 12775 {
6ccb9162
UW
12776 complaint (&symfile_complaints,
12777 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12778 }
6ccb9162
UW
12779
12780 switch (encoding)
c906108c 12781 {
6ccb9162
UW
12782 case DW_ATE_address:
12783 /* Turn DW_ATE_address into a void * pointer. */
12784 code = TYPE_CODE_PTR;
12785 type_flags |= TYPE_FLAG_UNSIGNED;
12786 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12787 break;
12788 case DW_ATE_boolean:
12789 code = TYPE_CODE_BOOL;
12790 type_flags |= TYPE_FLAG_UNSIGNED;
12791 break;
12792 case DW_ATE_complex_float:
12793 code = TYPE_CODE_COMPLEX;
12794 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12795 break;
12796 case DW_ATE_decimal_float:
12797 code = TYPE_CODE_DECFLOAT;
12798 break;
12799 case DW_ATE_float:
12800 code = TYPE_CODE_FLT;
12801 break;
12802 case DW_ATE_signed:
12803 break;
12804 case DW_ATE_unsigned:
12805 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12806 if (cu->language == language_fortran
12807 && name
12808 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12809 code = TYPE_CODE_CHAR;
6ccb9162
UW
12810 break;
12811 case DW_ATE_signed_char:
6e70227d 12812 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12813 || cu->language == language_pascal
12814 || cu->language == language_fortran)
6ccb9162
UW
12815 code = TYPE_CODE_CHAR;
12816 break;
12817 case DW_ATE_unsigned_char:
868a0084 12818 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12819 || cu->language == language_pascal
12820 || cu->language == language_fortran)
6ccb9162
UW
12821 code = TYPE_CODE_CHAR;
12822 type_flags |= TYPE_FLAG_UNSIGNED;
12823 break;
75079b2b
TT
12824 case DW_ATE_UTF:
12825 /* We just treat this as an integer and then recognize the
12826 type by name elsewhere. */
12827 break;
12828
6ccb9162
UW
12829 default:
12830 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12831 dwarf_type_encoding_name (encoding));
12832 break;
c906108c 12833 }
6ccb9162 12834
0114d602
DJ
12835 type = init_type (code, size, type_flags, NULL, objfile);
12836 TYPE_NAME (type) = name;
6ccb9162
UW
12837 TYPE_TARGET_TYPE (type) = target_type;
12838
0114d602 12839 if (name && strcmp (name, "char") == 0)
876cecd0 12840 TYPE_NOSIGN (type) = 1;
0114d602 12841
f792889a 12842 return set_die_type (die, type, cu);
c906108c
SS
12843}
12844
a02abb62
JB
12845/* Read the given DW_AT_subrange DIE. */
12846
f792889a 12847static struct type *
a02abb62
JB
12848read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12849{
4c9ad8c2 12850 struct type *base_type, *orig_base_type;
a02abb62
JB
12851 struct type *range_type;
12852 struct attribute *attr;
4fae6e18
JK
12853 LONGEST low, high;
12854 int low_default_is_valid;
15d034d0 12855 const char *name;
43bbcdc2 12856 LONGEST negative_mask;
e77813c8 12857
4c9ad8c2
TT
12858 orig_base_type = die_type (die, cu);
12859 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12860 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12861 creating the range type, but we use the result of check_typedef
12862 when examining properties of the type. */
12863 base_type = check_typedef (orig_base_type);
a02abb62 12864
7e314c57
JK
12865 /* The die_type call above may have already set the type for this DIE. */
12866 range_type = get_die_type (die, cu);
12867 if (range_type)
12868 return range_type;
12869
4fae6e18
JK
12870 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12871 omitting DW_AT_lower_bound. */
12872 switch (cu->language)
6e70227d 12873 {
4fae6e18
JK
12874 case language_c:
12875 case language_cplus:
12876 low = 0;
12877 low_default_is_valid = 1;
12878 break;
12879 case language_fortran:
12880 low = 1;
12881 low_default_is_valid = 1;
12882 break;
12883 case language_d:
12884 case language_java:
12885 case language_objc:
12886 low = 0;
12887 low_default_is_valid = (cu->header.version >= 4);
12888 break;
12889 case language_ada:
12890 case language_m2:
12891 case language_pascal:
a02abb62 12892 low = 1;
4fae6e18
JK
12893 low_default_is_valid = (cu->header.version >= 4);
12894 break;
12895 default:
12896 low = 0;
12897 low_default_is_valid = 0;
12898 break;
a02abb62
JB
12899 }
12900
dd5e6932
DJ
12901 /* FIXME: For variable sized arrays either of these could be
12902 a variable rather than a constant value. We'll allow it,
12903 but we don't know how to handle it. */
e142c38c 12904 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12905 if (attr)
4fae6e18
JK
12906 low = dwarf2_get_attr_constant_value (attr, low);
12907 else if (!low_default_is_valid)
12908 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12909 "- DIE at 0x%x [in module %s]"),
12910 die->offset.sect_off, cu->objfile->name);
a02abb62 12911
e142c38c 12912 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12913 if (attr)
6e70227d 12914 {
d48323d8 12915 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12916 {
12917 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12918 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12919 FIXME: GDB does not yet know how to handle dynamic
12920 arrays properly, treat them as arrays with unspecified
12921 length for now.
12922
12923 FIXME: jimb/2003-09-22: GDB does not really know
12924 how to handle arrays of unspecified length
12925 either; we just represent them as zero-length
12926 arrays. Choose an appropriate upper bound given
12927 the lower bound we've computed above. */
12928 high = low - 1;
12929 }
12930 else
12931 high = dwarf2_get_attr_constant_value (attr, 1);
12932 }
e77813c8
PM
12933 else
12934 {
12935 attr = dwarf2_attr (die, DW_AT_count, cu);
12936 if (attr)
12937 {
12938 int count = dwarf2_get_attr_constant_value (attr, 1);
12939 high = low + count - 1;
12940 }
c2ff108b
JK
12941 else
12942 {
12943 /* Unspecified array length. */
12944 high = low - 1;
12945 }
e77813c8
PM
12946 }
12947
12948 /* Dwarf-2 specifications explicitly allows to create subrange types
12949 without specifying a base type.
12950 In that case, the base type must be set to the type of
12951 the lower bound, upper bound or count, in that order, if any of these
12952 three attributes references an object that has a type.
12953 If no base type is found, the Dwarf-2 specifications say that
12954 a signed integer type of size equal to the size of an address should
12955 be used.
12956 For the following C code: `extern char gdb_int [];'
12957 GCC produces an empty range DIE.
12958 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12959 high bound or count are not yet handled by this code. */
e77813c8
PM
12960 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12961 {
12962 struct objfile *objfile = cu->objfile;
12963 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12964 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12965 struct type *int_type = objfile_type (objfile)->builtin_int;
12966
12967 /* Test "int", "long int", and "long long int" objfile types,
12968 and select the first one having a size above or equal to the
12969 architecture address size. */
12970 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12971 base_type = int_type;
12972 else
12973 {
12974 int_type = objfile_type (objfile)->builtin_long;
12975 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12976 base_type = int_type;
12977 else
12978 {
12979 int_type = objfile_type (objfile)->builtin_long_long;
12980 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12981 base_type = int_type;
12982 }
12983 }
12984 }
a02abb62 12985
6e70227d 12986 negative_mask =
43bbcdc2
PH
12987 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12988 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12989 low |= negative_mask;
12990 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12991 high |= negative_mask;
12992
4c9ad8c2 12993 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 12994
bbb0eef6
JK
12995 /* Mark arrays with dynamic length at least as an array of unspecified
12996 length. GDB could check the boundary but before it gets implemented at
12997 least allow accessing the array elements. */
d48323d8 12998 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12999 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13000
c2ff108b
JK
13001 /* Ada expects an empty array on no boundary attributes. */
13002 if (attr == NULL && cu->language != language_ada)
13003 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13004
39cbfefa
DJ
13005 name = dwarf2_name (die, cu);
13006 if (name)
13007 TYPE_NAME (range_type) = name;
6e70227d 13008
e142c38c 13009 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13010 if (attr)
13011 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13012
7e314c57
JK
13013 set_die_type (die, range_type, cu);
13014
13015 /* set_die_type should be already done. */
b4ba55a1
JB
13016 set_descriptive_type (range_type, die, cu);
13017
7e314c57 13018 return range_type;
a02abb62 13019}
6e70227d 13020
f792889a 13021static struct type *
81a17f79
JB
13022read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13023{
13024 struct type *type;
81a17f79 13025
81a17f79
JB
13026 /* For now, we only support the C meaning of an unspecified type: void. */
13027
0114d602
DJ
13028 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13029 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13030
f792889a 13031 return set_die_type (die, type, cu);
81a17f79 13032}
a02abb62 13033
639d11d3
DC
13034/* Read a single die and all its descendents. Set the die's sibling
13035 field to NULL; set other fields in the die correctly, and set all
13036 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13037 location of the info_ptr after reading all of those dies. PARENT
13038 is the parent of the die in question. */
13039
13040static struct die_info *
dee91e82
DE
13041read_die_and_children (const struct die_reader_specs *reader,
13042 gdb_byte *info_ptr,
13043 gdb_byte **new_info_ptr,
13044 struct die_info *parent)
639d11d3
DC
13045{
13046 struct die_info *die;
fe1b8b76 13047 gdb_byte *cur_ptr;
639d11d3
DC
13048 int has_children;
13049
93311388 13050 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
13051 if (die == NULL)
13052 {
13053 *new_info_ptr = cur_ptr;
13054 return NULL;
13055 }
93311388 13056 store_in_ref_table (die, reader->cu);
639d11d3
DC
13057
13058 if (has_children)
348e048f 13059 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13060 else
13061 {
13062 die->child = NULL;
13063 *new_info_ptr = cur_ptr;
13064 }
13065
13066 die->sibling = NULL;
13067 die->parent = parent;
13068 return die;
13069}
13070
13071/* Read a die, all of its descendents, and all of its siblings; set
13072 all of the fields of all of the dies correctly. Arguments are as
13073 in read_die_and_children. */
13074
13075static struct die_info *
93311388
DE
13076read_die_and_siblings (const struct die_reader_specs *reader,
13077 gdb_byte *info_ptr,
fe1b8b76 13078 gdb_byte **new_info_ptr,
639d11d3
DC
13079 struct die_info *parent)
13080{
13081 struct die_info *first_die, *last_sibling;
fe1b8b76 13082 gdb_byte *cur_ptr;
639d11d3 13083
c906108c 13084 cur_ptr = info_ptr;
639d11d3
DC
13085 first_die = last_sibling = NULL;
13086
13087 while (1)
c906108c 13088 {
639d11d3 13089 struct die_info *die
dee91e82 13090 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13091
1d325ec1 13092 if (die == NULL)
c906108c 13093 {
639d11d3
DC
13094 *new_info_ptr = cur_ptr;
13095 return first_die;
c906108c 13096 }
1d325ec1
DJ
13097
13098 if (!first_die)
13099 first_die = die;
c906108c 13100 else
1d325ec1
DJ
13101 last_sibling->sibling = die;
13102
13103 last_sibling = die;
c906108c 13104 }
c906108c
SS
13105}
13106
3019eac3
DE
13107/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13108 attributes.
13109 The caller is responsible for filling in the extra attributes
13110 and updating (*DIEP)->num_attrs.
13111 Set DIEP to point to a newly allocated die with its information,
13112 except for its child, sibling, and parent fields.
13113 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
13114
13115static gdb_byte *
3019eac3
DE
13116read_full_die_1 (const struct die_reader_specs *reader,
13117 struct die_info **diep, gdb_byte *info_ptr,
13118 int *has_children, int num_extra_attrs)
93311388 13119{
b64f50a1
JK
13120 unsigned int abbrev_number, bytes_read, i;
13121 sect_offset offset;
93311388
DE
13122 struct abbrev_info *abbrev;
13123 struct die_info *die;
13124 struct dwarf2_cu *cu = reader->cu;
13125 bfd *abfd = reader->abfd;
13126
b64f50a1 13127 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13128 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13129 info_ptr += bytes_read;
13130 if (!abbrev_number)
13131 {
13132 *diep = NULL;
13133 *has_children = 0;
13134 return info_ptr;
13135 }
13136
433df2d4 13137 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13138 if (!abbrev)
348e048f
DE
13139 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13140 abbrev_number,
13141 bfd_get_filename (abfd));
13142
3019eac3 13143 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13144 die->offset = offset;
13145 die->tag = abbrev->tag;
13146 die->abbrev = abbrev_number;
13147
3019eac3
DE
13148 /* Make the result usable.
13149 The caller needs to update num_attrs after adding the extra
13150 attributes. */
93311388
DE
13151 die->num_attrs = abbrev->num_attrs;
13152
13153 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13154 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13155 info_ptr);
93311388
DE
13156
13157 *diep = die;
13158 *has_children = abbrev->has_children;
13159 return info_ptr;
13160}
13161
3019eac3
DE
13162/* Read a die and all its attributes.
13163 Set DIEP to point to a newly allocated die with its information,
13164 except for its child, sibling, and parent fields.
13165 Set HAS_CHILDREN to tell whether the die has children or not. */
13166
13167static gdb_byte *
13168read_full_die (const struct die_reader_specs *reader,
13169 struct die_info **diep, gdb_byte *info_ptr,
13170 int *has_children)
13171{
13172 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13173}
433df2d4
DE
13174\f
13175/* Abbreviation tables.
3019eac3 13176
433df2d4 13177 In DWARF version 2, the description of the debugging information is
c906108c
SS
13178 stored in a separate .debug_abbrev section. Before we read any
13179 dies from a section we read in all abbreviations and install them
433df2d4
DE
13180 in a hash table. */
13181
13182/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13183
13184static struct abbrev_info *
13185abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13186{
13187 struct abbrev_info *abbrev;
13188
13189 abbrev = (struct abbrev_info *)
13190 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13191 memset (abbrev, 0, sizeof (struct abbrev_info));
13192 return abbrev;
13193}
13194
13195/* Add an abbreviation to the table. */
c906108c
SS
13196
13197static void
433df2d4
DE
13198abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13199 unsigned int abbrev_number,
13200 struct abbrev_info *abbrev)
13201{
13202 unsigned int hash_number;
13203
13204 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13205 abbrev->next = abbrev_table->abbrevs[hash_number];
13206 abbrev_table->abbrevs[hash_number] = abbrev;
13207}
dee91e82 13208
433df2d4
DE
13209/* Look up an abbrev in the table.
13210 Returns NULL if the abbrev is not found. */
13211
13212static struct abbrev_info *
13213abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13214 unsigned int abbrev_number)
c906108c 13215{
433df2d4
DE
13216 unsigned int hash_number;
13217 struct abbrev_info *abbrev;
13218
13219 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13220 abbrev = abbrev_table->abbrevs[hash_number];
13221
13222 while (abbrev)
13223 {
13224 if (abbrev->number == abbrev_number)
13225 return abbrev;
13226 abbrev = abbrev->next;
13227 }
13228 return NULL;
13229}
13230
13231/* Read in an abbrev table. */
13232
13233static struct abbrev_table *
13234abbrev_table_read_table (struct dwarf2_section_info *section,
13235 sect_offset offset)
13236{
13237 struct objfile *objfile = dwarf2_per_objfile->objfile;
13238 bfd *abfd = section->asection->owner;
13239 struct abbrev_table *abbrev_table;
fe1b8b76 13240 gdb_byte *abbrev_ptr;
c906108c
SS
13241 struct abbrev_info *cur_abbrev;
13242 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13243 unsigned int abbrev_form;
f3dd6933
DJ
13244 struct attr_abbrev *cur_attrs;
13245 unsigned int allocated_attrs;
c906108c 13246
433df2d4 13247 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13248 abbrev_table->offset = offset;
433df2d4
DE
13249 obstack_init (&abbrev_table->abbrev_obstack);
13250 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13251 (ABBREV_HASH_SIZE
13252 * sizeof (struct abbrev_info *)));
13253 memset (abbrev_table->abbrevs, 0,
13254 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13255
433df2d4
DE
13256 dwarf2_read_section (objfile, section);
13257 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13258 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13259 abbrev_ptr += bytes_read;
13260
f3dd6933
DJ
13261 allocated_attrs = ATTR_ALLOC_CHUNK;
13262 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13263
0963b4bd 13264 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13265 while (abbrev_number)
13266 {
433df2d4 13267 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13268
13269 /* read in abbrev header */
13270 cur_abbrev->number = abbrev_number;
13271 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13272 abbrev_ptr += bytes_read;
13273 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13274 abbrev_ptr += 1;
13275
13276 /* now read in declarations */
13277 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13278 abbrev_ptr += bytes_read;
13279 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13280 abbrev_ptr += bytes_read;
13281 while (abbrev_name)
13282 {
f3dd6933 13283 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13284 {
f3dd6933
DJ
13285 allocated_attrs += ATTR_ALLOC_CHUNK;
13286 cur_attrs
13287 = xrealloc (cur_attrs, (allocated_attrs
13288 * sizeof (struct attr_abbrev)));
c906108c 13289 }
ae038cb0 13290
f3dd6933
DJ
13291 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13292 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13293 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13294 abbrev_ptr += bytes_read;
13295 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13296 abbrev_ptr += bytes_read;
13297 }
13298
433df2d4 13299 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13300 (cur_abbrev->num_attrs
13301 * sizeof (struct attr_abbrev)));
13302 memcpy (cur_abbrev->attrs, cur_attrs,
13303 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13304
433df2d4 13305 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13306
13307 /* Get next abbreviation.
13308 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13309 always properly terminated with an abbrev number of 0.
13310 Exit loop if we encounter an abbreviation which we have
13311 already read (which means we are about to read the abbreviations
13312 for the next compile unit) or if the end of the abbreviation
13313 table is reached. */
433df2d4 13314 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13315 break;
13316 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13317 abbrev_ptr += bytes_read;
433df2d4 13318 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13319 break;
13320 }
f3dd6933
DJ
13321
13322 xfree (cur_attrs);
433df2d4 13323 return abbrev_table;
c906108c
SS
13324}
13325
433df2d4 13326/* Free the resources held by ABBREV_TABLE. */
c906108c 13327
c906108c 13328static void
433df2d4 13329abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13330{
433df2d4
DE
13331 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13332 xfree (abbrev_table);
c906108c
SS
13333}
13334
f4dc4d17
DE
13335/* Same as abbrev_table_free but as a cleanup.
13336 We pass in a pointer to the pointer to the table so that we can
13337 set the pointer to NULL when we're done. It also simplifies
13338 build_type_unit_groups. */
13339
13340static void
13341abbrev_table_free_cleanup (void *table_ptr)
13342{
13343 struct abbrev_table **abbrev_table_ptr = table_ptr;
13344
13345 if (*abbrev_table_ptr != NULL)
13346 abbrev_table_free (*abbrev_table_ptr);
13347 *abbrev_table_ptr = NULL;
13348}
13349
433df2d4
DE
13350/* Read the abbrev table for CU from ABBREV_SECTION. */
13351
13352static void
13353dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13354 struct dwarf2_section_info *abbrev_section)
c906108c 13355{
433df2d4
DE
13356 cu->abbrev_table =
13357 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13358}
c906108c 13359
433df2d4 13360/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13361
433df2d4
DE
13362static void
13363dwarf2_free_abbrev_table (void *ptr_to_cu)
13364{
13365 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13366
433df2d4
DE
13367 abbrev_table_free (cu->abbrev_table);
13368 /* Set this to NULL so that we SEGV if we try to read it later,
13369 and also because free_comp_unit verifies this is NULL. */
13370 cu->abbrev_table = NULL;
13371}
13372\f
72bf9492
DJ
13373/* Returns nonzero if TAG represents a type that we might generate a partial
13374 symbol for. */
13375
13376static int
13377is_type_tag_for_partial (int tag)
13378{
13379 switch (tag)
13380 {
13381#if 0
13382 /* Some types that would be reasonable to generate partial symbols for,
13383 that we don't at present. */
13384 case DW_TAG_array_type:
13385 case DW_TAG_file_type:
13386 case DW_TAG_ptr_to_member_type:
13387 case DW_TAG_set_type:
13388 case DW_TAG_string_type:
13389 case DW_TAG_subroutine_type:
13390#endif
13391 case DW_TAG_base_type:
13392 case DW_TAG_class_type:
680b30c7 13393 case DW_TAG_interface_type:
72bf9492
DJ
13394 case DW_TAG_enumeration_type:
13395 case DW_TAG_structure_type:
13396 case DW_TAG_subrange_type:
13397 case DW_TAG_typedef:
13398 case DW_TAG_union_type:
13399 return 1;
13400 default:
13401 return 0;
13402 }
13403}
13404
13405/* Load all DIEs that are interesting for partial symbols into memory. */
13406
13407static struct partial_die_info *
dee91e82
DE
13408load_partial_dies (const struct die_reader_specs *reader,
13409 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13410{
dee91e82 13411 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13412 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13413 struct partial_die_info *part_die;
13414 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13415 struct abbrev_info *abbrev;
13416 unsigned int bytes_read;
5afb4e99 13417 unsigned int load_all = 0;
72bf9492
DJ
13418 int nesting_level = 1;
13419
13420 parent_die = NULL;
13421 last_die = NULL;
13422
7adf1e79
DE
13423 gdb_assert (cu->per_cu != NULL);
13424 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13425 load_all = 1;
13426
72bf9492
DJ
13427 cu->partial_dies
13428 = htab_create_alloc_ex (cu->header.length / 12,
13429 partial_die_hash,
13430 partial_die_eq,
13431 NULL,
13432 &cu->comp_unit_obstack,
13433 hashtab_obstack_allocate,
13434 dummy_obstack_deallocate);
13435
13436 part_die = obstack_alloc (&cu->comp_unit_obstack,
13437 sizeof (struct partial_die_info));
13438
13439 while (1)
13440 {
13441 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13442
13443 /* A NULL abbrev means the end of a series of children. */
13444 if (abbrev == NULL)
13445 {
13446 if (--nesting_level == 0)
13447 {
13448 /* PART_DIE was probably the last thing allocated on the
13449 comp_unit_obstack, so we could call obstack_free
13450 here. We don't do that because the waste is small,
13451 and will be cleaned up when we're done with this
13452 compilation unit. This way, we're also more robust
13453 against other users of the comp_unit_obstack. */
13454 return first_die;
13455 }
13456 info_ptr += bytes_read;
13457 last_die = parent_die;
13458 parent_die = parent_die->die_parent;
13459 continue;
13460 }
13461
98bfdba5
PA
13462 /* Check for template arguments. We never save these; if
13463 they're seen, we just mark the parent, and go on our way. */
13464 if (parent_die != NULL
13465 && cu->language == language_cplus
13466 && (abbrev->tag == DW_TAG_template_type_param
13467 || abbrev->tag == DW_TAG_template_value_param))
13468 {
13469 parent_die->has_template_arguments = 1;
13470
13471 if (!load_all)
13472 {
13473 /* We don't need a partial DIE for the template argument. */
dee91e82 13474 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13475 continue;
13476 }
13477 }
13478
0d99eb77 13479 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13480 Skip their other children. */
13481 if (!load_all
13482 && cu->language == language_cplus
13483 && parent_die != NULL
13484 && parent_die->tag == DW_TAG_subprogram)
13485 {
dee91e82 13486 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13487 continue;
13488 }
13489
5afb4e99
DJ
13490 /* Check whether this DIE is interesting enough to save. Normally
13491 we would not be interested in members here, but there may be
13492 later variables referencing them via DW_AT_specification (for
13493 static members). */
13494 if (!load_all
13495 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13496 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13497 && abbrev->tag != DW_TAG_enumerator
13498 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13499 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13500 && abbrev->tag != DW_TAG_variable
5afb4e99 13501 && abbrev->tag != DW_TAG_namespace
f55ee35c 13502 && abbrev->tag != DW_TAG_module
95554aad
TT
13503 && abbrev->tag != DW_TAG_member
13504 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13505 {
13506 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13507 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13508 continue;
13509 }
13510
dee91e82
DE
13511 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13512 info_ptr);
72bf9492
DJ
13513
13514 /* This two-pass algorithm for processing partial symbols has a
13515 high cost in cache pressure. Thus, handle some simple cases
13516 here which cover the majority of C partial symbols. DIEs
13517 which neither have specification tags in them, nor could have
13518 specification tags elsewhere pointing at them, can simply be
13519 processed and discarded.
13520
13521 This segment is also optional; scan_partial_symbols and
13522 add_partial_symbol will handle these DIEs if we chain
13523 them in normally. When compilers which do not emit large
13524 quantities of duplicate debug information are more common,
13525 this code can probably be removed. */
13526
13527 /* Any complete simple types at the top level (pretty much all
13528 of them, for a language without namespaces), can be processed
13529 directly. */
13530 if (parent_die == NULL
13531 && part_die->has_specification == 0
13532 && part_die->is_declaration == 0
d8228535 13533 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13534 || part_die->tag == DW_TAG_base_type
13535 || part_die->tag == DW_TAG_subrange_type))
13536 {
13537 if (building_psymtab && part_die->name != NULL)
04a679b8 13538 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13539 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13540 &objfile->static_psymbols,
13541 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13542 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13543 continue;
13544 }
13545
d8228535
JK
13546 /* The exception for DW_TAG_typedef with has_children above is
13547 a workaround of GCC PR debug/47510. In the case of this complaint
13548 type_name_no_tag_or_error will error on such types later.
13549
13550 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13551 it could not find the child DIEs referenced later, this is checked
13552 above. In correct DWARF DW_TAG_typedef should have no children. */
13553
13554 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13555 complaint (&symfile_complaints,
13556 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13557 "- DIE at 0x%x [in module %s]"),
b64f50a1 13558 part_die->offset.sect_off, objfile->name);
d8228535 13559
72bf9492
DJ
13560 /* If we're at the second level, and we're an enumerator, and
13561 our parent has no specification (meaning possibly lives in a
13562 namespace elsewhere), then we can add the partial symbol now
13563 instead of queueing it. */
13564 if (part_die->tag == DW_TAG_enumerator
13565 && parent_die != NULL
13566 && parent_die->die_parent == NULL
13567 && parent_die->tag == DW_TAG_enumeration_type
13568 && parent_die->has_specification == 0)
13569 {
13570 if (part_die->name == NULL)
3e43a32a
MS
13571 complaint (&symfile_complaints,
13572 _("malformed enumerator DIE ignored"));
72bf9492 13573 else if (building_psymtab)
04a679b8 13574 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13575 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13576 (cu->language == language_cplus
13577 || cu->language == language_java)
bb5ed363
DE
13578 ? &objfile->global_psymbols
13579 : &objfile->static_psymbols,
13580 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13581
dee91e82 13582 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13583 continue;
13584 }
13585
13586 /* We'll save this DIE so link it in. */
13587 part_die->die_parent = parent_die;
13588 part_die->die_sibling = NULL;
13589 part_die->die_child = NULL;
13590
13591 if (last_die && last_die == parent_die)
13592 last_die->die_child = part_die;
13593 else if (last_die)
13594 last_die->die_sibling = part_die;
13595
13596 last_die = part_die;
13597
13598 if (first_die == NULL)
13599 first_die = part_die;
13600
13601 /* Maybe add the DIE to the hash table. Not all DIEs that we
13602 find interesting need to be in the hash table, because we
13603 also have the parent/sibling/child chains; only those that we
13604 might refer to by offset later during partial symbol reading.
13605
13606 For now this means things that might have be the target of a
13607 DW_AT_specification, DW_AT_abstract_origin, or
13608 DW_AT_extension. DW_AT_extension will refer only to
13609 namespaces; DW_AT_abstract_origin refers to functions (and
13610 many things under the function DIE, but we do not recurse
13611 into function DIEs during partial symbol reading) and
13612 possibly variables as well; DW_AT_specification refers to
13613 declarations. Declarations ought to have the DW_AT_declaration
13614 flag. It happens that GCC forgets to put it in sometimes, but
13615 only for functions, not for types.
13616
13617 Adding more things than necessary to the hash table is harmless
13618 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13619 wasted time in find_partial_die, when we reread the compilation
13620 unit with load_all_dies set. */
72bf9492 13621
5afb4e99 13622 if (load_all
72929c62 13623 || abbrev->tag == DW_TAG_constant
5afb4e99 13624 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13625 || abbrev->tag == DW_TAG_variable
13626 || abbrev->tag == DW_TAG_namespace
13627 || part_die->is_declaration)
13628 {
13629 void **slot;
13630
13631 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13632 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13633 *slot = part_die;
13634 }
13635
13636 part_die = obstack_alloc (&cu->comp_unit_obstack,
13637 sizeof (struct partial_die_info));
13638
13639 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13640 we have no reason to follow the children of structures; for other
98bfdba5
PA
13641 languages we have to, so that we can get at method physnames
13642 to infer fully qualified class names, for DW_AT_specification,
13643 and for C++ template arguments. For C++, we also look one level
13644 inside functions to find template arguments (if the name of the
13645 function does not already contain the template arguments).
bc30ff58
JB
13646
13647 For Ada, we need to scan the children of subprograms and lexical
13648 blocks as well because Ada allows the definition of nested
13649 entities that could be interesting for the debugger, such as
13650 nested subprograms for instance. */
72bf9492 13651 if (last_die->has_children
5afb4e99
DJ
13652 && (load_all
13653 || last_die->tag == DW_TAG_namespace
f55ee35c 13654 || last_die->tag == DW_TAG_module
72bf9492 13655 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13656 || (cu->language == language_cplus
13657 && last_die->tag == DW_TAG_subprogram
13658 && (last_die->name == NULL
13659 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13660 || (cu->language != language_c
13661 && (last_die->tag == DW_TAG_class_type
680b30c7 13662 || last_die->tag == DW_TAG_interface_type
72bf9492 13663 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13664 || last_die->tag == DW_TAG_union_type))
13665 || (cu->language == language_ada
13666 && (last_die->tag == DW_TAG_subprogram
13667 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13668 {
13669 nesting_level++;
13670 parent_die = last_die;
13671 continue;
13672 }
13673
13674 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13675 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13676
13677 /* Back to the top, do it again. */
13678 }
13679}
13680
c906108c
SS
13681/* Read a minimal amount of information into the minimal die structure. */
13682
fe1b8b76 13683static gdb_byte *
dee91e82
DE
13684read_partial_die (const struct die_reader_specs *reader,
13685 struct partial_die_info *part_die,
13686 struct abbrev_info *abbrev, unsigned int abbrev_len,
13687 gdb_byte *info_ptr)
c906108c 13688{
dee91e82 13689 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13690 struct objfile *objfile = cu->objfile;
dee91e82 13691 gdb_byte *buffer = reader->buffer;
fa238c03 13692 unsigned int i;
c906108c 13693 struct attribute attr;
c5aa993b 13694 int has_low_pc_attr = 0;
c906108c 13695 int has_high_pc_attr = 0;
91da1414 13696 int high_pc_relative = 0;
c906108c 13697
72bf9492 13698 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13699
b64f50a1 13700 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13701
13702 info_ptr += abbrev_len;
13703
13704 if (abbrev == NULL)
13705 return info_ptr;
13706
c906108c
SS
13707 part_die->tag = abbrev->tag;
13708 part_die->has_children = abbrev->has_children;
c906108c
SS
13709
13710 for (i = 0; i < abbrev->num_attrs; ++i)
13711 {
dee91e82 13712 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13713
13714 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13715 partial symbol table. */
c906108c
SS
13716 switch (attr.name)
13717 {
13718 case DW_AT_name:
71c25dea
TT
13719 switch (part_die->tag)
13720 {
13721 case DW_TAG_compile_unit:
95554aad 13722 case DW_TAG_partial_unit:
348e048f 13723 case DW_TAG_type_unit:
71c25dea
TT
13724 /* Compilation units have a DW_AT_name that is a filename, not
13725 a source language identifier. */
13726 case DW_TAG_enumeration_type:
13727 case DW_TAG_enumerator:
13728 /* These tags always have simple identifiers already; no need
13729 to canonicalize them. */
13730 part_die->name = DW_STRING (&attr);
13731 break;
13732 default:
13733 part_die->name
13734 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13735 &objfile->objfile_obstack);
71c25dea
TT
13736 break;
13737 }
c906108c 13738 break;
31ef98ae 13739 case DW_AT_linkage_name:
c906108c 13740 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13741 /* Note that both forms of linkage name might appear. We
13742 assume they will be the same, and we only store the last
13743 one we see. */
94af9270
KS
13744 if (cu->language == language_ada)
13745 part_die->name = DW_STRING (&attr);
abc72ce4 13746 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13747 break;
13748 case DW_AT_low_pc:
13749 has_low_pc_attr = 1;
13750 part_die->lowpc = DW_ADDR (&attr);
13751 break;
13752 case DW_AT_high_pc:
13753 has_high_pc_attr = 1;
3019eac3
DE
13754 if (attr.form == DW_FORM_addr
13755 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13756 part_die->highpc = DW_ADDR (&attr);
13757 else
13758 {
13759 high_pc_relative = 1;
13760 part_die->highpc = DW_UNSND (&attr);
13761 }
c906108c
SS
13762 break;
13763 case DW_AT_location:
0963b4bd 13764 /* Support the .debug_loc offsets. */
8e19ed76
PS
13765 if (attr_form_is_block (&attr))
13766 {
95554aad 13767 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13768 }
3690dd37 13769 else if (attr_form_is_section_offset (&attr))
8e19ed76 13770 {
4d3c2250 13771 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13772 }
13773 else
13774 {
4d3c2250
KB
13775 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13776 "partial symbol information");
8e19ed76 13777 }
c906108c 13778 break;
c906108c
SS
13779 case DW_AT_external:
13780 part_die->is_external = DW_UNSND (&attr);
13781 break;
13782 case DW_AT_declaration:
13783 part_die->is_declaration = DW_UNSND (&attr);
13784 break;
13785 case DW_AT_type:
13786 part_die->has_type = 1;
13787 break;
13788 case DW_AT_abstract_origin:
13789 case DW_AT_specification:
72bf9492
DJ
13790 case DW_AT_extension:
13791 part_die->has_specification = 1;
c764a876 13792 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13793 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13794 || cu->per_cu->is_dwz);
c906108c
SS
13795 break;
13796 case DW_AT_sibling:
13797 /* Ignore absolute siblings, they might point outside of
13798 the current compile unit. */
13799 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13800 complaint (&symfile_complaints,
13801 _("ignoring absolute DW_AT_sibling"));
c906108c 13802 else
b64f50a1 13803 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13804 break;
fa4028e9
JB
13805 case DW_AT_byte_size:
13806 part_die->has_byte_size = 1;
13807 break;
68511cec
CES
13808 case DW_AT_calling_convention:
13809 /* DWARF doesn't provide a way to identify a program's source-level
13810 entry point. DW_AT_calling_convention attributes are only meant
13811 to describe functions' calling conventions.
13812
13813 However, because it's a necessary piece of information in
13814 Fortran, and because DW_CC_program is the only piece of debugging
13815 information whose definition refers to a 'main program' at all,
13816 several compilers have begun marking Fortran main programs with
13817 DW_CC_program --- even when those functions use the standard
13818 calling conventions.
13819
13820 So until DWARF specifies a way to provide this information and
13821 compilers pick up the new representation, we'll support this
13822 practice. */
13823 if (DW_UNSND (&attr) == DW_CC_program
13824 && cu->language == language_fortran)
01f8c46d
JK
13825 {
13826 set_main_name (part_die->name);
13827
13828 /* As this DIE has a static linkage the name would be difficult
13829 to look up later. */
13830 language_of_main = language_fortran;
13831 }
68511cec 13832 break;
481860b3
GB
13833 case DW_AT_inline:
13834 if (DW_UNSND (&attr) == DW_INL_inlined
13835 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13836 part_die->may_be_inlined = 1;
13837 break;
95554aad
TT
13838
13839 case DW_AT_import:
13840 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13841 {
13842 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13843 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13844 || cu->per_cu->is_dwz);
13845 }
95554aad
TT
13846 break;
13847
c906108c
SS
13848 default:
13849 break;
13850 }
13851 }
13852
91da1414
MW
13853 if (high_pc_relative)
13854 part_die->highpc += part_die->lowpc;
13855
9373cf26
JK
13856 if (has_low_pc_attr && has_high_pc_attr)
13857 {
13858 /* When using the GNU linker, .gnu.linkonce. sections are used to
13859 eliminate duplicate copies of functions and vtables and such.
13860 The linker will arbitrarily choose one and discard the others.
13861 The AT_*_pc values for such functions refer to local labels in
13862 these sections. If the section from that file was discarded, the
13863 labels are not in the output, so the relocs get a value of 0.
13864 If this is a discarded function, mark the pc bounds as invalid,
13865 so that GDB will ignore it. */
13866 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13867 {
bb5ed363 13868 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13869
13870 complaint (&symfile_complaints,
13871 _("DW_AT_low_pc %s is zero "
13872 "for DIE at 0x%x [in module %s]"),
13873 paddress (gdbarch, part_die->lowpc),
b64f50a1 13874 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13875 }
13876 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13877 else if (part_die->lowpc >= part_die->highpc)
13878 {
bb5ed363 13879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13880
13881 complaint (&symfile_complaints,
13882 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13883 "for DIE at 0x%x [in module %s]"),
13884 paddress (gdbarch, part_die->lowpc),
13885 paddress (gdbarch, part_die->highpc),
b64f50a1 13886 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13887 }
13888 else
13889 part_die->has_pc_info = 1;
13890 }
85cbf3d3 13891
c906108c
SS
13892 return info_ptr;
13893}
13894
72bf9492
DJ
13895/* Find a cached partial DIE at OFFSET in CU. */
13896
13897static struct partial_die_info *
b64f50a1 13898find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13899{
13900 struct partial_die_info *lookup_die = NULL;
13901 struct partial_die_info part_die;
13902
13903 part_die.offset = offset;
b64f50a1
JK
13904 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13905 offset.sect_off);
72bf9492 13906
72bf9492
DJ
13907 return lookup_die;
13908}
13909
348e048f
DE
13910/* Find a partial DIE at OFFSET, which may or may not be in CU,
13911 except in the case of .debug_types DIEs which do not reference
13912 outside their CU (they do however referencing other types via
55f1336d 13913 DW_FORM_ref_sig8). */
72bf9492
DJ
13914
13915static struct partial_die_info *
36586728 13916find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13917{
bb5ed363 13918 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13919 struct dwarf2_per_cu_data *per_cu = NULL;
13920 struct partial_die_info *pd = NULL;
72bf9492 13921
36586728
TT
13922 if (offset_in_dwz == cu->per_cu->is_dwz
13923 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13924 {
13925 pd = find_partial_die_in_comp_unit (offset, cu);
13926 if (pd != NULL)
13927 return pd;
0d99eb77
DE
13928 /* We missed recording what we needed.
13929 Load all dies and try again. */
13930 per_cu = cu->per_cu;
5afb4e99 13931 }
0d99eb77
DE
13932 else
13933 {
13934 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13935 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13936 {
13937 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13938 " external reference to offset 0x%lx [in module %s].\n"),
13939 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13940 bfd_get_filename (objfile->obfd));
13941 }
36586728
TT
13942 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13943 objfile);
72bf9492 13944
0d99eb77
DE
13945 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13946 load_partial_comp_unit (per_cu);
ae038cb0 13947
0d99eb77
DE
13948 per_cu->cu->last_used = 0;
13949 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13950 }
5afb4e99 13951
dee91e82
DE
13952 /* If we didn't find it, and not all dies have been loaded,
13953 load them all and try again. */
13954
5afb4e99
DJ
13955 if (pd == NULL && per_cu->load_all_dies == 0)
13956 {
5afb4e99 13957 per_cu->load_all_dies = 1;
fd820528
DE
13958
13959 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13960 THIS_CU->cu may already be in use. So we can't just free it and
13961 replace its DIEs with the ones we read in. Instead, we leave those
13962 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13963 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13964 set. */
dee91e82 13965 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13966
13967 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13968 }
13969
13970 if (pd == NULL)
13971 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13972 _("could not find partial DIE 0x%x "
13973 "in cache [from module %s]\n"),
b64f50a1 13974 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13975 return pd;
72bf9492
DJ
13976}
13977
abc72ce4
DE
13978/* See if we can figure out if the class lives in a namespace. We do
13979 this by looking for a member function; its demangled name will
13980 contain namespace info, if there is any. */
13981
13982static void
13983guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13984 struct dwarf2_cu *cu)
13985{
13986 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13987 what template types look like, because the demangler
13988 frequently doesn't give the same name as the debug info. We
13989 could fix this by only using the demangled name to get the
13990 prefix (but see comment in read_structure_type). */
13991
13992 struct partial_die_info *real_pdi;
13993 struct partial_die_info *child_pdi;
13994
13995 /* If this DIE (this DIE's specification, if any) has a parent, then
13996 we should not do this. We'll prepend the parent's fully qualified
13997 name when we create the partial symbol. */
13998
13999 real_pdi = struct_pdi;
14000 while (real_pdi->has_specification)
36586728
TT
14001 real_pdi = find_partial_die (real_pdi->spec_offset,
14002 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14003
14004 if (real_pdi->die_parent != NULL)
14005 return;
14006
14007 for (child_pdi = struct_pdi->die_child;
14008 child_pdi != NULL;
14009 child_pdi = child_pdi->die_sibling)
14010 {
14011 if (child_pdi->tag == DW_TAG_subprogram
14012 && child_pdi->linkage_name != NULL)
14013 {
14014 char *actual_class_name
14015 = language_class_name_from_physname (cu->language_defn,
14016 child_pdi->linkage_name);
14017 if (actual_class_name != NULL)
14018 {
14019 struct_pdi->name
10f0c4bb
TT
14020 = obstack_copy0 (&cu->objfile->objfile_obstack,
14021 actual_class_name,
14022 strlen (actual_class_name));
abc72ce4
DE
14023 xfree (actual_class_name);
14024 }
14025 break;
14026 }
14027 }
14028}
14029
72bf9492
DJ
14030/* Adjust PART_DIE before generating a symbol for it. This function
14031 may set the is_external flag or change the DIE's name. */
14032
14033static void
14034fixup_partial_die (struct partial_die_info *part_die,
14035 struct dwarf2_cu *cu)
14036{
abc72ce4
DE
14037 /* Once we've fixed up a die, there's no point in doing so again.
14038 This also avoids a memory leak if we were to call
14039 guess_partial_die_structure_name multiple times. */
14040 if (part_die->fixup_called)
14041 return;
14042
72bf9492
DJ
14043 /* If we found a reference attribute and the DIE has no name, try
14044 to find a name in the referred to DIE. */
14045
14046 if (part_die->name == NULL && part_die->has_specification)
14047 {
14048 struct partial_die_info *spec_die;
72bf9492 14049
36586728
TT
14050 spec_die = find_partial_die (part_die->spec_offset,
14051 part_die->spec_is_dwz, cu);
72bf9492 14052
10b3939b 14053 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14054
14055 if (spec_die->name)
14056 {
14057 part_die->name = spec_die->name;
14058
14059 /* Copy DW_AT_external attribute if it is set. */
14060 if (spec_die->is_external)
14061 part_die->is_external = spec_die->is_external;
14062 }
14063 }
14064
14065 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14066
14067 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14068 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14069
abc72ce4
DE
14070 /* If there is no parent die to provide a namespace, and there are
14071 children, see if we can determine the namespace from their linkage
122d1940 14072 name. */
abc72ce4 14073 if (cu->language == language_cplus
8b70b953 14074 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14075 && part_die->die_parent == NULL
14076 && part_die->has_children
14077 && (part_die->tag == DW_TAG_class_type
14078 || part_die->tag == DW_TAG_structure_type
14079 || part_die->tag == DW_TAG_union_type))
14080 guess_partial_die_structure_name (part_die, cu);
14081
53832f31
TT
14082 /* GCC might emit a nameless struct or union that has a linkage
14083 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14084 if (part_die->name == NULL
96408a79
SA
14085 && (part_die->tag == DW_TAG_class_type
14086 || part_die->tag == DW_TAG_interface_type
14087 || part_die->tag == DW_TAG_structure_type
14088 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14089 && part_die->linkage_name != NULL)
14090 {
14091 char *demangled;
14092
14093 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14094 if (demangled)
14095 {
96408a79
SA
14096 const char *base;
14097
14098 /* Strip any leading namespaces/classes, keep only the base name.
14099 DW_AT_name for named DIEs does not contain the prefixes. */
14100 base = strrchr (demangled, ':');
14101 if (base && base > demangled && base[-1] == ':')
14102 base++;
14103 else
14104 base = demangled;
14105
10f0c4bb
TT
14106 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14107 base, strlen (base));
53832f31
TT
14108 xfree (demangled);
14109 }
14110 }
14111
abc72ce4 14112 part_die->fixup_called = 1;
72bf9492
DJ
14113}
14114
a8329558 14115/* Read an attribute value described by an attribute form. */
c906108c 14116
fe1b8b76 14117static gdb_byte *
dee91e82
DE
14118read_attribute_value (const struct die_reader_specs *reader,
14119 struct attribute *attr, unsigned form,
14120 gdb_byte *info_ptr)
c906108c 14121{
dee91e82
DE
14122 struct dwarf2_cu *cu = reader->cu;
14123 bfd *abfd = reader->abfd;
e7c27a73 14124 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14125 unsigned int bytes_read;
14126 struct dwarf_block *blk;
14127
a8329558
KW
14128 attr->form = form;
14129 switch (form)
c906108c 14130 {
c906108c 14131 case DW_FORM_ref_addr:
ae411497 14132 if (cu->header.version == 2)
4568ecf9 14133 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14134 else
4568ecf9
DE
14135 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14136 &cu->header, &bytes_read);
ae411497
TT
14137 info_ptr += bytes_read;
14138 break;
36586728
TT
14139 case DW_FORM_GNU_ref_alt:
14140 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14141 info_ptr += bytes_read;
14142 break;
ae411497 14143 case DW_FORM_addr:
e7c27a73 14144 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14145 info_ptr += bytes_read;
c906108c
SS
14146 break;
14147 case DW_FORM_block2:
7b5a2f43 14148 blk = dwarf_alloc_block (cu);
c906108c
SS
14149 blk->size = read_2_bytes (abfd, info_ptr);
14150 info_ptr += 2;
14151 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14152 info_ptr += blk->size;
14153 DW_BLOCK (attr) = blk;
14154 break;
14155 case DW_FORM_block4:
7b5a2f43 14156 blk = dwarf_alloc_block (cu);
c906108c
SS
14157 blk->size = read_4_bytes (abfd, info_ptr);
14158 info_ptr += 4;
14159 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14160 info_ptr += blk->size;
14161 DW_BLOCK (attr) = blk;
14162 break;
14163 case DW_FORM_data2:
14164 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14165 info_ptr += 2;
14166 break;
14167 case DW_FORM_data4:
14168 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14169 info_ptr += 4;
14170 break;
14171 case DW_FORM_data8:
14172 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14173 info_ptr += 8;
14174 break;
2dc7f7b3
TT
14175 case DW_FORM_sec_offset:
14176 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14177 info_ptr += bytes_read;
14178 break;
c906108c 14179 case DW_FORM_string:
9b1c24c8 14180 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14181 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14182 info_ptr += bytes_read;
14183 break;
4bdf3d34 14184 case DW_FORM_strp:
36586728
TT
14185 if (!cu->per_cu->is_dwz)
14186 {
14187 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14188 &bytes_read);
14189 DW_STRING_IS_CANONICAL (attr) = 0;
14190 info_ptr += bytes_read;
14191 break;
14192 }
14193 /* FALLTHROUGH */
14194 case DW_FORM_GNU_strp_alt:
14195 {
14196 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14197 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14198 &bytes_read);
14199
14200 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14201 DW_STRING_IS_CANONICAL (attr) = 0;
14202 info_ptr += bytes_read;
14203 }
4bdf3d34 14204 break;
2dc7f7b3 14205 case DW_FORM_exprloc:
c906108c 14206 case DW_FORM_block:
7b5a2f43 14207 blk = dwarf_alloc_block (cu);
c906108c
SS
14208 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14209 info_ptr += bytes_read;
14210 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14211 info_ptr += blk->size;
14212 DW_BLOCK (attr) = blk;
14213 break;
14214 case DW_FORM_block1:
7b5a2f43 14215 blk = dwarf_alloc_block (cu);
c906108c
SS
14216 blk->size = read_1_byte (abfd, info_ptr);
14217 info_ptr += 1;
14218 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14219 info_ptr += blk->size;
14220 DW_BLOCK (attr) = blk;
14221 break;
14222 case DW_FORM_data1:
14223 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14224 info_ptr += 1;
14225 break;
14226 case DW_FORM_flag:
14227 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14228 info_ptr += 1;
14229 break;
2dc7f7b3
TT
14230 case DW_FORM_flag_present:
14231 DW_UNSND (attr) = 1;
14232 break;
c906108c
SS
14233 case DW_FORM_sdata:
14234 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14235 info_ptr += bytes_read;
14236 break;
14237 case DW_FORM_udata:
14238 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14239 info_ptr += bytes_read;
14240 break;
14241 case DW_FORM_ref1:
4568ecf9
DE
14242 DW_UNSND (attr) = (cu->header.offset.sect_off
14243 + read_1_byte (abfd, info_ptr));
c906108c
SS
14244 info_ptr += 1;
14245 break;
14246 case DW_FORM_ref2:
4568ecf9
DE
14247 DW_UNSND (attr) = (cu->header.offset.sect_off
14248 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14249 info_ptr += 2;
14250 break;
14251 case DW_FORM_ref4:
4568ecf9
DE
14252 DW_UNSND (attr) = (cu->header.offset.sect_off
14253 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14254 info_ptr += 4;
14255 break;
613e1657 14256 case DW_FORM_ref8:
4568ecf9
DE
14257 DW_UNSND (attr) = (cu->header.offset.sect_off
14258 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14259 info_ptr += 8;
14260 break;
55f1336d 14261 case DW_FORM_ref_sig8:
348e048f
DE
14262 /* Convert the signature to something we can record in DW_UNSND
14263 for later lookup.
14264 NOTE: This is NULL if the type wasn't found. */
14265 DW_SIGNATURED_TYPE (attr) =
e319fa28 14266 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14267 info_ptr += 8;
14268 break;
c906108c 14269 case DW_FORM_ref_udata:
4568ecf9
DE
14270 DW_UNSND (attr) = (cu->header.offset.sect_off
14271 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14272 info_ptr += bytes_read;
14273 break;
c906108c 14274 case DW_FORM_indirect:
a8329558
KW
14275 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14276 info_ptr += bytes_read;
dee91e82 14277 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14278 break;
3019eac3
DE
14279 case DW_FORM_GNU_addr_index:
14280 if (reader->dwo_file == NULL)
14281 {
14282 /* For now flag a hard error.
14283 Later we can turn this into a complaint. */
14284 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14285 dwarf_form_name (form),
14286 bfd_get_filename (abfd));
14287 }
14288 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14289 info_ptr += bytes_read;
14290 break;
14291 case DW_FORM_GNU_str_index:
14292 if (reader->dwo_file == NULL)
14293 {
14294 /* For now flag a hard error.
14295 Later we can turn this into a complaint if warranted. */
14296 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14297 dwarf_form_name (form),
14298 bfd_get_filename (abfd));
14299 }
14300 {
14301 ULONGEST str_index =
14302 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14303
14304 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14305 DW_STRING_IS_CANONICAL (attr) = 0;
14306 info_ptr += bytes_read;
14307 }
14308 break;
c906108c 14309 default:
8a3fe4f8 14310 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14311 dwarf_form_name (form),
14312 bfd_get_filename (abfd));
c906108c 14313 }
28e94949 14314
36586728
TT
14315 /* Super hack. */
14316 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14317 attr->form = DW_FORM_GNU_ref_alt;
14318
28e94949
JB
14319 /* We have seen instances where the compiler tried to emit a byte
14320 size attribute of -1 which ended up being encoded as an unsigned
14321 0xffffffff. Although 0xffffffff is technically a valid size value,
14322 an object of this size seems pretty unlikely so we can relatively
14323 safely treat these cases as if the size attribute was invalid and
14324 treat them as zero by default. */
14325 if (attr->name == DW_AT_byte_size
14326 && form == DW_FORM_data4
14327 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14328 {
14329 complaint
14330 (&symfile_complaints,
43bbcdc2
PH
14331 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14332 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14333 DW_UNSND (attr) = 0;
14334 }
28e94949 14335
c906108c
SS
14336 return info_ptr;
14337}
14338
a8329558
KW
14339/* Read an attribute described by an abbreviated attribute. */
14340
fe1b8b76 14341static gdb_byte *
dee91e82
DE
14342read_attribute (const struct die_reader_specs *reader,
14343 struct attribute *attr, struct attr_abbrev *abbrev,
14344 gdb_byte *info_ptr)
a8329558
KW
14345{
14346 attr->name = abbrev->name;
dee91e82 14347 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14348}
14349
0963b4bd 14350/* Read dwarf information from a buffer. */
c906108c
SS
14351
14352static unsigned int
a1855c1d 14353read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14354{
fe1b8b76 14355 return bfd_get_8 (abfd, buf);
c906108c
SS
14356}
14357
14358static int
a1855c1d 14359read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14360{
fe1b8b76 14361 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14362}
14363
14364static unsigned int
a1855c1d 14365read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14366{
fe1b8b76 14367 return bfd_get_16 (abfd, buf);
c906108c
SS
14368}
14369
21ae7a4d 14370static int
a1855c1d 14371read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14372{
14373 return bfd_get_signed_16 (abfd, buf);
14374}
14375
c906108c 14376static unsigned int
a1855c1d 14377read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14378{
fe1b8b76 14379 return bfd_get_32 (abfd, buf);
c906108c
SS
14380}
14381
21ae7a4d 14382static int
a1855c1d 14383read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14384{
14385 return bfd_get_signed_32 (abfd, buf);
14386}
14387
93311388 14388static ULONGEST
a1855c1d 14389read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14390{
fe1b8b76 14391 return bfd_get_64 (abfd, buf);
c906108c
SS
14392}
14393
14394static CORE_ADDR
fe1b8b76 14395read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14396 unsigned int *bytes_read)
c906108c 14397{
e7c27a73 14398 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14399 CORE_ADDR retval = 0;
14400
107d2387 14401 if (cu_header->signed_addr_p)
c906108c 14402 {
107d2387
AC
14403 switch (cu_header->addr_size)
14404 {
14405 case 2:
fe1b8b76 14406 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14407 break;
14408 case 4:
fe1b8b76 14409 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14410 break;
14411 case 8:
fe1b8b76 14412 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14413 break;
14414 default:
8e65ff28 14415 internal_error (__FILE__, __LINE__,
e2e0b3e5 14416 _("read_address: bad switch, signed [in module %s]"),
659b0389 14417 bfd_get_filename (abfd));
107d2387
AC
14418 }
14419 }
14420 else
14421 {
14422 switch (cu_header->addr_size)
14423 {
14424 case 2:
fe1b8b76 14425 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14426 break;
14427 case 4:
fe1b8b76 14428 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14429 break;
14430 case 8:
fe1b8b76 14431 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14432 break;
14433 default:
8e65ff28 14434 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14435 _("read_address: bad switch, "
14436 "unsigned [in module %s]"),
659b0389 14437 bfd_get_filename (abfd));
107d2387 14438 }
c906108c 14439 }
64367e0a 14440
107d2387
AC
14441 *bytes_read = cu_header->addr_size;
14442 return retval;
c906108c
SS
14443}
14444
f7ef9339 14445/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14446 specification allows the initial length to take up either 4 bytes
14447 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14448 bytes describe the length and all offsets will be 8 bytes in length
14449 instead of 4.
14450
f7ef9339
KB
14451 An older, non-standard 64-bit format is also handled by this
14452 function. The older format in question stores the initial length
14453 as an 8-byte quantity without an escape value. Lengths greater
14454 than 2^32 aren't very common which means that the initial 4 bytes
14455 is almost always zero. Since a length value of zero doesn't make
14456 sense for the 32-bit format, this initial zero can be considered to
14457 be an escape value which indicates the presence of the older 64-bit
14458 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14459 greater than 4GB. If it becomes necessary to handle lengths
14460 somewhat larger than 4GB, we could allow other small values (such
14461 as the non-sensical values of 1, 2, and 3) to also be used as
14462 escape values indicating the presence of the old format.
f7ef9339 14463
917c78fc
MK
14464 The value returned via bytes_read should be used to increment the
14465 relevant pointer after calling read_initial_length().
c764a876 14466
613e1657
KB
14467 [ Note: read_initial_length() and read_offset() are based on the
14468 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14469 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14470 from:
14471
f7ef9339 14472 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14473
613e1657
KB
14474 This document is only a draft and is subject to change. (So beware.)
14475
f7ef9339 14476 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14477 determined empirically by examining 64-bit ELF files produced by
14478 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14479
14480 - Kevin, July 16, 2002
613e1657
KB
14481 ] */
14482
14483static LONGEST
c764a876 14484read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14485{
fe1b8b76 14486 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14487
dd373385 14488 if (length == 0xffffffff)
613e1657 14489 {
fe1b8b76 14490 length = bfd_get_64 (abfd, buf + 4);
613e1657 14491 *bytes_read = 12;
613e1657 14492 }
dd373385 14493 else if (length == 0)
f7ef9339 14494 {
dd373385 14495 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14496 length = bfd_get_64 (abfd, buf);
f7ef9339 14497 *bytes_read = 8;
f7ef9339 14498 }
613e1657
KB
14499 else
14500 {
14501 *bytes_read = 4;
613e1657
KB
14502 }
14503
c764a876
DE
14504 return length;
14505}
dd373385 14506
c764a876
DE
14507/* Cover function for read_initial_length.
14508 Returns the length of the object at BUF, and stores the size of the
14509 initial length in *BYTES_READ and stores the size that offsets will be in
14510 *OFFSET_SIZE.
14511 If the initial length size is not equivalent to that specified in
14512 CU_HEADER then issue a complaint.
14513 This is useful when reading non-comp-unit headers. */
dd373385 14514
c764a876
DE
14515static LONGEST
14516read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14517 const struct comp_unit_head *cu_header,
14518 unsigned int *bytes_read,
14519 unsigned int *offset_size)
14520{
14521 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14522
14523 gdb_assert (cu_header->initial_length_size == 4
14524 || cu_header->initial_length_size == 8
14525 || cu_header->initial_length_size == 12);
14526
14527 if (cu_header->initial_length_size != *bytes_read)
14528 complaint (&symfile_complaints,
14529 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14530
c764a876 14531 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14532 return length;
613e1657
KB
14533}
14534
14535/* Read an offset from the data stream. The size of the offset is
917c78fc 14536 given by cu_header->offset_size. */
613e1657
KB
14537
14538static LONGEST
fe1b8b76 14539read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14540 unsigned int *bytes_read)
c764a876
DE
14541{
14542 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14543
c764a876
DE
14544 *bytes_read = cu_header->offset_size;
14545 return offset;
14546}
14547
14548/* Read an offset from the data stream. */
14549
14550static LONGEST
14551read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14552{
14553 LONGEST retval = 0;
14554
c764a876 14555 switch (offset_size)
613e1657
KB
14556 {
14557 case 4:
fe1b8b76 14558 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14559 break;
14560 case 8:
fe1b8b76 14561 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14562 break;
14563 default:
8e65ff28 14564 internal_error (__FILE__, __LINE__,
c764a876 14565 _("read_offset_1: bad switch [in module %s]"),
659b0389 14566 bfd_get_filename (abfd));
613e1657
KB
14567 }
14568
917c78fc 14569 return retval;
613e1657
KB
14570}
14571
fe1b8b76
JB
14572static gdb_byte *
14573read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14574{
14575 /* If the size of a host char is 8 bits, we can return a pointer
14576 to the buffer, otherwise we have to copy the data to a buffer
14577 allocated on the temporary obstack. */
4bdf3d34 14578 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14579 return buf;
c906108c
SS
14580}
14581
14582static char *
9b1c24c8 14583read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14584{
14585 /* If the size of a host char is 8 bits, we can return a pointer
14586 to the string, otherwise we have to copy the string to a buffer
14587 allocated on the temporary obstack. */
4bdf3d34 14588 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14589 if (*buf == '\0')
14590 {
14591 *bytes_read_ptr = 1;
14592 return NULL;
14593 }
fe1b8b76
JB
14594 *bytes_read_ptr = strlen ((char *) buf) + 1;
14595 return (char *) buf;
4bdf3d34
JJ
14596}
14597
14598static char *
cf2c3c16 14599read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14600{
be391dca 14601 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14602 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14603 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14604 bfd_get_filename (abfd));
dce234bc 14605 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14606 error (_("DW_FORM_strp pointing outside of "
14607 ".debug_str section [in module %s]"),
14608 bfd_get_filename (abfd));
4bdf3d34 14609 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14610 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14611 return NULL;
dce234bc 14612 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14613}
14614
36586728
TT
14615/* Read a string at offset STR_OFFSET in the .debug_str section from
14616 the .dwz file DWZ. Throw an error if the offset is too large. If
14617 the string consists of a single NUL byte, return NULL; otherwise
14618 return a pointer to the string. */
14619
14620static char *
14621read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14622{
14623 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14624
14625 if (dwz->str.buffer == NULL)
14626 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14627 "section [in module %s]"),
14628 bfd_get_filename (dwz->dwz_bfd));
14629 if (str_offset >= dwz->str.size)
14630 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14631 ".debug_str section [in module %s]"),
14632 bfd_get_filename (dwz->dwz_bfd));
14633 gdb_assert (HOST_CHAR_BIT == 8);
14634 if (dwz->str.buffer[str_offset] == '\0')
14635 return NULL;
14636 return (char *) (dwz->str.buffer + str_offset);
14637}
14638
cf2c3c16
TT
14639static char *
14640read_indirect_string (bfd *abfd, gdb_byte *buf,
14641 const struct comp_unit_head *cu_header,
14642 unsigned int *bytes_read_ptr)
14643{
14644 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14645
14646 return read_indirect_string_at_offset (abfd, str_offset);
14647}
14648
12df843f 14649static ULONGEST
fe1b8b76 14650read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14651{
12df843f 14652 ULONGEST result;
ce5d95e1 14653 unsigned int num_read;
c906108c
SS
14654 int i, shift;
14655 unsigned char byte;
14656
14657 result = 0;
14658 shift = 0;
14659 num_read = 0;
14660 i = 0;
14661 while (1)
14662 {
fe1b8b76 14663 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14664 buf++;
14665 num_read++;
12df843f 14666 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14667 if ((byte & 128) == 0)
14668 {
14669 break;
14670 }
14671 shift += 7;
14672 }
14673 *bytes_read_ptr = num_read;
14674 return result;
14675}
14676
12df843f 14677static LONGEST
fe1b8b76 14678read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14679{
12df843f 14680 LONGEST result;
77e0b926 14681 int i, shift, num_read;
c906108c
SS
14682 unsigned char byte;
14683
14684 result = 0;
14685 shift = 0;
c906108c
SS
14686 num_read = 0;
14687 i = 0;
14688 while (1)
14689 {
fe1b8b76 14690 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14691 buf++;
14692 num_read++;
12df843f 14693 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14694 shift += 7;
14695 if ((byte & 128) == 0)
14696 {
14697 break;
14698 }
14699 }
77e0b926 14700 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14701 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14702 *bytes_read_ptr = num_read;
14703 return result;
14704}
14705
3019eac3
DE
14706/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14707 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14708 ADDR_SIZE is the size of addresses from the CU header. */
14709
14710static CORE_ADDR
14711read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14712{
14713 struct objfile *objfile = dwarf2_per_objfile->objfile;
14714 bfd *abfd = objfile->obfd;
14715 const gdb_byte *info_ptr;
14716
14717 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14718 if (dwarf2_per_objfile->addr.buffer == NULL)
14719 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14720 objfile->name);
14721 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14722 error (_("DW_FORM_addr_index pointing outside of "
14723 ".debug_addr section [in module %s]"),
14724 objfile->name);
14725 info_ptr = (dwarf2_per_objfile->addr.buffer
14726 + addr_base + addr_index * addr_size);
14727 if (addr_size == 4)
14728 return bfd_get_32 (abfd, info_ptr);
14729 else
14730 return bfd_get_64 (abfd, info_ptr);
14731}
14732
14733/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14734
14735static CORE_ADDR
14736read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14737{
14738 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14739}
14740
14741/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14742
14743static CORE_ADDR
14744read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14745 unsigned int *bytes_read)
14746{
14747 bfd *abfd = cu->objfile->obfd;
14748 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14749
14750 return read_addr_index (cu, addr_index);
14751}
14752
14753/* Data structure to pass results from dwarf2_read_addr_index_reader
14754 back to dwarf2_read_addr_index. */
14755
14756struct dwarf2_read_addr_index_data
14757{
14758 ULONGEST addr_base;
14759 int addr_size;
14760};
14761
14762/* die_reader_func for dwarf2_read_addr_index. */
14763
14764static void
14765dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14766 gdb_byte *info_ptr,
14767 struct die_info *comp_unit_die,
14768 int has_children,
14769 void *data)
14770{
14771 struct dwarf2_cu *cu = reader->cu;
14772 struct dwarf2_read_addr_index_data *aidata =
14773 (struct dwarf2_read_addr_index_data *) data;
14774
14775 aidata->addr_base = cu->addr_base;
14776 aidata->addr_size = cu->header.addr_size;
14777}
14778
14779/* Given an index in .debug_addr, fetch the value.
14780 NOTE: This can be called during dwarf expression evaluation,
14781 long after the debug information has been read, and thus per_cu->cu
14782 may no longer exist. */
14783
14784CORE_ADDR
14785dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14786 unsigned int addr_index)
14787{
14788 struct objfile *objfile = per_cu->objfile;
14789 struct dwarf2_cu *cu = per_cu->cu;
14790 ULONGEST addr_base;
14791 int addr_size;
14792
14793 /* This is intended to be called from outside this file. */
14794 dw2_setup (objfile);
14795
14796 /* We need addr_base and addr_size.
14797 If we don't have PER_CU->cu, we have to get it.
14798 Nasty, but the alternative is storing the needed info in PER_CU,
14799 which at this point doesn't seem justified: it's not clear how frequently
14800 it would get used and it would increase the size of every PER_CU.
14801 Entry points like dwarf2_per_cu_addr_size do a similar thing
14802 so we're not in uncharted territory here.
14803 Alas we need to be a bit more complicated as addr_base is contained
14804 in the DIE.
14805
14806 We don't need to read the entire CU(/TU).
14807 We just need the header and top level die.
a1b64ce1 14808
3019eac3 14809 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14810 For now we skip this optimization. */
3019eac3
DE
14811
14812 if (cu != NULL)
14813 {
14814 addr_base = cu->addr_base;
14815 addr_size = cu->header.addr_size;
14816 }
14817 else
14818 {
14819 struct dwarf2_read_addr_index_data aidata;
14820
a1b64ce1
DE
14821 /* Note: We can't use init_cutu_and_read_dies_simple here,
14822 we need addr_base. */
14823 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14824 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14825 addr_base = aidata.addr_base;
14826 addr_size = aidata.addr_size;
14827 }
14828
14829 return read_addr_index_1 (addr_index, addr_base, addr_size);
14830}
14831
14832/* Given a DW_AT_str_index, fetch the string. */
14833
14834static char *
14835read_str_index (const struct die_reader_specs *reader,
14836 struct dwarf2_cu *cu, ULONGEST str_index)
14837{
14838 struct objfile *objfile = dwarf2_per_objfile->objfile;
14839 const char *dwo_name = objfile->name;
14840 bfd *abfd = objfile->obfd;
14841 struct dwo_sections *sections = &reader->dwo_file->sections;
14842 gdb_byte *info_ptr;
14843 ULONGEST str_offset;
14844
14845 dwarf2_read_section (objfile, &sections->str);
14846 dwarf2_read_section (objfile, &sections->str_offsets);
14847 if (sections->str.buffer == NULL)
14848 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14849 " in CU at offset 0x%lx [in module %s]"),
14850 (long) cu->header.offset.sect_off, dwo_name);
14851 if (sections->str_offsets.buffer == NULL)
14852 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14853 " in CU at offset 0x%lx [in module %s]"),
14854 (long) cu->header.offset.sect_off, dwo_name);
14855 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14856 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14857 " section in CU at offset 0x%lx [in module %s]"),
14858 (long) cu->header.offset.sect_off, dwo_name);
14859 info_ptr = (sections->str_offsets.buffer
14860 + str_index * cu->header.offset_size);
14861 if (cu->header.offset_size == 4)
14862 str_offset = bfd_get_32 (abfd, info_ptr);
14863 else
14864 str_offset = bfd_get_64 (abfd, info_ptr);
14865 if (str_offset >= sections->str.size)
14866 error (_("Offset from DW_FORM_str_index pointing outside of"
14867 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14868 (long) cu->header.offset.sect_off, dwo_name);
14869 return (char *) (sections->str.buffer + str_offset);
14870}
14871
3019eac3
DE
14872/* Return the length of an LEB128 number in BUF. */
14873
14874static int
14875leb128_size (const gdb_byte *buf)
14876{
14877 const gdb_byte *begin = buf;
14878 gdb_byte byte;
14879
14880 while (1)
14881 {
14882 byte = *buf++;
14883 if ((byte & 128) == 0)
14884 return buf - begin;
14885 }
14886}
14887
c906108c 14888static void
e142c38c 14889set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14890{
14891 switch (lang)
14892 {
14893 case DW_LANG_C89:
76bee0cc 14894 case DW_LANG_C99:
c906108c 14895 case DW_LANG_C:
e142c38c 14896 cu->language = language_c;
c906108c
SS
14897 break;
14898 case DW_LANG_C_plus_plus:
e142c38c 14899 cu->language = language_cplus;
c906108c 14900 break;
6aecb9c2
JB
14901 case DW_LANG_D:
14902 cu->language = language_d;
14903 break;
c906108c
SS
14904 case DW_LANG_Fortran77:
14905 case DW_LANG_Fortran90:
b21b22e0 14906 case DW_LANG_Fortran95:
e142c38c 14907 cu->language = language_fortran;
c906108c 14908 break;
a766d390
DE
14909 case DW_LANG_Go:
14910 cu->language = language_go;
14911 break;
c906108c 14912 case DW_LANG_Mips_Assembler:
e142c38c 14913 cu->language = language_asm;
c906108c 14914 break;
bebd888e 14915 case DW_LANG_Java:
e142c38c 14916 cu->language = language_java;
bebd888e 14917 break;
c906108c 14918 case DW_LANG_Ada83:
8aaf0b47 14919 case DW_LANG_Ada95:
bc5f45f8
JB
14920 cu->language = language_ada;
14921 break;
72019c9c
GM
14922 case DW_LANG_Modula2:
14923 cu->language = language_m2;
14924 break;
fe8e67fd
PM
14925 case DW_LANG_Pascal83:
14926 cu->language = language_pascal;
14927 break;
22566fbd
DJ
14928 case DW_LANG_ObjC:
14929 cu->language = language_objc;
14930 break;
c906108c
SS
14931 case DW_LANG_Cobol74:
14932 case DW_LANG_Cobol85:
c906108c 14933 default:
e142c38c 14934 cu->language = language_minimal;
c906108c
SS
14935 break;
14936 }
e142c38c 14937 cu->language_defn = language_def (cu->language);
c906108c
SS
14938}
14939
14940/* Return the named attribute or NULL if not there. */
14941
14942static struct attribute *
e142c38c 14943dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14944{
a48e046c 14945 for (;;)
c906108c 14946 {
a48e046c
TT
14947 unsigned int i;
14948 struct attribute *spec = NULL;
14949
14950 for (i = 0; i < die->num_attrs; ++i)
14951 {
14952 if (die->attrs[i].name == name)
14953 return &die->attrs[i];
14954 if (die->attrs[i].name == DW_AT_specification
14955 || die->attrs[i].name == DW_AT_abstract_origin)
14956 spec = &die->attrs[i];
14957 }
14958
14959 if (!spec)
14960 break;
c906108c 14961
f2f0e013 14962 die = follow_die_ref (die, spec, &cu);
f2f0e013 14963 }
c5aa993b 14964
c906108c
SS
14965 return NULL;
14966}
14967
348e048f
DE
14968/* Return the named attribute or NULL if not there,
14969 but do not follow DW_AT_specification, etc.
14970 This is for use in contexts where we're reading .debug_types dies.
14971 Following DW_AT_specification, DW_AT_abstract_origin will take us
14972 back up the chain, and we want to go down. */
14973
14974static struct attribute *
45e58e77 14975dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14976{
14977 unsigned int i;
14978
14979 for (i = 0; i < die->num_attrs; ++i)
14980 if (die->attrs[i].name == name)
14981 return &die->attrs[i];
14982
14983 return NULL;
14984}
14985
05cf31d1
JB
14986/* Return non-zero iff the attribute NAME is defined for the given DIE,
14987 and holds a non-zero value. This function should only be used for
2dc7f7b3 14988 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14989
14990static int
14991dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14992{
14993 struct attribute *attr = dwarf2_attr (die, name, cu);
14994
14995 return (attr && DW_UNSND (attr));
14996}
14997
3ca72b44 14998static int
e142c38c 14999die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15000{
05cf31d1
JB
15001 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15002 which value is non-zero. However, we have to be careful with
15003 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15004 (via dwarf2_flag_true_p) follows this attribute. So we may
15005 end up accidently finding a declaration attribute that belongs
15006 to a different DIE referenced by the specification attribute,
15007 even though the given DIE does not have a declaration attribute. */
15008 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15009 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15010}
15011
63d06c5c 15012/* Return the die giving the specification for DIE, if there is
f2f0e013 15013 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15014 containing the return value on output. If there is no
15015 specification, but there is an abstract origin, that is
15016 returned. */
63d06c5c
DC
15017
15018static struct die_info *
f2f0e013 15019die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15020{
f2f0e013
DJ
15021 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15022 *spec_cu);
63d06c5c 15023
edb3359d
DJ
15024 if (spec_attr == NULL)
15025 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15026
63d06c5c
DC
15027 if (spec_attr == NULL)
15028 return NULL;
15029 else
f2f0e013 15030 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15031}
c906108c 15032
debd256d 15033/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15034 refers to.
15035 NOTE: This is also used as a "cleanup" function. */
15036
debd256d
JB
15037static void
15038free_line_header (struct line_header *lh)
15039{
15040 if (lh->standard_opcode_lengths)
a8bc7b56 15041 xfree (lh->standard_opcode_lengths);
debd256d
JB
15042
15043 /* Remember that all the lh->file_names[i].name pointers are
15044 pointers into debug_line_buffer, and don't need to be freed. */
15045 if (lh->file_names)
a8bc7b56 15046 xfree (lh->file_names);
debd256d
JB
15047
15048 /* Similarly for the include directory names. */
15049 if (lh->include_dirs)
a8bc7b56 15050 xfree (lh->include_dirs);
debd256d 15051
a8bc7b56 15052 xfree (lh);
debd256d
JB
15053}
15054
debd256d 15055/* Add an entry to LH's include directory table. */
ae2de4f8 15056
debd256d
JB
15057static void
15058add_include_dir (struct line_header *lh, char *include_dir)
c906108c 15059{
debd256d
JB
15060 /* Grow the array if necessary. */
15061 if (lh->include_dirs_size == 0)
c5aa993b 15062 {
debd256d
JB
15063 lh->include_dirs_size = 1; /* for testing */
15064 lh->include_dirs = xmalloc (lh->include_dirs_size
15065 * sizeof (*lh->include_dirs));
15066 }
15067 else if (lh->num_include_dirs >= lh->include_dirs_size)
15068 {
15069 lh->include_dirs_size *= 2;
15070 lh->include_dirs = xrealloc (lh->include_dirs,
15071 (lh->include_dirs_size
15072 * sizeof (*lh->include_dirs)));
c5aa993b 15073 }
c906108c 15074
debd256d
JB
15075 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15076}
6e70227d 15077
debd256d 15078/* Add an entry to LH's file name table. */
ae2de4f8 15079
debd256d
JB
15080static void
15081add_file_name (struct line_header *lh,
15082 char *name,
15083 unsigned int dir_index,
15084 unsigned int mod_time,
15085 unsigned int length)
15086{
15087 struct file_entry *fe;
15088
15089 /* Grow the array if necessary. */
15090 if (lh->file_names_size == 0)
15091 {
15092 lh->file_names_size = 1; /* for testing */
15093 lh->file_names = xmalloc (lh->file_names_size
15094 * sizeof (*lh->file_names));
15095 }
15096 else if (lh->num_file_names >= lh->file_names_size)
15097 {
15098 lh->file_names_size *= 2;
15099 lh->file_names = xrealloc (lh->file_names,
15100 (lh->file_names_size
15101 * sizeof (*lh->file_names)));
15102 }
15103
15104 fe = &lh->file_names[lh->num_file_names++];
15105 fe->name = name;
15106 fe->dir_index = dir_index;
15107 fe->mod_time = mod_time;
15108 fe->length = length;
aaa75496 15109 fe->included_p = 0;
cb1df416 15110 fe->symtab = NULL;
debd256d 15111}
6e70227d 15112
36586728
TT
15113/* A convenience function to find the proper .debug_line section for a
15114 CU. */
15115
15116static struct dwarf2_section_info *
15117get_debug_line_section (struct dwarf2_cu *cu)
15118{
15119 struct dwarf2_section_info *section;
15120
15121 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15122 DWO file. */
15123 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15124 section = &cu->dwo_unit->dwo_file->sections.line;
15125 else if (cu->per_cu->is_dwz)
15126 {
15127 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15128
15129 section = &dwz->line;
15130 }
15131 else
15132 section = &dwarf2_per_objfile->line;
15133
15134 return section;
15135}
15136
debd256d 15137/* Read the statement program header starting at OFFSET in
3019eac3 15138 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15139 to a struct line_header, allocated using xmalloc.
debd256d
JB
15140
15141 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15142 the returned object point into the dwarf line section buffer,
15143 and must not be freed. */
ae2de4f8 15144
debd256d 15145static struct line_header *
3019eac3 15146dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15147{
15148 struct cleanup *back_to;
15149 struct line_header *lh;
fe1b8b76 15150 gdb_byte *line_ptr;
c764a876 15151 unsigned int bytes_read, offset_size;
debd256d
JB
15152 int i;
15153 char *cur_dir, *cur_file;
3019eac3
DE
15154 struct dwarf2_section_info *section;
15155 bfd *abfd;
15156
36586728 15157 section = get_debug_line_section (cu);
3019eac3
DE
15158 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15159 if (section->buffer == NULL)
debd256d 15160 {
3019eac3
DE
15161 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15162 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15163 else
15164 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15165 return 0;
15166 }
15167
fceca515
DE
15168 /* We can't do this until we know the section is non-empty.
15169 Only then do we know we have such a section. */
15170 abfd = section->asection->owner;
15171
a738430d
MK
15172 /* Make sure that at least there's room for the total_length field.
15173 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15174 if (offset + 4 >= section->size)
debd256d 15175 {
4d3c2250 15176 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15177 return 0;
15178 }
15179
15180 lh = xmalloc (sizeof (*lh));
15181 memset (lh, 0, sizeof (*lh));
15182 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15183 (void *) lh);
15184
3019eac3 15185 line_ptr = section->buffer + offset;
debd256d 15186
a738430d 15187 /* Read in the header. */
6e70227d 15188 lh->total_length =
c764a876
DE
15189 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15190 &bytes_read, &offset_size);
debd256d 15191 line_ptr += bytes_read;
3019eac3 15192 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15193 {
4d3c2250 15194 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15195 return 0;
15196 }
15197 lh->statement_program_end = line_ptr + lh->total_length;
15198 lh->version = read_2_bytes (abfd, line_ptr);
15199 line_ptr += 2;
c764a876
DE
15200 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15201 line_ptr += offset_size;
debd256d
JB
15202 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15203 line_ptr += 1;
2dc7f7b3
TT
15204 if (lh->version >= 4)
15205 {
15206 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15207 line_ptr += 1;
15208 }
15209 else
15210 lh->maximum_ops_per_instruction = 1;
15211
15212 if (lh->maximum_ops_per_instruction == 0)
15213 {
15214 lh->maximum_ops_per_instruction = 1;
15215 complaint (&symfile_complaints,
3e43a32a
MS
15216 _("invalid maximum_ops_per_instruction "
15217 "in `.debug_line' section"));
2dc7f7b3
TT
15218 }
15219
debd256d
JB
15220 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15221 line_ptr += 1;
15222 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15223 line_ptr += 1;
15224 lh->line_range = read_1_byte (abfd, line_ptr);
15225 line_ptr += 1;
15226 lh->opcode_base = read_1_byte (abfd, line_ptr);
15227 line_ptr += 1;
15228 lh->standard_opcode_lengths
fe1b8b76 15229 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15230
15231 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15232 for (i = 1; i < lh->opcode_base; ++i)
15233 {
15234 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15235 line_ptr += 1;
15236 }
15237
a738430d 15238 /* Read directory table. */
9b1c24c8 15239 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15240 {
15241 line_ptr += bytes_read;
15242 add_include_dir (lh, cur_dir);
15243 }
15244 line_ptr += bytes_read;
15245
a738430d 15246 /* Read file name table. */
9b1c24c8 15247 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15248 {
15249 unsigned int dir_index, mod_time, length;
15250
15251 line_ptr += bytes_read;
15252 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15253 line_ptr += bytes_read;
15254 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15255 line_ptr += bytes_read;
15256 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15257 line_ptr += bytes_read;
15258
15259 add_file_name (lh, cur_file, dir_index, mod_time, length);
15260 }
15261 line_ptr += bytes_read;
6e70227d 15262 lh->statement_program_start = line_ptr;
debd256d 15263
3019eac3 15264 if (line_ptr > (section->buffer + section->size))
4d3c2250 15265 complaint (&symfile_complaints,
3e43a32a
MS
15266 _("line number info header doesn't "
15267 "fit in `.debug_line' section"));
debd256d
JB
15268
15269 discard_cleanups (back_to);
15270 return lh;
15271}
c906108c 15272
c6da4cef
DE
15273/* Subroutine of dwarf_decode_lines to simplify it.
15274 Return the file name of the psymtab for included file FILE_INDEX
15275 in line header LH of PST.
15276 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15277 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15278 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15279
15280 The function creates dangling cleanup registration. */
c6da4cef
DE
15281
15282static char *
15283psymtab_include_file_name (const struct line_header *lh, int file_index,
15284 const struct partial_symtab *pst,
15285 const char *comp_dir)
15286{
15287 const struct file_entry fe = lh->file_names [file_index];
15288 char *include_name = fe.name;
15289 char *include_name_to_compare = include_name;
15290 char *dir_name = NULL;
72b9f47f
TT
15291 const char *pst_filename;
15292 char *copied_name = NULL;
c6da4cef
DE
15293 int file_is_pst;
15294
15295 if (fe.dir_index)
15296 dir_name = lh->include_dirs[fe.dir_index - 1];
15297
15298 if (!IS_ABSOLUTE_PATH (include_name)
15299 && (dir_name != NULL || comp_dir != NULL))
15300 {
15301 /* Avoid creating a duplicate psymtab for PST.
15302 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15303 Before we do the comparison, however, we need to account
15304 for DIR_NAME and COMP_DIR.
15305 First prepend dir_name (if non-NULL). If we still don't
15306 have an absolute path prepend comp_dir (if non-NULL).
15307 However, the directory we record in the include-file's
15308 psymtab does not contain COMP_DIR (to match the
15309 corresponding symtab(s)).
15310
15311 Example:
15312
15313 bash$ cd /tmp
15314 bash$ gcc -g ./hello.c
15315 include_name = "hello.c"
15316 dir_name = "."
15317 DW_AT_comp_dir = comp_dir = "/tmp"
15318 DW_AT_name = "./hello.c" */
15319
15320 if (dir_name != NULL)
15321 {
15322 include_name = concat (dir_name, SLASH_STRING,
15323 include_name, (char *)NULL);
15324 include_name_to_compare = include_name;
15325 make_cleanup (xfree, include_name);
15326 }
15327 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15328 {
15329 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15330 include_name, (char *)NULL);
15331 }
15332 }
15333
15334 pst_filename = pst->filename;
15335 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15336 {
72b9f47f
TT
15337 copied_name = concat (pst->dirname, SLASH_STRING,
15338 pst_filename, (char *)NULL);
15339 pst_filename = copied_name;
c6da4cef
DE
15340 }
15341
1e3fad37 15342 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15343
15344 if (include_name_to_compare != include_name)
15345 xfree (include_name_to_compare);
72b9f47f
TT
15346 if (copied_name != NULL)
15347 xfree (copied_name);
c6da4cef
DE
15348
15349 if (file_is_pst)
15350 return NULL;
15351 return include_name;
15352}
15353
c91513d8
PP
15354/* Ignore this record_line request. */
15355
15356static void
15357noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15358{
15359 return;
15360}
15361
f3f5162e
DE
15362/* Subroutine of dwarf_decode_lines to simplify it.
15363 Process the line number information in LH. */
debd256d 15364
c906108c 15365static void
f3f5162e
DE
15366dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15367 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15368{
a8c50c1f 15369 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15370 gdb_byte *line_end;
a8c50c1f 15371 unsigned int bytes_read, extended_len;
c906108c 15372 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15373 CORE_ADDR baseaddr;
15374 struct objfile *objfile = cu->objfile;
f3f5162e 15375 bfd *abfd = objfile->obfd;
fbf65064 15376 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15377 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15378 struct subfile *last_subfile = NULL;
c91513d8
PP
15379 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15380 = record_line;
e142c38c
DJ
15381
15382 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15383
debd256d
JB
15384 line_ptr = lh->statement_program_start;
15385 line_end = lh->statement_program_end;
c906108c
SS
15386
15387 /* Read the statement sequences until there's nothing left. */
15388 while (line_ptr < line_end)
15389 {
15390 /* state machine registers */
15391 CORE_ADDR address = 0;
15392 unsigned int file = 1;
15393 unsigned int line = 1;
15394 unsigned int column = 0;
debd256d 15395 int is_stmt = lh->default_is_stmt;
c906108c
SS
15396 int basic_block = 0;
15397 int end_sequence = 0;
fbf65064 15398 CORE_ADDR addr;
2dc7f7b3 15399 unsigned char op_index = 0;
c906108c 15400
aaa75496 15401 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15402 {
aaa75496 15403 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15404 /* lh->include_dirs and lh->file_names are 0-based, but the
15405 directory and file name numbers in the statement program
15406 are 1-based. */
15407 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15408 char *dir = NULL;
a738430d 15409
debd256d
JB
15410 if (fe->dir_index)
15411 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15412
15413 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15414 }
15415
a738430d 15416 /* Decode the table. */
c5aa993b 15417 while (!end_sequence)
c906108c
SS
15418 {
15419 op_code = read_1_byte (abfd, line_ptr);
15420 line_ptr += 1;
59205f5a
JB
15421 if (line_ptr > line_end)
15422 {
15423 dwarf2_debug_line_missing_end_sequence_complaint ();
15424 break;
15425 }
9aa1fe7e 15426
debd256d 15427 if (op_code >= lh->opcode_base)
6e70227d 15428 {
a738430d 15429 /* Special operand. */
debd256d 15430 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15431 address += (((op_index + (adj_opcode / lh->line_range))
15432 / lh->maximum_ops_per_instruction)
15433 * lh->minimum_instruction_length);
15434 op_index = ((op_index + (adj_opcode / lh->line_range))
15435 % lh->maximum_ops_per_instruction);
debd256d 15436 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15437 if (lh->num_file_names < file || file == 0)
25e43795 15438 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15439 /* For now we ignore lines not starting on an
15440 instruction boundary. */
15441 else if (op_index == 0)
25e43795
DJ
15442 {
15443 lh->file_names[file - 1].included_p = 1;
ca5f395d 15444 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15445 {
15446 if (last_subfile != current_subfile)
15447 {
15448 addr = gdbarch_addr_bits_remove (gdbarch, address);
15449 if (last_subfile)
c91513d8 15450 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15451 last_subfile = current_subfile;
15452 }
25e43795 15453 /* Append row to matrix using current values. */
7019d805 15454 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15455 (*p_record_line) (current_subfile, line, addr);
366da635 15456 }
25e43795 15457 }
ca5f395d 15458 basic_block = 0;
9aa1fe7e
GK
15459 }
15460 else switch (op_code)
c906108c
SS
15461 {
15462 case DW_LNS_extended_op:
3e43a32a
MS
15463 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15464 &bytes_read);
473b7be6 15465 line_ptr += bytes_read;
a8c50c1f 15466 extended_end = line_ptr + extended_len;
c906108c
SS
15467 extended_op = read_1_byte (abfd, line_ptr);
15468 line_ptr += 1;
15469 switch (extended_op)
15470 {
15471 case DW_LNE_end_sequence:
c91513d8 15472 p_record_line = record_line;
c906108c 15473 end_sequence = 1;
c906108c
SS
15474 break;
15475 case DW_LNE_set_address:
e7c27a73 15476 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15477
15478 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15479 {
15480 /* This line table is for a function which has been
15481 GCd by the linker. Ignore it. PR gdb/12528 */
15482
15483 long line_offset
36586728 15484 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15485
15486 complaint (&symfile_complaints,
15487 _(".debug_line address at offset 0x%lx is 0 "
15488 "[in module %s]"),
bb5ed363 15489 line_offset, objfile->name);
c91513d8
PP
15490 p_record_line = noop_record_line;
15491 }
15492
2dc7f7b3 15493 op_index = 0;
107d2387
AC
15494 line_ptr += bytes_read;
15495 address += baseaddr;
c906108c
SS
15496 break;
15497 case DW_LNE_define_file:
debd256d
JB
15498 {
15499 char *cur_file;
15500 unsigned int dir_index, mod_time, length;
6e70227d 15501
3e43a32a
MS
15502 cur_file = read_direct_string (abfd, line_ptr,
15503 &bytes_read);
debd256d
JB
15504 line_ptr += bytes_read;
15505 dir_index =
15506 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15507 line_ptr += bytes_read;
15508 mod_time =
15509 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15510 line_ptr += bytes_read;
15511 length =
15512 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15513 line_ptr += bytes_read;
15514 add_file_name (lh, cur_file, dir_index, mod_time, length);
15515 }
c906108c 15516 break;
d0c6ba3d
CC
15517 case DW_LNE_set_discriminator:
15518 /* The discriminator is not interesting to the debugger;
15519 just ignore it. */
15520 line_ptr = extended_end;
15521 break;
c906108c 15522 default:
4d3c2250 15523 complaint (&symfile_complaints,
e2e0b3e5 15524 _("mangled .debug_line section"));
debd256d 15525 return;
c906108c 15526 }
a8c50c1f
DJ
15527 /* Make sure that we parsed the extended op correctly. If e.g.
15528 we expected a different address size than the producer used,
15529 we may have read the wrong number of bytes. */
15530 if (line_ptr != extended_end)
15531 {
15532 complaint (&symfile_complaints,
15533 _("mangled .debug_line section"));
15534 return;
15535 }
c906108c
SS
15536 break;
15537 case DW_LNS_copy:
59205f5a 15538 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15539 dwarf2_debug_line_missing_file_complaint ();
15540 else
366da635 15541 {
25e43795 15542 lh->file_names[file - 1].included_p = 1;
ca5f395d 15543 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15544 {
15545 if (last_subfile != current_subfile)
15546 {
15547 addr = gdbarch_addr_bits_remove (gdbarch, address);
15548 if (last_subfile)
c91513d8 15549 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15550 last_subfile = current_subfile;
15551 }
7019d805 15552 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15553 (*p_record_line) (current_subfile, line, addr);
fbf65064 15554 }
366da635 15555 }
c906108c
SS
15556 basic_block = 0;
15557 break;
15558 case DW_LNS_advance_pc:
2dc7f7b3
TT
15559 {
15560 CORE_ADDR adjust
15561 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15562
15563 address += (((op_index + adjust)
15564 / lh->maximum_ops_per_instruction)
15565 * lh->minimum_instruction_length);
15566 op_index = ((op_index + adjust)
15567 % lh->maximum_ops_per_instruction);
15568 line_ptr += bytes_read;
15569 }
c906108c
SS
15570 break;
15571 case DW_LNS_advance_line:
15572 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15573 line_ptr += bytes_read;
15574 break;
15575 case DW_LNS_set_file:
debd256d 15576 {
a738430d
MK
15577 /* The arrays lh->include_dirs and lh->file_names are
15578 0-based, but the directory and file name numbers in
15579 the statement program are 1-based. */
debd256d 15580 struct file_entry *fe;
4f1520fb 15581 char *dir = NULL;
a738430d 15582
debd256d
JB
15583 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15584 line_ptr += bytes_read;
59205f5a 15585 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15586 dwarf2_debug_line_missing_file_complaint ();
15587 else
15588 {
15589 fe = &lh->file_names[file - 1];
15590 if (fe->dir_index)
15591 dir = lh->include_dirs[fe->dir_index - 1];
15592 if (!decode_for_pst_p)
15593 {
15594 last_subfile = current_subfile;
15595 dwarf2_start_subfile (fe->name, dir, comp_dir);
15596 }
15597 }
debd256d 15598 }
c906108c
SS
15599 break;
15600 case DW_LNS_set_column:
15601 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15602 line_ptr += bytes_read;
15603 break;
15604 case DW_LNS_negate_stmt:
15605 is_stmt = (!is_stmt);
15606 break;
15607 case DW_LNS_set_basic_block:
15608 basic_block = 1;
15609 break;
c2c6d25f
JM
15610 /* Add to the address register of the state machine the
15611 address increment value corresponding to special opcode
a738430d
MK
15612 255. I.e., this value is scaled by the minimum
15613 instruction length since special opcode 255 would have
b021a221 15614 scaled the increment. */
c906108c 15615 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15616 {
15617 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15618
15619 address += (((op_index + adjust)
15620 / lh->maximum_ops_per_instruction)
15621 * lh->minimum_instruction_length);
15622 op_index = ((op_index + adjust)
15623 % lh->maximum_ops_per_instruction);
15624 }
c906108c
SS
15625 break;
15626 case DW_LNS_fixed_advance_pc:
15627 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15628 op_index = 0;
c906108c
SS
15629 line_ptr += 2;
15630 break;
9aa1fe7e 15631 default:
a738430d
MK
15632 {
15633 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15634 int i;
a738430d 15635
debd256d 15636 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15637 {
15638 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15639 line_ptr += bytes_read;
15640 }
15641 }
c906108c
SS
15642 }
15643 }
59205f5a
JB
15644 if (lh->num_file_names < file || file == 0)
15645 dwarf2_debug_line_missing_file_complaint ();
15646 else
15647 {
15648 lh->file_names[file - 1].included_p = 1;
15649 if (!decode_for_pst_p)
fbf65064
UW
15650 {
15651 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15652 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15653 }
59205f5a 15654 }
c906108c 15655 }
f3f5162e
DE
15656}
15657
15658/* Decode the Line Number Program (LNP) for the given line_header
15659 structure and CU. The actual information extracted and the type
15660 of structures created from the LNP depends on the value of PST.
15661
15662 1. If PST is NULL, then this procedure uses the data from the program
15663 to create all necessary symbol tables, and their linetables.
15664
15665 2. If PST is not NULL, this procedure reads the program to determine
15666 the list of files included by the unit represented by PST, and
15667 builds all the associated partial symbol tables.
15668
15669 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15670 It is used for relative paths in the line table.
15671 NOTE: When processing partial symtabs (pst != NULL),
15672 comp_dir == pst->dirname.
15673
15674 NOTE: It is important that psymtabs have the same file name (via strcmp)
15675 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15676 symtab we don't use it in the name of the psymtabs we create.
15677 E.g. expand_line_sal requires this when finding psymtabs to expand.
15678 A good testcase for this is mb-inline.exp. */
15679
15680static void
15681dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15682 struct dwarf2_cu *cu, struct partial_symtab *pst,
15683 int want_line_info)
15684{
15685 struct objfile *objfile = cu->objfile;
15686 const int decode_for_pst_p = (pst != NULL);
15687 struct subfile *first_subfile = current_subfile;
15688
15689 if (want_line_info)
15690 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15691
15692 if (decode_for_pst_p)
15693 {
15694 int file_index;
15695
15696 /* Now that we're done scanning the Line Header Program, we can
15697 create the psymtab of each included file. */
15698 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15699 if (lh->file_names[file_index].included_p == 1)
15700 {
c6da4cef
DE
15701 char *include_name =
15702 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15703 if (include_name != NULL)
aaa75496
JB
15704 dwarf2_create_include_psymtab (include_name, pst, objfile);
15705 }
15706 }
cb1df416
DJ
15707 else
15708 {
15709 /* Make sure a symtab is created for every file, even files
15710 which contain only variables (i.e. no code with associated
15711 line numbers). */
cb1df416 15712 int i;
cb1df416
DJ
15713
15714 for (i = 0; i < lh->num_file_names; i++)
15715 {
15716 char *dir = NULL;
f3f5162e 15717 struct file_entry *fe;
9a619af0 15718
cb1df416
DJ
15719 fe = &lh->file_names[i];
15720 if (fe->dir_index)
15721 dir = lh->include_dirs[fe->dir_index - 1];
15722 dwarf2_start_subfile (fe->name, dir, comp_dir);
15723
15724 /* Skip the main file; we don't need it, and it must be
15725 allocated last, so that it will show up before the
15726 non-primary symtabs in the objfile's symtab list. */
15727 if (current_subfile == first_subfile)
15728 continue;
15729
15730 if (current_subfile->symtab == NULL)
15731 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15732 objfile);
cb1df416
DJ
15733 fe->symtab = current_subfile->symtab;
15734 }
15735 }
c906108c
SS
15736}
15737
15738/* Start a subfile for DWARF. FILENAME is the name of the file and
15739 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15740 or NULL if not known. COMP_DIR is the compilation directory for the
15741 linetable's compilation unit or NULL if not known.
c906108c
SS
15742 This routine tries to keep line numbers from identical absolute and
15743 relative file names in a common subfile.
15744
15745 Using the `list' example from the GDB testsuite, which resides in
15746 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15747 of /srcdir/list0.c yields the following debugging information for list0.c:
15748
c5aa993b
JM
15749 DW_AT_name: /srcdir/list0.c
15750 DW_AT_comp_dir: /compdir
357e46e7 15751 files.files[0].name: list0.h
c5aa993b 15752 files.files[0].dir: /srcdir
357e46e7 15753 files.files[1].name: list0.c
c5aa993b 15754 files.files[1].dir: /srcdir
c906108c
SS
15755
15756 The line number information for list0.c has to end up in a single
4f1520fb
FR
15757 subfile, so that `break /srcdir/list0.c:1' works as expected.
15758 start_subfile will ensure that this happens provided that we pass the
15759 concatenation of files.files[1].dir and files.files[1].name as the
15760 subfile's name. */
c906108c
SS
15761
15762static void
3e43a32a
MS
15763dwarf2_start_subfile (char *filename, const char *dirname,
15764 const char *comp_dir)
c906108c 15765{
4f1520fb
FR
15766 char *fullname;
15767
15768 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15769 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15770 second argument to start_subfile. To be consistent, we do the
15771 same here. In order not to lose the line information directory,
15772 we concatenate it to the filename when it makes sense.
15773 Note that the Dwarf3 standard says (speaking of filenames in line
15774 information): ``The directory index is ignored for file names
15775 that represent full path names''. Thus ignoring dirname in the
15776 `else' branch below isn't an issue. */
c906108c 15777
d5166ae1 15778 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15779 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15780 else
15781 fullname = filename;
c906108c 15782
4f1520fb
FR
15783 start_subfile (fullname, comp_dir);
15784
15785 if (fullname != filename)
15786 xfree (fullname);
c906108c
SS
15787}
15788
f4dc4d17
DE
15789/* Start a symtab for DWARF.
15790 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15791
15792static void
15793dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15794 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15795{
15796 start_symtab (name, comp_dir, low_pc);
15797 record_debugformat ("DWARF 2");
15798 record_producer (cu->producer);
15799
15800 /* We assume that we're processing GCC output. */
15801 processing_gcc_compilation = 2;
15802
4d4ec4e5 15803 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15804}
15805
4c2df51b
DJ
15806static void
15807var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15808 struct dwarf2_cu *cu)
4c2df51b 15809{
e7c27a73
DJ
15810 struct objfile *objfile = cu->objfile;
15811 struct comp_unit_head *cu_header = &cu->header;
15812
4c2df51b
DJ
15813 /* NOTE drow/2003-01-30: There used to be a comment and some special
15814 code here to turn a symbol with DW_AT_external and a
15815 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15816 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15817 with some versions of binutils) where shared libraries could have
15818 relocations against symbols in their debug information - the
15819 minimal symbol would have the right address, but the debug info
15820 would not. It's no longer necessary, because we will explicitly
15821 apply relocations when we read in the debug information now. */
15822
15823 /* A DW_AT_location attribute with no contents indicates that a
15824 variable has been optimized away. */
15825 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15826 {
f1e6e072 15827 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15828 return;
15829 }
15830
15831 /* Handle one degenerate form of location expression specially, to
15832 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15833 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15834 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15835
15836 if (attr_form_is_block (attr)
3019eac3
DE
15837 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15838 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15839 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15840 && (DW_BLOCK (attr)->size
15841 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15842 {
891d2f0b 15843 unsigned int dummy;
4c2df51b 15844
3019eac3
DE
15845 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15846 SYMBOL_VALUE_ADDRESS (sym) =
15847 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15848 else
15849 SYMBOL_VALUE_ADDRESS (sym) =
15850 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15851 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15852 fixup_symbol_section (sym, objfile);
15853 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15854 SYMBOL_SECTION (sym));
4c2df51b
DJ
15855 return;
15856 }
15857
15858 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15859 expression evaluator, and use LOC_COMPUTED only when necessary
15860 (i.e. when the value of a register or memory location is
15861 referenced, or a thread-local block, etc.). Then again, it might
15862 not be worthwhile. I'm assuming that it isn't unless performance
15863 or memory numbers show me otherwise. */
15864
f1e6e072 15865 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15866
f1e6e072 15867 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15868 cu->has_loclist = 1;
4c2df51b
DJ
15869}
15870
c906108c
SS
15871/* Given a pointer to a DWARF information entry, figure out if we need
15872 to make a symbol table entry for it, and if so, create a new entry
15873 and return a pointer to it.
15874 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15875 used the passed type.
15876 If SPACE is not NULL, use it to hold the new symbol. If it is
15877 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15878
15879static struct symbol *
34eaf542
TT
15880new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15881 struct symbol *space)
c906108c 15882{
e7c27a73 15883 struct objfile *objfile = cu->objfile;
c906108c 15884 struct symbol *sym = NULL;
15d034d0 15885 const char *name;
c906108c
SS
15886 struct attribute *attr = NULL;
15887 struct attribute *attr2 = NULL;
e142c38c 15888 CORE_ADDR baseaddr;
e37fd15a
SW
15889 struct pending **list_to_add = NULL;
15890
edb3359d 15891 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15892
15893 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15894
94af9270 15895 name = dwarf2_name (die, cu);
c906108c
SS
15896 if (name)
15897 {
94af9270 15898 const char *linkagename;
34eaf542 15899 int suppress_add = 0;
94af9270 15900
34eaf542
TT
15901 if (space)
15902 sym = space;
15903 else
15904 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15905 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15906
15907 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15908 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15909 linkagename = dwarf2_physname (name, die, cu);
15910 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15911
f55ee35c
JK
15912 /* Fortran does not have mangling standard and the mangling does differ
15913 between gfortran, iFort etc. */
15914 if (cu->language == language_fortran
b250c185 15915 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15916 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15917 dwarf2_full_name (name, die, cu),
29df156d 15918 NULL);
f55ee35c 15919
c906108c 15920 /* Default assumptions.
c5aa993b 15921 Use the passed type or decode it from the die. */
176620f1 15922 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 15923 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15924 if (type != NULL)
15925 SYMBOL_TYPE (sym) = type;
15926 else
e7c27a73 15927 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15928 attr = dwarf2_attr (die,
15929 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15930 cu);
c906108c
SS
15931 if (attr)
15932 {
15933 SYMBOL_LINE (sym) = DW_UNSND (attr);
15934 }
cb1df416 15935
edb3359d
DJ
15936 attr = dwarf2_attr (die,
15937 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15938 cu);
cb1df416
DJ
15939 if (attr)
15940 {
15941 int file_index = DW_UNSND (attr);
9a619af0 15942
cb1df416
DJ
15943 if (cu->line_header == NULL
15944 || file_index > cu->line_header->num_file_names)
15945 complaint (&symfile_complaints,
15946 _("file index out of range"));
1c3d648d 15947 else if (file_index > 0)
cb1df416
DJ
15948 {
15949 struct file_entry *fe;
9a619af0 15950
cb1df416
DJ
15951 fe = &cu->line_header->file_names[file_index - 1];
15952 SYMBOL_SYMTAB (sym) = fe->symtab;
15953 }
15954 }
15955
c906108c
SS
15956 switch (die->tag)
15957 {
15958 case DW_TAG_label:
e142c38c 15959 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15960 if (attr)
15961 {
15962 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15963 }
0f5238ed
TT
15964 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15965 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 15966 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 15967 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15968 break;
15969 case DW_TAG_subprogram:
15970 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15971 finish_block. */
f1e6e072 15972 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 15973 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15974 if ((attr2 && (DW_UNSND (attr2) != 0))
15975 || cu->language == language_ada)
c906108c 15976 {
2cfa0c8d
JB
15977 /* Subprograms marked external are stored as a global symbol.
15978 Ada subprograms, whether marked external or not, are always
15979 stored as a global symbol, because we want to be able to
15980 access them globally. For instance, we want to be able
15981 to break on a nested subprogram without having to
15982 specify the context. */
e37fd15a 15983 list_to_add = &global_symbols;
c906108c
SS
15984 }
15985 else
15986 {
e37fd15a 15987 list_to_add = cu->list_in_scope;
c906108c
SS
15988 }
15989 break;
edb3359d
DJ
15990 case DW_TAG_inlined_subroutine:
15991 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15992 finish_block. */
f1e6e072 15993 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 15994 SYMBOL_INLINED (sym) = 1;
481860b3 15995 list_to_add = cu->list_in_scope;
edb3359d 15996 break;
34eaf542
TT
15997 case DW_TAG_template_value_param:
15998 suppress_add = 1;
15999 /* Fall through. */
72929c62 16000 case DW_TAG_constant:
c906108c 16001 case DW_TAG_variable:
254e6b9e 16002 case DW_TAG_member:
0963b4bd
MS
16003 /* Compilation with minimal debug info may result in
16004 variables with missing type entries. Change the
16005 misleading `void' type to something sensible. */
c906108c 16006 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16007 SYMBOL_TYPE (sym)
46bf5051 16008 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16009
e142c38c 16010 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16011 /* In the case of DW_TAG_member, we should only be called for
16012 static const members. */
16013 if (die->tag == DW_TAG_member)
16014 {
3863f96c
DE
16015 /* dwarf2_add_field uses die_is_declaration,
16016 so we do the same. */
254e6b9e
DE
16017 gdb_assert (die_is_declaration (die, cu));
16018 gdb_assert (attr);
16019 }
c906108c
SS
16020 if (attr)
16021 {
e7c27a73 16022 dwarf2_const_value (attr, sym, cu);
e142c38c 16023 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16024 if (!suppress_add)
34eaf542
TT
16025 {
16026 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16027 list_to_add = &global_symbols;
34eaf542 16028 else
e37fd15a 16029 list_to_add = cu->list_in_scope;
34eaf542 16030 }
c906108c
SS
16031 break;
16032 }
e142c38c 16033 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16034 if (attr)
16035 {
e7c27a73 16036 var_decode_location (attr, sym, cu);
e142c38c 16037 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16038
16039 /* Fortran explicitly imports any global symbols to the local
16040 scope by DW_TAG_common_block. */
16041 if (cu->language == language_fortran && die->parent
16042 && die->parent->tag == DW_TAG_common_block)
16043 attr2 = NULL;
16044
caac4577
JG
16045 if (SYMBOL_CLASS (sym) == LOC_STATIC
16046 && SYMBOL_VALUE_ADDRESS (sym) == 0
16047 && !dwarf2_per_objfile->has_section_at_zero)
16048 {
16049 /* When a static variable is eliminated by the linker,
16050 the corresponding debug information is not stripped
16051 out, but the variable address is set to null;
16052 do not add such variables into symbol table. */
16053 }
16054 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16055 {
f55ee35c
JK
16056 /* Workaround gfortran PR debug/40040 - it uses
16057 DW_AT_location for variables in -fPIC libraries which may
16058 get overriden by other libraries/executable and get
16059 a different address. Resolve it by the minimal symbol
16060 which may come from inferior's executable using copy
16061 relocation. Make this workaround only for gfortran as for
16062 other compilers GDB cannot guess the minimal symbol
16063 Fortran mangling kind. */
16064 if (cu->language == language_fortran && die->parent
16065 && die->parent->tag == DW_TAG_module
16066 && cu->producer
16067 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16068 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16069
1c809c68
TT
16070 /* A variable with DW_AT_external is never static,
16071 but it may be block-scoped. */
16072 list_to_add = (cu->list_in_scope == &file_symbols
16073 ? &global_symbols : cu->list_in_scope);
1c809c68 16074 }
c906108c 16075 else
e37fd15a 16076 list_to_add = cu->list_in_scope;
c906108c
SS
16077 }
16078 else
16079 {
16080 /* We do not know the address of this symbol.
c5aa993b
JM
16081 If it is an external symbol and we have type information
16082 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16083 The address of the variable will then be determined from
16084 the minimal symbol table whenever the variable is
16085 referenced. */
e142c38c 16086 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16087
16088 /* Fortran explicitly imports any global symbols to the local
16089 scope by DW_TAG_common_block. */
16090 if (cu->language == language_fortran && die->parent
16091 && die->parent->tag == DW_TAG_common_block)
16092 {
16093 /* SYMBOL_CLASS doesn't matter here because
16094 read_common_block is going to reset it. */
16095 if (!suppress_add)
16096 list_to_add = cu->list_in_scope;
16097 }
16098 else if (attr2 && (DW_UNSND (attr2) != 0)
16099 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16100 {
0fe7935b
DJ
16101 /* A variable with DW_AT_external is never static, but it
16102 may be block-scoped. */
16103 list_to_add = (cu->list_in_scope == &file_symbols
16104 ? &global_symbols : cu->list_in_scope);
16105
f1e6e072 16106 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16107 }
442ddf59
JK
16108 else if (!die_is_declaration (die, cu))
16109 {
16110 /* Use the default LOC_OPTIMIZED_OUT class. */
16111 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16112 if (!suppress_add)
16113 list_to_add = cu->list_in_scope;
442ddf59 16114 }
c906108c
SS
16115 }
16116 break;
16117 case DW_TAG_formal_parameter:
edb3359d
DJ
16118 /* If we are inside a function, mark this as an argument. If
16119 not, we might be looking at an argument to an inlined function
16120 when we do not have enough information to show inlined frames;
16121 pretend it's a local variable in that case so that the user can
16122 still see it. */
16123 if (context_stack_depth > 0
16124 && context_stack[context_stack_depth - 1].name != NULL)
16125 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16126 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16127 if (attr)
16128 {
e7c27a73 16129 var_decode_location (attr, sym, cu);
c906108c 16130 }
e142c38c 16131 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16132 if (attr)
16133 {
e7c27a73 16134 dwarf2_const_value (attr, sym, cu);
c906108c 16135 }
f346a30d 16136
e37fd15a 16137 list_to_add = cu->list_in_scope;
c906108c
SS
16138 break;
16139 case DW_TAG_unspecified_parameters:
16140 /* From varargs functions; gdb doesn't seem to have any
16141 interest in this information, so just ignore it for now.
16142 (FIXME?) */
16143 break;
34eaf542
TT
16144 case DW_TAG_template_type_param:
16145 suppress_add = 1;
16146 /* Fall through. */
c906108c 16147 case DW_TAG_class_type:
680b30c7 16148 case DW_TAG_interface_type:
c906108c
SS
16149 case DW_TAG_structure_type:
16150 case DW_TAG_union_type:
72019c9c 16151 case DW_TAG_set_type:
c906108c 16152 case DW_TAG_enumeration_type:
f1e6e072 16153 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16154 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16155
63d06c5c 16156 {
987504bb 16157 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16158 really ever be static objects: otherwise, if you try
16159 to, say, break of a class's method and you're in a file
16160 which doesn't mention that class, it won't work unless
16161 the check for all static symbols in lookup_symbol_aux
16162 saves you. See the OtherFileClass tests in
16163 gdb.c++/namespace.exp. */
16164
e37fd15a 16165 if (!suppress_add)
34eaf542 16166 {
34eaf542
TT
16167 list_to_add = (cu->list_in_scope == &file_symbols
16168 && (cu->language == language_cplus
16169 || cu->language == language_java)
16170 ? &global_symbols : cu->list_in_scope);
63d06c5c 16171
64382290
TT
16172 /* The semantics of C++ state that "struct foo {
16173 ... }" also defines a typedef for "foo". A Java
16174 class declaration also defines a typedef for the
16175 class. */
16176 if (cu->language == language_cplus
16177 || cu->language == language_java
16178 || cu->language == language_ada)
16179 {
16180 /* The symbol's name is already allocated along
16181 with this objfile, so we don't need to
16182 duplicate it for the type. */
16183 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16184 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16185 }
63d06c5c
DC
16186 }
16187 }
c906108c
SS
16188 break;
16189 case DW_TAG_typedef:
f1e6e072 16190 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16191 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16192 list_to_add = cu->list_in_scope;
63d06c5c 16193 break;
c906108c 16194 case DW_TAG_base_type:
a02abb62 16195 case DW_TAG_subrange_type:
f1e6e072 16196 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16197 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16198 list_to_add = cu->list_in_scope;
c906108c
SS
16199 break;
16200 case DW_TAG_enumerator:
e142c38c 16201 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16202 if (attr)
16203 {
e7c27a73 16204 dwarf2_const_value (attr, sym, cu);
c906108c 16205 }
63d06c5c
DC
16206 {
16207 /* NOTE: carlton/2003-11-10: See comment above in the
16208 DW_TAG_class_type, etc. block. */
16209
e142c38c 16210 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16211 && (cu->language == language_cplus
16212 || cu->language == language_java)
e142c38c 16213 ? &global_symbols : cu->list_in_scope);
63d06c5c 16214 }
c906108c 16215 break;
5c4e30ca 16216 case DW_TAG_namespace:
f1e6e072 16217 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16218 list_to_add = &global_symbols;
5c4e30ca 16219 break;
4357ac6c 16220 case DW_TAG_common_block:
f1e6e072 16221 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16222 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16223 add_symbol_to_list (sym, cu->list_in_scope);
16224 break;
c906108c
SS
16225 default:
16226 /* Not a tag we recognize. Hopefully we aren't processing
16227 trash data, but since we must specifically ignore things
16228 we don't recognize, there is nothing else we should do at
0963b4bd 16229 this point. */
e2e0b3e5 16230 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16231 dwarf_tag_name (die->tag));
c906108c
SS
16232 break;
16233 }
df8a16a1 16234
e37fd15a
SW
16235 if (suppress_add)
16236 {
16237 sym->hash_next = objfile->template_symbols;
16238 objfile->template_symbols = sym;
16239 list_to_add = NULL;
16240 }
16241
16242 if (list_to_add != NULL)
16243 add_symbol_to_list (sym, list_to_add);
16244
df8a16a1
DJ
16245 /* For the benefit of old versions of GCC, check for anonymous
16246 namespaces based on the demangled name. */
4d4ec4e5 16247 if (!cu->processing_has_namespace_info
94af9270 16248 && cu->language == language_cplus)
a10964d1 16249 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16250 }
16251 return (sym);
16252}
16253
34eaf542
TT
16254/* A wrapper for new_symbol_full that always allocates a new symbol. */
16255
16256static struct symbol *
16257new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16258{
16259 return new_symbol_full (die, type, cu, NULL);
16260}
16261
98bfdba5
PA
16262/* Given an attr with a DW_FORM_dataN value in host byte order,
16263 zero-extend it as appropriate for the symbol's type. The DWARF
16264 standard (v4) is not entirely clear about the meaning of using
16265 DW_FORM_dataN for a constant with a signed type, where the type is
16266 wider than the data. The conclusion of a discussion on the DWARF
16267 list was that this is unspecified. We choose to always zero-extend
16268 because that is the interpretation long in use by GCC. */
c906108c 16269
98bfdba5
PA
16270static gdb_byte *
16271dwarf2_const_value_data (struct attribute *attr, struct type *type,
16272 const char *name, struct obstack *obstack,
12df843f 16273 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16274{
e7c27a73 16275 struct objfile *objfile = cu->objfile;
e17a4113
UW
16276 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16277 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16278 LONGEST l = DW_UNSND (attr);
16279
16280 if (bits < sizeof (*value) * 8)
16281 {
16282 l &= ((LONGEST) 1 << bits) - 1;
16283 *value = l;
16284 }
16285 else if (bits == sizeof (*value) * 8)
16286 *value = l;
16287 else
16288 {
16289 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16290 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16291 return bytes;
16292 }
16293
16294 return NULL;
16295}
16296
16297/* Read a constant value from an attribute. Either set *VALUE, or if
16298 the value does not fit in *VALUE, set *BYTES - either already
16299 allocated on the objfile obstack, or newly allocated on OBSTACK,
16300 or, set *BATON, if we translated the constant to a location
16301 expression. */
16302
16303static void
16304dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16305 const char *name, struct obstack *obstack,
16306 struct dwarf2_cu *cu,
12df843f 16307 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16308 struct dwarf2_locexpr_baton **baton)
16309{
16310 struct objfile *objfile = cu->objfile;
16311 struct comp_unit_head *cu_header = &cu->header;
c906108c 16312 struct dwarf_block *blk;
98bfdba5
PA
16313 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16314 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16315
16316 *value = 0;
16317 *bytes = NULL;
16318 *baton = NULL;
c906108c
SS
16319
16320 switch (attr->form)
16321 {
16322 case DW_FORM_addr:
3019eac3 16323 case DW_FORM_GNU_addr_index:
ac56253d 16324 {
ac56253d
TT
16325 gdb_byte *data;
16326
98bfdba5
PA
16327 if (TYPE_LENGTH (type) != cu_header->addr_size)
16328 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16329 cu_header->addr_size,
98bfdba5 16330 TYPE_LENGTH (type));
ac56253d
TT
16331 /* Symbols of this form are reasonably rare, so we just
16332 piggyback on the existing location code rather than writing
16333 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16334 *baton = obstack_alloc (&objfile->objfile_obstack,
16335 sizeof (struct dwarf2_locexpr_baton));
16336 (*baton)->per_cu = cu->per_cu;
16337 gdb_assert ((*baton)->per_cu);
ac56253d 16338
98bfdba5
PA
16339 (*baton)->size = 2 + cu_header->addr_size;
16340 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16341 (*baton)->data = data;
ac56253d
TT
16342
16343 data[0] = DW_OP_addr;
16344 store_unsigned_integer (&data[1], cu_header->addr_size,
16345 byte_order, DW_ADDR (attr));
16346 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16347 }
c906108c 16348 break;
4ac36638 16349 case DW_FORM_string:
93b5768b 16350 case DW_FORM_strp:
3019eac3 16351 case DW_FORM_GNU_str_index:
36586728 16352 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16353 /* DW_STRING is already allocated on the objfile obstack, point
16354 directly to it. */
16355 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16356 break;
c906108c
SS
16357 case DW_FORM_block1:
16358 case DW_FORM_block2:
16359 case DW_FORM_block4:
16360 case DW_FORM_block:
2dc7f7b3 16361 case DW_FORM_exprloc:
c906108c 16362 blk = DW_BLOCK (attr);
98bfdba5
PA
16363 if (TYPE_LENGTH (type) != blk->size)
16364 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16365 TYPE_LENGTH (type));
16366 *bytes = blk->data;
c906108c 16367 break;
2df3850c
JM
16368
16369 /* The DW_AT_const_value attributes are supposed to carry the
16370 symbol's value "represented as it would be on the target
16371 architecture." By the time we get here, it's already been
16372 converted to host endianness, so we just need to sign- or
16373 zero-extend it as appropriate. */
16374 case DW_FORM_data1:
3e43a32a
MS
16375 *bytes = dwarf2_const_value_data (attr, type, name,
16376 obstack, cu, value, 8);
2df3850c 16377 break;
c906108c 16378 case DW_FORM_data2:
3e43a32a
MS
16379 *bytes = dwarf2_const_value_data (attr, type, name,
16380 obstack, cu, value, 16);
2df3850c 16381 break;
c906108c 16382 case DW_FORM_data4:
3e43a32a
MS
16383 *bytes = dwarf2_const_value_data (attr, type, name,
16384 obstack, cu, value, 32);
2df3850c 16385 break;
c906108c 16386 case DW_FORM_data8:
3e43a32a
MS
16387 *bytes = dwarf2_const_value_data (attr, type, name,
16388 obstack, cu, value, 64);
2df3850c
JM
16389 break;
16390
c906108c 16391 case DW_FORM_sdata:
98bfdba5 16392 *value = DW_SND (attr);
2df3850c
JM
16393 break;
16394
c906108c 16395 case DW_FORM_udata:
98bfdba5 16396 *value = DW_UNSND (attr);
c906108c 16397 break;
2df3850c 16398
c906108c 16399 default:
4d3c2250 16400 complaint (&symfile_complaints,
e2e0b3e5 16401 _("unsupported const value attribute form: '%s'"),
4d3c2250 16402 dwarf_form_name (attr->form));
98bfdba5 16403 *value = 0;
c906108c
SS
16404 break;
16405 }
16406}
16407
2df3850c 16408
98bfdba5
PA
16409/* Copy constant value from an attribute to a symbol. */
16410
2df3850c 16411static void
98bfdba5
PA
16412dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16413 struct dwarf2_cu *cu)
2df3850c 16414{
98bfdba5
PA
16415 struct objfile *objfile = cu->objfile;
16416 struct comp_unit_head *cu_header = &cu->header;
12df843f 16417 LONGEST value;
98bfdba5
PA
16418 gdb_byte *bytes;
16419 struct dwarf2_locexpr_baton *baton;
2df3850c 16420
98bfdba5
PA
16421 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16422 SYMBOL_PRINT_NAME (sym),
16423 &objfile->objfile_obstack, cu,
16424 &value, &bytes, &baton);
2df3850c 16425
98bfdba5
PA
16426 if (baton != NULL)
16427 {
98bfdba5 16428 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16429 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16430 }
16431 else if (bytes != NULL)
16432 {
16433 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16434 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16435 }
16436 else
16437 {
16438 SYMBOL_VALUE (sym) = value;
f1e6e072 16439 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16440 }
2df3850c
JM
16441}
16442
c906108c
SS
16443/* Return the type of the die in question using its DW_AT_type attribute. */
16444
16445static struct type *
e7c27a73 16446die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16447{
c906108c 16448 struct attribute *type_attr;
c906108c 16449
e142c38c 16450 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16451 if (!type_attr)
16452 {
16453 /* A missing DW_AT_type represents a void type. */
46bf5051 16454 return objfile_type (cu->objfile)->builtin_void;
c906108c 16455 }
348e048f 16456
673bfd45 16457 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16458}
16459
b4ba55a1
JB
16460/* True iff CU's producer generates GNAT Ada auxiliary information
16461 that allows to find parallel types through that information instead
16462 of having to do expensive parallel lookups by type name. */
16463
16464static int
16465need_gnat_info (struct dwarf2_cu *cu)
16466{
16467 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16468 of GNAT produces this auxiliary information, without any indication
16469 that it is produced. Part of enhancing the FSF version of GNAT
16470 to produce that information will be to put in place an indicator
16471 that we can use in order to determine whether the descriptive type
16472 info is available or not. One suggestion that has been made is
16473 to use a new attribute, attached to the CU die. For now, assume
16474 that the descriptive type info is not available. */
16475 return 0;
16476}
16477
b4ba55a1
JB
16478/* Return the auxiliary type of the die in question using its
16479 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16480 attribute is not present. */
16481
16482static struct type *
16483die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16484{
b4ba55a1 16485 struct attribute *type_attr;
b4ba55a1
JB
16486
16487 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16488 if (!type_attr)
16489 return NULL;
16490
673bfd45 16491 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16492}
16493
16494/* If DIE has a descriptive_type attribute, then set the TYPE's
16495 descriptive type accordingly. */
16496
16497static void
16498set_descriptive_type (struct type *type, struct die_info *die,
16499 struct dwarf2_cu *cu)
16500{
16501 struct type *descriptive_type = die_descriptive_type (die, cu);
16502
16503 if (descriptive_type)
16504 {
16505 ALLOCATE_GNAT_AUX_TYPE (type);
16506 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16507 }
16508}
16509
c906108c
SS
16510/* Return the containing type of the die in question using its
16511 DW_AT_containing_type attribute. */
16512
16513static struct type *
e7c27a73 16514die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16515{
c906108c 16516 struct attribute *type_attr;
c906108c 16517
e142c38c 16518 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16519 if (!type_attr)
16520 error (_("Dwarf Error: Problem turning containing type into gdb type "
16521 "[in module %s]"), cu->objfile->name);
16522
673bfd45 16523 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16524}
16525
673bfd45
DE
16526/* Look up the type of DIE in CU using its type attribute ATTR.
16527 If there is no type substitute an error marker. */
16528
c906108c 16529static struct type *
673bfd45
DE
16530lookup_die_type (struct die_info *die, struct attribute *attr,
16531 struct dwarf2_cu *cu)
c906108c 16532{
bb5ed363 16533 struct objfile *objfile = cu->objfile;
f792889a
DJ
16534 struct type *this_type;
16535
673bfd45
DE
16536 /* First see if we have it cached. */
16537
36586728
TT
16538 if (attr->form == DW_FORM_GNU_ref_alt)
16539 {
16540 struct dwarf2_per_cu_data *per_cu;
16541 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16542
16543 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16544 this_type = get_die_type_at_offset (offset, per_cu);
16545 }
16546 else if (is_ref_attr (attr))
673bfd45 16547 {
b64f50a1 16548 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16549
16550 this_type = get_die_type_at_offset (offset, cu->per_cu);
16551 }
55f1336d 16552 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16553 {
16554 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16555
16556 /* sig_type will be NULL if the signatured type is missing from
16557 the debug info. */
16558 if (sig_type == NULL)
16559 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16560 "at 0x%x [in module %s]"),
b64f50a1 16561 die->offset.sect_off, objfile->name);
673bfd45 16562
3019eac3
DE
16563 gdb_assert (sig_type->per_cu.is_debug_types);
16564 /* If we haven't filled in type_offset_in_section yet, then we
16565 haven't read the type in yet. */
16566 this_type = NULL;
16567 if (sig_type->type_offset_in_section.sect_off != 0)
16568 {
16569 this_type =
16570 get_die_type_at_offset (sig_type->type_offset_in_section,
16571 &sig_type->per_cu);
16572 }
673bfd45
DE
16573 }
16574 else
16575 {
16576 dump_die_for_error (die);
16577 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16578 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16579 }
16580
16581 /* If not cached we need to read it in. */
16582
16583 if (this_type == NULL)
16584 {
16585 struct die_info *type_die;
16586 struct dwarf2_cu *type_cu = cu;
16587
16588 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16589 /* If we found the type now, it's probably because the type came
16590 from an inter-CU reference and the type's CU got expanded before
16591 ours. */
16592 this_type = get_die_type (type_die, type_cu);
16593 if (this_type == NULL)
16594 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16595 }
16596
16597 /* If we still don't have a type use an error marker. */
16598
16599 if (this_type == NULL)
c906108c 16600 {
b00fdb78
TT
16601 char *message, *saved;
16602
16603 /* read_type_die already issued a complaint. */
16604 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16605 objfile->name,
b64f50a1
JK
16606 cu->header.offset.sect_off,
16607 die->offset.sect_off);
bb5ed363 16608 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16609 message, strlen (message));
16610 xfree (message);
16611
bb5ed363 16612 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16613 }
673bfd45 16614
f792889a 16615 return this_type;
c906108c
SS
16616}
16617
673bfd45
DE
16618/* Return the type in DIE, CU.
16619 Returns NULL for invalid types.
16620
16621 This first does a lookup in the appropriate type_hash table,
16622 and only reads the die in if necessary.
16623
16624 NOTE: This can be called when reading in partial or full symbols. */
16625
f792889a 16626static struct type *
e7c27a73 16627read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16628{
f792889a
DJ
16629 struct type *this_type;
16630
16631 this_type = get_die_type (die, cu);
16632 if (this_type)
16633 return this_type;
16634
673bfd45
DE
16635 return read_type_die_1 (die, cu);
16636}
16637
16638/* Read the type in DIE, CU.
16639 Returns NULL for invalid types. */
16640
16641static struct type *
16642read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16643{
16644 struct type *this_type = NULL;
16645
c906108c
SS
16646 switch (die->tag)
16647 {
16648 case DW_TAG_class_type:
680b30c7 16649 case DW_TAG_interface_type:
c906108c
SS
16650 case DW_TAG_structure_type:
16651 case DW_TAG_union_type:
f792889a 16652 this_type = read_structure_type (die, cu);
c906108c
SS
16653 break;
16654 case DW_TAG_enumeration_type:
f792889a 16655 this_type = read_enumeration_type (die, cu);
c906108c
SS
16656 break;
16657 case DW_TAG_subprogram:
16658 case DW_TAG_subroutine_type:
edb3359d 16659 case DW_TAG_inlined_subroutine:
f792889a 16660 this_type = read_subroutine_type (die, cu);
c906108c
SS
16661 break;
16662 case DW_TAG_array_type:
f792889a 16663 this_type = read_array_type (die, cu);
c906108c 16664 break;
72019c9c 16665 case DW_TAG_set_type:
f792889a 16666 this_type = read_set_type (die, cu);
72019c9c 16667 break;
c906108c 16668 case DW_TAG_pointer_type:
f792889a 16669 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16670 break;
16671 case DW_TAG_ptr_to_member_type:
f792889a 16672 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16673 break;
16674 case DW_TAG_reference_type:
f792889a 16675 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16676 break;
16677 case DW_TAG_const_type:
f792889a 16678 this_type = read_tag_const_type (die, cu);
c906108c
SS
16679 break;
16680 case DW_TAG_volatile_type:
f792889a 16681 this_type = read_tag_volatile_type (die, cu);
c906108c 16682 break;
06d66ee9
TT
16683 case DW_TAG_restrict_type:
16684 this_type = read_tag_restrict_type (die, cu);
16685 break;
c906108c 16686 case DW_TAG_string_type:
f792889a 16687 this_type = read_tag_string_type (die, cu);
c906108c
SS
16688 break;
16689 case DW_TAG_typedef:
f792889a 16690 this_type = read_typedef (die, cu);
c906108c 16691 break;
a02abb62 16692 case DW_TAG_subrange_type:
f792889a 16693 this_type = read_subrange_type (die, cu);
a02abb62 16694 break;
c906108c 16695 case DW_TAG_base_type:
f792889a 16696 this_type = read_base_type (die, cu);
c906108c 16697 break;
81a17f79 16698 case DW_TAG_unspecified_type:
f792889a 16699 this_type = read_unspecified_type (die, cu);
81a17f79 16700 break;
0114d602
DJ
16701 case DW_TAG_namespace:
16702 this_type = read_namespace_type (die, cu);
16703 break;
f55ee35c
JK
16704 case DW_TAG_module:
16705 this_type = read_module_type (die, cu);
16706 break;
c906108c 16707 default:
3e43a32a
MS
16708 complaint (&symfile_complaints,
16709 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16710 dwarf_tag_name (die->tag));
c906108c
SS
16711 break;
16712 }
63d06c5c 16713
f792889a 16714 return this_type;
63d06c5c
DC
16715}
16716
abc72ce4
DE
16717/* See if we can figure out if the class lives in a namespace. We do
16718 this by looking for a member function; its demangled name will
16719 contain namespace info, if there is any.
16720 Return the computed name or NULL.
16721 Space for the result is allocated on the objfile's obstack.
16722 This is the full-die version of guess_partial_die_structure_name.
16723 In this case we know DIE has no useful parent. */
16724
16725static char *
16726guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16727{
16728 struct die_info *spec_die;
16729 struct dwarf2_cu *spec_cu;
16730 struct die_info *child;
16731
16732 spec_cu = cu;
16733 spec_die = die_specification (die, &spec_cu);
16734 if (spec_die != NULL)
16735 {
16736 die = spec_die;
16737 cu = spec_cu;
16738 }
16739
16740 for (child = die->child;
16741 child != NULL;
16742 child = child->sibling)
16743 {
16744 if (child->tag == DW_TAG_subprogram)
16745 {
16746 struct attribute *attr;
16747
16748 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16749 if (attr == NULL)
16750 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16751 if (attr != NULL)
16752 {
16753 char *actual_name
16754 = language_class_name_from_physname (cu->language_defn,
16755 DW_STRING (attr));
16756 char *name = NULL;
16757
16758 if (actual_name != NULL)
16759 {
15d034d0 16760 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16761
16762 if (die_name != NULL
16763 && strcmp (die_name, actual_name) != 0)
16764 {
16765 /* Strip off the class name from the full name.
16766 We want the prefix. */
16767 int die_name_len = strlen (die_name);
16768 int actual_name_len = strlen (actual_name);
16769
16770 /* Test for '::' as a sanity check. */
16771 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16772 && actual_name[actual_name_len
16773 - die_name_len - 1] == ':')
abc72ce4 16774 name =
10f0c4bb
TT
16775 obstack_copy0 (&cu->objfile->objfile_obstack,
16776 actual_name,
16777 actual_name_len - die_name_len - 2);
abc72ce4
DE
16778 }
16779 }
16780 xfree (actual_name);
16781 return name;
16782 }
16783 }
16784 }
16785
16786 return NULL;
16787}
16788
96408a79
SA
16789/* GCC might emit a nameless typedef that has a linkage name. Determine the
16790 prefix part in such case. See
16791 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16792
16793static char *
16794anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16795{
16796 struct attribute *attr;
16797 char *base;
16798
16799 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16800 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16801 return NULL;
16802
16803 attr = dwarf2_attr (die, DW_AT_name, cu);
16804 if (attr != NULL && DW_STRING (attr) != NULL)
16805 return NULL;
16806
16807 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16808 if (attr == NULL)
16809 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16810 if (attr == NULL || DW_STRING (attr) == NULL)
16811 return NULL;
16812
16813 /* dwarf2_name had to be already called. */
16814 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16815
16816 /* Strip the base name, keep any leading namespaces/classes. */
16817 base = strrchr (DW_STRING (attr), ':');
16818 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16819 return "";
16820
10f0c4bb
TT
16821 return obstack_copy0 (&cu->objfile->objfile_obstack,
16822 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16823}
16824
fdde2d81 16825/* Return the name of the namespace/class that DIE is defined within,
0114d602 16826 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16827
0114d602
DJ
16828 For example, if we're within the method foo() in the following
16829 code:
16830
16831 namespace N {
16832 class C {
16833 void foo () {
16834 }
16835 };
16836 }
16837
16838 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16839
0d5cff50 16840static const char *
e142c38c 16841determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16842{
0114d602
DJ
16843 struct die_info *parent, *spec_die;
16844 struct dwarf2_cu *spec_cu;
16845 struct type *parent_type;
96408a79 16846 char *retval;
63d06c5c 16847
f55ee35c
JK
16848 if (cu->language != language_cplus && cu->language != language_java
16849 && cu->language != language_fortran)
0114d602
DJ
16850 return "";
16851
96408a79
SA
16852 retval = anonymous_struct_prefix (die, cu);
16853 if (retval)
16854 return retval;
16855
0114d602
DJ
16856 /* We have to be careful in the presence of DW_AT_specification.
16857 For example, with GCC 3.4, given the code
16858
16859 namespace N {
16860 void foo() {
16861 // Definition of N::foo.
16862 }
16863 }
16864
16865 then we'll have a tree of DIEs like this:
16866
16867 1: DW_TAG_compile_unit
16868 2: DW_TAG_namespace // N
16869 3: DW_TAG_subprogram // declaration of N::foo
16870 4: DW_TAG_subprogram // definition of N::foo
16871 DW_AT_specification // refers to die #3
16872
16873 Thus, when processing die #4, we have to pretend that we're in
16874 the context of its DW_AT_specification, namely the contex of die
16875 #3. */
16876 spec_cu = cu;
16877 spec_die = die_specification (die, &spec_cu);
16878 if (spec_die == NULL)
16879 parent = die->parent;
16880 else
63d06c5c 16881 {
0114d602
DJ
16882 parent = spec_die->parent;
16883 cu = spec_cu;
63d06c5c 16884 }
0114d602
DJ
16885
16886 if (parent == NULL)
16887 return "";
98bfdba5
PA
16888 else if (parent->building_fullname)
16889 {
16890 const char *name;
16891 const char *parent_name;
16892
16893 /* It has been seen on RealView 2.2 built binaries,
16894 DW_TAG_template_type_param types actually _defined_ as
16895 children of the parent class:
16896
16897 enum E {};
16898 template class <class Enum> Class{};
16899 Class<enum E> class_e;
16900
16901 1: DW_TAG_class_type (Class)
16902 2: DW_TAG_enumeration_type (E)
16903 3: DW_TAG_enumerator (enum1:0)
16904 3: DW_TAG_enumerator (enum2:1)
16905 ...
16906 2: DW_TAG_template_type_param
16907 DW_AT_type DW_FORM_ref_udata (E)
16908
16909 Besides being broken debug info, it can put GDB into an
16910 infinite loop. Consider:
16911
16912 When we're building the full name for Class<E>, we'll start
16913 at Class, and go look over its template type parameters,
16914 finding E. We'll then try to build the full name of E, and
16915 reach here. We're now trying to build the full name of E,
16916 and look over the parent DIE for containing scope. In the
16917 broken case, if we followed the parent DIE of E, we'd again
16918 find Class, and once again go look at its template type
16919 arguments, etc., etc. Simply don't consider such parent die
16920 as source-level parent of this die (it can't be, the language
16921 doesn't allow it), and break the loop here. */
16922 name = dwarf2_name (die, cu);
16923 parent_name = dwarf2_name (parent, cu);
16924 complaint (&symfile_complaints,
16925 _("template param type '%s' defined within parent '%s'"),
16926 name ? name : "<unknown>",
16927 parent_name ? parent_name : "<unknown>");
16928 return "";
16929 }
63d06c5c 16930 else
0114d602
DJ
16931 switch (parent->tag)
16932 {
63d06c5c 16933 case DW_TAG_namespace:
0114d602 16934 parent_type = read_type_die (parent, cu);
acebe513
UW
16935 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16936 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16937 Work around this problem here. */
16938 if (cu->language == language_cplus
16939 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16940 return "";
0114d602
DJ
16941 /* We give a name to even anonymous namespaces. */
16942 return TYPE_TAG_NAME (parent_type);
63d06c5c 16943 case DW_TAG_class_type:
680b30c7 16944 case DW_TAG_interface_type:
63d06c5c 16945 case DW_TAG_structure_type:
0114d602 16946 case DW_TAG_union_type:
f55ee35c 16947 case DW_TAG_module:
0114d602
DJ
16948 parent_type = read_type_die (parent, cu);
16949 if (TYPE_TAG_NAME (parent_type) != NULL)
16950 return TYPE_TAG_NAME (parent_type);
16951 else
16952 /* An anonymous structure is only allowed non-static data
16953 members; no typedefs, no member functions, et cetera.
16954 So it does not need a prefix. */
16955 return "";
abc72ce4 16956 case DW_TAG_compile_unit:
95554aad 16957 case DW_TAG_partial_unit:
abc72ce4
DE
16958 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16959 if (cu->language == language_cplus
8b70b953 16960 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16961 && die->child != NULL
16962 && (die->tag == DW_TAG_class_type
16963 || die->tag == DW_TAG_structure_type
16964 || die->tag == DW_TAG_union_type))
16965 {
16966 char *name = guess_full_die_structure_name (die, cu);
16967 if (name != NULL)
16968 return name;
16969 }
16970 return "";
63d06c5c 16971 default:
8176b9b8 16972 return determine_prefix (parent, cu);
63d06c5c 16973 }
63d06c5c
DC
16974}
16975
3e43a32a
MS
16976/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16977 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16978 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16979 an obconcat, otherwise allocate storage for the result. The CU argument is
16980 used to determine the language and hence, the appropriate separator. */
987504bb 16981
f55ee35c 16982#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16983
16984static char *
f55ee35c
JK
16985typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16986 int physname, struct dwarf2_cu *cu)
63d06c5c 16987{
f55ee35c 16988 const char *lead = "";
5c315b68 16989 const char *sep;
63d06c5c 16990
3e43a32a
MS
16991 if (suffix == NULL || suffix[0] == '\0'
16992 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16993 sep = "";
16994 else if (cu->language == language_java)
16995 sep = ".";
f55ee35c
JK
16996 else if (cu->language == language_fortran && physname)
16997 {
16998 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16999 DW_AT_MIPS_linkage_name is preferred and used instead. */
17000
17001 lead = "__";
17002 sep = "_MOD_";
17003 }
987504bb
JJ
17004 else
17005 sep = "::";
63d06c5c 17006
6dd47d34
DE
17007 if (prefix == NULL)
17008 prefix = "";
17009 if (suffix == NULL)
17010 suffix = "";
17011
987504bb
JJ
17012 if (obs == NULL)
17013 {
3e43a32a
MS
17014 char *retval
17015 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17016
f55ee35c
JK
17017 strcpy (retval, lead);
17018 strcat (retval, prefix);
6dd47d34
DE
17019 strcat (retval, sep);
17020 strcat (retval, suffix);
63d06c5c
DC
17021 return retval;
17022 }
987504bb
JJ
17023 else
17024 {
17025 /* We have an obstack. */
f55ee35c 17026 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17027 }
63d06c5c
DC
17028}
17029
c906108c
SS
17030/* Return sibling of die, NULL if no sibling. */
17031
f9aca02d 17032static struct die_info *
fba45db2 17033sibling_die (struct die_info *die)
c906108c 17034{
639d11d3 17035 return die->sibling;
c906108c
SS
17036}
17037
71c25dea
TT
17038/* Get name of a die, return NULL if not found. */
17039
15d034d0
TT
17040static const char *
17041dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17042 struct obstack *obstack)
17043{
17044 if (name && cu->language == language_cplus)
17045 {
17046 char *canon_name = cp_canonicalize_string (name);
17047
17048 if (canon_name != NULL)
17049 {
17050 if (strcmp (canon_name, name) != 0)
10f0c4bb 17051 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17052 xfree (canon_name);
17053 }
17054 }
17055
17056 return name;
c906108c
SS
17057}
17058
9219021c
DC
17059/* Get name of a die, return NULL if not found. */
17060
15d034d0 17061static const char *
e142c38c 17062dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17063{
17064 struct attribute *attr;
17065
e142c38c 17066 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17067 if ((!attr || !DW_STRING (attr))
17068 && die->tag != DW_TAG_class_type
17069 && die->tag != DW_TAG_interface_type
17070 && die->tag != DW_TAG_structure_type
17071 && die->tag != DW_TAG_union_type)
71c25dea
TT
17072 return NULL;
17073
17074 switch (die->tag)
17075 {
17076 case DW_TAG_compile_unit:
95554aad 17077 case DW_TAG_partial_unit:
71c25dea
TT
17078 /* Compilation units have a DW_AT_name that is a filename, not
17079 a source language identifier. */
17080 case DW_TAG_enumeration_type:
17081 case DW_TAG_enumerator:
17082 /* These tags always have simple identifiers already; no need
17083 to canonicalize them. */
17084 return DW_STRING (attr);
907af001 17085
418835cc
KS
17086 case DW_TAG_subprogram:
17087 /* Java constructors will all be named "<init>", so return
17088 the class name when we see this special case. */
17089 if (cu->language == language_java
17090 && DW_STRING (attr) != NULL
17091 && strcmp (DW_STRING (attr), "<init>") == 0)
17092 {
17093 struct dwarf2_cu *spec_cu = cu;
17094 struct die_info *spec_die;
17095
17096 /* GCJ will output '<init>' for Java constructor names.
17097 For this special case, return the name of the parent class. */
17098
17099 /* GCJ may output suprogram DIEs with AT_specification set.
17100 If so, use the name of the specified DIE. */
17101 spec_die = die_specification (die, &spec_cu);
17102 if (spec_die != NULL)
17103 return dwarf2_name (spec_die, spec_cu);
17104
17105 do
17106 {
17107 die = die->parent;
17108 if (die->tag == DW_TAG_class_type)
17109 return dwarf2_name (die, cu);
17110 }
95554aad
TT
17111 while (die->tag != DW_TAG_compile_unit
17112 && die->tag != DW_TAG_partial_unit);
418835cc 17113 }
907af001
UW
17114 break;
17115
17116 case DW_TAG_class_type:
17117 case DW_TAG_interface_type:
17118 case DW_TAG_structure_type:
17119 case DW_TAG_union_type:
17120 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17121 structures or unions. These were of the form "._%d" in GCC 4.1,
17122 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17123 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17124 if (attr && DW_STRING (attr)
17125 && (strncmp (DW_STRING (attr), "._", 2) == 0
17126 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17127 return NULL;
53832f31
TT
17128
17129 /* GCC might emit a nameless typedef that has a linkage name. See
17130 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17131 if (!attr || DW_STRING (attr) == NULL)
17132 {
df5c6c50 17133 char *demangled = NULL;
53832f31
TT
17134
17135 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17136 if (attr == NULL)
17137 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17138
17139 if (attr == NULL || DW_STRING (attr) == NULL)
17140 return NULL;
17141
df5c6c50
JK
17142 /* Avoid demangling DW_STRING (attr) the second time on a second
17143 call for the same DIE. */
17144 if (!DW_STRING_IS_CANONICAL (attr))
17145 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17146
17147 if (demangled)
17148 {
96408a79
SA
17149 char *base;
17150
53832f31 17151 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17152 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17153 demangled, strlen (demangled));
53832f31
TT
17154 DW_STRING_IS_CANONICAL (attr) = 1;
17155 xfree (demangled);
96408a79
SA
17156
17157 /* Strip any leading namespaces/classes, keep only the base name.
17158 DW_AT_name for named DIEs does not contain the prefixes. */
17159 base = strrchr (DW_STRING (attr), ':');
17160 if (base && base > DW_STRING (attr) && base[-1] == ':')
17161 return &base[1];
17162 else
17163 return DW_STRING (attr);
53832f31
TT
17164 }
17165 }
907af001
UW
17166 break;
17167
71c25dea 17168 default:
907af001
UW
17169 break;
17170 }
17171
17172 if (!DW_STRING_IS_CANONICAL (attr))
17173 {
17174 DW_STRING (attr)
17175 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17176 &cu->objfile->objfile_obstack);
17177 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17178 }
907af001 17179 return DW_STRING (attr);
9219021c
DC
17180}
17181
17182/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17183 is none. *EXT_CU is the CU containing DIE on input, and the CU
17184 containing the return value on output. */
9219021c
DC
17185
17186static struct die_info *
f2f0e013 17187dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17188{
17189 struct attribute *attr;
9219021c 17190
f2f0e013 17191 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17192 if (attr == NULL)
17193 return NULL;
17194
f2f0e013 17195 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17196}
17197
c906108c
SS
17198/* Convert a DIE tag into its string name. */
17199
f39c6ffd 17200static const char *
aa1ee363 17201dwarf_tag_name (unsigned tag)
c906108c 17202{
f39c6ffd
TT
17203 const char *name = get_DW_TAG_name (tag);
17204
17205 if (name == NULL)
17206 return "DW_TAG_<unknown>";
17207
17208 return name;
c906108c
SS
17209}
17210
17211/* Convert a DWARF attribute code into its string name. */
17212
f39c6ffd 17213static const char *
aa1ee363 17214dwarf_attr_name (unsigned attr)
c906108c 17215{
f39c6ffd
TT
17216 const char *name;
17217
c764a876 17218#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17219 if (attr == DW_AT_MIPS_fde)
17220 return "DW_AT_MIPS_fde";
17221#else
17222 if (attr == DW_AT_HP_block_index)
17223 return "DW_AT_HP_block_index";
c764a876 17224#endif
f39c6ffd
TT
17225
17226 name = get_DW_AT_name (attr);
17227
17228 if (name == NULL)
17229 return "DW_AT_<unknown>";
17230
17231 return name;
c906108c
SS
17232}
17233
17234/* Convert a DWARF value form code into its string name. */
17235
f39c6ffd 17236static const char *
aa1ee363 17237dwarf_form_name (unsigned form)
c906108c 17238{
f39c6ffd
TT
17239 const char *name = get_DW_FORM_name (form);
17240
17241 if (name == NULL)
17242 return "DW_FORM_<unknown>";
17243
17244 return name;
c906108c
SS
17245}
17246
17247static char *
fba45db2 17248dwarf_bool_name (unsigned mybool)
c906108c
SS
17249{
17250 if (mybool)
17251 return "TRUE";
17252 else
17253 return "FALSE";
17254}
17255
17256/* Convert a DWARF type code into its string name. */
17257
f39c6ffd 17258static const char *
aa1ee363 17259dwarf_type_encoding_name (unsigned enc)
c906108c 17260{
f39c6ffd 17261 const char *name = get_DW_ATE_name (enc);
c906108c 17262
f39c6ffd
TT
17263 if (name == NULL)
17264 return "DW_ATE_<unknown>";
c906108c 17265
f39c6ffd 17266 return name;
c906108c 17267}
c906108c 17268
f9aca02d 17269static void
d97bc12b 17270dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17271{
17272 unsigned int i;
17273
d97bc12b
DE
17274 print_spaces (indent, f);
17275 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17276 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17277
17278 if (die->parent != NULL)
17279 {
17280 print_spaces (indent, f);
17281 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17282 die->parent->offset.sect_off);
d97bc12b
DE
17283 }
17284
17285 print_spaces (indent, f);
17286 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17287 dwarf_bool_name (die->child != NULL));
c906108c 17288
d97bc12b
DE
17289 print_spaces (indent, f);
17290 fprintf_unfiltered (f, " attributes:\n");
17291
c906108c
SS
17292 for (i = 0; i < die->num_attrs; ++i)
17293 {
d97bc12b
DE
17294 print_spaces (indent, f);
17295 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17296 dwarf_attr_name (die->attrs[i].name),
17297 dwarf_form_name (die->attrs[i].form));
d97bc12b 17298
c906108c
SS
17299 switch (die->attrs[i].form)
17300 {
c906108c 17301 case DW_FORM_addr:
3019eac3 17302 case DW_FORM_GNU_addr_index:
d97bc12b 17303 fprintf_unfiltered (f, "address: ");
5af949e3 17304 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17305 break;
17306 case DW_FORM_block2:
17307 case DW_FORM_block4:
17308 case DW_FORM_block:
17309 case DW_FORM_block1:
56eb65bd
SP
17310 fprintf_unfiltered (f, "block: size %s",
17311 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17312 break;
2dc7f7b3 17313 case DW_FORM_exprloc:
56eb65bd
SP
17314 fprintf_unfiltered (f, "expression: size %s",
17315 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17316 break;
4568ecf9
DE
17317 case DW_FORM_ref_addr:
17318 fprintf_unfiltered (f, "ref address: ");
17319 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17320 break;
36586728
TT
17321 case DW_FORM_GNU_ref_alt:
17322 fprintf_unfiltered (f, "alt ref address: ");
17323 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17324 break;
10b3939b
DJ
17325 case DW_FORM_ref1:
17326 case DW_FORM_ref2:
17327 case DW_FORM_ref4:
4568ecf9
DE
17328 case DW_FORM_ref8:
17329 case DW_FORM_ref_udata:
d97bc12b 17330 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17331 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17332 break;
c906108c
SS
17333 case DW_FORM_data1:
17334 case DW_FORM_data2:
17335 case DW_FORM_data4:
ce5d95e1 17336 case DW_FORM_data8:
c906108c
SS
17337 case DW_FORM_udata:
17338 case DW_FORM_sdata:
43bbcdc2
PH
17339 fprintf_unfiltered (f, "constant: %s",
17340 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17341 break;
2dc7f7b3
TT
17342 case DW_FORM_sec_offset:
17343 fprintf_unfiltered (f, "section offset: %s",
17344 pulongest (DW_UNSND (&die->attrs[i])));
17345 break;
55f1336d 17346 case DW_FORM_ref_sig8:
348e048f 17347 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
e4a48d9d
DE
17348 {
17349 struct signatured_type *sig_type =
17350 DW_SIGNATURED_TYPE (&die->attrs[i]);
17351
17352 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17353 hex_string (sig_type->signature),
17354 sig_type->per_cu.offset.sect_off);
17355 }
348e048f 17356 else
e4a48d9d 17357 fprintf_unfiltered (f, "signatured type, unknown");
348e048f 17358 break;
c906108c 17359 case DW_FORM_string:
4bdf3d34 17360 case DW_FORM_strp:
3019eac3 17361 case DW_FORM_GNU_str_index:
36586728 17362 case DW_FORM_GNU_strp_alt:
8285870a 17363 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17364 DW_STRING (&die->attrs[i])
8285870a
JK
17365 ? DW_STRING (&die->attrs[i]) : "",
17366 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17367 break;
17368 case DW_FORM_flag:
17369 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17370 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17371 else
d97bc12b 17372 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17373 break;
2dc7f7b3
TT
17374 case DW_FORM_flag_present:
17375 fprintf_unfiltered (f, "flag: TRUE");
17376 break;
a8329558 17377 case DW_FORM_indirect:
0963b4bd
MS
17378 /* The reader will have reduced the indirect form to
17379 the "base form" so this form should not occur. */
3e43a32a
MS
17380 fprintf_unfiltered (f,
17381 "unexpected attribute form: DW_FORM_indirect");
a8329558 17382 break;
c906108c 17383 default:
d97bc12b 17384 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17385 die->attrs[i].form);
d97bc12b 17386 break;
c906108c 17387 }
d97bc12b 17388 fprintf_unfiltered (f, "\n");
c906108c
SS
17389 }
17390}
17391
f9aca02d 17392static void
d97bc12b 17393dump_die_for_error (struct die_info *die)
c906108c 17394{
d97bc12b
DE
17395 dump_die_shallow (gdb_stderr, 0, die);
17396}
17397
17398static void
17399dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17400{
17401 int indent = level * 4;
17402
17403 gdb_assert (die != NULL);
17404
17405 if (level >= max_level)
17406 return;
17407
17408 dump_die_shallow (f, indent, die);
17409
17410 if (die->child != NULL)
c906108c 17411 {
d97bc12b
DE
17412 print_spaces (indent, f);
17413 fprintf_unfiltered (f, " Children:");
17414 if (level + 1 < max_level)
17415 {
17416 fprintf_unfiltered (f, "\n");
17417 dump_die_1 (f, level + 1, max_level, die->child);
17418 }
17419 else
17420 {
3e43a32a
MS
17421 fprintf_unfiltered (f,
17422 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17423 }
17424 }
17425
17426 if (die->sibling != NULL && level > 0)
17427 {
17428 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17429 }
17430}
17431
d97bc12b
DE
17432/* This is called from the pdie macro in gdbinit.in.
17433 It's not static so gcc will keep a copy callable from gdb. */
17434
17435void
17436dump_die (struct die_info *die, int max_level)
17437{
17438 dump_die_1 (gdb_stdlog, 0, max_level, die);
17439}
17440
f9aca02d 17441static void
51545339 17442store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17443{
51545339 17444 void **slot;
c906108c 17445
b64f50a1
JK
17446 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17447 INSERT);
51545339
DJ
17448
17449 *slot = die;
c906108c
SS
17450}
17451
b64f50a1
JK
17452/* DW_ADDR is always stored already as sect_offset; despite for the forms
17453 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17454
93311388
DE
17455static int
17456is_ref_attr (struct attribute *attr)
c906108c 17457{
c906108c
SS
17458 switch (attr->form)
17459 {
17460 case DW_FORM_ref_addr:
c906108c
SS
17461 case DW_FORM_ref1:
17462 case DW_FORM_ref2:
17463 case DW_FORM_ref4:
613e1657 17464 case DW_FORM_ref8:
c906108c 17465 case DW_FORM_ref_udata:
36586728 17466 case DW_FORM_GNU_ref_alt:
93311388 17467 return 1;
c906108c 17468 default:
93311388 17469 return 0;
c906108c 17470 }
93311388
DE
17471}
17472
b64f50a1
JK
17473/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17474 required kind. */
17475
17476static sect_offset
93311388
DE
17477dwarf2_get_ref_die_offset (struct attribute *attr)
17478{
4568ecf9 17479 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17480
93311388 17481 if (is_ref_attr (attr))
b64f50a1 17482 return retval;
93311388 17483
b64f50a1 17484 retval.sect_off = 0;
93311388
DE
17485 complaint (&symfile_complaints,
17486 _("unsupported die ref attribute form: '%s'"),
17487 dwarf_form_name (attr->form));
b64f50a1 17488 return retval;
c906108c
SS
17489}
17490
43bbcdc2
PH
17491/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17492 * the value held by the attribute is not constant. */
a02abb62 17493
43bbcdc2 17494static LONGEST
a02abb62
JB
17495dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17496{
17497 if (attr->form == DW_FORM_sdata)
17498 return DW_SND (attr);
17499 else if (attr->form == DW_FORM_udata
17500 || attr->form == DW_FORM_data1
17501 || attr->form == DW_FORM_data2
17502 || attr->form == DW_FORM_data4
17503 || attr->form == DW_FORM_data8)
17504 return DW_UNSND (attr);
17505 else
17506 {
3e43a32a
MS
17507 complaint (&symfile_complaints,
17508 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17509 dwarf_form_name (attr->form));
17510 return default_value;
17511 }
17512}
17513
348e048f
DE
17514/* Follow reference or signature attribute ATTR of SRC_DIE.
17515 On entry *REF_CU is the CU of SRC_DIE.
17516 On exit *REF_CU is the CU of the result. */
17517
17518static struct die_info *
17519follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17520 struct dwarf2_cu **ref_cu)
17521{
17522 struct die_info *die;
17523
17524 if (is_ref_attr (attr))
17525 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17526 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17527 die = follow_die_sig (src_die, attr, ref_cu);
17528 else
17529 {
17530 dump_die_for_error (src_die);
17531 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17532 (*ref_cu)->objfile->name);
17533 }
17534
17535 return die;
03dd20cc
DJ
17536}
17537
5c631832 17538/* Follow reference OFFSET.
673bfd45
DE
17539 On entry *REF_CU is the CU of the source die referencing OFFSET.
17540 On exit *REF_CU is the CU of the result.
17541 Returns NULL if OFFSET is invalid. */
f504f079 17542
f9aca02d 17543static struct die_info *
36586728
TT
17544follow_die_offset (sect_offset offset, int offset_in_dwz,
17545 struct dwarf2_cu **ref_cu)
c906108c 17546{
10b3939b 17547 struct die_info temp_die;
f2f0e013 17548 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17549
348e048f
DE
17550 gdb_assert (cu->per_cu != NULL);
17551
98bfdba5
PA
17552 target_cu = cu;
17553
3019eac3 17554 if (cu->per_cu->is_debug_types)
348e048f
DE
17555 {
17556 /* .debug_types CUs cannot reference anything outside their CU.
17557 If they need to, they have to reference a signatured type via
55f1336d 17558 DW_FORM_ref_sig8. */
348e048f 17559 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17560 return NULL;
348e048f 17561 }
36586728
TT
17562 else if (offset_in_dwz != cu->per_cu->is_dwz
17563 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17564 {
17565 struct dwarf2_per_cu_data *per_cu;
9a619af0 17566
36586728
TT
17567 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17568 cu->objfile);
03dd20cc
DJ
17569
17570 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17571 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17572 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17573
10b3939b
DJ
17574 target_cu = per_cu->cu;
17575 }
98bfdba5
PA
17576 else if (cu->dies == NULL)
17577 {
17578 /* We're loading full DIEs during partial symbol reading. */
17579 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17580 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17581 }
c906108c 17582
f2f0e013 17583 *ref_cu = target_cu;
51545339 17584 temp_die.offset = offset;
b64f50a1 17585 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17586}
10b3939b 17587
5c631832
JK
17588/* Follow reference attribute ATTR of SRC_DIE.
17589 On entry *REF_CU is the CU of SRC_DIE.
17590 On exit *REF_CU is the CU of the result. */
17591
17592static struct die_info *
17593follow_die_ref (struct die_info *src_die, struct attribute *attr,
17594 struct dwarf2_cu **ref_cu)
17595{
b64f50a1 17596 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17597 struct dwarf2_cu *cu = *ref_cu;
17598 struct die_info *die;
17599
36586728
TT
17600 die = follow_die_offset (offset,
17601 (attr->form == DW_FORM_GNU_ref_alt
17602 || cu->per_cu->is_dwz),
17603 ref_cu);
5c631832
JK
17604 if (!die)
17605 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17606 "at 0x%x [in module %s]"),
b64f50a1 17607 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17608
5c631832
JK
17609 return die;
17610}
17611
d83e736b
JK
17612/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17613 Returned value is intended for DW_OP_call*. Returned
17614 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17615
17616struct dwarf2_locexpr_baton
8b9737bf
TT
17617dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17618 struct dwarf2_per_cu_data *per_cu,
17619 CORE_ADDR (*get_frame_pc) (void *baton),
17620 void *baton)
5c631832 17621{
918dd910 17622 struct dwarf2_cu *cu;
5c631832
JK
17623 struct die_info *die;
17624 struct attribute *attr;
17625 struct dwarf2_locexpr_baton retval;
17626
8cf6f0b1
TT
17627 dw2_setup (per_cu->objfile);
17628
918dd910
JK
17629 if (per_cu->cu == NULL)
17630 load_cu (per_cu);
17631 cu = per_cu->cu;
17632
36586728 17633 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17634 if (!die)
17635 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17636 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17637
17638 attr = dwarf2_attr (die, DW_AT_location, cu);
17639 if (!attr)
17640 {
e103e986
JK
17641 /* DWARF: "If there is no such attribute, then there is no effect.".
17642 DATA is ignored if SIZE is 0. */
5c631832 17643
e103e986 17644 retval.data = NULL;
5c631832
JK
17645 retval.size = 0;
17646 }
8cf6f0b1
TT
17647 else if (attr_form_is_section_offset (attr))
17648 {
17649 struct dwarf2_loclist_baton loclist_baton;
17650 CORE_ADDR pc = (*get_frame_pc) (baton);
17651 size_t size;
17652
17653 fill_in_loclist_baton (cu, &loclist_baton, attr);
17654
17655 retval.data = dwarf2_find_location_expression (&loclist_baton,
17656 &size, pc);
17657 retval.size = size;
17658 }
5c631832
JK
17659 else
17660 {
17661 if (!attr_form_is_block (attr))
17662 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17663 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17664 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17665
17666 retval.data = DW_BLOCK (attr)->data;
17667 retval.size = DW_BLOCK (attr)->size;
17668 }
17669 retval.per_cu = cu->per_cu;
918dd910 17670
918dd910
JK
17671 age_cached_comp_units ();
17672
5c631832 17673 return retval;
348e048f
DE
17674}
17675
8b9737bf
TT
17676/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17677 offset. */
17678
17679struct dwarf2_locexpr_baton
17680dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17681 struct dwarf2_per_cu_data *per_cu,
17682 CORE_ADDR (*get_frame_pc) (void *baton),
17683 void *baton)
17684{
17685 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17686
17687 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17688}
17689
8a9b8146
TT
17690/* Return the type of the DIE at DIE_OFFSET in the CU named by
17691 PER_CU. */
17692
17693struct type *
b64f50a1 17694dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17695 struct dwarf2_per_cu_data *per_cu)
17696{
b64f50a1
JK
17697 sect_offset die_offset_sect;
17698
8a9b8146 17699 dw2_setup (per_cu->objfile);
b64f50a1
JK
17700
17701 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17702 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17703}
17704
348e048f
DE
17705/* Follow the signature attribute ATTR in SRC_DIE.
17706 On entry *REF_CU is the CU of SRC_DIE.
17707 On exit *REF_CU is the CU of the result. */
17708
17709static struct die_info *
17710follow_die_sig (struct die_info *src_die, struct attribute *attr,
17711 struct dwarf2_cu **ref_cu)
17712{
17713 struct objfile *objfile = (*ref_cu)->objfile;
17714 struct die_info temp_die;
17715 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17716 struct dwarf2_cu *sig_cu;
17717 struct die_info *die;
17718
17719 /* sig_type will be NULL if the signatured type is missing from
17720 the debug info. */
17721 if (sig_type == NULL)
17722 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17723 "at 0x%x [in module %s]"),
b64f50a1 17724 src_die->offset.sect_off, objfile->name);
348e048f
DE
17725
17726 /* If necessary, add it to the queue and load its DIEs. */
17727
95554aad 17728 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17729 read_signatured_type (sig_type);
348e048f
DE
17730
17731 gdb_assert (sig_type->per_cu.cu != NULL);
17732
17733 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17734 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17735 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17736 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17737 temp_die.offset.sect_off);
348e048f
DE
17738 if (die)
17739 {
796a7ff8
DE
17740 /* For .gdb_index version 7 keep track of included TUs.
17741 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17742 if (dwarf2_per_objfile->index_table != NULL
17743 && dwarf2_per_objfile->index_table->version <= 7)
17744 {
17745 VEC_safe_push (dwarf2_per_cu_ptr,
17746 (*ref_cu)->per_cu->imported_symtabs,
17747 sig_cu->per_cu);
17748 }
17749
348e048f
DE
17750 *ref_cu = sig_cu;
17751 return die;
17752 }
17753
3e43a32a
MS
17754 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17755 "from DIE at 0x%x [in module %s]"),
b64f50a1 17756 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17757}
17758
e5fe5e75 17759/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17760
17761static void
e5fe5e75 17762load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17763{
52dc124a 17764 struct signatured_type *sig_type;
348e048f 17765
f4dc4d17
DE
17766 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17767 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17768
6721b2ec
DE
17769 /* We have the per_cu, but we need the signatured_type.
17770 Fortunately this is an easy translation. */
17771 gdb_assert (per_cu->is_debug_types);
17772 sig_type = (struct signatured_type *) per_cu;
348e048f 17773
6721b2ec 17774 gdb_assert (per_cu->cu == NULL);
348e048f 17775
52dc124a 17776 read_signatured_type (sig_type);
348e048f 17777
6721b2ec 17778 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17779}
17780
dee91e82
DE
17781/* die_reader_func for read_signatured_type.
17782 This is identical to load_full_comp_unit_reader,
17783 but is kept separate for now. */
348e048f
DE
17784
17785static void
dee91e82
DE
17786read_signatured_type_reader (const struct die_reader_specs *reader,
17787 gdb_byte *info_ptr,
17788 struct die_info *comp_unit_die,
17789 int has_children,
17790 void *data)
348e048f 17791{
dee91e82 17792 struct dwarf2_cu *cu = reader->cu;
348e048f 17793
dee91e82
DE
17794 gdb_assert (cu->die_hash == NULL);
17795 cu->die_hash =
17796 htab_create_alloc_ex (cu->header.length / 12,
17797 die_hash,
17798 die_eq,
17799 NULL,
17800 &cu->comp_unit_obstack,
17801 hashtab_obstack_allocate,
17802 dummy_obstack_deallocate);
348e048f 17803
dee91e82
DE
17804 if (has_children)
17805 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17806 &info_ptr, comp_unit_die);
17807 cu->dies = comp_unit_die;
17808 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17809
17810 /* We try not to read any attributes in this function, because not
9cdd5dbd 17811 all CUs needed for references have been loaded yet, and symbol
348e048f 17812 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17813 or we won't be able to build types correctly.
17814 Similarly, if we do not read the producer, we can not apply
17815 producer-specific interpretation. */
95554aad 17816 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17817}
348e048f 17818
3019eac3
DE
17819/* Read in a signatured type and build its CU and DIEs.
17820 If the type is a stub for the real type in a DWO file,
17821 read in the real type from the DWO file as well. */
dee91e82
DE
17822
17823static void
17824read_signatured_type (struct signatured_type *sig_type)
17825{
17826 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17827
3019eac3 17828 gdb_assert (per_cu->is_debug_types);
dee91e82 17829 gdb_assert (per_cu->cu == NULL);
348e048f 17830
f4dc4d17
DE
17831 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17832 read_signatured_type_reader, NULL);
c906108c
SS
17833}
17834
c906108c
SS
17835/* Decode simple location descriptions.
17836 Given a pointer to a dwarf block that defines a location, compute
17837 the location and return the value.
17838
4cecd739
DJ
17839 NOTE drow/2003-11-18: This function is called in two situations
17840 now: for the address of static or global variables (partial symbols
17841 only) and for offsets into structures which are expected to be
17842 (more or less) constant. The partial symbol case should go away,
17843 and only the constant case should remain. That will let this
17844 function complain more accurately. A few special modes are allowed
17845 without complaint for global variables (for instance, global
17846 register values and thread-local values).
c906108c
SS
17847
17848 A location description containing no operations indicates that the
4cecd739 17849 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17850 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17851 callers will only want a very basic result and this can become a
21ae7a4d
JK
17852 complaint.
17853
17854 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17855
17856static CORE_ADDR
e7c27a73 17857decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17858{
e7c27a73 17859 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17860 size_t i;
17861 size_t size = blk->size;
21ae7a4d
JK
17862 gdb_byte *data = blk->data;
17863 CORE_ADDR stack[64];
17864 int stacki;
17865 unsigned int bytes_read, unsnd;
17866 gdb_byte op;
c906108c 17867
21ae7a4d
JK
17868 i = 0;
17869 stacki = 0;
17870 stack[stacki] = 0;
17871 stack[++stacki] = 0;
17872
17873 while (i < size)
17874 {
17875 op = data[i++];
17876 switch (op)
17877 {
17878 case DW_OP_lit0:
17879 case DW_OP_lit1:
17880 case DW_OP_lit2:
17881 case DW_OP_lit3:
17882 case DW_OP_lit4:
17883 case DW_OP_lit5:
17884 case DW_OP_lit6:
17885 case DW_OP_lit7:
17886 case DW_OP_lit8:
17887 case DW_OP_lit9:
17888 case DW_OP_lit10:
17889 case DW_OP_lit11:
17890 case DW_OP_lit12:
17891 case DW_OP_lit13:
17892 case DW_OP_lit14:
17893 case DW_OP_lit15:
17894 case DW_OP_lit16:
17895 case DW_OP_lit17:
17896 case DW_OP_lit18:
17897 case DW_OP_lit19:
17898 case DW_OP_lit20:
17899 case DW_OP_lit21:
17900 case DW_OP_lit22:
17901 case DW_OP_lit23:
17902 case DW_OP_lit24:
17903 case DW_OP_lit25:
17904 case DW_OP_lit26:
17905 case DW_OP_lit27:
17906 case DW_OP_lit28:
17907 case DW_OP_lit29:
17908 case DW_OP_lit30:
17909 case DW_OP_lit31:
17910 stack[++stacki] = op - DW_OP_lit0;
17911 break;
f1bea926 17912
21ae7a4d
JK
17913 case DW_OP_reg0:
17914 case DW_OP_reg1:
17915 case DW_OP_reg2:
17916 case DW_OP_reg3:
17917 case DW_OP_reg4:
17918 case DW_OP_reg5:
17919 case DW_OP_reg6:
17920 case DW_OP_reg7:
17921 case DW_OP_reg8:
17922 case DW_OP_reg9:
17923 case DW_OP_reg10:
17924 case DW_OP_reg11:
17925 case DW_OP_reg12:
17926 case DW_OP_reg13:
17927 case DW_OP_reg14:
17928 case DW_OP_reg15:
17929 case DW_OP_reg16:
17930 case DW_OP_reg17:
17931 case DW_OP_reg18:
17932 case DW_OP_reg19:
17933 case DW_OP_reg20:
17934 case DW_OP_reg21:
17935 case DW_OP_reg22:
17936 case DW_OP_reg23:
17937 case DW_OP_reg24:
17938 case DW_OP_reg25:
17939 case DW_OP_reg26:
17940 case DW_OP_reg27:
17941 case DW_OP_reg28:
17942 case DW_OP_reg29:
17943 case DW_OP_reg30:
17944 case DW_OP_reg31:
17945 stack[++stacki] = op - DW_OP_reg0;
17946 if (i < size)
17947 dwarf2_complex_location_expr_complaint ();
17948 break;
c906108c 17949
21ae7a4d
JK
17950 case DW_OP_regx:
17951 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17952 i += bytes_read;
17953 stack[++stacki] = unsnd;
17954 if (i < size)
17955 dwarf2_complex_location_expr_complaint ();
17956 break;
c906108c 17957
21ae7a4d
JK
17958 case DW_OP_addr:
17959 stack[++stacki] = read_address (objfile->obfd, &data[i],
17960 cu, &bytes_read);
17961 i += bytes_read;
17962 break;
d53d4ac5 17963
21ae7a4d
JK
17964 case DW_OP_const1u:
17965 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17966 i += 1;
17967 break;
17968
17969 case DW_OP_const1s:
17970 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17971 i += 1;
17972 break;
17973
17974 case DW_OP_const2u:
17975 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17976 i += 2;
17977 break;
17978
17979 case DW_OP_const2s:
17980 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17981 i += 2;
17982 break;
d53d4ac5 17983
21ae7a4d
JK
17984 case DW_OP_const4u:
17985 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17986 i += 4;
17987 break;
17988
17989 case DW_OP_const4s:
17990 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17991 i += 4;
17992 break;
17993
585861ea
JK
17994 case DW_OP_const8u:
17995 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17996 i += 8;
17997 break;
17998
21ae7a4d
JK
17999 case DW_OP_constu:
18000 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18001 &bytes_read);
18002 i += bytes_read;
18003 break;
18004
18005 case DW_OP_consts:
18006 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18007 i += bytes_read;
18008 break;
18009
18010 case DW_OP_dup:
18011 stack[stacki + 1] = stack[stacki];
18012 stacki++;
18013 break;
18014
18015 case DW_OP_plus:
18016 stack[stacki - 1] += stack[stacki];
18017 stacki--;
18018 break;
18019
18020 case DW_OP_plus_uconst:
18021 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18022 &bytes_read);
18023 i += bytes_read;
18024 break;
18025
18026 case DW_OP_minus:
18027 stack[stacki - 1] -= stack[stacki];
18028 stacki--;
18029 break;
18030
18031 case DW_OP_deref:
18032 /* If we're not the last op, then we definitely can't encode
18033 this using GDB's address_class enum. This is valid for partial
18034 global symbols, although the variable's address will be bogus
18035 in the psymtab. */
18036 if (i < size)
18037 dwarf2_complex_location_expr_complaint ();
18038 break;
18039
18040 case DW_OP_GNU_push_tls_address:
18041 /* The top of the stack has the offset from the beginning
18042 of the thread control block at which the variable is located. */
18043 /* Nothing should follow this operator, so the top of stack would
18044 be returned. */
18045 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18046 address will be bogus in the psymtab. Make it always at least
18047 non-zero to not look as a variable garbage collected by linker
18048 which have DW_OP_addr 0. */
21ae7a4d
JK
18049 if (i < size)
18050 dwarf2_complex_location_expr_complaint ();
585861ea 18051 stack[stacki]++;
21ae7a4d
JK
18052 break;
18053
18054 case DW_OP_GNU_uninit:
18055 break;
18056
3019eac3 18057 case DW_OP_GNU_addr_index:
49f6c839 18058 case DW_OP_GNU_const_index:
3019eac3
DE
18059 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18060 &bytes_read);
18061 i += bytes_read;
18062 break;
18063
21ae7a4d
JK
18064 default:
18065 {
f39c6ffd 18066 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18067
18068 if (name)
18069 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18070 name);
18071 else
18072 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18073 op);
18074 }
18075
18076 return (stack[stacki]);
d53d4ac5 18077 }
3c6e0cb3 18078
21ae7a4d
JK
18079 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18080 outside of the allocated space. Also enforce minimum>0. */
18081 if (stacki >= ARRAY_SIZE (stack) - 1)
18082 {
18083 complaint (&symfile_complaints,
18084 _("location description stack overflow"));
18085 return 0;
18086 }
18087
18088 if (stacki <= 0)
18089 {
18090 complaint (&symfile_complaints,
18091 _("location description stack underflow"));
18092 return 0;
18093 }
18094 }
18095 return (stack[stacki]);
c906108c
SS
18096}
18097
18098/* memory allocation interface */
18099
c906108c 18100static struct dwarf_block *
7b5a2f43 18101dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18102{
18103 struct dwarf_block *blk;
18104
18105 blk = (struct dwarf_block *)
7b5a2f43 18106 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18107 return (blk);
18108}
18109
c906108c 18110static struct die_info *
b60c80d6 18111dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18112{
18113 struct die_info *die;
b60c80d6
DJ
18114 size_t size = sizeof (struct die_info);
18115
18116 if (num_attrs > 1)
18117 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18118
b60c80d6 18119 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18120 memset (die, 0, sizeof (struct die_info));
18121 return (die);
18122}
2e276125
JB
18123
18124\f
18125/* Macro support. */
18126
233d95b5
JK
18127/* Return file name relative to the compilation directory of file number I in
18128 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18129 responsible for freeing it. */
233d95b5 18130
2e276125 18131static char *
233d95b5 18132file_file_name (int file, struct line_header *lh)
2e276125 18133{
6a83a1e6
EZ
18134 /* Is the file number a valid index into the line header's file name
18135 table? Remember that file numbers start with one, not zero. */
18136 if (1 <= file && file <= lh->num_file_names)
18137 {
18138 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18139
233d95b5 18140 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18141 return xstrdup (fe->name);
233d95b5
JK
18142 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18143 fe->name, NULL);
6a83a1e6 18144 }
2e276125
JB
18145 else
18146 {
6a83a1e6
EZ
18147 /* The compiler produced a bogus file number. We can at least
18148 record the macro definitions made in the file, even if we
18149 won't be able to find the file by name. */
18150 char fake_name[80];
9a619af0 18151
8c042590
PM
18152 xsnprintf (fake_name, sizeof (fake_name),
18153 "<bad macro file number %d>", file);
2e276125 18154
6e70227d 18155 complaint (&symfile_complaints,
6a83a1e6
EZ
18156 _("bad file number in macro information (%d)"),
18157 file);
2e276125 18158
6a83a1e6 18159 return xstrdup (fake_name);
2e276125
JB
18160 }
18161}
18162
233d95b5
JK
18163/* Return the full name of file number I in *LH's file name table.
18164 Use COMP_DIR as the name of the current directory of the
18165 compilation. The result is allocated using xmalloc; the caller is
18166 responsible for freeing it. */
18167static char *
18168file_full_name (int file, struct line_header *lh, const char *comp_dir)
18169{
18170 /* Is the file number a valid index into the line header's file name
18171 table? Remember that file numbers start with one, not zero. */
18172 if (1 <= file && file <= lh->num_file_names)
18173 {
18174 char *relative = file_file_name (file, lh);
18175
18176 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18177 return relative;
18178 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18179 }
18180 else
18181 return file_file_name (file, lh);
18182}
18183
2e276125
JB
18184
18185static struct macro_source_file *
18186macro_start_file (int file, int line,
18187 struct macro_source_file *current_file,
18188 const char *comp_dir,
18189 struct line_header *lh, struct objfile *objfile)
18190{
233d95b5
JK
18191 /* File name relative to the compilation directory of this source file. */
18192 char *file_name = file_file_name (file, lh);
2e276125
JB
18193
18194 /* We don't create a macro table for this compilation unit
18195 at all until we actually get a filename. */
18196 if (! pending_macros)
6532ff36 18197 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18198 objfile->per_bfd->macro_cache,
18199 comp_dir);
2e276125
JB
18200
18201 if (! current_file)
abc9d0dc
TT
18202 {
18203 /* If we have no current file, then this must be the start_file
18204 directive for the compilation unit's main source file. */
233d95b5 18205 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18206 macro_define_special (pending_macros);
18207 }
2e276125 18208 else
233d95b5 18209 current_file = macro_include (current_file, line, file_name);
2e276125 18210
233d95b5 18211 xfree (file_name);
6e70227d 18212
2e276125
JB
18213 return current_file;
18214}
18215
18216
18217/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18218 followed by a null byte. */
18219static char *
18220copy_string (const char *buf, int len)
18221{
18222 char *s = xmalloc (len + 1);
9a619af0 18223
2e276125
JB
18224 memcpy (s, buf, len);
18225 s[len] = '\0';
2e276125
JB
18226 return s;
18227}
18228
18229
18230static const char *
18231consume_improper_spaces (const char *p, const char *body)
18232{
18233 if (*p == ' ')
18234 {
4d3c2250 18235 complaint (&symfile_complaints,
3e43a32a
MS
18236 _("macro definition contains spaces "
18237 "in formal argument list:\n`%s'"),
4d3c2250 18238 body);
2e276125
JB
18239
18240 while (*p == ' ')
18241 p++;
18242 }
18243
18244 return p;
18245}
18246
18247
18248static void
18249parse_macro_definition (struct macro_source_file *file, int line,
18250 const char *body)
18251{
18252 const char *p;
18253
18254 /* The body string takes one of two forms. For object-like macro
18255 definitions, it should be:
18256
18257 <macro name> " " <definition>
18258
18259 For function-like macro definitions, it should be:
18260
18261 <macro name> "() " <definition>
18262 or
18263 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18264
18265 Spaces may appear only where explicitly indicated, and in the
18266 <definition>.
18267
18268 The Dwarf 2 spec says that an object-like macro's name is always
18269 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18270 the space when the macro's definition is the empty string.
2e276125
JB
18271
18272 The Dwarf 2 spec says that there should be no spaces between the
18273 formal arguments in a function-like macro's formal argument list,
18274 but versions of GCC around March 2002 include spaces after the
18275 commas. */
18276
18277
18278 /* Find the extent of the macro name. The macro name is terminated
18279 by either a space or null character (for an object-like macro) or
18280 an opening paren (for a function-like macro). */
18281 for (p = body; *p; p++)
18282 if (*p == ' ' || *p == '(')
18283 break;
18284
18285 if (*p == ' ' || *p == '\0')
18286 {
18287 /* It's an object-like macro. */
18288 int name_len = p - body;
18289 char *name = copy_string (body, name_len);
18290 const char *replacement;
18291
18292 if (*p == ' ')
18293 replacement = body + name_len + 1;
18294 else
18295 {
4d3c2250 18296 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18297 replacement = body + name_len;
18298 }
6e70227d 18299
2e276125
JB
18300 macro_define_object (file, line, name, replacement);
18301
18302 xfree (name);
18303 }
18304 else if (*p == '(')
18305 {
18306 /* It's a function-like macro. */
18307 char *name = copy_string (body, p - body);
18308 int argc = 0;
18309 int argv_size = 1;
18310 char **argv = xmalloc (argv_size * sizeof (*argv));
18311
18312 p++;
18313
18314 p = consume_improper_spaces (p, body);
18315
18316 /* Parse the formal argument list. */
18317 while (*p && *p != ')')
18318 {
18319 /* Find the extent of the current argument name. */
18320 const char *arg_start = p;
18321
18322 while (*p && *p != ',' && *p != ')' && *p != ' ')
18323 p++;
18324
18325 if (! *p || p == arg_start)
4d3c2250 18326 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18327 else
18328 {
18329 /* Make sure argv has room for the new argument. */
18330 if (argc >= argv_size)
18331 {
18332 argv_size *= 2;
18333 argv = xrealloc (argv, argv_size * sizeof (*argv));
18334 }
18335
18336 argv[argc++] = copy_string (arg_start, p - arg_start);
18337 }
18338
18339 p = consume_improper_spaces (p, body);
18340
18341 /* Consume the comma, if present. */
18342 if (*p == ',')
18343 {
18344 p++;
18345
18346 p = consume_improper_spaces (p, body);
18347 }
18348 }
18349
18350 if (*p == ')')
18351 {
18352 p++;
18353
18354 if (*p == ' ')
18355 /* Perfectly formed definition, no complaints. */
18356 macro_define_function (file, line, name,
6e70227d 18357 argc, (const char **) argv,
2e276125
JB
18358 p + 1);
18359 else if (*p == '\0')
18360 {
18361 /* Complain, but do define it. */
4d3c2250 18362 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18363 macro_define_function (file, line, name,
6e70227d 18364 argc, (const char **) argv,
2e276125
JB
18365 p);
18366 }
18367 else
18368 /* Just complain. */
4d3c2250 18369 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18370 }
18371 else
18372 /* Just complain. */
4d3c2250 18373 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18374
18375 xfree (name);
18376 {
18377 int i;
18378
18379 for (i = 0; i < argc; i++)
18380 xfree (argv[i]);
18381 }
18382 xfree (argv);
18383 }
18384 else
4d3c2250 18385 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18386}
18387
cf2c3c16
TT
18388/* Skip some bytes from BYTES according to the form given in FORM.
18389 Returns the new pointer. */
2e276125 18390
cf2c3c16 18391static gdb_byte *
f664829e 18392skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18393 enum dwarf_form form,
18394 unsigned int offset_size,
18395 struct dwarf2_section_info *section)
2e276125 18396{
cf2c3c16 18397 unsigned int bytes_read;
2e276125 18398
cf2c3c16 18399 switch (form)
2e276125 18400 {
cf2c3c16
TT
18401 case DW_FORM_data1:
18402 case DW_FORM_flag:
18403 ++bytes;
18404 break;
18405
18406 case DW_FORM_data2:
18407 bytes += 2;
18408 break;
18409
18410 case DW_FORM_data4:
18411 bytes += 4;
18412 break;
18413
18414 case DW_FORM_data8:
18415 bytes += 8;
18416 break;
18417
18418 case DW_FORM_string:
18419 read_direct_string (abfd, bytes, &bytes_read);
18420 bytes += bytes_read;
18421 break;
18422
18423 case DW_FORM_sec_offset:
18424 case DW_FORM_strp:
36586728 18425 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18426 bytes += offset_size;
18427 break;
18428
18429 case DW_FORM_block:
18430 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18431 bytes += bytes_read;
18432 break;
18433
18434 case DW_FORM_block1:
18435 bytes += 1 + read_1_byte (abfd, bytes);
18436 break;
18437 case DW_FORM_block2:
18438 bytes += 2 + read_2_bytes (abfd, bytes);
18439 break;
18440 case DW_FORM_block4:
18441 bytes += 4 + read_4_bytes (abfd, bytes);
18442 break;
18443
18444 case DW_FORM_sdata:
18445 case DW_FORM_udata:
3019eac3
DE
18446 case DW_FORM_GNU_addr_index:
18447 case DW_FORM_GNU_str_index:
f664829e
DE
18448 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18449 if (bytes == NULL)
18450 {
18451 dwarf2_section_buffer_overflow_complaint (section);
18452 return NULL;
18453 }
cf2c3c16
TT
18454 break;
18455
18456 default:
18457 {
18458 complain:
18459 complaint (&symfile_complaints,
18460 _("invalid form 0x%x in `%s'"),
18461 form,
18462 section->asection->name);
18463 return NULL;
18464 }
2e276125
JB
18465 }
18466
cf2c3c16
TT
18467 return bytes;
18468}
757a13d0 18469
cf2c3c16
TT
18470/* A helper for dwarf_decode_macros that handles skipping an unknown
18471 opcode. Returns an updated pointer to the macro data buffer; or,
18472 on error, issues a complaint and returns NULL. */
757a13d0 18473
cf2c3c16
TT
18474static gdb_byte *
18475skip_unknown_opcode (unsigned int opcode,
18476 gdb_byte **opcode_definitions,
f664829e 18477 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18478 bfd *abfd,
18479 unsigned int offset_size,
18480 struct dwarf2_section_info *section)
18481{
18482 unsigned int bytes_read, i;
18483 unsigned long arg;
18484 gdb_byte *defn;
2e276125 18485
cf2c3c16 18486 if (opcode_definitions[opcode] == NULL)
2e276125 18487 {
cf2c3c16
TT
18488 complaint (&symfile_complaints,
18489 _("unrecognized DW_MACFINO opcode 0x%x"),
18490 opcode);
18491 return NULL;
18492 }
2e276125 18493
cf2c3c16
TT
18494 defn = opcode_definitions[opcode];
18495 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18496 defn += bytes_read;
2e276125 18497
cf2c3c16
TT
18498 for (i = 0; i < arg; ++i)
18499 {
f664829e
DE
18500 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18501 section);
cf2c3c16
TT
18502 if (mac_ptr == NULL)
18503 {
18504 /* skip_form_bytes already issued the complaint. */
18505 return NULL;
18506 }
18507 }
757a13d0 18508
cf2c3c16
TT
18509 return mac_ptr;
18510}
757a13d0 18511
cf2c3c16
TT
18512/* A helper function which parses the header of a macro section.
18513 If the macro section is the extended (for now called "GNU") type,
18514 then this updates *OFFSET_SIZE. Returns a pointer to just after
18515 the header, or issues a complaint and returns NULL on error. */
757a13d0 18516
cf2c3c16
TT
18517static gdb_byte *
18518dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18519 bfd *abfd,
18520 gdb_byte *mac_ptr,
18521 unsigned int *offset_size,
18522 int section_is_gnu)
18523{
18524 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18525
cf2c3c16
TT
18526 if (section_is_gnu)
18527 {
18528 unsigned int version, flags;
757a13d0 18529
cf2c3c16
TT
18530 version = read_2_bytes (abfd, mac_ptr);
18531 if (version != 4)
18532 {
18533 complaint (&symfile_complaints,
18534 _("unrecognized version `%d' in .debug_macro section"),
18535 version);
18536 return NULL;
18537 }
18538 mac_ptr += 2;
757a13d0 18539
cf2c3c16
TT
18540 flags = read_1_byte (abfd, mac_ptr);
18541 ++mac_ptr;
18542 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18543
cf2c3c16
TT
18544 if ((flags & 2) != 0)
18545 /* We don't need the line table offset. */
18546 mac_ptr += *offset_size;
757a13d0 18547
cf2c3c16
TT
18548 /* Vendor opcode descriptions. */
18549 if ((flags & 4) != 0)
18550 {
18551 unsigned int i, count;
757a13d0 18552
cf2c3c16
TT
18553 count = read_1_byte (abfd, mac_ptr);
18554 ++mac_ptr;
18555 for (i = 0; i < count; ++i)
18556 {
18557 unsigned int opcode, bytes_read;
18558 unsigned long arg;
18559
18560 opcode = read_1_byte (abfd, mac_ptr);
18561 ++mac_ptr;
18562 opcode_definitions[opcode] = mac_ptr;
18563 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18564 mac_ptr += bytes_read;
18565 mac_ptr += arg;
18566 }
757a13d0 18567 }
cf2c3c16 18568 }
757a13d0 18569
cf2c3c16
TT
18570 return mac_ptr;
18571}
757a13d0 18572
cf2c3c16 18573/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18574 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18575
18576static void
18577dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18578 struct macro_source_file *current_file,
15d034d0 18579 struct line_header *lh, const char *comp_dir,
cf2c3c16 18580 struct dwarf2_section_info *section,
36586728 18581 int section_is_gnu, int section_is_dwz,
cf2c3c16 18582 unsigned int offset_size,
8fc3fc34
TT
18583 struct objfile *objfile,
18584 htab_t include_hash)
cf2c3c16
TT
18585{
18586 enum dwarf_macro_record_type macinfo_type;
18587 int at_commandline;
18588 gdb_byte *opcode_definitions[256];
757a13d0 18589
cf2c3c16
TT
18590 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18591 &offset_size, section_is_gnu);
18592 if (mac_ptr == NULL)
18593 {
18594 /* We already issued a complaint. */
18595 return;
18596 }
757a13d0
JK
18597
18598 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18599 GDB is still reading the definitions from command line. First
18600 DW_MACINFO_start_file will need to be ignored as it was already executed
18601 to create CURRENT_FILE for the main source holding also the command line
18602 definitions. On first met DW_MACINFO_start_file this flag is reset to
18603 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18604
18605 at_commandline = 1;
18606
18607 do
18608 {
18609 /* Do we at least have room for a macinfo type byte? */
18610 if (mac_ptr >= mac_end)
18611 {
f664829e 18612 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18613 break;
18614 }
18615
18616 macinfo_type = read_1_byte (abfd, mac_ptr);
18617 mac_ptr++;
18618
cf2c3c16
TT
18619 /* Note that we rely on the fact that the corresponding GNU and
18620 DWARF constants are the same. */
757a13d0
JK
18621 switch (macinfo_type)
18622 {
18623 /* A zero macinfo type indicates the end of the macro
18624 information. */
18625 case 0:
18626 break;
2e276125 18627
cf2c3c16
TT
18628 case DW_MACRO_GNU_define:
18629 case DW_MACRO_GNU_undef:
18630 case DW_MACRO_GNU_define_indirect:
18631 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18632 case DW_MACRO_GNU_define_indirect_alt:
18633 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18634 {
891d2f0b 18635 unsigned int bytes_read;
2e276125
JB
18636 int line;
18637 char *body;
cf2c3c16 18638 int is_define;
2e276125 18639
cf2c3c16
TT
18640 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18641 mac_ptr += bytes_read;
18642
18643 if (macinfo_type == DW_MACRO_GNU_define
18644 || macinfo_type == DW_MACRO_GNU_undef)
18645 {
18646 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18647 mac_ptr += bytes_read;
18648 }
18649 else
18650 {
18651 LONGEST str_offset;
18652
18653 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18654 mac_ptr += offset_size;
2e276125 18655
36586728 18656 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18657 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18658 || section_is_dwz)
36586728
TT
18659 {
18660 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18661
18662 body = read_indirect_string_from_dwz (dwz, str_offset);
18663 }
18664 else
18665 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18666 }
18667
18668 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18669 || macinfo_type == DW_MACRO_GNU_define_indirect
18670 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18671 if (! current_file)
757a13d0
JK
18672 {
18673 /* DWARF violation as no main source is present. */
18674 complaint (&symfile_complaints,
18675 _("debug info with no main source gives macro %s "
18676 "on line %d: %s"),
cf2c3c16
TT
18677 is_define ? _("definition") : _("undefinition"),
18678 line, body);
757a13d0
JK
18679 break;
18680 }
3e43a32a
MS
18681 if ((line == 0 && !at_commandline)
18682 || (line != 0 && at_commandline))
4d3c2250 18683 complaint (&symfile_complaints,
757a13d0
JK
18684 _("debug info gives %s macro %s with %s line %d: %s"),
18685 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18686 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18687 line == 0 ? _("zero") : _("non-zero"), line, body);
18688
cf2c3c16 18689 if (is_define)
757a13d0 18690 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18691 else
18692 {
18693 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18694 || macinfo_type == DW_MACRO_GNU_undef_indirect
18695 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18696 macro_undef (current_file, line, body);
18697 }
2e276125
JB
18698 }
18699 break;
18700
cf2c3c16 18701 case DW_MACRO_GNU_start_file:
2e276125 18702 {
891d2f0b 18703 unsigned int bytes_read;
2e276125
JB
18704 int line, file;
18705
18706 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18707 mac_ptr += bytes_read;
18708 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18709 mac_ptr += bytes_read;
18710
3e43a32a
MS
18711 if ((line == 0 && !at_commandline)
18712 || (line != 0 && at_commandline))
757a13d0
JK
18713 complaint (&symfile_complaints,
18714 _("debug info gives source %d included "
18715 "from %s at %s line %d"),
18716 file, at_commandline ? _("command-line") : _("file"),
18717 line == 0 ? _("zero") : _("non-zero"), line);
18718
18719 if (at_commandline)
18720 {
cf2c3c16
TT
18721 /* This DW_MACRO_GNU_start_file was executed in the
18722 pass one. */
757a13d0
JK
18723 at_commandline = 0;
18724 }
18725 else
18726 current_file = macro_start_file (file, line,
18727 current_file, comp_dir,
cf2c3c16 18728 lh, objfile);
2e276125
JB
18729 }
18730 break;
18731
cf2c3c16 18732 case DW_MACRO_GNU_end_file:
2e276125 18733 if (! current_file)
4d3c2250 18734 complaint (&symfile_complaints,
3e43a32a
MS
18735 _("macro debug info has an unmatched "
18736 "`close_file' directive"));
2e276125
JB
18737 else
18738 {
18739 current_file = current_file->included_by;
18740 if (! current_file)
18741 {
cf2c3c16 18742 enum dwarf_macro_record_type next_type;
2e276125
JB
18743
18744 /* GCC circa March 2002 doesn't produce the zero
18745 type byte marking the end of the compilation
18746 unit. Complain if it's not there, but exit no
18747 matter what. */
18748
18749 /* Do we at least have room for a macinfo type byte? */
18750 if (mac_ptr >= mac_end)
18751 {
f664829e 18752 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18753 return;
18754 }
18755
18756 /* We don't increment mac_ptr here, so this is just
18757 a look-ahead. */
18758 next_type = read_1_byte (abfd, mac_ptr);
18759 if (next_type != 0)
4d3c2250 18760 complaint (&symfile_complaints,
3e43a32a
MS
18761 _("no terminating 0-type entry for "
18762 "macros in `.debug_macinfo' section"));
2e276125
JB
18763
18764 return;
18765 }
18766 }
18767 break;
18768
cf2c3c16 18769 case DW_MACRO_GNU_transparent_include:
36586728 18770 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18771 {
18772 LONGEST offset;
8fc3fc34 18773 void **slot;
a036ba48
TT
18774 bfd *include_bfd = abfd;
18775 struct dwarf2_section_info *include_section = section;
18776 struct dwarf2_section_info alt_section;
18777 gdb_byte *include_mac_end = mac_end;
18778 int is_dwz = section_is_dwz;
18779 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18780
18781 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18782 mac_ptr += offset_size;
18783
a036ba48
TT
18784 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18785 {
18786 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18787
18788 dwarf2_read_section (dwarf2_per_objfile->objfile,
18789 &dwz->macro);
18790
18791 include_bfd = dwz->macro.asection->owner;
18792 include_section = &dwz->macro;
18793 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18794 is_dwz = 1;
18795 }
18796
18797 new_mac_ptr = include_section->buffer + offset;
18798 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18799
8fc3fc34
TT
18800 if (*slot != NULL)
18801 {
18802 /* This has actually happened; see
18803 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18804 complaint (&symfile_complaints,
18805 _("recursive DW_MACRO_GNU_transparent_include in "
18806 ".debug_macro section"));
18807 }
18808 else
18809 {
a036ba48 18810 *slot = new_mac_ptr;
36586728 18811
a036ba48 18812 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18813 include_mac_end, current_file,
8fc3fc34 18814 lh, comp_dir,
36586728 18815 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18816 offset_size, objfile, include_hash);
18817
a036ba48 18818 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18819 }
cf2c3c16
TT
18820 }
18821 break;
18822
2e276125 18823 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18824 if (!section_is_gnu)
18825 {
18826 unsigned int bytes_read;
18827 int constant;
2e276125 18828
cf2c3c16
TT
18829 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18830 mac_ptr += bytes_read;
18831 read_direct_string (abfd, mac_ptr, &bytes_read);
18832 mac_ptr += bytes_read;
2e276125 18833
cf2c3c16
TT
18834 /* We don't recognize any vendor extensions. */
18835 break;
18836 }
18837 /* FALLTHROUGH */
18838
18839 default:
18840 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18841 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18842 section);
18843 if (mac_ptr == NULL)
18844 return;
18845 break;
2e276125 18846 }
757a13d0 18847 } while (macinfo_type != 0);
2e276125 18848}
8e19ed76 18849
cf2c3c16 18850static void
09262596 18851dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18852 const char *comp_dir, int section_is_gnu)
cf2c3c16 18853{
bb5ed363 18854 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18855 struct line_header *lh = cu->line_header;
18856 bfd *abfd;
cf2c3c16
TT
18857 gdb_byte *mac_ptr, *mac_end;
18858 struct macro_source_file *current_file = 0;
18859 enum dwarf_macro_record_type macinfo_type;
18860 unsigned int offset_size = cu->header.offset_size;
18861 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18862 struct cleanup *cleanup;
18863 htab_t include_hash;
18864 void **slot;
09262596
DE
18865 struct dwarf2_section_info *section;
18866 const char *section_name;
18867
18868 if (cu->dwo_unit != NULL)
18869 {
18870 if (section_is_gnu)
18871 {
18872 section = &cu->dwo_unit->dwo_file->sections.macro;
18873 section_name = ".debug_macro.dwo";
18874 }
18875 else
18876 {
18877 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18878 section_name = ".debug_macinfo.dwo";
18879 }
18880 }
18881 else
18882 {
18883 if (section_is_gnu)
18884 {
18885 section = &dwarf2_per_objfile->macro;
18886 section_name = ".debug_macro";
18887 }
18888 else
18889 {
18890 section = &dwarf2_per_objfile->macinfo;
18891 section_name = ".debug_macinfo";
18892 }
18893 }
cf2c3c16 18894
bb5ed363 18895 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18896 if (section->buffer == NULL)
18897 {
fceca515 18898 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18899 return;
18900 }
09262596 18901 abfd = section->asection->owner;
cf2c3c16
TT
18902
18903 /* First pass: Find the name of the base filename.
18904 This filename is needed in order to process all macros whose definition
18905 (or undefinition) comes from the command line. These macros are defined
18906 before the first DW_MACINFO_start_file entry, and yet still need to be
18907 associated to the base file.
18908
18909 To determine the base file name, we scan the macro definitions until we
18910 reach the first DW_MACINFO_start_file entry. We then initialize
18911 CURRENT_FILE accordingly so that any macro definition found before the
18912 first DW_MACINFO_start_file can still be associated to the base file. */
18913
18914 mac_ptr = section->buffer + offset;
18915 mac_end = section->buffer + section->size;
18916
18917 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18918 &offset_size, section_is_gnu);
18919 if (mac_ptr == NULL)
18920 {
18921 /* We already issued a complaint. */
18922 return;
18923 }
18924
18925 do
18926 {
18927 /* Do we at least have room for a macinfo type byte? */
18928 if (mac_ptr >= mac_end)
18929 {
18930 /* Complaint is printed during the second pass as GDB will probably
18931 stop the first pass earlier upon finding
18932 DW_MACINFO_start_file. */
18933 break;
18934 }
18935
18936 macinfo_type = read_1_byte (abfd, mac_ptr);
18937 mac_ptr++;
18938
18939 /* Note that we rely on the fact that the corresponding GNU and
18940 DWARF constants are the same. */
18941 switch (macinfo_type)
18942 {
18943 /* A zero macinfo type indicates the end of the macro
18944 information. */
18945 case 0:
18946 break;
18947
18948 case DW_MACRO_GNU_define:
18949 case DW_MACRO_GNU_undef:
18950 /* Only skip the data by MAC_PTR. */
18951 {
18952 unsigned int bytes_read;
18953
18954 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18955 mac_ptr += bytes_read;
18956 read_direct_string (abfd, mac_ptr, &bytes_read);
18957 mac_ptr += bytes_read;
18958 }
18959 break;
18960
18961 case DW_MACRO_GNU_start_file:
18962 {
18963 unsigned int bytes_read;
18964 int line, file;
18965
18966 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18967 mac_ptr += bytes_read;
18968 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18969 mac_ptr += bytes_read;
18970
18971 current_file = macro_start_file (file, line, current_file,
bb5ed363 18972 comp_dir, lh, objfile);
cf2c3c16
TT
18973 }
18974 break;
18975
18976 case DW_MACRO_GNU_end_file:
18977 /* No data to skip by MAC_PTR. */
18978 break;
18979
18980 case DW_MACRO_GNU_define_indirect:
18981 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18982 case DW_MACRO_GNU_define_indirect_alt:
18983 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18984 {
18985 unsigned int bytes_read;
18986
18987 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18988 mac_ptr += bytes_read;
18989 mac_ptr += offset_size;
18990 }
18991 break;
18992
18993 case DW_MACRO_GNU_transparent_include:
f7a35f02 18994 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18995 /* Note that, according to the spec, a transparent include
18996 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18997 skip this opcode. */
18998 mac_ptr += offset_size;
18999 break;
19000
19001 case DW_MACINFO_vendor_ext:
19002 /* Only skip the data by MAC_PTR. */
19003 if (!section_is_gnu)
19004 {
19005 unsigned int bytes_read;
19006
19007 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19008 mac_ptr += bytes_read;
19009 read_direct_string (abfd, mac_ptr, &bytes_read);
19010 mac_ptr += bytes_read;
19011 }
19012 /* FALLTHROUGH */
19013
19014 default:
19015 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19016 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19017 section);
19018 if (mac_ptr == NULL)
19019 return;
19020 break;
19021 }
19022 } while (macinfo_type != 0 && current_file == NULL);
19023
19024 /* Second pass: Process all entries.
19025
19026 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19027 command-line macro definitions/undefinitions. This flag is unset when we
19028 reach the first DW_MACINFO_start_file entry. */
19029
8fc3fc34
TT
19030 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19031 NULL, xcalloc, xfree);
19032 cleanup = make_cleanup_htab_delete (include_hash);
19033 mac_ptr = section->buffer + offset;
19034 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19035 *slot = mac_ptr;
19036 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19037 current_file, lh, comp_dir, section,
19038 section_is_gnu, 0,
8fc3fc34
TT
19039 offset_size, objfile, include_hash);
19040 do_cleanups (cleanup);
cf2c3c16
TT
19041}
19042
8e19ed76 19043/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19044 if so return true else false. */
380bca97 19045
8e19ed76
PS
19046static int
19047attr_form_is_block (struct attribute *attr)
19048{
19049 return (attr == NULL ? 0 :
19050 attr->form == DW_FORM_block1
19051 || attr->form == DW_FORM_block2
19052 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19053 || attr->form == DW_FORM_block
19054 || attr->form == DW_FORM_exprloc);
8e19ed76 19055}
4c2df51b 19056
c6a0999f
JB
19057/* Return non-zero if ATTR's value is a section offset --- classes
19058 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19059 You may use DW_UNSND (attr) to retrieve such offsets.
19060
19061 Section 7.5.4, "Attribute Encodings", explains that no attribute
19062 may have a value that belongs to more than one of these classes; it
19063 would be ambiguous if we did, because we use the same forms for all
19064 of them. */
380bca97 19065
3690dd37
JB
19066static int
19067attr_form_is_section_offset (struct attribute *attr)
19068{
19069 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19070 || attr->form == DW_FORM_data8
19071 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19072}
19073
3690dd37
JB
19074/* Return non-zero if ATTR's value falls in the 'constant' class, or
19075 zero otherwise. When this function returns true, you can apply
19076 dwarf2_get_attr_constant_value to it.
19077
19078 However, note that for some attributes you must check
19079 attr_form_is_section_offset before using this test. DW_FORM_data4
19080 and DW_FORM_data8 are members of both the constant class, and of
19081 the classes that contain offsets into other debug sections
19082 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19083 that, if an attribute's can be either a constant or one of the
19084 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19085 taken as section offsets, not constants. */
380bca97 19086
3690dd37
JB
19087static int
19088attr_form_is_constant (struct attribute *attr)
19089{
19090 switch (attr->form)
19091 {
19092 case DW_FORM_sdata:
19093 case DW_FORM_udata:
19094 case DW_FORM_data1:
19095 case DW_FORM_data2:
19096 case DW_FORM_data4:
19097 case DW_FORM_data8:
19098 return 1;
19099 default:
19100 return 0;
19101 }
19102}
19103
3019eac3
DE
19104/* Return the .debug_loc section to use for CU.
19105 For DWO files use .debug_loc.dwo. */
19106
19107static struct dwarf2_section_info *
19108cu_debug_loc_section (struct dwarf2_cu *cu)
19109{
19110 if (cu->dwo_unit)
19111 return &cu->dwo_unit->dwo_file->sections.loc;
19112 return &dwarf2_per_objfile->loc;
19113}
19114
8cf6f0b1
TT
19115/* A helper function that fills in a dwarf2_loclist_baton. */
19116
19117static void
19118fill_in_loclist_baton (struct dwarf2_cu *cu,
19119 struct dwarf2_loclist_baton *baton,
19120 struct attribute *attr)
19121{
3019eac3
DE
19122 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19123
19124 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19125
19126 baton->per_cu = cu->per_cu;
19127 gdb_assert (baton->per_cu);
19128 /* We don't know how long the location list is, but make sure we
19129 don't run off the edge of the section. */
3019eac3
DE
19130 baton->size = section->size - DW_UNSND (attr);
19131 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19132 baton->base_address = cu->base_address;
f664829e 19133 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19134}
19135
4c2df51b
DJ
19136static void
19137dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19138 struct dwarf2_cu *cu, int is_block)
4c2df51b 19139{
bb5ed363 19140 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19141 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19142
3690dd37 19143 if (attr_form_is_section_offset (attr)
3019eac3 19144 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19145 the section. If so, fall through to the complaint in the
19146 other branch. */
3019eac3 19147 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19148 {
0d53c4c4 19149 struct dwarf2_loclist_baton *baton;
4c2df51b 19150
bb5ed363 19151 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19152 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19153
8cf6f0b1 19154 fill_in_loclist_baton (cu, baton, attr);
be391dca 19155
d00adf39 19156 if (cu->base_known == 0)
0d53c4c4 19157 complaint (&symfile_complaints,
3e43a32a
MS
19158 _("Location list used without "
19159 "specifying the CU base address."));
4c2df51b 19160
f1e6e072
TT
19161 SYMBOL_ACLASS_INDEX (sym) = (is_block
19162 ? dwarf2_loclist_block_index
19163 : dwarf2_loclist_index);
0d53c4c4
DJ
19164 SYMBOL_LOCATION_BATON (sym) = baton;
19165 }
19166 else
19167 {
19168 struct dwarf2_locexpr_baton *baton;
19169
bb5ed363 19170 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19171 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19172 baton->per_cu = cu->per_cu;
19173 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19174
19175 if (attr_form_is_block (attr))
19176 {
19177 /* Note that we're just copying the block's data pointer
19178 here, not the actual data. We're still pointing into the
6502dd73
DJ
19179 info_buffer for SYM's objfile; right now we never release
19180 that buffer, but when we do clean up properly this may
19181 need to change. */
0d53c4c4
DJ
19182 baton->size = DW_BLOCK (attr)->size;
19183 baton->data = DW_BLOCK (attr)->data;
19184 }
19185 else
19186 {
19187 dwarf2_invalid_attrib_class_complaint ("location description",
19188 SYMBOL_NATURAL_NAME (sym));
19189 baton->size = 0;
0d53c4c4 19190 }
6e70227d 19191
f1e6e072
TT
19192 SYMBOL_ACLASS_INDEX (sym) = (is_block
19193 ? dwarf2_locexpr_block_index
19194 : dwarf2_locexpr_index);
0d53c4c4
DJ
19195 SYMBOL_LOCATION_BATON (sym) = baton;
19196 }
4c2df51b 19197}
6502dd73 19198
9aa1f1e3
TT
19199/* Return the OBJFILE associated with the compilation unit CU. If CU
19200 came from a separate debuginfo file, then the master objfile is
19201 returned. */
ae0d2f24
UW
19202
19203struct objfile *
19204dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19205{
9291a0cd 19206 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19207
19208 /* Return the master objfile, so that we can report and look up the
19209 correct file containing this variable. */
19210 if (objfile->separate_debug_objfile_backlink)
19211 objfile = objfile->separate_debug_objfile_backlink;
19212
19213 return objfile;
19214}
19215
96408a79
SA
19216/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19217 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19218 CU_HEADERP first. */
19219
19220static const struct comp_unit_head *
19221per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19222 struct dwarf2_per_cu_data *per_cu)
19223{
96408a79
SA
19224 gdb_byte *info_ptr;
19225
19226 if (per_cu->cu)
19227 return &per_cu->cu->header;
19228
8a0459fd 19229 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19230
19231 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19232 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19233
19234 return cu_headerp;
19235}
19236
ae0d2f24
UW
19237/* Return the address size given in the compilation unit header for CU. */
19238
98714339 19239int
ae0d2f24
UW
19240dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19241{
96408a79
SA
19242 struct comp_unit_head cu_header_local;
19243 const struct comp_unit_head *cu_headerp;
c471e790 19244
96408a79
SA
19245 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19246
19247 return cu_headerp->addr_size;
ae0d2f24
UW
19248}
19249
9eae7c52
TT
19250/* Return the offset size given in the compilation unit header for CU. */
19251
19252int
19253dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19254{
96408a79
SA
19255 struct comp_unit_head cu_header_local;
19256 const struct comp_unit_head *cu_headerp;
9c6c53f7 19257
96408a79
SA
19258 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19259
19260 return cu_headerp->offset_size;
19261}
19262
19263/* See its dwarf2loc.h declaration. */
19264
19265int
19266dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19267{
19268 struct comp_unit_head cu_header_local;
19269 const struct comp_unit_head *cu_headerp;
19270
19271 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19272
19273 if (cu_headerp->version == 2)
19274 return cu_headerp->addr_size;
19275 else
19276 return cu_headerp->offset_size;
181cebd4
JK
19277}
19278
9aa1f1e3
TT
19279/* Return the text offset of the CU. The returned offset comes from
19280 this CU's objfile. If this objfile came from a separate debuginfo
19281 file, then the offset may be different from the corresponding
19282 offset in the parent objfile. */
19283
19284CORE_ADDR
19285dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19286{
bb3fa9d0 19287 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19288
19289 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19290}
19291
348e048f
DE
19292/* Locate the .debug_info compilation unit from CU's objfile which contains
19293 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19294
19295static struct dwarf2_per_cu_data *
b64f50a1 19296dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19297 unsigned int offset_in_dwz,
ae038cb0
DJ
19298 struct objfile *objfile)
19299{
19300 struct dwarf2_per_cu_data *this_cu;
19301 int low, high;
36586728 19302 const sect_offset *cu_off;
ae038cb0 19303
ae038cb0
DJ
19304 low = 0;
19305 high = dwarf2_per_objfile->n_comp_units - 1;
19306 while (high > low)
19307 {
36586728 19308 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19309 int mid = low + (high - low) / 2;
9a619af0 19310
36586728
TT
19311 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19312 cu_off = &mid_cu->offset;
19313 if (mid_cu->is_dwz > offset_in_dwz
19314 || (mid_cu->is_dwz == offset_in_dwz
19315 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19316 high = mid;
19317 else
19318 low = mid + 1;
19319 }
19320 gdb_assert (low == high);
36586728
TT
19321 this_cu = dwarf2_per_objfile->all_comp_units[low];
19322 cu_off = &this_cu->offset;
19323 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19324 {
36586728 19325 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19326 error (_("Dwarf Error: could not find partial DIE containing "
19327 "offset 0x%lx [in module %s]"),
b64f50a1 19328 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19329
b64f50a1
JK
19330 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19331 <= offset.sect_off);
ae038cb0
DJ
19332 return dwarf2_per_objfile->all_comp_units[low-1];
19333 }
19334 else
19335 {
19336 this_cu = dwarf2_per_objfile->all_comp_units[low];
19337 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19338 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19339 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19340 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19341 return this_cu;
19342 }
19343}
19344
23745b47 19345/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19346
9816fde3 19347static void
23745b47 19348init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19349{
9816fde3 19350 memset (cu, 0, sizeof (*cu));
23745b47
DE
19351 per_cu->cu = cu;
19352 cu->per_cu = per_cu;
19353 cu->objfile = per_cu->objfile;
93311388 19354 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19355}
19356
19357/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19358
19359static void
95554aad
TT
19360prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19361 enum language pretend_language)
9816fde3
JK
19362{
19363 struct attribute *attr;
19364
19365 /* Set the language we're debugging. */
19366 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19367 if (attr)
19368 set_cu_language (DW_UNSND (attr), cu);
19369 else
9cded63f 19370 {
95554aad 19371 cu->language = pretend_language;
9cded63f
TT
19372 cu->language_defn = language_def (cu->language);
19373 }
dee91e82
DE
19374
19375 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19376 if (attr)
19377 cu->producer = DW_STRING (attr);
93311388
DE
19378}
19379
ae038cb0
DJ
19380/* Release one cached compilation unit, CU. We unlink it from the tree
19381 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19382 the caller is responsible for that.
19383 NOTE: DATA is a void * because this function is also used as a
19384 cleanup routine. */
ae038cb0
DJ
19385
19386static void
68dc6402 19387free_heap_comp_unit (void *data)
ae038cb0
DJ
19388{
19389 struct dwarf2_cu *cu = data;
19390
23745b47
DE
19391 gdb_assert (cu->per_cu != NULL);
19392 cu->per_cu->cu = NULL;
ae038cb0
DJ
19393 cu->per_cu = NULL;
19394
19395 obstack_free (&cu->comp_unit_obstack, NULL);
19396
19397 xfree (cu);
19398}
19399
72bf9492 19400/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19401 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19402 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19403
19404static void
19405free_stack_comp_unit (void *data)
19406{
19407 struct dwarf2_cu *cu = data;
19408
23745b47
DE
19409 gdb_assert (cu->per_cu != NULL);
19410 cu->per_cu->cu = NULL;
19411 cu->per_cu = NULL;
19412
72bf9492
DJ
19413 obstack_free (&cu->comp_unit_obstack, NULL);
19414 cu->partial_dies = NULL;
ae038cb0
DJ
19415}
19416
19417/* Free all cached compilation units. */
19418
19419static void
19420free_cached_comp_units (void *data)
19421{
19422 struct dwarf2_per_cu_data *per_cu, **last_chain;
19423
19424 per_cu = dwarf2_per_objfile->read_in_chain;
19425 last_chain = &dwarf2_per_objfile->read_in_chain;
19426 while (per_cu != NULL)
19427 {
19428 struct dwarf2_per_cu_data *next_cu;
19429
19430 next_cu = per_cu->cu->read_in_chain;
19431
68dc6402 19432 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19433 *last_chain = next_cu;
19434
19435 per_cu = next_cu;
19436 }
19437}
19438
19439/* Increase the age counter on each cached compilation unit, and free
19440 any that are too old. */
19441
19442static void
19443age_cached_comp_units (void)
19444{
19445 struct dwarf2_per_cu_data *per_cu, **last_chain;
19446
19447 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19448 per_cu = dwarf2_per_objfile->read_in_chain;
19449 while (per_cu != NULL)
19450 {
19451 per_cu->cu->last_used ++;
19452 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19453 dwarf2_mark (per_cu->cu);
19454 per_cu = per_cu->cu->read_in_chain;
19455 }
19456
19457 per_cu = dwarf2_per_objfile->read_in_chain;
19458 last_chain = &dwarf2_per_objfile->read_in_chain;
19459 while (per_cu != NULL)
19460 {
19461 struct dwarf2_per_cu_data *next_cu;
19462
19463 next_cu = per_cu->cu->read_in_chain;
19464
19465 if (!per_cu->cu->mark)
19466 {
68dc6402 19467 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19468 *last_chain = next_cu;
19469 }
19470 else
19471 last_chain = &per_cu->cu->read_in_chain;
19472
19473 per_cu = next_cu;
19474 }
19475}
19476
19477/* Remove a single compilation unit from the cache. */
19478
19479static void
dee91e82 19480free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19481{
19482 struct dwarf2_per_cu_data *per_cu, **last_chain;
19483
19484 per_cu = dwarf2_per_objfile->read_in_chain;
19485 last_chain = &dwarf2_per_objfile->read_in_chain;
19486 while (per_cu != NULL)
19487 {
19488 struct dwarf2_per_cu_data *next_cu;
19489
19490 next_cu = per_cu->cu->read_in_chain;
19491
dee91e82 19492 if (per_cu == target_per_cu)
ae038cb0 19493 {
68dc6402 19494 free_heap_comp_unit (per_cu->cu);
dee91e82 19495 per_cu->cu = NULL;
ae038cb0
DJ
19496 *last_chain = next_cu;
19497 break;
19498 }
19499 else
19500 last_chain = &per_cu->cu->read_in_chain;
19501
19502 per_cu = next_cu;
19503 }
19504}
19505
fe3e1990
DJ
19506/* Release all extra memory associated with OBJFILE. */
19507
19508void
19509dwarf2_free_objfile (struct objfile *objfile)
19510{
19511 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19512
19513 if (dwarf2_per_objfile == NULL)
19514 return;
19515
19516 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19517 free_cached_comp_units (NULL);
19518
7b9f3c50
DE
19519 if (dwarf2_per_objfile->quick_file_names_table)
19520 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19521
fe3e1990
DJ
19522 /* Everything else should be on the objfile obstack. */
19523}
19524
dee91e82
DE
19525/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19526 We store these in a hash table separate from the DIEs, and preserve them
19527 when the DIEs are flushed out of cache.
19528
19529 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 19530 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
19531 or the type may come from a DWO file. Furthermore, while it's more logical
19532 to use per_cu->section+offset, with Fission the section with the data is in
19533 the DWO file but we don't know that section at the point we need it.
19534 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19535 because we can enter the lookup routine, get_die_type_at_offset, from
19536 outside this file, and thus won't necessarily have PER_CU->cu.
19537 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 19538
dee91e82 19539struct dwarf2_per_cu_offset_and_type
1c379e20 19540{
dee91e82 19541 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19542 sect_offset offset;
1c379e20
DJ
19543 struct type *type;
19544};
19545
dee91e82 19546/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19547
19548static hashval_t
dee91e82 19549per_cu_offset_and_type_hash (const void *item)
1c379e20 19550{
dee91e82 19551 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19552
dee91e82 19553 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19554}
19555
dee91e82 19556/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19557
19558static int
dee91e82 19559per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19560{
dee91e82
DE
19561 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19562 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19563
dee91e82
DE
19564 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19565 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19566}
19567
19568/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19569 table if necessary. For convenience, return TYPE.
19570
19571 The DIEs reading must have careful ordering to:
19572 * Not cause infite loops trying to read in DIEs as a prerequisite for
19573 reading current DIE.
19574 * Not trying to dereference contents of still incompletely read in types
19575 while reading in other DIEs.
19576 * Enable referencing still incompletely read in types just by a pointer to
19577 the type without accessing its fields.
19578
19579 Therefore caller should follow these rules:
19580 * Try to fetch any prerequisite types we may need to build this DIE type
19581 before building the type and calling set_die_type.
e71ec853 19582 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19583 possible before fetching more types to complete the current type.
19584 * Make the type as complete as possible before fetching more types. */
1c379e20 19585
f792889a 19586static struct type *
1c379e20
DJ
19587set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19588{
dee91e82 19589 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19590 struct objfile *objfile = cu->objfile;
1c379e20 19591
b4ba55a1
JB
19592 /* For Ada types, make sure that the gnat-specific data is always
19593 initialized (if not already set). There are a few types where
19594 we should not be doing so, because the type-specific area is
19595 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19596 where the type-specific area is used to store the floatformat).
19597 But this is not a problem, because the gnat-specific information
19598 is actually not needed for these types. */
19599 if (need_gnat_info (cu)
19600 && TYPE_CODE (type) != TYPE_CODE_FUNC
19601 && TYPE_CODE (type) != TYPE_CODE_FLT
19602 && !HAVE_GNAT_AUX_INFO (type))
19603 INIT_GNAT_SPECIFIC (type);
19604
dee91e82 19605 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19606 {
dee91e82
DE
19607 dwarf2_per_objfile->die_type_hash =
19608 htab_create_alloc_ex (127,
19609 per_cu_offset_and_type_hash,
19610 per_cu_offset_and_type_eq,
19611 NULL,
19612 &objfile->objfile_obstack,
19613 hashtab_obstack_allocate,
19614 dummy_obstack_deallocate);
f792889a 19615 }
1c379e20 19616
dee91e82 19617 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19618 ofs.offset = die->offset;
19619 ofs.type = type;
dee91e82
DE
19620 slot = (struct dwarf2_per_cu_offset_and_type **)
19621 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19622 if (*slot)
19623 complaint (&symfile_complaints,
19624 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19625 die->offset.sect_off);
673bfd45 19626 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19627 **slot = ofs;
f792889a 19628 return type;
1c379e20
DJ
19629}
19630
380bca97 19631/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19632 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19633
19634static struct type *
b64f50a1 19635get_die_type_at_offset (sect_offset offset,
673bfd45 19636 struct dwarf2_per_cu_data *per_cu)
1c379e20 19637{
dee91e82 19638 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19639
dee91e82 19640 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19641 return NULL;
1c379e20 19642
dee91e82 19643 ofs.per_cu = per_cu;
673bfd45 19644 ofs.offset = offset;
dee91e82 19645 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19646 if (slot)
19647 return slot->type;
19648 else
19649 return NULL;
19650}
19651
673bfd45
DE
19652/* Look up the type for DIE in the appropriate type_hash table,
19653 or return NULL if DIE does not have a saved type. */
19654
19655static struct type *
19656get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19657{
19658 return get_die_type_at_offset (die->offset, cu->per_cu);
19659}
19660
10b3939b
DJ
19661/* Add a dependence relationship from CU to REF_PER_CU. */
19662
19663static void
19664dwarf2_add_dependence (struct dwarf2_cu *cu,
19665 struct dwarf2_per_cu_data *ref_per_cu)
19666{
19667 void **slot;
19668
19669 if (cu->dependencies == NULL)
19670 cu->dependencies
19671 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19672 NULL, &cu->comp_unit_obstack,
19673 hashtab_obstack_allocate,
19674 dummy_obstack_deallocate);
19675
19676 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19677 if (*slot == NULL)
19678 *slot = ref_per_cu;
19679}
1c379e20 19680
f504f079
DE
19681/* Subroutine of dwarf2_mark to pass to htab_traverse.
19682 Set the mark field in every compilation unit in the
ae038cb0
DJ
19683 cache that we must keep because we are keeping CU. */
19684
10b3939b
DJ
19685static int
19686dwarf2_mark_helper (void **slot, void *data)
19687{
19688 struct dwarf2_per_cu_data *per_cu;
19689
19690 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19691
19692 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19693 reading of the chain. As such dependencies remain valid it is not much
19694 useful to track and undo them during QUIT cleanups. */
19695 if (per_cu->cu == NULL)
19696 return 1;
19697
10b3939b
DJ
19698 if (per_cu->cu->mark)
19699 return 1;
19700 per_cu->cu->mark = 1;
19701
19702 if (per_cu->cu->dependencies != NULL)
19703 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19704
19705 return 1;
19706}
19707
f504f079
DE
19708/* Set the mark field in CU and in every other compilation unit in the
19709 cache that we must keep because we are keeping CU. */
19710
ae038cb0
DJ
19711static void
19712dwarf2_mark (struct dwarf2_cu *cu)
19713{
19714 if (cu->mark)
19715 return;
19716 cu->mark = 1;
10b3939b
DJ
19717 if (cu->dependencies != NULL)
19718 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19719}
19720
19721static void
19722dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19723{
19724 while (per_cu)
19725 {
19726 per_cu->cu->mark = 0;
19727 per_cu = per_cu->cu->read_in_chain;
19728 }
72bf9492
DJ
19729}
19730
72bf9492
DJ
19731/* Trivial hash function for partial_die_info: the hash value of a DIE
19732 is its offset in .debug_info for this objfile. */
19733
19734static hashval_t
19735partial_die_hash (const void *item)
19736{
19737 const struct partial_die_info *part_die = item;
9a619af0 19738
b64f50a1 19739 return part_die->offset.sect_off;
72bf9492
DJ
19740}
19741
19742/* Trivial comparison function for partial_die_info structures: two DIEs
19743 are equal if they have the same offset. */
19744
19745static int
19746partial_die_eq (const void *item_lhs, const void *item_rhs)
19747{
19748 const struct partial_die_info *part_die_lhs = item_lhs;
19749 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19750
b64f50a1 19751 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19752}
19753
ae038cb0
DJ
19754static struct cmd_list_element *set_dwarf2_cmdlist;
19755static struct cmd_list_element *show_dwarf2_cmdlist;
19756
19757static void
19758set_dwarf2_cmd (char *args, int from_tty)
19759{
19760 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19761}
19762
19763static void
19764show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19765{
ae038cb0
DJ
19766 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19767}
19768
4bf44c1c 19769/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19770
19771static void
c1bd65d0 19772dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19773{
19774 struct dwarf2_per_objfile *data = d;
8b70b953 19775 int ix;
8b70b953 19776
95554aad
TT
19777 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19778 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19779 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19780
19781 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19782 VEC_free (dwarf2_per_cu_ptr,
19783 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19784
8b70b953 19785 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19786
19787 if (data->dwo_files)
19788 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
19789 if (data->dwp_file)
19790 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
19791
19792 if (data->dwz_file && data->dwz_file->dwz_bfd)
19793 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19794}
19795
19796\f
ae2de4f8 19797/* The "save gdb-index" command. */
9291a0cd
TT
19798
19799/* The contents of the hash table we create when building the string
19800 table. */
19801struct strtab_entry
19802{
19803 offset_type offset;
19804 const char *str;
19805};
19806
559a7a62
JK
19807/* Hash function for a strtab_entry.
19808
19809 Function is used only during write_hash_table so no index format backward
19810 compatibility is needed. */
b89be57b 19811
9291a0cd
TT
19812static hashval_t
19813hash_strtab_entry (const void *e)
19814{
19815 const struct strtab_entry *entry = e;
559a7a62 19816 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19817}
19818
19819/* Equality function for a strtab_entry. */
b89be57b 19820
9291a0cd
TT
19821static int
19822eq_strtab_entry (const void *a, const void *b)
19823{
19824 const struct strtab_entry *ea = a;
19825 const struct strtab_entry *eb = b;
19826 return !strcmp (ea->str, eb->str);
19827}
19828
19829/* Create a strtab_entry hash table. */
b89be57b 19830
9291a0cd
TT
19831static htab_t
19832create_strtab (void)
19833{
19834 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19835 xfree, xcalloc, xfree);
19836}
19837
19838/* Add a string to the constant pool. Return the string's offset in
19839 host order. */
b89be57b 19840
9291a0cd
TT
19841static offset_type
19842add_string (htab_t table, struct obstack *cpool, const char *str)
19843{
19844 void **slot;
19845 struct strtab_entry entry;
19846 struct strtab_entry *result;
19847
19848 entry.str = str;
19849 slot = htab_find_slot (table, &entry, INSERT);
19850 if (*slot)
19851 result = *slot;
19852 else
19853 {
19854 result = XNEW (struct strtab_entry);
19855 result->offset = obstack_object_size (cpool);
19856 result->str = str;
19857 obstack_grow_str0 (cpool, str);
19858 *slot = result;
19859 }
19860 return result->offset;
19861}
19862
19863/* An entry in the symbol table. */
19864struct symtab_index_entry
19865{
19866 /* The name of the symbol. */
19867 const char *name;
19868 /* The offset of the name in the constant pool. */
19869 offset_type index_offset;
19870 /* A sorted vector of the indices of all the CUs that hold an object
19871 of this name. */
19872 VEC (offset_type) *cu_indices;
19873};
19874
19875/* The symbol table. This is a power-of-2-sized hash table. */
19876struct mapped_symtab
19877{
19878 offset_type n_elements;
19879 offset_type size;
19880 struct symtab_index_entry **data;
19881};
19882
19883/* Hash function for a symtab_index_entry. */
b89be57b 19884
9291a0cd
TT
19885static hashval_t
19886hash_symtab_entry (const void *e)
19887{
19888 const struct symtab_index_entry *entry = e;
19889 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19890 sizeof (offset_type) * VEC_length (offset_type,
19891 entry->cu_indices),
19892 0);
19893}
19894
19895/* Equality function for a symtab_index_entry. */
b89be57b 19896
9291a0cd
TT
19897static int
19898eq_symtab_entry (const void *a, const void *b)
19899{
19900 const struct symtab_index_entry *ea = a;
19901 const struct symtab_index_entry *eb = b;
19902 int len = VEC_length (offset_type, ea->cu_indices);
19903 if (len != VEC_length (offset_type, eb->cu_indices))
19904 return 0;
19905 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19906 VEC_address (offset_type, eb->cu_indices),
19907 sizeof (offset_type) * len);
19908}
19909
19910/* Destroy a symtab_index_entry. */
b89be57b 19911
9291a0cd
TT
19912static void
19913delete_symtab_entry (void *p)
19914{
19915 struct symtab_index_entry *entry = p;
19916 VEC_free (offset_type, entry->cu_indices);
19917 xfree (entry);
19918}
19919
19920/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19921
9291a0cd 19922static htab_t
3876f04e 19923create_symbol_hash_table (void)
9291a0cd
TT
19924{
19925 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19926 delete_symtab_entry, xcalloc, xfree);
19927}
19928
19929/* Create a new mapped symtab object. */
b89be57b 19930
9291a0cd
TT
19931static struct mapped_symtab *
19932create_mapped_symtab (void)
19933{
19934 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19935 symtab->n_elements = 0;
19936 symtab->size = 1024;
19937 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19938 return symtab;
19939}
19940
19941/* Destroy a mapped_symtab. */
b89be57b 19942
9291a0cd
TT
19943static void
19944cleanup_mapped_symtab (void *p)
19945{
19946 struct mapped_symtab *symtab = p;
19947 /* The contents of the array are freed when the other hash table is
19948 destroyed. */
19949 xfree (symtab->data);
19950 xfree (symtab);
19951}
19952
19953/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19954 the slot.
19955
19956 Function is used only during write_hash_table so no index format backward
19957 compatibility is needed. */
b89be57b 19958
9291a0cd
TT
19959static struct symtab_index_entry **
19960find_slot (struct mapped_symtab *symtab, const char *name)
19961{
559a7a62 19962 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19963
19964 index = hash & (symtab->size - 1);
19965 step = ((hash * 17) & (symtab->size - 1)) | 1;
19966
19967 for (;;)
19968 {
19969 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19970 return &symtab->data[index];
19971 index = (index + step) & (symtab->size - 1);
19972 }
19973}
19974
19975/* Expand SYMTAB's hash table. */
b89be57b 19976
9291a0cd
TT
19977static void
19978hash_expand (struct mapped_symtab *symtab)
19979{
19980 offset_type old_size = symtab->size;
19981 offset_type i;
19982 struct symtab_index_entry **old_entries = symtab->data;
19983
19984 symtab->size *= 2;
19985 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19986
19987 for (i = 0; i < old_size; ++i)
19988 {
19989 if (old_entries[i])
19990 {
19991 struct symtab_index_entry **slot = find_slot (symtab,
19992 old_entries[i]->name);
19993 *slot = old_entries[i];
19994 }
19995 }
19996
19997 xfree (old_entries);
19998}
19999
156942c7
DE
20000/* Add an entry to SYMTAB. NAME is the name of the symbol.
20001 CU_INDEX is the index of the CU in which the symbol appears.
20002 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20003
9291a0cd
TT
20004static void
20005add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20006 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20007 offset_type cu_index)
20008{
20009 struct symtab_index_entry **slot;
156942c7 20010 offset_type cu_index_and_attrs;
9291a0cd
TT
20011
20012 ++symtab->n_elements;
20013 if (4 * symtab->n_elements / 3 >= symtab->size)
20014 hash_expand (symtab);
20015
20016 slot = find_slot (symtab, name);
20017 if (!*slot)
20018 {
20019 *slot = XNEW (struct symtab_index_entry);
20020 (*slot)->name = name;
156942c7 20021 /* index_offset is set later. */
9291a0cd
TT
20022 (*slot)->cu_indices = NULL;
20023 }
156942c7
DE
20024
20025 cu_index_and_attrs = 0;
20026 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20027 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20028 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20029
20030 /* We don't want to record an index value twice as we want to avoid the
20031 duplication.
20032 We process all global symbols and then all static symbols
20033 (which would allow us to avoid the duplication by only having to check
20034 the last entry pushed), but a symbol could have multiple kinds in one CU.
20035 To keep things simple we don't worry about the duplication here and
20036 sort and uniqufy the list after we've processed all symbols. */
20037 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20038}
20039
20040/* qsort helper routine for uniquify_cu_indices. */
20041
20042static int
20043offset_type_compare (const void *ap, const void *bp)
20044{
20045 offset_type a = *(offset_type *) ap;
20046 offset_type b = *(offset_type *) bp;
20047
20048 return (a > b) - (b > a);
20049}
20050
20051/* Sort and remove duplicates of all symbols' cu_indices lists. */
20052
20053static void
20054uniquify_cu_indices (struct mapped_symtab *symtab)
20055{
20056 int i;
20057
20058 for (i = 0; i < symtab->size; ++i)
20059 {
20060 struct symtab_index_entry *entry = symtab->data[i];
20061
20062 if (entry
20063 && entry->cu_indices != NULL)
20064 {
20065 unsigned int next_to_insert, next_to_check;
20066 offset_type last_value;
20067
20068 qsort (VEC_address (offset_type, entry->cu_indices),
20069 VEC_length (offset_type, entry->cu_indices),
20070 sizeof (offset_type), offset_type_compare);
20071
20072 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20073 next_to_insert = 1;
20074 for (next_to_check = 1;
20075 next_to_check < VEC_length (offset_type, entry->cu_indices);
20076 ++next_to_check)
20077 {
20078 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20079 != last_value)
20080 {
20081 last_value = VEC_index (offset_type, entry->cu_indices,
20082 next_to_check);
20083 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20084 last_value);
20085 ++next_to_insert;
20086 }
20087 }
20088 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20089 }
20090 }
9291a0cd
TT
20091}
20092
20093/* Add a vector of indices to the constant pool. */
b89be57b 20094
9291a0cd 20095static offset_type
3876f04e 20096add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20097 struct symtab_index_entry *entry)
20098{
20099 void **slot;
20100
3876f04e 20101 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20102 if (!*slot)
20103 {
20104 offset_type len = VEC_length (offset_type, entry->cu_indices);
20105 offset_type val = MAYBE_SWAP (len);
20106 offset_type iter;
20107 int i;
20108
20109 *slot = entry;
20110 entry->index_offset = obstack_object_size (cpool);
20111
20112 obstack_grow (cpool, &val, sizeof (val));
20113 for (i = 0;
20114 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20115 ++i)
20116 {
20117 val = MAYBE_SWAP (iter);
20118 obstack_grow (cpool, &val, sizeof (val));
20119 }
20120 }
20121 else
20122 {
20123 struct symtab_index_entry *old_entry = *slot;
20124 entry->index_offset = old_entry->index_offset;
20125 entry = old_entry;
20126 }
20127 return entry->index_offset;
20128}
20129
20130/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20131 constant pool entries going into the obstack CPOOL. */
b89be57b 20132
9291a0cd
TT
20133static void
20134write_hash_table (struct mapped_symtab *symtab,
20135 struct obstack *output, struct obstack *cpool)
20136{
20137 offset_type i;
3876f04e 20138 htab_t symbol_hash_table;
9291a0cd
TT
20139 htab_t str_table;
20140
3876f04e 20141 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20142 str_table = create_strtab ();
3876f04e 20143
9291a0cd
TT
20144 /* We add all the index vectors to the constant pool first, to
20145 ensure alignment is ok. */
20146 for (i = 0; i < symtab->size; ++i)
20147 {
20148 if (symtab->data[i])
3876f04e 20149 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20150 }
20151
20152 /* Now write out the hash table. */
20153 for (i = 0; i < symtab->size; ++i)
20154 {
20155 offset_type str_off, vec_off;
20156
20157 if (symtab->data[i])
20158 {
20159 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20160 vec_off = symtab->data[i]->index_offset;
20161 }
20162 else
20163 {
20164 /* While 0 is a valid constant pool index, it is not valid
20165 to have 0 for both offsets. */
20166 str_off = 0;
20167 vec_off = 0;
20168 }
20169
20170 str_off = MAYBE_SWAP (str_off);
20171 vec_off = MAYBE_SWAP (vec_off);
20172
20173 obstack_grow (output, &str_off, sizeof (str_off));
20174 obstack_grow (output, &vec_off, sizeof (vec_off));
20175 }
20176
20177 htab_delete (str_table);
3876f04e 20178 htab_delete (symbol_hash_table);
9291a0cd
TT
20179}
20180
0a5429f6
DE
20181/* Struct to map psymtab to CU index in the index file. */
20182struct psymtab_cu_index_map
20183{
20184 struct partial_symtab *psymtab;
20185 unsigned int cu_index;
20186};
20187
20188static hashval_t
20189hash_psymtab_cu_index (const void *item)
20190{
20191 const struct psymtab_cu_index_map *map = item;
20192
20193 return htab_hash_pointer (map->psymtab);
20194}
20195
20196static int
20197eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20198{
20199 const struct psymtab_cu_index_map *lhs = item_lhs;
20200 const struct psymtab_cu_index_map *rhs = item_rhs;
20201
20202 return lhs->psymtab == rhs->psymtab;
20203}
20204
20205/* Helper struct for building the address table. */
20206struct addrmap_index_data
20207{
20208 struct objfile *objfile;
20209 struct obstack *addr_obstack;
20210 htab_t cu_index_htab;
20211
20212 /* Non-zero if the previous_* fields are valid.
20213 We can't write an entry until we see the next entry (since it is only then
20214 that we know the end of the entry). */
20215 int previous_valid;
20216 /* Index of the CU in the table of all CUs in the index file. */
20217 unsigned int previous_cu_index;
0963b4bd 20218 /* Start address of the CU. */
0a5429f6
DE
20219 CORE_ADDR previous_cu_start;
20220};
20221
20222/* Write an address entry to OBSTACK. */
b89be57b 20223
9291a0cd 20224static void
0a5429f6
DE
20225add_address_entry (struct objfile *objfile, struct obstack *obstack,
20226 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20227{
0a5429f6 20228 offset_type cu_index_to_write;
9291a0cd
TT
20229 char addr[8];
20230 CORE_ADDR baseaddr;
20231
20232 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20233
0a5429f6
DE
20234 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20235 obstack_grow (obstack, addr, 8);
20236 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20237 obstack_grow (obstack, addr, 8);
20238 cu_index_to_write = MAYBE_SWAP (cu_index);
20239 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20240}
20241
20242/* Worker function for traversing an addrmap to build the address table. */
20243
20244static int
20245add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20246{
20247 struct addrmap_index_data *data = datap;
20248 struct partial_symtab *pst = obj;
0a5429f6
DE
20249
20250 if (data->previous_valid)
20251 add_address_entry (data->objfile, data->addr_obstack,
20252 data->previous_cu_start, start_addr,
20253 data->previous_cu_index);
20254
20255 data->previous_cu_start = start_addr;
20256 if (pst != NULL)
20257 {
20258 struct psymtab_cu_index_map find_map, *map;
20259 find_map.psymtab = pst;
20260 map = htab_find (data->cu_index_htab, &find_map);
20261 gdb_assert (map != NULL);
20262 data->previous_cu_index = map->cu_index;
20263 data->previous_valid = 1;
20264 }
20265 else
20266 data->previous_valid = 0;
20267
20268 return 0;
20269}
20270
20271/* Write OBJFILE's address map to OBSTACK.
20272 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20273 in the index file. */
20274
20275static void
20276write_address_map (struct objfile *objfile, struct obstack *obstack,
20277 htab_t cu_index_htab)
20278{
20279 struct addrmap_index_data addrmap_index_data;
20280
20281 /* When writing the address table, we have to cope with the fact that
20282 the addrmap iterator only provides the start of a region; we have to
20283 wait until the next invocation to get the start of the next region. */
20284
20285 addrmap_index_data.objfile = objfile;
20286 addrmap_index_data.addr_obstack = obstack;
20287 addrmap_index_data.cu_index_htab = cu_index_htab;
20288 addrmap_index_data.previous_valid = 0;
20289
20290 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20291 &addrmap_index_data);
20292
20293 /* It's highly unlikely the last entry (end address = 0xff...ff)
20294 is valid, but we should still handle it.
20295 The end address is recorded as the start of the next region, but that
20296 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20297 anyway. */
20298 if (addrmap_index_data.previous_valid)
20299 add_address_entry (objfile, obstack,
20300 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20301 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20302}
20303
156942c7
DE
20304/* Return the symbol kind of PSYM. */
20305
20306static gdb_index_symbol_kind
20307symbol_kind (struct partial_symbol *psym)
20308{
20309 domain_enum domain = PSYMBOL_DOMAIN (psym);
20310 enum address_class aclass = PSYMBOL_CLASS (psym);
20311
20312 switch (domain)
20313 {
20314 case VAR_DOMAIN:
20315 switch (aclass)
20316 {
20317 case LOC_BLOCK:
20318 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20319 case LOC_TYPEDEF:
20320 return GDB_INDEX_SYMBOL_KIND_TYPE;
20321 case LOC_COMPUTED:
20322 case LOC_CONST_BYTES:
20323 case LOC_OPTIMIZED_OUT:
20324 case LOC_STATIC:
20325 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20326 case LOC_CONST:
20327 /* Note: It's currently impossible to recognize psyms as enum values
20328 short of reading the type info. For now punt. */
20329 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20330 default:
20331 /* There are other LOC_FOO values that one might want to classify
20332 as variables, but dwarf2read.c doesn't currently use them. */
20333 return GDB_INDEX_SYMBOL_KIND_OTHER;
20334 }
20335 case STRUCT_DOMAIN:
20336 return GDB_INDEX_SYMBOL_KIND_TYPE;
20337 default:
20338 return GDB_INDEX_SYMBOL_KIND_OTHER;
20339 }
20340}
20341
9291a0cd 20342/* Add a list of partial symbols to SYMTAB. */
b89be57b 20343
9291a0cd
TT
20344static void
20345write_psymbols (struct mapped_symtab *symtab,
987d643c 20346 htab_t psyms_seen,
9291a0cd
TT
20347 struct partial_symbol **psymp,
20348 int count,
987d643c
TT
20349 offset_type cu_index,
20350 int is_static)
9291a0cd
TT
20351{
20352 for (; count-- > 0; ++psymp)
20353 {
156942c7
DE
20354 struct partial_symbol *psym = *psymp;
20355 void **slot;
987d643c 20356
156942c7 20357 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20358 error (_("Ada is not currently supported by the index"));
987d643c 20359
987d643c 20360 /* Only add a given psymbol once. */
156942c7 20361 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20362 if (!*slot)
20363 {
156942c7
DE
20364 gdb_index_symbol_kind kind = symbol_kind (psym);
20365
20366 *slot = psym;
20367 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20368 is_static, kind, cu_index);
987d643c 20369 }
9291a0cd
TT
20370 }
20371}
20372
20373/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20374 exception if there is an error. */
b89be57b 20375
9291a0cd
TT
20376static void
20377write_obstack (FILE *file, struct obstack *obstack)
20378{
20379 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20380 file)
20381 != obstack_object_size (obstack))
20382 error (_("couldn't data write to file"));
20383}
20384
20385/* Unlink a file if the argument is not NULL. */
b89be57b 20386
9291a0cd
TT
20387static void
20388unlink_if_set (void *p)
20389{
20390 char **filename = p;
20391 if (*filename)
20392 unlink (*filename);
20393}
20394
1fd400ff
TT
20395/* A helper struct used when iterating over debug_types. */
20396struct signatured_type_index_data
20397{
20398 struct objfile *objfile;
20399 struct mapped_symtab *symtab;
20400 struct obstack *types_list;
987d643c 20401 htab_t psyms_seen;
1fd400ff
TT
20402 int cu_index;
20403};
20404
20405/* A helper function that writes a single signatured_type to an
20406 obstack. */
b89be57b 20407
1fd400ff
TT
20408static int
20409write_one_signatured_type (void **slot, void *d)
20410{
20411 struct signatured_type_index_data *info = d;
20412 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 20413 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
20414 gdb_byte val[8];
20415
20416 write_psymbols (info->symtab,
987d643c 20417 info->psyms_seen,
3e43a32a
MS
20418 info->objfile->global_psymbols.list
20419 + psymtab->globals_offset,
987d643c
TT
20420 psymtab->n_global_syms, info->cu_index,
20421 0);
1fd400ff 20422 write_psymbols (info->symtab,
987d643c 20423 info->psyms_seen,
3e43a32a
MS
20424 info->objfile->static_psymbols.list
20425 + psymtab->statics_offset,
987d643c
TT
20426 psymtab->n_static_syms, info->cu_index,
20427 1);
1fd400ff 20428
b64f50a1
JK
20429 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20430 entry->per_cu.offset.sect_off);
1fd400ff 20431 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20432 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20433 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20434 obstack_grow (info->types_list, val, 8);
20435 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20436 obstack_grow (info->types_list, val, 8);
20437
20438 ++info->cu_index;
20439
20440 return 1;
20441}
20442
95554aad
TT
20443/* Recurse into all "included" dependencies and write their symbols as
20444 if they appeared in this psymtab. */
20445
20446static void
20447recursively_write_psymbols (struct objfile *objfile,
20448 struct partial_symtab *psymtab,
20449 struct mapped_symtab *symtab,
20450 htab_t psyms_seen,
20451 offset_type cu_index)
20452{
20453 int i;
20454
20455 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20456 if (psymtab->dependencies[i]->user != NULL)
20457 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20458 symtab, psyms_seen, cu_index);
20459
20460 write_psymbols (symtab,
20461 psyms_seen,
20462 objfile->global_psymbols.list + psymtab->globals_offset,
20463 psymtab->n_global_syms, cu_index,
20464 0);
20465 write_psymbols (symtab,
20466 psyms_seen,
20467 objfile->static_psymbols.list + psymtab->statics_offset,
20468 psymtab->n_static_syms, cu_index,
20469 1);
20470}
20471
9291a0cd 20472/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20473
9291a0cd
TT
20474static void
20475write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20476{
20477 struct cleanup *cleanup;
20478 char *filename, *cleanup_filename;
1fd400ff
TT
20479 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20480 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20481 int i;
20482 FILE *out_file;
20483 struct mapped_symtab *symtab;
20484 offset_type val, size_of_contents, total_len;
20485 struct stat st;
987d643c 20486 htab_t psyms_seen;
0a5429f6
DE
20487 htab_t cu_index_htab;
20488 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20489
b4f2f049 20490 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20491 return;
b4f2f049 20492
9291a0cd
TT
20493 if (dwarf2_per_objfile->using_index)
20494 error (_("Cannot use an index to create the index"));
20495
8b70b953
TT
20496 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20497 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20498
9291a0cd 20499 if (stat (objfile->name, &st) < 0)
7e17e088 20500 perror_with_name (objfile->name);
9291a0cd
TT
20501
20502 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20503 INDEX_SUFFIX, (char *) NULL);
20504 cleanup = make_cleanup (xfree, filename);
20505
20506 out_file = fopen (filename, "wb");
20507 if (!out_file)
20508 error (_("Can't open `%s' for writing"), filename);
20509
20510 cleanup_filename = filename;
20511 make_cleanup (unlink_if_set, &cleanup_filename);
20512
20513 symtab = create_mapped_symtab ();
20514 make_cleanup (cleanup_mapped_symtab, symtab);
20515
20516 obstack_init (&addr_obstack);
20517 make_cleanup_obstack_free (&addr_obstack);
20518
20519 obstack_init (&cu_list);
20520 make_cleanup_obstack_free (&cu_list);
20521
1fd400ff
TT
20522 obstack_init (&types_cu_list);
20523 make_cleanup_obstack_free (&types_cu_list);
20524
987d643c
TT
20525 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20526 NULL, xcalloc, xfree);
96408a79 20527 make_cleanup_htab_delete (psyms_seen);
987d643c 20528
0a5429f6
DE
20529 /* While we're scanning CU's create a table that maps a psymtab pointer
20530 (which is what addrmap records) to its index (which is what is recorded
20531 in the index file). This will later be needed to write the address
20532 table. */
20533 cu_index_htab = htab_create_alloc (100,
20534 hash_psymtab_cu_index,
20535 eq_psymtab_cu_index,
20536 NULL, xcalloc, xfree);
96408a79 20537 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20538 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20539 xmalloc (sizeof (struct psymtab_cu_index_map)
20540 * dwarf2_per_objfile->n_comp_units);
20541 make_cleanup (xfree, psymtab_cu_index_map);
20542
20543 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20544 work here. Also, the debug_types entries do not appear in
20545 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20546 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20547 {
3e43a32a
MS
20548 struct dwarf2_per_cu_data *per_cu
20549 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20550 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20551 gdb_byte val[8];
0a5429f6
DE
20552 struct psymtab_cu_index_map *map;
20553 void **slot;
9291a0cd 20554
95554aad
TT
20555 if (psymtab->user == NULL)
20556 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20557
0a5429f6
DE
20558 map = &psymtab_cu_index_map[i];
20559 map->psymtab = psymtab;
20560 map->cu_index = i;
20561 slot = htab_find_slot (cu_index_htab, map, INSERT);
20562 gdb_assert (slot != NULL);
20563 gdb_assert (*slot == NULL);
20564 *slot = map;
9291a0cd 20565
b64f50a1
JK
20566 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20567 per_cu->offset.sect_off);
9291a0cd 20568 obstack_grow (&cu_list, val, 8);
e254ef6a 20569 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20570 obstack_grow (&cu_list, val, 8);
20571 }
20572
0a5429f6
DE
20573 /* Dump the address map. */
20574 write_address_map (objfile, &addr_obstack, cu_index_htab);
20575
1fd400ff
TT
20576 /* Write out the .debug_type entries, if any. */
20577 if (dwarf2_per_objfile->signatured_types)
20578 {
20579 struct signatured_type_index_data sig_data;
20580
20581 sig_data.objfile = objfile;
20582 sig_data.symtab = symtab;
20583 sig_data.types_list = &types_cu_list;
987d643c 20584 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20585 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20586 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20587 write_one_signatured_type, &sig_data);
20588 }
20589
156942c7
DE
20590 /* Now that we've processed all symbols we can shrink their cu_indices
20591 lists. */
20592 uniquify_cu_indices (symtab);
20593
9291a0cd
TT
20594 obstack_init (&constant_pool);
20595 make_cleanup_obstack_free (&constant_pool);
20596 obstack_init (&symtab_obstack);
20597 make_cleanup_obstack_free (&symtab_obstack);
20598 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20599
20600 obstack_init (&contents);
20601 make_cleanup_obstack_free (&contents);
1fd400ff 20602 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20603 total_len = size_of_contents;
20604
20605 /* The version number. */
796a7ff8 20606 val = MAYBE_SWAP (8);
9291a0cd
TT
20607 obstack_grow (&contents, &val, sizeof (val));
20608
20609 /* The offset of the CU list from the start of the file. */
20610 val = MAYBE_SWAP (total_len);
20611 obstack_grow (&contents, &val, sizeof (val));
20612 total_len += obstack_object_size (&cu_list);
20613
1fd400ff
TT
20614 /* The offset of the types CU list from the start of the file. */
20615 val = MAYBE_SWAP (total_len);
20616 obstack_grow (&contents, &val, sizeof (val));
20617 total_len += obstack_object_size (&types_cu_list);
20618
9291a0cd
TT
20619 /* The offset of the address table from the start of the file. */
20620 val = MAYBE_SWAP (total_len);
20621 obstack_grow (&contents, &val, sizeof (val));
20622 total_len += obstack_object_size (&addr_obstack);
20623
20624 /* The offset of the symbol table from the start of the file. */
20625 val = MAYBE_SWAP (total_len);
20626 obstack_grow (&contents, &val, sizeof (val));
20627 total_len += obstack_object_size (&symtab_obstack);
20628
20629 /* The offset of the constant pool from the start of the file. */
20630 val = MAYBE_SWAP (total_len);
20631 obstack_grow (&contents, &val, sizeof (val));
20632 total_len += obstack_object_size (&constant_pool);
20633
20634 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20635
20636 write_obstack (out_file, &contents);
20637 write_obstack (out_file, &cu_list);
1fd400ff 20638 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20639 write_obstack (out_file, &addr_obstack);
20640 write_obstack (out_file, &symtab_obstack);
20641 write_obstack (out_file, &constant_pool);
20642
20643 fclose (out_file);
20644
20645 /* We want to keep the file, so we set cleanup_filename to NULL
20646 here. See unlink_if_set. */
20647 cleanup_filename = NULL;
20648
20649 do_cleanups (cleanup);
20650}
20651
90476074
TT
20652/* Implementation of the `save gdb-index' command.
20653
20654 Note that the file format used by this command is documented in the
20655 GDB manual. Any changes here must be documented there. */
11570e71 20656
9291a0cd
TT
20657static void
20658save_gdb_index_command (char *arg, int from_tty)
20659{
20660 struct objfile *objfile;
20661
20662 if (!arg || !*arg)
96d19272 20663 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20664
20665 ALL_OBJFILES (objfile)
20666 {
20667 struct stat st;
20668
20669 /* If the objfile does not correspond to an actual file, skip it. */
20670 if (stat (objfile->name, &st) < 0)
20671 continue;
20672
20673 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20674 if (dwarf2_per_objfile)
20675 {
20676 volatile struct gdb_exception except;
20677
20678 TRY_CATCH (except, RETURN_MASK_ERROR)
20679 {
20680 write_psymtabs_to_index (objfile, arg);
20681 }
20682 if (except.reason < 0)
20683 exception_fprintf (gdb_stderr, except,
20684 _("Error while writing index for `%s': "),
20685 objfile->name);
20686 }
20687 }
dce234bc
PP
20688}
20689
9291a0cd
TT
20690\f
20691
9eae7c52
TT
20692int dwarf2_always_disassemble;
20693
20694static void
20695show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20696 struct cmd_list_element *c, const char *value)
20697{
3e43a32a
MS
20698 fprintf_filtered (file,
20699 _("Whether to always disassemble "
20700 "DWARF expressions is %s.\n"),
9eae7c52
TT
20701 value);
20702}
20703
900e11f9
JK
20704static void
20705show_check_physname (struct ui_file *file, int from_tty,
20706 struct cmd_list_element *c, const char *value)
20707{
20708 fprintf_filtered (file,
20709 _("Whether to check \"physname\" is %s.\n"),
20710 value);
20711}
20712
6502dd73
DJ
20713void _initialize_dwarf2_read (void);
20714
20715void
20716_initialize_dwarf2_read (void)
20717{
96d19272
JK
20718 struct cmd_list_element *c;
20719
dce234bc 20720 dwarf2_objfile_data_key
c1bd65d0 20721 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20722
1bedd215
AC
20723 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20724Set DWARF 2 specific variables.\n\
20725Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20726 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20727 0/*allow-unknown*/, &maintenance_set_cmdlist);
20728
1bedd215
AC
20729 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20730Show DWARF 2 specific variables\n\
20731Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20732 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20733 0/*allow-unknown*/, &maintenance_show_cmdlist);
20734
20735 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20736 &dwarf2_max_cache_age, _("\
20737Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20738Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20739A higher limit means that cached compilation units will be stored\n\
20740in memory longer, and more total memory will be used. Zero disables\n\
20741caching, which can slow down startup."),
2c5b56ce 20742 NULL,
920d2a44 20743 show_dwarf2_max_cache_age,
2c5b56ce 20744 &set_dwarf2_cmdlist,
ae038cb0 20745 &show_dwarf2_cmdlist);
d97bc12b 20746
9eae7c52
TT
20747 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20748 &dwarf2_always_disassemble, _("\
20749Set whether `info address' always disassembles DWARF expressions."), _("\
20750Show whether `info address' always disassembles DWARF expressions."), _("\
20751When enabled, DWARF expressions are always printed in an assembly-like\n\
20752syntax. When disabled, expressions will be printed in a more\n\
20753conversational style, when possible."),
20754 NULL,
20755 show_dwarf2_always_disassemble,
20756 &set_dwarf2_cmdlist,
20757 &show_dwarf2_cmdlist);
20758
45cfd468
DE
20759 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20760Set debugging of the dwarf2 reader."), _("\
20761Show debugging of the dwarf2 reader."), _("\
20762When enabled, debugging messages are printed during dwarf2 reading\n\
20763and symtab expansion."),
20764 NULL,
20765 NULL,
20766 &setdebuglist, &showdebuglist);
20767
ccce17b0 20768 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20769Set debugging of the dwarf2 DIE reader."), _("\
20770Show debugging of the dwarf2 DIE reader."), _("\
20771When enabled (non-zero), DIEs are dumped after they are read in.\n\
20772The value is the maximum depth to print."),
ccce17b0
YQ
20773 NULL,
20774 NULL,
20775 &setdebuglist, &showdebuglist);
9291a0cd 20776
900e11f9
JK
20777 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20778Set cross-checking of \"physname\" code against demangler."), _("\
20779Show cross-checking of \"physname\" code against demangler."), _("\
20780When enabled, GDB's internal \"physname\" code is checked against\n\
20781the demangler."),
20782 NULL, show_check_physname,
20783 &setdebuglist, &showdebuglist);
20784
e615022a
DE
20785 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20786 no_class, &use_deprecated_index_sections, _("\
20787Set whether to use deprecated gdb_index sections."), _("\
20788Show whether to use deprecated gdb_index sections."), _("\
20789When enabled, deprecated .gdb_index sections are used anyway.\n\
20790Normally they are ignored either because of a missing feature or\n\
20791performance issue.\n\
20792Warning: This option must be enabled before gdb reads the file."),
20793 NULL,
20794 NULL,
20795 &setlist, &showlist);
20796
96d19272 20797 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20798 _("\
fc1a9d6e 20799Save a gdb-index file.\n\
11570e71 20800Usage: save gdb-index DIRECTORY"),
96d19272
JK
20801 &save_cmdlist);
20802 set_cmd_completer (c, filename_completer);
f1e6e072
TT
20803
20804 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20805 &dwarf2_locexpr_funcs);
20806 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20807 &dwarf2_loclist_funcs);
20808
20809 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20810 &dwarf2_block_frame_base_locexpr_funcs);
20811 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20812 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 20813}
This page took 3.735998 seconds and 4 git commands to generate.