* dwarf2read.c (lookup_signatured_type): Remove complaint about
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
4c2df51b 71
c906108c
SS
72#include <fcntl.h>
73#include "gdb_string.h"
4bdf3d34 74#include "gdb_assert.h"
c906108c 75#include <sys/types.h>
d8151005 76
34eaf542
TT
77typedef struct symbol *symbolp;
78DEF_VEC_P (symbolp);
79
45cfd468
DE
80/* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82static int dwarf2_read_debug = 0;
83
d97bc12b 84/* When non-zero, dump DIEs after they are read in. */
ccce17b0 85static unsigned int dwarf2_die_debug = 0;
d97bc12b 86
900e11f9
JK
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
481860b3 90/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 91static int use_deprecated_index_sections = 0;
481860b3 92
6502dd73
DJ
93static const struct objfile_data *dwarf2_objfile_data_key;
94
f1e6e072
TT
95/* The "aclass" indices for various kinds of computed DWARF symbols. */
96
97static int dwarf2_locexpr_index;
98static int dwarf2_loclist_index;
99static int dwarf2_locexpr_block_index;
100static int dwarf2_loclist_block_index;
101
dce234bc
PP
102struct dwarf2_section_info
103{
104 asection *asection;
105 gdb_byte *buffer;
106 bfd_size_type size;
be391dca
TT
107 /* True if we have tried to read this section. */
108 int readin;
dce234bc
PP
109};
110
8b70b953
TT
111typedef struct dwarf2_section_info dwarf2_section_info_def;
112DEF_VEC_O (dwarf2_section_info_def);
113
9291a0cd
TT
114/* All offsets in the index are of this type. It must be
115 architecture-independent. */
116typedef uint32_t offset_type;
117
118DEF_VEC_I (offset_type);
119
156942c7
DE
120/* Ensure only legit values are used. */
121#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
122 do { \
123 gdb_assert ((unsigned int) (value) <= 1); \
124 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
125 } while (0)
126
127/* Ensure only legit values are used. */
128#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
129 do { \
130 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
133 } while (0)
134
135/* Ensure we don't use more than the alloted nuber of bits for the CU. */
136#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
137 do { \
138 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
140 } while (0)
141
9291a0cd
TT
142/* A description of the mapped index. The file format is described in
143 a comment by the code that writes the index. */
144struct mapped_index
145{
559a7a62
JK
146 /* Index data format version. */
147 int version;
148
9291a0cd
TT
149 /* The total length of the buffer. */
150 off_t total_size;
b11b1f88 151
9291a0cd
TT
152 /* A pointer to the address table data. */
153 const gdb_byte *address_table;
b11b1f88 154
9291a0cd
TT
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
b11b1f88 157
3876f04e
DE
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
b11b1f88 160
9291a0cd 161 /* Size in slots, each slot is 2 offset_types. */
3876f04e 162 offset_type symbol_table_slots;
b11b1f88 163
9291a0cd
TT
164 /* A pointer to the constant pool. */
165 const char *constant_pool;
166};
167
95554aad
TT
168typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169DEF_VEC_P (dwarf2_per_cu_ptr);
170
9cdd5dbd
DE
171/* Collection of data recorded per objfile.
172 This hangs off of dwarf2_objfile_data_key. */
173
6502dd73
DJ
174struct dwarf2_per_objfile
175{
dce234bc
PP
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
dce234bc
PP
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
cf2c3c16 181 struct dwarf2_section_info macro;
dce234bc
PP
182 struct dwarf2_section_info str;
183 struct dwarf2_section_info ranges;
3019eac3 184 struct dwarf2_section_info addr;
dce234bc
PP
185 struct dwarf2_section_info frame;
186 struct dwarf2_section_info eh_frame;
9291a0cd 187 struct dwarf2_section_info gdb_index;
ae038cb0 188
8b70b953
TT
189 VEC (dwarf2_section_info_def) *types;
190
be391dca
TT
191 /* Back link. */
192 struct objfile *objfile;
193
d467dd73 194 /* Table of all the compilation units. This is used to locate
10b3939b 195 the target compilation unit of a particular reference. */
ae038cb0
DJ
196 struct dwarf2_per_cu_data **all_comp_units;
197
198 /* The number of compilation units in ALL_COMP_UNITS. */
199 int n_comp_units;
200
1fd400ff 201 /* The number of .debug_types-related CUs. */
d467dd73 202 int n_type_units;
1fd400ff 203
d467dd73 204 /* The .debug_types-related CUs (TUs). */
b4dd5633 205 struct signatured_type **all_type_units;
1fd400ff 206
f4dc4d17
DE
207 /* The number of entries in all_type_unit_groups. */
208 int n_type_unit_groups;
209
210 /* Table of type unit groups.
211 This exists to make it easy to iterate over all CUs and TU groups. */
212 struct type_unit_group **all_type_unit_groups;
213
214 /* Table of struct type_unit_group objects.
215 The hash key is the DW_AT_stmt_list value. */
216 htab_t type_unit_groups;
72dca2f5 217
348e048f
DE
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
221
f4dc4d17
DE
222 /* Type unit statistics, to see how well the scaling improvements
223 are doing. */
224 struct tu_stats
225 {
226 int nr_uniq_abbrev_tables;
227 int nr_symtabs;
228 int nr_symtab_sharers;
229 int nr_stmt_less_type_units;
230 } tu_stats;
231
232 /* A chain of compilation units that are currently read in, so that
233 they can be freed later. */
234 struct dwarf2_per_cu_data *read_in_chain;
235
3019eac3
DE
236 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237 This is NULL if the table hasn't been allocated yet. */
238 htab_t dwo_files;
239
80626a55
DE
240 /* Non-zero if we've check for whether there is a DWP file. */
241 int dwp_checked;
242
243 /* The DWP file if there is one, or NULL. */
244 struct dwp_file *dwp_file;
245
36586728
TT
246 /* The shared '.dwz' file, if one exists. This is used when the
247 original data was compressed using 'dwz -m'. */
248 struct dwz_file *dwz_file;
249
72dca2f5
FR
250 /* A flag indicating wether this objfile has a section loaded at a
251 VMA of 0. */
252 int has_section_at_zero;
9291a0cd 253
ae2de4f8
DE
254 /* True if we are using the mapped index,
255 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
256 unsigned char using_index;
257
ae2de4f8 258 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 259 struct mapped_index *index_table;
98bfdba5 260
7b9f3c50 261 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
262 TUs typically share line table entries with a CU, so we maintain a
263 separate table of all line table entries to support the sharing.
264 Note that while there can be way more TUs than CUs, we've already
265 sorted all the TUs into "type unit groups", grouped by their
266 DW_AT_stmt_list value. Therefore the only sharing done here is with a
267 CU and its associated TU group if there is one. */
7b9f3c50
DE
268 htab_t quick_file_names_table;
269
98bfdba5
PA
270 /* Set during partial symbol reading, to prevent queueing of full
271 symbols. */
272 int reading_partial_symbols;
673bfd45 273
dee91e82 274 /* Table mapping type DIEs to their struct type *.
673bfd45 275 This is NULL if not allocated yet.
dee91e82
DE
276 The mapping is done via (CU/TU signature + DIE offset) -> type. */
277 htab_t die_type_hash;
95554aad
TT
278
279 /* The CUs we recently read. */
280 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
281};
282
283static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 284
251d32d9 285/* Default names of the debugging sections. */
c906108c 286
233a11ab
CS
287/* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
9cdd5dbd
DE
290static const struct dwarf2_debug_sections dwarf2_elf_names =
291{
251d32d9
TG
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 297 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_ranges", ".zdebug_ranges" },
300 { ".debug_types", ".zdebug_types" },
3019eac3 301 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
24d3216f
TT
304 { ".gdb_index", ".zgdb_index" },
305 23
251d32d9 306};
c906108c 307
80626a55 308/* List of DWO/DWP sections. */
3019eac3 309
80626a55 310static const struct dwop_section_names
3019eac3
DE
311{
312 struct dwarf2_section_names abbrev_dwo;
313 struct dwarf2_section_names info_dwo;
314 struct dwarf2_section_names line_dwo;
315 struct dwarf2_section_names loc_dwo;
09262596
DE
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
3019eac3
DE
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
80626a55
DE
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
3019eac3 323}
80626a55 324dwop_section_names =
3019eac3
DE
325{
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
330 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
332 { ".debug_str.dwo", ".zdebug_str.dwo" },
333 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
335 { ".debug_cu_index", ".zdebug_cu_index" },
336 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
337};
338
c906108c
SS
339/* local data types */
340
107d2387
AC
341/* The data in a compilation unit header, after target2host
342 translation, looks like this. */
c906108c 343struct comp_unit_head
a738430d 344{
c764a876 345 unsigned int length;
a738430d 346 short version;
a738430d
MK
347 unsigned char addr_size;
348 unsigned char signed_addr_p;
b64f50a1 349 sect_offset abbrev_offset;
57349743 350
a738430d
MK
351 /* Size of file offsets; either 4 or 8. */
352 unsigned int offset_size;
57349743 353
a738430d
MK
354 /* Size of the length field; either 4 or 12. */
355 unsigned int initial_length_size;
57349743 356
a738430d
MK
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
b64f50a1 359 sect_offset offset;
57349743 360
d00adf39
DE
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
b64f50a1 363 cu_offset first_die_offset;
a738430d 364};
c906108c 365
3da10d80
KS
366/* Type used for delaying computation of method physnames.
367 See comments for compute_delayed_physnames. */
368struct delayed_method_info
369{
370 /* The type to which the method is attached, i.e., its parent class. */
371 struct type *type;
372
373 /* The index of the method in the type's function fieldlists. */
374 int fnfield_index;
375
376 /* The index of the method in the fieldlist. */
377 int index;
378
379 /* The name of the DIE. */
380 const char *name;
381
382 /* The DIE associated with this method. */
383 struct die_info *die;
384};
385
386typedef struct delayed_method_info delayed_method_info;
387DEF_VEC_O (delayed_method_info);
388
e7c27a73
DJ
389/* Internal state when decoding a particular compilation unit. */
390struct dwarf2_cu
391{
392 /* The objfile containing this compilation unit. */
393 struct objfile *objfile;
394
d00adf39 395 /* The header of the compilation unit. */
e7c27a73 396 struct comp_unit_head header;
e142c38c 397
d00adf39
DE
398 /* Base address of this compilation unit. */
399 CORE_ADDR base_address;
400
401 /* Non-zero if base_address has been set. */
402 int base_known;
403
e142c38c
DJ
404 /* The language we are debugging. */
405 enum language language;
406 const struct language_defn *language_defn;
407
b0f35d58
DL
408 const char *producer;
409
e142c38c
DJ
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope;
420
433df2d4
DE
421 /* The abbrev table for this CU.
422 Normally this points to the abbrev table in the objfile.
423 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
424 struct abbrev_table *abbrev_table;
72bf9492 425
b64f50a1
JK
426 /* Hash table holding all the loaded partial DIEs
427 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
428 htab_t partial_dies;
429
430 /* Storage for things with the same lifetime as this read-in compilation
431 unit, including partial DIEs. */
432 struct obstack comp_unit_obstack;
433
ae038cb0
DJ
434 /* When multiple dwarf2_cu structures are living in memory, this field
435 chains them all together, so that they can be released efficiently.
436 We will probably also want a generation counter so that most-recently-used
437 compilation units are cached... */
438 struct dwarf2_per_cu_data *read_in_chain;
439
440 /* Backchain to our per_cu entry if the tree has been built. */
441 struct dwarf2_per_cu_data *per_cu;
442
443 /* How many compilation units ago was this CU last referenced? */
444 int last_used;
445
b64f50a1
JK
446 /* A hash table of DIE cu_offset for following references with
447 die_info->offset.sect_off as hash. */
51545339 448 htab_t die_hash;
10b3939b
DJ
449
450 /* Full DIEs if read in. */
451 struct die_info *dies;
452
453 /* A set of pointers to dwarf2_per_cu_data objects for compilation
454 units referenced by this one. Only set during full symbol processing;
455 partial symbol tables do not have dependencies. */
456 htab_t dependencies;
457
cb1df416
DJ
458 /* Header data from the line table, during full symbol processing. */
459 struct line_header *line_header;
460
3da10d80
KS
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 VEC (delayed_method_info) *method_list;
464
96408a79
SA
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab;
467
034e5797
DE
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
3019eac3
DE
477 struct dwo_unit *dwo_unit;
478
479 /* The DW_AT_addr_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE. */
482 ULONGEST addr_base;
483
2e3cf129
DE
484 /* The DW_AT_ranges_base attribute if present, zero otherwise
485 (zero is a valid value though).
486 Note this value comes from the stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_ranges_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
494 ULONGEST ranges_base;
495
ae038cb0
DJ
496 /* Mark used when releasing cached dies. */
497 unsigned int mark : 1;
498
8be455d7
JK
499 /* This CU references .debug_loc. See the symtab->locations_valid field.
500 This test is imperfect as there may exist optimized debug code not using
501 any location list and still facing inlining issues if handled as
502 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 503 unsigned int has_loclist : 1;
ba919b58 504
1b80a9fa
JK
505 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
506 if all the producer_is_* fields are valid. This information is cached
507 because profiling CU expansion showed excessive time spent in
508 producer_is_gxx_lt_4_6. */
ba919b58
TT
509 unsigned int checked_producer : 1;
510 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 511 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 512 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
513
514 /* When set, the file that we're processing is known to have
515 debugging info for C++ namespaces. GCC 3.3.x did not produce
516 this information, but later versions do. */
517
518 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
519};
520
10b3939b
DJ
521/* Persistent data held for a compilation unit, even when not
522 processing it. We put a pointer to this structure in the
28dee7f5 523 read_symtab_private field of the psymtab. */
10b3939b 524
ae038cb0
DJ
525struct dwarf2_per_cu_data
526{
36586728 527 /* The start offset and length of this compilation unit.
45452591 528 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
529 initial_length_size.
530 If the DIE refers to a DWO file, this is always of the original die,
531 not the DWO file. */
b64f50a1 532 sect_offset offset;
36586728 533 unsigned int length;
ae038cb0
DJ
534
535 /* Flag indicating this compilation unit will be read in before
536 any of the current compilation units are processed. */
c764a876 537 unsigned int queued : 1;
ae038cb0 538
0d99eb77
DE
539 /* This flag will be set when reading partial DIEs if we need to load
540 absolutely all DIEs for this compilation unit, instead of just the ones
541 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
542 hash table and don't find it. */
543 unsigned int load_all_dies : 1;
544
3019eac3
DE
545 /* Non-zero if this CU is from .debug_types. */
546 unsigned int is_debug_types : 1;
547
36586728
TT
548 /* Non-zero if this CU is from the .dwz file. */
549 unsigned int is_dwz : 1;
550
3019eac3
DE
551 /* The section this CU/TU lives in.
552 If the DIE refers to a DWO file, this is always the original die,
553 not the DWO file. */
8a0459fd 554 struct dwarf2_section_info *section;
348e048f 555
17ea53c3
JK
556 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
557 of the CU cache it gets reset to NULL again. */
ae038cb0 558 struct dwarf2_cu *cu;
1c379e20 559
9cdd5dbd
DE
560 /* The corresponding objfile.
561 Normally we can get the objfile from dwarf2_per_objfile.
562 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
563 struct objfile *objfile;
564
565 /* When using partial symbol tables, the 'psymtab' field is active.
566 Otherwise the 'quick' field is active. */
567 union
568 {
569 /* The partial symbol table associated with this compilation unit,
95554aad 570 or NULL for unread partial units. */
9291a0cd
TT
571 struct partial_symtab *psymtab;
572
573 /* Data needed by the "quick" functions. */
574 struct dwarf2_per_cu_quick_data *quick;
575 } v;
95554aad 576
796a7ff8
DE
577 /* The CUs we import using DW_TAG_imported_unit. This is filled in
578 while reading psymtabs, used to compute the psymtab dependencies,
579 and then cleared. Then it is filled in again while reading full
580 symbols, and only deleted when the objfile is destroyed.
581
582 This is also used to work around a difference between the way gold
583 generates .gdb_index version <=7 and the way gdb does. Arguably this
584 is a gold bug. For symbols coming from TUs, gold records in the index
585 the CU that includes the TU instead of the TU itself. This breaks
586 dw2_lookup_symbol: It assumes that if the index says symbol X lives
587 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
588 will find X. Alas TUs live in their own symtab, so after expanding CU Y
589 we need to look in TU Z to find X. Fortunately, this is akin to
590 DW_TAG_imported_unit, so we just use the same mechanism: For
591 .gdb_index version <=7 this also records the TUs that the CU referred
592 to. Concurrently with this change gdb was modified to emit version 8
593 indices so we only pay a price for gold generated indices. */
594 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
595
596 /* Type units are grouped by their DW_AT_stmt_list entry so that they
597 can share them. If this is a TU, this points to the containing
598 symtab. */
599 struct type_unit_group *type_unit_group;
ae038cb0
DJ
600};
601
348e048f
DE
602/* Entry in the signatured_types hash table. */
603
604struct signatured_type
605{
42e7ad6c
DE
606 /* The "per_cu" object of this type.
607 N.B.: This is the first member so that it's easy to convert pointers
608 between them. */
609 struct dwarf2_per_cu_data per_cu;
610
3019eac3 611 /* The type's signature. */
348e048f
DE
612 ULONGEST signature;
613
3019eac3
DE
614 /* Offset in the TU of the type's DIE, as read from the TU header.
615 If the definition lives in a DWO file, this value is unusable. */
616 cu_offset type_offset_in_tu;
617
618 /* Offset in the section of the type's DIE.
619 If the definition lives in a DWO file, this is the offset in the
620 .debug_types.dwo section.
621 The value is zero until the actual value is known.
622 Zero is otherwise not a valid section offset. */
623 sect_offset type_offset_in_section;
348e048f
DE
624};
625
094b34ac
DE
626/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
627 This includes type_unit_group and quick_file_names. */
628
629struct stmt_list_hash
630{
631 /* The DWO unit this table is from or NULL if there is none. */
632 struct dwo_unit *dwo_unit;
633
634 /* Offset in .debug_line or .debug_line.dwo. */
635 sect_offset line_offset;
636};
637
f4dc4d17
DE
638/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
639 an object of this type. */
640
641struct type_unit_group
642{
643 /* dwarf2read.c's main "handle" on the symtab.
644 To simplify things we create an artificial CU that "includes" all the
645 type units using this stmt_list so that the rest of the code still has
646 a "per_cu" handle on the symtab.
647 This PER_CU is recognized by having no section. */
8a0459fd 648#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
649 struct dwarf2_per_cu_data per_cu;
650
651 union
652 {
653 /* The TUs that share this DW_AT_stmt_list entry.
654 This is added to while parsing type units to build partial symtabs,
655 and is deleted afterwards and not used again. */
656 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 657
094b34ac
DE
658 /* When reading the line table in "quick" functions, we need a real TU.
659 Any will do, we know they all share the same DW_AT_stmt_list entry.
660 For simplicity's sake, we pick the first one. */
661 struct dwarf2_per_cu_data *first_tu;
662 } t;
f4dc4d17
DE
663
664 /* The primary symtab.
094b34ac
DE
665 Type units in a group needn't all be defined in the same source file,
666 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
667 struct symtab *primary_symtab;
668
094b34ac
DE
669 /* The data used to construct the hash key. */
670 struct stmt_list_hash hash;
f4dc4d17
DE
671
672 /* The number of symtabs from the line header.
673 The value here must match line_header.num_file_names. */
674 unsigned int num_symtabs;
675
676 /* The symbol tables for this TU (obtained from the files listed in
677 DW_AT_stmt_list).
678 WARNING: The order of entries here must match the order of entries
679 in the line header. After the first TU using this type_unit_group, the
680 line header for the subsequent TUs is recreated from this. This is done
681 because we need to use the same symtabs for each TU using the same
682 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
683 there's no guarantee the line header doesn't have duplicate entries. */
684 struct symtab **symtabs;
685};
686
80626a55 687/* These sections are what may appear in a DWO file. */
3019eac3
DE
688
689struct dwo_sections
690{
691 struct dwarf2_section_info abbrev;
3019eac3
DE
692 struct dwarf2_section_info line;
693 struct dwarf2_section_info loc;
09262596
DE
694 struct dwarf2_section_info macinfo;
695 struct dwarf2_section_info macro;
3019eac3
DE
696 struct dwarf2_section_info str;
697 struct dwarf2_section_info str_offsets;
80626a55
DE
698 /* In the case of a virtual DWO file, these two are unused. */
699 struct dwarf2_section_info info;
3019eac3
DE
700 VEC (dwarf2_section_info_def) *types;
701};
702
703/* Common bits of DWO CUs/TUs. */
704
705struct dwo_unit
706{
707 /* Backlink to the containing struct dwo_file. */
708 struct dwo_file *dwo_file;
709
710 /* The "id" that distinguishes this CU/TU.
711 .debug_info calls this "dwo_id", .debug_types calls this "signature".
712 Since signatures came first, we stick with it for consistency. */
713 ULONGEST signature;
714
715 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 716 struct dwarf2_section_info *section;
3019eac3
DE
717
718 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
719 sect_offset offset;
720 unsigned int length;
721
722 /* For types, offset in the type's DIE of the type defined by this TU. */
723 cu_offset type_offset_in_tu;
724};
725
80626a55
DE
726/* Data for one DWO file.
727 This includes virtual DWO files that have been packaged into a
728 DWP file. */
3019eac3
DE
729
730struct dwo_file
731{
80626a55
DE
732 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
733 For virtual DWO files the name is constructed from the section offsets
734 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
735 from related CU+TUs. */
736 const char *name;
3019eac3 737
80626a55
DE
738 /* The bfd, when the file is open. Otherwise this is NULL.
739 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
740 bfd *dbfd;
3019eac3
DE
741
742 /* Section info for this file. */
743 struct dwo_sections sections;
744
745 /* Table of CUs in the file.
746 Each element is a struct dwo_unit. */
747 htab_t cus;
748
749 /* Table of TUs in the file.
750 Each element is a struct dwo_unit. */
751 htab_t tus;
752};
753
80626a55
DE
754/* These sections are what may appear in a DWP file. */
755
756struct dwp_sections
757{
758 struct dwarf2_section_info str;
759 struct dwarf2_section_info cu_index;
760 struct dwarf2_section_info tu_index;
761 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
762 by section number. We don't need to record them here. */
763};
764
765/* These sections are what may appear in a virtual DWO file. */
766
767struct virtual_dwo_sections
768{
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 776 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
777 struct dwarf2_section_info info_or_types;
778};
779
780/* Contents of DWP hash tables. */
781
782struct dwp_hash_table
783{
784 uint32_t nr_units, nr_slots;
785 const gdb_byte *hash_table, *unit_table, *section_pool;
786};
787
788/* Data for one DWP file. */
789
790struct dwp_file
791{
792 /* Name of the file. */
793 const char *name;
794
795 /* The bfd, when the file is open. Otherwise this is NULL. */
796 bfd *dbfd;
797
798 /* Section info for this file. */
799 struct dwp_sections sections;
800
801 /* Table of CUs in the file. */
802 const struct dwp_hash_table *cus;
803
804 /* Table of TUs in the file. */
805 const struct dwp_hash_table *tus;
806
807 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
808 htab_t loaded_cutus;
809
810 /* Table to map ELF section numbers to their sections. */
811 unsigned int num_sections;
812 asection **elf_sections;
813};
814
36586728
TT
815/* This represents a '.dwz' file. */
816
817struct dwz_file
818{
819 /* A dwz file can only contain a few sections. */
820 struct dwarf2_section_info abbrev;
821 struct dwarf2_section_info info;
822 struct dwarf2_section_info str;
823 struct dwarf2_section_info line;
824 struct dwarf2_section_info macro;
2ec9a5e0 825 struct dwarf2_section_info gdb_index;
36586728
TT
826
827 /* The dwz's BFD. */
828 bfd *dwz_bfd;
829};
830
0963b4bd
MS
831/* Struct used to pass misc. parameters to read_die_and_children, et
832 al. which are used for both .debug_info and .debug_types dies.
833 All parameters here are unchanging for the life of the call. This
dee91e82 834 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
835
836struct die_reader_specs
837{
dee91e82 838 /* die_section->asection->owner. */
93311388
DE
839 bfd* abfd;
840
841 /* The CU of the DIE we are parsing. */
842 struct dwarf2_cu *cu;
843
80626a55 844 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
845 struct dwo_file *dwo_file;
846
dee91e82 847 /* The section the die comes from.
3019eac3 848 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
849 struct dwarf2_section_info *die_section;
850
851 /* die_section->buffer. */
852 gdb_byte *buffer;
f664829e
DE
853
854 /* The end of the buffer. */
855 const gdb_byte *buffer_end;
93311388
DE
856};
857
fd820528 858/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
859typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
860 gdb_byte *info_ptr,
861 struct die_info *comp_unit_die,
862 int has_children,
863 void *data);
864
debd256d
JB
865/* The line number information for a compilation unit (found in the
866 .debug_line section) begins with a "statement program header",
867 which contains the following information. */
868struct line_header
869{
870 unsigned int total_length;
871 unsigned short version;
872 unsigned int header_length;
873 unsigned char minimum_instruction_length;
2dc7f7b3 874 unsigned char maximum_ops_per_instruction;
debd256d
JB
875 unsigned char default_is_stmt;
876 int line_base;
877 unsigned char line_range;
878 unsigned char opcode_base;
879
880 /* standard_opcode_lengths[i] is the number of operands for the
881 standard opcode whose value is i. This means that
882 standard_opcode_lengths[0] is unused, and the last meaningful
883 element is standard_opcode_lengths[opcode_base - 1]. */
884 unsigned char *standard_opcode_lengths;
885
886 /* The include_directories table. NOTE! These strings are not
887 allocated with xmalloc; instead, they are pointers into
888 debug_line_buffer. If you try to free them, `free' will get
889 indigestion. */
890 unsigned int num_include_dirs, include_dirs_size;
891 char **include_dirs;
892
893 /* The file_names table. NOTE! These strings are not allocated
894 with xmalloc; instead, they are pointers into debug_line_buffer.
895 Don't try to free them directly. */
896 unsigned int num_file_names, file_names_size;
897 struct file_entry
c906108c 898 {
debd256d
JB
899 char *name;
900 unsigned int dir_index;
901 unsigned int mod_time;
902 unsigned int length;
aaa75496 903 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 904 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
905 } *file_names;
906
907 /* The start and end of the statement program following this
6502dd73 908 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 909 gdb_byte *statement_program_start, *statement_program_end;
debd256d 910};
c906108c
SS
911
912/* When we construct a partial symbol table entry we only
0963b4bd 913 need this much information. */
c906108c
SS
914struct partial_die_info
915 {
72bf9492 916 /* Offset of this DIE. */
b64f50a1 917 sect_offset offset;
72bf9492
DJ
918
919 /* DWARF-2 tag for this DIE. */
920 ENUM_BITFIELD(dwarf_tag) tag : 16;
921
72bf9492
DJ
922 /* Assorted flags describing the data found in this DIE. */
923 unsigned int has_children : 1;
924 unsigned int is_external : 1;
925 unsigned int is_declaration : 1;
926 unsigned int has_type : 1;
927 unsigned int has_specification : 1;
928 unsigned int has_pc_info : 1;
481860b3 929 unsigned int may_be_inlined : 1;
72bf9492
DJ
930
931 /* Flag set if the SCOPE field of this structure has been
932 computed. */
933 unsigned int scope_set : 1;
934
fa4028e9
JB
935 /* Flag set if the DIE has a byte_size attribute. */
936 unsigned int has_byte_size : 1;
937
98bfdba5
PA
938 /* Flag set if any of the DIE's children are template arguments. */
939 unsigned int has_template_arguments : 1;
940
abc72ce4
DE
941 /* Flag set if fixup_partial_die has been called on this die. */
942 unsigned int fixup_called : 1;
943
36586728
TT
944 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
945 unsigned int is_dwz : 1;
946
947 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
948 unsigned int spec_is_dwz : 1;
949
72bf9492 950 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 951 sometimes a default name for unnamed DIEs. */
15d034d0 952 const char *name;
72bf9492 953
abc72ce4
DE
954 /* The linkage name, if present. */
955 const char *linkage_name;
956
72bf9492
DJ
957 /* The scope to prepend to our children. This is generally
958 allocated on the comp_unit_obstack, so will disappear
959 when this compilation unit leaves the cache. */
15d034d0 960 const char *scope;
72bf9492 961
95554aad
TT
962 /* Some data associated with the partial DIE. The tag determines
963 which field is live. */
964 union
965 {
966 /* The location description associated with this DIE, if any. */
967 struct dwarf_block *locdesc;
968 /* The offset of an import, for DW_TAG_imported_unit. */
969 sect_offset offset;
970 } d;
72bf9492
DJ
971
972 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
973 CORE_ADDR lowpc;
974 CORE_ADDR highpc;
72bf9492 975
93311388 976 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 977 DW_AT_sibling, if any. */
abc72ce4
DE
978 /* NOTE: This member isn't strictly necessary, read_partial_die could
979 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 980 gdb_byte *sibling;
72bf9492
DJ
981
982 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
983 DW_AT_specification (or DW_AT_abstract_origin or
984 DW_AT_extension). */
b64f50a1 985 sect_offset spec_offset;
72bf9492
DJ
986
987 /* Pointers to this DIE's parent, first child, and next sibling,
988 if any. */
989 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
990 };
991
0963b4bd 992/* This data structure holds the information of an abbrev. */
c906108c
SS
993struct abbrev_info
994 {
995 unsigned int number; /* number identifying abbrev */
996 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
997 unsigned short has_children; /* boolean */
998 unsigned short num_attrs; /* number of attributes */
c906108c
SS
999 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1000 struct abbrev_info *next; /* next in chain */
1001 };
1002
1003struct attr_abbrev
1004 {
9d25dd43
DE
1005 ENUM_BITFIELD(dwarf_attribute) name : 16;
1006 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1007 };
1008
433df2d4
DE
1009/* Size of abbrev_table.abbrev_hash_table. */
1010#define ABBREV_HASH_SIZE 121
1011
1012/* Top level data structure to contain an abbreviation table. */
1013
1014struct abbrev_table
1015{
f4dc4d17
DE
1016 /* Where the abbrev table came from.
1017 This is used as a sanity check when the table is used. */
433df2d4
DE
1018 sect_offset offset;
1019
1020 /* Storage for the abbrev table. */
1021 struct obstack abbrev_obstack;
1022
1023 /* Hash table of abbrevs.
1024 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1025 It could be statically allocated, but the previous code didn't so we
1026 don't either. */
1027 struct abbrev_info **abbrevs;
1028};
1029
0963b4bd 1030/* Attributes have a name and a value. */
b60c80d6
DJ
1031struct attribute
1032 {
9d25dd43 1033 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1034 ENUM_BITFIELD(dwarf_form) form : 15;
1035
1036 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1037 field should be in u.str (existing only for DW_STRING) but it is kept
1038 here for better struct attribute alignment. */
1039 unsigned int string_is_canonical : 1;
1040
b60c80d6
DJ
1041 union
1042 {
15d034d0 1043 const char *str;
b60c80d6 1044 struct dwarf_block *blk;
43bbcdc2
PH
1045 ULONGEST unsnd;
1046 LONGEST snd;
b60c80d6 1047 CORE_ADDR addr;
348e048f 1048 struct signatured_type *signatured_type;
b60c80d6
DJ
1049 }
1050 u;
1051 };
1052
0963b4bd 1053/* This data structure holds a complete die structure. */
c906108c
SS
1054struct die_info
1055 {
76815b17
DE
1056 /* DWARF-2 tag for this DIE. */
1057 ENUM_BITFIELD(dwarf_tag) tag : 16;
1058
1059 /* Number of attributes */
98bfdba5
PA
1060 unsigned char num_attrs;
1061
1062 /* True if we're presently building the full type name for the
1063 type derived from this DIE. */
1064 unsigned char building_fullname : 1;
76815b17
DE
1065
1066 /* Abbrev number */
1067 unsigned int abbrev;
1068
93311388 1069 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1070 sect_offset offset;
78ba4af6
JB
1071
1072 /* The dies in a compilation unit form an n-ary tree. PARENT
1073 points to this die's parent; CHILD points to the first child of
1074 this node; and all the children of a given node are chained
4950bc1c 1075 together via their SIBLING fields. */
639d11d3
DC
1076 struct die_info *child; /* Its first child, if any. */
1077 struct die_info *sibling; /* Its next sibling, if any. */
1078 struct die_info *parent; /* Its parent, if any. */
c906108c 1079
b60c80d6
DJ
1080 /* An array of attributes, with NUM_ATTRS elements. There may be
1081 zero, but it's not common and zero-sized arrays are not
1082 sufficiently portable C. */
1083 struct attribute attrs[1];
c906108c
SS
1084 };
1085
0963b4bd 1086/* Get at parts of an attribute structure. */
c906108c
SS
1087
1088#define DW_STRING(attr) ((attr)->u.str)
8285870a 1089#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1090#define DW_UNSND(attr) ((attr)->u.unsnd)
1091#define DW_BLOCK(attr) ((attr)->u.blk)
1092#define DW_SND(attr) ((attr)->u.snd)
1093#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1094#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1095
0963b4bd 1096/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1097struct dwarf_block
1098 {
56eb65bd 1099 size_t size;
1d6edc3c
JK
1100
1101 /* Valid only if SIZE is not zero. */
fe1b8b76 1102 gdb_byte *data;
c906108c
SS
1103 };
1104
c906108c
SS
1105#ifndef ATTR_ALLOC_CHUNK
1106#define ATTR_ALLOC_CHUNK 4
1107#endif
1108
c906108c
SS
1109/* Allocate fields for structs, unions and enums in this size. */
1110#ifndef DW_FIELD_ALLOC_CHUNK
1111#define DW_FIELD_ALLOC_CHUNK 4
1112#endif
1113
c906108c
SS
1114/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1115 but this would require a corresponding change in unpack_field_as_long
1116 and friends. */
1117static int bits_per_byte = 8;
1118
1119/* The routines that read and process dies for a C struct or C++ class
1120 pass lists of data member fields and lists of member function fields
1121 in an instance of a field_info structure, as defined below. */
1122struct field_info
c5aa993b 1123 {
0963b4bd 1124 /* List of data member and baseclasses fields. */
c5aa993b
JM
1125 struct nextfield
1126 {
1127 struct nextfield *next;
1128 int accessibility;
1129 int virtuality;
1130 struct field field;
1131 }
7d0ccb61 1132 *fields, *baseclasses;
c906108c 1133
7d0ccb61 1134 /* Number of fields (including baseclasses). */
c5aa993b 1135 int nfields;
c906108c 1136
c5aa993b
JM
1137 /* Number of baseclasses. */
1138 int nbaseclasses;
c906108c 1139
c5aa993b
JM
1140 /* Set if the accesibility of one of the fields is not public. */
1141 int non_public_fields;
c906108c 1142
c5aa993b
JM
1143 /* Member function fields array, entries are allocated in the order they
1144 are encountered in the object file. */
1145 struct nextfnfield
1146 {
1147 struct nextfnfield *next;
1148 struct fn_field fnfield;
1149 }
1150 *fnfields;
c906108c 1151
c5aa993b
JM
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
1155 struct fnfieldlist
1156 {
15d034d0 1157 const char *name;
c5aa993b
JM
1158 int length;
1159 struct nextfnfield *head;
1160 }
1161 *fnfieldlists;
c906108c 1162
c5aa993b
JM
1163 /* Number of entries in the fnfieldlists array. */
1164 int nfnfields;
98751a41
JK
1165
1166 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1167 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1168 struct typedef_field_list
1169 {
1170 struct typedef_field field;
1171 struct typedef_field_list *next;
1172 }
1173 *typedef_field_list;
1174 unsigned typedef_field_list_count;
c5aa993b 1175 };
c906108c 1176
10b3939b
DJ
1177/* One item on the queue of compilation units to read in full symbols
1178 for. */
1179struct dwarf2_queue_item
1180{
1181 struct dwarf2_per_cu_data *per_cu;
95554aad 1182 enum language pretend_language;
10b3939b
DJ
1183 struct dwarf2_queue_item *next;
1184};
1185
1186/* The current queue. */
1187static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1188
ae038cb0
DJ
1189/* Loaded secondary compilation units are kept in memory until they
1190 have not been referenced for the processing of this many
1191 compilation units. Set this to zero to disable caching. Cache
1192 sizes of up to at least twenty will improve startup time for
1193 typical inter-CU-reference binaries, at an obvious memory cost. */
1194static int dwarf2_max_cache_age = 5;
920d2a44
AC
1195static void
1196show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1197 struct cmd_list_element *c, const char *value)
1198{
3e43a32a
MS
1199 fprintf_filtered (file, _("The upper bound on the age of cached "
1200 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1201 value);
1202}
1203
ae038cb0 1204
0963b4bd 1205/* Various complaints about symbol reading that don't abort the process. */
c906108c 1206
4d3c2250
KB
1207static void
1208dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1209{
4d3c2250 1210 complaint (&symfile_complaints,
e2e0b3e5 1211 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1212}
1213
25e43795
DJ
1214static void
1215dwarf2_debug_line_missing_file_complaint (void)
1216{
1217 complaint (&symfile_complaints,
1218 _(".debug_line section has line data without a file"));
1219}
1220
59205f5a
JB
1221static void
1222dwarf2_debug_line_missing_end_sequence_complaint (void)
1223{
1224 complaint (&symfile_complaints,
3e43a32a
MS
1225 _(".debug_line section has line "
1226 "program sequence without an end"));
59205f5a
JB
1227}
1228
4d3c2250
KB
1229static void
1230dwarf2_complex_location_expr_complaint (void)
2e276125 1231{
e2e0b3e5 1232 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1233}
1234
4d3c2250
KB
1235static void
1236dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1237 int arg3)
2e276125 1238{
4d3c2250 1239 complaint (&symfile_complaints,
3e43a32a
MS
1240 _("const value length mismatch for '%s', got %d, expected %d"),
1241 arg1, arg2, arg3);
4d3c2250
KB
1242}
1243
1244static void
f664829e 1245dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1246{
4d3c2250 1247 complaint (&symfile_complaints,
f664829e
DE
1248 _("debug info runs off end of %s section"
1249 " [in module %s]"),
1250 section->asection->name,
1251 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1252}
1253
1254static void
1255dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1256{
4d3c2250 1257 complaint (&symfile_complaints,
3e43a32a
MS
1258 _("macro debug info contains a "
1259 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1260 arg1);
1261}
1262
1263static void
1264dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1265{
4d3c2250 1266 complaint (&symfile_complaints,
3e43a32a
MS
1267 _("invalid attribute class or form for '%s' in '%s'"),
1268 arg1, arg2);
4d3c2250 1269}
c906108c 1270
c906108c
SS
1271/* local function prototypes */
1272
4efb68b1 1273static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1274
aaa75496
JB
1275static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1276 struct objfile *);
1277
918dd910
JK
1278static void dwarf2_find_base_address (struct die_info *die,
1279 struct dwarf2_cu *cu);
1280
0018ea6f
DE
1281static struct partial_symtab *create_partial_symtab
1282 (struct dwarf2_per_cu_data *per_cu, const char *name);
1283
c67a9c90 1284static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1285
72bf9492
DJ
1286static void scan_partial_symbols (struct partial_die_info *,
1287 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1288 int, struct dwarf2_cu *);
c906108c 1289
72bf9492
DJ
1290static void add_partial_symbol (struct partial_die_info *,
1291 struct dwarf2_cu *);
63d06c5c 1292
72bf9492
DJ
1293static void add_partial_namespace (struct partial_die_info *pdi,
1294 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1295 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1296
5d7cb8df
JK
1297static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1298 CORE_ADDR *highpc, int need_pc,
1299 struct dwarf2_cu *cu);
1300
72bf9492
DJ
1301static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1302 struct dwarf2_cu *cu);
91c24f0a 1303
bc30ff58
JB
1304static void add_partial_subprogram (struct partial_die_info *pdi,
1305 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1306 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1307
257e7a09
YQ
1308static void dwarf2_read_symtab (struct partial_symtab *,
1309 struct objfile *);
c906108c 1310
a14ed312 1311static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1312
433df2d4
DE
1313static struct abbrev_info *abbrev_table_lookup_abbrev
1314 (const struct abbrev_table *, unsigned int);
1315
1316static struct abbrev_table *abbrev_table_read_table
1317 (struct dwarf2_section_info *, sect_offset);
1318
1319static void abbrev_table_free (struct abbrev_table *);
1320
f4dc4d17
DE
1321static void abbrev_table_free_cleanup (void *);
1322
dee91e82
DE
1323static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1324 struct dwarf2_section_info *);
c906108c 1325
f3dd6933 1326static void dwarf2_free_abbrev_table (void *);
c906108c 1327
6caca83c
CC
1328static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1329
dee91e82
DE
1330static struct partial_die_info *load_partial_dies
1331 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1332
dee91e82
DE
1333static gdb_byte *read_partial_die (const struct die_reader_specs *,
1334 struct partial_die_info *,
1335 struct abbrev_info *,
1336 unsigned int,
1337 gdb_byte *);
c906108c 1338
36586728 1339static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1340 struct dwarf2_cu *);
72bf9492
DJ
1341
1342static void fixup_partial_die (struct partial_die_info *,
1343 struct dwarf2_cu *);
1344
dee91e82
DE
1345static gdb_byte *read_attribute (const struct die_reader_specs *,
1346 struct attribute *, struct attr_abbrev *,
1347 gdb_byte *);
a8329558 1348
a1855c1d 1349static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1350
a1855c1d 1351static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1352
a1855c1d 1353static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1354
a1855c1d 1355static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1356
a1855c1d 1357static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1358
fe1b8b76 1359static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1360 unsigned int *);
c906108c 1361
c764a876
DE
1362static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1363
1364static LONGEST read_checked_initial_length_and_offset
1365 (bfd *, gdb_byte *, const struct comp_unit_head *,
1366 unsigned int *, unsigned int *);
613e1657 1367
fe1b8b76 1368static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1369 unsigned int *);
1370
1371static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1372
f4dc4d17
DE
1373static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1374 sect_offset);
1375
fe1b8b76 1376static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1377
9b1c24c8 1378static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1379
fe1b8b76
JB
1380static char *read_indirect_string (bfd *, gdb_byte *,
1381 const struct comp_unit_head *,
1382 unsigned int *);
4bdf3d34 1383
36586728
TT
1384static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1385
12df843f 1386static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1387
12df843f 1388static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1389
3019eac3
DE
1390static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1391 unsigned int *);
1392
1393static char *read_str_index (const struct die_reader_specs *reader,
1394 struct dwarf2_cu *cu, ULONGEST str_index);
1395
e142c38c 1396static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1397
e142c38c
DJ
1398static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1399 struct dwarf2_cu *);
c906108c 1400
348e048f 1401static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1402 unsigned int);
348e048f 1403
05cf31d1
JB
1404static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1405 struct dwarf2_cu *cu);
1406
e142c38c 1407static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1408
e142c38c 1409static struct die_info *die_specification (struct die_info *die,
f2f0e013 1410 struct dwarf2_cu **);
63d06c5c 1411
debd256d
JB
1412static void free_line_header (struct line_header *lh);
1413
aaa75496
JB
1414static void add_file_name (struct line_header *, char *, unsigned int,
1415 unsigned int, unsigned int);
1416
3019eac3
DE
1417static struct line_header *dwarf_decode_line_header (unsigned int offset,
1418 struct dwarf2_cu *cu);
debd256d 1419
f3f5162e
DE
1420static void dwarf_decode_lines (struct line_header *, const char *,
1421 struct dwarf2_cu *, struct partial_symtab *,
1422 int);
c906108c 1423
72b9f47f 1424static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1425
f4dc4d17 1426static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1427 const char *, const char *, CORE_ADDR);
f4dc4d17 1428
a14ed312 1429static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1430 struct dwarf2_cu *);
c906108c 1431
34eaf542
TT
1432static struct symbol *new_symbol_full (struct die_info *, struct type *,
1433 struct dwarf2_cu *, struct symbol *);
1434
a14ed312 1435static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1436 struct dwarf2_cu *);
c906108c 1437
98bfdba5
PA
1438static void dwarf2_const_value_attr (struct attribute *attr,
1439 struct type *type,
1440 const char *name,
1441 struct obstack *obstack,
12df843f 1442 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1443 gdb_byte **bytes,
1444 struct dwarf2_locexpr_baton **baton);
2df3850c 1445
e7c27a73 1446static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1447
b4ba55a1
JB
1448static int need_gnat_info (struct dwarf2_cu *);
1449
3e43a32a
MS
1450static struct type *die_descriptive_type (struct die_info *,
1451 struct dwarf2_cu *);
b4ba55a1
JB
1452
1453static void set_descriptive_type (struct type *, struct die_info *,
1454 struct dwarf2_cu *);
1455
e7c27a73
DJ
1456static struct type *die_containing_type (struct die_info *,
1457 struct dwarf2_cu *);
c906108c 1458
673bfd45
DE
1459static struct type *lookup_die_type (struct die_info *, struct attribute *,
1460 struct dwarf2_cu *);
c906108c 1461
f792889a 1462static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1463
673bfd45
DE
1464static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1465
0d5cff50 1466static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1467
6e70227d 1468static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1469 const char *suffix, int physname,
1470 struct dwarf2_cu *cu);
63d06c5c 1471
e7c27a73 1472static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1473
348e048f
DE
1474static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1475
e7c27a73 1476static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1477
e7c27a73 1478static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1479
96408a79
SA
1480static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1481
ff013f42
JK
1482static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *, struct partial_symtab *);
1484
a14ed312 1485static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1486 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1487 struct partial_symtab *);
c906108c 1488
fae299cd
DC
1489static void get_scope_pc_bounds (struct die_info *,
1490 CORE_ADDR *, CORE_ADDR *,
1491 struct dwarf2_cu *);
1492
801e3a5b
JB
1493static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1494 CORE_ADDR, struct dwarf2_cu *);
1495
a14ed312 1496static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1497 struct dwarf2_cu *);
c906108c 1498
a14ed312 1499static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1500 struct type *, struct dwarf2_cu *);
c906108c 1501
a14ed312 1502static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1503 struct die_info *, struct type *,
e7c27a73 1504 struct dwarf2_cu *);
c906108c 1505
a14ed312 1506static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1507 struct type *,
1508 struct dwarf2_cu *);
c906108c 1509
134d01f1 1510static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1511
e7c27a73 1512static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1513
e7c27a73 1514static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1515
5d7cb8df
JK
1516static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1517
27aa8d6a
SW
1518static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1519
f55ee35c
JK
1520static struct type *read_module_type (struct die_info *die,
1521 struct dwarf2_cu *cu);
1522
38d518c9 1523static const char *namespace_name (struct die_info *die,
e142c38c 1524 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1525
134d01f1 1526static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1527
e7c27a73 1528static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1529
6e70227d 1530static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1531 struct dwarf2_cu *);
1532
dee91e82 1533static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1534 gdb_byte *info_ptr,
fe1b8b76 1535 gdb_byte **new_info_ptr,
639d11d3
DC
1536 struct die_info *parent);
1537
dee91e82 1538static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1539 gdb_byte *info_ptr,
fe1b8b76 1540 gdb_byte **new_info_ptr,
639d11d3
DC
1541 struct die_info *parent);
1542
3019eac3
DE
1543static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1544 struct die_info **, gdb_byte *, int *, int);
1545
dee91e82
DE
1546static gdb_byte *read_full_die (const struct die_reader_specs *,
1547 struct die_info **, gdb_byte *, int *);
93311388 1548
e7c27a73 1549static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1550
15d034d0
TT
1551static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1552 struct obstack *);
71c25dea 1553
15d034d0 1554static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1555
15d034d0 1556static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1557 struct die_info *die,
1558 struct dwarf2_cu *cu);
1559
ca69b9e6
DE
1560static const char *dwarf2_physname (const char *name, struct die_info *die,
1561 struct dwarf2_cu *cu);
1562
e142c38c 1563static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1564 struct dwarf2_cu **);
9219021c 1565
f39c6ffd 1566static const char *dwarf_tag_name (unsigned int);
c906108c 1567
f39c6ffd 1568static const char *dwarf_attr_name (unsigned int);
c906108c 1569
f39c6ffd 1570static const char *dwarf_form_name (unsigned int);
c906108c 1571
a14ed312 1572static char *dwarf_bool_name (unsigned int);
c906108c 1573
f39c6ffd 1574static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1575
f9aca02d 1576static struct die_info *sibling_die (struct die_info *);
c906108c 1577
d97bc12b
DE
1578static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1579
1580static void dump_die_for_error (struct die_info *);
1581
1582static void dump_die_1 (struct ui_file *, int level, int max_level,
1583 struct die_info *);
c906108c 1584
d97bc12b 1585/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1586
51545339 1587static void store_in_ref_table (struct die_info *,
10b3939b 1588 struct dwarf2_cu *);
c906108c 1589
93311388
DE
1590static int is_ref_attr (struct attribute *);
1591
b64f50a1 1592static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1593
43bbcdc2 1594static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1595
348e048f
DE
1596static struct die_info *follow_die_ref_or_sig (struct die_info *,
1597 struct attribute *,
1598 struct dwarf2_cu **);
1599
10b3939b
DJ
1600static struct die_info *follow_die_ref (struct die_info *,
1601 struct attribute *,
f2f0e013 1602 struct dwarf2_cu **);
c906108c 1603
348e048f
DE
1604static struct die_info *follow_die_sig (struct die_info *,
1605 struct attribute *,
1606 struct dwarf2_cu **);
1607
6c83ed52
TT
1608static struct signatured_type *lookup_signatured_type_at_offset
1609 (struct objfile *objfile,
b64f50a1 1610 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1611
e5fe5e75 1612static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1613
52dc124a 1614static void read_signatured_type (struct signatured_type *);
348e048f 1615
f4dc4d17 1616static struct type_unit_group *get_type_unit_group
094b34ac 1617 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1618
1619static void build_type_unit_groups (die_reader_func_ftype *, void *);
1620
c906108c
SS
1621/* memory allocation interface */
1622
7b5a2f43 1623static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1624
b60c80d6 1625static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1626
09262596 1627static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1628 const char *, int);
2e276125 1629
8e19ed76
PS
1630static int attr_form_is_block (struct attribute *);
1631
3690dd37
JB
1632static int attr_form_is_section_offset (struct attribute *);
1633
1634static int attr_form_is_constant (struct attribute *);
1635
8cf6f0b1
TT
1636static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1637 struct dwarf2_loclist_baton *baton,
1638 struct attribute *attr);
1639
93e7bd98
DJ
1640static void dwarf2_symbol_mark_computed (struct attribute *attr,
1641 struct symbol *sym,
f1e6e072
TT
1642 struct dwarf2_cu *cu,
1643 int is_block);
4c2df51b 1644
dee91e82
DE
1645static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1646 gdb_byte *info_ptr,
1647 struct abbrev_info *abbrev);
4bb7a0a7 1648
72bf9492
DJ
1649static void free_stack_comp_unit (void *);
1650
72bf9492
DJ
1651static hashval_t partial_die_hash (const void *item);
1652
1653static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1654
ae038cb0 1655static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1656 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1657
9816fde3 1658static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1659 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1660
1661static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1662 struct die_info *comp_unit_die,
1663 enum language pretend_language);
93311388 1664
68dc6402 1665static void free_heap_comp_unit (void *);
ae038cb0
DJ
1666
1667static void free_cached_comp_units (void *);
1668
1669static void age_cached_comp_units (void);
1670
dee91e82 1671static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1672
f792889a
DJ
1673static struct type *set_die_type (struct die_info *, struct type *,
1674 struct dwarf2_cu *);
1c379e20 1675
ae038cb0
DJ
1676static void create_all_comp_units (struct objfile *);
1677
0e50663e 1678static int create_all_type_units (struct objfile *);
1fd400ff 1679
95554aad
TT
1680static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1681 enum language);
10b3939b 1682
95554aad
TT
1683static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1684 enum language);
10b3939b 1685
f4dc4d17
DE
1686static void process_full_type_unit (struct dwarf2_per_cu_data *,
1687 enum language);
1688
10b3939b
DJ
1689static void dwarf2_add_dependence (struct dwarf2_cu *,
1690 struct dwarf2_per_cu_data *);
1691
ae038cb0
DJ
1692static void dwarf2_mark (struct dwarf2_cu *);
1693
1694static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1695
b64f50a1 1696static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1697 struct dwarf2_per_cu_data *per_cu);
1698
f792889a 1699static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1700
9291a0cd
TT
1701static void dwarf2_release_queue (void *dummy);
1702
95554aad
TT
1703static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1704 enum language pretend_language);
1705
1706static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1707 struct dwarf2_per_cu_data *per_cu,
1708 enum language pretend_language);
9291a0cd 1709
a0f42c21 1710static void process_queue (void);
9291a0cd
TT
1711
1712static void find_file_and_directory (struct die_info *die,
1713 struct dwarf2_cu *cu,
15d034d0 1714 const char **name, const char **comp_dir);
9291a0cd
TT
1715
1716static char *file_full_name (int file, struct line_header *lh,
1717 const char *comp_dir);
1718
36586728
TT
1719static gdb_byte *read_and_check_comp_unit_head
1720 (struct comp_unit_head *header,
1721 struct dwarf2_section_info *section,
1722 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1723 int is_debug_types_section);
1724
fd820528 1725static void init_cutu_and_read_dies
f4dc4d17
DE
1726 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1727 int use_existing_cu, int keep,
3019eac3
DE
1728 die_reader_func_ftype *die_reader_func, void *data);
1729
dee91e82
DE
1730static void init_cutu_and_read_dies_simple
1731 (struct dwarf2_per_cu_data *this_cu,
1732 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1733
673bfd45 1734static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1735
3019eac3
DE
1736static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1737
1738static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1739 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1740
1741static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1742 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1743
1744static void free_dwo_file_cleanup (void *);
1745
95554aad
TT
1746static void process_cu_includes (void);
1747
1b80a9fa
JK
1748static void check_producer (struct dwarf2_cu *cu);
1749
9291a0cd
TT
1750#if WORDS_BIGENDIAN
1751
1752/* Convert VALUE between big- and little-endian. */
1753static offset_type
1754byte_swap (offset_type value)
1755{
1756 offset_type result;
1757
1758 result = (value & 0xff) << 24;
1759 result |= (value & 0xff00) << 8;
1760 result |= (value & 0xff0000) >> 8;
1761 result |= (value & 0xff000000) >> 24;
1762 return result;
1763}
1764
1765#define MAYBE_SWAP(V) byte_swap (V)
1766
1767#else
1768#define MAYBE_SWAP(V) (V)
1769#endif /* WORDS_BIGENDIAN */
1770
1771/* The suffix for an index file. */
1772#define INDEX_SUFFIX ".gdb-index"
1773
c906108c 1774/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1775 information and return true if we have enough to do something.
1776 NAMES points to the dwarf2 section names, or is NULL if the standard
1777 ELF names are used. */
c906108c
SS
1778
1779int
251d32d9
TG
1780dwarf2_has_info (struct objfile *objfile,
1781 const struct dwarf2_debug_sections *names)
c906108c 1782{
be391dca
TT
1783 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1784 if (!dwarf2_per_objfile)
1785 {
1786 /* Initialize per-objfile state. */
1787 struct dwarf2_per_objfile *data
1788 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1789
be391dca
TT
1790 memset (data, 0, sizeof (*data));
1791 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1792 dwarf2_per_objfile = data;
6502dd73 1793
251d32d9
TG
1794 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1795 (void *) names);
be391dca
TT
1796 dwarf2_per_objfile->objfile = objfile;
1797 }
1798 return (dwarf2_per_objfile->info.asection != NULL
1799 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1800}
1801
251d32d9
TG
1802/* When loading sections, we look either for uncompressed section or for
1803 compressed section names. */
233a11ab
CS
1804
1805static int
251d32d9
TG
1806section_is_p (const char *section_name,
1807 const struct dwarf2_section_names *names)
233a11ab 1808{
251d32d9
TG
1809 if (names->normal != NULL
1810 && strcmp (section_name, names->normal) == 0)
1811 return 1;
1812 if (names->compressed != NULL
1813 && strcmp (section_name, names->compressed) == 0)
1814 return 1;
1815 return 0;
233a11ab
CS
1816}
1817
c906108c
SS
1818/* This function is mapped across the sections and remembers the
1819 offset and size of each of the debugging sections we are interested
1820 in. */
1821
1822static void
251d32d9 1823dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1824{
251d32d9 1825 const struct dwarf2_debug_sections *names;
dc7650b8 1826 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1827
1828 if (vnames == NULL)
1829 names = &dwarf2_elf_names;
1830 else
1831 names = (const struct dwarf2_debug_sections *) vnames;
1832
dc7650b8
JK
1833 if ((aflag & SEC_HAS_CONTENTS) == 0)
1834 {
1835 }
1836 else if (section_is_p (sectp->name, &names->info))
c906108c 1837 {
dce234bc
PP
1838 dwarf2_per_objfile->info.asection = sectp;
1839 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1840 }
251d32d9 1841 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1842 {
dce234bc
PP
1843 dwarf2_per_objfile->abbrev.asection = sectp;
1844 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1845 }
251d32d9 1846 else if (section_is_p (sectp->name, &names->line))
c906108c 1847 {
dce234bc
PP
1848 dwarf2_per_objfile->line.asection = sectp;
1849 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1850 }
251d32d9 1851 else if (section_is_p (sectp->name, &names->loc))
c906108c 1852 {
dce234bc
PP
1853 dwarf2_per_objfile->loc.asection = sectp;
1854 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1855 }
251d32d9 1856 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1857 {
dce234bc
PP
1858 dwarf2_per_objfile->macinfo.asection = sectp;
1859 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1860 }
cf2c3c16
TT
1861 else if (section_is_p (sectp->name, &names->macro))
1862 {
1863 dwarf2_per_objfile->macro.asection = sectp;
1864 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1865 }
251d32d9 1866 else if (section_is_p (sectp->name, &names->str))
c906108c 1867 {
dce234bc
PP
1868 dwarf2_per_objfile->str.asection = sectp;
1869 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1870 }
3019eac3
DE
1871 else if (section_is_p (sectp->name, &names->addr))
1872 {
1873 dwarf2_per_objfile->addr.asection = sectp;
1874 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1875 }
251d32d9 1876 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1877 {
dce234bc
PP
1878 dwarf2_per_objfile->frame.asection = sectp;
1879 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1880 }
251d32d9 1881 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1882 {
dc7650b8
JK
1883 dwarf2_per_objfile->eh_frame.asection = sectp;
1884 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1885 }
251d32d9 1886 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1887 {
dce234bc
PP
1888 dwarf2_per_objfile->ranges.asection = sectp;
1889 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1890 }
251d32d9 1891 else if (section_is_p (sectp->name, &names->types))
348e048f 1892 {
8b70b953
TT
1893 struct dwarf2_section_info type_section;
1894
1895 memset (&type_section, 0, sizeof (type_section));
1896 type_section.asection = sectp;
1897 type_section.size = bfd_get_section_size (sectp);
1898
1899 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1900 &type_section);
348e048f 1901 }
251d32d9 1902 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1903 {
1904 dwarf2_per_objfile->gdb_index.asection = sectp;
1905 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1906 }
dce234bc 1907
72dca2f5
FR
1908 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1909 && bfd_section_vma (abfd, sectp) == 0)
1910 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1911}
1912
fceca515
DE
1913/* A helper function that decides whether a section is empty,
1914 or not present. */
9e0ac564
TT
1915
1916static int
1917dwarf2_section_empty_p (struct dwarf2_section_info *info)
1918{
1919 return info->asection == NULL || info->size == 0;
1920}
1921
3019eac3
DE
1922/* Read the contents of the section INFO.
1923 OBJFILE is the main object file, but not necessarily the file where
1924 the section comes from. E.g., for DWO files INFO->asection->owner
1925 is the bfd of the DWO file.
dce234bc 1926 If the section is compressed, uncompress it before returning. */
c906108c 1927
dce234bc
PP
1928static void
1929dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1930{
dce234bc 1931 asection *sectp = info->asection;
3019eac3 1932 bfd *abfd;
dce234bc
PP
1933 gdb_byte *buf, *retbuf;
1934 unsigned char header[4];
c906108c 1935
be391dca
TT
1936 if (info->readin)
1937 return;
dce234bc 1938 info->buffer = NULL;
be391dca 1939 info->readin = 1;
188dd5d6 1940
9e0ac564 1941 if (dwarf2_section_empty_p (info))
dce234bc 1942 return;
c906108c 1943
3019eac3
DE
1944 abfd = sectp->owner;
1945
4bf44c1c
TT
1946 /* If the section has relocations, we must read it ourselves.
1947 Otherwise we attach it to the BFD. */
1948 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1949 {
4bf44c1c 1950 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1951
4bf44c1c
TT
1952 /* We have to cast away const here for historical reasons.
1953 Fixing dwarf2read to be const-correct would be quite nice. */
1954 info->buffer = (gdb_byte *) bytes;
1955 return;
dce234bc 1956 }
dce234bc 1957
4bf44c1c
TT
1958 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1959 info->buffer = buf;
dce234bc
PP
1960
1961 /* When debugging .o files, we may need to apply relocations; see
1962 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1963 We never compress sections in .o files, so we only need to
1964 try this when the section is not compressed. */
ac8035ab 1965 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1966 if (retbuf != NULL)
1967 {
1968 info->buffer = retbuf;
1969 return;
1970 }
1971
1972 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1973 || bfd_bread (buf, info->size, abfd) != info->size)
1974 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1975 bfd_get_filename (abfd));
1976}
1977
9e0ac564
TT
1978/* A helper function that returns the size of a section in a safe way.
1979 If you are positive that the section has been read before using the
1980 size, then it is safe to refer to the dwarf2_section_info object's
1981 "size" field directly. In other cases, you must call this
1982 function, because for compressed sections the size field is not set
1983 correctly until the section has been read. */
1984
1985static bfd_size_type
1986dwarf2_section_size (struct objfile *objfile,
1987 struct dwarf2_section_info *info)
1988{
1989 if (!info->readin)
1990 dwarf2_read_section (objfile, info);
1991 return info->size;
1992}
1993
dce234bc 1994/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1995 SECTION_NAME. */
af34e669 1996
dce234bc 1997void
3017a003
TG
1998dwarf2_get_section_info (struct objfile *objfile,
1999 enum dwarf2_section_enum sect,
dce234bc
PP
2000 asection **sectp, gdb_byte **bufp,
2001 bfd_size_type *sizep)
2002{
2003 struct dwarf2_per_objfile *data
2004 = objfile_data (objfile, dwarf2_objfile_data_key);
2005 struct dwarf2_section_info *info;
a3b2a86b
TT
2006
2007 /* We may see an objfile without any DWARF, in which case we just
2008 return nothing. */
2009 if (data == NULL)
2010 {
2011 *sectp = NULL;
2012 *bufp = NULL;
2013 *sizep = 0;
2014 return;
2015 }
3017a003
TG
2016 switch (sect)
2017 {
2018 case DWARF2_DEBUG_FRAME:
2019 info = &data->frame;
2020 break;
2021 case DWARF2_EH_FRAME:
2022 info = &data->eh_frame;
2023 break;
2024 default:
2025 gdb_assert_not_reached ("unexpected section");
2026 }
dce234bc 2027
9e0ac564 2028 dwarf2_read_section (objfile, info);
dce234bc
PP
2029
2030 *sectp = info->asection;
2031 *bufp = info->buffer;
2032 *sizep = info->size;
2033}
2034
36586728
TT
2035/* A helper function to find the sections for a .dwz file. */
2036
2037static void
2038locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2039{
2040 struct dwz_file *dwz_file = arg;
2041
2042 /* Note that we only support the standard ELF names, because .dwz
2043 is ELF-only (at the time of writing). */
2044 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2045 {
2046 dwz_file->abbrev.asection = sectp;
2047 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2048 }
2049 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2050 {
2051 dwz_file->info.asection = sectp;
2052 dwz_file->info.size = bfd_get_section_size (sectp);
2053 }
2054 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2055 {
2056 dwz_file->str.asection = sectp;
2057 dwz_file->str.size = bfd_get_section_size (sectp);
2058 }
2059 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2060 {
2061 dwz_file->line.asection = sectp;
2062 dwz_file->line.size = bfd_get_section_size (sectp);
2063 }
2064 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2065 {
2066 dwz_file->macro.asection = sectp;
2067 dwz_file->macro.size = bfd_get_section_size (sectp);
2068 }
2ec9a5e0
TT
2069 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2070 {
2071 dwz_file->gdb_index.asection = sectp;
2072 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2073 }
36586728
TT
2074}
2075
2076/* Open the separate '.dwz' debug file, if needed. Error if the file
2077 cannot be found. */
2078
2079static struct dwz_file *
2080dwarf2_get_dwz_file (void)
2081{
2082 bfd *abfd, *dwz_bfd;
2083 asection *section;
2084 gdb_byte *data;
2085 struct cleanup *cleanup;
2086 const char *filename;
2087 struct dwz_file *result;
2088
2089 if (dwarf2_per_objfile->dwz_file != NULL)
2090 return dwarf2_per_objfile->dwz_file;
2091
2092 abfd = dwarf2_per_objfile->objfile->obfd;
2093 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2094 if (section == NULL)
2095 error (_("could not find '.gnu_debugaltlink' section"));
2096 if (!bfd_malloc_and_get_section (abfd, section, &data))
2097 error (_("could not read '.gnu_debugaltlink' section: %s"),
2098 bfd_errmsg (bfd_get_error ()));
2099 cleanup = make_cleanup (xfree, data);
2100
2101 filename = data;
2102 if (!IS_ABSOLUTE_PATH (filename))
2103 {
2104 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2105 char *rel;
2106
2107 make_cleanup (xfree, abs);
2108 abs = ldirname (abs);
2109 make_cleanup (xfree, abs);
2110
2111 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2112 make_cleanup (xfree, rel);
2113 filename = rel;
2114 }
2115
2116 /* The format is just a NUL-terminated file name, followed by the
2117 build-id. For now, though, we ignore the build-id. */
2118 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2119 if (dwz_bfd == NULL)
2120 error (_("could not read '%s': %s"), filename,
2121 bfd_errmsg (bfd_get_error ()));
2122
2123 if (!bfd_check_format (dwz_bfd, bfd_object))
2124 {
2125 gdb_bfd_unref (dwz_bfd);
2126 error (_("file '%s' was not usable: %s"), filename,
2127 bfd_errmsg (bfd_get_error ()));
2128 }
2129
2130 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2131 struct dwz_file);
2132 result->dwz_bfd = dwz_bfd;
2133
2134 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2135
2136 do_cleanups (cleanup);
2137
8d2cc612 2138 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2139 return result;
2140}
9291a0cd 2141\f
7b9f3c50
DE
2142/* DWARF quick_symbols_functions support. */
2143
2144/* TUs can share .debug_line entries, and there can be a lot more TUs than
2145 unique line tables, so we maintain a separate table of all .debug_line
2146 derived entries to support the sharing.
2147 All the quick functions need is the list of file names. We discard the
2148 line_header when we're done and don't need to record it here. */
2149struct quick_file_names
2150{
094b34ac
DE
2151 /* The data used to construct the hash key. */
2152 struct stmt_list_hash hash;
7b9f3c50
DE
2153
2154 /* The number of entries in file_names, real_names. */
2155 unsigned int num_file_names;
2156
2157 /* The file names from the line table, after being run through
2158 file_full_name. */
2159 const char **file_names;
2160
2161 /* The file names from the line table after being run through
2162 gdb_realpath. These are computed lazily. */
2163 const char **real_names;
2164};
2165
2166/* When using the index (and thus not using psymtabs), each CU has an
2167 object of this type. This is used to hold information needed by
2168 the various "quick" methods. */
2169struct dwarf2_per_cu_quick_data
2170{
2171 /* The file table. This can be NULL if there was no file table
2172 or it's currently not read in.
2173 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2174 struct quick_file_names *file_names;
2175
2176 /* The corresponding symbol table. This is NULL if symbols for this
2177 CU have not yet been read. */
2178 struct symtab *symtab;
2179
2180 /* A temporary mark bit used when iterating over all CUs in
2181 expand_symtabs_matching. */
2182 unsigned int mark : 1;
2183
2184 /* True if we've tried to read the file table and found there isn't one.
2185 There will be no point in trying to read it again next time. */
2186 unsigned int no_file_data : 1;
2187};
2188
094b34ac
DE
2189/* Utility hash function for a stmt_list_hash. */
2190
2191static hashval_t
2192hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2193{
2194 hashval_t v = 0;
2195
2196 if (stmt_list_hash->dwo_unit != NULL)
2197 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2198 v += stmt_list_hash->line_offset.sect_off;
2199 return v;
2200}
2201
2202/* Utility equality function for a stmt_list_hash. */
2203
2204static int
2205eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2206 const struct stmt_list_hash *rhs)
2207{
2208 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2209 return 0;
2210 if (lhs->dwo_unit != NULL
2211 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2212 return 0;
2213
2214 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2215}
2216
7b9f3c50
DE
2217/* Hash function for a quick_file_names. */
2218
2219static hashval_t
2220hash_file_name_entry (const void *e)
2221{
2222 const struct quick_file_names *file_data = e;
2223
094b34ac 2224 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2225}
2226
2227/* Equality function for a quick_file_names. */
2228
2229static int
2230eq_file_name_entry (const void *a, const void *b)
2231{
2232 const struct quick_file_names *ea = a;
2233 const struct quick_file_names *eb = b;
2234
094b34ac 2235 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2236}
2237
2238/* Delete function for a quick_file_names. */
2239
2240static void
2241delete_file_name_entry (void *e)
2242{
2243 struct quick_file_names *file_data = e;
2244 int i;
2245
2246 for (i = 0; i < file_data->num_file_names; ++i)
2247 {
2248 xfree ((void*) file_data->file_names[i]);
2249 if (file_data->real_names)
2250 xfree ((void*) file_data->real_names[i]);
2251 }
2252
2253 /* The space for the struct itself lives on objfile_obstack,
2254 so we don't free it here. */
2255}
2256
2257/* Create a quick_file_names hash table. */
2258
2259static htab_t
2260create_quick_file_names_table (unsigned int nr_initial_entries)
2261{
2262 return htab_create_alloc (nr_initial_entries,
2263 hash_file_name_entry, eq_file_name_entry,
2264 delete_file_name_entry, xcalloc, xfree);
2265}
9291a0cd 2266
918dd910
JK
2267/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2268 have to be created afterwards. You should call age_cached_comp_units after
2269 processing PER_CU->CU. dw2_setup must have been already called. */
2270
2271static void
2272load_cu (struct dwarf2_per_cu_data *per_cu)
2273{
3019eac3 2274 if (per_cu->is_debug_types)
e5fe5e75 2275 load_full_type_unit (per_cu);
918dd910 2276 else
95554aad 2277 load_full_comp_unit (per_cu, language_minimal);
918dd910 2278
918dd910 2279 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2280
2281 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2282}
2283
a0f42c21 2284/* Read in the symbols for PER_CU. */
2fdf6df6 2285
9291a0cd 2286static void
a0f42c21 2287dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2288{
2289 struct cleanup *back_to;
2290
f4dc4d17
DE
2291 /* Skip type_unit_groups, reading the type units they contain
2292 is handled elsewhere. */
2293 if (IS_TYPE_UNIT_GROUP (per_cu))
2294 return;
2295
9291a0cd
TT
2296 back_to = make_cleanup (dwarf2_release_queue, NULL);
2297
95554aad
TT
2298 if (dwarf2_per_objfile->using_index
2299 ? per_cu->v.quick->symtab == NULL
2300 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2301 {
2302 queue_comp_unit (per_cu, language_minimal);
2303 load_cu (per_cu);
2304 }
9291a0cd 2305
a0f42c21 2306 process_queue ();
9291a0cd
TT
2307
2308 /* Age the cache, releasing compilation units that have not
2309 been used recently. */
2310 age_cached_comp_units ();
2311
2312 do_cleanups (back_to);
2313}
2314
2315/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2316 the objfile from which this CU came. Returns the resulting symbol
2317 table. */
2fdf6df6 2318
9291a0cd 2319static struct symtab *
a0f42c21 2320dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2321{
95554aad 2322 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2323 if (!per_cu->v.quick->symtab)
2324 {
2325 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2326 increment_reading_symtab ();
a0f42c21 2327 dw2_do_instantiate_symtab (per_cu);
95554aad 2328 process_cu_includes ();
9291a0cd
TT
2329 do_cleanups (back_to);
2330 }
2331 return per_cu->v.quick->symtab;
2332}
2333
f4dc4d17
DE
2334/* Return the CU given its index.
2335
2336 This is intended for loops like:
2337
2338 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2339 + dwarf2_per_objfile->n_type_units); ++i)
2340 {
2341 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2342
2343 ...;
2344 }
2345*/
2fdf6df6 2346
1fd400ff
TT
2347static struct dwarf2_per_cu_data *
2348dw2_get_cu (int index)
2349{
2350 if (index >= dwarf2_per_objfile->n_comp_units)
2351 {
f4dc4d17 2352 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2353 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2354 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2355 }
2356
2357 return dwarf2_per_objfile->all_comp_units[index];
2358}
2359
2360/* Return the primary CU given its index.
2361 The difference between this function and dw2_get_cu is in the handling
2362 of type units (TUs). Here we return the type_unit_group object.
2363
2364 This is intended for loops like:
2365
2366 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2367 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2368 {
2369 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2370
2371 ...;
2372 }
2373*/
2374
2375static struct dwarf2_per_cu_data *
2376dw2_get_primary_cu (int index)
2377{
2378 if (index >= dwarf2_per_objfile->n_comp_units)
2379 {
1fd400ff 2380 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2381 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2382 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2383 }
f4dc4d17 2384
1fd400ff
TT
2385 return dwarf2_per_objfile->all_comp_units[index];
2386}
2387
2ec9a5e0
TT
2388/* A helper for create_cus_from_index that handles a given list of
2389 CUs. */
2fdf6df6 2390
74a0d9f6 2391static void
2ec9a5e0
TT
2392create_cus_from_index_list (struct objfile *objfile,
2393 const gdb_byte *cu_list, offset_type n_elements,
2394 struct dwarf2_section_info *section,
2395 int is_dwz,
2396 int base_offset)
9291a0cd
TT
2397{
2398 offset_type i;
9291a0cd 2399
2ec9a5e0 2400 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2401 {
2402 struct dwarf2_per_cu_data *the_cu;
2403 ULONGEST offset, length;
2404
74a0d9f6
JK
2405 gdb_static_assert (sizeof (ULONGEST) >= 8);
2406 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2407 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2408 cu_list += 2 * 8;
2409
2410 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2411 struct dwarf2_per_cu_data);
b64f50a1 2412 the_cu->offset.sect_off = offset;
9291a0cd
TT
2413 the_cu->length = length;
2414 the_cu->objfile = objfile;
8a0459fd 2415 the_cu->section = section;
9291a0cd
TT
2416 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2417 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2418 the_cu->is_dwz = is_dwz;
2419 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2420 }
9291a0cd
TT
2421}
2422
2ec9a5e0 2423/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2424 the CU objects for this objfile. */
2ec9a5e0 2425
74a0d9f6 2426static void
2ec9a5e0
TT
2427create_cus_from_index (struct objfile *objfile,
2428 const gdb_byte *cu_list, offset_type cu_list_elements,
2429 const gdb_byte *dwz_list, offset_type dwz_elements)
2430{
2431 struct dwz_file *dwz;
2432
2433 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2434 dwarf2_per_objfile->all_comp_units
2435 = obstack_alloc (&objfile->objfile_obstack,
2436 dwarf2_per_objfile->n_comp_units
2437 * sizeof (struct dwarf2_per_cu_data *));
2438
74a0d9f6
JK
2439 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2440 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2441
2442 if (dwz_elements == 0)
74a0d9f6 2443 return;
2ec9a5e0
TT
2444
2445 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2446 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2447 cu_list_elements / 2);
2ec9a5e0
TT
2448}
2449
1fd400ff 2450/* Create the signatured type hash table from the index. */
673bfd45 2451
74a0d9f6 2452static void
673bfd45 2453create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2454 struct dwarf2_section_info *section,
673bfd45
DE
2455 const gdb_byte *bytes,
2456 offset_type elements)
1fd400ff
TT
2457{
2458 offset_type i;
673bfd45 2459 htab_t sig_types_hash;
1fd400ff 2460
d467dd73
DE
2461 dwarf2_per_objfile->n_type_units = elements / 3;
2462 dwarf2_per_objfile->all_type_units
1fd400ff 2463 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2464 dwarf2_per_objfile->n_type_units
b4dd5633 2465 * sizeof (struct signatured_type *));
1fd400ff 2466
673bfd45 2467 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2468
2469 for (i = 0; i < elements; i += 3)
2470 {
52dc124a
DE
2471 struct signatured_type *sig_type;
2472 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2473 void **slot;
2474
74a0d9f6
JK
2475 gdb_static_assert (sizeof (ULONGEST) >= 8);
2476 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2477 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2478 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2479 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2480 bytes += 3 * 8;
2481
52dc124a 2482 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2483 struct signatured_type);
52dc124a 2484 sig_type->signature = signature;
3019eac3
DE
2485 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2486 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2487 sig_type->per_cu.section = section;
52dc124a
DE
2488 sig_type->per_cu.offset.sect_off = offset;
2489 sig_type->per_cu.objfile = objfile;
2490 sig_type->per_cu.v.quick
1fd400ff
TT
2491 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2492 struct dwarf2_per_cu_quick_data);
2493
52dc124a
DE
2494 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2495 *slot = sig_type;
1fd400ff 2496
b4dd5633 2497 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2498 }
2499
673bfd45 2500 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2501}
2502
9291a0cd
TT
2503/* Read the address map data from the mapped index, and use it to
2504 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2505
9291a0cd
TT
2506static void
2507create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2508{
2509 const gdb_byte *iter, *end;
2510 struct obstack temp_obstack;
2511 struct addrmap *mutable_map;
2512 struct cleanup *cleanup;
2513 CORE_ADDR baseaddr;
2514
2515 obstack_init (&temp_obstack);
2516 cleanup = make_cleanup_obstack_free (&temp_obstack);
2517 mutable_map = addrmap_create_mutable (&temp_obstack);
2518
2519 iter = index->address_table;
2520 end = iter + index->address_table_size;
2521
2522 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2523
2524 while (iter < end)
2525 {
2526 ULONGEST hi, lo, cu_index;
2527 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2528 iter += 8;
2529 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2530 iter += 8;
2531 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2532 iter += 4;
f652bce2
DE
2533
2534 if (cu_index < dwarf2_per_objfile->n_comp_units)
2535 {
2536 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2537 dw2_get_cu (cu_index));
2538 }
2539 else
2540 {
2541 complaint (&symfile_complaints,
2542 _(".gdb_index address table has invalid CU number %u"),
2543 (unsigned) cu_index);
2544 }
9291a0cd
TT
2545 }
2546
2547 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2548 &objfile->objfile_obstack);
2549 do_cleanups (cleanup);
2550}
2551
59d7bcaf
JK
2552/* The hash function for strings in the mapped index. This is the same as
2553 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2554 implementation. This is necessary because the hash function is tied to the
2555 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2556 SYMBOL_HASH_NEXT.
2557
2558 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2559
9291a0cd 2560static hashval_t
559a7a62 2561mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2562{
2563 const unsigned char *str = (const unsigned char *) p;
2564 hashval_t r = 0;
2565 unsigned char c;
2566
2567 while ((c = *str++) != 0)
559a7a62
JK
2568 {
2569 if (index_version >= 5)
2570 c = tolower (c);
2571 r = r * 67 + c - 113;
2572 }
9291a0cd
TT
2573
2574 return r;
2575}
2576
2577/* Find a slot in the mapped index INDEX for the object named NAME.
2578 If NAME is found, set *VEC_OUT to point to the CU vector in the
2579 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2580
9291a0cd
TT
2581static int
2582find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2583 offset_type **vec_out)
2584{
0cf03b49
JK
2585 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2586 offset_type hash;
9291a0cd 2587 offset_type slot, step;
559a7a62 2588 int (*cmp) (const char *, const char *);
9291a0cd 2589
0cf03b49
JK
2590 if (current_language->la_language == language_cplus
2591 || current_language->la_language == language_java
2592 || current_language->la_language == language_fortran)
2593 {
2594 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2595 not contain any. */
2596 const char *paren = strchr (name, '(');
2597
2598 if (paren)
2599 {
2600 char *dup;
2601
2602 dup = xmalloc (paren - name + 1);
2603 memcpy (dup, name, paren - name);
2604 dup[paren - name] = 0;
2605
2606 make_cleanup (xfree, dup);
2607 name = dup;
2608 }
2609 }
2610
559a7a62 2611 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2612 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2613 simulate our NAME being searched is also lowercased. */
2614 hash = mapped_index_string_hash ((index->version == 4
2615 && case_sensitivity == case_sensitive_off
2616 ? 5 : index->version),
2617 name);
2618
3876f04e
DE
2619 slot = hash & (index->symbol_table_slots - 1);
2620 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2621 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2622
2623 for (;;)
2624 {
2625 /* Convert a slot number to an offset into the table. */
2626 offset_type i = 2 * slot;
2627 const char *str;
3876f04e 2628 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2629 {
2630 do_cleanups (back_to);
2631 return 0;
2632 }
9291a0cd 2633
3876f04e 2634 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2635 if (!cmp (name, str))
9291a0cd
TT
2636 {
2637 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2638 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2639 do_cleanups (back_to);
9291a0cd
TT
2640 return 1;
2641 }
2642
3876f04e 2643 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2644 }
2645}
2646
2ec9a5e0
TT
2647/* A helper function that reads the .gdb_index from SECTION and fills
2648 in MAP. FILENAME is the name of the file containing the section;
2649 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2650 ok to use deprecated sections.
2651
2652 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2653 out parameters that are filled in with information about the CU and
2654 TU lists in the section.
2655
2656 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2657
9291a0cd 2658static int
2ec9a5e0
TT
2659read_index_from_section (struct objfile *objfile,
2660 const char *filename,
2661 int deprecated_ok,
2662 struct dwarf2_section_info *section,
2663 struct mapped_index *map,
2664 const gdb_byte **cu_list,
2665 offset_type *cu_list_elements,
2666 const gdb_byte **types_list,
2667 offset_type *types_list_elements)
9291a0cd 2668{
9291a0cd 2669 char *addr;
2ec9a5e0 2670 offset_type version;
b3b272e1 2671 offset_type *metadata;
1fd400ff 2672 int i;
9291a0cd 2673
2ec9a5e0 2674 if (dwarf2_section_empty_p (section))
9291a0cd 2675 return 0;
82430852
JK
2676
2677 /* Older elfutils strip versions could keep the section in the main
2678 executable while splitting it for the separate debug info file. */
2ec9a5e0 2679 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2680 return 0;
2681
2ec9a5e0 2682 dwarf2_read_section (objfile, section);
9291a0cd 2683
2ec9a5e0 2684 addr = section->buffer;
9291a0cd 2685 /* Version check. */
1fd400ff 2686 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2687 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2688 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2689 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2690 indices. */
831adc1f 2691 if (version < 4)
481860b3
GB
2692 {
2693 static int warning_printed = 0;
2694 if (!warning_printed)
2695 {
2696 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2697 filename);
481860b3
GB
2698 warning_printed = 1;
2699 }
2700 return 0;
2701 }
2702 /* Index version 4 uses a different hash function than index version
2703 5 and later.
2704
2705 Versions earlier than 6 did not emit psymbols for inlined
2706 functions. Using these files will cause GDB not to be able to
2707 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2708 indices unless the user has done
2709 "set use-deprecated-index-sections on". */
2ec9a5e0 2710 if (version < 6 && !deprecated_ok)
481860b3
GB
2711 {
2712 static int warning_printed = 0;
2713 if (!warning_printed)
2714 {
e615022a
DE
2715 warning (_("\
2716Skipping deprecated .gdb_index section in %s.\n\
2717Do \"set use-deprecated-index-sections on\" before the file is read\n\
2718to use the section anyway."),
2ec9a5e0 2719 filename);
481860b3
GB
2720 warning_printed = 1;
2721 }
2722 return 0;
2723 }
796a7ff8
DE
2724 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2725 of the TU (for symbols coming from TUs). It's just a performance bug, and
2726 we can't distinguish gdb-generated indices from gold-generated ones, so
2727 nothing to do here. */
2728
481860b3 2729 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2730 longer backward compatible. */
796a7ff8 2731 if (version > 8)
594e8718 2732 return 0;
9291a0cd 2733
559a7a62 2734 map->version = version;
2ec9a5e0 2735 map->total_size = section->size;
9291a0cd
TT
2736
2737 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2738
2739 i = 0;
2ec9a5e0
TT
2740 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2741 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2742 / 8);
1fd400ff
TT
2743 ++i;
2744
2ec9a5e0
TT
2745 *types_list = addr + MAYBE_SWAP (metadata[i]);
2746 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2747 - MAYBE_SWAP (metadata[i]))
2748 / 8);
987d643c 2749 ++i;
1fd400ff
TT
2750
2751 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2752 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2753 - MAYBE_SWAP (metadata[i]));
2754 ++i;
2755
3876f04e
DE
2756 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2757 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2758 - MAYBE_SWAP (metadata[i]))
2759 / (2 * sizeof (offset_type)));
1fd400ff 2760 ++i;
9291a0cd 2761
1fd400ff
TT
2762 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2763
2ec9a5e0
TT
2764 return 1;
2765}
2766
2767
2768/* Read the index file. If everything went ok, initialize the "quick"
2769 elements of all the CUs and return 1. Otherwise, return 0. */
2770
2771static int
2772dwarf2_read_index (struct objfile *objfile)
2773{
2774 struct mapped_index local_map, *map;
2775 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2776 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2777
2778 if (!read_index_from_section (objfile, objfile->name,
2779 use_deprecated_index_sections,
2780 &dwarf2_per_objfile->gdb_index, &local_map,
2781 &cu_list, &cu_list_elements,
2782 &types_list, &types_list_elements))
2783 return 0;
2784
0fefef59 2785 /* Don't use the index if it's empty. */
2ec9a5e0 2786 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2787 return 0;
2788
2ec9a5e0
TT
2789 /* If there is a .dwz file, read it so we can get its CU list as
2790 well. */
2791 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2792 {
2793 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2794 struct mapped_index dwz_map;
2795 const gdb_byte *dwz_types_ignore;
2796 offset_type dwz_types_elements_ignore;
2797
2798 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2799 1,
2800 &dwz->gdb_index, &dwz_map,
2801 &dwz_list, &dwz_list_elements,
2802 &dwz_types_ignore,
2803 &dwz_types_elements_ignore))
2804 {
2805 warning (_("could not read '.gdb_index' section from %s; skipping"),
2806 bfd_get_filename (dwz->dwz_bfd));
2807 return 0;
2808 }
2809 }
2810
74a0d9f6
JK
2811 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2812 dwz_list_elements);
1fd400ff 2813
8b70b953
TT
2814 if (types_list_elements)
2815 {
2816 struct dwarf2_section_info *section;
2817
2818 /* We can only handle a single .debug_types when we have an
2819 index. */
2820 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2821 return 0;
2822
2823 section = VEC_index (dwarf2_section_info_def,
2824 dwarf2_per_objfile->types, 0);
2825
74a0d9f6
JK
2826 create_signatured_type_table_from_index (objfile, section, types_list,
2827 types_list_elements);
8b70b953 2828 }
9291a0cd 2829
2ec9a5e0
TT
2830 create_addrmap_from_index (objfile, &local_map);
2831
2832 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2833 *map = local_map;
9291a0cd
TT
2834
2835 dwarf2_per_objfile->index_table = map;
2836 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2837 dwarf2_per_objfile->quick_file_names_table =
2838 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2839
2840 return 1;
2841}
2842
2843/* A helper for the "quick" functions which sets the global
2844 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2845
9291a0cd
TT
2846static void
2847dw2_setup (struct objfile *objfile)
2848{
2849 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2850 gdb_assert (dwarf2_per_objfile);
2851}
2852
dee91e82 2853/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2854
dee91e82
DE
2855static void
2856dw2_get_file_names_reader (const struct die_reader_specs *reader,
2857 gdb_byte *info_ptr,
2858 struct die_info *comp_unit_die,
2859 int has_children,
2860 void *data)
9291a0cd 2861{
dee91e82
DE
2862 struct dwarf2_cu *cu = reader->cu;
2863 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2864 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2865 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2866 struct line_header *lh;
9291a0cd 2867 struct attribute *attr;
dee91e82 2868 int i;
15d034d0 2869 const char *name, *comp_dir;
7b9f3c50
DE
2870 void **slot;
2871 struct quick_file_names *qfn;
2872 unsigned int line_offset;
9291a0cd 2873
07261596
TT
2874 /* Our callers never want to match partial units -- instead they
2875 will match the enclosing full CU. */
2876 if (comp_unit_die->tag == DW_TAG_partial_unit)
2877 {
2878 this_cu->v.quick->no_file_data = 1;
2879 return;
2880 }
2881
094b34ac
DE
2882 /* If we're reading the line header for TUs, store it in the "per_cu"
2883 for tu_group. */
2884 if (this_cu->is_debug_types)
2885 {
2886 struct type_unit_group *tu_group = data;
2887
2888 gdb_assert (tu_group != NULL);
2889 lh_cu = &tu_group->per_cu;
2890 }
2891 else
2892 lh_cu = this_cu;
2893
7b9f3c50
DE
2894 lh = NULL;
2895 slot = NULL;
2896 line_offset = 0;
dee91e82
DE
2897
2898 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2899 if (attr)
2900 {
7b9f3c50
DE
2901 struct quick_file_names find_entry;
2902
2903 line_offset = DW_UNSND (attr);
2904
2905 /* We may have already read in this line header (TU line header sharing).
2906 If we have we're done. */
094b34ac
DE
2907 find_entry.hash.dwo_unit = cu->dwo_unit;
2908 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2909 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2910 &find_entry, INSERT);
2911 if (*slot != NULL)
2912 {
094b34ac 2913 lh_cu->v.quick->file_names = *slot;
dee91e82 2914 return;
7b9f3c50
DE
2915 }
2916
3019eac3 2917 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2918 }
2919 if (lh == NULL)
2920 {
094b34ac 2921 lh_cu->v.quick->no_file_data = 1;
dee91e82 2922 return;
9291a0cd
TT
2923 }
2924
7b9f3c50 2925 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2926 qfn->hash.dwo_unit = cu->dwo_unit;
2927 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2928 gdb_assert (slot != NULL);
2929 *slot = qfn;
9291a0cd 2930
dee91e82 2931 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2932
7b9f3c50
DE
2933 qfn->num_file_names = lh->num_file_names;
2934 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2935 lh->num_file_names * sizeof (char *));
9291a0cd 2936 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2937 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2938 qfn->real_names = NULL;
9291a0cd 2939
7b9f3c50 2940 free_line_header (lh);
7b9f3c50 2941
094b34ac 2942 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2943}
2944
2945/* A helper for the "quick" functions which attempts to read the line
2946 table for THIS_CU. */
2947
2948static struct quick_file_names *
e4a48d9d 2949dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2950{
f4dc4d17
DE
2951 /* For TUs this should only be called on the parent group. */
2952 if (this_cu->is_debug_types)
2953 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2954
dee91e82
DE
2955 if (this_cu->v.quick->file_names != NULL)
2956 return this_cu->v.quick->file_names;
2957 /* If we know there is no line data, no point in looking again. */
2958 if (this_cu->v.quick->no_file_data)
2959 return NULL;
2960
3019eac3
DE
2961 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2962 in the stub for CUs, there's is no need to lookup the DWO file.
2963 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2964 DWO file. */
2965 if (this_cu->is_debug_types)
094b34ac 2966 {
796a7ff8 2967 struct type_unit_group *tu_group = this_cu->type_unit_group;
094b34ac
DE
2968
2969 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2970 dw2_get_file_names_reader, tu_group);
2971 }
3019eac3
DE
2972 else
2973 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2974
2975 if (this_cu->v.quick->no_file_data)
2976 return NULL;
2977 return this_cu->v.quick->file_names;
9291a0cd
TT
2978}
2979
2980/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2981 real path for a given file name from the line table. */
2fdf6df6 2982
9291a0cd 2983static const char *
7b9f3c50
DE
2984dw2_get_real_path (struct objfile *objfile,
2985 struct quick_file_names *qfn, int index)
9291a0cd 2986{
7b9f3c50
DE
2987 if (qfn->real_names == NULL)
2988 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2989 qfn->num_file_names, sizeof (char *));
9291a0cd 2990
7b9f3c50
DE
2991 if (qfn->real_names[index] == NULL)
2992 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2993
7b9f3c50 2994 return qfn->real_names[index];
9291a0cd
TT
2995}
2996
2997static struct symtab *
2998dw2_find_last_source_symtab (struct objfile *objfile)
2999{
3000 int index;
ae2de4f8 3001
9291a0cd
TT
3002 dw2_setup (objfile);
3003 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3004 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3005}
3006
7b9f3c50
DE
3007/* Traversal function for dw2_forget_cached_source_info. */
3008
3009static int
3010dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3011{
7b9f3c50 3012 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3013
7b9f3c50 3014 if (file_data->real_names)
9291a0cd 3015 {
7b9f3c50 3016 int i;
9291a0cd 3017
7b9f3c50 3018 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3019 {
7b9f3c50
DE
3020 xfree ((void*) file_data->real_names[i]);
3021 file_data->real_names[i] = NULL;
9291a0cd
TT
3022 }
3023 }
7b9f3c50
DE
3024
3025 return 1;
3026}
3027
3028static void
3029dw2_forget_cached_source_info (struct objfile *objfile)
3030{
3031 dw2_setup (objfile);
3032
3033 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3034 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3035}
3036
f8eba3c6
TT
3037/* Helper function for dw2_map_symtabs_matching_filename that expands
3038 the symtabs and calls the iterator. */
3039
3040static int
3041dw2_map_expand_apply (struct objfile *objfile,
3042 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3043 const char *name, const char *real_path,
f8eba3c6
TT
3044 int (*callback) (struct symtab *, void *),
3045 void *data)
3046{
3047 struct symtab *last_made = objfile->symtabs;
3048
3049 /* Don't visit already-expanded CUs. */
3050 if (per_cu->v.quick->symtab)
3051 return 0;
3052
3053 /* This may expand more than one symtab, and we want to iterate over
3054 all of them. */
a0f42c21 3055 dw2_instantiate_symtab (per_cu);
f8eba3c6 3056
f5b95b50 3057 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3058 objfile->symtabs, last_made);
3059}
3060
3061/* Implementation of the map_symtabs_matching_filename method. */
3062
9291a0cd 3063static int
f8eba3c6 3064dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3065 const char *real_path,
f8eba3c6
TT
3066 int (*callback) (struct symtab *, void *),
3067 void *data)
9291a0cd
TT
3068{
3069 int i;
c011a4f4 3070 const char *name_basename = lbasename (name);
9291a0cd
TT
3071
3072 dw2_setup (objfile);
ae2de4f8 3073
848e3e78
DE
3074 /* The rule is CUs specify all the files, including those used by
3075 any TU, so there's no need to scan TUs here. */
f4dc4d17 3076
848e3e78 3077 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3078 {
3079 int j;
f4dc4d17 3080 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3081 struct quick_file_names *file_data;
9291a0cd 3082
3d7bb9d9 3083 /* We only need to look at symtabs not already expanded. */
e254ef6a 3084 if (per_cu->v.quick->symtab)
9291a0cd
TT
3085 continue;
3086
e4a48d9d 3087 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3088 if (file_data == NULL)
9291a0cd
TT
3089 continue;
3090
7b9f3c50 3091 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3092 {
7b9f3c50 3093 const char *this_name = file_data->file_names[j];
da235a7c 3094 const char *this_real_name;
9291a0cd 3095
af529f8f 3096 if (compare_filenames_for_search (this_name, name))
9291a0cd 3097 {
f5b95b50 3098 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3099 callback, data))
3100 return 1;
288e77a7 3101 continue;
4aac40c8 3102 }
9291a0cd 3103
c011a4f4
DE
3104 /* Before we invoke realpath, which can get expensive when many
3105 files are involved, do a quick comparison of the basenames. */
3106 if (! basenames_may_differ
3107 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3108 continue;
3109
da235a7c
JK
3110 this_real_name = dw2_get_real_path (objfile, file_data, j);
3111 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3112 {
da235a7c
JK
3113 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3114 callback, data))
3115 return 1;
288e77a7 3116 continue;
da235a7c 3117 }
9291a0cd 3118
da235a7c
JK
3119 if (real_path != NULL)
3120 {
af529f8f
JK
3121 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3122 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3123 if (this_real_name != NULL
af529f8f 3124 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3125 {
f5b95b50 3126 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3127 callback, data))
3128 return 1;
288e77a7 3129 continue;
9291a0cd
TT
3130 }
3131 }
3132 }
3133 }
3134
9291a0cd
TT
3135 return 0;
3136}
3137
da51c347
DE
3138/* Struct used to manage iterating over all CUs looking for a symbol. */
3139
3140struct dw2_symtab_iterator
9291a0cd 3141{
da51c347
DE
3142 /* The internalized form of .gdb_index. */
3143 struct mapped_index *index;
3144 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3145 int want_specific_block;
3146 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3147 Unused if !WANT_SPECIFIC_BLOCK. */
3148 int block_index;
3149 /* The kind of symbol we're looking for. */
3150 domain_enum domain;
3151 /* The list of CUs from the index entry of the symbol,
3152 or NULL if not found. */
3153 offset_type *vec;
3154 /* The next element in VEC to look at. */
3155 int next;
3156 /* The number of elements in VEC, or zero if there is no match. */
3157 int length;
3158};
9291a0cd 3159
da51c347
DE
3160/* Initialize the index symtab iterator ITER.
3161 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3162 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3163
9291a0cd 3164static void
da51c347
DE
3165dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3166 struct mapped_index *index,
3167 int want_specific_block,
3168 int block_index,
3169 domain_enum domain,
3170 const char *name)
3171{
3172 iter->index = index;
3173 iter->want_specific_block = want_specific_block;
3174 iter->block_index = block_index;
3175 iter->domain = domain;
3176 iter->next = 0;
3177
3178 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3179 iter->length = MAYBE_SWAP (*iter->vec);
3180 else
3181 {
3182 iter->vec = NULL;
3183 iter->length = 0;
3184 }
3185}
3186
3187/* Return the next matching CU or NULL if there are no more. */
3188
3189static struct dwarf2_per_cu_data *
3190dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3191{
3192 for ( ; iter->next < iter->length; ++iter->next)
3193 {
3194 offset_type cu_index_and_attrs =
3195 MAYBE_SWAP (iter->vec[iter->next + 1]);
3196 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3197 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3198 int want_static = iter->block_index != GLOBAL_BLOCK;
3199 /* This value is only valid for index versions >= 7. */
3200 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3201 gdb_index_symbol_kind symbol_kind =
3202 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3203 /* Only check the symbol attributes if they're present.
3204 Indices prior to version 7 don't record them,
3205 and indices >= 7 may elide them for certain symbols
3206 (gold does this). */
3207 int attrs_valid =
3208 (iter->index->version >= 7
3209 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3210
3211 /* Skip if already read in. */
3212 if (per_cu->v.quick->symtab)
3213 continue;
3214
3215 if (attrs_valid
3216 && iter->want_specific_block
3217 && want_static != is_static)
3218 continue;
3219
3220 /* Only check the symbol's kind if it has one. */
3221 if (attrs_valid)
3222 {
3223 switch (iter->domain)
3224 {
3225 case VAR_DOMAIN:
3226 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3227 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3228 /* Some types are also in VAR_DOMAIN. */
3229 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3230 continue;
3231 break;
3232 case STRUCT_DOMAIN:
3233 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3234 continue;
3235 break;
3236 case LABEL_DOMAIN:
3237 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3238 continue;
3239 break;
3240 default:
3241 break;
3242 }
3243 }
3244
3245 ++iter->next;
3246 return per_cu;
3247 }
3248
3249 return NULL;
3250}
3251
3252static struct symtab *
3253dw2_lookup_symbol (struct objfile *objfile, int block_index,
3254 const char *name, domain_enum domain)
9291a0cd 3255{
da51c347 3256 struct symtab *stab_best = NULL;
156942c7
DE
3257 struct mapped_index *index;
3258
9291a0cd
TT
3259 dw2_setup (objfile);
3260
156942c7
DE
3261 index = dwarf2_per_objfile->index_table;
3262
da51c347 3263 /* index is NULL if OBJF_READNOW. */
156942c7 3264 if (index)
9291a0cd 3265 {
da51c347
DE
3266 struct dw2_symtab_iterator iter;
3267 struct dwarf2_per_cu_data *per_cu;
3268
3269 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3270
da51c347 3271 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3272 {
da51c347
DE
3273 struct symbol *sym = NULL;
3274 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3275
3276 /* Some caution must be observed with overloaded functions
3277 and methods, since the index will not contain any overload
3278 information (but NAME might contain it). */
3279 if (stab->primary)
9291a0cd 3280 {
da51c347
DE
3281 struct blockvector *bv = BLOCKVECTOR (stab);
3282 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3283
da51c347
DE
3284 sym = lookup_block_symbol (block, name, domain);
3285 }
1fd400ff 3286
da51c347
DE
3287 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3288 {
3289 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3290 return stab;
3291
3292 stab_best = stab;
9291a0cd 3293 }
da51c347
DE
3294
3295 /* Keep looking through other CUs. */
9291a0cd
TT
3296 }
3297 }
9291a0cd 3298
da51c347 3299 return stab_best;
9291a0cd
TT
3300}
3301
3302static void
3303dw2_print_stats (struct objfile *objfile)
3304{
e4a48d9d 3305 int i, total, count;
9291a0cd
TT
3306
3307 dw2_setup (objfile);
e4a48d9d 3308 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3309 count = 0;
e4a48d9d 3310 for (i = 0; i < total; ++i)
9291a0cd 3311 {
e254ef6a 3312 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3313
e254ef6a 3314 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3315 ++count;
3316 }
e4a48d9d 3317 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3318 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3319}
3320
3321static void
3322dw2_dump (struct objfile *objfile)
3323{
3324 /* Nothing worth printing. */
3325}
3326
3327static void
3328dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3329 struct section_offsets *delta)
3330{
3331 /* There's nothing to relocate here. */
3332}
3333
3334static void
3335dw2_expand_symtabs_for_function (struct objfile *objfile,
3336 const char *func_name)
3337{
da51c347
DE
3338 struct mapped_index *index;
3339
3340 dw2_setup (objfile);
3341
3342 index = dwarf2_per_objfile->index_table;
3343
3344 /* index is NULL if OBJF_READNOW. */
3345 if (index)
3346 {
3347 struct dw2_symtab_iterator iter;
3348 struct dwarf2_per_cu_data *per_cu;
3349
3350 /* Note: It doesn't matter what we pass for block_index here. */
3351 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3352 func_name);
3353
3354 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3355 dw2_instantiate_symtab (per_cu);
3356 }
9291a0cd
TT
3357}
3358
3359static void
3360dw2_expand_all_symtabs (struct objfile *objfile)
3361{
3362 int i;
3363
3364 dw2_setup (objfile);
1fd400ff
TT
3365
3366 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3367 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3368 {
e254ef6a 3369 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3370
a0f42c21 3371 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3372 }
3373}
3374
3375static void
652a8996
JK
3376dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3377 const char *fullname)
9291a0cd
TT
3378{
3379 int i;
3380
3381 dw2_setup (objfile);
d4637a04
DE
3382
3383 /* We don't need to consider type units here.
3384 This is only called for examining code, e.g. expand_line_sal.
3385 There can be an order of magnitude (or more) more type units
3386 than comp units, and we avoid them if we can. */
3387
3388 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3389 {
3390 int j;
e254ef6a 3391 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3392 struct quick_file_names *file_data;
9291a0cd 3393
3d7bb9d9 3394 /* We only need to look at symtabs not already expanded. */
e254ef6a 3395 if (per_cu->v.quick->symtab)
9291a0cd
TT
3396 continue;
3397
e4a48d9d 3398 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3399 if (file_data == NULL)
9291a0cd
TT
3400 continue;
3401
7b9f3c50 3402 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3403 {
652a8996
JK
3404 const char *this_fullname = file_data->file_names[j];
3405
3406 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3407 {
a0f42c21 3408 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3409 break;
3410 }
3411 }
3412 }
3413}
3414
356d9f9d
TT
3415/* A helper function for dw2_find_symbol_file that finds the primary
3416 file name for a given CU. This is a die_reader_func. */
3417
3418static void
3419dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3420 gdb_byte *info_ptr,
3421 struct die_info *comp_unit_die,
3422 int has_children,
3423 void *data)
3424{
3425 const char **result_ptr = data;
3426 struct dwarf2_cu *cu = reader->cu;
3427 struct attribute *attr;
3428
3429 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3430 if (attr == NULL)
3431 *result_ptr = NULL;
3432 else
3433 *result_ptr = DW_STRING (attr);
3434}
3435
dd786858 3436static const char *
9291a0cd
TT
3437dw2_find_symbol_file (struct objfile *objfile, const char *name)
3438{
e254ef6a 3439 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3440 offset_type *vec;
356d9f9d 3441 const char *filename;
9291a0cd
TT
3442
3443 dw2_setup (objfile);
3444
ae2de4f8 3445 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3446 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3447 {
3448 struct symtab *s;
3449
d790cf0a
DE
3450 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3451 {
3452 struct blockvector *bv = BLOCKVECTOR (s);
3453 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3454 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3455
3456 if (sym)
652a8996
JK
3457 {
3458 /* Only file extension of returned filename is recognized. */
3459 return SYMBOL_SYMTAB (sym)->filename;
3460 }
d790cf0a 3461 }
96408a79
SA
3462 return NULL;
3463 }
9291a0cd
TT
3464
3465 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3466 name, &vec))
3467 return NULL;
3468
3469 /* Note that this just looks at the very first one named NAME -- but
3470 actually we are looking for a function. find_main_filename
3471 should be rewritten so that it doesn't require a custom hook. It
3472 could just use the ordinary symbol tables. */
3473 /* vec[0] is the length, which must always be >0. */
156942c7 3474 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3475
356d9f9d 3476 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3477 {
3478 /* Only file extension of returned filename is recognized. */
3479 return per_cu->v.quick->symtab->filename;
3480 }
356d9f9d 3481
f4dc4d17
DE
3482 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3483 dw2_get_primary_filename_reader, &filename);
9291a0cd 3484
652a8996 3485 /* Only file extension of returned filename is recognized. */
356d9f9d 3486 return filename;
9291a0cd
TT
3487}
3488
3489static void
40658b94
PH
3490dw2_map_matching_symbols (const char * name, domain_enum namespace,
3491 struct objfile *objfile, int global,
3492 int (*callback) (struct block *,
3493 struct symbol *, void *),
2edb89d3
JK
3494 void *data, symbol_compare_ftype *match,
3495 symbol_compare_ftype *ordered_compare)
9291a0cd 3496{
40658b94 3497 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3498 current language is Ada for a non-Ada objfile using GNU index. As Ada
3499 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3500}
3501
3502static void
f8eba3c6
TT
3503dw2_expand_symtabs_matching
3504 (struct objfile *objfile,
fbd9ab74 3505 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3506 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3507 enum search_domain kind,
3508 void *data)
9291a0cd
TT
3509{
3510 int i;
3511 offset_type iter;
4b5246aa 3512 struct mapped_index *index;
9291a0cd
TT
3513
3514 dw2_setup (objfile);
ae2de4f8
DE
3515
3516 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3517 if (!dwarf2_per_objfile->index_table)
3518 return;
4b5246aa 3519 index = dwarf2_per_objfile->index_table;
9291a0cd 3520
7b08b9eb 3521 if (file_matcher != NULL)
24c79950
TT
3522 {
3523 struct cleanup *cleanup;
3524 htab_t visited_found, visited_not_found;
3525
3526 visited_found = htab_create_alloc (10,
3527 htab_hash_pointer, htab_eq_pointer,
3528 NULL, xcalloc, xfree);
3529 cleanup = make_cleanup_htab_delete (visited_found);
3530 visited_not_found = htab_create_alloc (10,
3531 htab_hash_pointer, htab_eq_pointer,
3532 NULL, xcalloc, xfree);
3533 make_cleanup_htab_delete (visited_not_found);
3534
848e3e78
DE
3535 /* The rule is CUs specify all the files, including those used by
3536 any TU, so there's no need to scan TUs here. */
3537
3538 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3539 {
3540 int j;
f4dc4d17 3541 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3542 struct quick_file_names *file_data;
3543 void **slot;
7b08b9eb 3544
24c79950 3545 per_cu->v.quick->mark = 0;
3d7bb9d9 3546
24c79950
TT
3547 /* We only need to look at symtabs not already expanded. */
3548 if (per_cu->v.quick->symtab)
3549 continue;
7b08b9eb 3550
e4a48d9d 3551 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3552 if (file_data == NULL)
3553 continue;
7b08b9eb 3554
24c79950
TT
3555 if (htab_find (visited_not_found, file_data) != NULL)
3556 continue;
3557 else if (htab_find (visited_found, file_data) != NULL)
3558 {
3559 per_cu->v.quick->mark = 1;
3560 continue;
3561 }
3562
3563 for (j = 0; j < file_data->num_file_names; ++j)
3564 {
da235a7c
JK
3565 const char *this_real_name;
3566
fbd9ab74 3567 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3568 {
3569 per_cu->v.quick->mark = 1;
3570 break;
3571 }
da235a7c
JK
3572
3573 /* Before we invoke realpath, which can get expensive when many
3574 files are involved, do a quick comparison of the basenames. */
3575 if (!basenames_may_differ
3576 && !file_matcher (lbasename (file_data->file_names[j]),
3577 data, 1))
3578 continue;
3579
3580 this_real_name = dw2_get_real_path (objfile, file_data, j);
3581 if (file_matcher (this_real_name, data, 0))
3582 {
3583 per_cu->v.quick->mark = 1;
3584 break;
3585 }
24c79950
TT
3586 }
3587
3588 slot = htab_find_slot (per_cu->v.quick->mark
3589 ? visited_found
3590 : visited_not_found,
3591 file_data, INSERT);
3592 *slot = file_data;
3593 }
3594
3595 do_cleanups (cleanup);
3596 }
9291a0cd 3597
3876f04e 3598 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3599 {
3600 offset_type idx = 2 * iter;
3601 const char *name;
3602 offset_type *vec, vec_len, vec_idx;
3603
3876f04e 3604 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3605 continue;
3606
3876f04e 3607 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3608
e078317b 3609 if (! (*name_matcher) (name, data))
9291a0cd
TT
3610 continue;
3611
3612 /* The name was matched, now expand corresponding CUs that were
3613 marked. */
4b5246aa 3614 vec = (offset_type *) (index->constant_pool
3876f04e 3615 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3616 vec_len = MAYBE_SWAP (vec[0]);
3617 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3618 {
e254ef6a 3619 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3620 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3621 gdb_index_symbol_kind symbol_kind =
3622 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3623 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3624
3625 /* Don't crash on bad data. */
3626 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3627 + dwarf2_per_objfile->n_type_units))
156942c7 3628 continue;
1fd400ff 3629
156942c7
DE
3630 /* Only check the symbol's kind if it has one.
3631 Indices prior to version 7 don't record it. */
3632 if (index->version >= 7)
3633 {
3634 switch (kind)
3635 {
3636 case VARIABLES_DOMAIN:
3637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3638 continue;
3639 break;
3640 case FUNCTIONS_DOMAIN:
3641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3642 continue;
3643 break;
3644 case TYPES_DOMAIN:
3645 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3646 continue;
3647 break;
3648 default:
3649 break;
3650 }
3651 }
3652
3653 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3654 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3655 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3656 }
3657 }
3658}
3659
9703b513
TT
3660/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3661 symtab. */
3662
3663static struct symtab *
3664recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3665{
3666 int i;
3667
3668 if (BLOCKVECTOR (symtab) != NULL
3669 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3670 return symtab;
3671
a3ec0bb1
DE
3672 if (symtab->includes == NULL)
3673 return NULL;
3674
9703b513
TT
3675 for (i = 0; symtab->includes[i]; ++i)
3676 {
a3ec0bb1 3677 struct symtab *s = symtab->includes[i];
9703b513
TT
3678
3679 s = recursively_find_pc_sect_symtab (s, pc);
3680 if (s != NULL)
3681 return s;
3682 }
3683
3684 return NULL;
3685}
3686
9291a0cd
TT
3687static struct symtab *
3688dw2_find_pc_sect_symtab (struct objfile *objfile,
3689 struct minimal_symbol *msymbol,
3690 CORE_ADDR pc,
3691 struct obj_section *section,
3692 int warn_if_readin)
3693{
3694 struct dwarf2_per_cu_data *data;
9703b513 3695 struct symtab *result;
9291a0cd
TT
3696
3697 dw2_setup (objfile);
3698
3699 if (!objfile->psymtabs_addrmap)
3700 return NULL;
3701
3702 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3703 if (!data)
3704 return NULL;
3705
3706 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3707 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3708 paddress (get_objfile_arch (objfile), pc));
3709
9703b513
TT
3710 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3711 gdb_assert (result != NULL);
3712 return result;
9291a0cd
TT
3713}
3714
9291a0cd 3715static void
44b13c5a 3716dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3717 void *data, int need_fullname)
9291a0cd
TT
3718{
3719 int i;
24c79950
TT
3720 struct cleanup *cleanup;
3721 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3722 NULL, xcalloc, xfree);
9291a0cd 3723
24c79950 3724 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3725 dw2_setup (objfile);
ae2de4f8 3726
848e3e78
DE
3727 /* The rule is CUs specify all the files, including those used by
3728 any TU, so there's no need to scan TUs here.
3729 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3730
848e3e78 3731 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3732 {
3733 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3734
3735 if (per_cu->v.quick->symtab)
3736 {
3737 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3738 INSERT);
3739
3740 *slot = per_cu->v.quick->file_names;
3741 }
3742 }
3743
848e3e78 3744 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3745 {
3746 int j;
f4dc4d17 3747 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3748 struct quick_file_names *file_data;
24c79950 3749 void **slot;
9291a0cd 3750
3d7bb9d9 3751 /* We only need to look at symtabs not already expanded. */
e254ef6a 3752 if (per_cu->v.quick->symtab)
9291a0cd
TT
3753 continue;
3754
e4a48d9d 3755 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3756 if (file_data == NULL)
9291a0cd
TT
3757 continue;
3758
24c79950
TT
3759 slot = htab_find_slot (visited, file_data, INSERT);
3760 if (*slot)
3761 {
3762 /* Already visited. */
3763 continue;
3764 }
3765 *slot = file_data;
3766
7b9f3c50 3767 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3768 {
74e2f255
DE
3769 const char *this_real_name;
3770
3771 if (need_fullname)
3772 this_real_name = dw2_get_real_path (objfile, file_data, j);
3773 else
3774 this_real_name = NULL;
7b9f3c50 3775 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3776 }
3777 }
24c79950
TT
3778
3779 do_cleanups (cleanup);
9291a0cd
TT
3780}
3781
3782static int
3783dw2_has_symbols (struct objfile *objfile)
3784{
3785 return 1;
3786}
3787
3788const struct quick_symbol_functions dwarf2_gdb_index_functions =
3789{
3790 dw2_has_symbols,
3791 dw2_find_last_source_symtab,
3792 dw2_forget_cached_source_info,
f8eba3c6 3793 dw2_map_symtabs_matching_filename,
9291a0cd 3794 dw2_lookup_symbol,
9291a0cd
TT
3795 dw2_print_stats,
3796 dw2_dump,
3797 dw2_relocate,
3798 dw2_expand_symtabs_for_function,
3799 dw2_expand_all_symtabs,
652a8996 3800 dw2_expand_symtabs_with_fullname,
9291a0cd 3801 dw2_find_symbol_file,
40658b94 3802 dw2_map_matching_symbols,
9291a0cd
TT
3803 dw2_expand_symtabs_matching,
3804 dw2_find_pc_sect_symtab,
9291a0cd
TT
3805 dw2_map_symbol_filenames
3806};
3807
3808/* Initialize for reading DWARF for this objfile. Return 0 if this
3809 file will use psymtabs, or 1 if using the GNU index. */
3810
3811int
3812dwarf2_initialize_objfile (struct objfile *objfile)
3813{
3814 /* If we're about to read full symbols, don't bother with the
3815 indices. In this case we also don't care if some other debug
3816 format is making psymtabs, because they are all about to be
3817 expanded anyway. */
3818 if ((objfile->flags & OBJF_READNOW))
3819 {
3820 int i;
3821
3822 dwarf2_per_objfile->using_index = 1;
3823 create_all_comp_units (objfile);
0e50663e 3824 create_all_type_units (objfile);
7b9f3c50
DE
3825 dwarf2_per_objfile->quick_file_names_table =
3826 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3827
1fd400ff 3828 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3829 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3830 {
e254ef6a 3831 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3832
e254ef6a
DE
3833 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3834 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3835 }
3836
3837 /* Return 1 so that gdb sees the "quick" functions. However,
3838 these functions will be no-ops because we will have expanded
3839 all symtabs. */
3840 return 1;
3841 }
3842
3843 if (dwarf2_read_index (objfile))
3844 return 1;
3845
9291a0cd
TT
3846 return 0;
3847}
3848
3849\f
3850
dce234bc
PP
3851/* Build a partial symbol table. */
3852
3853void
f29dff0a 3854dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3855{
c9bf0622
TT
3856 volatile struct gdb_exception except;
3857
f29dff0a 3858 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3859 {
3860 init_psymbol_list (objfile, 1024);
3861 }
3862
c9bf0622
TT
3863 TRY_CATCH (except, RETURN_MASK_ERROR)
3864 {
3865 /* This isn't really ideal: all the data we allocate on the
3866 objfile's obstack is still uselessly kept around. However,
3867 freeing it seems unsafe. */
3868 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3869
3870 dwarf2_build_psymtabs_hard (objfile);
3871 discard_cleanups (cleanups);
3872 }
3873 if (except.reason < 0)
3874 exception_print (gdb_stderr, except);
c906108c 3875}
c906108c 3876
1ce1cefd
DE
3877/* Return the total length of the CU described by HEADER. */
3878
3879static unsigned int
3880get_cu_length (const struct comp_unit_head *header)
3881{
3882 return header->initial_length_size + header->length;
3883}
3884
45452591
DE
3885/* Return TRUE if OFFSET is within CU_HEADER. */
3886
3887static inline int
b64f50a1 3888offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3889{
b64f50a1 3890 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3891 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3892
b64f50a1 3893 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3894}
3895
3b80fe9b
DE
3896/* Find the base address of the compilation unit for range lists and
3897 location lists. It will normally be specified by DW_AT_low_pc.
3898 In DWARF-3 draft 4, the base address could be overridden by
3899 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3900 compilation units with discontinuous ranges. */
3901
3902static void
3903dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3904{
3905 struct attribute *attr;
3906
3907 cu->base_known = 0;
3908 cu->base_address = 0;
3909
3910 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3911 if (attr)
3912 {
3913 cu->base_address = DW_ADDR (attr);
3914 cu->base_known = 1;
3915 }
3916 else
3917 {
3918 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3919 if (attr)
3920 {
3921 cu->base_address = DW_ADDR (attr);
3922 cu->base_known = 1;
3923 }
3924 }
3925}
3926
93311388
DE
3927/* Read in the comp unit header information from the debug_info at info_ptr.
3928 NOTE: This leaves members offset, first_die_offset to be filled in
3929 by the caller. */
107d2387 3930
fe1b8b76 3931static gdb_byte *
107d2387 3932read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3933 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3934{
3935 int signed_addr;
891d2f0b 3936 unsigned int bytes_read;
c764a876
DE
3937
3938 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3939 cu_header->initial_length_size = bytes_read;
3940 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3941 info_ptr += bytes_read;
107d2387
AC
3942 cu_header->version = read_2_bytes (abfd, info_ptr);
3943 info_ptr += 2;
b64f50a1
JK
3944 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3945 &bytes_read);
613e1657 3946 info_ptr += bytes_read;
107d2387
AC
3947 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3948 info_ptr += 1;
3949 signed_addr = bfd_get_sign_extend_vma (abfd);
3950 if (signed_addr < 0)
8e65ff28 3951 internal_error (__FILE__, __LINE__,
e2e0b3e5 3952 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3953 cu_header->signed_addr_p = signed_addr;
c764a876 3954
107d2387
AC
3955 return info_ptr;
3956}
3957
36586728
TT
3958/* Helper function that returns the proper abbrev section for
3959 THIS_CU. */
3960
3961static struct dwarf2_section_info *
3962get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3963{
3964 struct dwarf2_section_info *abbrev;
3965
3966 if (this_cu->is_dwz)
3967 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3968 else
3969 abbrev = &dwarf2_per_objfile->abbrev;
3970
3971 return abbrev;
3972}
3973
9ff913ba
DE
3974/* Subroutine of read_and_check_comp_unit_head and
3975 read_and_check_type_unit_head to simplify them.
3976 Perform various error checking on the header. */
3977
3978static void
3979error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3980 struct dwarf2_section_info *section,
3981 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3982{
3983 bfd *abfd = section->asection->owner;
3984 const char *filename = bfd_get_filename (abfd);
3985
3986 if (header->version != 2 && header->version != 3 && header->version != 4)
3987 error (_("Dwarf Error: wrong version in compilation unit header "
3988 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3989 filename);
3990
b64f50a1 3991 if (header->abbrev_offset.sect_off
36586728 3992 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3993 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3994 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3995 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3996 filename);
3997
3998 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3999 avoid potential 32-bit overflow. */
1ce1cefd 4000 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4001 > section->size)
4002 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4003 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4004 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4005 filename);
4006}
4007
4008/* Read in a CU/TU header and perform some basic error checking.
4009 The contents of the header are stored in HEADER.
4010 The result is a pointer to the start of the first DIE. */
adabb602 4011
fe1b8b76 4012static gdb_byte *
9ff913ba
DE
4013read_and_check_comp_unit_head (struct comp_unit_head *header,
4014 struct dwarf2_section_info *section,
4bdcc0c1 4015 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
4016 gdb_byte *info_ptr,
4017 int is_debug_types_section)
72bf9492 4018{
fe1b8b76 4019 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4020 bfd *abfd = section->asection->owner;
72bf9492 4021
b64f50a1 4022 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4023
72bf9492
DJ
4024 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4025
460c1c54
CC
4026 /* If we're reading a type unit, skip over the signature and
4027 type_offset fields. */
b0df02fd 4028 if (is_debug_types_section)
460c1c54
CC
4029 info_ptr += 8 /*signature*/ + header->offset_size;
4030
b64f50a1 4031 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4032
4bdcc0c1 4033 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4034
4035 return info_ptr;
4036}
4037
348e048f
DE
4038/* Read in the types comp unit header information from .debug_types entry at
4039 types_ptr. The result is a pointer to one past the end of the header. */
4040
4041static gdb_byte *
9ff913ba
DE
4042read_and_check_type_unit_head (struct comp_unit_head *header,
4043 struct dwarf2_section_info *section,
4bdcc0c1 4044 struct dwarf2_section_info *abbrev_section,
9ff913ba 4045 gdb_byte *info_ptr,
dee91e82
DE
4046 ULONGEST *signature,
4047 cu_offset *type_offset_in_tu)
348e048f 4048{
9ff913ba
DE
4049 gdb_byte *beg_of_comp_unit = info_ptr;
4050 bfd *abfd = section->asection->owner;
348e048f 4051
b64f50a1 4052 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4053
9ff913ba 4054 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4055
9ff913ba
DE
4056 /* If we're reading a type unit, skip over the signature and
4057 type_offset fields. */
4058 if (signature != NULL)
4059 *signature = read_8_bytes (abfd, info_ptr);
4060 info_ptr += 8;
dee91e82
DE
4061 if (type_offset_in_tu != NULL)
4062 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4063 header->offset_size);
9ff913ba
DE
4064 info_ptr += header->offset_size;
4065
b64f50a1 4066 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4067
4bdcc0c1 4068 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4069
4070 return info_ptr;
348e048f
DE
4071}
4072
f4dc4d17
DE
4073/* Fetch the abbreviation table offset from a comp or type unit header. */
4074
4075static sect_offset
4076read_abbrev_offset (struct dwarf2_section_info *section,
4077 sect_offset offset)
4078{
4079 bfd *abfd = section->asection->owner;
4080 gdb_byte *info_ptr;
4081 unsigned int length, initial_length_size, offset_size;
4082 sect_offset abbrev_offset;
4083
4084 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4085 info_ptr = section->buffer + offset.sect_off;
4086 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4087 offset_size = initial_length_size == 4 ? 4 : 8;
4088 info_ptr += initial_length_size + 2 /*version*/;
4089 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4090 return abbrev_offset;
4091}
4092
aaa75496
JB
4093/* Allocate a new partial symtab for file named NAME and mark this new
4094 partial symtab as being an include of PST. */
4095
4096static void
4097dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4098 struct objfile *objfile)
4099{
4100 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4101
fbd9ab74
JK
4102 if (!IS_ABSOLUTE_PATH (subpst->filename))
4103 {
4104 /* It shares objfile->objfile_obstack. */
4105 subpst->dirname = pst->dirname;
4106 }
4107
aaa75496
JB
4108 subpst->section_offsets = pst->section_offsets;
4109 subpst->textlow = 0;
4110 subpst->texthigh = 0;
4111
4112 subpst->dependencies = (struct partial_symtab **)
4113 obstack_alloc (&objfile->objfile_obstack,
4114 sizeof (struct partial_symtab *));
4115 subpst->dependencies[0] = pst;
4116 subpst->number_of_dependencies = 1;
4117
4118 subpst->globals_offset = 0;
4119 subpst->n_global_syms = 0;
4120 subpst->statics_offset = 0;
4121 subpst->n_static_syms = 0;
4122 subpst->symtab = NULL;
4123 subpst->read_symtab = pst->read_symtab;
4124 subpst->readin = 0;
4125
4126 /* No private part is necessary for include psymtabs. This property
4127 can be used to differentiate between such include psymtabs and
10b3939b 4128 the regular ones. */
58a9656e 4129 subpst->read_symtab_private = NULL;
aaa75496
JB
4130}
4131
4132/* Read the Line Number Program data and extract the list of files
4133 included by the source file represented by PST. Build an include
d85a05f0 4134 partial symtab for each of these included files. */
aaa75496
JB
4135
4136static void
4137dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4138 struct die_info *die,
4139 struct partial_symtab *pst)
aaa75496 4140{
d85a05f0
DJ
4141 struct line_header *lh = NULL;
4142 struct attribute *attr;
aaa75496 4143
d85a05f0
DJ
4144 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4145 if (attr)
3019eac3 4146 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4147 if (lh == NULL)
4148 return; /* No linetable, so no includes. */
4149
c6da4cef 4150 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4151 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4152
4153 free_line_header (lh);
4154}
4155
348e048f 4156static hashval_t
52dc124a 4157hash_signatured_type (const void *item)
348e048f 4158{
52dc124a 4159 const struct signatured_type *sig_type = item;
9a619af0 4160
348e048f 4161 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4162 return sig_type->signature;
348e048f
DE
4163}
4164
4165static int
52dc124a 4166eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4167{
4168 const struct signatured_type *lhs = item_lhs;
4169 const struct signatured_type *rhs = item_rhs;
9a619af0 4170
348e048f
DE
4171 return lhs->signature == rhs->signature;
4172}
4173
1fd400ff
TT
4174/* Allocate a hash table for signatured types. */
4175
4176static htab_t
673bfd45 4177allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4178{
4179 return htab_create_alloc_ex (41,
52dc124a
DE
4180 hash_signatured_type,
4181 eq_signatured_type,
1fd400ff
TT
4182 NULL,
4183 &objfile->objfile_obstack,
4184 hashtab_obstack_allocate,
4185 dummy_obstack_deallocate);
4186}
4187
d467dd73 4188/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4189
4190static int
d467dd73 4191add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4192{
4193 struct signatured_type *sigt = *slot;
b4dd5633 4194 struct signatured_type ***datap = datum;
1fd400ff 4195
b4dd5633 4196 **datap = sigt;
1fd400ff
TT
4197 ++*datap;
4198
4199 return 1;
4200}
4201
3019eac3 4202/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4203 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4204 NULL otherwise.
4205 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4206 The result is a pointer to the hash table or NULL if there are
4207 no types. */
348e048f 4208
3019eac3
DE
4209static htab_t
4210create_debug_types_hash_table (struct dwo_file *dwo_file,
4211 VEC (dwarf2_section_info_def) *types)
348e048f 4212{
3019eac3 4213 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4214 htab_t types_htab = NULL;
8b70b953
TT
4215 int ix;
4216 struct dwarf2_section_info *section;
4bdcc0c1 4217 struct dwarf2_section_info *abbrev_section;
348e048f 4218
3019eac3
DE
4219 if (VEC_empty (dwarf2_section_info_def, types))
4220 return NULL;
348e048f 4221
4bdcc0c1
DE
4222 abbrev_section = (dwo_file != NULL
4223 ? &dwo_file->sections.abbrev
4224 : &dwarf2_per_objfile->abbrev);
4225
09406207
DE
4226 if (dwarf2_read_debug)
4227 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4228 dwo_file ? ".dwo" : "",
4229 bfd_get_filename (abbrev_section->asection->owner));
4230
8b70b953 4231 for (ix = 0;
3019eac3 4232 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4233 ++ix)
4234 {
3019eac3 4235 bfd *abfd;
8b70b953 4236 gdb_byte *info_ptr, *end_ptr;
36586728 4237 struct dwarf2_section_info *abbrev_section;
348e048f 4238
8b70b953
TT
4239 dwarf2_read_section (objfile, section);
4240 info_ptr = section->buffer;
348e048f 4241
8b70b953
TT
4242 if (info_ptr == NULL)
4243 continue;
348e048f 4244
3019eac3
DE
4245 /* We can't set abfd until now because the section may be empty or
4246 not present, in which case section->asection will be NULL. */
4247 abfd = section->asection->owner;
4248
36586728
TT
4249 if (dwo_file)
4250 abbrev_section = &dwo_file->sections.abbrev;
4251 else
4252 abbrev_section = &dwarf2_per_objfile->abbrev;
4253
dee91e82
DE
4254 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4255 because we don't need to read any dies: the signature is in the
4256 header. */
8b70b953
TT
4257
4258 end_ptr = info_ptr + section->size;
4259 while (info_ptr < end_ptr)
4260 {
b64f50a1 4261 sect_offset offset;
3019eac3 4262 cu_offset type_offset_in_tu;
8b70b953 4263 ULONGEST signature;
52dc124a 4264 struct signatured_type *sig_type;
3019eac3 4265 struct dwo_unit *dwo_tu;
8b70b953
TT
4266 void **slot;
4267 gdb_byte *ptr = info_ptr;
9ff913ba 4268 struct comp_unit_head header;
dee91e82 4269 unsigned int length;
348e048f 4270
b64f50a1 4271 offset.sect_off = ptr - section->buffer;
348e048f 4272
8b70b953 4273 /* We need to read the type's signature in order to build the hash
9ff913ba 4274 table, but we don't need anything else just yet. */
348e048f 4275
4bdcc0c1
DE
4276 ptr = read_and_check_type_unit_head (&header, section,
4277 abbrev_section, ptr,
3019eac3 4278 &signature, &type_offset_in_tu);
6caca83c 4279
1ce1cefd 4280 length = get_cu_length (&header);
dee91e82 4281
6caca83c 4282 /* Skip dummy type units. */
dee91e82
DE
4283 if (ptr >= info_ptr + length
4284 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4285 {
1ce1cefd 4286 info_ptr += length;
6caca83c
CC
4287 continue;
4288 }
8b70b953 4289
0349ea22
DE
4290 if (types_htab == NULL)
4291 {
4292 if (dwo_file)
4293 types_htab = allocate_dwo_unit_table (objfile);
4294 else
4295 types_htab = allocate_signatured_type_table (objfile);
4296 }
4297
3019eac3
DE
4298 if (dwo_file)
4299 {
4300 sig_type = NULL;
4301 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4302 struct dwo_unit);
4303 dwo_tu->dwo_file = dwo_file;
4304 dwo_tu->signature = signature;
4305 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4306 dwo_tu->section = section;
3019eac3
DE
4307 dwo_tu->offset = offset;
4308 dwo_tu->length = length;
4309 }
4310 else
4311 {
4312 /* N.B.: type_offset is not usable if this type uses a DWO file.
4313 The real type_offset is in the DWO file. */
4314 dwo_tu = NULL;
4315 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4316 struct signatured_type);
4317 sig_type->signature = signature;
4318 sig_type->type_offset_in_tu = type_offset_in_tu;
4319 sig_type->per_cu.objfile = objfile;
4320 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4321 sig_type->per_cu.section = section;
3019eac3
DE
4322 sig_type->per_cu.offset = offset;
4323 sig_type->per_cu.length = length;
4324 }
8b70b953 4325
3019eac3
DE
4326 slot = htab_find_slot (types_htab,
4327 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4328 INSERT);
8b70b953
TT
4329 gdb_assert (slot != NULL);
4330 if (*slot != NULL)
4331 {
3019eac3
DE
4332 sect_offset dup_offset;
4333
4334 if (dwo_file)
4335 {
4336 const struct dwo_unit *dup_tu = *slot;
4337
4338 dup_offset = dup_tu->offset;
4339 }
4340 else
4341 {
4342 const struct signatured_type *dup_tu = *slot;
4343
4344 dup_offset = dup_tu->per_cu.offset;
4345 }
b3c8eb43 4346
8b70b953
TT
4347 complaint (&symfile_complaints,
4348 _("debug type entry at offset 0x%x is duplicate to the "
4349 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4350 offset.sect_off, dup_offset.sect_off,
8b70b953 4351 phex (signature, sizeof (signature)));
8b70b953 4352 }
3019eac3 4353 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4354
09406207 4355 if (dwarf2_read_debug)
8b70b953 4356 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4357 offset.sect_off,
4358 phex (signature, sizeof (signature)));
348e048f 4359
dee91e82 4360 info_ptr += length;
8b70b953 4361 }
348e048f
DE
4362 }
4363
3019eac3
DE
4364 return types_htab;
4365}
4366
4367/* Create the hash table of all entries in the .debug_types section,
4368 and initialize all_type_units.
4369 The result is zero if there is an error (e.g. missing .debug_types section),
4370 otherwise non-zero. */
4371
4372static int
4373create_all_type_units (struct objfile *objfile)
4374{
4375 htab_t types_htab;
b4dd5633 4376 struct signatured_type **iter;
3019eac3
DE
4377
4378 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4379 if (types_htab == NULL)
4380 {
4381 dwarf2_per_objfile->signatured_types = NULL;
4382 return 0;
4383 }
4384
348e048f
DE
4385 dwarf2_per_objfile->signatured_types = types_htab;
4386
d467dd73
DE
4387 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4388 dwarf2_per_objfile->all_type_units
1fd400ff 4389 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4390 dwarf2_per_objfile->n_type_units
b4dd5633 4391 * sizeof (struct signatured_type *));
d467dd73
DE
4392 iter = &dwarf2_per_objfile->all_type_units[0];
4393 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4394 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4395 == dwarf2_per_objfile->n_type_units);
1fd400ff 4396
348e048f
DE
4397 return 1;
4398}
4399
380bca97 4400/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4401 Returns NULL if signature SIG is not present in the table.
4402 It is up to the caller to complain about this. */
348e048f
DE
4403
4404static struct signatured_type *
e319fa28 4405lookup_signatured_type (ULONGEST sig)
348e048f
DE
4406{
4407 struct signatured_type find_entry, *entry;
4408
4409 if (dwarf2_per_objfile->signatured_types == NULL)
5a8b3f62 4410 return NULL;
348e048f
DE
4411 find_entry.signature = sig;
4412 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4413 return entry;
4414}
42e7ad6c
DE
4415\f
4416/* Low level DIE reading support. */
348e048f 4417
d85a05f0
DJ
4418/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4419
4420static void
4421init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4422 struct dwarf2_cu *cu,
3019eac3
DE
4423 struct dwarf2_section_info *section,
4424 struct dwo_file *dwo_file)
d85a05f0 4425{
fceca515 4426 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4427 reader->abfd = section->asection->owner;
d85a05f0 4428 reader->cu = cu;
3019eac3 4429 reader->dwo_file = dwo_file;
dee91e82
DE
4430 reader->die_section = section;
4431 reader->buffer = section->buffer;
f664829e 4432 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4433}
4434
fd820528 4435/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4436 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4437
f4dc4d17
DE
4438 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4439 Otherwise the table specified in the comp unit header is read in and used.
4440 This is an optimization for when we already have the abbrev table.
4441
dee91e82
DE
4442 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4443 Otherwise, a new CU is allocated with xmalloc.
4444
4445 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4446 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4447
4448 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4449 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4450
70221824 4451static void
fd820528 4452init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4453 struct abbrev_table *abbrev_table,
fd820528
DE
4454 int use_existing_cu, int keep,
4455 die_reader_func_ftype *die_reader_func,
4456 void *data)
c906108c 4457{
dee91e82 4458 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4459 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4460 bfd *abfd = section->asection->owner;
dee91e82
DE
4461 struct dwarf2_cu *cu;
4462 gdb_byte *begin_info_ptr, *info_ptr;
4463 struct die_reader_specs reader;
d85a05f0 4464 struct die_info *comp_unit_die;
dee91e82 4465 int has_children;
d85a05f0 4466 struct attribute *attr;
dee91e82
DE
4467 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4468 struct signatured_type *sig_type = NULL;
4bdcc0c1 4469 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4470 /* Non-zero if CU currently points to a DWO file and we need to
4471 reread it. When this happens we need to reread the skeleton die
4472 before we can reread the DWO file. */
4473 int rereading_dwo_cu = 0;
c906108c 4474
09406207
DE
4475 if (dwarf2_die_debug)
4476 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4477 this_cu->is_debug_types ? "type" : "comp",
4478 this_cu->offset.sect_off);
4479
dee91e82
DE
4480 if (use_existing_cu)
4481 gdb_assert (keep);
23745b47 4482
dee91e82
DE
4483 cleanups = make_cleanup (null_cleanup, NULL);
4484
4485 /* This is cheap if the section is already read in. */
4486 dwarf2_read_section (objfile, section);
4487
4488 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4489
4490 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4491
4492 if (use_existing_cu && this_cu->cu != NULL)
4493 {
4494 cu = this_cu->cu;
42e7ad6c
DE
4495
4496 /* If this CU is from a DWO file we need to start over, we need to
4497 refetch the attributes from the skeleton CU.
4498 This could be optimized by retrieving those attributes from when we
4499 were here the first time: the previous comp_unit_die was stored in
4500 comp_unit_obstack. But there's no data yet that we need this
4501 optimization. */
4502 if (cu->dwo_unit != NULL)
4503 rereading_dwo_cu = 1;
dee91e82
DE
4504 }
4505 else
4506 {
4507 /* If !use_existing_cu, this_cu->cu must be NULL. */
4508 gdb_assert (this_cu->cu == NULL);
4509
4510 cu = xmalloc (sizeof (*cu));
4511 init_one_comp_unit (cu, this_cu);
4512
4513 /* If an error occurs while loading, release our storage. */
4514 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4515 }
dee91e82 4516
42e7ad6c
DE
4517 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4518 {
4519 /* We already have the header, there's no need to read it in again. */
4520 info_ptr += cu->header.first_die_offset.cu_off;
4521 }
4522 else
4523 {
3019eac3 4524 if (this_cu->is_debug_types)
dee91e82
DE
4525 {
4526 ULONGEST signature;
42e7ad6c 4527 cu_offset type_offset_in_tu;
dee91e82 4528
4bdcc0c1
DE
4529 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4530 abbrev_section, info_ptr,
42e7ad6c
DE
4531 &signature,
4532 &type_offset_in_tu);
dee91e82 4533
42e7ad6c
DE
4534 /* Since per_cu is the first member of struct signatured_type,
4535 we can go from a pointer to one to a pointer to the other. */
4536 sig_type = (struct signatured_type *) this_cu;
4537 gdb_assert (sig_type->signature == signature);
4538 gdb_assert (sig_type->type_offset_in_tu.cu_off
4539 == type_offset_in_tu.cu_off);
dee91e82
DE
4540 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4541
42e7ad6c
DE
4542 /* LENGTH has not been set yet for type units if we're
4543 using .gdb_index. */
1ce1cefd 4544 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4545
4546 /* Establish the type offset that can be used to lookup the type. */
4547 sig_type->type_offset_in_section.sect_off =
4548 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4549 }
4550 else
4551 {
4bdcc0c1
DE
4552 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4553 abbrev_section,
4554 info_ptr, 0);
dee91e82
DE
4555
4556 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4557 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4558 }
4559 }
10b3939b 4560
6caca83c 4561 /* Skip dummy compilation units. */
dee91e82 4562 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4563 || peek_abbrev_code (abfd, info_ptr) == 0)
4564 {
dee91e82 4565 do_cleanups (cleanups);
21b2bd31 4566 return;
6caca83c
CC
4567 }
4568
433df2d4
DE
4569 /* If we don't have them yet, read the abbrevs for this compilation unit.
4570 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4571 done. Note that it's important that if the CU had an abbrev table
4572 on entry we don't free it when we're done: Somewhere up the call stack
4573 it may be in use. */
f4dc4d17
DE
4574 if (abbrev_table != NULL)
4575 {
4576 gdb_assert (cu->abbrev_table == NULL);
4577 gdb_assert (cu->header.abbrev_offset.sect_off
4578 == abbrev_table->offset.sect_off);
4579 cu->abbrev_table = abbrev_table;
4580 }
4581 else if (cu->abbrev_table == NULL)
dee91e82 4582 {
4bdcc0c1 4583 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4584 make_cleanup (dwarf2_free_abbrev_table, cu);
4585 }
42e7ad6c
DE
4586 else if (rereading_dwo_cu)
4587 {
4588 dwarf2_free_abbrev_table (cu);
4589 dwarf2_read_abbrevs (cu, abbrev_section);
4590 }
af703f96 4591
dee91e82 4592 /* Read the top level CU/TU die. */
3019eac3 4593 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4594 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4595
3019eac3
DE
4596 /* If we have a DWO stub, process it and then read in the DWO file.
4597 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4598 a DWO CU, that this test will fail. */
4599 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4600 if (attr)
4601 {
15d034d0 4602 const char *dwo_name = DW_STRING (attr);
42e7ad6c 4603 const char *comp_dir_string;
3019eac3
DE
4604 struct dwo_unit *dwo_unit;
4605 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4606 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4607 int i,num_extra_attrs;
4bdcc0c1 4608 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4609
4610 if (has_children)
4611 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4612 " has children (offset 0x%x) [in module %s]"),
4613 this_cu->offset.sect_off, bfd_get_filename (abfd));
4614
4615 /* These attributes aren't processed until later:
4616 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4617 However, the attribute is found in the stub which we won't have later.
4618 In order to not impose this complication on the rest of the code,
4619 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4620
4621 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4622 DWO file. */
42e7ad6c 4623 stmt_list = NULL;
3019eac3
DE
4624 if (! this_cu->is_debug_types)
4625 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4626 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4627 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4628 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4629 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4630
4631 /* There should be a DW_AT_addr_base attribute here (if needed).
4632 We need the value before we can process DW_FORM_GNU_addr_index. */
4633 cu->addr_base = 0;
3019eac3
DE
4634 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4635 if (attr)
2e3cf129
DE
4636 cu->addr_base = DW_UNSND (attr);
4637
4638 /* There should be a DW_AT_ranges_base attribute here (if needed).
4639 We need the value before we can process DW_AT_ranges. */
4640 cu->ranges_base = 0;
4641 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4642 if (attr)
4643 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4644
4645 if (this_cu->is_debug_types)
4646 {
4647 gdb_assert (sig_type != NULL);
4648 signature = sig_type->signature;
4649 }
4650 else
4651 {
4652 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4653 if (! attr)
4654 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4655 dwo_name);
4656 signature = DW_UNSND (attr);
4657 }
4658
4659 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4660 comp_dir_string = NULL;
4661 if (comp_dir)
4662 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4663
4664 if (this_cu->is_debug_types)
42e7ad6c 4665 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4666 else
42e7ad6c 4667 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4668 signature);
4669
4670 if (dwo_unit == NULL)
4671 {
4672 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4673 " with ID %s [in module %s]"),
4674 this_cu->offset.sect_off,
4675 phex (signature, sizeof (signature)),
4676 objfile->name);
4677 }
4678
4679 /* Set up for reading the DWO CU/TU. */
4680 cu->dwo_unit = dwo_unit;
8a0459fd 4681 section = dwo_unit->section;
80626a55 4682 dwarf2_read_section (objfile, section);
3019eac3 4683 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4684 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4685 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4686
4687 if (this_cu->is_debug_types)
4688 {
4689 ULONGEST signature;
80626a55 4690 cu_offset type_offset_in_tu;
3019eac3 4691
4bdcc0c1
DE
4692 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4693 dwo_abbrev_section,
4694 info_ptr,
80626a55
DE
4695 &signature,
4696 &type_offset_in_tu);
3019eac3
DE
4697 gdb_assert (sig_type->signature == signature);
4698 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4699 /* For DWOs coming from DWP files, we don't know the CU length
4700 nor the type's offset in the TU until now. */
4701 dwo_unit->length = get_cu_length (&cu->header);
4702 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4703
4704 /* Establish the type offset that can be used to lookup the type.
4705 For DWO files, we don't know it until now. */
4706 sig_type->type_offset_in_section.sect_off =
4707 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4708 }
4709 else
4710 {
4bdcc0c1
DE
4711 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4712 dwo_abbrev_section,
4713 info_ptr, 0);
3019eac3 4714 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4715 /* For DWOs coming from DWP files, we don't know the CU length
4716 until now. */
4717 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4718 }
4719
4720 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4721 if (abbrev_table == NULL)
4722 {
4723 dwarf2_free_abbrev_table (cu);
4724 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4725 }
4726 else
4727 {
4728 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4729 make_cleanup (dwarf2_free_abbrev_table, cu);
4730 }
3019eac3
DE
4731
4732 /* Read in the die, but leave space to copy over the attributes
4733 from the stub. This has the benefit of simplifying the rest of
4734 the code - all the real work is done here. */
4735 num_extra_attrs = ((stmt_list != NULL)
4736 + (low_pc != NULL)
4737 + (high_pc != NULL)
42e7ad6c
DE
4738 + (ranges != NULL)
4739 + (comp_dir != NULL));
3019eac3
DE
4740 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4741 &has_children, num_extra_attrs);
4742
4743 /* Copy over the attributes from the stub to the DWO die. */
4744 i = comp_unit_die->num_attrs;
4745 if (stmt_list != NULL)
4746 comp_unit_die->attrs[i++] = *stmt_list;
4747 if (low_pc != NULL)
4748 comp_unit_die->attrs[i++] = *low_pc;
4749 if (high_pc != NULL)
4750 comp_unit_die->attrs[i++] = *high_pc;
4751 if (ranges != NULL)
4752 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4753 if (comp_dir != NULL)
4754 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4755 comp_unit_die->num_attrs += num_extra_attrs;
4756
4757 /* Skip dummy compilation units. */
4758 if (info_ptr >= begin_info_ptr + dwo_unit->length
4759 || peek_abbrev_code (abfd, info_ptr) == 0)
4760 {
4761 do_cleanups (cleanups);
4762 return;
4763 }
4764 }
4765
dee91e82
DE
4766 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4767
4768 if (free_cu_cleanup != NULL)
348e048f 4769 {
dee91e82
DE
4770 if (keep)
4771 {
4772 /* We've successfully allocated this compilation unit. Let our
4773 caller clean it up when finished with it. */
4774 discard_cleanups (free_cu_cleanup);
4775
4776 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4777 So we have to manually free the abbrev table. */
4778 dwarf2_free_abbrev_table (cu);
4779
4780 /* Link this CU into read_in_chain. */
4781 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4782 dwarf2_per_objfile->read_in_chain = this_cu;
4783 }
4784 else
4785 do_cleanups (free_cu_cleanup);
348e048f 4786 }
dee91e82
DE
4787
4788 do_cleanups (cleanups);
4789}
4790
3019eac3
DE
4791/* Read CU/TU THIS_CU in section SECTION,
4792 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4793 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4794 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4795
4796 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4797 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4798
4799 We fill in THIS_CU->length.
4800
4801 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4802 linker) then DIE_READER_FUNC will not get called.
4803
4804 THIS_CU->cu is always freed when done.
3019eac3
DE
4805 This is done in order to not leave THIS_CU->cu in a state where we have
4806 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4807
4808static void
4809init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4810 struct dwarf2_section_info *abbrev_section,
3019eac3 4811 struct dwo_file *dwo_file,
dee91e82
DE
4812 die_reader_func_ftype *die_reader_func,
4813 void *data)
4814{
4815 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4816 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4817 bfd *abfd = section->asection->owner;
dee91e82
DE
4818 struct dwarf2_cu cu;
4819 gdb_byte *begin_info_ptr, *info_ptr;
4820 struct die_reader_specs reader;
4821 struct cleanup *cleanups;
4822 struct die_info *comp_unit_die;
4823 int has_children;
4824
09406207
DE
4825 if (dwarf2_die_debug)
4826 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4827 this_cu->is_debug_types ? "type" : "comp",
4828 this_cu->offset.sect_off);
4829
dee91e82
DE
4830 gdb_assert (this_cu->cu == NULL);
4831
dee91e82
DE
4832 /* This is cheap if the section is already read in. */
4833 dwarf2_read_section (objfile, section);
4834
4835 init_one_comp_unit (&cu, this_cu);
4836
4837 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4838
4839 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4840 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4841 abbrev_section, info_ptr,
3019eac3 4842 this_cu->is_debug_types);
dee91e82 4843
1ce1cefd 4844 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4845
4846 /* Skip dummy compilation units. */
4847 if (info_ptr >= begin_info_ptr + this_cu->length
4848 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4849 {
dee91e82 4850 do_cleanups (cleanups);
21b2bd31 4851 return;
93311388 4852 }
72bf9492 4853
dee91e82
DE
4854 dwarf2_read_abbrevs (&cu, abbrev_section);
4855 make_cleanup (dwarf2_free_abbrev_table, &cu);
4856
3019eac3 4857 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4858 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4859
4860 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4861
4862 do_cleanups (cleanups);
4863}
4864
3019eac3
DE
4865/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4866 does not lookup the specified DWO file.
4867 This cannot be used to read DWO files.
dee91e82
DE
4868
4869 THIS_CU->cu is always freed when done.
3019eac3
DE
4870 This is done in order to not leave THIS_CU->cu in a state where we have
4871 to care whether it refers to the "main" CU or the DWO CU.
4872 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4873
4874static void
4875init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4876 die_reader_func_ftype *die_reader_func,
4877 void *data)
4878{
4879 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4880 get_abbrev_section_for_cu (this_cu),
3019eac3 4881 NULL,
dee91e82
DE
4882 die_reader_func, data);
4883}
0018ea6f
DE
4884\f
4885/* Type Unit Groups.
dee91e82 4886
0018ea6f
DE
4887 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4888 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4889 so that all types coming from the same compilation (.o file) are grouped
4890 together. A future step could be to put the types in the same symtab as
4891 the CU the types ultimately came from. */
ff013f42 4892
f4dc4d17
DE
4893static hashval_t
4894hash_type_unit_group (const void *item)
4895{
094b34ac 4896 const struct type_unit_group *tu_group = item;
f4dc4d17 4897
094b34ac 4898 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4899}
348e048f
DE
4900
4901static int
f4dc4d17 4902eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4903{
f4dc4d17
DE
4904 const struct type_unit_group *lhs = item_lhs;
4905 const struct type_unit_group *rhs = item_rhs;
348e048f 4906
094b34ac 4907 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4908}
348e048f 4909
f4dc4d17
DE
4910/* Allocate a hash table for type unit groups. */
4911
4912static htab_t
4913allocate_type_unit_groups_table (void)
4914{
4915 return htab_create_alloc_ex (3,
4916 hash_type_unit_group,
4917 eq_type_unit_group,
4918 NULL,
4919 &dwarf2_per_objfile->objfile->objfile_obstack,
4920 hashtab_obstack_allocate,
4921 dummy_obstack_deallocate);
4922}
dee91e82 4923
f4dc4d17
DE
4924/* Type units that don't have DW_AT_stmt_list are grouped into their own
4925 partial symtabs. We combine several TUs per psymtab to not let the size
4926 of any one psymtab grow too big. */
4927#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4928#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 4929
094b34ac 4930/* Helper routine for get_type_unit_group.
f4dc4d17
DE
4931 Create the type_unit_group object used to hold one or more TUs. */
4932
4933static struct type_unit_group *
094b34ac 4934create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
4935{
4936 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 4937 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 4938 struct type_unit_group *tu_group;
f4dc4d17
DE
4939
4940 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4941 struct type_unit_group);
094b34ac 4942 per_cu = &tu_group->per_cu;
f4dc4d17
DE
4943 per_cu->objfile = objfile;
4944 per_cu->is_debug_types = 1;
796a7ff8 4945 per_cu->type_unit_group = tu_group;
f4dc4d17 4946
094b34ac
DE
4947 if (dwarf2_per_objfile->using_index)
4948 {
4949 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4950 struct dwarf2_per_cu_quick_data);
4951 tu_group->t.first_tu = cu->per_cu;
4952 }
4953 else
4954 {
4955 unsigned int line_offset = line_offset_struct.sect_off;
4956 struct partial_symtab *pst;
4957 char *name;
4958
4959 /* Give the symtab a useful name for debug purposes. */
4960 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4961 name = xstrprintf ("<type_units_%d>",
4962 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4963 else
4964 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4965
4966 pst = create_partial_symtab (per_cu, name);
4967 pst->anonymous = 1;
f4dc4d17 4968
094b34ac
DE
4969 xfree (name);
4970 }
f4dc4d17 4971
094b34ac
DE
4972 tu_group->hash.dwo_unit = cu->dwo_unit;
4973 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
4974
4975 return tu_group;
4976}
4977
094b34ac
DE
4978/* Look up the type_unit_group for type unit CU, and create it if necessary.
4979 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
4980
4981static struct type_unit_group *
094b34ac 4982get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
4983{
4984 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4985 struct type_unit_group *tu_group;
4986 void **slot;
4987 unsigned int line_offset;
4988 struct type_unit_group type_unit_group_for_lookup;
4989
4990 if (dwarf2_per_objfile->type_unit_groups == NULL)
4991 {
4992 dwarf2_per_objfile->type_unit_groups =
4993 allocate_type_unit_groups_table ();
4994 }
4995
4996 /* Do we need to create a new group, or can we use an existing one? */
4997
4998 if (stmt_list)
4999 {
5000 line_offset = DW_UNSND (stmt_list);
5001 ++tu_stats->nr_symtab_sharers;
5002 }
5003 else
5004 {
5005 /* Ugh, no stmt_list. Rare, but we have to handle it.
5006 We can do various things here like create one group per TU or
5007 spread them over multiple groups to split up the expansion work.
5008 To avoid worst case scenarios (too many groups or too large groups)
5009 we, umm, group them in bunches. */
5010 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5011 | (tu_stats->nr_stmt_less_type_units
5012 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5013 ++tu_stats->nr_stmt_less_type_units;
5014 }
5015
094b34ac
DE
5016 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5017 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5018 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5019 &type_unit_group_for_lookup, INSERT);
5020 if (*slot != NULL)
5021 {
5022 tu_group = *slot;
5023 gdb_assert (tu_group != NULL);
5024 }
5025 else
5026 {
5027 sect_offset line_offset_struct;
5028
5029 line_offset_struct.sect_off = line_offset;
094b34ac 5030 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5031 *slot = tu_group;
5032 ++tu_stats->nr_symtabs;
5033 }
5034
5035 return tu_group;
5036}
5037
5038/* Struct used to sort TUs by their abbreviation table offset. */
5039
5040struct tu_abbrev_offset
5041{
5042 struct signatured_type *sig_type;
5043 sect_offset abbrev_offset;
5044};
5045
5046/* Helper routine for build_type_unit_groups, passed to qsort. */
5047
5048static int
5049sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5050{
5051 const struct tu_abbrev_offset * const *a = ap;
5052 const struct tu_abbrev_offset * const *b = bp;
5053 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5054 unsigned int boff = (*b)->abbrev_offset.sect_off;
5055
5056 return (aoff > boff) - (aoff < boff);
5057}
5058
5059/* A helper function to add a type_unit_group to a table. */
5060
5061static int
5062add_type_unit_group_to_table (void **slot, void *datum)
5063{
5064 struct type_unit_group *tu_group = *slot;
5065 struct type_unit_group ***datap = datum;
5066
5067 **datap = tu_group;
5068 ++*datap;
5069
5070 return 1;
5071}
5072
5073/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5074 each one passing FUNC,DATA.
5075
5076 The efficiency is because we sort TUs by the abbrev table they use and
5077 only read each abbrev table once. In one program there are 200K TUs
5078 sharing 8K abbrev tables.
5079
5080 The main purpose of this function is to support building the
5081 dwarf2_per_objfile->type_unit_groups table.
5082 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5083 can collapse the search space by grouping them by stmt_list.
5084 The savings can be significant, in the same program from above the 200K TUs
5085 share 8K stmt_list tables.
5086
5087 FUNC is expected to call get_type_unit_group, which will create the
5088 struct type_unit_group if necessary and add it to
5089 dwarf2_per_objfile->type_unit_groups. */
5090
5091static void
5092build_type_unit_groups (die_reader_func_ftype *func, void *data)
5093{
5094 struct objfile *objfile = dwarf2_per_objfile->objfile;
5095 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5096 struct cleanup *cleanups;
5097 struct abbrev_table *abbrev_table;
5098 sect_offset abbrev_offset;
5099 struct tu_abbrev_offset *sorted_by_abbrev;
5100 struct type_unit_group **iter;
5101 int i;
5102
5103 /* It's up to the caller to not call us multiple times. */
5104 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5105
5106 if (dwarf2_per_objfile->n_type_units == 0)
5107 return;
5108
5109 /* TUs typically share abbrev tables, and there can be way more TUs than
5110 abbrev tables. Sort by abbrev table to reduce the number of times we
5111 read each abbrev table in.
5112 Alternatives are to punt or to maintain a cache of abbrev tables.
5113 This is simpler and efficient enough for now.
5114
5115 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5116 symtab to use). Typically TUs with the same abbrev offset have the same
5117 stmt_list value too so in practice this should work well.
5118
5119 The basic algorithm here is:
5120
5121 sort TUs by abbrev table
5122 for each TU with same abbrev table:
5123 read abbrev table if first user
5124 read TU top level DIE
5125 [IWBN if DWO skeletons had DW_AT_stmt_list]
5126 call FUNC */
5127
5128 if (dwarf2_read_debug)
5129 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5130
5131 /* Sort in a separate table to maintain the order of all_type_units
5132 for .gdb_index: TU indices directly index all_type_units. */
5133 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5134 dwarf2_per_objfile->n_type_units);
5135 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5136 {
5137 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5138
5139 sorted_by_abbrev[i].sig_type = sig_type;
5140 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5141 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5142 sig_type->per_cu.offset);
5143 }
5144 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5145 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5146 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5147
094b34ac
DE
5148 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5149 called any number of times, so we don't reset tu_stats here. */
5150
f4dc4d17
DE
5151 abbrev_offset.sect_off = ~(unsigned) 0;
5152 abbrev_table = NULL;
5153 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5154
5155 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5156 {
5157 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5158
5159 /* Switch to the next abbrev table if necessary. */
5160 if (abbrev_table == NULL
5161 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5162 {
5163 if (abbrev_table != NULL)
5164 {
5165 abbrev_table_free (abbrev_table);
5166 /* Reset to NULL in case abbrev_table_read_table throws
5167 an error: abbrev_table_free_cleanup will get called. */
5168 abbrev_table = NULL;
5169 }
5170 abbrev_offset = tu->abbrev_offset;
5171 abbrev_table =
5172 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5173 abbrev_offset);
5174 ++tu_stats->nr_uniq_abbrev_tables;
5175 }
5176
5177 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5178 func, data);
5179 }
5180
5181 /* Create a vector of pointers to primary type units to make it easy to
5182 iterate over them and CUs. See dw2_get_primary_cu. */
5183 dwarf2_per_objfile->n_type_unit_groups =
5184 htab_elements (dwarf2_per_objfile->type_unit_groups);
5185 dwarf2_per_objfile->all_type_unit_groups =
5186 obstack_alloc (&objfile->objfile_obstack,
5187 dwarf2_per_objfile->n_type_unit_groups
5188 * sizeof (struct type_unit_group *));
5189 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5190 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5191 add_type_unit_group_to_table, &iter);
5192 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5193 == dwarf2_per_objfile->n_type_unit_groups);
5194
5195 do_cleanups (cleanups);
5196
5197 if (dwarf2_read_debug)
5198 {
5199 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5200 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5201 dwarf2_per_objfile->n_type_units);
5202 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5203 tu_stats->nr_uniq_abbrev_tables);
5204 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5205 tu_stats->nr_symtabs);
5206 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5207 tu_stats->nr_symtab_sharers);
5208 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5209 tu_stats->nr_stmt_less_type_units);
5210 }
5211}
0018ea6f
DE
5212\f
5213/* Partial symbol tables. */
5214
5215/* Create a psymtab named NAME and assign it to PER_CU.
5216
5217 The caller must fill in the following details:
5218 dirname, textlow, texthigh. */
5219
5220static struct partial_symtab *
5221create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5222{
5223 struct objfile *objfile = per_cu->objfile;
5224 struct partial_symtab *pst;
5225
5226 pst = start_psymtab_common (objfile, objfile->section_offsets,
5227 name, 0,
5228 objfile->global_psymbols.next,
5229 objfile->static_psymbols.next);
5230
5231 pst->psymtabs_addrmap_supported = 1;
5232
5233 /* This is the glue that links PST into GDB's symbol API. */
5234 pst->read_symtab_private = per_cu;
5235 pst->read_symtab = dwarf2_read_symtab;
5236 per_cu->v.psymtab = pst;
5237
5238 return pst;
5239}
5240
5241/* die_reader_func for process_psymtab_comp_unit. */
5242
5243static void
5244process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5245 gdb_byte *info_ptr,
5246 struct die_info *comp_unit_die,
5247 int has_children,
5248 void *data)
5249{
5250 struct dwarf2_cu *cu = reader->cu;
5251 struct objfile *objfile = cu->objfile;
5252 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5253 struct attribute *attr;
5254 CORE_ADDR baseaddr;
5255 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5256 struct partial_symtab *pst;
5257 int has_pc_info;
5258 const char *filename;
5259 int *want_partial_unit_ptr = data;
5260
5261 if (comp_unit_die->tag == DW_TAG_partial_unit
5262 && (want_partial_unit_ptr == NULL
5263 || !*want_partial_unit_ptr))
5264 return;
5265
5266 gdb_assert (! per_cu->is_debug_types);
5267
5268 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5269
5270 cu->list_in_scope = &file_symbols;
5271
5272 /* Allocate a new partial symbol table structure. */
5273 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5274 if (attr == NULL || !DW_STRING (attr))
5275 filename = "";
5276 else
5277 filename = DW_STRING (attr);
5278
5279 pst = create_partial_symtab (per_cu, filename);
5280
5281 /* This must be done before calling dwarf2_build_include_psymtabs. */
5282 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5283 if (attr != NULL)
5284 pst->dirname = DW_STRING (attr);
5285
5286 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5287
5288 dwarf2_find_base_address (comp_unit_die, cu);
5289
5290 /* Possibly set the default values of LOWPC and HIGHPC from
5291 `DW_AT_ranges'. */
5292 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5293 &best_highpc, cu, pst);
5294 if (has_pc_info == 1 && best_lowpc < best_highpc)
5295 /* Store the contiguous range if it is not empty; it can be empty for
5296 CUs with no code. */
5297 addrmap_set_empty (objfile->psymtabs_addrmap,
5298 best_lowpc + baseaddr,
5299 best_highpc + baseaddr - 1, pst);
5300
5301 /* Check if comp unit has_children.
5302 If so, read the rest of the partial symbols from this comp unit.
5303 If not, there's no more debug_info for this comp unit. */
5304 if (has_children)
5305 {
5306 struct partial_die_info *first_die;
5307 CORE_ADDR lowpc, highpc;
5308
5309 lowpc = ((CORE_ADDR) -1);
5310 highpc = ((CORE_ADDR) 0);
5311
5312 first_die = load_partial_dies (reader, info_ptr, 1);
5313
5314 scan_partial_symbols (first_die, &lowpc, &highpc,
5315 ! has_pc_info, cu);
5316
5317 /* If we didn't find a lowpc, set it to highpc to avoid
5318 complaints from `maint check'. */
5319 if (lowpc == ((CORE_ADDR) -1))
5320 lowpc = highpc;
5321
5322 /* If the compilation unit didn't have an explicit address range,
5323 then use the information extracted from its child dies. */
5324 if (! has_pc_info)
5325 {
5326 best_lowpc = lowpc;
5327 best_highpc = highpc;
5328 }
5329 }
5330 pst->textlow = best_lowpc + baseaddr;
5331 pst->texthigh = best_highpc + baseaddr;
5332
5333 pst->n_global_syms = objfile->global_psymbols.next -
5334 (objfile->global_psymbols.list + pst->globals_offset);
5335 pst->n_static_syms = objfile->static_psymbols.next -
5336 (objfile->static_psymbols.list + pst->statics_offset);
5337 sort_pst_symbols (objfile, pst);
5338
5339 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5340 {
5341 int i;
5342 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5343 struct dwarf2_per_cu_data *iter;
5344
5345 /* Fill in 'dependencies' here; we fill in 'users' in a
5346 post-pass. */
5347 pst->number_of_dependencies = len;
5348 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5349 len * sizeof (struct symtab *));
5350 for (i = 0;
5351 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5352 i, iter);
5353 ++i)
5354 pst->dependencies[i] = iter->v.psymtab;
5355
5356 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5357 }
5358
5359 /* Get the list of files included in the current compilation unit,
5360 and build a psymtab for each of them. */
5361 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5362
5363 if (dwarf2_read_debug)
5364 {
5365 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5366
5367 fprintf_unfiltered (gdb_stdlog,
5368 "Psymtab for %s unit @0x%x: %s - %s"
5369 ", %d global, %d static syms\n",
5370 per_cu->is_debug_types ? "type" : "comp",
5371 per_cu->offset.sect_off,
5372 paddress (gdbarch, pst->textlow),
5373 paddress (gdbarch, pst->texthigh),
5374 pst->n_global_syms, pst->n_static_syms);
5375 }
5376}
5377
5378/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5379 Process compilation unit THIS_CU for a psymtab. */
5380
5381static void
5382process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5383 int want_partial_unit)
5384{
5385 /* If this compilation unit was already read in, free the
5386 cached copy in order to read it in again. This is
5387 necessary because we skipped some symbols when we first
5388 read in the compilation unit (see load_partial_dies).
5389 This problem could be avoided, but the benefit is unclear. */
5390 if (this_cu->cu != NULL)
5391 free_one_cached_comp_unit (this_cu);
5392
5393 gdb_assert (! this_cu->is_debug_types);
5394 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5395 process_psymtab_comp_unit_reader,
5396 &want_partial_unit);
5397
5398 /* Age out any secondary CUs. */
5399 age_cached_comp_units ();
5400}
f4dc4d17
DE
5401
5402/* Reader function for build_type_psymtabs. */
5403
5404static void
5405build_type_psymtabs_reader (const struct die_reader_specs *reader,
5406 gdb_byte *info_ptr,
5407 struct die_info *type_unit_die,
5408 int has_children,
5409 void *data)
5410{
5411 struct objfile *objfile = dwarf2_per_objfile->objfile;
5412 struct dwarf2_cu *cu = reader->cu;
5413 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5414 struct type_unit_group *tu_group;
5415 struct attribute *attr;
5416 struct partial_die_info *first_die;
5417 CORE_ADDR lowpc, highpc;
5418 struct partial_symtab *pst;
5419
5420 gdb_assert (data == NULL);
5421
5422 if (! has_children)
5423 return;
5424
5425 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5426 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5427
094b34ac 5428 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5429
5430 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5431 cu->list_in_scope = &file_symbols;
5432 pst = create_partial_symtab (per_cu, "");
5433 pst->anonymous = 1;
5434
5435 first_die = load_partial_dies (reader, info_ptr, 1);
5436
5437 lowpc = (CORE_ADDR) -1;
5438 highpc = (CORE_ADDR) 0;
5439 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5440
5441 pst->n_global_syms = objfile->global_psymbols.next -
5442 (objfile->global_psymbols.list + pst->globals_offset);
5443 pst->n_static_syms = objfile->static_psymbols.next -
5444 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5445 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5446}
5447
5448/* Traversal function for build_type_psymtabs. */
5449
5450static int
5451build_type_psymtab_dependencies (void **slot, void *info)
5452{
5453 struct objfile *objfile = dwarf2_per_objfile->objfile;
5454 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5455 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5456 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5457 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5458 struct dwarf2_per_cu_data *iter;
5459 int i;
5460
5461 gdb_assert (len > 0);
5462
5463 pst->number_of_dependencies = len;
5464 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5465 len * sizeof (struct psymtab *));
5466 for (i = 0;
094b34ac 5467 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5468 ++i)
5469 {
5470 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 5471 iter->type_unit_group = tu_group;
f4dc4d17
DE
5472 }
5473
094b34ac 5474 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5475
5476 return 1;
5477}
5478
5479/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5480 Build partial symbol tables for the .debug_types comp-units. */
5481
5482static void
5483build_type_psymtabs (struct objfile *objfile)
5484{
0e50663e 5485 if (! create_all_type_units (objfile))
348e048f
DE
5486 return;
5487
f4dc4d17
DE
5488 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5489
5490 /* Now that all TUs have been processed we can fill in the dependencies. */
5491 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5492 build_type_psymtab_dependencies, NULL);
348e048f
DE
5493}
5494
60606b2c
TT
5495/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5496
5497static void
5498psymtabs_addrmap_cleanup (void *o)
5499{
5500 struct objfile *objfile = o;
ec61707d 5501
60606b2c
TT
5502 objfile->psymtabs_addrmap = NULL;
5503}
5504
95554aad
TT
5505/* Compute the 'user' field for each psymtab in OBJFILE. */
5506
5507static void
5508set_partial_user (struct objfile *objfile)
5509{
5510 int i;
5511
5512 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5513 {
5514 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5515 struct partial_symtab *pst = per_cu->v.psymtab;
5516 int j;
5517
36586728
TT
5518 if (pst == NULL)
5519 continue;
5520
95554aad
TT
5521 for (j = 0; j < pst->number_of_dependencies; ++j)
5522 {
5523 /* Set the 'user' field only if it is not already set. */
5524 if (pst->dependencies[j]->user == NULL)
5525 pst->dependencies[j]->user = pst;
5526 }
5527 }
5528}
5529
93311388
DE
5530/* Build the partial symbol table by doing a quick pass through the
5531 .debug_info and .debug_abbrev sections. */
72bf9492 5532
93311388 5533static void
c67a9c90 5534dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5535{
60606b2c
TT
5536 struct cleanup *back_to, *addrmap_cleanup;
5537 struct obstack temp_obstack;
21b2bd31 5538 int i;
93311388 5539
45cfd468
DE
5540 if (dwarf2_read_debug)
5541 {
5542 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5543 objfile->name);
5544 }
5545
98bfdba5
PA
5546 dwarf2_per_objfile->reading_partial_symbols = 1;
5547
be391dca 5548 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5549
93311388
DE
5550 /* Any cached compilation units will be linked by the per-objfile
5551 read_in_chain. Make sure to free them when we're done. */
5552 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5553
348e048f
DE
5554 build_type_psymtabs (objfile);
5555
93311388 5556 create_all_comp_units (objfile);
c906108c 5557
60606b2c
TT
5558 /* Create a temporary address map on a temporary obstack. We later
5559 copy this to the final obstack. */
5560 obstack_init (&temp_obstack);
5561 make_cleanup_obstack_free (&temp_obstack);
5562 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5563 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5564
21b2bd31 5565 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5566 {
21b2bd31 5567 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5568
95554aad 5569 process_psymtab_comp_unit (per_cu, 0);
c906108c 5570 }
ff013f42 5571
95554aad
TT
5572 set_partial_user (objfile);
5573
ff013f42
JK
5574 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5575 &objfile->objfile_obstack);
60606b2c 5576 discard_cleanups (addrmap_cleanup);
ff013f42 5577
ae038cb0 5578 do_cleanups (back_to);
45cfd468
DE
5579
5580 if (dwarf2_read_debug)
5581 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5582 objfile->name);
ae038cb0
DJ
5583}
5584
3019eac3 5585/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5586
5587static void
dee91e82
DE
5588load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5589 gdb_byte *info_ptr,
5590 struct die_info *comp_unit_die,
5591 int has_children,
5592 void *data)
ae038cb0 5593{
dee91e82 5594 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5595
95554aad 5596 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5597
ae038cb0
DJ
5598 /* Check if comp unit has_children.
5599 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5600 If not, there's no more debug_info for this comp unit. */
d85a05f0 5601 if (has_children)
dee91e82
DE
5602 load_partial_dies (reader, info_ptr, 0);
5603}
98bfdba5 5604
dee91e82
DE
5605/* Load the partial DIEs for a secondary CU into memory.
5606 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5607
dee91e82
DE
5608static void
5609load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5610{
f4dc4d17
DE
5611 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5612 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5613}
5614
ae038cb0 5615static void
36586728
TT
5616read_comp_units_from_section (struct objfile *objfile,
5617 struct dwarf2_section_info *section,
5618 unsigned int is_dwz,
5619 int *n_allocated,
5620 int *n_comp_units,
5621 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5622{
be391dca 5623 gdb_byte *info_ptr;
36586728 5624 bfd *abfd = section->asection->owner;
be391dca 5625
36586728 5626 dwarf2_read_section (objfile, section);
ae038cb0 5627
36586728 5628 info_ptr = section->buffer;
6e70227d 5629
36586728 5630 while (info_ptr < section->buffer + section->size)
ae038cb0 5631 {
c764a876 5632 unsigned int length, initial_length_size;
ae038cb0 5633 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5634 sect_offset offset;
ae038cb0 5635
36586728 5636 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5637
5638 /* Read just enough information to find out where the next
5639 compilation unit is. */
36586728 5640 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5641
5642 /* Save the compilation unit for later lookup. */
5643 this_cu = obstack_alloc (&objfile->objfile_obstack,
5644 sizeof (struct dwarf2_per_cu_data));
5645 memset (this_cu, 0, sizeof (*this_cu));
5646 this_cu->offset = offset;
c764a876 5647 this_cu->length = length + initial_length_size;
36586728 5648 this_cu->is_dwz = is_dwz;
9291a0cd 5649 this_cu->objfile = objfile;
8a0459fd 5650 this_cu->section = section;
ae038cb0 5651
36586728 5652 if (*n_comp_units == *n_allocated)
ae038cb0 5653 {
36586728
TT
5654 *n_allocated *= 2;
5655 *all_comp_units = xrealloc (*all_comp_units,
5656 *n_allocated
5657 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5658 }
36586728
TT
5659 (*all_comp_units)[*n_comp_units] = this_cu;
5660 ++*n_comp_units;
ae038cb0
DJ
5661
5662 info_ptr = info_ptr + this_cu->length;
5663 }
36586728
TT
5664}
5665
5666/* Create a list of all compilation units in OBJFILE.
5667 This is only done for -readnow and building partial symtabs. */
5668
5669static void
5670create_all_comp_units (struct objfile *objfile)
5671{
5672 int n_allocated;
5673 int n_comp_units;
5674 struct dwarf2_per_cu_data **all_comp_units;
5675
5676 n_comp_units = 0;
5677 n_allocated = 10;
5678 all_comp_units = xmalloc (n_allocated
5679 * sizeof (struct dwarf2_per_cu_data *));
5680
5681 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5682 &n_allocated, &n_comp_units, &all_comp_units);
5683
5684 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5685 {
5686 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5687
5688 read_comp_units_from_section (objfile, &dwz->info, 1,
5689 &n_allocated, &n_comp_units,
5690 &all_comp_units);
5691 }
ae038cb0
DJ
5692
5693 dwarf2_per_objfile->all_comp_units
5694 = obstack_alloc (&objfile->objfile_obstack,
5695 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5696 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5697 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5698 xfree (all_comp_units);
5699 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5700}
5701
5734ee8b
DJ
5702/* Process all loaded DIEs for compilation unit CU, starting at
5703 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5704 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5705 DW_AT_ranges). If NEED_PC is set, then this function will set
5706 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5707 and record the covered ranges in the addrmap. */
c906108c 5708
72bf9492
DJ
5709static void
5710scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5711 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5712{
72bf9492 5713 struct partial_die_info *pdi;
c906108c 5714
91c24f0a
DC
5715 /* Now, march along the PDI's, descending into ones which have
5716 interesting children but skipping the children of the other ones,
5717 until we reach the end of the compilation unit. */
c906108c 5718
72bf9492 5719 pdi = first_die;
91c24f0a 5720
72bf9492
DJ
5721 while (pdi != NULL)
5722 {
5723 fixup_partial_die (pdi, cu);
c906108c 5724
f55ee35c 5725 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5726 children, so we need to look at them. Ditto for anonymous
5727 enums. */
933c6fe4 5728
72bf9492 5729 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5730 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5731 || pdi->tag == DW_TAG_imported_unit)
c906108c 5732 {
72bf9492 5733 switch (pdi->tag)
c906108c
SS
5734 {
5735 case DW_TAG_subprogram:
5734ee8b 5736 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5737 break;
72929c62 5738 case DW_TAG_constant:
c906108c
SS
5739 case DW_TAG_variable:
5740 case DW_TAG_typedef:
91c24f0a 5741 case DW_TAG_union_type:
72bf9492 5742 if (!pdi->is_declaration)
63d06c5c 5743 {
72bf9492 5744 add_partial_symbol (pdi, cu);
63d06c5c
DC
5745 }
5746 break;
c906108c 5747 case DW_TAG_class_type:
680b30c7 5748 case DW_TAG_interface_type:
c906108c 5749 case DW_TAG_structure_type:
72bf9492 5750 if (!pdi->is_declaration)
c906108c 5751 {
72bf9492 5752 add_partial_symbol (pdi, cu);
c906108c
SS
5753 }
5754 break;
91c24f0a 5755 case DW_TAG_enumeration_type:
72bf9492
DJ
5756 if (!pdi->is_declaration)
5757 add_partial_enumeration (pdi, cu);
c906108c
SS
5758 break;
5759 case DW_TAG_base_type:
a02abb62 5760 case DW_TAG_subrange_type:
c906108c 5761 /* File scope base type definitions are added to the partial
c5aa993b 5762 symbol table. */
72bf9492 5763 add_partial_symbol (pdi, cu);
c906108c 5764 break;
d9fa45fe 5765 case DW_TAG_namespace:
5734ee8b 5766 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5767 break;
5d7cb8df
JK
5768 case DW_TAG_module:
5769 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5770 break;
95554aad
TT
5771 case DW_TAG_imported_unit:
5772 {
5773 struct dwarf2_per_cu_data *per_cu;
5774
f4dc4d17
DE
5775 /* For now we don't handle imported units in type units. */
5776 if (cu->per_cu->is_debug_types)
5777 {
5778 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5779 " supported in type units [in module %s]"),
5780 cu->objfile->name);
5781 }
5782
95554aad 5783 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5784 pdi->is_dwz,
95554aad
TT
5785 cu->objfile);
5786
5787 /* Go read the partial unit, if needed. */
5788 if (per_cu->v.psymtab == NULL)
5789 process_psymtab_comp_unit (per_cu, 1);
5790
f4dc4d17 5791 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5792 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5793 }
5794 break;
c906108c
SS
5795 default:
5796 break;
5797 }
5798 }
5799
72bf9492
DJ
5800 /* If the die has a sibling, skip to the sibling. */
5801
5802 pdi = pdi->die_sibling;
5803 }
5804}
5805
5806/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5807
72bf9492 5808 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5809 name is concatenated with "::" and the partial DIE's name. For
5810 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5811 Enumerators are an exception; they use the scope of their parent
5812 enumeration type, i.e. the name of the enumeration type is not
5813 prepended to the enumerator.
91c24f0a 5814
72bf9492
DJ
5815 There are two complexities. One is DW_AT_specification; in this
5816 case "parent" means the parent of the target of the specification,
5817 instead of the direct parent of the DIE. The other is compilers
5818 which do not emit DW_TAG_namespace; in this case we try to guess
5819 the fully qualified name of structure types from their members'
5820 linkage names. This must be done using the DIE's children rather
5821 than the children of any DW_AT_specification target. We only need
5822 to do this for structures at the top level, i.e. if the target of
5823 any DW_AT_specification (if any; otherwise the DIE itself) does not
5824 have a parent. */
5825
5826/* Compute the scope prefix associated with PDI's parent, in
5827 compilation unit CU. The result will be allocated on CU's
5828 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5829 field. NULL is returned if no prefix is necessary. */
15d034d0 5830static const char *
72bf9492
DJ
5831partial_die_parent_scope (struct partial_die_info *pdi,
5832 struct dwarf2_cu *cu)
5833{
15d034d0 5834 const char *grandparent_scope;
72bf9492 5835 struct partial_die_info *parent, *real_pdi;
91c24f0a 5836
72bf9492
DJ
5837 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5838 then this means the parent of the specification DIE. */
5839
5840 real_pdi = pdi;
72bf9492 5841 while (real_pdi->has_specification)
36586728
TT
5842 real_pdi = find_partial_die (real_pdi->spec_offset,
5843 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5844
5845 parent = real_pdi->die_parent;
5846 if (parent == NULL)
5847 return NULL;
5848
5849 if (parent->scope_set)
5850 return parent->scope;
5851
5852 fixup_partial_die (parent, cu);
5853
10b3939b 5854 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5855
acebe513
UW
5856 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5857 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5858 Work around this problem here. */
5859 if (cu->language == language_cplus
6e70227d 5860 && parent->tag == DW_TAG_namespace
acebe513
UW
5861 && strcmp (parent->name, "::") == 0
5862 && grandparent_scope == NULL)
5863 {
5864 parent->scope = NULL;
5865 parent->scope_set = 1;
5866 return NULL;
5867 }
5868
9c6c53f7
SA
5869 if (pdi->tag == DW_TAG_enumerator)
5870 /* Enumerators should not get the name of the enumeration as a prefix. */
5871 parent->scope = grandparent_scope;
5872 else if (parent->tag == DW_TAG_namespace
f55ee35c 5873 || parent->tag == DW_TAG_module
72bf9492
DJ
5874 || parent->tag == DW_TAG_structure_type
5875 || parent->tag == DW_TAG_class_type
680b30c7 5876 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5877 || parent->tag == DW_TAG_union_type
5878 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5879 {
5880 if (grandparent_scope == NULL)
5881 parent->scope = parent->name;
5882 else
3e43a32a
MS
5883 parent->scope = typename_concat (&cu->comp_unit_obstack,
5884 grandparent_scope,
f55ee35c 5885 parent->name, 0, cu);
72bf9492 5886 }
72bf9492
DJ
5887 else
5888 {
5889 /* FIXME drow/2004-04-01: What should we be doing with
5890 function-local names? For partial symbols, we should probably be
5891 ignoring them. */
5892 complaint (&symfile_complaints,
e2e0b3e5 5893 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5894 parent->tag, pdi->offset.sect_off);
72bf9492 5895 parent->scope = grandparent_scope;
c906108c
SS
5896 }
5897
72bf9492
DJ
5898 parent->scope_set = 1;
5899 return parent->scope;
5900}
5901
5902/* Return the fully scoped name associated with PDI, from compilation unit
5903 CU. The result will be allocated with malloc. */
4568ecf9 5904
72bf9492
DJ
5905static char *
5906partial_die_full_name (struct partial_die_info *pdi,
5907 struct dwarf2_cu *cu)
5908{
15d034d0 5909 const char *parent_scope;
72bf9492 5910
98bfdba5
PA
5911 /* If this is a template instantiation, we can not work out the
5912 template arguments from partial DIEs. So, unfortunately, we have
5913 to go through the full DIEs. At least any work we do building
5914 types here will be reused if full symbols are loaded later. */
5915 if (pdi->has_template_arguments)
5916 {
5917 fixup_partial_die (pdi, cu);
5918
5919 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5920 {
5921 struct die_info *die;
5922 struct attribute attr;
5923 struct dwarf2_cu *ref_cu = cu;
5924
b64f50a1 5925 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5926 attr.name = 0;
5927 attr.form = DW_FORM_ref_addr;
4568ecf9 5928 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5929 die = follow_die_ref (NULL, &attr, &ref_cu);
5930
5931 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5932 }
5933 }
5934
72bf9492
DJ
5935 parent_scope = partial_die_parent_scope (pdi, cu);
5936 if (parent_scope == NULL)
5937 return NULL;
5938 else
f55ee35c 5939 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5940}
5941
5942static void
72bf9492 5943add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5944{
e7c27a73 5945 struct objfile *objfile = cu->objfile;
c906108c 5946 CORE_ADDR addr = 0;
15d034d0 5947 const char *actual_name = NULL;
e142c38c 5948 CORE_ADDR baseaddr;
15d034d0 5949 char *built_actual_name;
e142c38c
DJ
5950
5951 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5952
15d034d0
TT
5953 built_actual_name = partial_die_full_name (pdi, cu);
5954 if (built_actual_name != NULL)
5955 actual_name = built_actual_name;
63d06c5c 5956
72bf9492
DJ
5957 if (actual_name == NULL)
5958 actual_name = pdi->name;
5959
c906108c
SS
5960 switch (pdi->tag)
5961 {
5962 case DW_TAG_subprogram:
2cfa0c8d 5963 if (pdi->is_external || cu->language == language_ada)
c906108c 5964 {
2cfa0c8d
JB
5965 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5966 of the global scope. But in Ada, we want to be able to access
5967 nested procedures globally. So all Ada subprograms are stored
5968 in the global scope. */
f47fb265 5969 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5970 mst_text, objfile); */
f47fb265 5971 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5972 built_actual_name != NULL,
f47fb265
MS
5973 VAR_DOMAIN, LOC_BLOCK,
5974 &objfile->global_psymbols,
5975 0, pdi->lowpc + baseaddr,
5976 cu->language, objfile);
c906108c
SS
5977 }
5978 else
5979 {
f47fb265 5980 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5981 mst_file_text, objfile); */
f47fb265 5982 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5983 built_actual_name != NULL,
f47fb265
MS
5984 VAR_DOMAIN, LOC_BLOCK,
5985 &objfile->static_psymbols,
5986 0, pdi->lowpc + baseaddr,
5987 cu->language, objfile);
c906108c
SS
5988 }
5989 break;
72929c62
JB
5990 case DW_TAG_constant:
5991 {
5992 struct psymbol_allocation_list *list;
5993
5994 if (pdi->is_external)
5995 list = &objfile->global_psymbols;
5996 else
5997 list = &objfile->static_psymbols;
f47fb265 5998 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5999 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6000 list, 0, 0, cu->language, objfile);
72929c62
JB
6001 }
6002 break;
c906108c 6003 case DW_TAG_variable:
95554aad
TT
6004 if (pdi->d.locdesc)
6005 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6006
95554aad 6007 if (pdi->d.locdesc
caac4577
JG
6008 && addr == 0
6009 && !dwarf2_per_objfile->has_section_at_zero)
6010 {
6011 /* A global or static variable may also have been stripped
6012 out by the linker if unused, in which case its address
6013 will be nullified; do not add such variables into partial
6014 symbol table then. */
6015 }
6016 else if (pdi->is_external)
c906108c
SS
6017 {
6018 /* Global Variable.
6019 Don't enter into the minimal symbol tables as there is
6020 a minimal symbol table entry from the ELF symbols already.
6021 Enter into partial symbol table if it has a location
6022 descriptor or a type.
6023 If the location descriptor is missing, new_symbol will create
6024 a LOC_UNRESOLVED symbol, the address of the variable will then
6025 be determined from the minimal symbol table whenever the variable
6026 is referenced.
6027 The address for the partial symbol table entry is not
6028 used by GDB, but it comes in handy for debugging partial symbol
6029 table building. */
6030
95554aad 6031 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6032 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6033 built_actual_name != NULL,
f47fb265
MS
6034 VAR_DOMAIN, LOC_STATIC,
6035 &objfile->global_psymbols,
6036 0, addr + baseaddr,
6037 cu->language, objfile);
c906108c
SS
6038 }
6039 else
6040 {
0963b4bd 6041 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6042 if (pdi->d.locdesc == NULL)
decbce07 6043 {
15d034d0 6044 xfree (built_actual_name);
decbce07
MS
6045 return;
6046 }
f47fb265 6047 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6048 mst_file_data, objfile); */
f47fb265 6049 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6050 built_actual_name != NULL,
f47fb265
MS
6051 VAR_DOMAIN, LOC_STATIC,
6052 &objfile->static_psymbols,
6053 0, addr + baseaddr,
6054 cu->language, objfile);
c906108c
SS
6055 }
6056 break;
6057 case DW_TAG_typedef:
6058 case DW_TAG_base_type:
a02abb62 6059 case DW_TAG_subrange_type:
38d518c9 6060 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6061 built_actual_name != NULL,
176620f1 6062 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6063 &objfile->static_psymbols,
e142c38c 6064 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6065 break;
72bf9492
DJ
6066 case DW_TAG_namespace:
6067 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6068 built_actual_name != NULL,
72bf9492
DJ
6069 VAR_DOMAIN, LOC_TYPEDEF,
6070 &objfile->global_psymbols,
6071 0, (CORE_ADDR) 0, cu->language, objfile);
6072 break;
c906108c 6073 case DW_TAG_class_type:
680b30c7 6074 case DW_TAG_interface_type:
c906108c
SS
6075 case DW_TAG_structure_type:
6076 case DW_TAG_union_type:
6077 case DW_TAG_enumeration_type:
fa4028e9
JB
6078 /* Skip external references. The DWARF standard says in the section
6079 about "Structure, Union, and Class Type Entries": "An incomplete
6080 structure, union or class type is represented by a structure,
6081 union or class entry that does not have a byte size attribute
6082 and that has a DW_AT_declaration attribute." */
6083 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6084 {
15d034d0 6085 xfree (built_actual_name);
decbce07
MS
6086 return;
6087 }
fa4028e9 6088
63d06c5c
DC
6089 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6090 static vs. global. */
38d518c9 6091 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6092 built_actual_name != NULL,
176620f1 6093 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6094 (cu->language == language_cplus
6095 || cu->language == language_java)
63d06c5c
DC
6096 ? &objfile->global_psymbols
6097 : &objfile->static_psymbols,
e142c38c 6098 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6099
c906108c
SS
6100 break;
6101 case DW_TAG_enumerator:
38d518c9 6102 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6103 built_actual_name != NULL,
176620f1 6104 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6105 (cu->language == language_cplus
6106 || cu->language == language_java)
f6fe98ef
DJ
6107 ? &objfile->global_psymbols
6108 : &objfile->static_psymbols,
e142c38c 6109 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6110 break;
6111 default:
6112 break;
6113 }
5c4e30ca 6114
15d034d0 6115 xfree (built_actual_name);
c906108c
SS
6116}
6117
5c4e30ca
DC
6118/* Read a partial die corresponding to a namespace; also, add a symbol
6119 corresponding to that namespace to the symbol table. NAMESPACE is
6120 the name of the enclosing namespace. */
91c24f0a 6121
72bf9492
DJ
6122static void
6123add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6124 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6125 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6126{
72bf9492 6127 /* Add a symbol for the namespace. */
e7c27a73 6128
72bf9492 6129 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6130
6131 /* Now scan partial symbols in that namespace. */
6132
91c24f0a 6133 if (pdi->has_children)
5734ee8b 6134 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6135}
6136
5d7cb8df
JK
6137/* Read a partial die corresponding to a Fortran module. */
6138
6139static void
6140add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6141 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6142{
f55ee35c 6143 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6144
6145 if (pdi->has_children)
6146 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6147}
6148
bc30ff58
JB
6149/* Read a partial die corresponding to a subprogram and create a partial
6150 symbol for that subprogram. When the CU language allows it, this
6151 routine also defines a partial symbol for each nested subprogram
6152 that this subprogram contains.
6e70227d 6153
bc30ff58
JB
6154 DIE my also be a lexical block, in which case we simply search
6155 recursively for suprograms defined inside that lexical block.
6156 Again, this is only performed when the CU language allows this
6157 type of definitions. */
6158
6159static void
6160add_partial_subprogram (struct partial_die_info *pdi,
6161 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6162 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6163{
6164 if (pdi->tag == DW_TAG_subprogram)
6165 {
6166 if (pdi->has_pc_info)
6167 {
6168 if (pdi->lowpc < *lowpc)
6169 *lowpc = pdi->lowpc;
6170 if (pdi->highpc > *highpc)
6171 *highpc = pdi->highpc;
5734ee8b
DJ
6172 if (need_pc)
6173 {
6174 CORE_ADDR baseaddr;
6175 struct objfile *objfile = cu->objfile;
6176
6177 baseaddr = ANOFFSET (objfile->section_offsets,
6178 SECT_OFF_TEXT (objfile));
6179 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6180 pdi->lowpc + baseaddr,
6181 pdi->highpc - 1 + baseaddr,
9291a0cd 6182 cu->per_cu->v.psymtab);
5734ee8b 6183 }
481860b3
GB
6184 }
6185
6186 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6187 {
bc30ff58 6188 if (!pdi->is_declaration)
e8d05480
JB
6189 /* Ignore subprogram DIEs that do not have a name, they are
6190 illegal. Do not emit a complaint at this point, we will
6191 do so when we convert this psymtab into a symtab. */
6192 if (pdi->name)
6193 add_partial_symbol (pdi, cu);
bc30ff58
JB
6194 }
6195 }
6e70227d 6196
bc30ff58
JB
6197 if (! pdi->has_children)
6198 return;
6199
6200 if (cu->language == language_ada)
6201 {
6202 pdi = pdi->die_child;
6203 while (pdi != NULL)
6204 {
6205 fixup_partial_die (pdi, cu);
6206 if (pdi->tag == DW_TAG_subprogram
6207 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6208 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6209 pdi = pdi->die_sibling;
6210 }
6211 }
6212}
6213
91c24f0a
DC
6214/* Read a partial die corresponding to an enumeration type. */
6215
72bf9492
DJ
6216static void
6217add_partial_enumeration (struct partial_die_info *enum_pdi,
6218 struct dwarf2_cu *cu)
91c24f0a 6219{
72bf9492 6220 struct partial_die_info *pdi;
91c24f0a
DC
6221
6222 if (enum_pdi->name != NULL)
72bf9492
DJ
6223 add_partial_symbol (enum_pdi, cu);
6224
6225 pdi = enum_pdi->die_child;
6226 while (pdi)
91c24f0a 6227 {
72bf9492 6228 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6229 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6230 else
72bf9492
DJ
6231 add_partial_symbol (pdi, cu);
6232 pdi = pdi->die_sibling;
91c24f0a 6233 }
91c24f0a
DC
6234}
6235
6caca83c
CC
6236/* Return the initial uleb128 in the die at INFO_PTR. */
6237
6238static unsigned int
6239peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6240{
6241 unsigned int bytes_read;
6242
6243 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6244}
6245
4bb7a0a7
DJ
6246/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6247 Return the corresponding abbrev, or NULL if the number is zero (indicating
6248 an empty DIE). In either case *BYTES_READ will be set to the length of
6249 the initial number. */
6250
6251static struct abbrev_info *
fe1b8b76 6252peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6253 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6254{
6255 bfd *abfd = cu->objfile->obfd;
6256 unsigned int abbrev_number;
6257 struct abbrev_info *abbrev;
6258
6259 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6260
6261 if (abbrev_number == 0)
6262 return NULL;
6263
433df2d4 6264 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6265 if (!abbrev)
6266 {
3e43a32a
MS
6267 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6268 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6269 }
6270
6271 return abbrev;
6272}
6273
93311388
DE
6274/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6275 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6276 DIE. Any children of the skipped DIEs will also be skipped. */
6277
fe1b8b76 6278static gdb_byte *
dee91e82 6279skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6280{
dee91e82 6281 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6282 struct abbrev_info *abbrev;
6283 unsigned int bytes_read;
6284
6285 while (1)
6286 {
6287 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6288 if (abbrev == NULL)
6289 return info_ptr + bytes_read;
6290 else
dee91e82 6291 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6292 }
6293}
6294
93311388
DE
6295/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6296 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6297 abbrev corresponding to that skipped uleb128 should be passed in
6298 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6299 children. */
6300
fe1b8b76 6301static gdb_byte *
dee91e82
DE
6302skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6303 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6304{
6305 unsigned int bytes_read;
6306 struct attribute attr;
dee91e82
DE
6307 bfd *abfd = reader->abfd;
6308 struct dwarf2_cu *cu = reader->cu;
6309 gdb_byte *buffer = reader->buffer;
f664829e
DE
6310 const gdb_byte *buffer_end = reader->buffer_end;
6311 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6312 unsigned int form, i;
6313
6314 for (i = 0; i < abbrev->num_attrs; i++)
6315 {
6316 /* The only abbrev we care about is DW_AT_sibling. */
6317 if (abbrev->attrs[i].name == DW_AT_sibling)
6318 {
dee91e82 6319 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6320 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6321 complaint (&symfile_complaints,
6322 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6323 else
b64f50a1 6324 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6325 }
6326
6327 /* If it isn't DW_AT_sibling, skip this attribute. */
6328 form = abbrev->attrs[i].form;
6329 skip_attribute:
6330 switch (form)
6331 {
4bb7a0a7 6332 case DW_FORM_ref_addr:
ae411497
TT
6333 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6334 and later it is offset sized. */
6335 if (cu->header.version == 2)
6336 info_ptr += cu->header.addr_size;
6337 else
6338 info_ptr += cu->header.offset_size;
6339 break;
36586728
TT
6340 case DW_FORM_GNU_ref_alt:
6341 info_ptr += cu->header.offset_size;
6342 break;
ae411497 6343 case DW_FORM_addr:
4bb7a0a7
DJ
6344 info_ptr += cu->header.addr_size;
6345 break;
6346 case DW_FORM_data1:
6347 case DW_FORM_ref1:
6348 case DW_FORM_flag:
6349 info_ptr += 1;
6350 break;
2dc7f7b3
TT
6351 case DW_FORM_flag_present:
6352 break;
4bb7a0a7
DJ
6353 case DW_FORM_data2:
6354 case DW_FORM_ref2:
6355 info_ptr += 2;
6356 break;
6357 case DW_FORM_data4:
6358 case DW_FORM_ref4:
6359 info_ptr += 4;
6360 break;
6361 case DW_FORM_data8:
6362 case DW_FORM_ref8:
55f1336d 6363 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6364 info_ptr += 8;
6365 break;
6366 case DW_FORM_string:
9b1c24c8 6367 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6368 info_ptr += bytes_read;
6369 break;
2dc7f7b3 6370 case DW_FORM_sec_offset:
4bb7a0a7 6371 case DW_FORM_strp:
36586728 6372 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6373 info_ptr += cu->header.offset_size;
6374 break;
2dc7f7b3 6375 case DW_FORM_exprloc:
4bb7a0a7
DJ
6376 case DW_FORM_block:
6377 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6378 info_ptr += bytes_read;
6379 break;
6380 case DW_FORM_block1:
6381 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6382 break;
6383 case DW_FORM_block2:
6384 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6385 break;
6386 case DW_FORM_block4:
6387 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6388 break;
6389 case DW_FORM_sdata:
6390 case DW_FORM_udata:
6391 case DW_FORM_ref_udata:
3019eac3
DE
6392 case DW_FORM_GNU_addr_index:
6393 case DW_FORM_GNU_str_index:
f664829e 6394 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6395 break;
6396 case DW_FORM_indirect:
6397 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6398 info_ptr += bytes_read;
6399 /* We need to continue parsing from here, so just go back to
6400 the top. */
6401 goto skip_attribute;
6402
6403 default:
3e43a32a
MS
6404 error (_("Dwarf Error: Cannot handle %s "
6405 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6406 dwarf_form_name (form),
6407 bfd_get_filename (abfd));
6408 }
6409 }
6410
6411 if (abbrev->has_children)
dee91e82 6412 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6413 else
6414 return info_ptr;
6415}
6416
93311388 6417/* Locate ORIG_PDI's sibling.
dee91e82 6418 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6419
fe1b8b76 6420static gdb_byte *
dee91e82
DE
6421locate_pdi_sibling (const struct die_reader_specs *reader,
6422 struct partial_die_info *orig_pdi,
6423 gdb_byte *info_ptr)
91c24f0a
DC
6424{
6425 /* Do we know the sibling already? */
72bf9492 6426
91c24f0a
DC
6427 if (orig_pdi->sibling)
6428 return orig_pdi->sibling;
6429
6430 /* Are there any children to deal with? */
6431
6432 if (!orig_pdi->has_children)
6433 return info_ptr;
6434
4bb7a0a7 6435 /* Skip the children the long way. */
91c24f0a 6436
dee91e82 6437 return skip_children (reader, info_ptr);
91c24f0a
DC
6438}
6439
257e7a09 6440/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6441 not NULL. */
c906108c
SS
6442
6443static void
257e7a09
YQ
6444dwarf2_read_symtab (struct partial_symtab *self,
6445 struct objfile *objfile)
c906108c 6446{
257e7a09 6447 if (self->readin)
c906108c 6448 {
442e4d9c 6449 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6450 self->filename);
442e4d9c
YQ
6451 }
6452 else
6453 {
6454 if (info_verbose)
c906108c 6455 {
442e4d9c 6456 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6457 self->filename);
442e4d9c 6458 gdb_flush (gdb_stdout);
c906108c 6459 }
c906108c 6460
442e4d9c
YQ
6461 /* Restore our global data. */
6462 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6463
442e4d9c
YQ
6464 /* If this psymtab is constructed from a debug-only objfile, the
6465 has_section_at_zero flag will not necessarily be correct. We
6466 can get the correct value for this flag by looking at the data
6467 associated with the (presumably stripped) associated objfile. */
6468 if (objfile->separate_debug_objfile_backlink)
6469 {
6470 struct dwarf2_per_objfile *dpo_backlink
6471 = objfile_data (objfile->separate_debug_objfile_backlink,
6472 dwarf2_objfile_data_key);
9a619af0 6473
442e4d9c
YQ
6474 dwarf2_per_objfile->has_section_at_zero
6475 = dpo_backlink->has_section_at_zero;
6476 }
b2ab525c 6477
442e4d9c 6478 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6479
257e7a09 6480 psymtab_to_symtab_1 (self);
c906108c 6481
442e4d9c
YQ
6482 /* Finish up the debug error message. */
6483 if (info_verbose)
6484 printf_filtered (_("done.\n"));
c906108c 6485 }
95554aad
TT
6486
6487 process_cu_includes ();
c906108c 6488}
9cdd5dbd
DE
6489\f
6490/* Reading in full CUs. */
c906108c 6491
10b3939b
DJ
6492/* Add PER_CU to the queue. */
6493
6494static void
95554aad
TT
6495queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6496 enum language pretend_language)
10b3939b
DJ
6497{
6498 struct dwarf2_queue_item *item;
6499
6500 per_cu->queued = 1;
6501 item = xmalloc (sizeof (*item));
6502 item->per_cu = per_cu;
95554aad 6503 item->pretend_language = pretend_language;
10b3939b
DJ
6504 item->next = NULL;
6505
6506 if (dwarf2_queue == NULL)
6507 dwarf2_queue = item;
6508 else
6509 dwarf2_queue_tail->next = item;
6510
6511 dwarf2_queue_tail = item;
6512}
6513
0907af0c
DE
6514/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6515 unit and add it to our queue.
6516 The result is non-zero if PER_CU was queued, otherwise the result is zero
6517 meaning either PER_CU is already queued or it is already loaded. */
6518
6519static int
6520maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6521 struct dwarf2_per_cu_data *per_cu,
6522 enum language pretend_language)
6523{
6524 /* We may arrive here during partial symbol reading, if we need full
6525 DIEs to process an unusual case (e.g. template arguments). Do
6526 not queue PER_CU, just tell our caller to load its DIEs. */
6527 if (dwarf2_per_objfile->reading_partial_symbols)
6528 {
6529 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6530 return 1;
6531 return 0;
6532 }
6533
6534 /* Mark the dependence relation so that we don't flush PER_CU
6535 too early. */
6536 dwarf2_add_dependence (this_cu, per_cu);
6537
6538 /* If it's already on the queue, we have nothing to do. */
6539 if (per_cu->queued)
6540 return 0;
6541
6542 /* If the compilation unit is already loaded, just mark it as
6543 used. */
6544 if (per_cu->cu != NULL)
6545 {
6546 per_cu->cu->last_used = 0;
6547 return 0;
6548 }
6549
6550 /* Add it to the queue. */
6551 queue_comp_unit (per_cu, pretend_language);
6552
6553 return 1;
6554}
6555
10b3939b
DJ
6556/* Process the queue. */
6557
6558static void
a0f42c21 6559process_queue (void)
10b3939b
DJ
6560{
6561 struct dwarf2_queue_item *item, *next_item;
6562
45cfd468
DE
6563 if (dwarf2_read_debug)
6564 {
6565 fprintf_unfiltered (gdb_stdlog,
6566 "Expanding one or more symtabs of objfile %s ...\n",
6567 dwarf2_per_objfile->objfile->name);
6568 }
6569
03dd20cc
DJ
6570 /* The queue starts out with one item, but following a DIE reference
6571 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6572 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6573 {
9291a0cd
TT
6574 if (dwarf2_per_objfile->using_index
6575 ? !item->per_cu->v.quick->symtab
6576 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6577 {
6578 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6579
6580 if (dwarf2_read_debug)
6581 {
6582 fprintf_unfiltered (gdb_stdlog,
6583 "Expanding symtab of %s at offset 0x%x\n",
6584 per_cu->is_debug_types ? "TU" : "CU",
6585 per_cu->offset.sect_off);
6586 }
6587
6588 if (per_cu->is_debug_types)
6589 process_full_type_unit (per_cu, item->pretend_language);
6590 else
6591 process_full_comp_unit (per_cu, item->pretend_language);
6592
6593 if (dwarf2_read_debug)
6594 {
6595 fprintf_unfiltered (gdb_stdlog,
6596 "Done expanding %s at offset 0x%x\n",
6597 per_cu->is_debug_types ? "TU" : "CU",
6598 per_cu->offset.sect_off);
6599 }
6600 }
10b3939b
DJ
6601
6602 item->per_cu->queued = 0;
6603 next_item = item->next;
6604 xfree (item);
6605 }
6606
6607 dwarf2_queue_tail = NULL;
45cfd468
DE
6608
6609 if (dwarf2_read_debug)
6610 {
6611 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6612 dwarf2_per_objfile->objfile->name);
6613 }
10b3939b
DJ
6614}
6615
6616/* Free all allocated queue entries. This function only releases anything if
6617 an error was thrown; if the queue was processed then it would have been
6618 freed as we went along. */
6619
6620static void
6621dwarf2_release_queue (void *dummy)
6622{
6623 struct dwarf2_queue_item *item, *last;
6624
6625 item = dwarf2_queue;
6626 while (item)
6627 {
6628 /* Anything still marked queued is likely to be in an
6629 inconsistent state, so discard it. */
6630 if (item->per_cu->queued)
6631 {
6632 if (item->per_cu->cu != NULL)
dee91e82 6633 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6634 item->per_cu->queued = 0;
6635 }
6636
6637 last = item;
6638 item = item->next;
6639 xfree (last);
6640 }
6641
6642 dwarf2_queue = dwarf2_queue_tail = NULL;
6643}
6644
6645/* Read in full symbols for PST, and anything it depends on. */
6646
c906108c 6647static void
fba45db2 6648psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6649{
10b3939b 6650 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6651 int i;
6652
95554aad
TT
6653 if (pst->readin)
6654 return;
6655
aaa75496 6656 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6657 if (!pst->dependencies[i]->readin
6658 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6659 {
6660 /* Inform about additional files that need to be read in. */
6661 if (info_verbose)
6662 {
a3f17187 6663 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6664 fputs_filtered (" ", gdb_stdout);
6665 wrap_here ("");
6666 fputs_filtered ("and ", gdb_stdout);
6667 wrap_here ("");
6668 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6669 wrap_here (""); /* Flush output. */
aaa75496
JB
6670 gdb_flush (gdb_stdout);
6671 }
6672 psymtab_to_symtab_1 (pst->dependencies[i]);
6673 }
6674
e38df1d0 6675 per_cu = pst->read_symtab_private;
10b3939b
DJ
6676
6677 if (per_cu == NULL)
aaa75496
JB
6678 {
6679 /* It's an include file, no symbols to read for it.
6680 Everything is in the parent symtab. */
6681 pst->readin = 1;
6682 return;
6683 }
c906108c 6684
a0f42c21 6685 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6686}
6687
dee91e82
DE
6688/* Trivial hash function for die_info: the hash value of a DIE
6689 is its offset in .debug_info for this objfile. */
10b3939b 6690
dee91e82
DE
6691static hashval_t
6692die_hash (const void *item)
10b3939b 6693{
dee91e82 6694 const struct die_info *die = item;
6502dd73 6695
dee91e82
DE
6696 return die->offset.sect_off;
6697}
63d06c5c 6698
dee91e82
DE
6699/* Trivial comparison function for die_info structures: two DIEs
6700 are equal if they have the same offset. */
98bfdba5 6701
dee91e82
DE
6702static int
6703die_eq (const void *item_lhs, const void *item_rhs)
6704{
6705 const struct die_info *die_lhs = item_lhs;
6706 const struct die_info *die_rhs = item_rhs;
c906108c 6707
dee91e82
DE
6708 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6709}
c906108c 6710
dee91e82
DE
6711/* die_reader_func for load_full_comp_unit.
6712 This is identical to read_signatured_type_reader,
6713 but is kept separate for now. */
c906108c 6714
dee91e82
DE
6715static void
6716load_full_comp_unit_reader (const struct die_reader_specs *reader,
6717 gdb_byte *info_ptr,
6718 struct die_info *comp_unit_die,
6719 int has_children,
6720 void *data)
6721{
6722 struct dwarf2_cu *cu = reader->cu;
95554aad 6723 enum language *language_ptr = data;
6caca83c 6724
dee91e82
DE
6725 gdb_assert (cu->die_hash == NULL);
6726 cu->die_hash =
6727 htab_create_alloc_ex (cu->header.length / 12,
6728 die_hash,
6729 die_eq,
6730 NULL,
6731 &cu->comp_unit_obstack,
6732 hashtab_obstack_allocate,
6733 dummy_obstack_deallocate);
e142c38c 6734
dee91e82
DE
6735 if (has_children)
6736 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6737 &info_ptr, comp_unit_die);
6738 cu->dies = comp_unit_die;
6739 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6740
6741 /* We try not to read any attributes in this function, because not
9cdd5dbd 6742 all CUs needed for references have been loaded yet, and symbol
10b3939b 6743 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6744 or we won't be able to build types correctly.
6745 Similarly, if we do not read the producer, we can not apply
6746 producer-specific interpretation. */
95554aad 6747 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6748}
10b3939b 6749
dee91e82 6750/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6751
dee91e82 6752static void
95554aad
TT
6753load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6754 enum language pretend_language)
dee91e82 6755{
3019eac3 6756 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6757
f4dc4d17
DE
6758 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6759 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6760}
6761
3da10d80
KS
6762/* Add a DIE to the delayed physname list. */
6763
6764static void
6765add_to_method_list (struct type *type, int fnfield_index, int index,
6766 const char *name, struct die_info *die,
6767 struct dwarf2_cu *cu)
6768{
6769 struct delayed_method_info mi;
6770 mi.type = type;
6771 mi.fnfield_index = fnfield_index;
6772 mi.index = index;
6773 mi.name = name;
6774 mi.die = die;
6775 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6776}
6777
6778/* A cleanup for freeing the delayed method list. */
6779
6780static void
6781free_delayed_list (void *ptr)
6782{
6783 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6784 if (cu->method_list != NULL)
6785 {
6786 VEC_free (delayed_method_info, cu->method_list);
6787 cu->method_list = NULL;
6788 }
6789}
6790
6791/* Compute the physnames of any methods on the CU's method list.
6792
6793 The computation of method physnames is delayed in order to avoid the
6794 (bad) condition that one of the method's formal parameters is of an as yet
6795 incomplete type. */
6796
6797static void
6798compute_delayed_physnames (struct dwarf2_cu *cu)
6799{
6800 int i;
6801 struct delayed_method_info *mi;
6802 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6803 {
1d06ead6 6804 const char *physname;
3da10d80
KS
6805 struct fn_fieldlist *fn_flp
6806 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6807 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6808 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6809 }
6810}
6811
a766d390
DE
6812/* Go objects should be embedded in a DW_TAG_module DIE,
6813 and it's not clear if/how imported objects will appear.
6814 To keep Go support simple until that's worked out,
6815 go back through what we've read and create something usable.
6816 We could do this while processing each DIE, and feels kinda cleaner,
6817 but that way is more invasive.
6818 This is to, for example, allow the user to type "p var" or "b main"
6819 without having to specify the package name, and allow lookups
6820 of module.object to work in contexts that use the expression
6821 parser. */
6822
6823static void
6824fixup_go_packaging (struct dwarf2_cu *cu)
6825{
6826 char *package_name = NULL;
6827 struct pending *list;
6828 int i;
6829
6830 for (list = global_symbols; list != NULL; list = list->next)
6831 {
6832 for (i = 0; i < list->nsyms; ++i)
6833 {
6834 struct symbol *sym = list->symbol[i];
6835
6836 if (SYMBOL_LANGUAGE (sym) == language_go
6837 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6838 {
6839 char *this_package_name = go_symbol_package_name (sym);
6840
6841 if (this_package_name == NULL)
6842 continue;
6843 if (package_name == NULL)
6844 package_name = this_package_name;
6845 else
6846 {
6847 if (strcmp (package_name, this_package_name) != 0)
6848 complaint (&symfile_complaints,
6849 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6850 (SYMBOL_SYMTAB (sym)
05cba821 6851 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6852 : cu->objfile->name),
6853 this_package_name, package_name);
6854 xfree (this_package_name);
6855 }
6856 }
6857 }
6858 }
6859
6860 if (package_name != NULL)
6861 {
6862 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6863 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6864 package_name,
6865 strlen (package_name));
a766d390 6866 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6867 saved_package_name, objfile);
a766d390
DE
6868 struct symbol *sym;
6869
6870 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6871
6872 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6873 SYMBOL_SET_LANGUAGE (sym, language_go);
86f62fd7
TT
6874 SYMBOL_SET_NAMES (sym, saved_package_name,
6875 strlen (saved_package_name), 0, objfile);
a766d390
DE
6876 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6877 e.g., "main" finds the "main" module and not C's main(). */
6878 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6879 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6880 SYMBOL_TYPE (sym) = type;
6881
6882 add_symbol_to_list (sym, &global_symbols);
6883
6884 xfree (package_name);
6885 }
6886}
6887
95554aad
TT
6888/* Return the symtab for PER_CU. This works properly regardless of
6889 whether we're using the index or psymtabs. */
6890
6891static struct symtab *
6892get_symtab (struct dwarf2_per_cu_data *per_cu)
6893{
6894 return (dwarf2_per_objfile->using_index
6895 ? per_cu->v.quick->symtab
6896 : per_cu->v.psymtab->symtab);
6897}
6898
6899/* A helper function for computing the list of all symbol tables
6900 included by PER_CU. */
6901
6902static void
6903recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6904 htab_t all_children,
6905 struct dwarf2_per_cu_data *per_cu)
6906{
6907 void **slot;
6908 int ix;
6909 struct dwarf2_per_cu_data *iter;
6910
6911 slot = htab_find_slot (all_children, per_cu, INSERT);
6912 if (*slot != NULL)
6913 {
6914 /* This inclusion and its children have been processed. */
6915 return;
6916 }
6917
6918 *slot = per_cu;
6919 /* Only add a CU if it has a symbol table. */
6920 if (get_symtab (per_cu) != NULL)
6921 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6922
6923 for (ix = 0;
796a7ff8 6924 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
6925 ++ix)
6926 recursively_compute_inclusions (result, all_children, iter);
6927}
6928
6929/* Compute the symtab 'includes' fields for the symtab related to
6930 PER_CU. */
6931
6932static void
6933compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6934{
f4dc4d17
DE
6935 gdb_assert (! per_cu->is_debug_types);
6936
796a7ff8 6937 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
6938 {
6939 int ix, len;
6940 struct dwarf2_per_cu_data *iter;
6941 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6942 htab_t all_children;
6943 struct symtab *symtab = get_symtab (per_cu);
6944
6945 /* If we don't have a symtab, we can just skip this case. */
6946 if (symtab == NULL)
6947 return;
6948
6949 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6950 NULL, xcalloc, xfree);
6951
6952 for (ix = 0;
796a7ff8 6953 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
6954 ix, iter);
6955 ++ix)
6956 recursively_compute_inclusions (&result_children, all_children, iter);
6957
796a7ff8
DE
6958 /* Now we have a transitive closure of all the included CUs, and
6959 for .gdb_index version 7 the included TUs, so we can convert it
6960 to a list of symtabs. */
95554aad
TT
6961 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6962 symtab->includes
6963 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6964 (len + 1) * sizeof (struct symtab *));
6965 for (ix = 0;
6966 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6967 ++ix)
6968 symtab->includes[ix] = get_symtab (iter);
6969 symtab->includes[len] = NULL;
6970
6971 VEC_free (dwarf2_per_cu_ptr, result_children);
6972 htab_delete (all_children);
6973 }
6974}
6975
6976/* Compute the 'includes' field for the symtabs of all the CUs we just
6977 read. */
6978
6979static void
6980process_cu_includes (void)
6981{
6982 int ix;
6983 struct dwarf2_per_cu_data *iter;
6984
6985 for (ix = 0;
6986 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6987 ix, iter);
6988 ++ix)
f4dc4d17
DE
6989 {
6990 if (! iter->is_debug_types)
6991 compute_symtab_includes (iter);
6992 }
95554aad
TT
6993
6994 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6995}
6996
9cdd5dbd 6997/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6998 already been loaded into memory. */
6999
7000static void
95554aad
TT
7001process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7002 enum language pretend_language)
10b3939b 7003{
10b3939b 7004 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7005 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7006 CORE_ADDR lowpc, highpc;
7007 struct symtab *symtab;
3da10d80 7008 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7009 CORE_ADDR baseaddr;
4359dff1 7010 struct block *static_block;
10b3939b
DJ
7011
7012 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7013
10b3939b
DJ
7014 buildsym_init ();
7015 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7016 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7017
7018 cu->list_in_scope = &file_symbols;
c906108c 7019
95554aad
TT
7020 cu->language = pretend_language;
7021 cu->language_defn = language_def (cu->language);
7022
c906108c 7023 /* Do line number decoding in read_file_scope () */
10b3939b 7024 process_die (cu->dies, cu);
c906108c 7025
a766d390
DE
7026 /* For now fudge the Go package. */
7027 if (cu->language == language_go)
7028 fixup_go_packaging (cu);
7029
3da10d80
KS
7030 /* Now that we have processed all the DIEs in the CU, all the types
7031 should be complete, and it should now be safe to compute all of the
7032 physnames. */
7033 compute_delayed_physnames (cu);
7034 do_cleanups (delayed_list_cleanup);
7035
fae299cd
DC
7036 /* Some compilers don't define a DW_AT_high_pc attribute for the
7037 compilation unit. If the DW_AT_high_pc is missing, synthesize
7038 it, by scanning the DIE's below the compilation unit. */
10b3939b 7039 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7040
36586728
TT
7041 static_block
7042 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7043 per_cu->imported_symtabs != NULL);
4359dff1
JK
7044
7045 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7046 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7047 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7048 addrmap to help ensure it has an accurate map of pc values belonging to
7049 this comp unit. */
7050 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7051
7052 symtab = end_symtab_from_static_block (static_block, objfile,
7053 SECT_OFF_TEXT (objfile), 0);
c906108c 7054
8be455d7 7055 if (symtab != NULL)
c906108c 7056 {
df15bd07 7057 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7058
8be455d7
JK
7059 /* Set symtab language to language from DW_AT_language. If the
7060 compilation is from a C file generated by language preprocessors, do
7061 not set the language if it was already deduced by start_subfile. */
7062 if (!(cu->language == language_c && symtab->language != language_c))
7063 symtab->language = cu->language;
7064
7065 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7066 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7067 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7068 there were bugs in prologue debug info, fixed later in GCC-4.5
7069 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7070
7071 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7072 needed, it would be wrong due to missing DW_AT_producer there.
7073
7074 Still one can confuse GDB by using non-standard GCC compilation
7075 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7076 */
ab260dad 7077 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7078 symtab->locations_valid = 1;
e0d00bc7
JK
7079
7080 if (gcc_4_minor >= 5)
7081 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7082
7083 symtab->call_site_htab = cu->call_site_htab;
c906108c 7084 }
9291a0cd
TT
7085
7086 if (dwarf2_per_objfile->using_index)
7087 per_cu->v.quick->symtab = symtab;
7088 else
7089 {
7090 struct partial_symtab *pst = per_cu->v.psymtab;
7091 pst->symtab = symtab;
7092 pst->readin = 1;
7093 }
c906108c 7094
95554aad
TT
7095 /* Push it for inclusion processing later. */
7096 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7097
c906108c 7098 do_cleanups (back_to);
f4dc4d17 7099}
45cfd468 7100
f4dc4d17
DE
7101/* Generate full symbol information for type unit PER_CU, whose DIEs have
7102 already been loaded into memory. */
7103
7104static void
7105process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7106 enum language pretend_language)
7107{
7108 struct dwarf2_cu *cu = per_cu->cu;
7109 struct objfile *objfile = per_cu->objfile;
7110 struct symtab *symtab;
7111 struct cleanup *back_to, *delayed_list_cleanup;
7112
7113 buildsym_init ();
7114 back_to = make_cleanup (really_free_pendings, NULL);
7115 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7116
7117 cu->list_in_scope = &file_symbols;
7118
7119 cu->language = pretend_language;
7120 cu->language_defn = language_def (cu->language);
7121
7122 /* The symbol tables are set up in read_type_unit_scope. */
7123 process_die (cu->dies, cu);
7124
7125 /* For now fudge the Go package. */
7126 if (cu->language == language_go)
7127 fixup_go_packaging (cu);
7128
7129 /* Now that we have processed all the DIEs in the CU, all the types
7130 should be complete, and it should now be safe to compute all of the
7131 physnames. */
7132 compute_delayed_physnames (cu);
7133 do_cleanups (delayed_list_cleanup);
7134
7135 /* TUs share symbol tables.
7136 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7137 of it with end_expandable_symtab. Otherwise, complete the addition of
7138 this TU's symbols to the existing symtab. */
796a7ff8 7139 if (per_cu->type_unit_group->primary_symtab == NULL)
45cfd468 7140 {
f4dc4d17 7141 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
796a7ff8 7142 per_cu->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7143
7144 if (symtab != NULL)
7145 {
7146 /* Set symtab language to language from DW_AT_language. If the
7147 compilation is from a C file generated by language preprocessors,
7148 do not set the language if it was already deduced by
7149 start_subfile. */
7150 if (!(cu->language == language_c && symtab->language != language_c))
7151 symtab->language = cu->language;
7152 }
7153 }
7154 else
7155 {
7156 augment_type_symtab (objfile,
796a7ff8
DE
7157 per_cu->type_unit_group->primary_symtab);
7158 symtab = per_cu->type_unit_group->primary_symtab;
f4dc4d17
DE
7159 }
7160
7161 if (dwarf2_per_objfile->using_index)
7162 per_cu->v.quick->symtab = symtab;
7163 else
7164 {
7165 struct partial_symtab *pst = per_cu->v.psymtab;
7166 pst->symtab = symtab;
7167 pst->readin = 1;
45cfd468 7168 }
f4dc4d17
DE
7169
7170 do_cleanups (back_to);
c906108c
SS
7171}
7172
95554aad
TT
7173/* Process an imported unit DIE. */
7174
7175static void
7176process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7177{
7178 struct attribute *attr;
7179
f4dc4d17
DE
7180 /* For now we don't handle imported units in type units. */
7181 if (cu->per_cu->is_debug_types)
7182 {
7183 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7184 " supported in type units [in module %s]"),
7185 cu->objfile->name);
7186 }
7187
95554aad
TT
7188 attr = dwarf2_attr (die, DW_AT_import, cu);
7189 if (attr != NULL)
7190 {
7191 struct dwarf2_per_cu_data *per_cu;
7192 struct symtab *imported_symtab;
7193 sect_offset offset;
36586728 7194 int is_dwz;
95554aad
TT
7195
7196 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7197 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7198 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7199
7200 /* Queue the unit, if needed. */
7201 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7202 load_full_comp_unit (per_cu, cu->language);
7203
796a7ff8 7204 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7205 per_cu);
7206 }
7207}
7208
c906108c
SS
7209/* Process a die and its children. */
7210
7211static void
e7c27a73 7212process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7213{
7214 switch (die->tag)
7215 {
7216 case DW_TAG_padding:
7217 break;
7218 case DW_TAG_compile_unit:
95554aad 7219 case DW_TAG_partial_unit:
e7c27a73 7220 read_file_scope (die, cu);
c906108c 7221 break;
348e048f
DE
7222 case DW_TAG_type_unit:
7223 read_type_unit_scope (die, cu);
7224 break;
c906108c 7225 case DW_TAG_subprogram:
c906108c 7226 case DW_TAG_inlined_subroutine:
edb3359d 7227 read_func_scope (die, cu);
c906108c
SS
7228 break;
7229 case DW_TAG_lexical_block:
14898363
L
7230 case DW_TAG_try_block:
7231 case DW_TAG_catch_block:
e7c27a73 7232 read_lexical_block_scope (die, cu);
c906108c 7233 break;
96408a79
SA
7234 case DW_TAG_GNU_call_site:
7235 read_call_site_scope (die, cu);
7236 break;
c906108c 7237 case DW_TAG_class_type:
680b30c7 7238 case DW_TAG_interface_type:
c906108c
SS
7239 case DW_TAG_structure_type:
7240 case DW_TAG_union_type:
134d01f1 7241 process_structure_scope (die, cu);
c906108c
SS
7242 break;
7243 case DW_TAG_enumeration_type:
134d01f1 7244 process_enumeration_scope (die, cu);
c906108c 7245 break;
134d01f1 7246
f792889a
DJ
7247 /* These dies have a type, but processing them does not create
7248 a symbol or recurse to process the children. Therefore we can
7249 read them on-demand through read_type_die. */
c906108c 7250 case DW_TAG_subroutine_type:
72019c9c 7251 case DW_TAG_set_type:
c906108c 7252 case DW_TAG_array_type:
c906108c 7253 case DW_TAG_pointer_type:
c906108c 7254 case DW_TAG_ptr_to_member_type:
c906108c 7255 case DW_TAG_reference_type:
c906108c 7256 case DW_TAG_string_type:
c906108c 7257 break;
134d01f1 7258
c906108c 7259 case DW_TAG_base_type:
a02abb62 7260 case DW_TAG_subrange_type:
cb249c71 7261 case DW_TAG_typedef:
134d01f1
DJ
7262 /* Add a typedef symbol for the type definition, if it has a
7263 DW_AT_name. */
f792889a 7264 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7265 break;
c906108c 7266 case DW_TAG_common_block:
e7c27a73 7267 read_common_block (die, cu);
c906108c
SS
7268 break;
7269 case DW_TAG_common_inclusion:
7270 break;
d9fa45fe 7271 case DW_TAG_namespace:
4d4ec4e5 7272 cu->processing_has_namespace_info = 1;
e7c27a73 7273 read_namespace (die, cu);
d9fa45fe 7274 break;
5d7cb8df 7275 case DW_TAG_module:
4d4ec4e5 7276 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7277 read_module (die, cu);
7278 break;
d9fa45fe
DC
7279 case DW_TAG_imported_declaration:
7280 case DW_TAG_imported_module:
4d4ec4e5 7281 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7282 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7283 || cu->language != language_fortran))
7284 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7285 dwarf_tag_name (die->tag));
7286 read_import_statement (die, cu);
d9fa45fe 7287 break;
95554aad
TT
7288
7289 case DW_TAG_imported_unit:
7290 process_imported_unit_die (die, cu);
7291 break;
7292
c906108c 7293 default:
e7c27a73 7294 new_symbol (die, NULL, cu);
c906108c
SS
7295 break;
7296 }
7297}
ca69b9e6
DE
7298\f
7299/* DWARF name computation. */
c906108c 7300
94af9270
KS
7301/* A helper function for dwarf2_compute_name which determines whether DIE
7302 needs to have the name of the scope prepended to the name listed in the
7303 die. */
7304
7305static int
7306die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7307{
1c809c68
TT
7308 struct attribute *attr;
7309
94af9270
KS
7310 switch (die->tag)
7311 {
7312 case DW_TAG_namespace:
7313 case DW_TAG_typedef:
7314 case DW_TAG_class_type:
7315 case DW_TAG_interface_type:
7316 case DW_TAG_structure_type:
7317 case DW_TAG_union_type:
7318 case DW_TAG_enumeration_type:
7319 case DW_TAG_enumerator:
7320 case DW_TAG_subprogram:
7321 case DW_TAG_member:
7322 return 1;
7323
7324 case DW_TAG_variable:
c2b0a229 7325 case DW_TAG_constant:
94af9270
KS
7326 /* We only need to prefix "globally" visible variables. These include
7327 any variable marked with DW_AT_external or any variable that
7328 lives in a namespace. [Variables in anonymous namespaces
7329 require prefixing, but they are not DW_AT_external.] */
7330
7331 if (dwarf2_attr (die, DW_AT_specification, cu))
7332 {
7333 struct dwarf2_cu *spec_cu = cu;
9a619af0 7334
94af9270
KS
7335 return die_needs_namespace (die_specification (die, &spec_cu),
7336 spec_cu);
7337 }
7338
1c809c68 7339 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7340 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7341 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7342 return 0;
7343 /* A variable in a lexical block of some kind does not need a
7344 namespace, even though in C++ such variables may be external
7345 and have a mangled name. */
7346 if (die->parent->tag == DW_TAG_lexical_block
7347 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7348 || die->parent->tag == DW_TAG_catch_block
7349 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7350 return 0;
7351 return 1;
94af9270
KS
7352
7353 default:
7354 return 0;
7355 }
7356}
7357
98bfdba5
PA
7358/* Retrieve the last character from a mem_file. */
7359
7360static void
7361do_ui_file_peek_last (void *object, const char *buffer, long length)
7362{
7363 char *last_char_p = (char *) object;
7364
7365 if (length > 0)
7366 *last_char_p = buffer[length - 1];
7367}
7368
94af9270 7369/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7370 compute the physname for the object, which include a method's:
7371 - formal parameters (C++/Java),
7372 - receiver type (Go),
7373 - return type (Java).
7374
7375 The term "physname" is a bit confusing.
7376 For C++, for example, it is the demangled name.
7377 For Go, for example, it's the mangled name.
94af9270 7378
af6b7be1
JB
7379 For Ada, return the DIE's linkage name rather than the fully qualified
7380 name. PHYSNAME is ignored..
7381
94af9270
KS
7382 The result is allocated on the objfile_obstack and canonicalized. */
7383
7384static const char *
15d034d0
TT
7385dwarf2_compute_name (const char *name,
7386 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7387 int physname)
7388{
bb5ed363
DE
7389 struct objfile *objfile = cu->objfile;
7390
94af9270
KS
7391 if (name == NULL)
7392 name = dwarf2_name (die, cu);
7393
f55ee35c
JK
7394 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7395 compute it by typename_concat inside GDB. */
7396 if (cu->language == language_ada
7397 || (cu->language == language_fortran && physname))
7398 {
7399 /* For Ada unit, we prefer the linkage name over the name, as
7400 the former contains the exported name, which the user expects
7401 to be able to reference. Ideally, we want the user to be able
7402 to reference this entity using either natural or linkage name,
7403 but we haven't started looking at this enhancement yet. */
7404 struct attribute *attr;
7405
7406 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7407 if (attr == NULL)
7408 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7409 if (attr && DW_STRING (attr))
7410 return DW_STRING (attr);
7411 }
7412
94af9270
KS
7413 /* These are the only languages we know how to qualify names in. */
7414 if (name != NULL
f55ee35c
JK
7415 && (cu->language == language_cplus || cu->language == language_java
7416 || cu->language == language_fortran))
94af9270
KS
7417 {
7418 if (die_needs_namespace (die, cu))
7419 {
7420 long length;
0d5cff50 7421 const char *prefix;
94af9270
KS
7422 struct ui_file *buf;
7423
7424 prefix = determine_prefix (die, cu);
7425 buf = mem_fileopen ();
7426 if (*prefix != '\0')
7427 {
f55ee35c
JK
7428 char *prefixed_name = typename_concat (NULL, prefix, name,
7429 physname, cu);
9a619af0 7430
94af9270
KS
7431 fputs_unfiltered (prefixed_name, buf);
7432 xfree (prefixed_name);
7433 }
7434 else
62d5b8da 7435 fputs_unfiltered (name, buf);
94af9270 7436
98bfdba5
PA
7437 /* Template parameters may be specified in the DIE's DW_AT_name, or
7438 as children with DW_TAG_template_type_param or
7439 DW_TAG_value_type_param. If the latter, add them to the name
7440 here. If the name already has template parameters, then
7441 skip this step; some versions of GCC emit both, and
7442 it is more efficient to use the pre-computed name.
7443
7444 Something to keep in mind about this process: it is very
7445 unlikely, or in some cases downright impossible, to produce
7446 something that will match the mangled name of a function.
7447 If the definition of the function has the same debug info,
7448 we should be able to match up with it anyway. But fallbacks
7449 using the minimal symbol, for instance to find a method
7450 implemented in a stripped copy of libstdc++, will not work.
7451 If we do not have debug info for the definition, we will have to
7452 match them up some other way.
7453
7454 When we do name matching there is a related problem with function
7455 templates; two instantiated function templates are allowed to
7456 differ only by their return types, which we do not add here. */
7457
7458 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7459 {
7460 struct attribute *attr;
7461 struct die_info *child;
7462 int first = 1;
7463
7464 die->building_fullname = 1;
7465
7466 for (child = die->child; child != NULL; child = child->sibling)
7467 {
7468 struct type *type;
12df843f 7469 LONGEST value;
98bfdba5
PA
7470 gdb_byte *bytes;
7471 struct dwarf2_locexpr_baton *baton;
7472 struct value *v;
7473
7474 if (child->tag != DW_TAG_template_type_param
7475 && child->tag != DW_TAG_template_value_param)
7476 continue;
7477
7478 if (first)
7479 {
7480 fputs_unfiltered ("<", buf);
7481 first = 0;
7482 }
7483 else
7484 fputs_unfiltered (", ", buf);
7485
7486 attr = dwarf2_attr (child, DW_AT_type, cu);
7487 if (attr == NULL)
7488 {
7489 complaint (&symfile_complaints,
7490 _("template parameter missing DW_AT_type"));
7491 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7492 continue;
7493 }
7494 type = die_type (child, cu);
7495
7496 if (child->tag == DW_TAG_template_type_param)
7497 {
79d43c61 7498 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7499 continue;
7500 }
7501
7502 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7503 if (attr == NULL)
7504 {
7505 complaint (&symfile_complaints,
3e43a32a
MS
7506 _("template parameter missing "
7507 "DW_AT_const_value"));
98bfdba5
PA
7508 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7509 continue;
7510 }
7511
7512 dwarf2_const_value_attr (attr, type, name,
7513 &cu->comp_unit_obstack, cu,
7514 &value, &bytes, &baton);
7515
7516 if (TYPE_NOSIGN (type))
7517 /* GDB prints characters as NUMBER 'CHAR'. If that's
7518 changed, this can use value_print instead. */
7519 c_printchar (value, type, buf);
7520 else
7521 {
7522 struct value_print_options opts;
7523
7524 if (baton != NULL)
7525 v = dwarf2_evaluate_loc_desc (type, NULL,
7526 baton->data,
7527 baton->size,
7528 baton->per_cu);
7529 else if (bytes != NULL)
7530 {
7531 v = allocate_value (type);
7532 memcpy (value_contents_writeable (v), bytes,
7533 TYPE_LENGTH (type));
7534 }
7535 else
7536 v = value_from_longest (type, value);
7537
3e43a32a
MS
7538 /* Specify decimal so that we do not depend on
7539 the radix. */
98bfdba5
PA
7540 get_formatted_print_options (&opts, 'd');
7541 opts.raw = 1;
7542 value_print (v, buf, &opts);
7543 release_value (v);
7544 value_free (v);
7545 }
7546 }
7547
7548 die->building_fullname = 0;
7549
7550 if (!first)
7551 {
7552 /* Close the argument list, with a space if necessary
7553 (nested templates). */
7554 char last_char = '\0';
7555 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7556 if (last_char == '>')
7557 fputs_unfiltered (" >", buf);
7558 else
7559 fputs_unfiltered (">", buf);
7560 }
7561 }
7562
94af9270
KS
7563 /* For Java and C++ methods, append formal parameter type
7564 information, if PHYSNAME. */
6e70227d 7565
94af9270
KS
7566 if (physname && die->tag == DW_TAG_subprogram
7567 && (cu->language == language_cplus
7568 || cu->language == language_java))
7569 {
7570 struct type *type = read_type_die (die, cu);
7571
79d43c61
TT
7572 c_type_print_args (type, buf, 1, cu->language,
7573 &type_print_raw_options);
94af9270
KS
7574
7575 if (cu->language == language_java)
7576 {
7577 /* For java, we must append the return type to method
0963b4bd 7578 names. */
94af9270
KS
7579 if (die->tag == DW_TAG_subprogram)
7580 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7581 0, 0, &type_print_raw_options);
94af9270
KS
7582 }
7583 else if (cu->language == language_cplus)
7584 {
60430eff
DJ
7585 /* Assume that an artificial first parameter is
7586 "this", but do not crash if it is not. RealView
7587 marks unnamed (and thus unused) parameters as
7588 artificial; there is no way to differentiate
7589 the two cases. */
94af9270
KS
7590 if (TYPE_NFIELDS (type) > 0
7591 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7592 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7593 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7594 0))))
94af9270
KS
7595 fputs_unfiltered (" const", buf);
7596 }
7597 }
7598
bb5ed363 7599 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7600 &length);
7601 ui_file_delete (buf);
7602
7603 if (cu->language == language_cplus)
7604 {
15d034d0 7605 const char *cname
94af9270 7606 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7607 &objfile->objfile_obstack);
9a619af0 7608
94af9270
KS
7609 if (cname != NULL)
7610 name = cname;
7611 }
7612 }
7613 }
7614
7615 return name;
7616}
7617
0114d602
DJ
7618/* Return the fully qualified name of DIE, based on its DW_AT_name.
7619 If scope qualifiers are appropriate they will be added. The result
7620 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7621 not have a name. NAME may either be from a previous call to
7622 dwarf2_name or NULL.
7623
0963b4bd 7624 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7625
7626static const char *
15d034d0 7627dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7628{
94af9270
KS
7629 return dwarf2_compute_name (name, die, cu, 0);
7630}
0114d602 7631
94af9270
KS
7632/* Construct a physname for the given DIE in CU. NAME may either be
7633 from a previous call to dwarf2_name or NULL. The result will be
7634 allocated on the objfile_objstack or NULL if the DIE does not have a
7635 name.
0114d602 7636
94af9270 7637 The output string will be canonicalized (if C++/Java). */
0114d602 7638
94af9270 7639static const char *
15d034d0 7640dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7641{
bb5ed363 7642 struct objfile *objfile = cu->objfile;
900e11f9
JK
7643 struct attribute *attr;
7644 const char *retval, *mangled = NULL, *canon = NULL;
7645 struct cleanup *back_to;
7646 int need_copy = 1;
7647
7648 /* In this case dwarf2_compute_name is just a shortcut not building anything
7649 on its own. */
7650 if (!die_needs_namespace (die, cu))
7651 return dwarf2_compute_name (name, die, cu, 1);
7652
7653 back_to = make_cleanup (null_cleanup, NULL);
7654
7655 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7656 if (!attr)
7657 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7658
7659 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7660 has computed. */
7661 if (attr && DW_STRING (attr))
7662 {
7663 char *demangled;
7664
7665 mangled = DW_STRING (attr);
7666
7667 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7668 type. It is easier for GDB users to search for such functions as
7669 `name(params)' than `long name(params)'. In such case the minimal
7670 symbol names do not match the full symbol names but for template
7671 functions there is never a need to look up their definition from their
7672 declaration so the only disadvantage remains the minimal symbol
7673 variant `long name(params)' does not have the proper inferior type.
7674 */
7675
a766d390
DE
7676 if (cu->language == language_go)
7677 {
7678 /* This is a lie, but we already lie to the caller new_symbol_full.
7679 new_symbol_full assumes we return the mangled name.
7680 This just undoes that lie until things are cleaned up. */
7681 demangled = NULL;
7682 }
7683 else
7684 {
7685 demangled = cplus_demangle (mangled,
7686 (DMGL_PARAMS | DMGL_ANSI
7687 | (cu->language == language_java
7688 ? DMGL_JAVA | DMGL_RET_POSTFIX
7689 : DMGL_RET_DROP)));
7690 }
900e11f9
JK
7691 if (demangled)
7692 {
7693 make_cleanup (xfree, demangled);
7694 canon = demangled;
7695 }
7696 else
7697 {
7698 canon = mangled;
7699 need_copy = 0;
7700 }
7701 }
7702
7703 if (canon == NULL || check_physname)
7704 {
7705 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7706
7707 if (canon != NULL && strcmp (physname, canon) != 0)
7708 {
7709 /* It may not mean a bug in GDB. The compiler could also
7710 compute DW_AT_linkage_name incorrectly. But in such case
7711 GDB would need to be bug-to-bug compatible. */
7712
7713 complaint (&symfile_complaints,
7714 _("Computed physname <%s> does not match demangled <%s> "
7715 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7716 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7717
7718 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7719 is available here - over computed PHYSNAME. It is safer
7720 against both buggy GDB and buggy compilers. */
7721
7722 retval = canon;
7723 }
7724 else
7725 {
7726 retval = physname;
7727 need_copy = 0;
7728 }
7729 }
7730 else
7731 retval = canon;
7732
7733 if (need_copy)
10f0c4bb 7734 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7735
7736 do_cleanups (back_to);
7737 return retval;
0114d602
DJ
7738}
7739
27aa8d6a
SW
7740/* Read the import statement specified by the given die and record it. */
7741
7742static void
7743read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7744{
bb5ed363 7745 struct objfile *objfile = cu->objfile;
27aa8d6a 7746 struct attribute *import_attr;
32019081 7747 struct die_info *imported_die, *child_die;
de4affc9 7748 struct dwarf2_cu *imported_cu;
27aa8d6a 7749 const char *imported_name;
794684b6 7750 const char *imported_name_prefix;
13387711
SW
7751 const char *canonical_name;
7752 const char *import_alias;
7753 const char *imported_declaration = NULL;
794684b6 7754 const char *import_prefix;
32019081
JK
7755 VEC (const_char_ptr) *excludes = NULL;
7756 struct cleanup *cleanups;
13387711 7757
27aa8d6a
SW
7758 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7759 if (import_attr == NULL)
7760 {
7761 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7762 dwarf_tag_name (die->tag));
7763 return;
7764 }
7765
de4affc9
CC
7766 imported_cu = cu;
7767 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7768 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7769 if (imported_name == NULL)
7770 {
7771 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7772
7773 The import in the following code:
7774 namespace A
7775 {
7776 typedef int B;
7777 }
7778
7779 int main ()
7780 {
7781 using A::B;
7782 B b;
7783 return b;
7784 }
7785
7786 ...
7787 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7788 <52> DW_AT_decl_file : 1
7789 <53> DW_AT_decl_line : 6
7790 <54> DW_AT_import : <0x75>
7791 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7792 <59> DW_AT_name : B
7793 <5b> DW_AT_decl_file : 1
7794 <5c> DW_AT_decl_line : 2
7795 <5d> DW_AT_type : <0x6e>
7796 ...
7797 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7798 <76> DW_AT_byte_size : 4
7799 <77> DW_AT_encoding : 5 (signed)
7800
7801 imports the wrong die ( 0x75 instead of 0x58 ).
7802 This case will be ignored until the gcc bug is fixed. */
7803 return;
7804 }
7805
82856980
SW
7806 /* Figure out the local name after import. */
7807 import_alias = dwarf2_name (die, cu);
27aa8d6a 7808
794684b6
SW
7809 /* Figure out where the statement is being imported to. */
7810 import_prefix = determine_prefix (die, cu);
7811
7812 /* Figure out what the scope of the imported die is and prepend it
7813 to the name of the imported die. */
de4affc9 7814 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7815
f55ee35c
JK
7816 if (imported_die->tag != DW_TAG_namespace
7817 && imported_die->tag != DW_TAG_module)
794684b6 7818 {
13387711
SW
7819 imported_declaration = imported_name;
7820 canonical_name = imported_name_prefix;
794684b6 7821 }
13387711 7822 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7823 canonical_name = obconcat (&objfile->objfile_obstack,
7824 imported_name_prefix, "::", imported_name,
7825 (char *) NULL);
13387711
SW
7826 else
7827 canonical_name = imported_name;
794684b6 7828
32019081
JK
7829 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7830
7831 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7832 for (child_die = die->child; child_die && child_die->tag;
7833 child_die = sibling_die (child_die))
7834 {
7835 /* DWARF-4: A Fortran use statement with a “rename list” may be
7836 represented by an imported module entry with an import attribute
7837 referring to the module and owned entries corresponding to those
7838 entities that are renamed as part of being imported. */
7839
7840 if (child_die->tag != DW_TAG_imported_declaration)
7841 {
7842 complaint (&symfile_complaints,
7843 _("child DW_TAG_imported_declaration expected "
7844 "- DIE at 0x%x [in module %s]"),
b64f50a1 7845 child_die->offset.sect_off, objfile->name);
32019081
JK
7846 continue;
7847 }
7848
7849 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7850 if (import_attr == NULL)
7851 {
7852 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7853 dwarf_tag_name (child_die->tag));
7854 continue;
7855 }
7856
7857 imported_cu = cu;
7858 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7859 &imported_cu);
7860 imported_name = dwarf2_name (imported_die, imported_cu);
7861 if (imported_name == NULL)
7862 {
7863 complaint (&symfile_complaints,
7864 _("child DW_TAG_imported_declaration has unknown "
7865 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7866 child_die->offset.sect_off, objfile->name);
32019081
JK
7867 continue;
7868 }
7869
7870 VEC_safe_push (const_char_ptr, excludes, imported_name);
7871
7872 process_die (child_die, cu);
7873 }
7874
c0cc3a76
SW
7875 cp_add_using_directive (import_prefix,
7876 canonical_name,
7877 import_alias,
13387711 7878 imported_declaration,
32019081 7879 excludes,
12aaed36 7880 0,
bb5ed363 7881 &objfile->objfile_obstack);
32019081
JK
7882
7883 do_cleanups (cleanups);
27aa8d6a
SW
7884}
7885
f4dc4d17 7886/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7887
cb1df416
DJ
7888static void
7889free_cu_line_header (void *arg)
7890{
7891 struct dwarf2_cu *cu = arg;
7892
7893 free_line_header (cu->line_header);
7894 cu->line_header = NULL;
7895}
7896
1b80a9fa
JK
7897/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7898 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7899 this, it was first present in GCC release 4.3.0. */
7900
7901static int
7902producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7903{
7904 if (!cu->checked_producer)
7905 check_producer (cu);
7906
7907 return cu->producer_is_gcc_lt_4_3;
7908}
7909
9291a0cd
TT
7910static void
7911find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7912 const char **name, const char **comp_dir)
9291a0cd
TT
7913{
7914 struct attribute *attr;
7915
7916 *name = NULL;
7917 *comp_dir = NULL;
7918
7919 /* Find the filename. Do not use dwarf2_name here, since the filename
7920 is not a source language identifier. */
7921 attr = dwarf2_attr (die, DW_AT_name, cu);
7922 if (attr)
7923 {
7924 *name = DW_STRING (attr);
7925 }
7926
7927 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7928 if (attr)
7929 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7930 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7931 && IS_ABSOLUTE_PATH (*name))
9291a0cd 7932 {
15d034d0
TT
7933 char *d = ldirname (*name);
7934
7935 *comp_dir = d;
7936 if (d != NULL)
7937 make_cleanup (xfree, d);
9291a0cd
TT
7938 }
7939 if (*comp_dir != NULL)
7940 {
7941 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7942 directory, get rid of it. */
7943 char *cp = strchr (*comp_dir, ':');
7944
7945 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7946 *comp_dir = cp + 1;
7947 }
7948
7949 if (*name == NULL)
7950 *name = "<unknown>";
7951}
7952
f4dc4d17
DE
7953/* Handle DW_AT_stmt_list for a compilation unit.
7954 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7955 COMP_DIR is the compilation directory.
7956 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7957
7958static void
7959handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7960 const char *comp_dir)
2ab95328
TT
7961{
7962 struct attribute *attr;
2ab95328 7963
f4dc4d17
DE
7964 gdb_assert (! cu->per_cu->is_debug_types);
7965
2ab95328
TT
7966 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7967 if (attr)
7968 {
7969 unsigned int line_offset = DW_UNSND (attr);
7970 struct line_header *line_header
3019eac3 7971 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7972
7973 if (line_header)
dee91e82
DE
7974 {
7975 cu->line_header = line_header;
7976 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7977 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7978 }
2ab95328
TT
7979 }
7980}
7981
95554aad 7982/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7983
c906108c 7984static void
e7c27a73 7985read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7986{
dee91e82 7987 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7988 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7989 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7990 CORE_ADDR highpc = ((CORE_ADDR) 0);
7991 struct attribute *attr;
15d034d0
TT
7992 const char *name = NULL;
7993 const char *comp_dir = NULL;
c906108c
SS
7994 struct die_info *child_die;
7995 bfd *abfd = objfile->obfd;
e142c38c 7996 CORE_ADDR baseaddr;
6e70227d 7997
e142c38c 7998 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7999
fae299cd 8000 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8001
8002 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8003 from finish_block. */
2acceee2 8004 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8005 lowpc = highpc;
8006 lowpc += baseaddr;
8007 highpc += baseaddr;
8008
9291a0cd 8009 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8010
95554aad 8011 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8012
f4b8a18d
KW
8013 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8014 standardised yet. As a workaround for the language detection we fall
8015 back to the DW_AT_producer string. */
8016 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8017 cu->language = language_opencl;
8018
3019eac3
DE
8019 /* Similar hack for Go. */
8020 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8021 set_cu_language (DW_LANG_Go, cu);
8022
f4dc4d17 8023 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8024
8025 /* Decode line number information if present. We do this before
8026 processing child DIEs, so that the line header table is available
8027 for DW_AT_decl_file. */
f4dc4d17 8028 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8029
8030 /* Process all dies in compilation unit. */
8031 if (die->child != NULL)
8032 {
8033 child_die = die->child;
8034 while (child_die && child_die->tag)
8035 {
8036 process_die (child_die, cu);
8037 child_die = sibling_die (child_die);
8038 }
8039 }
8040
8041 /* Decode macro information, if present. Dwarf 2 macro information
8042 refers to information in the line number info statement program
8043 header, so we can only read it if we've read the header
8044 successfully. */
8045 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8046 if (attr && cu->line_header)
8047 {
8048 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8049 complaint (&symfile_complaints,
8050 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8051
09262596 8052 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8053 }
8054 else
8055 {
8056 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8057 if (attr && cu->line_header)
8058 {
8059 unsigned int macro_offset = DW_UNSND (attr);
8060
09262596 8061 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8062 }
8063 }
8064
8065 do_cleanups (back_to);
8066}
8067
f4dc4d17
DE
8068/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8069 Create the set of symtabs used by this TU, or if this TU is sharing
8070 symtabs with another TU and the symtabs have already been created
8071 then restore those symtabs in the line header.
8072 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8073
8074static void
f4dc4d17 8075setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8076{
f4dc4d17
DE
8077 struct objfile *objfile = dwarf2_per_objfile->objfile;
8078 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8079 struct type_unit_group *tu_group;
8080 int first_time;
8081 struct line_header *lh;
3019eac3 8082 struct attribute *attr;
f4dc4d17 8083 unsigned int i, line_offset;
3019eac3 8084
f4dc4d17 8085 gdb_assert (per_cu->is_debug_types);
3019eac3 8086
f4dc4d17 8087 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8088
f4dc4d17
DE
8089 /* If we're using .gdb_index (includes -readnow) then
8090 per_cu->s.type_unit_group may not have been set up yet. */
796a7ff8
DE
8091 if (per_cu->type_unit_group == NULL)
8092 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8093 tu_group = per_cu->type_unit_group;
f4dc4d17
DE
8094
8095 /* If we've already processed this stmt_list there's no real need to
8096 do it again, we could fake it and just recreate the part we need
8097 (file name,index -> symtab mapping). If data shows this optimization
8098 is useful we can do it then. */
8099 first_time = tu_group->primary_symtab == NULL;
8100
8101 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8102 debug info. */
8103 lh = NULL;
8104 if (attr != NULL)
3019eac3 8105 {
f4dc4d17
DE
8106 line_offset = DW_UNSND (attr);
8107 lh = dwarf_decode_line_header (line_offset, cu);
8108 }
8109 if (lh == NULL)
8110 {
8111 if (first_time)
8112 dwarf2_start_symtab (cu, "", NULL, 0);
8113 else
8114 {
8115 gdb_assert (tu_group->symtabs == NULL);
8116 restart_symtab (0);
8117 }
8118 /* Note: The primary symtab will get allocated at the end. */
8119 return;
3019eac3
DE
8120 }
8121
f4dc4d17
DE
8122 cu->line_header = lh;
8123 make_cleanup (free_cu_line_header, cu);
3019eac3 8124
f4dc4d17
DE
8125 if (first_time)
8126 {
8127 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8128
f4dc4d17
DE
8129 tu_group->num_symtabs = lh->num_file_names;
8130 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8131
f4dc4d17
DE
8132 for (i = 0; i < lh->num_file_names; ++i)
8133 {
8134 char *dir = NULL;
8135 struct file_entry *fe = &lh->file_names[i];
3019eac3 8136
f4dc4d17
DE
8137 if (fe->dir_index)
8138 dir = lh->include_dirs[fe->dir_index - 1];
8139 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8140
f4dc4d17
DE
8141 /* Note: We don't have to watch for the main subfile here, type units
8142 don't have DW_AT_name. */
3019eac3 8143
f4dc4d17
DE
8144 if (current_subfile->symtab == NULL)
8145 {
8146 /* NOTE: start_subfile will recognize when it's been passed
8147 a file it has already seen. So we can't assume there's a
8148 simple mapping from lh->file_names to subfiles,
8149 lh->file_names may contain dups. */
8150 current_subfile->symtab = allocate_symtab (current_subfile->name,
8151 objfile);
8152 }
8153
8154 fe->symtab = current_subfile->symtab;
8155 tu_group->symtabs[i] = fe->symtab;
8156 }
8157 }
8158 else
3019eac3 8159 {
f4dc4d17
DE
8160 restart_symtab (0);
8161
8162 for (i = 0; i < lh->num_file_names; ++i)
8163 {
8164 struct file_entry *fe = &lh->file_names[i];
8165
8166 fe->symtab = tu_group->symtabs[i];
8167 }
3019eac3
DE
8168 }
8169
f4dc4d17
DE
8170 /* The main symtab is allocated last. Type units don't have DW_AT_name
8171 so they don't have a "real" (so to speak) symtab anyway.
8172 There is later code that will assign the main symtab to all symbols
8173 that don't have one. We need to handle the case of a symbol with a
8174 missing symtab (DW_AT_decl_file) anyway. */
8175}
3019eac3 8176
f4dc4d17
DE
8177/* Process DW_TAG_type_unit.
8178 For TUs we want to skip the first top level sibling if it's not the
8179 actual type being defined by this TU. In this case the first top
8180 level sibling is there to provide context only. */
3019eac3 8181
f4dc4d17
DE
8182static void
8183read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8184{
8185 struct die_info *child_die;
3019eac3 8186
f4dc4d17
DE
8187 prepare_one_comp_unit (cu, die, language_minimal);
8188
8189 /* Initialize (or reinitialize) the machinery for building symtabs.
8190 We do this before processing child DIEs, so that the line header table
8191 is available for DW_AT_decl_file. */
8192 setup_type_unit_groups (die, cu);
8193
8194 if (die->child != NULL)
8195 {
8196 child_die = die->child;
8197 while (child_die && child_die->tag)
8198 {
8199 process_die (child_die, cu);
8200 child_die = sibling_die (child_die);
8201 }
8202 }
3019eac3
DE
8203}
8204\f
80626a55
DE
8205/* DWO/DWP files.
8206
8207 http://gcc.gnu.org/wiki/DebugFission
8208 http://gcc.gnu.org/wiki/DebugFissionDWP
8209
8210 To simplify handling of both DWO files ("object" files with the DWARF info)
8211 and DWP files (a file with the DWOs packaged up into one file), we treat
8212 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8213
8214static hashval_t
8215hash_dwo_file (const void *item)
8216{
8217 const struct dwo_file *dwo_file = item;
8218
80626a55 8219 return htab_hash_string (dwo_file->name);
3019eac3
DE
8220}
8221
8222static int
8223eq_dwo_file (const void *item_lhs, const void *item_rhs)
8224{
8225 const struct dwo_file *lhs = item_lhs;
8226 const struct dwo_file *rhs = item_rhs;
8227
80626a55 8228 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8229}
8230
8231/* Allocate a hash table for DWO files. */
8232
8233static htab_t
8234allocate_dwo_file_hash_table (void)
8235{
8236 struct objfile *objfile = dwarf2_per_objfile->objfile;
8237
8238 return htab_create_alloc_ex (41,
8239 hash_dwo_file,
8240 eq_dwo_file,
8241 NULL,
8242 &objfile->objfile_obstack,
8243 hashtab_obstack_allocate,
8244 dummy_obstack_deallocate);
8245}
8246
80626a55
DE
8247/* Lookup DWO file DWO_NAME. */
8248
8249static void **
8250lookup_dwo_file_slot (const char *dwo_name)
8251{
8252 struct dwo_file find_entry;
8253 void **slot;
8254
8255 if (dwarf2_per_objfile->dwo_files == NULL)
8256 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8257
8258 memset (&find_entry, 0, sizeof (find_entry));
8259 find_entry.name = dwo_name;
8260 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8261
8262 return slot;
8263}
8264
3019eac3
DE
8265static hashval_t
8266hash_dwo_unit (const void *item)
8267{
8268 const struct dwo_unit *dwo_unit = item;
8269
8270 /* This drops the top 32 bits of the id, but is ok for a hash. */
8271 return dwo_unit->signature;
8272}
8273
8274static int
8275eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8276{
8277 const struct dwo_unit *lhs = item_lhs;
8278 const struct dwo_unit *rhs = item_rhs;
8279
8280 /* The signature is assumed to be unique within the DWO file.
8281 So while object file CU dwo_id's always have the value zero,
8282 that's OK, assuming each object file DWO file has only one CU,
8283 and that's the rule for now. */
8284 return lhs->signature == rhs->signature;
8285}
8286
8287/* Allocate a hash table for DWO CUs,TUs.
8288 There is one of these tables for each of CUs,TUs for each DWO file. */
8289
8290static htab_t
8291allocate_dwo_unit_table (struct objfile *objfile)
8292{
8293 /* Start out with a pretty small number.
8294 Generally DWO files contain only one CU and maybe some TUs. */
8295 return htab_create_alloc_ex (3,
8296 hash_dwo_unit,
8297 eq_dwo_unit,
8298 NULL,
8299 &objfile->objfile_obstack,
8300 hashtab_obstack_allocate,
8301 dummy_obstack_deallocate);
8302}
8303
80626a55 8304/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8305
8306struct create_dwo_info_table_data
8307{
8308 struct dwo_file *dwo_file;
8309 htab_t cu_htab;
8310};
8311
80626a55 8312/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8313
8314static void
80626a55
DE
8315create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8316 gdb_byte *info_ptr,
8317 struct die_info *comp_unit_die,
8318 int has_children,
8319 void *datap)
3019eac3
DE
8320{
8321 struct dwarf2_cu *cu = reader->cu;
8322 struct objfile *objfile = dwarf2_per_objfile->objfile;
8323 sect_offset offset = cu->per_cu->offset;
8a0459fd 8324 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3
DE
8325 struct create_dwo_info_table_data *data = datap;
8326 struct dwo_file *dwo_file = data->dwo_file;
8327 htab_t cu_htab = data->cu_htab;
8328 void **slot;
8329 struct attribute *attr;
8330 struct dwo_unit *dwo_unit;
8331
8332 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8333 if (attr == NULL)
8334 {
8335 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8336 " its dwo_id [in module %s]"),
80626a55 8337 offset.sect_off, dwo_file->name);
3019eac3
DE
8338 return;
8339 }
8340
8341 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8342 dwo_unit->dwo_file = dwo_file;
8343 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8344 dwo_unit->section = section;
3019eac3
DE
8345 dwo_unit->offset = offset;
8346 dwo_unit->length = cu->per_cu->length;
8347
8348 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8349 gdb_assert (slot != NULL);
8350 if (*slot != NULL)
8351 {
8352 const struct dwo_unit *dup_dwo_unit = *slot;
8353
8354 complaint (&symfile_complaints,
8355 _("debug entry at offset 0x%x is duplicate to the entry at"
8356 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8357 offset.sect_off, dup_dwo_unit->offset.sect_off,
8358 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8359 dwo_file->name);
3019eac3
DE
8360 }
8361 else
8362 *slot = dwo_unit;
8363
09406207 8364 if (dwarf2_read_debug)
3019eac3
DE
8365 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8366 offset.sect_off,
8367 phex (dwo_unit->signature,
8368 sizeof (dwo_unit->signature)));
8369}
8370
80626a55
DE
8371/* Create a hash table to map DWO IDs to their CU entry in
8372 .debug_info.dwo in DWO_FILE.
8373 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8374
8375static htab_t
80626a55 8376create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8377{
8378 struct objfile *objfile = dwarf2_per_objfile->objfile;
8379 struct dwarf2_section_info *section = &dwo_file->sections.info;
8380 bfd *abfd;
8381 htab_t cu_htab;
8382 gdb_byte *info_ptr, *end_ptr;
8383 struct create_dwo_info_table_data create_dwo_info_table_data;
8384
8385 dwarf2_read_section (objfile, section);
8386 info_ptr = section->buffer;
8387
8388 if (info_ptr == NULL)
8389 return NULL;
8390
8391 /* We can't set abfd until now because the section may be empty or
8392 not present, in which case section->asection will be NULL. */
8393 abfd = section->asection->owner;
8394
09406207 8395 if (dwarf2_read_debug)
3019eac3
DE
8396 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8397 bfd_get_filename (abfd));
8398
8399 cu_htab = allocate_dwo_unit_table (objfile);
8400
8401 create_dwo_info_table_data.dwo_file = dwo_file;
8402 create_dwo_info_table_data.cu_htab = cu_htab;
8403
8404 end_ptr = info_ptr + section->size;
8405 while (info_ptr < end_ptr)
8406 {
8407 struct dwarf2_per_cu_data per_cu;
8408
8409 memset (&per_cu, 0, sizeof (per_cu));
8410 per_cu.objfile = objfile;
8411 per_cu.is_debug_types = 0;
8412 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8413 per_cu.section = section;
3019eac3
DE
8414
8415 init_cutu_and_read_dies_no_follow (&per_cu,
8416 &dwo_file->sections.abbrev,
8417 dwo_file,
80626a55 8418 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8419 &create_dwo_info_table_data);
8420
8421 info_ptr += per_cu.length;
8422 }
8423
8424 return cu_htab;
8425}
8426
80626a55
DE
8427/* DWP file .debug_{cu,tu}_index section format:
8428 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8429
8430 Both index sections have the same format, and serve to map a 64-bit
8431 signature to a set of section numbers. Each section begins with a header,
8432 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8433 indexes, and a pool of 32-bit section numbers. The index sections will be
8434 aligned at 8-byte boundaries in the file.
8435
8436 The index section header contains two unsigned 32-bit values (using the
8437 byte order of the application binary):
8438
8439 N, the number of compilation units or type units in the index
8440 M, the number of slots in the hash table
8441
8442 (We assume that N and M will not exceed 2^32 - 1.)
8443
8444 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8445
8446 The hash table begins at offset 8 in the section, and consists of an array
8447 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8448 order of the application binary). Unused slots in the hash table are 0.
8449 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8450
8451 The parallel table begins immediately after the hash table
8452 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8453 array of 32-bit indexes (using the byte order of the application binary),
8454 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8455 table contains a 32-bit index into the pool of section numbers. For unused
8456 hash table slots, the corresponding entry in the parallel table will be 0.
8457
8458 Given a 64-bit compilation unit signature or a type signature S, an entry
8459 in the hash table is located as follows:
8460
8461 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8462 the low-order k bits all set to 1.
8463
8464 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8465
8466 3) If the hash table entry at index H matches the signature, use that
8467 entry. If the hash table entry at index H is unused (all zeroes),
8468 terminate the search: the signature is not present in the table.
8469
8470 4) Let H = (H + H') modulo M. Repeat at Step 3.
8471
8472 Because M > N and H' and M are relatively prime, the search is guaranteed
8473 to stop at an unused slot or find the match.
8474
8475 The pool of section numbers begins immediately following the hash table
8476 (at offset 8 + 12 * M from the beginning of the section). The pool of
8477 section numbers consists of an array of 32-bit words (using the byte order
8478 of the application binary). Each item in the array is indexed starting
8479 from 0. The hash table entry provides the index of the first section
8480 number in the set. Additional section numbers in the set follow, and the
8481 set is terminated by a 0 entry (section number 0 is not used in ELF).
8482
8483 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8484 section must be the first entry in the set, and the .debug_abbrev.dwo must
8485 be the second entry. Other members of the set may follow in any order. */
8486
8487/* Create a hash table to map DWO IDs to their CU/TU entry in
8488 .debug_{info,types}.dwo in DWP_FILE.
8489 Returns NULL if there isn't one.
8490 Note: This function processes DWP files only, not DWO files. */
8491
8492static struct dwp_hash_table *
8493create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8494{
8495 struct objfile *objfile = dwarf2_per_objfile->objfile;
8496 bfd *dbfd = dwp_file->dbfd;
8497 char *index_ptr, *index_end;
8498 struct dwarf2_section_info *index;
8499 uint32_t version, nr_units, nr_slots;
8500 struct dwp_hash_table *htab;
8501
8502 if (is_debug_types)
8503 index = &dwp_file->sections.tu_index;
8504 else
8505 index = &dwp_file->sections.cu_index;
8506
8507 if (dwarf2_section_empty_p (index))
8508 return NULL;
8509 dwarf2_read_section (objfile, index);
8510
8511 index_ptr = index->buffer;
8512 index_end = index_ptr + index->size;
8513
8514 version = read_4_bytes (dbfd, index_ptr);
8515 index_ptr += 8; /* Skip the unused word. */
8516 nr_units = read_4_bytes (dbfd, index_ptr);
8517 index_ptr += 4;
8518 nr_slots = read_4_bytes (dbfd, index_ptr);
8519 index_ptr += 4;
8520
8521 if (version != 1)
8522 {
8523 error (_("Dwarf Error: unsupported DWP file version (%u)"
8524 " [in module %s]"),
8525 version, dwp_file->name);
8526 }
8527 if (nr_slots != (nr_slots & -nr_slots))
8528 {
8529 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8530 " is not power of 2 [in module %s]"),
8531 nr_slots, dwp_file->name);
8532 }
8533
8534 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8535 htab->nr_units = nr_units;
8536 htab->nr_slots = nr_slots;
8537 htab->hash_table = index_ptr;
8538 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8539 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8540
8541 return htab;
8542}
8543
8544/* Update SECTIONS with the data from SECTP.
8545
8546 This function is like the other "locate" section routines that are
8547 passed to bfd_map_over_sections, but in this context the sections to
8548 read comes from the DWP hash table, not the full ELF section table.
8549
8550 The result is non-zero for success, or zero if an error was found. */
8551
8552static int
8553locate_virtual_dwo_sections (asection *sectp,
8554 struct virtual_dwo_sections *sections)
8555{
8556 const struct dwop_section_names *names = &dwop_section_names;
8557
8558 if (section_is_p (sectp->name, &names->abbrev_dwo))
8559 {
8560 /* There can be only one. */
8561 if (sections->abbrev.asection != NULL)
8562 return 0;
8563 sections->abbrev.asection = sectp;
8564 sections->abbrev.size = bfd_get_section_size (sectp);
8565 }
8566 else if (section_is_p (sectp->name, &names->info_dwo)
8567 || section_is_p (sectp->name, &names->types_dwo))
8568 {
8569 /* There can be only one. */
8570 if (sections->info_or_types.asection != NULL)
8571 return 0;
8572 sections->info_or_types.asection = sectp;
8573 sections->info_or_types.size = bfd_get_section_size (sectp);
8574 }
8575 else if (section_is_p (sectp->name, &names->line_dwo))
8576 {
8577 /* There can be only one. */
8578 if (sections->line.asection != NULL)
8579 return 0;
8580 sections->line.asection = sectp;
8581 sections->line.size = bfd_get_section_size (sectp);
8582 }
8583 else if (section_is_p (sectp->name, &names->loc_dwo))
8584 {
8585 /* There can be only one. */
8586 if (sections->loc.asection != NULL)
8587 return 0;
8588 sections->loc.asection = sectp;
8589 sections->loc.size = bfd_get_section_size (sectp);
8590 }
8591 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8592 {
8593 /* There can be only one. */
8594 if (sections->macinfo.asection != NULL)
8595 return 0;
8596 sections->macinfo.asection = sectp;
8597 sections->macinfo.size = bfd_get_section_size (sectp);
8598 }
8599 else if (section_is_p (sectp->name, &names->macro_dwo))
8600 {
8601 /* There can be only one. */
8602 if (sections->macro.asection != NULL)
8603 return 0;
8604 sections->macro.asection = sectp;
8605 sections->macro.size = bfd_get_section_size (sectp);
8606 }
8607 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8608 {
8609 /* There can be only one. */
8610 if (sections->str_offsets.asection != NULL)
8611 return 0;
8612 sections->str_offsets.asection = sectp;
8613 sections->str_offsets.size = bfd_get_section_size (sectp);
8614 }
8615 else
8616 {
8617 /* No other kind of section is valid. */
8618 return 0;
8619 }
8620
8621 return 1;
8622}
8623
8624/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8625 HTAB is the hash table from the DWP file.
8626 SECTION_INDEX is the index of the DWO in HTAB. */
8627
8628static struct dwo_unit *
8629create_dwo_in_dwp (struct dwp_file *dwp_file,
8630 const struct dwp_hash_table *htab,
8631 uint32_t section_index,
8632 ULONGEST signature, int is_debug_types)
8633{
8634 struct objfile *objfile = dwarf2_per_objfile->objfile;
8635 bfd *dbfd = dwp_file->dbfd;
8636 const char *kind = is_debug_types ? "TU" : "CU";
8637 struct dwo_file *dwo_file;
8638 struct dwo_unit *dwo_unit;
8639 struct virtual_dwo_sections sections;
8640 void **dwo_file_slot;
8641 char *virtual_dwo_name;
8642 struct dwarf2_section_info *cutu;
8643 struct cleanup *cleanups;
8644 int i;
8645
8646 if (dwarf2_read_debug)
8647 {
8648 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8649 kind,
8650 section_index, phex (signature, sizeof (signature)),
8651 dwp_file->name);
8652 }
8653
8654 /* Fetch the sections of this DWO.
8655 Put a limit on the number of sections we look for so that bad data
8656 doesn't cause us to loop forever. */
8657
8658#define MAX_NR_DWO_SECTIONS \
8659 (1 /* .debug_info or .debug_types */ \
8660 + 1 /* .debug_abbrev */ \
8661 + 1 /* .debug_line */ \
8662 + 1 /* .debug_loc */ \
8663 + 1 /* .debug_str_offsets */ \
8664 + 1 /* .debug_macro */ \
8665 + 1 /* .debug_macinfo */ \
8666 + 1 /* trailing zero */)
8667
8668 memset (&sections, 0, sizeof (sections));
8669 cleanups = make_cleanup (null_cleanup, 0);
8670
8671 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8672 {
8673 asection *sectp;
8674 uint32_t section_nr =
8675 read_4_bytes (dbfd,
8676 htab->section_pool
8677 + (section_index + i) * sizeof (uint32_t));
8678
8679 if (section_nr == 0)
8680 break;
8681 if (section_nr >= dwp_file->num_sections)
8682 {
8683 error (_("Dwarf Error: bad DWP hash table, section number too large"
8684 " [in module %s]"),
8685 dwp_file->name);
8686 }
8687
8688 sectp = dwp_file->elf_sections[section_nr];
8689 if (! locate_virtual_dwo_sections (sectp, &sections))
8690 {
8691 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8692 " [in module %s]"),
8693 dwp_file->name);
8694 }
8695 }
8696
8697 if (i < 2
8698 || sections.info_or_types.asection == NULL
8699 || sections.abbrev.asection == NULL)
8700 {
8701 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8702 " [in module %s]"),
8703 dwp_file->name);
8704 }
8705 if (i == MAX_NR_DWO_SECTIONS)
8706 {
8707 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8708 " [in module %s]"),
8709 dwp_file->name);
8710 }
8711
8712 /* It's easier for the rest of the code if we fake a struct dwo_file and
8713 have dwo_unit "live" in that. At least for now.
8714
8715 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8716 However, for each CU + set of TUs that came from the same original DWO
8717 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8718 (fewer struct dwo_file objects to allocated). Remember that for really
8719 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8720
2792b94d
PM
8721 virtual_dwo_name =
8722 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8723 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8724 sections.line.asection ? sections.line.asection->id : 0,
8725 sections.loc.asection ? sections.loc.asection->id : 0,
8726 (sections.str_offsets.asection
8727 ? sections.str_offsets.asection->id
8728 : 0));
80626a55
DE
8729 make_cleanup (xfree, virtual_dwo_name);
8730 /* Can we use an existing virtual DWO file? */
8731 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8732 /* Create one if necessary. */
8733 if (*dwo_file_slot == NULL)
8734 {
8735 if (dwarf2_read_debug)
8736 {
8737 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8738 virtual_dwo_name);
8739 }
8740 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8741 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8742 virtual_dwo_name,
8743 strlen (virtual_dwo_name));
8744 dwo_file->sections.abbrev = sections.abbrev;
8745 dwo_file->sections.line = sections.line;
8746 dwo_file->sections.loc = sections.loc;
8747 dwo_file->sections.macinfo = sections.macinfo;
8748 dwo_file->sections.macro = sections.macro;
8749 dwo_file->sections.str_offsets = sections.str_offsets;
8750 /* The "str" section is global to the entire DWP file. */
8751 dwo_file->sections.str = dwp_file->sections.str;
8752 /* The info or types section is assigned later to dwo_unit,
8753 there's no need to record it in dwo_file.
8754 Also, we can't simply record type sections in dwo_file because
8755 we record a pointer into the vector in dwo_unit. As we collect more
8756 types we'll grow the vector and eventually have to reallocate space
8757 for it, invalidating all the pointers into the current copy. */
8758 *dwo_file_slot = dwo_file;
8759 }
8760 else
8761 {
8762 if (dwarf2_read_debug)
8763 {
8764 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8765 virtual_dwo_name);
8766 }
8767 dwo_file = *dwo_file_slot;
8768 }
8769 do_cleanups (cleanups);
8770
8771 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8772 dwo_unit->dwo_file = dwo_file;
8773 dwo_unit->signature = signature;
8a0459fd
DE
8774 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8775 sizeof (struct dwarf2_section_info));
8776 *dwo_unit->section = sections.info_or_types;
80626a55
DE
8777 /* offset, length, type_offset_in_tu are set later. */
8778
8779 return dwo_unit;
8780}
8781
8782/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8783
8784static struct dwo_unit *
8785lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8786 const struct dwp_hash_table *htab,
8787 ULONGEST signature, int is_debug_types)
8788{
8789 bfd *dbfd = dwp_file->dbfd;
8790 uint32_t mask = htab->nr_slots - 1;
8791 uint32_t hash = signature & mask;
8792 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8793 unsigned int i;
8794 void **slot;
8795 struct dwo_unit find_dwo_cu, *dwo_cu;
8796
8797 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8798 find_dwo_cu.signature = signature;
8799 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8800
8801 if (*slot != NULL)
8802 return *slot;
8803
8804 /* Use a for loop so that we don't loop forever on bad debug info. */
8805 for (i = 0; i < htab->nr_slots; ++i)
8806 {
8807 ULONGEST signature_in_table;
8808
8809 signature_in_table =
8810 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8811 if (signature_in_table == signature)
8812 {
8813 uint32_t section_index =
8814 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8815
8816 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8817 signature, is_debug_types);
8818 return *slot;
8819 }
8820 if (signature_in_table == 0)
8821 return NULL;
8822 hash = (hash + hash2) & mask;
8823 }
8824
8825 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8826 " [in module %s]"),
8827 dwp_file->name);
8828}
8829
8830/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8831 Open the file specified by FILE_NAME and hand it off to BFD for
8832 preliminary analysis. Return a newly initialized bfd *, which
8833 includes a canonicalized copy of FILE_NAME.
80626a55 8834 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8835 In case of trouble, return NULL.
8836 NOTE: This function is derived from symfile_bfd_open. */
8837
8838static bfd *
80626a55 8839try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8840{
8841 bfd *sym_bfd;
80626a55 8842 int desc, flags;
3019eac3 8843 char *absolute_name;
3019eac3 8844
80626a55
DE
8845 flags = OPF_TRY_CWD_FIRST;
8846 if (is_dwp)
8847 flags |= OPF_SEARCH_IN_PATH;
8848 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8849 O_RDONLY | O_BINARY, &absolute_name);
8850 if (desc < 0)
8851 return NULL;
8852
bb397797 8853 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8854 if (!sym_bfd)
8855 {
3019eac3
DE
8856 xfree (absolute_name);
8857 return NULL;
8858 }
a4453b7e 8859 xfree (absolute_name);
3019eac3
DE
8860 bfd_set_cacheable (sym_bfd, 1);
8861
8862 if (!bfd_check_format (sym_bfd, bfd_object))
8863 {
cbb099e8 8864 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8865 return NULL;
8866 }
8867
3019eac3
DE
8868 return sym_bfd;
8869}
8870
80626a55 8871/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8872 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8873 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8874 The result is the bfd handle of the file.
8875 If there is a problem finding or opening the file, return NULL.
8876 Upon success, the canonicalized path of the file is stored in the bfd,
8877 same as symfile_bfd_open. */
8878
8879static bfd *
80626a55 8880open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8881{
8882 bfd *abfd;
3019eac3 8883
80626a55
DE
8884 if (IS_ABSOLUTE_PATH (file_name))
8885 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8886
8887 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8888
8889 if (comp_dir != NULL)
8890 {
80626a55 8891 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8892
8893 /* NOTE: If comp_dir is a relative path, this will also try the
8894 search path, which seems useful. */
80626a55 8895 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8896 xfree (path_to_try);
8897 if (abfd != NULL)
8898 return abfd;
8899 }
8900
8901 /* That didn't work, try debug-file-directory, which, despite its name,
8902 is a list of paths. */
8903
8904 if (*debug_file_directory == '\0')
8905 return NULL;
8906
80626a55 8907 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8908}
8909
80626a55
DE
8910/* This function is mapped across the sections and remembers the offset and
8911 size of each of the DWO debugging sections we are interested in. */
8912
8913static void
8914dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8915{
8916 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8917 const struct dwop_section_names *names = &dwop_section_names;
8918
8919 if (section_is_p (sectp->name, &names->abbrev_dwo))
8920 {
8921 dwo_sections->abbrev.asection = sectp;
8922 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8923 }
8924 else if (section_is_p (sectp->name, &names->info_dwo))
8925 {
8926 dwo_sections->info.asection = sectp;
8927 dwo_sections->info.size = bfd_get_section_size (sectp);
8928 }
8929 else if (section_is_p (sectp->name, &names->line_dwo))
8930 {
8931 dwo_sections->line.asection = sectp;
8932 dwo_sections->line.size = bfd_get_section_size (sectp);
8933 }
8934 else if (section_is_p (sectp->name, &names->loc_dwo))
8935 {
8936 dwo_sections->loc.asection = sectp;
8937 dwo_sections->loc.size = bfd_get_section_size (sectp);
8938 }
8939 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8940 {
8941 dwo_sections->macinfo.asection = sectp;
8942 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8943 }
8944 else if (section_is_p (sectp->name, &names->macro_dwo))
8945 {
8946 dwo_sections->macro.asection = sectp;
8947 dwo_sections->macro.size = bfd_get_section_size (sectp);
8948 }
8949 else if (section_is_p (sectp->name, &names->str_dwo))
8950 {
8951 dwo_sections->str.asection = sectp;
8952 dwo_sections->str.size = bfd_get_section_size (sectp);
8953 }
8954 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8955 {
8956 dwo_sections->str_offsets.asection = sectp;
8957 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8958 }
8959 else if (section_is_p (sectp->name, &names->types_dwo))
8960 {
8961 struct dwarf2_section_info type_section;
8962
8963 memset (&type_section, 0, sizeof (type_section));
8964 type_section.asection = sectp;
8965 type_section.size = bfd_get_section_size (sectp);
8966 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8967 &type_section);
8968 }
8969}
8970
8971/* Initialize the use of the DWO file specified by DWO_NAME.
8972 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8973
8974static struct dwo_file *
80626a55 8975open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8976{
8977 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8978 struct dwo_file *dwo_file;
8979 bfd *dbfd;
3019eac3
DE
8980 struct cleanup *cleanups;
8981
80626a55
DE
8982 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8983 if (dbfd == NULL)
8984 {
8985 if (dwarf2_read_debug)
8986 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8987 return NULL;
8988 }
8989 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8990 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8991 dwo_name, strlen (dwo_name));
8992 dwo_file->dbfd = dbfd;
3019eac3
DE
8993
8994 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8995
80626a55 8996 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8997
80626a55 8998 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8999
9000 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9001 dwo_file->sections.types);
9002
9003 discard_cleanups (cleanups);
9004
80626a55
DE
9005 if (dwarf2_read_debug)
9006 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9007
3019eac3
DE
9008 return dwo_file;
9009}
9010
80626a55
DE
9011/* This function is mapped across the sections and remembers the offset and
9012 size of each of the DWP debugging sections we are interested in. */
3019eac3 9013
80626a55
DE
9014static void
9015dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9016{
80626a55
DE
9017 struct dwp_file *dwp_file = dwp_file_ptr;
9018 const struct dwop_section_names *names = &dwop_section_names;
9019 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9020
80626a55
DE
9021 /* Record the ELF section number for later lookup: this is what the
9022 .debug_cu_index,.debug_tu_index tables use. */
9023 gdb_assert (elf_section_nr < dwp_file->num_sections);
9024 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9025
80626a55
DE
9026 /* Look for specific sections that we need. */
9027 if (section_is_p (sectp->name, &names->str_dwo))
9028 {
9029 dwp_file->sections.str.asection = sectp;
9030 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9031 }
9032 else if (section_is_p (sectp->name, &names->cu_index))
9033 {
9034 dwp_file->sections.cu_index.asection = sectp;
9035 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9036 }
9037 else if (section_is_p (sectp->name, &names->tu_index))
9038 {
9039 dwp_file->sections.tu_index.asection = sectp;
9040 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9041 }
9042}
3019eac3 9043
80626a55 9044/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9045
80626a55
DE
9046static hashval_t
9047hash_dwp_loaded_cutus (const void *item)
9048{
9049 const struct dwo_unit *dwo_unit = item;
3019eac3 9050
80626a55
DE
9051 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9052 return dwo_unit->signature;
3019eac3
DE
9053}
9054
80626a55 9055/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9056
80626a55
DE
9057static int
9058eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9059{
80626a55
DE
9060 const struct dwo_unit *dua = a;
9061 const struct dwo_unit *dub = b;
3019eac3 9062
80626a55
DE
9063 return dua->signature == dub->signature;
9064}
3019eac3 9065
80626a55 9066/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9067
80626a55
DE
9068static htab_t
9069allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9070{
9071 return htab_create_alloc_ex (3,
9072 hash_dwp_loaded_cutus,
9073 eq_dwp_loaded_cutus,
9074 NULL,
9075 &objfile->objfile_obstack,
9076 hashtab_obstack_allocate,
9077 dummy_obstack_deallocate);
9078}
3019eac3 9079
80626a55
DE
9080/* Initialize the use of the DWP file for the current objfile.
9081 By convention the name of the DWP file is ${objfile}.dwp.
9082 The result is NULL if it can't be found. */
a766d390 9083
80626a55
DE
9084static struct dwp_file *
9085open_and_init_dwp_file (const char *comp_dir)
9086{
9087 struct objfile *objfile = dwarf2_per_objfile->objfile;
9088 struct dwp_file *dwp_file;
9089 char *dwp_name;
9090 bfd *dbfd;
9091 struct cleanup *cleanups;
9092
2792b94d 9093 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9094 cleanups = make_cleanup (xfree, dwp_name);
9095
9096 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9097 if (dbfd == NULL)
9098 {
9099 if (dwarf2_read_debug)
9100 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9101 do_cleanups (cleanups);
9102 return NULL;
3019eac3 9103 }
80626a55
DE
9104 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9105 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9106 dwp_name, strlen (dwp_name));
9107 dwp_file->dbfd = dbfd;
9108 do_cleanups (cleanups);
c906108c 9109
80626a55 9110 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9111
80626a55
DE
9112 /* +1: section 0 is unused */
9113 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9114 dwp_file->elf_sections =
9115 OBSTACK_CALLOC (&objfile->objfile_obstack,
9116 dwp_file->num_sections, asection *);
9117
9118 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9119
9120 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9121
9122 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9123
9124 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9125
9126 discard_cleanups (cleanups);
9127
9128 if (dwarf2_read_debug)
9129 {
9130 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9131 fprintf_unfiltered (gdb_stdlog,
9132 " %u CUs, %u TUs\n",
9133 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9134 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9135 }
9136
9137 return dwp_file;
3019eac3 9138}
c906108c 9139
80626a55
DE
9140/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9141 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9142 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9143 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9144 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9145
9146 This is called, for example, when wanting to read a variable with a
9147 complex location. Therefore we don't want to do file i/o for every call.
9148 Therefore we don't want to look for a DWO file on every call.
9149 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9150 then we check if we've already seen DWO_NAME, and only THEN do we check
9151 for a DWO file.
9152
1c658ad5 9153 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9154 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9155
3019eac3 9156static struct dwo_unit *
80626a55
DE
9157lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9158 const char *dwo_name, const char *comp_dir,
9159 ULONGEST signature, int is_debug_types)
3019eac3
DE
9160{
9161 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9162 const char *kind = is_debug_types ? "TU" : "CU";
9163 void **dwo_file_slot;
3019eac3 9164 struct dwo_file *dwo_file;
80626a55 9165 struct dwp_file *dwp_file;
cb1df416 9166
80626a55 9167 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9168
80626a55
DE
9169 if (! dwarf2_per_objfile->dwp_checked)
9170 {
9171 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9172 dwarf2_per_objfile->dwp_checked = 1;
9173 }
9174 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9175
80626a55 9176 if (dwp_file != NULL)
cf2c3c16 9177 {
80626a55
DE
9178 const struct dwp_hash_table *dwp_htab =
9179 is_debug_types ? dwp_file->tus : dwp_file->cus;
9180
9181 if (dwp_htab != NULL)
9182 {
9183 struct dwo_unit *dwo_cutu =
9184 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9185
9186 if (dwo_cutu != NULL)
9187 {
9188 if (dwarf2_read_debug)
9189 {
9190 fprintf_unfiltered (gdb_stdlog,
9191 "Virtual DWO %s %s found: @%s\n",
9192 kind, hex_string (signature),
9193 host_address_to_string (dwo_cutu));
9194 }
9195 return dwo_cutu;
9196 }
9197 }
9198 }
9199
9200 /* Have we already seen DWO_NAME? */
9201
9202 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9203 if (*dwo_file_slot == NULL)
9204 {
9205 /* Read in the file and build a table of the DWOs it contains. */
9206 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9207 }
9208 /* NOTE: This will be NULL if unable to open the file. */
9209 dwo_file = *dwo_file_slot;
9210
9211 if (dwo_file != NULL)
9212 {
9213 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9214
9215 if (htab != NULL)
9216 {
9217 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9218
80626a55
DE
9219 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9220 find_dwo_cutu.signature = signature;
9221 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9222
80626a55
DE
9223 if (dwo_cutu != NULL)
9224 {
9225 if (dwarf2_read_debug)
9226 {
9227 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9228 kind, dwo_name, hex_string (signature),
9229 host_address_to_string (dwo_cutu));
9230 }
9231 return dwo_cutu;
9232 }
9233 }
2e276125 9234 }
9cdd5dbd 9235
80626a55
DE
9236 /* We didn't find it. This could mean a dwo_id mismatch, or
9237 someone deleted the DWO/DWP file, or the search path isn't set up
9238 correctly to find the file. */
9239
9240 if (dwarf2_read_debug)
9241 {
9242 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9243 kind, dwo_name, hex_string (signature));
9244 }
3019eac3
DE
9245
9246 complaint (&symfile_complaints,
80626a55 9247 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9248 " [in module %s]"),
80626a55 9249 this_unit->offset.sect_off, objfile->name);
3019eac3 9250 return NULL;
5fb290d7
DJ
9251}
9252
80626a55
DE
9253/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9254 See lookup_dwo_cutu_unit for details. */
9255
9256static struct dwo_unit *
9257lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9258 const char *dwo_name, const char *comp_dir,
9259 ULONGEST signature)
9260{
9261 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9262}
9263
9264/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9265 See lookup_dwo_cutu_unit for details. */
9266
9267static struct dwo_unit *
9268lookup_dwo_type_unit (struct signatured_type *this_tu,
9269 const char *dwo_name, const char *comp_dir)
9270{
9271 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9272}
9273
3019eac3
DE
9274/* Free all resources associated with DWO_FILE.
9275 Close the DWO file and munmap the sections.
9276 All memory should be on the objfile obstack. */
348e048f
DE
9277
9278static void
3019eac3 9279free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9280{
3019eac3
DE
9281 int ix;
9282 struct dwarf2_section_info *section;
348e048f 9283
80626a55 9284 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9285
3019eac3
DE
9286 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9287}
348e048f 9288
3019eac3 9289/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9290
3019eac3
DE
9291static void
9292free_dwo_file_cleanup (void *arg)
9293{
9294 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9295 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9296
3019eac3
DE
9297 free_dwo_file (dwo_file, objfile);
9298}
348e048f 9299
3019eac3 9300/* Traversal function for free_dwo_files. */
2ab95328 9301
3019eac3
DE
9302static int
9303free_dwo_file_from_slot (void **slot, void *info)
9304{
9305 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9306 struct objfile *objfile = (struct objfile *) info;
348e048f 9307
3019eac3 9308 free_dwo_file (dwo_file, objfile);
348e048f 9309
3019eac3
DE
9310 return 1;
9311}
348e048f 9312
3019eac3 9313/* Free all resources associated with DWO_FILES. */
348e048f 9314
3019eac3
DE
9315static void
9316free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9317{
9318 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9319}
3019eac3
DE
9320\f
9321/* Read in various DIEs. */
348e048f 9322
d389af10
JK
9323/* qsort helper for inherit_abstract_dies. */
9324
9325static int
9326unsigned_int_compar (const void *ap, const void *bp)
9327{
9328 unsigned int a = *(unsigned int *) ap;
9329 unsigned int b = *(unsigned int *) bp;
9330
9331 return (a > b) - (b > a);
9332}
9333
9334/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9335 Inherit only the children of the DW_AT_abstract_origin DIE not being
9336 already referenced by DW_AT_abstract_origin from the children of the
9337 current DIE. */
d389af10
JK
9338
9339static void
9340inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9341{
9342 struct die_info *child_die;
9343 unsigned die_children_count;
9344 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9345 sect_offset *offsets;
9346 sect_offset *offsets_end, *offsetp;
d389af10
JK
9347 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9348 struct die_info *origin_die;
9349 /* Iterator of the ORIGIN_DIE children. */
9350 struct die_info *origin_child_die;
9351 struct cleanup *cleanups;
9352 struct attribute *attr;
cd02d79d
PA
9353 struct dwarf2_cu *origin_cu;
9354 struct pending **origin_previous_list_in_scope;
d389af10
JK
9355
9356 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9357 if (!attr)
9358 return;
9359
cd02d79d
PA
9360 /* Note that following die references may follow to a die in a
9361 different cu. */
9362
9363 origin_cu = cu;
9364 origin_die = follow_die_ref (die, attr, &origin_cu);
9365
9366 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9367 symbols in. */
9368 origin_previous_list_in_scope = origin_cu->list_in_scope;
9369 origin_cu->list_in_scope = cu->list_in_scope;
9370
edb3359d
DJ
9371 if (die->tag != origin_die->tag
9372 && !(die->tag == DW_TAG_inlined_subroutine
9373 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9374 complaint (&symfile_complaints,
9375 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9376 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9377
9378 child_die = die->child;
9379 die_children_count = 0;
9380 while (child_die && child_die->tag)
9381 {
9382 child_die = sibling_die (child_die);
9383 die_children_count++;
9384 }
9385 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9386 cleanups = make_cleanup (xfree, offsets);
9387
9388 offsets_end = offsets;
9389 child_die = die->child;
9390 while (child_die && child_die->tag)
9391 {
c38f313d
DJ
9392 /* For each CHILD_DIE, find the corresponding child of
9393 ORIGIN_DIE. If there is more than one layer of
9394 DW_AT_abstract_origin, follow them all; there shouldn't be,
9395 but GCC versions at least through 4.4 generate this (GCC PR
9396 40573). */
9397 struct die_info *child_origin_die = child_die;
cd02d79d 9398 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9399
c38f313d
DJ
9400 while (1)
9401 {
cd02d79d
PA
9402 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9403 child_origin_cu);
c38f313d
DJ
9404 if (attr == NULL)
9405 break;
cd02d79d
PA
9406 child_origin_die = follow_die_ref (child_origin_die, attr,
9407 &child_origin_cu);
c38f313d
DJ
9408 }
9409
d389af10
JK
9410 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9411 counterpart may exist. */
c38f313d 9412 if (child_origin_die != child_die)
d389af10 9413 {
edb3359d
DJ
9414 if (child_die->tag != child_origin_die->tag
9415 && !(child_die->tag == DW_TAG_inlined_subroutine
9416 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9417 complaint (&symfile_complaints,
9418 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9419 "different tags"), child_die->offset.sect_off,
9420 child_origin_die->offset.sect_off);
c38f313d
DJ
9421 if (child_origin_die->parent != origin_die)
9422 complaint (&symfile_complaints,
9423 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9424 "different parents"), child_die->offset.sect_off,
9425 child_origin_die->offset.sect_off);
c38f313d
DJ
9426 else
9427 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9428 }
9429 child_die = sibling_die (child_die);
9430 }
9431 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9432 unsigned_int_compar);
9433 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9434 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9435 complaint (&symfile_complaints,
9436 _("Multiple children of DIE 0x%x refer "
9437 "to DIE 0x%x as their abstract origin"),
b64f50a1 9438 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9439
9440 offsetp = offsets;
9441 origin_child_die = origin_die->child;
9442 while (origin_child_die && origin_child_die->tag)
9443 {
9444 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9445 while (offsetp < offsets_end
9446 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9447 offsetp++;
b64f50a1
JK
9448 if (offsetp >= offsets_end
9449 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9450 {
9451 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9452 process_die (origin_child_die, origin_cu);
d389af10
JK
9453 }
9454 origin_child_die = sibling_die (origin_child_die);
9455 }
cd02d79d 9456 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9457
9458 do_cleanups (cleanups);
9459}
9460
c906108c 9461static void
e7c27a73 9462read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9463{
e7c27a73 9464 struct objfile *objfile = cu->objfile;
52f0bd74 9465 struct context_stack *new;
c906108c
SS
9466 CORE_ADDR lowpc;
9467 CORE_ADDR highpc;
9468 struct die_info *child_die;
edb3359d 9469 struct attribute *attr, *call_line, *call_file;
15d034d0 9470 const char *name;
e142c38c 9471 CORE_ADDR baseaddr;
801e3a5b 9472 struct block *block;
edb3359d 9473 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9474 VEC (symbolp) *template_args = NULL;
9475 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9476
9477 if (inlined_func)
9478 {
9479 /* If we do not have call site information, we can't show the
9480 caller of this inlined function. That's too confusing, so
9481 only use the scope for local variables. */
9482 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9483 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9484 if (call_line == NULL || call_file == NULL)
9485 {
9486 read_lexical_block_scope (die, cu);
9487 return;
9488 }
9489 }
c906108c 9490
e142c38c
DJ
9491 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9492
94af9270 9493 name = dwarf2_name (die, cu);
c906108c 9494
e8d05480
JB
9495 /* Ignore functions with missing or empty names. These are actually
9496 illegal according to the DWARF standard. */
9497 if (name == NULL)
9498 {
9499 complaint (&symfile_complaints,
b64f50a1
JK
9500 _("missing name for subprogram DIE at %d"),
9501 die->offset.sect_off);
e8d05480
JB
9502 return;
9503 }
9504
9505 /* Ignore functions with missing or invalid low and high pc attributes. */
9506 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9507 {
ae4d0c03
PM
9508 attr = dwarf2_attr (die, DW_AT_external, cu);
9509 if (!attr || !DW_UNSND (attr))
9510 complaint (&symfile_complaints,
3e43a32a
MS
9511 _("cannot get low and high bounds "
9512 "for subprogram DIE at %d"),
b64f50a1 9513 die->offset.sect_off);
e8d05480
JB
9514 return;
9515 }
c906108c
SS
9516
9517 lowpc += baseaddr;
9518 highpc += baseaddr;
9519
34eaf542
TT
9520 /* If we have any template arguments, then we must allocate a
9521 different sort of symbol. */
9522 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9523 {
9524 if (child_die->tag == DW_TAG_template_type_param
9525 || child_die->tag == DW_TAG_template_value_param)
9526 {
9527 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9528 struct template_symbol);
9529 templ_func->base.is_cplus_template_function = 1;
9530 break;
9531 }
9532 }
9533
c906108c 9534 new = push_context (0, lowpc);
34eaf542
TT
9535 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9536 (struct symbol *) templ_func);
4c2df51b 9537
4cecd739
DJ
9538 /* If there is a location expression for DW_AT_frame_base, record
9539 it. */
e142c38c 9540 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9541 if (attr)
f1e6e072 9542 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9543
e142c38c 9544 cu->list_in_scope = &local_symbols;
c906108c 9545
639d11d3 9546 if (die->child != NULL)
c906108c 9547 {
639d11d3 9548 child_die = die->child;
c906108c
SS
9549 while (child_die && child_die->tag)
9550 {
34eaf542
TT
9551 if (child_die->tag == DW_TAG_template_type_param
9552 || child_die->tag == DW_TAG_template_value_param)
9553 {
9554 struct symbol *arg = new_symbol (child_die, NULL, cu);
9555
f1078f66
DJ
9556 if (arg != NULL)
9557 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9558 }
9559 else
9560 process_die (child_die, cu);
c906108c
SS
9561 child_die = sibling_die (child_die);
9562 }
9563 }
9564
d389af10
JK
9565 inherit_abstract_dies (die, cu);
9566
4a811a97
UW
9567 /* If we have a DW_AT_specification, we might need to import using
9568 directives from the context of the specification DIE. See the
9569 comment in determine_prefix. */
9570 if (cu->language == language_cplus
9571 && dwarf2_attr (die, DW_AT_specification, cu))
9572 {
9573 struct dwarf2_cu *spec_cu = cu;
9574 struct die_info *spec_die = die_specification (die, &spec_cu);
9575
9576 while (spec_die)
9577 {
9578 child_die = spec_die->child;
9579 while (child_die && child_die->tag)
9580 {
9581 if (child_die->tag == DW_TAG_imported_module)
9582 process_die (child_die, spec_cu);
9583 child_die = sibling_die (child_die);
9584 }
9585
9586 /* In some cases, GCC generates specification DIEs that
9587 themselves contain DW_AT_specification attributes. */
9588 spec_die = die_specification (spec_die, &spec_cu);
9589 }
9590 }
9591
c906108c
SS
9592 new = pop_context ();
9593 /* Make a block for the local symbols within. */
801e3a5b
JB
9594 block = finish_block (new->name, &local_symbols, new->old_blocks,
9595 lowpc, highpc, objfile);
9596
df8a16a1 9597 /* For C++, set the block's scope. */
195a3f6c 9598 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9599 && cu->processing_has_namespace_info)
195a3f6c
TT
9600 block_set_scope (block, determine_prefix (die, cu),
9601 &objfile->objfile_obstack);
df8a16a1 9602
801e3a5b
JB
9603 /* If we have address ranges, record them. */
9604 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9605
34eaf542
TT
9606 /* Attach template arguments to function. */
9607 if (! VEC_empty (symbolp, template_args))
9608 {
9609 gdb_assert (templ_func != NULL);
9610
9611 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9612 templ_func->template_arguments
9613 = obstack_alloc (&objfile->objfile_obstack,
9614 (templ_func->n_template_arguments
9615 * sizeof (struct symbol *)));
9616 memcpy (templ_func->template_arguments,
9617 VEC_address (symbolp, template_args),
9618 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9619 VEC_free (symbolp, template_args);
9620 }
9621
208d8187
JB
9622 /* In C++, we can have functions nested inside functions (e.g., when
9623 a function declares a class that has methods). This means that
9624 when we finish processing a function scope, we may need to go
9625 back to building a containing block's symbol lists. */
9626 local_symbols = new->locals;
27aa8d6a 9627 using_directives = new->using_directives;
208d8187 9628
921e78cf
JB
9629 /* If we've finished processing a top-level function, subsequent
9630 symbols go in the file symbol list. */
9631 if (outermost_context_p ())
e142c38c 9632 cu->list_in_scope = &file_symbols;
c906108c
SS
9633}
9634
9635/* Process all the DIES contained within a lexical block scope. Start
9636 a new scope, process the dies, and then close the scope. */
9637
9638static void
e7c27a73 9639read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9640{
e7c27a73 9641 struct objfile *objfile = cu->objfile;
52f0bd74 9642 struct context_stack *new;
c906108c
SS
9643 CORE_ADDR lowpc, highpc;
9644 struct die_info *child_die;
e142c38c
DJ
9645 CORE_ADDR baseaddr;
9646
9647 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9648
9649 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9650 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9651 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9652 be nasty. Might be easier to properly extend generic blocks to
af34e669 9653 describe ranges. */
d85a05f0 9654 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9655 return;
9656 lowpc += baseaddr;
9657 highpc += baseaddr;
9658
9659 push_context (0, lowpc);
639d11d3 9660 if (die->child != NULL)
c906108c 9661 {
639d11d3 9662 child_die = die->child;
c906108c
SS
9663 while (child_die && child_die->tag)
9664 {
e7c27a73 9665 process_die (child_die, cu);
c906108c
SS
9666 child_die = sibling_die (child_die);
9667 }
9668 }
9669 new = pop_context ();
9670
8540c487 9671 if (local_symbols != NULL || using_directives != NULL)
c906108c 9672 {
801e3a5b
JB
9673 struct block *block
9674 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9675 highpc, objfile);
9676
9677 /* Note that recording ranges after traversing children, as we
9678 do here, means that recording a parent's ranges entails
9679 walking across all its children's ranges as they appear in
9680 the address map, which is quadratic behavior.
9681
9682 It would be nicer to record the parent's ranges before
9683 traversing its children, simply overriding whatever you find
9684 there. But since we don't even decide whether to create a
9685 block until after we've traversed its children, that's hard
9686 to do. */
9687 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9688 }
9689 local_symbols = new->locals;
27aa8d6a 9690 using_directives = new->using_directives;
c906108c
SS
9691}
9692
96408a79
SA
9693/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9694
9695static void
9696read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9697{
9698 struct objfile *objfile = cu->objfile;
9699 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9700 CORE_ADDR pc, baseaddr;
9701 struct attribute *attr;
9702 struct call_site *call_site, call_site_local;
9703 void **slot;
9704 int nparams;
9705 struct die_info *child_die;
9706
9707 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9708
9709 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9710 if (!attr)
9711 {
9712 complaint (&symfile_complaints,
9713 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9714 "DIE 0x%x [in module %s]"),
b64f50a1 9715 die->offset.sect_off, objfile->name);
96408a79
SA
9716 return;
9717 }
9718 pc = DW_ADDR (attr) + baseaddr;
9719
9720 if (cu->call_site_htab == NULL)
9721 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9722 NULL, &objfile->objfile_obstack,
9723 hashtab_obstack_allocate, NULL);
9724 call_site_local.pc = pc;
9725 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9726 if (*slot != NULL)
9727 {
9728 complaint (&symfile_complaints,
9729 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9730 "DIE 0x%x [in module %s]"),
b64f50a1 9731 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9732 return;
9733 }
9734
9735 /* Count parameters at the caller. */
9736
9737 nparams = 0;
9738 for (child_die = die->child; child_die && child_die->tag;
9739 child_die = sibling_die (child_die))
9740 {
9741 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9742 {
9743 complaint (&symfile_complaints,
9744 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9745 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9746 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9747 continue;
9748 }
9749
9750 nparams++;
9751 }
9752
9753 call_site = obstack_alloc (&objfile->objfile_obstack,
9754 (sizeof (*call_site)
9755 + (sizeof (*call_site->parameter)
9756 * (nparams - 1))));
9757 *slot = call_site;
9758 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9759 call_site->pc = pc;
9760
9761 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9762 {
9763 struct die_info *func_die;
9764
9765 /* Skip also over DW_TAG_inlined_subroutine. */
9766 for (func_die = die->parent;
9767 func_die && func_die->tag != DW_TAG_subprogram
9768 && func_die->tag != DW_TAG_subroutine_type;
9769 func_die = func_die->parent);
9770
9771 /* DW_AT_GNU_all_call_sites is a superset
9772 of DW_AT_GNU_all_tail_call_sites. */
9773 if (func_die
9774 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9775 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9776 {
9777 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9778 not complete. But keep CALL_SITE for look ups via call_site_htab,
9779 both the initial caller containing the real return address PC and
9780 the final callee containing the current PC of a chain of tail
9781 calls do not need to have the tail call list complete. But any
9782 function candidate for a virtual tail call frame searched via
9783 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9784 determined unambiguously. */
9785 }
9786 else
9787 {
9788 struct type *func_type = NULL;
9789
9790 if (func_die)
9791 func_type = get_die_type (func_die, cu);
9792 if (func_type != NULL)
9793 {
9794 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9795
9796 /* Enlist this call site to the function. */
9797 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9798 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9799 }
9800 else
9801 complaint (&symfile_complaints,
9802 _("Cannot find function owning DW_TAG_GNU_call_site "
9803 "DIE 0x%x [in module %s]"),
b64f50a1 9804 die->offset.sect_off, objfile->name);
96408a79
SA
9805 }
9806 }
9807
9808 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9809 if (attr == NULL)
9810 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9811 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9812 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9813 /* Keep NULL DWARF_BLOCK. */;
9814 else if (attr_form_is_block (attr))
9815 {
9816 struct dwarf2_locexpr_baton *dlbaton;
9817
9818 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9819 dlbaton->data = DW_BLOCK (attr)->data;
9820 dlbaton->size = DW_BLOCK (attr)->size;
9821 dlbaton->per_cu = cu->per_cu;
9822
9823 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9824 }
9825 else if (is_ref_attr (attr))
9826 {
96408a79
SA
9827 struct dwarf2_cu *target_cu = cu;
9828 struct die_info *target_die;
9829
9830 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9831 gdb_assert (target_cu->objfile == objfile);
9832 if (die_is_declaration (target_die, target_cu))
9833 {
9112db09
JK
9834 const char *target_physname = NULL;
9835 struct attribute *target_attr;
9836
9837 /* Prefer the mangled name; otherwise compute the demangled one. */
9838 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9839 if (target_attr == NULL)
9840 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9841 target_cu);
9842 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9843 target_physname = DW_STRING (target_attr);
9844 else
9845 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9846 if (target_physname == NULL)
9847 complaint (&symfile_complaints,
9848 _("DW_AT_GNU_call_site_target target DIE has invalid "
9849 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9850 die->offset.sect_off, objfile->name);
96408a79 9851 else
7d455152 9852 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9853 }
9854 else
9855 {
9856 CORE_ADDR lowpc;
9857
9858 /* DW_AT_entry_pc should be preferred. */
9859 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9860 complaint (&symfile_complaints,
9861 _("DW_AT_GNU_call_site_target target DIE has invalid "
9862 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9863 die->offset.sect_off, objfile->name);
96408a79
SA
9864 else
9865 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9866 }
9867 }
9868 else
9869 complaint (&symfile_complaints,
9870 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9871 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9872 die->offset.sect_off, objfile->name);
96408a79
SA
9873
9874 call_site->per_cu = cu->per_cu;
9875
9876 for (child_die = die->child;
9877 child_die && child_die->tag;
9878 child_die = sibling_die (child_die))
9879 {
96408a79 9880 struct call_site_parameter *parameter;
1788b2d3 9881 struct attribute *loc, *origin;
96408a79
SA
9882
9883 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9884 {
9885 /* Already printed the complaint above. */
9886 continue;
9887 }
9888
9889 gdb_assert (call_site->parameter_count < nparams);
9890 parameter = &call_site->parameter[call_site->parameter_count];
9891
1788b2d3
JK
9892 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9893 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9894 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9895
24c5c679 9896 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9897 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9898 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9899 {
9900 sect_offset offset;
9901
9902 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9903 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9904 if (!offset_in_cu_p (&cu->header, offset))
9905 {
9906 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9907 binding can be done only inside one CU. Such referenced DIE
9908 therefore cannot be even moved to DW_TAG_partial_unit. */
9909 complaint (&symfile_complaints,
9910 _("DW_AT_abstract_origin offset is not in CU for "
9911 "DW_TAG_GNU_call_site child DIE 0x%x "
9912 "[in module %s]"),
9913 child_die->offset.sect_off, objfile->name);
9914 continue;
9915 }
1788b2d3
JK
9916 parameter->u.param_offset.cu_off = (offset.sect_off
9917 - cu->header.offset.sect_off);
9918 }
9919 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9920 {
9921 complaint (&symfile_complaints,
9922 _("No DW_FORM_block* DW_AT_location for "
9923 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9924 child_die->offset.sect_off, objfile->name);
96408a79
SA
9925 continue;
9926 }
24c5c679 9927 else
96408a79 9928 {
24c5c679
JK
9929 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9930 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9931 if (parameter->u.dwarf_reg != -1)
9932 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9933 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9934 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9935 &parameter->u.fb_offset))
9936 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9937 else
9938 {
9939 complaint (&symfile_complaints,
9940 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9941 "for DW_FORM_block* DW_AT_location is supported for "
9942 "DW_TAG_GNU_call_site child DIE 0x%x "
9943 "[in module %s]"),
9944 child_die->offset.sect_off, objfile->name);
9945 continue;
9946 }
96408a79
SA
9947 }
9948
9949 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9950 if (!attr_form_is_block (attr))
9951 {
9952 complaint (&symfile_complaints,
9953 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9954 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9955 child_die->offset.sect_off, objfile->name);
96408a79
SA
9956 continue;
9957 }
9958 parameter->value = DW_BLOCK (attr)->data;
9959 parameter->value_size = DW_BLOCK (attr)->size;
9960
9961 /* Parameters are not pre-cleared by memset above. */
9962 parameter->data_value = NULL;
9963 parameter->data_value_size = 0;
9964 call_site->parameter_count++;
9965
9966 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9967 if (attr)
9968 {
9969 if (!attr_form_is_block (attr))
9970 complaint (&symfile_complaints,
9971 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9972 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9973 child_die->offset.sect_off, objfile->name);
96408a79
SA
9974 else
9975 {
9976 parameter->data_value = DW_BLOCK (attr)->data;
9977 parameter->data_value_size = DW_BLOCK (attr)->size;
9978 }
9979 }
9980 }
9981}
9982
43039443 9983/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9984 Return 1 if the attributes are present and valid, otherwise, return 0.
9985 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9986
9987static int
9988dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9989 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9990 struct partial_symtab *ranges_pst)
43039443
JK
9991{
9992 struct objfile *objfile = cu->objfile;
9993 struct comp_unit_head *cu_header = &cu->header;
9994 bfd *obfd = objfile->obfd;
9995 unsigned int addr_size = cu_header->addr_size;
9996 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9997 /* Base address selection entry. */
9998 CORE_ADDR base;
9999 int found_base;
10000 unsigned int dummy;
10001 gdb_byte *buffer;
10002 CORE_ADDR marker;
10003 int low_set;
10004 CORE_ADDR low = 0;
10005 CORE_ADDR high = 0;
ff013f42 10006 CORE_ADDR baseaddr;
43039443 10007
d00adf39
DE
10008 found_base = cu->base_known;
10009 base = cu->base_address;
43039443 10010
be391dca 10011 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10012 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10013 {
10014 complaint (&symfile_complaints,
10015 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10016 offset);
10017 return 0;
10018 }
dce234bc 10019 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10020
10021 /* Read in the largest possible address. */
10022 marker = read_address (obfd, buffer, cu, &dummy);
10023 if ((marker & mask) == mask)
10024 {
10025 /* If we found the largest possible address, then
10026 read the base address. */
10027 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10028 buffer += 2 * addr_size;
10029 offset += 2 * addr_size;
10030 found_base = 1;
10031 }
10032
10033 low_set = 0;
10034
e7030f15 10035 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10036
43039443
JK
10037 while (1)
10038 {
10039 CORE_ADDR range_beginning, range_end;
10040
10041 range_beginning = read_address (obfd, buffer, cu, &dummy);
10042 buffer += addr_size;
10043 range_end = read_address (obfd, buffer, cu, &dummy);
10044 buffer += addr_size;
10045 offset += 2 * addr_size;
10046
10047 /* An end of list marker is a pair of zero addresses. */
10048 if (range_beginning == 0 && range_end == 0)
10049 /* Found the end of list entry. */
10050 break;
10051
10052 /* Each base address selection entry is a pair of 2 values.
10053 The first is the largest possible address, the second is
10054 the base address. Check for a base address here. */
10055 if ((range_beginning & mask) == mask)
10056 {
10057 /* If we found the largest possible address, then
10058 read the base address. */
10059 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10060 found_base = 1;
10061 continue;
10062 }
10063
10064 if (!found_base)
10065 {
10066 /* We have no valid base address for the ranges
10067 data. */
10068 complaint (&symfile_complaints,
10069 _("Invalid .debug_ranges data (no base address)"));
10070 return 0;
10071 }
10072
9277c30c
UW
10073 if (range_beginning > range_end)
10074 {
10075 /* Inverted range entries are invalid. */
10076 complaint (&symfile_complaints,
10077 _("Invalid .debug_ranges data (inverted range)"));
10078 return 0;
10079 }
10080
10081 /* Empty range entries have no effect. */
10082 if (range_beginning == range_end)
10083 continue;
10084
43039443
JK
10085 range_beginning += base;
10086 range_end += base;
10087
01093045
DE
10088 /* A not-uncommon case of bad debug info.
10089 Don't pollute the addrmap with bad data. */
10090 if (range_beginning + baseaddr == 0
10091 && !dwarf2_per_objfile->has_section_at_zero)
10092 {
10093 complaint (&symfile_complaints,
10094 _(".debug_ranges entry has start address of zero"
10095 " [in module %s]"), objfile->name);
10096 continue;
10097 }
10098
9277c30c 10099 if (ranges_pst != NULL)
ff013f42 10100 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10101 range_beginning + baseaddr,
10102 range_end - 1 + baseaddr,
ff013f42
JK
10103 ranges_pst);
10104
43039443
JK
10105 /* FIXME: This is recording everything as a low-high
10106 segment of consecutive addresses. We should have a
10107 data structure for discontiguous block ranges
10108 instead. */
10109 if (! low_set)
10110 {
10111 low = range_beginning;
10112 high = range_end;
10113 low_set = 1;
10114 }
10115 else
10116 {
10117 if (range_beginning < low)
10118 low = range_beginning;
10119 if (range_end > high)
10120 high = range_end;
10121 }
10122 }
10123
10124 if (! low_set)
10125 /* If the first entry is an end-of-list marker, the range
10126 describes an empty scope, i.e. no instructions. */
10127 return 0;
10128
10129 if (low_return)
10130 *low_return = low;
10131 if (high_return)
10132 *high_return = high;
10133 return 1;
10134}
10135
af34e669
DJ
10136/* Get low and high pc attributes from a die. Return 1 if the attributes
10137 are present and valid, otherwise, return 0. Return -1 if the range is
10138 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10139
c906108c 10140static int
af34e669 10141dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10142 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10143 struct partial_symtab *pst)
c906108c
SS
10144{
10145 struct attribute *attr;
91da1414 10146 struct attribute *attr_high;
af34e669
DJ
10147 CORE_ADDR low = 0;
10148 CORE_ADDR high = 0;
10149 int ret = 0;
c906108c 10150
91da1414
MW
10151 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10152 if (attr_high)
af34e669 10153 {
e142c38c 10154 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10155 if (attr)
91da1414
MW
10156 {
10157 low = DW_ADDR (attr);
3019eac3
DE
10158 if (attr_high->form == DW_FORM_addr
10159 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10160 high = DW_ADDR (attr_high);
10161 else
10162 high = low + DW_UNSND (attr_high);
10163 }
af34e669
DJ
10164 else
10165 /* Found high w/o low attribute. */
10166 return 0;
10167
10168 /* Found consecutive range of addresses. */
10169 ret = 1;
10170 }
c906108c 10171 else
af34e669 10172 {
e142c38c 10173 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10174 if (attr != NULL)
10175 {
ab435259
DE
10176 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10177 We take advantage of the fact that DW_AT_ranges does not appear
10178 in DW_TAG_compile_unit of DWO files. */
10179 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10180 unsigned int ranges_offset = (DW_UNSND (attr)
10181 + (need_ranges_base
10182 ? cu->ranges_base
10183 : 0));
2e3cf129 10184
af34e669 10185 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10186 .debug_ranges section. */
2e3cf129 10187 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10188 return 0;
43039443 10189 /* Found discontinuous range of addresses. */
af34e669
DJ
10190 ret = -1;
10191 }
10192 }
c906108c 10193
9373cf26
JK
10194 /* read_partial_die has also the strict LOW < HIGH requirement. */
10195 if (high <= low)
c906108c
SS
10196 return 0;
10197
10198 /* When using the GNU linker, .gnu.linkonce. sections are used to
10199 eliminate duplicate copies of functions and vtables and such.
10200 The linker will arbitrarily choose one and discard the others.
10201 The AT_*_pc values for such functions refer to local labels in
10202 these sections. If the section from that file was discarded, the
10203 labels are not in the output, so the relocs get a value of 0.
10204 If this is a discarded function, mark the pc bounds as invalid,
10205 so that GDB will ignore it. */
72dca2f5 10206 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10207 return 0;
10208
10209 *lowpc = low;
96408a79
SA
10210 if (highpc)
10211 *highpc = high;
af34e669 10212 return ret;
c906108c
SS
10213}
10214
b084d499
JB
10215/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10216 its low and high PC addresses. Do nothing if these addresses could not
10217 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10218 and HIGHPC to the high address if greater than HIGHPC. */
10219
10220static void
10221dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10223 struct dwarf2_cu *cu)
10224{
10225 CORE_ADDR low, high;
10226 struct die_info *child = die->child;
10227
d85a05f0 10228 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10229 {
10230 *lowpc = min (*lowpc, low);
10231 *highpc = max (*highpc, high);
10232 }
10233
10234 /* If the language does not allow nested subprograms (either inside
10235 subprograms or lexical blocks), we're done. */
10236 if (cu->language != language_ada)
10237 return;
6e70227d 10238
b084d499
JB
10239 /* Check all the children of the given DIE. If it contains nested
10240 subprograms, then check their pc bounds. Likewise, we need to
10241 check lexical blocks as well, as they may also contain subprogram
10242 definitions. */
10243 while (child && child->tag)
10244 {
10245 if (child->tag == DW_TAG_subprogram
10246 || child->tag == DW_TAG_lexical_block)
10247 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10248 child = sibling_die (child);
10249 }
10250}
10251
fae299cd
DC
10252/* Get the low and high pc's represented by the scope DIE, and store
10253 them in *LOWPC and *HIGHPC. If the correct values can't be
10254 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10255
10256static void
10257get_scope_pc_bounds (struct die_info *die,
10258 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10259 struct dwarf2_cu *cu)
10260{
10261 CORE_ADDR best_low = (CORE_ADDR) -1;
10262 CORE_ADDR best_high = (CORE_ADDR) 0;
10263 CORE_ADDR current_low, current_high;
10264
d85a05f0 10265 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10266 {
10267 best_low = current_low;
10268 best_high = current_high;
10269 }
10270 else
10271 {
10272 struct die_info *child = die->child;
10273
10274 while (child && child->tag)
10275 {
10276 switch (child->tag) {
10277 case DW_TAG_subprogram:
b084d499 10278 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10279 break;
10280 case DW_TAG_namespace:
f55ee35c 10281 case DW_TAG_module:
fae299cd
DC
10282 /* FIXME: carlton/2004-01-16: Should we do this for
10283 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10284 that current GCC's always emit the DIEs corresponding
10285 to definitions of methods of classes as children of a
10286 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10287 the DIEs giving the declarations, which could be
10288 anywhere). But I don't see any reason why the
10289 standards says that they have to be there. */
10290 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10291
10292 if (current_low != ((CORE_ADDR) -1))
10293 {
10294 best_low = min (best_low, current_low);
10295 best_high = max (best_high, current_high);
10296 }
10297 break;
10298 default:
0963b4bd 10299 /* Ignore. */
fae299cd
DC
10300 break;
10301 }
10302
10303 child = sibling_die (child);
10304 }
10305 }
10306
10307 *lowpc = best_low;
10308 *highpc = best_high;
10309}
10310
801e3a5b
JB
10311/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10312 in DIE. */
380bca97 10313
801e3a5b
JB
10314static void
10315dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10316 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10317{
bb5ed363 10318 struct objfile *objfile = cu->objfile;
801e3a5b 10319 struct attribute *attr;
91da1414 10320 struct attribute *attr_high;
801e3a5b 10321
91da1414
MW
10322 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10323 if (attr_high)
801e3a5b 10324 {
801e3a5b
JB
10325 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10326 if (attr)
10327 {
10328 CORE_ADDR low = DW_ADDR (attr);
91da1414 10329 CORE_ADDR high;
3019eac3
DE
10330 if (attr_high->form == DW_FORM_addr
10331 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10332 high = DW_ADDR (attr_high);
10333 else
10334 high = low + DW_UNSND (attr_high);
9a619af0 10335
801e3a5b
JB
10336 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10337 }
10338 }
10339
10340 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10341 if (attr)
10342 {
bb5ed363 10343 bfd *obfd = objfile->obfd;
ab435259
DE
10344 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10345 We take advantage of the fact that DW_AT_ranges does not appear
10346 in DW_TAG_compile_unit of DWO files. */
10347 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10348
10349 /* The value of the DW_AT_ranges attribute is the offset of the
10350 address range list in the .debug_ranges section. */
ab435259
DE
10351 unsigned long offset = (DW_UNSND (attr)
10352 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10353 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10354
10355 /* For some target architectures, but not others, the
10356 read_address function sign-extends the addresses it returns.
10357 To recognize base address selection entries, we need a
10358 mask. */
10359 unsigned int addr_size = cu->header.addr_size;
10360 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10361
10362 /* The base address, to which the next pair is relative. Note
10363 that this 'base' is a DWARF concept: most entries in a range
10364 list are relative, to reduce the number of relocs against the
10365 debugging information. This is separate from this function's
10366 'baseaddr' argument, which GDB uses to relocate debugging
10367 information from a shared library based on the address at
10368 which the library was loaded. */
d00adf39
DE
10369 CORE_ADDR base = cu->base_address;
10370 int base_known = cu->base_known;
801e3a5b 10371
be391dca 10372 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10373 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10374 {
10375 complaint (&symfile_complaints,
10376 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10377 offset);
10378 return;
10379 }
10380
10381 for (;;)
10382 {
10383 unsigned int bytes_read;
10384 CORE_ADDR start, end;
10385
10386 start = read_address (obfd, buffer, cu, &bytes_read);
10387 buffer += bytes_read;
10388 end = read_address (obfd, buffer, cu, &bytes_read);
10389 buffer += bytes_read;
10390
10391 /* Did we find the end of the range list? */
10392 if (start == 0 && end == 0)
10393 break;
10394
10395 /* Did we find a base address selection entry? */
10396 else if ((start & base_select_mask) == base_select_mask)
10397 {
10398 base = end;
10399 base_known = 1;
10400 }
10401
10402 /* We found an ordinary address range. */
10403 else
10404 {
10405 if (!base_known)
10406 {
10407 complaint (&symfile_complaints,
3e43a32a
MS
10408 _("Invalid .debug_ranges data "
10409 "(no base address)"));
801e3a5b
JB
10410 return;
10411 }
10412
9277c30c
UW
10413 if (start > end)
10414 {
10415 /* Inverted range entries are invalid. */
10416 complaint (&symfile_complaints,
10417 _("Invalid .debug_ranges data "
10418 "(inverted range)"));
10419 return;
10420 }
10421
10422 /* Empty range entries have no effect. */
10423 if (start == end)
10424 continue;
10425
01093045
DE
10426 start += base + baseaddr;
10427 end += base + baseaddr;
10428
10429 /* A not-uncommon case of bad debug info.
10430 Don't pollute the addrmap with bad data. */
10431 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10432 {
10433 complaint (&symfile_complaints,
10434 _(".debug_ranges entry has start address of zero"
10435 " [in module %s]"), objfile->name);
10436 continue;
10437 }
10438
10439 record_block_range (block, start, end - 1);
801e3a5b
JB
10440 }
10441 }
10442 }
10443}
10444
685b1105
JK
10445/* Check whether the producer field indicates either of GCC < 4.6, or the
10446 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10447
685b1105
JK
10448static void
10449check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10450{
10451 const char *cs;
10452 int major, minor, release;
10453
10454 if (cu->producer == NULL)
10455 {
10456 /* For unknown compilers expect their behavior is DWARF version
10457 compliant.
10458
10459 GCC started to support .debug_types sections by -gdwarf-4 since
10460 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10461 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10462 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10463 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10464 }
685b1105 10465 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10466 {
685b1105
JK
10467 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10468
ba919b58
TT
10469 cs = &cu->producer[strlen ("GNU ")];
10470 while (*cs && !isdigit (*cs))
10471 cs++;
10472 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10473 {
10474 /* Not recognized as GCC. */
10475 }
10476 else
1b80a9fa
JK
10477 {
10478 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10479 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10480 }
685b1105
JK
10481 }
10482 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10483 cu->producer_is_icc = 1;
10484 else
10485 {
10486 /* For other non-GCC compilers, expect their behavior is DWARF version
10487 compliant. */
60d5a603
JK
10488 }
10489
ba919b58 10490 cu->checked_producer = 1;
685b1105 10491}
ba919b58 10492
685b1105
JK
10493/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10494 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10495 during 4.6.0 experimental. */
10496
10497static int
10498producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10499{
10500 if (!cu->checked_producer)
10501 check_producer (cu);
10502
10503 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10504}
10505
10506/* Return the default accessibility type if it is not overriden by
10507 DW_AT_accessibility. */
10508
10509static enum dwarf_access_attribute
10510dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10511{
10512 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10513 {
10514 /* The default DWARF 2 accessibility for members is public, the default
10515 accessibility for inheritance is private. */
10516
10517 if (die->tag != DW_TAG_inheritance)
10518 return DW_ACCESS_public;
10519 else
10520 return DW_ACCESS_private;
10521 }
10522 else
10523 {
10524 /* DWARF 3+ defines the default accessibility a different way. The same
10525 rules apply now for DW_TAG_inheritance as for the members and it only
10526 depends on the container kind. */
10527
10528 if (die->parent->tag == DW_TAG_class_type)
10529 return DW_ACCESS_private;
10530 else
10531 return DW_ACCESS_public;
10532 }
10533}
10534
74ac6d43
TT
10535/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10536 offset. If the attribute was not found return 0, otherwise return
10537 1. If it was found but could not properly be handled, set *OFFSET
10538 to 0. */
10539
10540static int
10541handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10542 LONGEST *offset)
10543{
10544 struct attribute *attr;
10545
10546 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10547 if (attr != NULL)
10548 {
10549 *offset = 0;
10550
10551 /* Note that we do not check for a section offset first here.
10552 This is because DW_AT_data_member_location is new in DWARF 4,
10553 so if we see it, we can assume that a constant form is really
10554 a constant and not a section offset. */
10555 if (attr_form_is_constant (attr))
10556 *offset = dwarf2_get_attr_constant_value (attr, 0);
10557 else if (attr_form_is_section_offset (attr))
10558 dwarf2_complex_location_expr_complaint ();
10559 else if (attr_form_is_block (attr))
10560 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10561 else
10562 dwarf2_complex_location_expr_complaint ();
10563
10564 return 1;
10565 }
10566
10567 return 0;
10568}
10569
c906108c
SS
10570/* Add an aggregate field to the field list. */
10571
10572static void
107d2387 10573dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10574 struct dwarf2_cu *cu)
6e70227d 10575{
e7c27a73 10576 struct objfile *objfile = cu->objfile;
5e2b427d 10577 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10578 struct nextfield *new_field;
10579 struct attribute *attr;
10580 struct field *fp;
15d034d0 10581 const char *fieldname = "";
c906108c
SS
10582
10583 /* Allocate a new field list entry and link it in. */
10584 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10585 make_cleanup (xfree, new_field);
c906108c 10586 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10587
10588 if (die->tag == DW_TAG_inheritance)
10589 {
10590 new_field->next = fip->baseclasses;
10591 fip->baseclasses = new_field;
10592 }
10593 else
10594 {
10595 new_field->next = fip->fields;
10596 fip->fields = new_field;
10597 }
c906108c
SS
10598 fip->nfields++;
10599
e142c38c 10600 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10601 if (attr)
10602 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10603 else
10604 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10605 if (new_field->accessibility != DW_ACCESS_public)
10606 fip->non_public_fields = 1;
60d5a603 10607
e142c38c 10608 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10609 if (attr)
10610 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10611 else
10612 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10613
10614 fp = &new_field->field;
a9a9bd0f 10615
e142c38c 10616 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10617 {
74ac6d43
TT
10618 LONGEST offset;
10619
a9a9bd0f 10620 /* Data member other than a C++ static data member. */
6e70227d 10621
c906108c 10622 /* Get type of field. */
e7c27a73 10623 fp->type = die_type (die, cu);
c906108c 10624
d6a843b5 10625 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10626
c906108c 10627 /* Get bit size of field (zero if none). */
e142c38c 10628 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10629 if (attr)
10630 {
10631 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10632 }
10633 else
10634 {
10635 FIELD_BITSIZE (*fp) = 0;
10636 }
10637
10638 /* Get bit offset of field. */
74ac6d43
TT
10639 if (handle_data_member_location (die, cu, &offset))
10640 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10641 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10642 if (attr)
10643 {
5e2b427d 10644 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10645 {
10646 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10647 additional bit offset from the MSB of the containing
10648 anonymous object to the MSB of the field. We don't
10649 have to do anything special since we don't need to
10650 know the size of the anonymous object. */
f41f5e61 10651 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10652 }
10653 else
10654 {
10655 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10656 MSB of the anonymous object, subtract off the number of
10657 bits from the MSB of the field to the MSB of the
10658 object, and then subtract off the number of bits of
10659 the field itself. The result is the bit offset of
10660 the LSB of the field. */
c906108c
SS
10661 int anonymous_size;
10662 int bit_offset = DW_UNSND (attr);
10663
e142c38c 10664 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10665 if (attr)
10666 {
10667 /* The size of the anonymous object containing
10668 the bit field is explicit, so use the
10669 indicated size (in bytes). */
10670 anonymous_size = DW_UNSND (attr);
10671 }
10672 else
10673 {
10674 /* The size of the anonymous object containing
10675 the bit field must be inferred from the type
10676 attribute of the data member containing the
10677 bit field. */
10678 anonymous_size = TYPE_LENGTH (fp->type);
10679 }
f41f5e61
PA
10680 SET_FIELD_BITPOS (*fp,
10681 (FIELD_BITPOS (*fp)
10682 + anonymous_size * bits_per_byte
10683 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10684 }
10685 }
10686
10687 /* Get name of field. */
39cbfefa
DJ
10688 fieldname = dwarf2_name (die, cu);
10689 if (fieldname == NULL)
10690 fieldname = "";
d8151005
DJ
10691
10692 /* The name is already allocated along with this objfile, so we don't
10693 need to duplicate it for the type. */
10694 fp->name = fieldname;
c906108c
SS
10695
10696 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10697 pointer or virtual base class pointer) to private. */
e142c38c 10698 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10699 {
d48cc9dd 10700 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10701 new_field->accessibility = DW_ACCESS_private;
10702 fip->non_public_fields = 1;
10703 }
10704 }
a9a9bd0f 10705 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10706 {
a9a9bd0f
DC
10707 /* C++ static member. */
10708
10709 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10710 is a declaration, but all versions of G++ as of this writing
10711 (so through at least 3.2.1) incorrectly generate
10712 DW_TAG_variable tags. */
6e70227d 10713
ff355380 10714 const char *physname;
c906108c 10715
a9a9bd0f 10716 /* Get name of field. */
39cbfefa
DJ
10717 fieldname = dwarf2_name (die, cu);
10718 if (fieldname == NULL)
c906108c
SS
10719 return;
10720
254e6b9e 10721 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10722 if (attr
10723 /* Only create a symbol if this is an external value.
10724 new_symbol checks this and puts the value in the global symbol
10725 table, which we want. If it is not external, new_symbol
10726 will try to put the value in cu->list_in_scope which is wrong. */
10727 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10728 {
10729 /* A static const member, not much different than an enum as far as
10730 we're concerned, except that we can support more types. */
10731 new_symbol (die, NULL, cu);
10732 }
10733
2df3850c 10734 /* Get physical name. */
ff355380 10735 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10736
d8151005
DJ
10737 /* The name is already allocated along with this objfile, so we don't
10738 need to duplicate it for the type. */
10739 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10740 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10741 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10742 }
10743 else if (die->tag == DW_TAG_inheritance)
10744 {
74ac6d43 10745 LONGEST offset;
d4b96c9a 10746
74ac6d43
TT
10747 /* C++ base class field. */
10748 if (handle_data_member_location (die, cu, &offset))
10749 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10750 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10751 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10752 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10753 fip->nbaseclasses++;
10754 }
10755}
10756
98751a41
JK
10757/* Add a typedef defined in the scope of the FIP's class. */
10758
10759static void
10760dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10761 struct dwarf2_cu *cu)
6e70227d 10762{
98751a41 10763 struct objfile *objfile = cu->objfile;
98751a41
JK
10764 struct typedef_field_list *new_field;
10765 struct attribute *attr;
10766 struct typedef_field *fp;
10767 char *fieldname = "";
10768
10769 /* Allocate a new field list entry and link it in. */
10770 new_field = xzalloc (sizeof (*new_field));
10771 make_cleanup (xfree, new_field);
10772
10773 gdb_assert (die->tag == DW_TAG_typedef);
10774
10775 fp = &new_field->field;
10776
10777 /* Get name of field. */
10778 fp->name = dwarf2_name (die, cu);
10779 if (fp->name == NULL)
10780 return;
10781
10782 fp->type = read_type_die (die, cu);
10783
10784 new_field->next = fip->typedef_field_list;
10785 fip->typedef_field_list = new_field;
10786 fip->typedef_field_list_count++;
10787}
10788
c906108c
SS
10789/* Create the vector of fields, and attach it to the type. */
10790
10791static void
fba45db2 10792dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10793 struct dwarf2_cu *cu)
c906108c
SS
10794{
10795 int nfields = fip->nfields;
10796
10797 /* Record the field count, allocate space for the array of fields,
10798 and create blank accessibility bitfields if necessary. */
10799 TYPE_NFIELDS (type) = nfields;
10800 TYPE_FIELDS (type) = (struct field *)
10801 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10802 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10803
b4ba55a1 10804 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10805 {
10806 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10807
10808 TYPE_FIELD_PRIVATE_BITS (type) =
10809 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10810 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10811
10812 TYPE_FIELD_PROTECTED_BITS (type) =
10813 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10814 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10815
774b6a14
TT
10816 TYPE_FIELD_IGNORE_BITS (type) =
10817 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10818 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10819 }
10820
10821 /* If the type has baseclasses, allocate and clear a bit vector for
10822 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10823 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10824 {
10825 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10826 unsigned char *pointer;
c906108c
SS
10827
10828 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10829 pointer = TYPE_ALLOC (type, num_bytes);
10830 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10831 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10832 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10833 }
10834
3e43a32a
MS
10835 /* Copy the saved-up fields into the field vector. Start from the head of
10836 the list, adding to the tail of the field array, so that they end up in
10837 the same order in the array in which they were added to the list. */
c906108c
SS
10838 while (nfields-- > 0)
10839 {
7d0ccb61
DJ
10840 struct nextfield *fieldp;
10841
10842 if (fip->fields)
10843 {
10844 fieldp = fip->fields;
10845 fip->fields = fieldp->next;
10846 }
10847 else
10848 {
10849 fieldp = fip->baseclasses;
10850 fip->baseclasses = fieldp->next;
10851 }
10852
10853 TYPE_FIELD (type, nfields) = fieldp->field;
10854 switch (fieldp->accessibility)
c906108c 10855 {
c5aa993b 10856 case DW_ACCESS_private:
b4ba55a1
JB
10857 if (cu->language != language_ada)
10858 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10859 break;
c906108c 10860
c5aa993b 10861 case DW_ACCESS_protected:
b4ba55a1
JB
10862 if (cu->language != language_ada)
10863 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10864 break;
c906108c 10865
c5aa993b
JM
10866 case DW_ACCESS_public:
10867 break;
c906108c 10868
c5aa993b
JM
10869 default:
10870 /* Unknown accessibility. Complain and treat it as public. */
10871 {
e2e0b3e5 10872 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10873 fieldp->accessibility);
c5aa993b
JM
10874 }
10875 break;
c906108c
SS
10876 }
10877 if (nfields < fip->nbaseclasses)
10878 {
7d0ccb61 10879 switch (fieldp->virtuality)
c906108c 10880 {
c5aa993b
JM
10881 case DW_VIRTUALITY_virtual:
10882 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10883 if (cu->language == language_ada)
a73c6dcd 10884 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10885 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10886 break;
c906108c
SS
10887 }
10888 }
c906108c
SS
10889 }
10890}
10891
7d27a96d
TT
10892/* Return true if this member function is a constructor, false
10893 otherwise. */
10894
10895static int
10896dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10897{
10898 const char *fieldname;
10899 const char *typename;
10900 int len;
10901
10902 if (die->parent == NULL)
10903 return 0;
10904
10905 if (die->parent->tag != DW_TAG_structure_type
10906 && die->parent->tag != DW_TAG_union_type
10907 && die->parent->tag != DW_TAG_class_type)
10908 return 0;
10909
10910 fieldname = dwarf2_name (die, cu);
10911 typename = dwarf2_name (die->parent, cu);
10912 if (fieldname == NULL || typename == NULL)
10913 return 0;
10914
10915 len = strlen (fieldname);
10916 return (strncmp (fieldname, typename, len) == 0
10917 && (typename[len] == '\0' || typename[len] == '<'));
10918}
10919
c906108c
SS
10920/* Add a member function to the proper fieldlist. */
10921
10922static void
107d2387 10923dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10924 struct type *type, struct dwarf2_cu *cu)
c906108c 10925{
e7c27a73 10926 struct objfile *objfile = cu->objfile;
c906108c
SS
10927 struct attribute *attr;
10928 struct fnfieldlist *flp;
10929 int i;
10930 struct fn_field *fnp;
15d034d0 10931 const char *fieldname;
c906108c 10932 struct nextfnfield *new_fnfield;
f792889a 10933 struct type *this_type;
60d5a603 10934 enum dwarf_access_attribute accessibility;
c906108c 10935
b4ba55a1 10936 if (cu->language == language_ada)
a73c6dcd 10937 error (_("unexpected member function in Ada type"));
b4ba55a1 10938
2df3850c 10939 /* Get name of member function. */
39cbfefa
DJ
10940 fieldname = dwarf2_name (die, cu);
10941 if (fieldname == NULL)
2df3850c 10942 return;
c906108c 10943
c906108c
SS
10944 /* Look up member function name in fieldlist. */
10945 for (i = 0; i < fip->nfnfields; i++)
10946 {
27bfe10e 10947 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10948 break;
10949 }
10950
10951 /* Create new list element if necessary. */
10952 if (i < fip->nfnfields)
10953 flp = &fip->fnfieldlists[i];
10954 else
10955 {
10956 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10957 {
10958 fip->fnfieldlists = (struct fnfieldlist *)
10959 xrealloc (fip->fnfieldlists,
10960 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10961 * sizeof (struct fnfieldlist));
c906108c 10962 if (fip->nfnfields == 0)
c13c43fd 10963 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10964 }
10965 flp = &fip->fnfieldlists[fip->nfnfields];
10966 flp->name = fieldname;
10967 flp->length = 0;
10968 flp->head = NULL;
3da10d80 10969 i = fip->nfnfields++;
c906108c
SS
10970 }
10971
10972 /* Create a new member function field and chain it to the field list
0963b4bd 10973 entry. */
c906108c 10974 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10975 make_cleanup (xfree, new_fnfield);
c906108c
SS
10976 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10977 new_fnfield->next = flp->head;
10978 flp->head = new_fnfield;
10979 flp->length++;
10980
10981 /* Fill in the member function field info. */
10982 fnp = &new_fnfield->fnfield;
3da10d80
KS
10983
10984 /* Delay processing of the physname until later. */
10985 if (cu->language == language_cplus || cu->language == language_java)
10986 {
10987 add_to_method_list (type, i, flp->length - 1, fieldname,
10988 die, cu);
10989 }
10990 else
10991 {
1d06ead6 10992 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10993 fnp->physname = physname ? physname : "";
10994 }
10995
c906108c 10996 fnp->type = alloc_type (objfile);
f792889a
DJ
10997 this_type = read_type_die (die, cu);
10998 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10999 {
f792889a 11000 int nparams = TYPE_NFIELDS (this_type);
c906108c 11001
f792889a 11002 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11003 of the method itself (TYPE_CODE_METHOD). */
11004 smash_to_method_type (fnp->type, type,
f792889a
DJ
11005 TYPE_TARGET_TYPE (this_type),
11006 TYPE_FIELDS (this_type),
11007 TYPE_NFIELDS (this_type),
11008 TYPE_VARARGS (this_type));
c906108c
SS
11009
11010 /* Handle static member functions.
c5aa993b 11011 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11012 member functions. G++ helps GDB by marking the first
11013 parameter for non-static member functions (which is the this
11014 pointer) as artificial. We obtain this information from
11015 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11016 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11017 fnp->voffset = VOFFSET_STATIC;
11018 }
11019 else
e2e0b3e5 11020 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11021 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11022
11023 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11024 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11025 fnp->fcontext = die_containing_type (die, cu);
c906108c 11026
3e43a32a
MS
11027 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11028 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11029
11030 /* Get accessibility. */
e142c38c 11031 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11032 if (attr)
60d5a603
JK
11033 accessibility = DW_UNSND (attr);
11034 else
11035 accessibility = dwarf2_default_access_attribute (die, cu);
11036 switch (accessibility)
c906108c 11037 {
60d5a603
JK
11038 case DW_ACCESS_private:
11039 fnp->is_private = 1;
11040 break;
11041 case DW_ACCESS_protected:
11042 fnp->is_protected = 1;
11043 break;
c906108c
SS
11044 }
11045
b02dede2 11046 /* Check for artificial methods. */
e142c38c 11047 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11048 if (attr && DW_UNSND (attr) != 0)
11049 fnp->is_artificial = 1;
11050
7d27a96d
TT
11051 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11052
0d564a31 11053 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11054 function. For older versions of GCC, this is an offset in the
11055 appropriate virtual table, as specified by DW_AT_containing_type.
11056 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11057 to the object address. */
11058
e142c38c 11059 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11060 if (attr)
8e19ed76 11061 {
aec5aa8b 11062 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11063 {
aec5aa8b
TT
11064 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11065 {
11066 /* Old-style GCC. */
11067 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11068 }
11069 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11070 || (DW_BLOCK (attr)->size > 1
11071 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11072 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11073 {
11074 struct dwarf_block blk;
11075 int offset;
11076
11077 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11078 ? 1 : 2);
11079 blk.size = DW_BLOCK (attr)->size - offset;
11080 blk.data = DW_BLOCK (attr)->data + offset;
11081 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11082 if ((fnp->voffset % cu->header.addr_size) != 0)
11083 dwarf2_complex_location_expr_complaint ();
11084 else
11085 fnp->voffset /= cu->header.addr_size;
11086 fnp->voffset += 2;
11087 }
11088 else
11089 dwarf2_complex_location_expr_complaint ();
11090
11091 if (!fnp->fcontext)
11092 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11093 }
3690dd37 11094 else if (attr_form_is_section_offset (attr))
8e19ed76 11095 {
4d3c2250 11096 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11097 }
11098 else
11099 {
4d3c2250
KB
11100 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11101 fieldname);
8e19ed76 11102 }
0d564a31 11103 }
d48cc9dd
DJ
11104 else
11105 {
11106 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11107 if (attr && DW_UNSND (attr))
11108 {
11109 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11110 complaint (&symfile_complaints,
3e43a32a
MS
11111 _("Member function \"%s\" (offset %d) is virtual "
11112 "but the vtable offset is not specified"),
b64f50a1 11113 fieldname, die->offset.sect_off);
9655fd1a 11114 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11115 TYPE_CPLUS_DYNAMIC (type) = 1;
11116 }
11117 }
c906108c
SS
11118}
11119
11120/* Create the vector of member function fields, and attach it to the type. */
11121
11122static void
fba45db2 11123dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11124 struct dwarf2_cu *cu)
c906108c
SS
11125{
11126 struct fnfieldlist *flp;
c906108c
SS
11127 int i;
11128
b4ba55a1 11129 if (cu->language == language_ada)
a73c6dcd 11130 error (_("unexpected member functions in Ada type"));
b4ba55a1 11131
c906108c
SS
11132 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11133 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11134 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11135
11136 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11137 {
11138 struct nextfnfield *nfp = flp->head;
11139 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11140 int k;
11141
11142 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11143 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11144 fn_flp->fn_fields = (struct fn_field *)
11145 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11146 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11147 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11148 }
11149
11150 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11151}
11152
1168df01
JB
11153/* Returns non-zero if NAME is the name of a vtable member in CU's
11154 language, zero otherwise. */
11155static int
11156is_vtable_name (const char *name, struct dwarf2_cu *cu)
11157{
11158 static const char vptr[] = "_vptr";
987504bb 11159 static const char vtable[] = "vtable";
1168df01 11160
987504bb
JJ
11161 /* Look for the C++ and Java forms of the vtable. */
11162 if ((cu->language == language_java
11163 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11164 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11165 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11166 return 1;
11167
11168 return 0;
11169}
11170
c0dd20ea 11171/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11172 functions, with the ABI-specified layout. If TYPE describes
11173 such a structure, smash it into a member function type.
61049d3b
DJ
11174
11175 GCC shouldn't do this; it should just output pointer to member DIEs.
11176 This is GCC PR debug/28767. */
c0dd20ea 11177
0b92b5bb
TT
11178static void
11179quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11180{
0b92b5bb 11181 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11182
11183 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11184 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11185 return;
c0dd20ea
DJ
11186
11187 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11188 if (TYPE_FIELD_NAME (type, 0) == NULL
11189 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11190 || TYPE_FIELD_NAME (type, 1) == NULL
11191 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11192 return;
c0dd20ea
DJ
11193
11194 /* Find the type of the method. */
0b92b5bb 11195 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11196 if (pfn_type == NULL
11197 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11198 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11199 return;
c0dd20ea
DJ
11200
11201 /* Look for the "this" argument. */
11202 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11203 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11204 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11205 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11206 return;
c0dd20ea
DJ
11207
11208 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11209 new_type = alloc_type (objfile);
11210 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11211 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11212 TYPE_VARARGS (pfn_type));
0b92b5bb 11213 smash_to_methodptr_type (type, new_type);
c0dd20ea 11214}
1168df01 11215
685b1105
JK
11216/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11217 (icc). */
11218
11219static int
11220producer_is_icc (struct dwarf2_cu *cu)
11221{
11222 if (!cu->checked_producer)
11223 check_producer (cu);
11224
11225 return cu->producer_is_icc;
11226}
11227
c906108c 11228/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11229 (definition) to create a type for the structure or union. Fill in
11230 the type's name and general properties; the members will not be
11231 processed until process_structure_type.
c906108c 11232
c767944b
DJ
11233 NOTE: we need to call these functions regardless of whether or not the
11234 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11235 structure or union. This gets the type entered into our set of
11236 user defined types.
11237
11238 However, if the structure is incomplete (an opaque struct/union)
11239 then suppress creating a symbol table entry for it since gdb only
11240 wants to find the one with the complete definition. Note that if
11241 it is complete, we just call new_symbol, which does it's own
11242 checking about whether the struct/union is anonymous or not (and
11243 suppresses creating a symbol table entry itself). */
11244
f792889a 11245static struct type *
134d01f1 11246read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11247{
e7c27a73 11248 struct objfile *objfile = cu->objfile;
c906108c
SS
11249 struct type *type;
11250 struct attribute *attr;
15d034d0 11251 const char *name;
c906108c 11252
348e048f
DE
11253 /* If the definition of this type lives in .debug_types, read that type.
11254 Don't follow DW_AT_specification though, that will take us back up
11255 the chain and we want to go down. */
45e58e77 11256 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11257 if (attr)
11258 {
11259 struct dwarf2_cu *type_cu = cu;
11260 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11261
348e048f
DE
11262 /* We could just recurse on read_structure_type, but we need to call
11263 get_die_type to ensure only one type for this DIE is created.
11264 This is important, for example, because for c++ classes we need
11265 TYPE_NAME set which is only done by new_symbol. Blech. */
11266 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11267
11268 /* TYPE_CU may not be the same as CU.
11269 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11270 return set_die_type (die, type, cu);
11271 }
11272
c0dd20ea 11273 type = alloc_type (objfile);
c906108c 11274 INIT_CPLUS_SPECIFIC (type);
93311388 11275
39cbfefa
DJ
11276 name = dwarf2_name (die, cu);
11277 if (name != NULL)
c906108c 11278 {
987504bb
JJ
11279 if (cu->language == language_cplus
11280 || cu->language == language_java)
63d06c5c 11281 {
15d034d0 11282 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11283
11284 /* dwarf2_full_name might have already finished building the DIE's
11285 type. If so, there is no need to continue. */
11286 if (get_die_type (die, cu) != NULL)
11287 return get_die_type (die, cu);
11288
11289 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11290 if (die->tag == DW_TAG_structure_type
11291 || die->tag == DW_TAG_class_type)
11292 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11293 }
11294 else
11295 {
d8151005
DJ
11296 /* The name is already allocated along with this objfile, so
11297 we don't need to duplicate it for the type. */
7d455152 11298 TYPE_TAG_NAME (type) = name;
94af9270
KS
11299 if (die->tag == DW_TAG_class_type)
11300 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11301 }
c906108c
SS
11302 }
11303
11304 if (die->tag == DW_TAG_structure_type)
11305 {
11306 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11307 }
11308 else if (die->tag == DW_TAG_union_type)
11309 {
11310 TYPE_CODE (type) = TYPE_CODE_UNION;
11311 }
11312 else
11313 {
c906108c
SS
11314 TYPE_CODE (type) = TYPE_CODE_CLASS;
11315 }
11316
0cc2414c
TT
11317 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11318 TYPE_DECLARED_CLASS (type) = 1;
11319
e142c38c 11320 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11321 if (attr)
11322 {
11323 TYPE_LENGTH (type) = DW_UNSND (attr);
11324 }
11325 else
11326 {
11327 TYPE_LENGTH (type) = 0;
11328 }
11329
685b1105
JK
11330 if (producer_is_icc (cu))
11331 {
11332 /* ICC does not output the required DW_AT_declaration
11333 on incomplete types, but gives them a size of zero. */
11334 }
11335 else
11336 TYPE_STUB_SUPPORTED (type) = 1;
11337
dc718098 11338 if (die_is_declaration (die, cu))
876cecd0 11339 TYPE_STUB (type) = 1;
a6c727b2
DJ
11340 else if (attr == NULL && die->child == NULL
11341 && producer_is_realview (cu->producer))
11342 /* RealView does not output the required DW_AT_declaration
11343 on incomplete types. */
11344 TYPE_STUB (type) = 1;
dc718098 11345
c906108c
SS
11346 /* We need to add the type field to the die immediately so we don't
11347 infinitely recurse when dealing with pointers to the structure
0963b4bd 11348 type within the structure itself. */
1c379e20 11349 set_die_type (die, type, cu);
c906108c 11350
7e314c57
JK
11351 /* set_die_type should be already done. */
11352 set_descriptive_type (type, die, cu);
11353
c767944b
DJ
11354 return type;
11355}
11356
11357/* Finish creating a structure or union type, including filling in
11358 its members and creating a symbol for it. */
11359
11360static void
11361process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11362{
11363 struct objfile *objfile = cu->objfile;
11364 struct die_info *child_die = die->child;
11365 struct type *type;
11366
11367 type = get_die_type (die, cu);
11368 if (type == NULL)
11369 type = read_structure_type (die, cu);
11370
e142c38c 11371 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11372 {
11373 struct field_info fi;
11374 struct die_info *child_die;
34eaf542 11375 VEC (symbolp) *template_args = NULL;
c767944b 11376 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11377
11378 memset (&fi, 0, sizeof (struct field_info));
11379
639d11d3 11380 child_die = die->child;
c906108c
SS
11381
11382 while (child_die && child_die->tag)
11383 {
a9a9bd0f
DC
11384 if (child_die->tag == DW_TAG_member
11385 || child_die->tag == DW_TAG_variable)
c906108c 11386 {
a9a9bd0f
DC
11387 /* NOTE: carlton/2002-11-05: A C++ static data member
11388 should be a DW_TAG_member that is a declaration, but
11389 all versions of G++ as of this writing (so through at
11390 least 3.2.1) incorrectly generate DW_TAG_variable
11391 tags for them instead. */
e7c27a73 11392 dwarf2_add_field (&fi, child_die, cu);
c906108c 11393 }
8713b1b1 11394 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11395 {
0963b4bd 11396 /* C++ member function. */
e7c27a73 11397 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11398 }
11399 else if (child_die->tag == DW_TAG_inheritance)
11400 {
11401 /* C++ base class field. */
e7c27a73 11402 dwarf2_add_field (&fi, child_die, cu);
c906108c 11403 }
98751a41
JK
11404 else if (child_die->tag == DW_TAG_typedef)
11405 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11406 else if (child_die->tag == DW_TAG_template_type_param
11407 || child_die->tag == DW_TAG_template_value_param)
11408 {
11409 struct symbol *arg = new_symbol (child_die, NULL, cu);
11410
f1078f66
DJ
11411 if (arg != NULL)
11412 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11413 }
11414
c906108c
SS
11415 child_die = sibling_die (child_die);
11416 }
11417
34eaf542
TT
11418 /* Attach template arguments to type. */
11419 if (! VEC_empty (symbolp, template_args))
11420 {
11421 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11422 TYPE_N_TEMPLATE_ARGUMENTS (type)
11423 = VEC_length (symbolp, template_args);
11424 TYPE_TEMPLATE_ARGUMENTS (type)
11425 = obstack_alloc (&objfile->objfile_obstack,
11426 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11427 * sizeof (struct symbol *)));
11428 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11429 VEC_address (symbolp, template_args),
11430 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11431 * sizeof (struct symbol *)));
11432 VEC_free (symbolp, template_args);
11433 }
11434
c906108c
SS
11435 /* Attach fields and member functions to the type. */
11436 if (fi.nfields)
e7c27a73 11437 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11438 if (fi.nfnfields)
11439 {
e7c27a73 11440 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11441
c5aa993b 11442 /* Get the type which refers to the base class (possibly this
c906108c 11443 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11444 class from the DW_AT_containing_type attribute. This use of
11445 DW_AT_containing_type is a GNU extension. */
c906108c 11446
e142c38c 11447 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11448 {
e7c27a73 11449 struct type *t = die_containing_type (die, cu);
c906108c
SS
11450
11451 TYPE_VPTR_BASETYPE (type) = t;
11452 if (type == t)
11453 {
c906108c
SS
11454 int i;
11455
11456 /* Our own class provides vtbl ptr. */
11457 for (i = TYPE_NFIELDS (t) - 1;
11458 i >= TYPE_N_BASECLASSES (t);
11459 --i)
11460 {
0d5cff50 11461 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11462
1168df01 11463 if (is_vtable_name (fieldname, cu))
c906108c
SS
11464 {
11465 TYPE_VPTR_FIELDNO (type) = i;
11466 break;
11467 }
11468 }
11469
11470 /* Complain if virtual function table field not found. */
11471 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11472 complaint (&symfile_complaints,
3e43a32a
MS
11473 _("virtual function table pointer "
11474 "not found when defining class '%s'"),
4d3c2250
KB
11475 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11476 "");
c906108c
SS
11477 }
11478 else
11479 {
11480 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11481 }
11482 }
f6235d4c
EZ
11483 else if (cu->producer
11484 && strncmp (cu->producer,
11485 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11486 {
11487 /* The IBM XLC compiler does not provide direct indication
11488 of the containing type, but the vtable pointer is
11489 always named __vfp. */
11490
11491 int i;
11492
11493 for (i = TYPE_NFIELDS (type) - 1;
11494 i >= TYPE_N_BASECLASSES (type);
11495 --i)
11496 {
11497 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11498 {
11499 TYPE_VPTR_FIELDNO (type) = i;
11500 TYPE_VPTR_BASETYPE (type) = type;
11501 break;
11502 }
11503 }
11504 }
c906108c 11505 }
98751a41
JK
11506
11507 /* Copy fi.typedef_field_list linked list elements content into the
11508 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11509 if (fi.typedef_field_list)
11510 {
11511 int i = fi.typedef_field_list_count;
11512
a0d7a4ff 11513 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11514 TYPE_TYPEDEF_FIELD_ARRAY (type)
11515 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11516 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11517
11518 /* Reverse the list order to keep the debug info elements order. */
11519 while (--i >= 0)
11520 {
11521 struct typedef_field *dest, *src;
6e70227d 11522
98751a41
JK
11523 dest = &TYPE_TYPEDEF_FIELD (type, i);
11524 src = &fi.typedef_field_list->field;
11525 fi.typedef_field_list = fi.typedef_field_list->next;
11526 *dest = *src;
11527 }
11528 }
c767944b
DJ
11529
11530 do_cleanups (back_to);
eb2a6f42
TT
11531
11532 if (HAVE_CPLUS_STRUCT (type))
11533 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11534 }
63d06c5c 11535
bb5ed363 11536 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11537
90aeadfc
DC
11538 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11539 snapshots) has been known to create a die giving a declaration
11540 for a class that has, as a child, a die giving a definition for a
11541 nested class. So we have to process our children even if the
11542 current die is a declaration. Normally, of course, a declaration
11543 won't have any children at all. */
134d01f1 11544
90aeadfc
DC
11545 while (child_die != NULL && child_die->tag)
11546 {
11547 if (child_die->tag == DW_TAG_member
11548 || child_die->tag == DW_TAG_variable
34eaf542
TT
11549 || child_die->tag == DW_TAG_inheritance
11550 || child_die->tag == DW_TAG_template_value_param
11551 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11552 {
90aeadfc 11553 /* Do nothing. */
134d01f1 11554 }
90aeadfc
DC
11555 else
11556 process_die (child_die, cu);
134d01f1 11557
90aeadfc 11558 child_die = sibling_die (child_die);
134d01f1
DJ
11559 }
11560
fa4028e9
JB
11561 /* Do not consider external references. According to the DWARF standard,
11562 these DIEs are identified by the fact that they have no byte_size
11563 attribute, and a declaration attribute. */
11564 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11565 || !die_is_declaration (die, cu))
c767944b 11566 new_symbol (die, type, cu);
134d01f1
DJ
11567}
11568
11569/* Given a DW_AT_enumeration_type die, set its type. We do not
11570 complete the type's fields yet, or create any symbols. */
c906108c 11571
f792889a 11572static struct type *
134d01f1 11573read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11574{
e7c27a73 11575 struct objfile *objfile = cu->objfile;
c906108c 11576 struct type *type;
c906108c 11577 struct attribute *attr;
0114d602 11578 const char *name;
134d01f1 11579
348e048f
DE
11580 /* If the definition of this type lives in .debug_types, read that type.
11581 Don't follow DW_AT_specification though, that will take us back up
11582 the chain and we want to go down. */
45e58e77 11583 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11584 if (attr)
11585 {
11586 struct dwarf2_cu *type_cu = cu;
11587 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11588
348e048f 11589 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11590
11591 /* TYPE_CU may not be the same as CU.
11592 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11593 return set_die_type (die, type, cu);
11594 }
11595
c906108c
SS
11596 type = alloc_type (objfile);
11597
11598 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11599 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11600 if (name != NULL)
7d455152 11601 TYPE_TAG_NAME (type) = name;
c906108c 11602
e142c38c 11603 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11604 if (attr)
11605 {
11606 TYPE_LENGTH (type) = DW_UNSND (attr);
11607 }
11608 else
11609 {
11610 TYPE_LENGTH (type) = 0;
11611 }
11612
137033e9
JB
11613 /* The enumeration DIE can be incomplete. In Ada, any type can be
11614 declared as private in the package spec, and then defined only
11615 inside the package body. Such types are known as Taft Amendment
11616 Types. When another package uses such a type, an incomplete DIE
11617 may be generated by the compiler. */
02eb380e 11618 if (die_is_declaration (die, cu))
876cecd0 11619 TYPE_STUB (type) = 1;
02eb380e 11620
f792889a 11621 return set_die_type (die, type, cu);
134d01f1
DJ
11622}
11623
11624/* Given a pointer to a die which begins an enumeration, process all
11625 the dies that define the members of the enumeration, and create the
11626 symbol for the enumeration type.
11627
11628 NOTE: We reverse the order of the element list. */
11629
11630static void
11631process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11632{
f792889a 11633 struct type *this_type;
134d01f1 11634
f792889a
DJ
11635 this_type = get_die_type (die, cu);
11636 if (this_type == NULL)
11637 this_type = read_enumeration_type (die, cu);
9dc481d3 11638
639d11d3 11639 if (die->child != NULL)
c906108c 11640 {
9dc481d3
DE
11641 struct die_info *child_die;
11642 struct symbol *sym;
11643 struct field *fields = NULL;
11644 int num_fields = 0;
11645 int unsigned_enum = 1;
15d034d0 11646 const char *name;
cafec441
TT
11647 int flag_enum = 1;
11648 ULONGEST mask = 0;
9dc481d3 11649
639d11d3 11650 child_die = die->child;
c906108c
SS
11651 while (child_die && child_die->tag)
11652 {
11653 if (child_die->tag != DW_TAG_enumerator)
11654 {
e7c27a73 11655 process_die (child_die, cu);
c906108c
SS
11656 }
11657 else
11658 {
39cbfefa
DJ
11659 name = dwarf2_name (child_die, cu);
11660 if (name)
c906108c 11661 {
f792889a 11662 sym = new_symbol (child_die, this_type, cu);
c906108c 11663 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11664 {
11665 unsigned_enum = 0;
11666 flag_enum = 0;
11667 }
11668 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11669 flag_enum = 0;
11670 else
11671 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11672
11673 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11674 {
11675 fields = (struct field *)
11676 xrealloc (fields,
11677 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11678 * sizeof (struct field));
c906108c
SS
11679 }
11680
3567439c 11681 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11682 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11683 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11684 FIELD_BITSIZE (fields[num_fields]) = 0;
11685
11686 num_fields++;
11687 }
11688 }
11689
11690 child_die = sibling_die (child_die);
11691 }
11692
11693 if (num_fields)
11694 {
f792889a
DJ
11695 TYPE_NFIELDS (this_type) = num_fields;
11696 TYPE_FIELDS (this_type) = (struct field *)
11697 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11698 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11699 sizeof (struct field) * num_fields);
b8c9b27d 11700 xfree (fields);
c906108c
SS
11701 }
11702 if (unsigned_enum)
876cecd0 11703 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11704 if (flag_enum)
11705 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11706 }
134d01f1 11707
6c83ed52
TT
11708 /* If we are reading an enum from a .debug_types unit, and the enum
11709 is a declaration, and the enum is not the signatured type in the
11710 unit, then we do not want to add a symbol for it. Adding a
11711 symbol would in some cases obscure the true definition of the
11712 enum, giving users an incomplete type when the definition is
11713 actually available. Note that we do not want to do this for all
11714 enums which are just declarations, because C++0x allows forward
11715 enum declarations. */
3019eac3 11716 if (cu->per_cu->is_debug_types
6c83ed52
TT
11717 && die_is_declaration (die, cu))
11718 {
52dc124a 11719 struct signatured_type *sig_type;
6c83ed52 11720
52dc124a 11721 sig_type
6c83ed52 11722 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8a0459fd 11723 cu->per_cu->section,
6c83ed52 11724 cu->per_cu->offset);
3019eac3
DE
11725 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11726 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11727 return;
11728 }
11729
f792889a 11730 new_symbol (die, this_type, cu);
c906108c
SS
11731}
11732
11733/* Extract all information from a DW_TAG_array_type DIE and put it in
11734 the DIE's type field. For now, this only handles one dimensional
11735 arrays. */
11736
f792889a 11737static struct type *
e7c27a73 11738read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11739{
e7c27a73 11740 struct objfile *objfile = cu->objfile;
c906108c 11741 struct die_info *child_die;
7e314c57 11742 struct type *type;
c906108c
SS
11743 struct type *element_type, *range_type, *index_type;
11744 struct type **range_types = NULL;
11745 struct attribute *attr;
11746 int ndim = 0;
11747 struct cleanup *back_to;
15d034d0 11748 const char *name;
c906108c 11749
e7c27a73 11750 element_type = die_type (die, cu);
c906108c 11751
7e314c57
JK
11752 /* The die_type call above may have already set the type for this DIE. */
11753 type = get_die_type (die, cu);
11754 if (type)
11755 return type;
11756
c906108c
SS
11757 /* Irix 6.2 native cc creates array types without children for
11758 arrays with unspecified length. */
639d11d3 11759 if (die->child == NULL)
c906108c 11760 {
46bf5051 11761 index_type = objfile_type (objfile)->builtin_int;
c906108c 11762 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11763 type = create_array_type (NULL, element_type, range_type);
11764 return set_die_type (die, type, cu);
c906108c
SS
11765 }
11766
11767 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11768 child_die = die->child;
c906108c
SS
11769 while (child_die && child_die->tag)
11770 {
11771 if (child_die->tag == DW_TAG_subrange_type)
11772 {
f792889a 11773 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11774
f792889a 11775 if (child_type != NULL)
a02abb62 11776 {
0963b4bd
MS
11777 /* The range type was succesfully read. Save it for the
11778 array type creation. */
a02abb62
JB
11779 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11780 {
11781 range_types = (struct type **)
11782 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11783 * sizeof (struct type *));
11784 if (ndim == 0)
11785 make_cleanup (free_current_contents, &range_types);
11786 }
f792889a 11787 range_types[ndim++] = child_type;
a02abb62 11788 }
c906108c
SS
11789 }
11790 child_die = sibling_die (child_die);
11791 }
11792
11793 /* Dwarf2 dimensions are output from left to right, create the
11794 necessary array types in backwards order. */
7ca2d3a3 11795
c906108c 11796 type = element_type;
7ca2d3a3
DL
11797
11798 if (read_array_order (die, cu) == DW_ORD_col_major)
11799 {
11800 int i = 0;
9a619af0 11801
7ca2d3a3
DL
11802 while (i < ndim)
11803 type = create_array_type (NULL, type, range_types[i++]);
11804 }
11805 else
11806 {
11807 while (ndim-- > 0)
11808 type = create_array_type (NULL, type, range_types[ndim]);
11809 }
c906108c 11810
f5f8a009
EZ
11811 /* Understand Dwarf2 support for vector types (like they occur on
11812 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11813 array type. This is not part of the Dwarf2/3 standard yet, but a
11814 custom vendor extension. The main difference between a regular
11815 array and the vector variant is that vectors are passed by value
11816 to functions. */
e142c38c 11817 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11818 if (attr)
ea37ba09 11819 make_vector_type (type);
f5f8a009 11820
dbc98a8b
KW
11821 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11822 implementation may choose to implement triple vectors using this
11823 attribute. */
11824 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11825 if (attr)
11826 {
11827 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11828 TYPE_LENGTH (type) = DW_UNSND (attr);
11829 else
3e43a32a
MS
11830 complaint (&symfile_complaints,
11831 _("DW_AT_byte_size for array type smaller "
11832 "than the total size of elements"));
dbc98a8b
KW
11833 }
11834
39cbfefa
DJ
11835 name = dwarf2_name (die, cu);
11836 if (name)
11837 TYPE_NAME (type) = name;
6e70227d 11838
0963b4bd 11839 /* Install the type in the die. */
7e314c57
JK
11840 set_die_type (die, type, cu);
11841
11842 /* set_die_type should be already done. */
b4ba55a1
JB
11843 set_descriptive_type (type, die, cu);
11844
c906108c
SS
11845 do_cleanups (back_to);
11846
7e314c57 11847 return type;
c906108c
SS
11848}
11849
7ca2d3a3 11850static enum dwarf_array_dim_ordering
6e70227d 11851read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11852{
11853 struct attribute *attr;
11854
11855 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11856
11857 if (attr) return DW_SND (attr);
11858
0963b4bd
MS
11859 /* GNU F77 is a special case, as at 08/2004 array type info is the
11860 opposite order to the dwarf2 specification, but data is still
11861 laid out as per normal fortran.
7ca2d3a3 11862
0963b4bd
MS
11863 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11864 version checking. */
7ca2d3a3 11865
905e0470
PM
11866 if (cu->language == language_fortran
11867 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11868 {
11869 return DW_ORD_row_major;
11870 }
11871
6e70227d 11872 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11873 {
11874 case array_column_major:
11875 return DW_ORD_col_major;
11876 case array_row_major:
11877 default:
11878 return DW_ORD_row_major;
11879 };
11880}
11881
72019c9c 11882/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11883 the DIE's type field. */
72019c9c 11884
f792889a 11885static struct type *
72019c9c
GM
11886read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11887{
7e314c57
JK
11888 struct type *domain_type, *set_type;
11889 struct attribute *attr;
f792889a 11890
7e314c57
JK
11891 domain_type = die_type (die, cu);
11892
11893 /* The die_type call above may have already set the type for this DIE. */
11894 set_type = get_die_type (die, cu);
11895 if (set_type)
11896 return set_type;
11897
11898 set_type = create_set_type (NULL, domain_type);
11899
11900 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11901 if (attr)
11902 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11903
f792889a 11904 return set_die_type (die, set_type, cu);
72019c9c 11905}
7ca2d3a3 11906
0971de02
TT
11907/* A helper for read_common_block that creates a locexpr baton.
11908 SYM is the symbol which we are marking as computed.
11909 COMMON_DIE is the DIE for the common block.
11910 COMMON_LOC is the location expression attribute for the common
11911 block itself.
11912 MEMBER_LOC is the location expression attribute for the particular
11913 member of the common block that we are processing.
11914 CU is the CU from which the above come. */
11915
11916static void
11917mark_common_block_symbol_computed (struct symbol *sym,
11918 struct die_info *common_die,
11919 struct attribute *common_loc,
11920 struct attribute *member_loc,
11921 struct dwarf2_cu *cu)
11922{
11923 struct objfile *objfile = dwarf2_per_objfile->objfile;
11924 struct dwarf2_locexpr_baton *baton;
11925 gdb_byte *ptr;
11926 unsigned int cu_off;
11927 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11928 LONGEST offset = 0;
11929
11930 gdb_assert (common_loc && member_loc);
11931 gdb_assert (attr_form_is_block (common_loc));
11932 gdb_assert (attr_form_is_block (member_loc)
11933 || attr_form_is_constant (member_loc));
11934
11935 baton = obstack_alloc (&objfile->objfile_obstack,
11936 sizeof (struct dwarf2_locexpr_baton));
11937 baton->per_cu = cu->per_cu;
11938 gdb_assert (baton->per_cu);
11939
11940 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11941
11942 if (attr_form_is_constant (member_loc))
11943 {
11944 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11945 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11946 }
11947 else
11948 baton->size += DW_BLOCK (member_loc)->size;
11949
11950 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11951 baton->data = ptr;
11952
11953 *ptr++ = DW_OP_call4;
11954 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11955 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11956 ptr += 4;
11957
11958 if (attr_form_is_constant (member_loc))
11959 {
11960 *ptr++ = DW_OP_addr;
11961 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11962 ptr += cu->header.addr_size;
11963 }
11964 else
11965 {
11966 /* We have to copy the data here, because DW_OP_call4 will only
11967 use a DW_AT_location attribute. */
11968 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11969 ptr += DW_BLOCK (member_loc)->size;
11970 }
11971
11972 *ptr++ = DW_OP_plus;
11973 gdb_assert (ptr - baton->data == baton->size);
11974
0971de02 11975 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 11976 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
11977}
11978
4357ac6c
TT
11979/* Create appropriate locally-scoped variables for all the
11980 DW_TAG_common_block entries. Also create a struct common_block
11981 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11982 is used to sepate the common blocks name namespace from regular
11983 variable names. */
c906108c
SS
11984
11985static void
e7c27a73 11986read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11987{
0971de02
TT
11988 struct attribute *attr;
11989
11990 attr = dwarf2_attr (die, DW_AT_location, cu);
11991 if (attr)
11992 {
11993 /* Support the .debug_loc offsets. */
11994 if (attr_form_is_block (attr))
11995 {
11996 /* Ok. */
11997 }
11998 else if (attr_form_is_section_offset (attr))
11999 {
12000 dwarf2_complex_location_expr_complaint ();
12001 attr = NULL;
12002 }
12003 else
12004 {
12005 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12006 "common block member");
12007 attr = NULL;
12008 }
12009 }
12010
639d11d3 12011 if (die->child != NULL)
c906108c 12012 {
4357ac6c
TT
12013 struct objfile *objfile = cu->objfile;
12014 struct die_info *child_die;
12015 size_t n_entries = 0, size;
12016 struct common_block *common_block;
12017 struct symbol *sym;
74ac6d43 12018
4357ac6c
TT
12019 for (child_die = die->child;
12020 child_die && child_die->tag;
12021 child_die = sibling_die (child_die))
12022 ++n_entries;
12023
12024 size = (sizeof (struct common_block)
12025 + (n_entries - 1) * sizeof (struct symbol *));
12026 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12027 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12028 common_block->n_entries = 0;
12029
12030 for (child_die = die->child;
12031 child_die && child_die->tag;
12032 child_die = sibling_die (child_die))
12033 {
12034 /* Create the symbol in the DW_TAG_common_block block in the current
12035 symbol scope. */
e7c27a73 12036 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12037 if (sym != NULL)
12038 {
12039 struct attribute *member_loc;
12040
12041 common_block->contents[common_block->n_entries++] = sym;
12042
12043 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12044 cu);
12045 if (member_loc)
12046 {
12047 /* GDB has handled this for a long time, but it is
12048 not specified by DWARF. It seems to have been
12049 emitted by gfortran at least as recently as:
12050 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12051 complaint (&symfile_complaints,
12052 _("Variable in common block has "
12053 "DW_AT_data_member_location "
12054 "- DIE at 0x%x [in module %s]"),
12055 child_die->offset.sect_off, cu->objfile->name);
12056
12057 if (attr_form_is_section_offset (member_loc))
12058 dwarf2_complex_location_expr_complaint ();
12059 else if (attr_form_is_constant (member_loc)
12060 || attr_form_is_block (member_loc))
12061 {
12062 if (attr)
12063 mark_common_block_symbol_computed (sym, die, attr,
12064 member_loc, cu);
12065 }
12066 else
12067 dwarf2_complex_location_expr_complaint ();
12068 }
12069 }
c906108c 12070 }
4357ac6c
TT
12071
12072 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12073 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12074 }
12075}
12076
0114d602 12077/* Create a type for a C++ namespace. */
d9fa45fe 12078
0114d602
DJ
12079static struct type *
12080read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12081{
e7c27a73 12082 struct objfile *objfile = cu->objfile;
0114d602 12083 const char *previous_prefix, *name;
9219021c 12084 int is_anonymous;
0114d602
DJ
12085 struct type *type;
12086
12087 /* For extensions, reuse the type of the original namespace. */
12088 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12089 {
12090 struct die_info *ext_die;
12091 struct dwarf2_cu *ext_cu = cu;
9a619af0 12092
0114d602
DJ
12093 ext_die = dwarf2_extension (die, &ext_cu);
12094 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12095
12096 /* EXT_CU may not be the same as CU.
12097 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12098 return set_die_type (die, type, cu);
12099 }
9219021c 12100
e142c38c 12101 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12102
12103 /* Now build the name of the current namespace. */
12104
0114d602
DJ
12105 previous_prefix = determine_prefix (die, cu);
12106 if (previous_prefix[0] != '\0')
12107 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12108 previous_prefix, name, 0, cu);
0114d602
DJ
12109
12110 /* Create the type. */
12111 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12112 objfile);
abee88f2 12113 TYPE_NAME (type) = name;
0114d602
DJ
12114 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12115
60531b24 12116 return set_die_type (die, type, cu);
0114d602
DJ
12117}
12118
12119/* Read a C++ namespace. */
12120
12121static void
12122read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12123{
12124 struct objfile *objfile = cu->objfile;
0114d602 12125 int is_anonymous;
9219021c 12126
5c4e30ca
DC
12127 /* Add a symbol associated to this if we haven't seen the namespace
12128 before. Also, add a using directive if it's an anonymous
12129 namespace. */
9219021c 12130
f2f0e013 12131 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12132 {
12133 struct type *type;
12134
0114d602 12135 type = read_type_die (die, cu);
e7c27a73 12136 new_symbol (die, type, cu);
5c4e30ca 12137
e8e80198 12138 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12139 if (is_anonymous)
0114d602
DJ
12140 {
12141 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12142
c0cc3a76 12143 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12144 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12145 }
5c4e30ca 12146 }
9219021c 12147
639d11d3 12148 if (die->child != NULL)
d9fa45fe 12149 {
639d11d3 12150 struct die_info *child_die = die->child;
6e70227d 12151
d9fa45fe
DC
12152 while (child_die && child_die->tag)
12153 {
e7c27a73 12154 process_die (child_die, cu);
d9fa45fe
DC
12155 child_die = sibling_die (child_die);
12156 }
12157 }
38d518c9
EZ
12158}
12159
f55ee35c
JK
12160/* Read a Fortran module as type. This DIE can be only a declaration used for
12161 imported module. Still we need that type as local Fortran "use ... only"
12162 declaration imports depend on the created type in determine_prefix. */
12163
12164static struct type *
12165read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12166{
12167 struct objfile *objfile = cu->objfile;
15d034d0 12168 const char *module_name;
f55ee35c
JK
12169 struct type *type;
12170
12171 module_name = dwarf2_name (die, cu);
12172 if (!module_name)
3e43a32a
MS
12173 complaint (&symfile_complaints,
12174 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12175 die->offset.sect_off);
f55ee35c
JK
12176 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12177
12178 /* determine_prefix uses TYPE_TAG_NAME. */
12179 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12180
12181 return set_die_type (die, type, cu);
12182}
12183
5d7cb8df
JK
12184/* Read a Fortran module. */
12185
12186static void
12187read_module (struct die_info *die, struct dwarf2_cu *cu)
12188{
12189 struct die_info *child_die = die->child;
12190
5d7cb8df
JK
12191 while (child_die && child_die->tag)
12192 {
12193 process_die (child_die, cu);
12194 child_die = sibling_die (child_die);
12195 }
12196}
12197
38d518c9
EZ
12198/* Return the name of the namespace represented by DIE. Set
12199 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12200 namespace. */
12201
12202static const char *
e142c38c 12203namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12204{
12205 struct die_info *current_die;
12206 const char *name = NULL;
12207
12208 /* Loop through the extensions until we find a name. */
12209
12210 for (current_die = die;
12211 current_die != NULL;
f2f0e013 12212 current_die = dwarf2_extension (die, &cu))
38d518c9 12213 {
e142c38c 12214 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12215 if (name != NULL)
12216 break;
12217 }
12218
12219 /* Is it an anonymous namespace? */
12220
12221 *is_anonymous = (name == NULL);
12222 if (*is_anonymous)
2b1dbab0 12223 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12224
12225 return name;
d9fa45fe
DC
12226}
12227
c906108c
SS
12228/* Extract all information from a DW_TAG_pointer_type DIE and add to
12229 the user defined type vector. */
12230
f792889a 12231static struct type *
e7c27a73 12232read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12233{
5e2b427d 12234 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12235 struct comp_unit_head *cu_header = &cu->header;
c906108c 12236 struct type *type;
8b2dbe47
KB
12237 struct attribute *attr_byte_size;
12238 struct attribute *attr_address_class;
12239 int byte_size, addr_class;
7e314c57
JK
12240 struct type *target_type;
12241
12242 target_type = die_type (die, cu);
c906108c 12243
7e314c57
JK
12244 /* The die_type call above may have already set the type for this DIE. */
12245 type = get_die_type (die, cu);
12246 if (type)
12247 return type;
12248
12249 type = lookup_pointer_type (target_type);
8b2dbe47 12250
e142c38c 12251 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12252 if (attr_byte_size)
12253 byte_size = DW_UNSND (attr_byte_size);
c906108c 12254 else
8b2dbe47
KB
12255 byte_size = cu_header->addr_size;
12256
e142c38c 12257 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12258 if (attr_address_class)
12259 addr_class = DW_UNSND (attr_address_class);
12260 else
12261 addr_class = DW_ADDR_none;
12262
12263 /* If the pointer size or address class is different than the
12264 default, create a type variant marked as such and set the
12265 length accordingly. */
12266 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12267 {
5e2b427d 12268 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12269 {
12270 int type_flags;
12271
849957d9 12272 type_flags = gdbarch_address_class_type_flags
5e2b427d 12273 (gdbarch, byte_size, addr_class);
876cecd0
TT
12274 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12275 == 0);
8b2dbe47
KB
12276 type = make_type_with_address_space (type, type_flags);
12277 }
12278 else if (TYPE_LENGTH (type) != byte_size)
12279 {
3e43a32a
MS
12280 complaint (&symfile_complaints,
12281 _("invalid pointer size %d"), byte_size);
8b2dbe47 12282 }
6e70227d 12283 else
9a619af0
MS
12284 {
12285 /* Should we also complain about unhandled address classes? */
12286 }
c906108c 12287 }
8b2dbe47
KB
12288
12289 TYPE_LENGTH (type) = byte_size;
f792889a 12290 return set_die_type (die, type, cu);
c906108c
SS
12291}
12292
12293/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12294 the user defined type vector. */
12295
f792889a 12296static struct type *
e7c27a73 12297read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12298{
12299 struct type *type;
12300 struct type *to_type;
12301 struct type *domain;
12302
e7c27a73
DJ
12303 to_type = die_type (die, cu);
12304 domain = die_containing_type (die, cu);
0d5de010 12305
7e314c57
JK
12306 /* The calls above may have already set the type for this DIE. */
12307 type = get_die_type (die, cu);
12308 if (type)
12309 return type;
12310
0d5de010
DJ
12311 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12312 type = lookup_methodptr_type (to_type);
7078baeb
TT
12313 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12314 {
12315 struct type *new_type = alloc_type (cu->objfile);
12316
12317 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12318 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12319 TYPE_VARARGS (to_type));
12320 type = lookup_methodptr_type (new_type);
12321 }
0d5de010
DJ
12322 else
12323 type = lookup_memberptr_type (to_type, domain);
c906108c 12324
f792889a 12325 return set_die_type (die, type, cu);
c906108c
SS
12326}
12327
12328/* Extract all information from a DW_TAG_reference_type DIE and add to
12329 the user defined type vector. */
12330
f792889a 12331static struct type *
e7c27a73 12332read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12333{
e7c27a73 12334 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12335 struct type *type, *target_type;
c906108c
SS
12336 struct attribute *attr;
12337
7e314c57
JK
12338 target_type = die_type (die, cu);
12339
12340 /* The die_type call above may have already set the type for this DIE. */
12341 type = get_die_type (die, cu);
12342 if (type)
12343 return type;
12344
12345 type = lookup_reference_type (target_type);
e142c38c 12346 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12347 if (attr)
12348 {
12349 TYPE_LENGTH (type) = DW_UNSND (attr);
12350 }
12351 else
12352 {
107d2387 12353 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12354 }
f792889a 12355 return set_die_type (die, type, cu);
c906108c
SS
12356}
12357
f792889a 12358static struct type *
e7c27a73 12359read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12360{
f792889a 12361 struct type *base_type, *cv_type;
c906108c 12362
e7c27a73 12363 base_type = die_type (die, cu);
7e314c57
JK
12364
12365 /* The die_type call above may have already set the type for this DIE. */
12366 cv_type = get_die_type (die, cu);
12367 if (cv_type)
12368 return cv_type;
12369
2f608a3a
KW
12370 /* In case the const qualifier is applied to an array type, the element type
12371 is so qualified, not the array type (section 6.7.3 of C99). */
12372 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12373 {
12374 struct type *el_type, *inner_array;
12375
12376 base_type = copy_type (base_type);
12377 inner_array = base_type;
12378
12379 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12380 {
12381 TYPE_TARGET_TYPE (inner_array) =
12382 copy_type (TYPE_TARGET_TYPE (inner_array));
12383 inner_array = TYPE_TARGET_TYPE (inner_array);
12384 }
12385
12386 el_type = TYPE_TARGET_TYPE (inner_array);
12387 TYPE_TARGET_TYPE (inner_array) =
12388 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12389
12390 return set_die_type (die, base_type, cu);
12391 }
12392
f792889a
DJ
12393 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12394 return set_die_type (die, cv_type, cu);
c906108c
SS
12395}
12396
f792889a 12397static struct type *
e7c27a73 12398read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12399{
f792889a 12400 struct type *base_type, *cv_type;
c906108c 12401
e7c27a73 12402 base_type = die_type (die, cu);
7e314c57
JK
12403
12404 /* The die_type call above may have already set the type for this DIE. */
12405 cv_type = get_die_type (die, cu);
12406 if (cv_type)
12407 return cv_type;
12408
f792889a
DJ
12409 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12410 return set_die_type (die, cv_type, cu);
c906108c
SS
12411}
12412
06d66ee9
TT
12413/* Handle DW_TAG_restrict_type. */
12414
12415static struct type *
12416read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12417{
12418 struct type *base_type, *cv_type;
12419
12420 base_type = die_type (die, cu);
12421
12422 /* The die_type call above may have already set the type for this DIE. */
12423 cv_type = get_die_type (die, cu);
12424 if (cv_type)
12425 return cv_type;
12426
12427 cv_type = make_restrict_type (base_type);
12428 return set_die_type (die, cv_type, cu);
12429}
12430
c906108c
SS
12431/* Extract all information from a DW_TAG_string_type DIE and add to
12432 the user defined type vector. It isn't really a user defined type,
12433 but it behaves like one, with other DIE's using an AT_user_def_type
12434 attribute to reference it. */
12435
f792889a 12436static struct type *
e7c27a73 12437read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12438{
e7c27a73 12439 struct objfile *objfile = cu->objfile;
3b7538c0 12440 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12441 struct type *type, *range_type, *index_type, *char_type;
12442 struct attribute *attr;
12443 unsigned int length;
12444
e142c38c 12445 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12446 if (attr)
12447 {
12448 length = DW_UNSND (attr);
12449 }
12450 else
12451 {
0963b4bd 12452 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12453 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12454 if (attr)
12455 {
12456 length = DW_UNSND (attr);
12457 }
12458 else
12459 {
12460 length = 1;
12461 }
c906108c 12462 }
6ccb9162 12463
46bf5051 12464 index_type = objfile_type (objfile)->builtin_int;
c906108c 12465 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12466 char_type = language_string_char_type (cu->language_defn, gdbarch);
12467 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12468
f792889a 12469 return set_die_type (die, type, cu);
c906108c
SS
12470}
12471
12472/* Handle DIES due to C code like:
12473
12474 struct foo
c5aa993b
JM
12475 {
12476 int (*funcp)(int a, long l);
12477 int b;
12478 };
c906108c 12479
0963b4bd 12480 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12481
f792889a 12482static struct type *
e7c27a73 12483read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12484{
bb5ed363 12485 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12486 struct type *type; /* Type that this function returns. */
12487 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12488 struct attribute *attr;
12489
e7c27a73 12490 type = die_type (die, cu);
7e314c57
JK
12491
12492 /* The die_type call above may have already set the type for this DIE. */
12493 ftype = get_die_type (die, cu);
12494 if (ftype)
12495 return ftype;
12496
0c8b41f1 12497 ftype = lookup_function_type (type);
c906108c 12498
5b8101ae 12499 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12500 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12501 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12502 || cu->language == language_cplus
5b8101ae
PM
12503 || cu->language == language_java
12504 || cu->language == language_pascal)
876cecd0 12505 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12506 else if (producer_is_realview (cu->producer))
12507 /* RealView does not emit DW_AT_prototyped. We can not
12508 distinguish prototyped and unprototyped functions; default to
12509 prototyped, since that is more common in modern code (and
12510 RealView warns about unprototyped functions). */
12511 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12512
c055b101
CV
12513 /* Store the calling convention in the type if it's available in
12514 the subroutine die. Otherwise set the calling convention to
12515 the default value DW_CC_normal. */
12516 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12517 if (attr)
12518 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12519 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12520 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12521 else
12522 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12523
12524 /* We need to add the subroutine type to the die immediately so
12525 we don't infinitely recurse when dealing with parameters
0963b4bd 12526 declared as the same subroutine type. */
76c10ea2 12527 set_die_type (die, ftype, cu);
6e70227d 12528
639d11d3 12529 if (die->child != NULL)
c906108c 12530 {
bb5ed363 12531 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12532 struct die_info *child_die;
8072405b 12533 int nparams, iparams;
c906108c
SS
12534
12535 /* Count the number of parameters.
12536 FIXME: GDB currently ignores vararg functions, but knows about
12537 vararg member functions. */
8072405b 12538 nparams = 0;
639d11d3 12539 child_die = die->child;
c906108c
SS
12540 while (child_die && child_die->tag)
12541 {
12542 if (child_die->tag == DW_TAG_formal_parameter)
12543 nparams++;
12544 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12545 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12546 child_die = sibling_die (child_die);
12547 }
12548
12549 /* Allocate storage for parameters and fill them in. */
12550 TYPE_NFIELDS (ftype) = nparams;
12551 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12552 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12553
8072405b
JK
12554 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12555 even if we error out during the parameters reading below. */
12556 for (iparams = 0; iparams < nparams; iparams++)
12557 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12558
12559 iparams = 0;
639d11d3 12560 child_die = die->child;
c906108c
SS
12561 while (child_die && child_die->tag)
12562 {
12563 if (child_die->tag == DW_TAG_formal_parameter)
12564 {
3ce3b1ba
PA
12565 struct type *arg_type;
12566
12567 /* DWARF version 2 has no clean way to discern C++
12568 static and non-static member functions. G++ helps
12569 GDB by marking the first parameter for non-static
12570 member functions (which is the this pointer) as
12571 artificial. We pass this information to
12572 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12573
12574 DWARF version 3 added DW_AT_object_pointer, which GCC
12575 4.5 does not yet generate. */
e142c38c 12576 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12577 if (attr)
12578 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12579 else
418835cc
KS
12580 {
12581 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12582
12583 /* GCC/43521: In java, the formal parameter
12584 "this" is sometimes not marked with DW_AT_artificial. */
12585 if (cu->language == language_java)
12586 {
12587 const char *name = dwarf2_name (child_die, cu);
9a619af0 12588
418835cc
KS
12589 if (name && !strcmp (name, "this"))
12590 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12591 }
12592 }
3ce3b1ba
PA
12593 arg_type = die_type (child_die, cu);
12594
12595 /* RealView does not mark THIS as const, which the testsuite
12596 expects. GCC marks THIS as const in method definitions,
12597 but not in the class specifications (GCC PR 43053). */
12598 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12599 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12600 {
12601 int is_this = 0;
12602 struct dwarf2_cu *arg_cu = cu;
12603 const char *name = dwarf2_name (child_die, cu);
12604
12605 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12606 if (attr)
12607 {
12608 /* If the compiler emits this, use it. */
12609 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12610 is_this = 1;
12611 }
12612 else if (name && strcmp (name, "this") == 0)
12613 /* Function definitions will have the argument names. */
12614 is_this = 1;
12615 else if (name == NULL && iparams == 0)
12616 /* Declarations may not have the names, so like
12617 elsewhere in GDB, assume an artificial first
12618 argument is "this". */
12619 is_this = 1;
12620
12621 if (is_this)
12622 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12623 arg_type, 0);
12624 }
12625
12626 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12627 iparams++;
12628 }
12629 child_die = sibling_die (child_die);
12630 }
12631 }
12632
76c10ea2 12633 return ftype;
c906108c
SS
12634}
12635
f792889a 12636static struct type *
e7c27a73 12637read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12638{
e7c27a73 12639 struct objfile *objfile = cu->objfile;
0114d602 12640 const char *name = NULL;
3c8e0968 12641 struct type *this_type, *target_type;
c906108c 12642
94af9270 12643 name = dwarf2_full_name (NULL, die, cu);
f792889a 12644 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12645 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12646 TYPE_NAME (this_type) = name;
f792889a 12647 set_die_type (die, this_type, cu);
3c8e0968
DE
12648 target_type = die_type (die, cu);
12649 if (target_type != this_type)
12650 TYPE_TARGET_TYPE (this_type) = target_type;
12651 else
12652 {
12653 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12654 spec and cause infinite loops in GDB. */
12655 complaint (&symfile_complaints,
12656 _("Self-referential DW_TAG_typedef "
12657 "- DIE at 0x%x [in module %s]"),
b64f50a1 12658 die->offset.sect_off, objfile->name);
3c8e0968
DE
12659 TYPE_TARGET_TYPE (this_type) = NULL;
12660 }
f792889a 12661 return this_type;
c906108c
SS
12662}
12663
12664/* Find a representation of a given base type and install
12665 it in the TYPE field of the die. */
12666
f792889a 12667static struct type *
e7c27a73 12668read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12669{
e7c27a73 12670 struct objfile *objfile = cu->objfile;
c906108c
SS
12671 struct type *type;
12672 struct attribute *attr;
12673 int encoding = 0, size = 0;
15d034d0 12674 const char *name;
6ccb9162
UW
12675 enum type_code code = TYPE_CODE_INT;
12676 int type_flags = 0;
12677 struct type *target_type = NULL;
c906108c 12678
e142c38c 12679 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12680 if (attr)
12681 {
12682 encoding = DW_UNSND (attr);
12683 }
e142c38c 12684 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12685 if (attr)
12686 {
12687 size = DW_UNSND (attr);
12688 }
39cbfefa 12689 name = dwarf2_name (die, cu);
6ccb9162 12690 if (!name)
c906108c 12691 {
6ccb9162
UW
12692 complaint (&symfile_complaints,
12693 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12694 }
6ccb9162
UW
12695
12696 switch (encoding)
c906108c 12697 {
6ccb9162
UW
12698 case DW_ATE_address:
12699 /* Turn DW_ATE_address into a void * pointer. */
12700 code = TYPE_CODE_PTR;
12701 type_flags |= TYPE_FLAG_UNSIGNED;
12702 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12703 break;
12704 case DW_ATE_boolean:
12705 code = TYPE_CODE_BOOL;
12706 type_flags |= TYPE_FLAG_UNSIGNED;
12707 break;
12708 case DW_ATE_complex_float:
12709 code = TYPE_CODE_COMPLEX;
12710 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12711 break;
12712 case DW_ATE_decimal_float:
12713 code = TYPE_CODE_DECFLOAT;
12714 break;
12715 case DW_ATE_float:
12716 code = TYPE_CODE_FLT;
12717 break;
12718 case DW_ATE_signed:
12719 break;
12720 case DW_ATE_unsigned:
12721 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12722 if (cu->language == language_fortran
12723 && name
12724 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12725 code = TYPE_CODE_CHAR;
6ccb9162
UW
12726 break;
12727 case DW_ATE_signed_char:
6e70227d 12728 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12729 || cu->language == language_pascal
12730 || cu->language == language_fortran)
6ccb9162
UW
12731 code = TYPE_CODE_CHAR;
12732 break;
12733 case DW_ATE_unsigned_char:
868a0084 12734 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12735 || cu->language == language_pascal
12736 || cu->language == language_fortran)
6ccb9162
UW
12737 code = TYPE_CODE_CHAR;
12738 type_flags |= TYPE_FLAG_UNSIGNED;
12739 break;
75079b2b
TT
12740 case DW_ATE_UTF:
12741 /* We just treat this as an integer and then recognize the
12742 type by name elsewhere. */
12743 break;
12744
6ccb9162
UW
12745 default:
12746 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12747 dwarf_type_encoding_name (encoding));
12748 break;
c906108c 12749 }
6ccb9162 12750
0114d602
DJ
12751 type = init_type (code, size, type_flags, NULL, objfile);
12752 TYPE_NAME (type) = name;
6ccb9162
UW
12753 TYPE_TARGET_TYPE (type) = target_type;
12754
0114d602 12755 if (name && strcmp (name, "char") == 0)
876cecd0 12756 TYPE_NOSIGN (type) = 1;
0114d602 12757
f792889a 12758 return set_die_type (die, type, cu);
c906108c
SS
12759}
12760
a02abb62
JB
12761/* Read the given DW_AT_subrange DIE. */
12762
f792889a 12763static struct type *
a02abb62
JB
12764read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12765{
4c9ad8c2 12766 struct type *base_type, *orig_base_type;
a02abb62
JB
12767 struct type *range_type;
12768 struct attribute *attr;
4fae6e18
JK
12769 LONGEST low, high;
12770 int low_default_is_valid;
15d034d0 12771 const char *name;
43bbcdc2 12772 LONGEST negative_mask;
e77813c8 12773
4c9ad8c2
TT
12774 orig_base_type = die_type (die, cu);
12775 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12776 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12777 creating the range type, but we use the result of check_typedef
12778 when examining properties of the type. */
12779 base_type = check_typedef (orig_base_type);
a02abb62 12780
7e314c57
JK
12781 /* The die_type call above may have already set the type for this DIE. */
12782 range_type = get_die_type (die, cu);
12783 if (range_type)
12784 return range_type;
12785
4fae6e18
JK
12786 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12787 omitting DW_AT_lower_bound. */
12788 switch (cu->language)
6e70227d 12789 {
4fae6e18
JK
12790 case language_c:
12791 case language_cplus:
12792 low = 0;
12793 low_default_is_valid = 1;
12794 break;
12795 case language_fortran:
12796 low = 1;
12797 low_default_is_valid = 1;
12798 break;
12799 case language_d:
12800 case language_java:
12801 case language_objc:
12802 low = 0;
12803 low_default_is_valid = (cu->header.version >= 4);
12804 break;
12805 case language_ada:
12806 case language_m2:
12807 case language_pascal:
a02abb62 12808 low = 1;
4fae6e18
JK
12809 low_default_is_valid = (cu->header.version >= 4);
12810 break;
12811 default:
12812 low = 0;
12813 low_default_is_valid = 0;
12814 break;
a02abb62
JB
12815 }
12816
dd5e6932
DJ
12817 /* FIXME: For variable sized arrays either of these could be
12818 a variable rather than a constant value. We'll allow it,
12819 but we don't know how to handle it. */
e142c38c 12820 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12821 if (attr)
4fae6e18
JK
12822 low = dwarf2_get_attr_constant_value (attr, low);
12823 else if (!low_default_is_valid)
12824 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12825 "- DIE at 0x%x [in module %s]"),
12826 die->offset.sect_off, cu->objfile->name);
a02abb62 12827
e142c38c 12828 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12829 if (attr)
6e70227d 12830 {
d48323d8 12831 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12832 {
12833 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12834 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12835 FIXME: GDB does not yet know how to handle dynamic
12836 arrays properly, treat them as arrays with unspecified
12837 length for now.
12838
12839 FIXME: jimb/2003-09-22: GDB does not really know
12840 how to handle arrays of unspecified length
12841 either; we just represent them as zero-length
12842 arrays. Choose an appropriate upper bound given
12843 the lower bound we've computed above. */
12844 high = low - 1;
12845 }
12846 else
12847 high = dwarf2_get_attr_constant_value (attr, 1);
12848 }
e77813c8
PM
12849 else
12850 {
12851 attr = dwarf2_attr (die, DW_AT_count, cu);
12852 if (attr)
12853 {
12854 int count = dwarf2_get_attr_constant_value (attr, 1);
12855 high = low + count - 1;
12856 }
c2ff108b
JK
12857 else
12858 {
12859 /* Unspecified array length. */
12860 high = low - 1;
12861 }
e77813c8
PM
12862 }
12863
12864 /* Dwarf-2 specifications explicitly allows to create subrange types
12865 without specifying a base type.
12866 In that case, the base type must be set to the type of
12867 the lower bound, upper bound or count, in that order, if any of these
12868 three attributes references an object that has a type.
12869 If no base type is found, the Dwarf-2 specifications say that
12870 a signed integer type of size equal to the size of an address should
12871 be used.
12872 For the following C code: `extern char gdb_int [];'
12873 GCC produces an empty range DIE.
12874 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12875 high bound or count are not yet handled by this code. */
e77813c8
PM
12876 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12877 {
12878 struct objfile *objfile = cu->objfile;
12879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12880 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12881 struct type *int_type = objfile_type (objfile)->builtin_int;
12882
12883 /* Test "int", "long int", and "long long int" objfile types,
12884 and select the first one having a size above or equal to the
12885 architecture address size. */
12886 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12887 base_type = int_type;
12888 else
12889 {
12890 int_type = objfile_type (objfile)->builtin_long;
12891 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12892 base_type = int_type;
12893 else
12894 {
12895 int_type = objfile_type (objfile)->builtin_long_long;
12896 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12897 base_type = int_type;
12898 }
12899 }
12900 }
a02abb62 12901
6e70227d 12902 negative_mask =
43bbcdc2
PH
12903 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12904 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12905 low |= negative_mask;
12906 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12907 high |= negative_mask;
12908
4c9ad8c2 12909 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 12910
bbb0eef6
JK
12911 /* Mark arrays with dynamic length at least as an array of unspecified
12912 length. GDB could check the boundary but before it gets implemented at
12913 least allow accessing the array elements. */
d48323d8 12914 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12915 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12916
c2ff108b
JK
12917 /* Ada expects an empty array on no boundary attributes. */
12918 if (attr == NULL && cu->language != language_ada)
12919 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12920
39cbfefa
DJ
12921 name = dwarf2_name (die, cu);
12922 if (name)
12923 TYPE_NAME (range_type) = name;
6e70227d 12924
e142c38c 12925 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12926 if (attr)
12927 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12928
7e314c57
JK
12929 set_die_type (die, range_type, cu);
12930
12931 /* set_die_type should be already done. */
b4ba55a1
JB
12932 set_descriptive_type (range_type, die, cu);
12933
7e314c57 12934 return range_type;
a02abb62 12935}
6e70227d 12936
f792889a 12937static struct type *
81a17f79
JB
12938read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12939{
12940 struct type *type;
81a17f79 12941
81a17f79
JB
12942 /* For now, we only support the C meaning of an unspecified type: void. */
12943
0114d602
DJ
12944 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12945 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12946
f792889a 12947 return set_die_type (die, type, cu);
81a17f79 12948}
a02abb62 12949
639d11d3
DC
12950/* Read a single die and all its descendents. Set the die's sibling
12951 field to NULL; set other fields in the die correctly, and set all
12952 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12953 location of the info_ptr after reading all of those dies. PARENT
12954 is the parent of the die in question. */
12955
12956static struct die_info *
dee91e82
DE
12957read_die_and_children (const struct die_reader_specs *reader,
12958 gdb_byte *info_ptr,
12959 gdb_byte **new_info_ptr,
12960 struct die_info *parent)
639d11d3
DC
12961{
12962 struct die_info *die;
fe1b8b76 12963 gdb_byte *cur_ptr;
639d11d3
DC
12964 int has_children;
12965
93311388 12966 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12967 if (die == NULL)
12968 {
12969 *new_info_ptr = cur_ptr;
12970 return NULL;
12971 }
93311388 12972 store_in_ref_table (die, reader->cu);
639d11d3
DC
12973
12974 if (has_children)
348e048f 12975 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12976 else
12977 {
12978 die->child = NULL;
12979 *new_info_ptr = cur_ptr;
12980 }
12981
12982 die->sibling = NULL;
12983 die->parent = parent;
12984 return die;
12985}
12986
12987/* Read a die, all of its descendents, and all of its siblings; set
12988 all of the fields of all of the dies correctly. Arguments are as
12989 in read_die_and_children. */
12990
12991static struct die_info *
93311388
DE
12992read_die_and_siblings (const struct die_reader_specs *reader,
12993 gdb_byte *info_ptr,
fe1b8b76 12994 gdb_byte **new_info_ptr,
639d11d3
DC
12995 struct die_info *parent)
12996{
12997 struct die_info *first_die, *last_sibling;
fe1b8b76 12998 gdb_byte *cur_ptr;
639d11d3 12999
c906108c 13000 cur_ptr = info_ptr;
639d11d3
DC
13001 first_die = last_sibling = NULL;
13002
13003 while (1)
c906108c 13004 {
639d11d3 13005 struct die_info *die
dee91e82 13006 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13007
1d325ec1 13008 if (die == NULL)
c906108c 13009 {
639d11d3
DC
13010 *new_info_ptr = cur_ptr;
13011 return first_die;
c906108c 13012 }
1d325ec1
DJ
13013
13014 if (!first_die)
13015 first_die = die;
c906108c 13016 else
1d325ec1
DJ
13017 last_sibling->sibling = die;
13018
13019 last_sibling = die;
c906108c 13020 }
c906108c
SS
13021}
13022
3019eac3
DE
13023/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13024 attributes.
13025 The caller is responsible for filling in the extra attributes
13026 and updating (*DIEP)->num_attrs.
13027 Set DIEP to point to a newly allocated die with its information,
13028 except for its child, sibling, and parent fields.
13029 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
13030
13031static gdb_byte *
3019eac3
DE
13032read_full_die_1 (const struct die_reader_specs *reader,
13033 struct die_info **diep, gdb_byte *info_ptr,
13034 int *has_children, int num_extra_attrs)
93311388 13035{
b64f50a1
JK
13036 unsigned int abbrev_number, bytes_read, i;
13037 sect_offset offset;
93311388
DE
13038 struct abbrev_info *abbrev;
13039 struct die_info *die;
13040 struct dwarf2_cu *cu = reader->cu;
13041 bfd *abfd = reader->abfd;
13042
b64f50a1 13043 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13044 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13045 info_ptr += bytes_read;
13046 if (!abbrev_number)
13047 {
13048 *diep = NULL;
13049 *has_children = 0;
13050 return info_ptr;
13051 }
13052
433df2d4 13053 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13054 if (!abbrev)
348e048f
DE
13055 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13056 abbrev_number,
13057 bfd_get_filename (abfd));
13058
3019eac3 13059 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13060 die->offset = offset;
13061 die->tag = abbrev->tag;
13062 die->abbrev = abbrev_number;
13063
3019eac3
DE
13064 /* Make the result usable.
13065 The caller needs to update num_attrs after adding the extra
13066 attributes. */
93311388
DE
13067 die->num_attrs = abbrev->num_attrs;
13068
13069 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13070 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13071 info_ptr);
93311388
DE
13072
13073 *diep = die;
13074 *has_children = abbrev->has_children;
13075 return info_ptr;
13076}
13077
3019eac3
DE
13078/* Read a die and all its attributes.
13079 Set DIEP to point to a newly allocated die with its information,
13080 except for its child, sibling, and parent fields.
13081 Set HAS_CHILDREN to tell whether the die has children or not. */
13082
13083static gdb_byte *
13084read_full_die (const struct die_reader_specs *reader,
13085 struct die_info **diep, gdb_byte *info_ptr,
13086 int *has_children)
13087{
13088 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13089}
433df2d4
DE
13090\f
13091/* Abbreviation tables.
3019eac3 13092
433df2d4 13093 In DWARF version 2, the description of the debugging information is
c906108c
SS
13094 stored in a separate .debug_abbrev section. Before we read any
13095 dies from a section we read in all abbreviations and install them
433df2d4
DE
13096 in a hash table. */
13097
13098/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13099
13100static struct abbrev_info *
13101abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13102{
13103 struct abbrev_info *abbrev;
13104
13105 abbrev = (struct abbrev_info *)
13106 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13107 memset (abbrev, 0, sizeof (struct abbrev_info));
13108 return abbrev;
13109}
13110
13111/* Add an abbreviation to the table. */
c906108c
SS
13112
13113static void
433df2d4
DE
13114abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13115 unsigned int abbrev_number,
13116 struct abbrev_info *abbrev)
13117{
13118 unsigned int hash_number;
13119
13120 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13121 abbrev->next = abbrev_table->abbrevs[hash_number];
13122 abbrev_table->abbrevs[hash_number] = abbrev;
13123}
dee91e82 13124
433df2d4
DE
13125/* Look up an abbrev in the table.
13126 Returns NULL if the abbrev is not found. */
13127
13128static struct abbrev_info *
13129abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13130 unsigned int abbrev_number)
c906108c 13131{
433df2d4
DE
13132 unsigned int hash_number;
13133 struct abbrev_info *abbrev;
13134
13135 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13136 abbrev = abbrev_table->abbrevs[hash_number];
13137
13138 while (abbrev)
13139 {
13140 if (abbrev->number == abbrev_number)
13141 return abbrev;
13142 abbrev = abbrev->next;
13143 }
13144 return NULL;
13145}
13146
13147/* Read in an abbrev table. */
13148
13149static struct abbrev_table *
13150abbrev_table_read_table (struct dwarf2_section_info *section,
13151 sect_offset offset)
13152{
13153 struct objfile *objfile = dwarf2_per_objfile->objfile;
13154 bfd *abfd = section->asection->owner;
13155 struct abbrev_table *abbrev_table;
fe1b8b76 13156 gdb_byte *abbrev_ptr;
c906108c
SS
13157 struct abbrev_info *cur_abbrev;
13158 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13159 unsigned int abbrev_form;
f3dd6933
DJ
13160 struct attr_abbrev *cur_attrs;
13161 unsigned int allocated_attrs;
c906108c 13162
433df2d4 13163 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13164 abbrev_table->offset = offset;
433df2d4
DE
13165 obstack_init (&abbrev_table->abbrev_obstack);
13166 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13167 (ABBREV_HASH_SIZE
13168 * sizeof (struct abbrev_info *)));
13169 memset (abbrev_table->abbrevs, 0,
13170 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13171
433df2d4
DE
13172 dwarf2_read_section (objfile, section);
13173 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13174 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13175 abbrev_ptr += bytes_read;
13176
f3dd6933
DJ
13177 allocated_attrs = ATTR_ALLOC_CHUNK;
13178 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13179
0963b4bd 13180 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13181 while (abbrev_number)
13182 {
433df2d4 13183 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13184
13185 /* read in abbrev header */
13186 cur_abbrev->number = abbrev_number;
13187 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13188 abbrev_ptr += bytes_read;
13189 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13190 abbrev_ptr += 1;
13191
13192 /* now read in declarations */
13193 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13194 abbrev_ptr += bytes_read;
13195 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13196 abbrev_ptr += bytes_read;
13197 while (abbrev_name)
13198 {
f3dd6933 13199 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13200 {
f3dd6933
DJ
13201 allocated_attrs += ATTR_ALLOC_CHUNK;
13202 cur_attrs
13203 = xrealloc (cur_attrs, (allocated_attrs
13204 * sizeof (struct attr_abbrev)));
c906108c 13205 }
ae038cb0 13206
f3dd6933
DJ
13207 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13208 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13209 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13210 abbrev_ptr += bytes_read;
13211 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13212 abbrev_ptr += bytes_read;
13213 }
13214
433df2d4 13215 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13216 (cur_abbrev->num_attrs
13217 * sizeof (struct attr_abbrev)));
13218 memcpy (cur_abbrev->attrs, cur_attrs,
13219 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13220
433df2d4 13221 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13222
13223 /* Get next abbreviation.
13224 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13225 always properly terminated with an abbrev number of 0.
13226 Exit loop if we encounter an abbreviation which we have
13227 already read (which means we are about to read the abbreviations
13228 for the next compile unit) or if the end of the abbreviation
13229 table is reached. */
433df2d4 13230 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13231 break;
13232 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13233 abbrev_ptr += bytes_read;
433df2d4 13234 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13235 break;
13236 }
f3dd6933
DJ
13237
13238 xfree (cur_attrs);
433df2d4 13239 return abbrev_table;
c906108c
SS
13240}
13241
433df2d4 13242/* Free the resources held by ABBREV_TABLE. */
c906108c 13243
c906108c 13244static void
433df2d4 13245abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13246{
433df2d4
DE
13247 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13248 xfree (abbrev_table);
c906108c
SS
13249}
13250
f4dc4d17
DE
13251/* Same as abbrev_table_free but as a cleanup.
13252 We pass in a pointer to the pointer to the table so that we can
13253 set the pointer to NULL when we're done. It also simplifies
13254 build_type_unit_groups. */
13255
13256static void
13257abbrev_table_free_cleanup (void *table_ptr)
13258{
13259 struct abbrev_table **abbrev_table_ptr = table_ptr;
13260
13261 if (*abbrev_table_ptr != NULL)
13262 abbrev_table_free (*abbrev_table_ptr);
13263 *abbrev_table_ptr = NULL;
13264}
13265
433df2d4
DE
13266/* Read the abbrev table for CU from ABBREV_SECTION. */
13267
13268static void
13269dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13270 struct dwarf2_section_info *abbrev_section)
c906108c 13271{
433df2d4
DE
13272 cu->abbrev_table =
13273 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13274}
c906108c 13275
433df2d4 13276/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13277
433df2d4
DE
13278static void
13279dwarf2_free_abbrev_table (void *ptr_to_cu)
13280{
13281 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13282
433df2d4
DE
13283 abbrev_table_free (cu->abbrev_table);
13284 /* Set this to NULL so that we SEGV if we try to read it later,
13285 and also because free_comp_unit verifies this is NULL. */
13286 cu->abbrev_table = NULL;
13287}
13288\f
72bf9492
DJ
13289/* Returns nonzero if TAG represents a type that we might generate a partial
13290 symbol for. */
13291
13292static int
13293is_type_tag_for_partial (int tag)
13294{
13295 switch (tag)
13296 {
13297#if 0
13298 /* Some types that would be reasonable to generate partial symbols for,
13299 that we don't at present. */
13300 case DW_TAG_array_type:
13301 case DW_TAG_file_type:
13302 case DW_TAG_ptr_to_member_type:
13303 case DW_TAG_set_type:
13304 case DW_TAG_string_type:
13305 case DW_TAG_subroutine_type:
13306#endif
13307 case DW_TAG_base_type:
13308 case DW_TAG_class_type:
680b30c7 13309 case DW_TAG_interface_type:
72bf9492
DJ
13310 case DW_TAG_enumeration_type:
13311 case DW_TAG_structure_type:
13312 case DW_TAG_subrange_type:
13313 case DW_TAG_typedef:
13314 case DW_TAG_union_type:
13315 return 1;
13316 default:
13317 return 0;
13318 }
13319}
13320
13321/* Load all DIEs that are interesting for partial symbols into memory. */
13322
13323static struct partial_die_info *
dee91e82
DE
13324load_partial_dies (const struct die_reader_specs *reader,
13325 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13326{
dee91e82 13327 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13328 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13329 struct partial_die_info *part_die;
13330 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13331 struct abbrev_info *abbrev;
13332 unsigned int bytes_read;
5afb4e99 13333 unsigned int load_all = 0;
72bf9492
DJ
13334 int nesting_level = 1;
13335
13336 parent_die = NULL;
13337 last_die = NULL;
13338
7adf1e79
DE
13339 gdb_assert (cu->per_cu != NULL);
13340 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13341 load_all = 1;
13342
72bf9492
DJ
13343 cu->partial_dies
13344 = htab_create_alloc_ex (cu->header.length / 12,
13345 partial_die_hash,
13346 partial_die_eq,
13347 NULL,
13348 &cu->comp_unit_obstack,
13349 hashtab_obstack_allocate,
13350 dummy_obstack_deallocate);
13351
13352 part_die = obstack_alloc (&cu->comp_unit_obstack,
13353 sizeof (struct partial_die_info));
13354
13355 while (1)
13356 {
13357 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13358
13359 /* A NULL abbrev means the end of a series of children. */
13360 if (abbrev == NULL)
13361 {
13362 if (--nesting_level == 0)
13363 {
13364 /* PART_DIE was probably the last thing allocated on the
13365 comp_unit_obstack, so we could call obstack_free
13366 here. We don't do that because the waste is small,
13367 and will be cleaned up when we're done with this
13368 compilation unit. This way, we're also more robust
13369 against other users of the comp_unit_obstack. */
13370 return first_die;
13371 }
13372 info_ptr += bytes_read;
13373 last_die = parent_die;
13374 parent_die = parent_die->die_parent;
13375 continue;
13376 }
13377
98bfdba5
PA
13378 /* Check for template arguments. We never save these; if
13379 they're seen, we just mark the parent, and go on our way. */
13380 if (parent_die != NULL
13381 && cu->language == language_cplus
13382 && (abbrev->tag == DW_TAG_template_type_param
13383 || abbrev->tag == DW_TAG_template_value_param))
13384 {
13385 parent_die->has_template_arguments = 1;
13386
13387 if (!load_all)
13388 {
13389 /* We don't need a partial DIE for the template argument. */
dee91e82 13390 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13391 continue;
13392 }
13393 }
13394
0d99eb77 13395 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13396 Skip their other children. */
13397 if (!load_all
13398 && cu->language == language_cplus
13399 && parent_die != NULL
13400 && parent_die->tag == DW_TAG_subprogram)
13401 {
dee91e82 13402 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13403 continue;
13404 }
13405
5afb4e99
DJ
13406 /* Check whether this DIE is interesting enough to save. Normally
13407 we would not be interested in members here, but there may be
13408 later variables referencing them via DW_AT_specification (for
13409 static members). */
13410 if (!load_all
13411 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13412 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13413 && abbrev->tag != DW_TAG_enumerator
13414 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13415 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13416 && abbrev->tag != DW_TAG_variable
5afb4e99 13417 && abbrev->tag != DW_TAG_namespace
f55ee35c 13418 && abbrev->tag != DW_TAG_module
95554aad
TT
13419 && abbrev->tag != DW_TAG_member
13420 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13421 {
13422 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13423 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13424 continue;
13425 }
13426
dee91e82
DE
13427 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13428 info_ptr);
72bf9492
DJ
13429
13430 /* This two-pass algorithm for processing partial symbols has a
13431 high cost in cache pressure. Thus, handle some simple cases
13432 here which cover the majority of C partial symbols. DIEs
13433 which neither have specification tags in them, nor could have
13434 specification tags elsewhere pointing at them, can simply be
13435 processed and discarded.
13436
13437 This segment is also optional; scan_partial_symbols and
13438 add_partial_symbol will handle these DIEs if we chain
13439 them in normally. When compilers which do not emit large
13440 quantities of duplicate debug information are more common,
13441 this code can probably be removed. */
13442
13443 /* Any complete simple types at the top level (pretty much all
13444 of them, for a language without namespaces), can be processed
13445 directly. */
13446 if (parent_die == NULL
13447 && part_die->has_specification == 0
13448 && part_die->is_declaration == 0
d8228535 13449 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13450 || part_die->tag == DW_TAG_base_type
13451 || part_die->tag == DW_TAG_subrange_type))
13452 {
13453 if (building_psymtab && part_die->name != NULL)
04a679b8 13454 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13455 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13456 &objfile->static_psymbols,
13457 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13458 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13459 continue;
13460 }
13461
d8228535
JK
13462 /* The exception for DW_TAG_typedef with has_children above is
13463 a workaround of GCC PR debug/47510. In the case of this complaint
13464 type_name_no_tag_or_error will error on such types later.
13465
13466 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13467 it could not find the child DIEs referenced later, this is checked
13468 above. In correct DWARF DW_TAG_typedef should have no children. */
13469
13470 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13471 complaint (&symfile_complaints,
13472 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13473 "- DIE at 0x%x [in module %s]"),
b64f50a1 13474 part_die->offset.sect_off, objfile->name);
d8228535 13475
72bf9492
DJ
13476 /* If we're at the second level, and we're an enumerator, and
13477 our parent has no specification (meaning possibly lives in a
13478 namespace elsewhere), then we can add the partial symbol now
13479 instead of queueing it. */
13480 if (part_die->tag == DW_TAG_enumerator
13481 && parent_die != NULL
13482 && parent_die->die_parent == NULL
13483 && parent_die->tag == DW_TAG_enumeration_type
13484 && parent_die->has_specification == 0)
13485 {
13486 if (part_die->name == NULL)
3e43a32a
MS
13487 complaint (&symfile_complaints,
13488 _("malformed enumerator DIE ignored"));
72bf9492 13489 else if (building_psymtab)
04a679b8 13490 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13491 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13492 (cu->language == language_cplus
13493 || cu->language == language_java)
bb5ed363
DE
13494 ? &objfile->global_psymbols
13495 : &objfile->static_psymbols,
13496 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13497
dee91e82 13498 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13499 continue;
13500 }
13501
13502 /* We'll save this DIE so link it in. */
13503 part_die->die_parent = parent_die;
13504 part_die->die_sibling = NULL;
13505 part_die->die_child = NULL;
13506
13507 if (last_die && last_die == parent_die)
13508 last_die->die_child = part_die;
13509 else if (last_die)
13510 last_die->die_sibling = part_die;
13511
13512 last_die = part_die;
13513
13514 if (first_die == NULL)
13515 first_die = part_die;
13516
13517 /* Maybe add the DIE to the hash table. Not all DIEs that we
13518 find interesting need to be in the hash table, because we
13519 also have the parent/sibling/child chains; only those that we
13520 might refer to by offset later during partial symbol reading.
13521
13522 For now this means things that might have be the target of a
13523 DW_AT_specification, DW_AT_abstract_origin, or
13524 DW_AT_extension. DW_AT_extension will refer only to
13525 namespaces; DW_AT_abstract_origin refers to functions (and
13526 many things under the function DIE, but we do not recurse
13527 into function DIEs during partial symbol reading) and
13528 possibly variables as well; DW_AT_specification refers to
13529 declarations. Declarations ought to have the DW_AT_declaration
13530 flag. It happens that GCC forgets to put it in sometimes, but
13531 only for functions, not for types.
13532
13533 Adding more things than necessary to the hash table is harmless
13534 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13535 wasted time in find_partial_die, when we reread the compilation
13536 unit with load_all_dies set. */
72bf9492 13537
5afb4e99 13538 if (load_all
72929c62 13539 || abbrev->tag == DW_TAG_constant
5afb4e99 13540 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13541 || abbrev->tag == DW_TAG_variable
13542 || abbrev->tag == DW_TAG_namespace
13543 || part_die->is_declaration)
13544 {
13545 void **slot;
13546
13547 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13548 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13549 *slot = part_die;
13550 }
13551
13552 part_die = obstack_alloc (&cu->comp_unit_obstack,
13553 sizeof (struct partial_die_info));
13554
13555 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13556 we have no reason to follow the children of structures; for other
98bfdba5
PA
13557 languages we have to, so that we can get at method physnames
13558 to infer fully qualified class names, for DW_AT_specification,
13559 and for C++ template arguments. For C++, we also look one level
13560 inside functions to find template arguments (if the name of the
13561 function does not already contain the template arguments).
bc30ff58
JB
13562
13563 For Ada, we need to scan the children of subprograms and lexical
13564 blocks as well because Ada allows the definition of nested
13565 entities that could be interesting for the debugger, such as
13566 nested subprograms for instance. */
72bf9492 13567 if (last_die->has_children
5afb4e99
DJ
13568 && (load_all
13569 || last_die->tag == DW_TAG_namespace
f55ee35c 13570 || last_die->tag == DW_TAG_module
72bf9492 13571 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13572 || (cu->language == language_cplus
13573 && last_die->tag == DW_TAG_subprogram
13574 && (last_die->name == NULL
13575 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13576 || (cu->language != language_c
13577 && (last_die->tag == DW_TAG_class_type
680b30c7 13578 || last_die->tag == DW_TAG_interface_type
72bf9492 13579 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13580 || last_die->tag == DW_TAG_union_type))
13581 || (cu->language == language_ada
13582 && (last_die->tag == DW_TAG_subprogram
13583 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13584 {
13585 nesting_level++;
13586 parent_die = last_die;
13587 continue;
13588 }
13589
13590 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13591 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13592
13593 /* Back to the top, do it again. */
13594 }
13595}
13596
c906108c
SS
13597/* Read a minimal amount of information into the minimal die structure. */
13598
fe1b8b76 13599static gdb_byte *
dee91e82
DE
13600read_partial_die (const struct die_reader_specs *reader,
13601 struct partial_die_info *part_die,
13602 struct abbrev_info *abbrev, unsigned int abbrev_len,
13603 gdb_byte *info_ptr)
c906108c 13604{
dee91e82 13605 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13606 struct objfile *objfile = cu->objfile;
dee91e82 13607 gdb_byte *buffer = reader->buffer;
fa238c03 13608 unsigned int i;
c906108c 13609 struct attribute attr;
c5aa993b 13610 int has_low_pc_attr = 0;
c906108c 13611 int has_high_pc_attr = 0;
91da1414 13612 int high_pc_relative = 0;
c906108c 13613
72bf9492 13614 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13615
b64f50a1 13616 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13617
13618 info_ptr += abbrev_len;
13619
13620 if (abbrev == NULL)
13621 return info_ptr;
13622
c906108c
SS
13623 part_die->tag = abbrev->tag;
13624 part_die->has_children = abbrev->has_children;
c906108c
SS
13625
13626 for (i = 0; i < abbrev->num_attrs; ++i)
13627 {
dee91e82 13628 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13629
13630 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13631 partial symbol table. */
c906108c
SS
13632 switch (attr.name)
13633 {
13634 case DW_AT_name:
71c25dea
TT
13635 switch (part_die->tag)
13636 {
13637 case DW_TAG_compile_unit:
95554aad 13638 case DW_TAG_partial_unit:
348e048f 13639 case DW_TAG_type_unit:
71c25dea
TT
13640 /* Compilation units have a DW_AT_name that is a filename, not
13641 a source language identifier. */
13642 case DW_TAG_enumeration_type:
13643 case DW_TAG_enumerator:
13644 /* These tags always have simple identifiers already; no need
13645 to canonicalize them. */
13646 part_die->name = DW_STRING (&attr);
13647 break;
13648 default:
13649 part_die->name
13650 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13651 &objfile->objfile_obstack);
71c25dea
TT
13652 break;
13653 }
c906108c 13654 break;
31ef98ae 13655 case DW_AT_linkage_name:
c906108c 13656 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13657 /* Note that both forms of linkage name might appear. We
13658 assume they will be the same, and we only store the last
13659 one we see. */
94af9270
KS
13660 if (cu->language == language_ada)
13661 part_die->name = DW_STRING (&attr);
abc72ce4 13662 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13663 break;
13664 case DW_AT_low_pc:
13665 has_low_pc_attr = 1;
13666 part_die->lowpc = DW_ADDR (&attr);
13667 break;
13668 case DW_AT_high_pc:
13669 has_high_pc_attr = 1;
3019eac3
DE
13670 if (attr.form == DW_FORM_addr
13671 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13672 part_die->highpc = DW_ADDR (&attr);
13673 else
13674 {
13675 high_pc_relative = 1;
13676 part_die->highpc = DW_UNSND (&attr);
13677 }
c906108c
SS
13678 break;
13679 case DW_AT_location:
0963b4bd 13680 /* Support the .debug_loc offsets. */
8e19ed76
PS
13681 if (attr_form_is_block (&attr))
13682 {
95554aad 13683 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13684 }
3690dd37 13685 else if (attr_form_is_section_offset (&attr))
8e19ed76 13686 {
4d3c2250 13687 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13688 }
13689 else
13690 {
4d3c2250
KB
13691 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13692 "partial symbol information");
8e19ed76 13693 }
c906108c 13694 break;
c906108c
SS
13695 case DW_AT_external:
13696 part_die->is_external = DW_UNSND (&attr);
13697 break;
13698 case DW_AT_declaration:
13699 part_die->is_declaration = DW_UNSND (&attr);
13700 break;
13701 case DW_AT_type:
13702 part_die->has_type = 1;
13703 break;
13704 case DW_AT_abstract_origin:
13705 case DW_AT_specification:
72bf9492
DJ
13706 case DW_AT_extension:
13707 part_die->has_specification = 1;
c764a876 13708 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13709 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13710 || cu->per_cu->is_dwz);
c906108c
SS
13711 break;
13712 case DW_AT_sibling:
13713 /* Ignore absolute siblings, they might point outside of
13714 the current compile unit. */
13715 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13716 complaint (&symfile_complaints,
13717 _("ignoring absolute DW_AT_sibling"));
c906108c 13718 else
b64f50a1 13719 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13720 break;
fa4028e9
JB
13721 case DW_AT_byte_size:
13722 part_die->has_byte_size = 1;
13723 break;
68511cec
CES
13724 case DW_AT_calling_convention:
13725 /* DWARF doesn't provide a way to identify a program's source-level
13726 entry point. DW_AT_calling_convention attributes are only meant
13727 to describe functions' calling conventions.
13728
13729 However, because it's a necessary piece of information in
13730 Fortran, and because DW_CC_program is the only piece of debugging
13731 information whose definition refers to a 'main program' at all,
13732 several compilers have begun marking Fortran main programs with
13733 DW_CC_program --- even when those functions use the standard
13734 calling conventions.
13735
13736 So until DWARF specifies a way to provide this information and
13737 compilers pick up the new representation, we'll support this
13738 practice. */
13739 if (DW_UNSND (&attr) == DW_CC_program
13740 && cu->language == language_fortran)
01f8c46d
JK
13741 {
13742 set_main_name (part_die->name);
13743
13744 /* As this DIE has a static linkage the name would be difficult
13745 to look up later. */
13746 language_of_main = language_fortran;
13747 }
68511cec 13748 break;
481860b3
GB
13749 case DW_AT_inline:
13750 if (DW_UNSND (&attr) == DW_INL_inlined
13751 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13752 part_die->may_be_inlined = 1;
13753 break;
95554aad
TT
13754
13755 case DW_AT_import:
13756 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13757 {
13758 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13759 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13760 || cu->per_cu->is_dwz);
13761 }
95554aad
TT
13762 break;
13763
c906108c
SS
13764 default:
13765 break;
13766 }
13767 }
13768
91da1414
MW
13769 if (high_pc_relative)
13770 part_die->highpc += part_die->lowpc;
13771
9373cf26
JK
13772 if (has_low_pc_attr && has_high_pc_attr)
13773 {
13774 /* When using the GNU linker, .gnu.linkonce. sections are used to
13775 eliminate duplicate copies of functions and vtables and such.
13776 The linker will arbitrarily choose one and discard the others.
13777 The AT_*_pc values for such functions refer to local labels in
13778 these sections. If the section from that file was discarded, the
13779 labels are not in the output, so the relocs get a value of 0.
13780 If this is a discarded function, mark the pc bounds as invalid,
13781 so that GDB will ignore it. */
13782 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13783 {
bb5ed363 13784 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13785
13786 complaint (&symfile_complaints,
13787 _("DW_AT_low_pc %s is zero "
13788 "for DIE at 0x%x [in module %s]"),
13789 paddress (gdbarch, part_die->lowpc),
b64f50a1 13790 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13791 }
13792 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13793 else if (part_die->lowpc >= part_die->highpc)
13794 {
bb5ed363 13795 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13796
13797 complaint (&symfile_complaints,
13798 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13799 "for DIE at 0x%x [in module %s]"),
13800 paddress (gdbarch, part_die->lowpc),
13801 paddress (gdbarch, part_die->highpc),
b64f50a1 13802 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13803 }
13804 else
13805 part_die->has_pc_info = 1;
13806 }
85cbf3d3 13807
c906108c
SS
13808 return info_ptr;
13809}
13810
72bf9492
DJ
13811/* Find a cached partial DIE at OFFSET in CU. */
13812
13813static struct partial_die_info *
b64f50a1 13814find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13815{
13816 struct partial_die_info *lookup_die = NULL;
13817 struct partial_die_info part_die;
13818
13819 part_die.offset = offset;
b64f50a1
JK
13820 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13821 offset.sect_off);
72bf9492 13822
72bf9492
DJ
13823 return lookup_die;
13824}
13825
348e048f
DE
13826/* Find a partial DIE at OFFSET, which may or may not be in CU,
13827 except in the case of .debug_types DIEs which do not reference
13828 outside their CU (they do however referencing other types via
55f1336d 13829 DW_FORM_ref_sig8). */
72bf9492
DJ
13830
13831static struct partial_die_info *
36586728 13832find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13833{
bb5ed363 13834 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13835 struct dwarf2_per_cu_data *per_cu = NULL;
13836 struct partial_die_info *pd = NULL;
72bf9492 13837
36586728
TT
13838 if (offset_in_dwz == cu->per_cu->is_dwz
13839 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13840 {
13841 pd = find_partial_die_in_comp_unit (offset, cu);
13842 if (pd != NULL)
13843 return pd;
0d99eb77
DE
13844 /* We missed recording what we needed.
13845 Load all dies and try again. */
13846 per_cu = cu->per_cu;
5afb4e99 13847 }
0d99eb77
DE
13848 else
13849 {
13850 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13851 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13852 {
13853 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13854 " external reference to offset 0x%lx [in module %s].\n"),
13855 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13856 bfd_get_filename (objfile->obfd));
13857 }
36586728
TT
13858 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13859 objfile);
72bf9492 13860
0d99eb77
DE
13861 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13862 load_partial_comp_unit (per_cu);
ae038cb0 13863
0d99eb77
DE
13864 per_cu->cu->last_used = 0;
13865 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13866 }
5afb4e99 13867
dee91e82
DE
13868 /* If we didn't find it, and not all dies have been loaded,
13869 load them all and try again. */
13870
5afb4e99
DJ
13871 if (pd == NULL && per_cu->load_all_dies == 0)
13872 {
5afb4e99 13873 per_cu->load_all_dies = 1;
fd820528
DE
13874
13875 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13876 THIS_CU->cu may already be in use. So we can't just free it and
13877 replace its DIEs with the ones we read in. Instead, we leave those
13878 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13879 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13880 set. */
dee91e82 13881 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13882
13883 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13884 }
13885
13886 if (pd == NULL)
13887 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13888 _("could not find partial DIE 0x%x "
13889 "in cache [from module %s]\n"),
b64f50a1 13890 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13891 return pd;
72bf9492
DJ
13892}
13893
abc72ce4
DE
13894/* See if we can figure out if the class lives in a namespace. We do
13895 this by looking for a member function; its demangled name will
13896 contain namespace info, if there is any. */
13897
13898static void
13899guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13900 struct dwarf2_cu *cu)
13901{
13902 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13903 what template types look like, because the demangler
13904 frequently doesn't give the same name as the debug info. We
13905 could fix this by only using the demangled name to get the
13906 prefix (but see comment in read_structure_type). */
13907
13908 struct partial_die_info *real_pdi;
13909 struct partial_die_info *child_pdi;
13910
13911 /* If this DIE (this DIE's specification, if any) has a parent, then
13912 we should not do this. We'll prepend the parent's fully qualified
13913 name when we create the partial symbol. */
13914
13915 real_pdi = struct_pdi;
13916 while (real_pdi->has_specification)
36586728
TT
13917 real_pdi = find_partial_die (real_pdi->spec_offset,
13918 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13919
13920 if (real_pdi->die_parent != NULL)
13921 return;
13922
13923 for (child_pdi = struct_pdi->die_child;
13924 child_pdi != NULL;
13925 child_pdi = child_pdi->die_sibling)
13926 {
13927 if (child_pdi->tag == DW_TAG_subprogram
13928 && child_pdi->linkage_name != NULL)
13929 {
13930 char *actual_class_name
13931 = language_class_name_from_physname (cu->language_defn,
13932 child_pdi->linkage_name);
13933 if (actual_class_name != NULL)
13934 {
13935 struct_pdi->name
10f0c4bb
TT
13936 = obstack_copy0 (&cu->objfile->objfile_obstack,
13937 actual_class_name,
13938 strlen (actual_class_name));
abc72ce4
DE
13939 xfree (actual_class_name);
13940 }
13941 break;
13942 }
13943 }
13944}
13945
72bf9492
DJ
13946/* Adjust PART_DIE before generating a symbol for it. This function
13947 may set the is_external flag or change the DIE's name. */
13948
13949static void
13950fixup_partial_die (struct partial_die_info *part_die,
13951 struct dwarf2_cu *cu)
13952{
abc72ce4
DE
13953 /* Once we've fixed up a die, there's no point in doing so again.
13954 This also avoids a memory leak if we were to call
13955 guess_partial_die_structure_name multiple times. */
13956 if (part_die->fixup_called)
13957 return;
13958
72bf9492
DJ
13959 /* If we found a reference attribute and the DIE has no name, try
13960 to find a name in the referred to DIE. */
13961
13962 if (part_die->name == NULL && part_die->has_specification)
13963 {
13964 struct partial_die_info *spec_die;
72bf9492 13965
36586728
TT
13966 spec_die = find_partial_die (part_die->spec_offset,
13967 part_die->spec_is_dwz, cu);
72bf9492 13968
10b3939b 13969 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13970
13971 if (spec_die->name)
13972 {
13973 part_die->name = spec_die->name;
13974
13975 /* Copy DW_AT_external attribute if it is set. */
13976 if (spec_die->is_external)
13977 part_die->is_external = spec_die->is_external;
13978 }
13979 }
13980
13981 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13982
13983 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13984 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13985
abc72ce4
DE
13986 /* If there is no parent die to provide a namespace, and there are
13987 children, see if we can determine the namespace from their linkage
122d1940 13988 name. */
abc72ce4 13989 if (cu->language == language_cplus
8b70b953 13990 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13991 && part_die->die_parent == NULL
13992 && part_die->has_children
13993 && (part_die->tag == DW_TAG_class_type
13994 || part_die->tag == DW_TAG_structure_type
13995 || part_die->tag == DW_TAG_union_type))
13996 guess_partial_die_structure_name (part_die, cu);
13997
53832f31
TT
13998 /* GCC might emit a nameless struct or union that has a linkage
13999 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14000 if (part_die->name == NULL
96408a79
SA
14001 && (part_die->tag == DW_TAG_class_type
14002 || part_die->tag == DW_TAG_interface_type
14003 || part_die->tag == DW_TAG_structure_type
14004 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14005 && part_die->linkage_name != NULL)
14006 {
14007 char *demangled;
14008
14009 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14010 if (demangled)
14011 {
96408a79
SA
14012 const char *base;
14013
14014 /* Strip any leading namespaces/classes, keep only the base name.
14015 DW_AT_name for named DIEs does not contain the prefixes. */
14016 base = strrchr (demangled, ':');
14017 if (base && base > demangled && base[-1] == ':')
14018 base++;
14019 else
14020 base = demangled;
14021
10f0c4bb
TT
14022 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14023 base, strlen (base));
53832f31
TT
14024 xfree (demangled);
14025 }
14026 }
14027
abc72ce4 14028 part_die->fixup_called = 1;
72bf9492
DJ
14029}
14030
a8329558 14031/* Read an attribute value described by an attribute form. */
c906108c 14032
fe1b8b76 14033static gdb_byte *
dee91e82
DE
14034read_attribute_value (const struct die_reader_specs *reader,
14035 struct attribute *attr, unsigned form,
14036 gdb_byte *info_ptr)
c906108c 14037{
dee91e82
DE
14038 struct dwarf2_cu *cu = reader->cu;
14039 bfd *abfd = reader->abfd;
e7c27a73 14040 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14041 unsigned int bytes_read;
14042 struct dwarf_block *blk;
14043
a8329558
KW
14044 attr->form = form;
14045 switch (form)
c906108c 14046 {
c906108c 14047 case DW_FORM_ref_addr:
ae411497 14048 if (cu->header.version == 2)
4568ecf9 14049 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14050 else
4568ecf9
DE
14051 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14052 &cu->header, &bytes_read);
ae411497
TT
14053 info_ptr += bytes_read;
14054 break;
36586728
TT
14055 case DW_FORM_GNU_ref_alt:
14056 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14057 info_ptr += bytes_read;
14058 break;
ae411497 14059 case DW_FORM_addr:
e7c27a73 14060 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14061 info_ptr += bytes_read;
c906108c
SS
14062 break;
14063 case DW_FORM_block2:
7b5a2f43 14064 blk = dwarf_alloc_block (cu);
c906108c
SS
14065 blk->size = read_2_bytes (abfd, info_ptr);
14066 info_ptr += 2;
14067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14068 info_ptr += blk->size;
14069 DW_BLOCK (attr) = blk;
14070 break;
14071 case DW_FORM_block4:
7b5a2f43 14072 blk = dwarf_alloc_block (cu);
c906108c
SS
14073 blk->size = read_4_bytes (abfd, info_ptr);
14074 info_ptr += 4;
14075 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14076 info_ptr += blk->size;
14077 DW_BLOCK (attr) = blk;
14078 break;
14079 case DW_FORM_data2:
14080 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14081 info_ptr += 2;
14082 break;
14083 case DW_FORM_data4:
14084 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14085 info_ptr += 4;
14086 break;
14087 case DW_FORM_data8:
14088 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14089 info_ptr += 8;
14090 break;
2dc7f7b3
TT
14091 case DW_FORM_sec_offset:
14092 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14093 info_ptr += bytes_read;
14094 break;
c906108c 14095 case DW_FORM_string:
9b1c24c8 14096 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14097 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14098 info_ptr += bytes_read;
14099 break;
4bdf3d34 14100 case DW_FORM_strp:
36586728
TT
14101 if (!cu->per_cu->is_dwz)
14102 {
14103 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14104 &bytes_read);
14105 DW_STRING_IS_CANONICAL (attr) = 0;
14106 info_ptr += bytes_read;
14107 break;
14108 }
14109 /* FALLTHROUGH */
14110 case DW_FORM_GNU_strp_alt:
14111 {
14112 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14113 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14114 &bytes_read);
14115
14116 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14117 DW_STRING_IS_CANONICAL (attr) = 0;
14118 info_ptr += bytes_read;
14119 }
4bdf3d34 14120 break;
2dc7f7b3 14121 case DW_FORM_exprloc:
c906108c 14122 case DW_FORM_block:
7b5a2f43 14123 blk = dwarf_alloc_block (cu);
c906108c
SS
14124 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14125 info_ptr += bytes_read;
14126 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14127 info_ptr += blk->size;
14128 DW_BLOCK (attr) = blk;
14129 break;
14130 case DW_FORM_block1:
7b5a2f43 14131 blk = dwarf_alloc_block (cu);
c906108c
SS
14132 blk->size = read_1_byte (abfd, info_ptr);
14133 info_ptr += 1;
14134 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14135 info_ptr += blk->size;
14136 DW_BLOCK (attr) = blk;
14137 break;
14138 case DW_FORM_data1:
14139 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14140 info_ptr += 1;
14141 break;
14142 case DW_FORM_flag:
14143 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14144 info_ptr += 1;
14145 break;
2dc7f7b3
TT
14146 case DW_FORM_flag_present:
14147 DW_UNSND (attr) = 1;
14148 break;
c906108c
SS
14149 case DW_FORM_sdata:
14150 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14151 info_ptr += bytes_read;
14152 break;
14153 case DW_FORM_udata:
14154 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14155 info_ptr += bytes_read;
14156 break;
14157 case DW_FORM_ref1:
4568ecf9
DE
14158 DW_UNSND (attr) = (cu->header.offset.sect_off
14159 + read_1_byte (abfd, info_ptr));
c906108c
SS
14160 info_ptr += 1;
14161 break;
14162 case DW_FORM_ref2:
4568ecf9
DE
14163 DW_UNSND (attr) = (cu->header.offset.sect_off
14164 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14165 info_ptr += 2;
14166 break;
14167 case DW_FORM_ref4:
4568ecf9
DE
14168 DW_UNSND (attr) = (cu->header.offset.sect_off
14169 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14170 info_ptr += 4;
14171 break;
613e1657 14172 case DW_FORM_ref8:
4568ecf9
DE
14173 DW_UNSND (attr) = (cu->header.offset.sect_off
14174 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14175 info_ptr += 8;
14176 break;
55f1336d 14177 case DW_FORM_ref_sig8:
348e048f
DE
14178 /* Convert the signature to something we can record in DW_UNSND
14179 for later lookup.
14180 NOTE: This is NULL if the type wasn't found. */
14181 DW_SIGNATURED_TYPE (attr) =
e319fa28 14182 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14183 info_ptr += 8;
14184 break;
c906108c 14185 case DW_FORM_ref_udata:
4568ecf9
DE
14186 DW_UNSND (attr) = (cu->header.offset.sect_off
14187 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14188 info_ptr += bytes_read;
14189 break;
c906108c 14190 case DW_FORM_indirect:
a8329558
KW
14191 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14192 info_ptr += bytes_read;
dee91e82 14193 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14194 break;
3019eac3
DE
14195 case DW_FORM_GNU_addr_index:
14196 if (reader->dwo_file == NULL)
14197 {
14198 /* For now flag a hard error.
14199 Later we can turn this into a complaint. */
14200 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14201 dwarf_form_name (form),
14202 bfd_get_filename (abfd));
14203 }
14204 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14205 info_ptr += bytes_read;
14206 break;
14207 case DW_FORM_GNU_str_index:
14208 if (reader->dwo_file == NULL)
14209 {
14210 /* For now flag a hard error.
14211 Later we can turn this into a complaint if warranted. */
14212 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14213 dwarf_form_name (form),
14214 bfd_get_filename (abfd));
14215 }
14216 {
14217 ULONGEST str_index =
14218 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14219
14220 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14221 DW_STRING_IS_CANONICAL (attr) = 0;
14222 info_ptr += bytes_read;
14223 }
14224 break;
c906108c 14225 default:
8a3fe4f8 14226 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14227 dwarf_form_name (form),
14228 bfd_get_filename (abfd));
c906108c 14229 }
28e94949 14230
36586728
TT
14231 /* Super hack. */
14232 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14233 attr->form = DW_FORM_GNU_ref_alt;
14234
28e94949
JB
14235 /* We have seen instances where the compiler tried to emit a byte
14236 size attribute of -1 which ended up being encoded as an unsigned
14237 0xffffffff. Although 0xffffffff is technically a valid size value,
14238 an object of this size seems pretty unlikely so we can relatively
14239 safely treat these cases as if the size attribute was invalid and
14240 treat them as zero by default. */
14241 if (attr->name == DW_AT_byte_size
14242 && form == DW_FORM_data4
14243 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14244 {
14245 complaint
14246 (&symfile_complaints,
43bbcdc2
PH
14247 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14248 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14249 DW_UNSND (attr) = 0;
14250 }
28e94949 14251
c906108c
SS
14252 return info_ptr;
14253}
14254
a8329558
KW
14255/* Read an attribute described by an abbreviated attribute. */
14256
fe1b8b76 14257static gdb_byte *
dee91e82
DE
14258read_attribute (const struct die_reader_specs *reader,
14259 struct attribute *attr, struct attr_abbrev *abbrev,
14260 gdb_byte *info_ptr)
a8329558
KW
14261{
14262 attr->name = abbrev->name;
dee91e82 14263 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14264}
14265
0963b4bd 14266/* Read dwarf information from a buffer. */
c906108c
SS
14267
14268static unsigned int
a1855c1d 14269read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14270{
fe1b8b76 14271 return bfd_get_8 (abfd, buf);
c906108c
SS
14272}
14273
14274static int
a1855c1d 14275read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14276{
fe1b8b76 14277 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14278}
14279
14280static unsigned int
a1855c1d 14281read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14282{
fe1b8b76 14283 return bfd_get_16 (abfd, buf);
c906108c
SS
14284}
14285
21ae7a4d 14286static int
a1855c1d 14287read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14288{
14289 return bfd_get_signed_16 (abfd, buf);
14290}
14291
c906108c 14292static unsigned int
a1855c1d 14293read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14294{
fe1b8b76 14295 return bfd_get_32 (abfd, buf);
c906108c
SS
14296}
14297
21ae7a4d 14298static int
a1855c1d 14299read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14300{
14301 return bfd_get_signed_32 (abfd, buf);
14302}
14303
93311388 14304static ULONGEST
a1855c1d 14305read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14306{
fe1b8b76 14307 return bfd_get_64 (abfd, buf);
c906108c
SS
14308}
14309
14310static CORE_ADDR
fe1b8b76 14311read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14312 unsigned int *bytes_read)
c906108c 14313{
e7c27a73 14314 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14315 CORE_ADDR retval = 0;
14316
107d2387 14317 if (cu_header->signed_addr_p)
c906108c 14318 {
107d2387
AC
14319 switch (cu_header->addr_size)
14320 {
14321 case 2:
fe1b8b76 14322 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14323 break;
14324 case 4:
fe1b8b76 14325 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14326 break;
14327 case 8:
fe1b8b76 14328 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14329 break;
14330 default:
8e65ff28 14331 internal_error (__FILE__, __LINE__,
e2e0b3e5 14332 _("read_address: bad switch, signed [in module %s]"),
659b0389 14333 bfd_get_filename (abfd));
107d2387
AC
14334 }
14335 }
14336 else
14337 {
14338 switch (cu_header->addr_size)
14339 {
14340 case 2:
fe1b8b76 14341 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14342 break;
14343 case 4:
fe1b8b76 14344 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14345 break;
14346 case 8:
fe1b8b76 14347 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14348 break;
14349 default:
8e65ff28 14350 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14351 _("read_address: bad switch, "
14352 "unsigned [in module %s]"),
659b0389 14353 bfd_get_filename (abfd));
107d2387 14354 }
c906108c 14355 }
64367e0a 14356
107d2387
AC
14357 *bytes_read = cu_header->addr_size;
14358 return retval;
c906108c
SS
14359}
14360
f7ef9339 14361/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14362 specification allows the initial length to take up either 4 bytes
14363 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14364 bytes describe the length and all offsets will be 8 bytes in length
14365 instead of 4.
14366
f7ef9339
KB
14367 An older, non-standard 64-bit format is also handled by this
14368 function. The older format in question stores the initial length
14369 as an 8-byte quantity without an escape value. Lengths greater
14370 than 2^32 aren't very common which means that the initial 4 bytes
14371 is almost always zero. Since a length value of zero doesn't make
14372 sense for the 32-bit format, this initial zero can be considered to
14373 be an escape value which indicates the presence of the older 64-bit
14374 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14375 greater than 4GB. If it becomes necessary to handle lengths
14376 somewhat larger than 4GB, we could allow other small values (such
14377 as the non-sensical values of 1, 2, and 3) to also be used as
14378 escape values indicating the presence of the old format.
f7ef9339 14379
917c78fc
MK
14380 The value returned via bytes_read should be used to increment the
14381 relevant pointer after calling read_initial_length().
c764a876 14382
613e1657
KB
14383 [ Note: read_initial_length() and read_offset() are based on the
14384 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14385 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14386 from:
14387
f7ef9339 14388 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14389
613e1657
KB
14390 This document is only a draft and is subject to change. (So beware.)
14391
f7ef9339 14392 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14393 determined empirically by examining 64-bit ELF files produced by
14394 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14395
14396 - Kevin, July 16, 2002
613e1657
KB
14397 ] */
14398
14399static LONGEST
c764a876 14400read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14401{
fe1b8b76 14402 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14403
dd373385 14404 if (length == 0xffffffff)
613e1657 14405 {
fe1b8b76 14406 length = bfd_get_64 (abfd, buf + 4);
613e1657 14407 *bytes_read = 12;
613e1657 14408 }
dd373385 14409 else if (length == 0)
f7ef9339 14410 {
dd373385 14411 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14412 length = bfd_get_64 (abfd, buf);
f7ef9339 14413 *bytes_read = 8;
f7ef9339 14414 }
613e1657
KB
14415 else
14416 {
14417 *bytes_read = 4;
613e1657
KB
14418 }
14419
c764a876
DE
14420 return length;
14421}
dd373385 14422
c764a876
DE
14423/* Cover function for read_initial_length.
14424 Returns the length of the object at BUF, and stores the size of the
14425 initial length in *BYTES_READ and stores the size that offsets will be in
14426 *OFFSET_SIZE.
14427 If the initial length size is not equivalent to that specified in
14428 CU_HEADER then issue a complaint.
14429 This is useful when reading non-comp-unit headers. */
dd373385 14430
c764a876
DE
14431static LONGEST
14432read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14433 const struct comp_unit_head *cu_header,
14434 unsigned int *bytes_read,
14435 unsigned int *offset_size)
14436{
14437 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14438
14439 gdb_assert (cu_header->initial_length_size == 4
14440 || cu_header->initial_length_size == 8
14441 || cu_header->initial_length_size == 12);
14442
14443 if (cu_header->initial_length_size != *bytes_read)
14444 complaint (&symfile_complaints,
14445 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14446
c764a876 14447 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14448 return length;
613e1657
KB
14449}
14450
14451/* Read an offset from the data stream. The size of the offset is
917c78fc 14452 given by cu_header->offset_size. */
613e1657
KB
14453
14454static LONGEST
fe1b8b76 14455read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14456 unsigned int *bytes_read)
c764a876
DE
14457{
14458 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14459
c764a876
DE
14460 *bytes_read = cu_header->offset_size;
14461 return offset;
14462}
14463
14464/* Read an offset from the data stream. */
14465
14466static LONGEST
14467read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14468{
14469 LONGEST retval = 0;
14470
c764a876 14471 switch (offset_size)
613e1657
KB
14472 {
14473 case 4:
fe1b8b76 14474 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14475 break;
14476 case 8:
fe1b8b76 14477 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14478 break;
14479 default:
8e65ff28 14480 internal_error (__FILE__, __LINE__,
c764a876 14481 _("read_offset_1: bad switch [in module %s]"),
659b0389 14482 bfd_get_filename (abfd));
613e1657
KB
14483 }
14484
917c78fc 14485 return retval;
613e1657
KB
14486}
14487
fe1b8b76
JB
14488static gdb_byte *
14489read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14490{
14491 /* If the size of a host char is 8 bits, we can return a pointer
14492 to the buffer, otherwise we have to copy the data to a buffer
14493 allocated on the temporary obstack. */
4bdf3d34 14494 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14495 return buf;
c906108c
SS
14496}
14497
14498static char *
9b1c24c8 14499read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14500{
14501 /* If the size of a host char is 8 bits, we can return a pointer
14502 to the string, otherwise we have to copy the string to a buffer
14503 allocated on the temporary obstack. */
4bdf3d34 14504 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14505 if (*buf == '\0')
14506 {
14507 *bytes_read_ptr = 1;
14508 return NULL;
14509 }
fe1b8b76
JB
14510 *bytes_read_ptr = strlen ((char *) buf) + 1;
14511 return (char *) buf;
4bdf3d34
JJ
14512}
14513
14514static char *
cf2c3c16 14515read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14516{
be391dca 14517 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14518 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14519 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14520 bfd_get_filename (abfd));
dce234bc 14521 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14522 error (_("DW_FORM_strp pointing outside of "
14523 ".debug_str section [in module %s]"),
14524 bfd_get_filename (abfd));
4bdf3d34 14525 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14526 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14527 return NULL;
dce234bc 14528 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14529}
14530
36586728
TT
14531/* Read a string at offset STR_OFFSET in the .debug_str section from
14532 the .dwz file DWZ. Throw an error if the offset is too large. If
14533 the string consists of a single NUL byte, return NULL; otherwise
14534 return a pointer to the string. */
14535
14536static char *
14537read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14538{
14539 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14540
14541 if (dwz->str.buffer == NULL)
14542 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14543 "section [in module %s]"),
14544 bfd_get_filename (dwz->dwz_bfd));
14545 if (str_offset >= dwz->str.size)
14546 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14547 ".debug_str section [in module %s]"),
14548 bfd_get_filename (dwz->dwz_bfd));
14549 gdb_assert (HOST_CHAR_BIT == 8);
14550 if (dwz->str.buffer[str_offset] == '\0')
14551 return NULL;
14552 return (char *) (dwz->str.buffer + str_offset);
14553}
14554
cf2c3c16
TT
14555static char *
14556read_indirect_string (bfd *abfd, gdb_byte *buf,
14557 const struct comp_unit_head *cu_header,
14558 unsigned int *bytes_read_ptr)
14559{
14560 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14561
14562 return read_indirect_string_at_offset (abfd, str_offset);
14563}
14564
12df843f 14565static ULONGEST
fe1b8b76 14566read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14567{
12df843f 14568 ULONGEST result;
ce5d95e1 14569 unsigned int num_read;
c906108c
SS
14570 int i, shift;
14571 unsigned char byte;
14572
14573 result = 0;
14574 shift = 0;
14575 num_read = 0;
14576 i = 0;
14577 while (1)
14578 {
fe1b8b76 14579 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14580 buf++;
14581 num_read++;
12df843f 14582 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14583 if ((byte & 128) == 0)
14584 {
14585 break;
14586 }
14587 shift += 7;
14588 }
14589 *bytes_read_ptr = num_read;
14590 return result;
14591}
14592
12df843f 14593static LONGEST
fe1b8b76 14594read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14595{
12df843f 14596 LONGEST result;
77e0b926 14597 int i, shift, num_read;
c906108c
SS
14598 unsigned char byte;
14599
14600 result = 0;
14601 shift = 0;
c906108c
SS
14602 num_read = 0;
14603 i = 0;
14604 while (1)
14605 {
fe1b8b76 14606 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14607 buf++;
14608 num_read++;
12df843f 14609 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14610 shift += 7;
14611 if ((byte & 128) == 0)
14612 {
14613 break;
14614 }
14615 }
77e0b926 14616 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14617 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14618 *bytes_read_ptr = num_read;
14619 return result;
14620}
14621
3019eac3
DE
14622/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14623 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14624 ADDR_SIZE is the size of addresses from the CU header. */
14625
14626static CORE_ADDR
14627read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14628{
14629 struct objfile *objfile = dwarf2_per_objfile->objfile;
14630 bfd *abfd = objfile->obfd;
14631 const gdb_byte *info_ptr;
14632
14633 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14634 if (dwarf2_per_objfile->addr.buffer == NULL)
14635 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14636 objfile->name);
14637 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14638 error (_("DW_FORM_addr_index pointing outside of "
14639 ".debug_addr section [in module %s]"),
14640 objfile->name);
14641 info_ptr = (dwarf2_per_objfile->addr.buffer
14642 + addr_base + addr_index * addr_size);
14643 if (addr_size == 4)
14644 return bfd_get_32 (abfd, info_ptr);
14645 else
14646 return bfd_get_64 (abfd, info_ptr);
14647}
14648
14649/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14650
14651static CORE_ADDR
14652read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14653{
14654 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14655}
14656
14657/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14658
14659static CORE_ADDR
14660read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14661 unsigned int *bytes_read)
14662{
14663 bfd *abfd = cu->objfile->obfd;
14664 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14665
14666 return read_addr_index (cu, addr_index);
14667}
14668
14669/* Data structure to pass results from dwarf2_read_addr_index_reader
14670 back to dwarf2_read_addr_index. */
14671
14672struct dwarf2_read_addr_index_data
14673{
14674 ULONGEST addr_base;
14675 int addr_size;
14676};
14677
14678/* die_reader_func for dwarf2_read_addr_index. */
14679
14680static void
14681dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14682 gdb_byte *info_ptr,
14683 struct die_info *comp_unit_die,
14684 int has_children,
14685 void *data)
14686{
14687 struct dwarf2_cu *cu = reader->cu;
14688 struct dwarf2_read_addr_index_data *aidata =
14689 (struct dwarf2_read_addr_index_data *) data;
14690
14691 aidata->addr_base = cu->addr_base;
14692 aidata->addr_size = cu->header.addr_size;
14693}
14694
14695/* Given an index in .debug_addr, fetch the value.
14696 NOTE: This can be called during dwarf expression evaluation,
14697 long after the debug information has been read, and thus per_cu->cu
14698 may no longer exist. */
14699
14700CORE_ADDR
14701dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14702 unsigned int addr_index)
14703{
14704 struct objfile *objfile = per_cu->objfile;
14705 struct dwarf2_cu *cu = per_cu->cu;
14706 ULONGEST addr_base;
14707 int addr_size;
14708
14709 /* This is intended to be called from outside this file. */
14710 dw2_setup (objfile);
14711
14712 /* We need addr_base and addr_size.
14713 If we don't have PER_CU->cu, we have to get it.
14714 Nasty, but the alternative is storing the needed info in PER_CU,
14715 which at this point doesn't seem justified: it's not clear how frequently
14716 it would get used and it would increase the size of every PER_CU.
14717 Entry points like dwarf2_per_cu_addr_size do a similar thing
14718 so we're not in uncharted territory here.
14719 Alas we need to be a bit more complicated as addr_base is contained
14720 in the DIE.
14721
14722 We don't need to read the entire CU(/TU).
14723 We just need the header and top level die.
a1b64ce1 14724
3019eac3 14725 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14726 For now we skip this optimization. */
3019eac3
DE
14727
14728 if (cu != NULL)
14729 {
14730 addr_base = cu->addr_base;
14731 addr_size = cu->header.addr_size;
14732 }
14733 else
14734 {
14735 struct dwarf2_read_addr_index_data aidata;
14736
a1b64ce1
DE
14737 /* Note: We can't use init_cutu_and_read_dies_simple here,
14738 we need addr_base. */
14739 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14740 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14741 addr_base = aidata.addr_base;
14742 addr_size = aidata.addr_size;
14743 }
14744
14745 return read_addr_index_1 (addr_index, addr_base, addr_size);
14746}
14747
14748/* Given a DW_AT_str_index, fetch the string. */
14749
14750static char *
14751read_str_index (const struct die_reader_specs *reader,
14752 struct dwarf2_cu *cu, ULONGEST str_index)
14753{
14754 struct objfile *objfile = dwarf2_per_objfile->objfile;
14755 const char *dwo_name = objfile->name;
14756 bfd *abfd = objfile->obfd;
14757 struct dwo_sections *sections = &reader->dwo_file->sections;
14758 gdb_byte *info_ptr;
14759 ULONGEST str_offset;
14760
14761 dwarf2_read_section (objfile, &sections->str);
14762 dwarf2_read_section (objfile, &sections->str_offsets);
14763 if (sections->str.buffer == NULL)
14764 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14765 " in CU at offset 0x%lx [in module %s]"),
14766 (long) cu->header.offset.sect_off, dwo_name);
14767 if (sections->str_offsets.buffer == NULL)
14768 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14769 " in CU at offset 0x%lx [in module %s]"),
14770 (long) cu->header.offset.sect_off, dwo_name);
14771 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14772 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14773 " section in CU at offset 0x%lx [in module %s]"),
14774 (long) cu->header.offset.sect_off, dwo_name);
14775 info_ptr = (sections->str_offsets.buffer
14776 + str_index * cu->header.offset_size);
14777 if (cu->header.offset_size == 4)
14778 str_offset = bfd_get_32 (abfd, info_ptr);
14779 else
14780 str_offset = bfd_get_64 (abfd, info_ptr);
14781 if (str_offset >= sections->str.size)
14782 error (_("Offset from DW_FORM_str_index pointing outside of"
14783 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14784 (long) cu->header.offset.sect_off, dwo_name);
14785 return (char *) (sections->str.buffer + str_offset);
14786}
14787
3019eac3
DE
14788/* Return the length of an LEB128 number in BUF. */
14789
14790static int
14791leb128_size (const gdb_byte *buf)
14792{
14793 const gdb_byte *begin = buf;
14794 gdb_byte byte;
14795
14796 while (1)
14797 {
14798 byte = *buf++;
14799 if ((byte & 128) == 0)
14800 return buf - begin;
14801 }
14802}
14803
c906108c 14804static void
e142c38c 14805set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14806{
14807 switch (lang)
14808 {
14809 case DW_LANG_C89:
76bee0cc 14810 case DW_LANG_C99:
c906108c 14811 case DW_LANG_C:
e142c38c 14812 cu->language = language_c;
c906108c
SS
14813 break;
14814 case DW_LANG_C_plus_plus:
e142c38c 14815 cu->language = language_cplus;
c906108c 14816 break;
6aecb9c2
JB
14817 case DW_LANG_D:
14818 cu->language = language_d;
14819 break;
c906108c
SS
14820 case DW_LANG_Fortran77:
14821 case DW_LANG_Fortran90:
b21b22e0 14822 case DW_LANG_Fortran95:
e142c38c 14823 cu->language = language_fortran;
c906108c 14824 break;
a766d390
DE
14825 case DW_LANG_Go:
14826 cu->language = language_go;
14827 break;
c906108c 14828 case DW_LANG_Mips_Assembler:
e142c38c 14829 cu->language = language_asm;
c906108c 14830 break;
bebd888e 14831 case DW_LANG_Java:
e142c38c 14832 cu->language = language_java;
bebd888e 14833 break;
c906108c 14834 case DW_LANG_Ada83:
8aaf0b47 14835 case DW_LANG_Ada95:
bc5f45f8
JB
14836 cu->language = language_ada;
14837 break;
72019c9c
GM
14838 case DW_LANG_Modula2:
14839 cu->language = language_m2;
14840 break;
fe8e67fd
PM
14841 case DW_LANG_Pascal83:
14842 cu->language = language_pascal;
14843 break;
22566fbd
DJ
14844 case DW_LANG_ObjC:
14845 cu->language = language_objc;
14846 break;
c906108c
SS
14847 case DW_LANG_Cobol74:
14848 case DW_LANG_Cobol85:
c906108c 14849 default:
e142c38c 14850 cu->language = language_minimal;
c906108c
SS
14851 break;
14852 }
e142c38c 14853 cu->language_defn = language_def (cu->language);
c906108c
SS
14854}
14855
14856/* Return the named attribute or NULL if not there. */
14857
14858static struct attribute *
e142c38c 14859dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14860{
a48e046c 14861 for (;;)
c906108c 14862 {
a48e046c
TT
14863 unsigned int i;
14864 struct attribute *spec = NULL;
14865
14866 for (i = 0; i < die->num_attrs; ++i)
14867 {
14868 if (die->attrs[i].name == name)
14869 return &die->attrs[i];
14870 if (die->attrs[i].name == DW_AT_specification
14871 || die->attrs[i].name == DW_AT_abstract_origin)
14872 spec = &die->attrs[i];
14873 }
14874
14875 if (!spec)
14876 break;
c906108c 14877
f2f0e013 14878 die = follow_die_ref (die, spec, &cu);
f2f0e013 14879 }
c5aa993b 14880
c906108c
SS
14881 return NULL;
14882}
14883
348e048f
DE
14884/* Return the named attribute or NULL if not there,
14885 but do not follow DW_AT_specification, etc.
14886 This is for use in contexts where we're reading .debug_types dies.
14887 Following DW_AT_specification, DW_AT_abstract_origin will take us
14888 back up the chain, and we want to go down. */
14889
14890static struct attribute *
45e58e77 14891dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14892{
14893 unsigned int i;
14894
14895 for (i = 0; i < die->num_attrs; ++i)
14896 if (die->attrs[i].name == name)
14897 return &die->attrs[i];
14898
14899 return NULL;
14900}
14901
05cf31d1
JB
14902/* Return non-zero iff the attribute NAME is defined for the given DIE,
14903 and holds a non-zero value. This function should only be used for
2dc7f7b3 14904 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14905
14906static int
14907dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14908{
14909 struct attribute *attr = dwarf2_attr (die, name, cu);
14910
14911 return (attr && DW_UNSND (attr));
14912}
14913
3ca72b44 14914static int
e142c38c 14915die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14916{
05cf31d1
JB
14917 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14918 which value is non-zero. However, we have to be careful with
14919 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14920 (via dwarf2_flag_true_p) follows this attribute. So we may
14921 end up accidently finding a declaration attribute that belongs
14922 to a different DIE referenced by the specification attribute,
14923 even though the given DIE does not have a declaration attribute. */
14924 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14925 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14926}
14927
63d06c5c 14928/* Return the die giving the specification for DIE, if there is
f2f0e013 14929 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14930 containing the return value on output. If there is no
14931 specification, but there is an abstract origin, that is
14932 returned. */
63d06c5c
DC
14933
14934static struct die_info *
f2f0e013 14935die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14936{
f2f0e013
DJ
14937 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14938 *spec_cu);
63d06c5c 14939
edb3359d
DJ
14940 if (spec_attr == NULL)
14941 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14942
63d06c5c
DC
14943 if (spec_attr == NULL)
14944 return NULL;
14945 else
f2f0e013 14946 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14947}
c906108c 14948
debd256d 14949/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14950 refers to.
14951 NOTE: This is also used as a "cleanup" function. */
14952
debd256d
JB
14953static void
14954free_line_header (struct line_header *lh)
14955{
14956 if (lh->standard_opcode_lengths)
a8bc7b56 14957 xfree (lh->standard_opcode_lengths);
debd256d
JB
14958
14959 /* Remember that all the lh->file_names[i].name pointers are
14960 pointers into debug_line_buffer, and don't need to be freed. */
14961 if (lh->file_names)
a8bc7b56 14962 xfree (lh->file_names);
debd256d
JB
14963
14964 /* Similarly for the include directory names. */
14965 if (lh->include_dirs)
a8bc7b56 14966 xfree (lh->include_dirs);
debd256d 14967
a8bc7b56 14968 xfree (lh);
debd256d
JB
14969}
14970
debd256d 14971/* Add an entry to LH's include directory table. */
ae2de4f8 14972
debd256d
JB
14973static void
14974add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14975{
debd256d
JB
14976 /* Grow the array if necessary. */
14977 if (lh->include_dirs_size == 0)
c5aa993b 14978 {
debd256d
JB
14979 lh->include_dirs_size = 1; /* for testing */
14980 lh->include_dirs = xmalloc (lh->include_dirs_size
14981 * sizeof (*lh->include_dirs));
14982 }
14983 else if (lh->num_include_dirs >= lh->include_dirs_size)
14984 {
14985 lh->include_dirs_size *= 2;
14986 lh->include_dirs = xrealloc (lh->include_dirs,
14987 (lh->include_dirs_size
14988 * sizeof (*lh->include_dirs)));
c5aa993b 14989 }
c906108c 14990
debd256d
JB
14991 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14992}
6e70227d 14993
debd256d 14994/* Add an entry to LH's file name table. */
ae2de4f8 14995
debd256d
JB
14996static void
14997add_file_name (struct line_header *lh,
14998 char *name,
14999 unsigned int dir_index,
15000 unsigned int mod_time,
15001 unsigned int length)
15002{
15003 struct file_entry *fe;
15004
15005 /* Grow the array if necessary. */
15006 if (lh->file_names_size == 0)
15007 {
15008 lh->file_names_size = 1; /* for testing */
15009 lh->file_names = xmalloc (lh->file_names_size
15010 * sizeof (*lh->file_names));
15011 }
15012 else if (lh->num_file_names >= lh->file_names_size)
15013 {
15014 lh->file_names_size *= 2;
15015 lh->file_names = xrealloc (lh->file_names,
15016 (lh->file_names_size
15017 * sizeof (*lh->file_names)));
15018 }
15019
15020 fe = &lh->file_names[lh->num_file_names++];
15021 fe->name = name;
15022 fe->dir_index = dir_index;
15023 fe->mod_time = mod_time;
15024 fe->length = length;
aaa75496 15025 fe->included_p = 0;
cb1df416 15026 fe->symtab = NULL;
debd256d 15027}
6e70227d 15028
36586728
TT
15029/* A convenience function to find the proper .debug_line section for a
15030 CU. */
15031
15032static struct dwarf2_section_info *
15033get_debug_line_section (struct dwarf2_cu *cu)
15034{
15035 struct dwarf2_section_info *section;
15036
15037 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15038 DWO file. */
15039 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15040 section = &cu->dwo_unit->dwo_file->sections.line;
15041 else if (cu->per_cu->is_dwz)
15042 {
15043 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15044
15045 section = &dwz->line;
15046 }
15047 else
15048 section = &dwarf2_per_objfile->line;
15049
15050 return section;
15051}
15052
debd256d 15053/* Read the statement program header starting at OFFSET in
3019eac3 15054 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15055 to a struct line_header, allocated using xmalloc.
debd256d
JB
15056
15057 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15058 the returned object point into the dwarf line section buffer,
15059 and must not be freed. */
ae2de4f8 15060
debd256d 15061static struct line_header *
3019eac3 15062dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15063{
15064 struct cleanup *back_to;
15065 struct line_header *lh;
fe1b8b76 15066 gdb_byte *line_ptr;
c764a876 15067 unsigned int bytes_read, offset_size;
debd256d
JB
15068 int i;
15069 char *cur_dir, *cur_file;
3019eac3
DE
15070 struct dwarf2_section_info *section;
15071 bfd *abfd;
15072
36586728 15073 section = get_debug_line_section (cu);
3019eac3
DE
15074 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15075 if (section->buffer == NULL)
debd256d 15076 {
3019eac3
DE
15077 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15078 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15079 else
15080 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15081 return 0;
15082 }
15083
fceca515
DE
15084 /* We can't do this until we know the section is non-empty.
15085 Only then do we know we have such a section. */
15086 abfd = section->asection->owner;
15087
a738430d
MK
15088 /* Make sure that at least there's room for the total_length field.
15089 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15090 if (offset + 4 >= section->size)
debd256d 15091 {
4d3c2250 15092 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15093 return 0;
15094 }
15095
15096 lh = xmalloc (sizeof (*lh));
15097 memset (lh, 0, sizeof (*lh));
15098 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15099 (void *) lh);
15100
3019eac3 15101 line_ptr = section->buffer + offset;
debd256d 15102
a738430d 15103 /* Read in the header. */
6e70227d 15104 lh->total_length =
c764a876
DE
15105 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15106 &bytes_read, &offset_size);
debd256d 15107 line_ptr += bytes_read;
3019eac3 15108 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15109 {
4d3c2250 15110 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15111 return 0;
15112 }
15113 lh->statement_program_end = line_ptr + lh->total_length;
15114 lh->version = read_2_bytes (abfd, line_ptr);
15115 line_ptr += 2;
c764a876
DE
15116 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15117 line_ptr += offset_size;
debd256d
JB
15118 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15119 line_ptr += 1;
2dc7f7b3
TT
15120 if (lh->version >= 4)
15121 {
15122 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15123 line_ptr += 1;
15124 }
15125 else
15126 lh->maximum_ops_per_instruction = 1;
15127
15128 if (lh->maximum_ops_per_instruction == 0)
15129 {
15130 lh->maximum_ops_per_instruction = 1;
15131 complaint (&symfile_complaints,
3e43a32a
MS
15132 _("invalid maximum_ops_per_instruction "
15133 "in `.debug_line' section"));
2dc7f7b3
TT
15134 }
15135
debd256d
JB
15136 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15137 line_ptr += 1;
15138 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15139 line_ptr += 1;
15140 lh->line_range = read_1_byte (abfd, line_ptr);
15141 line_ptr += 1;
15142 lh->opcode_base = read_1_byte (abfd, line_ptr);
15143 line_ptr += 1;
15144 lh->standard_opcode_lengths
fe1b8b76 15145 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15146
15147 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15148 for (i = 1; i < lh->opcode_base; ++i)
15149 {
15150 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15151 line_ptr += 1;
15152 }
15153
a738430d 15154 /* Read directory table. */
9b1c24c8 15155 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15156 {
15157 line_ptr += bytes_read;
15158 add_include_dir (lh, cur_dir);
15159 }
15160 line_ptr += bytes_read;
15161
a738430d 15162 /* Read file name table. */
9b1c24c8 15163 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15164 {
15165 unsigned int dir_index, mod_time, length;
15166
15167 line_ptr += bytes_read;
15168 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15169 line_ptr += bytes_read;
15170 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15171 line_ptr += bytes_read;
15172 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15173 line_ptr += bytes_read;
15174
15175 add_file_name (lh, cur_file, dir_index, mod_time, length);
15176 }
15177 line_ptr += bytes_read;
6e70227d 15178 lh->statement_program_start = line_ptr;
debd256d 15179
3019eac3 15180 if (line_ptr > (section->buffer + section->size))
4d3c2250 15181 complaint (&symfile_complaints,
3e43a32a
MS
15182 _("line number info header doesn't "
15183 "fit in `.debug_line' section"));
debd256d
JB
15184
15185 discard_cleanups (back_to);
15186 return lh;
15187}
c906108c 15188
c6da4cef
DE
15189/* Subroutine of dwarf_decode_lines to simplify it.
15190 Return the file name of the psymtab for included file FILE_INDEX
15191 in line header LH of PST.
15192 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15193 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15194 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15195
15196 The function creates dangling cleanup registration. */
c6da4cef
DE
15197
15198static char *
15199psymtab_include_file_name (const struct line_header *lh, int file_index,
15200 const struct partial_symtab *pst,
15201 const char *comp_dir)
15202{
15203 const struct file_entry fe = lh->file_names [file_index];
15204 char *include_name = fe.name;
15205 char *include_name_to_compare = include_name;
15206 char *dir_name = NULL;
72b9f47f
TT
15207 const char *pst_filename;
15208 char *copied_name = NULL;
c6da4cef
DE
15209 int file_is_pst;
15210
15211 if (fe.dir_index)
15212 dir_name = lh->include_dirs[fe.dir_index - 1];
15213
15214 if (!IS_ABSOLUTE_PATH (include_name)
15215 && (dir_name != NULL || comp_dir != NULL))
15216 {
15217 /* Avoid creating a duplicate psymtab for PST.
15218 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15219 Before we do the comparison, however, we need to account
15220 for DIR_NAME and COMP_DIR.
15221 First prepend dir_name (if non-NULL). If we still don't
15222 have an absolute path prepend comp_dir (if non-NULL).
15223 However, the directory we record in the include-file's
15224 psymtab does not contain COMP_DIR (to match the
15225 corresponding symtab(s)).
15226
15227 Example:
15228
15229 bash$ cd /tmp
15230 bash$ gcc -g ./hello.c
15231 include_name = "hello.c"
15232 dir_name = "."
15233 DW_AT_comp_dir = comp_dir = "/tmp"
15234 DW_AT_name = "./hello.c" */
15235
15236 if (dir_name != NULL)
15237 {
15238 include_name = concat (dir_name, SLASH_STRING,
15239 include_name, (char *)NULL);
15240 include_name_to_compare = include_name;
15241 make_cleanup (xfree, include_name);
15242 }
15243 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15244 {
15245 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15246 include_name, (char *)NULL);
15247 }
15248 }
15249
15250 pst_filename = pst->filename;
15251 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15252 {
72b9f47f
TT
15253 copied_name = concat (pst->dirname, SLASH_STRING,
15254 pst_filename, (char *)NULL);
15255 pst_filename = copied_name;
c6da4cef
DE
15256 }
15257
1e3fad37 15258 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15259
15260 if (include_name_to_compare != include_name)
15261 xfree (include_name_to_compare);
72b9f47f
TT
15262 if (copied_name != NULL)
15263 xfree (copied_name);
c6da4cef
DE
15264
15265 if (file_is_pst)
15266 return NULL;
15267 return include_name;
15268}
15269
c91513d8
PP
15270/* Ignore this record_line request. */
15271
15272static void
15273noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15274{
15275 return;
15276}
15277
f3f5162e
DE
15278/* Subroutine of dwarf_decode_lines to simplify it.
15279 Process the line number information in LH. */
debd256d 15280
c906108c 15281static void
f3f5162e
DE
15282dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15283 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15284{
a8c50c1f 15285 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15286 gdb_byte *line_end;
a8c50c1f 15287 unsigned int bytes_read, extended_len;
c906108c 15288 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15289 CORE_ADDR baseaddr;
15290 struct objfile *objfile = cu->objfile;
f3f5162e 15291 bfd *abfd = objfile->obfd;
fbf65064 15292 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15293 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15294 struct subfile *last_subfile = NULL;
c91513d8
PP
15295 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15296 = record_line;
e142c38c
DJ
15297
15298 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15299
debd256d
JB
15300 line_ptr = lh->statement_program_start;
15301 line_end = lh->statement_program_end;
c906108c
SS
15302
15303 /* Read the statement sequences until there's nothing left. */
15304 while (line_ptr < line_end)
15305 {
15306 /* state machine registers */
15307 CORE_ADDR address = 0;
15308 unsigned int file = 1;
15309 unsigned int line = 1;
15310 unsigned int column = 0;
debd256d 15311 int is_stmt = lh->default_is_stmt;
c906108c
SS
15312 int basic_block = 0;
15313 int end_sequence = 0;
fbf65064 15314 CORE_ADDR addr;
2dc7f7b3 15315 unsigned char op_index = 0;
c906108c 15316
aaa75496 15317 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15318 {
aaa75496 15319 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15320 /* lh->include_dirs and lh->file_names are 0-based, but the
15321 directory and file name numbers in the statement program
15322 are 1-based. */
15323 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15324 char *dir = NULL;
a738430d 15325
debd256d
JB
15326 if (fe->dir_index)
15327 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15328
15329 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15330 }
15331
a738430d 15332 /* Decode the table. */
c5aa993b 15333 while (!end_sequence)
c906108c
SS
15334 {
15335 op_code = read_1_byte (abfd, line_ptr);
15336 line_ptr += 1;
59205f5a
JB
15337 if (line_ptr > line_end)
15338 {
15339 dwarf2_debug_line_missing_end_sequence_complaint ();
15340 break;
15341 }
9aa1fe7e 15342
debd256d 15343 if (op_code >= lh->opcode_base)
6e70227d 15344 {
a738430d 15345 /* Special operand. */
debd256d 15346 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15347 address += (((op_index + (adj_opcode / lh->line_range))
15348 / lh->maximum_ops_per_instruction)
15349 * lh->minimum_instruction_length);
15350 op_index = ((op_index + (adj_opcode / lh->line_range))
15351 % lh->maximum_ops_per_instruction);
debd256d 15352 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15353 if (lh->num_file_names < file || file == 0)
25e43795 15354 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15355 /* For now we ignore lines not starting on an
15356 instruction boundary. */
15357 else if (op_index == 0)
25e43795
DJ
15358 {
15359 lh->file_names[file - 1].included_p = 1;
ca5f395d 15360 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15361 {
15362 if (last_subfile != current_subfile)
15363 {
15364 addr = gdbarch_addr_bits_remove (gdbarch, address);
15365 if (last_subfile)
c91513d8 15366 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15367 last_subfile = current_subfile;
15368 }
25e43795 15369 /* Append row to matrix using current values. */
7019d805 15370 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15371 (*p_record_line) (current_subfile, line, addr);
366da635 15372 }
25e43795 15373 }
ca5f395d 15374 basic_block = 0;
9aa1fe7e
GK
15375 }
15376 else switch (op_code)
c906108c
SS
15377 {
15378 case DW_LNS_extended_op:
3e43a32a
MS
15379 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15380 &bytes_read);
473b7be6 15381 line_ptr += bytes_read;
a8c50c1f 15382 extended_end = line_ptr + extended_len;
c906108c
SS
15383 extended_op = read_1_byte (abfd, line_ptr);
15384 line_ptr += 1;
15385 switch (extended_op)
15386 {
15387 case DW_LNE_end_sequence:
c91513d8 15388 p_record_line = record_line;
c906108c 15389 end_sequence = 1;
c906108c
SS
15390 break;
15391 case DW_LNE_set_address:
e7c27a73 15392 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15393
15394 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15395 {
15396 /* This line table is for a function which has been
15397 GCd by the linker. Ignore it. PR gdb/12528 */
15398
15399 long line_offset
36586728 15400 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15401
15402 complaint (&symfile_complaints,
15403 _(".debug_line address at offset 0x%lx is 0 "
15404 "[in module %s]"),
bb5ed363 15405 line_offset, objfile->name);
c91513d8
PP
15406 p_record_line = noop_record_line;
15407 }
15408
2dc7f7b3 15409 op_index = 0;
107d2387
AC
15410 line_ptr += bytes_read;
15411 address += baseaddr;
c906108c
SS
15412 break;
15413 case DW_LNE_define_file:
debd256d
JB
15414 {
15415 char *cur_file;
15416 unsigned int dir_index, mod_time, length;
6e70227d 15417
3e43a32a
MS
15418 cur_file = read_direct_string (abfd, line_ptr,
15419 &bytes_read);
debd256d
JB
15420 line_ptr += bytes_read;
15421 dir_index =
15422 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15423 line_ptr += bytes_read;
15424 mod_time =
15425 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15426 line_ptr += bytes_read;
15427 length =
15428 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15429 line_ptr += bytes_read;
15430 add_file_name (lh, cur_file, dir_index, mod_time, length);
15431 }
c906108c 15432 break;
d0c6ba3d
CC
15433 case DW_LNE_set_discriminator:
15434 /* The discriminator is not interesting to the debugger;
15435 just ignore it. */
15436 line_ptr = extended_end;
15437 break;
c906108c 15438 default:
4d3c2250 15439 complaint (&symfile_complaints,
e2e0b3e5 15440 _("mangled .debug_line section"));
debd256d 15441 return;
c906108c 15442 }
a8c50c1f
DJ
15443 /* Make sure that we parsed the extended op correctly. If e.g.
15444 we expected a different address size than the producer used,
15445 we may have read the wrong number of bytes. */
15446 if (line_ptr != extended_end)
15447 {
15448 complaint (&symfile_complaints,
15449 _("mangled .debug_line section"));
15450 return;
15451 }
c906108c
SS
15452 break;
15453 case DW_LNS_copy:
59205f5a 15454 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15455 dwarf2_debug_line_missing_file_complaint ();
15456 else
366da635 15457 {
25e43795 15458 lh->file_names[file - 1].included_p = 1;
ca5f395d 15459 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15460 {
15461 if (last_subfile != current_subfile)
15462 {
15463 addr = gdbarch_addr_bits_remove (gdbarch, address);
15464 if (last_subfile)
c91513d8 15465 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15466 last_subfile = current_subfile;
15467 }
7019d805 15468 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15469 (*p_record_line) (current_subfile, line, addr);
fbf65064 15470 }
366da635 15471 }
c906108c
SS
15472 basic_block = 0;
15473 break;
15474 case DW_LNS_advance_pc:
2dc7f7b3
TT
15475 {
15476 CORE_ADDR adjust
15477 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15478
15479 address += (((op_index + adjust)
15480 / lh->maximum_ops_per_instruction)
15481 * lh->minimum_instruction_length);
15482 op_index = ((op_index + adjust)
15483 % lh->maximum_ops_per_instruction);
15484 line_ptr += bytes_read;
15485 }
c906108c
SS
15486 break;
15487 case DW_LNS_advance_line:
15488 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15489 line_ptr += bytes_read;
15490 break;
15491 case DW_LNS_set_file:
debd256d 15492 {
a738430d
MK
15493 /* The arrays lh->include_dirs and lh->file_names are
15494 0-based, but the directory and file name numbers in
15495 the statement program are 1-based. */
debd256d 15496 struct file_entry *fe;
4f1520fb 15497 char *dir = NULL;
a738430d 15498
debd256d
JB
15499 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15500 line_ptr += bytes_read;
59205f5a 15501 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15502 dwarf2_debug_line_missing_file_complaint ();
15503 else
15504 {
15505 fe = &lh->file_names[file - 1];
15506 if (fe->dir_index)
15507 dir = lh->include_dirs[fe->dir_index - 1];
15508 if (!decode_for_pst_p)
15509 {
15510 last_subfile = current_subfile;
15511 dwarf2_start_subfile (fe->name, dir, comp_dir);
15512 }
15513 }
debd256d 15514 }
c906108c
SS
15515 break;
15516 case DW_LNS_set_column:
15517 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15518 line_ptr += bytes_read;
15519 break;
15520 case DW_LNS_negate_stmt:
15521 is_stmt = (!is_stmt);
15522 break;
15523 case DW_LNS_set_basic_block:
15524 basic_block = 1;
15525 break;
c2c6d25f
JM
15526 /* Add to the address register of the state machine the
15527 address increment value corresponding to special opcode
a738430d
MK
15528 255. I.e., this value is scaled by the minimum
15529 instruction length since special opcode 255 would have
b021a221 15530 scaled the increment. */
c906108c 15531 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15532 {
15533 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15534
15535 address += (((op_index + adjust)
15536 / lh->maximum_ops_per_instruction)
15537 * lh->minimum_instruction_length);
15538 op_index = ((op_index + adjust)
15539 % lh->maximum_ops_per_instruction);
15540 }
c906108c
SS
15541 break;
15542 case DW_LNS_fixed_advance_pc:
15543 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15544 op_index = 0;
c906108c
SS
15545 line_ptr += 2;
15546 break;
9aa1fe7e 15547 default:
a738430d
MK
15548 {
15549 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15550 int i;
a738430d 15551
debd256d 15552 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15553 {
15554 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15555 line_ptr += bytes_read;
15556 }
15557 }
c906108c
SS
15558 }
15559 }
59205f5a
JB
15560 if (lh->num_file_names < file || file == 0)
15561 dwarf2_debug_line_missing_file_complaint ();
15562 else
15563 {
15564 lh->file_names[file - 1].included_p = 1;
15565 if (!decode_for_pst_p)
fbf65064
UW
15566 {
15567 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15568 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15569 }
59205f5a 15570 }
c906108c 15571 }
f3f5162e
DE
15572}
15573
15574/* Decode the Line Number Program (LNP) for the given line_header
15575 structure and CU. The actual information extracted and the type
15576 of structures created from the LNP depends on the value of PST.
15577
15578 1. If PST is NULL, then this procedure uses the data from the program
15579 to create all necessary symbol tables, and their linetables.
15580
15581 2. If PST is not NULL, this procedure reads the program to determine
15582 the list of files included by the unit represented by PST, and
15583 builds all the associated partial symbol tables.
15584
15585 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15586 It is used for relative paths in the line table.
15587 NOTE: When processing partial symtabs (pst != NULL),
15588 comp_dir == pst->dirname.
15589
15590 NOTE: It is important that psymtabs have the same file name (via strcmp)
15591 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15592 symtab we don't use it in the name of the psymtabs we create.
15593 E.g. expand_line_sal requires this when finding psymtabs to expand.
15594 A good testcase for this is mb-inline.exp. */
15595
15596static void
15597dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15598 struct dwarf2_cu *cu, struct partial_symtab *pst,
15599 int want_line_info)
15600{
15601 struct objfile *objfile = cu->objfile;
15602 const int decode_for_pst_p = (pst != NULL);
15603 struct subfile *first_subfile = current_subfile;
15604
15605 if (want_line_info)
15606 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15607
15608 if (decode_for_pst_p)
15609 {
15610 int file_index;
15611
15612 /* Now that we're done scanning the Line Header Program, we can
15613 create the psymtab of each included file. */
15614 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15615 if (lh->file_names[file_index].included_p == 1)
15616 {
c6da4cef
DE
15617 char *include_name =
15618 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15619 if (include_name != NULL)
aaa75496
JB
15620 dwarf2_create_include_psymtab (include_name, pst, objfile);
15621 }
15622 }
cb1df416
DJ
15623 else
15624 {
15625 /* Make sure a symtab is created for every file, even files
15626 which contain only variables (i.e. no code with associated
15627 line numbers). */
cb1df416 15628 int i;
cb1df416
DJ
15629
15630 for (i = 0; i < lh->num_file_names; i++)
15631 {
15632 char *dir = NULL;
f3f5162e 15633 struct file_entry *fe;
9a619af0 15634
cb1df416
DJ
15635 fe = &lh->file_names[i];
15636 if (fe->dir_index)
15637 dir = lh->include_dirs[fe->dir_index - 1];
15638 dwarf2_start_subfile (fe->name, dir, comp_dir);
15639
15640 /* Skip the main file; we don't need it, and it must be
15641 allocated last, so that it will show up before the
15642 non-primary symtabs in the objfile's symtab list. */
15643 if (current_subfile == first_subfile)
15644 continue;
15645
15646 if (current_subfile->symtab == NULL)
15647 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15648 objfile);
cb1df416
DJ
15649 fe->symtab = current_subfile->symtab;
15650 }
15651 }
c906108c
SS
15652}
15653
15654/* Start a subfile for DWARF. FILENAME is the name of the file and
15655 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15656 or NULL if not known. COMP_DIR is the compilation directory for the
15657 linetable's compilation unit or NULL if not known.
c906108c
SS
15658 This routine tries to keep line numbers from identical absolute and
15659 relative file names in a common subfile.
15660
15661 Using the `list' example from the GDB testsuite, which resides in
15662 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15663 of /srcdir/list0.c yields the following debugging information for list0.c:
15664
c5aa993b
JM
15665 DW_AT_name: /srcdir/list0.c
15666 DW_AT_comp_dir: /compdir
357e46e7 15667 files.files[0].name: list0.h
c5aa993b 15668 files.files[0].dir: /srcdir
357e46e7 15669 files.files[1].name: list0.c
c5aa993b 15670 files.files[1].dir: /srcdir
c906108c
SS
15671
15672 The line number information for list0.c has to end up in a single
4f1520fb
FR
15673 subfile, so that `break /srcdir/list0.c:1' works as expected.
15674 start_subfile will ensure that this happens provided that we pass the
15675 concatenation of files.files[1].dir and files.files[1].name as the
15676 subfile's name. */
c906108c
SS
15677
15678static void
3e43a32a
MS
15679dwarf2_start_subfile (char *filename, const char *dirname,
15680 const char *comp_dir)
c906108c 15681{
4f1520fb
FR
15682 char *fullname;
15683
15684 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15685 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15686 second argument to start_subfile. To be consistent, we do the
15687 same here. In order not to lose the line information directory,
15688 we concatenate it to the filename when it makes sense.
15689 Note that the Dwarf3 standard says (speaking of filenames in line
15690 information): ``The directory index is ignored for file names
15691 that represent full path names''. Thus ignoring dirname in the
15692 `else' branch below isn't an issue. */
c906108c 15693
d5166ae1 15694 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15695 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15696 else
15697 fullname = filename;
c906108c 15698
4f1520fb
FR
15699 start_subfile (fullname, comp_dir);
15700
15701 if (fullname != filename)
15702 xfree (fullname);
c906108c
SS
15703}
15704
f4dc4d17
DE
15705/* Start a symtab for DWARF.
15706 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15707
15708static void
15709dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15710 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15711{
15712 start_symtab (name, comp_dir, low_pc);
15713 record_debugformat ("DWARF 2");
15714 record_producer (cu->producer);
15715
15716 /* We assume that we're processing GCC output. */
15717 processing_gcc_compilation = 2;
15718
4d4ec4e5 15719 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15720}
15721
4c2df51b
DJ
15722static void
15723var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15724 struct dwarf2_cu *cu)
4c2df51b 15725{
e7c27a73
DJ
15726 struct objfile *objfile = cu->objfile;
15727 struct comp_unit_head *cu_header = &cu->header;
15728
4c2df51b
DJ
15729 /* NOTE drow/2003-01-30: There used to be a comment and some special
15730 code here to turn a symbol with DW_AT_external and a
15731 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15732 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15733 with some versions of binutils) where shared libraries could have
15734 relocations against symbols in their debug information - the
15735 minimal symbol would have the right address, but the debug info
15736 would not. It's no longer necessary, because we will explicitly
15737 apply relocations when we read in the debug information now. */
15738
15739 /* A DW_AT_location attribute with no contents indicates that a
15740 variable has been optimized away. */
15741 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15742 {
f1e6e072 15743 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15744 return;
15745 }
15746
15747 /* Handle one degenerate form of location expression specially, to
15748 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15749 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15750 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15751
15752 if (attr_form_is_block (attr)
3019eac3
DE
15753 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15754 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15755 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15756 && (DW_BLOCK (attr)->size
15757 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15758 {
891d2f0b 15759 unsigned int dummy;
4c2df51b 15760
3019eac3
DE
15761 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15762 SYMBOL_VALUE_ADDRESS (sym) =
15763 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15764 else
15765 SYMBOL_VALUE_ADDRESS (sym) =
15766 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15767 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15768 fixup_symbol_section (sym, objfile);
15769 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15770 SYMBOL_SECTION (sym));
4c2df51b
DJ
15771 return;
15772 }
15773
15774 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15775 expression evaluator, and use LOC_COMPUTED only when necessary
15776 (i.e. when the value of a register or memory location is
15777 referenced, or a thread-local block, etc.). Then again, it might
15778 not be worthwhile. I'm assuming that it isn't unless performance
15779 or memory numbers show me otherwise. */
15780
f1e6e072 15781 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15782
f1e6e072 15783 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15784 cu->has_loclist = 1;
4c2df51b
DJ
15785}
15786
c906108c
SS
15787/* Given a pointer to a DWARF information entry, figure out if we need
15788 to make a symbol table entry for it, and if so, create a new entry
15789 and return a pointer to it.
15790 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15791 used the passed type.
15792 If SPACE is not NULL, use it to hold the new symbol. If it is
15793 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15794
15795static struct symbol *
34eaf542
TT
15796new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15797 struct symbol *space)
c906108c 15798{
e7c27a73 15799 struct objfile *objfile = cu->objfile;
c906108c 15800 struct symbol *sym = NULL;
15d034d0 15801 const char *name;
c906108c
SS
15802 struct attribute *attr = NULL;
15803 struct attribute *attr2 = NULL;
e142c38c 15804 CORE_ADDR baseaddr;
e37fd15a
SW
15805 struct pending **list_to_add = NULL;
15806
edb3359d 15807 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15808
15809 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15810
94af9270 15811 name = dwarf2_name (die, cu);
c906108c
SS
15812 if (name)
15813 {
94af9270 15814 const char *linkagename;
34eaf542 15815 int suppress_add = 0;
94af9270 15816
34eaf542
TT
15817 if (space)
15818 sym = space;
15819 else
15820 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15821 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15822
15823 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15824 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15825 linkagename = dwarf2_physname (name, die, cu);
15826 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15827
f55ee35c
JK
15828 /* Fortran does not have mangling standard and the mangling does differ
15829 between gfortran, iFort etc. */
15830 if (cu->language == language_fortran
b250c185 15831 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15832 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15833 dwarf2_full_name (name, die, cu),
29df156d 15834 NULL);
f55ee35c 15835
c906108c 15836 /* Default assumptions.
c5aa993b 15837 Use the passed type or decode it from the die. */
176620f1 15838 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 15839 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15840 if (type != NULL)
15841 SYMBOL_TYPE (sym) = type;
15842 else
e7c27a73 15843 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15844 attr = dwarf2_attr (die,
15845 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15846 cu);
c906108c
SS
15847 if (attr)
15848 {
15849 SYMBOL_LINE (sym) = DW_UNSND (attr);
15850 }
cb1df416 15851
edb3359d
DJ
15852 attr = dwarf2_attr (die,
15853 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15854 cu);
cb1df416
DJ
15855 if (attr)
15856 {
15857 int file_index = DW_UNSND (attr);
9a619af0 15858
cb1df416
DJ
15859 if (cu->line_header == NULL
15860 || file_index > cu->line_header->num_file_names)
15861 complaint (&symfile_complaints,
15862 _("file index out of range"));
1c3d648d 15863 else if (file_index > 0)
cb1df416
DJ
15864 {
15865 struct file_entry *fe;
9a619af0 15866
cb1df416
DJ
15867 fe = &cu->line_header->file_names[file_index - 1];
15868 SYMBOL_SYMTAB (sym) = fe->symtab;
15869 }
15870 }
15871
c906108c
SS
15872 switch (die->tag)
15873 {
15874 case DW_TAG_label:
e142c38c 15875 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15876 if (attr)
15877 {
15878 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15879 }
0f5238ed
TT
15880 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15881 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 15882 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 15883 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15884 break;
15885 case DW_TAG_subprogram:
15886 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15887 finish_block. */
f1e6e072 15888 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 15889 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15890 if ((attr2 && (DW_UNSND (attr2) != 0))
15891 || cu->language == language_ada)
c906108c 15892 {
2cfa0c8d
JB
15893 /* Subprograms marked external are stored as a global symbol.
15894 Ada subprograms, whether marked external or not, are always
15895 stored as a global symbol, because we want to be able to
15896 access them globally. For instance, we want to be able
15897 to break on a nested subprogram without having to
15898 specify the context. */
e37fd15a 15899 list_to_add = &global_symbols;
c906108c
SS
15900 }
15901 else
15902 {
e37fd15a 15903 list_to_add = cu->list_in_scope;
c906108c
SS
15904 }
15905 break;
edb3359d
DJ
15906 case DW_TAG_inlined_subroutine:
15907 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15908 finish_block. */
f1e6e072 15909 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 15910 SYMBOL_INLINED (sym) = 1;
481860b3 15911 list_to_add = cu->list_in_scope;
edb3359d 15912 break;
34eaf542
TT
15913 case DW_TAG_template_value_param:
15914 suppress_add = 1;
15915 /* Fall through. */
72929c62 15916 case DW_TAG_constant:
c906108c 15917 case DW_TAG_variable:
254e6b9e 15918 case DW_TAG_member:
0963b4bd
MS
15919 /* Compilation with minimal debug info may result in
15920 variables with missing type entries. Change the
15921 misleading `void' type to something sensible. */
c906108c 15922 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15923 SYMBOL_TYPE (sym)
46bf5051 15924 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15925
e142c38c 15926 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15927 /* In the case of DW_TAG_member, we should only be called for
15928 static const members. */
15929 if (die->tag == DW_TAG_member)
15930 {
3863f96c
DE
15931 /* dwarf2_add_field uses die_is_declaration,
15932 so we do the same. */
254e6b9e
DE
15933 gdb_assert (die_is_declaration (die, cu));
15934 gdb_assert (attr);
15935 }
c906108c
SS
15936 if (attr)
15937 {
e7c27a73 15938 dwarf2_const_value (attr, sym, cu);
e142c38c 15939 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15940 if (!suppress_add)
34eaf542
TT
15941 {
15942 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15943 list_to_add = &global_symbols;
34eaf542 15944 else
e37fd15a 15945 list_to_add = cu->list_in_scope;
34eaf542 15946 }
c906108c
SS
15947 break;
15948 }
e142c38c 15949 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15950 if (attr)
15951 {
e7c27a73 15952 var_decode_location (attr, sym, cu);
e142c38c 15953 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15954
15955 /* Fortran explicitly imports any global symbols to the local
15956 scope by DW_TAG_common_block. */
15957 if (cu->language == language_fortran && die->parent
15958 && die->parent->tag == DW_TAG_common_block)
15959 attr2 = NULL;
15960
caac4577
JG
15961 if (SYMBOL_CLASS (sym) == LOC_STATIC
15962 && SYMBOL_VALUE_ADDRESS (sym) == 0
15963 && !dwarf2_per_objfile->has_section_at_zero)
15964 {
15965 /* When a static variable is eliminated by the linker,
15966 the corresponding debug information is not stripped
15967 out, but the variable address is set to null;
15968 do not add such variables into symbol table. */
15969 }
15970 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15971 {
f55ee35c
JK
15972 /* Workaround gfortran PR debug/40040 - it uses
15973 DW_AT_location for variables in -fPIC libraries which may
15974 get overriden by other libraries/executable and get
15975 a different address. Resolve it by the minimal symbol
15976 which may come from inferior's executable using copy
15977 relocation. Make this workaround only for gfortran as for
15978 other compilers GDB cannot guess the minimal symbol
15979 Fortran mangling kind. */
15980 if (cu->language == language_fortran && die->parent
15981 && die->parent->tag == DW_TAG_module
15982 && cu->producer
15983 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 15984 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 15985
1c809c68
TT
15986 /* A variable with DW_AT_external is never static,
15987 but it may be block-scoped. */
15988 list_to_add = (cu->list_in_scope == &file_symbols
15989 ? &global_symbols : cu->list_in_scope);
1c809c68 15990 }
c906108c 15991 else
e37fd15a 15992 list_to_add = cu->list_in_scope;
c906108c
SS
15993 }
15994 else
15995 {
15996 /* We do not know the address of this symbol.
c5aa993b
JM
15997 If it is an external symbol and we have type information
15998 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15999 The address of the variable will then be determined from
16000 the minimal symbol table whenever the variable is
16001 referenced. */
e142c38c 16002 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16003
16004 /* Fortran explicitly imports any global symbols to the local
16005 scope by DW_TAG_common_block. */
16006 if (cu->language == language_fortran && die->parent
16007 && die->parent->tag == DW_TAG_common_block)
16008 {
16009 /* SYMBOL_CLASS doesn't matter here because
16010 read_common_block is going to reset it. */
16011 if (!suppress_add)
16012 list_to_add = cu->list_in_scope;
16013 }
16014 else if (attr2 && (DW_UNSND (attr2) != 0)
16015 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16016 {
0fe7935b
DJ
16017 /* A variable with DW_AT_external is never static, but it
16018 may be block-scoped. */
16019 list_to_add = (cu->list_in_scope == &file_symbols
16020 ? &global_symbols : cu->list_in_scope);
16021
f1e6e072 16022 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16023 }
442ddf59
JK
16024 else if (!die_is_declaration (die, cu))
16025 {
16026 /* Use the default LOC_OPTIMIZED_OUT class. */
16027 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16028 if (!suppress_add)
16029 list_to_add = cu->list_in_scope;
442ddf59 16030 }
c906108c
SS
16031 }
16032 break;
16033 case DW_TAG_formal_parameter:
edb3359d
DJ
16034 /* If we are inside a function, mark this as an argument. If
16035 not, we might be looking at an argument to an inlined function
16036 when we do not have enough information to show inlined frames;
16037 pretend it's a local variable in that case so that the user can
16038 still see it. */
16039 if (context_stack_depth > 0
16040 && context_stack[context_stack_depth - 1].name != NULL)
16041 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16042 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16043 if (attr)
16044 {
e7c27a73 16045 var_decode_location (attr, sym, cu);
c906108c 16046 }
e142c38c 16047 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16048 if (attr)
16049 {
e7c27a73 16050 dwarf2_const_value (attr, sym, cu);
c906108c 16051 }
f346a30d 16052
e37fd15a 16053 list_to_add = cu->list_in_scope;
c906108c
SS
16054 break;
16055 case DW_TAG_unspecified_parameters:
16056 /* From varargs functions; gdb doesn't seem to have any
16057 interest in this information, so just ignore it for now.
16058 (FIXME?) */
16059 break;
34eaf542
TT
16060 case DW_TAG_template_type_param:
16061 suppress_add = 1;
16062 /* Fall through. */
c906108c 16063 case DW_TAG_class_type:
680b30c7 16064 case DW_TAG_interface_type:
c906108c
SS
16065 case DW_TAG_structure_type:
16066 case DW_TAG_union_type:
72019c9c 16067 case DW_TAG_set_type:
c906108c 16068 case DW_TAG_enumeration_type:
f1e6e072 16069 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16070 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16071
63d06c5c 16072 {
987504bb 16073 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16074 really ever be static objects: otherwise, if you try
16075 to, say, break of a class's method and you're in a file
16076 which doesn't mention that class, it won't work unless
16077 the check for all static symbols in lookup_symbol_aux
16078 saves you. See the OtherFileClass tests in
16079 gdb.c++/namespace.exp. */
16080
e37fd15a 16081 if (!suppress_add)
34eaf542 16082 {
34eaf542
TT
16083 list_to_add = (cu->list_in_scope == &file_symbols
16084 && (cu->language == language_cplus
16085 || cu->language == language_java)
16086 ? &global_symbols : cu->list_in_scope);
63d06c5c 16087
64382290
TT
16088 /* The semantics of C++ state that "struct foo {
16089 ... }" also defines a typedef for "foo". A Java
16090 class declaration also defines a typedef for the
16091 class. */
16092 if (cu->language == language_cplus
16093 || cu->language == language_java
16094 || cu->language == language_ada)
16095 {
16096 /* The symbol's name is already allocated along
16097 with this objfile, so we don't need to
16098 duplicate it for the type. */
16099 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16100 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16101 }
63d06c5c
DC
16102 }
16103 }
c906108c
SS
16104 break;
16105 case DW_TAG_typedef:
f1e6e072 16106 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16107 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16108 list_to_add = cu->list_in_scope;
63d06c5c 16109 break;
c906108c 16110 case DW_TAG_base_type:
a02abb62 16111 case DW_TAG_subrange_type:
f1e6e072 16112 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16113 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16114 list_to_add = cu->list_in_scope;
c906108c
SS
16115 break;
16116 case DW_TAG_enumerator:
e142c38c 16117 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16118 if (attr)
16119 {
e7c27a73 16120 dwarf2_const_value (attr, sym, cu);
c906108c 16121 }
63d06c5c
DC
16122 {
16123 /* NOTE: carlton/2003-11-10: See comment above in the
16124 DW_TAG_class_type, etc. block. */
16125
e142c38c 16126 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16127 && (cu->language == language_cplus
16128 || cu->language == language_java)
e142c38c 16129 ? &global_symbols : cu->list_in_scope);
63d06c5c 16130 }
c906108c 16131 break;
5c4e30ca 16132 case DW_TAG_namespace:
f1e6e072 16133 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16134 list_to_add = &global_symbols;
5c4e30ca 16135 break;
4357ac6c 16136 case DW_TAG_common_block:
f1e6e072 16137 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16138 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16139 add_symbol_to_list (sym, cu->list_in_scope);
16140 break;
c906108c
SS
16141 default:
16142 /* Not a tag we recognize. Hopefully we aren't processing
16143 trash data, but since we must specifically ignore things
16144 we don't recognize, there is nothing else we should do at
0963b4bd 16145 this point. */
e2e0b3e5 16146 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16147 dwarf_tag_name (die->tag));
c906108c
SS
16148 break;
16149 }
df8a16a1 16150
e37fd15a
SW
16151 if (suppress_add)
16152 {
16153 sym->hash_next = objfile->template_symbols;
16154 objfile->template_symbols = sym;
16155 list_to_add = NULL;
16156 }
16157
16158 if (list_to_add != NULL)
16159 add_symbol_to_list (sym, list_to_add);
16160
df8a16a1
DJ
16161 /* For the benefit of old versions of GCC, check for anonymous
16162 namespaces based on the demangled name. */
4d4ec4e5 16163 if (!cu->processing_has_namespace_info
94af9270 16164 && cu->language == language_cplus)
a10964d1 16165 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16166 }
16167 return (sym);
16168}
16169
34eaf542
TT
16170/* A wrapper for new_symbol_full that always allocates a new symbol. */
16171
16172static struct symbol *
16173new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16174{
16175 return new_symbol_full (die, type, cu, NULL);
16176}
16177
98bfdba5
PA
16178/* Given an attr with a DW_FORM_dataN value in host byte order,
16179 zero-extend it as appropriate for the symbol's type. The DWARF
16180 standard (v4) is not entirely clear about the meaning of using
16181 DW_FORM_dataN for a constant with a signed type, where the type is
16182 wider than the data. The conclusion of a discussion on the DWARF
16183 list was that this is unspecified. We choose to always zero-extend
16184 because that is the interpretation long in use by GCC. */
c906108c 16185
98bfdba5
PA
16186static gdb_byte *
16187dwarf2_const_value_data (struct attribute *attr, struct type *type,
16188 const char *name, struct obstack *obstack,
12df843f 16189 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16190{
e7c27a73 16191 struct objfile *objfile = cu->objfile;
e17a4113
UW
16192 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16193 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16194 LONGEST l = DW_UNSND (attr);
16195
16196 if (bits < sizeof (*value) * 8)
16197 {
16198 l &= ((LONGEST) 1 << bits) - 1;
16199 *value = l;
16200 }
16201 else if (bits == sizeof (*value) * 8)
16202 *value = l;
16203 else
16204 {
16205 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16206 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16207 return bytes;
16208 }
16209
16210 return NULL;
16211}
16212
16213/* Read a constant value from an attribute. Either set *VALUE, or if
16214 the value does not fit in *VALUE, set *BYTES - either already
16215 allocated on the objfile obstack, or newly allocated on OBSTACK,
16216 or, set *BATON, if we translated the constant to a location
16217 expression. */
16218
16219static void
16220dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16221 const char *name, struct obstack *obstack,
16222 struct dwarf2_cu *cu,
12df843f 16223 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16224 struct dwarf2_locexpr_baton **baton)
16225{
16226 struct objfile *objfile = cu->objfile;
16227 struct comp_unit_head *cu_header = &cu->header;
c906108c 16228 struct dwarf_block *blk;
98bfdba5
PA
16229 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16230 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16231
16232 *value = 0;
16233 *bytes = NULL;
16234 *baton = NULL;
c906108c
SS
16235
16236 switch (attr->form)
16237 {
16238 case DW_FORM_addr:
3019eac3 16239 case DW_FORM_GNU_addr_index:
ac56253d 16240 {
ac56253d
TT
16241 gdb_byte *data;
16242
98bfdba5
PA
16243 if (TYPE_LENGTH (type) != cu_header->addr_size)
16244 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16245 cu_header->addr_size,
98bfdba5 16246 TYPE_LENGTH (type));
ac56253d
TT
16247 /* Symbols of this form are reasonably rare, so we just
16248 piggyback on the existing location code rather than writing
16249 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16250 *baton = obstack_alloc (&objfile->objfile_obstack,
16251 sizeof (struct dwarf2_locexpr_baton));
16252 (*baton)->per_cu = cu->per_cu;
16253 gdb_assert ((*baton)->per_cu);
ac56253d 16254
98bfdba5
PA
16255 (*baton)->size = 2 + cu_header->addr_size;
16256 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16257 (*baton)->data = data;
ac56253d
TT
16258
16259 data[0] = DW_OP_addr;
16260 store_unsigned_integer (&data[1], cu_header->addr_size,
16261 byte_order, DW_ADDR (attr));
16262 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16263 }
c906108c 16264 break;
4ac36638 16265 case DW_FORM_string:
93b5768b 16266 case DW_FORM_strp:
3019eac3 16267 case DW_FORM_GNU_str_index:
36586728 16268 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16269 /* DW_STRING is already allocated on the objfile obstack, point
16270 directly to it. */
16271 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16272 break;
c906108c
SS
16273 case DW_FORM_block1:
16274 case DW_FORM_block2:
16275 case DW_FORM_block4:
16276 case DW_FORM_block:
2dc7f7b3 16277 case DW_FORM_exprloc:
c906108c 16278 blk = DW_BLOCK (attr);
98bfdba5
PA
16279 if (TYPE_LENGTH (type) != blk->size)
16280 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16281 TYPE_LENGTH (type));
16282 *bytes = blk->data;
c906108c 16283 break;
2df3850c
JM
16284
16285 /* The DW_AT_const_value attributes are supposed to carry the
16286 symbol's value "represented as it would be on the target
16287 architecture." By the time we get here, it's already been
16288 converted to host endianness, so we just need to sign- or
16289 zero-extend it as appropriate. */
16290 case DW_FORM_data1:
3e43a32a
MS
16291 *bytes = dwarf2_const_value_data (attr, type, name,
16292 obstack, cu, value, 8);
2df3850c 16293 break;
c906108c 16294 case DW_FORM_data2:
3e43a32a
MS
16295 *bytes = dwarf2_const_value_data (attr, type, name,
16296 obstack, cu, value, 16);
2df3850c 16297 break;
c906108c 16298 case DW_FORM_data4:
3e43a32a
MS
16299 *bytes = dwarf2_const_value_data (attr, type, name,
16300 obstack, cu, value, 32);
2df3850c 16301 break;
c906108c 16302 case DW_FORM_data8:
3e43a32a
MS
16303 *bytes = dwarf2_const_value_data (attr, type, name,
16304 obstack, cu, value, 64);
2df3850c
JM
16305 break;
16306
c906108c 16307 case DW_FORM_sdata:
98bfdba5 16308 *value = DW_SND (attr);
2df3850c
JM
16309 break;
16310
c906108c 16311 case DW_FORM_udata:
98bfdba5 16312 *value = DW_UNSND (attr);
c906108c 16313 break;
2df3850c 16314
c906108c 16315 default:
4d3c2250 16316 complaint (&symfile_complaints,
e2e0b3e5 16317 _("unsupported const value attribute form: '%s'"),
4d3c2250 16318 dwarf_form_name (attr->form));
98bfdba5 16319 *value = 0;
c906108c
SS
16320 break;
16321 }
16322}
16323
2df3850c 16324
98bfdba5
PA
16325/* Copy constant value from an attribute to a symbol. */
16326
2df3850c 16327static void
98bfdba5
PA
16328dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16329 struct dwarf2_cu *cu)
2df3850c 16330{
98bfdba5
PA
16331 struct objfile *objfile = cu->objfile;
16332 struct comp_unit_head *cu_header = &cu->header;
12df843f 16333 LONGEST value;
98bfdba5
PA
16334 gdb_byte *bytes;
16335 struct dwarf2_locexpr_baton *baton;
2df3850c 16336
98bfdba5
PA
16337 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16338 SYMBOL_PRINT_NAME (sym),
16339 &objfile->objfile_obstack, cu,
16340 &value, &bytes, &baton);
2df3850c 16341
98bfdba5
PA
16342 if (baton != NULL)
16343 {
98bfdba5 16344 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16345 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16346 }
16347 else if (bytes != NULL)
16348 {
16349 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16350 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16351 }
16352 else
16353 {
16354 SYMBOL_VALUE (sym) = value;
f1e6e072 16355 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16356 }
2df3850c
JM
16357}
16358
c906108c
SS
16359/* Return the type of the die in question using its DW_AT_type attribute. */
16360
16361static struct type *
e7c27a73 16362die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16363{
c906108c 16364 struct attribute *type_attr;
c906108c 16365
e142c38c 16366 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16367 if (!type_attr)
16368 {
16369 /* A missing DW_AT_type represents a void type. */
46bf5051 16370 return objfile_type (cu->objfile)->builtin_void;
c906108c 16371 }
348e048f 16372
673bfd45 16373 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16374}
16375
b4ba55a1
JB
16376/* True iff CU's producer generates GNAT Ada auxiliary information
16377 that allows to find parallel types through that information instead
16378 of having to do expensive parallel lookups by type name. */
16379
16380static int
16381need_gnat_info (struct dwarf2_cu *cu)
16382{
16383 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16384 of GNAT produces this auxiliary information, without any indication
16385 that it is produced. Part of enhancing the FSF version of GNAT
16386 to produce that information will be to put in place an indicator
16387 that we can use in order to determine whether the descriptive type
16388 info is available or not. One suggestion that has been made is
16389 to use a new attribute, attached to the CU die. For now, assume
16390 that the descriptive type info is not available. */
16391 return 0;
16392}
16393
b4ba55a1
JB
16394/* Return the auxiliary type of the die in question using its
16395 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16396 attribute is not present. */
16397
16398static struct type *
16399die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16400{
b4ba55a1 16401 struct attribute *type_attr;
b4ba55a1
JB
16402
16403 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16404 if (!type_attr)
16405 return NULL;
16406
673bfd45 16407 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16408}
16409
16410/* If DIE has a descriptive_type attribute, then set the TYPE's
16411 descriptive type accordingly. */
16412
16413static void
16414set_descriptive_type (struct type *type, struct die_info *die,
16415 struct dwarf2_cu *cu)
16416{
16417 struct type *descriptive_type = die_descriptive_type (die, cu);
16418
16419 if (descriptive_type)
16420 {
16421 ALLOCATE_GNAT_AUX_TYPE (type);
16422 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16423 }
16424}
16425
c906108c
SS
16426/* Return the containing type of the die in question using its
16427 DW_AT_containing_type attribute. */
16428
16429static struct type *
e7c27a73 16430die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16431{
c906108c 16432 struct attribute *type_attr;
c906108c 16433
e142c38c 16434 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16435 if (!type_attr)
16436 error (_("Dwarf Error: Problem turning containing type into gdb type "
16437 "[in module %s]"), cu->objfile->name);
16438
673bfd45 16439 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16440}
16441
673bfd45
DE
16442/* Look up the type of DIE in CU using its type attribute ATTR.
16443 If there is no type substitute an error marker. */
16444
c906108c 16445static struct type *
673bfd45
DE
16446lookup_die_type (struct die_info *die, struct attribute *attr,
16447 struct dwarf2_cu *cu)
c906108c 16448{
bb5ed363 16449 struct objfile *objfile = cu->objfile;
f792889a
DJ
16450 struct type *this_type;
16451
673bfd45
DE
16452 /* First see if we have it cached. */
16453
36586728
TT
16454 if (attr->form == DW_FORM_GNU_ref_alt)
16455 {
16456 struct dwarf2_per_cu_data *per_cu;
16457 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16458
16459 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16460 this_type = get_die_type_at_offset (offset, per_cu);
16461 }
16462 else if (is_ref_attr (attr))
673bfd45 16463 {
b64f50a1 16464 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16465
16466 this_type = get_die_type_at_offset (offset, cu->per_cu);
16467 }
55f1336d 16468 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16469 {
16470 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16471
16472 /* sig_type will be NULL if the signatured type is missing from
16473 the debug info. */
16474 if (sig_type == NULL)
16475 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16476 "at 0x%x [in module %s]"),
b64f50a1 16477 die->offset.sect_off, objfile->name);
673bfd45 16478
3019eac3
DE
16479 gdb_assert (sig_type->per_cu.is_debug_types);
16480 /* If we haven't filled in type_offset_in_section yet, then we
16481 haven't read the type in yet. */
16482 this_type = NULL;
16483 if (sig_type->type_offset_in_section.sect_off != 0)
16484 {
16485 this_type =
16486 get_die_type_at_offset (sig_type->type_offset_in_section,
16487 &sig_type->per_cu);
16488 }
673bfd45
DE
16489 }
16490 else
16491 {
16492 dump_die_for_error (die);
16493 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16494 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16495 }
16496
16497 /* If not cached we need to read it in. */
16498
16499 if (this_type == NULL)
16500 {
16501 struct die_info *type_die;
16502 struct dwarf2_cu *type_cu = cu;
16503
16504 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16505 /* If we found the type now, it's probably because the type came
16506 from an inter-CU reference and the type's CU got expanded before
16507 ours. */
16508 this_type = get_die_type (type_die, type_cu);
16509 if (this_type == NULL)
16510 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16511 }
16512
16513 /* If we still don't have a type use an error marker. */
16514
16515 if (this_type == NULL)
c906108c 16516 {
b00fdb78
TT
16517 char *message, *saved;
16518
16519 /* read_type_die already issued a complaint. */
16520 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16521 objfile->name,
b64f50a1
JK
16522 cu->header.offset.sect_off,
16523 die->offset.sect_off);
bb5ed363 16524 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16525 message, strlen (message));
16526 xfree (message);
16527
bb5ed363 16528 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16529 }
673bfd45 16530
f792889a 16531 return this_type;
c906108c
SS
16532}
16533
673bfd45
DE
16534/* Return the type in DIE, CU.
16535 Returns NULL for invalid types.
16536
16537 This first does a lookup in the appropriate type_hash table,
16538 and only reads the die in if necessary.
16539
16540 NOTE: This can be called when reading in partial or full symbols. */
16541
f792889a 16542static struct type *
e7c27a73 16543read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16544{
f792889a
DJ
16545 struct type *this_type;
16546
16547 this_type = get_die_type (die, cu);
16548 if (this_type)
16549 return this_type;
16550
673bfd45
DE
16551 return read_type_die_1 (die, cu);
16552}
16553
16554/* Read the type in DIE, CU.
16555 Returns NULL for invalid types. */
16556
16557static struct type *
16558read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16559{
16560 struct type *this_type = NULL;
16561
c906108c
SS
16562 switch (die->tag)
16563 {
16564 case DW_TAG_class_type:
680b30c7 16565 case DW_TAG_interface_type:
c906108c
SS
16566 case DW_TAG_structure_type:
16567 case DW_TAG_union_type:
f792889a 16568 this_type = read_structure_type (die, cu);
c906108c
SS
16569 break;
16570 case DW_TAG_enumeration_type:
f792889a 16571 this_type = read_enumeration_type (die, cu);
c906108c
SS
16572 break;
16573 case DW_TAG_subprogram:
16574 case DW_TAG_subroutine_type:
edb3359d 16575 case DW_TAG_inlined_subroutine:
f792889a 16576 this_type = read_subroutine_type (die, cu);
c906108c
SS
16577 break;
16578 case DW_TAG_array_type:
f792889a 16579 this_type = read_array_type (die, cu);
c906108c 16580 break;
72019c9c 16581 case DW_TAG_set_type:
f792889a 16582 this_type = read_set_type (die, cu);
72019c9c 16583 break;
c906108c 16584 case DW_TAG_pointer_type:
f792889a 16585 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16586 break;
16587 case DW_TAG_ptr_to_member_type:
f792889a 16588 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16589 break;
16590 case DW_TAG_reference_type:
f792889a 16591 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16592 break;
16593 case DW_TAG_const_type:
f792889a 16594 this_type = read_tag_const_type (die, cu);
c906108c
SS
16595 break;
16596 case DW_TAG_volatile_type:
f792889a 16597 this_type = read_tag_volatile_type (die, cu);
c906108c 16598 break;
06d66ee9
TT
16599 case DW_TAG_restrict_type:
16600 this_type = read_tag_restrict_type (die, cu);
16601 break;
c906108c 16602 case DW_TAG_string_type:
f792889a 16603 this_type = read_tag_string_type (die, cu);
c906108c
SS
16604 break;
16605 case DW_TAG_typedef:
f792889a 16606 this_type = read_typedef (die, cu);
c906108c 16607 break;
a02abb62 16608 case DW_TAG_subrange_type:
f792889a 16609 this_type = read_subrange_type (die, cu);
a02abb62 16610 break;
c906108c 16611 case DW_TAG_base_type:
f792889a 16612 this_type = read_base_type (die, cu);
c906108c 16613 break;
81a17f79 16614 case DW_TAG_unspecified_type:
f792889a 16615 this_type = read_unspecified_type (die, cu);
81a17f79 16616 break;
0114d602
DJ
16617 case DW_TAG_namespace:
16618 this_type = read_namespace_type (die, cu);
16619 break;
f55ee35c
JK
16620 case DW_TAG_module:
16621 this_type = read_module_type (die, cu);
16622 break;
c906108c 16623 default:
3e43a32a
MS
16624 complaint (&symfile_complaints,
16625 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16626 dwarf_tag_name (die->tag));
c906108c
SS
16627 break;
16628 }
63d06c5c 16629
f792889a 16630 return this_type;
63d06c5c
DC
16631}
16632
abc72ce4
DE
16633/* See if we can figure out if the class lives in a namespace. We do
16634 this by looking for a member function; its demangled name will
16635 contain namespace info, if there is any.
16636 Return the computed name or NULL.
16637 Space for the result is allocated on the objfile's obstack.
16638 This is the full-die version of guess_partial_die_structure_name.
16639 In this case we know DIE has no useful parent. */
16640
16641static char *
16642guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16643{
16644 struct die_info *spec_die;
16645 struct dwarf2_cu *spec_cu;
16646 struct die_info *child;
16647
16648 spec_cu = cu;
16649 spec_die = die_specification (die, &spec_cu);
16650 if (spec_die != NULL)
16651 {
16652 die = spec_die;
16653 cu = spec_cu;
16654 }
16655
16656 for (child = die->child;
16657 child != NULL;
16658 child = child->sibling)
16659 {
16660 if (child->tag == DW_TAG_subprogram)
16661 {
16662 struct attribute *attr;
16663
16664 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16665 if (attr == NULL)
16666 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16667 if (attr != NULL)
16668 {
16669 char *actual_name
16670 = language_class_name_from_physname (cu->language_defn,
16671 DW_STRING (attr));
16672 char *name = NULL;
16673
16674 if (actual_name != NULL)
16675 {
15d034d0 16676 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16677
16678 if (die_name != NULL
16679 && strcmp (die_name, actual_name) != 0)
16680 {
16681 /* Strip off the class name from the full name.
16682 We want the prefix. */
16683 int die_name_len = strlen (die_name);
16684 int actual_name_len = strlen (actual_name);
16685
16686 /* Test for '::' as a sanity check. */
16687 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16688 && actual_name[actual_name_len
16689 - die_name_len - 1] == ':')
abc72ce4 16690 name =
10f0c4bb
TT
16691 obstack_copy0 (&cu->objfile->objfile_obstack,
16692 actual_name,
16693 actual_name_len - die_name_len - 2);
abc72ce4
DE
16694 }
16695 }
16696 xfree (actual_name);
16697 return name;
16698 }
16699 }
16700 }
16701
16702 return NULL;
16703}
16704
96408a79
SA
16705/* GCC might emit a nameless typedef that has a linkage name. Determine the
16706 prefix part in such case. See
16707 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16708
16709static char *
16710anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16711{
16712 struct attribute *attr;
16713 char *base;
16714
16715 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16716 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16717 return NULL;
16718
16719 attr = dwarf2_attr (die, DW_AT_name, cu);
16720 if (attr != NULL && DW_STRING (attr) != NULL)
16721 return NULL;
16722
16723 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16724 if (attr == NULL)
16725 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16726 if (attr == NULL || DW_STRING (attr) == NULL)
16727 return NULL;
16728
16729 /* dwarf2_name had to be already called. */
16730 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16731
16732 /* Strip the base name, keep any leading namespaces/classes. */
16733 base = strrchr (DW_STRING (attr), ':');
16734 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16735 return "";
16736
10f0c4bb
TT
16737 return obstack_copy0 (&cu->objfile->objfile_obstack,
16738 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16739}
16740
fdde2d81 16741/* Return the name of the namespace/class that DIE is defined within,
0114d602 16742 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16743
0114d602
DJ
16744 For example, if we're within the method foo() in the following
16745 code:
16746
16747 namespace N {
16748 class C {
16749 void foo () {
16750 }
16751 };
16752 }
16753
16754 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16755
0d5cff50 16756static const char *
e142c38c 16757determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16758{
0114d602
DJ
16759 struct die_info *parent, *spec_die;
16760 struct dwarf2_cu *spec_cu;
16761 struct type *parent_type;
96408a79 16762 char *retval;
63d06c5c 16763
f55ee35c
JK
16764 if (cu->language != language_cplus && cu->language != language_java
16765 && cu->language != language_fortran)
0114d602
DJ
16766 return "";
16767
96408a79
SA
16768 retval = anonymous_struct_prefix (die, cu);
16769 if (retval)
16770 return retval;
16771
0114d602
DJ
16772 /* We have to be careful in the presence of DW_AT_specification.
16773 For example, with GCC 3.4, given the code
16774
16775 namespace N {
16776 void foo() {
16777 // Definition of N::foo.
16778 }
16779 }
16780
16781 then we'll have a tree of DIEs like this:
16782
16783 1: DW_TAG_compile_unit
16784 2: DW_TAG_namespace // N
16785 3: DW_TAG_subprogram // declaration of N::foo
16786 4: DW_TAG_subprogram // definition of N::foo
16787 DW_AT_specification // refers to die #3
16788
16789 Thus, when processing die #4, we have to pretend that we're in
16790 the context of its DW_AT_specification, namely the contex of die
16791 #3. */
16792 spec_cu = cu;
16793 spec_die = die_specification (die, &spec_cu);
16794 if (spec_die == NULL)
16795 parent = die->parent;
16796 else
63d06c5c 16797 {
0114d602
DJ
16798 parent = spec_die->parent;
16799 cu = spec_cu;
63d06c5c 16800 }
0114d602
DJ
16801
16802 if (parent == NULL)
16803 return "";
98bfdba5
PA
16804 else if (parent->building_fullname)
16805 {
16806 const char *name;
16807 const char *parent_name;
16808
16809 /* It has been seen on RealView 2.2 built binaries,
16810 DW_TAG_template_type_param types actually _defined_ as
16811 children of the parent class:
16812
16813 enum E {};
16814 template class <class Enum> Class{};
16815 Class<enum E> class_e;
16816
16817 1: DW_TAG_class_type (Class)
16818 2: DW_TAG_enumeration_type (E)
16819 3: DW_TAG_enumerator (enum1:0)
16820 3: DW_TAG_enumerator (enum2:1)
16821 ...
16822 2: DW_TAG_template_type_param
16823 DW_AT_type DW_FORM_ref_udata (E)
16824
16825 Besides being broken debug info, it can put GDB into an
16826 infinite loop. Consider:
16827
16828 When we're building the full name for Class<E>, we'll start
16829 at Class, and go look over its template type parameters,
16830 finding E. We'll then try to build the full name of E, and
16831 reach here. We're now trying to build the full name of E,
16832 and look over the parent DIE for containing scope. In the
16833 broken case, if we followed the parent DIE of E, we'd again
16834 find Class, and once again go look at its template type
16835 arguments, etc., etc. Simply don't consider such parent die
16836 as source-level parent of this die (it can't be, the language
16837 doesn't allow it), and break the loop here. */
16838 name = dwarf2_name (die, cu);
16839 parent_name = dwarf2_name (parent, cu);
16840 complaint (&symfile_complaints,
16841 _("template param type '%s' defined within parent '%s'"),
16842 name ? name : "<unknown>",
16843 parent_name ? parent_name : "<unknown>");
16844 return "";
16845 }
63d06c5c 16846 else
0114d602
DJ
16847 switch (parent->tag)
16848 {
63d06c5c 16849 case DW_TAG_namespace:
0114d602 16850 parent_type = read_type_die (parent, cu);
acebe513
UW
16851 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16852 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16853 Work around this problem here. */
16854 if (cu->language == language_cplus
16855 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16856 return "";
0114d602
DJ
16857 /* We give a name to even anonymous namespaces. */
16858 return TYPE_TAG_NAME (parent_type);
63d06c5c 16859 case DW_TAG_class_type:
680b30c7 16860 case DW_TAG_interface_type:
63d06c5c 16861 case DW_TAG_structure_type:
0114d602 16862 case DW_TAG_union_type:
f55ee35c 16863 case DW_TAG_module:
0114d602
DJ
16864 parent_type = read_type_die (parent, cu);
16865 if (TYPE_TAG_NAME (parent_type) != NULL)
16866 return TYPE_TAG_NAME (parent_type);
16867 else
16868 /* An anonymous structure is only allowed non-static data
16869 members; no typedefs, no member functions, et cetera.
16870 So it does not need a prefix. */
16871 return "";
abc72ce4 16872 case DW_TAG_compile_unit:
95554aad 16873 case DW_TAG_partial_unit:
abc72ce4
DE
16874 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16875 if (cu->language == language_cplus
8b70b953 16876 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16877 && die->child != NULL
16878 && (die->tag == DW_TAG_class_type
16879 || die->tag == DW_TAG_structure_type
16880 || die->tag == DW_TAG_union_type))
16881 {
16882 char *name = guess_full_die_structure_name (die, cu);
16883 if (name != NULL)
16884 return name;
16885 }
16886 return "";
63d06c5c 16887 default:
8176b9b8 16888 return determine_prefix (parent, cu);
63d06c5c 16889 }
63d06c5c
DC
16890}
16891
3e43a32a
MS
16892/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16893 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16894 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16895 an obconcat, otherwise allocate storage for the result. The CU argument is
16896 used to determine the language and hence, the appropriate separator. */
987504bb 16897
f55ee35c 16898#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16899
16900static char *
f55ee35c
JK
16901typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16902 int physname, struct dwarf2_cu *cu)
63d06c5c 16903{
f55ee35c 16904 const char *lead = "";
5c315b68 16905 const char *sep;
63d06c5c 16906
3e43a32a
MS
16907 if (suffix == NULL || suffix[0] == '\0'
16908 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16909 sep = "";
16910 else if (cu->language == language_java)
16911 sep = ".";
f55ee35c
JK
16912 else if (cu->language == language_fortran && physname)
16913 {
16914 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16915 DW_AT_MIPS_linkage_name is preferred and used instead. */
16916
16917 lead = "__";
16918 sep = "_MOD_";
16919 }
987504bb
JJ
16920 else
16921 sep = "::";
63d06c5c 16922
6dd47d34
DE
16923 if (prefix == NULL)
16924 prefix = "";
16925 if (suffix == NULL)
16926 suffix = "";
16927
987504bb
JJ
16928 if (obs == NULL)
16929 {
3e43a32a
MS
16930 char *retval
16931 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16932
f55ee35c
JK
16933 strcpy (retval, lead);
16934 strcat (retval, prefix);
6dd47d34
DE
16935 strcat (retval, sep);
16936 strcat (retval, suffix);
63d06c5c
DC
16937 return retval;
16938 }
987504bb
JJ
16939 else
16940 {
16941 /* We have an obstack. */
f55ee35c 16942 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16943 }
63d06c5c
DC
16944}
16945
c906108c
SS
16946/* Return sibling of die, NULL if no sibling. */
16947
f9aca02d 16948static struct die_info *
fba45db2 16949sibling_die (struct die_info *die)
c906108c 16950{
639d11d3 16951 return die->sibling;
c906108c
SS
16952}
16953
71c25dea
TT
16954/* Get name of a die, return NULL if not found. */
16955
15d034d0
TT
16956static const char *
16957dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
16958 struct obstack *obstack)
16959{
16960 if (name && cu->language == language_cplus)
16961 {
16962 char *canon_name = cp_canonicalize_string (name);
16963
16964 if (canon_name != NULL)
16965 {
16966 if (strcmp (canon_name, name) != 0)
10f0c4bb 16967 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
16968 xfree (canon_name);
16969 }
16970 }
16971
16972 return name;
c906108c
SS
16973}
16974
9219021c
DC
16975/* Get name of a die, return NULL if not found. */
16976
15d034d0 16977static const char *
e142c38c 16978dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16979{
16980 struct attribute *attr;
16981
e142c38c 16982 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16983 if ((!attr || !DW_STRING (attr))
16984 && die->tag != DW_TAG_class_type
16985 && die->tag != DW_TAG_interface_type
16986 && die->tag != DW_TAG_structure_type
16987 && die->tag != DW_TAG_union_type)
71c25dea
TT
16988 return NULL;
16989
16990 switch (die->tag)
16991 {
16992 case DW_TAG_compile_unit:
95554aad 16993 case DW_TAG_partial_unit:
71c25dea
TT
16994 /* Compilation units have a DW_AT_name that is a filename, not
16995 a source language identifier. */
16996 case DW_TAG_enumeration_type:
16997 case DW_TAG_enumerator:
16998 /* These tags always have simple identifiers already; no need
16999 to canonicalize them. */
17000 return DW_STRING (attr);
907af001 17001
418835cc
KS
17002 case DW_TAG_subprogram:
17003 /* Java constructors will all be named "<init>", so return
17004 the class name when we see this special case. */
17005 if (cu->language == language_java
17006 && DW_STRING (attr) != NULL
17007 && strcmp (DW_STRING (attr), "<init>") == 0)
17008 {
17009 struct dwarf2_cu *spec_cu = cu;
17010 struct die_info *spec_die;
17011
17012 /* GCJ will output '<init>' for Java constructor names.
17013 For this special case, return the name of the parent class. */
17014
17015 /* GCJ may output suprogram DIEs with AT_specification set.
17016 If so, use the name of the specified DIE. */
17017 spec_die = die_specification (die, &spec_cu);
17018 if (spec_die != NULL)
17019 return dwarf2_name (spec_die, spec_cu);
17020
17021 do
17022 {
17023 die = die->parent;
17024 if (die->tag == DW_TAG_class_type)
17025 return dwarf2_name (die, cu);
17026 }
95554aad
TT
17027 while (die->tag != DW_TAG_compile_unit
17028 && die->tag != DW_TAG_partial_unit);
418835cc 17029 }
907af001
UW
17030 break;
17031
17032 case DW_TAG_class_type:
17033 case DW_TAG_interface_type:
17034 case DW_TAG_structure_type:
17035 case DW_TAG_union_type:
17036 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17037 structures or unions. These were of the form "._%d" in GCC 4.1,
17038 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17039 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17040 if (attr && DW_STRING (attr)
17041 && (strncmp (DW_STRING (attr), "._", 2) == 0
17042 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17043 return NULL;
53832f31
TT
17044
17045 /* GCC might emit a nameless typedef that has a linkage name. See
17046 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17047 if (!attr || DW_STRING (attr) == NULL)
17048 {
df5c6c50 17049 char *demangled = NULL;
53832f31
TT
17050
17051 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17052 if (attr == NULL)
17053 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17054
17055 if (attr == NULL || DW_STRING (attr) == NULL)
17056 return NULL;
17057
df5c6c50
JK
17058 /* Avoid demangling DW_STRING (attr) the second time on a second
17059 call for the same DIE. */
17060 if (!DW_STRING_IS_CANONICAL (attr))
17061 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17062
17063 if (demangled)
17064 {
96408a79
SA
17065 char *base;
17066
53832f31 17067 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17068 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17069 demangled, strlen (demangled));
53832f31
TT
17070 DW_STRING_IS_CANONICAL (attr) = 1;
17071 xfree (demangled);
96408a79
SA
17072
17073 /* Strip any leading namespaces/classes, keep only the base name.
17074 DW_AT_name for named DIEs does not contain the prefixes. */
17075 base = strrchr (DW_STRING (attr), ':');
17076 if (base && base > DW_STRING (attr) && base[-1] == ':')
17077 return &base[1];
17078 else
17079 return DW_STRING (attr);
53832f31
TT
17080 }
17081 }
907af001
UW
17082 break;
17083
71c25dea 17084 default:
907af001
UW
17085 break;
17086 }
17087
17088 if (!DW_STRING_IS_CANONICAL (attr))
17089 {
17090 DW_STRING (attr)
17091 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17092 &cu->objfile->objfile_obstack);
17093 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17094 }
907af001 17095 return DW_STRING (attr);
9219021c
DC
17096}
17097
17098/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17099 is none. *EXT_CU is the CU containing DIE on input, and the CU
17100 containing the return value on output. */
9219021c
DC
17101
17102static struct die_info *
f2f0e013 17103dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17104{
17105 struct attribute *attr;
9219021c 17106
f2f0e013 17107 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17108 if (attr == NULL)
17109 return NULL;
17110
f2f0e013 17111 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17112}
17113
c906108c
SS
17114/* Convert a DIE tag into its string name. */
17115
f39c6ffd 17116static const char *
aa1ee363 17117dwarf_tag_name (unsigned tag)
c906108c 17118{
f39c6ffd
TT
17119 const char *name = get_DW_TAG_name (tag);
17120
17121 if (name == NULL)
17122 return "DW_TAG_<unknown>";
17123
17124 return name;
c906108c
SS
17125}
17126
17127/* Convert a DWARF attribute code into its string name. */
17128
f39c6ffd 17129static const char *
aa1ee363 17130dwarf_attr_name (unsigned attr)
c906108c 17131{
f39c6ffd
TT
17132 const char *name;
17133
c764a876 17134#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17135 if (attr == DW_AT_MIPS_fde)
17136 return "DW_AT_MIPS_fde";
17137#else
17138 if (attr == DW_AT_HP_block_index)
17139 return "DW_AT_HP_block_index";
c764a876 17140#endif
f39c6ffd
TT
17141
17142 name = get_DW_AT_name (attr);
17143
17144 if (name == NULL)
17145 return "DW_AT_<unknown>";
17146
17147 return name;
c906108c
SS
17148}
17149
17150/* Convert a DWARF value form code into its string name. */
17151
f39c6ffd 17152static const char *
aa1ee363 17153dwarf_form_name (unsigned form)
c906108c 17154{
f39c6ffd
TT
17155 const char *name = get_DW_FORM_name (form);
17156
17157 if (name == NULL)
17158 return "DW_FORM_<unknown>";
17159
17160 return name;
c906108c
SS
17161}
17162
17163static char *
fba45db2 17164dwarf_bool_name (unsigned mybool)
c906108c
SS
17165{
17166 if (mybool)
17167 return "TRUE";
17168 else
17169 return "FALSE";
17170}
17171
17172/* Convert a DWARF type code into its string name. */
17173
f39c6ffd 17174static const char *
aa1ee363 17175dwarf_type_encoding_name (unsigned enc)
c906108c 17176{
f39c6ffd 17177 const char *name = get_DW_ATE_name (enc);
c906108c 17178
f39c6ffd
TT
17179 if (name == NULL)
17180 return "DW_ATE_<unknown>";
c906108c 17181
f39c6ffd 17182 return name;
c906108c 17183}
c906108c 17184
f9aca02d 17185static void
d97bc12b 17186dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17187{
17188 unsigned int i;
17189
d97bc12b
DE
17190 print_spaces (indent, f);
17191 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17192 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17193
17194 if (die->parent != NULL)
17195 {
17196 print_spaces (indent, f);
17197 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17198 die->parent->offset.sect_off);
d97bc12b
DE
17199 }
17200
17201 print_spaces (indent, f);
17202 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17203 dwarf_bool_name (die->child != NULL));
c906108c 17204
d97bc12b
DE
17205 print_spaces (indent, f);
17206 fprintf_unfiltered (f, " attributes:\n");
17207
c906108c
SS
17208 for (i = 0; i < die->num_attrs; ++i)
17209 {
d97bc12b
DE
17210 print_spaces (indent, f);
17211 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17212 dwarf_attr_name (die->attrs[i].name),
17213 dwarf_form_name (die->attrs[i].form));
d97bc12b 17214
c906108c
SS
17215 switch (die->attrs[i].form)
17216 {
c906108c 17217 case DW_FORM_addr:
3019eac3 17218 case DW_FORM_GNU_addr_index:
d97bc12b 17219 fprintf_unfiltered (f, "address: ");
5af949e3 17220 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17221 break;
17222 case DW_FORM_block2:
17223 case DW_FORM_block4:
17224 case DW_FORM_block:
17225 case DW_FORM_block1:
56eb65bd
SP
17226 fprintf_unfiltered (f, "block: size %s",
17227 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17228 break;
2dc7f7b3 17229 case DW_FORM_exprloc:
56eb65bd
SP
17230 fprintf_unfiltered (f, "expression: size %s",
17231 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17232 break;
4568ecf9
DE
17233 case DW_FORM_ref_addr:
17234 fprintf_unfiltered (f, "ref address: ");
17235 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17236 break;
36586728
TT
17237 case DW_FORM_GNU_ref_alt:
17238 fprintf_unfiltered (f, "alt ref address: ");
17239 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17240 break;
10b3939b
DJ
17241 case DW_FORM_ref1:
17242 case DW_FORM_ref2:
17243 case DW_FORM_ref4:
4568ecf9
DE
17244 case DW_FORM_ref8:
17245 case DW_FORM_ref_udata:
d97bc12b 17246 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17247 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17248 break;
c906108c
SS
17249 case DW_FORM_data1:
17250 case DW_FORM_data2:
17251 case DW_FORM_data4:
ce5d95e1 17252 case DW_FORM_data8:
c906108c
SS
17253 case DW_FORM_udata:
17254 case DW_FORM_sdata:
43bbcdc2
PH
17255 fprintf_unfiltered (f, "constant: %s",
17256 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17257 break;
2dc7f7b3
TT
17258 case DW_FORM_sec_offset:
17259 fprintf_unfiltered (f, "section offset: %s",
17260 pulongest (DW_UNSND (&die->attrs[i])));
17261 break;
55f1336d 17262 case DW_FORM_ref_sig8:
348e048f 17263 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
e4a48d9d
DE
17264 {
17265 struct signatured_type *sig_type =
17266 DW_SIGNATURED_TYPE (&die->attrs[i]);
17267
17268 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17269 hex_string (sig_type->signature),
17270 sig_type->per_cu.offset.sect_off);
17271 }
348e048f 17272 else
e4a48d9d 17273 fprintf_unfiltered (f, "signatured type, unknown");
348e048f 17274 break;
c906108c 17275 case DW_FORM_string:
4bdf3d34 17276 case DW_FORM_strp:
3019eac3 17277 case DW_FORM_GNU_str_index:
36586728 17278 case DW_FORM_GNU_strp_alt:
8285870a 17279 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17280 DW_STRING (&die->attrs[i])
8285870a
JK
17281 ? DW_STRING (&die->attrs[i]) : "",
17282 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17283 break;
17284 case DW_FORM_flag:
17285 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17286 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17287 else
d97bc12b 17288 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17289 break;
2dc7f7b3
TT
17290 case DW_FORM_flag_present:
17291 fprintf_unfiltered (f, "flag: TRUE");
17292 break;
a8329558 17293 case DW_FORM_indirect:
0963b4bd
MS
17294 /* The reader will have reduced the indirect form to
17295 the "base form" so this form should not occur. */
3e43a32a
MS
17296 fprintf_unfiltered (f,
17297 "unexpected attribute form: DW_FORM_indirect");
a8329558 17298 break;
c906108c 17299 default:
d97bc12b 17300 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17301 die->attrs[i].form);
d97bc12b 17302 break;
c906108c 17303 }
d97bc12b 17304 fprintf_unfiltered (f, "\n");
c906108c
SS
17305 }
17306}
17307
f9aca02d 17308static void
d97bc12b 17309dump_die_for_error (struct die_info *die)
c906108c 17310{
d97bc12b
DE
17311 dump_die_shallow (gdb_stderr, 0, die);
17312}
17313
17314static void
17315dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17316{
17317 int indent = level * 4;
17318
17319 gdb_assert (die != NULL);
17320
17321 if (level >= max_level)
17322 return;
17323
17324 dump_die_shallow (f, indent, die);
17325
17326 if (die->child != NULL)
c906108c 17327 {
d97bc12b
DE
17328 print_spaces (indent, f);
17329 fprintf_unfiltered (f, " Children:");
17330 if (level + 1 < max_level)
17331 {
17332 fprintf_unfiltered (f, "\n");
17333 dump_die_1 (f, level + 1, max_level, die->child);
17334 }
17335 else
17336 {
3e43a32a
MS
17337 fprintf_unfiltered (f,
17338 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17339 }
17340 }
17341
17342 if (die->sibling != NULL && level > 0)
17343 {
17344 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17345 }
17346}
17347
d97bc12b
DE
17348/* This is called from the pdie macro in gdbinit.in.
17349 It's not static so gcc will keep a copy callable from gdb. */
17350
17351void
17352dump_die (struct die_info *die, int max_level)
17353{
17354 dump_die_1 (gdb_stdlog, 0, max_level, die);
17355}
17356
f9aca02d 17357static void
51545339 17358store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17359{
51545339 17360 void **slot;
c906108c 17361
b64f50a1
JK
17362 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17363 INSERT);
51545339
DJ
17364
17365 *slot = die;
c906108c
SS
17366}
17367
b64f50a1
JK
17368/* DW_ADDR is always stored already as sect_offset; despite for the forms
17369 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17370
93311388
DE
17371static int
17372is_ref_attr (struct attribute *attr)
c906108c 17373{
c906108c
SS
17374 switch (attr->form)
17375 {
17376 case DW_FORM_ref_addr:
c906108c
SS
17377 case DW_FORM_ref1:
17378 case DW_FORM_ref2:
17379 case DW_FORM_ref4:
613e1657 17380 case DW_FORM_ref8:
c906108c 17381 case DW_FORM_ref_udata:
36586728 17382 case DW_FORM_GNU_ref_alt:
93311388 17383 return 1;
c906108c 17384 default:
93311388 17385 return 0;
c906108c 17386 }
93311388
DE
17387}
17388
b64f50a1
JK
17389/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17390 required kind. */
17391
17392static sect_offset
93311388
DE
17393dwarf2_get_ref_die_offset (struct attribute *attr)
17394{
4568ecf9 17395 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17396
93311388 17397 if (is_ref_attr (attr))
b64f50a1 17398 return retval;
93311388 17399
b64f50a1 17400 retval.sect_off = 0;
93311388
DE
17401 complaint (&symfile_complaints,
17402 _("unsupported die ref attribute form: '%s'"),
17403 dwarf_form_name (attr->form));
b64f50a1 17404 return retval;
c906108c
SS
17405}
17406
43bbcdc2
PH
17407/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17408 * the value held by the attribute is not constant. */
a02abb62 17409
43bbcdc2 17410static LONGEST
a02abb62
JB
17411dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17412{
17413 if (attr->form == DW_FORM_sdata)
17414 return DW_SND (attr);
17415 else if (attr->form == DW_FORM_udata
17416 || attr->form == DW_FORM_data1
17417 || attr->form == DW_FORM_data2
17418 || attr->form == DW_FORM_data4
17419 || attr->form == DW_FORM_data8)
17420 return DW_UNSND (attr);
17421 else
17422 {
3e43a32a
MS
17423 complaint (&symfile_complaints,
17424 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17425 dwarf_form_name (attr->form));
17426 return default_value;
17427 }
17428}
17429
348e048f
DE
17430/* Follow reference or signature attribute ATTR of SRC_DIE.
17431 On entry *REF_CU is the CU of SRC_DIE.
17432 On exit *REF_CU is the CU of the result. */
17433
17434static struct die_info *
17435follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17436 struct dwarf2_cu **ref_cu)
17437{
17438 struct die_info *die;
17439
17440 if (is_ref_attr (attr))
17441 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17442 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17443 die = follow_die_sig (src_die, attr, ref_cu);
17444 else
17445 {
17446 dump_die_for_error (src_die);
17447 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17448 (*ref_cu)->objfile->name);
17449 }
17450
17451 return die;
03dd20cc
DJ
17452}
17453
5c631832 17454/* Follow reference OFFSET.
673bfd45
DE
17455 On entry *REF_CU is the CU of the source die referencing OFFSET.
17456 On exit *REF_CU is the CU of the result.
17457 Returns NULL if OFFSET is invalid. */
f504f079 17458
f9aca02d 17459static struct die_info *
36586728
TT
17460follow_die_offset (sect_offset offset, int offset_in_dwz,
17461 struct dwarf2_cu **ref_cu)
c906108c 17462{
10b3939b 17463 struct die_info temp_die;
f2f0e013 17464 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17465
348e048f
DE
17466 gdb_assert (cu->per_cu != NULL);
17467
98bfdba5
PA
17468 target_cu = cu;
17469
3019eac3 17470 if (cu->per_cu->is_debug_types)
348e048f
DE
17471 {
17472 /* .debug_types CUs cannot reference anything outside their CU.
17473 If they need to, they have to reference a signatured type via
55f1336d 17474 DW_FORM_ref_sig8. */
348e048f 17475 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17476 return NULL;
348e048f 17477 }
36586728
TT
17478 else if (offset_in_dwz != cu->per_cu->is_dwz
17479 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17480 {
17481 struct dwarf2_per_cu_data *per_cu;
9a619af0 17482
36586728
TT
17483 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17484 cu->objfile);
03dd20cc
DJ
17485
17486 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17487 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17488 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17489
10b3939b
DJ
17490 target_cu = per_cu->cu;
17491 }
98bfdba5
PA
17492 else if (cu->dies == NULL)
17493 {
17494 /* We're loading full DIEs during partial symbol reading. */
17495 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17496 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17497 }
c906108c 17498
f2f0e013 17499 *ref_cu = target_cu;
51545339 17500 temp_die.offset = offset;
b64f50a1 17501 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17502}
10b3939b 17503
5c631832
JK
17504/* Follow reference attribute ATTR of SRC_DIE.
17505 On entry *REF_CU is the CU of SRC_DIE.
17506 On exit *REF_CU is the CU of the result. */
17507
17508static struct die_info *
17509follow_die_ref (struct die_info *src_die, struct attribute *attr,
17510 struct dwarf2_cu **ref_cu)
17511{
b64f50a1 17512 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17513 struct dwarf2_cu *cu = *ref_cu;
17514 struct die_info *die;
17515
36586728
TT
17516 die = follow_die_offset (offset,
17517 (attr->form == DW_FORM_GNU_ref_alt
17518 || cu->per_cu->is_dwz),
17519 ref_cu);
5c631832
JK
17520 if (!die)
17521 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17522 "at 0x%x [in module %s]"),
b64f50a1 17523 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17524
5c631832
JK
17525 return die;
17526}
17527
d83e736b
JK
17528/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17529 Returned value is intended for DW_OP_call*. Returned
17530 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17531
17532struct dwarf2_locexpr_baton
8b9737bf
TT
17533dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17534 struct dwarf2_per_cu_data *per_cu,
17535 CORE_ADDR (*get_frame_pc) (void *baton),
17536 void *baton)
5c631832 17537{
918dd910 17538 struct dwarf2_cu *cu;
5c631832
JK
17539 struct die_info *die;
17540 struct attribute *attr;
17541 struct dwarf2_locexpr_baton retval;
17542
8cf6f0b1
TT
17543 dw2_setup (per_cu->objfile);
17544
918dd910
JK
17545 if (per_cu->cu == NULL)
17546 load_cu (per_cu);
17547 cu = per_cu->cu;
17548
36586728 17549 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17550 if (!die)
17551 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17552 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17553
17554 attr = dwarf2_attr (die, DW_AT_location, cu);
17555 if (!attr)
17556 {
e103e986
JK
17557 /* DWARF: "If there is no such attribute, then there is no effect.".
17558 DATA is ignored if SIZE is 0. */
5c631832 17559
e103e986 17560 retval.data = NULL;
5c631832
JK
17561 retval.size = 0;
17562 }
8cf6f0b1
TT
17563 else if (attr_form_is_section_offset (attr))
17564 {
17565 struct dwarf2_loclist_baton loclist_baton;
17566 CORE_ADDR pc = (*get_frame_pc) (baton);
17567 size_t size;
17568
17569 fill_in_loclist_baton (cu, &loclist_baton, attr);
17570
17571 retval.data = dwarf2_find_location_expression (&loclist_baton,
17572 &size, pc);
17573 retval.size = size;
17574 }
5c631832
JK
17575 else
17576 {
17577 if (!attr_form_is_block (attr))
17578 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17579 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17580 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17581
17582 retval.data = DW_BLOCK (attr)->data;
17583 retval.size = DW_BLOCK (attr)->size;
17584 }
17585 retval.per_cu = cu->per_cu;
918dd910 17586
918dd910
JK
17587 age_cached_comp_units ();
17588
5c631832 17589 return retval;
348e048f
DE
17590}
17591
8b9737bf
TT
17592/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17593 offset. */
17594
17595struct dwarf2_locexpr_baton
17596dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17597 struct dwarf2_per_cu_data *per_cu,
17598 CORE_ADDR (*get_frame_pc) (void *baton),
17599 void *baton)
17600{
17601 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17602
17603 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17604}
17605
8a9b8146
TT
17606/* Return the type of the DIE at DIE_OFFSET in the CU named by
17607 PER_CU. */
17608
17609struct type *
b64f50a1 17610dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17611 struct dwarf2_per_cu_data *per_cu)
17612{
b64f50a1
JK
17613 sect_offset die_offset_sect;
17614
8a9b8146 17615 dw2_setup (per_cu->objfile);
b64f50a1
JK
17616
17617 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17618 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17619}
17620
348e048f
DE
17621/* Follow the signature attribute ATTR in SRC_DIE.
17622 On entry *REF_CU is the CU of SRC_DIE.
17623 On exit *REF_CU is the CU of the result. */
17624
17625static struct die_info *
17626follow_die_sig (struct die_info *src_die, struct attribute *attr,
17627 struct dwarf2_cu **ref_cu)
17628{
17629 struct objfile *objfile = (*ref_cu)->objfile;
17630 struct die_info temp_die;
17631 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17632 struct dwarf2_cu *sig_cu;
17633 struct die_info *die;
17634
17635 /* sig_type will be NULL if the signatured type is missing from
17636 the debug info. */
17637 if (sig_type == NULL)
17638 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17639 "at 0x%x [in module %s]"),
b64f50a1 17640 src_die->offset.sect_off, objfile->name);
348e048f
DE
17641
17642 /* If necessary, add it to the queue and load its DIEs. */
17643
95554aad 17644 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17645 read_signatured_type (sig_type);
348e048f
DE
17646
17647 gdb_assert (sig_type->per_cu.cu != NULL);
17648
17649 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17650 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17651 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17652 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17653 temp_die.offset.sect_off);
348e048f
DE
17654 if (die)
17655 {
796a7ff8
DE
17656 /* For .gdb_index version 7 keep track of included TUs.
17657 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17658 if (dwarf2_per_objfile->index_table != NULL
17659 && dwarf2_per_objfile->index_table->version <= 7)
17660 {
17661 VEC_safe_push (dwarf2_per_cu_ptr,
17662 (*ref_cu)->per_cu->imported_symtabs,
17663 sig_cu->per_cu);
17664 }
17665
348e048f
DE
17666 *ref_cu = sig_cu;
17667 return die;
17668 }
17669
3e43a32a
MS
17670 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17671 "from DIE at 0x%x [in module %s]"),
b64f50a1 17672 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17673}
17674
17675/* Given an offset of a signatured type, return its signatured_type. */
17676
17677static struct signatured_type *
8b70b953
TT
17678lookup_signatured_type_at_offset (struct objfile *objfile,
17679 struct dwarf2_section_info *section,
b64f50a1 17680 sect_offset offset)
348e048f 17681{
b64f50a1 17682 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17683 unsigned int length, initial_length_size;
17684 unsigned int sig_offset;
52dc124a 17685 struct signatured_type find_entry, *sig_type;
348e048f
DE
17686
17687 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17688 sig_offset = (initial_length_size
17689 + 2 /*version*/
17690 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17691 + 1 /*address_size*/);
17692 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17693 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17694
17695 /* This is only used to lookup previously recorded types.
17696 If we didn't find it, it's our bug. */
52dc124a
DE
17697 gdb_assert (sig_type != NULL);
17698 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17699
52dc124a 17700 return sig_type;
348e048f
DE
17701}
17702
e5fe5e75 17703/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17704
17705static void
e5fe5e75 17706load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17707{
52dc124a 17708 struct signatured_type *sig_type;
348e048f 17709
f4dc4d17
DE
17710 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17711 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17712
6721b2ec
DE
17713 /* We have the per_cu, but we need the signatured_type.
17714 Fortunately this is an easy translation. */
17715 gdb_assert (per_cu->is_debug_types);
17716 sig_type = (struct signatured_type *) per_cu;
348e048f 17717
6721b2ec 17718 gdb_assert (per_cu->cu == NULL);
348e048f 17719
52dc124a 17720 read_signatured_type (sig_type);
348e048f 17721
6721b2ec 17722 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17723}
17724
dee91e82
DE
17725/* die_reader_func for read_signatured_type.
17726 This is identical to load_full_comp_unit_reader,
17727 but is kept separate for now. */
348e048f
DE
17728
17729static void
dee91e82
DE
17730read_signatured_type_reader (const struct die_reader_specs *reader,
17731 gdb_byte *info_ptr,
17732 struct die_info *comp_unit_die,
17733 int has_children,
17734 void *data)
348e048f 17735{
dee91e82 17736 struct dwarf2_cu *cu = reader->cu;
348e048f 17737
dee91e82
DE
17738 gdb_assert (cu->die_hash == NULL);
17739 cu->die_hash =
17740 htab_create_alloc_ex (cu->header.length / 12,
17741 die_hash,
17742 die_eq,
17743 NULL,
17744 &cu->comp_unit_obstack,
17745 hashtab_obstack_allocate,
17746 dummy_obstack_deallocate);
348e048f 17747
dee91e82
DE
17748 if (has_children)
17749 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17750 &info_ptr, comp_unit_die);
17751 cu->dies = comp_unit_die;
17752 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17753
17754 /* We try not to read any attributes in this function, because not
9cdd5dbd 17755 all CUs needed for references have been loaded yet, and symbol
348e048f 17756 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17757 or we won't be able to build types correctly.
17758 Similarly, if we do not read the producer, we can not apply
17759 producer-specific interpretation. */
95554aad 17760 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17761}
348e048f 17762
3019eac3
DE
17763/* Read in a signatured type and build its CU and DIEs.
17764 If the type is a stub for the real type in a DWO file,
17765 read in the real type from the DWO file as well. */
dee91e82
DE
17766
17767static void
17768read_signatured_type (struct signatured_type *sig_type)
17769{
17770 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17771
3019eac3 17772 gdb_assert (per_cu->is_debug_types);
dee91e82 17773 gdb_assert (per_cu->cu == NULL);
348e048f 17774
f4dc4d17
DE
17775 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17776 read_signatured_type_reader, NULL);
c906108c
SS
17777}
17778
c906108c
SS
17779/* Decode simple location descriptions.
17780 Given a pointer to a dwarf block that defines a location, compute
17781 the location and return the value.
17782
4cecd739
DJ
17783 NOTE drow/2003-11-18: This function is called in two situations
17784 now: for the address of static or global variables (partial symbols
17785 only) and for offsets into structures which are expected to be
17786 (more or less) constant. The partial symbol case should go away,
17787 and only the constant case should remain. That will let this
17788 function complain more accurately. A few special modes are allowed
17789 without complaint for global variables (for instance, global
17790 register values and thread-local values).
c906108c
SS
17791
17792 A location description containing no operations indicates that the
4cecd739 17793 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17794 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17795 callers will only want a very basic result and this can become a
21ae7a4d
JK
17796 complaint.
17797
17798 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17799
17800static CORE_ADDR
e7c27a73 17801decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17802{
e7c27a73 17803 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17804 size_t i;
17805 size_t size = blk->size;
21ae7a4d
JK
17806 gdb_byte *data = blk->data;
17807 CORE_ADDR stack[64];
17808 int stacki;
17809 unsigned int bytes_read, unsnd;
17810 gdb_byte op;
c906108c 17811
21ae7a4d
JK
17812 i = 0;
17813 stacki = 0;
17814 stack[stacki] = 0;
17815 stack[++stacki] = 0;
17816
17817 while (i < size)
17818 {
17819 op = data[i++];
17820 switch (op)
17821 {
17822 case DW_OP_lit0:
17823 case DW_OP_lit1:
17824 case DW_OP_lit2:
17825 case DW_OP_lit3:
17826 case DW_OP_lit4:
17827 case DW_OP_lit5:
17828 case DW_OP_lit6:
17829 case DW_OP_lit7:
17830 case DW_OP_lit8:
17831 case DW_OP_lit9:
17832 case DW_OP_lit10:
17833 case DW_OP_lit11:
17834 case DW_OP_lit12:
17835 case DW_OP_lit13:
17836 case DW_OP_lit14:
17837 case DW_OP_lit15:
17838 case DW_OP_lit16:
17839 case DW_OP_lit17:
17840 case DW_OP_lit18:
17841 case DW_OP_lit19:
17842 case DW_OP_lit20:
17843 case DW_OP_lit21:
17844 case DW_OP_lit22:
17845 case DW_OP_lit23:
17846 case DW_OP_lit24:
17847 case DW_OP_lit25:
17848 case DW_OP_lit26:
17849 case DW_OP_lit27:
17850 case DW_OP_lit28:
17851 case DW_OP_lit29:
17852 case DW_OP_lit30:
17853 case DW_OP_lit31:
17854 stack[++stacki] = op - DW_OP_lit0;
17855 break;
f1bea926 17856
21ae7a4d
JK
17857 case DW_OP_reg0:
17858 case DW_OP_reg1:
17859 case DW_OP_reg2:
17860 case DW_OP_reg3:
17861 case DW_OP_reg4:
17862 case DW_OP_reg5:
17863 case DW_OP_reg6:
17864 case DW_OP_reg7:
17865 case DW_OP_reg8:
17866 case DW_OP_reg9:
17867 case DW_OP_reg10:
17868 case DW_OP_reg11:
17869 case DW_OP_reg12:
17870 case DW_OP_reg13:
17871 case DW_OP_reg14:
17872 case DW_OP_reg15:
17873 case DW_OP_reg16:
17874 case DW_OP_reg17:
17875 case DW_OP_reg18:
17876 case DW_OP_reg19:
17877 case DW_OP_reg20:
17878 case DW_OP_reg21:
17879 case DW_OP_reg22:
17880 case DW_OP_reg23:
17881 case DW_OP_reg24:
17882 case DW_OP_reg25:
17883 case DW_OP_reg26:
17884 case DW_OP_reg27:
17885 case DW_OP_reg28:
17886 case DW_OP_reg29:
17887 case DW_OP_reg30:
17888 case DW_OP_reg31:
17889 stack[++stacki] = op - DW_OP_reg0;
17890 if (i < size)
17891 dwarf2_complex_location_expr_complaint ();
17892 break;
c906108c 17893
21ae7a4d
JK
17894 case DW_OP_regx:
17895 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17896 i += bytes_read;
17897 stack[++stacki] = unsnd;
17898 if (i < size)
17899 dwarf2_complex_location_expr_complaint ();
17900 break;
c906108c 17901
21ae7a4d
JK
17902 case DW_OP_addr:
17903 stack[++stacki] = read_address (objfile->obfd, &data[i],
17904 cu, &bytes_read);
17905 i += bytes_read;
17906 break;
d53d4ac5 17907
21ae7a4d
JK
17908 case DW_OP_const1u:
17909 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17910 i += 1;
17911 break;
17912
17913 case DW_OP_const1s:
17914 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17915 i += 1;
17916 break;
17917
17918 case DW_OP_const2u:
17919 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17920 i += 2;
17921 break;
17922
17923 case DW_OP_const2s:
17924 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17925 i += 2;
17926 break;
d53d4ac5 17927
21ae7a4d
JK
17928 case DW_OP_const4u:
17929 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17930 i += 4;
17931 break;
17932
17933 case DW_OP_const4s:
17934 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17935 i += 4;
17936 break;
17937
585861ea
JK
17938 case DW_OP_const8u:
17939 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17940 i += 8;
17941 break;
17942
21ae7a4d
JK
17943 case DW_OP_constu:
17944 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17945 &bytes_read);
17946 i += bytes_read;
17947 break;
17948
17949 case DW_OP_consts:
17950 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17951 i += bytes_read;
17952 break;
17953
17954 case DW_OP_dup:
17955 stack[stacki + 1] = stack[stacki];
17956 stacki++;
17957 break;
17958
17959 case DW_OP_plus:
17960 stack[stacki - 1] += stack[stacki];
17961 stacki--;
17962 break;
17963
17964 case DW_OP_plus_uconst:
17965 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17966 &bytes_read);
17967 i += bytes_read;
17968 break;
17969
17970 case DW_OP_minus:
17971 stack[stacki - 1] -= stack[stacki];
17972 stacki--;
17973 break;
17974
17975 case DW_OP_deref:
17976 /* If we're not the last op, then we definitely can't encode
17977 this using GDB's address_class enum. This is valid for partial
17978 global symbols, although the variable's address will be bogus
17979 in the psymtab. */
17980 if (i < size)
17981 dwarf2_complex_location_expr_complaint ();
17982 break;
17983
17984 case DW_OP_GNU_push_tls_address:
17985 /* The top of the stack has the offset from the beginning
17986 of the thread control block at which the variable is located. */
17987 /* Nothing should follow this operator, so the top of stack would
17988 be returned. */
17989 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17990 address will be bogus in the psymtab. Make it always at least
17991 non-zero to not look as a variable garbage collected by linker
17992 which have DW_OP_addr 0. */
21ae7a4d
JK
17993 if (i < size)
17994 dwarf2_complex_location_expr_complaint ();
585861ea 17995 stack[stacki]++;
21ae7a4d
JK
17996 break;
17997
17998 case DW_OP_GNU_uninit:
17999 break;
18000
3019eac3 18001 case DW_OP_GNU_addr_index:
49f6c839 18002 case DW_OP_GNU_const_index:
3019eac3
DE
18003 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18004 &bytes_read);
18005 i += bytes_read;
18006 break;
18007
21ae7a4d
JK
18008 default:
18009 {
f39c6ffd 18010 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18011
18012 if (name)
18013 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18014 name);
18015 else
18016 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18017 op);
18018 }
18019
18020 return (stack[stacki]);
d53d4ac5 18021 }
3c6e0cb3 18022
21ae7a4d
JK
18023 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18024 outside of the allocated space. Also enforce minimum>0. */
18025 if (stacki >= ARRAY_SIZE (stack) - 1)
18026 {
18027 complaint (&symfile_complaints,
18028 _("location description stack overflow"));
18029 return 0;
18030 }
18031
18032 if (stacki <= 0)
18033 {
18034 complaint (&symfile_complaints,
18035 _("location description stack underflow"));
18036 return 0;
18037 }
18038 }
18039 return (stack[stacki]);
c906108c
SS
18040}
18041
18042/* memory allocation interface */
18043
c906108c 18044static struct dwarf_block *
7b5a2f43 18045dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18046{
18047 struct dwarf_block *blk;
18048
18049 blk = (struct dwarf_block *)
7b5a2f43 18050 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18051 return (blk);
18052}
18053
c906108c 18054static struct die_info *
b60c80d6 18055dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18056{
18057 struct die_info *die;
b60c80d6
DJ
18058 size_t size = sizeof (struct die_info);
18059
18060 if (num_attrs > 1)
18061 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18062
b60c80d6 18063 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18064 memset (die, 0, sizeof (struct die_info));
18065 return (die);
18066}
2e276125
JB
18067
18068\f
18069/* Macro support. */
18070
233d95b5
JK
18071/* Return file name relative to the compilation directory of file number I in
18072 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18073 responsible for freeing it. */
233d95b5 18074
2e276125 18075static char *
233d95b5 18076file_file_name (int file, struct line_header *lh)
2e276125 18077{
6a83a1e6
EZ
18078 /* Is the file number a valid index into the line header's file name
18079 table? Remember that file numbers start with one, not zero. */
18080 if (1 <= file && file <= lh->num_file_names)
18081 {
18082 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18083
233d95b5 18084 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18085 return xstrdup (fe->name);
233d95b5
JK
18086 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18087 fe->name, NULL);
6a83a1e6 18088 }
2e276125
JB
18089 else
18090 {
6a83a1e6
EZ
18091 /* The compiler produced a bogus file number. We can at least
18092 record the macro definitions made in the file, even if we
18093 won't be able to find the file by name. */
18094 char fake_name[80];
9a619af0 18095
8c042590
PM
18096 xsnprintf (fake_name, sizeof (fake_name),
18097 "<bad macro file number %d>", file);
2e276125 18098
6e70227d 18099 complaint (&symfile_complaints,
6a83a1e6
EZ
18100 _("bad file number in macro information (%d)"),
18101 file);
2e276125 18102
6a83a1e6 18103 return xstrdup (fake_name);
2e276125
JB
18104 }
18105}
18106
233d95b5
JK
18107/* Return the full name of file number I in *LH's file name table.
18108 Use COMP_DIR as the name of the current directory of the
18109 compilation. The result is allocated using xmalloc; the caller is
18110 responsible for freeing it. */
18111static char *
18112file_full_name (int file, struct line_header *lh, const char *comp_dir)
18113{
18114 /* Is the file number a valid index into the line header's file name
18115 table? Remember that file numbers start with one, not zero. */
18116 if (1 <= file && file <= lh->num_file_names)
18117 {
18118 char *relative = file_file_name (file, lh);
18119
18120 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18121 return relative;
18122 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18123 }
18124 else
18125 return file_file_name (file, lh);
18126}
18127
2e276125
JB
18128
18129static struct macro_source_file *
18130macro_start_file (int file, int line,
18131 struct macro_source_file *current_file,
18132 const char *comp_dir,
18133 struct line_header *lh, struct objfile *objfile)
18134{
233d95b5
JK
18135 /* File name relative to the compilation directory of this source file. */
18136 char *file_name = file_file_name (file, lh);
2e276125
JB
18137
18138 /* We don't create a macro table for this compilation unit
18139 at all until we actually get a filename. */
18140 if (! pending_macros)
6532ff36 18141 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18142 objfile->per_bfd->macro_cache,
18143 comp_dir);
2e276125
JB
18144
18145 if (! current_file)
abc9d0dc
TT
18146 {
18147 /* If we have no current file, then this must be the start_file
18148 directive for the compilation unit's main source file. */
233d95b5 18149 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18150 macro_define_special (pending_macros);
18151 }
2e276125 18152 else
233d95b5 18153 current_file = macro_include (current_file, line, file_name);
2e276125 18154
233d95b5 18155 xfree (file_name);
6e70227d 18156
2e276125
JB
18157 return current_file;
18158}
18159
18160
18161/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18162 followed by a null byte. */
18163static char *
18164copy_string (const char *buf, int len)
18165{
18166 char *s = xmalloc (len + 1);
9a619af0 18167
2e276125
JB
18168 memcpy (s, buf, len);
18169 s[len] = '\0';
2e276125
JB
18170 return s;
18171}
18172
18173
18174static const char *
18175consume_improper_spaces (const char *p, const char *body)
18176{
18177 if (*p == ' ')
18178 {
4d3c2250 18179 complaint (&symfile_complaints,
3e43a32a
MS
18180 _("macro definition contains spaces "
18181 "in formal argument list:\n`%s'"),
4d3c2250 18182 body);
2e276125
JB
18183
18184 while (*p == ' ')
18185 p++;
18186 }
18187
18188 return p;
18189}
18190
18191
18192static void
18193parse_macro_definition (struct macro_source_file *file, int line,
18194 const char *body)
18195{
18196 const char *p;
18197
18198 /* The body string takes one of two forms. For object-like macro
18199 definitions, it should be:
18200
18201 <macro name> " " <definition>
18202
18203 For function-like macro definitions, it should be:
18204
18205 <macro name> "() " <definition>
18206 or
18207 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18208
18209 Spaces may appear only where explicitly indicated, and in the
18210 <definition>.
18211
18212 The Dwarf 2 spec says that an object-like macro's name is always
18213 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18214 the space when the macro's definition is the empty string.
2e276125
JB
18215
18216 The Dwarf 2 spec says that there should be no spaces between the
18217 formal arguments in a function-like macro's formal argument list,
18218 but versions of GCC around March 2002 include spaces after the
18219 commas. */
18220
18221
18222 /* Find the extent of the macro name. The macro name is terminated
18223 by either a space or null character (for an object-like macro) or
18224 an opening paren (for a function-like macro). */
18225 for (p = body; *p; p++)
18226 if (*p == ' ' || *p == '(')
18227 break;
18228
18229 if (*p == ' ' || *p == '\0')
18230 {
18231 /* It's an object-like macro. */
18232 int name_len = p - body;
18233 char *name = copy_string (body, name_len);
18234 const char *replacement;
18235
18236 if (*p == ' ')
18237 replacement = body + name_len + 1;
18238 else
18239 {
4d3c2250 18240 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18241 replacement = body + name_len;
18242 }
6e70227d 18243
2e276125
JB
18244 macro_define_object (file, line, name, replacement);
18245
18246 xfree (name);
18247 }
18248 else if (*p == '(')
18249 {
18250 /* It's a function-like macro. */
18251 char *name = copy_string (body, p - body);
18252 int argc = 0;
18253 int argv_size = 1;
18254 char **argv = xmalloc (argv_size * sizeof (*argv));
18255
18256 p++;
18257
18258 p = consume_improper_spaces (p, body);
18259
18260 /* Parse the formal argument list. */
18261 while (*p && *p != ')')
18262 {
18263 /* Find the extent of the current argument name. */
18264 const char *arg_start = p;
18265
18266 while (*p && *p != ',' && *p != ')' && *p != ' ')
18267 p++;
18268
18269 if (! *p || p == arg_start)
4d3c2250 18270 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18271 else
18272 {
18273 /* Make sure argv has room for the new argument. */
18274 if (argc >= argv_size)
18275 {
18276 argv_size *= 2;
18277 argv = xrealloc (argv, argv_size * sizeof (*argv));
18278 }
18279
18280 argv[argc++] = copy_string (arg_start, p - arg_start);
18281 }
18282
18283 p = consume_improper_spaces (p, body);
18284
18285 /* Consume the comma, if present. */
18286 if (*p == ',')
18287 {
18288 p++;
18289
18290 p = consume_improper_spaces (p, body);
18291 }
18292 }
18293
18294 if (*p == ')')
18295 {
18296 p++;
18297
18298 if (*p == ' ')
18299 /* Perfectly formed definition, no complaints. */
18300 macro_define_function (file, line, name,
6e70227d 18301 argc, (const char **) argv,
2e276125
JB
18302 p + 1);
18303 else if (*p == '\0')
18304 {
18305 /* Complain, but do define it. */
4d3c2250 18306 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18307 macro_define_function (file, line, name,
6e70227d 18308 argc, (const char **) argv,
2e276125
JB
18309 p);
18310 }
18311 else
18312 /* Just complain. */
4d3c2250 18313 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18314 }
18315 else
18316 /* Just complain. */
4d3c2250 18317 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18318
18319 xfree (name);
18320 {
18321 int i;
18322
18323 for (i = 0; i < argc; i++)
18324 xfree (argv[i]);
18325 }
18326 xfree (argv);
18327 }
18328 else
4d3c2250 18329 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18330}
18331
cf2c3c16
TT
18332/* Skip some bytes from BYTES according to the form given in FORM.
18333 Returns the new pointer. */
2e276125 18334
cf2c3c16 18335static gdb_byte *
f664829e 18336skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18337 enum dwarf_form form,
18338 unsigned int offset_size,
18339 struct dwarf2_section_info *section)
2e276125 18340{
cf2c3c16 18341 unsigned int bytes_read;
2e276125 18342
cf2c3c16 18343 switch (form)
2e276125 18344 {
cf2c3c16
TT
18345 case DW_FORM_data1:
18346 case DW_FORM_flag:
18347 ++bytes;
18348 break;
18349
18350 case DW_FORM_data2:
18351 bytes += 2;
18352 break;
18353
18354 case DW_FORM_data4:
18355 bytes += 4;
18356 break;
18357
18358 case DW_FORM_data8:
18359 bytes += 8;
18360 break;
18361
18362 case DW_FORM_string:
18363 read_direct_string (abfd, bytes, &bytes_read);
18364 bytes += bytes_read;
18365 break;
18366
18367 case DW_FORM_sec_offset:
18368 case DW_FORM_strp:
36586728 18369 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18370 bytes += offset_size;
18371 break;
18372
18373 case DW_FORM_block:
18374 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18375 bytes += bytes_read;
18376 break;
18377
18378 case DW_FORM_block1:
18379 bytes += 1 + read_1_byte (abfd, bytes);
18380 break;
18381 case DW_FORM_block2:
18382 bytes += 2 + read_2_bytes (abfd, bytes);
18383 break;
18384 case DW_FORM_block4:
18385 bytes += 4 + read_4_bytes (abfd, bytes);
18386 break;
18387
18388 case DW_FORM_sdata:
18389 case DW_FORM_udata:
3019eac3
DE
18390 case DW_FORM_GNU_addr_index:
18391 case DW_FORM_GNU_str_index:
f664829e
DE
18392 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18393 if (bytes == NULL)
18394 {
18395 dwarf2_section_buffer_overflow_complaint (section);
18396 return NULL;
18397 }
cf2c3c16
TT
18398 break;
18399
18400 default:
18401 {
18402 complain:
18403 complaint (&symfile_complaints,
18404 _("invalid form 0x%x in `%s'"),
18405 form,
18406 section->asection->name);
18407 return NULL;
18408 }
2e276125
JB
18409 }
18410
cf2c3c16
TT
18411 return bytes;
18412}
757a13d0 18413
cf2c3c16
TT
18414/* A helper for dwarf_decode_macros that handles skipping an unknown
18415 opcode. Returns an updated pointer to the macro data buffer; or,
18416 on error, issues a complaint and returns NULL. */
757a13d0 18417
cf2c3c16
TT
18418static gdb_byte *
18419skip_unknown_opcode (unsigned int opcode,
18420 gdb_byte **opcode_definitions,
f664829e 18421 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18422 bfd *abfd,
18423 unsigned int offset_size,
18424 struct dwarf2_section_info *section)
18425{
18426 unsigned int bytes_read, i;
18427 unsigned long arg;
18428 gdb_byte *defn;
2e276125 18429
cf2c3c16 18430 if (opcode_definitions[opcode] == NULL)
2e276125 18431 {
cf2c3c16
TT
18432 complaint (&symfile_complaints,
18433 _("unrecognized DW_MACFINO opcode 0x%x"),
18434 opcode);
18435 return NULL;
18436 }
2e276125 18437
cf2c3c16
TT
18438 defn = opcode_definitions[opcode];
18439 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18440 defn += bytes_read;
2e276125 18441
cf2c3c16
TT
18442 for (i = 0; i < arg; ++i)
18443 {
f664829e
DE
18444 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18445 section);
cf2c3c16
TT
18446 if (mac_ptr == NULL)
18447 {
18448 /* skip_form_bytes already issued the complaint. */
18449 return NULL;
18450 }
18451 }
757a13d0 18452
cf2c3c16
TT
18453 return mac_ptr;
18454}
757a13d0 18455
cf2c3c16
TT
18456/* A helper function which parses the header of a macro section.
18457 If the macro section is the extended (for now called "GNU") type,
18458 then this updates *OFFSET_SIZE. Returns a pointer to just after
18459 the header, or issues a complaint and returns NULL on error. */
757a13d0 18460
cf2c3c16
TT
18461static gdb_byte *
18462dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18463 bfd *abfd,
18464 gdb_byte *mac_ptr,
18465 unsigned int *offset_size,
18466 int section_is_gnu)
18467{
18468 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18469
cf2c3c16
TT
18470 if (section_is_gnu)
18471 {
18472 unsigned int version, flags;
757a13d0 18473
cf2c3c16
TT
18474 version = read_2_bytes (abfd, mac_ptr);
18475 if (version != 4)
18476 {
18477 complaint (&symfile_complaints,
18478 _("unrecognized version `%d' in .debug_macro section"),
18479 version);
18480 return NULL;
18481 }
18482 mac_ptr += 2;
757a13d0 18483
cf2c3c16
TT
18484 flags = read_1_byte (abfd, mac_ptr);
18485 ++mac_ptr;
18486 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18487
cf2c3c16
TT
18488 if ((flags & 2) != 0)
18489 /* We don't need the line table offset. */
18490 mac_ptr += *offset_size;
757a13d0 18491
cf2c3c16
TT
18492 /* Vendor opcode descriptions. */
18493 if ((flags & 4) != 0)
18494 {
18495 unsigned int i, count;
757a13d0 18496
cf2c3c16
TT
18497 count = read_1_byte (abfd, mac_ptr);
18498 ++mac_ptr;
18499 for (i = 0; i < count; ++i)
18500 {
18501 unsigned int opcode, bytes_read;
18502 unsigned long arg;
18503
18504 opcode = read_1_byte (abfd, mac_ptr);
18505 ++mac_ptr;
18506 opcode_definitions[opcode] = mac_ptr;
18507 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18508 mac_ptr += bytes_read;
18509 mac_ptr += arg;
18510 }
757a13d0 18511 }
cf2c3c16 18512 }
757a13d0 18513
cf2c3c16
TT
18514 return mac_ptr;
18515}
757a13d0 18516
cf2c3c16 18517/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18518 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18519
18520static void
18521dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18522 struct macro_source_file *current_file,
15d034d0 18523 struct line_header *lh, const char *comp_dir,
cf2c3c16 18524 struct dwarf2_section_info *section,
36586728 18525 int section_is_gnu, int section_is_dwz,
cf2c3c16 18526 unsigned int offset_size,
8fc3fc34
TT
18527 struct objfile *objfile,
18528 htab_t include_hash)
cf2c3c16
TT
18529{
18530 enum dwarf_macro_record_type macinfo_type;
18531 int at_commandline;
18532 gdb_byte *opcode_definitions[256];
757a13d0 18533
cf2c3c16
TT
18534 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18535 &offset_size, section_is_gnu);
18536 if (mac_ptr == NULL)
18537 {
18538 /* We already issued a complaint. */
18539 return;
18540 }
757a13d0
JK
18541
18542 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18543 GDB is still reading the definitions from command line. First
18544 DW_MACINFO_start_file will need to be ignored as it was already executed
18545 to create CURRENT_FILE for the main source holding also the command line
18546 definitions. On first met DW_MACINFO_start_file this flag is reset to
18547 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18548
18549 at_commandline = 1;
18550
18551 do
18552 {
18553 /* Do we at least have room for a macinfo type byte? */
18554 if (mac_ptr >= mac_end)
18555 {
f664829e 18556 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18557 break;
18558 }
18559
18560 macinfo_type = read_1_byte (abfd, mac_ptr);
18561 mac_ptr++;
18562
cf2c3c16
TT
18563 /* Note that we rely on the fact that the corresponding GNU and
18564 DWARF constants are the same. */
757a13d0
JK
18565 switch (macinfo_type)
18566 {
18567 /* A zero macinfo type indicates the end of the macro
18568 information. */
18569 case 0:
18570 break;
2e276125 18571
cf2c3c16
TT
18572 case DW_MACRO_GNU_define:
18573 case DW_MACRO_GNU_undef:
18574 case DW_MACRO_GNU_define_indirect:
18575 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18576 case DW_MACRO_GNU_define_indirect_alt:
18577 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18578 {
891d2f0b 18579 unsigned int bytes_read;
2e276125
JB
18580 int line;
18581 char *body;
cf2c3c16 18582 int is_define;
2e276125 18583
cf2c3c16
TT
18584 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18585 mac_ptr += bytes_read;
18586
18587 if (macinfo_type == DW_MACRO_GNU_define
18588 || macinfo_type == DW_MACRO_GNU_undef)
18589 {
18590 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18591 mac_ptr += bytes_read;
18592 }
18593 else
18594 {
18595 LONGEST str_offset;
18596
18597 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18598 mac_ptr += offset_size;
2e276125 18599
36586728 18600 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18601 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18602 || section_is_dwz)
36586728
TT
18603 {
18604 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18605
18606 body = read_indirect_string_from_dwz (dwz, str_offset);
18607 }
18608 else
18609 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18610 }
18611
18612 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18613 || macinfo_type == DW_MACRO_GNU_define_indirect
18614 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18615 if (! current_file)
757a13d0
JK
18616 {
18617 /* DWARF violation as no main source is present. */
18618 complaint (&symfile_complaints,
18619 _("debug info with no main source gives macro %s "
18620 "on line %d: %s"),
cf2c3c16
TT
18621 is_define ? _("definition") : _("undefinition"),
18622 line, body);
757a13d0
JK
18623 break;
18624 }
3e43a32a
MS
18625 if ((line == 0 && !at_commandline)
18626 || (line != 0 && at_commandline))
4d3c2250 18627 complaint (&symfile_complaints,
757a13d0
JK
18628 _("debug info gives %s macro %s with %s line %d: %s"),
18629 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18630 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18631 line == 0 ? _("zero") : _("non-zero"), line, body);
18632
cf2c3c16 18633 if (is_define)
757a13d0 18634 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18635 else
18636 {
18637 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18638 || macinfo_type == DW_MACRO_GNU_undef_indirect
18639 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18640 macro_undef (current_file, line, body);
18641 }
2e276125
JB
18642 }
18643 break;
18644
cf2c3c16 18645 case DW_MACRO_GNU_start_file:
2e276125 18646 {
891d2f0b 18647 unsigned int bytes_read;
2e276125
JB
18648 int line, file;
18649
18650 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18651 mac_ptr += bytes_read;
18652 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18653 mac_ptr += bytes_read;
18654
3e43a32a
MS
18655 if ((line == 0 && !at_commandline)
18656 || (line != 0 && at_commandline))
757a13d0
JK
18657 complaint (&symfile_complaints,
18658 _("debug info gives source %d included "
18659 "from %s at %s line %d"),
18660 file, at_commandline ? _("command-line") : _("file"),
18661 line == 0 ? _("zero") : _("non-zero"), line);
18662
18663 if (at_commandline)
18664 {
cf2c3c16
TT
18665 /* This DW_MACRO_GNU_start_file was executed in the
18666 pass one. */
757a13d0
JK
18667 at_commandline = 0;
18668 }
18669 else
18670 current_file = macro_start_file (file, line,
18671 current_file, comp_dir,
cf2c3c16 18672 lh, objfile);
2e276125
JB
18673 }
18674 break;
18675
cf2c3c16 18676 case DW_MACRO_GNU_end_file:
2e276125 18677 if (! current_file)
4d3c2250 18678 complaint (&symfile_complaints,
3e43a32a
MS
18679 _("macro debug info has an unmatched "
18680 "`close_file' directive"));
2e276125
JB
18681 else
18682 {
18683 current_file = current_file->included_by;
18684 if (! current_file)
18685 {
cf2c3c16 18686 enum dwarf_macro_record_type next_type;
2e276125
JB
18687
18688 /* GCC circa March 2002 doesn't produce the zero
18689 type byte marking the end of the compilation
18690 unit. Complain if it's not there, but exit no
18691 matter what. */
18692
18693 /* Do we at least have room for a macinfo type byte? */
18694 if (mac_ptr >= mac_end)
18695 {
f664829e 18696 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18697 return;
18698 }
18699
18700 /* We don't increment mac_ptr here, so this is just
18701 a look-ahead. */
18702 next_type = read_1_byte (abfd, mac_ptr);
18703 if (next_type != 0)
4d3c2250 18704 complaint (&symfile_complaints,
3e43a32a
MS
18705 _("no terminating 0-type entry for "
18706 "macros in `.debug_macinfo' section"));
2e276125
JB
18707
18708 return;
18709 }
18710 }
18711 break;
18712
cf2c3c16 18713 case DW_MACRO_GNU_transparent_include:
36586728 18714 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18715 {
18716 LONGEST offset;
8fc3fc34 18717 void **slot;
a036ba48
TT
18718 bfd *include_bfd = abfd;
18719 struct dwarf2_section_info *include_section = section;
18720 struct dwarf2_section_info alt_section;
18721 gdb_byte *include_mac_end = mac_end;
18722 int is_dwz = section_is_dwz;
18723 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18724
18725 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18726 mac_ptr += offset_size;
18727
a036ba48
TT
18728 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18729 {
18730 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18731
18732 dwarf2_read_section (dwarf2_per_objfile->objfile,
18733 &dwz->macro);
18734
18735 include_bfd = dwz->macro.asection->owner;
18736 include_section = &dwz->macro;
18737 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18738 is_dwz = 1;
18739 }
18740
18741 new_mac_ptr = include_section->buffer + offset;
18742 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18743
8fc3fc34
TT
18744 if (*slot != NULL)
18745 {
18746 /* This has actually happened; see
18747 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18748 complaint (&symfile_complaints,
18749 _("recursive DW_MACRO_GNU_transparent_include in "
18750 ".debug_macro section"));
18751 }
18752 else
18753 {
a036ba48 18754 *slot = new_mac_ptr;
36586728 18755
a036ba48 18756 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18757 include_mac_end, current_file,
8fc3fc34 18758 lh, comp_dir,
36586728 18759 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18760 offset_size, objfile, include_hash);
18761
a036ba48 18762 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18763 }
cf2c3c16
TT
18764 }
18765 break;
18766
2e276125 18767 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18768 if (!section_is_gnu)
18769 {
18770 unsigned int bytes_read;
18771 int constant;
2e276125 18772
cf2c3c16
TT
18773 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18774 mac_ptr += bytes_read;
18775 read_direct_string (abfd, mac_ptr, &bytes_read);
18776 mac_ptr += bytes_read;
2e276125 18777
cf2c3c16
TT
18778 /* We don't recognize any vendor extensions. */
18779 break;
18780 }
18781 /* FALLTHROUGH */
18782
18783 default:
18784 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18785 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18786 section);
18787 if (mac_ptr == NULL)
18788 return;
18789 break;
2e276125 18790 }
757a13d0 18791 } while (macinfo_type != 0);
2e276125 18792}
8e19ed76 18793
cf2c3c16 18794static void
09262596 18795dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18796 const char *comp_dir, int section_is_gnu)
cf2c3c16 18797{
bb5ed363 18798 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18799 struct line_header *lh = cu->line_header;
18800 bfd *abfd;
cf2c3c16
TT
18801 gdb_byte *mac_ptr, *mac_end;
18802 struct macro_source_file *current_file = 0;
18803 enum dwarf_macro_record_type macinfo_type;
18804 unsigned int offset_size = cu->header.offset_size;
18805 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18806 struct cleanup *cleanup;
18807 htab_t include_hash;
18808 void **slot;
09262596
DE
18809 struct dwarf2_section_info *section;
18810 const char *section_name;
18811
18812 if (cu->dwo_unit != NULL)
18813 {
18814 if (section_is_gnu)
18815 {
18816 section = &cu->dwo_unit->dwo_file->sections.macro;
18817 section_name = ".debug_macro.dwo";
18818 }
18819 else
18820 {
18821 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18822 section_name = ".debug_macinfo.dwo";
18823 }
18824 }
18825 else
18826 {
18827 if (section_is_gnu)
18828 {
18829 section = &dwarf2_per_objfile->macro;
18830 section_name = ".debug_macro";
18831 }
18832 else
18833 {
18834 section = &dwarf2_per_objfile->macinfo;
18835 section_name = ".debug_macinfo";
18836 }
18837 }
cf2c3c16 18838
bb5ed363 18839 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18840 if (section->buffer == NULL)
18841 {
fceca515 18842 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18843 return;
18844 }
09262596 18845 abfd = section->asection->owner;
cf2c3c16
TT
18846
18847 /* First pass: Find the name of the base filename.
18848 This filename is needed in order to process all macros whose definition
18849 (or undefinition) comes from the command line. These macros are defined
18850 before the first DW_MACINFO_start_file entry, and yet still need to be
18851 associated to the base file.
18852
18853 To determine the base file name, we scan the macro definitions until we
18854 reach the first DW_MACINFO_start_file entry. We then initialize
18855 CURRENT_FILE accordingly so that any macro definition found before the
18856 first DW_MACINFO_start_file can still be associated to the base file. */
18857
18858 mac_ptr = section->buffer + offset;
18859 mac_end = section->buffer + section->size;
18860
18861 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18862 &offset_size, section_is_gnu);
18863 if (mac_ptr == NULL)
18864 {
18865 /* We already issued a complaint. */
18866 return;
18867 }
18868
18869 do
18870 {
18871 /* Do we at least have room for a macinfo type byte? */
18872 if (mac_ptr >= mac_end)
18873 {
18874 /* Complaint is printed during the second pass as GDB will probably
18875 stop the first pass earlier upon finding
18876 DW_MACINFO_start_file. */
18877 break;
18878 }
18879
18880 macinfo_type = read_1_byte (abfd, mac_ptr);
18881 mac_ptr++;
18882
18883 /* Note that we rely on the fact that the corresponding GNU and
18884 DWARF constants are the same. */
18885 switch (macinfo_type)
18886 {
18887 /* A zero macinfo type indicates the end of the macro
18888 information. */
18889 case 0:
18890 break;
18891
18892 case DW_MACRO_GNU_define:
18893 case DW_MACRO_GNU_undef:
18894 /* Only skip the data by MAC_PTR. */
18895 {
18896 unsigned int bytes_read;
18897
18898 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18899 mac_ptr += bytes_read;
18900 read_direct_string (abfd, mac_ptr, &bytes_read);
18901 mac_ptr += bytes_read;
18902 }
18903 break;
18904
18905 case DW_MACRO_GNU_start_file:
18906 {
18907 unsigned int bytes_read;
18908 int line, file;
18909
18910 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18911 mac_ptr += bytes_read;
18912 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18913 mac_ptr += bytes_read;
18914
18915 current_file = macro_start_file (file, line, current_file,
bb5ed363 18916 comp_dir, lh, objfile);
cf2c3c16
TT
18917 }
18918 break;
18919
18920 case DW_MACRO_GNU_end_file:
18921 /* No data to skip by MAC_PTR. */
18922 break;
18923
18924 case DW_MACRO_GNU_define_indirect:
18925 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18926 case DW_MACRO_GNU_define_indirect_alt:
18927 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18928 {
18929 unsigned int bytes_read;
18930
18931 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18932 mac_ptr += bytes_read;
18933 mac_ptr += offset_size;
18934 }
18935 break;
18936
18937 case DW_MACRO_GNU_transparent_include:
f7a35f02 18938 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18939 /* Note that, according to the spec, a transparent include
18940 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18941 skip this opcode. */
18942 mac_ptr += offset_size;
18943 break;
18944
18945 case DW_MACINFO_vendor_ext:
18946 /* Only skip the data by MAC_PTR. */
18947 if (!section_is_gnu)
18948 {
18949 unsigned int bytes_read;
18950
18951 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18952 mac_ptr += bytes_read;
18953 read_direct_string (abfd, mac_ptr, &bytes_read);
18954 mac_ptr += bytes_read;
18955 }
18956 /* FALLTHROUGH */
18957
18958 default:
18959 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18960 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18961 section);
18962 if (mac_ptr == NULL)
18963 return;
18964 break;
18965 }
18966 } while (macinfo_type != 0 && current_file == NULL);
18967
18968 /* Second pass: Process all entries.
18969
18970 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18971 command-line macro definitions/undefinitions. This flag is unset when we
18972 reach the first DW_MACINFO_start_file entry. */
18973
8fc3fc34
TT
18974 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18975 NULL, xcalloc, xfree);
18976 cleanup = make_cleanup_htab_delete (include_hash);
18977 mac_ptr = section->buffer + offset;
18978 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18979 *slot = mac_ptr;
18980 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18981 current_file, lh, comp_dir, section,
18982 section_is_gnu, 0,
8fc3fc34
TT
18983 offset_size, objfile, include_hash);
18984 do_cleanups (cleanup);
cf2c3c16
TT
18985}
18986
8e19ed76 18987/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18988 if so return true else false. */
380bca97 18989
8e19ed76
PS
18990static int
18991attr_form_is_block (struct attribute *attr)
18992{
18993 return (attr == NULL ? 0 :
18994 attr->form == DW_FORM_block1
18995 || attr->form == DW_FORM_block2
18996 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18997 || attr->form == DW_FORM_block
18998 || attr->form == DW_FORM_exprloc);
8e19ed76 18999}
4c2df51b 19000
c6a0999f
JB
19001/* Return non-zero if ATTR's value is a section offset --- classes
19002 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19003 You may use DW_UNSND (attr) to retrieve such offsets.
19004
19005 Section 7.5.4, "Attribute Encodings", explains that no attribute
19006 may have a value that belongs to more than one of these classes; it
19007 would be ambiguous if we did, because we use the same forms for all
19008 of them. */
380bca97 19009
3690dd37
JB
19010static int
19011attr_form_is_section_offset (struct attribute *attr)
19012{
19013 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19014 || attr->form == DW_FORM_data8
19015 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19016}
19017
3690dd37
JB
19018/* Return non-zero if ATTR's value falls in the 'constant' class, or
19019 zero otherwise. When this function returns true, you can apply
19020 dwarf2_get_attr_constant_value to it.
19021
19022 However, note that for some attributes you must check
19023 attr_form_is_section_offset before using this test. DW_FORM_data4
19024 and DW_FORM_data8 are members of both the constant class, and of
19025 the classes that contain offsets into other debug sections
19026 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19027 that, if an attribute's can be either a constant or one of the
19028 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19029 taken as section offsets, not constants. */
380bca97 19030
3690dd37
JB
19031static int
19032attr_form_is_constant (struct attribute *attr)
19033{
19034 switch (attr->form)
19035 {
19036 case DW_FORM_sdata:
19037 case DW_FORM_udata:
19038 case DW_FORM_data1:
19039 case DW_FORM_data2:
19040 case DW_FORM_data4:
19041 case DW_FORM_data8:
19042 return 1;
19043 default:
19044 return 0;
19045 }
19046}
19047
3019eac3
DE
19048/* Return the .debug_loc section to use for CU.
19049 For DWO files use .debug_loc.dwo. */
19050
19051static struct dwarf2_section_info *
19052cu_debug_loc_section (struct dwarf2_cu *cu)
19053{
19054 if (cu->dwo_unit)
19055 return &cu->dwo_unit->dwo_file->sections.loc;
19056 return &dwarf2_per_objfile->loc;
19057}
19058
8cf6f0b1
TT
19059/* A helper function that fills in a dwarf2_loclist_baton. */
19060
19061static void
19062fill_in_loclist_baton (struct dwarf2_cu *cu,
19063 struct dwarf2_loclist_baton *baton,
19064 struct attribute *attr)
19065{
3019eac3
DE
19066 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19067
19068 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19069
19070 baton->per_cu = cu->per_cu;
19071 gdb_assert (baton->per_cu);
19072 /* We don't know how long the location list is, but make sure we
19073 don't run off the edge of the section. */
3019eac3
DE
19074 baton->size = section->size - DW_UNSND (attr);
19075 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19076 baton->base_address = cu->base_address;
f664829e 19077 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19078}
19079
4c2df51b
DJ
19080static void
19081dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19082 struct dwarf2_cu *cu, int is_block)
4c2df51b 19083{
bb5ed363 19084 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19085 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19086
3690dd37 19087 if (attr_form_is_section_offset (attr)
3019eac3 19088 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19089 the section. If so, fall through to the complaint in the
19090 other branch. */
3019eac3 19091 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19092 {
0d53c4c4 19093 struct dwarf2_loclist_baton *baton;
4c2df51b 19094
bb5ed363 19095 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19096 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19097
8cf6f0b1 19098 fill_in_loclist_baton (cu, baton, attr);
be391dca 19099
d00adf39 19100 if (cu->base_known == 0)
0d53c4c4 19101 complaint (&symfile_complaints,
3e43a32a
MS
19102 _("Location list used without "
19103 "specifying the CU base address."));
4c2df51b 19104
f1e6e072
TT
19105 SYMBOL_ACLASS_INDEX (sym) = (is_block
19106 ? dwarf2_loclist_block_index
19107 : dwarf2_loclist_index);
0d53c4c4
DJ
19108 SYMBOL_LOCATION_BATON (sym) = baton;
19109 }
19110 else
19111 {
19112 struct dwarf2_locexpr_baton *baton;
19113
bb5ed363 19114 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19115 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19116 baton->per_cu = cu->per_cu;
19117 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19118
19119 if (attr_form_is_block (attr))
19120 {
19121 /* Note that we're just copying the block's data pointer
19122 here, not the actual data. We're still pointing into the
6502dd73
DJ
19123 info_buffer for SYM's objfile; right now we never release
19124 that buffer, but when we do clean up properly this may
19125 need to change. */
0d53c4c4
DJ
19126 baton->size = DW_BLOCK (attr)->size;
19127 baton->data = DW_BLOCK (attr)->data;
19128 }
19129 else
19130 {
19131 dwarf2_invalid_attrib_class_complaint ("location description",
19132 SYMBOL_NATURAL_NAME (sym));
19133 baton->size = 0;
0d53c4c4 19134 }
6e70227d 19135
f1e6e072
TT
19136 SYMBOL_ACLASS_INDEX (sym) = (is_block
19137 ? dwarf2_locexpr_block_index
19138 : dwarf2_locexpr_index);
0d53c4c4
DJ
19139 SYMBOL_LOCATION_BATON (sym) = baton;
19140 }
4c2df51b 19141}
6502dd73 19142
9aa1f1e3
TT
19143/* Return the OBJFILE associated with the compilation unit CU. If CU
19144 came from a separate debuginfo file, then the master objfile is
19145 returned. */
ae0d2f24
UW
19146
19147struct objfile *
19148dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19149{
9291a0cd 19150 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19151
19152 /* Return the master objfile, so that we can report and look up the
19153 correct file containing this variable. */
19154 if (objfile->separate_debug_objfile_backlink)
19155 objfile = objfile->separate_debug_objfile_backlink;
19156
19157 return objfile;
19158}
19159
96408a79
SA
19160/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19161 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19162 CU_HEADERP first. */
19163
19164static const struct comp_unit_head *
19165per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19166 struct dwarf2_per_cu_data *per_cu)
19167{
96408a79
SA
19168 gdb_byte *info_ptr;
19169
19170 if (per_cu->cu)
19171 return &per_cu->cu->header;
19172
8a0459fd 19173 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19174
19175 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19176 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19177
19178 return cu_headerp;
19179}
19180
ae0d2f24
UW
19181/* Return the address size given in the compilation unit header for CU. */
19182
98714339 19183int
ae0d2f24
UW
19184dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19185{
96408a79
SA
19186 struct comp_unit_head cu_header_local;
19187 const struct comp_unit_head *cu_headerp;
c471e790 19188
96408a79
SA
19189 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19190
19191 return cu_headerp->addr_size;
ae0d2f24
UW
19192}
19193
9eae7c52
TT
19194/* Return the offset size given in the compilation unit header for CU. */
19195
19196int
19197dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19198{
96408a79
SA
19199 struct comp_unit_head cu_header_local;
19200 const struct comp_unit_head *cu_headerp;
9c6c53f7 19201
96408a79
SA
19202 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19203
19204 return cu_headerp->offset_size;
19205}
19206
19207/* See its dwarf2loc.h declaration. */
19208
19209int
19210dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19211{
19212 struct comp_unit_head cu_header_local;
19213 const struct comp_unit_head *cu_headerp;
19214
19215 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19216
19217 if (cu_headerp->version == 2)
19218 return cu_headerp->addr_size;
19219 else
19220 return cu_headerp->offset_size;
181cebd4
JK
19221}
19222
9aa1f1e3
TT
19223/* Return the text offset of the CU. The returned offset comes from
19224 this CU's objfile. If this objfile came from a separate debuginfo
19225 file, then the offset may be different from the corresponding
19226 offset in the parent objfile. */
19227
19228CORE_ADDR
19229dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19230{
bb3fa9d0 19231 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19232
19233 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19234}
19235
348e048f
DE
19236/* Locate the .debug_info compilation unit from CU's objfile which contains
19237 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19238
19239static struct dwarf2_per_cu_data *
b64f50a1 19240dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19241 unsigned int offset_in_dwz,
ae038cb0
DJ
19242 struct objfile *objfile)
19243{
19244 struct dwarf2_per_cu_data *this_cu;
19245 int low, high;
36586728 19246 const sect_offset *cu_off;
ae038cb0 19247
ae038cb0
DJ
19248 low = 0;
19249 high = dwarf2_per_objfile->n_comp_units - 1;
19250 while (high > low)
19251 {
36586728 19252 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19253 int mid = low + (high - low) / 2;
9a619af0 19254
36586728
TT
19255 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19256 cu_off = &mid_cu->offset;
19257 if (mid_cu->is_dwz > offset_in_dwz
19258 || (mid_cu->is_dwz == offset_in_dwz
19259 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19260 high = mid;
19261 else
19262 low = mid + 1;
19263 }
19264 gdb_assert (low == high);
36586728
TT
19265 this_cu = dwarf2_per_objfile->all_comp_units[low];
19266 cu_off = &this_cu->offset;
19267 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19268 {
36586728 19269 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19270 error (_("Dwarf Error: could not find partial DIE containing "
19271 "offset 0x%lx [in module %s]"),
b64f50a1 19272 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19273
b64f50a1
JK
19274 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19275 <= offset.sect_off);
ae038cb0
DJ
19276 return dwarf2_per_objfile->all_comp_units[low-1];
19277 }
19278 else
19279 {
19280 this_cu = dwarf2_per_objfile->all_comp_units[low];
19281 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19282 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19283 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19284 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19285 return this_cu;
19286 }
19287}
19288
23745b47 19289/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19290
9816fde3 19291static void
23745b47 19292init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19293{
9816fde3 19294 memset (cu, 0, sizeof (*cu));
23745b47
DE
19295 per_cu->cu = cu;
19296 cu->per_cu = per_cu;
19297 cu->objfile = per_cu->objfile;
93311388 19298 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19299}
19300
19301/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19302
19303static void
95554aad
TT
19304prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19305 enum language pretend_language)
9816fde3
JK
19306{
19307 struct attribute *attr;
19308
19309 /* Set the language we're debugging. */
19310 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19311 if (attr)
19312 set_cu_language (DW_UNSND (attr), cu);
19313 else
9cded63f 19314 {
95554aad 19315 cu->language = pretend_language;
9cded63f
TT
19316 cu->language_defn = language_def (cu->language);
19317 }
dee91e82
DE
19318
19319 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19320 if (attr)
19321 cu->producer = DW_STRING (attr);
93311388
DE
19322}
19323
ae038cb0
DJ
19324/* Release one cached compilation unit, CU. We unlink it from the tree
19325 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19326 the caller is responsible for that.
19327 NOTE: DATA is a void * because this function is also used as a
19328 cleanup routine. */
ae038cb0
DJ
19329
19330static void
68dc6402 19331free_heap_comp_unit (void *data)
ae038cb0
DJ
19332{
19333 struct dwarf2_cu *cu = data;
19334
23745b47
DE
19335 gdb_assert (cu->per_cu != NULL);
19336 cu->per_cu->cu = NULL;
ae038cb0
DJ
19337 cu->per_cu = NULL;
19338
19339 obstack_free (&cu->comp_unit_obstack, NULL);
19340
19341 xfree (cu);
19342}
19343
72bf9492 19344/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19345 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19346 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19347
19348static void
19349free_stack_comp_unit (void *data)
19350{
19351 struct dwarf2_cu *cu = data;
19352
23745b47
DE
19353 gdb_assert (cu->per_cu != NULL);
19354 cu->per_cu->cu = NULL;
19355 cu->per_cu = NULL;
19356
72bf9492
DJ
19357 obstack_free (&cu->comp_unit_obstack, NULL);
19358 cu->partial_dies = NULL;
ae038cb0
DJ
19359}
19360
19361/* Free all cached compilation units. */
19362
19363static void
19364free_cached_comp_units (void *data)
19365{
19366 struct dwarf2_per_cu_data *per_cu, **last_chain;
19367
19368 per_cu = dwarf2_per_objfile->read_in_chain;
19369 last_chain = &dwarf2_per_objfile->read_in_chain;
19370 while (per_cu != NULL)
19371 {
19372 struct dwarf2_per_cu_data *next_cu;
19373
19374 next_cu = per_cu->cu->read_in_chain;
19375
68dc6402 19376 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19377 *last_chain = next_cu;
19378
19379 per_cu = next_cu;
19380 }
19381}
19382
19383/* Increase the age counter on each cached compilation unit, and free
19384 any that are too old. */
19385
19386static void
19387age_cached_comp_units (void)
19388{
19389 struct dwarf2_per_cu_data *per_cu, **last_chain;
19390
19391 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19392 per_cu = dwarf2_per_objfile->read_in_chain;
19393 while (per_cu != NULL)
19394 {
19395 per_cu->cu->last_used ++;
19396 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19397 dwarf2_mark (per_cu->cu);
19398 per_cu = per_cu->cu->read_in_chain;
19399 }
19400
19401 per_cu = dwarf2_per_objfile->read_in_chain;
19402 last_chain = &dwarf2_per_objfile->read_in_chain;
19403 while (per_cu != NULL)
19404 {
19405 struct dwarf2_per_cu_data *next_cu;
19406
19407 next_cu = per_cu->cu->read_in_chain;
19408
19409 if (!per_cu->cu->mark)
19410 {
68dc6402 19411 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19412 *last_chain = next_cu;
19413 }
19414 else
19415 last_chain = &per_cu->cu->read_in_chain;
19416
19417 per_cu = next_cu;
19418 }
19419}
19420
19421/* Remove a single compilation unit from the cache. */
19422
19423static void
dee91e82 19424free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19425{
19426 struct dwarf2_per_cu_data *per_cu, **last_chain;
19427
19428 per_cu = dwarf2_per_objfile->read_in_chain;
19429 last_chain = &dwarf2_per_objfile->read_in_chain;
19430 while (per_cu != NULL)
19431 {
19432 struct dwarf2_per_cu_data *next_cu;
19433
19434 next_cu = per_cu->cu->read_in_chain;
19435
dee91e82 19436 if (per_cu == target_per_cu)
ae038cb0 19437 {
68dc6402 19438 free_heap_comp_unit (per_cu->cu);
dee91e82 19439 per_cu->cu = NULL;
ae038cb0
DJ
19440 *last_chain = next_cu;
19441 break;
19442 }
19443 else
19444 last_chain = &per_cu->cu->read_in_chain;
19445
19446 per_cu = next_cu;
19447 }
19448}
19449
fe3e1990
DJ
19450/* Release all extra memory associated with OBJFILE. */
19451
19452void
19453dwarf2_free_objfile (struct objfile *objfile)
19454{
19455 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19456
19457 if (dwarf2_per_objfile == NULL)
19458 return;
19459
19460 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19461 free_cached_comp_units (NULL);
19462
7b9f3c50
DE
19463 if (dwarf2_per_objfile->quick_file_names_table)
19464 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19465
fe3e1990
DJ
19466 /* Everything else should be on the objfile obstack. */
19467}
19468
dee91e82
DE
19469/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19470 We store these in a hash table separate from the DIEs, and preserve them
19471 when the DIEs are flushed out of cache.
19472
19473 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19474 uniquely identify the type. A file may have multiple .debug_types sections,
19475 or the type may come from a DWO file. We have to use something in
19476 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19477 routine, get_die_type_at_offset, from outside this file, and thus won't
19478 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19479 of the objfile. */
1c379e20 19480
dee91e82 19481struct dwarf2_per_cu_offset_and_type
1c379e20 19482{
dee91e82 19483 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19484 sect_offset offset;
1c379e20
DJ
19485 struct type *type;
19486};
19487
dee91e82 19488/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19489
19490static hashval_t
dee91e82 19491per_cu_offset_and_type_hash (const void *item)
1c379e20 19492{
dee91e82 19493 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19494
dee91e82 19495 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19496}
19497
dee91e82 19498/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19499
19500static int
dee91e82 19501per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19502{
dee91e82
DE
19503 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19504 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19505
dee91e82
DE
19506 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19507 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19508}
19509
19510/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19511 table if necessary. For convenience, return TYPE.
19512
19513 The DIEs reading must have careful ordering to:
19514 * Not cause infite loops trying to read in DIEs as a prerequisite for
19515 reading current DIE.
19516 * Not trying to dereference contents of still incompletely read in types
19517 while reading in other DIEs.
19518 * Enable referencing still incompletely read in types just by a pointer to
19519 the type without accessing its fields.
19520
19521 Therefore caller should follow these rules:
19522 * Try to fetch any prerequisite types we may need to build this DIE type
19523 before building the type and calling set_die_type.
e71ec853 19524 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19525 possible before fetching more types to complete the current type.
19526 * Make the type as complete as possible before fetching more types. */
1c379e20 19527
f792889a 19528static struct type *
1c379e20
DJ
19529set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19530{
dee91e82 19531 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19532 struct objfile *objfile = cu->objfile;
1c379e20 19533
b4ba55a1
JB
19534 /* For Ada types, make sure that the gnat-specific data is always
19535 initialized (if not already set). There are a few types where
19536 we should not be doing so, because the type-specific area is
19537 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19538 where the type-specific area is used to store the floatformat).
19539 But this is not a problem, because the gnat-specific information
19540 is actually not needed for these types. */
19541 if (need_gnat_info (cu)
19542 && TYPE_CODE (type) != TYPE_CODE_FUNC
19543 && TYPE_CODE (type) != TYPE_CODE_FLT
19544 && !HAVE_GNAT_AUX_INFO (type))
19545 INIT_GNAT_SPECIFIC (type);
19546
dee91e82 19547 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19548 {
dee91e82
DE
19549 dwarf2_per_objfile->die_type_hash =
19550 htab_create_alloc_ex (127,
19551 per_cu_offset_and_type_hash,
19552 per_cu_offset_and_type_eq,
19553 NULL,
19554 &objfile->objfile_obstack,
19555 hashtab_obstack_allocate,
19556 dummy_obstack_deallocate);
f792889a 19557 }
1c379e20 19558
dee91e82 19559 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19560 ofs.offset = die->offset;
19561 ofs.type = type;
dee91e82
DE
19562 slot = (struct dwarf2_per_cu_offset_and_type **)
19563 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19564 if (*slot)
19565 complaint (&symfile_complaints,
19566 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19567 die->offset.sect_off);
673bfd45 19568 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19569 **slot = ofs;
f792889a 19570 return type;
1c379e20
DJ
19571}
19572
380bca97 19573/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19574 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19575
19576static struct type *
b64f50a1 19577get_die_type_at_offset (sect_offset offset,
673bfd45 19578 struct dwarf2_per_cu_data *per_cu)
1c379e20 19579{
dee91e82 19580 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19581
dee91e82 19582 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19583 return NULL;
1c379e20 19584
dee91e82 19585 ofs.per_cu = per_cu;
673bfd45 19586 ofs.offset = offset;
dee91e82 19587 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19588 if (slot)
19589 return slot->type;
19590 else
19591 return NULL;
19592}
19593
673bfd45
DE
19594/* Look up the type for DIE in the appropriate type_hash table,
19595 or return NULL if DIE does not have a saved type. */
19596
19597static struct type *
19598get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19599{
19600 return get_die_type_at_offset (die->offset, cu->per_cu);
19601}
19602
10b3939b
DJ
19603/* Add a dependence relationship from CU to REF_PER_CU. */
19604
19605static void
19606dwarf2_add_dependence (struct dwarf2_cu *cu,
19607 struct dwarf2_per_cu_data *ref_per_cu)
19608{
19609 void **slot;
19610
19611 if (cu->dependencies == NULL)
19612 cu->dependencies
19613 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19614 NULL, &cu->comp_unit_obstack,
19615 hashtab_obstack_allocate,
19616 dummy_obstack_deallocate);
19617
19618 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19619 if (*slot == NULL)
19620 *slot = ref_per_cu;
19621}
1c379e20 19622
f504f079
DE
19623/* Subroutine of dwarf2_mark to pass to htab_traverse.
19624 Set the mark field in every compilation unit in the
ae038cb0
DJ
19625 cache that we must keep because we are keeping CU. */
19626
10b3939b
DJ
19627static int
19628dwarf2_mark_helper (void **slot, void *data)
19629{
19630 struct dwarf2_per_cu_data *per_cu;
19631
19632 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19633
19634 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19635 reading of the chain. As such dependencies remain valid it is not much
19636 useful to track and undo them during QUIT cleanups. */
19637 if (per_cu->cu == NULL)
19638 return 1;
19639
10b3939b
DJ
19640 if (per_cu->cu->mark)
19641 return 1;
19642 per_cu->cu->mark = 1;
19643
19644 if (per_cu->cu->dependencies != NULL)
19645 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19646
19647 return 1;
19648}
19649
f504f079
DE
19650/* Set the mark field in CU and in every other compilation unit in the
19651 cache that we must keep because we are keeping CU. */
19652
ae038cb0
DJ
19653static void
19654dwarf2_mark (struct dwarf2_cu *cu)
19655{
19656 if (cu->mark)
19657 return;
19658 cu->mark = 1;
10b3939b
DJ
19659 if (cu->dependencies != NULL)
19660 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19661}
19662
19663static void
19664dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19665{
19666 while (per_cu)
19667 {
19668 per_cu->cu->mark = 0;
19669 per_cu = per_cu->cu->read_in_chain;
19670 }
72bf9492
DJ
19671}
19672
72bf9492
DJ
19673/* Trivial hash function for partial_die_info: the hash value of a DIE
19674 is its offset in .debug_info for this objfile. */
19675
19676static hashval_t
19677partial_die_hash (const void *item)
19678{
19679 const struct partial_die_info *part_die = item;
9a619af0 19680
b64f50a1 19681 return part_die->offset.sect_off;
72bf9492
DJ
19682}
19683
19684/* Trivial comparison function for partial_die_info structures: two DIEs
19685 are equal if they have the same offset. */
19686
19687static int
19688partial_die_eq (const void *item_lhs, const void *item_rhs)
19689{
19690 const struct partial_die_info *part_die_lhs = item_lhs;
19691 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19692
b64f50a1 19693 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19694}
19695
ae038cb0
DJ
19696static struct cmd_list_element *set_dwarf2_cmdlist;
19697static struct cmd_list_element *show_dwarf2_cmdlist;
19698
19699static void
19700set_dwarf2_cmd (char *args, int from_tty)
19701{
19702 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19703}
19704
19705static void
19706show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19707{
ae038cb0
DJ
19708 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19709}
19710
4bf44c1c 19711/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19712
19713static void
c1bd65d0 19714dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19715{
19716 struct dwarf2_per_objfile *data = d;
8b70b953 19717 int ix;
8b70b953 19718
95554aad
TT
19719 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19720 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19721 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19722
19723 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19724 VEC_free (dwarf2_per_cu_ptr,
19725 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19726
8b70b953 19727 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19728
19729 if (data->dwo_files)
19730 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19731
19732 if (data->dwz_file && data->dwz_file->dwz_bfd)
19733 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19734}
19735
19736\f
ae2de4f8 19737/* The "save gdb-index" command. */
9291a0cd
TT
19738
19739/* The contents of the hash table we create when building the string
19740 table. */
19741struct strtab_entry
19742{
19743 offset_type offset;
19744 const char *str;
19745};
19746
559a7a62
JK
19747/* Hash function for a strtab_entry.
19748
19749 Function is used only during write_hash_table so no index format backward
19750 compatibility is needed. */
b89be57b 19751
9291a0cd
TT
19752static hashval_t
19753hash_strtab_entry (const void *e)
19754{
19755 const struct strtab_entry *entry = e;
559a7a62 19756 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19757}
19758
19759/* Equality function for a strtab_entry. */
b89be57b 19760
9291a0cd
TT
19761static int
19762eq_strtab_entry (const void *a, const void *b)
19763{
19764 const struct strtab_entry *ea = a;
19765 const struct strtab_entry *eb = b;
19766 return !strcmp (ea->str, eb->str);
19767}
19768
19769/* Create a strtab_entry hash table. */
b89be57b 19770
9291a0cd
TT
19771static htab_t
19772create_strtab (void)
19773{
19774 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19775 xfree, xcalloc, xfree);
19776}
19777
19778/* Add a string to the constant pool. Return the string's offset in
19779 host order. */
b89be57b 19780
9291a0cd
TT
19781static offset_type
19782add_string (htab_t table, struct obstack *cpool, const char *str)
19783{
19784 void **slot;
19785 struct strtab_entry entry;
19786 struct strtab_entry *result;
19787
19788 entry.str = str;
19789 slot = htab_find_slot (table, &entry, INSERT);
19790 if (*slot)
19791 result = *slot;
19792 else
19793 {
19794 result = XNEW (struct strtab_entry);
19795 result->offset = obstack_object_size (cpool);
19796 result->str = str;
19797 obstack_grow_str0 (cpool, str);
19798 *slot = result;
19799 }
19800 return result->offset;
19801}
19802
19803/* An entry in the symbol table. */
19804struct symtab_index_entry
19805{
19806 /* The name of the symbol. */
19807 const char *name;
19808 /* The offset of the name in the constant pool. */
19809 offset_type index_offset;
19810 /* A sorted vector of the indices of all the CUs that hold an object
19811 of this name. */
19812 VEC (offset_type) *cu_indices;
19813};
19814
19815/* The symbol table. This is a power-of-2-sized hash table. */
19816struct mapped_symtab
19817{
19818 offset_type n_elements;
19819 offset_type size;
19820 struct symtab_index_entry **data;
19821};
19822
19823/* Hash function for a symtab_index_entry. */
b89be57b 19824
9291a0cd
TT
19825static hashval_t
19826hash_symtab_entry (const void *e)
19827{
19828 const struct symtab_index_entry *entry = e;
19829 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19830 sizeof (offset_type) * VEC_length (offset_type,
19831 entry->cu_indices),
19832 0);
19833}
19834
19835/* Equality function for a symtab_index_entry. */
b89be57b 19836
9291a0cd
TT
19837static int
19838eq_symtab_entry (const void *a, const void *b)
19839{
19840 const struct symtab_index_entry *ea = a;
19841 const struct symtab_index_entry *eb = b;
19842 int len = VEC_length (offset_type, ea->cu_indices);
19843 if (len != VEC_length (offset_type, eb->cu_indices))
19844 return 0;
19845 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19846 VEC_address (offset_type, eb->cu_indices),
19847 sizeof (offset_type) * len);
19848}
19849
19850/* Destroy a symtab_index_entry. */
b89be57b 19851
9291a0cd
TT
19852static void
19853delete_symtab_entry (void *p)
19854{
19855 struct symtab_index_entry *entry = p;
19856 VEC_free (offset_type, entry->cu_indices);
19857 xfree (entry);
19858}
19859
19860/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19861
9291a0cd 19862static htab_t
3876f04e 19863create_symbol_hash_table (void)
9291a0cd
TT
19864{
19865 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19866 delete_symtab_entry, xcalloc, xfree);
19867}
19868
19869/* Create a new mapped symtab object. */
b89be57b 19870
9291a0cd
TT
19871static struct mapped_symtab *
19872create_mapped_symtab (void)
19873{
19874 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19875 symtab->n_elements = 0;
19876 symtab->size = 1024;
19877 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19878 return symtab;
19879}
19880
19881/* Destroy a mapped_symtab. */
b89be57b 19882
9291a0cd
TT
19883static void
19884cleanup_mapped_symtab (void *p)
19885{
19886 struct mapped_symtab *symtab = p;
19887 /* The contents of the array are freed when the other hash table is
19888 destroyed. */
19889 xfree (symtab->data);
19890 xfree (symtab);
19891}
19892
19893/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19894 the slot.
19895
19896 Function is used only during write_hash_table so no index format backward
19897 compatibility is needed. */
b89be57b 19898
9291a0cd
TT
19899static struct symtab_index_entry **
19900find_slot (struct mapped_symtab *symtab, const char *name)
19901{
559a7a62 19902 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19903
19904 index = hash & (symtab->size - 1);
19905 step = ((hash * 17) & (symtab->size - 1)) | 1;
19906
19907 for (;;)
19908 {
19909 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19910 return &symtab->data[index];
19911 index = (index + step) & (symtab->size - 1);
19912 }
19913}
19914
19915/* Expand SYMTAB's hash table. */
b89be57b 19916
9291a0cd
TT
19917static void
19918hash_expand (struct mapped_symtab *symtab)
19919{
19920 offset_type old_size = symtab->size;
19921 offset_type i;
19922 struct symtab_index_entry **old_entries = symtab->data;
19923
19924 symtab->size *= 2;
19925 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19926
19927 for (i = 0; i < old_size; ++i)
19928 {
19929 if (old_entries[i])
19930 {
19931 struct symtab_index_entry **slot = find_slot (symtab,
19932 old_entries[i]->name);
19933 *slot = old_entries[i];
19934 }
19935 }
19936
19937 xfree (old_entries);
19938}
19939
156942c7
DE
19940/* Add an entry to SYMTAB. NAME is the name of the symbol.
19941 CU_INDEX is the index of the CU in which the symbol appears.
19942 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19943
9291a0cd
TT
19944static void
19945add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19946 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19947 offset_type cu_index)
19948{
19949 struct symtab_index_entry **slot;
156942c7 19950 offset_type cu_index_and_attrs;
9291a0cd
TT
19951
19952 ++symtab->n_elements;
19953 if (4 * symtab->n_elements / 3 >= symtab->size)
19954 hash_expand (symtab);
19955
19956 slot = find_slot (symtab, name);
19957 if (!*slot)
19958 {
19959 *slot = XNEW (struct symtab_index_entry);
19960 (*slot)->name = name;
156942c7 19961 /* index_offset is set later. */
9291a0cd
TT
19962 (*slot)->cu_indices = NULL;
19963 }
156942c7
DE
19964
19965 cu_index_and_attrs = 0;
19966 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19967 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19968 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19969
19970 /* We don't want to record an index value twice as we want to avoid the
19971 duplication.
19972 We process all global symbols and then all static symbols
19973 (which would allow us to avoid the duplication by only having to check
19974 the last entry pushed), but a symbol could have multiple kinds in one CU.
19975 To keep things simple we don't worry about the duplication here and
19976 sort and uniqufy the list after we've processed all symbols. */
19977 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19978}
19979
19980/* qsort helper routine for uniquify_cu_indices. */
19981
19982static int
19983offset_type_compare (const void *ap, const void *bp)
19984{
19985 offset_type a = *(offset_type *) ap;
19986 offset_type b = *(offset_type *) bp;
19987
19988 return (a > b) - (b > a);
19989}
19990
19991/* Sort and remove duplicates of all symbols' cu_indices lists. */
19992
19993static void
19994uniquify_cu_indices (struct mapped_symtab *symtab)
19995{
19996 int i;
19997
19998 for (i = 0; i < symtab->size; ++i)
19999 {
20000 struct symtab_index_entry *entry = symtab->data[i];
20001
20002 if (entry
20003 && entry->cu_indices != NULL)
20004 {
20005 unsigned int next_to_insert, next_to_check;
20006 offset_type last_value;
20007
20008 qsort (VEC_address (offset_type, entry->cu_indices),
20009 VEC_length (offset_type, entry->cu_indices),
20010 sizeof (offset_type), offset_type_compare);
20011
20012 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20013 next_to_insert = 1;
20014 for (next_to_check = 1;
20015 next_to_check < VEC_length (offset_type, entry->cu_indices);
20016 ++next_to_check)
20017 {
20018 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20019 != last_value)
20020 {
20021 last_value = VEC_index (offset_type, entry->cu_indices,
20022 next_to_check);
20023 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20024 last_value);
20025 ++next_to_insert;
20026 }
20027 }
20028 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20029 }
20030 }
9291a0cd
TT
20031}
20032
20033/* Add a vector of indices to the constant pool. */
b89be57b 20034
9291a0cd 20035static offset_type
3876f04e 20036add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20037 struct symtab_index_entry *entry)
20038{
20039 void **slot;
20040
3876f04e 20041 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20042 if (!*slot)
20043 {
20044 offset_type len = VEC_length (offset_type, entry->cu_indices);
20045 offset_type val = MAYBE_SWAP (len);
20046 offset_type iter;
20047 int i;
20048
20049 *slot = entry;
20050 entry->index_offset = obstack_object_size (cpool);
20051
20052 obstack_grow (cpool, &val, sizeof (val));
20053 for (i = 0;
20054 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20055 ++i)
20056 {
20057 val = MAYBE_SWAP (iter);
20058 obstack_grow (cpool, &val, sizeof (val));
20059 }
20060 }
20061 else
20062 {
20063 struct symtab_index_entry *old_entry = *slot;
20064 entry->index_offset = old_entry->index_offset;
20065 entry = old_entry;
20066 }
20067 return entry->index_offset;
20068}
20069
20070/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20071 constant pool entries going into the obstack CPOOL. */
b89be57b 20072
9291a0cd
TT
20073static void
20074write_hash_table (struct mapped_symtab *symtab,
20075 struct obstack *output, struct obstack *cpool)
20076{
20077 offset_type i;
3876f04e 20078 htab_t symbol_hash_table;
9291a0cd
TT
20079 htab_t str_table;
20080
3876f04e 20081 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20082 str_table = create_strtab ();
3876f04e 20083
9291a0cd
TT
20084 /* We add all the index vectors to the constant pool first, to
20085 ensure alignment is ok. */
20086 for (i = 0; i < symtab->size; ++i)
20087 {
20088 if (symtab->data[i])
3876f04e 20089 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20090 }
20091
20092 /* Now write out the hash table. */
20093 for (i = 0; i < symtab->size; ++i)
20094 {
20095 offset_type str_off, vec_off;
20096
20097 if (symtab->data[i])
20098 {
20099 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20100 vec_off = symtab->data[i]->index_offset;
20101 }
20102 else
20103 {
20104 /* While 0 is a valid constant pool index, it is not valid
20105 to have 0 for both offsets. */
20106 str_off = 0;
20107 vec_off = 0;
20108 }
20109
20110 str_off = MAYBE_SWAP (str_off);
20111 vec_off = MAYBE_SWAP (vec_off);
20112
20113 obstack_grow (output, &str_off, sizeof (str_off));
20114 obstack_grow (output, &vec_off, sizeof (vec_off));
20115 }
20116
20117 htab_delete (str_table);
3876f04e 20118 htab_delete (symbol_hash_table);
9291a0cd
TT
20119}
20120
0a5429f6
DE
20121/* Struct to map psymtab to CU index in the index file. */
20122struct psymtab_cu_index_map
20123{
20124 struct partial_symtab *psymtab;
20125 unsigned int cu_index;
20126};
20127
20128static hashval_t
20129hash_psymtab_cu_index (const void *item)
20130{
20131 const struct psymtab_cu_index_map *map = item;
20132
20133 return htab_hash_pointer (map->psymtab);
20134}
20135
20136static int
20137eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20138{
20139 const struct psymtab_cu_index_map *lhs = item_lhs;
20140 const struct psymtab_cu_index_map *rhs = item_rhs;
20141
20142 return lhs->psymtab == rhs->psymtab;
20143}
20144
20145/* Helper struct for building the address table. */
20146struct addrmap_index_data
20147{
20148 struct objfile *objfile;
20149 struct obstack *addr_obstack;
20150 htab_t cu_index_htab;
20151
20152 /* Non-zero if the previous_* fields are valid.
20153 We can't write an entry until we see the next entry (since it is only then
20154 that we know the end of the entry). */
20155 int previous_valid;
20156 /* Index of the CU in the table of all CUs in the index file. */
20157 unsigned int previous_cu_index;
0963b4bd 20158 /* Start address of the CU. */
0a5429f6
DE
20159 CORE_ADDR previous_cu_start;
20160};
20161
20162/* Write an address entry to OBSTACK. */
b89be57b 20163
9291a0cd 20164static void
0a5429f6
DE
20165add_address_entry (struct objfile *objfile, struct obstack *obstack,
20166 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20167{
0a5429f6 20168 offset_type cu_index_to_write;
9291a0cd
TT
20169 char addr[8];
20170 CORE_ADDR baseaddr;
20171
20172 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20173
0a5429f6
DE
20174 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20175 obstack_grow (obstack, addr, 8);
20176 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20177 obstack_grow (obstack, addr, 8);
20178 cu_index_to_write = MAYBE_SWAP (cu_index);
20179 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20180}
20181
20182/* Worker function for traversing an addrmap to build the address table. */
20183
20184static int
20185add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20186{
20187 struct addrmap_index_data *data = datap;
20188 struct partial_symtab *pst = obj;
0a5429f6
DE
20189
20190 if (data->previous_valid)
20191 add_address_entry (data->objfile, data->addr_obstack,
20192 data->previous_cu_start, start_addr,
20193 data->previous_cu_index);
20194
20195 data->previous_cu_start = start_addr;
20196 if (pst != NULL)
20197 {
20198 struct psymtab_cu_index_map find_map, *map;
20199 find_map.psymtab = pst;
20200 map = htab_find (data->cu_index_htab, &find_map);
20201 gdb_assert (map != NULL);
20202 data->previous_cu_index = map->cu_index;
20203 data->previous_valid = 1;
20204 }
20205 else
20206 data->previous_valid = 0;
20207
20208 return 0;
20209}
20210
20211/* Write OBJFILE's address map to OBSTACK.
20212 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20213 in the index file. */
20214
20215static void
20216write_address_map (struct objfile *objfile, struct obstack *obstack,
20217 htab_t cu_index_htab)
20218{
20219 struct addrmap_index_data addrmap_index_data;
20220
20221 /* When writing the address table, we have to cope with the fact that
20222 the addrmap iterator only provides the start of a region; we have to
20223 wait until the next invocation to get the start of the next region. */
20224
20225 addrmap_index_data.objfile = objfile;
20226 addrmap_index_data.addr_obstack = obstack;
20227 addrmap_index_data.cu_index_htab = cu_index_htab;
20228 addrmap_index_data.previous_valid = 0;
20229
20230 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20231 &addrmap_index_data);
20232
20233 /* It's highly unlikely the last entry (end address = 0xff...ff)
20234 is valid, but we should still handle it.
20235 The end address is recorded as the start of the next region, but that
20236 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20237 anyway. */
20238 if (addrmap_index_data.previous_valid)
20239 add_address_entry (objfile, obstack,
20240 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20241 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20242}
20243
156942c7
DE
20244/* Return the symbol kind of PSYM. */
20245
20246static gdb_index_symbol_kind
20247symbol_kind (struct partial_symbol *psym)
20248{
20249 domain_enum domain = PSYMBOL_DOMAIN (psym);
20250 enum address_class aclass = PSYMBOL_CLASS (psym);
20251
20252 switch (domain)
20253 {
20254 case VAR_DOMAIN:
20255 switch (aclass)
20256 {
20257 case LOC_BLOCK:
20258 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20259 case LOC_TYPEDEF:
20260 return GDB_INDEX_SYMBOL_KIND_TYPE;
20261 case LOC_COMPUTED:
20262 case LOC_CONST_BYTES:
20263 case LOC_OPTIMIZED_OUT:
20264 case LOC_STATIC:
20265 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20266 case LOC_CONST:
20267 /* Note: It's currently impossible to recognize psyms as enum values
20268 short of reading the type info. For now punt. */
20269 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20270 default:
20271 /* There are other LOC_FOO values that one might want to classify
20272 as variables, but dwarf2read.c doesn't currently use them. */
20273 return GDB_INDEX_SYMBOL_KIND_OTHER;
20274 }
20275 case STRUCT_DOMAIN:
20276 return GDB_INDEX_SYMBOL_KIND_TYPE;
20277 default:
20278 return GDB_INDEX_SYMBOL_KIND_OTHER;
20279 }
20280}
20281
9291a0cd 20282/* Add a list of partial symbols to SYMTAB. */
b89be57b 20283
9291a0cd
TT
20284static void
20285write_psymbols (struct mapped_symtab *symtab,
987d643c 20286 htab_t psyms_seen,
9291a0cd
TT
20287 struct partial_symbol **psymp,
20288 int count,
987d643c
TT
20289 offset_type cu_index,
20290 int is_static)
9291a0cd
TT
20291{
20292 for (; count-- > 0; ++psymp)
20293 {
156942c7
DE
20294 struct partial_symbol *psym = *psymp;
20295 void **slot;
987d643c 20296
156942c7 20297 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20298 error (_("Ada is not currently supported by the index"));
987d643c 20299
987d643c 20300 /* Only add a given psymbol once. */
156942c7 20301 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20302 if (!*slot)
20303 {
156942c7
DE
20304 gdb_index_symbol_kind kind = symbol_kind (psym);
20305
20306 *slot = psym;
20307 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20308 is_static, kind, cu_index);
987d643c 20309 }
9291a0cd
TT
20310 }
20311}
20312
20313/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20314 exception if there is an error. */
b89be57b 20315
9291a0cd
TT
20316static void
20317write_obstack (FILE *file, struct obstack *obstack)
20318{
20319 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20320 file)
20321 != obstack_object_size (obstack))
20322 error (_("couldn't data write to file"));
20323}
20324
20325/* Unlink a file if the argument is not NULL. */
b89be57b 20326
9291a0cd
TT
20327static void
20328unlink_if_set (void *p)
20329{
20330 char **filename = p;
20331 if (*filename)
20332 unlink (*filename);
20333}
20334
1fd400ff
TT
20335/* A helper struct used when iterating over debug_types. */
20336struct signatured_type_index_data
20337{
20338 struct objfile *objfile;
20339 struct mapped_symtab *symtab;
20340 struct obstack *types_list;
987d643c 20341 htab_t psyms_seen;
1fd400ff
TT
20342 int cu_index;
20343};
20344
20345/* A helper function that writes a single signatured_type to an
20346 obstack. */
b89be57b 20347
1fd400ff
TT
20348static int
20349write_one_signatured_type (void **slot, void *d)
20350{
20351 struct signatured_type_index_data *info = d;
20352 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20353 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20354 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20355 gdb_byte val[8];
20356
20357 write_psymbols (info->symtab,
987d643c 20358 info->psyms_seen,
3e43a32a
MS
20359 info->objfile->global_psymbols.list
20360 + psymtab->globals_offset,
987d643c
TT
20361 psymtab->n_global_syms, info->cu_index,
20362 0);
1fd400ff 20363 write_psymbols (info->symtab,
987d643c 20364 info->psyms_seen,
3e43a32a
MS
20365 info->objfile->static_psymbols.list
20366 + psymtab->statics_offset,
987d643c
TT
20367 psymtab->n_static_syms, info->cu_index,
20368 1);
1fd400ff 20369
b64f50a1
JK
20370 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20371 entry->per_cu.offset.sect_off);
1fd400ff 20372 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20373 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20374 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20375 obstack_grow (info->types_list, val, 8);
20376 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20377 obstack_grow (info->types_list, val, 8);
20378
20379 ++info->cu_index;
20380
20381 return 1;
20382}
20383
95554aad
TT
20384/* Recurse into all "included" dependencies and write their symbols as
20385 if they appeared in this psymtab. */
20386
20387static void
20388recursively_write_psymbols (struct objfile *objfile,
20389 struct partial_symtab *psymtab,
20390 struct mapped_symtab *symtab,
20391 htab_t psyms_seen,
20392 offset_type cu_index)
20393{
20394 int i;
20395
20396 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20397 if (psymtab->dependencies[i]->user != NULL)
20398 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20399 symtab, psyms_seen, cu_index);
20400
20401 write_psymbols (symtab,
20402 psyms_seen,
20403 objfile->global_psymbols.list + psymtab->globals_offset,
20404 psymtab->n_global_syms, cu_index,
20405 0);
20406 write_psymbols (symtab,
20407 psyms_seen,
20408 objfile->static_psymbols.list + psymtab->statics_offset,
20409 psymtab->n_static_syms, cu_index,
20410 1);
20411}
20412
9291a0cd 20413/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20414
9291a0cd
TT
20415static void
20416write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20417{
20418 struct cleanup *cleanup;
20419 char *filename, *cleanup_filename;
1fd400ff
TT
20420 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20421 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20422 int i;
20423 FILE *out_file;
20424 struct mapped_symtab *symtab;
20425 offset_type val, size_of_contents, total_len;
20426 struct stat st;
987d643c 20427 htab_t psyms_seen;
0a5429f6
DE
20428 htab_t cu_index_htab;
20429 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20430
b4f2f049 20431 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20432 return;
b4f2f049 20433
9291a0cd
TT
20434 if (dwarf2_per_objfile->using_index)
20435 error (_("Cannot use an index to create the index"));
20436
8b70b953
TT
20437 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20438 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20439
9291a0cd 20440 if (stat (objfile->name, &st) < 0)
7e17e088 20441 perror_with_name (objfile->name);
9291a0cd
TT
20442
20443 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20444 INDEX_SUFFIX, (char *) NULL);
20445 cleanup = make_cleanup (xfree, filename);
20446
20447 out_file = fopen (filename, "wb");
20448 if (!out_file)
20449 error (_("Can't open `%s' for writing"), filename);
20450
20451 cleanup_filename = filename;
20452 make_cleanup (unlink_if_set, &cleanup_filename);
20453
20454 symtab = create_mapped_symtab ();
20455 make_cleanup (cleanup_mapped_symtab, symtab);
20456
20457 obstack_init (&addr_obstack);
20458 make_cleanup_obstack_free (&addr_obstack);
20459
20460 obstack_init (&cu_list);
20461 make_cleanup_obstack_free (&cu_list);
20462
1fd400ff
TT
20463 obstack_init (&types_cu_list);
20464 make_cleanup_obstack_free (&types_cu_list);
20465
987d643c
TT
20466 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20467 NULL, xcalloc, xfree);
96408a79 20468 make_cleanup_htab_delete (psyms_seen);
987d643c 20469
0a5429f6
DE
20470 /* While we're scanning CU's create a table that maps a psymtab pointer
20471 (which is what addrmap records) to its index (which is what is recorded
20472 in the index file). This will later be needed to write the address
20473 table. */
20474 cu_index_htab = htab_create_alloc (100,
20475 hash_psymtab_cu_index,
20476 eq_psymtab_cu_index,
20477 NULL, xcalloc, xfree);
96408a79 20478 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20479 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20480 xmalloc (sizeof (struct psymtab_cu_index_map)
20481 * dwarf2_per_objfile->n_comp_units);
20482 make_cleanup (xfree, psymtab_cu_index_map);
20483
20484 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20485 work here. Also, the debug_types entries do not appear in
20486 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20487 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20488 {
3e43a32a
MS
20489 struct dwarf2_per_cu_data *per_cu
20490 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20491 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20492 gdb_byte val[8];
0a5429f6
DE
20493 struct psymtab_cu_index_map *map;
20494 void **slot;
9291a0cd 20495
95554aad
TT
20496 if (psymtab->user == NULL)
20497 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20498
0a5429f6
DE
20499 map = &psymtab_cu_index_map[i];
20500 map->psymtab = psymtab;
20501 map->cu_index = i;
20502 slot = htab_find_slot (cu_index_htab, map, INSERT);
20503 gdb_assert (slot != NULL);
20504 gdb_assert (*slot == NULL);
20505 *slot = map;
9291a0cd 20506
b64f50a1
JK
20507 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20508 per_cu->offset.sect_off);
9291a0cd 20509 obstack_grow (&cu_list, val, 8);
e254ef6a 20510 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20511 obstack_grow (&cu_list, val, 8);
20512 }
20513
0a5429f6
DE
20514 /* Dump the address map. */
20515 write_address_map (objfile, &addr_obstack, cu_index_htab);
20516
1fd400ff
TT
20517 /* Write out the .debug_type entries, if any. */
20518 if (dwarf2_per_objfile->signatured_types)
20519 {
20520 struct signatured_type_index_data sig_data;
20521
20522 sig_data.objfile = objfile;
20523 sig_data.symtab = symtab;
20524 sig_data.types_list = &types_cu_list;
987d643c 20525 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20526 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20527 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20528 write_one_signatured_type, &sig_data);
20529 }
20530
156942c7
DE
20531 /* Now that we've processed all symbols we can shrink their cu_indices
20532 lists. */
20533 uniquify_cu_indices (symtab);
20534
9291a0cd
TT
20535 obstack_init (&constant_pool);
20536 make_cleanup_obstack_free (&constant_pool);
20537 obstack_init (&symtab_obstack);
20538 make_cleanup_obstack_free (&symtab_obstack);
20539 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20540
20541 obstack_init (&contents);
20542 make_cleanup_obstack_free (&contents);
1fd400ff 20543 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20544 total_len = size_of_contents;
20545
20546 /* The version number. */
796a7ff8 20547 val = MAYBE_SWAP (8);
9291a0cd
TT
20548 obstack_grow (&contents, &val, sizeof (val));
20549
20550 /* The offset of the CU list from the start of the file. */
20551 val = MAYBE_SWAP (total_len);
20552 obstack_grow (&contents, &val, sizeof (val));
20553 total_len += obstack_object_size (&cu_list);
20554
1fd400ff
TT
20555 /* The offset of the types CU list from the start of the file. */
20556 val = MAYBE_SWAP (total_len);
20557 obstack_grow (&contents, &val, sizeof (val));
20558 total_len += obstack_object_size (&types_cu_list);
20559
9291a0cd
TT
20560 /* The offset of the address table from the start of the file. */
20561 val = MAYBE_SWAP (total_len);
20562 obstack_grow (&contents, &val, sizeof (val));
20563 total_len += obstack_object_size (&addr_obstack);
20564
20565 /* The offset of the symbol table from the start of the file. */
20566 val = MAYBE_SWAP (total_len);
20567 obstack_grow (&contents, &val, sizeof (val));
20568 total_len += obstack_object_size (&symtab_obstack);
20569
20570 /* The offset of the constant pool from the start of the file. */
20571 val = MAYBE_SWAP (total_len);
20572 obstack_grow (&contents, &val, sizeof (val));
20573 total_len += obstack_object_size (&constant_pool);
20574
20575 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20576
20577 write_obstack (out_file, &contents);
20578 write_obstack (out_file, &cu_list);
1fd400ff 20579 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20580 write_obstack (out_file, &addr_obstack);
20581 write_obstack (out_file, &symtab_obstack);
20582 write_obstack (out_file, &constant_pool);
20583
20584 fclose (out_file);
20585
20586 /* We want to keep the file, so we set cleanup_filename to NULL
20587 here. See unlink_if_set. */
20588 cleanup_filename = NULL;
20589
20590 do_cleanups (cleanup);
20591}
20592
90476074
TT
20593/* Implementation of the `save gdb-index' command.
20594
20595 Note that the file format used by this command is documented in the
20596 GDB manual. Any changes here must be documented there. */
11570e71 20597
9291a0cd
TT
20598static void
20599save_gdb_index_command (char *arg, int from_tty)
20600{
20601 struct objfile *objfile;
20602
20603 if (!arg || !*arg)
96d19272 20604 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20605
20606 ALL_OBJFILES (objfile)
20607 {
20608 struct stat st;
20609
20610 /* If the objfile does not correspond to an actual file, skip it. */
20611 if (stat (objfile->name, &st) < 0)
20612 continue;
20613
20614 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20615 if (dwarf2_per_objfile)
20616 {
20617 volatile struct gdb_exception except;
20618
20619 TRY_CATCH (except, RETURN_MASK_ERROR)
20620 {
20621 write_psymtabs_to_index (objfile, arg);
20622 }
20623 if (except.reason < 0)
20624 exception_fprintf (gdb_stderr, except,
20625 _("Error while writing index for `%s': "),
20626 objfile->name);
20627 }
20628 }
dce234bc
PP
20629}
20630
9291a0cd
TT
20631\f
20632
9eae7c52
TT
20633int dwarf2_always_disassemble;
20634
20635static void
20636show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20637 struct cmd_list_element *c, const char *value)
20638{
3e43a32a
MS
20639 fprintf_filtered (file,
20640 _("Whether to always disassemble "
20641 "DWARF expressions is %s.\n"),
9eae7c52
TT
20642 value);
20643}
20644
900e11f9
JK
20645static void
20646show_check_physname (struct ui_file *file, int from_tty,
20647 struct cmd_list_element *c, const char *value)
20648{
20649 fprintf_filtered (file,
20650 _("Whether to check \"physname\" is %s.\n"),
20651 value);
20652}
20653
6502dd73
DJ
20654void _initialize_dwarf2_read (void);
20655
20656void
20657_initialize_dwarf2_read (void)
20658{
96d19272
JK
20659 struct cmd_list_element *c;
20660
dce234bc 20661 dwarf2_objfile_data_key
c1bd65d0 20662 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20663
1bedd215
AC
20664 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20665Set DWARF 2 specific variables.\n\
20666Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20667 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20668 0/*allow-unknown*/, &maintenance_set_cmdlist);
20669
1bedd215
AC
20670 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20671Show DWARF 2 specific variables\n\
20672Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20673 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20674 0/*allow-unknown*/, &maintenance_show_cmdlist);
20675
20676 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20677 &dwarf2_max_cache_age, _("\
20678Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20679Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20680A higher limit means that cached compilation units will be stored\n\
20681in memory longer, and more total memory will be used. Zero disables\n\
20682caching, which can slow down startup."),
2c5b56ce 20683 NULL,
920d2a44 20684 show_dwarf2_max_cache_age,
2c5b56ce 20685 &set_dwarf2_cmdlist,
ae038cb0 20686 &show_dwarf2_cmdlist);
d97bc12b 20687
9eae7c52
TT
20688 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20689 &dwarf2_always_disassemble, _("\
20690Set whether `info address' always disassembles DWARF expressions."), _("\
20691Show whether `info address' always disassembles DWARF expressions."), _("\
20692When enabled, DWARF expressions are always printed in an assembly-like\n\
20693syntax. When disabled, expressions will be printed in a more\n\
20694conversational style, when possible."),
20695 NULL,
20696 show_dwarf2_always_disassemble,
20697 &set_dwarf2_cmdlist,
20698 &show_dwarf2_cmdlist);
20699
45cfd468
DE
20700 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20701Set debugging of the dwarf2 reader."), _("\
20702Show debugging of the dwarf2 reader."), _("\
20703When enabled, debugging messages are printed during dwarf2 reading\n\
20704and symtab expansion."),
20705 NULL,
20706 NULL,
20707 &setdebuglist, &showdebuglist);
20708
ccce17b0 20709 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20710Set debugging of the dwarf2 DIE reader."), _("\
20711Show debugging of the dwarf2 DIE reader."), _("\
20712When enabled (non-zero), DIEs are dumped after they are read in.\n\
20713The value is the maximum depth to print."),
ccce17b0
YQ
20714 NULL,
20715 NULL,
20716 &setdebuglist, &showdebuglist);
9291a0cd 20717
900e11f9
JK
20718 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20719Set cross-checking of \"physname\" code against demangler."), _("\
20720Show cross-checking of \"physname\" code against demangler."), _("\
20721When enabled, GDB's internal \"physname\" code is checked against\n\
20722the demangler."),
20723 NULL, show_check_physname,
20724 &setdebuglist, &showdebuglist);
20725
e615022a
DE
20726 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20727 no_class, &use_deprecated_index_sections, _("\
20728Set whether to use deprecated gdb_index sections."), _("\
20729Show whether to use deprecated gdb_index sections."), _("\
20730When enabled, deprecated .gdb_index sections are used anyway.\n\
20731Normally they are ignored either because of a missing feature or\n\
20732performance issue.\n\
20733Warning: This option must be enabled before gdb reads the file."),
20734 NULL,
20735 NULL,
20736 &setlist, &showlist);
20737
96d19272 20738 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20739 _("\
fc1a9d6e 20740Save a gdb-index file.\n\
11570e71 20741Usage: save gdb-index DIRECTORY"),
96d19272
JK
20742 &save_cmdlist);
20743 set_cmd_completer (c, filename_completer);
f1e6e072
TT
20744
20745 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20746 &dwarf2_locexpr_funcs);
20747 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20748 &dwarf2_loclist_funcs);
20749
20750 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20751 &dwarf2_block_frame_base_locexpr_funcs);
20752 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20753 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 20754}
This page took 2.95433 seconds and 4 git commands to generate.