Fix the x87 FP register printout when issuing the “info float” command.
[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;
d521ce57 105 const gdb_byte *buffer;
dce234bc 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.
02142a6c 276 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 277 htab_t die_type_hash;
95554aad
TT
278
279 /* The CUs we recently read. */
280 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
281};
282
283static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 284
251d32d9 285/* Default names of the debugging sections. */
c906108c 286
233a11ab
CS
287/* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
9cdd5dbd
DE
290static const struct dwarf2_debug_sections dwarf2_elf_names =
291{
251d32d9
TG
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 297 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_ranges", ".zdebug_ranges" },
300 { ".debug_types", ".zdebug_types" },
3019eac3 301 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
24d3216f
TT
304 { ".gdb_index", ".zgdb_index" },
305 23
251d32d9 306};
c906108c 307
80626a55 308/* List of DWO/DWP sections. */
3019eac3 309
80626a55 310static const struct dwop_section_names
3019eac3
DE
311{
312 struct dwarf2_section_names abbrev_dwo;
313 struct dwarf2_section_names info_dwo;
314 struct dwarf2_section_names line_dwo;
315 struct dwarf2_section_names loc_dwo;
09262596
DE
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
3019eac3
DE
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
80626a55
DE
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
3019eac3 323}
80626a55 324dwop_section_names =
3019eac3
DE
325{
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
330 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
332 { ".debug_str.dwo", ".zdebug_str.dwo" },
333 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
335 { ".debug_cu_index", ".zdebug_cu_index" },
336 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
337};
338
c906108c
SS
339/* local data types */
340
107d2387
AC
341/* The data in a compilation unit header, after target2host
342 translation, looks like this. */
c906108c 343struct comp_unit_head
a738430d 344{
c764a876 345 unsigned int length;
a738430d 346 short version;
a738430d
MK
347 unsigned char addr_size;
348 unsigned char signed_addr_p;
b64f50a1 349 sect_offset abbrev_offset;
57349743 350
a738430d
MK
351 /* Size of file offsets; either 4 or 8. */
352 unsigned int offset_size;
57349743 353
a738430d
MK
354 /* Size of the length field; either 4 or 12. */
355 unsigned int initial_length_size;
57349743 356
a738430d
MK
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
b64f50a1 359 sect_offset offset;
57349743 360
d00adf39
DE
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
b64f50a1 363 cu_offset first_die_offset;
a738430d 364};
c906108c 365
3da10d80
KS
366/* Type used for delaying computation of method physnames.
367 See comments for compute_delayed_physnames. */
368struct delayed_method_info
369{
370 /* The type to which the method is attached, i.e., its parent class. */
371 struct type *type;
372
373 /* The index of the method in the type's function fieldlists. */
374 int fnfield_index;
375
376 /* The index of the method in the fieldlist. */
377 int index;
378
379 /* The name of the DIE. */
380 const char *name;
381
382 /* The DIE associated with this method. */
383 struct die_info *die;
384};
385
386typedef struct delayed_method_info delayed_method_info;
387DEF_VEC_O (delayed_method_info);
388
e7c27a73
DJ
389/* Internal state when decoding a particular compilation unit. */
390struct dwarf2_cu
391{
392 /* The objfile containing this compilation unit. */
393 struct objfile *objfile;
394
d00adf39 395 /* The header of the compilation unit. */
e7c27a73 396 struct comp_unit_head header;
e142c38c 397
d00adf39
DE
398 /* Base address of this compilation unit. */
399 CORE_ADDR base_address;
400
401 /* Non-zero if base_address has been set. */
402 int base_known;
403
e142c38c
DJ
404 /* The language we are debugging. */
405 enum language language;
406 const struct language_defn *language_defn;
407
b0f35d58
DL
408 const char *producer;
409
e142c38c
DJ
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope;
420
433df2d4
DE
421 /* The abbrev table for this CU.
422 Normally this points to the abbrev table in the objfile.
423 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
424 struct abbrev_table *abbrev_table;
72bf9492 425
b64f50a1
JK
426 /* Hash table holding all the loaded partial DIEs
427 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
428 htab_t partial_dies;
429
430 /* Storage for things with the same lifetime as this read-in compilation
431 unit, including partial DIEs. */
432 struct obstack comp_unit_obstack;
433
ae038cb0
DJ
434 /* When multiple dwarf2_cu structures are living in memory, this field
435 chains them all together, so that they can be released efficiently.
436 We will probably also want a generation counter so that most-recently-used
437 compilation units are cached... */
438 struct dwarf2_per_cu_data *read_in_chain;
439
440 /* Backchain to our per_cu entry if the tree has been built. */
441 struct dwarf2_per_cu_data *per_cu;
442
443 /* How many compilation units ago was this CU last referenced? */
444 int last_used;
445
b64f50a1
JK
446 /* A hash table of DIE cu_offset for following references with
447 die_info->offset.sect_off as hash. */
51545339 448 htab_t die_hash;
10b3939b
DJ
449
450 /* Full DIEs if read in. */
451 struct die_info *dies;
452
453 /* A set of pointers to dwarf2_per_cu_data objects for compilation
454 units referenced by this one. Only set during full symbol processing;
455 partial symbol tables do not have dependencies. */
456 htab_t dependencies;
457
cb1df416
DJ
458 /* Header data from the line table, during full symbol processing. */
459 struct line_header *line_header;
460
3da10d80
KS
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 VEC (delayed_method_info) *method_list;
464
96408a79
SA
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab;
467
034e5797
DE
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
3019eac3
DE
477 struct dwo_unit *dwo_unit;
478
479 /* The DW_AT_addr_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE. */
482 ULONGEST addr_base;
483
2e3cf129
DE
484 /* The DW_AT_ranges_base attribute if present, zero otherwise
485 (zero is a valid value though).
486 Note this value comes from the stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_ranges_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
494 ULONGEST ranges_base;
495
ae038cb0
DJ
496 /* Mark used when releasing cached dies. */
497 unsigned int mark : 1;
498
8be455d7
JK
499 /* This CU references .debug_loc. See the symtab->locations_valid field.
500 This test is imperfect as there may exist optimized debug code not using
501 any location list and still facing inlining issues if handled as
502 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 503 unsigned int has_loclist : 1;
ba919b58 504
1b80a9fa
JK
505 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
506 if all the producer_is_* fields are valid. This information is cached
507 because profiling CU expansion showed excessive time spent in
508 producer_is_gxx_lt_4_6. */
ba919b58
TT
509 unsigned int checked_producer : 1;
510 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 511 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 512 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
513
514 /* When set, the file that we're processing is known to have
515 debugging info for C++ namespaces. GCC 3.3.x did not produce
516 this information, but later versions do. */
517
518 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
519};
520
10b3939b
DJ
521/* Persistent data held for a compilation unit, even when not
522 processing it. We put a pointer to this structure in the
28dee7f5 523 read_symtab_private field of the psymtab. */
10b3939b 524
ae038cb0
DJ
525struct dwarf2_per_cu_data
526{
36586728 527 /* The start offset and length of this compilation unit.
45452591 528 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
529 initial_length_size.
530 If the DIE refers to a DWO file, this is always of the original die,
531 not the DWO file. */
b64f50a1 532 sect_offset offset;
36586728 533 unsigned int length;
ae038cb0
DJ
534
535 /* Flag indicating this compilation unit will be read in before
536 any of the current compilation units are processed. */
c764a876 537 unsigned int queued : 1;
ae038cb0 538
0d99eb77
DE
539 /* This flag will be set when reading partial DIEs if we need to load
540 absolutely all DIEs for this compilation unit, instead of just the ones
541 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
542 hash table and don't find it. */
543 unsigned int load_all_dies : 1;
544
0186c6a7
DE
545 /* Non-zero if this CU is from .debug_types.
546 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
547 this is non-zero. */
3019eac3
DE
548 unsigned int is_debug_types : 1;
549
36586728
TT
550 /* Non-zero if this CU is from the .dwz file. */
551 unsigned int is_dwz : 1;
552
3019eac3
DE
553 /* The section this CU/TU lives in.
554 If the DIE refers to a DWO file, this is always the original die,
555 not the DWO file. */
8a0459fd 556 struct dwarf2_section_info *section;
348e048f 557
17ea53c3
JK
558 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
559 of the CU cache it gets reset to NULL again. */
ae038cb0 560 struct dwarf2_cu *cu;
1c379e20 561
9cdd5dbd
DE
562 /* The corresponding objfile.
563 Normally we can get the objfile from dwarf2_per_objfile.
564 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
565 struct objfile *objfile;
566
567 /* When using partial symbol tables, the 'psymtab' field is active.
568 Otherwise the 'quick' field is active. */
569 union
570 {
571 /* The partial symbol table associated with this compilation unit,
95554aad 572 or NULL for unread partial units. */
9291a0cd
TT
573 struct partial_symtab *psymtab;
574
575 /* Data needed by the "quick" functions. */
576 struct dwarf2_per_cu_quick_data *quick;
577 } v;
95554aad 578
796a7ff8
DE
579 /* The CUs we import using DW_TAG_imported_unit. This is filled in
580 while reading psymtabs, used to compute the psymtab dependencies,
581 and then cleared. Then it is filled in again while reading full
582 symbols, and only deleted when the objfile is destroyed.
583
584 This is also used to work around a difference between the way gold
585 generates .gdb_index version <=7 and the way gdb does. Arguably this
586 is a gold bug. For symbols coming from TUs, gold records in the index
587 the CU that includes the TU instead of the TU itself. This breaks
588 dw2_lookup_symbol: It assumes that if the index says symbol X lives
589 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
590 will find X. Alas TUs live in their own symtab, so after expanding CU Y
591 we need to look in TU Z to find X. Fortunately, this is akin to
592 DW_TAG_imported_unit, so we just use the same mechanism: For
593 .gdb_index version <=7 this also records the TUs that the CU referred
594 to. Concurrently with this change gdb was modified to emit version 8
595 indices so we only pay a price for gold generated indices. */
596 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
597};
598
348e048f
DE
599/* Entry in the signatured_types hash table. */
600
601struct signatured_type
602{
42e7ad6c 603 /* The "per_cu" object of this type.
ac9ec31b 604 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
605 N.B.: This is the first member so that it's easy to convert pointers
606 between them. */
607 struct dwarf2_per_cu_data per_cu;
608
3019eac3 609 /* The type's signature. */
348e048f
DE
610 ULONGEST signature;
611
3019eac3 612 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
613 If this TU is a DWO stub and the definition lives in a DWO file
614 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
615 cu_offset type_offset_in_tu;
616
617 /* Offset in the section of the type's DIE.
618 If the definition lives in a DWO file, this is the offset in the
619 .debug_types.dwo section.
620 The value is zero until the actual value is known.
621 Zero is otherwise not a valid section offset. */
622 sect_offset type_offset_in_section;
0186c6a7
DE
623
624 /* Type units are grouped by their DW_AT_stmt_list entry so that they
625 can share them. This points to the containing symtab. */
626 struct type_unit_group *type_unit_group;
ac9ec31b
DE
627
628 /* The type.
629 The first time we encounter this type we fully read it in and install it
630 in the symbol tables. Subsequent times we only need the type. */
631 struct type *type;
348e048f
DE
632};
633
0186c6a7
DE
634typedef struct signatured_type *sig_type_ptr;
635DEF_VEC_P (sig_type_ptr);
636
094b34ac
DE
637/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
638 This includes type_unit_group and quick_file_names. */
639
640struct stmt_list_hash
641{
642 /* The DWO unit this table is from or NULL if there is none. */
643 struct dwo_unit *dwo_unit;
644
645 /* Offset in .debug_line or .debug_line.dwo. */
646 sect_offset line_offset;
647};
648
f4dc4d17
DE
649/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
650 an object of this type. */
651
652struct type_unit_group
653{
0186c6a7 654 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
655 To simplify things we create an artificial CU that "includes" all the
656 type units using this stmt_list so that the rest of the code still has
657 a "per_cu" handle on the symtab.
658 This PER_CU is recognized by having no section. */
8a0459fd 659#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
660 struct dwarf2_per_cu_data per_cu;
661
0186c6a7
DE
662 /* The TUs that share this DW_AT_stmt_list entry.
663 This is added to while parsing type units to build partial symtabs,
664 and is deleted afterwards and not used again. */
665 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
666
667 /* The primary symtab.
094b34ac
DE
668 Type units in a group needn't all be defined in the same source file,
669 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
670 struct symtab *primary_symtab;
671
094b34ac
DE
672 /* The data used to construct the hash key. */
673 struct stmt_list_hash hash;
f4dc4d17
DE
674
675 /* The number of symtabs from the line header.
676 The value here must match line_header.num_file_names. */
677 unsigned int num_symtabs;
678
679 /* The symbol tables for this TU (obtained from the files listed in
680 DW_AT_stmt_list).
681 WARNING: The order of entries here must match the order of entries
682 in the line header. After the first TU using this type_unit_group, the
683 line header for the subsequent TUs is recreated from this. This is done
684 because we need to use the same symtabs for each TU using the same
685 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
686 there's no guarantee the line header doesn't have duplicate entries. */
687 struct symtab **symtabs;
688};
689
80626a55 690/* These sections are what may appear in a DWO file. */
3019eac3
DE
691
692struct dwo_sections
693{
694 struct dwarf2_section_info abbrev;
3019eac3
DE
695 struct dwarf2_section_info line;
696 struct dwarf2_section_info loc;
09262596
DE
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
3019eac3
DE
699 struct dwarf2_section_info str;
700 struct dwarf2_section_info str_offsets;
80626a55
DE
701 /* In the case of a virtual DWO file, these two are unused. */
702 struct dwarf2_section_info info;
3019eac3
DE
703 VEC (dwarf2_section_info_def) *types;
704};
705
c88ee1f0 706/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
707
708struct dwo_unit
709{
710 /* Backlink to the containing struct dwo_file. */
711 struct dwo_file *dwo_file;
712
713 /* The "id" that distinguishes this CU/TU.
714 .debug_info calls this "dwo_id", .debug_types calls this "signature".
715 Since signatures came first, we stick with it for consistency. */
716 ULONGEST signature;
717
718 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 719 struct dwarf2_section_info *section;
3019eac3
DE
720
721 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
722 sect_offset offset;
723 unsigned int length;
724
725 /* For types, offset in the type's DIE of the type defined by this TU. */
726 cu_offset type_offset_in_tu;
727};
728
80626a55
DE
729/* Data for one DWO file.
730 This includes virtual DWO files that have been packaged into a
731 DWP file. */
3019eac3
DE
732
733struct dwo_file
734{
0ac5b59e 735 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
736 For virtual DWO files the name is constructed from the section offsets
737 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
738 from related CU+TUs. */
0ac5b59e
DE
739 const char *dwo_name;
740
741 /* The DW_AT_comp_dir attribute. */
742 const char *comp_dir;
3019eac3 743
80626a55
DE
744 /* The bfd, when the file is open. Otherwise this is NULL.
745 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
746 bfd *dbfd;
3019eac3
DE
747
748 /* Section info for this file. */
749 struct dwo_sections sections;
750
19c3d4c9
DE
751 /* The CU in the file.
752 We only support one because having more than one requires hacking the
753 dwo_name of each to match, which is highly unlikely to happen.
754 Doing this means all TUs can share comp_dir: We also assume that
755 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
756 struct dwo_unit *cu;
3019eac3
DE
757
758 /* Table of TUs in the file.
759 Each element is a struct dwo_unit. */
760 htab_t tus;
761};
762
80626a55
DE
763/* These sections are what may appear in a DWP file. */
764
765struct dwp_sections
766{
767 struct dwarf2_section_info str;
768 struct dwarf2_section_info cu_index;
769 struct dwarf2_section_info tu_index;
770 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
771 by section number. We don't need to record them here. */
772};
773
774/* These sections are what may appear in a virtual DWO file. */
775
776struct virtual_dwo_sections
777{
778 struct dwarf2_section_info abbrev;
779 struct dwarf2_section_info line;
780 struct dwarf2_section_info loc;
781 struct dwarf2_section_info macinfo;
782 struct dwarf2_section_info macro;
783 struct dwarf2_section_info str_offsets;
784 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 785 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
786 struct dwarf2_section_info info_or_types;
787};
788
789/* Contents of DWP hash tables. */
790
791struct dwp_hash_table
792{
793 uint32_t nr_units, nr_slots;
794 const gdb_byte *hash_table, *unit_table, *section_pool;
795};
796
797/* Data for one DWP file. */
798
799struct dwp_file
800{
801 /* Name of the file. */
802 const char *name;
803
804 /* The bfd, when the file is open. Otherwise this is NULL. */
805 bfd *dbfd;
806
807 /* Section info for this file. */
808 struct dwp_sections sections;
809
810 /* Table of CUs in the file. */
811 const struct dwp_hash_table *cus;
812
813 /* Table of TUs in the file. */
814 const struct dwp_hash_table *tus;
815
816 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
817 htab_t loaded_cutus;
818
819 /* Table to map ELF section numbers to their sections. */
820 unsigned int num_sections;
821 asection **elf_sections;
822};
823
36586728
TT
824/* This represents a '.dwz' file. */
825
826struct dwz_file
827{
828 /* A dwz file can only contain a few sections. */
829 struct dwarf2_section_info abbrev;
830 struct dwarf2_section_info info;
831 struct dwarf2_section_info str;
832 struct dwarf2_section_info line;
833 struct dwarf2_section_info macro;
2ec9a5e0 834 struct dwarf2_section_info gdb_index;
36586728
TT
835
836 /* The dwz's BFD. */
837 bfd *dwz_bfd;
838};
839
0963b4bd
MS
840/* Struct used to pass misc. parameters to read_die_and_children, et
841 al. which are used for both .debug_info and .debug_types dies.
842 All parameters here are unchanging for the life of the call. This
dee91e82 843 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
844
845struct die_reader_specs
846{
dee91e82 847 /* die_section->asection->owner. */
93311388
DE
848 bfd* abfd;
849
850 /* The CU of the DIE we are parsing. */
851 struct dwarf2_cu *cu;
852
80626a55 853 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
854 struct dwo_file *dwo_file;
855
dee91e82 856 /* The section the die comes from.
3019eac3 857 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
858 struct dwarf2_section_info *die_section;
859
860 /* die_section->buffer. */
d521ce57 861 const gdb_byte *buffer;
f664829e
DE
862
863 /* The end of the buffer. */
864 const gdb_byte *buffer_end;
93311388
DE
865};
866
fd820528 867/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 868typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 869 const gdb_byte *info_ptr,
dee91e82
DE
870 struct die_info *comp_unit_die,
871 int has_children,
872 void *data);
873
debd256d
JB
874/* The line number information for a compilation unit (found in the
875 .debug_line section) begins with a "statement program header",
876 which contains the following information. */
877struct line_header
878{
879 unsigned int total_length;
880 unsigned short version;
881 unsigned int header_length;
882 unsigned char minimum_instruction_length;
2dc7f7b3 883 unsigned char maximum_ops_per_instruction;
debd256d
JB
884 unsigned char default_is_stmt;
885 int line_base;
886 unsigned char line_range;
887 unsigned char opcode_base;
888
889 /* standard_opcode_lengths[i] is the number of operands for the
890 standard opcode whose value is i. This means that
891 standard_opcode_lengths[0] is unused, and the last meaningful
892 element is standard_opcode_lengths[opcode_base - 1]. */
893 unsigned char *standard_opcode_lengths;
894
895 /* The include_directories table. NOTE! These strings are not
896 allocated with xmalloc; instead, they are pointers into
897 debug_line_buffer. If you try to free them, `free' will get
898 indigestion. */
899 unsigned int num_include_dirs, include_dirs_size;
d521ce57 900 const char **include_dirs;
debd256d
JB
901
902 /* The file_names table. NOTE! These strings are not allocated
903 with xmalloc; instead, they are pointers into debug_line_buffer.
904 Don't try to free them directly. */
905 unsigned int num_file_names, file_names_size;
906 struct file_entry
c906108c 907 {
d521ce57 908 const char *name;
debd256d
JB
909 unsigned int dir_index;
910 unsigned int mod_time;
911 unsigned int length;
aaa75496 912 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 913 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
914 } *file_names;
915
916 /* The start and end of the statement program following this
6502dd73 917 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 918 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 919};
c906108c
SS
920
921/* When we construct a partial symbol table entry we only
0963b4bd 922 need this much information. */
c906108c
SS
923struct partial_die_info
924 {
72bf9492 925 /* Offset of this DIE. */
b64f50a1 926 sect_offset offset;
72bf9492
DJ
927
928 /* DWARF-2 tag for this DIE. */
929 ENUM_BITFIELD(dwarf_tag) tag : 16;
930
72bf9492
DJ
931 /* Assorted flags describing the data found in this DIE. */
932 unsigned int has_children : 1;
933 unsigned int is_external : 1;
934 unsigned int is_declaration : 1;
935 unsigned int has_type : 1;
936 unsigned int has_specification : 1;
937 unsigned int has_pc_info : 1;
481860b3 938 unsigned int may_be_inlined : 1;
72bf9492
DJ
939
940 /* Flag set if the SCOPE field of this structure has been
941 computed. */
942 unsigned int scope_set : 1;
943
fa4028e9
JB
944 /* Flag set if the DIE has a byte_size attribute. */
945 unsigned int has_byte_size : 1;
946
98bfdba5
PA
947 /* Flag set if any of the DIE's children are template arguments. */
948 unsigned int has_template_arguments : 1;
949
abc72ce4
DE
950 /* Flag set if fixup_partial_die has been called on this die. */
951 unsigned int fixup_called : 1;
952
36586728
TT
953 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
954 unsigned int is_dwz : 1;
955
956 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
957 unsigned int spec_is_dwz : 1;
958
72bf9492 959 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 960 sometimes a default name for unnamed DIEs. */
15d034d0 961 const char *name;
72bf9492 962
abc72ce4
DE
963 /* The linkage name, if present. */
964 const char *linkage_name;
965
72bf9492
DJ
966 /* The scope to prepend to our children. This is generally
967 allocated on the comp_unit_obstack, so will disappear
968 when this compilation unit leaves the cache. */
15d034d0 969 const char *scope;
72bf9492 970
95554aad
TT
971 /* Some data associated with the partial DIE. The tag determines
972 which field is live. */
973 union
974 {
975 /* The location description associated with this DIE, if any. */
976 struct dwarf_block *locdesc;
977 /* The offset of an import, for DW_TAG_imported_unit. */
978 sect_offset offset;
979 } d;
72bf9492
DJ
980
981 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
982 CORE_ADDR lowpc;
983 CORE_ADDR highpc;
72bf9492 984
93311388 985 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 986 DW_AT_sibling, if any. */
abc72ce4
DE
987 /* NOTE: This member isn't strictly necessary, read_partial_die could
988 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 989 const gdb_byte *sibling;
72bf9492
DJ
990
991 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
992 DW_AT_specification (or DW_AT_abstract_origin or
993 DW_AT_extension). */
b64f50a1 994 sect_offset spec_offset;
72bf9492
DJ
995
996 /* Pointers to this DIE's parent, first child, and next sibling,
997 if any. */
998 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
999 };
1000
0963b4bd 1001/* This data structure holds the information of an abbrev. */
c906108c
SS
1002struct abbrev_info
1003 {
1004 unsigned int number; /* number identifying abbrev */
1005 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1006 unsigned short has_children; /* boolean */
1007 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1008 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1009 struct abbrev_info *next; /* next in chain */
1010 };
1011
1012struct attr_abbrev
1013 {
9d25dd43
DE
1014 ENUM_BITFIELD(dwarf_attribute) name : 16;
1015 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1016 };
1017
433df2d4
DE
1018/* Size of abbrev_table.abbrev_hash_table. */
1019#define ABBREV_HASH_SIZE 121
1020
1021/* Top level data structure to contain an abbreviation table. */
1022
1023struct abbrev_table
1024{
f4dc4d17
DE
1025 /* Where the abbrev table came from.
1026 This is used as a sanity check when the table is used. */
433df2d4
DE
1027 sect_offset offset;
1028
1029 /* Storage for the abbrev table. */
1030 struct obstack abbrev_obstack;
1031
1032 /* Hash table of abbrevs.
1033 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1034 It could be statically allocated, but the previous code didn't so we
1035 don't either. */
1036 struct abbrev_info **abbrevs;
1037};
1038
0963b4bd 1039/* Attributes have a name and a value. */
b60c80d6
DJ
1040struct attribute
1041 {
9d25dd43 1042 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1043 ENUM_BITFIELD(dwarf_form) form : 15;
1044
1045 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1046 field should be in u.str (existing only for DW_STRING) but it is kept
1047 here for better struct attribute alignment. */
1048 unsigned int string_is_canonical : 1;
1049
b60c80d6
DJ
1050 union
1051 {
15d034d0 1052 const char *str;
b60c80d6 1053 struct dwarf_block *blk;
43bbcdc2
PH
1054 ULONGEST unsnd;
1055 LONGEST snd;
b60c80d6 1056 CORE_ADDR addr;
ac9ec31b 1057 ULONGEST signature;
b60c80d6
DJ
1058 }
1059 u;
1060 };
1061
0963b4bd 1062/* This data structure holds a complete die structure. */
c906108c
SS
1063struct die_info
1064 {
76815b17
DE
1065 /* DWARF-2 tag for this DIE. */
1066 ENUM_BITFIELD(dwarf_tag) tag : 16;
1067
1068 /* Number of attributes */
98bfdba5
PA
1069 unsigned char num_attrs;
1070
1071 /* True if we're presently building the full type name for the
1072 type derived from this DIE. */
1073 unsigned char building_fullname : 1;
76815b17
DE
1074
1075 /* Abbrev number */
1076 unsigned int abbrev;
1077
93311388 1078 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1079 sect_offset offset;
78ba4af6
JB
1080
1081 /* The dies in a compilation unit form an n-ary tree. PARENT
1082 points to this die's parent; CHILD points to the first child of
1083 this node; and all the children of a given node are chained
4950bc1c 1084 together via their SIBLING fields. */
639d11d3
DC
1085 struct die_info *child; /* Its first child, if any. */
1086 struct die_info *sibling; /* Its next sibling, if any. */
1087 struct die_info *parent; /* Its parent, if any. */
c906108c 1088
b60c80d6
DJ
1089 /* An array of attributes, with NUM_ATTRS elements. There may be
1090 zero, but it's not common and zero-sized arrays are not
1091 sufficiently portable C. */
1092 struct attribute attrs[1];
c906108c
SS
1093 };
1094
0963b4bd 1095/* Get at parts of an attribute structure. */
c906108c
SS
1096
1097#define DW_STRING(attr) ((attr)->u.str)
8285870a 1098#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1099#define DW_UNSND(attr) ((attr)->u.unsnd)
1100#define DW_BLOCK(attr) ((attr)->u.blk)
1101#define DW_SND(attr) ((attr)->u.snd)
1102#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1103#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1104
0963b4bd 1105/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1106struct dwarf_block
1107 {
56eb65bd 1108 size_t size;
1d6edc3c
JK
1109
1110 /* Valid only if SIZE is not zero. */
d521ce57 1111 const gdb_byte *data;
c906108c
SS
1112 };
1113
c906108c
SS
1114#ifndef ATTR_ALLOC_CHUNK
1115#define ATTR_ALLOC_CHUNK 4
1116#endif
1117
c906108c
SS
1118/* Allocate fields for structs, unions and enums in this size. */
1119#ifndef DW_FIELD_ALLOC_CHUNK
1120#define DW_FIELD_ALLOC_CHUNK 4
1121#endif
1122
c906108c
SS
1123/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1124 but this would require a corresponding change in unpack_field_as_long
1125 and friends. */
1126static int bits_per_byte = 8;
1127
1128/* The routines that read and process dies for a C struct or C++ class
1129 pass lists of data member fields and lists of member function fields
1130 in an instance of a field_info structure, as defined below. */
1131struct field_info
c5aa993b 1132 {
0963b4bd 1133 /* List of data member and baseclasses fields. */
c5aa993b
JM
1134 struct nextfield
1135 {
1136 struct nextfield *next;
1137 int accessibility;
1138 int virtuality;
1139 struct field field;
1140 }
7d0ccb61 1141 *fields, *baseclasses;
c906108c 1142
7d0ccb61 1143 /* Number of fields (including baseclasses). */
c5aa993b 1144 int nfields;
c906108c 1145
c5aa993b
JM
1146 /* Number of baseclasses. */
1147 int nbaseclasses;
c906108c 1148
c5aa993b
JM
1149 /* Set if the accesibility of one of the fields is not public. */
1150 int non_public_fields;
c906108c 1151
c5aa993b
JM
1152 /* Member function fields array, entries are allocated in the order they
1153 are encountered in the object file. */
1154 struct nextfnfield
1155 {
1156 struct nextfnfield *next;
1157 struct fn_field fnfield;
1158 }
1159 *fnfields;
c906108c 1160
c5aa993b
JM
1161 /* Member function fieldlist array, contains name of possibly overloaded
1162 member function, number of overloaded member functions and a pointer
1163 to the head of the member function field chain. */
1164 struct fnfieldlist
1165 {
15d034d0 1166 const char *name;
c5aa993b
JM
1167 int length;
1168 struct nextfnfield *head;
1169 }
1170 *fnfieldlists;
c906108c 1171
c5aa993b
JM
1172 /* Number of entries in the fnfieldlists array. */
1173 int nfnfields;
98751a41
JK
1174
1175 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1176 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1177 struct typedef_field_list
1178 {
1179 struct typedef_field field;
1180 struct typedef_field_list *next;
1181 }
1182 *typedef_field_list;
1183 unsigned typedef_field_list_count;
c5aa993b 1184 };
c906108c 1185
10b3939b
DJ
1186/* One item on the queue of compilation units to read in full symbols
1187 for. */
1188struct dwarf2_queue_item
1189{
1190 struct dwarf2_per_cu_data *per_cu;
95554aad 1191 enum language pretend_language;
10b3939b
DJ
1192 struct dwarf2_queue_item *next;
1193};
1194
1195/* The current queue. */
1196static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1197
ae038cb0
DJ
1198/* Loaded secondary compilation units are kept in memory until they
1199 have not been referenced for the processing of this many
1200 compilation units. Set this to zero to disable caching. Cache
1201 sizes of up to at least twenty will improve startup time for
1202 typical inter-CU-reference binaries, at an obvious memory cost. */
1203static int dwarf2_max_cache_age = 5;
920d2a44
AC
1204static void
1205show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1206 struct cmd_list_element *c, const char *value)
1207{
3e43a32a
MS
1208 fprintf_filtered (file, _("The upper bound on the age of cached "
1209 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1210 value);
1211}
1212
ae038cb0 1213
0963b4bd 1214/* Various complaints about symbol reading that don't abort the process. */
c906108c 1215
4d3c2250
KB
1216static void
1217dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1218{
4d3c2250 1219 complaint (&symfile_complaints,
e2e0b3e5 1220 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1221}
1222
25e43795
DJ
1223static void
1224dwarf2_debug_line_missing_file_complaint (void)
1225{
1226 complaint (&symfile_complaints,
1227 _(".debug_line section has line data without a file"));
1228}
1229
59205f5a
JB
1230static void
1231dwarf2_debug_line_missing_end_sequence_complaint (void)
1232{
1233 complaint (&symfile_complaints,
3e43a32a
MS
1234 _(".debug_line section has line "
1235 "program sequence without an end"));
59205f5a
JB
1236}
1237
4d3c2250
KB
1238static void
1239dwarf2_complex_location_expr_complaint (void)
2e276125 1240{
e2e0b3e5 1241 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1242}
1243
4d3c2250
KB
1244static void
1245dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1246 int arg3)
2e276125 1247{
4d3c2250 1248 complaint (&symfile_complaints,
3e43a32a
MS
1249 _("const value length mismatch for '%s', got %d, expected %d"),
1250 arg1, arg2, arg3);
4d3c2250
KB
1251}
1252
1253static void
f664829e 1254dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1255{
4d3c2250 1256 complaint (&symfile_complaints,
f664829e
DE
1257 _("debug info runs off end of %s section"
1258 " [in module %s]"),
1259 section->asection->name,
1260 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1261}
1262
1263static void
1264dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1265{
4d3c2250 1266 complaint (&symfile_complaints,
3e43a32a
MS
1267 _("macro debug info contains a "
1268 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1269 arg1);
1270}
1271
1272static void
1273dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1274{
4d3c2250 1275 complaint (&symfile_complaints,
3e43a32a
MS
1276 _("invalid attribute class or form for '%s' in '%s'"),
1277 arg1, arg2);
4d3c2250 1278}
c906108c 1279
c906108c
SS
1280/* local function prototypes */
1281
4efb68b1 1282static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1283
918dd910
JK
1284static void dwarf2_find_base_address (struct die_info *die,
1285 struct dwarf2_cu *cu);
1286
0018ea6f
DE
1287static struct partial_symtab *create_partial_symtab
1288 (struct dwarf2_per_cu_data *per_cu, const char *name);
1289
c67a9c90 1290static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1291
72bf9492
DJ
1292static void scan_partial_symbols (struct partial_die_info *,
1293 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1294 int, struct dwarf2_cu *);
c906108c 1295
72bf9492
DJ
1296static void add_partial_symbol (struct partial_die_info *,
1297 struct dwarf2_cu *);
63d06c5c 1298
72bf9492
DJ
1299static void add_partial_namespace (struct partial_die_info *pdi,
1300 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1301 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1302
5d7cb8df
JK
1303static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1304 CORE_ADDR *highpc, int need_pc,
1305 struct dwarf2_cu *cu);
1306
72bf9492
DJ
1307static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1308 struct dwarf2_cu *cu);
91c24f0a 1309
bc30ff58
JB
1310static void add_partial_subprogram (struct partial_die_info *pdi,
1311 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1312 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1313
257e7a09
YQ
1314static void dwarf2_read_symtab (struct partial_symtab *,
1315 struct objfile *);
c906108c 1316
a14ed312 1317static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1318
433df2d4
DE
1319static struct abbrev_info *abbrev_table_lookup_abbrev
1320 (const struct abbrev_table *, unsigned int);
1321
1322static struct abbrev_table *abbrev_table_read_table
1323 (struct dwarf2_section_info *, sect_offset);
1324
1325static void abbrev_table_free (struct abbrev_table *);
1326
f4dc4d17
DE
1327static void abbrev_table_free_cleanup (void *);
1328
dee91e82
DE
1329static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1330 struct dwarf2_section_info *);
c906108c 1331
f3dd6933 1332static void dwarf2_free_abbrev_table (void *);
c906108c 1333
d521ce57 1334static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1335
dee91e82 1336static struct partial_die_info *load_partial_dies
d521ce57 1337 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1338
d521ce57
TT
1339static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1340 struct partial_die_info *,
1341 struct abbrev_info *,
1342 unsigned int,
1343 const gdb_byte *);
c906108c 1344
36586728 1345static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1346 struct dwarf2_cu *);
72bf9492
DJ
1347
1348static void fixup_partial_die (struct partial_die_info *,
1349 struct dwarf2_cu *);
1350
d521ce57
TT
1351static const gdb_byte *read_attribute (const struct die_reader_specs *,
1352 struct attribute *, struct attr_abbrev *,
1353 const gdb_byte *);
a8329558 1354
a1855c1d 1355static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1356
a1855c1d 1357static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1358
a1855c1d 1359static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1360
a1855c1d 1361static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1362
a1855c1d 1363static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1364
d521ce57 1365static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1366 unsigned int *);
c906108c 1367
d521ce57 1368static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1369
1370static LONGEST read_checked_initial_length_and_offset
d521ce57 1371 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1372 unsigned int *, unsigned int *);
613e1657 1373
d521ce57
TT
1374static LONGEST read_offset (bfd *, const gdb_byte *,
1375 const struct comp_unit_head *,
c764a876
DE
1376 unsigned int *);
1377
d521ce57 1378static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1379
f4dc4d17
DE
1380static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1381 sect_offset);
1382
d521ce57 1383static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1384
d521ce57 1385static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1386
d521ce57
TT
1387static const char *read_indirect_string (bfd *, const gdb_byte *,
1388 const struct comp_unit_head *,
1389 unsigned int *);
4bdf3d34 1390
d521ce57 1391static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1392
d521ce57 1393static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1394
d521ce57 1395static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1396
d521ce57
TT
1397static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1398 const gdb_byte *,
3019eac3
DE
1399 unsigned int *);
1400
d521ce57
TT
1401static const char *read_str_index (const struct die_reader_specs *reader,
1402 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1403
e142c38c 1404static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1405
e142c38c
DJ
1406static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1407 struct dwarf2_cu *);
c906108c 1408
348e048f 1409static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1410 unsigned int);
348e048f 1411
05cf31d1
JB
1412static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1413 struct dwarf2_cu *cu);
1414
e142c38c 1415static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1416
e142c38c 1417static struct die_info *die_specification (struct die_info *die,
f2f0e013 1418 struct dwarf2_cu **);
63d06c5c 1419
debd256d
JB
1420static void free_line_header (struct line_header *lh);
1421
3019eac3
DE
1422static struct line_header *dwarf_decode_line_header (unsigned int offset,
1423 struct dwarf2_cu *cu);
debd256d 1424
f3f5162e
DE
1425static void dwarf_decode_lines (struct line_header *, const char *,
1426 struct dwarf2_cu *, struct partial_symtab *,
1427 int);
c906108c 1428
d521ce57 1429static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1430
f4dc4d17 1431static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1432 const char *, const char *, CORE_ADDR);
f4dc4d17 1433
a14ed312 1434static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1435 struct dwarf2_cu *);
c906108c 1436
34eaf542
TT
1437static struct symbol *new_symbol_full (struct die_info *, struct type *,
1438 struct dwarf2_cu *, struct symbol *);
1439
a14ed312 1440static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1441 struct dwarf2_cu *);
c906108c 1442
98bfdba5
PA
1443static void dwarf2_const_value_attr (struct attribute *attr,
1444 struct type *type,
1445 const char *name,
1446 struct obstack *obstack,
12df843f 1447 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1448 const gdb_byte **bytes,
98bfdba5 1449 struct dwarf2_locexpr_baton **baton);
2df3850c 1450
e7c27a73 1451static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1452
b4ba55a1
JB
1453static int need_gnat_info (struct dwarf2_cu *);
1454
3e43a32a
MS
1455static struct type *die_descriptive_type (struct die_info *,
1456 struct dwarf2_cu *);
b4ba55a1
JB
1457
1458static void set_descriptive_type (struct type *, struct die_info *,
1459 struct dwarf2_cu *);
1460
e7c27a73
DJ
1461static struct type *die_containing_type (struct die_info *,
1462 struct dwarf2_cu *);
c906108c 1463
673bfd45
DE
1464static struct type *lookup_die_type (struct die_info *, struct attribute *,
1465 struct dwarf2_cu *);
c906108c 1466
f792889a 1467static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1468
673bfd45
DE
1469static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1470
0d5cff50 1471static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1472
6e70227d 1473static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1474 const char *suffix, int physname,
1475 struct dwarf2_cu *cu);
63d06c5c 1476
e7c27a73 1477static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1478
348e048f
DE
1479static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1480
e7c27a73 1481static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1482
e7c27a73 1483static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1484
96408a79
SA
1485static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1486
ff013f42
JK
1487static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *, struct partial_symtab *);
1489
a14ed312 1490static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1491 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1492 struct partial_symtab *);
c906108c 1493
fae299cd
DC
1494static void get_scope_pc_bounds (struct die_info *,
1495 CORE_ADDR *, CORE_ADDR *,
1496 struct dwarf2_cu *);
1497
801e3a5b
JB
1498static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1499 CORE_ADDR, struct dwarf2_cu *);
1500
a14ed312 1501static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1502 struct dwarf2_cu *);
c906108c 1503
a14ed312 1504static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1505 struct type *, struct dwarf2_cu *);
c906108c 1506
a14ed312 1507static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1508 struct die_info *, struct type *,
e7c27a73 1509 struct dwarf2_cu *);
c906108c 1510
a14ed312 1511static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1512 struct type *,
1513 struct dwarf2_cu *);
c906108c 1514
134d01f1 1515static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1516
e7c27a73 1517static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1518
e7c27a73 1519static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1520
5d7cb8df
JK
1521static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1522
27aa8d6a
SW
1523static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1524
f55ee35c
JK
1525static struct type *read_module_type (struct die_info *die,
1526 struct dwarf2_cu *cu);
1527
38d518c9 1528static const char *namespace_name (struct die_info *die,
e142c38c 1529 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1530
134d01f1 1531static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1532
e7c27a73 1533static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1534
6e70227d 1535static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1536 struct dwarf2_cu *);
1537
bf6af496 1538static struct die_info *read_die_and_siblings_1
d521ce57 1539 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1540 struct die_info *);
639d11d3 1541
dee91e82 1542static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1543 const gdb_byte *info_ptr,
1544 const gdb_byte **new_info_ptr,
639d11d3
DC
1545 struct die_info *parent);
1546
d521ce57
TT
1547static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1548 struct die_info **, const gdb_byte *,
1549 int *, int);
3019eac3 1550
d521ce57
TT
1551static const gdb_byte *read_full_die (const struct die_reader_specs *,
1552 struct die_info **, const gdb_byte *,
1553 int *);
93311388 1554
e7c27a73 1555static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1556
15d034d0
TT
1557static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1558 struct obstack *);
71c25dea 1559
15d034d0 1560static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1561
15d034d0 1562static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1563 struct die_info *die,
1564 struct dwarf2_cu *cu);
1565
ca69b9e6
DE
1566static const char *dwarf2_physname (const char *name, struct die_info *die,
1567 struct dwarf2_cu *cu);
1568
e142c38c 1569static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1570 struct dwarf2_cu **);
9219021c 1571
f39c6ffd 1572static const char *dwarf_tag_name (unsigned int);
c906108c 1573
f39c6ffd 1574static const char *dwarf_attr_name (unsigned int);
c906108c 1575
f39c6ffd 1576static const char *dwarf_form_name (unsigned int);
c906108c 1577
a14ed312 1578static char *dwarf_bool_name (unsigned int);
c906108c 1579
f39c6ffd 1580static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1581
f9aca02d 1582static struct die_info *sibling_die (struct die_info *);
c906108c 1583
d97bc12b
DE
1584static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1585
1586static void dump_die_for_error (struct die_info *);
1587
1588static void dump_die_1 (struct ui_file *, int level, int max_level,
1589 struct die_info *);
c906108c 1590
d97bc12b 1591/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1592
51545339 1593static void store_in_ref_table (struct die_info *,
10b3939b 1594 struct dwarf2_cu *);
c906108c 1595
93311388
DE
1596static int is_ref_attr (struct attribute *);
1597
b64f50a1 1598static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1599
43bbcdc2 1600static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1601
348e048f
DE
1602static struct die_info *follow_die_ref_or_sig (struct die_info *,
1603 struct attribute *,
1604 struct dwarf2_cu **);
1605
10b3939b
DJ
1606static struct die_info *follow_die_ref (struct die_info *,
1607 struct attribute *,
f2f0e013 1608 struct dwarf2_cu **);
c906108c 1609
348e048f
DE
1610static struct die_info *follow_die_sig (struct die_info *,
1611 struct attribute *,
1612 struct dwarf2_cu **);
1613
ac9ec31b
DE
1614static struct type *get_signatured_type (struct die_info *, ULONGEST,
1615 struct dwarf2_cu *);
1616
1617static struct type *get_DW_AT_signature_type (struct die_info *,
1618 struct attribute *,
1619 struct dwarf2_cu *);
1620
e5fe5e75 1621static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1622
52dc124a 1623static void read_signatured_type (struct signatured_type *);
348e048f 1624
f4dc4d17 1625static struct type_unit_group *get_type_unit_group
094b34ac 1626 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1627
1628static void build_type_unit_groups (die_reader_func_ftype *, void *);
1629
c906108c
SS
1630/* memory allocation interface */
1631
7b5a2f43 1632static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1633
b60c80d6 1634static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1635
09262596 1636static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1637 const char *, int);
2e276125 1638
8e19ed76
PS
1639static int attr_form_is_block (struct attribute *);
1640
3690dd37
JB
1641static int attr_form_is_section_offset (struct attribute *);
1642
1643static int attr_form_is_constant (struct attribute *);
1644
8cf6f0b1
TT
1645static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1646 struct dwarf2_loclist_baton *baton,
1647 struct attribute *attr);
1648
93e7bd98
DJ
1649static void dwarf2_symbol_mark_computed (struct attribute *attr,
1650 struct symbol *sym,
f1e6e072
TT
1651 struct dwarf2_cu *cu,
1652 int is_block);
4c2df51b 1653
d521ce57
TT
1654static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1655 const gdb_byte *info_ptr,
1656 struct abbrev_info *abbrev);
4bb7a0a7 1657
72bf9492
DJ
1658static void free_stack_comp_unit (void *);
1659
72bf9492
DJ
1660static hashval_t partial_die_hash (const void *item);
1661
1662static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1663
ae038cb0 1664static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1665 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1666
9816fde3 1667static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1668 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1669
1670static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1671 struct die_info *comp_unit_die,
1672 enum language pretend_language);
93311388 1673
68dc6402 1674static void free_heap_comp_unit (void *);
ae038cb0
DJ
1675
1676static void free_cached_comp_units (void *);
1677
1678static void age_cached_comp_units (void);
1679
dee91e82 1680static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1681
f792889a
DJ
1682static struct type *set_die_type (struct die_info *, struct type *,
1683 struct dwarf2_cu *);
1c379e20 1684
ae038cb0
DJ
1685static void create_all_comp_units (struct objfile *);
1686
0e50663e 1687static int create_all_type_units (struct objfile *);
1fd400ff 1688
95554aad
TT
1689static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1690 enum language);
10b3939b 1691
95554aad
TT
1692static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1693 enum language);
10b3939b 1694
f4dc4d17
DE
1695static void process_full_type_unit (struct dwarf2_per_cu_data *,
1696 enum language);
1697
10b3939b
DJ
1698static void dwarf2_add_dependence (struct dwarf2_cu *,
1699 struct dwarf2_per_cu_data *);
1700
ae038cb0
DJ
1701static void dwarf2_mark (struct dwarf2_cu *);
1702
1703static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1704
b64f50a1 1705static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1706 struct dwarf2_per_cu_data *);
673bfd45 1707
f792889a 1708static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1709
9291a0cd
TT
1710static void dwarf2_release_queue (void *dummy);
1711
95554aad
TT
1712static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1713 enum language pretend_language);
1714
1715static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1716 struct dwarf2_per_cu_data *per_cu,
1717 enum language pretend_language);
9291a0cd 1718
a0f42c21 1719static void process_queue (void);
9291a0cd
TT
1720
1721static void find_file_and_directory (struct die_info *die,
1722 struct dwarf2_cu *cu,
15d034d0 1723 const char **name, const char **comp_dir);
9291a0cd
TT
1724
1725static char *file_full_name (int file, struct line_header *lh,
1726 const char *comp_dir);
1727
d521ce57 1728static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1729 (struct comp_unit_head *header,
1730 struct dwarf2_section_info *section,
d521ce57 1731 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1732 int is_debug_types_section);
1733
fd820528 1734static void init_cutu_and_read_dies
f4dc4d17
DE
1735 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1736 int use_existing_cu, int keep,
3019eac3
DE
1737 die_reader_func_ftype *die_reader_func, void *data);
1738
dee91e82
DE
1739static void init_cutu_and_read_dies_simple
1740 (struct dwarf2_per_cu_data *this_cu,
1741 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1742
673bfd45 1743static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1744
3019eac3
DE
1745static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1746
1747static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1748 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1749
1750static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1751 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1752
1753static void free_dwo_file_cleanup (void *);
1754
95554aad
TT
1755static void process_cu_includes (void);
1756
1b80a9fa
JK
1757static void check_producer (struct dwarf2_cu *cu);
1758
9291a0cd
TT
1759#if WORDS_BIGENDIAN
1760
1761/* Convert VALUE between big- and little-endian. */
1762static offset_type
1763byte_swap (offset_type value)
1764{
1765 offset_type result;
1766
1767 result = (value & 0xff) << 24;
1768 result |= (value & 0xff00) << 8;
1769 result |= (value & 0xff0000) >> 8;
1770 result |= (value & 0xff000000) >> 24;
1771 return result;
1772}
1773
1774#define MAYBE_SWAP(V) byte_swap (V)
1775
1776#else
1777#define MAYBE_SWAP(V) (V)
1778#endif /* WORDS_BIGENDIAN */
1779
1780/* The suffix for an index file. */
1781#define INDEX_SUFFIX ".gdb-index"
1782
c906108c 1783/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1784 information and return true if we have enough to do something.
1785 NAMES points to the dwarf2 section names, or is NULL if the standard
1786 ELF names are used. */
c906108c
SS
1787
1788int
251d32d9
TG
1789dwarf2_has_info (struct objfile *objfile,
1790 const struct dwarf2_debug_sections *names)
c906108c 1791{
be391dca
TT
1792 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1793 if (!dwarf2_per_objfile)
1794 {
1795 /* Initialize per-objfile state. */
1796 struct dwarf2_per_objfile *data
1797 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1798
be391dca
TT
1799 memset (data, 0, sizeof (*data));
1800 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1801 dwarf2_per_objfile = data;
6502dd73 1802
251d32d9
TG
1803 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1804 (void *) names);
be391dca
TT
1805 dwarf2_per_objfile->objfile = objfile;
1806 }
1807 return (dwarf2_per_objfile->info.asection != NULL
1808 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1809}
1810
251d32d9
TG
1811/* When loading sections, we look either for uncompressed section or for
1812 compressed section names. */
233a11ab
CS
1813
1814static int
251d32d9
TG
1815section_is_p (const char *section_name,
1816 const struct dwarf2_section_names *names)
233a11ab 1817{
251d32d9
TG
1818 if (names->normal != NULL
1819 && strcmp (section_name, names->normal) == 0)
1820 return 1;
1821 if (names->compressed != NULL
1822 && strcmp (section_name, names->compressed) == 0)
1823 return 1;
1824 return 0;
233a11ab
CS
1825}
1826
c906108c
SS
1827/* This function is mapped across the sections and remembers the
1828 offset and size of each of the debugging sections we are interested
1829 in. */
1830
1831static void
251d32d9 1832dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1833{
251d32d9 1834 const struct dwarf2_debug_sections *names;
dc7650b8 1835 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1836
1837 if (vnames == NULL)
1838 names = &dwarf2_elf_names;
1839 else
1840 names = (const struct dwarf2_debug_sections *) vnames;
1841
dc7650b8
JK
1842 if ((aflag & SEC_HAS_CONTENTS) == 0)
1843 {
1844 }
1845 else if (section_is_p (sectp->name, &names->info))
c906108c 1846 {
dce234bc
PP
1847 dwarf2_per_objfile->info.asection = sectp;
1848 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1849 }
251d32d9 1850 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1851 {
dce234bc
PP
1852 dwarf2_per_objfile->abbrev.asection = sectp;
1853 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1854 }
251d32d9 1855 else if (section_is_p (sectp->name, &names->line))
c906108c 1856 {
dce234bc
PP
1857 dwarf2_per_objfile->line.asection = sectp;
1858 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1859 }
251d32d9 1860 else if (section_is_p (sectp->name, &names->loc))
c906108c 1861 {
dce234bc
PP
1862 dwarf2_per_objfile->loc.asection = sectp;
1863 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1864 }
251d32d9 1865 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1866 {
dce234bc
PP
1867 dwarf2_per_objfile->macinfo.asection = sectp;
1868 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1869 }
cf2c3c16
TT
1870 else if (section_is_p (sectp->name, &names->macro))
1871 {
1872 dwarf2_per_objfile->macro.asection = sectp;
1873 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1874 }
251d32d9 1875 else if (section_is_p (sectp->name, &names->str))
c906108c 1876 {
dce234bc
PP
1877 dwarf2_per_objfile->str.asection = sectp;
1878 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1879 }
3019eac3
DE
1880 else if (section_is_p (sectp->name, &names->addr))
1881 {
1882 dwarf2_per_objfile->addr.asection = sectp;
1883 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1884 }
251d32d9 1885 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1886 {
dce234bc
PP
1887 dwarf2_per_objfile->frame.asection = sectp;
1888 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1889 }
251d32d9 1890 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1891 {
dc7650b8
JK
1892 dwarf2_per_objfile->eh_frame.asection = sectp;
1893 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1894 }
251d32d9 1895 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1896 {
dce234bc
PP
1897 dwarf2_per_objfile->ranges.asection = sectp;
1898 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1899 }
251d32d9 1900 else if (section_is_p (sectp->name, &names->types))
348e048f 1901 {
8b70b953
TT
1902 struct dwarf2_section_info type_section;
1903
1904 memset (&type_section, 0, sizeof (type_section));
1905 type_section.asection = sectp;
1906 type_section.size = bfd_get_section_size (sectp);
1907
1908 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1909 &type_section);
348e048f 1910 }
251d32d9 1911 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1912 {
1913 dwarf2_per_objfile->gdb_index.asection = sectp;
1914 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1915 }
dce234bc 1916
72dca2f5
FR
1917 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1918 && bfd_section_vma (abfd, sectp) == 0)
1919 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1920}
1921
fceca515
DE
1922/* A helper function that decides whether a section is empty,
1923 or not present. */
9e0ac564
TT
1924
1925static int
1926dwarf2_section_empty_p (struct dwarf2_section_info *info)
1927{
1928 return info->asection == NULL || info->size == 0;
1929}
1930
3019eac3
DE
1931/* Read the contents of the section INFO.
1932 OBJFILE is the main object file, but not necessarily the file where
1933 the section comes from. E.g., for DWO files INFO->asection->owner
1934 is the bfd of the DWO file.
dce234bc 1935 If the section is compressed, uncompress it before returning. */
c906108c 1936
dce234bc
PP
1937static void
1938dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1939{
dce234bc 1940 asection *sectp = info->asection;
3019eac3 1941 bfd *abfd;
dce234bc
PP
1942 gdb_byte *buf, *retbuf;
1943 unsigned char header[4];
c906108c 1944
be391dca
TT
1945 if (info->readin)
1946 return;
dce234bc 1947 info->buffer = NULL;
be391dca 1948 info->readin = 1;
188dd5d6 1949
9e0ac564 1950 if (dwarf2_section_empty_p (info))
dce234bc 1951 return;
c906108c 1952
3019eac3
DE
1953 abfd = sectp->owner;
1954
4bf44c1c
TT
1955 /* If the section has relocations, we must read it ourselves.
1956 Otherwise we attach it to the BFD. */
1957 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1958 {
d521ce57 1959 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1960 return;
dce234bc 1961 }
dce234bc 1962
4bf44c1c
TT
1963 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1964 info->buffer = buf;
dce234bc
PP
1965
1966 /* When debugging .o files, we may need to apply relocations; see
1967 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1968 We never compress sections in .o files, so we only need to
1969 try this when the section is not compressed. */
ac8035ab 1970 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1971 if (retbuf != NULL)
1972 {
1973 info->buffer = retbuf;
1974 return;
1975 }
1976
1977 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1978 || bfd_bread (buf, info->size, abfd) != info->size)
1979 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1980 bfd_get_filename (abfd));
1981}
1982
9e0ac564
TT
1983/* A helper function that returns the size of a section in a safe way.
1984 If you are positive that the section has been read before using the
1985 size, then it is safe to refer to the dwarf2_section_info object's
1986 "size" field directly. In other cases, you must call this
1987 function, because for compressed sections the size field is not set
1988 correctly until the section has been read. */
1989
1990static bfd_size_type
1991dwarf2_section_size (struct objfile *objfile,
1992 struct dwarf2_section_info *info)
1993{
1994 if (!info->readin)
1995 dwarf2_read_section (objfile, info);
1996 return info->size;
1997}
1998
dce234bc 1999/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2000 SECTION_NAME. */
af34e669 2001
dce234bc 2002void
3017a003
TG
2003dwarf2_get_section_info (struct objfile *objfile,
2004 enum dwarf2_section_enum sect,
d521ce57 2005 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2006 bfd_size_type *sizep)
2007{
2008 struct dwarf2_per_objfile *data
2009 = objfile_data (objfile, dwarf2_objfile_data_key);
2010 struct dwarf2_section_info *info;
a3b2a86b
TT
2011
2012 /* We may see an objfile without any DWARF, in which case we just
2013 return nothing. */
2014 if (data == NULL)
2015 {
2016 *sectp = NULL;
2017 *bufp = NULL;
2018 *sizep = 0;
2019 return;
2020 }
3017a003
TG
2021 switch (sect)
2022 {
2023 case DWARF2_DEBUG_FRAME:
2024 info = &data->frame;
2025 break;
2026 case DWARF2_EH_FRAME:
2027 info = &data->eh_frame;
2028 break;
2029 default:
2030 gdb_assert_not_reached ("unexpected section");
2031 }
dce234bc 2032
9e0ac564 2033 dwarf2_read_section (objfile, info);
dce234bc
PP
2034
2035 *sectp = info->asection;
2036 *bufp = info->buffer;
2037 *sizep = info->size;
2038}
2039
36586728
TT
2040/* A helper function to find the sections for a .dwz file. */
2041
2042static void
2043locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2044{
2045 struct dwz_file *dwz_file = arg;
2046
2047 /* Note that we only support the standard ELF names, because .dwz
2048 is ELF-only (at the time of writing). */
2049 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2050 {
2051 dwz_file->abbrev.asection = sectp;
2052 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2053 }
2054 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2055 {
2056 dwz_file->info.asection = sectp;
2057 dwz_file->info.size = bfd_get_section_size (sectp);
2058 }
2059 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2060 {
2061 dwz_file->str.asection = sectp;
2062 dwz_file->str.size = bfd_get_section_size (sectp);
2063 }
2064 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2065 {
2066 dwz_file->line.asection = sectp;
2067 dwz_file->line.size = bfd_get_section_size (sectp);
2068 }
2069 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2070 {
2071 dwz_file->macro.asection = sectp;
2072 dwz_file->macro.size = bfd_get_section_size (sectp);
2073 }
2ec9a5e0
TT
2074 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2075 {
2076 dwz_file->gdb_index.asection = sectp;
2077 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2078 }
36586728
TT
2079}
2080
2081/* Open the separate '.dwz' debug file, if needed. Error if the file
2082 cannot be found. */
2083
2084static struct dwz_file *
2085dwarf2_get_dwz_file (void)
2086{
2087 bfd *abfd, *dwz_bfd;
2088 asection *section;
2089 gdb_byte *data;
2090 struct cleanup *cleanup;
2091 const char *filename;
2092 struct dwz_file *result;
2093
2094 if (dwarf2_per_objfile->dwz_file != NULL)
2095 return dwarf2_per_objfile->dwz_file;
2096
2097 abfd = dwarf2_per_objfile->objfile->obfd;
2098 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2099 if (section == NULL)
2100 error (_("could not find '.gnu_debugaltlink' section"));
2101 if (!bfd_malloc_and_get_section (abfd, section, &data))
2102 error (_("could not read '.gnu_debugaltlink' section: %s"),
2103 bfd_errmsg (bfd_get_error ()));
2104 cleanup = make_cleanup (xfree, data);
2105
2106 filename = data;
2107 if (!IS_ABSOLUTE_PATH (filename))
2108 {
2109 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2110 char *rel;
2111
2112 make_cleanup (xfree, abs);
2113 abs = ldirname (abs);
2114 make_cleanup (xfree, abs);
2115
2116 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2117 make_cleanup (xfree, rel);
2118 filename = rel;
2119 }
2120
2121 /* The format is just a NUL-terminated file name, followed by the
2122 build-id. For now, though, we ignore the build-id. */
2123 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2124 if (dwz_bfd == NULL)
2125 error (_("could not read '%s': %s"), filename,
2126 bfd_errmsg (bfd_get_error ()));
2127
2128 if (!bfd_check_format (dwz_bfd, bfd_object))
2129 {
2130 gdb_bfd_unref (dwz_bfd);
2131 error (_("file '%s' was not usable: %s"), filename,
2132 bfd_errmsg (bfd_get_error ()));
2133 }
2134
2135 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2136 struct dwz_file);
2137 result->dwz_bfd = dwz_bfd;
2138
2139 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2140
2141 do_cleanups (cleanup);
2142
8d2cc612 2143 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2144 return result;
2145}
9291a0cd 2146\f
7b9f3c50
DE
2147/* DWARF quick_symbols_functions support. */
2148
2149/* TUs can share .debug_line entries, and there can be a lot more TUs than
2150 unique line tables, so we maintain a separate table of all .debug_line
2151 derived entries to support the sharing.
2152 All the quick functions need is the list of file names. We discard the
2153 line_header when we're done and don't need to record it here. */
2154struct quick_file_names
2155{
094b34ac
DE
2156 /* The data used to construct the hash key. */
2157 struct stmt_list_hash hash;
7b9f3c50
DE
2158
2159 /* The number of entries in file_names, real_names. */
2160 unsigned int num_file_names;
2161
2162 /* The file names from the line table, after being run through
2163 file_full_name. */
2164 const char **file_names;
2165
2166 /* The file names from the line table after being run through
2167 gdb_realpath. These are computed lazily. */
2168 const char **real_names;
2169};
2170
2171/* When using the index (and thus not using psymtabs), each CU has an
2172 object of this type. This is used to hold information needed by
2173 the various "quick" methods. */
2174struct dwarf2_per_cu_quick_data
2175{
2176 /* The file table. This can be NULL if there was no file table
2177 or it's currently not read in.
2178 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2179 struct quick_file_names *file_names;
2180
2181 /* The corresponding symbol table. This is NULL if symbols for this
2182 CU have not yet been read. */
2183 struct symtab *symtab;
2184
2185 /* A temporary mark bit used when iterating over all CUs in
2186 expand_symtabs_matching. */
2187 unsigned int mark : 1;
2188
2189 /* True if we've tried to read the file table and found there isn't one.
2190 There will be no point in trying to read it again next time. */
2191 unsigned int no_file_data : 1;
2192};
2193
094b34ac
DE
2194/* Utility hash function for a stmt_list_hash. */
2195
2196static hashval_t
2197hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2198{
2199 hashval_t v = 0;
2200
2201 if (stmt_list_hash->dwo_unit != NULL)
2202 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2203 v += stmt_list_hash->line_offset.sect_off;
2204 return v;
2205}
2206
2207/* Utility equality function for a stmt_list_hash. */
2208
2209static int
2210eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2211 const struct stmt_list_hash *rhs)
2212{
2213 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2214 return 0;
2215 if (lhs->dwo_unit != NULL
2216 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2217 return 0;
2218
2219 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2220}
2221
7b9f3c50
DE
2222/* Hash function for a quick_file_names. */
2223
2224static hashval_t
2225hash_file_name_entry (const void *e)
2226{
2227 const struct quick_file_names *file_data = e;
2228
094b34ac 2229 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2230}
2231
2232/* Equality function for a quick_file_names. */
2233
2234static int
2235eq_file_name_entry (const void *a, const void *b)
2236{
2237 const struct quick_file_names *ea = a;
2238 const struct quick_file_names *eb = b;
2239
094b34ac 2240 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2241}
2242
2243/* Delete function for a quick_file_names. */
2244
2245static void
2246delete_file_name_entry (void *e)
2247{
2248 struct quick_file_names *file_data = e;
2249 int i;
2250
2251 for (i = 0; i < file_data->num_file_names; ++i)
2252 {
2253 xfree ((void*) file_data->file_names[i]);
2254 if (file_data->real_names)
2255 xfree ((void*) file_data->real_names[i]);
2256 }
2257
2258 /* The space for the struct itself lives on objfile_obstack,
2259 so we don't free it here. */
2260}
2261
2262/* Create a quick_file_names hash table. */
2263
2264static htab_t
2265create_quick_file_names_table (unsigned int nr_initial_entries)
2266{
2267 return htab_create_alloc (nr_initial_entries,
2268 hash_file_name_entry, eq_file_name_entry,
2269 delete_file_name_entry, xcalloc, xfree);
2270}
9291a0cd 2271
918dd910
JK
2272/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2273 have to be created afterwards. You should call age_cached_comp_units after
2274 processing PER_CU->CU. dw2_setup must have been already called. */
2275
2276static void
2277load_cu (struct dwarf2_per_cu_data *per_cu)
2278{
3019eac3 2279 if (per_cu->is_debug_types)
e5fe5e75 2280 load_full_type_unit (per_cu);
918dd910 2281 else
95554aad 2282 load_full_comp_unit (per_cu, language_minimal);
918dd910 2283
918dd910 2284 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2285
2286 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2287}
2288
a0f42c21 2289/* Read in the symbols for PER_CU. */
2fdf6df6 2290
9291a0cd 2291static void
a0f42c21 2292dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2293{
2294 struct cleanup *back_to;
2295
f4dc4d17
DE
2296 /* Skip type_unit_groups, reading the type units they contain
2297 is handled elsewhere. */
2298 if (IS_TYPE_UNIT_GROUP (per_cu))
2299 return;
2300
9291a0cd
TT
2301 back_to = make_cleanup (dwarf2_release_queue, NULL);
2302
95554aad
TT
2303 if (dwarf2_per_objfile->using_index
2304 ? per_cu->v.quick->symtab == NULL
2305 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2306 {
2307 queue_comp_unit (per_cu, language_minimal);
2308 load_cu (per_cu);
2309 }
9291a0cd 2310
a0f42c21 2311 process_queue ();
9291a0cd
TT
2312
2313 /* Age the cache, releasing compilation units that have not
2314 been used recently. */
2315 age_cached_comp_units ();
2316
2317 do_cleanups (back_to);
2318}
2319
2320/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2321 the objfile from which this CU came. Returns the resulting symbol
2322 table. */
2fdf6df6 2323
9291a0cd 2324static struct symtab *
a0f42c21 2325dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2326{
95554aad 2327 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2328 if (!per_cu->v.quick->symtab)
2329 {
2330 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2331 increment_reading_symtab ();
a0f42c21 2332 dw2_do_instantiate_symtab (per_cu);
95554aad 2333 process_cu_includes ();
9291a0cd
TT
2334 do_cleanups (back_to);
2335 }
2336 return per_cu->v.quick->symtab;
2337}
2338
f4dc4d17
DE
2339/* Return the CU given its index.
2340
2341 This is intended for loops like:
2342
2343 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2344 + dwarf2_per_objfile->n_type_units); ++i)
2345 {
2346 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2347
2348 ...;
2349 }
2350*/
2fdf6df6 2351
1fd400ff
TT
2352static struct dwarf2_per_cu_data *
2353dw2_get_cu (int index)
2354{
2355 if (index >= dwarf2_per_objfile->n_comp_units)
2356 {
f4dc4d17 2357 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2358 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2359 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2360 }
2361
2362 return dwarf2_per_objfile->all_comp_units[index];
2363}
2364
2365/* Return the primary CU given its index.
2366 The difference between this function and dw2_get_cu is in the handling
2367 of type units (TUs). Here we return the type_unit_group object.
2368
2369 This is intended for loops like:
2370
2371 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2372 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2373 {
2374 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2375
2376 ...;
2377 }
2378*/
2379
2380static struct dwarf2_per_cu_data *
2381dw2_get_primary_cu (int index)
2382{
2383 if (index >= dwarf2_per_objfile->n_comp_units)
2384 {
1fd400ff 2385 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2386 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2387 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2388 }
f4dc4d17 2389
1fd400ff
TT
2390 return dwarf2_per_objfile->all_comp_units[index];
2391}
2392
2ec9a5e0
TT
2393/* A helper for create_cus_from_index that handles a given list of
2394 CUs. */
2fdf6df6 2395
74a0d9f6 2396static void
2ec9a5e0
TT
2397create_cus_from_index_list (struct objfile *objfile,
2398 const gdb_byte *cu_list, offset_type n_elements,
2399 struct dwarf2_section_info *section,
2400 int is_dwz,
2401 int base_offset)
9291a0cd
TT
2402{
2403 offset_type i;
9291a0cd 2404
2ec9a5e0 2405 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2406 {
2407 struct dwarf2_per_cu_data *the_cu;
2408 ULONGEST offset, length;
2409
74a0d9f6
JK
2410 gdb_static_assert (sizeof (ULONGEST) >= 8);
2411 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2412 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2413 cu_list += 2 * 8;
2414
2415 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2416 struct dwarf2_per_cu_data);
b64f50a1 2417 the_cu->offset.sect_off = offset;
9291a0cd
TT
2418 the_cu->length = length;
2419 the_cu->objfile = objfile;
8a0459fd 2420 the_cu->section = section;
9291a0cd
TT
2421 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2422 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2423 the_cu->is_dwz = is_dwz;
2424 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2425 }
9291a0cd
TT
2426}
2427
2ec9a5e0 2428/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2429 the CU objects for this objfile. */
2ec9a5e0 2430
74a0d9f6 2431static void
2ec9a5e0
TT
2432create_cus_from_index (struct objfile *objfile,
2433 const gdb_byte *cu_list, offset_type cu_list_elements,
2434 const gdb_byte *dwz_list, offset_type dwz_elements)
2435{
2436 struct dwz_file *dwz;
2437
2438 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2439 dwarf2_per_objfile->all_comp_units
2440 = obstack_alloc (&objfile->objfile_obstack,
2441 dwarf2_per_objfile->n_comp_units
2442 * sizeof (struct dwarf2_per_cu_data *));
2443
74a0d9f6
JK
2444 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2445 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2446
2447 if (dwz_elements == 0)
74a0d9f6 2448 return;
2ec9a5e0
TT
2449
2450 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2451 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2452 cu_list_elements / 2);
2ec9a5e0
TT
2453}
2454
1fd400ff 2455/* Create the signatured type hash table from the index. */
673bfd45 2456
74a0d9f6 2457static void
673bfd45 2458create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2459 struct dwarf2_section_info *section,
673bfd45
DE
2460 const gdb_byte *bytes,
2461 offset_type elements)
1fd400ff
TT
2462{
2463 offset_type i;
673bfd45 2464 htab_t sig_types_hash;
1fd400ff 2465
d467dd73
DE
2466 dwarf2_per_objfile->n_type_units = elements / 3;
2467 dwarf2_per_objfile->all_type_units
1fd400ff 2468 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2469 dwarf2_per_objfile->n_type_units
b4dd5633 2470 * sizeof (struct signatured_type *));
1fd400ff 2471
673bfd45 2472 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2473
2474 for (i = 0; i < elements; i += 3)
2475 {
52dc124a
DE
2476 struct signatured_type *sig_type;
2477 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2478 void **slot;
2479
74a0d9f6
JK
2480 gdb_static_assert (sizeof (ULONGEST) >= 8);
2481 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2482 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2483 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2484 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2485 bytes += 3 * 8;
2486
52dc124a 2487 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2488 struct signatured_type);
52dc124a 2489 sig_type->signature = signature;
3019eac3
DE
2490 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2491 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2492 sig_type->per_cu.section = section;
52dc124a
DE
2493 sig_type->per_cu.offset.sect_off = offset;
2494 sig_type->per_cu.objfile = objfile;
2495 sig_type->per_cu.v.quick
1fd400ff
TT
2496 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2497 struct dwarf2_per_cu_quick_data);
2498
52dc124a
DE
2499 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2500 *slot = sig_type;
1fd400ff 2501
b4dd5633 2502 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2503 }
2504
673bfd45 2505 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2506}
2507
9291a0cd
TT
2508/* Read the address map data from the mapped index, and use it to
2509 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2510
9291a0cd
TT
2511static void
2512create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2513{
2514 const gdb_byte *iter, *end;
2515 struct obstack temp_obstack;
2516 struct addrmap *mutable_map;
2517 struct cleanup *cleanup;
2518 CORE_ADDR baseaddr;
2519
2520 obstack_init (&temp_obstack);
2521 cleanup = make_cleanup_obstack_free (&temp_obstack);
2522 mutable_map = addrmap_create_mutable (&temp_obstack);
2523
2524 iter = index->address_table;
2525 end = iter + index->address_table_size;
2526
2527 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2528
2529 while (iter < end)
2530 {
2531 ULONGEST hi, lo, cu_index;
2532 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2533 iter += 8;
2534 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2535 iter += 8;
2536 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2537 iter += 4;
f652bce2
DE
2538
2539 if (cu_index < dwarf2_per_objfile->n_comp_units)
2540 {
2541 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2542 dw2_get_cu (cu_index));
2543 }
2544 else
2545 {
2546 complaint (&symfile_complaints,
2547 _(".gdb_index address table has invalid CU number %u"),
2548 (unsigned) cu_index);
2549 }
9291a0cd
TT
2550 }
2551
2552 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2553 &objfile->objfile_obstack);
2554 do_cleanups (cleanup);
2555}
2556
59d7bcaf
JK
2557/* The hash function for strings in the mapped index. This is the same as
2558 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2559 implementation. This is necessary because the hash function is tied to the
2560 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2561 SYMBOL_HASH_NEXT.
2562
2563 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2564
9291a0cd 2565static hashval_t
559a7a62 2566mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2567{
2568 const unsigned char *str = (const unsigned char *) p;
2569 hashval_t r = 0;
2570 unsigned char c;
2571
2572 while ((c = *str++) != 0)
559a7a62
JK
2573 {
2574 if (index_version >= 5)
2575 c = tolower (c);
2576 r = r * 67 + c - 113;
2577 }
9291a0cd
TT
2578
2579 return r;
2580}
2581
2582/* Find a slot in the mapped index INDEX for the object named NAME.
2583 If NAME is found, set *VEC_OUT to point to the CU vector in the
2584 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2585
9291a0cd
TT
2586static int
2587find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2588 offset_type **vec_out)
2589{
0cf03b49
JK
2590 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2591 offset_type hash;
9291a0cd 2592 offset_type slot, step;
559a7a62 2593 int (*cmp) (const char *, const char *);
9291a0cd 2594
0cf03b49
JK
2595 if (current_language->la_language == language_cplus
2596 || current_language->la_language == language_java
2597 || current_language->la_language == language_fortran)
2598 {
2599 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2600 not contain any. */
2601 const char *paren = strchr (name, '(');
2602
2603 if (paren)
2604 {
2605 char *dup;
2606
2607 dup = xmalloc (paren - name + 1);
2608 memcpy (dup, name, paren - name);
2609 dup[paren - name] = 0;
2610
2611 make_cleanup (xfree, dup);
2612 name = dup;
2613 }
2614 }
2615
559a7a62 2616 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2617 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2618 simulate our NAME being searched is also lowercased. */
2619 hash = mapped_index_string_hash ((index->version == 4
2620 && case_sensitivity == case_sensitive_off
2621 ? 5 : index->version),
2622 name);
2623
3876f04e
DE
2624 slot = hash & (index->symbol_table_slots - 1);
2625 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2626 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2627
2628 for (;;)
2629 {
2630 /* Convert a slot number to an offset into the table. */
2631 offset_type i = 2 * slot;
2632 const char *str;
3876f04e 2633 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2634 {
2635 do_cleanups (back_to);
2636 return 0;
2637 }
9291a0cd 2638
3876f04e 2639 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2640 if (!cmp (name, str))
9291a0cd
TT
2641 {
2642 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2643 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2644 do_cleanups (back_to);
9291a0cd
TT
2645 return 1;
2646 }
2647
3876f04e 2648 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2649 }
2650}
2651
2ec9a5e0
TT
2652/* A helper function that reads the .gdb_index from SECTION and fills
2653 in MAP. FILENAME is the name of the file containing the section;
2654 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2655 ok to use deprecated sections.
2656
2657 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2658 out parameters that are filled in with information about the CU and
2659 TU lists in the section.
2660
2661 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2662
9291a0cd 2663static int
2ec9a5e0
TT
2664read_index_from_section (struct objfile *objfile,
2665 const char *filename,
2666 int deprecated_ok,
2667 struct dwarf2_section_info *section,
2668 struct mapped_index *map,
2669 const gdb_byte **cu_list,
2670 offset_type *cu_list_elements,
2671 const gdb_byte **types_list,
2672 offset_type *types_list_elements)
9291a0cd 2673{
d521ce57 2674 const char *addr;
2ec9a5e0 2675 offset_type version;
b3b272e1 2676 offset_type *metadata;
1fd400ff 2677 int i;
9291a0cd 2678
2ec9a5e0 2679 if (dwarf2_section_empty_p (section))
9291a0cd 2680 return 0;
82430852
JK
2681
2682 /* Older elfutils strip versions could keep the section in the main
2683 executable while splitting it for the separate debug info file. */
2ec9a5e0 2684 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2685 return 0;
2686
2ec9a5e0 2687 dwarf2_read_section (objfile, section);
9291a0cd 2688
2ec9a5e0 2689 addr = section->buffer;
9291a0cd 2690 /* Version check. */
1fd400ff 2691 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2692 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2693 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2694 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2695 indices. */
831adc1f 2696 if (version < 4)
481860b3
GB
2697 {
2698 static int warning_printed = 0;
2699 if (!warning_printed)
2700 {
2701 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2702 filename);
481860b3
GB
2703 warning_printed = 1;
2704 }
2705 return 0;
2706 }
2707 /* Index version 4 uses a different hash function than index version
2708 5 and later.
2709
2710 Versions earlier than 6 did not emit psymbols for inlined
2711 functions. Using these files will cause GDB not to be able to
2712 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2713 indices unless the user has done
2714 "set use-deprecated-index-sections on". */
2ec9a5e0 2715 if (version < 6 && !deprecated_ok)
481860b3
GB
2716 {
2717 static int warning_printed = 0;
2718 if (!warning_printed)
2719 {
e615022a
DE
2720 warning (_("\
2721Skipping deprecated .gdb_index section in %s.\n\
2722Do \"set use-deprecated-index-sections on\" before the file is read\n\
2723to use the section anyway."),
2ec9a5e0 2724 filename);
481860b3
GB
2725 warning_printed = 1;
2726 }
2727 return 0;
2728 }
796a7ff8
DE
2729 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2730 of the TU (for symbols coming from TUs). It's just a performance bug, and
2731 we can't distinguish gdb-generated indices from gold-generated ones, so
2732 nothing to do here. */
2733
481860b3 2734 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2735 longer backward compatible. */
796a7ff8 2736 if (version > 8)
594e8718 2737 return 0;
9291a0cd 2738
559a7a62 2739 map->version = version;
2ec9a5e0 2740 map->total_size = section->size;
9291a0cd
TT
2741
2742 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2743
2744 i = 0;
2ec9a5e0
TT
2745 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2746 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2747 / 8);
1fd400ff
TT
2748 ++i;
2749
2ec9a5e0
TT
2750 *types_list = addr + MAYBE_SWAP (metadata[i]);
2751 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2752 - MAYBE_SWAP (metadata[i]))
2753 / 8);
987d643c 2754 ++i;
1fd400ff
TT
2755
2756 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2757 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2758 - MAYBE_SWAP (metadata[i]));
2759 ++i;
2760
3876f04e
DE
2761 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2762 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2763 - MAYBE_SWAP (metadata[i]))
2764 / (2 * sizeof (offset_type)));
1fd400ff 2765 ++i;
9291a0cd 2766
1fd400ff
TT
2767 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2768
2ec9a5e0
TT
2769 return 1;
2770}
2771
2772
2773/* Read the index file. If everything went ok, initialize the "quick"
2774 elements of all the CUs and return 1. Otherwise, return 0. */
2775
2776static int
2777dwarf2_read_index (struct objfile *objfile)
2778{
2779 struct mapped_index local_map, *map;
2780 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2781 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2782
2783 if (!read_index_from_section (objfile, objfile->name,
2784 use_deprecated_index_sections,
2785 &dwarf2_per_objfile->gdb_index, &local_map,
2786 &cu_list, &cu_list_elements,
2787 &types_list, &types_list_elements))
2788 return 0;
2789
0fefef59 2790 /* Don't use the index if it's empty. */
2ec9a5e0 2791 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2792 return 0;
2793
2ec9a5e0
TT
2794 /* If there is a .dwz file, read it so we can get its CU list as
2795 well. */
2796 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2797 {
2798 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2799 struct mapped_index dwz_map;
2800 const gdb_byte *dwz_types_ignore;
2801 offset_type dwz_types_elements_ignore;
2802
2803 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2804 1,
2805 &dwz->gdb_index, &dwz_map,
2806 &dwz_list, &dwz_list_elements,
2807 &dwz_types_ignore,
2808 &dwz_types_elements_ignore))
2809 {
2810 warning (_("could not read '.gdb_index' section from %s; skipping"),
2811 bfd_get_filename (dwz->dwz_bfd));
2812 return 0;
2813 }
2814 }
2815
74a0d9f6
JK
2816 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2817 dwz_list_elements);
1fd400ff 2818
8b70b953
TT
2819 if (types_list_elements)
2820 {
2821 struct dwarf2_section_info *section;
2822
2823 /* We can only handle a single .debug_types when we have an
2824 index. */
2825 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2826 return 0;
2827
2828 section = VEC_index (dwarf2_section_info_def,
2829 dwarf2_per_objfile->types, 0);
2830
74a0d9f6
JK
2831 create_signatured_type_table_from_index (objfile, section, types_list,
2832 types_list_elements);
8b70b953 2833 }
9291a0cd 2834
2ec9a5e0
TT
2835 create_addrmap_from_index (objfile, &local_map);
2836
2837 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2838 *map = local_map;
9291a0cd
TT
2839
2840 dwarf2_per_objfile->index_table = map;
2841 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2842 dwarf2_per_objfile->quick_file_names_table =
2843 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2844
2845 return 1;
2846}
2847
2848/* A helper for the "quick" functions which sets the global
2849 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2850
9291a0cd
TT
2851static void
2852dw2_setup (struct objfile *objfile)
2853{
2854 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2855 gdb_assert (dwarf2_per_objfile);
2856}
2857
dee91e82 2858/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2859
dee91e82
DE
2860static void
2861dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2862 const gdb_byte *info_ptr,
dee91e82
DE
2863 struct die_info *comp_unit_die,
2864 int has_children,
2865 void *data)
9291a0cd 2866{
dee91e82
DE
2867 struct dwarf2_cu *cu = reader->cu;
2868 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2869 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2870 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2871 struct line_header *lh;
9291a0cd 2872 struct attribute *attr;
dee91e82 2873 int i;
15d034d0 2874 const char *name, *comp_dir;
7b9f3c50
DE
2875 void **slot;
2876 struct quick_file_names *qfn;
2877 unsigned int line_offset;
9291a0cd 2878
0186c6a7
DE
2879 gdb_assert (! this_cu->is_debug_types);
2880
07261596
TT
2881 /* Our callers never want to match partial units -- instead they
2882 will match the enclosing full CU. */
2883 if (comp_unit_die->tag == DW_TAG_partial_unit)
2884 {
2885 this_cu->v.quick->no_file_data = 1;
2886 return;
2887 }
2888
0186c6a7 2889 lh_cu = this_cu;
7b9f3c50
DE
2890 lh = NULL;
2891 slot = NULL;
2892 line_offset = 0;
dee91e82
DE
2893
2894 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2895 if (attr)
2896 {
7b9f3c50
DE
2897 struct quick_file_names find_entry;
2898
2899 line_offset = DW_UNSND (attr);
2900
2901 /* We may have already read in this line header (TU line header sharing).
2902 If we have we're done. */
094b34ac
DE
2903 find_entry.hash.dwo_unit = cu->dwo_unit;
2904 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2905 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2906 &find_entry, INSERT);
2907 if (*slot != NULL)
2908 {
094b34ac 2909 lh_cu->v.quick->file_names = *slot;
dee91e82 2910 return;
7b9f3c50
DE
2911 }
2912
3019eac3 2913 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2914 }
2915 if (lh == NULL)
2916 {
094b34ac 2917 lh_cu->v.quick->no_file_data = 1;
dee91e82 2918 return;
9291a0cd
TT
2919 }
2920
7b9f3c50 2921 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2922 qfn->hash.dwo_unit = cu->dwo_unit;
2923 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2924 gdb_assert (slot != NULL);
2925 *slot = qfn;
9291a0cd 2926
dee91e82 2927 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2928
7b9f3c50
DE
2929 qfn->num_file_names = lh->num_file_names;
2930 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2931 lh->num_file_names * sizeof (char *));
9291a0cd 2932 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2933 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2934 qfn->real_names = NULL;
9291a0cd 2935
7b9f3c50 2936 free_line_header (lh);
7b9f3c50 2937
094b34ac 2938 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2939}
2940
2941/* A helper for the "quick" functions which attempts to read the line
2942 table for THIS_CU. */
2943
2944static struct quick_file_names *
e4a48d9d 2945dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2946{
0186c6a7
DE
2947 /* This should never be called for TUs. */
2948 gdb_assert (! this_cu->is_debug_types);
2949 /* Nor type unit groups. */
2950 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2951
dee91e82
DE
2952 if (this_cu->v.quick->file_names != NULL)
2953 return this_cu->v.quick->file_names;
2954 /* If we know there is no line data, no point in looking again. */
2955 if (this_cu->v.quick->no_file_data)
2956 return NULL;
2957
0186c6a7 2958 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2959
2960 if (this_cu->v.quick->no_file_data)
2961 return NULL;
2962 return this_cu->v.quick->file_names;
9291a0cd
TT
2963}
2964
2965/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2966 real path for a given file name from the line table. */
2fdf6df6 2967
9291a0cd 2968static const char *
7b9f3c50
DE
2969dw2_get_real_path (struct objfile *objfile,
2970 struct quick_file_names *qfn, int index)
9291a0cd 2971{
7b9f3c50
DE
2972 if (qfn->real_names == NULL)
2973 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2974 qfn->num_file_names, sizeof (char *));
9291a0cd 2975
7b9f3c50
DE
2976 if (qfn->real_names[index] == NULL)
2977 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2978
7b9f3c50 2979 return qfn->real_names[index];
9291a0cd
TT
2980}
2981
2982static struct symtab *
2983dw2_find_last_source_symtab (struct objfile *objfile)
2984{
2985 int index;
ae2de4f8 2986
9291a0cd
TT
2987 dw2_setup (objfile);
2988 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2989 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2990}
2991
7b9f3c50
DE
2992/* Traversal function for dw2_forget_cached_source_info. */
2993
2994static int
2995dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2996{
7b9f3c50 2997 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2998
7b9f3c50 2999 if (file_data->real_names)
9291a0cd 3000 {
7b9f3c50 3001 int i;
9291a0cd 3002
7b9f3c50 3003 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3004 {
7b9f3c50
DE
3005 xfree ((void*) file_data->real_names[i]);
3006 file_data->real_names[i] = NULL;
9291a0cd
TT
3007 }
3008 }
7b9f3c50
DE
3009
3010 return 1;
3011}
3012
3013static void
3014dw2_forget_cached_source_info (struct objfile *objfile)
3015{
3016 dw2_setup (objfile);
3017
3018 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3019 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3020}
3021
f8eba3c6
TT
3022/* Helper function for dw2_map_symtabs_matching_filename that expands
3023 the symtabs and calls the iterator. */
3024
3025static int
3026dw2_map_expand_apply (struct objfile *objfile,
3027 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3028 const char *name, const char *real_path,
f8eba3c6
TT
3029 int (*callback) (struct symtab *, void *),
3030 void *data)
3031{
3032 struct symtab *last_made = objfile->symtabs;
3033
3034 /* Don't visit already-expanded CUs. */
3035 if (per_cu->v.quick->symtab)
3036 return 0;
3037
3038 /* This may expand more than one symtab, and we want to iterate over
3039 all of them. */
a0f42c21 3040 dw2_instantiate_symtab (per_cu);
f8eba3c6 3041
f5b95b50 3042 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3043 objfile->symtabs, last_made);
3044}
3045
3046/* Implementation of the map_symtabs_matching_filename method. */
3047
9291a0cd 3048static int
f8eba3c6 3049dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3050 const char *real_path,
f8eba3c6
TT
3051 int (*callback) (struct symtab *, void *),
3052 void *data)
9291a0cd
TT
3053{
3054 int i;
c011a4f4 3055 const char *name_basename = lbasename (name);
9291a0cd
TT
3056
3057 dw2_setup (objfile);
ae2de4f8 3058
848e3e78
DE
3059 /* The rule is CUs specify all the files, including those used by
3060 any TU, so there's no need to scan TUs here. */
f4dc4d17 3061
848e3e78 3062 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3063 {
3064 int j;
f4dc4d17 3065 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3066 struct quick_file_names *file_data;
9291a0cd 3067
3d7bb9d9 3068 /* We only need to look at symtabs not already expanded. */
e254ef6a 3069 if (per_cu->v.quick->symtab)
9291a0cd
TT
3070 continue;
3071
e4a48d9d 3072 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3073 if (file_data == NULL)
9291a0cd
TT
3074 continue;
3075
7b9f3c50 3076 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3077 {
7b9f3c50 3078 const char *this_name = file_data->file_names[j];
da235a7c 3079 const char *this_real_name;
9291a0cd 3080
af529f8f 3081 if (compare_filenames_for_search (this_name, name))
9291a0cd 3082 {
f5b95b50 3083 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3084 callback, data))
3085 return 1;
288e77a7 3086 continue;
4aac40c8 3087 }
9291a0cd 3088
c011a4f4
DE
3089 /* Before we invoke realpath, which can get expensive when many
3090 files are involved, do a quick comparison of the basenames. */
3091 if (! basenames_may_differ
3092 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3093 continue;
3094
da235a7c
JK
3095 this_real_name = dw2_get_real_path (objfile, file_data, j);
3096 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3097 {
da235a7c
JK
3098 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3099 callback, data))
3100 return 1;
288e77a7 3101 continue;
da235a7c 3102 }
9291a0cd 3103
da235a7c
JK
3104 if (real_path != NULL)
3105 {
af529f8f
JK
3106 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3107 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3108 if (this_real_name != NULL
af529f8f 3109 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3110 {
f5b95b50 3111 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3112 callback, data))
3113 return 1;
288e77a7 3114 continue;
9291a0cd
TT
3115 }
3116 }
3117 }
3118 }
3119
9291a0cd
TT
3120 return 0;
3121}
3122
da51c347
DE
3123/* Struct used to manage iterating over all CUs looking for a symbol. */
3124
3125struct dw2_symtab_iterator
9291a0cd 3126{
da51c347
DE
3127 /* The internalized form of .gdb_index. */
3128 struct mapped_index *index;
3129 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3130 int want_specific_block;
3131 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3132 Unused if !WANT_SPECIFIC_BLOCK. */
3133 int block_index;
3134 /* The kind of symbol we're looking for. */
3135 domain_enum domain;
3136 /* The list of CUs from the index entry of the symbol,
3137 or NULL if not found. */
3138 offset_type *vec;
3139 /* The next element in VEC to look at. */
3140 int next;
3141 /* The number of elements in VEC, or zero if there is no match. */
3142 int length;
3143};
9291a0cd 3144
da51c347
DE
3145/* Initialize the index symtab iterator ITER.
3146 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3147 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3148
9291a0cd 3149static void
da51c347
DE
3150dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3151 struct mapped_index *index,
3152 int want_specific_block,
3153 int block_index,
3154 domain_enum domain,
3155 const char *name)
3156{
3157 iter->index = index;
3158 iter->want_specific_block = want_specific_block;
3159 iter->block_index = block_index;
3160 iter->domain = domain;
3161 iter->next = 0;
3162
3163 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3164 iter->length = MAYBE_SWAP (*iter->vec);
3165 else
3166 {
3167 iter->vec = NULL;
3168 iter->length = 0;
3169 }
3170}
3171
3172/* Return the next matching CU or NULL if there are no more. */
3173
3174static struct dwarf2_per_cu_data *
3175dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3176{
3177 for ( ; iter->next < iter->length; ++iter->next)
3178 {
3179 offset_type cu_index_and_attrs =
3180 MAYBE_SWAP (iter->vec[iter->next + 1]);
3181 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3182 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3183 int want_static = iter->block_index != GLOBAL_BLOCK;
3184 /* This value is only valid for index versions >= 7. */
3185 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3186 gdb_index_symbol_kind symbol_kind =
3187 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3188 /* Only check the symbol attributes if they're present.
3189 Indices prior to version 7 don't record them,
3190 and indices >= 7 may elide them for certain symbols
3191 (gold does this). */
3192 int attrs_valid =
3193 (iter->index->version >= 7
3194 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3195
3196 /* Skip if already read in. */
3197 if (per_cu->v.quick->symtab)
3198 continue;
3199
3200 if (attrs_valid
3201 && iter->want_specific_block
3202 && want_static != is_static)
3203 continue;
3204
3205 /* Only check the symbol's kind if it has one. */
3206 if (attrs_valid)
3207 {
3208 switch (iter->domain)
3209 {
3210 case VAR_DOMAIN:
3211 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3212 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3213 /* Some types are also in VAR_DOMAIN. */
3214 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3215 continue;
3216 break;
3217 case STRUCT_DOMAIN:
3218 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3219 continue;
3220 break;
3221 case LABEL_DOMAIN:
3222 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3223 continue;
3224 break;
3225 default:
3226 break;
3227 }
3228 }
3229
3230 ++iter->next;
3231 return per_cu;
3232 }
3233
3234 return NULL;
3235}
3236
3237static struct symtab *
3238dw2_lookup_symbol (struct objfile *objfile, int block_index,
3239 const char *name, domain_enum domain)
9291a0cd 3240{
da51c347 3241 struct symtab *stab_best = NULL;
156942c7
DE
3242 struct mapped_index *index;
3243
9291a0cd
TT
3244 dw2_setup (objfile);
3245
156942c7
DE
3246 index = dwarf2_per_objfile->index_table;
3247
da51c347 3248 /* index is NULL if OBJF_READNOW. */
156942c7 3249 if (index)
9291a0cd 3250 {
da51c347
DE
3251 struct dw2_symtab_iterator iter;
3252 struct dwarf2_per_cu_data *per_cu;
3253
3254 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3255
da51c347 3256 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3257 {
da51c347
DE
3258 struct symbol *sym = NULL;
3259 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3260
3261 /* Some caution must be observed with overloaded functions
3262 and methods, since the index will not contain any overload
3263 information (but NAME might contain it). */
3264 if (stab->primary)
9291a0cd 3265 {
da51c347
DE
3266 struct blockvector *bv = BLOCKVECTOR (stab);
3267 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3268
da51c347
DE
3269 sym = lookup_block_symbol (block, name, domain);
3270 }
1fd400ff 3271
da51c347
DE
3272 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3273 {
3274 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3275 return stab;
3276
3277 stab_best = stab;
9291a0cd 3278 }
da51c347
DE
3279
3280 /* Keep looking through other CUs. */
9291a0cd
TT
3281 }
3282 }
9291a0cd 3283
da51c347 3284 return stab_best;
9291a0cd
TT
3285}
3286
3287static void
3288dw2_print_stats (struct objfile *objfile)
3289{
e4a48d9d 3290 int i, total, count;
9291a0cd
TT
3291
3292 dw2_setup (objfile);
e4a48d9d 3293 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3294 count = 0;
e4a48d9d 3295 for (i = 0; i < total; ++i)
9291a0cd 3296 {
e254ef6a 3297 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3298
e254ef6a 3299 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3300 ++count;
3301 }
e4a48d9d 3302 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3303 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3304}
3305
3306static void
3307dw2_dump (struct objfile *objfile)
3308{
3309 /* Nothing worth printing. */
3310}
3311
3312static void
3313dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3314 struct section_offsets *delta)
3315{
3316 /* There's nothing to relocate here. */
3317}
3318
3319static void
3320dw2_expand_symtabs_for_function (struct objfile *objfile,
3321 const char *func_name)
3322{
da51c347
DE
3323 struct mapped_index *index;
3324
3325 dw2_setup (objfile);
3326
3327 index = dwarf2_per_objfile->index_table;
3328
3329 /* index is NULL if OBJF_READNOW. */
3330 if (index)
3331 {
3332 struct dw2_symtab_iterator iter;
3333 struct dwarf2_per_cu_data *per_cu;
3334
3335 /* Note: It doesn't matter what we pass for block_index here. */
3336 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3337 func_name);
3338
3339 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3340 dw2_instantiate_symtab (per_cu);
3341 }
9291a0cd
TT
3342}
3343
3344static void
3345dw2_expand_all_symtabs (struct objfile *objfile)
3346{
3347 int i;
3348
3349 dw2_setup (objfile);
1fd400ff
TT
3350
3351 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3352 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3353 {
e254ef6a 3354 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3355
a0f42c21 3356 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3357 }
3358}
3359
3360static void
652a8996
JK
3361dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3362 const char *fullname)
9291a0cd
TT
3363{
3364 int i;
3365
3366 dw2_setup (objfile);
d4637a04
DE
3367
3368 /* We don't need to consider type units here.
3369 This is only called for examining code, e.g. expand_line_sal.
3370 There can be an order of magnitude (or more) more type units
3371 than comp units, and we avoid them if we can. */
3372
3373 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3374 {
3375 int j;
e254ef6a 3376 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3377 struct quick_file_names *file_data;
9291a0cd 3378
3d7bb9d9 3379 /* We only need to look at symtabs not already expanded. */
e254ef6a 3380 if (per_cu->v.quick->symtab)
9291a0cd
TT
3381 continue;
3382
e4a48d9d 3383 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3384 if (file_data == NULL)
9291a0cd
TT
3385 continue;
3386
7b9f3c50 3387 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3388 {
652a8996
JK
3389 const char *this_fullname = file_data->file_names[j];
3390
3391 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3392 {
a0f42c21 3393 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3394 break;
3395 }
3396 }
3397 }
3398}
3399
356d9f9d
TT
3400/* A helper function for dw2_find_symbol_file that finds the primary
3401 file name for a given CU. This is a die_reader_func. */
3402
3403static void
3404dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
d521ce57 3405 const gdb_byte *info_ptr,
356d9f9d
TT
3406 struct die_info *comp_unit_die,
3407 int has_children,
3408 void *data)
3409{
3410 const char **result_ptr = data;
3411 struct dwarf2_cu *cu = reader->cu;
3412 struct attribute *attr;
3413
3414 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3415 if (attr == NULL)
3416 *result_ptr = NULL;
3417 else
3418 *result_ptr = DW_STRING (attr);
3419}
3420
dd786858 3421static const char *
9291a0cd
TT
3422dw2_find_symbol_file (struct objfile *objfile, const char *name)
3423{
e254ef6a 3424 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3425 offset_type *vec;
356d9f9d 3426 const char *filename;
9291a0cd
TT
3427
3428 dw2_setup (objfile);
3429
ae2de4f8 3430 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3431 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3432 {
3433 struct symtab *s;
3434
d790cf0a
DE
3435 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3436 {
3437 struct blockvector *bv = BLOCKVECTOR (s);
3438 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3439 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3440
3441 if (sym)
652a8996
JK
3442 {
3443 /* Only file extension of returned filename is recognized. */
3444 return SYMBOL_SYMTAB (sym)->filename;
3445 }
d790cf0a 3446 }
96408a79
SA
3447 return NULL;
3448 }
9291a0cd
TT
3449
3450 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3451 name, &vec))
3452 return NULL;
3453
3454 /* Note that this just looks at the very first one named NAME -- but
3455 actually we are looking for a function. find_main_filename
3456 should be rewritten so that it doesn't require a custom hook. It
3457 could just use the ordinary symbol tables. */
3458 /* vec[0] is the length, which must always be >0. */
156942c7 3459 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3460
356d9f9d 3461 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3462 {
3463 /* Only file extension of returned filename is recognized. */
3464 return per_cu->v.quick->symtab->filename;
3465 }
356d9f9d 3466
f4dc4d17
DE
3467 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3468 dw2_get_primary_filename_reader, &filename);
9291a0cd 3469
652a8996 3470 /* Only file extension of returned filename is recognized. */
356d9f9d 3471 return filename;
9291a0cd
TT
3472}
3473
3474static void
40658b94
PH
3475dw2_map_matching_symbols (const char * name, domain_enum namespace,
3476 struct objfile *objfile, int global,
3477 int (*callback) (struct block *,
3478 struct symbol *, void *),
2edb89d3
JK
3479 void *data, symbol_compare_ftype *match,
3480 symbol_compare_ftype *ordered_compare)
9291a0cd 3481{
40658b94 3482 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3483 current language is Ada for a non-Ada objfile using GNU index. As Ada
3484 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3485}
3486
3487static void
f8eba3c6
TT
3488dw2_expand_symtabs_matching
3489 (struct objfile *objfile,
fbd9ab74 3490 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3491 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3492 enum search_domain kind,
3493 void *data)
9291a0cd
TT
3494{
3495 int i;
3496 offset_type iter;
4b5246aa 3497 struct mapped_index *index;
9291a0cd
TT
3498
3499 dw2_setup (objfile);
ae2de4f8
DE
3500
3501 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3502 if (!dwarf2_per_objfile->index_table)
3503 return;
4b5246aa 3504 index = dwarf2_per_objfile->index_table;
9291a0cd 3505
7b08b9eb 3506 if (file_matcher != NULL)
24c79950
TT
3507 {
3508 struct cleanup *cleanup;
3509 htab_t visited_found, visited_not_found;
3510
3511 visited_found = htab_create_alloc (10,
3512 htab_hash_pointer, htab_eq_pointer,
3513 NULL, xcalloc, xfree);
3514 cleanup = make_cleanup_htab_delete (visited_found);
3515 visited_not_found = htab_create_alloc (10,
3516 htab_hash_pointer, htab_eq_pointer,
3517 NULL, xcalloc, xfree);
3518 make_cleanup_htab_delete (visited_not_found);
3519
848e3e78
DE
3520 /* The rule is CUs specify all the files, including those used by
3521 any TU, so there's no need to scan TUs here. */
3522
3523 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3524 {
3525 int j;
f4dc4d17 3526 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3527 struct quick_file_names *file_data;
3528 void **slot;
7b08b9eb 3529
24c79950 3530 per_cu->v.quick->mark = 0;
3d7bb9d9 3531
24c79950
TT
3532 /* We only need to look at symtabs not already expanded. */
3533 if (per_cu->v.quick->symtab)
3534 continue;
7b08b9eb 3535
e4a48d9d 3536 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3537 if (file_data == NULL)
3538 continue;
7b08b9eb 3539
24c79950
TT
3540 if (htab_find (visited_not_found, file_data) != NULL)
3541 continue;
3542 else if (htab_find (visited_found, file_data) != NULL)
3543 {
3544 per_cu->v.quick->mark = 1;
3545 continue;
3546 }
3547
3548 for (j = 0; j < file_data->num_file_names; ++j)
3549 {
da235a7c
JK
3550 const char *this_real_name;
3551
fbd9ab74 3552 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3553 {
3554 per_cu->v.quick->mark = 1;
3555 break;
3556 }
da235a7c
JK
3557
3558 /* Before we invoke realpath, which can get expensive when many
3559 files are involved, do a quick comparison of the basenames. */
3560 if (!basenames_may_differ
3561 && !file_matcher (lbasename (file_data->file_names[j]),
3562 data, 1))
3563 continue;
3564
3565 this_real_name = dw2_get_real_path (objfile, file_data, j);
3566 if (file_matcher (this_real_name, data, 0))
3567 {
3568 per_cu->v.quick->mark = 1;
3569 break;
3570 }
24c79950
TT
3571 }
3572
3573 slot = htab_find_slot (per_cu->v.quick->mark
3574 ? visited_found
3575 : visited_not_found,
3576 file_data, INSERT);
3577 *slot = file_data;
3578 }
3579
3580 do_cleanups (cleanup);
3581 }
9291a0cd 3582
3876f04e 3583 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3584 {
3585 offset_type idx = 2 * iter;
3586 const char *name;
3587 offset_type *vec, vec_len, vec_idx;
3588
3876f04e 3589 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3590 continue;
3591
3876f04e 3592 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3593
e078317b 3594 if (! (*name_matcher) (name, data))
9291a0cd
TT
3595 continue;
3596
3597 /* The name was matched, now expand corresponding CUs that were
3598 marked. */
4b5246aa 3599 vec = (offset_type *) (index->constant_pool
3876f04e 3600 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3601 vec_len = MAYBE_SWAP (vec[0]);
3602 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3603 {
e254ef6a 3604 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3605 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3606 gdb_index_symbol_kind symbol_kind =
3607 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3608 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3609
3610 /* Don't crash on bad data. */
3611 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3612 + dwarf2_per_objfile->n_type_units))
156942c7 3613 continue;
1fd400ff 3614
156942c7
DE
3615 /* Only check the symbol's kind if it has one.
3616 Indices prior to version 7 don't record it. */
3617 if (index->version >= 7)
3618 {
3619 switch (kind)
3620 {
3621 case VARIABLES_DOMAIN:
3622 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3623 continue;
3624 break;
3625 case FUNCTIONS_DOMAIN:
3626 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3627 continue;
3628 break;
3629 case TYPES_DOMAIN:
3630 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3631 continue;
3632 break;
3633 default:
3634 break;
3635 }
3636 }
3637
3638 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3639 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3640 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3641 }
3642 }
3643}
3644
9703b513
TT
3645/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3646 symtab. */
3647
3648static struct symtab *
3649recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3650{
3651 int i;
3652
3653 if (BLOCKVECTOR (symtab) != NULL
3654 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3655 return symtab;
3656
a3ec0bb1
DE
3657 if (symtab->includes == NULL)
3658 return NULL;
3659
9703b513
TT
3660 for (i = 0; symtab->includes[i]; ++i)
3661 {
a3ec0bb1 3662 struct symtab *s = symtab->includes[i];
9703b513
TT
3663
3664 s = recursively_find_pc_sect_symtab (s, pc);
3665 if (s != NULL)
3666 return s;
3667 }
3668
3669 return NULL;
3670}
3671
9291a0cd
TT
3672static struct symtab *
3673dw2_find_pc_sect_symtab (struct objfile *objfile,
3674 struct minimal_symbol *msymbol,
3675 CORE_ADDR pc,
3676 struct obj_section *section,
3677 int warn_if_readin)
3678{
3679 struct dwarf2_per_cu_data *data;
9703b513 3680 struct symtab *result;
9291a0cd
TT
3681
3682 dw2_setup (objfile);
3683
3684 if (!objfile->psymtabs_addrmap)
3685 return NULL;
3686
3687 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3688 if (!data)
3689 return NULL;
3690
3691 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3692 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3693 paddress (get_objfile_arch (objfile), pc));
3694
9703b513
TT
3695 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3696 gdb_assert (result != NULL);
3697 return result;
9291a0cd
TT
3698}
3699
9291a0cd 3700static void
44b13c5a 3701dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3702 void *data, int need_fullname)
9291a0cd
TT
3703{
3704 int i;
24c79950
TT
3705 struct cleanup *cleanup;
3706 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3707 NULL, xcalloc, xfree);
9291a0cd 3708
24c79950 3709 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3710 dw2_setup (objfile);
ae2de4f8 3711
848e3e78
DE
3712 /* The rule is CUs specify all the files, including those used by
3713 any TU, so there's no need to scan TUs here.
3714 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3715
848e3e78 3716 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3717 {
3718 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3719
3720 if (per_cu->v.quick->symtab)
3721 {
3722 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3723 INSERT);
3724
3725 *slot = per_cu->v.quick->file_names;
3726 }
3727 }
3728
848e3e78 3729 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3730 {
3731 int j;
f4dc4d17 3732 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3733 struct quick_file_names *file_data;
24c79950 3734 void **slot;
9291a0cd 3735
3d7bb9d9 3736 /* We only need to look at symtabs not already expanded. */
e254ef6a 3737 if (per_cu->v.quick->symtab)
9291a0cd
TT
3738 continue;
3739
e4a48d9d 3740 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3741 if (file_data == NULL)
9291a0cd
TT
3742 continue;
3743
24c79950
TT
3744 slot = htab_find_slot (visited, file_data, INSERT);
3745 if (*slot)
3746 {
3747 /* Already visited. */
3748 continue;
3749 }
3750 *slot = file_data;
3751
7b9f3c50 3752 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3753 {
74e2f255
DE
3754 const char *this_real_name;
3755
3756 if (need_fullname)
3757 this_real_name = dw2_get_real_path (objfile, file_data, j);
3758 else
3759 this_real_name = NULL;
7b9f3c50 3760 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3761 }
3762 }
24c79950
TT
3763
3764 do_cleanups (cleanup);
9291a0cd
TT
3765}
3766
3767static int
3768dw2_has_symbols (struct objfile *objfile)
3769{
3770 return 1;
3771}
3772
3773const struct quick_symbol_functions dwarf2_gdb_index_functions =
3774{
3775 dw2_has_symbols,
3776 dw2_find_last_source_symtab,
3777 dw2_forget_cached_source_info,
f8eba3c6 3778 dw2_map_symtabs_matching_filename,
9291a0cd 3779 dw2_lookup_symbol,
9291a0cd
TT
3780 dw2_print_stats,
3781 dw2_dump,
3782 dw2_relocate,
3783 dw2_expand_symtabs_for_function,
3784 dw2_expand_all_symtabs,
652a8996 3785 dw2_expand_symtabs_with_fullname,
9291a0cd 3786 dw2_find_symbol_file,
40658b94 3787 dw2_map_matching_symbols,
9291a0cd
TT
3788 dw2_expand_symtabs_matching,
3789 dw2_find_pc_sect_symtab,
9291a0cd
TT
3790 dw2_map_symbol_filenames
3791};
3792
3793/* Initialize for reading DWARF for this objfile. Return 0 if this
3794 file will use psymtabs, or 1 if using the GNU index. */
3795
3796int
3797dwarf2_initialize_objfile (struct objfile *objfile)
3798{
3799 /* If we're about to read full symbols, don't bother with the
3800 indices. In this case we also don't care if some other debug
3801 format is making psymtabs, because they are all about to be
3802 expanded anyway. */
3803 if ((objfile->flags & OBJF_READNOW))
3804 {
3805 int i;
3806
3807 dwarf2_per_objfile->using_index = 1;
3808 create_all_comp_units (objfile);
0e50663e 3809 create_all_type_units (objfile);
7b9f3c50
DE
3810 dwarf2_per_objfile->quick_file_names_table =
3811 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3812
1fd400ff 3813 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3814 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3815 {
e254ef6a 3816 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3817
e254ef6a
DE
3818 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3819 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3820 }
3821
3822 /* Return 1 so that gdb sees the "quick" functions. However,
3823 these functions will be no-ops because we will have expanded
3824 all symtabs. */
3825 return 1;
3826 }
3827
3828 if (dwarf2_read_index (objfile))
3829 return 1;
3830
9291a0cd
TT
3831 return 0;
3832}
3833
3834\f
3835
dce234bc
PP
3836/* Build a partial symbol table. */
3837
3838void
f29dff0a 3839dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3840{
c9bf0622
TT
3841 volatile struct gdb_exception except;
3842
f29dff0a 3843 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3844 {
3845 init_psymbol_list (objfile, 1024);
3846 }
3847
c9bf0622
TT
3848 TRY_CATCH (except, RETURN_MASK_ERROR)
3849 {
3850 /* This isn't really ideal: all the data we allocate on the
3851 objfile's obstack is still uselessly kept around. However,
3852 freeing it seems unsafe. */
3853 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3854
3855 dwarf2_build_psymtabs_hard (objfile);
3856 discard_cleanups (cleanups);
3857 }
3858 if (except.reason < 0)
3859 exception_print (gdb_stderr, except);
c906108c 3860}
c906108c 3861
1ce1cefd
DE
3862/* Return the total length of the CU described by HEADER. */
3863
3864static unsigned int
3865get_cu_length (const struct comp_unit_head *header)
3866{
3867 return header->initial_length_size + header->length;
3868}
3869
45452591
DE
3870/* Return TRUE if OFFSET is within CU_HEADER. */
3871
3872static inline int
b64f50a1 3873offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3874{
b64f50a1 3875 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3876 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3877
b64f50a1 3878 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3879}
3880
3b80fe9b
DE
3881/* Find the base address of the compilation unit for range lists and
3882 location lists. It will normally be specified by DW_AT_low_pc.
3883 In DWARF-3 draft 4, the base address could be overridden by
3884 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3885 compilation units with discontinuous ranges. */
3886
3887static void
3888dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3889{
3890 struct attribute *attr;
3891
3892 cu->base_known = 0;
3893 cu->base_address = 0;
3894
3895 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3896 if (attr)
3897 {
3898 cu->base_address = DW_ADDR (attr);
3899 cu->base_known = 1;
3900 }
3901 else
3902 {
3903 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3904 if (attr)
3905 {
3906 cu->base_address = DW_ADDR (attr);
3907 cu->base_known = 1;
3908 }
3909 }
3910}
3911
93311388
DE
3912/* Read in the comp unit header information from the debug_info at info_ptr.
3913 NOTE: This leaves members offset, first_die_offset to be filled in
3914 by the caller. */
107d2387 3915
d521ce57 3916static const gdb_byte *
107d2387 3917read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3918 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3919{
3920 int signed_addr;
891d2f0b 3921 unsigned int bytes_read;
c764a876
DE
3922
3923 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3924 cu_header->initial_length_size = bytes_read;
3925 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3926 info_ptr += bytes_read;
107d2387
AC
3927 cu_header->version = read_2_bytes (abfd, info_ptr);
3928 info_ptr += 2;
b64f50a1
JK
3929 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3930 &bytes_read);
613e1657 3931 info_ptr += bytes_read;
107d2387
AC
3932 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3933 info_ptr += 1;
3934 signed_addr = bfd_get_sign_extend_vma (abfd);
3935 if (signed_addr < 0)
8e65ff28 3936 internal_error (__FILE__, __LINE__,
e2e0b3e5 3937 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3938 cu_header->signed_addr_p = signed_addr;
c764a876 3939
107d2387
AC
3940 return info_ptr;
3941}
3942
36586728
TT
3943/* Helper function that returns the proper abbrev section for
3944 THIS_CU. */
3945
3946static struct dwarf2_section_info *
3947get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3948{
3949 struct dwarf2_section_info *abbrev;
3950
3951 if (this_cu->is_dwz)
3952 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3953 else
3954 abbrev = &dwarf2_per_objfile->abbrev;
3955
3956 return abbrev;
3957}
3958
9ff913ba
DE
3959/* Subroutine of read_and_check_comp_unit_head and
3960 read_and_check_type_unit_head to simplify them.
3961 Perform various error checking on the header. */
3962
3963static void
3964error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3965 struct dwarf2_section_info *section,
3966 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3967{
3968 bfd *abfd = section->asection->owner;
3969 const char *filename = bfd_get_filename (abfd);
3970
3971 if (header->version != 2 && header->version != 3 && header->version != 4)
3972 error (_("Dwarf Error: wrong version in compilation unit header "
3973 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3974 filename);
3975
b64f50a1 3976 if (header->abbrev_offset.sect_off
36586728 3977 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3978 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3979 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3980 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3981 filename);
3982
3983 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3984 avoid potential 32-bit overflow. */
1ce1cefd 3985 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3986 > section->size)
3987 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3988 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3989 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3990 filename);
3991}
3992
3993/* Read in a CU/TU header and perform some basic error checking.
3994 The contents of the header are stored in HEADER.
3995 The result is a pointer to the start of the first DIE. */
adabb602 3996
d521ce57 3997static const gdb_byte *
9ff913ba
DE
3998read_and_check_comp_unit_head (struct comp_unit_head *header,
3999 struct dwarf2_section_info *section,
4bdcc0c1 4000 struct dwarf2_section_info *abbrev_section,
d521ce57 4001 const gdb_byte *info_ptr,
9ff913ba 4002 int is_debug_types_section)
72bf9492 4003{
d521ce57 4004 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4005 bfd *abfd = section->asection->owner;
72bf9492 4006
b64f50a1 4007 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4008
72bf9492
DJ
4009 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4010
460c1c54
CC
4011 /* If we're reading a type unit, skip over the signature and
4012 type_offset fields. */
b0df02fd 4013 if (is_debug_types_section)
460c1c54
CC
4014 info_ptr += 8 /*signature*/ + header->offset_size;
4015
b64f50a1 4016 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4017
4bdcc0c1 4018 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4019
4020 return info_ptr;
4021}
4022
348e048f
DE
4023/* Read in the types comp unit header information from .debug_types entry at
4024 types_ptr. The result is a pointer to one past the end of the header. */
4025
d521ce57 4026static const gdb_byte *
9ff913ba
DE
4027read_and_check_type_unit_head (struct comp_unit_head *header,
4028 struct dwarf2_section_info *section,
4bdcc0c1 4029 struct dwarf2_section_info *abbrev_section,
d521ce57 4030 const gdb_byte *info_ptr,
dee91e82
DE
4031 ULONGEST *signature,
4032 cu_offset *type_offset_in_tu)
348e048f 4033{
d521ce57 4034 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4035 bfd *abfd = section->asection->owner;
348e048f 4036
b64f50a1 4037 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4038
9ff913ba 4039 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4040
9ff913ba
DE
4041 /* If we're reading a type unit, skip over the signature and
4042 type_offset fields. */
4043 if (signature != NULL)
4044 *signature = read_8_bytes (abfd, info_ptr);
4045 info_ptr += 8;
dee91e82
DE
4046 if (type_offset_in_tu != NULL)
4047 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4048 header->offset_size);
9ff913ba
DE
4049 info_ptr += header->offset_size;
4050
b64f50a1 4051 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4052
4bdcc0c1 4053 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4054
4055 return info_ptr;
348e048f
DE
4056}
4057
f4dc4d17
DE
4058/* Fetch the abbreviation table offset from a comp or type unit header. */
4059
4060static sect_offset
4061read_abbrev_offset (struct dwarf2_section_info *section,
4062 sect_offset offset)
4063{
4064 bfd *abfd = section->asection->owner;
d521ce57 4065 const gdb_byte *info_ptr;
f4dc4d17
DE
4066 unsigned int length, initial_length_size, offset_size;
4067 sect_offset abbrev_offset;
4068
4069 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4070 info_ptr = section->buffer + offset.sect_off;
4071 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4072 offset_size = initial_length_size == 4 ? 4 : 8;
4073 info_ptr += initial_length_size + 2 /*version*/;
4074 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4075 return abbrev_offset;
4076}
4077
aaa75496
JB
4078/* Allocate a new partial symtab for file named NAME and mark this new
4079 partial symtab as being an include of PST. */
4080
4081static void
d521ce57 4082dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4083 struct objfile *objfile)
4084{
4085 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4086
fbd9ab74
JK
4087 if (!IS_ABSOLUTE_PATH (subpst->filename))
4088 {
4089 /* It shares objfile->objfile_obstack. */
4090 subpst->dirname = pst->dirname;
4091 }
4092
aaa75496
JB
4093 subpst->section_offsets = pst->section_offsets;
4094 subpst->textlow = 0;
4095 subpst->texthigh = 0;
4096
4097 subpst->dependencies = (struct partial_symtab **)
4098 obstack_alloc (&objfile->objfile_obstack,
4099 sizeof (struct partial_symtab *));
4100 subpst->dependencies[0] = pst;
4101 subpst->number_of_dependencies = 1;
4102
4103 subpst->globals_offset = 0;
4104 subpst->n_global_syms = 0;
4105 subpst->statics_offset = 0;
4106 subpst->n_static_syms = 0;
4107 subpst->symtab = NULL;
4108 subpst->read_symtab = pst->read_symtab;
4109 subpst->readin = 0;
4110
4111 /* No private part is necessary for include psymtabs. This property
4112 can be used to differentiate between such include psymtabs and
10b3939b 4113 the regular ones. */
58a9656e 4114 subpst->read_symtab_private = NULL;
aaa75496
JB
4115}
4116
4117/* Read the Line Number Program data and extract the list of files
4118 included by the source file represented by PST. Build an include
d85a05f0 4119 partial symtab for each of these included files. */
aaa75496
JB
4120
4121static void
4122dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4123 struct die_info *die,
4124 struct partial_symtab *pst)
aaa75496 4125{
d85a05f0
DJ
4126 struct line_header *lh = NULL;
4127 struct attribute *attr;
aaa75496 4128
d85a05f0
DJ
4129 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4130 if (attr)
3019eac3 4131 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4132 if (lh == NULL)
4133 return; /* No linetable, so no includes. */
4134
c6da4cef 4135 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4136 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4137
4138 free_line_header (lh);
4139}
4140
348e048f 4141static hashval_t
52dc124a 4142hash_signatured_type (const void *item)
348e048f 4143{
52dc124a 4144 const struct signatured_type *sig_type = item;
9a619af0 4145
348e048f 4146 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4147 return sig_type->signature;
348e048f
DE
4148}
4149
4150static int
52dc124a 4151eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4152{
4153 const struct signatured_type *lhs = item_lhs;
4154 const struct signatured_type *rhs = item_rhs;
9a619af0 4155
348e048f
DE
4156 return lhs->signature == rhs->signature;
4157}
4158
1fd400ff
TT
4159/* Allocate a hash table for signatured types. */
4160
4161static htab_t
673bfd45 4162allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4163{
4164 return htab_create_alloc_ex (41,
52dc124a
DE
4165 hash_signatured_type,
4166 eq_signatured_type,
1fd400ff
TT
4167 NULL,
4168 &objfile->objfile_obstack,
4169 hashtab_obstack_allocate,
4170 dummy_obstack_deallocate);
4171}
4172
d467dd73 4173/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4174
4175static int
d467dd73 4176add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4177{
4178 struct signatured_type *sigt = *slot;
b4dd5633 4179 struct signatured_type ***datap = datum;
1fd400ff 4180
b4dd5633 4181 **datap = sigt;
1fd400ff
TT
4182 ++*datap;
4183
4184 return 1;
4185}
4186
c88ee1f0
DE
4187/* Create the hash table of all entries in the .debug_types
4188 (or .debug_types.dwo) section(s).
4189 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4190 otherwise it is NULL.
4191
4192 The result is a pointer to the hash table or NULL if there are no types.
4193
4194 Note: This function processes DWO files only, not DWP files. */
348e048f 4195
3019eac3
DE
4196static htab_t
4197create_debug_types_hash_table (struct dwo_file *dwo_file,
4198 VEC (dwarf2_section_info_def) *types)
348e048f 4199{
3019eac3 4200 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4201 htab_t types_htab = NULL;
8b70b953
TT
4202 int ix;
4203 struct dwarf2_section_info *section;
4bdcc0c1 4204 struct dwarf2_section_info *abbrev_section;
348e048f 4205
3019eac3
DE
4206 if (VEC_empty (dwarf2_section_info_def, types))
4207 return NULL;
348e048f 4208
4bdcc0c1
DE
4209 abbrev_section = (dwo_file != NULL
4210 ? &dwo_file->sections.abbrev
4211 : &dwarf2_per_objfile->abbrev);
4212
09406207
DE
4213 if (dwarf2_read_debug)
4214 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4215 dwo_file ? ".dwo" : "",
4216 bfd_get_filename (abbrev_section->asection->owner));
4217
8b70b953 4218 for (ix = 0;
3019eac3 4219 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4220 ++ix)
4221 {
3019eac3 4222 bfd *abfd;
d521ce57 4223 const gdb_byte *info_ptr, *end_ptr;
36586728 4224 struct dwarf2_section_info *abbrev_section;
348e048f 4225
8b70b953
TT
4226 dwarf2_read_section (objfile, section);
4227 info_ptr = section->buffer;
348e048f 4228
8b70b953
TT
4229 if (info_ptr == NULL)
4230 continue;
348e048f 4231
3019eac3
DE
4232 /* We can't set abfd until now because the section may be empty or
4233 not present, in which case section->asection will be NULL. */
4234 abfd = section->asection->owner;
4235
36586728
TT
4236 if (dwo_file)
4237 abbrev_section = &dwo_file->sections.abbrev;
4238 else
4239 abbrev_section = &dwarf2_per_objfile->abbrev;
4240
dee91e82
DE
4241 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4242 because we don't need to read any dies: the signature is in the
4243 header. */
8b70b953
TT
4244
4245 end_ptr = info_ptr + section->size;
4246 while (info_ptr < end_ptr)
4247 {
b64f50a1 4248 sect_offset offset;
3019eac3 4249 cu_offset type_offset_in_tu;
8b70b953 4250 ULONGEST signature;
52dc124a 4251 struct signatured_type *sig_type;
3019eac3 4252 struct dwo_unit *dwo_tu;
8b70b953 4253 void **slot;
d521ce57 4254 const gdb_byte *ptr = info_ptr;
9ff913ba 4255 struct comp_unit_head header;
dee91e82 4256 unsigned int length;
348e048f 4257
b64f50a1 4258 offset.sect_off = ptr - section->buffer;
348e048f 4259
8b70b953 4260 /* We need to read the type's signature in order to build the hash
9ff913ba 4261 table, but we don't need anything else just yet. */
348e048f 4262
4bdcc0c1
DE
4263 ptr = read_and_check_type_unit_head (&header, section,
4264 abbrev_section, ptr,
3019eac3 4265 &signature, &type_offset_in_tu);
6caca83c 4266
1ce1cefd 4267 length = get_cu_length (&header);
dee91e82 4268
6caca83c 4269 /* Skip dummy type units. */
dee91e82
DE
4270 if (ptr >= info_ptr + length
4271 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4272 {
1ce1cefd 4273 info_ptr += length;
6caca83c
CC
4274 continue;
4275 }
8b70b953 4276
0349ea22
DE
4277 if (types_htab == NULL)
4278 {
4279 if (dwo_file)
4280 types_htab = allocate_dwo_unit_table (objfile);
4281 else
4282 types_htab = allocate_signatured_type_table (objfile);
4283 }
4284
3019eac3
DE
4285 if (dwo_file)
4286 {
4287 sig_type = NULL;
4288 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4289 struct dwo_unit);
4290 dwo_tu->dwo_file = dwo_file;
4291 dwo_tu->signature = signature;
4292 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4293 dwo_tu->section = section;
3019eac3
DE
4294 dwo_tu->offset = offset;
4295 dwo_tu->length = length;
4296 }
4297 else
4298 {
4299 /* N.B.: type_offset is not usable if this type uses a DWO file.
4300 The real type_offset is in the DWO file. */
4301 dwo_tu = NULL;
4302 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4303 struct signatured_type);
4304 sig_type->signature = signature;
4305 sig_type->type_offset_in_tu = type_offset_in_tu;
4306 sig_type->per_cu.objfile = objfile;
4307 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4308 sig_type->per_cu.section = section;
3019eac3
DE
4309 sig_type->per_cu.offset = offset;
4310 sig_type->per_cu.length = length;
4311 }
8b70b953 4312
3019eac3
DE
4313 slot = htab_find_slot (types_htab,
4314 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4315 INSERT);
8b70b953
TT
4316 gdb_assert (slot != NULL);
4317 if (*slot != NULL)
4318 {
3019eac3
DE
4319 sect_offset dup_offset;
4320
4321 if (dwo_file)
4322 {
4323 const struct dwo_unit *dup_tu = *slot;
4324
4325 dup_offset = dup_tu->offset;
4326 }
4327 else
4328 {
4329 const struct signatured_type *dup_tu = *slot;
4330
4331 dup_offset = dup_tu->per_cu.offset;
4332 }
b3c8eb43 4333
8b70b953 4334 complaint (&symfile_complaints,
c88ee1f0 4335 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4336 " the entry at offset 0x%x, signature %s"),
3019eac3 4337 offset.sect_off, dup_offset.sect_off,
4031ecc5 4338 hex_string (signature));
8b70b953 4339 }
3019eac3 4340 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4341
09406207 4342 if (dwarf2_read_debug)
4031ecc5 4343 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4344 offset.sect_off,
4031ecc5 4345 hex_string (signature));
348e048f 4346
dee91e82 4347 info_ptr += length;
8b70b953 4348 }
348e048f
DE
4349 }
4350
3019eac3
DE
4351 return types_htab;
4352}
4353
4354/* Create the hash table of all entries in the .debug_types section,
4355 and initialize all_type_units.
4356 The result is zero if there is an error (e.g. missing .debug_types section),
4357 otherwise non-zero. */
4358
4359static int
4360create_all_type_units (struct objfile *objfile)
4361{
4362 htab_t types_htab;
b4dd5633 4363 struct signatured_type **iter;
3019eac3
DE
4364
4365 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4366 if (types_htab == NULL)
4367 {
4368 dwarf2_per_objfile->signatured_types = NULL;
4369 return 0;
4370 }
4371
348e048f
DE
4372 dwarf2_per_objfile->signatured_types = types_htab;
4373
d467dd73
DE
4374 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4375 dwarf2_per_objfile->all_type_units
1fd400ff 4376 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4377 dwarf2_per_objfile->n_type_units
b4dd5633 4378 * sizeof (struct signatured_type *));
d467dd73
DE
4379 iter = &dwarf2_per_objfile->all_type_units[0];
4380 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4381 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4382 == dwarf2_per_objfile->n_type_units);
1fd400ff 4383
348e048f
DE
4384 return 1;
4385}
4386
380bca97 4387/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4388 Returns NULL if signature SIG is not present in the table.
4389 It is up to the caller to complain about this. */
348e048f
DE
4390
4391static struct signatured_type *
e319fa28 4392lookup_signatured_type (ULONGEST sig)
348e048f
DE
4393{
4394 struct signatured_type find_entry, *entry;
4395
4396 if (dwarf2_per_objfile->signatured_types == NULL)
5a8b3f62 4397 return NULL;
348e048f
DE
4398 find_entry.signature = sig;
4399 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4400 return entry;
4401}
42e7ad6c
DE
4402\f
4403/* Low level DIE reading support. */
348e048f 4404
d85a05f0
DJ
4405/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4406
4407static void
4408init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4409 struct dwarf2_cu *cu,
3019eac3
DE
4410 struct dwarf2_section_info *section,
4411 struct dwo_file *dwo_file)
d85a05f0 4412{
fceca515 4413 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4414 reader->abfd = section->asection->owner;
d85a05f0 4415 reader->cu = cu;
3019eac3 4416 reader->dwo_file = dwo_file;
dee91e82
DE
4417 reader->die_section = section;
4418 reader->buffer = section->buffer;
f664829e 4419 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4420}
4421
b0c7bfa9
DE
4422/* Subroutine of init_cutu_and_read_dies to simplify it.
4423 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4424 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4425 already.
4426
4427 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4428 from it to the DIE in the DWO. If NULL we are skipping the stub.
4429 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4430 are filled in with the info of the DIE from the DWO file.
4431 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4432 provided an abbrev table to use.
4433 The result is non-zero if a valid (non-dummy) DIE was found. */
4434
4435static int
4436read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4437 struct dwo_unit *dwo_unit,
4438 int abbrev_table_provided,
4439 struct die_info *stub_comp_unit_die,
4440 struct die_reader_specs *result_reader,
d521ce57 4441 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4442 struct die_info **result_comp_unit_die,
4443 int *result_has_children)
4444{
4445 struct objfile *objfile = dwarf2_per_objfile->objfile;
4446 struct dwarf2_cu *cu = this_cu->cu;
4447 struct dwarf2_section_info *section;
4448 bfd *abfd;
d521ce57 4449 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4450 const char *comp_dir_string;
4451 ULONGEST signature; /* Or dwo_id. */
4452 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4453 int i,num_extra_attrs;
4454 struct dwarf2_section_info *dwo_abbrev_section;
4455 struct attribute *attr;
4456 struct die_info *comp_unit_die;
4457
4458 /* These attributes aren't processed until later:
4459 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4460 However, the attribute is found in the stub which we won't have later.
4461 In order to not impose this complication on the rest of the code,
4462 we read them here and copy them to the DWO CU/TU die. */
4463
4464 stmt_list = NULL;
4465 low_pc = NULL;
4466 high_pc = NULL;
4467 ranges = NULL;
4468 comp_dir = NULL;
4469
4470 if (stub_comp_unit_die != NULL)
4471 {
4472 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4473 DWO file. */
4474 if (! this_cu->is_debug_types)
4475 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4476 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4477 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4478 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4479 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4480
4481 /* There should be a DW_AT_addr_base attribute here (if needed).
4482 We need the value before we can process DW_FORM_GNU_addr_index. */
4483 cu->addr_base = 0;
4484 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4485 if (attr)
4486 cu->addr_base = DW_UNSND (attr);
4487
4488 /* There should be a DW_AT_ranges_base attribute here (if needed).
4489 We need the value before we can process DW_AT_ranges. */
4490 cu->ranges_base = 0;
4491 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4492 if (attr)
4493 cu->ranges_base = DW_UNSND (attr);
4494 }
4495
4496 /* Set up for reading the DWO CU/TU. */
4497 cu->dwo_unit = dwo_unit;
4498 section = dwo_unit->section;
4499 dwarf2_read_section (objfile, section);
4500 abfd = section->asection->owner;
4501 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4502 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4503 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4504
4505 if (this_cu->is_debug_types)
4506 {
4507 ULONGEST header_signature;
4508 cu_offset type_offset_in_tu;
4509 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4510
4511 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4512 dwo_abbrev_section,
4513 info_ptr,
4514 &header_signature,
4515 &type_offset_in_tu);
4516 gdb_assert (sig_type->signature == header_signature);
4517 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4518 /* For DWOs coming from DWP files, we don't know the CU length
4519 nor the type's offset in the TU until now. */
4520 dwo_unit->length = get_cu_length (&cu->header);
4521 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4522
4523 /* Establish the type offset that can be used to lookup the type.
4524 For DWO files, we don't know it until now. */
4525 sig_type->type_offset_in_section.sect_off =
4526 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4527 }
4528 else
4529 {
4530 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4531 dwo_abbrev_section,
4532 info_ptr, 0);
4533 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4534 /* For DWOs coming from DWP files, we don't know the CU length
4535 until now. */
4536 dwo_unit->length = get_cu_length (&cu->header);
4537 }
4538
02142a6c
DE
4539 /* Replace the CU's original abbrev table with the DWO's.
4540 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4541 if (abbrev_table_provided)
4542 {
4543 /* Don't free the provided abbrev table, the caller of
4544 init_cutu_and_read_dies owns it. */
4545 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4546 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4547 make_cleanup (dwarf2_free_abbrev_table, cu);
4548 }
4549 else
4550 {
4551 dwarf2_free_abbrev_table (cu);
4552 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4553 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4554 }
4555
4556 /* Read in the die, but leave space to copy over the attributes
4557 from the stub. This has the benefit of simplifying the rest of
4558 the code - all the work to maintain the illusion of a single
4559 DW_TAG_{compile,type}_unit DIE is done here. */
4560 num_extra_attrs = ((stmt_list != NULL)
4561 + (low_pc != NULL)
4562 + (high_pc != NULL)
4563 + (ranges != NULL)
4564 + (comp_dir != NULL));
4565 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4566 result_has_children, num_extra_attrs);
4567
4568 /* Copy over the attributes from the stub to the DIE we just read in. */
4569 comp_unit_die = *result_comp_unit_die;
4570 i = comp_unit_die->num_attrs;
4571 if (stmt_list != NULL)
4572 comp_unit_die->attrs[i++] = *stmt_list;
4573 if (low_pc != NULL)
4574 comp_unit_die->attrs[i++] = *low_pc;
4575 if (high_pc != NULL)
4576 comp_unit_die->attrs[i++] = *high_pc;
4577 if (ranges != NULL)
4578 comp_unit_die->attrs[i++] = *ranges;
4579 if (comp_dir != NULL)
4580 comp_unit_die->attrs[i++] = *comp_dir;
4581 comp_unit_die->num_attrs += num_extra_attrs;
4582
bf6af496
DE
4583 if (dwarf2_die_debug)
4584 {
4585 fprintf_unfiltered (gdb_stdlog,
4586 "Read die from %s@0x%x of %s:\n",
4587 bfd_section_name (abfd, section->asection),
4588 (unsigned) (begin_info_ptr - section->buffer),
4589 bfd_get_filename (abfd));
4590 dump_die (comp_unit_die, dwarf2_die_debug);
4591 }
4592
b0c7bfa9
DE
4593 /* Skip dummy compilation units. */
4594 if (info_ptr >= begin_info_ptr + dwo_unit->length
4595 || peek_abbrev_code (abfd, info_ptr) == 0)
4596 return 0;
4597
4598 *result_info_ptr = info_ptr;
4599 return 1;
4600}
4601
4602/* Subroutine of init_cutu_and_read_dies to simplify it.
4603 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4604 If the specified DWO unit cannot be found an error is thrown. */
4605
4606static struct dwo_unit *
4607lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4608 struct die_info *comp_unit_die)
4609{
4610 struct dwarf2_cu *cu = this_cu->cu;
4611 struct attribute *attr;
4612 ULONGEST signature;
4613 struct dwo_unit *dwo_unit;
4614 const char *comp_dir, *dwo_name;
4615
4616 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4617 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4618 gdb_assert (attr != NULL);
4619 dwo_name = DW_STRING (attr);
4620 comp_dir = NULL;
4621 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4622 if (attr)
4623 comp_dir = DW_STRING (attr);
4624
4625 if (this_cu->is_debug_types)
4626 {
4627 struct signatured_type *sig_type;
4628
4629 /* Since this_cu is the first member of struct signatured_type,
4630 we can go from a pointer to one to a pointer to the other. */
4631 sig_type = (struct signatured_type *) this_cu;
4632 signature = sig_type->signature;
4633 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4634 }
4635 else
4636 {
4637 struct attribute *attr;
4638
4639 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4640 if (! attr)
4641 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4642 " [in module %s]"),
4643 dwo_name, this_cu->objfile->name);
4644 signature = DW_UNSND (attr);
4645 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4646 signature);
4647 }
4648
4649 if (dwo_unit == NULL)
4650 {
4651 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4652 " with ID %s [in module %s]"),
4031ecc5 4653 this_cu->offset.sect_off, hex_string (signature),
b0c7bfa9
DE
4654 this_cu->objfile->name);
4655 }
4656
4657 return dwo_unit;
4658}
4659
fd820528 4660/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4661 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4662
f4dc4d17
DE
4663 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4664 Otherwise the table specified in the comp unit header is read in and used.
4665 This is an optimization for when we already have the abbrev table.
4666
dee91e82
DE
4667 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4668 Otherwise, a new CU is allocated with xmalloc.
4669
4670 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4671 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4672
4673 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4674 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4675
70221824 4676static void
fd820528 4677init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4678 struct abbrev_table *abbrev_table,
fd820528
DE
4679 int use_existing_cu, int keep,
4680 die_reader_func_ftype *die_reader_func,
4681 void *data)
c906108c 4682{
dee91e82 4683 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4684 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4685 bfd *abfd = section->asection->owner;
dee91e82 4686 struct dwarf2_cu *cu;
d521ce57 4687 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4688 struct die_reader_specs reader;
d85a05f0 4689 struct die_info *comp_unit_die;
dee91e82 4690 int has_children;
d85a05f0 4691 struct attribute *attr;
dee91e82
DE
4692 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4693 struct signatured_type *sig_type = NULL;
4bdcc0c1 4694 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4695 /* Non-zero if CU currently points to a DWO file and we need to
4696 reread it. When this happens we need to reread the skeleton die
4697 before we can reread the DWO file. */
4698 int rereading_dwo_cu = 0;
c906108c 4699
09406207
DE
4700 if (dwarf2_die_debug)
4701 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4702 this_cu->is_debug_types ? "type" : "comp",
4703 this_cu->offset.sect_off);
4704
dee91e82
DE
4705 if (use_existing_cu)
4706 gdb_assert (keep);
23745b47 4707
dee91e82
DE
4708 cleanups = make_cleanup (null_cleanup, NULL);
4709
4710 /* This is cheap if the section is already read in. */
4711 dwarf2_read_section (objfile, section);
4712
4713 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4714
4715 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4716
4717 if (use_existing_cu && this_cu->cu != NULL)
4718 {
4719 cu = this_cu->cu;
42e7ad6c
DE
4720
4721 /* If this CU is from a DWO file we need to start over, we need to
4722 refetch the attributes from the skeleton CU.
4723 This could be optimized by retrieving those attributes from when we
4724 were here the first time: the previous comp_unit_die was stored in
4725 comp_unit_obstack. But there's no data yet that we need this
4726 optimization. */
4727 if (cu->dwo_unit != NULL)
4728 rereading_dwo_cu = 1;
dee91e82
DE
4729 }
4730 else
4731 {
4732 /* If !use_existing_cu, this_cu->cu must be NULL. */
4733 gdb_assert (this_cu->cu == NULL);
4734
4735 cu = xmalloc (sizeof (*cu));
4736 init_one_comp_unit (cu, this_cu);
4737
4738 /* If an error occurs while loading, release our storage. */
4739 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4740 }
dee91e82 4741
b0c7bfa9 4742 /* Get the header. */
42e7ad6c
DE
4743 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4744 {
4745 /* We already have the header, there's no need to read it in again. */
4746 info_ptr += cu->header.first_die_offset.cu_off;
4747 }
4748 else
4749 {
3019eac3 4750 if (this_cu->is_debug_types)
dee91e82
DE
4751 {
4752 ULONGEST signature;
42e7ad6c 4753 cu_offset type_offset_in_tu;
dee91e82 4754
4bdcc0c1
DE
4755 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4756 abbrev_section, info_ptr,
42e7ad6c
DE
4757 &signature,
4758 &type_offset_in_tu);
dee91e82 4759
42e7ad6c
DE
4760 /* Since per_cu is the first member of struct signatured_type,
4761 we can go from a pointer to one to a pointer to the other. */
4762 sig_type = (struct signatured_type *) this_cu;
4763 gdb_assert (sig_type->signature == signature);
4764 gdb_assert (sig_type->type_offset_in_tu.cu_off
4765 == type_offset_in_tu.cu_off);
dee91e82
DE
4766 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4767
42e7ad6c
DE
4768 /* LENGTH has not been set yet for type units if we're
4769 using .gdb_index. */
1ce1cefd 4770 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4771
4772 /* Establish the type offset that can be used to lookup the type. */
4773 sig_type->type_offset_in_section.sect_off =
4774 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4775 }
4776 else
4777 {
4bdcc0c1
DE
4778 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4779 abbrev_section,
4780 info_ptr, 0);
dee91e82
DE
4781
4782 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4783 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4784 }
4785 }
10b3939b 4786
6caca83c 4787 /* Skip dummy compilation units. */
dee91e82 4788 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4789 || peek_abbrev_code (abfd, info_ptr) == 0)
4790 {
dee91e82 4791 do_cleanups (cleanups);
21b2bd31 4792 return;
6caca83c
CC
4793 }
4794
433df2d4
DE
4795 /* If we don't have them yet, read the abbrevs for this compilation unit.
4796 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4797 done. Note that it's important that if the CU had an abbrev table
4798 on entry we don't free it when we're done: Somewhere up the call stack
4799 it may be in use. */
f4dc4d17
DE
4800 if (abbrev_table != NULL)
4801 {
4802 gdb_assert (cu->abbrev_table == NULL);
4803 gdb_assert (cu->header.abbrev_offset.sect_off
4804 == abbrev_table->offset.sect_off);
4805 cu->abbrev_table = abbrev_table;
4806 }
4807 else if (cu->abbrev_table == NULL)
dee91e82 4808 {
4bdcc0c1 4809 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4810 make_cleanup (dwarf2_free_abbrev_table, cu);
4811 }
42e7ad6c
DE
4812 else if (rereading_dwo_cu)
4813 {
4814 dwarf2_free_abbrev_table (cu);
4815 dwarf2_read_abbrevs (cu, abbrev_section);
4816 }
af703f96 4817
dee91e82 4818 /* Read the top level CU/TU die. */
3019eac3 4819 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4820 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4821
b0c7bfa9
DE
4822 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4823 from the DWO file.
4824 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4825 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
4826 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4827 if (attr)
4828 {
3019eac3 4829 struct dwo_unit *dwo_unit;
b0c7bfa9 4830 struct die_info *dwo_comp_unit_die;
3019eac3
DE
4831
4832 if (has_children)
4833 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4834 " has children (offset 0x%x) [in module %s]"),
4835 this_cu->offset.sect_off, bfd_get_filename (abfd));
b0c7bfa9
DE
4836 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
4837 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4838 abbrev_table != NULL,
4839 comp_unit_die,
4840 &reader, &info_ptr,
4841 &dwo_comp_unit_die, &has_children) == 0)
3019eac3 4842 {
b0c7bfa9 4843 /* Dummy die. */
3019eac3
DE
4844 do_cleanups (cleanups);
4845 return;
4846 }
b0c7bfa9 4847 comp_unit_die = dwo_comp_unit_die;
3019eac3
DE
4848 }
4849
b0c7bfa9 4850 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
4851 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4852
b0c7bfa9 4853 /* Done, clean up. */
dee91e82 4854 if (free_cu_cleanup != NULL)
348e048f 4855 {
dee91e82
DE
4856 if (keep)
4857 {
4858 /* We've successfully allocated this compilation unit. Let our
4859 caller clean it up when finished with it. */
4860 discard_cleanups (free_cu_cleanup);
4861
4862 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4863 So we have to manually free the abbrev table. */
4864 dwarf2_free_abbrev_table (cu);
4865
4866 /* Link this CU into read_in_chain. */
4867 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4868 dwarf2_per_objfile->read_in_chain = this_cu;
4869 }
4870 else
4871 do_cleanups (free_cu_cleanup);
348e048f 4872 }
dee91e82
DE
4873
4874 do_cleanups (cleanups);
4875}
4876
3019eac3
DE
4877/* Read CU/TU THIS_CU in section SECTION,
4878 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4879 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4880 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4881
4882 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4883 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4884
4885 We fill in THIS_CU->length.
4886
4887 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4888 linker) then DIE_READER_FUNC will not get called.
4889
4890 THIS_CU->cu is always freed when done.
3019eac3
DE
4891 This is done in order to not leave THIS_CU->cu in a state where we have
4892 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4893
4894static void
4895init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4896 struct dwarf2_section_info *abbrev_section,
3019eac3 4897 struct dwo_file *dwo_file,
dee91e82
DE
4898 die_reader_func_ftype *die_reader_func,
4899 void *data)
4900{
4901 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4902 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4903 bfd *abfd = section->asection->owner;
dee91e82 4904 struct dwarf2_cu cu;
d521ce57 4905 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
4906 struct die_reader_specs reader;
4907 struct cleanup *cleanups;
4908 struct die_info *comp_unit_die;
4909 int has_children;
4910
09406207
DE
4911 if (dwarf2_die_debug)
4912 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4913 this_cu->is_debug_types ? "type" : "comp",
4914 this_cu->offset.sect_off);
4915
dee91e82
DE
4916 gdb_assert (this_cu->cu == NULL);
4917
dee91e82
DE
4918 /* This is cheap if the section is already read in. */
4919 dwarf2_read_section (objfile, section);
4920
4921 init_one_comp_unit (&cu, this_cu);
4922
4923 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4924
4925 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4926 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4927 abbrev_section, info_ptr,
3019eac3 4928 this_cu->is_debug_types);
dee91e82 4929
1ce1cefd 4930 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4931
4932 /* Skip dummy compilation units. */
4933 if (info_ptr >= begin_info_ptr + this_cu->length
4934 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4935 {
dee91e82 4936 do_cleanups (cleanups);
21b2bd31 4937 return;
93311388 4938 }
72bf9492 4939
dee91e82
DE
4940 dwarf2_read_abbrevs (&cu, abbrev_section);
4941 make_cleanup (dwarf2_free_abbrev_table, &cu);
4942
3019eac3 4943 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4944 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4945
4946 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4947
4948 do_cleanups (cleanups);
4949}
4950
3019eac3
DE
4951/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4952 does not lookup the specified DWO file.
4953 This cannot be used to read DWO files.
dee91e82
DE
4954
4955 THIS_CU->cu is always freed when done.
3019eac3
DE
4956 This is done in order to not leave THIS_CU->cu in a state where we have
4957 to care whether it refers to the "main" CU or the DWO CU.
4958 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4959
4960static void
4961init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4962 die_reader_func_ftype *die_reader_func,
4963 void *data)
4964{
4965 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4966 get_abbrev_section_for_cu (this_cu),
3019eac3 4967 NULL,
dee91e82
DE
4968 die_reader_func, data);
4969}
0018ea6f
DE
4970\f
4971/* Type Unit Groups.
dee91e82 4972
0018ea6f
DE
4973 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4974 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4975 so that all types coming from the same compilation (.o file) are grouped
4976 together. A future step could be to put the types in the same symtab as
4977 the CU the types ultimately came from. */
ff013f42 4978
f4dc4d17
DE
4979static hashval_t
4980hash_type_unit_group (const void *item)
4981{
094b34ac 4982 const struct type_unit_group *tu_group = item;
f4dc4d17 4983
094b34ac 4984 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4985}
348e048f
DE
4986
4987static int
f4dc4d17 4988eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4989{
f4dc4d17
DE
4990 const struct type_unit_group *lhs = item_lhs;
4991 const struct type_unit_group *rhs = item_rhs;
348e048f 4992
094b34ac 4993 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4994}
348e048f 4995
f4dc4d17
DE
4996/* Allocate a hash table for type unit groups. */
4997
4998static htab_t
4999allocate_type_unit_groups_table (void)
5000{
5001 return htab_create_alloc_ex (3,
5002 hash_type_unit_group,
5003 eq_type_unit_group,
5004 NULL,
5005 &dwarf2_per_objfile->objfile->objfile_obstack,
5006 hashtab_obstack_allocate,
5007 dummy_obstack_deallocate);
5008}
dee91e82 5009
f4dc4d17
DE
5010/* Type units that don't have DW_AT_stmt_list are grouped into their own
5011 partial symtabs. We combine several TUs per psymtab to not let the size
5012 of any one psymtab grow too big. */
5013#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5014#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5015
094b34ac 5016/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5017 Create the type_unit_group object used to hold one or more TUs. */
5018
5019static struct type_unit_group *
094b34ac 5020create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5021{
5022 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5023 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5024 struct type_unit_group *tu_group;
f4dc4d17
DE
5025
5026 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5027 struct type_unit_group);
094b34ac 5028 per_cu = &tu_group->per_cu;
f4dc4d17 5029 per_cu->objfile = objfile;
f4dc4d17 5030
094b34ac
DE
5031 if (dwarf2_per_objfile->using_index)
5032 {
5033 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5034 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5035 }
5036 else
5037 {
5038 unsigned int line_offset = line_offset_struct.sect_off;
5039 struct partial_symtab *pst;
5040 char *name;
5041
5042 /* Give the symtab a useful name for debug purposes. */
5043 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5044 name = xstrprintf ("<type_units_%d>",
5045 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5046 else
5047 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5048
5049 pst = create_partial_symtab (per_cu, name);
5050 pst->anonymous = 1;
f4dc4d17 5051
094b34ac
DE
5052 xfree (name);
5053 }
f4dc4d17 5054
094b34ac
DE
5055 tu_group->hash.dwo_unit = cu->dwo_unit;
5056 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5057
5058 return tu_group;
5059}
5060
094b34ac
DE
5061/* Look up the type_unit_group for type unit CU, and create it if necessary.
5062 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5063
5064static struct type_unit_group *
094b34ac 5065get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5066{
5067 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5068 struct type_unit_group *tu_group;
5069 void **slot;
5070 unsigned int line_offset;
5071 struct type_unit_group type_unit_group_for_lookup;
5072
5073 if (dwarf2_per_objfile->type_unit_groups == NULL)
5074 {
5075 dwarf2_per_objfile->type_unit_groups =
5076 allocate_type_unit_groups_table ();
5077 }
5078
5079 /* Do we need to create a new group, or can we use an existing one? */
5080
5081 if (stmt_list)
5082 {
5083 line_offset = DW_UNSND (stmt_list);
5084 ++tu_stats->nr_symtab_sharers;
5085 }
5086 else
5087 {
5088 /* Ugh, no stmt_list. Rare, but we have to handle it.
5089 We can do various things here like create one group per TU or
5090 spread them over multiple groups to split up the expansion work.
5091 To avoid worst case scenarios (too many groups or too large groups)
5092 we, umm, group them in bunches. */
5093 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5094 | (tu_stats->nr_stmt_less_type_units
5095 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5096 ++tu_stats->nr_stmt_less_type_units;
5097 }
5098
094b34ac
DE
5099 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5100 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5101 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5102 &type_unit_group_for_lookup, INSERT);
5103 if (*slot != NULL)
5104 {
5105 tu_group = *slot;
5106 gdb_assert (tu_group != NULL);
5107 }
5108 else
5109 {
5110 sect_offset line_offset_struct;
5111
5112 line_offset_struct.sect_off = line_offset;
094b34ac 5113 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5114 *slot = tu_group;
5115 ++tu_stats->nr_symtabs;
5116 }
5117
5118 return tu_group;
5119}
5120
5121/* Struct used to sort TUs by their abbreviation table offset. */
5122
5123struct tu_abbrev_offset
5124{
5125 struct signatured_type *sig_type;
5126 sect_offset abbrev_offset;
5127};
5128
5129/* Helper routine for build_type_unit_groups, passed to qsort. */
5130
5131static int
5132sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5133{
5134 const struct tu_abbrev_offset * const *a = ap;
5135 const struct tu_abbrev_offset * const *b = bp;
5136 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5137 unsigned int boff = (*b)->abbrev_offset.sect_off;
5138
5139 return (aoff > boff) - (aoff < boff);
5140}
5141
5142/* A helper function to add a type_unit_group to a table. */
5143
5144static int
5145add_type_unit_group_to_table (void **slot, void *datum)
5146{
5147 struct type_unit_group *tu_group = *slot;
5148 struct type_unit_group ***datap = datum;
5149
5150 **datap = tu_group;
5151 ++*datap;
5152
5153 return 1;
5154}
5155
5156/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5157 each one passing FUNC,DATA.
5158
5159 The efficiency is because we sort TUs by the abbrev table they use and
5160 only read each abbrev table once. In one program there are 200K TUs
5161 sharing 8K abbrev tables.
5162
5163 The main purpose of this function is to support building the
5164 dwarf2_per_objfile->type_unit_groups table.
5165 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5166 can collapse the search space by grouping them by stmt_list.
5167 The savings can be significant, in the same program from above the 200K TUs
5168 share 8K stmt_list tables.
5169
5170 FUNC is expected to call get_type_unit_group, which will create the
5171 struct type_unit_group if necessary and add it to
5172 dwarf2_per_objfile->type_unit_groups. */
5173
5174static void
5175build_type_unit_groups (die_reader_func_ftype *func, void *data)
5176{
5177 struct objfile *objfile = dwarf2_per_objfile->objfile;
5178 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5179 struct cleanup *cleanups;
5180 struct abbrev_table *abbrev_table;
5181 sect_offset abbrev_offset;
5182 struct tu_abbrev_offset *sorted_by_abbrev;
5183 struct type_unit_group **iter;
5184 int i;
5185
5186 /* It's up to the caller to not call us multiple times. */
5187 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5188
5189 if (dwarf2_per_objfile->n_type_units == 0)
5190 return;
5191
5192 /* TUs typically share abbrev tables, and there can be way more TUs than
5193 abbrev tables. Sort by abbrev table to reduce the number of times we
5194 read each abbrev table in.
5195 Alternatives are to punt or to maintain a cache of abbrev tables.
5196 This is simpler and efficient enough for now.
5197
5198 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5199 symtab to use). Typically TUs with the same abbrev offset have the same
5200 stmt_list value too so in practice this should work well.
5201
5202 The basic algorithm here is:
5203
5204 sort TUs by abbrev table
5205 for each TU with same abbrev table:
5206 read abbrev table if first user
5207 read TU top level DIE
5208 [IWBN if DWO skeletons had DW_AT_stmt_list]
5209 call FUNC */
5210
5211 if (dwarf2_read_debug)
5212 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5213
5214 /* Sort in a separate table to maintain the order of all_type_units
5215 for .gdb_index: TU indices directly index all_type_units. */
5216 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5217 dwarf2_per_objfile->n_type_units);
5218 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5219 {
5220 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5221
5222 sorted_by_abbrev[i].sig_type = sig_type;
5223 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5224 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5225 sig_type->per_cu.offset);
5226 }
5227 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5228 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5229 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5230
094b34ac
DE
5231 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5232 called any number of times, so we don't reset tu_stats here. */
5233
f4dc4d17
DE
5234 abbrev_offset.sect_off = ~(unsigned) 0;
5235 abbrev_table = NULL;
5236 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5237
5238 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5239 {
5240 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5241
5242 /* Switch to the next abbrev table if necessary. */
5243 if (abbrev_table == NULL
5244 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5245 {
5246 if (abbrev_table != NULL)
5247 {
5248 abbrev_table_free (abbrev_table);
5249 /* Reset to NULL in case abbrev_table_read_table throws
5250 an error: abbrev_table_free_cleanup will get called. */
5251 abbrev_table = NULL;
5252 }
5253 abbrev_offset = tu->abbrev_offset;
5254 abbrev_table =
5255 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5256 abbrev_offset);
5257 ++tu_stats->nr_uniq_abbrev_tables;
5258 }
5259
5260 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5261 func, data);
5262 }
5263
5264 /* Create a vector of pointers to primary type units to make it easy to
5265 iterate over them and CUs. See dw2_get_primary_cu. */
5266 dwarf2_per_objfile->n_type_unit_groups =
5267 htab_elements (dwarf2_per_objfile->type_unit_groups);
5268 dwarf2_per_objfile->all_type_unit_groups =
5269 obstack_alloc (&objfile->objfile_obstack,
5270 dwarf2_per_objfile->n_type_unit_groups
5271 * sizeof (struct type_unit_group *));
5272 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5273 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5274 add_type_unit_group_to_table, &iter);
5275 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5276 == dwarf2_per_objfile->n_type_unit_groups);
5277
5278 do_cleanups (cleanups);
5279
5280 if (dwarf2_read_debug)
5281 {
5282 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5283 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5284 dwarf2_per_objfile->n_type_units);
5285 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5286 tu_stats->nr_uniq_abbrev_tables);
5287 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5288 tu_stats->nr_symtabs);
5289 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5290 tu_stats->nr_symtab_sharers);
5291 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5292 tu_stats->nr_stmt_less_type_units);
5293 }
5294}
0018ea6f
DE
5295\f
5296/* Partial symbol tables. */
5297
5298/* Create a psymtab named NAME and assign it to PER_CU.
5299
5300 The caller must fill in the following details:
5301 dirname, textlow, texthigh. */
5302
5303static struct partial_symtab *
5304create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5305{
5306 struct objfile *objfile = per_cu->objfile;
5307 struct partial_symtab *pst;
5308
5309 pst = start_psymtab_common (objfile, objfile->section_offsets,
5310 name, 0,
5311 objfile->global_psymbols.next,
5312 objfile->static_psymbols.next);
5313
5314 pst->psymtabs_addrmap_supported = 1;
5315
5316 /* This is the glue that links PST into GDB's symbol API. */
5317 pst->read_symtab_private = per_cu;
5318 pst->read_symtab = dwarf2_read_symtab;
5319 per_cu->v.psymtab = pst;
5320
5321 return pst;
5322}
5323
5324/* die_reader_func for process_psymtab_comp_unit. */
5325
5326static void
5327process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5328 const gdb_byte *info_ptr,
0018ea6f
DE
5329 struct die_info *comp_unit_die,
5330 int has_children,
5331 void *data)
5332{
5333 struct dwarf2_cu *cu = reader->cu;
5334 struct objfile *objfile = cu->objfile;
5335 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5336 struct attribute *attr;
5337 CORE_ADDR baseaddr;
5338 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5339 struct partial_symtab *pst;
5340 int has_pc_info;
5341 const char *filename;
5342 int *want_partial_unit_ptr = data;
5343
5344 if (comp_unit_die->tag == DW_TAG_partial_unit
5345 && (want_partial_unit_ptr == NULL
5346 || !*want_partial_unit_ptr))
5347 return;
5348
5349 gdb_assert (! per_cu->is_debug_types);
5350
5351 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5352
5353 cu->list_in_scope = &file_symbols;
5354
5355 /* Allocate a new partial symbol table structure. */
5356 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5357 if (attr == NULL || !DW_STRING (attr))
5358 filename = "";
5359 else
5360 filename = DW_STRING (attr);
5361
5362 pst = create_partial_symtab (per_cu, filename);
5363
5364 /* This must be done before calling dwarf2_build_include_psymtabs. */
5365 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5366 if (attr != NULL)
5367 pst->dirname = DW_STRING (attr);
5368
5369 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5370
5371 dwarf2_find_base_address (comp_unit_die, cu);
5372
5373 /* Possibly set the default values of LOWPC and HIGHPC from
5374 `DW_AT_ranges'. */
5375 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5376 &best_highpc, cu, pst);
5377 if (has_pc_info == 1 && best_lowpc < best_highpc)
5378 /* Store the contiguous range if it is not empty; it can be empty for
5379 CUs with no code. */
5380 addrmap_set_empty (objfile->psymtabs_addrmap,
5381 best_lowpc + baseaddr,
5382 best_highpc + baseaddr - 1, pst);
5383
5384 /* Check if comp unit has_children.
5385 If so, read the rest of the partial symbols from this comp unit.
5386 If not, there's no more debug_info for this comp unit. */
5387 if (has_children)
5388 {
5389 struct partial_die_info *first_die;
5390 CORE_ADDR lowpc, highpc;
5391
5392 lowpc = ((CORE_ADDR) -1);
5393 highpc = ((CORE_ADDR) 0);
5394
5395 first_die = load_partial_dies (reader, info_ptr, 1);
5396
5397 scan_partial_symbols (first_die, &lowpc, &highpc,
5398 ! has_pc_info, cu);
5399
5400 /* If we didn't find a lowpc, set it to highpc to avoid
5401 complaints from `maint check'. */
5402 if (lowpc == ((CORE_ADDR) -1))
5403 lowpc = highpc;
5404
5405 /* If the compilation unit didn't have an explicit address range,
5406 then use the information extracted from its child dies. */
5407 if (! has_pc_info)
5408 {
5409 best_lowpc = lowpc;
5410 best_highpc = highpc;
5411 }
5412 }
5413 pst->textlow = best_lowpc + baseaddr;
5414 pst->texthigh = best_highpc + baseaddr;
5415
5416 pst->n_global_syms = objfile->global_psymbols.next -
5417 (objfile->global_psymbols.list + pst->globals_offset);
5418 pst->n_static_syms = objfile->static_psymbols.next -
5419 (objfile->static_psymbols.list + pst->statics_offset);
5420 sort_pst_symbols (objfile, pst);
5421
5422 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5423 {
5424 int i;
5425 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5426 struct dwarf2_per_cu_data *iter;
5427
5428 /* Fill in 'dependencies' here; we fill in 'users' in a
5429 post-pass. */
5430 pst->number_of_dependencies = len;
5431 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5432 len * sizeof (struct symtab *));
5433 for (i = 0;
5434 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5435 i, iter);
5436 ++i)
5437 pst->dependencies[i] = iter->v.psymtab;
5438
5439 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5440 }
5441
5442 /* Get the list of files included in the current compilation unit,
5443 and build a psymtab for each of them. */
5444 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5445
5446 if (dwarf2_read_debug)
5447 {
5448 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5449
5450 fprintf_unfiltered (gdb_stdlog,
5451 "Psymtab for %s unit @0x%x: %s - %s"
5452 ", %d global, %d static syms\n",
5453 per_cu->is_debug_types ? "type" : "comp",
5454 per_cu->offset.sect_off,
5455 paddress (gdbarch, pst->textlow),
5456 paddress (gdbarch, pst->texthigh),
5457 pst->n_global_syms, pst->n_static_syms);
5458 }
5459}
5460
5461/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5462 Process compilation unit THIS_CU for a psymtab. */
5463
5464static void
5465process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5466 int want_partial_unit)
5467{
5468 /* If this compilation unit was already read in, free the
5469 cached copy in order to read it in again. This is
5470 necessary because we skipped some symbols when we first
5471 read in the compilation unit (see load_partial_dies).
5472 This problem could be avoided, but the benefit is unclear. */
5473 if (this_cu->cu != NULL)
5474 free_one_cached_comp_unit (this_cu);
5475
5476 gdb_assert (! this_cu->is_debug_types);
5477 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5478 process_psymtab_comp_unit_reader,
5479 &want_partial_unit);
5480
5481 /* Age out any secondary CUs. */
5482 age_cached_comp_units ();
5483}
f4dc4d17
DE
5484
5485/* Reader function for build_type_psymtabs. */
5486
5487static void
5488build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5489 const gdb_byte *info_ptr,
f4dc4d17
DE
5490 struct die_info *type_unit_die,
5491 int has_children,
5492 void *data)
5493{
5494 struct objfile *objfile = dwarf2_per_objfile->objfile;
5495 struct dwarf2_cu *cu = reader->cu;
5496 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5497 struct signatured_type *sig_type;
f4dc4d17
DE
5498 struct type_unit_group *tu_group;
5499 struct attribute *attr;
5500 struct partial_die_info *first_die;
5501 CORE_ADDR lowpc, highpc;
5502 struct partial_symtab *pst;
5503
5504 gdb_assert (data == NULL);
0186c6a7
DE
5505 gdb_assert (per_cu->is_debug_types);
5506 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5507
5508 if (! has_children)
5509 return;
5510
5511 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5512 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5513
0186c6a7 5514 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5515
5516 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5517 cu->list_in_scope = &file_symbols;
5518 pst = create_partial_symtab (per_cu, "");
5519 pst->anonymous = 1;
5520
5521 first_die = load_partial_dies (reader, info_ptr, 1);
5522
5523 lowpc = (CORE_ADDR) -1;
5524 highpc = (CORE_ADDR) 0;
5525 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5526
5527 pst->n_global_syms = objfile->global_psymbols.next -
5528 (objfile->global_psymbols.list + pst->globals_offset);
5529 pst->n_static_syms = objfile->static_psymbols.next -
5530 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5531 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5532}
5533
5534/* Traversal function for build_type_psymtabs. */
5535
5536static int
5537build_type_psymtab_dependencies (void **slot, void *info)
5538{
5539 struct objfile *objfile = dwarf2_per_objfile->objfile;
5540 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5541 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5542 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5543 int len = VEC_length (sig_type_ptr, tu_group->tus);
5544 struct signatured_type *iter;
f4dc4d17
DE
5545 int i;
5546
5547 gdb_assert (len > 0);
0186c6a7 5548 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5549
5550 pst->number_of_dependencies = len;
5551 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5552 len * sizeof (struct psymtab *));
5553 for (i = 0;
0186c6a7 5554 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5555 ++i)
5556 {
0186c6a7
DE
5557 gdb_assert (iter->per_cu.is_debug_types);
5558 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5559 iter->type_unit_group = tu_group;
f4dc4d17
DE
5560 }
5561
0186c6a7 5562 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5563
5564 return 1;
5565}
5566
5567/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5568 Build partial symbol tables for the .debug_types comp-units. */
5569
5570static void
5571build_type_psymtabs (struct objfile *objfile)
5572{
0e50663e 5573 if (! create_all_type_units (objfile))
348e048f
DE
5574 return;
5575
f4dc4d17
DE
5576 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5577
5578 /* Now that all TUs have been processed we can fill in the dependencies. */
5579 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5580 build_type_psymtab_dependencies, NULL);
348e048f
DE
5581}
5582
60606b2c
TT
5583/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5584
5585static void
5586psymtabs_addrmap_cleanup (void *o)
5587{
5588 struct objfile *objfile = o;
ec61707d 5589
60606b2c
TT
5590 objfile->psymtabs_addrmap = NULL;
5591}
5592
95554aad
TT
5593/* Compute the 'user' field for each psymtab in OBJFILE. */
5594
5595static void
5596set_partial_user (struct objfile *objfile)
5597{
5598 int i;
5599
5600 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5601 {
5602 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5603 struct partial_symtab *pst = per_cu->v.psymtab;
5604 int j;
5605
36586728
TT
5606 if (pst == NULL)
5607 continue;
5608
95554aad
TT
5609 for (j = 0; j < pst->number_of_dependencies; ++j)
5610 {
5611 /* Set the 'user' field only if it is not already set. */
5612 if (pst->dependencies[j]->user == NULL)
5613 pst->dependencies[j]->user = pst;
5614 }
5615 }
5616}
5617
93311388
DE
5618/* Build the partial symbol table by doing a quick pass through the
5619 .debug_info and .debug_abbrev sections. */
72bf9492 5620
93311388 5621static void
c67a9c90 5622dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5623{
60606b2c
TT
5624 struct cleanup *back_to, *addrmap_cleanup;
5625 struct obstack temp_obstack;
21b2bd31 5626 int i;
93311388 5627
45cfd468
DE
5628 if (dwarf2_read_debug)
5629 {
5630 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5631 objfile->name);
5632 }
5633
98bfdba5
PA
5634 dwarf2_per_objfile->reading_partial_symbols = 1;
5635
be391dca 5636 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5637
93311388
DE
5638 /* Any cached compilation units will be linked by the per-objfile
5639 read_in_chain. Make sure to free them when we're done. */
5640 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5641
348e048f
DE
5642 build_type_psymtabs (objfile);
5643
93311388 5644 create_all_comp_units (objfile);
c906108c 5645
60606b2c
TT
5646 /* Create a temporary address map on a temporary obstack. We later
5647 copy this to the final obstack. */
5648 obstack_init (&temp_obstack);
5649 make_cleanup_obstack_free (&temp_obstack);
5650 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5651 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5652
21b2bd31 5653 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5654 {
21b2bd31 5655 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5656
95554aad 5657 process_psymtab_comp_unit (per_cu, 0);
c906108c 5658 }
ff013f42 5659
95554aad
TT
5660 set_partial_user (objfile);
5661
ff013f42
JK
5662 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5663 &objfile->objfile_obstack);
60606b2c 5664 discard_cleanups (addrmap_cleanup);
ff013f42 5665
ae038cb0 5666 do_cleanups (back_to);
45cfd468
DE
5667
5668 if (dwarf2_read_debug)
5669 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5670 objfile->name);
ae038cb0
DJ
5671}
5672
3019eac3 5673/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5674
5675static void
dee91e82 5676load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5677 const gdb_byte *info_ptr,
dee91e82
DE
5678 struct die_info *comp_unit_die,
5679 int has_children,
5680 void *data)
ae038cb0 5681{
dee91e82 5682 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5683
95554aad 5684 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5685
ae038cb0
DJ
5686 /* Check if comp unit has_children.
5687 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5688 If not, there's no more debug_info for this comp unit. */
d85a05f0 5689 if (has_children)
dee91e82
DE
5690 load_partial_dies (reader, info_ptr, 0);
5691}
98bfdba5 5692
dee91e82
DE
5693/* Load the partial DIEs for a secondary CU into memory.
5694 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5695
dee91e82
DE
5696static void
5697load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5698{
f4dc4d17
DE
5699 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5700 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5701}
5702
ae038cb0 5703static void
36586728
TT
5704read_comp_units_from_section (struct objfile *objfile,
5705 struct dwarf2_section_info *section,
5706 unsigned int is_dwz,
5707 int *n_allocated,
5708 int *n_comp_units,
5709 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5710{
d521ce57 5711 const gdb_byte *info_ptr;
36586728 5712 bfd *abfd = section->asection->owner;
be391dca 5713
bf6af496
DE
5714 if (dwarf2_read_debug)
5715 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
5716 section->asection->name, bfd_get_filename (abfd));
5717
36586728 5718 dwarf2_read_section (objfile, section);
ae038cb0 5719
36586728 5720 info_ptr = section->buffer;
6e70227d 5721
36586728 5722 while (info_ptr < section->buffer + section->size)
ae038cb0 5723 {
c764a876 5724 unsigned int length, initial_length_size;
ae038cb0 5725 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5726 sect_offset offset;
ae038cb0 5727
36586728 5728 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5729
5730 /* Read just enough information to find out where the next
5731 compilation unit is. */
36586728 5732 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5733
5734 /* Save the compilation unit for later lookup. */
5735 this_cu = obstack_alloc (&objfile->objfile_obstack,
5736 sizeof (struct dwarf2_per_cu_data));
5737 memset (this_cu, 0, sizeof (*this_cu));
5738 this_cu->offset = offset;
c764a876 5739 this_cu->length = length + initial_length_size;
36586728 5740 this_cu->is_dwz = is_dwz;
9291a0cd 5741 this_cu->objfile = objfile;
8a0459fd 5742 this_cu->section = section;
ae038cb0 5743
36586728 5744 if (*n_comp_units == *n_allocated)
ae038cb0 5745 {
36586728
TT
5746 *n_allocated *= 2;
5747 *all_comp_units = xrealloc (*all_comp_units,
5748 *n_allocated
5749 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5750 }
36586728
TT
5751 (*all_comp_units)[*n_comp_units] = this_cu;
5752 ++*n_comp_units;
ae038cb0
DJ
5753
5754 info_ptr = info_ptr + this_cu->length;
5755 }
36586728
TT
5756}
5757
5758/* Create a list of all compilation units in OBJFILE.
5759 This is only done for -readnow and building partial symtabs. */
5760
5761static void
5762create_all_comp_units (struct objfile *objfile)
5763{
5764 int n_allocated;
5765 int n_comp_units;
5766 struct dwarf2_per_cu_data **all_comp_units;
5767
5768 n_comp_units = 0;
5769 n_allocated = 10;
5770 all_comp_units = xmalloc (n_allocated
5771 * sizeof (struct dwarf2_per_cu_data *));
5772
5773 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5774 &n_allocated, &n_comp_units, &all_comp_units);
5775
5776 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5777 {
5778 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5779
5780 read_comp_units_from_section (objfile, &dwz->info, 1,
5781 &n_allocated, &n_comp_units,
5782 &all_comp_units);
5783 }
ae038cb0
DJ
5784
5785 dwarf2_per_objfile->all_comp_units
5786 = obstack_alloc (&objfile->objfile_obstack,
5787 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5788 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5789 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5790 xfree (all_comp_units);
5791 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5792}
5793
5734ee8b
DJ
5794/* Process all loaded DIEs for compilation unit CU, starting at
5795 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5796 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5797 DW_AT_ranges). If NEED_PC is set, then this function will set
5798 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5799 and record the covered ranges in the addrmap. */
c906108c 5800
72bf9492
DJ
5801static void
5802scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5803 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5804{
72bf9492 5805 struct partial_die_info *pdi;
c906108c 5806
91c24f0a
DC
5807 /* Now, march along the PDI's, descending into ones which have
5808 interesting children but skipping the children of the other ones,
5809 until we reach the end of the compilation unit. */
c906108c 5810
72bf9492 5811 pdi = first_die;
91c24f0a 5812
72bf9492
DJ
5813 while (pdi != NULL)
5814 {
5815 fixup_partial_die (pdi, cu);
c906108c 5816
f55ee35c 5817 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5818 children, so we need to look at them. Ditto for anonymous
5819 enums. */
933c6fe4 5820
72bf9492 5821 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5822 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5823 || pdi->tag == DW_TAG_imported_unit)
c906108c 5824 {
72bf9492 5825 switch (pdi->tag)
c906108c
SS
5826 {
5827 case DW_TAG_subprogram:
5734ee8b 5828 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5829 break;
72929c62 5830 case DW_TAG_constant:
c906108c
SS
5831 case DW_TAG_variable:
5832 case DW_TAG_typedef:
91c24f0a 5833 case DW_TAG_union_type:
72bf9492 5834 if (!pdi->is_declaration)
63d06c5c 5835 {
72bf9492 5836 add_partial_symbol (pdi, cu);
63d06c5c
DC
5837 }
5838 break;
c906108c 5839 case DW_TAG_class_type:
680b30c7 5840 case DW_TAG_interface_type:
c906108c 5841 case DW_TAG_structure_type:
72bf9492 5842 if (!pdi->is_declaration)
c906108c 5843 {
72bf9492 5844 add_partial_symbol (pdi, cu);
c906108c
SS
5845 }
5846 break;
91c24f0a 5847 case DW_TAG_enumeration_type:
72bf9492
DJ
5848 if (!pdi->is_declaration)
5849 add_partial_enumeration (pdi, cu);
c906108c
SS
5850 break;
5851 case DW_TAG_base_type:
a02abb62 5852 case DW_TAG_subrange_type:
c906108c 5853 /* File scope base type definitions are added to the partial
c5aa993b 5854 symbol table. */
72bf9492 5855 add_partial_symbol (pdi, cu);
c906108c 5856 break;
d9fa45fe 5857 case DW_TAG_namespace:
5734ee8b 5858 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5859 break;
5d7cb8df
JK
5860 case DW_TAG_module:
5861 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5862 break;
95554aad
TT
5863 case DW_TAG_imported_unit:
5864 {
5865 struct dwarf2_per_cu_data *per_cu;
5866
f4dc4d17
DE
5867 /* For now we don't handle imported units in type units. */
5868 if (cu->per_cu->is_debug_types)
5869 {
5870 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5871 " supported in type units [in module %s]"),
5872 cu->objfile->name);
5873 }
5874
95554aad 5875 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5876 pdi->is_dwz,
95554aad
TT
5877 cu->objfile);
5878
5879 /* Go read the partial unit, if needed. */
5880 if (per_cu->v.psymtab == NULL)
5881 process_psymtab_comp_unit (per_cu, 1);
5882
f4dc4d17 5883 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5884 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5885 }
5886 break;
c906108c
SS
5887 default:
5888 break;
5889 }
5890 }
5891
72bf9492
DJ
5892 /* If the die has a sibling, skip to the sibling. */
5893
5894 pdi = pdi->die_sibling;
5895 }
5896}
5897
5898/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5899
72bf9492 5900 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5901 name is concatenated with "::" and the partial DIE's name. For
5902 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5903 Enumerators are an exception; they use the scope of their parent
5904 enumeration type, i.e. the name of the enumeration type is not
5905 prepended to the enumerator.
91c24f0a 5906
72bf9492
DJ
5907 There are two complexities. One is DW_AT_specification; in this
5908 case "parent" means the parent of the target of the specification,
5909 instead of the direct parent of the DIE. The other is compilers
5910 which do not emit DW_TAG_namespace; in this case we try to guess
5911 the fully qualified name of structure types from their members'
5912 linkage names. This must be done using the DIE's children rather
5913 than the children of any DW_AT_specification target. We only need
5914 to do this for structures at the top level, i.e. if the target of
5915 any DW_AT_specification (if any; otherwise the DIE itself) does not
5916 have a parent. */
5917
5918/* Compute the scope prefix associated with PDI's parent, in
5919 compilation unit CU. The result will be allocated on CU's
5920 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5921 field. NULL is returned if no prefix is necessary. */
15d034d0 5922static const char *
72bf9492
DJ
5923partial_die_parent_scope (struct partial_die_info *pdi,
5924 struct dwarf2_cu *cu)
5925{
15d034d0 5926 const char *grandparent_scope;
72bf9492 5927 struct partial_die_info *parent, *real_pdi;
91c24f0a 5928
72bf9492
DJ
5929 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5930 then this means the parent of the specification DIE. */
5931
5932 real_pdi = pdi;
72bf9492 5933 while (real_pdi->has_specification)
36586728
TT
5934 real_pdi = find_partial_die (real_pdi->spec_offset,
5935 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5936
5937 parent = real_pdi->die_parent;
5938 if (parent == NULL)
5939 return NULL;
5940
5941 if (parent->scope_set)
5942 return parent->scope;
5943
5944 fixup_partial_die (parent, cu);
5945
10b3939b 5946 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5947
acebe513
UW
5948 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5949 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5950 Work around this problem here. */
5951 if (cu->language == language_cplus
6e70227d 5952 && parent->tag == DW_TAG_namespace
acebe513
UW
5953 && strcmp (parent->name, "::") == 0
5954 && grandparent_scope == NULL)
5955 {
5956 parent->scope = NULL;
5957 parent->scope_set = 1;
5958 return NULL;
5959 }
5960
9c6c53f7
SA
5961 if (pdi->tag == DW_TAG_enumerator)
5962 /* Enumerators should not get the name of the enumeration as a prefix. */
5963 parent->scope = grandparent_scope;
5964 else if (parent->tag == DW_TAG_namespace
f55ee35c 5965 || parent->tag == DW_TAG_module
72bf9492
DJ
5966 || parent->tag == DW_TAG_structure_type
5967 || parent->tag == DW_TAG_class_type
680b30c7 5968 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5969 || parent->tag == DW_TAG_union_type
5970 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5971 {
5972 if (grandparent_scope == NULL)
5973 parent->scope = parent->name;
5974 else
3e43a32a
MS
5975 parent->scope = typename_concat (&cu->comp_unit_obstack,
5976 grandparent_scope,
f55ee35c 5977 parent->name, 0, cu);
72bf9492 5978 }
72bf9492
DJ
5979 else
5980 {
5981 /* FIXME drow/2004-04-01: What should we be doing with
5982 function-local names? For partial symbols, we should probably be
5983 ignoring them. */
5984 complaint (&symfile_complaints,
e2e0b3e5 5985 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5986 parent->tag, pdi->offset.sect_off);
72bf9492 5987 parent->scope = grandparent_scope;
c906108c
SS
5988 }
5989
72bf9492
DJ
5990 parent->scope_set = 1;
5991 return parent->scope;
5992}
5993
5994/* Return the fully scoped name associated with PDI, from compilation unit
5995 CU. The result will be allocated with malloc. */
4568ecf9 5996
72bf9492
DJ
5997static char *
5998partial_die_full_name (struct partial_die_info *pdi,
5999 struct dwarf2_cu *cu)
6000{
15d034d0 6001 const char *parent_scope;
72bf9492 6002
98bfdba5
PA
6003 /* If this is a template instantiation, we can not work out the
6004 template arguments from partial DIEs. So, unfortunately, we have
6005 to go through the full DIEs. At least any work we do building
6006 types here will be reused if full symbols are loaded later. */
6007 if (pdi->has_template_arguments)
6008 {
6009 fixup_partial_die (pdi, cu);
6010
6011 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6012 {
6013 struct die_info *die;
6014 struct attribute attr;
6015 struct dwarf2_cu *ref_cu = cu;
6016
b64f50a1 6017 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6018 attr.name = 0;
6019 attr.form = DW_FORM_ref_addr;
4568ecf9 6020 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6021 die = follow_die_ref (NULL, &attr, &ref_cu);
6022
6023 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6024 }
6025 }
6026
72bf9492
DJ
6027 parent_scope = partial_die_parent_scope (pdi, cu);
6028 if (parent_scope == NULL)
6029 return NULL;
6030 else
f55ee35c 6031 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6032}
6033
6034static void
72bf9492 6035add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6036{
e7c27a73 6037 struct objfile *objfile = cu->objfile;
c906108c 6038 CORE_ADDR addr = 0;
15d034d0 6039 const char *actual_name = NULL;
e142c38c 6040 CORE_ADDR baseaddr;
15d034d0 6041 char *built_actual_name;
e142c38c
DJ
6042
6043 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6044
15d034d0
TT
6045 built_actual_name = partial_die_full_name (pdi, cu);
6046 if (built_actual_name != NULL)
6047 actual_name = built_actual_name;
63d06c5c 6048
72bf9492
DJ
6049 if (actual_name == NULL)
6050 actual_name = pdi->name;
6051
c906108c
SS
6052 switch (pdi->tag)
6053 {
6054 case DW_TAG_subprogram:
2cfa0c8d 6055 if (pdi->is_external || cu->language == language_ada)
c906108c 6056 {
2cfa0c8d
JB
6057 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6058 of the global scope. But in Ada, we want to be able to access
6059 nested procedures globally. So all Ada subprograms are stored
6060 in the global scope. */
f47fb265 6061 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6062 mst_text, objfile); */
f47fb265 6063 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6064 built_actual_name != NULL,
f47fb265
MS
6065 VAR_DOMAIN, LOC_BLOCK,
6066 &objfile->global_psymbols,
6067 0, pdi->lowpc + baseaddr,
6068 cu->language, objfile);
c906108c
SS
6069 }
6070 else
6071 {
f47fb265 6072 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6073 mst_file_text, objfile); */
f47fb265 6074 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6075 built_actual_name != NULL,
f47fb265
MS
6076 VAR_DOMAIN, LOC_BLOCK,
6077 &objfile->static_psymbols,
6078 0, pdi->lowpc + baseaddr,
6079 cu->language, objfile);
c906108c
SS
6080 }
6081 break;
72929c62
JB
6082 case DW_TAG_constant:
6083 {
6084 struct psymbol_allocation_list *list;
6085
6086 if (pdi->is_external)
6087 list = &objfile->global_psymbols;
6088 else
6089 list = &objfile->static_psymbols;
f47fb265 6090 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6091 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6092 list, 0, 0, cu->language, objfile);
72929c62
JB
6093 }
6094 break;
c906108c 6095 case DW_TAG_variable:
95554aad
TT
6096 if (pdi->d.locdesc)
6097 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6098
95554aad 6099 if (pdi->d.locdesc
caac4577
JG
6100 && addr == 0
6101 && !dwarf2_per_objfile->has_section_at_zero)
6102 {
6103 /* A global or static variable may also have been stripped
6104 out by the linker if unused, in which case its address
6105 will be nullified; do not add such variables into partial
6106 symbol table then. */
6107 }
6108 else if (pdi->is_external)
c906108c
SS
6109 {
6110 /* Global Variable.
6111 Don't enter into the minimal symbol tables as there is
6112 a minimal symbol table entry from the ELF symbols already.
6113 Enter into partial symbol table if it has a location
6114 descriptor or a type.
6115 If the location descriptor is missing, new_symbol will create
6116 a LOC_UNRESOLVED symbol, the address of the variable will then
6117 be determined from the minimal symbol table whenever the variable
6118 is referenced.
6119 The address for the partial symbol table entry is not
6120 used by GDB, but it comes in handy for debugging partial symbol
6121 table building. */
6122
95554aad 6123 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6124 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6125 built_actual_name != NULL,
f47fb265
MS
6126 VAR_DOMAIN, LOC_STATIC,
6127 &objfile->global_psymbols,
6128 0, addr + baseaddr,
6129 cu->language, objfile);
c906108c
SS
6130 }
6131 else
6132 {
0963b4bd 6133 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6134 if (pdi->d.locdesc == NULL)
decbce07 6135 {
15d034d0 6136 xfree (built_actual_name);
decbce07
MS
6137 return;
6138 }
f47fb265 6139 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6140 mst_file_data, objfile); */
f47fb265 6141 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6142 built_actual_name != NULL,
f47fb265
MS
6143 VAR_DOMAIN, LOC_STATIC,
6144 &objfile->static_psymbols,
6145 0, addr + baseaddr,
6146 cu->language, objfile);
c906108c
SS
6147 }
6148 break;
6149 case DW_TAG_typedef:
6150 case DW_TAG_base_type:
a02abb62 6151 case DW_TAG_subrange_type:
38d518c9 6152 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6153 built_actual_name != NULL,
176620f1 6154 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6155 &objfile->static_psymbols,
e142c38c 6156 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6157 break;
72bf9492
DJ
6158 case DW_TAG_namespace:
6159 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6160 built_actual_name != NULL,
72bf9492
DJ
6161 VAR_DOMAIN, LOC_TYPEDEF,
6162 &objfile->global_psymbols,
6163 0, (CORE_ADDR) 0, cu->language, objfile);
6164 break;
c906108c 6165 case DW_TAG_class_type:
680b30c7 6166 case DW_TAG_interface_type:
c906108c
SS
6167 case DW_TAG_structure_type:
6168 case DW_TAG_union_type:
6169 case DW_TAG_enumeration_type:
fa4028e9
JB
6170 /* Skip external references. The DWARF standard says in the section
6171 about "Structure, Union, and Class Type Entries": "An incomplete
6172 structure, union or class type is represented by a structure,
6173 union or class entry that does not have a byte size attribute
6174 and that has a DW_AT_declaration attribute." */
6175 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6176 {
15d034d0 6177 xfree (built_actual_name);
decbce07
MS
6178 return;
6179 }
fa4028e9 6180
63d06c5c
DC
6181 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6182 static vs. global. */
38d518c9 6183 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6184 built_actual_name != NULL,
176620f1 6185 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6186 (cu->language == language_cplus
6187 || cu->language == language_java)
63d06c5c
DC
6188 ? &objfile->global_psymbols
6189 : &objfile->static_psymbols,
e142c38c 6190 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6191
c906108c
SS
6192 break;
6193 case DW_TAG_enumerator:
38d518c9 6194 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6195 built_actual_name != NULL,
176620f1 6196 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6197 (cu->language == language_cplus
6198 || cu->language == language_java)
f6fe98ef
DJ
6199 ? &objfile->global_psymbols
6200 : &objfile->static_psymbols,
e142c38c 6201 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6202 break;
6203 default:
6204 break;
6205 }
5c4e30ca 6206
15d034d0 6207 xfree (built_actual_name);
c906108c
SS
6208}
6209
5c4e30ca
DC
6210/* Read a partial die corresponding to a namespace; also, add a symbol
6211 corresponding to that namespace to the symbol table. NAMESPACE is
6212 the name of the enclosing namespace. */
91c24f0a 6213
72bf9492
DJ
6214static void
6215add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6216 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6217 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6218{
72bf9492 6219 /* Add a symbol for the namespace. */
e7c27a73 6220
72bf9492 6221 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6222
6223 /* Now scan partial symbols in that namespace. */
6224
91c24f0a 6225 if (pdi->has_children)
5734ee8b 6226 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6227}
6228
5d7cb8df
JK
6229/* Read a partial die corresponding to a Fortran module. */
6230
6231static void
6232add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6233 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6234{
f55ee35c 6235 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6236
6237 if (pdi->has_children)
6238 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6239}
6240
bc30ff58
JB
6241/* Read a partial die corresponding to a subprogram and create a partial
6242 symbol for that subprogram. When the CU language allows it, this
6243 routine also defines a partial symbol for each nested subprogram
6244 that this subprogram contains.
6e70227d 6245
bc30ff58
JB
6246 DIE my also be a lexical block, in which case we simply search
6247 recursively for suprograms defined inside that lexical block.
6248 Again, this is only performed when the CU language allows this
6249 type of definitions. */
6250
6251static void
6252add_partial_subprogram (struct partial_die_info *pdi,
6253 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6254 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6255{
6256 if (pdi->tag == DW_TAG_subprogram)
6257 {
6258 if (pdi->has_pc_info)
6259 {
6260 if (pdi->lowpc < *lowpc)
6261 *lowpc = pdi->lowpc;
6262 if (pdi->highpc > *highpc)
6263 *highpc = pdi->highpc;
5734ee8b
DJ
6264 if (need_pc)
6265 {
6266 CORE_ADDR baseaddr;
6267 struct objfile *objfile = cu->objfile;
6268
6269 baseaddr = ANOFFSET (objfile->section_offsets,
6270 SECT_OFF_TEXT (objfile));
6271 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6272 pdi->lowpc + baseaddr,
6273 pdi->highpc - 1 + baseaddr,
9291a0cd 6274 cu->per_cu->v.psymtab);
5734ee8b 6275 }
481860b3
GB
6276 }
6277
6278 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6279 {
bc30ff58 6280 if (!pdi->is_declaration)
e8d05480
JB
6281 /* Ignore subprogram DIEs that do not have a name, they are
6282 illegal. Do not emit a complaint at this point, we will
6283 do so when we convert this psymtab into a symtab. */
6284 if (pdi->name)
6285 add_partial_symbol (pdi, cu);
bc30ff58
JB
6286 }
6287 }
6e70227d 6288
bc30ff58
JB
6289 if (! pdi->has_children)
6290 return;
6291
6292 if (cu->language == language_ada)
6293 {
6294 pdi = pdi->die_child;
6295 while (pdi != NULL)
6296 {
6297 fixup_partial_die (pdi, cu);
6298 if (pdi->tag == DW_TAG_subprogram
6299 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6300 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6301 pdi = pdi->die_sibling;
6302 }
6303 }
6304}
6305
91c24f0a
DC
6306/* Read a partial die corresponding to an enumeration type. */
6307
72bf9492
DJ
6308static void
6309add_partial_enumeration (struct partial_die_info *enum_pdi,
6310 struct dwarf2_cu *cu)
91c24f0a 6311{
72bf9492 6312 struct partial_die_info *pdi;
91c24f0a
DC
6313
6314 if (enum_pdi->name != NULL)
72bf9492
DJ
6315 add_partial_symbol (enum_pdi, cu);
6316
6317 pdi = enum_pdi->die_child;
6318 while (pdi)
91c24f0a 6319 {
72bf9492 6320 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6321 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6322 else
72bf9492
DJ
6323 add_partial_symbol (pdi, cu);
6324 pdi = pdi->die_sibling;
91c24f0a 6325 }
91c24f0a
DC
6326}
6327
6caca83c
CC
6328/* Return the initial uleb128 in the die at INFO_PTR. */
6329
6330static unsigned int
d521ce57 6331peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6332{
6333 unsigned int bytes_read;
6334
6335 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6336}
6337
4bb7a0a7
DJ
6338/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6339 Return the corresponding abbrev, or NULL if the number is zero (indicating
6340 an empty DIE). In either case *BYTES_READ will be set to the length of
6341 the initial number. */
6342
6343static struct abbrev_info *
d521ce57 6344peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6345 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6346{
6347 bfd *abfd = cu->objfile->obfd;
6348 unsigned int abbrev_number;
6349 struct abbrev_info *abbrev;
6350
6351 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6352
6353 if (abbrev_number == 0)
6354 return NULL;
6355
433df2d4 6356 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6357 if (!abbrev)
6358 {
3e43a32a
MS
6359 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6360 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6361 }
6362
6363 return abbrev;
6364}
6365
93311388
DE
6366/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6367 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6368 DIE. Any children of the skipped DIEs will also be skipped. */
6369
d521ce57
TT
6370static const gdb_byte *
6371skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6372{
dee91e82 6373 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6374 struct abbrev_info *abbrev;
6375 unsigned int bytes_read;
6376
6377 while (1)
6378 {
6379 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6380 if (abbrev == NULL)
6381 return info_ptr + bytes_read;
6382 else
dee91e82 6383 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6384 }
6385}
6386
93311388
DE
6387/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6388 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6389 abbrev corresponding to that skipped uleb128 should be passed in
6390 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6391 children. */
6392
d521ce57
TT
6393static const gdb_byte *
6394skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6395 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6396{
6397 unsigned int bytes_read;
6398 struct attribute attr;
dee91e82
DE
6399 bfd *abfd = reader->abfd;
6400 struct dwarf2_cu *cu = reader->cu;
d521ce57 6401 const gdb_byte *buffer = reader->buffer;
f664829e 6402 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6403 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6404 unsigned int form, i;
6405
6406 for (i = 0; i < abbrev->num_attrs; i++)
6407 {
6408 /* The only abbrev we care about is DW_AT_sibling. */
6409 if (abbrev->attrs[i].name == DW_AT_sibling)
6410 {
dee91e82 6411 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6412 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6413 complaint (&symfile_complaints,
6414 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6415 else
b64f50a1 6416 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6417 }
6418
6419 /* If it isn't DW_AT_sibling, skip this attribute. */
6420 form = abbrev->attrs[i].form;
6421 skip_attribute:
6422 switch (form)
6423 {
4bb7a0a7 6424 case DW_FORM_ref_addr:
ae411497
TT
6425 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6426 and later it is offset sized. */
6427 if (cu->header.version == 2)
6428 info_ptr += cu->header.addr_size;
6429 else
6430 info_ptr += cu->header.offset_size;
6431 break;
36586728
TT
6432 case DW_FORM_GNU_ref_alt:
6433 info_ptr += cu->header.offset_size;
6434 break;
ae411497 6435 case DW_FORM_addr:
4bb7a0a7
DJ
6436 info_ptr += cu->header.addr_size;
6437 break;
6438 case DW_FORM_data1:
6439 case DW_FORM_ref1:
6440 case DW_FORM_flag:
6441 info_ptr += 1;
6442 break;
2dc7f7b3
TT
6443 case DW_FORM_flag_present:
6444 break;
4bb7a0a7
DJ
6445 case DW_FORM_data2:
6446 case DW_FORM_ref2:
6447 info_ptr += 2;
6448 break;
6449 case DW_FORM_data4:
6450 case DW_FORM_ref4:
6451 info_ptr += 4;
6452 break;
6453 case DW_FORM_data8:
6454 case DW_FORM_ref8:
55f1336d 6455 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6456 info_ptr += 8;
6457 break;
6458 case DW_FORM_string:
9b1c24c8 6459 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6460 info_ptr += bytes_read;
6461 break;
2dc7f7b3 6462 case DW_FORM_sec_offset:
4bb7a0a7 6463 case DW_FORM_strp:
36586728 6464 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6465 info_ptr += cu->header.offset_size;
6466 break;
2dc7f7b3 6467 case DW_FORM_exprloc:
4bb7a0a7
DJ
6468 case DW_FORM_block:
6469 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6470 info_ptr += bytes_read;
6471 break;
6472 case DW_FORM_block1:
6473 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6474 break;
6475 case DW_FORM_block2:
6476 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6477 break;
6478 case DW_FORM_block4:
6479 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6480 break;
6481 case DW_FORM_sdata:
6482 case DW_FORM_udata:
6483 case DW_FORM_ref_udata:
3019eac3
DE
6484 case DW_FORM_GNU_addr_index:
6485 case DW_FORM_GNU_str_index:
d521ce57 6486 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6487 break;
6488 case DW_FORM_indirect:
6489 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6490 info_ptr += bytes_read;
6491 /* We need to continue parsing from here, so just go back to
6492 the top. */
6493 goto skip_attribute;
6494
6495 default:
3e43a32a
MS
6496 error (_("Dwarf Error: Cannot handle %s "
6497 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6498 dwarf_form_name (form),
6499 bfd_get_filename (abfd));
6500 }
6501 }
6502
6503 if (abbrev->has_children)
dee91e82 6504 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6505 else
6506 return info_ptr;
6507}
6508
93311388 6509/* Locate ORIG_PDI's sibling.
dee91e82 6510 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6511
d521ce57 6512static const gdb_byte *
dee91e82
DE
6513locate_pdi_sibling (const struct die_reader_specs *reader,
6514 struct partial_die_info *orig_pdi,
d521ce57 6515 const gdb_byte *info_ptr)
91c24f0a
DC
6516{
6517 /* Do we know the sibling already? */
72bf9492 6518
91c24f0a
DC
6519 if (orig_pdi->sibling)
6520 return orig_pdi->sibling;
6521
6522 /* Are there any children to deal with? */
6523
6524 if (!orig_pdi->has_children)
6525 return info_ptr;
6526
4bb7a0a7 6527 /* Skip the children the long way. */
91c24f0a 6528
dee91e82 6529 return skip_children (reader, info_ptr);
91c24f0a
DC
6530}
6531
257e7a09 6532/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6533 not NULL. */
c906108c
SS
6534
6535static void
257e7a09
YQ
6536dwarf2_read_symtab (struct partial_symtab *self,
6537 struct objfile *objfile)
c906108c 6538{
257e7a09 6539 if (self->readin)
c906108c 6540 {
442e4d9c 6541 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6542 self->filename);
442e4d9c
YQ
6543 }
6544 else
6545 {
6546 if (info_verbose)
c906108c 6547 {
442e4d9c 6548 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6549 self->filename);
442e4d9c 6550 gdb_flush (gdb_stdout);
c906108c 6551 }
c906108c 6552
442e4d9c
YQ
6553 /* Restore our global data. */
6554 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6555
442e4d9c
YQ
6556 /* If this psymtab is constructed from a debug-only objfile, the
6557 has_section_at_zero flag will not necessarily be correct. We
6558 can get the correct value for this flag by looking at the data
6559 associated with the (presumably stripped) associated objfile. */
6560 if (objfile->separate_debug_objfile_backlink)
6561 {
6562 struct dwarf2_per_objfile *dpo_backlink
6563 = objfile_data (objfile->separate_debug_objfile_backlink,
6564 dwarf2_objfile_data_key);
9a619af0 6565
442e4d9c
YQ
6566 dwarf2_per_objfile->has_section_at_zero
6567 = dpo_backlink->has_section_at_zero;
6568 }
b2ab525c 6569
442e4d9c 6570 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6571
257e7a09 6572 psymtab_to_symtab_1 (self);
c906108c 6573
442e4d9c
YQ
6574 /* Finish up the debug error message. */
6575 if (info_verbose)
6576 printf_filtered (_("done.\n"));
c906108c 6577 }
95554aad
TT
6578
6579 process_cu_includes ();
c906108c 6580}
9cdd5dbd
DE
6581\f
6582/* Reading in full CUs. */
c906108c 6583
10b3939b
DJ
6584/* Add PER_CU to the queue. */
6585
6586static void
95554aad
TT
6587queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6588 enum language pretend_language)
10b3939b
DJ
6589{
6590 struct dwarf2_queue_item *item;
6591
6592 per_cu->queued = 1;
6593 item = xmalloc (sizeof (*item));
6594 item->per_cu = per_cu;
95554aad 6595 item->pretend_language = pretend_language;
10b3939b
DJ
6596 item->next = NULL;
6597
6598 if (dwarf2_queue == NULL)
6599 dwarf2_queue = item;
6600 else
6601 dwarf2_queue_tail->next = item;
6602
6603 dwarf2_queue_tail = item;
6604}
6605
0907af0c
DE
6606/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6607 unit and add it to our queue.
6608 The result is non-zero if PER_CU was queued, otherwise the result is zero
6609 meaning either PER_CU is already queued or it is already loaded. */
6610
6611static int
6612maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6613 struct dwarf2_per_cu_data *per_cu,
6614 enum language pretend_language)
6615{
6616 /* We may arrive here during partial symbol reading, if we need full
6617 DIEs to process an unusual case (e.g. template arguments). Do
6618 not queue PER_CU, just tell our caller to load its DIEs. */
6619 if (dwarf2_per_objfile->reading_partial_symbols)
6620 {
6621 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6622 return 1;
6623 return 0;
6624 }
6625
6626 /* Mark the dependence relation so that we don't flush PER_CU
6627 too early. */
6628 dwarf2_add_dependence (this_cu, per_cu);
6629
6630 /* If it's already on the queue, we have nothing to do. */
6631 if (per_cu->queued)
6632 return 0;
6633
6634 /* If the compilation unit is already loaded, just mark it as
6635 used. */
6636 if (per_cu->cu != NULL)
6637 {
6638 per_cu->cu->last_used = 0;
6639 return 0;
6640 }
6641
6642 /* Add it to the queue. */
6643 queue_comp_unit (per_cu, pretend_language);
6644
6645 return 1;
6646}
6647
10b3939b
DJ
6648/* Process the queue. */
6649
6650static void
a0f42c21 6651process_queue (void)
10b3939b
DJ
6652{
6653 struct dwarf2_queue_item *item, *next_item;
6654
45cfd468
DE
6655 if (dwarf2_read_debug)
6656 {
6657 fprintf_unfiltered (gdb_stdlog,
6658 "Expanding one or more symtabs of objfile %s ...\n",
6659 dwarf2_per_objfile->objfile->name);
6660 }
6661
03dd20cc
DJ
6662 /* The queue starts out with one item, but following a DIE reference
6663 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6664 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6665 {
9291a0cd
TT
6666 if (dwarf2_per_objfile->using_index
6667 ? !item->per_cu->v.quick->symtab
6668 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6669 {
6670 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6671
6672 if (dwarf2_read_debug)
6673 {
6674 fprintf_unfiltered (gdb_stdlog,
6675 "Expanding symtab of %s at offset 0x%x\n",
6676 per_cu->is_debug_types ? "TU" : "CU",
6677 per_cu->offset.sect_off);
6678 }
6679
6680 if (per_cu->is_debug_types)
6681 process_full_type_unit (per_cu, item->pretend_language);
6682 else
6683 process_full_comp_unit (per_cu, item->pretend_language);
6684
6685 if (dwarf2_read_debug)
6686 {
6687 fprintf_unfiltered (gdb_stdlog,
6688 "Done expanding %s at offset 0x%x\n",
6689 per_cu->is_debug_types ? "TU" : "CU",
6690 per_cu->offset.sect_off);
6691 }
6692 }
10b3939b
DJ
6693
6694 item->per_cu->queued = 0;
6695 next_item = item->next;
6696 xfree (item);
6697 }
6698
6699 dwarf2_queue_tail = NULL;
45cfd468
DE
6700
6701 if (dwarf2_read_debug)
6702 {
6703 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6704 dwarf2_per_objfile->objfile->name);
6705 }
10b3939b
DJ
6706}
6707
6708/* Free all allocated queue entries. This function only releases anything if
6709 an error was thrown; if the queue was processed then it would have been
6710 freed as we went along. */
6711
6712static void
6713dwarf2_release_queue (void *dummy)
6714{
6715 struct dwarf2_queue_item *item, *last;
6716
6717 item = dwarf2_queue;
6718 while (item)
6719 {
6720 /* Anything still marked queued is likely to be in an
6721 inconsistent state, so discard it. */
6722 if (item->per_cu->queued)
6723 {
6724 if (item->per_cu->cu != NULL)
dee91e82 6725 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6726 item->per_cu->queued = 0;
6727 }
6728
6729 last = item;
6730 item = item->next;
6731 xfree (last);
6732 }
6733
6734 dwarf2_queue = dwarf2_queue_tail = NULL;
6735}
6736
6737/* Read in full symbols for PST, and anything it depends on. */
6738
c906108c 6739static void
fba45db2 6740psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6741{
10b3939b 6742 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6743 int i;
6744
95554aad
TT
6745 if (pst->readin)
6746 return;
6747
aaa75496 6748 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6749 if (!pst->dependencies[i]->readin
6750 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6751 {
6752 /* Inform about additional files that need to be read in. */
6753 if (info_verbose)
6754 {
a3f17187 6755 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6756 fputs_filtered (" ", gdb_stdout);
6757 wrap_here ("");
6758 fputs_filtered ("and ", gdb_stdout);
6759 wrap_here ("");
6760 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6761 wrap_here (""); /* Flush output. */
aaa75496
JB
6762 gdb_flush (gdb_stdout);
6763 }
6764 psymtab_to_symtab_1 (pst->dependencies[i]);
6765 }
6766
e38df1d0 6767 per_cu = pst->read_symtab_private;
10b3939b
DJ
6768
6769 if (per_cu == NULL)
aaa75496
JB
6770 {
6771 /* It's an include file, no symbols to read for it.
6772 Everything is in the parent symtab. */
6773 pst->readin = 1;
6774 return;
6775 }
c906108c 6776
a0f42c21 6777 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6778}
6779
dee91e82
DE
6780/* Trivial hash function for die_info: the hash value of a DIE
6781 is its offset in .debug_info for this objfile. */
10b3939b 6782
dee91e82
DE
6783static hashval_t
6784die_hash (const void *item)
10b3939b 6785{
dee91e82 6786 const struct die_info *die = item;
6502dd73 6787
dee91e82
DE
6788 return die->offset.sect_off;
6789}
63d06c5c 6790
dee91e82
DE
6791/* Trivial comparison function for die_info structures: two DIEs
6792 are equal if they have the same offset. */
98bfdba5 6793
dee91e82
DE
6794static int
6795die_eq (const void *item_lhs, const void *item_rhs)
6796{
6797 const struct die_info *die_lhs = item_lhs;
6798 const struct die_info *die_rhs = item_rhs;
c906108c 6799
dee91e82
DE
6800 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6801}
c906108c 6802
dee91e82
DE
6803/* die_reader_func for load_full_comp_unit.
6804 This is identical to read_signatured_type_reader,
6805 but is kept separate for now. */
c906108c 6806
dee91e82
DE
6807static void
6808load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6809 const gdb_byte *info_ptr,
dee91e82
DE
6810 struct die_info *comp_unit_die,
6811 int has_children,
6812 void *data)
6813{
6814 struct dwarf2_cu *cu = reader->cu;
95554aad 6815 enum language *language_ptr = data;
6caca83c 6816
dee91e82
DE
6817 gdb_assert (cu->die_hash == NULL);
6818 cu->die_hash =
6819 htab_create_alloc_ex (cu->header.length / 12,
6820 die_hash,
6821 die_eq,
6822 NULL,
6823 &cu->comp_unit_obstack,
6824 hashtab_obstack_allocate,
6825 dummy_obstack_deallocate);
e142c38c 6826
dee91e82
DE
6827 if (has_children)
6828 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6829 &info_ptr, comp_unit_die);
6830 cu->dies = comp_unit_die;
6831 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6832
6833 /* We try not to read any attributes in this function, because not
9cdd5dbd 6834 all CUs needed for references have been loaded yet, and symbol
10b3939b 6835 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6836 or we won't be able to build types correctly.
6837 Similarly, if we do not read the producer, we can not apply
6838 producer-specific interpretation. */
95554aad 6839 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6840}
10b3939b 6841
dee91e82 6842/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6843
dee91e82 6844static void
95554aad
TT
6845load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6846 enum language pretend_language)
dee91e82 6847{
3019eac3 6848 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6849
f4dc4d17
DE
6850 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6851 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6852}
6853
3da10d80
KS
6854/* Add a DIE to the delayed physname list. */
6855
6856static void
6857add_to_method_list (struct type *type, int fnfield_index, int index,
6858 const char *name, struct die_info *die,
6859 struct dwarf2_cu *cu)
6860{
6861 struct delayed_method_info mi;
6862 mi.type = type;
6863 mi.fnfield_index = fnfield_index;
6864 mi.index = index;
6865 mi.name = name;
6866 mi.die = die;
6867 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6868}
6869
6870/* A cleanup for freeing the delayed method list. */
6871
6872static void
6873free_delayed_list (void *ptr)
6874{
6875 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6876 if (cu->method_list != NULL)
6877 {
6878 VEC_free (delayed_method_info, cu->method_list);
6879 cu->method_list = NULL;
6880 }
6881}
6882
6883/* Compute the physnames of any methods on the CU's method list.
6884
6885 The computation of method physnames is delayed in order to avoid the
6886 (bad) condition that one of the method's formal parameters is of an as yet
6887 incomplete type. */
6888
6889static void
6890compute_delayed_physnames (struct dwarf2_cu *cu)
6891{
6892 int i;
6893 struct delayed_method_info *mi;
6894 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6895 {
1d06ead6 6896 const char *physname;
3da10d80
KS
6897 struct fn_fieldlist *fn_flp
6898 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6899 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6900 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6901 }
6902}
6903
a766d390
DE
6904/* Go objects should be embedded in a DW_TAG_module DIE,
6905 and it's not clear if/how imported objects will appear.
6906 To keep Go support simple until that's worked out,
6907 go back through what we've read and create something usable.
6908 We could do this while processing each DIE, and feels kinda cleaner,
6909 but that way is more invasive.
6910 This is to, for example, allow the user to type "p var" or "b main"
6911 without having to specify the package name, and allow lookups
6912 of module.object to work in contexts that use the expression
6913 parser. */
6914
6915static void
6916fixup_go_packaging (struct dwarf2_cu *cu)
6917{
6918 char *package_name = NULL;
6919 struct pending *list;
6920 int i;
6921
6922 for (list = global_symbols; list != NULL; list = list->next)
6923 {
6924 for (i = 0; i < list->nsyms; ++i)
6925 {
6926 struct symbol *sym = list->symbol[i];
6927
6928 if (SYMBOL_LANGUAGE (sym) == language_go
6929 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6930 {
6931 char *this_package_name = go_symbol_package_name (sym);
6932
6933 if (this_package_name == NULL)
6934 continue;
6935 if (package_name == NULL)
6936 package_name = this_package_name;
6937 else
6938 {
6939 if (strcmp (package_name, this_package_name) != 0)
6940 complaint (&symfile_complaints,
6941 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6942 (SYMBOL_SYMTAB (sym)
05cba821 6943 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6944 : cu->objfile->name),
6945 this_package_name, package_name);
6946 xfree (this_package_name);
6947 }
6948 }
6949 }
6950 }
6951
6952 if (package_name != NULL)
6953 {
6954 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6955 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6956 package_name,
6957 strlen (package_name));
a766d390 6958 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6959 saved_package_name, objfile);
a766d390
DE
6960 struct symbol *sym;
6961
6962 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6963
e623cf5d 6964 sym = allocate_symbol (objfile);
f85f34ed 6965 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
6966 SYMBOL_SET_NAMES (sym, saved_package_name,
6967 strlen (saved_package_name), 0, objfile);
a766d390
DE
6968 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6969 e.g., "main" finds the "main" module and not C's main(). */
6970 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6971 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6972 SYMBOL_TYPE (sym) = type;
6973
6974 add_symbol_to_list (sym, &global_symbols);
6975
6976 xfree (package_name);
6977 }
6978}
6979
95554aad
TT
6980/* Return the symtab for PER_CU. This works properly regardless of
6981 whether we're using the index or psymtabs. */
6982
6983static struct symtab *
6984get_symtab (struct dwarf2_per_cu_data *per_cu)
6985{
6986 return (dwarf2_per_objfile->using_index
6987 ? per_cu->v.quick->symtab
6988 : per_cu->v.psymtab->symtab);
6989}
6990
6991/* A helper function for computing the list of all symbol tables
6992 included by PER_CU. */
6993
6994static void
6995recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6996 htab_t all_children,
6997 struct dwarf2_per_cu_data *per_cu)
6998{
6999 void **slot;
7000 int ix;
7001 struct dwarf2_per_cu_data *iter;
7002
7003 slot = htab_find_slot (all_children, per_cu, INSERT);
7004 if (*slot != NULL)
7005 {
7006 /* This inclusion and its children have been processed. */
7007 return;
7008 }
7009
7010 *slot = per_cu;
7011 /* Only add a CU if it has a symbol table. */
7012 if (get_symtab (per_cu) != NULL)
7013 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7014
7015 for (ix = 0;
796a7ff8 7016 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
7017 ++ix)
7018 recursively_compute_inclusions (result, all_children, iter);
7019}
7020
7021/* Compute the symtab 'includes' fields for the symtab related to
7022 PER_CU. */
7023
7024static void
7025compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7026{
f4dc4d17
DE
7027 gdb_assert (! per_cu->is_debug_types);
7028
796a7ff8 7029 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7030 {
7031 int ix, len;
7032 struct dwarf2_per_cu_data *iter;
7033 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7034 htab_t all_children;
7035 struct symtab *symtab = get_symtab (per_cu);
7036
7037 /* If we don't have a symtab, we can just skip this case. */
7038 if (symtab == NULL)
7039 return;
7040
7041 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7042 NULL, xcalloc, xfree);
7043
7044 for (ix = 0;
796a7ff8 7045 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7046 ix, iter);
7047 ++ix)
7048 recursively_compute_inclusions (&result_children, all_children, iter);
7049
796a7ff8
DE
7050 /* Now we have a transitive closure of all the included CUs, and
7051 for .gdb_index version 7 the included TUs, so we can convert it
7052 to a list of symtabs. */
95554aad
TT
7053 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7054 symtab->includes
7055 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7056 (len + 1) * sizeof (struct symtab *));
7057 for (ix = 0;
7058 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7059 ++ix)
7060 symtab->includes[ix] = get_symtab (iter);
7061 symtab->includes[len] = NULL;
7062
7063 VEC_free (dwarf2_per_cu_ptr, result_children);
7064 htab_delete (all_children);
7065 }
7066}
7067
7068/* Compute the 'includes' field for the symtabs of all the CUs we just
7069 read. */
7070
7071static void
7072process_cu_includes (void)
7073{
7074 int ix;
7075 struct dwarf2_per_cu_data *iter;
7076
7077 for (ix = 0;
7078 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7079 ix, iter);
7080 ++ix)
f4dc4d17
DE
7081 {
7082 if (! iter->is_debug_types)
7083 compute_symtab_includes (iter);
7084 }
95554aad
TT
7085
7086 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7087}
7088
9cdd5dbd 7089/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7090 already been loaded into memory. */
7091
7092static void
95554aad
TT
7093process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7094 enum language pretend_language)
10b3939b 7095{
10b3939b 7096 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7097 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7098 CORE_ADDR lowpc, highpc;
7099 struct symtab *symtab;
3da10d80 7100 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7101 CORE_ADDR baseaddr;
4359dff1 7102 struct block *static_block;
10b3939b
DJ
7103
7104 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7105
10b3939b
DJ
7106 buildsym_init ();
7107 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7108 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7109
7110 cu->list_in_scope = &file_symbols;
c906108c 7111
95554aad
TT
7112 cu->language = pretend_language;
7113 cu->language_defn = language_def (cu->language);
7114
c906108c 7115 /* Do line number decoding in read_file_scope () */
10b3939b 7116 process_die (cu->dies, cu);
c906108c 7117
a766d390
DE
7118 /* For now fudge the Go package. */
7119 if (cu->language == language_go)
7120 fixup_go_packaging (cu);
7121
3da10d80
KS
7122 /* Now that we have processed all the DIEs in the CU, all the types
7123 should be complete, and it should now be safe to compute all of the
7124 physnames. */
7125 compute_delayed_physnames (cu);
7126 do_cleanups (delayed_list_cleanup);
7127
fae299cd
DC
7128 /* Some compilers don't define a DW_AT_high_pc attribute for the
7129 compilation unit. If the DW_AT_high_pc is missing, synthesize
7130 it, by scanning the DIE's below the compilation unit. */
10b3939b 7131 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7132
36586728
TT
7133 static_block
7134 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7135 per_cu->imported_symtabs != NULL);
4359dff1
JK
7136
7137 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7138 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7139 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7140 addrmap to help ensure it has an accurate map of pc values belonging to
7141 this comp unit. */
7142 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7143
7144 symtab = end_symtab_from_static_block (static_block, objfile,
7145 SECT_OFF_TEXT (objfile), 0);
c906108c 7146
8be455d7 7147 if (symtab != NULL)
c906108c 7148 {
df15bd07 7149 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7150
8be455d7
JK
7151 /* Set symtab language to language from DW_AT_language. If the
7152 compilation is from a C file generated by language preprocessors, do
7153 not set the language if it was already deduced by start_subfile. */
7154 if (!(cu->language == language_c && symtab->language != language_c))
7155 symtab->language = cu->language;
7156
7157 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7158 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7159 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7160 there were bugs in prologue debug info, fixed later in GCC-4.5
7161 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7162
7163 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7164 needed, it would be wrong due to missing DW_AT_producer there.
7165
7166 Still one can confuse GDB by using non-standard GCC compilation
7167 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7168 */
ab260dad 7169 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7170 symtab->locations_valid = 1;
e0d00bc7
JK
7171
7172 if (gcc_4_minor >= 5)
7173 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7174
7175 symtab->call_site_htab = cu->call_site_htab;
c906108c 7176 }
9291a0cd
TT
7177
7178 if (dwarf2_per_objfile->using_index)
7179 per_cu->v.quick->symtab = symtab;
7180 else
7181 {
7182 struct partial_symtab *pst = per_cu->v.psymtab;
7183 pst->symtab = symtab;
7184 pst->readin = 1;
7185 }
c906108c 7186
95554aad
TT
7187 /* Push it for inclusion processing later. */
7188 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7189
c906108c 7190 do_cleanups (back_to);
f4dc4d17 7191}
45cfd468 7192
f4dc4d17
DE
7193/* Generate full symbol information for type unit PER_CU, whose DIEs have
7194 already been loaded into memory. */
7195
7196static void
7197process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7198 enum language pretend_language)
7199{
7200 struct dwarf2_cu *cu = per_cu->cu;
7201 struct objfile *objfile = per_cu->objfile;
7202 struct symtab *symtab;
7203 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7204 struct signatured_type *sig_type;
7205
7206 gdb_assert (per_cu->is_debug_types);
7207 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7208
7209 buildsym_init ();
7210 back_to = make_cleanup (really_free_pendings, NULL);
7211 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7212
7213 cu->list_in_scope = &file_symbols;
7214
7215 cu->language = pretend_language;
7216 cu->language_defn = language_def (cu->language);
7217
7218 /* The symbol tables are set up in read_type_unit_scope. */
7219 process_die (cu->dies, cu);
7220
7221 /* For now fudge the Go package. */
7222 if (cu->language == language_go)
7223 fixup_go_packaging (cu);
7224
7225 /* Now that we have processed all the DIEs in the CU, all the types
7226 should be complete, and it should now be safe to compute all of the
7227 physnames. */
7228 compute_delayed_physnames (cu);
7229 do_cleanups (delayed_list_cleanup);
7230
7231 /* TUs share symbol tables.
7232 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7233 of it with end_expandable_symtab. Otherwise, complete the addition of
7234 this TU's symbols to the existing symtab. */
0186c6a7 7235 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7236 {
f4dc4d17 7237 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7238 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7239
7240 if (symtab != NULL)
7241 {
7242 /* Set symtab language to language from DW_AT_language. If the
7243 compilation is from a C file generated by language preprocessors,
7244 do not set the language if it was already deduced by
7245 start_subfile. */
7246 if (!(cu->language == language_c && symtab->language != language_c))
7247 symtab->language = cu->language;
7248 }
7249 }
7250 else
7251 {
7252 augment_type_symtab (objfile,
0186c6a7
DE
7253 sig_type->type_unit_group->primary_symtab);
7254 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7255 }
7256
7257 if (dwarf2_per_objfile->using_index)
7258 per_cu->v.quick->symtab = symtab;
7259 else
7260 {
7261 struct partial_symtab *pst = per_cu->v.psymtab;
7262 pst->symtab = symtab;
7263 pst->readin = 1;
45cfd468 7264 }
f4dc4d17
DE
7265
7266 do_cleanups (back_to);
c906108c
SS
7267}
7268
95554aad
TT
7269/* Process an imported unit DIE. */
7270
7271static void
7272process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7273{
7274 struct attribute *attr;
7275
f4dc4d17
DE
7276 /* For now we don't handle imported units in type units. */
7277 if (cu->per_cu->is_debug_types)
7278 {
7279 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7280 " supported in type units [in module %s]"),
7281 cu->objfile->name);
7282 }
7283
95554aad
TT
7284 attr = dwarf2_attr (die, DW_AT_import, cu);
7285 if (attr != NULL)
7286 {
7287 struct dwarf2_per_cu_data *per_cu;
7288 struct symtab *imported_symtab;
7289 sect_offset offset;
36586728 7290 int is_dwz;
95554aad
TT
7291
7292 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7293 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7294 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7295
7296 /* Queue the unit, if needed. */
7297 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7298 load_full_comp_unit (per_cu, cu->language);
7299
796a7ff8 7300 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7301 per_cu);
7302 }
7303}
7304
c906108c
SS
7305/* Process a die and its children. */
7306
7307static void
e7c27a73 7308process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7309{
7310 switch (die->tag)
7311 {
7312 case DW_TAG_padding:
7313 break;
7314 case DW_TAG_compile_unit:
95554aad 7315 case DW_TAG_partial_unit:
e7c27a73 7316 read_file_scope (die, cu);
c906108c 7317 break;
348e048f
DE
7318 case DW_TAG_type_unit:
7319 read_type_unit_scope (die, cu);
7320 break;
c906108c 7321 case DW_TAG_subprogram:
c906108c 7322 case DW_TAG_inlined_subroutine:
edb3359d 7323 read_func_scope (die, cu);
c906108c
SS
7324 break;
7325 case DW_TAG_lexical_block:
14898363
L
7326 case DW_TAG_try_block:
7327 case DW_TAG_catch_block:
e7c27a73 7328 read_lexical_block_scope (die, cu);
c906108c 7329 break;
96408a79
SA
7330 case DW_TAG_GNU_call_site:
7331 read_call_site_scope (die, cu);
7332 break;
c906108c 7333 case DW_TAG_class_type:
680b30c7 7334 case DW_TAG_interface_type:
c906108c
SS
7335 case DW_TAG_structure_type:
7336 case DW_TAG_union_type:
134d01f1 7337 process_structure_scope (die, cu);
c906108c
SS
7338 break;
7339 case DW_TAG_enumeration_type:
134d01f1 7340 process_enumeration_scope (die, cu);
c906108c 7341 break;
134d01f1 7342
f792889a
DJ
7343 /* These dies have a type, but processing them does not create
7344 a symbol or recurse to process the children. Therefore we can
7345 read them on-demand through read_type_die. */
c906108c 7346 case DW_TAG_subroutine_type:
72019c9c 7347 case DW_TAG_set_type:
c906108c 7348 case DW_TAG_array_type:
c906108c 7349 case DW_TAG_pointer_type:
c906108c 7350 case DW_TAG_ptr_to_member_type:
c906108c 7351 case DW_TAG_reference_type:
c906108c 7352 case DW_TAG_string_type:
c906108c 7353 break;
134d01f1 7354
c906108c 7355 case DW_TAG_base_type:
a02abb62 7356 case DW_TAG_subrange_type:
cb249c71 7357 case DW_TAG_typedef:
134d01f1
DJ
7358 /* Add a typedef symbol for the type definition, if it has a
7359 DW_AT_name. */
f792889a 7360 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7361 break;
c906108c 7362 case DW_TAG_common_block:
e7c27a73 7363 read_common_block (die, cu);
c906108c
SS
7364 break;
7365 case DW_TAG_common_inclusion:
7366 break;
d9fa45fe 7367 case DW_TAG_namespace:
4d4ec4e5 7368 cu->processing_has_namespace_info = 1;
e7c27a73 7369 read_namespace (die, cu);
d9fa45fe 7370 break;
5d7cb8df 7371 case DW_TAG_module:
4d4ec4e5 7372 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7373 read_module (die, cu);
7374 break;
d9fa45fe
DC
7375 case DW_TAG_imported_declaration:
7376 case DW_TAG_imported_module:
4d4ec4e5 7377 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7378 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7379 || cu->language != language_fortran))
7380 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7381 dwarf_tag_name (die->tag));
7382 read_import_statement (die, cu);
d9fa45fe 7383 break;
95554aad
TT
7384
7385 case DW_TAG_imported_unit:
7386 process_imported_unit_die (die, cu);
7387 break;
7388
c906108c 7389 default:
e7c27a73 7390 new_symbol (die, NULL, cu);
c906108c
SS
7391 break;
7392 }
7393}
ca69b9e6
DE
7394\f
7395/* DWARF name computation. */
c906108c 7396
94af9270
KS
7397/* A helper function for dwarf2_compute_name which determines whether DIE
7398 needs to have the name of the scope prepended to the name listed in the
7399 die. */
7400
7401static int
7402die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7403{
1c809c68
TT
7404 struct attribute *attr;
7405
94af9270
KS
7406 switch (die->tag)
7407 {
7408 case DW_TAG_namespace:
7409 case DW_TAG_typedef:
7410 case DW_TAG_class_type:
7411 case DW_TAG_interface_type:
7412 case DW_TAG_structure_type:
7413 case DW_TAG_union_type:
7414 case DW_TAG_enumeration_type:
7415 case DW_TAG_enumerator:
7416 case DW_TAG_subprogram:
7417 case DW_TAG_member:
7418 return 1;
7419
7420 case DW_TAG_variable:
c2b0a229 7421 case DW_TAG_constant:
94af9270
KS
7422 /* We only need to prefix "globally" visible variables. These include
7423 any variable marked with DW_AT_external or any variable that
7424 lives in a namespace. [Variables in anonymous namespaces
7425 require prefixing, but they are not DW_AT_external.] */
7426
7427 if (dwarf2_attr (die, DW_AT_specification, cu))
7428 {
7429 struct dwarf2_cu *spec_cu = cu;
9a619af0 7430
94af9270
KS
7431 return die_needs_namespace (die_specification (die, &spec_cu),
7432 spec_cu);
7433 }
7434
1c809c68 7435 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7436 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7437 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7438 return 0;
7439 /* A variable in a lexical block of some kind does not need a
7440 namespace, even though in C++ such variables may be external
7441 and have a mangled name. */
7442 if (die->parent->tag == DW_TAG_lexical_block
7443 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7444 || die->parent->tag == DW_TAG_catch_block
7445 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7446 return 0;
7447 return 1;
94af9270
KS
7448
7449 default:
7450 return 0;
7451 }
7452}
7453
98bfdba5
PA
7454/* Retrieve the last character from a mem_file. */
7455
7456static void
7457do_ui_file_peek_last (void *object, const char *buffer, long length)
7458{
7459 char *last_char_p = (char *) object;
7460
7461 if (length > 0)
7462 *last_char_p = buffer[length - 1];
7463}
7464
94af9270 7465/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7466 compute the physname for the object, which include a method's:
7467 - formal parameters (C++/Java),
7468 - receiver type (Go),
7469 - return type (Java).
7470
7471 The term "physname" is a bit confusing.
7472 For C++, for example, it is the demangled name.
7473 For Go, for example, it's the mangled name.
94af9270 7474
af6b7be1
JB
7475 For Ada, return the DIE's linkage name rather than the fully qualified
7476 name. PHYSNAME is ignored..
7477
94af9270
KS
7478 The result is allocated on the objfile_obstack and canonicalized. */
7479
7480static const char *
15d034d0
TT
7481dwarf2_compute_name (const char *name,
7482 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7483 int physname)
7484{
bb5ed363
DE
7485 struct objfile *objfile = cu->objfile;
7486
94af9270
KS
7487 if (name == NULL)
7488 name = dwarf2_name (die, cu);
7489
f55ee35c
JK
7490 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7491 compute it by typename_concat inside GDB. */
7492 if (cu->language == language_ada
7493 || (cu->language == language_fortran && physname))
7494 {
7495 /* For Ada unit, we prefer the linkage name over the name, as
7496 the former contains the exported name, which the user expects
7497 to be able to reference. Ideally, we want the user to be able
7498 to reference this entity using either natural or linkage name,
7499 but we haven't started looking at this enhancement yet. */
7500 struct attribute *attr;
7501
7502 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7503 if (attr == NULL)
7504 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7505 if (attr && DW_STRING (attr))
7506 return DW_STRING (attr);
7507 }
7508
94af9270
KS
7509 /* These are the only languages we know how to qualify names in. */
7510 if (name != NULL
f55ee35c
JK
7511 && (cu->language == language_cplus || cu->language == language_java
7512 || cu->language == language_fortran))
94af9270
KS
7513 {
7514 if (die_needs_namespace (die, cu))
7515 {
7516 long length;
0d5cff50 7517 const char *prefix;
94af9270
KS
7518 struct ui_file *buf;
7519
7520 prefix = determine_prefix (die, cu);
7521 buf = mem_fileopen ();
7522 if (*prefix != '\0')
7523 {
f55ee35c
JK
7524 char *prefixed_name = typename_concat (NULL, prefix, name,
7525 physname, cu);
9a619af0 7526
94af9270
KS
7527 fputs_unfiltered (prefixed_name, buf);
7528 xfree (prefixed_name);
7529 }
7530 else
62d5b8da 7531 fputs_unfiltered (name, buf);
94af9270 7532
98bfdba5
PA
7533 /* Template parameters may be specified in the DIE's DW_AT_name, or
7534 as children with DW_TAG_template_type_param or
7535 DW_TAG_value_type_param. If the latter, add them to the name
7536 here. If the name already has template parameters, then
7537 skip this step; some versions of GCC emit both, and
7538 it is more efficient to use the pre-computed name.
7539
7540 Something to keep in mind about this process: it is very
7541 unlikely, or in some cases downright impossible, to produce
7542 something that will match the mangled name of a function.
7543 If the definition of the function has the same debug info,
7544 we should be able to match up with it anyway. But fallbacks
7545 using the minimal symbol, for instance to find a method
7546 implemented in a stripped copy of libstdc++, will not work.
7547 If we do not have debug info for the definition, we will have to
7548 match them up some other way.
7549
7550 When we do name matching there is a related problem with function
7551 templates; two instantiated function templates are allowed to
7552 differ only by their return types, which we do not add here. */
7553
7554 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7555 {
7556 struct attribute *attr;
7557 struct die_info *child;
7558 int first = 1;
7559
7560 die->building_fullname = 1;
7561
7562 for (child = die->child; child != NULL; child = child->sibling)
7563 {
7564 struct type *type;
12df843f 7565 LONGEST value;
d521ce57 7566 const gdb_byte *bytes;
98bfdba5
PA
7567 struct dwarf2_locexpr_baton *baton;
7568 struct value *v;
7569
7570 if (child->tag != DW_TAG_template_type_param
7571 && child->tag != DW_TAG_template_value_param)
7572 continue;
7573
7574 if (first)
7575 {
7576 fputs_unfiltered ("<", buf);
7577 first = 0;
7578 }
7579 else
7580 fputs_unfiltered (", ", buf);
7581
7582 attr = dwarf2_attr (child, DW_AT_type, cu);
7583 if (attr == NULL)
7584 {
7585 complaint (&symfile_complaints,
7586 _("template parameter missing DW_AT_type"));
7587 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7588 continue;
7589 }
7590 type = die_type (child, cu);
7591
7592 if (child->tag == DW_TAG_template_type_param)
7593 {
79d43c61 7594 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7595 continue;
7596 }
7597
7598 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7599 if (attr == NULL)
7600 {
7601 complaint (&symfile_complaints,
3e43a32a
MS
7602 _("template parameter missing "
7603 "DW_AT_const_value"));
98bfdba5
PA
7604 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7605 continue;
7606 }
7607
7608 dwarf2_const_value_attr (attr, type, name,
7609 &cu->comp_unit_obstack, cu,
7610 &value, &bytes, &baton);
7611
7612 if (TYPE_NOSIGN (type))
7613 /* GDB prints characters as NUMBER 'CHAR'. If that's
7614 changed, this can use value_print instead. */
7615 c_printchar (value, type, buf);
7616 else
7617 {
7618 struct value_print_options opts;
7619
7620 if (baton != NULL)
7621 v = dwarf2_evaluate_loc_desc (type, NULL,
7622 baton->data,
7623 baton->size,
7624 baton->per_cu);
7625 else if (bytes != NULL)
7626 {
7627 v = allocate_value (type);
7628 memcpy (value_contents_writeable (v), bytes,
7629 TYPE_LENGTH (type));
7630 }
7631 else
7632 v = value_from_longest (type, value);
7633
3e43a32a
MS
7634 /* Specify decimal so that we do not depend on
7635 the radix. */
98bfdba5
PA
7636 get_formatted_print_options (&opts, 'd');
7637 opts.raw = 1;
7638 value_print (v, buf, &opts);
7639 release_value (v);
7640 value_free (v);
7641 }
7642 }
7643
7644 die->building_fullname = 0;
7645
7646 if (!first)
7647 {
7648 /* Close the argument list, with a space if necessary
7649 (nested templates). */
7650 char last_char = '\0';
7651 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7652 if (last_char == '>')
7653 fputs_unfiltered (" >", buf);
7654 else
7655 fputs_unfiltered (">", buf);
7656 }
7657 }
7658
94af9270
KS
7659 /* For Java and C++ methods, append formal parameter type
7660 information, if PHYSNAME. */
6e70227d 7661
94af9270
KS
7662 if (physname && die->tag == DW_TAG_subprogram
7663 && (cu->language == language_cplus
7664 || cu->language == language_java))
7665 {
7666 struct type *type = read_type_die (die, cu);
7667
79d43c61
TT
7668 c_type_print_args (type, buf, 1, cu->language,
7669 &type_print_raw_options);
94af9270
KS
7670
7671 if (cu->language == language_java)
7672 {
7673 /* For java, we must append the return type to method
0963b4bd 7674 names. */
94af9270
KS
7675 if (die->tag == DW_TAG_subprogram)
7676 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7677 0, 0, &type_print_raw_options);
94af9270
KS
7678 }
7679 else if (cu->language == language_cplus)
7680 {
60430eff
DJ
7681 /* Assume that an artificial first parameter is
7682 "this", but do not crash if it is not. RealView
7683 marks unnamed (and thus unused) parameters as
7684 artificial; there is no way to differentiate
7685 the two cases. */
94af9270
KS
7686 if (TYPE_NFIELDS (type) > 0
7687 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7688 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7689 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7690 0))))
94af9270
KS
7691 fputs_unfiltered (" const", buf);
7692 }
7693 }
7694
bb5ed363 7695 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7696 &length);
7697 ui_file_delete (buf);
7698
7699 if (cu->language == language_cplus)
7700 {
15d034d0 7701 const char *cname
94af9270 7702 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7703 &objfile->objfile_obstack);
9a619af0 7704
94af9270
KS
7705 if (cname != NULL)
7706 name = cname;
7707 }
7708 }
7709 }
7710
7711 return name;
7712}
7713
0114d602
DJ
7714/* Return the fully qualified name of DIE, based on its DW_AT_name.
7715 If scope qualifiers are appropriate they will be added. The result
7716 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7717 not have a name. NAME may either be from a previous call to
7718 dwarf2_name or NULL.
7719
0963b4bd 7720 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7721
7722static const char *
15d034d0 7723dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7724{
94af9270
KS
7725 return dwarf2_compute_name (name, die, cu, 0);
7726}
0114d602 7727
94af9270
KS
7728/* Construct a physname for the given DIE in CU. NAME may either be
7729 from a previous call to dwarf2_name or NULL. The result will be
7730 allocated on the objfile_objstack or NULL if the DIE does not have a
7731 name.
0114d602 7732
94af9270 7733 The output string will be canonicalized (if C++/Java). */
0114d602 7734
94af9270 7735static const char *
15d034d0 7736dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7737{
bb5ed363 7738 struct objfile *objfile = cu->objfile;
900e11f9
JK
7739 struct attribute *attr;
7740 const char *retval, *mangled = NULL, *canon = NULL;
7741 struct cleanup *back_to;
7742 int need_copy = 1;
7743
7744 /* In this case dwarf2_compute_name is just a shortcut not building anything
7745 on its own. */
7746 if (!die_needs_namespace (die, cu))
7747 return dwarf2_compute_name (name, die, cu, 1);
7748
7749 back_to = make_cleanup (null_cleanup, NULL);
7750
7751 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7752 if (!attr)
7753 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7754
7755 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7756 has computed. */
7757 if (attr && DW_STRING (attr))
7758 {
7759 char *demangled;
7760
7761 mangled = DW_STRING (attr);
7762
7763 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7764 type. It is easier for GDB users to search for such functions as
7765 `name(params)' than `long name(params)'. In such case the minimal
7766 symbol names do not match the full symbol names but for template
7767 functions there is never a need to look up their definition from their
7768 declaration so the only disadvantage remains the minimal symbol
7769 variant `long name(params)' does not have the proper inferior type.
7770 */
7771
a766d390
DE
7772 if (cu->language == language_go)
7773 {
7774 /* This is a lie, but we already lie to the caller new_symbol_full.
7775 new_symbol_full assumes we return the mangled name.
7776 This just undoes that lie until things are cleaned up. */
7777 demangled = NULL;
7778 }
7779 else
7780 {
8de20a37
TT
7781 demangled = gdb_demangle (mangled,
7782 (DMGL_PARAMS | DMGL_ANSI
7783 | (cu->language == language_java
7784 ? DMGL_JAVA | DMGL_RET_POSTFIX
7785 : DMGL_RET_DROP)));
a766d390 7786 }
900e11f9
JK
7787 if (demangled)
7788 {
7789 make_cleanup (xfree, demangled);
7790 canon = demangled;
7791 }
7792 else
7793 {
7794 canon = mangled;
7795 need_copy = 0;
7796 }
7797 }
7798
7799 if (canon == NULL || check_physname)
7800 {
7801 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7802
7803 if (canon != NULL && strcmp (physname, canon) != 0)
7804 {
7805 /* It may not mean a bug in GDB. The compiler could also
7806 compute DW_AT_linkage_name incorrectly. But in such case
7807 GDB would need to be bug-to-bug compatible. */
7808
7809 complaint (&symfile_complaints,
7810 _("Computed physname <%s> does not match demangled <%s> "
7811 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7812 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7813
7814 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7815 is available here - over computed PHYSNAME. It is safer
7816 against both buggy GDB and buggy compilers. */
7817
7818 retval = canon;
7819 }
7820 else
7821 {
7822 retval = physname;
7823 need_copy = 0;
7824 }
7825 }
7826 else
7827 retval = canon;
7828
7829 if (need_copy)
10f0c4bb 7830 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7831
7832 do_cleanups (back_to);
7833 return retval;
0114d602
DJ
7834}
7835
27aa8d6a
SW
7836/* Read the import statement specified by the given die and record it. */
7837
7838static void
7839read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7840{
bb5ed363 7841 struct objfile *objfile = cu->objfile;
27aa8d6a 7842 struct attribute *import_attr;
32019081 7843 struct die_info *imported_die, *child_die;
de4affc9 7844 struct dwarf2_cu *imported_cu;
27aa8d6a 7845 const char *imported_name;
794684b6 7846 const char *imported_name_prefix;
13387711
SW
7847 const char *canonical_name;
7848 const char *import_alias;
7849 const char *imported_declaration = NULL;
794684b6 7850 const char *import_prefix;
32019081
JK
7851 VEC (const_char_ptr) *excludes = NULL;
7852 struct cleanup *cleanups;
13387711 7853
27aa8d6a
SW
7854 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7855 if (import_attr == NULL)
7856 {
7857 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7858 dwarf_tag_name (die->tag));
7859 return;
7860 }
7861
de4affc9
CC
7862 imported_cu = cu;
7863 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7864 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7865 if (imported_name == NULL)
7866 {
7867 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7868
7869 The import in the following code:
7870 namespace A
7871 {
7872 typedef int B;
7873 }
7874
7875 int main ()
7876 {
7877 using A::B;
7878 B b;
7879 return b;
7880 }
7881
7882 ...
7883 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7884 <52> DW_AT_decl_file : 1
7885 <53> DW_AT_decl_line : 6
7886 <54> DW_AT_import : <0x75>
7887 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7888 <59> DW_AT_name : B
7889 <5b> DW_AT_decl_file : 1
7890 <5c> DW_AT_decl_line : 2
7891 <5d> DW_AT_type : <0x6e>
7892 ...
7893 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7894 <76> DW_AT_byte_size : 4
7895 <77> DW_AT_encoding : 5 (signed)
7896
7897 imports the wrong die ( 0x75 instead of 0x58 ).
7898 This case will be ignored until the gcc bug is fixed. */
7899 return;
7900 }
7901
82856980
SW
7902 /* Figure out the local name after import. */
7903 import_alias = dwarf2_name (die, cu);
27aa8d6a 7904
794684b6
SW
7905 /* Figure out where the statement is being imported to. */
7906 import_prefix = determine_prefix (die, cu);
7907
7908 /* Figure out what the scope of the imported die is and prepend it
7909 to the name of the imported die. */
de4affc9 7910 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7911
f55ee35c
JK
7912 if (imported_die->tag != DW_TAG_namespace
7913 && imported_die->tag != DW_TAG_module)
794684b6 7914 {
13387711
SW
7915 imported_declaration = imported_name;
7916 canonical_name = imported_name_prefix;
794684b6 7917 }
13387711 7918 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7919 canonical_name = obconcat (&objfile->objfile_obstack,
7920 imported_name_prefix, "::", imported_name,
7921 (char *) NULL);
13387711
SW
7922 else
7923 canonical_name = imported_name;
794684b6 7924
32019081
JK
7925 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7926
7927 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7928 for (child_die = die->child; child_die && child_die->tag;
7929 child_die = sibling_die (child_die))
7930 {
7931 /* DWARF-4: A Fortran use statement with a “rename list” may be
7932 represented by an imported module entry with an import attribute
7933 referring to the module and owned entries corresponding to those
7934 entities that are renamed as part of being imported. */
7935
7936 if (child_die->tag != DW_TAG_imported_declaration)
7937 {
7938 complaint (&symfile_complaints,
7939 _("child DW_TAG_imported_declaration expected "
7940 "- DIE at 0x%x [in module %s]"),
b64f50a1 7941 child_die->offset.sect_off, objfile->name);
32019081
JK
7942 continue;
7943 }
7944
7945 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7946 if (import_attr == NULL)
7947 {
7948 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7949 dwarf_tag_name (child_die->tag));
7950 continue;
7951 }
7952
7953 imported_cu = cu;
7954 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7955 &imported_cu);
7956 imported_name = dwarf2_name (imported_die, imported_cu);
7957 if (imported_name == NULL)
7958 {
7959 complaint (&symfile_complaints,
7960 _("child DW_TAG_imported_declaration has unknown "
7961 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7962 child_die->offset.sect_off, objfile->name);
32019081
JK
7963 continue;
7964 }
7965
7966 VEC_safe_push (const_char_ptr, excludes, imported_name);
7967
7968 process_die (child_die, cu);
7969 }
7970
c0cc3a76
SW
7971 cp_add_using_directive (import_prefix,
7972 canonical_name,
7973 import_alias,
13387711 7974 imported_declaration,
32019081 7975 excludes,
12aaed36 7976 0,
bb5ed363 7977 &objfile->objfile_obstack);
32019081
JK
7978
7979 do_cleanups (cleanups);
27aa8d6a
SW
7980}
7981
f4dc4d17 7982/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7983
cb1df416
DJ
7984static void
7985free_cu_line_header (void *arg)
7986{
7987 struct dwarf2_cu *cu = arg;
7988
7989 free_line_header (cu->line_header);
7990 cu->line_header = NULL;
7991}
7992
1b80a9fa
JK
7993/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7994 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7995 this, it was first present in GCC release 4.3.0. */
7996
7997static int
7998producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7999{
8000 if (!cu->checked_producer)
8001 check_producer (cu);
8002
8003 return cu->producer_is_gcc_lt_4_3;
8004}
8005
9291a0cd
TT
8006static void
8007find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8008 const char **name, const char **comp_dir)
9291a0cd
TT
8009{
8010 struct attribute *attr;
8011
8012 *name = NULL;
8013 *comp_dir = NULL;
8014
8015 /* Find the filename. Do not use dwarf2_name here, since the filename
8016 is not a source language identifier. */
8017 attr = dwarf2_attr (die, DW_AT_name, cu);
8018 if (attr)
8019 {
8020 *name = DW_STRING (attr);
8021 }
8022
8023 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8024 if (attr)
8025 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8026 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8027 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8028 {
15d034d0
TT
8029 char *d = ldirname (*name);
8030
8031 *comp_dir = d;
8032 if (d != NULL)
8033 make_cleanup (xfree, d);
9291a0cd
TT
8034 }
8035 if (*comp_dir != NULL)
8036 {
8037 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8038 directory, get rid of it. */
8039 char *cp = strchr (*comp_dir, ':');
8040
8041 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8042 *comp_dir = cp + 1;
8043 }
8044
8045 if (*name == NULL)
8046 *name = "<unknown>";
8047}
8048
f4dc4d17
DE
8049/* Handle DW_AT_stmt_list for a compilation unit.
8050 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8051 COMP_DIR is the compilation directory.
8052 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8053
8054static void
8055handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 8056 const char *comp_dir)
2ab95328
TT
8057{
8058 struct attribute *attr;
2ab95328 8059
f4dc4d17
DE
8060 gdb_assert (! cu->per_cu->is_debug_types);
8061
2ab95328
TT
8062 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8063 if (attr)
8064 {
8065 unsigned int line_offset = DW_UNSND (attr);
8066 struct line_header *line_header
3019eac3 8067 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8068
8069 if (line_header)
dee91e82
DE
8070 {
8071 cu->line_header = line_header;
8072 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8073 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8074 }
2ab95328
TT
8075 }
8076}
8077
95554aad 8078/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8079
c906108c 8080static void
e7c27a73 8081read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8082{
dee91e82 8083 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8084 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8085 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8086 CORE_ADDR highpc = ((CORE_ADDR) 0);
8087 struct attribute *attr;
15d034d0
TT
8088 const char *name = NULL;
8089 const char *comp_dir = NULL;
c906108c
SS
8090 struct die_info *child_die;
8091 bfd *abfd = objfile->obfd;
e142c38c 8092 CORE_ADDR baseaddr;
6e70227d 8093
e142c38c 8094 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8095
fae299cd 8096 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8097
8098 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8099 from finish_block. */
2acceee2 8100 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8101 lowpc = highpc;
8102 lowpc += baseaddr;
8103 highpc += baseaddr;
8104
9291a0cd 8105 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8106
95554aad 8107 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8108
f4b8a18d
KW
8109 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8110 standardised yet. As a workaround for the language detection we fall
8111 back to the DW_AT_producer string. */
8112 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8113 cu->language = language_opencl;
8114
3019eac3
DE
8115 /* Similar hack for Go. */
8116 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8117 set_cu_language (DW_LANG_Go, cu);
8118
f4dc4d17 8119 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8120
8121 /* Decode line number information if present. We do this before
8122 processing child DIEs, so that the line header table is available
8123 for DW_AT_decl_file. */
f4dc4d17 8124 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8125
8126 /* Process all dies in compilation unit. */
8127 if (die->child != NULL)
8128 {
8129 child_die = die->child;
8130 while (child_die && child_die->tag)
8131 {
8132 process_die (child_die, cu);
8133 child_die = sibling_die (child_die);
8134 }
8135 }
8136
8137 /* Decode macro information, if present. Dwarf 2 macro information
8138 refers to information in the line number info statement program
8139 header, so we can only read it if we've read the header
8140 successfully. */
8141 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8142 if (attr && cu->line_header)
8143 {
8144 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8145 complaint (&symfile_complaints,
8146 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8147
09262596 8148 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8149 }
8150 else
8151 {
8152 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8153 if (attr && cu->line_header)
8154 {
8155 unsigned int macro_offset = DW_UNSND (attr);
8156
09262596 8157 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8158 }
8159 }
8160
8161 do_cleanups (back_to);
8162}
8163
f4dc4d17
DE
8164/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8165 Create the set of symtabs used by this TU, or if this TU is sharing
8166 symtabs with another TU and the symtabs have already been created
8167 then restore those symtabs in the line header.
8168 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8169
8170static void
f4dc4d17 8171setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8172{
f4dc4d17
DE
8173 struct objfile *objfile = dwarf2_per_objfile->objfile;
8174 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8175 struct type_unit_group *tu_group;
8176 int first_time;
8177 struct line_header *lh;
3019eac3 8178 struct attribute *attr;
f4dc4d17 8179 unsigned int i, line_offset;
0186c6a7 8180 struct signatured_type *sig_type;
3019eac3 8181
f4dc4d17 8182 gdb_assert (per_cu->is_debug_types);
0186c6a7 8183 sig_type = (struct signatured_type *) per_cu;
3019eac3 8184
f4dc4d17 8185 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8186
f4dc4d17
DE
8187 /* If we're using .gdb_index (includes -readnow) then
8188 per_cu->s.type_unit_group may not have been set up yet. */
0186c6a7
DE
8189 if (sig_type->type_unit_group == NULL)
8190 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8191 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8192
8193 /* If we've already processed this stmt_list there's no real need to
8194 do it again, we could fake it and just recreate the part we need
8195 (file name,index -> symtab mapping). If data shows this optimization
8196 is useful we can do it then. */
8197 first_time = tu_group->primary_symtab == NULL;
8198
8199 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8200 debug info. */
8201 lh = NULL;
8202 if (attr != NULL)
3019eac3 8203 {
f4dc4d17
DE
8204 line_offset = DW_UNSND (attr);
8205 lh = dwarf_decode_line_header (line_offset, cu);
8206 }
8207 if (lh == NULL)
8208 {
8209 if (first_time)
8210 dwarf2_start_symtab (cu, "", NULL, 0);
8211 else
8212 {
8213 gdb_assert (tu_group->symtabs == NULL);
8214 restart_symtab (0);
8215 }
8216 /* Note: The primary symtab will get allocated at the end. */
8217 return;
3019eac3
DE
8218 }
8219
f4dc4d17
DE
8220 cu->line_header = lh;
8221 make_cleanup (free_cu_line_header, cu);
3019eac3 8222
f4dc4d17
DE
8223 if (first_time)
8224 {
8225 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8226
f4dc4d17
DE
8227 tu_group->num_symtabs = lh->num_file_names;
8228 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8229
f4dc4d17
DE
8230 for (i = 0; i < lh->num_file_names; ++i)
8231 {
d521ce57 8232 const char *dir = NULL;
f4dc4d17 8233 struct file_entry *fe = &lh->file_names[i];
3019eac3 8234
f4dc4d17
DE
8235 if (fe->dir_index)
8236 dir = lh->include_dirs[fe->dir_index - 1];
8237 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8238
f4dc4d17
DE
8239 /* Note: We don't have to watch for the main subfile here, type units
8240 don't have DW_AT_name. */
3019eac3 8241
f4dc4d17
DE
8242 if (current_subfile->symtab == NULL)
8243 {
8244 /* NOTE: start_subfile will recognize when it's been passed
8245 a file it has already seen. So we can't assume there's a
8246 simple mapping from lh->file_names to subfiles,
8247 lh->file_names may contain dups. */
8248 current_subfile->symtab = allocate_symtab (current_subfile->name,
8249 objfile);
8250 }
8251
8252 fe->symtab = current_subfile->symtab;
8253 tu_group->symtabs[i] = fe->symtab;
8254 }
8255 }
8256 else
3019eac3 8257 {
f4dc4d17
DE
8258 restart_symtab (0);
8259
8260 for (i = 0; i < lh->num_file_names; ++i)
8261 {
8262 struct file_entry *fe = &lh->file_names[i];
8263
8264 fe->symtab = tu_group->symtabs[i];
8265 }
3019eac3
DE
8266 }
8267
f4dc4d17
DE
8268 /* The main symtab is allocated last. Type units don't have DW_AT_name
8269 so they don't have a "real" (so to speak) symtab anyway.
8270 There is later code that will assign the main symtab to all symbols
8271 that don't have one. We need to handle the case of a symbol with a
8272 missing symtab (DW_AT_decl_file) anyway. */
8273}
3019eac3 8274
f4dc4d17
DE
8275/* Process DW_TAG_type_unit.
8276 For TUs we want to skip the first top level sibling if it's not the
8277 actual type being defined by this TU. In this case the first top
8278 level sibling is there to provide context only. */
3019eac3 8279
f4dc4d17
DE
8280static void
8281read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8282{
8283 struct die_info *child_die;
3019eac3 8284
f4dc4d17
DE
8285 prepare_one_comp_unit (cu, die, language_minimal);
8286
8287 /* Initialize (or reinitialize) the machinery for building symtabs.
8288 We do this before processing child DIEs, so that the line header table
8289 is available for DW_AT_decl_file. */
8290 setup_type_unit_groups (die, cu);
8291
8292 if (die->child != NULL)
8293 {
8294 child_die = die->child;
8295 while (child_die && child_die->tag)
8296 {
8297 process_die (child_die, cu);
8298 child_die = sibling_die (child_die);
8299 }
8300 }
3019eac3
DE
8301}
8302\f
80626a55
DE
8303/* DWO/DWP files.
8304
8305 http://gcc.gnu.org/wiki/DebugFission
8306 http://gcc.gnu.org/wiki/DebugFissionDWP
8307
8308 To simplify handling of both DWO files ("object" files with the DWARF info)
8309 and DWP files (a file with the DWOs packaged up into one file), we treat
8310 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8311
8312static hashval_t
8313hash_dwo_file (const void *item)
8314{
8315 const struct dwo_file *dwo_file = item;
8316
0ac5b59e
DE
8317 return (htab_hash_string (dwo_file->dwo_name)
8318 + htab_hash_string (dwo_file->comp_dir));
3019eac3
DE
8319}
8320
8321static int
8322eq_dwo_file (const void *item_lhs, const void *item_rhs)
8323{
8324 const struct dwo_file *lhs = item_lhs;
8325 const struct dwo_file *rhs = item_rhs;
8326
0ac5b59e
DE
8327 return (strcmp (lhs->dwo_name, rhs->dwo_name) == 0
8328 && strcmp (lhs->comp_dir, rhs->comp_dir) == 0);
3019eac3
DE
8329}
8330
8331/* Allocate a hash table for DWO files. */
8332
8333static htab_t
8334allocate_dwo_file_hash_table (void)
8335{
8336 struct objfile *objfile = dwarf2_per_objfile->objfile;
8337
8338 return htab_create_alloc_ex (41,
8339 hash_dwo_file,
8340 eq_dwo_file,
8341 NULL,
8342 &objfile->objfile_obstack,
8343 hashtab_obstack_allocate,
8344 dummy_obstack_deallocate);
8345}
8346
80626a55
DE
8347/* Lookup DWO file DWO_NAME. */
8348
8349static void **
0ac5b59e 8350lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8351{
8352 struct dwo_file find_entry;
8353 void **slot;
8354
8355 if (dwarf2_per_objfile->dwo_files == NULL)
8356 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8357
8358 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8359 find_entry.dwo_name = dwo_name;
8360 find_entry.comp_dir = comp_dir;
80626a55
DE
8361 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8362
8363 return slot;
8364}
8365
3019eac3
DE
8366static hashval_t
8367hash_dwo_unit (const void *item)
8368{
8369 const struct dwo_unit *dwo_unit = item;
8370
8371 /* This drops the top 32 bits of the id, but is ok for a hash. */
8372 return dwo_unit->signature;
8373}
8374
8375static int
8376eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8377{
8378 const struct dwo_unit *lhs = item_lhs;
8379 const struct dwo_unit *rhs = item_rhs;
8380
8381 /* The signature is assumed to be unique within the DWO file.
8382 So while object file CU dwo_id's always have the value zero,
8383 that's OK, assuming each object file DWO file has only one CU,
8384 and that's the rule for now. */
8385 return lhs->signature == rhs->signature;
8386}
8387
8388/* Allocate a hash table for DWO CUs,TUs.
8389 There is one of these tables for each of CUs,TUs for each DWO file. */
8390
8391static htab_t
8392allocate_dwo_unit_table (struct objfile *objfile)
8393{
8394 /* Start out with a pretty small number.
8395 Generally DWO files contain only one CU and maybe some TUs. */
8396 return htab_create_alloc_ex (3,
8397 hash_dwo_unit,
8398 eq_dwo_unit,
8399 NULL,
8400 &objfile->objfile_obstack,
8401 hashtab_obstack_allocate,
8402 dummy_obstack_deallocate);
8403}
8404
80626a55 8405/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8406
19c3d4c9 8407struct create_dwo_cu_data
3019eac3
DE
8408{
8409 struct dwo_file *dwo_file;
19c3d4c9 8410 struct dwo_unit dwo_unit;
3019eac3
DE
8411};
8412
19c3d4c9 8413/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8414
8415static void
19c3d4c9
DE
8416create_dwo_cu_reader (const struct die_reader_specs *reader,
8417 const gdb_byte *info_ptr,
8418 struct die_info *comp_unit_die,
8419 int has_children,
8420 void *datap)
3019eac3
DE
8421{
8422 struct dwarf2_cu *cu = reader->cu;
8423 struct objfile *objfile = dwarf2_per_objfile->objfile;
8424 sect_offset offset = cu->per_cu->offset;
8a0459fd 8425 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8426 struct create_dwo_cu_data *data = datap;
3019eac3 8427 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8428 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8429 struct attribute *attr;
3019eac3
DE
8430
8431 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8432 if (attr == NULL)
8433 {
19c3d4c9
DE
8434 complaint (&symfile_complaints,
8435 _("Dwarf Error: debug entry at offset 0x%x is missing"
8436 " its dwo_id [in module %s]"),
8437 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8438 return;
8439 }
8440
3019eac3
DE
8441 dwo_unit->dwo_file = dwo_file;
8442 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8443 dwo_unit->section = section;
3019eac3
DE
8444 dwo_unit->offset = offset;
8445 dwo_unit->length = cu->per_cu->length;
8446
09406207 8447 if (dwarf2_read_debug)
4031ecc5
DE
8448 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8449 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8450}
8451
19c3d4c9
DE
8452/* Create the dwo_unit for the lone CU in DWO_FILE.
8453 Note: This function processes DWO files only, not DWP files. */
3019eac3 8454
19c3d4c9
DE
8455static struct dwo_unit *
8456create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8457{
8458 struct objfile *objfile = dwarf2_per_objfile->objfile;
8459 struct dwarf2_section_info *section = &dwo_file->sections.info;
8460 bfd *abfd;
8461 htab_t cu_htab;
d521ce57 8462 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8463 struct create_dwo_cu_data create_dwo_cu_data;
8464 struct dwo_unit *dwo_unit;
3019eac3
DE
8465
8466 dwarf2_read_section (objfile, section);
8467 info_ptr = section->buffer;
8468
8469 if (info_ptr == NULL)
8470 return NULL;
8471
8472 /* We can't set abfd until now because the section may be empty or
8473 not present, in which case section->asection will be NULL. */
8474 abfd = section->asection->owner;
8475
09406207 8476 if (dwarf2_read_debug)
19c3d4c9
DE
8477 {
8478 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8479 bfd_section_name (abfd, section->asection),
8480 bfd_get_filename (abfd));
8481 }
3019eac3 8482
19c3d4c9
DE
8483 create_dwo_cu_data.dwo_file = dwo_file;
8484 dwo_unit = NULL;
3019eac3
DE
8485
8486 end_ptr = info_ptr + section->size;
8487 while (info_ptr < end_ptr)
8488 {
8489 struct dwarf2_per_cu_data per_cu;
8490
19c3d4c9
DE
8491 memset (&create_dwo_cu_data.dwo_unit, 0,
8492 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8493 memset (&per_cu, 0, sizeof (per_cu));
8494 per_cu.objfile = objfile;
8495 per_cu.is_debug_types = 0;
8496 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8497 per_cu.section = section;
3019eac3
DE
8498
8499 init_cutu_and_read_dies_no_follow (&per_cu,
8500 &dwo_file->sections.abbrev,
8501 dwo_file,
19c3d4c9
DE
8502 create_dwo_cu_reader,
8503 &create_dwo_cu_data);
8504
8505 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8506 {
8507 /* If we've already found one, complain. We only support one
8508 because having more than one requires hacking the dwo_name of
8509 each to match, which is highly unlikely to happen. */
8510 if (dwo_unit != NULL)
8511 {
8512 complaint (&symfile_complaints,
8513 _("Multiple CUs in DWO file %s [in module %s]"),
8514 dwo_file->dwo_name, objfile->name);
8515 break;
8516 }
8517
8518 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8519 *dwo_unit = create_dwo_cu_data.dwo_unit;
8520 }
3019eac3
DE
8521
8522 info_ptr += per_cu.length;
8523 }
8524
19c3d4c9 8525 return dwo_unit;
3019eac3
DE
8526}
8527
80626a55
DE
8528/* DWP file .debug_{cu,tu}_index section format:
8529 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8530
8531 Both index sections have the same format, and serve to map a 64-bit
8532 signature to a set of section numbers. Each section begins with a header,
8533 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8534 indexes, and a pool of 32-bit section numbers. The index sections will be
8535 aligned at 8-byte boundaries in the file.
8536
8537 The index section header contains two unsigned 32-bit values (using the
8538 byte order of the application binary):
8539
8540 N, the number of compilation units or type units in the index
8541 M, the number of slots in the hash table
8542
8543 (We assume that N and M will not exceed 2^32 - 1.)
8544
8545 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8546
8547 The hash table begins at offset 8 in the section, and consists of an array
8548 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8549 order of the application binary). Unused slots in the hash table are 0.
8550 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8551
8552 The parallel table begins immediately after the hash table
8553 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8554 array of 32-bit indexes (using the byte order of the application binary),
8555 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8556 table contains a 32-bit index into the pool of section numbers. For unused
8557 hash table slots, the corresponding entry in the parallel table will be 0.
8558
8559 Given a 64-bit compilation unit signature or a type signature S, an entry
8560 in the hash table is located as follows:
8561
8562 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8563 the low-order k bits all set to 1.
8564
8565 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8566
8567 3) If the hash table entry at index H matches the signature, use that
8568 entry. If the hash table entry at index H is unused (all zeroes),
8569 terminate the search: the signature is not present in the table.
8570
8571 4) Let H = (H + H') modulo M. Repeat at Step 3.
8572
8573 Because M > N and H' and M are relatively prime, the search is guaranteed
8574 to stop at an unused slot or find the match.
8575
8576 The pool of section numbers begins immediately following the hash table
8577 (at offset 8 + 12 * M from the beginning of the section). The pool of
8578 section numbers consists of an array of 32-bit words (using the byte order
8579 of the application binary). Each item in the array is indexed starting
8580 from 0. The hash table entry provides the index of the first section
8581 number in the set. Additional section numbers in the set follow, and the
8582 set is terminated by a 0 entry (section number 0 is not used in ELF).
8583
8584 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8585 section must be the first entry in the set, and the .debug_abbrev.dwo must
8586 be the second entry. Other members of the set may follow in any order. */
8587
8588/* Create a hash table to map DWO IDs to their CU/TU entry in
8589 .debug_{info,types}.dwo in DWP_FILE.
8590 Returns NULL if there isn't one.
8591 Note: This function processes DWP files only, not DWO files. */
8592
8593static struct dwp_hash_table *
8594create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8595{
8596 struct objfile *objfile = dwarf2_per_objfile->objfile;
8597 bfd *dbfd = dwp_file->dbfd;
d521ce57 8598 const char *index_ptr, *index_end;
80626a55
DE
8599 struct dwarf2_section_info *index;
8600 uint32_t version, nr_units, nr_slots;
8601 struct dwp_hash_table *htab;
8602
8603 if (is_debug_types)
8604 index = &dwp_file->sections.tu_index;
8605 else
8606 index = &dwp_file->sections.cu_index;
8607
8608 if (dwarf2_section_empty_p (index))
8609 return NULL;
8610 dwarf2_read_section (objfile, index);
8611
8612 index_ptr = index->buffer;
8613 index_end = index_ptr + index->size;
8614
8615 version = read_4_bytes (dbfd, index_ptr);
8616 index_ptr += 8; /* Skip the unused word. */
8617 nr_units = read_4_bytes (dbfd, index_ptr);
8618 index_ptr += 4;
8619 nr_slots = read_4_bytes (dbfd, index_ptr);
8620 index_ptr += 4;
8621
8622 if (version != 1)
8623 {
8624 error (_("Dwarf Error: unsupported DWP file version (%u)"
8625 " [in module %s]"),
8626 version, dwp_file->name);
8627 }
8628 if (nr_slots != (nr_slots & -nr_slots))
8629 {
8630 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8631 " is not power of 2 [in module %s]"),
8632 nr_slots, dwp_file->name);
8633 }
8634
8635 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8636 htab->nr_units = nr_units;
8637 htab->nr_slots = nr_slots;
8638 htab->hash_table = index_ptr;
8639 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8640 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8641
8642 return htab;
8643}
8644
8645/* Update SECTIONS with the data from SECTP.
8646
8647 This function is like the other "locate" section routines that are
8648 passed to bfd_map_over_sections, but in this context the sections to
8649 read comes from the DWP hash table, not the full ELF section table.
8650
8651 The result is non-zero for success, or zero if an error was found. */
8652
8653static int
8654locate_virtual_dwo_sections (asection *sectp,
8655 struct virtual_dwo_sections *sections)
8656{
8657 const struct dwop_section_names *names = &dwop_section_names;
8658
8659 if (section_is_p (sectp->name, &names->abbrev_dwo))
8660 {
8661 /* There can be only one. */
8662 if (sections->abbrev.asection != NULL)
8663 return 0;
8664 sections->abbrev.asection = sectp;
8665 sections->abbrev.size = bfd_get_section_size (sectp);
8666 }
8667 else if (section_is_p (sectp->name, &names->info_dwo)
8668 || section_is_p (sectp->name, &names->types_dwo))
8669 {
8670 /* There can be only one. */
8671 if (sections->info_or_types.asection != NULL)
8672 return 0;
8673 sections->info_or_types.asection = sectp;
8674 sections->info_or_types.size = bfd_get_section_size (sectp);
8675 }
8676 else if (section_is_p (sectp->name, &names->line_dwo))
8677 {
8678 /* There can be only one. */
8679 if (sections->line.asection != NULL)
8680 return 0;
8681 sections->line.asection = sectp;
8682 sections->line.size = bfd_get_section_size (sectp);
8683 }
8684 else if (section_is_p (sectp->name, &names->loc_dwo))
8685 {
8686 /* There can be only one. */
8687 if (sections->loc.asection != NULL)
8688 return 0;
8689 sections->loc.asection = sectp;
8690 sections->loc.size = bfd_get_section_size (sectp);
8691 }
8692 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8693 {
8694 /* There can be only one. */
8695 if (sections->macinfo.asection != NULL)
8696 return 0;
8697 sections->macinfo.asection = sectp;
8698 sections->macinfo.size = bfd_get_section_size (sectp);
8699 }
8700 else if (section_is_p (sectp->name, &names->macro_dwo))
8701 {
8702 /* There can be only one. */
8703 if (sections->macro.asection != NULL)
8704 return 0;
8705 sections->macro.asection = sectp;
8706 sections->macro.size = bfd_get_section_size (sectp);
8707 }
8708 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8709 {
8710 /* There can be only one. */
8711 if (sections->str_offsets.asection != NULL)
8712 return 0;
8713 sections->str_offsets.asection = sectp;
8714 sections->str_offsets.size = bfd_get_section_size (sectp);
8715 }
8716 else
8717 {
8718 /* No other kind of section is valid. */
8719 return 0;
8720 }
8721
8722 return 1;
8723}
8724
8725/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8726 HTAB is the hash table from the DWP file.
0ac5b59e
DE
8727 SECTION_INDEX is the index of the DWO in HTAB.
8728 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
8729
8730static struct dwo_unit *
8731create_dwo_in_dwp (struct dwp_file *dwp_file,
8732 const struct dwp_hash_table *htab,
8733 uint32_t section_index,
0ac5b59e 8734 const char *comp_dir,
80626a55
DE
8735 ULONGEST signature, int is_debug_types)
8736{
8737 struct objfile *objfile = dwarf2_per_objfile->objfile;
8738 bfd *dbfd = dwp_file->dbfd;
8739 const char *kind = is_debug_types ? "TU" : "CU";
8740 struct dwo_file *dwo_file;
8741 struct dwo_unit *dwo_unit;
8742 struct virtual_dwo_sections sections;
8743 void **dwo_file_slot;
8744 char *virtual_dwo_name;
8745 struct dwarf2_section_info *cutu;
8746 struct cleanup *cleanups;
8747 int i;
8748
8749 if (dwarf2_read_debug)
8750 {
4031ecc5 8751 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/%s in DWP file: %s\n",
80626a55 8752 kind,
4031ecc5 8753 section_index, hex_string (signature),
80626a55
DE
8754 dwp_file->name);
8755 }
8756
8757 /* Fetch the sections of this DWO.
8758 Put a limit on the number of sections we look for so that bad data
8759 doesn't cause us to loop forever. */
8760
8761#define MAX_NR_DWO_SECTIONS \
8762 (1 /* .debug_info or .debug_types */ \
8763 + 1 /* .debug_abbrev */ \
8764 + 1 /* .debug_line */ \
8765 + 1 /* .debug_loc */ \
8766 + 1 /* .debug_str_offsets */ \
8767 + 1 /* .debug_macro */ \
8768 + 1 /* .debug_macinfo */ \
8769 + 1 /* trailing zero */)
8770
8771 memset (&sections, 0, sizeof (sections));
8772 cleanups = make_cleanup (null_cleanup, 0);
8773
8774 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8775 {
8776 asection *sectp;
8777 uint32_t section_nr =
8778 read_4_bytes (dbfd,
8779 htab->section_pool
8780 + (section_index + i) * sizeof (uint32_t));
8781
8782 if (section_nr == 0)
8783 break;
8784 if (section_nr >= dwp_file->num_sections)
8785 {
8786 error (_("Dwarf Error: bad DWP hash table, section number too large"
8787 " [in module %s]"),
8788 dwp_file->name);
8789 }
8790
8791 sectp = dwp_file->elf_sections[section_nr];
8792 if (! locate_virtual_dwo_sections (sectp, &sections))
8793 {
8794 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8795 " [in module %s]"),
8796 dwp_file->name);
8797 }
8798 }
8799
8800 if (i < 2
8801 || sections.info_or_types.asection == NULL
8802 || sections.abbrev.asection == NULL)
8803 {
8804 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8805 " [in module %s]"),
8806 dwp_file->name);
8807 }
8808 if (i == MAX_NR_DWO_SECTIONS)
8809 {
8810 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8811 " [in module %s]"),
8812 dwp_file->name);
8813 }
8814
8815 /* It's easier for the rest of the code if we fake a struct dwo_file and
8816 have dwo_unit "live" in that. At least for now.
8817
8818 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8819 However, for each CU + set of TUs that came from the same original DWO
8820 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8821 (fewer struct dwo_file objects to allocated). Remember that for really
8822 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8823
2792b94d
PM
8824 virtual_dwo_name =
8825 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8826 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8827 sections.line.asection ? sections.line.asection->id : 0,
8828 sections.loc.asection ? sections.loc.asection->id : 0,
8829 (sections.str_offsets.asection
8830 ? sections.str_offsets.asection->id
8831 : 0));
80626a55
DE
8832 make_cleanup (xfree, virtual_dwo_name);
8833 /* Can we use an existing virtual DWO file? */
0ac5b59e 8834 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
8835 /* Create one if necessary. */
8836 if (*dwo_file_slot == NULL)
8837 {
8838 if (dwarf2_read_debug)
8839 {
8840 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8841 virtual_dwo_name);
8842 }
8843 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
8844 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
8845 virtual_dwo_name,
8846 strlen (virtual_dwo_name));
8847 dwo_file->comp_dir = comp_dir;
80626a55
DE
8848 dwo_file->sections.abbrev = sections.abbrev;
8849 dwo_file->sections.line = sections.line;
8850 dwo_file->sections.loc = sections.loc;
8851 dwo_file->sections.macinfo = sections.macinfo;
8852 dwo_file->sections.macro = sections.macro;
8853 dwo_file->sections.str_offsets = sections.str_offsets;
8854 /* The "str" section is global to the entire DWP file. */
8855 dwo_file->sections.str = dwp_file->sections.str;
8856 /* The info or types section is assigned later to dwo_unit,
8857 there's no need to record it in dwo_file.
8858 Also, we can't simply record type sections in dwo_file because
8859 we record a pointer into the vector in dwo_unit. As we collect more
8860 types we'll grow the vector and eventually have to reallocate space
8861 for it, invalidating all the pointers into the current copy. */
8862 *dwo_file_slot = dwo_file;
8863 }
8864 else
8865 {
8866 if (dwarf2_read_debug)
8867 {
8868 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8869 virtual_dwo_name);
8870 }
8871 dwo_file = *dwo_file_slot;
8872 }
8873 do_cleanups (cleanups);
8874
8875 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8876 dwo_unit->dwo_file = dwo_file;
8877 dwo_unit->signature = signature;
8a0459fd
DE
8878 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8879 sizeof (struct dwarf2_section_info));
8880 *dwo_unit->section = sections.info_or_types;
80626a55
DE
8881 /* offset, length, type_offset_in_tu are set later. */
8882
8883 return dwo_unit;
8884}
8885
8886/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8887
8888static struct dwo_unit *
8889lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8890 const struct dwp_hash_table *htab,
0ac5b59e 8891 const char *comp_dir,
80626a55
DE
8892 ULONGEST signature, int is_debug_types)
8893{
8894 bfd *dbfd = dwp_file->dbfd;
8895 uint32_t mask = htab->nr_slots - 1;
8896 uint32_t hash = signature & mask;
8897 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8898 unsigned int i;
8899 void **slot;
8900 struct dwo_unit find_dwo_cu, *dwo_cu;
8901
8902 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8903 find_dwo_cu.signature = signature;
8904 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8905
8906 if (*slot != NULL)
8907 return *slot;
8908
8909 /* Use a for loop so that we don't loop forever on bad debug info. */
8910 for (i = 0; i < htab->nr_slots; ++i)
8911 {
8912 ULONGEST signature_in_table;
8913
8914 signature_in_table =
8915 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8916 if (signature_in_table == signature)
8917 {
8918 uint32_t section_index =
8919 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8920
8921 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 8922 comp_dir, signature, is_debug_types);
80626a55
DE
8923 return *slot;
8924 }
8925 if (signature_in_table == 0)
8926 return NULL;
8927 hash = (hash + hash2) & mask;
8928 }
8929
8930 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8931 " [in module %s]"),
8932 dwp_file->name);
8933}
8934
ab5088bf 8935/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
8936 Open the file specified by FILE_NAME and hand it off to BFD for
8937 preliminary analysis. Return a newly initialized bfd *, which
8938 includes a canonicalized copy of FILE_NAME.
80626a55 8939 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8940 In case of trouble, return NULL.
8941 NOTE: This function is derived from symfile_bfd_open. */
8942
8943static bfd *
80626a55 8944try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8945{
8946 bfd *sym_bfd;
80626a55 8947 int desc, flags;
3019eac3 8948 char *absolute_name;
3019eac3 8949
80626a55
DE
8950 flags = OPF_TRY_CWD_FIRST;
8951 if (is_dwp)
8952 flags |= OPF_SEARCH_IN_PATH;
8953 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8954 O_RDONLY | O_BINARY, &absolute_name);
8955 if (desc < 0)
8956 return NULL;
8957
bb397797 8958 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8959 if (!sym_bfd)
8960 {
3019eac3
DE
8961 xfree (absolute_name);
8962 return NULL;
8963 }
a4453b7e 8964 xfree (absolute_name);
3019eac3
DE
8965 bfd_set_cacheable (sym_bfd, 1);
8966
8967 if (!bfd_check_format (sym_bfd, bfd_object))
8968 {
cbb099e8 8969 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8970 return NULL;
8971 }
8972
3019eac3
DE
8973 return sym_bfd;
8974}
8975
ab5088bf 8976/* Try to open DWO file FILE_NAME.
3019eac3
DE
8977 COMP_DIR is the DW_AT_comp_dir attribute.
8978 The result is the bfd handle of the file.
8979 If there is a problem finding or opening the file, return NULL.
8980 Upon success, the canonicalized path of the file is stored in the bfd,
8981 same as symfile_bfd_open. */
8982
8983static bfd *
ab5088bf 8984open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
8985{
8986 bfd *abfd;
3019eac3 8987
80626a55 8988 if (IS_ABSOLUTE_PATH (file_name))
ab5088bf 8989 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
8990
8991 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8992
8993 if (comp_dir != NULL)
8994 {
80626a55 8995 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8996
8997 /* NOTE: If comp_dir is a relative path, this will also try the
8998 search path, which seems useful. */
ab5088bf 8999 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
3019eac3
DE
9000 xfree (path_to_try);
9001 if (abfd != NULL)
9002 return abfd;
9003 }
9004
9005 /* That didn't work, try debug-file-directory, which, despite its name,
9006 is a list of paths. */
9007
9008 if (*debug_file_directory == '\0')
9009 return NULL;
9010
ab5088bf 9011 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
9012}
9013
80626a55
DE
9014/* This function is mapped across the sections and remembers the offset and
9015 size of each of the DWO debugging sections we are interested in. */
9016
9017static void
9018dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9019{
9020 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9021 const struct dwop_section_names *names = &dwop_section_names;
9022
9023 if (section_is_p (sectp->name, &names->abbrev_dwo))
9024 {
9025 dwo_sections->abbrev.asection = sectp;
9026 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9027 }
9028 else if (section_is_p (sectp->name, &names->info_dwo))
9029 {
9030 dwo_sections->info.asection = sectp;
9031 dwo_sections->info.size = bfd_get_section_size (sectp);
9032 }
9033 else if (section_is_p (sectp->name, &names->line_dwo))
9034 {
9035 dwo_sections->line.asection = sectp;
9036 dwo_sections->line.size = bfd_get_section_size (sectp);
9037 }
9038 else if (section_is_p (sectp->name, &names->loc_dwo))
9039 {
9040 dwo_sections->loc.asection = sectp;
9041 dwo_sections->loc.size = bfd_get_section_size (sectp);
9042 }
9043 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9044 {
9045 dwo_sections->macinfo.asection = sectp;
9046 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9047 }
9048 else if (section_is_p (sectp->name, &names->macro_dwo))
9049 {
9050 dwo_sections->macro.asection = sectp;
9051 dwo_sections->macro.size = bfd_get_section_size (sectp);
9052 }
9053 else if (section_is_p (sectp->name, &names->str_dwo))
9054 {
9055 dwo_sections->str.asection = sectp;
9056 dwo_sections->str.size = bfd_get_section_size (sectp);
9057 }
9058 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9059 {
9060 dwo_sections->str_offsets.asection = sectp;
9061 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9062 }
9063 else if (section_is_p (sectp->name, &names->types_dwo))
9064 {
9065 struct dwarf2_section_info type_section;
9066
9067 memset (&type_section, 0, sizeof (type_section));
9068 type_section.asection = sectp;
9069 type_section.size = bfd_get_section_size (sectp);
9070 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9071 &type_section);
9072 }
9073}
9074
ab5088bf 9075/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9076 by PER_CU. This is for the non-DWP case.
80626a55 9077 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9078
9079static struct dwo_file *
0ac5b59e
DE
9080open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9081 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9082{
9083 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9084 struct dwo_file *dwo_file;
9085 bfd *dbfd;
3019eac3
DE
9086 struct cleanup *cleanups;
9087
ab5088bf 9088 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9089 if (dbfd == NULL)
9090 {
9091 if (dwarf2_read_debug)
9092 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9093 return NULL;
9094 }
9095 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9096 dwo_file->dwo_name = dwo_name;
9097 dwo_file->comp_dir = comp_dir;
80626a55 9098 dwo_file->dbfd = dbfd;
3019eac3
DE
9099
9100 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9101
80626a55 9102 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9103
19c3d4c9 9104 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9105
9106 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9107 dwo_file->sections.types);
9108
9109 discard_cleanups (cleanups);
9110
80626a55
DE
9111 if (dwarf2_read_debug)
9112 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9113
3019eac3
DE
9114 return dwo_file;
9115}
9116
80626a55
DE
9117/* This function is mapped across the sections and remembers the offset and
9118 size of each of the DWP debugging sections we are interested in. */
3019eac3 9119
80626a55
DE
9120static void
9121dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9122{
80626a55
DE
9123 struct dwp_file *dwp_file = dwp_file_ptr;
9124 const struct dwop_section_names *names = &dwop_section_names;
9125 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9126
80626a55
DE
9127 /* Record the ELF section number for later lookup: this is what the
9128 .debug_cu_index,.debug_tu_index tables use. */
9129 gdb_assert (elf_section_nr < dwp_file->num_sections);
9130 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9131
80626a55
DE
9132 /* Look for specific sections that we need. */
9133 if (section_is_p (sectp->name, &names->str_dwo))
9134 {
9135 dwp_file->sections.str.asection = sectp;
9136 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9137 }
9138 else if (section_is_p (sectp->name, &names->cu_index))
9139 {
9140 dwp_file->sections.cu_index.asection = sectp;
9141 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9142 }
9143 else if (section_is_p (sectp->name, &names->tu_index))
9144 {
9145 dwp_file->sections.tu_index.asection = sectp;
9146 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9147 }
9148}
3019eac3 9149
80626a55 9150/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9151
80626a55
DE
9152static hashval_t
9153hash_dwp_loaded_cutus (const void *item)
9154{
9155 const struct dwo_unit *dwo_unit = item;
3019eac3 9156
80626a55
DE
9157 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9158 return dwo_unit->signature;
3019eac3
DE
9159}
9160
80626a55 9161/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9162
80626a55
DE
9163static int
9164eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9165{
80626a55
DE
9166 const struct dwo_unit *dua = a;
9167 const struct dwo_unit *dub = b;
3019eac3 9168
80626a55
DE
9169 return dua->signature == dub->signature;
9170}
3019eac3 9171
80626a55 9172/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9173
80626a55
DE
9174static htab_t
9175allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9176{
9177 return htab_create_alloc_ex (3,
9178 hash_dwp_loaded_cutus,
9179 eq_dwp_loaded_cutus,
9180 NULL,
9181 &objfile->objfile_obstack,
9182 hashtab_obstack_allocate,
9183 dummy_obstack_deallocate);
9184}
3019eac3 9185
ab5088bf
DE
9186/* Try to open DWP file FILE_NAME.
9187 The result is the bfd handle of the file.
9188 If there is a problem finding or opening the file, return NULL.
9189 Upon success, the canonicalized path of the file is stored in the bfd,
9190 same as symfile_bfd_open. */
9191
9192static bfd *
9193open_dwp_file (const char *file_name)
9194{
9195 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9196}
9197
80626a55
DE
9198/* Initialize the use of the DWP file for the current objfile.
9199 By convention the name of the DWP file is ${objfile}.dwp.
9200 The result is NULL if it can't be found. */
a766d390 9201
80626a55 9202static struct dwp_file *
ab5088bf 9203open_and_init_dwp_file (void)
80626a55
DE
9204{
9205 struct objfile *objfile = dwarf2_per_objfile->objfile;
9206 struct dwp_file *dwp_file;
9207 char *dwp_name;
9208 bfd *dbfd;
9209 struct cleanup *cleanups;
9210
2792b94d 9211 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9212 cleanups = make_cleanup (xfree, dwp_name);
9213
ab5088bf 9214 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9215 if (dbfd == NULL)
9216 {
9217 if (dwarf2_read_debug)
9218 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9219 do_cleanups (cleanups);
9220 return NULL;
3019eac3 9221 }
80626a55
DE
9222 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9223 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9224 dwp_name, strlen (dwp_name));
9225 dwp_file->dbfd = dbfd;
9226 do_cleanups (cleanups);
c906108c 9227
80626a55
DE
9228 /* +1: section 0 is unused */
9229 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9230 dwp_file->elf_sections =
9231 OBSTACK_CALLOC (&objfile->objfile_obstack,
9232 dwp_file->num_sections, asection *);
9233
9234 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9235
9236 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9237
9238 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9239
9240 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9241
80626a55
DE
9242 if (dwarf2_read_debug)
9243 {
9244 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9245 fprintf_unfiltered (gdb_stdlog,
9246 " %u CUs, %u TUs\n",
9247 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9248 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9249 }
9250
9251 return dwp_file;
3019eac3 9252}
c906108c 9253
ab5088bf
DE
9254/* Wrapper around open_and_init_dwp_file, only open it once. */
9255
9256static struct dwp_file *
9257get_dwp_file (void)
9258{
9259 if (! dwarf2_per_objfile->dwp_checked)
9260 {
9261 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9262 dwarf2_per_objfile->dwp_checked = 1;
9263 }
9264 return dwarf2_per_objfile->dwp_file;
9265}
9266
80626a55
DE
9267/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9268 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9269 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9270 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9271 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9272
9273 This is called, for example, when wanting to read a variable with a
9274 complex location. Therefore we don't want to do file i/o for every call.
9275 Therefore we don't want to look for a DWO file on every call.
9276 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9277 then we check if we've already seen DWO_NAME, and only THEN do we check
9278 for a DWO file.
9279
1c658ad5 9280 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9281 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9282
3019eac3 9283static struct dwo_unit *
80626a55
DE
9284lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9285 const char *dwo_name, const char *comp_dir,
9286 ULONGEST signature, int is_debug_types)
3019eac3
DE
9287{
9288 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9289 const char *kind = is_debug_types ? "TU" : "CU";
9290 void **dwo_file_slot;
3019eac3 9291 struct dwo_file *dwo_file;
80626a55 9292 struct dwp_file *dwp_file;
cb1df416 9293
80626a55 9294 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9295
ab5088bf 9296 dwp_file = get_dwp_file ();
80626a55 9297 if (dwp_file != NULL)
cf2c3c16 9298 {
80626a55
DE
9299 const struct dwp_hash_table *dwp_htab =
9300 is_debug_types ? dwp_file->tus : dwp_file->cus;
9301
9302 if (dwp_htab != NULL)
9303 {
9304 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9305 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9306 signature, is_debug_types);
80626a55
DE
9307
9308 if (dwo_cutu != NULL)
9309 {
9310 if (dwarf2_read_debug)
9311 {
9312 fprintf_unfiltered (gdb_stdlog,
9313 "Virtual DWO %s %s found: @%s\n",
9314 kind, hex_string (signature),
9315 host_address_to_string (dwo_cutu));
9316 }
9317 return dwo_cutu;
9318 }
9319 }
9320 }
9321
9322 /* Have we already seen DWO_NAME? */
9323
0ac5b59e 9324 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
80626a55
DE
9325 if (*dwo_file_slot == NULL)
9326 {
9327 /* Read in the file and build a table of the DWOs it contains. */
0ac5b59e 9328 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
80626a55
DE
9329 }
9330 /* NOTE: This will be NULL if unable to open the file. */
9331 dwo_file = *dwo_file_slot;
9332
9333 if (dwo_file != NULL)
9334 {
19c3d4c9 9335 struct dwo_unit *dwo_cutu = NULL;
80626a55 9336
19c3d4c9 9337 if (is_debug_types && dwo_file->tus)
80626a55 9338 {
19c3d4c9 9339 struct dwo_unit find_dwo_cutu;
9a619af0 9340
80626a55
DE
9341 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9342 find_dwo_cutu.signature = signature;
19c3d4c9
DE
9343 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9344 }
9345 else if (!is_debug_types && dwo_file->cu)
9346 {
9347 if (signature == dwo_file->cu->signature)
9348 dwo_cutu = dwo_file->cu;
9349 }
3019eac3 9350
19c3d4c9
DE
9351 if (dwo_cutu != NULL)
9352 {
9353 if (dwarf2_read_debug)
80626a55 9354 {
19c3d4c9
DE
9355 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9356 kind, dwo_name, hex_string (signature),
9357 host_address_to_string (dwo_cutu));
80626a55 9358 }
19c3d4c9 9359 return dwo_cutu;
80626a55 9360 }
2e276125 9361 }
9cdd5dbd 9362
80626a55
DE
9363 /* We didn't find it. This could mean a dwo_id mismatch, or
9364 someone deleted the DWO/DWP file, or the search path isn't set up
9365 correctly to find the file. */
9366
9367 if (dwarf2_read_debug)
9368 {
9369 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9370 kind, dwo_name, hex_string (signature));
9371 }
3019eac3
DE
9372
9373 complaint (&symfile_complaints,
6296d8c1 9374 _("Could not find DWO %s referenced by CU at offset 0x%x"
3019eac3 9375 " [in module %s]"),
6296d8c1 9376 kind, this_unit->offset.sect_off, objfile->name);
3019eac3 9377 return NULL;
5fb290d7
DJ
9378}
9379
80626a55
DE
9380/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9381 See lookup_dwo_cutu_unit for details. */
9382
9383static struct dwo_unit *
9384lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9385 const char *dwo_name, const char *comp_dir,
9386 ULONGEST signature)
9387{
9388 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9389}
9390
9391/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9392 See lookup_dwo_cutu_unit for details. */
9393
9394static struct dwo_unit *
9395lookup_dwo_type_unit (struct signatured_type *this_tu,
9396 const char *dwo_name, const char *comp_dir)
9397{
9398 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9399}
9400
3019eac3
DE
9401/* Free all resources associated with DWO_FILE.
9402 Close the DWO file and munmap the sections.
9403 All memory should be on the objfile obstack. */
348e048f
DE
9404
9405static void
3019eac3 9406free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9407{
3019eac3
DE
9408 int ix;
9409 struct dwarf2_section_info *section;
348e048f 9410
5c6fa7ab 9411 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9412 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9413
3019eac3
DE
9414 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9415}
348e048f 9416
3019eac3 9417/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9418
3019eac3
DE
9419static void
9420free_dwo_file_cleanup (void *arg)
9421{
9422 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9423 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9424
3019eac3
DE
9425 free_dwo_file (dwo_file, objfile);
9426}
348e048f 9427
3019eac3 9428/* Traversal function for free_dwo_files. */
2ab95328 9429
3019eac3
DE
9430static int
9431free_dwo_file_from_slot (void **slot, void *info)
9432{
9433 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9434 struct objfile *objfile = (struct objfile *) info;
348e048f 9435
3019eac3 9436 free_dwo_file (dwo_file, objfile);
348e048f 9437
3019eac3
DE
9438 return 1;
9439}
348e048f 9440
3019eac3 9441/* Free all resources associated with DWO_FILES. */
348e048f 9442
3019eac3
DE
9443static void
9444free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9445{
9446 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9447}
3019eac3
DE
9448\f
9449/* Read in various DIEs. */
348e048f 9450
d389af10
JK
9451/* qsort helper for inherit_abstract_dies. */
9452
9453static int
9454unsigned_int_compar (const void *ap, const void *bp)
9455{
9456 unsigned int a = *(unsigned int *) ap;
9457 unsigned int b = *(unsigned int *) bp;
9458
9459 return (a > b) - (b > a);
9460}
9461
9462/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9463 Inherit only the children of the DW_AT_abstract_origin DIE not being
9464 already referenced by DW_AT_abstract_origin from the children of the
9465 current DIE. */
d389af10
JK
9466
9467static void
9468inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9469{
9470 struct die_info *child_die;
9471 unsigned die_children_count;
9472 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9473 sect_offset *offsets;
9474 sect_offset *offsets_end, *offsetp;
d389af10
JK
9475 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9476 struct die_info *origin_die;
9477 /* Iterator of the ORIGIN_DIE children. */
9478 struct die_info *origin_child_die;
9479 struct cleanup *cleanups;
9480 struct attribute *attr;
cd02d79d
PA
9481 struct dwarf2_cu *origin_cu;
9482 struct pending **origin_previous_list_in_scope;
d389af10
JK
9483
9484 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9485 if (!attr)
9486 return;
9487
cd02d79d
PA
9488 /* Note that following die references may follow to a die in a
9489 different cu. */
9490
9491 origin_cu = cu;
9492 origin_die = follow_die_ref (die, attr, &origin_cu);
9493
9494 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9495 symbols in. */
9496 origin_previous_list_in_scope = origin_cu->list_in_scope;
9497 origin_cu->list_in_scope = cu->list_in_scope;
9498
edb3359d
DJ
9499 if (die->tag != origin_die->tag
9500 && !(die->tag == DW_TAG_inlined_subroutine
9501 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9502 complaint (&symfile_complaints,
9503 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9504 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9505
9506 child_die = die->child;
9507 die_children_count = 0;
9508 while (child_die && child_die->tag)
9509 {
9510 child_die = sibling_die (child_die);
9511 die_children_count++;
9512 }
9513 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9514 cleanups = make_cleanup (xfree, offsets);
9515
9516 offsets_end = offsets;
9517 child_die = die->child;
9518 while (child_die && child_die->tag)
9519 {
c38f313d
DJ
9520 /* For each CHILD_DIE, find the corresponding child of
9521 ORIGIN_DIE. If there is more than one layer of
9522 DW_AT_abstract_origin, follow them all; there shouldn't be,
9523 but GCC versions at least through 4.4 generate this (GCC PR
9524 40573). */
9525 struct die_info *child_origin_die = child_die;
cd02d79d 9526 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9527
c38f313d
DJ
9528 while (1)
9529 {
cd02d79d
PA
9530 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9531 child_origin_cu);
c38f313d
DJ
9532 if (attr == NULL)
9533 break;
cd02d79d
PA
9534 child_origin_die = follow_die_ref (child_origin_die, attr,
9535 &child_origin_cu);
c38f313d
DJ
9536 }
9537
d389af10
JK
9538 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9539 counterpart may exist. */
c38f313d 9540 if (child_origin_die != child_die)
d389af10 9541 {
edb3359d
DJ
9542 if (child_die->tag != child_origin_die->tag
9543 && !(child_die->tag == DW_TAG_inlined_subroutine
9544 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9545 complaint (&symfile_complaints,
9546 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9547 "different tags"), child_die->offset.sect_off,
9548 child_origin_die->offset.sect_off);
c38f313d
DJ
9549 if (child_origin_die->parent != origin_die)
9550 complaint (&symfile_complaints,
9551 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9552 "different parents"), child_die->offset.sect_off,
9553 child_origin_die->offset.sect_off);
c38f313d
DJ
9554 else
9555 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9556 }
9557 child_die = sibling_die (child_die);
9558 }
9559 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9560 unsigned_int_compar);
9561 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9562 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9563 complaint (&symfile_complaints,
9564 _("Multiple children of DIE 0x%x refer "
9565 "to DIE 0x%x as their abstract origin"),
b64f50a1 9566 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9567
9568 offsetp = offsets;
9569 origin_child_die = origin_die->child;
9570 while (origin_child_die && origin_child_die->tag)
9571 {
9572 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9573 while (offsetp < offsets_end
9574 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9575 offsetp++;
b64f50a1
JK
9576 if (offsetp >= offsets_end
9577 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9578 {
9579 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9580 process_die (origin_child_die, origin_cu);
d389af10
JK
9581 }
9582 origin_child_die = sibling_die (origin_child_die);
9583 }
cd02d79d 9584 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9585
9586 do_cleanups (cleanups);
9587}
9588
c906108c 9589static void
e7c27a73 9590read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9591{
e7c27a73 9592 struct objfile *objfile = cu->objfile;
52f0bd74 9593 struct context_stack *new;
c906108c
SS
9594 CORE_ADDR lowpc;
9595 CORE_ADDR highpc;
9596 struct die_info *child_die;
edb3359d 9597 struct attribute *attr, *call_line, *call_file;
15d034d0 9598 const char *name;
e142c38c 9599 CORE_ADDR baseaddr;
801e3a5b 9600 struct block *block;
edb3359d 9601 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9602 VEC (symbolp) *template_args = NULL;
9603 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9604
9605 if (inlined_func)
9606 {
9607 /* If we do not have call site information, we can't show the
9608 caller of this inlined function. That's too confusing, so
9609 only use the scope for local variables. */
9610 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9611 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9612 if (call_line == NULL || call_file == NULL)
9613 {
9614 read_lexical_block_scope (die, cu);
9615 return;
9616 }
9617 }
c906108c 9618
e142c38c
DJ
9619 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9620
94af9270 9621 name = dwarf2_name (die, cu);
c906108c 9622
e8d05480
JB
9623 /* Ignore functions with missing or empty names. These are actually
9624 illegal according to the DWARF standard. */
9625 if (name == NULL)
9626 {
9627 complaint (&symfile_complaints,
b64f50a1
JK
9628 _("missing name for subprogram DIE at %d"),
9629 die->offset.sect_off);
e8d05480
JB
9630 return;
9631 }
9632
9633 /* Ignore functions with missing or invalid low and high pc attributes. */
9634 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9635 {
ae4d0c03
PM
9636 attr = dwarf2_attr (die, DW_AT_external, cu);
9637 if (!attr || !DW_UNSND (attr))
9638 complaint (&symfile_complaints,
3e43a32a
MS
9639 _("cannot get low and high bounds "
9640 "for subprogram DIE at %d"),
b64f50a1 9641 die->offset.sect_off);
e8d05480
JB
9642 return;
9643 }
c906108c
SS
9644
9645 lowpc += baseaddr;
9646 highpc += baseaddr;
9647
34eaf542
TT
9648 /* If we have any template arguments, then we must allocate a
9649 different sort of symbol. */
9650 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9651 {
9652 if (child_die->tag == DW_TAG_template_type_param
9653 || child_die->tag == DW_TAG_template_value_param)
9654 {
e623cf5d 9655 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
9656 templ_func->base.is_cplus_template_function = 1;
9657 break;
9658 }
9659 }
9660
c906108c 9661 new = push_context (0, lowpc);
34eaf542
TT
9662 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9663 (struct symbol *) templ_func);
4c2df51b 9664
4cecd739
DJ
9665 /* If there is a location expression for DW_AT_frame_base, record
9666 it. */
e142c38c 9667 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9668 if (attr)
f1e6e072 9669 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9670
e142c38c 9671 cu->list_in_scope = &local_symbols;
c906108c 9672
639d11d3 9673 if (die->child != NULL)
c906108c 9674 {
639d11d3 9675 child_die = die->child;
c906108c
SS
9676 while (child_die && child_die->tag)
9677 {
34eaf542
TT
9678 if (child_die->tag == DW_TAG_template_type_param
9679 || child_die->tag == DW_TAG_template_value_param)
9680 {
9681 struct symbol *arg = new_symbol (child_die, NULL, cu);
9682
f1078f66
DJ
9683 if (arg != NULL)
9684 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9685 }
9686 else
9687 process_die (child_die, cu);
c906108c
SS
9688 child_die = sibling_die (child_die);
9689 }
9690 }
9691
d389af10
JK
9692 inherit_abstract_dies (die, cu);
9693
4a811a97
UW
9694 /* If we have a DW_AT_specification, we might need to import using
9695 directives from the context of the specification DIE. See the
9696 comment in determine_prefix. */
9697 if (cu->language == language_cplus
9698 && dwarf2_attr (die, DW_AT_specification, cu))
9699 {
9700 struct dwarf2_cu *spec_cu = cu;
9701 struct die_info *spec_die = die_specification (die, &spec_cu);
9702
9703 while (spec_die)
9704 {
9705 child_die = spec_die->child;
9706 while (child_die && child_die->tag)
9707 {
9708 if (child_die->tag == DW_TAG_imported_module)
9709 process_die (child_die, spec_cu);
9710 child_die = sibling_die (child_die);
9711 }
9712
9713 /* In some cases, GCC generates specification DIEs that
9714 themselves contain DW_AT_specification attributes. */
9715 spec_die = die_specification (spec_die, &spec_cu);
9716 }
9717 }
9718
c906108c
SS
9719 new = pop_context ();
9720 /* Make a block for the local symbols within. */
801e3a5b
JB
9721 block = finish_block (new->name, &local_symbols, new->old_blocks,
9722 lowpc, highpc, objfile);
9723
df8a16a1 9724 /* For C++, set the block's scope. */
195a3f6c 9725 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9726 && cu->processing_has_namespace_info)
195a3f6c
TT
9727 block_set_scope (block, determine_prefix (die, cu),
9728 &objfile->objfile_obstack);
df8a16a1 9729
801e3a5b
JB
9730 /* If we have address ranges, record them. */
9731 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9732
34eaf542
TT
9733 /* Attach template arguments to function. */
9734 if (! VEC_empty (symbolp, template_args))
9735 {
9736 gdb_assert (templ_func != NULL);
9737
9738 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9739 templ_func->template_arguments
9740 = obstack_alloc (&objfile->objfile_obstack,
9741 (templ_func->n_template_arguments
9742 * sizeof (struct symbol *)));
9743 memcpy (templ_func->template_arguments,
9744 VEC_address (symbolp, template_args),
9745 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9746 VEC_free (symbolp, template_args);
9747 }
9748
208d8187
JB
9749 /* In C++, we can have functions nested inside functions (e.g., when
9750 a function declares a class that has methods). This means that
9751 when we finish processing a function scope, we may need to go
9752 back to building a containing block's symbol lists. */
9753 local_symbols = new->locals;
27aa8d6a 9754 using_directives = new->using_directives;
208d8187 9755
921e78cf
JB
9756 /* If we've finished processing a top-level function, subsequent
9757 symbols go in the file symbol list. */
9758 if (outermost_context_p ())
e142c38c 9759 cu->list_in_scope = &file_symbols;
c906108c
SS
9760}
9761
9762/* Process all the DIES contained within a lexical block scope. Start
9763 a new scope, process the dies, and then close the scope. */
9764
9765static void
e7c27a73 9766read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9767{
e7c27a73 9768 struct objfile *objfile = cu->objfile;
52f0bd74 9769 struct context_stack *new;
c906108c
SS
9770 CORE_ADDR lowpc, highpc;
9771 struct die_info *child_die;
e142c38c
DJ
9772 CORE_ADDR baseaddr;
9773
9774 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9775
9776 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9777 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9778 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9779 be nasty. Might be easier to properly extend generic blocks to
af34e669 9780 describe ranges. */
d85a05f0 9781 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9782 return;
9783 lowpc += baseaddr;
9784 highpc += baseaddr;
9785
9786 push_context (0, lowpc);
639d11d3 9787 if (die->child != NULL)
c906108c 9788 {
639d11d3 9789 child_die = die->child;
c906108c
SS
9790 while (child_die && child_die->tag)
9791 {
e7c27a73 9792 process_die (child_die, cu);
c906108c
SS
9793 child_die = sibling_die (child_die);
9794 }
9795 }
9796 new = pop_context ();
9797
8540c487 9798 if (local_symbols != NULL || using_directives != NULL)
c906108c 9799 {
801e3a5b
JB
9800 struct block *block
9801 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9802 highpc, objfile);
9803
9804 /* Note that recording ranges after traversing children, as we
9805 do here, means that recording a parent's ranges entails
9806 walking across all its children's ranges as they appear in
9807 the address map, which is quadratic behavior.
9808
9809 It would be nicer to record the parent's ranges before
9810 traversing its children, simply overriding whatever you find
9811 there. But since we don't even decide whether to create a
9812 block until after we've traversed its children, that's hard
9813 to do. */
9814 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9815 }
9816 local_symbols = new->locals;
27aa8d6a 9817 using_directives = new->using_directives;
c906108c
SS
9818}
9819
96408a79
SA
9820/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9821
9822static void
9823read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9824{
9825 struct objfile *objfile = cu->objfile;
9826 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9827 CORE_ADDR pc, baseaddr;
9828 struct attribute *attr;
9829 struct call_site *call_site, call_site_local;
9830 void **slot;
9831 int nparams;
9832 struct die_info *child_die;
9833
9834 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9835
9836 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9837 if (!attr)
9838 {
9839 complaint (&symfile_complaints,
9840 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9841 "DIE 0x%x [in module %s]"),
b64f50a1 9842 die->offset.sect_off, objfile->name);
96408a79
SA
9843 return;
9844 }
9845 pc = DW_ADDR (attr) + baseaddr;
9846
9847 if (cu->call_site_htab == NULL)
9848 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9849 NULL, &objfile->objfile_obstack,
9850 hashtab_obstack_allocate, NULL);
9851 call_site_local.pc = pc;
9852 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9853 if (*slot != NULL)
9854 {
9855 complaint (&symfile_complaints,
9856 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9857 "DIE 0x%x [in module %s]"),
b64f50a1 9858 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9859 return;
9860 }
9861
9862 /* Count parameters at the caller. */
9863
9864 nparams = 0;
9865 for (child_die = die->child; child_die && child_die->tag;
9866 child_die = sibling_die (child_die))
9867 {
9868 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9869 {
9870 complaint (&symfile_complaints,
9871 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9872 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9873 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9874 continue;
9875 }
9876
9877 nparams++;
9878 }
9879
9880 call_site = obstack_alloc (&objfile->objfile_obstack,
9881 (sizeof (*call_site)
9882 + (sizeof (*call_site->parameter)
9883 * (nparams - 1))));
9884 *slot = call_site;
9885 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9886 call_site->pc = pc;
9887
9888 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9889 {
9890 struct die_info *func_die;
9891
9892 /* Skip also over DW_TAG_inlined_subroutine. */
9893 for (func_die = die->parent;
9894 func_die && func_die->tag != DW_TAG_subprogram
9895 && func_die->tag != DW_TAG_subroutine_type;
9896 func_die = func_die->parent);
9897
9898 /* DW_AT_GNU_all_call_sites is a superset
9899 of DW_AT_GNU_all_tail_call_sites. */
9900 if (func_die
9901 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9902 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9903 {
9904 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9905 not complete. But keep CALL_SITE for look ups via call_site_htab,
9906 both the initial caller containing the real return address PC and
9907 the final callee containing the current PC of a chain of tail
9908 calls do not need to have the tail call list complete. But any
9909 function candidate for a virtual tail call frame searched via
9910 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9911 determined unambiguously. */
9912 }
9913 else
9914 {
9915 struct type *func_type = NULL;
9916
9917 if (func_die)
9918 func_type = get_die_type (func_die, cu);
9919 if (func_type != NULL)
9920 {
9921 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9922
9923 /* Enlist this call site to the function. */
9924 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9925 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9926 }
9927 else
9928 complaint (&symfile_complaints,
9929 _("Cannot find function owning DW_TAG_GNU_call_site "
9930 "DIE 0x%x [in module %s]"),
b64f50a1 9931 die->offset.sect_off, objfile->name);
96408a79
SA
9932 }
9933 }
9934
9935 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9936 if (attr == NULL)
9937 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9938 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9939 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9940 /* Keep NULL DWARF_BLOCK. */;
9941 else if (attr_form_is_block (attr))
9942 {
9943 struct dwarf2_locexpr_baton *dlbaton;
9944
9945 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9946 dlbaton->data = DW_BLOCK (attr)->data;
9947 dlbaton->size = DW_BLOCK (attr)->size;
9948 dlbaton->per_cu = cu->per_cu;
9949
9950 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9951 }
9952 else if (is_ref_attr (attr))
9953 {
96408a79
SA
9954 struct dwarf2_cu *target_cu = cu;
9955 struct die_info *target_die;
9956
ac9ec31b 9957 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
9958 gdb_assert (target_cu->objfile == objfile);
9959 if (die_is_declaration (target_die, target_cu))
9960 {
9112db09
JK
9961 const char *target_physname = NULL;
9962 struct attribute *target_attr;
9963
9964 /* Prefer the mangled name; otherwise compute the demangled one. */
9965 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9966 if (target_attr == NULL)
9967 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9968 target_cu);
9969 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9970 target_physname = DW_STRING (target_attr);
9971 else
9972 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9973 if (target_physname == NULL)
9974 complaint (&symfile_complaints,
9975 _("DW_AT_GNU_call_site_target target DIE has invalid "
9976 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9977 die->offset.sect_off, objfile->name);
96408a79 9978 else
7d455152 9979 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9980 }
9981 else
9982 {
9983 CORE_ADDR lowpc;
9984
9985 /* DW_AT_entry_pc should be preferred. */
9986 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9987 complaint (&symfile_complaints,
9988 _("DW_AT_GNU_call_site_target target DIE has invalid "
9989 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9990 die->offset.sect_off, objfile->name);
96408a79
SA
9991 else
9992 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9993 }
9994 }
9995 else
9996 complaint (&symfile_complaints,
9997 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9998 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9999 die->offset.sect_off, objfile->name);
96408a79
SA
10000
10001 call_site->per_cu = cu->per_cu;
10002
10003 for (child_die = die->child;
10004 child_die && child_die->tag;
10005 child_die = sibling_die (child_die))
10006 {
96408a79 10007 struct call_site_parameter *parameter;
1788b2d3 10008 struct attribute *loc, *origin;
96408a79
SA
10009
10010 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10011 {
10012 /* Already printed the complaint above. */
10013 continue;
10014 }
10015
10016 gdb_assert (call_site->parameter_count < nparams);
10017 parameter = &call_site->parameter[call_site->parameter_count];
10018
1788b2d3
JK
10019 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10020 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10021 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10022
24c5c679 10023 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
10024 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10025 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10026 {
10027 sect_offset offset;
10028
10029 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10030 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10031 if (!offset_in_cu_p (&cu->header, offset))
10032 {
10033 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10034 binding can be done only inside one CU. Such referenced DIE
10035 therefore cannot be even moved to DW_TAG_partial_unit. */
10036 complaint (&symfile_complaints,
10037 _("DW_AT_abstract_origin offset is not in CU for "
10038 "DW_TAG_GNU_call_site child DIE 0x%x "
10039 "[in module %s]"),
10040 child_die->offset.sect_off, objfile->name);
10041 continue;
10042 }
1788b2d3
JK
10043 parameter->u.param_offset.cu_off = (offset.sect_off
10044 - cu->header.offset.sect_off);
10045 }
10046 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10047 {
10048 complaint (&symfile_complaints,
10049 _("No DW_FORM_block* DW_AT_location for "
10050 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10051 child_die->offset.sect_off, objfile->name);
96408a79
SA
10052 continue;
10053 }
24c5c679 10054 else
96408a79 10055 {
24c5c679
JK
10056 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10057 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10058 if (parameter->u.dwarf_reg != -1)
10059 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10060 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10061 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10062 &parameter->u.fb_offset))
10063 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10064 else
10065 {
10066 complaint (&symfile_complaints,
10067 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10068 "for DW_FORM_block* DW_AT_location is supported for "
10069 "DW_TAG_GNU_call_site child DIE 0x%x "
10070 "[in module %s]"),
10071 child_die->offset.sect_off, objfile->name);
10072 continue;
10073 }
96408a79
SA
10074 }
10075
10076 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10077 if (!attr_form_is_block (attr))
10078 {
10079 complaint (&symfile_complaints,
10080 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10081 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10082 child_die->offset.sect_off, objfile->name);
96408a79
SA
10083 continue;
10084 }
10085 parameter->value = DW_BLOCK (attr)->data;
10086 parameter->value_size = DW_BLOCK (attr)->size;
10087
10088 /* Parameters are not pre-cleared by memset above. */
10089 parameter->data_value = NULL;
10090 parameter->data_value_size = 0;
10091 call_site->parameter_count++;
10092
10093 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10094 if (attr)
10095 {
10096 if (!attr_form_is_block (attr))
10097 complaint (&symfile_complaints,
10098 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10099 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10100 child_die->offset.sect_off, objfile->name);
96408a79
SA
10101 else
10102 {
10103 parameter->data_value = DW_BLOCK (attr)->data;
10104 parameter->data_value_size = DW_BLOCK (attr)->size;
10105 }
10106 }
10107 }
10108}
10109
43039443 10110/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10111 Return 1 if the attributes are present and valid, otherwise, return 0.
10112 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10113
10114static int
10115dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10116 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10117 struct partial_symtab *ranges_pst)
43039443
JK
10118{
10119 struct objfile *objfile = cu->objfile;
10120 struct comp_unit_head *cu_header = &cu->header;
10121 bfd *obfd = objfile->obfd;
10122 unsigned int addr_size = cu_header->addr_size;
10123 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10124 /* Base address selection entry. */
10125 CORE_ADDR base;
10126 int found_base;
10127 unsigned int dummy;
d521ce57 10128 const gdb_byte *buffer;
43039443
JK
10129 CORE_ADDR marker;
10130 int low_set;
10131 CORE_ADDR low = 0;
10132 CORE_ADDR high = 0;
ff013f42 10133 CORE_ADDR baseaddr;
43039443 10134
d00adf39
DE
10135 found_base = cu->base_known;
10136 base = cu->base_address;
43039443 10137
be391dca 10138 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10139 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10140 {
10141 complaint (&symfile_complaints,
10142 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10143 offset);
10144 return 0;
10145 }
dce234bc 10146 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10147
10148 /* Read in the largest possible address. */
10149 marker = read_address (obfd, buffer, cu, &dummy);
10150 if ((marker & mask) == mask)
10151 {
10152 /* If we found the largest possible address, then
10153 read the base address. */
10154 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10155 buffer += 2 * addr_size;
10156 offset += 2 * addr_size;
10157 found_base = 1;
10158 }
10159
10160 low_set = 0;
10161
e7030f15 10162 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10163
43039443
JK
10164 while (1)
10165 {
10166 CORE_ADDR range_beginning, range_end;
10167
10168 range_beginning = read_address (obfd, buffer, cu, &dummy);
10169 buffer += addr_size;
10170 range_end = read_address (obfd, buffer, cu, &dummy);
10171 buffer += addr_size;
10172 offset += 2 * addr_size;
10173
10174 /* An end of list marker is a pair of zero addresses. */
10175 if (range_beginning == 0 && range_end == 0)
10176 /* Found the end of list entry. */
10177 break;
10178
10179 /* Each base address selection entry is a pair of 2 values.
10180 The first is the largest possible address, the second is
10181 the base address. Check for a base address here. */
10182 if ((range_beginning & mask) == mask)
10183 {
10184 /* If we found the largest possible address, then
10185 read the base address. */
10186 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10187 found_base = 1;
10188 continue;
10189 }
10190
10191 if (!found_base)
10192 {
10193 /* We have no valid base address for the ranges
10194 data. */
10195 complaint (&symfile_complaints,
10196 _("Invalid .debug_ranges data (no base address)"));
10197 return 0;
10198 }
10199
9277c30c
UW
10200 if (range_beginning > range_end)
10201 {
10202 /* Inverted range entries are invalid. */
10203 complaint (&symfile_complaints,
10204 _("Invalid .debug_ranges data (inverted range)"));
10205 return 0;
10206 }
10207
10208 /* Empty range entries have no effect. */
10209 if (range_beginning == range_end)
10210 continue;
10211
43039443
JK
10212 range_beginning += base;
10213 range_end += base;
10214
01093045
DE
10215 /* A not-uncommon case of bad debug info.
10216 Don't pollute the addrmap with bad data. */
10217 if (range_beginning + baseaddr == 0
10218 && !dwarf2_per_objfile->has_section_at_zero)
10219 {
10220 complaint (&symfile_complaints,
10221 _(".debug_ranges entry has start address of zero"
10222 " [in module %s]"), objfile->name);
10223 continue;
10224 }
10225
9277c30c 10226 if (ranges_pst != NULL)
ff013f42 10227 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10228 range_beginning + baseaddr,
10229 range_end - 1 + baseaddr,
ff013f42
JK
10230 ranges_pst);
10231
43039443
JK
10232 /* FIXME: This is recording everything as a low-high
10233 segment of consecutive addresses. We should have a
10234 data structure for discontiguous block ranges
10235 instead. */
10236 if (! low_set)
10237 {
10238 low = range_beginning;
10239 high = range_end;
10240 low_set = 1;
10241 }
10242 else
10243 {
10244 if (range_beginning < low)
10245 low = range_beginning;
10246 if (range_end > high)
10247 high = range_end;
10248 }
10249 }
10250
10251 if (! low_set)
10252 /* If the first entry is an end-of-list marker, the range
10253 describes an empty scope, i.e. no instructions. */
10254 return 0;
10255
10256 if (low_return)
10257 *low_return = low;
10258 if (high_return)
10259 *high_return = high;
10260 return 1;
10261}
10262
af34e669
DJ
10263/* Get low and high pc attributes from a die. Return 1 if the attributes
10264 are present and valid, otherwise, return 0. Return -1 if the range is
10265 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10266
c906108c 10267static int
af34e669 10268dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10269 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10270 struct partial_symtab *pst)
c906108c
SS
10271{
10272 struct attribute *attr;
91da1414 10273 struct attribute *attr_high;
af34e669
DJ
10274 CORE_ADDR low = 0;
10275 CORE_ADDR high = 0;
10276 int ret = 0;
c906108c 10277
91da1414
MW
10278 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10279 if (attr_high)
af34e669 10280 {
e142c38c 10281 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10282 if (attr)
91da1414
MW
10283 {
10284 low = DW_ADDR (attr);
3019eac3
DE
10285 if (attr_high->form == DW_FORM_addr
10286 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10287 high = DW_ADDR (attr_high);
10288 else
10289 high = low + DW_UNSND (attr_high);
10290 }
af34e669
DJ
10291 else
10292 /* Found high w/o low attribute. */
10293 return 0;
10294
10295 /* Found consecutive range of addresses. */
10296 ret = 1;
10297 }
c906108c 10298 else
af34e669 10299 {
e142c38c 10300 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10301 if (attr != NULL)
10302 {
ab435259
DE
10303 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10304 We take advantage of the fact that DW_AT_ranges does not appear
10305 in DW_TAG_compile_unit of DWO files. */
10306 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10307 unsigned int ranges_offset = (DW_UNSND (attr)
10308 + (need_ranges_base
10309 ? cu->ranges_base
10310 : 0));
2e3cf129 10311
af34e669 10312 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10313 .debug_ranges section. */
2e3cf129 10314 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10315 return 0;
43039443 10316 /* Found discontinuous range of addresses. */
af34e669
DJ
10317 ret = -1;
10318 }
10319 }
c906108c 10320
9373cf26
JK
10321 /* read_partial_die has also the strict LOW < HIGH requirement. */
10322 if (high <= low)
c906108c
SS
10323 return 0;
10324
10325 /* When using the GNU linker, .gnu.linkonce. sections are used to
10326 eliminate duplicate copies of functions and vtables and such.
10327 The linker will arbitrarily choose one and discard the others.
10328 The AT_*_pc values for such functions refer to local labels in
10329 these sections. If the section from that file was discarded, the
10330 labels are not in the output, so the relocs get a value of 0.
10331 If this is a discarded function, mark the pc bounds as invalid,
10332 so that GDB will ignore it. */
72dca2f5 10333 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10334 return 0;
10335
10336 *lowpc = low;
96408a79
SA
10337 if (highpc)
10338 *highpc = high;
af34e669 10339 return ret;
c906108c
SS
10340}
10341
b084d499
JB
10342/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10343 its low and high PC addresses. Do nothing if these addresses could not
10344 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10345 and HIGHPC to the high address if greater than HIGHPC. */
10346
10347static void
10348dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10349 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10350 struct dwarf2_cu *cu)
10351{
10352 CORE_ADDR low, high;
10353 struct die_info *child = die->child;
10354
d85a05f0 10355 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10356 {
10357 *lowpc = min (*lowpc, low);
10358 *highpc = max (*highpc, high);
10359 }
10360
10361 /* If the language does not allow nested subprograms (either inside
10362 subprograms or lexical blocks), we're done. */
10363 if (cu->language != language_ada)
10364 return;
6e70227d 10365
b084d499
JB
10366 /* Check all the children of the given DIE. If it contains nested
10367 subprograms, then check their pc bounds. Likewise, we need to
10368 check lexical blocks as well, as they may also contain subprogram
10369 definitions. */
10370 while (child && child->tag)
10371 {
10372 if (child->tag == DW_TAG_subprogram
10373 || child->tag == DW_TAG_lexical_block)
10374 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10375 child = sibling_die (child);
10376 }
10377}
10378
fae299cd
DC
10379/* Get the low and high pc's represented by the scope DIE, and store
10380 them in *LOWPC and *HIGHPC. If the correct values can't be
10381 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10382
10383static void
10384get_scope_pc_bounds (struct die_info *die,
10385 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10386 struct dwarf2_cu *cu)
10387{
10388 CORE_ADDR best_low = (CORE_ADDR) -1;
10389 CORE_ADDR best_high = (CORE_ADDR) 0;
10390 CORE_ADDR current_low, current_high;
10391
d85a05f0 10392 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10393 {
10394 best_low = current_low;
10395 best_high = current_high;
10396 }
10397 else
10398 {
10399 struct die_info *child = die->child;
10400
10401 while (child && child->tag)
10402 {
10403 switch (child->tag) {
10404 case DW_TAG_subprogram:
b084d499 10405 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10406 break;
10407 case DW_TAG_namespace:
f55ee35c 10408 case DW_TAG_module:
fae299cd
DC
10409 /* FIXME: carlton/2004-01-16: Should we do this for
10410 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10411 that current GCC's always emit the DIEs corresponding
10412 to definitions of methods of classes as children of a
10413 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10414 the DIEs giving the declarations, which could be
10415 anywhere). But I don't see any reason why the
10416 standards says that they have to be there. */
10417 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10418
10419 if (current_low != ((CORE_ADDR) -1))
10420 {
10421 best_low = min (best_low, current_low);
10422 best_high = max (best_high, current_high);
10423 }
10424 break;
10425 default:
0963b4bd 10426 /* Ignore. */
fae299cd
DC
10427 break;
10428 }
10429
10430 child = sibling_die (child);
10431 }
10432 }
10433
10434 *lowpc = best_low;
10435 *highpc = best_high;
10436}
10437
801e3a5b
JB
10438/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10439 in DIE. */
380bca97 10440
801e3a5b
JB
10441static void
10442dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10443 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10444{
bb5ed363 10445 struct objfile *objfile = cu->objfile;
801e3a5b 10446 struct attribute *attr;
91da1414 10447 struct attribute *attr_high;
801e3a5b 10448
91da1414
MW
10449 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10450 if (attr_high)
801e3a5b 10451 {
801e3a5b
JB
10452 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10453 if (attr)
10454 {
10455 CORE_ADDR low = DW_ADDR (attr);
91da1414 10456 CORE_ADDR high;
3019eac3
DE
10457 if (attr_high->form == DW_FORM_addr
10458 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10459 high = DW_ADDR (attr_high);
10460 else
10461 high = low + DW_UNSND (attr_high);
9a619af0 10462
801e3a5b
JB
10463 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10464 }
10465 }
10466
10467 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10468 if (attr)
10469 {
bb5ed363 10470 bfd *obfd = objfile->obfd;
ab435259
DE
10471 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10472 We take advantage of the fact that DW_AT_ranges does not appear
10473 in DW_TAG_compile_unit of DWO files. */
10474 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10475
10476 /* The value of the DW_AT_ranges attribute is the offset of the
10477 address range list in the .debug_ranges section. */
ab435259
DE
10478 unsigned long offset = (DW_UNSND (attr)
10479 + (need_ranges_base ? cu->ranges_base : 0));
d521ce57 10480 const gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10481
10482 /* For some target architectures, but not others, the
10483 read_address function sign-extends the addresses it returns.
10484 To recognize base address selection entries, we need a
10485 mask. */
10486 unsigned int addr_size = cu->header.addr_size;
10487 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10488
10489 /* The base address, to which the next pair is relative. Note
10490 that this 'base' is a DWARF concept: most entries in a range
10491 list are relative, to reduce the number of relocs against the
10492 debugging information. This is separate from this function's
10493 'baseaddr' argument, which GDB uses to relocate debugging
10494 information from a shared library based on the address at
10495 which the library was loaded. */
d00adf39
DE
10496 CORE_ADDR base = cu->base_address;
10497 int base_known = cu->base_known;
801e3a5b 10498
be391dca 10499 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10500 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10501 {
10502 complaint (&symfile_complaints,
10503 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10504 offset);
10505 return;
10506 }
10507
10508 for (;;)
10509 {
10510 unsigned int bytes_read;
10511 CORE_ADDR start, end;
10512
10513 start = read_address (obfd, buffer, cu, &bytes_read);
10514 buffer += bytes_read;
10515 end = read_address (obfd, buffer, cu, &bytes_read);
10516 buffer += bytes_read;
10517
10518 /* Did we find the end of the range list? */
10519 if (start == 0 && end == 0)
10520 break;
10521
10522 /* Did we find a base address selection entry? */
10523 else if ((start & base_select_mask) == base_select_mask)
10524 {
10525 base = end;
10526 base_known = 1;
10527 }
10528
10529 /* We found an ordinary address range. */
10530 else
10531 {
10532 if (!base_known)
10533 {
10534 complaint (&symfile_complaints,
3e43a32a
MS
10535 _("Invalid .debug_ranges data "
10536 "(no base address)"));
801e3a5b
JB
10537 return;
10538 }
10539
9277c30c
UW
10540 if (start > end)
10541 {
10542 /* Inverted range entries are invalid. */
10543 complaint (&symfile_complaints,
10544 _("Invalid .debug_ranges data "
10545 "(inverted range)"));
10546 return;
10547 }
10548
10549 /* Empty range entries have no effect. */
10550 if (start == end)
10551 continue;
10552
01093045
DE
10553 start += base + baseaddr;
10554 end += base + baseaddr;
10555
10556 /* A not-uncommon case of bad debug info.
10557 Don't pollute the addrmap with bad data. */
10558 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10559 {
10560 complaint (&symfile_complaints,
10561 _(".debug_ranges entry has start address of zero"
10562 " [in module %s]"), objfile->name);
10563 continue;
10564 }
10565
10566 record_block_range (block, start, end - 1);
801e3a5b
JB
10567 }
10568 }
10569 }
10570}
10571
685b1105
JK
10572/* Check whether the producer field indicates either of GCC < 4.6, or the
10573 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10574
685b1105
JK
10575static void
10576check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10577{
10578 const char *cs;
10579 int major, minor, release;
10580
10581 if (cu->producer == NULL)
10582 {
10583 /* For unknown compilers expect their behavior is DWARF version
10584 compliant.
10585
10586 GCC started to support .debug_types sections by -gdwarf-4 since
10587 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10588 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10589 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10590 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10591 }
685b1105 10592 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10593 {
685b1105
JK
10594 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10595
ba919b58
TT
10596 cs = &cu->producer[strlen ("GNU ")];
10597 while (*cs && !isdigit (*cs))
10598 cs++;
10599 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10600 {
10601 /* Not recognized as GCC. */
10602 }
10603 else
1b80a9fa
JK
10604 {
10605 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10606 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10607 }
685b1105
JK
10608 }
10609 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10610 cu->producer_is_icc = 1;
10611 else
10612 {
10613 /* For other non-GCC compilers, expect their behavior is DWARF version
10614 compliant. */
60d5a603
JK
10615 }
10616
ba919b58 10617 cu->checked_producer = 1;
685b1105 10618}
ba919b58 10619
685b1105
JK
10620/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10621 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10622 during 4.6.0 experimental. */
10623
10624static int
10625producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10626{
10627 if (!cu->checked_producer)
10628 check_producer (cu);
10629
10630 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10631}
10632
10633/* Return the default accessibility type if it is not overriden by
10634 DW_AT_accessibility. */
10635
10636static enum dwarf_access_attribute
10637dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10638{
10639 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10640 {
10641 /* The default DWARF 2 accessibility for members is public, the default
10642 accessibility for inheritance is private. */
10643
10644 if (die->tag != DW_TAG_inheritance)
10645 return DW_ACCESS_public;
10646 else
10647 return DW_ACCESS_private;
10648 }
10649 else
10650 {
10651 /* DWARF 3+ defines the default accessibility a different way. The same
10652 rules apply now for DW_TAG_inheritance as for the members and it only
10653 depends on the container kind. */
10654
10655 if (die->parent->tag == DW_TAG_class_type)
10656 return DW_ACCESS_private;
10657 else
10658 return DW_ACCESS_public;
10659 }
10660}
10661
74ac6d43
TT
10662/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10663 offset. If the attribute was not found return 0, otherwise return
10664 1. If it was found but could not properly be handled, set *OFFSET
10665 to 0. */
10666
10667static int
10668handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10669 LONGEST *offset)
10670{
10671 struct attribute *attr;
10672
10673 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10674 if (attr != NULL)
10675 {
10676 *offset = 0;
10677
10678 /* Note that we do not check for a section offset first here.
10679 This is because DW_AT_data_member_location is new in DWARF 4,
10680 so if we see it, we can assume that a constant form is really
10681 a constant and not a section offset. */
10682 if (attr_form_is_constant (attr))
10683 *offset = dwarf2_get_attr_constant_value (attr, 0);
10684 else if (attr_form_is_section_offset (attr))
10685 dwarf2_complex_location_expr_complaint ();
10686 else if (attr_form_is_block (attr))
10687 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10688 else
10689 dwarf2_complex_location_expr_complaint ();
10690
10691 return 1;
10692 }
10693
10694 return 0;
10695}
10696
c906108c
SS
10697/* Add an aggregate field to the field list. */
10698
10699static void
107d2387 10700dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10701 struct dwarf2_cu *cu)
6e70227d 10702{
e7c27a73 10703 struct objfile *objfile = cu->objfile;
5e2b427d 10704 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10705 struct nextfield *new_field;
10706 struct attribute *attr;
10707 struct field *fp;
15d034d0 10708 const char *fieldname = "";
c906108c
SS
10709
10710 /* Allocate a new field list entry and link it in. */
10711 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10712 make_cleanup (xfree, new_field);
c906108c 10713 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10714
10715 if (die->tag == DW_TAG_inheritance)
10716 {
10717 new_field->next = fip->baseclasses;
10718 fip->baseclasses = new_field;
10719 }
10720 else
10721 {
10722 new_field->next = fip->fields;
10723 fip->fields = new_field;
10724 }
c906108c
SS
10725 fip->nfields++;
10726
e142c38c 10727 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10728 if (attr)
10729 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10730 else
10731 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10732 if (new_field->accessibility != DW_ACCESS_public)
10733 fip->non_public_fields = 1;
60d5a603 10734
e142c38c 10735 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10736 if (attr)
10737 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10738 else
10739 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10740
10741 fp = &new_field->field;
a9a9bd0f 10742
e142c38c 10743 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10744 {
74ac6d43
TT
10745 LONGEST offset;
10746
a9a9bd0f 10747 /* Data member other than a C++ static data member. */
6e70227d 10748
c906108c 10749 /* Get type of field. */
e7c27a73 10750 fp->type = die_type (die, cu);
c906108c 10751
d6a843b5 10752 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10753
c906108c 10754 /* Get bit size of field (zero if none). */
e142c38c 10755 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10756 if (attr)
10757 {
10758 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10759 }
10760 else
10761 {
10762 FIELD_BITSIZE (*fp) = 0;
10763 }
10764
10765 /* Get bit offset of field. */
74ac6d43
TT
10766 if (handle_data_member_location (die, cu, &offset))
10767 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10768 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10769 if (attr)
10770 {
5e2b427d 10771 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10772 {
10773 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10774 additional bit offset from the MSB of the containing
10775 anonymous object to the MSB of the field. We don't
10776 have to do anything special since we don't need to
10777 know the size of the anonymous object. */
f41f5e61 10778 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10779 }
10780 else
10781 {
10782 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10783 MSB of the anonymous object, subtract off the number of
10784 bits from the MSB of the field to the MSB of the
10785 object, and then subtract off the number of bits of
10786 the field itself. The result is the bit offset of
10787 the LSB of the field. */
c906108c
SS
10788 int anonymous_size;
10789 int bit_offset = DW_UNSND (attr);
10790
e142c38c 10791 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10792 if (attr)
10793 {
10794 /* The size of the anonymous object containing
10795 the bit field is explicit, so use the
10796 indicated size (in bytes). */
10797 anonymous_size = DW_UNSND (attr);
10798 }
10799 else
10800 {
10801 /* The size of the anonymous object containing
10802 the bit field must be inferred from the type
10803 attribute of the data member containing the
10804 bit field. */
10805 anonymous_size = TYPE_LENGTH (fp->type);
10806 }
f41f5e61
PA
10807 SET_FIELD_BITPOS (*fp,
10808 (FIELD_BITPOS (*fp)
10809 + anonymous_size * bits_per_byte
10810 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10811 }
10812 }
10813
10814 /* Get name of field. */
39cbfefa
DJ
10815 fieldname = dwarf2_name (die, cu);
10816 if (fieldname == NULL)
10817 fieldname = "";
d8151005
DJ
10818
10819 /* The name is already allocated along with this objfile, so we don't
10820 need to duplicate it for the type. */
10821 fp->name = fieldname;
c906108c
SS
10822
10823 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10824 pointer or virtual base class pointer) to private. */
e142c38c 10825 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10826 {
d48cc9dd 10827 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10828 new_field->accessibility = DW_ACCESS_private;
10829 fip->non_public_fields = 1;
10830 }
10831 }
a9a9bd0f 10832 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10833 {
a9a9bd0f
DC
10834 /* C++ static member. */
10835
10836 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10837 is a declaration, but all versions of G++ as of this writing
10838 (so through at least 3.2.1) incorrectly generate
10839 DW_TAG_variable tags. */
6e70227d 10840
ff355380 10841 const char *physname;
c906108c 10842
a9a9bd0f 10843 /* Get name of field. */
39cbfefa
DJ
10844 fieldname = dwarf2_name (die, cu);
10845 if (fieldname == NULL)
c906108c
SS
10846 return;
10847
254e6b9e 10848 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10849 if (attr
10850 /* Only create a symbol if this is an external value.
10851 new_symbol checks this and puts the value in the global symbol
10852 table, which we want. If it is not external, new_symbol
10853 will try to put the value in cu->list_in_scope which is wrong. */
10854 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10855 {
10856 /* A static const member, not much different than an enum as far as
10857 we're concerned, except that we can support more types. */
10858 new_symbol (die, NULL, cu);
10859 }
10860
2df3850c 10861 /* Get physical name. */
ff355380 10862 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10863
d8151005
DJ
10864 /* The name is already allocated along with this objfile, so we don't
10865 need to duplicate it for the type. */
10866 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10867 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10868 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10869 }
10870 else if (die->tag == DW_TAG_inheritance)
10871 {
74ac6d43 10872 LONGEST offset;
d4b96c9a 10873
74ac6d43
TT
10874 /* C++ base class field. */
10875 if (handle_data_member_location (die, cu, &offset))
10876 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10877 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10878 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10879 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10880 fip->nbaseclasses++;
10881 }
10882}
10883
98751a41
JK
10884/* Add a typedef defined in the scope of the FIP's class. */
10885
10886static void
10887dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10888 struct dwarf2_cu *cu)
6e70227d 10889{
98751a41 10890 struct objfile *objfile = cu->objfile;
98751a41
JK
10891 struct typedef_field_list *new_field;
10892 struct attribute *attr;
10893 struct typedef_field *fp;
10894 char *fieldname = "";
10895
10896 /* Allocate a new field list entry and link it in. */
10897 new_field = xzalloc (sizeof (*new_field));
10898 make_cleanup (xfree, new_field);
10899
10900 gdb_assert (die->tag == DW_TAG_typedef);
10901
10902 fp = &new_field->field;
10903
10904 /* Get name of field. */
10905 fp->name = dwarf2_name (die, cu);
10906 if (fp->name == NULL)
10907 return;
10908
10909 fp->type = read_type_die (die, cu);
10910
10911 new_field->next = fip->typedef_field_list;
10912 fip->typedef_field_list = new_field;
10913 fip->typedef_field_list_count++;
10914}
10915
c906108c
SS
10916/* Create the vector of fields, and attach it to the type. */
10917
10918static void
fba45db2 10919dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10920 struct dwarf2_cu *cu)
c906108c
SS
10921{
10922 int nfields = fip->nfields;
10923
10924 /* Record the field count, allocate space for the array of fields,
10925 and create blank accessibility bitfields if necessary. */
10926 TYPE_NFIELDS (type) = nfields;
10927 TYPE_FIELDS (type) = (struct field *)
10928 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10929 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10930
b4ba55a1 10931 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10932 {
10933 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10934
10935 TYPE_FIELD_PRIVATE_BITS (type) =
10936 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10937 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10938
10939 TYPE_FIELD_PROTECTED_BITS (type) =
10940 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10941 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10942
774b6a14
TT
10943 TYPE_FIELD_IGNORE_BITS (type) =
10944 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10945 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10946 }
10947
10948 /* If the type has baseclasses, allocate and clear a bit vector for
10949 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10950 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10951 {
10952 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10953 unsigned char *pointer;
c906108c
SS
10954
10955 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10956 pointer = TYPE_ALLOC (type, num_bytes);
10957 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10958 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10959 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10960 }
10961
3e43a32a
MS
10962 /* Copy the saved-up fields into the field vector. Start from the head of
10963 the list, adding to the tail of the field array, so that they end up in
10964 the same order in the array in which they were added to the list. */
c906108c
SS
10965 while (nfields-- > 0)
10966 {
7d0ccb61
DJ
10967 struct nextfield *fieldp;
10968
10969 if (fip->fields)
10970 {
10971 fieldp = fip->fields;
10972 fip->fields = fieldp->next;
10973 }
10974 else
10975 {
10976 fieldp = fip->baseclasses;
10977 fip->baseclasses = fieldp->next;
10978 }
10979
10980 TYPE_FIELD (type, nfields) = fieldp->field;
10981 switch (fieldp->accessibility)
c906108c 10982 {
c5aa993b 10983 case DW_ACCESS_private:
b4ba55a1
JB
10984 if (cu->language != language_ada)
10985 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10986 break;
c906108c 10987
c5aa993b 10988 case DW_ACCESS_protected:
b4ba55a1
JB
10989 if (cu->language != language_ada)
10990 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10991 break;
c906108c 10992
c5aa993b
JM
10993 case DW_ACCESS_public:
10994 break;
c906108c 10995
c5aa993b
JM
10996 default:
10997 /* Unknown accessibility. Complain and treat it as public. */
10998 {
e2e0b3e5 10999 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11000 fieldp->accessibility);
c5aa993b
JM
11001 }
11002 break;
c906108c
SS
11003 }
11004 if (nfields < fip->nbaseclasses)
11005 {
7d0ccb61 11006 switch (fieldp->virtuality)
c906108c 11007 {
c5aa993b
JM
11008 case DW_VIRTUALITY_virtual:
11009 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11010 if (cu->language == language_ada)
a73c6dcd 11011 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11012 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11013 break;
c906108c
SS
11014 }
11015 }
c906108c
SS
11016 }
11017}
11018
7d27a96d
TT
11019/* Return true if this member function is a constructor, false
11020 otherwise. */
11021
11022static int
11023dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11024{
11025 const char *fieldname;
11026 const char *typename;
11027 int len;
11028
11029 if (die->parent == NULL)
11030 return 0;
11031
11032 if (die->parent->tag != DW_TAG_structure_type
11033 && die->parent->tag != DW_TAG_union_type
11034 && die->parent->tag != DW_TAG_class_type)
11035 return 0;
11036
11037 fieldname = dwarf2_name (die, cu);
11038 typename = dwarf2_name (die->parent, cu);
11039 if (fieldname == NULL || typename == NULL)
11040 return 0;
11041
11042 len = strlen (fieldname);
11043 return (strncmp (fieldname, typename, len) == 0
11044 && (typename[len] == '\0' || typename[len] == '<'));
11045}
11046
c906108c
SS
11047/* Add a member function to the proper fieldlist. */
11048
11049static void
107d2387 11050dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11051 struct type *type, struct dwarf2_cu *cu)
c906108c 11052{
e7c27a73 11053 struct objfile *objfile = cu->objfile;
c906108c
SS
11054 struct attribute *attr;
11055 struct fnfieldlist *flp;
11056 int i;
11057 struct fn_field *fnp;
15d034d0 11058 const char *fieldname;
c906108c 11059 struct nextfnfield *new_fnfield;
f792889a 11060 struct type *this_type;
60d5a603 11061 enum dwarf_access_attribute accessibility;
c906108c 11062
b4ba55a1 11063 if (cu->language == language_ada)
a73c6dcd 11064 error (_("unexpected member function in Ada type"));
b4ba55a1 11065
2df3850c 11066 /* Get name of member function. */
39cbfefa
DJ
11067 fieldname = dwarf2_name (die, cu);
11068 if (fieldname == NULL)
2df3850c 11069 return;
c906108c 11070
c906108c
SS
11071 /* Look up member function name in fieldlist. */
11072 for (i = 0; i < fip->nfnfields; i++)
11073 {
27bfe10e 11074 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11075 break;
11076 }
11077
11078 /* Create new list element if necessary. */
11079 if (i < fip->nfnfields)
11080 flp = &fip->fnfieldlists[i];
11081 else
11082 {
11083 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11084 {
11085 fip->fnfieldlists = (struct fnfieldlist *)
11086 xrealloc (fip->fnfieldlists,
11087 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11088 * sizeof (struct fnfieldlist));
c906108c 11089 if (fip->nfnfields == 0)
c13c43fd 11090 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11091 }
11092 flp = &fip->fnfieldlists[fip->nfnfields];
11093 flp->name = fieldname;
11094 flp->length = 0;
11095 flp->head = NULL;
3da10d80 11096 i = fip->nfnfields++;
c906108c
SS
11097 }
11098
11099 /* Create a new member function field and chain it to the field list
0963b4bd 11100 entry. */
c906108c 11101 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11102 make_cleanup (xfree, new_fnfield);
c906108c
SS
11103 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11104 new_fnfield->next = flp->head;
11105 flp->head = new_fnfield;
11106 flp->length++;
11107
11108 /* Fill in the member function field info. */
11109 fnp = &new_fnfield->fnfield;
3da10d80
KS
11110
11111 /* Delay processing of the physname until later. */
11112 if (cu->language == language_cplus || cu->language == language_java)
11113 {
11114 add_to_method_list (type, i, flp->length - 1, fieldname,
11115 die, cu);
11116 }
11117 else
11118 {
1d06ead6 11119 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11120 fnp->physname = physname ? physname : "";
11121 }
11122
c906108c 11123 fnp->type = alloc_type (objfile);
f792889a
DJ
11124 this_type = read_type_die (die, cu);
11125 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11126 {
f792889a 11127 int nparams = TYPE_NFIELDS (this_type);
c906108c 11128
f792889a 11129 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11130 of the method itself (TYPE_CODE_METHOD). */
11131 smash_to_method_type (fnp->type, type,
f792889a
DJ
11132 TYPE_TARGET_TYPE (this_type),
11133 TYPE_FIELDS (this_type),
11134 TYPE_NFIELDS (this_type),
11135 TYPE_VARARGS (this_type));
c906108c
SS
11136
11137 /* Handle static member functions.
c5aa993b 11138 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11139 member functions. G++ helps GDB by marking the first
11140 parameter for non-static member functions (which is the this
11141 pointer) as artificial. We obtain this information from
11142 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11143 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11144 fnp->voffset = VOFFSET_STATIC;
11145 }
11146 else
e2e0b3e5 11147 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11148 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11149
11150 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11151 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11152 fnp->fcontext = die_containing_type (die, cu);
c906108c 11153
3e43a32a
MS
11154 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11155 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11156
11157 /* Get accessibility. */
e142c38c 11158 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11159 if (attr)
60d5a603
JK
11160 accessibility = DW_UNSND (attr);
11161 else
11162 accessibility = dwarf2_default_access_attribute (die, cu);
11163 switch (accessibility)
c906108c 11164 {
60d5a603
JK
11165 case DW_ACCESS_private:
11166 fnp->is_private = 1;
11167 break;
11168 case DW_ACCESS_protected:
11169 fnp->is_protected = 1;
11170 break;
c906108c
SS
11171 }
11172
b02dede2 11173 /* Check for artificial methods. */
e142c38c 11174 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11175 if (attr && DW_UNSND (attr) != 0)
11176 fnp->is_artificial = 1;
11177
7d27a96d
TT
11178 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11179
0d564a31 11180 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11181 function. For older versions of GCC, this is an offset in the
11182 appropriate virtual table, as specified by DW_AT_containing_type.
11183 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11184 to the object address. */
11185
e142c38c 11186 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11187 if (attr)
8e19ed76 11188 {
aec5aa8b 11189 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11190 {
aec5aa8b
TT
11191 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11192 {
11193 /* Old-style GCC. */
11194 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11195 }
11196 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11197 || (DW_BLOCK (attr)->size > 1
11198 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11199 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11200 {
11201 struct dwarf_block blk;
11202 int offset;
11203
11204 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11205 ? 1 : 2);
11206 blk.size = DW_BLOCK (attr)->size - offset;
11207 blk.data = DW_BLOCK (attr)->data + offset;
11208 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11209 if ((fnp->voffset % cu->header.addr_size) != 0)
11210 dwarf2_complex_location_expr_complaint ();
11211 else
11212 fnp->voffset /= cu->header.addr_size;
11213 fnp->voffset += 2;
11214 }
11215 else
11216 dwarf2_complex_location_expr_complaint ();
11217
11218 if (!fnp->fcontext)
11219 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11220 }
3690dd37 11221 else if (attr_form_is_section_offset (attr))
8e19ed76 11222 {
4d3c2250 11223 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11224 }
11225 else
11226 {
4d3c2250
KB
11227 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11228 fieldname);
8e19ed76 11229 }
0d564a31 11230 }
d48cc9dd
DJ
11231 else
11232 {
11233 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11234 if (attr && DW_UNSND (attr))
11235 {
11236 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11237 complaint (&symfile_complaints,
3e43a32a
MS
11238 _("Member function \"%s\" (offset %d) is virtual "
11239 "but the vtable offset is not specified"),
b64f50a1 11240 fieldname, die->offset.sect_off);
9655fd1a 11241 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11242 TYPE_CPLUS_DYNAMIC (type) = 1;
11243 }
11244 }
c906108c
SS
11245}
11246
11247/* Create the vector of member function fields, and attach it to the type. */
11248
11249static void
fba45db2 11250dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11251 struct dwarf2_cu *cu)
c906108c
SS
11252{
11253 struct fnfieldlist *flp;
c906108c
SS
11254 int i;
11255
b4ba55a1 11256 if (cu->language == language_ada)
a73c6dcd 11257 error (_("unexpected member functions in Ada type"));
b4ba55a1 11258
c906108c
SS
11259 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11260 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11261 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11262
11263 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11264 {
11265 struct nextfnfield *nfp = flp->head;
11266 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11267 int k;
11268
11269 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11270 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11271 fn_flp->fn_fields = (struct fn_field *)
11272 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11273 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11274 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11275 }
11276
11277 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11278}
11279
1168df01
JB
11280/* Returns non-zero if NAME is the name of a vtable member in CU's
11281 language, zero otherwise. */
11282static int
11283is_vtable_name (const char *name, struct dwarf2_cu *cu)
11284{
11285 static const char vptr[] = "_vptr";
987504bb 11286 static const char vtable[] = "vtable";
1168df01 11287
987504bb
JJ
11288 /* Look for the C++ and Java forms of the vtable. */
11289 if ((cu->language == language_java
11290 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11291 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11292 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11293 return 1;
11294
11295 return 0;
11296}
11297
c0dd20ea 11298/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11299 functions, with the ABI-specified layout. If TYPE describes
11300 such a structure, smash it into a member function type.
61049d3b
DJ
11301
11302 GCC shouldn't do this; it should just output pointer to member DIEs.
11303 This is GCC PR debug/28767. */
c0dd20ea 11304
0b92b5bb
TT
11305static void
11306quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11307{
0b92b5bb 11308 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11309
11310 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11311 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11312 return;
c0dd20ea
DJ
11313
11314 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11315 if (TYPE_FIELD_NAME (type, 0) == NULL
11316 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11317 || TYPE_FIELD_NAME (type, 1) == NULL
11318 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11319 return;
c0dd20ea
DJ
11320
11321 /* Find the type of the method. */
0b92b5bb 11322 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11323 if (pfn_type == NULL
11324 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11325 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11326 return;
c0dd20ea
DJ
11327
11328 /* Look for the "this" argument. */
11329 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11330 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11331 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11332 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11333 return;
c0dd20ea
DJ
11334
11335 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11336 new_type = alloc_type (objfile);
11337 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11338 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11339 TYPE_VARARGS (pfn_type));
0b92b5bb 11340 smash_to_methodptr_type (type, new_type);
c0dd20ea 11341}
1168df01 11342
685b1105
JK
11343/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11344 (icc). */
11345
11346static int
11347producer_is_icc (struct dwarf2_cu *cu)
11348{
11349 if (!cu->checked_producer)
11350 check_producer (cu);
11351
11352 return cu->producer_is_icc;
11353}
11354
c906108c 11355/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11356 (definition) to create a type for the structure or union. Fill in
11357 the type's name and general properties; the members will not be
3d1d5ea3 11358 processed until process_structure_scope.
c906108c 11359
c767944b
DJ
11360 NOTE: we need to call these functions regardless of whether or not the
11361 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11362 structure or union. This gets the type entered into our set of
11363 user defined types.
11364
11365 However, if the structure is incomplete (an opaque struct/union)
11366 then suppress creating a symbol table entry for it since gdb only
11367 wants to find the one with the complete definition. Note that if
11368 it is complete, we just call new_symbol, which does it's own
11369 checking about whether the struct/union is anonymous or not (and
11370 suppresses creating a symbol table entry itself). */
11371
f792889a 11372static struct type *
134d01f1 11373read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11374{
e7c27a73 11375 struct objfile *objfile = cu->objfile;
c906108c
SS
11376 struct type *type;
11377 struct attribute *attr;
15d034d0 11378 const char *name;
c906108c 11379
348e048f
DE
11380 /* If the definition of this type lives in .debug_types, read that type.
11381 Don't follow DW_AT_specification though, that will take us back up
11382 the chain and we want to go down. */
45e58e77 11383 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11384 if (attr)
11385 {
ac9ec31b 11386 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11387
ac9ec31b 11388 /* The type's CU may not be the same as CU.
02142a6c 11389 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11390 return set_die_type (die, type, cu);
11391 }
11392
c0dd20ea 11393 type = alloc_type (objfile);
c906108c 11394 INIT_CPLUS_SPECIFIC (type);
93311388 11395
39cbfefa
DJ
11396 name = dwarf2_name (die, cu);
11397 if (name != NULL)
c906108c 11398 {
987504bb
JJ
11399 if (cu->language == language_cplus
11400 || cu->language == language_java)
63d06c5c 11401 {
15d034d0 11402 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11403
11404 /* dwarf2_full_name might have already finished building the DIE's
11405 type. If so, there is no need to continue. */
11406 if (get_die_type (die, cu) != NULL)
11407 return get_die_type (die, cu);
11408
11409 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11410 if (die->tag == DW_TAG_structure_type
11411 || die->tag == DW_TAG_class_type)
11412 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11413 }
11414 else
11415 {
d8151005
DJ
11416 /* The name is already allocated along with this objfile, so
11417 we don't need to duplicate it for the type. */
7d455152 11418 TYPE_TAG_NAME (type) = name;
94af9270
KS
11419 if (die->tag == DW_TAG_class_type)
11420 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11421 }
c906108c
SS
11422 }
11423
11424 if (die->tag == DW_TAG_structure_type)
11425 {
11426 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11427 }
11428 else if (die->tag == DW_TAG_union_type)
11429 {
11430 TYPE_CODE (type) = TYPE_CODE_UNION;
11431 }
11432 else
11433 {
c906108c
SS
11434 TYPE_CODE (type) = TYPE_CODE_CLASS;
11435 }
11436
0cc2414c
TT
11437 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11438 TYPE_DECLARED_CLASS (type) = 1;
11439
e142c38c 11440 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11441 if (attr)
11442 {
11443 TYPE_LENGTH (type) = DW_UNSND (attr);
11444 }
11445 else
11446 {
11447 TYPE_LENGTH (type) = 0;
11448 }
11449
685b1105
JK
11450 if (producer_is_icc (cu))
11451 {
11452 /* ICC does not output the required DW_AT_declaration
11453 on incomplete types, but gives them a size of zero. */
11454 }
11455 else
11456 TYPE_STUB_SUPPORTED (type) = 1;
11457
dc718098 11458 if (die_is_declaration (die, cu))
876cecd0 11459 TYPE_STUB (type) = 1;
a6c727b2
DJ
11460 else if (attr == NULL && die->child == NULL
11461 && producer_is_realview (cu->producer))
11462 /* RealView does not output the required DW_AT_declaration
11463 on incomplete types. */
11464 TYPE_STUB (type) = 1;
dc718098 11465
c906108c
SS
11466 /* We need to add the type field to the die immediately so we don't
11467 infinitely recurse when dealing with pointers to the structure
0963b4bd 11468 type within the structure itself. */
1c379e20 11469 set_die_type (die, type, cu);
c906108c 11470
7e314c57
JK
11471 /* set_die_type should be already done. */
11472 set_descriptive_type (type, die, cu);
11473
c767944b
DJ
11474 return type;
11475}
11476
11477/* Finish creating a structure or union type, including filling in
11478 its members and creating a symbol for it. */
11479
11480static void
11481process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11482{
11483 struct objfile *objfile = cu->objfile;
11484 struct die_info *child_die = die->child;
11485 struct type *type;
11486
11487 type = get_die_type (die, cu);
11488 if (type == NULL)
11489 type = read_structure_type (die, cu);
11490
e142c38c 11491 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11492 {
11493 struct field_info fi;
11494 struct die_info *child_die;
34eaf542 11495 VEC (symbolp) *template_args = NULL;
c767944b 11496 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11497
11498 memset (&fi, 0, sizeof (struct field_info));
11499
639d11d3 11500 child_die = die->child;
c906108c
SS
11501
11502 while (child_die && child_die->tag)
11503 {
a9a9bd0f
DC
11504 if (child_die->tag == DW_TAG_member
11505 || child_die->tag == DW_TAG_variable)
c906108c 11506 {
a9a9bd0f
DC
11507 /* NOTE: carlton/2002-11-05: A C++ static data member
11508 should be a DW_TAG_member that is a declaration, but
11509 all versions of G++ as of this writing (so through at
11510 least 3.2.1) incorrectly generate DW_TAG_variable
11511 tags for them instead. */
e7c27a73 11512 dwarf2_add_field (&fi, child_die, cu);
c906108c 11513 }
8713b1b1 11514 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11515 {
0963b4bd 11516 /* C++ member function. */
e7c27a73 11517 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11518 }
11519 else if (child_die->tag == DW_TAG_inheritance)
11520 {
11521 /* C++ base class field. */
e7c27a73 11522 dwarf2_add_field (&fi, child_die, cu);
c906108c 11523 }
98751a41
JK
11524 else if (child_die->tag == DW_TAG_typedef)
11525 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11526 else if (child_die->tag == DW_TAG_template_type_param
11527 || child_die->tag == DW_TAG_template_value_param)
11528 {
11529 struct symbol *arg = new_symbol (child_die, NULL, cu);
11530
f1078f66
DJ
11531 if (arg != NULL)
11532 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11533 }
11534
c906108c
SS
11535 child_die = sibling_die (child_die);
11536 }
11537
34eaf542
TT
11538 /* Attach template arguments to type. */
11539 if (! VEC_empty (symbolp, template_args))
11540 {
11541 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11542 TYPE_N_TEMPLATE_ARGUMENTS (type)
11543 = VEC_length (symbolp, template_args);
11544 TYPE_TEMPLATE_ARGUMENTS (type)
11545 = obstack_alloc (&objfile->objfile_obstack,
11546 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11547 * sizeof (struct symbol *)));
11548 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11549 VEC_address (symbolp, template_args),
11550 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11551 * sizeof (struct symbol *)));
11552 VEC_free (symbolp, template_args);
11553 }
11554
c906108c
SS
11555 /* Attach fields and member functions to the type. */
11556 if (fi.nfields)
e7c27a73 11557 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11558 if (fi.nfnfields)
11559 {
e7c27a73 11560 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11561
c5aa993b 11562 /* Get the type which refers to the base class (possibly this
c906108c 11563 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11564 class from the DW_AT_containing_type attribute. This use of
11565 DW_AT_containing_type is a GNU extension. */
c906108c 11566
e142c38c 11567 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11568 {
e7c27a73 11569 struct type *t = die_containing_type (die, cu);
c906108c
SS
11570
11571 TYPE_VPTR_BASETYPE (type) = t;
11572 if (type == t)
11573 {
c906108c
SS
11574 int i;
11575
11576 /* Our own class provides vtbl ptr. */
11577 for (i = TYPE_NFIELDS (t) - 1;
11578 i >= TYPE_N_BASECLASSES (t);
11579 --i)
11580 {
0d5cff50 11581 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11582
1168df01 11583 if (is_vtable_name (fieldname, cu))
c906108c
SS
11584 {
11585 TYPE_VPTR_FIELDNO (type) = i;
11586 break;
11587 }
11588 }
11589
11590 /* Complain if virtual function table field not found. */
11591 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11592 complaint (&symfile_complaints,
3e43a32a
MS
11593 _("virtual function table pointer "
11594 "not found when defining class '%s'"),
4d3c2250
KB
11595 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11596 "");
c906108c
SS
11597 }
11598 else
11599 {
11600 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11601 }
11602 }
f6235d4c
EZ
11603 else if (cu->producer
11604 && strncmp (cu->producer,
11605 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11606 {
11607 /* The IBM XLC compiler does not provide direct indication
11608 of the containing type, but the vtable pointer is
11609 always named __vfp. */
11610
11611 int i;
11612
11613 for (i = TYPE_NFIELDS (type) - 1;
11614 i >= TYPE_N_BASECLASSES (type);
11615 --i)
11616 {
11617 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11618 {
11619 TYPE_VPTR_FIELDNO (type) = i;
11620 TYPE_VPTR_BASETYPE (type) = type;
11621 break;
11622 }
11623 }
11624 }
c906108c 11625 }
98751a41
JK
11626
11627 /* Copy fi.typedef_field_list linked list elements content into the
11628 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11629 if (fi.typedef_field_list)
11630 {
11631 int i = fi.typedef_field_list_count;
11632
a0d7a4ff 11633 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11634 TYPE_TYPEDEF_FIELD_ARRAY (type)
11635 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11636 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11637
11638 /* Reverse the list order to keep the debug info elements order. */
11639 while (--i >= 0)
11640 {
11641 struct typedef_field *dest, *src;
6e70227d 11642
98751a41
JK
11643 dest = &TYPE_TYPEDEF_FIELD (type, i);
11644 src = &fi.typedef_field_list->field;
11645 fi.typedef_field_list = fi.typedef_field_list->next;
11646 *dest = *src;
11647 }
11648 }
c767944b
DJ
11649
11650 do_cleanups (back_to);
eb2a6f42
TT
11651
11652 if (HAVE_CPLUS_STRUCT (type))
11653 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11654 }
63d06c5c 11655
bb5ed363 11656 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11657
90aeadfc
DC
11658 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11659 snapshots) has been known to create a die giving a declaration
11660 for a class that has, as a child, a die giving a definition for a
11661 nested class. So we have to process our children even if the
11662 current die is a declaration. Normally, of course, a declaration
11663 won't have any children at all. */
134d01f1 11664
90aeadfc
DC
11665 while (child_die != NULL && child_die->tag)
11666 {
11667 if (child_die->tag == DW_TAG_member
11668 || child_die->tag == DW_TAG_variable
34eaf542
TT
11669 || child_die->tag == DW_TAG_inheritance
11670 || child_die->tag == DW_TAG_template_value_param
11671 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11672 {
90aeadfc 11673 /* Do nothing. */
134d01f1 11674 }
90aeadfc
DC
11675 else
11676 process_die (child_die, cu);
134d01f1 11677
90aeadfc 11678 child_die = sibling_die (child_die);
134d01f1
DJ
11679 }
11680
fa4028e9
JB
11681 /* Do not consider external references. According to the DWARF standard,
11682 these DIEs are identified by the fact that they have no byte_size
11683 attribute, and a declaration attribute. */
11684 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11685 || !die_is_declaration (die, cu))
c767944b 11686 new_symbol (die, type, cu);
134d01f1
DJ
11687}
11688
11689/* Given a DW_AT_enumeration_type die, set its type. We do not
11690 complete the type's fields yet, or create any symbols. */
c906108c 11691
f792889a 11692static struct type *
134d01f1 11693read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11694{
e7c27a73 11695 struct objfile *objfile = cu->objfile;
c906108c 11696 struct type *type;
c906108c 11697 struct attribute *attr;
0114d602 11698 const char *name;
134d01f1 11699
348e048f
DE
11700 /* If the definition of this type lives in .debug_types, read that type.
11701 Don't follow DW_AT_specification though, that will take us back up
11702 the chain and we want to go down. */
45e58e77 11703 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11704 if (attr)
11705 {
ac9ec31b 11706 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11707
ac9ec31b 11708 /* The type's CU may not be the same as CU.
02142a6c 11709 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11710 return set_die_type (die, type, cu);
11711 }
11712
c906108c
SS
11713 type = alloc_type (objfile);
11714
11715 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11716 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11717 if (name != NULL)
7d455152 11718 TYPE_TAG_NAME (type) = name;
c906108c 11719
e142c38c 11720 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11721 if (attr)
11722 {
11723 TYPE_LENGTH (type) = DW_UNSND (attr);
11724 }
11725 else
11726 {
11727 TYPE_LENGTH (type) = 0;
11728 }
11729
137033e9
JB
11730 /* The enumeration DIE can be incomplete. In Ada, any type can be
11731 declared as private in the package spec, and then defined only
11732 inside the package body. Such types are known as Taft Amendment
11733 Types. When another package uses such a type, an incomplete DIE
11734 may be generated by the compiler. */
02eb380e 11735 if (die_is_declaration (die, cu))
876cecd0 11736 TYPE_STUB (type) = 1;
02eb380e 11737
f792889a 11738 return set_die_type (die, type, cu);
134d01f1
DJ
11739}
11740
11741/* Given a pointer to a die which begins an enumeration, process all
11742 the dies that define the members of the enumeration, and create the
11743 symbol for the enumeration type.
11744
11745 NOTE: We reverse the order of the element list. */
11746
11747static void
11748process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11749{
f792889a 11750 struct type *this_type;
134d01f1 11751
f792889a
DJ
11752 this_type = get_die_type (die, cu);
11753 if (this_type == NULL)
11754 this_type = read_enumeration_type (die, cu);
9dc481d3 11755
639d11d3 11756 if (die->child != NULL)
c906108c 11757 {
9dc481d3
DE
11758 struct die_info *child_die;
11759 struct symbol *sym;
11760 struct field *fields = NULL;
11761 int num_fields = 0;
11762 int unsigned_enum = 1;
15d034d0 11763 const char *name;
cafec441
TT
11764 int flag_enum = 1;
11765 ULONGEST mask = 0;
9dc481d3 11766
639d11d3 11767 child_die = die->child;
c906108c
SS
11768 while (child_die && child_die->tag)
11769 {
11770 if (child_die->tag != DW_TAG_enumerator)
11771 {
e7c27a73 11772 process_die (child_die, cu);
c906108c
SS
11773 }
11774 else
11775 {
39cbfefa
DJ
11776 name = dwarf2_name (child_die, cu);
11777 if (name)
c906108c 11778 {
f792889a 11779 sym = new_symbol (child_die, this_type, cu);
c906108c 11780 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11781 {
11782 unsigned_enum = 0;
11783 flag_enum = 0;
11784 }
11785 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11786 flag_enum = 0;
11787 else
11788 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11789
11790 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11791 {
11792 fields = (struct field *)
11793 xrealloc (fields,
11794 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11795 * sizeof (struct field));
c906108c
SS
11796 }
11797
3567439c 11798 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11799 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11800 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11801 FIELD_BITSIZE (fields[num_fields]) = 0;
11802
11803 num_fields++;
11804 }
11805 }
11806
11807 child_die = sibling_die (child_die);
11808 }
11809
11810 if (num_fields)
11811 {
f792889a
DJ
11812 TYPE_NFIELDS (this_type) = num_fields;
11813 TYPE_FIELDS (this_type) = (struct field *)
11814 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11815 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11816 sizeof (struct field) * num_fields);
b8c9b27d 11817 xfree (fields);
c906108c
SS
11818 }
11819 if (unsigned_enum)
876cecd0 11820 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11821 if (flag_enum)
11822 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11823 }
134d01f1 11824
6c83ed52
TT
11825 /* If we are reading an enum from a .debug_types unit, and the enum
11826 is a declaration, and the enum is not the signatured type in the
11827 unit, then we do not want to add a symbol for it. Adding a
11828 symbol would in some cases obscure the true definition of the
11829 enum, giving users an incomplete type when the definition is
11830 actually available. Note that we do not want to do this for all
11831 enums which are just declarations, because C++0x allows forward
11832 enum declarations. */
3019eac3 11833 if (cu->per_cu->is_debug_types
6c83ed52
TT
11834 && die_is_declaration (die, cu))
11835 {
52dc124a 11836 struct signatured_type *sig_type;
6c83ed52 11837
c0f78cd4 11838 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
11839 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11840 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11841 return;
11842 }
11843
f792889a 11844 new_symbol (die, this_type, cu);
c906108c
SS
11845}
11846
11847/* Extract all information from a DW_TAG_array_type DIE and put it in
11848 the DIE's type field. For now, this only handles one dimensional
11849 arrays. */
11850
f792889a 11851static struct type *
e7c27a73 11852read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11853{
e7c27a73 11854 struct objfile *objfile = cu->objfile;
c906108c 11855 struct die_info *child_die;
7e314c57 11856 struct type *type;
c906108c
SS
11857 struct type *element_type, *range_type, *index_type;
11858 struct type **range_types = NULL;
11859 struct attribute *attr;
11860 int ndim = 0;
11861 struct cleanup *back_to;
15d034d0 11862 const char *name;
c906108c 11863
e7c27a73 11864 element_type = die_type (die, cu);
c906108c 11865
7e314c57
JK
11866 /* The die_type call above may have already set the type for this DIE. */
11867 type = get_die_type (die, cu);
11868 if (type)
11869 return type;
11870
c906108c
SS
11871 /* Irix 6.2 native cc creates array types without children for
11872 arrays with unspecified length. */
639d11d3 11873 if (die->child == NULL)
c906108c 11874 {
46bf5051 11875 index_type = objfile_type (objfile)->builtin_int;
c906108c 11876 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11877 type = create_array_type (NULL, element_type, range_type);
11878 return set_die_type (die, type, cu);
c906108c
SS
11879 }
11880
11881 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11882 child_die = die->child;
c906108c
SS
11883 while (child_die && child_die->tag)
11884 {
11885 if (child_die->tag == DW_TAG_subrange_type)
11886 {
f792889a 11887 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11888
f792889a 11889 if (child_type != NULL)
a02abb62 11890 {
0963b4bd
MS
11891 /* The range type was succesfully read. Save it for the
11892 array type creation. */
a02abb62
JB
11893 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11894 {
11895 range_types = (struct type **)
11896 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11897 * sizeof (struct type *));
11898 if (ndim == 0)
11899 make_cleanup (free_current_contents, &range_types);
11900 }
f792889a 11901 range_types[ndim++] = child_type;
a02abb62 11902 }
c906108c
SS
11903 }
11904 child_die = sibling_die (child_die);
11905 }
11906
11907 /* Dwarf2 dimensions are output from left to right, create the
11908 necessary array types in backwards order. */
7ca2d3a3 11909
c906108c 11910 type = element_type;
7ca2d3a3
DL
11911
11912 if (read_array_order (die, cu) == DW_ORD_col_major)
11913 {
11914 int i = 0;
9a619af0 11915
7ca2d3a3
DL
11916 while (i < ndim)
11917 type = create_array_type (NULL, type, range_types[i++]);
11918 }
11919 else
11920 {
11921 while (ndim-- > 0)
11922 type = create_array_type (NULL, type, range_types[ndim]);
11923 }
c906108c 11924
f5f8a009
EZ
11925 /* Understand Dwarf2 support for vector types (like they occur on
11926 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11927 array type. This is not part of the Dwarf2/3 standard yet, but a
11928 custom vendor extension. The main difference between a regular
11929 array and the vector variant is that vectors are passed by value
11930 to functions. */
e142c38c 11931 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11932 if (attr)
ea37ba09 11933 make_vector_type (type);
f5f8a009 11934
dbc98a8b
KW
11935 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11936 implementation may choose to implement triple vectors using this
11937 attribute. */
11938 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11939 if (attr)
11940 {
11941 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11942 TYPE_LENGTH (type) = DW_UNSND (attr);
11943 else
3e43a32a
MS
11944 complaint (&symfile_complaints,
11945 _("DW_AT_byte_size for array type smaller "
11946 "than the total size of elements"));
dbc98a8b
KW
11947 }
11948
39cbfefa
DJ
11949 name = dwarf2_name (die, cu);
11950 if (name)
11951 TYPE_NAME (type) = name;
6e70227d 11952
0963b4bd 11953 /* Install the type in the die. */
7e314c57
JK
11954 set_die_type (die, type, cu);
11955
11956 /* set_die_type should be already done. */
b4ba55a1
JB
11957 set_descriptive_type (type, die, cu);
11958
c906108c
SS
11959 do_cleanups (back_to);
11960
7e314c57 11961 return type;
c906108c
SS
11962}
11963
7ca2d3a3 11964static enum dwarf_array_dim_ordering
6e70227d 11965read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11966{
11967 struct attribute *attr;
11968
11969 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11970
11971 if (attr) return DW_SND (attr);
11972
0963b4bd
MS
11973 /* GNU F77 is a special case, as at 08/2004 array type info is the
11974 opposite order to the dwarf2 specification, but data is still
11975 laid out as per normal fortran.
7ca2d3a3 11976
0963b4bd
MS
11977 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11978 version checking. */
7ca2d3a3 11979
905e0470
PM
11980 if (cu->language == language_fortran
11981 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11982 {
11983 return DW_ORD_row_major;
11984 }
11985
6e70227d 11986 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11987 {
11988 case array_column_major:
11989 return DW_ORD_col_major;
11990 case array_row_major:
11991 default:
11992 return DW_ORD_row_major;
11993 };
11994}
11995
72019c9c 11996/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11997 the DIE's type field. */
72019c9c 11998
f792889a 11999static struct type *
72019c9c
GM
12000read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12001{
7e314c57
JK
12002 struct type *domain_type, *set_type;
12003 struct attribute *attr;
f792889a 12004
7e314c57
JK
12005 domain_type = die_type (die, cu);
12006
12007 /* The die_type call above may have already set the type for this DIE. */
12008 set_type = get_die_type (die, cu);
12009 if (set_type)
12010 return set_type;
12011
12012 set_type = create_set_type (NULL, domain_type);
12013
12014 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12015 if (attr)
12016 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12017
f792889a 12018 return set_die_type (die, set_type, cu);
72019c9c 12019}
7ca2d3a3 12020
0971de02
TT
12021/* A helper for read_common_block that creates a locexpr baton.
12022 SYM is the symbol which we are marking as computed.
12023 COMMON_DIE is the DIE for the common block.
12024 COMMON_LOC is the location expression attribute for the common
12025 block itself.
12026 MEMBER_LOC is the location expression attribute for the particular
12027 member of the common block that we are processing.
12028 CU is the CU from which the above come. */
12029
12030static void
12031mark_common_block_symbol_computed (struct symbol *sym,
12032 struct die_info *common_die,
12033 struct attribute *common_loc,
12034 struct attribute *member_loc,
12035 struct dwarf2_cu *cu)
12036{
12037 struct objfile *objfile = dwarf2_per_objfile->objfile;
12038 struct dwarf2_locexpr_baton *baton;
12039 gdb_byte *ptr;
12040 unsigned int cu_off;
12041 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12042 LONGEST offset = 0;
12043
12044 gdb_assert (common_loc && member_loc);
12045 gdb_assert (attr_form_is_block (common_loc));
12046 gdb_assert (attr_form_is_block (member_loc)
12047 || attr_form_is_constant (member_loc));
12048
12049 baton = obstack_alloc (&objfile->objfile_obstack,
12050 sizeof (struct dwarf2_locexpr_baton));
12051 baton->per_cu = cu->per_cu;
12052 gdb_assert (baton->per_cu);
12053
12054 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12055
12056 if (attr_form_is_constant (member_loc))
12057 {
12058 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12059 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12060 }
12061 else
12062 baton->size += DW_BLOCK (member_loc)->size;
12063
12064 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12065 baton->data = ptr;
12066
12067 *ptr++ = DW_OP_call4;
12068 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12069 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12070 ptr += 4;
12071
12072 if (attr_form_is_constant (member_loc))
12073 {
12074 *ptr++ = DW_OP_addr;
12075 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12076 ptr += cu->header.addr_size;
12077 }
12078 else
12079 {
12080 /* We have to copy the data here, because DW_OP_call4 will only
12081 use a DW_AT_location attribute. */
12082 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12083 ptr += DW_BLOCK (member_loc)->size;
12084 }
12085
12086 *ptr++ = DW_OP_plus;
12087 gdb_assert (ptr - baton->data == baton->size);
12088
0971de02 12089 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12090 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12091}
12092
4357ac6c
TT
12093/* Create appropriate locally-scoped variables for all the
12094 DW_TAG_common_block entries. Also create a struct common_block
12095 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12096 is used to sepate the common blocks name namespace from regular
12097 variable names. */
c906108c
SS
12098
12099static void
e7c27a73 12100read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12101{
0971de02
TT
12102 struct attribute *attr;
12103
12104 attr = dwarf2_attr (die, DW_AT_location, cu);
12105 if (attr)
12106 {
12107 /* Support the .debug_loc offsets. */
12108 if (attr_form_is_block (attr))
12109 {
12110 /* Ok. */
12111 }
12112 else if (attr_form_is_section_offset (attr))
12113 {
12114 dwarf2_complex_location_expr_complaint ();
12115 attr = NULL;
12116 }
12117 else
12118 {
12119 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12120 "common block member");
12121 attr = NULL;
12122 }
12123 }
12124
639d11d3 12125 if (die->child != NULL)
c906108c 12126 {
4357ac6c
TT
12127 struct objfile *objfile = cu->objfile;
12128 struct die_info *child_die;
12129 size_t n_entries = 0, size;
12130 struct common_block *common_block;
12131 struct symbol *sym;
74ac6d43 12132
4357ac6c
TT
12133 for (child_die = die->child;
12134 child_die && child_die->tag;
12135 child_die = sibling_die (child_die))
12136 ++n_entries;
12137
12138 size = (sizeof (struct common_block)
12139 + (n_entries - 1) * sizeof (struct symbol *));
12140 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12141 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12142 common_block->n_entries = 0;
12143
12144 for (child_die = die->child;
12145 child_die && child_die->tag;
12146 child_die = sibling_die (child_die))
12147 {
12148 /* Create the symbol in the DW_TAG_common_block block in the current
12149 symbol scope. */
e7c27a73 12150 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12151 if (sym != NULL)
12152 {
12153 struct attribute *member_loc;
12154
12155 common_block->contents[common_block->n_entries++] = sym;
12156
12157 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12158 cu);
12159 if (member_loc)
12160 {
12161 /* GDB has handled this for a long time, but it is
12162 not specified by DWARF. It seems to have been
12163 emitted by gfortran at least as recently as:
12164 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12165 complaint (&symfile_complaints,
12166 _("Variable in common block has "
12167 "DW_AT_data_member_location "
12168 "- DIE at 0x%x [in module %s]"),
12169 child_die->offset.sect_off, cu->objfile->name);
12170
12171 if (attr_form_is_section_offset (member_loc))
12172 dwarf2_complex_location_expr_complaint ();
12173 else if (attr_form_is_constant (member_loc)
12174 || attr_form_is_block (member_loc))
12175 {
12176 if (attr)
12177 mark_common_block_symbol_computed (sym, die, attr,
12178 member_loc, cu);
12179 }
12180 else
12181 dwarf2_complex_location_expr_complaint ();
12182 }
12183 }
c906108c 12184 }
4357ac6c
TT
12185
12186 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12187 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12188 }
12189}
12190
0114d602 12191/* Create a type for a C++ namespace. */
d9fa45fe 12192
0114d602
DJ
12193static struct type *
12194read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12195{
e7c27a73 12196 struct objfile *objfile = cu->objfile;
0114d602 12197 const char *previous_prefix, *name;
9219021c 12198 int is_anonymous;
0114d602
DJ
12199 struct type *type;
12200
12201 /* For extensions, reuse the type of the original namespace. */
12202 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12203 {
12204 struct die_info *ext_die;
12205 struct dwarf2_cu *ext_cu = cu;
9a619af0 12206
0114d602
DJ
12207 ext_die = dwarf2_extension (die, &ext_cu);
12208 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12209
12210 /* EXT_CU may not be the same as CU.
02142a6c 12211 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12212 return set_die_type (die, type, cu);
12213 }
9219021c 12214
e142c38c 12215 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12216
12217 /* Now build the name of the current namespace. */
12218
0114d602
DJ
12219 previous_prefix = determine_prefix (die, cu);
12220 if (previous_prefix[0] != '\0')
12221 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12222 previous_prefix, name, 0, cu);
0114d602
DJ
12223
12224 /* Create the type. */
12225 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12226 objfile);
abee88f2 12227 TYPE_NAME (type) = name;
0114d602
DJ
12228 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12229
60531b24 12230 return set_die_type (die, type, cu);
0114d602
DJ
12231}
12232
12233/* Read a C++ namespace. */
12234
12235static void
12236read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12237{
12238 struct objfile *objfile = cu->objfile;
0114d602 12239 int is_anonymous;
9219021c 12240
5c4e30ca
DC
12241 /* Add a symbol associated to this if we haven't seen the namespace
12242 before. Also, add a using directive if it's an anonymous
12243 namespace. */
9219021c 12244
f2f0e013 12245 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12246 {
12247 struct type *type;
12248
0114d602 12249 type = read_type_die (die, cu);
e7c27a73 12250 new_symbol (die, type, cu);
5c4e30ca 12251
e8e80198 12252 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12253 if (is_anonymous)
0114d602
DJ
12254 {
12255 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12256
c0cc3a76 12257 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12258 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12259 }
5c4e30ca 12260 }
9219021c 12261
639d11d3 12262 if (die->child != NULL)
d9fa45fe 12263 {
639d11d3 12264 struct die_info *child_die = die->child;
6e70227d 12265
d9fa45fe
DC
12266 while (child_die && child_die->tag)
12267 {
e7c27a73 12268 process_die (child_die, cu);
d9fa45fe
DC
12269 child_die = sibling_die (child_die);
12270 }
12271 }
38d518c9
EZ
12272}
12273
f55ee35c
JK
12274/* Read a Fortran module as type. This DIE can be only a declaration used for
12275 imported module. Still we need that type as local Fortran "use ... only"
12276 declaration imports depend on the created type in determine_prefix. */
12277
12278static struct type *
12279read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12280{
12281 struct objfile *objfile = cu->objfile;
15d034d0 12282 const char *module_name;
f55ee35c
JK
12283 struct type *type;
12284
12285 module_name = dwarf2_name (die, cu);
12286 if (!module_name)
3e43a32a
MS
12287 complaint (&symfile_complaints,
12288 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12289 die->offset.sect_off);
f55ee35c
JK
12290 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12291
12292 /* determine_prefix uses TYPE_TAG_NAME. */
12293 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12294
12295 return set_die_type (die, type, cu);
12296}
12297
5d7cb8df
JK
12298/* Read a Fortran module. */
12299
12300static void
12301read_module (struct die_info *die, struct dwarf2_cu *cu)
12302{
12303 struct die_info *child_die = die->child;
12304
5d7cb8df
JK
12305 while (child_die && child_die->tag)
12306 {
12307 process_die (child_die, cu);
12308 child_die = sibling_die (child_die);
12309 }
12310}
12311
38d518c9
EZ
12312/* Return the name of the namespace represented by DIE. Set
12313 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12314 namespace. */
12315
12316static const char *
e142c38c 12317namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12318{
12319 struct die_info *current_die;
12320 const char *name = NULL;
12321
12322 /* Loop through the extensions until we find a name. */
12323
12324 for (current_die = die;
12325 current_die != NULL;
f2f0e013 12326 current_die = dwarf2_extension (die, &cu))
38d518c9 12327 {
e142c38c 12328 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12329 if (name != NULL)
12330 break;
12331 }
12332
12333 /* Is it an anonymous namespace? */
12334
12335 *is_anonymous = (name == NULL);
12336 if (*is_anonymous)
2b1dbab0 12337 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12338
12339 return name;
d9fa45fe
DC
12340}
12341
c906108c
SS
12342/* Extract all information from a DW_TAG_pointer_type DIE and add to
12343 the user defined type vector. */
12344
f792889a 12345static struct type *
e7c27a73 12346read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12347{
5e2b427d 12348 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12349 struct comp_unit_head *cu_header = &cu->header;
c906108c 12350 struct type *type;
8b2dbe47
KB
12351 struct attribute *attr_byte_size;
12352 struct attribute *attr_address_class;
12353 int byte_size, addr_class;
7e314c57
JK
12354 struct type *target_type;
12355
12356 target_type = die_type (die, cu);
c906108c 12357
7e314c57
JK
12358 /* The die_type call above may have already set the type for this DIE. */
12359 type = get_die_type (die, cu);
12360 if (type)
12361 return type;
12362
12363 type = lookup_pointer_type (target_type);
8b2dbe47 12364
e142c38c 12365 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12366 if (attr_byte_size)
12367 byte_size = DW_UNSND (attr_byte_size);
c906108c 12368 else
8b2dbe47
KB
12369 byte_size = cu_header->addr_size;
12370
e142c38c 12371 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12372 if (attr_address_class)
12373 addr_class = DW_UNSND (attr_address_class);
12374 else
12375 addr_class = DW_ADDR_none;
12376
12377 /* If the pointer size or address class is different than the
12378 default, create a type variant marked as such and set the
12379 length accordingly. */
12380 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12381 {
5e2b427d 12382 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12383 {
12384 int type_flags;
12385
849957d9 12386 type_flags = gdbarch_address_class_type_flags
5e2b427d 12387 (gdbarch, byte_size, addr_class);
876cecd0
TT
12388 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12389 == 0);
8b2dbe47
KB
12390 type = make_type_with_address_space (type, type_flags);
12391 }
12392 else if (TYPE_LENGTH (type) != byte_size)
12393 {
3e43a32a
MS
12394 complaint (&symfile_complaints,
12395 _("invalid pointer size %d"), byte_size);
8b2dbe47 12396 }
6e70227d 12397 else
9a619af0
MS
12398 {
12399 /* Should we also complain about unhandled address classes? */
12400 }
c906108c 12401 }
8b2dbe47
KB
12402
12403 TYPE_LENGTH (type) = byte_size;
f792889a 12404 return set_die_type (die, type, cu);
c906108c
SS
12405}
12406
12407/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12408 the user defined type vector. */
12409
f792889a 12410static struct type *
e7c27a73 12411read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12412{
12413 struct type *type;
12414 struct type *to_type;
12415 struct type *domain;
12416
e7c27a73
DJ
12417 to_type = die_type (die, cu);
12418 domain = die_containing_type (die, cu);
0d5de010 12419
7e314c57
JK
12420 /* The calls above may have already set the type for this DIE. */
12421 type = get_die_type (die, cu);
12422 if (type)
12423 return type;
12424
0d5de010
DJ
12425 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12426 type = lookup_methodptr_type (to_type);
7078baeb
TT
12427 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12428 {
12429 struct type *new_type = alloc_type (cu->objfile);
12430
12431 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12432 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12433 TYPE_VARARGS (to_type));
12434 type = lookup_methodptr_type (new_type);
12435 }
0d5de010
DJ
12436 else
12437 type = lookup_memberptr_type (to_type, domain);
c906108c 12438
f792889a 12439 return set_die_type (die, type, cu);
c906108c
SS
12440}
12441
12442/* Extract all information from a DW_TAG_reference_type DIE and add to
12443 the user defined type vector. */
12444
f792889a 12445static struct type *
e7c27a73 12446read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12447{
e7c27a73 12448 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12449 struct type *type, *target_type;
c906108c
SS
12450 struct attribute *attr;
12451
7e314c57
JK
12452 target_type = die_type (die, cu);
12453
12454 /* The die_type call above may have already set the type for this DIE. */
12455 type = get_die_type (die, cu);
12456 if (type)
12457 return type;
12458
12459 type = lookup_reference_type (target_type);
e142c38c 12460 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12461 if (attr)
12462 {
12463 TYPE_LENGTH (type) = DW_UNSND (attr);
12464 }
12465 else
12466 {
107d2387 12467 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12468 }
f792889a 12469 return set_die_type (die, type, cu);
c906108c
SS
12470}
12471
f792889a 12472static struct type *
e7c27a73 12473read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12474{
f792889a 12475 struct type *base_type, *cv_type;
c906108c 12476
e7c27a73 12477 base_type = die_type (die, cu);
7e314c57
JK
12478
12479 /* The die_type call above may have already set the type for this DIE. */
12480 cv_type = get_die_type (die, cu);
12481 if (cv_type)
12482 return cv_type;
12483
2f608a3a
KW
12484 /* In case the const qualifier is applied to an array type, the element type
12485 is so qualified, not the array type (section 6.7.3 of C99). */
12486 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12487 {
12488 struct type *el_type, *inner_array;
12489
12490 base_type = copy_type (base_type);
12491 inner_array = base_type;
12492
12493 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12494 {
12495 TYPE_TARGET_TYPE (inner_array) =
12496 copy_type (TYPE_TARGET_TYPE (inner_array));
12497 inner_array = TYPE_TARGET_TYPE (inner_array);
12498 }
12499
12500 el_type = TYPE_TARGET_TYPE (inner_array);
12501 TYPE_TARGET_TYPE (inner_array) =
12502 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12503
12504 return set_die_type (die, base_type, cu);
12505 }
12506
f792889a
DJ
12507 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12508 return set_die_type (die, cv_type, cu);
c906108c
SS
12509}
12510
f792889a 12511static struct type *
e7c27a73 12512read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12513{
f792889a 12514 struct type *base_type, *cv_type;
c906108c 12515
e7c27a73 12516 base_type = die_type (die, cu);
7e314c57
JK
12517
12518 /* The die_type call above may have already set the type for this DIE. */
12519 cv_type = get_die_type (die, cu);
12520 if (cv_type)
12521 return cv_type;
12522
f792889a
DJ
12523 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12524 return set_die_type (die, cv_type, cu);
c906108c
SS
12525}
12526
06d66ee9
TT
12527/* Handle DW_TAG_restrict_type. */
12528
12529static struct type *
12530read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12531{
12532 struct type *base_type, *cv_type;
12533
12534 base_type = die_type (die, cu);
12535
12536 /* The die_type call above may have already set the type for this DIE. */
12537 cv_type = get_die_type (die, cu);
12538 if (cv_type)
12539 return cv_type;
12540
12541 cv_type = make_restrict_type (base_type);
12542 return set_die_type (die, cv_type, cu);
12543}
12544
c906108c
SS
12545/* Extract all information from a DW_TAG_string_type DIE and add to
12546 the user defined type vector. It isn't really a user defined type,
12547 but it behaves like one, with other DIE's using an AT_user_def_type
12548 attribute to reference it. */
12549
f792889a 12550static struct type *
e7c27a73 12551read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12552{
e7c27a73 12553 struct objfile *objfile = cu->objfile;
3b7538c0 12554 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12555 struct type *type, *range_type, *index_type, *char_type;
12556 struct attribute *attr;
12557 unsigned int length;
12558
e142c38c 12559 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12560 if (attr)
12561 {
12562 length = DW_UNSND (attr);
12563 }
12564 else
12565 {
0963b4bd 12566 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12567 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12568 if (attr)
12569 {
12570 length = DW_UNSND (attr);
12571 }
12572 else
12573 {
12574 length = 1;
12575 }
c906108c 12576 }
6ccb9162 12577
46bf5051 12578 index_type = objfile_type (objfile)->builtin_int;
c906108c 12579 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12580 char_type = language_string_char_type (cu->language_defn, gdbarch);
12581 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12582
f792889a 12583 return set_die_type (die, type, cu);
c906108c
SS
12584}
12585
12586/* Handle DIES due to C code like:
12587
12588 struct foo
c5aa993b
JM
12589 {
12590 int (*funcp)(int a, long l);
12591 int b;
12592 };
c906108c 12593
0963b4bd 12594 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12595
f792889a 12596static struct type *
e7c27a73 12597read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12598{
bb5ed363 12599 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12600 struct type *type; /* Type that this function returns. */
12601 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12602 struct attribute *attr;
12603
e7c27a73 12604 type = die_type (die, cu);
7e314c57
JK
12605
12606 /* The die_type call above may have already set the type for this DIE. */
12607 ftype = get_die_type (die, cu);
12608 if (ftype)
12609 return ftype;
12610
0c8b41f1 12611 ftype = lookup_function_type (type);
c906108c 12612
5b8101ae 12613 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12614 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12615 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12616 || cu->language == language_cplus
5b8101ae
PM
12617 || cu->language == language_java
12618 || cu->language == language_pascal)
876cecd0 12619 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12620 else if (producer_is_realview (cu->producer))
12621 /* RealView does not emit DW_AT_prototyped. We can not
12622 distinguish prototyped and unprototyped functions; default to
12623 prototyped, since that is more common in modern code (and
12624 RealView warns about unprototyped functions). */
12625 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12626
c055b101
CV
12627 /* Store the calling convention in the type if it's available in
12628 the subroutine die. Otherwise set the calling convention to
12629 the default value DW_CC_normal. */
12630 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12631 if (attr)
12632 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12633 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12634 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12635 else
12636 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12637
12638 /* We need to add the subroutine type to the die immediately so
12639 we don't infinitely recurse when dealing with parameters
0963b4bd 12640 declared as the same subroutine type. */
76c10ea2 12641 set_die_type (die, ftype, cu);
6e70227d 12642
639d11d3 12643 if (die->child != NULL)
c906108c 12644 {
bb5ed363 12645 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12646 struct die_info *child_die;
8072405b 12647 int nparams, iparams;
c906108c
SS
12648
12649 /* Count the number of parameters.
12650 FIXME: GDB currently ignores vararg functions, but knows about
12651 vararg member functions. */
8072405b 12652 nparams = 0;
639d11d3 12653 child_die = die->child;
c906108c
SS
12654 while (child_die && child_die->tag)
12655 {
12656 if (child_die->tag == DW_TAG_formal_parameter)
12657 nparams++;
12658 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12659 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12660 child_die = sibling_die (child_die);
12661 }
12662
12663 /* Allocate storage for parameters and fill them in. */
12664 TYPE_NFIELDS (ftype) = nparams;
12665 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12666 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12667
8072405b
JK
12668 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12669 even if we error out during the parameters reading below. */
12670 for (iparams = 0; iparams < nparams; iparams++)
12671 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12672
12673 iparams = 0;
639d11d3 12674 child_die = die->child;
c906108c
SS
12675 while (child_die && child_die->tag)
12676 {
12677 if (child_die->tag == DW_TAG_formal_parameter)
12678 {
3ce3b1ba
PA
12679 struct type *arg_type;
12680
12681 /* DWARF version 2 has no clean way to discern C++
12682 static and non-static member functions. G++ helps
12683 GDB by marking the first parameter for non-static
12684 member functions (which is the this pointer) as
12685 artificial. We pass this information to
12686 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12687
12688 DWARF version 3 added DW_AT_object_pointer, which GCC
12689 4.5 does not yet generate. */
e142c38c 12690 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12691 if (attr)
12692 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12693 else
418835cc
KS
12694 {
12695 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12696
12697 /* GCC/43521: In java, the formal parameter
12698 "this" is sometimes not marked with DW_AT_artificial. */
12699 if (cu->language == language_java)
12700 {
12701 const char *name = dwarf2_name (child_die, cu);
9a619af0 12702
418835cc
KS
12703 if (name && !strcmp (name, "this"))
12704 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12705 }
12706 }
3ce3b1ba
PA
12707 arg_type = die_type (child_die, cu);
12708
12709 /* RealView does not mark THIS as const, which the testsuite
12710 expects. GCC marks THIS as const in method definitions,
12711 but not in the class specifications (GCC PR 43053). */
12712 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12713 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12714 {
12715 int is_this = 0;
12716 struct dwarf2_cu *arg_cu = cu;
12717 const char *name = dwarf2_name (child_die, cu);
12718
12719 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12720 if (attr)
12721 {
12722 /* If the compiler emits this, use it. */
12723 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12724 is_this = 1;
12725 }
12726 else if (name && strcmp (name, "this") == 0)
12727 /* Function definitions will have the argument names. */
12728 is_this = 1;
12729 else if (name == NULL && iparams == 0)
12730 /* Declarations may not have the names, so like
12731 elsewhere in GDB, assume an artificial first
12732 argument is "this". */
12733 is_this = 1;
12734
12735 if (is_this)
12736 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12737 arg_type, 0);
12738 }
12739
12740 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12741 iparams++;
12742 }
12743 child_die = sibling_die (child_die);
12744 }
12745 }
12746
76c10ea2 12747 return ftype;
c906108c
SS
12748}
12749
f792889a 12750static struct type *
e7c27a73 12751read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12752{
e7c27a73 12753 struct objfile *objfile = cu->objfile;
0114d602 12754 const char *name = NULL;
3c8e0968 12755 struct type *this_type, *target_type;
c906108c 12756
94af9270 12757 name = dwarf2_full_name (NULL, die, cu);
f792889a 12758 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12759 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12760 TYPE_NAME (this_type) = name;
f792889a 12761 set_die_type (die, this_type, cu);
3c8e0968
DE
12762 target_type = die_type (die, cu);
12763 if (target_type != this_type)
12764 TYPE_TARGET_TYPE (this_type) = target_type;
12765 else
12766 {
12767 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12768 spec and cause infinite loops in GDB. */
12769 complaint (&symfile_complaints,
12770 _("Self-referential DW_TAG_typedef "
12771 "- DIE at 0x%x [in module %s]"),
b64f50a1 12772 die->offset.sect_off, objfile->name);
3c8e0968
DE
12773 TYPE_TARGET_TYPE (this_type) = NULL;
12774 }
f792889a 12775 return this_type;
c906108c
SS
12776}
12777
12778/* Find a representation of a given base type and install
12779 it in the TYPE field of the die. */
12780
f792889a 12781static struct type *
e7c27a73 12782read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12783{
e7c27a73 12784 struct objfile *objfile = cu->objfile;
c906108c
SS
12785 struct type *type;
12786 struct attribute *attr;
12787 int encoding = 0, size = 0;
15d034d0 12788 const char *name;
6ccb9162
UW
12789 enum type_code code = TYPE_CODE_INT;
12790 int type_flags = 0;
12791 struct type *target_type = NULL;
c906108c 12792
e142c38c 12793 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12794 if (attr)
12795 {
12796 encoding = DW_UNSND (attr);
12797 }
e142c38c 12798 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12799 if (attr)
12800 {
12801 size = DW_UNSND (attr);
12802 }
39cbfefa 12803 name = dwarf2_name (die, cu);
6ccb9162 12804 if (!name)
c906108c 12805 {
6ccb9162
UW
12806 complaint (&symfile_complaints,
12807 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12808 }
6ccb9162
UW
12809
12810 switch (encoding)
c906108c 12811 {
6ccb9162
UW
12812 case DW_ATE_address:
12813 /* Turn DW_ATE_address into a void * pointer. */
12814 code = TYPE_CODE_PTR;
12815 type_flags |= TYPE_FLAG_UNSIGNED;
12816 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12817 break;
12818 case DW_ATE_boolean:
12819 code = TYPE_CODE_BOOL;
12820 type_flags |= TYPE_FLAG_UNSIGNED;
12821 break;
12822 case DW_ATE_complex_float:
12823 code = TYPE_CODE_COMPLEX;
12824 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12825 break;
12826 case DW_ATE_decimal_float:
12827 code = TYPE_CODE_DECFLOAT;
12828 break;
12829 case DW_ATE_float:
12830 code = TYPE_CODE_FLT;
12831 break;
12832 case DW_ATE_signed:
12833 break;
12834 case DW_ATE_unsigned:
12835 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12836 if (cu->language == language_fortran
12837 && name
12838 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12839 code = TYPE_CODE_CHAR;
6ccb9162
UW
12840 break;
12841 case DW_ATE_signed_char:
6e70227d 12842 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12843 || cu->language == language_pascal
12844 || cu->language == language_fortran)
6ccb9162
UW
12845 code = TYPE_CODE_CHAR;
12846 break;
12847 case DW_ATE_unsigned_char:
868a0084 12848 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12849 || cu->language == language_pascal
12850 || cu->language == language_fortran)
6ccb9162
UW
12851 code = TYPE_CODE_CHAR;
12852 type_flags |= TYPE_FLAG_UNSIGNED;
12853 break;
75079b2b
TT
12854 case DW_ATE_UTF:
12855 /* We just treat this as an integer and then recognize the
12856 type by name elsewhere. */
12857 break;
12858
6ccb9162
UW
12859 default:
12860 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12861 dwarf_type_encoding_name (encoding));
12862 break;
c906108c 12863 }
6ccb9162 12864
0114d602
DJ
12865 type = init_type (code, size, type_flags, NULL, objfile);
12866 TYPE_NAME (type) = name;
6ccb9162
UW
12867 TYPE_TARGET_TYPE (type) = target_type;
12868
0114d602 12869 if (name && strcmp (name, "char") == 0)
876cecd0 12870 TYPE_NOSIGN (type) = 1;
0114d602 12871
f792889a 12872 return set_die_type (die, type, cu);
c906108c
SS
12873}
12874
a02abb62
JB
12875/* Read the given DW_AT_subrange DIE. */
12876
f792889a 12877static struct type *
a02abb62
JB
12878read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12879{
4c9ad8c2 12880 struct type *base_type, *orig_base_type;
a02abb62
JB
12881 struct type *range_type;
12882 struct attribute *attr;
4fae6e18
JK
12883 LONGEST low, high;
12884 int low_default_is_valid;
15d034d0 12885 const char *name;
43bbcdc2 12886 LONGEST negative_mask;
e77813c8 12887
4c9ad8c2
TT
12888 orig_base_type = die_type (die, cu);
12889 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12890 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12891 creating the range type, but we use the result of check_typedef
12892 when examining properties of the type. */
12893 base_type = check_typedef (orig_base_type);
a02abb62 12894
7e314c57
JK
12895 /* The die_type call above may have already set the type for this DIE. */
12896 range_type = get_die_type (die, cu);
12897 if (range_type)
12898 return range_type;
12899
4fae6e18
JK
12900 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12901 omitting DW_AT_lower_bound. */
12902 switch (cu->language)
6e70227d 12903 {
4fae6e18
JK
12904 case language_c:
12905 case language_cplus:
12906 low = 0;
12907 low_default_is_valid = 1;
12908 break;
12909 case language_fortran:
12910 low = 1;
12911 low_default_is_valid = 1;
12912 break;
12913 case language_d:
12914 case language_java:
12915 case language_objc:
12916 low = 0;
12917 low_default_is_valid = (cu->header.version >= 4);
12918 break;
12919 case language_ada:
12920 case language_m2:
12921 case language_pascal:
a02abb62 12922 low = 1;
4fae6e18
JK
12923 low_default_is_valid = (cu->header.version >= 4);
12924 break;
12925 default:
12926 low = 0;
12927 low_default_is_valid = 0;
12928 break;
a02abb62
JB
12929 }
12930
dd5e6932
DJ
12931 /* FIXME: For variable sized arrays either of these could be
12932 a variable rather than a constant value. We'll allow it,
12933 but we don't know how to handle it. */
e142c38c 12934 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12935 if (attr)
4fae6e18
JK
12936 low = dwarf2_get_attr_constant_value (attr, low);
12937 else if (!low_default_is_valid)
12938 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12939 "- DIE at 0x%x [in module %s]"),
12940 die->offset.sect_off, cu->objfile->name);
a02abb62 12941
e142c38c 12942 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12943 if (attr)
6e70227d 12944 {
d48323d8 12945 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12946 {
12947 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12948 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12949 FIXME: GDB does not yet know how to handle dynamic
12950 arrays properly, treat them as arrays with unspecified
12951 length for now.
12952
12953 FIXME: jimb/2003-09-22: GDB does not really know
12954 how to handle arrays of unspecified length
12955 either; we just represent them as zero-length
12956 arrays. Choose an appropriate upper bound given
12957 the lower bound we've computed above. */
12958 high = low - 1;
12959 }
12960 else
12961 high = dwarf2_get_attr_constant_value (attr, 1);
12962 }
e77813c8
PM
12963 else
12964 {
12965 attr = dwarf2_attr (die, DW_AT_count, cu);
12966 if (attr)
12967 {
12968 int count = dwarf2_get_attr_constant_value (attr, 1);
12969 high = low + count - 1;
12970 }
c2ff108b
JK
12971 else
12972 {
12973 /* Unspecified array length. */
12974 high = low - 1;
12975 }
e77813c8
PM
12976 }
12977
12978 /* Dwarf-2 specifications explicitly allows to create subrange types
12979 without specifying a base type.
12980 In that case, the base type must be set to the type of
12981 the lower bound, upper bound or count, in that order, if any of these
12982 three attributes references an object that has a type.
12983 If no base type is found, the Dwarf-2 specifications say that
12984 a signed integer type of size equal to the size of an address should
12985 be used.
12986 For the following C code: `extern char gdb_int [];'
12987 GCC produces an empty range DIE.
12988 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12989 high bound or count are not yet handled by this code. */
e77813c8
PM
12990 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12991 {
12992 struct objfile *objfile = cu->objfile;
12993 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12994 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12995 struct type *int_type = objfile_type (objfile)->builtin_int;
12996
12997 /* Test "int", "long int", and "long long int" objfile types,
12998 and select the first one having a size above or equal to the
12999 architecture address size. */
13000 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13001 base_type = int_type;
13002 else
13003 {
13004 int_type = objfile_type (objfile)->builtin_long;
13005 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13006 base_type = int_type;
13007 else
13008 {
13009 int_type = objfile_type (objfile)->builtin_long_long;
13010 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13011 base_type = int_type;
13012 }
13013 }
13014 }
a02abb62 13015
6e70227d 13016 negative_mask =
43bbcdc2
PH
13017 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13018 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13019 low |= negative_mask;
13020 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13021 high |= negative_mask;
13022
4c9ad8c2 13023 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13024
bbb0eef6
JK
13025 /* Mark arrays with dynamic length at least as an array of unspecified
13026 length. GDB could check the boundary but before it gets implemented at
13027 least allow accessing the array elements. */
d48323d8 13028 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13029 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13030
c2ff108b
JK
13031 /* Ada expects an empty array on no boundary attributes. */
13032 if (attr == NULL && cu->language != language_ada)
13033 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13034
39cbfefa
DJ
13035 name = dwarf2_name (die, cu);
13036 if (name)
13037 TYPE_NAME (range_type) = name;
6e70227d 13038
e142c38c 13039 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13040 if (attr)
13041 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13042
7e314c57
JK
13043 set_die_type (die, range_type, cu);
13044
13045 /* set_die_type should be already done. */
b4ba55a1
JB
13046 set_descriptive_type (range_type, die, cu);
13047
7e314c57 13048 return range_type;
a02abb62 13049}
6e70227d 13050
f792889a 13051static struct type *
81a17f79
JB
13052read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13053{
13054 struct type *type;
81a17f79 13055
81a17f79
JB
13056 /* For now, we only support the C meaning of an unspecified type: void. */
13057
0114d602
DJ
13058 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13059 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13060
f792889a 13061 return set_die_type (die, type, cu);
81a17f79 13062}
a02abb62 13063
639d11d3
DC
13064/* Read a single die and all its descendents. Set the die's sibling
13065 field to NULL; set other fields in the die correctly, and set all
13066 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13067 location of the info_ptr after reading all of those dies. PARENT
13068 is the parent of the die in question. */
13069
13070static struct die_info *
dee91e82 13071read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13072 const gdb_byte *info_ptr,
13073 const gdb_byte **new_info_ptr,
dee91e82 13074 struct die_info *parent)
639d11d3
DC
13075{
13076 struct die_info *die;
d521ce57 13077 const gdb_byte *cur_ptr;
639d11d3
DC
13078 int has_children;
13079
bf6af496 13080 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13081 if (die == NULL)
13082 {
13083 *new_info_ptr = cur_ptr;
13084 return NULL;
13085 }
93311388 13086 store_in_ref_table (die, reader->cu);
639d11d3
DC
13087
13088 if (has_children)
bf6af496 13089 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13090 else
13091 {
13092 die->child = NULL;
13093 *new_info_ptr = cur_ptr;
13094 }
13095
13096 die->sibling = NULL;
13097 die->parent = parent;
13098 return die;
13099}
13100
13101/* Read a die, all of its descendents, and all of its siblings; set
13102 all of the fields of all of the dies correctly. Arguments are as
13103 in read_die_and_children. */
13104
13105static struct die_info *
bf6af496 13106read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13107 const gdb_byte *info_ptr,
13108 const gdb_byte **new_info_ptr,
bf6af496 13109 struct die_info *parent)
639d11d3
DC
13110{
13111 struct die_info *first_die, *last_sibling;
d521ce57 13112 const gdb_byte *cur_ptr;
639d11d3 13113
c906108c 13114 cur_ptr = info_ptr;
639d11d3
DC
13115 first_die = last_sibling = NULL;
13116
13117 while (1)
c906108c 13118 {
639d11d3 13119 struct die_info *die
dee91e82 13120 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13121
1d325ec1 13122 if (die == NULL)
c906108c 13123 {
639d11d3
DC
13124 *new_info_ptr = cur_ptr;
13125 return first_die;
c906108c 13126 }
1d325ec1
DJ
13127
13128 if (!first_die)
13129 first_die = die;
c906108c 13130 else
1d325ec1
DJ
13131 last_sibling->sibling = die;
13132
13133 last_sibling = die;
c906108c 13134 }
c906108c
SS
13135}
13136
bf6af496
DE
13137/* Read a die, all of its descendents, and all of its siblings; set
13138 all of the fields of all of the dies correctly. Arguments are as
13139 in read_die_and_children.
13140 This the main entry point for reading a DIE and all its children. */
13141
13142static struct die_info *
13143read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13144 const gdb_byte *info_ptr,
13145 const gdb_byte **new_info_ptr,
bf6af496
DE
13146 struct die_info *parent)
13147{
13148 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13149 new_info_ptr, parent);
13150
13151 if (dwarf2_die_debug)
13152 {
13153 fprintf_unfiltered (gdb_stdlog,
13154 "Read die from %s@0x%x of %s:\n",
13155 bfd_section_name (reader->abfd,
13156 reader->die_section->asection),
13157 (unsigned) (info_ptr - reader->die_section->buffer),
13158 bfd_get_filename (reader->abfd));
13159 dump_die (die, dwarf2_die_debug);
13160 }
13161
13162 return die;
13163}
13164
3019eac3
DE
13165/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13166 attributes.
13167 The caller is responsible for filling in the extra attributes
13168 and updating (*DIEP)->num_attrs.
13169 Set DIEP to point to a newly allocated die with its information,
13170 except for its child, sibling, and parent fields.
13171 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13172
d521ce57 13173static const gdb_byte *
3019eac3 13174read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13175 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13176 int *has_children, int num_extra_attrs)
93311388 13177{
b64f50a1
JK
13178 unsigned int abbrev_number, bytes_read, i;
13179 sect_offset offset;
93311388
DE
13180 struct abbrev_info *abbrev;
13181 struct die_info *die;
13182 struct dwarf2_cu *cu = reader->cu;
13183 bfd *abfd = reader->abfd;
13184
b64f50a1 13185 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13186 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13187 info_ptr += bytes_read;
13188 if (!abbrev_number)
13189 {
13190 *diep = NULL;
13191 *has_children = 0;
13192 return info_ptr;
13193 }
13194
433df2d4 13195 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13196 if (!abbrev)
348e048f
DE
13197 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13198 abbrev_number,
13199 bfd_get_filename (abfd));
13200
3019eac3 13201 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13202 die->offset = offset;
13203 die->tag = abbrev->tag;
13204 die->abbrev = abbrev_number;
13205
3019eac3
DE
13206 /* Make the result usable.
13207 The caller needs to update num_attrs after adding the extra
13208 attributes. */
93311388
DE
13209 die->num_attrs = abbrev->num_attrs;
13210
13211 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13212 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13213 info_ptr);
93311388
DE
13214
13215 *diep = die;
13216 *has_children = abbrev->has_children;
13217 return info_ptr;
13218}
13219
3019eac3
DE
13220/* Read a die and all its attributes.
13221 Set DIEP to point to a newly allocated die with its information,
13222 except for its child, sibling, and parent fields.
13223 Set HAS_CHILDREN to tell whether the die has children or not. */
13224
d521ce57 13225static const gdb_byte *
3019eac3 13226read_full_die (const struct die_reader_specs *reader,
d521ce57 13227 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13228 int *has_children)
13229{
d521ce57 13230 const gdb_byte *result;
bf6af496
DE
13231
13232 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13233
13234 if (dwarf2_die_debug)
13235 {
13236 fprintf_unfiltered (gdb_stdlog,
13237 "Read die from %s@0x%x of %s:\n",
13238 bfd_section_name (reader->abfd,
13239 reader->die_section->asection),
13240 (unsigned) (info_ptr - reader->die_section->buffer),
13241 bfd_get_filename (reader->abfd));
13242 dump_die (*diep, dwarf2_die_debug);
13243 }
13244
13245 return result;
3019eac3 13246}
433df2d4
DE
13247\f
13248/* Abbreviation tables.
3019eac3 13249
433df2d4 13250 In DWARF version 2, the description of the debugging information is
c906108c
SS
13251 stored in a separate .debug_abbrev section. Before we read any
13252 dies from a section we read in all abbreviations and install them
433df2d4
DE
13253 in a hash table. */
13254
13255/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13256
13257static struct abbrev_info *
13258abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13259{
13260 struct abbrev_info *abbrev;
13261
13262 abbrev = (struct abbrev_info *)
13263 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13264 memset (abbrev, 0, sizeof (struct abbrev_info));
13265 return abbrev;
13266}
13267
13268/* Add an abbreviation to the table. */
c906108c
SS
13269
13270static void
433df2d4
DE
13271abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13272 unsigned int abbrev_number,
13273 struct abbrev_info *abbrev)
13274{
13275 unsigned int hash_number;
13276
13277 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13278 abbrev->next = abbrev_table->abbrevs[hash_number];
13279 abbrev_table->abbrevs[hash_number] = abbrev;
13280}
dee91e82 13281
433df2d4
DE
13282/* Look up an abbrev in the table.
13283 Returns NULL if the abbrev is not found. */
13284
13285static struct abbrev_info *
13286abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13287 unsigned int abbrev_number)
c906108c 13288{
433df2d4
DE
13289 unsigned int hash_number;
13290 struct abbrev_info *abbrev;
13291
13292 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13293 abbrev = abbrev_table->abbrevs[hash_number];
13294
13295 while (abbrev)
13296 {
13297 if (abbrev->number == abbrev_number)
13298 return abbrev;
13299 abbrev = abbrev->next;
13300 }
13301 return NULL;
13302}
13303
13304/* Read in an abbrev table. */
13305
13306static struct abbrev_table *
13307abbrev_table_read_table (struct dwarf2_section_info *section,
13308 sect_offset offset)
13309{
13310 struct objfile *objfile = dwarf2_per_objfile->objfile;
13311 bfd *abfd = section->asection->owner;
13312 struct abbrev_table *abbrev_table;
d521ce57 13313 const gdb_byte *abbrev_ptr;
c906108c
SS
13314 struct abbrev_info *cur_abbrev;
13315 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13316 unsigned int abbrev_form;
f3dd6933
DJ
13317 struct attr_abbrev *cur_attrs;
13318 unsigned int allocated_attrs;
c906108c 13319
433df2d4 13320 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13321 abbrev_table->offset = offset;
433df2d4
DE
13322 obstack_init (&abbrev_table->abbrev_obstack);
13323 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13324 (ABBREV_HASH_SIZE
13325 * sizeof (struct abbrev_info *)));
13326 memset (abbrev_table->abbrevs, 0,
13327 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13328
433df2d4
DE
13329 dwarf2_read_section (objfile, section);
13330 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13331 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13332 abbrev_ptr += bytes_read;
13333
f3dd6933
DJ
13334 allocated_attrs = ATTR_ALLOC_CHUNK;
13335 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13336
0963b4bd 13337 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13338 while (abbrev_number)
13339 {
433df2d4 13340 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13341
13342 /* read in abbrev header */
13343 cur_abbrev->number = abbrev_number;
13344 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13345 abbrev_ptr += bytes_read;
13346 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13347 abbrev_ptr += 1;
13348
13349 /* now read in declarations */
13350 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13351 abbrev_ptr += bytes_read;
13352 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13353 abbrev_ptr += bytes_read;
13354 while (abbrev_name)
13355 {
f3dd6933 13356 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13357 {
f3dd6933
DJ
13358 allocated_attrs += ATTR_ALLOC_CHUNK;
13359 cur_attrs
13360 = xrealloc (cur_attrs, (allocated_attrs
13361 * sizeof (struct attr_abbrev)));
c906108c 13362 }
ae038cb0 13363
f3dd6933
DJ
13364 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13365 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13366 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13367 abbrev_ptr += bytes_read;
13368 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13369 abbrev_ptr += bytes_read;
13370 }
13371
433df2d4 13372 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13373 (cur_abbrev->num_attrs
13374 * sizeof (struct attr_abbrev)));
13375 memcpy (cur_abbrev->attrs, cur_attrs,
13376 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13377
433df2d4 13378 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13379
13380 /* Get next abbreviation.
13381 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13382 always properly terminated with an abbrev number of 0.
13383 Exit loop if we encounter an abbreviation which we have
13384 already read (which means we are about to read the abbreviations
13385 for the next compile unit) or if the end of the abbreviation
13386 table is reached. */
433df2d4 13387 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13388 break;
13389 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13390 abbrev_ptr += bytes_read;
433df2d4 13391 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13392 break;
13393 }
f3dd6933
DJ
13394
13395 xfree (cur_attrs);
433df2d4 13396 return abbrev_table;
c906108c
SS
13397}
13398
433df2d4 13399/* Free the resources held by ABBREV_TABLE. */
c906108c 13400
c906108c 13401static void
433df2d4 13402abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13403{
433df2d4
DE
13404 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13405 xfree (abbrev_table);
c906108c
SS
13406}
13407
f4dc4d17
DE
13408/* Same as abbrev_table_free but as a cleanup.
13409 We pass in a pointer to the pointer to the table so that we can
13410 set the pointer to NULL when we're done. It also simplifies
13411 build_type_unit_groups. */
13412
13413static void
13414abbrev_table_free_cleanup (void *table_ptr)
13415{
13416 struct abbrev_table **abbrev_table_ptr = table_ptr;
13417
13418 if (*abbrev_table_ptr != NULL)
13419 abbrev_table_free (*abbrev_table_ptr);
13420 *abbrev_table_ptr = NULL;
13421}
13422
433df2d4
DE
13423/* Read the abbrev table for CU from ABBREV_SECTION. */
13424
13425static void
13426dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13427 struct dwarf2_section_info *abbrev_section)
c906108c 13428{
433df2d4
DE
13429 cu->abbrev_table =
13430 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13431}
c906108c 13432
433df2d4 13433/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13434
433df2d4
DE
13435static void
13436dwarf2_free_abbrev_table (void *ptr_to_cu)
13437{
13438 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13439
433df2d4
DE
13440 abbrev_table_free (cu->abbrev_table);
13441 /* Set this to NULL so that we SEGV if we try to read it later,
13442 and also because free_comp_unit verifies this is NULL. */
13443 cu->abbrev_table = NULL;
13444}
13445\f
72bf9492
DJ
13446/* Returns nonzero if TAG represents a type that we might generate a partial
13447 symbol for. */
13448
13449static int
13450is_type_tag_for_partial (int tag)
13451{
13452 switch (tag)
13453 {
13454#if 0
13455 /* Some types that would be reasonable to generate partial symbols for,
13456 that we don't at present. */
13457 case DW_TAG_array_type:
13458 case DW_TAG_file_type:
13459 case DW_TAG_ptr_to_member_type:
13460 case DW_TAG_set_type:
13461 case DW_TAG_string_type:
13462 case DW_TAG_subroutine_type:
13463#endif
13464 case DW_TAG_base_type:
13465 case DW_TAG_class_type:
680b30c7 13466 case DW_TAG_interface_type:
72bf9492
DJ
13467 case DW_TAG_enumeration_type:
13468 case DW_TAG_structure_type:
13469 case DW_TAG_subrange_type:
13470 case DW_TAG_typedef:
13471 case DW_TAG_union_type:
13472 return 1;
13473 default:
13474 return 0;
13475 }
13476}
13477
13478/* Load all DIEs that are interesting for partial symbols into memory. */
13479
13480static struct partial_die_info *
dee91e82 13481load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13482 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13483{
dee91e82 13484 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13485 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13486 struct partial_die_info *part_die;
13487 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13488 struct abbrev_info *abbrev;
13489 unsigned int bytes_read;
5afb4e99 13490 unsigned int load_all = 0;
72bf9492
DJ
13491 int nesting_level = 1;
13492
13493 parent_die = NULL;
13494 last_die = NULL;
13495
7adf1e79
DE
13496 gdb_assert (cu->per_cu != NULL);
13497 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13498 load_all = 1;
13499
72bf9492
DJ
13500 cu->partial_dies
13501 = htab_create_alloc_ex (cu->header.length / 12,
13502 partial_die_hash,
13503 partial_die_eq,
13504 NULL,
13505 &cu->comp_unit_obstack,
13506 hashtab_obstack_allocate,
13507 dummy_obstack_deallocate);
13508
13509 part_die = obstack_alloc (&cu->comp_unit_obstack,
13510 sizeof (struct partial_die_info));
13511
13512 while (1)
13513 {
13514 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13515
13516 /* A NULL abbrev means the end of a series of children. */
13517 if (abbrev == NULL)
13518 {
13519 if (--nesting_level == 0)
13520 {
13521 /* PART_DIE was probably the last thing allocated on the
13522 comp_unit_obstack, so we could call obstack_free
13523 here. We don't do that because the waste is small,
13524 and will be cleaned up when we're done with this
13525 compilation unit. This way, we're also more robust
13526 against other users of the comp_unit_obstack. */
13527 return first_die;
13528 }
13529 info_ptr += bytes_read;
13530 last_die = parent_die;
13531 parent_die = parent_die->die_parent;
13532 continue;
13533 }
13534
98bfdba5
PA
13535 /* Check for template arguments. We never save these; if
13536 they're seen, we just mark the parent, and go on our way. */
13537 if (parent_die != NULL
13538 && cu->language == language_cplus
13539 && (abbrev->tag == DW_TAG_template_type_param
13540 || abbrev->tag == DW_TAG_template_value_param))
13541 {
13542 parent_die->has_template_arguments = 1;
13543
13544 if (!load_all)
13545 {
13546 /* We don't need a partial DIE for the template argument. */
dee91e82 13547 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13548 continue;
13549 }
13550 }
13551
0d99eb77 13552 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13553 Skip their other children. */
13554 if (!load_all
13555 && cu->language == language_cplus
13556 && parent_die != NULL
13557 && parent_die->tag == DW_TAG_subprogram)
13558 {
dee91e82 13559 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13560 continue;
13561 }
13562
5afb4e99
DJ
13563 /* Check whether this DIE is interesting enough to save. Normally
13564 we would not be interested in members here, but there may be
13565 later variables referencing them via DW_AT_specification (for
13566 static members). */
13567 if (!load_all
13568 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13569 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13570 && abbrev->tag != DW_TAG_enumerator
13571 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13572 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13573 && abbrev->tag != DW_TAG_variable
5afb4e99 13574 && abbrev->tag != DW_TAG_namespace
f55ee35c 13575 && abbrev->tag != DW_TAG_module
95554aad
TT
13576 && abbrev->tag != DW_TAG_member
13577 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13578 {
13579 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13580 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13581 continue;
13582 }
13583
dee91e82
DE
13584 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13585 info_ptr);
72bf9492
DJ
13586
13587 /* This two-pass algorithm for processing partial symbols has a
13588 high cost in cache pressure. Thus, handle some simple cases
13589 here which cover the majority of C partial symbols. DIEs
13590 which neither have specification tags in them, nor could have
13591 specification tags elsewhere pointing at them, can simply be
13592 processed and discarded.
13593
13594 This segment is also optional; scan_partial_symbols and
13595 add_partial_symbol will handle these DIEs if we chain
13596 them in normally. When compilers which do not emit large
13597 quantities of duplicate debug information are more common,
13598 this code can probably be removed. */
13599
13600 /* Any complete simple types at the top level (pretty much all
13601 of them, for a language without namespaces), can be processed
13602 directly. */
13603 if (parent_die == NULL
13604 && part_die->has_specification == 0
13605 && part_die->is_declaration == 0
d8228535 13606 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13607 || part_die->tag == DW_TAG_base_type
13608 || part_die->tag == DW_TAG_subrange_type))
13609 {
13610 if (building_psymtab && part_die->name != NULL)
04a679b8 13611 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13612 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13613 &objfile->static_psymbols,
13614 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13615 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13616 continue;
13617 }
13618
d8228535
JK
13619 /* The exception for DW_TAG_typedef with has_children above is
13620 a workaround of GCC PR debug/47510. In the case of this complaint
13621 type_name_no_tag_or_error will error on such types later.
13622
13623 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13624 it could not find the child DIEs referenced later, this is checked
13625 above. In correct DWARF DW_TAG_typedef should have no children. */
13626
13627 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13628 complaint (&symfile_complaints,
13629 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13630 "- DIE at 0x%x [in module %s]"),
b64f50a1 13631 part_die->offset.sect_off, objfile->name);
d8228535 13632
72bf9492
DJ
13633 /* If we're at the second level, and we're an enumerator, and
13634 our parent has no specification (meaning possibly lives in a
13635 namespace elsewhere), then we can add the partial symbol now
13636 instead of queueing it. */
13637 if (part_die->tag == DW_TAG_enumerator
13638 && parent_die != NULL
13639 && parent_die->die_parent == NULL
13640 && parent_die->tag == DW_TAG_enumeration_type
13641 && parent_die->has_specification == 0)
13642 {
13643 if (part_die->name == NULL)
3e43a32a
MS
13644 complaint (&symfile_complaints,
13645 _("malformed enumerator DIE ignored"));
72bf9492 13646 else if (building_psymtab)
04a679b8 13647 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13648 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13649 (cu->language == language_cplus
13650 || cu->language == language_java)
bb5ed363
DE
13651 ? &objfile->global_psymbols
13652 : &objfile->static_psymbols,
13653 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13654
dee91e82 13655 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13656 continue;
13657 }
13658
13659 /* We'll save this DIE so link it in. */
13660 part_die->die_parent = parent_die;
13661 part_die->die_sibling = NULL;
13662 part_die->die_child = NULL;
13663
13664 if (last_die && last_die == parent_die)
13665 last_die->die_child = part_die;
13666 else if (last_die)
13667 last_die->die_sibling = part_die;
13668
13669 last_die = part_die;
13670
13671 if (first_die == NULL)
13672 first_die = part_die;
13673
13674 /* Maybe add the DIE to the hash table. Not all DIEs that we
13675 find interesting need to be in the hash table, because we
13676 also have the parent/sibling/child chains; only those that we
13677 might refer to by offset later during partial symbol reading.
13678
13679 For now this means things that might have be the target of a
13680 DW_AT_specification, DW_AT_abstract_origin, or
13681 DW_AT_extension. DW_AT_extension will refer only to
13682 namespaces; DW_AT_abstract_origin refers to functions (and
13683 many things under the function DIE, but we do not recurse
13684 into function DIEs during partial symbol reading) and
13685 possibly variables as well; DW_AT_specification refers to
13686 declarations. Declarations ought to have the DW_AT_declaration
13687 flag. It happens that GCC forgets to put it in sometimes, but
13688 only for functions, not for types.
13689
13690 Adding more things than necessary to the hash table is harmless
13691 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13692 wasted time in find_partial_die, when we reread the compilation
13693 unit with load_all_dies set. */
72bf9492 13694
5afb4e99 13695 if (load_all
72929c62 13696 || abbrev->tag == DW_TAG_constant
5afb4e99 13697 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13698 || abbrev->tag == DW_TAG_variable
13699 || abbrev->tag == DW_TAG_namespace
13700 || part_die->is_declaration)
13701 {
13702 void **slot;
13703
13704 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13705 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13706 *slot = part_die;
13707 }
13708
13709 part_die = obstack_alloc (&cu->comp_unit_obstack,
13710 sizeof (struct partial_die_info));
13711
13712 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13713 we have no reason to follow the children of structures; for other
98bfdba5
PA
13714 languages we have to, so that we can get at method physnames
13715 to infer fully qualified class names, for DW_AT_specification,
13716 and for C++ template arguments. For C++, we also look one level
13717 inside functions to find template arguments (if the name of the
13718 function does not already contain the template arguments).
bc30ff58
JB
13719
13720 For Ada, we need to scan the children of subprograms and lexical
13721 blocks as well because Ada allows the definition of nested
13722 entities that could be interesting for the debugger, such as
13723 nested subprograms for instance. */
72bf9492 13724 if (last_die->has_children
5afb4e99
DJ
13725 && (load_all
13726 || last_die->tag == DW_TAG_namespace
f55ee35c 13727 || last_die->tag == DW_TAG_module
72bf9492 13728 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13729 || (cu->language == language_cplus
13730 && last_die->tag == DW_TAG_subprogram
13731 && (last_die->name == NULL
13732 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13733 || (cu->language != language_c
13734 && (last_die->tag == DW_TAG_class_type
680b30c7 13735 || last_die->tag == DW_TAG_interface_type
72bf9492 13736 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13737 || last_die->tag == DW_TAG_union_type))
13738 || (cu->language == language_ada
13739 && (last_die->tag == DW_TAG_subprogram
13740 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13741 {
13742 nesting_level++;
13743 parent_die = last_die;
13744 continue;
13745 }
13746
13747 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13748 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13749
13750 /* Back to the top, do it again. */
13751 }
13752}
13753
c906108c
SS
13754/* Read a minimal amount of information into the minimal die structure. */
13755
d521ce57 13756static const gdb_byte *
dee91e82
DE
13757read_partial_die (const struct die_reader_specs *reader,
13758 struct partial_die_info *part_die,
13759 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 13760 const gdb_byte *info_ptr)
c906108c 13761{
dee91e82 13762 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13763 struct objfile *objfile = cu->objfile;
d521ce57 13764 const gdb_byte *buffer = reader->buffer;
fa238c03 13765 unsigned int i;
c906108c 13766 struct attribute attr;
c5aa993b 13767 int has_low_pc_attr = 0;
c906108c 13768 int has_high_pc_attr = 0;
91da1414 13769 int high_pc_relative = 0;
c906108c 13770
72bf9492 13771 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13772
b64f50a1 13773 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13774
13775 info_ptr += abbrev_len;
13776
13777 if (abbrev == NULL)
13778 return info_ptr;
13779
c906108c
SS
13780 part_die->tag = abbrev->tag;
13781 part_die->has_children = abbrev->has_children;
c906108c
SS
13782
13783 for (i = 0; i < abbrev->num_attrs; ++i)
13784 {
dee91e82 13785 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13786
13787 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13788 partial symbol table. */
c906108c
SS
13789 switch (attr.name)
13790 {
13791 case DW_AT_name:
71c25dea
TT
13792 switch (part_die->tag)
13793 {
13794 case DW_TAG_compile_unit:
95554aad 13795 case DW_TAG_partial_unit:
348e048f 13796 case DW_TAG_type_unit:
71c25dea
TT
13797 /* Compilation units have a DW_AT_name that is a filename, not
13798 a source language identifier. */
13799 case DW_TAG_enumeration_type:
13800 case DW_TAG_enumerator:
13801 /* These tags always have simple identifiers already; no need
13802 to canonicalize them. */
13803 part_die->name = DW_STRING (&attr);
13804 break;
13805 default:
13806 part_die->name
13807 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13808 &objfile->objfile_obstack);
71c25dea
TT
13809 break;
13810 }
c906108c 13811 break;
31ef98ae 13812 case DW_AT_linkage_name:
c906108c 13813 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13814 /* Note that both forms of linkage name might appear. We
13815 assume they will be the same, and we only store the last
13816 one we see. */
94af9270
KS
13817 if (cu->language == language_ada)
13818 part_die->name = DW_STRING (&attr);
abc72ce4 13819 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13820 break;
13821 case DW_AT_low_pc:
13822 has_low_pc_attr = 1;
13823 part_die->lowpc = DW_ADDR (&attr);
13824 break;
13825 case DW_AT_high_pc:
13826 has_high_pc_attr = 1;
3019eac3
DE
13827 if (attr.form == DW_FORM_addr
13828 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13829 part_die->highpc = DW_ADDR (&attr);
13830 else
13831 {
13832 high_pc_relative = 1;
13833 part_die->highpc = DW_UNSND (&attr);
13834 }
c906108c
SS
13835 break;
13836 case DW_AT_location:
0963b4bd 13837 /* Support the .debug_loc offsets. */
8e19ed76
PS
13838 if (attr_form_is_block (&attr))
13839 {
95554aad 13840 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13841 }
3690dd37 13842 else if (attr_form_is_section_offset (&attr))
8e19ed76 13843 {
4d3c2250 13844 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13845 }
13846 else
13847 {
4d3c2250
KB
13848 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13849 "partial symbol information");
8e19ed76 13850 }
c906108c 13851 break;
c906108c
SS
13852 case DW_AT_external:
13853 part_die->is_external = DW_UNSND (&attr);
13854 break;
13855 case DW_AT_declaration:
13856 part_die->is_declaration = DW_UNSND (&attr);
13857 break;
13858 case DW_AT_type:
13859 part_die->has_type = 1;
13860 break;
13861 case DW_AT_abstract_origin:
13862 case DW_AT_specification:
72bf9492
DJ
13863 case DW_AT_extension:
13864 part_die->has_specification = 1;
c764a876 13865 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13866 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13867 || cu->per_cu->is_dwz);
c906108c
SS
13868 break;
13869 case DW_AT_sibling:
13870 /* Ignore absolute siblings, they might point outside of
13871 the current compile unit. */
13872 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13873 complaint (&symfile_complaints,
13874 _("ignoring absolute DW_AT_sibling"));
c906108c 13875 else
b64f50a1 13876 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13877 break;
fa4028e9
JB
13878 case DW_AT_byte_size:
13879 part_die->has_byte_size = 1;
13880 break;
68511cec
CES
13881 case DW_AT_calling_convention:
13882 /* DWARF doesn't provide a way to identify a program's source-level
13883 entry point. DW_AT_calling_convention attributes are only meant
13884 to describe functions' calling conventions.
13885
13886 However, because it's a necessary piece of information in
13887 Fortran, and because DW_CC_program is the only piece of debugging
13888 information whose definition refers to a 'main program' at all,
13889 several compilers have begun marking Fortran main programs with
13890 DW_CC_program --- even when those functions use the standard
13891 calling conventions.
13892
13893 So until DWARF specifies a way to provide this information and
13894 compilers pick up the new representation, we'll support this
13895 practice. */
13896 if (DW_UNSND (&attr) == DW_CC_program
13897 && cu->language == language_fortran)
01f8c46d
JK
13898 {
13899 set_main_name (part_die->name);
13900
13901 /* As this DIE has a static linkage the name would be difficult
13902 to look up later. */
13903 language_of_main = language_fortran;
13904 }
68511cec 13905 break;
481860b3
GB
13906 case DW_AT_inline:
13907 if (DW_UNSND (&attr) == DW_INL_inlined
13908 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13909 part_die->may_be_inlined = 1;
13910 break;
95554aad
TT
13911
13912 case DW_AT_import:
13913 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13914 {
13915 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13916 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13917 || cu->per_cu->is_dwz);
13918 }
95554aad
TT
13919 break;
13920
c906108c
SS
13921 default:
13922 break;
13923 }
13924 }
13925
91da1414
MW
13926 if (high_pc_relative)
13927 part_die->highpc += part_die->lowpc;
13928
9373cf26
JK
13929 if (has_low_pc_attr && has_high_pc_attr)
13930 {
13931 /* When using the GNU linker, .gnu.linkonce. sections are used to
13932 eliminate duplicate copies of functions and vtables and such.
13933 The linker will arbitrarily choose one and discard the others.
13934 The AT_*_pc values for such functions refer to local labels in
13935 these sections. If the section from that file was discarded, the
13936 labels are not in the output, so the relocs get a value of 0.
13937 If this is a discarded function, mark the pc bounds as invalid,
13938 so that GDB will ignore it. */
13939 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13940 {
bb5ed363 13941 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13942
13943 complaint (&symfile_complaints,
13944 _("DW_AT_low_pc %s is zero "
13945 "for DIE at 0x%x [in module %s]"),
13946 paddress (gdbarch, part_die->lowpc),
b64f50a1 13947 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13948 }
13949 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13950 else if (part_die->lowpc >= part_die->highpc)
13951 {
bb5ed363 13952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13953
13954 complaint (&symfile_complaints,
13955 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13956 "for DIE at 0x%x [in module %s]"),
13957 paddress (gdbarch, part_die->lowpc),
13958 paddress (gdbarch, part_die->highpc),
b64f50a1 13959 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13960 }
13961 else
13962 part_die->has_pc_info = 1;
13963 }
85cbf3d3 13964
c906108c
SS
13965 return info_ptr;
13966}
13967
72bf9492
DJ
13968/* Find a cached partial DIE at OFFSET in CU. */
13969
13970static struct partial_die_info *
b64f50a1 13971find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13972{
13973 struct partial_die_info *lookup_die = NULL;
13974 struct partial_die_info part_die;
13975
13976 part_die.offset = offset;
b64f50a1
JK
13977 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13978 offset.sect_off);
72bf9492 13979
72bf9492
DJ
13980 return lookup_die;
13981}
13982
348e048f
DE
13983/* Find a partial DIE at OFFSET, which may or may not be in CU,
13984 except in the case of .debug_types DIEs which do not reference
13985 outside their CU (they do however referencing other types via
55f1336d 13986 DW_FORM_ref_sig8). */
72bf9492
DJ
13987
13988static struct partial_die_info *
36586728 13989find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13990{
bb5ed363 13991 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13992 struct dwarf2_per_cu_data *per_cu = NULL;
13993 struct partial_die_info *pd = NULL;
72bf9492 13994
36586728
TT
13995 if (offset_in_dwz == cu->per_cu->is_dwz
13996 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13997 {
13998 pd = find_partial_die_in_comp_unit (offset, cu);
13999 if (pd != NULL)
14000 return pd;
0d99eb77
DE
14001 /* We missed recording what we needed.
14002 Load all dies and try again. */
14003 per_cu = cu->per_cu;
5afb4e99 14004 }
0d99eb77
DE
14005 else
14006 {
14007 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14008 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14009 {
14010 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14011 " external reference to offset 0x%lx [in module %s].\n"),
14012 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14013 bfd_get_filename (objfile->obfd));
14014 }
36586728
TT
14015 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14016 objfile);
72bf9492 14017
0d99eb77
DE
14018 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14019 load_partial_comp_unit (per_cu);
ae038cb0 14020
0d99eb77
DE
14021 per_cu->cu->last_used = 0;
14022 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14023 }
5afb4e99 14024
dee91e82
DE
14025 /* If we didn't find it, and not all dies have been loaded,
14026 load them all and try again. */
14027
5afb4e99
DJ
14028 if (pd == NULL && per_cu->load_all_dies == 0)
14029 {
5afb4e99 14030 per_cu->load_all_dies = 1;
fd820528
DE
14031
14032 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14033 THIS_CU->cu may already be in use. So we can't just free it and
14034 replace its DIEs with the ones we read in. Instead, we leave those
14035 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14036 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14037 set. */
dee91e82 14038 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14039
14040 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14041 }
14042
14043 if (pd == NULL)
14044 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14045 _("could not find partial DIE 0x%x "
14046 "in cache [from module %s]\n"),
b64f50a1 14047 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14048 return pd;
72bf9492
DJ
14049}
14050
abc72ce4
DE
14051/* See if we can figure out if the class lives in a namespace. We do
14052 this by looking for a member function; its demangled name will
14053 contain namespace info, if there is any. */
14054
14055static void
14056guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14057 struct dwarf2_cu *cu)
14058{
14059 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14060 what template types look like, because the demangler
14061 frequently doesn't give the same name as the debug info. We
14062 could fix this by only using the demangled name to get the
14063 prefix (but see comment in read_structure_type). */
14064
14065 struct partial_die_info *real_pdi;
14066 struct partial_die_info *child_pdi;
14067
14068 /* If this DIE (this DIE's specification, if any) has a parent, then
14069 we should not do this. We'll prepend the parent's fully qualified
14070 name when we create the partial symbol. */
14071
14072 real_pdi = struct_pdi;
14073 while (real_pdi->has_specification)
36586728
TT
14074 real_pdi = find_partial_die (real_pdi->spec_offset,
14075 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14076
14077 if (real_pdi->die_parent != NULL)
14078 return;
14079
14080 for (child_pdi = struct_pdi->die_child;
14081 child_pdi != NULL;
14082 child_pdi = child_pdi->die_sibling)
14083 {
14084 if (child_pdi->tag == DW_TAG_subprogram
14085 && child_pdi->linkage_name != NULL)
14086 {
14087 char *actual_class_name
14088 = language_class_name_from_physname (cu->language_defn,
14089 child_pdi->linkage_name);
14090 if (actual_class_name != NULL)
14091 {
14092 struct_pdi->name
10f0c4bb
TT
14093 = obstack_copy0 (&cu->objfile->objfile_obstack,
14094 actual_class_name,
14095 strlen (actual_class_name));
abc72ce4
DE
14096 xfree (actual_class_name);
14097 }
14098 break;
14099 }
14100 }
14101}
14102
72bf9492
DJ
14103/* Adjust PART_DIE before generating a symbol for it. This function
14104 may set the is_external flag or change the DIE's name. */
14105
14106static void
14107fixup_partial_die (struct partial_die_info *part_die,
14108 struct dwarf2_cu *cu)
14109{
abc72ce4
DE
14110 /* Once we've fixed up a die, there's no point in doing so again.
14111 This also avoids a memory leak if we were to call
14112 guess_partial_die_structure_name multiple times. */
14113 if (part_die->fixup_called)
14114 return;
14115
72bf9492
DJ
14116 /* If we found a reference attribute and the DIE has no name, try
14117 to find a name in the referred to DIE. */
14118
14119 if (part_die->name == NULL && part_die->has_specification)
14120 {
14121 struct partial_die_info *spec_die;
72bf9492 14122
36586728
TT
14123 spec_die = find_partial_die (part_die->spec_offset,
14124 part_die->spec_is_dwz, cu);
72bf9492 14125
10b3939b 14126 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14127
14128 if (spec_die->name)
14129 {
14130 part_die->name = spec_die->name;
14131
14132 /* Copy DW_AT_external attribute if it is set. */
14133 if (spec_die->is_external)
14134 part_die->is_external = spec_die->is_external;
14135 }
14136 }
14137
14138 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14139
14140 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14141 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14142
abc72ce4
DE
14143 /* If there is no parent die to provide a namespace, and there are
14144 children, see if we can determine the namespace from their linkage
122d1940 14145 name. */
abc72ce4 14146 if (cu->language == language_cplus
8b70b953 14147 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14148 && part_die->die_parent == NULL
14149 && part_die->has_children
14150 && (part_die->tag == DW_TAG_class_type
14151 || part_die->tag == DW_TAG_structure_type
14152 || part_die->tag == DW_TAG_union_type))
14153 guess_partial_die_structure_name (part_die, cu);
14154
53832f31
TT
14155 /* GCC might emit a nameless struct or union that has a linkage
14156 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14157 if (part_die->name == NULL
96408a79
SA
14158 && (part_die->tag == DW_TAG_class_type
14159 || part_die->tag == DW_TAG_interface_type
14160 || part_die->tag == DW_TAG_structure_type
14161 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14162 && part_die->linkage_name != NULL)
14163 {
14164 char *demangled;
14165
8de20a37 14166 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14167 if (demangled)
14168 {
96408a79
SA
14169 const char *base;
14170
14171 /* Strip any leading namespaces/classes, keep only the base name.
14172 DW_AT_name for named DIEs does not contain the prefixes. */
14173 base = strrchr (demangled, ':');
14174 if (base && base > demangled && base[-1] == ':')
14175 base++;
14176 else
14177 base = demangled;
14178
10f0c4bb
TT
14179 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14180 base, strlen (base));
53832f31
TT
14181 xfree (demangled);
14182 }
14183 }
14184
abc72ce4 14185 part_die->fixup_called = 1;
72bf9492
DJ
14186}
14187
a8329558 14188/* Read an attribute value described by an attribute form. */
c906108c 14189
d521ce57 14190static const gdb_byte *
dee91e82
DE
14191read_attribute_value (const struct die_reader_specs *reader,
14192 struct attribute *attr, unsigned form,
d521ce57 14193 const gdb_byte *info_ptr)
c906108c 14194{
dee91e82
DE
14195 struct dwarf2_cu *cu = reader->cu;
14196 bfd *abfd = reader->abfd;
e7c27a73 14197 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14198 unsigned int bytes_read;
14199 struct dwarf_block *blk;
14200
a8329558
KW
14201 attr->form = form;
14202 switch (form)
c906108c 14203 {
c906108c 14204 case DW_FORM_ref_addr:
ae411497 14205 if (cu->header.version == 2)
4568ecf9 14206 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14207 else
4568ecf9
DE
14208 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14209 &cu->header, &bytes_read);
ae411497
TT
14210 info_ptr += bytes_read;
14211 break;
36586728
TT
14212 case DW_FORM_GNU_ref_alt:
14213 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14214 info_ptr += bytes_read;
14215 break;
ae411497 14216 case DW_FORM_addr:
e7c27a73 14217 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14218 info_ptr += bytes_read;
c906108c
SS
14219 break;
14220 case DW_FORM_block2:
7b5a2f43 14221 blk = dwarf_alloc_block (cu);
c906108c
SS
14222 blk->size = read_2_bytes (abfd, info_ptr);
14223 info_ptr += 2;
14224 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14225 info_ptr += blk->size;
14226 DW_BLOCK (attr) = blk;
14227 break;
14228 case DW_FORM_block4:
7b5a2f43 14229 blk = dwarf_alloc_block (cu);
c906108c
SS
14230 blk->size = read_4_bytes (abfd, info_ptr);
14231 info_ptr += 4;
14232 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14233 info_ptr += blk->size;
14234 DW_BLOCK (attr) = blk;
14235 break;
14236 case DW_FORM_data2:
14237 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14238 info_ptr += 2;
14239 break;
14240 case DW_FORM_data4:
14241 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14242 info_ptr += 4;
14243 break;
14244 case DW_FORM_data8:
14245 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14246 info_ptr += 8;
14247 break;
2dc7f7b3
TT
14248 case DW_FORM_sec_offset:
14249 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14250 info_ptr += bytes_read;
14251 break;
c906108c 14252 case DW_FORM_string:
9b1c24c8 14253 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14254 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14255 info_ptr += bytes_read;
14256 break;
4bdf3d34 14257 case DW_FORM_strp:
36586728
TT
14258 if (!cu->per_cu->is_dwz)
14259 {
14260 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14261 &bytes_read);
14262 DW_STRING_IS_CANONICAL (attr) = 0;
14263 info_ptr += bytes_read;
14264 break;
14265 }
14266 /* FALLTHROUGH */
14267 case DW_FORM_GNU_strp_alt:
14268 {
14269 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14270 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14271 &bytes_read);
14272
14273 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14274 DW_STRING_IS_CANONICAL (attr) = 0;
14275 info_ptr += bytes_read;
14276 }
4bdf3d34 14277 break;
2dc7f7b3 14278 case DW_FORM_exprloc:
c906108c 14279 case DW_FORM_block:
7b5a2f43 14280 blk = dwarf_alloc_block (cu);
c906108c
SS
14281 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14282 info_ptr += bytes_read;
14283 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14284 info_ptr += blk->size;
14285 DW_BLOCK (attr) = blk;
14286 break;
14287 case DW_FORM_block1:
7b5a2f43 14288 blk = dwarf_alloc_block (cu);
c906108c
SS
14289 blk->size = read_1_byte (abfd, info_ptr);
14290 info_ptr += 1;
14291 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14292 info_ptr += blk->size;
14293 DW_BLOCK (attr) = blk;
14294 break;
14295 case DW_FORM_data1:
14296 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14297 info_ptr += 1;
14298 break;
14299 case DW_FORM_flag:
14300 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14301 info_ptr += 1;
14302 break;
2dc7f7b3
TT
14303 case DW_FORM_flag_present:
14304 DW_UNSND (attr) = 1;
14305 break;
c906108c
SS
14306 case DW_FORM_sdata:
14307 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14308 info_ptr += bytes_read;
14309 break;
14310 case DW_FORM_udata:
14311 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14312 info_ptr += bytes_read;
14313 break;
14314 case DW_FORM_ref1:
4568ecf9
DE
14315 DW_UNSND (attr) = (cu->header.offset.sect_off
14316 + read_1_byte (abfd, info_ptr));
c906108c
SS
14317 info_ptr += 1;
14318 break;
14319 case DW_FORM_ref2:
4568ecf9
DE
14320 DW_UNSND (attr) = (cu->header.offset.sect_off
14321 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14322 info_ptr += 2;
14323 break;
14324 case DW_FORM_ref4:
4568ecf9
DE
14325 DW_UNSND (attr) = (cu->header.offset.sect_off
14326 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14327 info_ptr += 4;
14328 break;
613e1657 14329 case DW_FORM_ref8:
4568ecf9
DE
14330 DW_UNSND (attr) = (cu->header.offset.sect_off
14331 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14332 info_ptr += 8;
14333 break;
55f1336d 14334 case DW_FORM_ref_sig8:
ac9ec31b 14335 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14336 info_ptr += 8;
14337 break;
c906108c 14338 case DW_FORM_ref_udata:
4568ecf9
DE
14339 DW_UNSND (attr) = (cu->header.offset.sect_off
14340 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14341 info_ptr += bytes_read;
14342 break;
c906108c 14343 case DW_FORM_indirect:
a8329558
KW
14344 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14345 info_ptr += bytes_read;
dee91e82 14346 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14347 break;
3019eac3
DE
14348 case DW_FORM_GNU_addr_index:
14349 if (reader->dwo_file == NULL)
14350 {
14351 /* For now flag a hard error.
14352 Later we can turn this into a complaint. */
14353 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14354 dwarf_form_name (form),
14355 bfd_get_filename (abfd));
14356 }
14357 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14358 info_ptr += bytes_read;
14359 break;
14360 case DW_FORM_GNU_str_index:
14361 if (reader->dwo_file == NULL)
14362 {
14363 /* For now flag a hard error.
14364 Later we can turn this into a complaint if warranted. */
14365 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14366 dwarf_form_name (form),
14367 bfd_get_filename (abfd));
14368 }
14369 {
14370 ULONGEST str_index =
14371 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14372
14373 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14374 DW_STRING_IS_CANONICAL (attr) = 0;
14375 info_ptr += bytes_read;
14376 }
14377 break;
c906108c 14378 default:
8a3fe4f8 14379 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14380 dwarf_form_name (form),
14381 bfd_get_filename (abfd));
c906108c 14382 }
28e94949 14383
36586728
TT
14384 /* Super hack. */
14385 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14386 attr->form = DW_FORM_GNU_ref_alt;
14387
28e94949
JB
14388 /* We have seen instances where the compiler tried to emit a byte
14389 size attribute of -1 which ended up being encoded as an unsigned
14390 0xffffffff. Although 0xffffffff is technically a valid size value,
14391 an object of this size seems pretty unlikely so we can relatively
14392 safely treat these cases as if the size attribute was invalid and
14393 treat them as zero by default. */
14394 if (attr->name == DW_AT_byte_size
14395 && form == DW_FORM_data4
14396 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14397 {
14398 complaint
14399 (&symfile_complaints,
43bbcdc2
PH
14400 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14401 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14402 DW_UNSND (attr) = 0;
14403 }
28e94949 14404
c906108c
SS
14405 return info_ptr;
14406}
14407
a8329558
KW
14408/* Read an attribute described by an abbreviated attribute. */
14409
d521ce57 14410static const gdb_byte *
dee91e82
DE
14411read_attribute (const struct die_reader_specs *reader,
14412 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14413 const gdb_byte *info_ptr)
a8329558
KW
14414{
14415 attr->name = abbrev->name;
dee91e82 14416 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14417}
14418
0963b4bd 14419/* Read dwarf information from a buffer. */
c906108c
SS
14420
14421static unsigned int
a1855c1d 14422read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14423{
fe1b8b76 14424 return bfd_get_8 (abfd, buf);
c906108c
SS
14425}
14426
14427static int
a1855c1d 14428read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14429{
fe1b8b76 14430 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14431}
14432
14433static unsigned int
a1855c1d 14434read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14435{
fe1b8b76 14436 return bfd_get_16 (abfd, buf);
c906108c
SS
14437}
14438
21ae7a4d 14439static int
a1855c1d 14440read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14441{
14442 return bfd_get_signed_16 (abfd, buf);
14443}
14444
c906108c 14445static unsigned int
a1855c1d 14446read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14447{
fe1b8b76 14448 return bfd_get_32 (abfd, buf);
c906108c
SS
14449}
14450
21ae7a4d 14451static int
a1855c1d 14452read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14453{
14454 return bfd_get_signed_32 (abfd, buf);
14455}
14456
93311388 14457static ULONGEST
a1855c1d 14458read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14459{
fe1b8b76 14460 return bfd_get_64 (abfd, buf);
c906108c
SS
14461}
14462
14463static CORE_ADDR
d521ce57 14464read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14465 unsigned int *bytes_read)
c906108c 14466{
e7c27a73 14467 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14468 CORE_ADDR retval = 0;
14469
107d2387 14470 if (cu_header->signed_addr_p)
c906108c 14471 {
107d2387
AC
14472 switch (cu_header->addr_size)
14473 {
14474 case 2:
fe1b8b76 14475 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14476 break;
14477 case 4:
fe1b8b76 14478 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14479 break;
14480 case 8:
fe1b8b76 14481 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14482 break;
14483 default:
8e65ff28 14484 internal_error (__FILE__, __LINE__,
e2e0b3e5 14485 _("read_address: bad switch, signed [in module %s]"),
659b0389 14486 bfd_get_filename (abfd));
107d2387
AC
14487 }
14488 }
14489 else
14490 {
14491 switch (cu_header->addr_size)
14492 {
14493 case 2:
fe1b8b76 14494 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14495 break;
14496 case 4:
fe1b8b76 14497 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14498 break;
14499 case 8:
fe1b8b76 14500 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14501 break;
14502 default:
8e65ff28 14503 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14504 _("read_address: bad switch, "
14505 "unsigned [in module %s]"),
659b0389 14506 bfd_get_filename (abfd));
107d2387 14507 }
c906108c 14508 }
64367e0a 14509
107d2387
AC
14510 *bytes_read = cu_header->addr_size;
14511 return retval;
c906108c
SS
14512}
14513
f7ef9339 14514/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14515 specification allows the initial length to take up either 4 bytes
14516 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14517 bytes describe the length and all offsets will be 8 bytes in length
14518 instead of 4.
14519
f7ef9339
KB
14520 An older, non-standard 64-bit format is also handled by this
14521 function. The older format in question stores the initial length
14522 as an 8-byte quantity without an escape value. Lengths greater
14523 than 2^32 aren't very common which means that the initial 4 bytes
14524 is almost always zero. Since a length value of zero doesn't make
14525 sense for the 32-bit format, this initial zero can be considered to
14526 be an escape value which indicates the presence of the older 64-bit
14527 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14528 greater than 4GB. If it becomes necessary to handle lengths
14529 somewhat larger than 4GB, we could allow other small values (such
14530 as the non-sensical values of 1, 2, and 3) to also be used as
14531 escape values indicating the presence of the old format.
f7ef9339 14532
917c78fc
MK
14533 The value returned via bytes_read should be used to increment the
14534 relevant pointer after calling read_initial_length().
c764a876 14535
613e1657
KB
14536 [ Note: read_initial_length() and read_offset() are based on the
14537 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14538 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14539 from:
14540
f7ef9339 14541 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14542
613e1657
KB
14543 This document is only a draft and is subject to change. (So beware.)
14544
f7ef9339 14545 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14546 determined empirically by examining 64-bit ELF files produced by
14547 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14548
14549 - Kevin, July 16, 2002
613e1657
KB
14550 ] */
14551
14552static LONGEST
d521ce57 14553read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 14554{
fe1b8b76 14555 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14556
dd373385 14557 if (length == 0xffffffff)
613e1657 14558 {
fe1b8b76 14559 length = bfd_get_64 (abfd, buf + 4);
613e1657 14560 *bytes_read = 12;
613e1657 14561 }
dd373385 14562 else if (length == 0)
f7ef9339 14563 {
dd373385 14564 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14565 length = bfd_get_64 (abfd, buf);
f7ef9339 14566 *bytes_read = 8;
f7ef9339 14567 }
613e1657
KB
14568 else
14569 {
14570 *bytes_read = 4;
613e1657
KB
14571 }
14572
c764a876
DE
14573 return length;
14574}
dd373385 14575
c764a876
DE
14576/* Cover function for read_initial_length.
14577 Returns the length of the object at BUF, and stores the size of the
14578 initial length in *BYTES_READ and stores the size that offsets will be in
14579 *OFFSET_SIZE.
14580 If the initial length size is not equivalent to that specified in
14581 CU_HEADER then issue a complaint.
14582 This is useful when reading non-comp-unit headers. */
dd373385 14583
c764a876 14584static LONGEST
d521ce57 14585read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
14586 const struct comp_unit_head *cu_header,
14587 unsigned int *bytes_read,
14588 unsigned int *offset_size)
14589{
14590 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14591
14592 gdb_assert (cu_header->initial_length_size == 4
14593 || cu_header->initial_length_size == 8
14594 || cu_header->initial_length_size == 12);
14595
14596 if (cu_header->initial_length_size != *bytes_read)
14597 complaint (&symfile_complaints,
14598 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14599
c764a876 14600 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14601 return length;
613e1657
KB
14602}
14603
14604/* Read an offset from the data stream. The size of the offset is
917c78fc 14605 given by cu_header->offset_size. */
613e1657
KB
14606
14607static LONGEST
d521ce57
TT
14608read_offset (bfd *abfd, const gdb_byte *buf,
14609 const struct comp_unit_head *cu_header,
891d2f0b 14610 unsigned int *bytes_read)
c764a876
DE
14611{
14612 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14613
c764a876
DE
14614 *bytes_read = cu_header->offset_size;
14615 return offset;
14616}
14617
14618/* Read an offset from the data stream. */
14619
14620static LONGEST
d521ce57 14621read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14622{
14623 LONGEST retval = 0;
14624
c764a876 14625 switch (offset_size)
613e1657
KB
14626 {
14627 case 4:
fe1b8b76 14628 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14629 break;
14630 case 8:
fe1b8b76 14631 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14632 break;
14633 default:
8e65ff28 14634 internal_error (__FILE__, __LINE__,
c764a876 14635 _("read_offset_1: bad switch [in module %s]"),
659b0389 14636 bfd_get_filename (abfd));
613e1657
KB
14637 }
14638
917c78fc 14639 return retval;
613e1657
KB
14640}
14641
d521ce57
TT
14642static const gdb_byte *
14643read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
14644{
14645 /* If the size of a host char is 8 bits, we can return a pointer
14646 to the buffer, otherwise we have to copy the data to a buffer
14647 allocated on the temporary obstack. */
4bdf3d34 14648 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14649 return buf;
c906108c
SS
14650}
14651
d521ce57
TT
14652static const char *
14653read_direct_string (bfd *abfd, const gdb_byte *buf,
14654 unsigned int *bytes_read_ptr)
c906108c
SS
14655{
14656 /* If the size of a host char is 8 bits, we can return a pointer
14657 to the string, otherwise we have to copy the string to a buffer
14658 allocated on the temporary obstack. */
4bdf3d34 14659 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14660 if (*buf == '\0')
14661 {
14662 *bytes_read_ptr = 1;
14663 return NULL;
14664 }
d521ce57
TT
14665 *bytes_read_ptr = strlen ((const char *) buf) + 1;
14666 return (const char *) buf;
4bdf3d34
JJ
14667}
14668
d521ce57 14669static const char *
cf2c3c16 14670read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14671{
be391dca 14672 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14673 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14674 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14675 bfd_get_filename (abfd));
dce234bc 14676 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14677 error (_("DW_FORM_strp pointing outside of "
14678 ".debug_str section [in module %s]"),
14679 bfd_get_filename (abfd));
4bdf3d34 14680 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14681 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14682 return NULL;
d521ce57 14683 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14684}
14685
36586728
TT
14686/* Read a string at offset STR_OFFSET in the .debug_str section from
14687 the .dwz file DWZ. Throw an error if the offset is too large. If
14688 the string consists of a single NUL byte, return NULL; otherwise
14689 return a pointer to the string. */
14690
d521ce57 14691static const char *
36586728
TT
14692read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14693{
14694 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14695
14696 if (dwz->str.buffer == NULL)
14697 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14698 "section [in module %s]"),
14699 bfd_get_filename (dwz->dwz_bfd));
14700 if (str_offset >= dwz->str.size)
14701 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14702 ".debug_str section [in module %s]"),
14703 bfd_get_filename (dwz->dwz_bfd));
14704 gdb_assert (HOST_CHAR_BIT == 8);
14705 if (dwz->str.buffer[str_offset] == '\0')
14706 return NULL;
d521ce57 14707 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
14708}
14709
d521ce57
TT
14710static const char *
14711read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
14712 const struct comp_unit_head *cu_header,
14713 unsigned int *bytes_read_ptr)
14714{
14715 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14716
14717 return read_indirect_string_at_offset (abfd, str_offset);
14718}
14719
12df843f 14720static ULONGEST
d521ce57
TT
14721read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
14722 unsigned int *bytes_read_ptr)
c906108c 14723{
12df843f 14724 ULONGEST result;
ce5d95e1 14725 unsigned int num_read;
c906108c
SS
14726 int i, shift;
14727 unsigned char byte;
14728
14729 result = 0;
14730 shift = 0;
14731 num_read = 0;
14732 i = 0;
14733 while (1)
14734 {
fe1b8b76 14735 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14736 buf++;
14737 num_read++;
12df843f 14738 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14739 if ((byte & 128) == 0)
14740 {
14741 break;
14742 }
14743 shift += 7;
14744 }
14745 *bytes_read_ptr = num_read;
14746 return result;
14747}
14748
12df843f 14749static LONGEST
d521ce57
TT
14750read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
14751 unsigned int *bytes_read_ptr)
c906108c 14752{
12df843f 14753 LONGEST result;
77e0b926 14754 int i, shift, num_read;
c906108c
SS
14755 unsigned char byte;
14756
14757 result = 0;
14758 shift = 0;
c906108c
SS
14759 num_read = 0;
14760 i = 0;
14761 while (1)
14762 {
fe1b8b76 14763 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14764 buf++;
14765 num_read++;
12df843f 14766 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14767 shift += 7;
14768 if ((byte & 128) == 0)
14769 {
14770 break;
14771 }
14772 }
77e0b926 14773 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14774 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14775 *bytes_read_ptr = num_read;
14776 return result;
14777}
14778
3019eac3
DE
14779/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14780 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14781 ADDR_SIZE is the size of addresses from the CU header. */
14782
14783static CORE_ADDR
14784read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14785{
14786 struct objfile *objfile = dwarf2_per_objfile->objfile;
14787 bfd *abfd = objfile->obfd;
14788 const gdb_byte *info_ptr;
14789
14790 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14791 if (dwarf2_per_objfile->addr.buffer == NULL)
14792 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14793 objfile->name);
14794 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14795 error (_("DW_FORM_addr_index pointing outside of "
14796 ".debug_addr section [in module %s]"),
14797 objfile->name);
14798 info_ptr = (dwarf2_per_objfile->addr.buffer
14799 + addr_base + addr_index * addr_size);
14800 if (addr_size == 4)
14801 return bfd_get_32 (abfd, info_ptr);
14802 else
14803 return bfd_get_64 (abfd, info_ptr);
14804}
14805
14806/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14807
14808static CORE_ADDR
14809read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14810{
14811 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14812}
14813
14814/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14815
14816static CORE_ADDR
d521ce57 14817read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
14818 unsigned int *bytes_read)
14819{
14820 bfd *abfd = cu->objfile->obfd;
14821 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14822
14823 return read_addr_index (cu, addr_index);
14824}
14825
14826/* Data structure to pass results from dwarf2_read_addr_index_reader
14827 back to dwarf2_read_addr_index. */
14828
14829struct dwarf2_read_addr_index_data
14830{
14831 ULONGEST addr_base;
14832 int addr_size;
14833};
14834
14835/* die_reader_func for dwarf2_read_addr_index. */
14836
14837static void
14838dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 14839 const gdb_byte *info_ptr,
3019eac3
DE
14840 struct die_info *comp_unit_die,
14841 int has_children,
14842 void *data)
14843{
14844 struct dwarf2_cu *cu = reader->cu;
14845 struct dwarf2_read_addr_index_data *aidata =
14846 (struct dwarf2_read_addr_index_data *) data;
14847
14848 aidata->addr_base = cu->addr_base;
14849 aidata->addr_size = cu->header.addr_size;
14850}
14851
14852/* Given an index in .debug_addr, fetch the value.
14853 NOTE: This can be called during dwarf expression evaluation,
14854 long after the debug information has been read, and thus per_cu->cu
14855 may no longer exist. */
14856
14857CORE_ADDR
14858dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14859 unsigned int addr_index)
14860{
14861 struct objfile *objfile = per_cu->objfile;
14862 struct dwarf2_cu *cu = per_cu->cu;
14863 ULONGEST addr_base;
14864 int addr_size;
14865
14866 /* This is intended to be called from outside this file. */
14867 dw2_setup (objfile);
14868
14869 /* We need addr_base and addr_size.
14870 If we don't have PER_CU->cu, we have to get it.
14871 Nasty, but the alternative is storing the needed info in PER_CU,
14872 which at this point doesn't seem justified: it's not clear how frequently
14873 it would get used and it would increase the size of every PER_CU.
14874 Entry points like dwarf2_per_cu_addr_size do a similar thing
14875 so we're not in uncharted territory here.
14876 Alas we need to be a bit more complicated as addr_base is contained
14877 in the DIE.
14878
14879 We don't need to read the entire CU(/TU).
14880 We just need the header and top level die.
a1b64ce1 14881
3019eac3 14882 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14883 For now we skip this optimization. */
3019eac3
DE
14884
14885 if (cu != NULL)
14886 {
14887 addr_base = cu->addr_base;
14888 addr_size = cu->header.addr_size;
14889 }
14890 else
14891 {
14892 struct dwarf2_read_addr_index_data aidata;
14893
a1b64ce1
DE
14894 /* Note: We can't use init_cutu_and_read_dies_simple here,
14895 we need addr_base. */
14896 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14897 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14898 addr_base = aidata.addr_base;
14899 addr_size = aidata.addr_size;
14900 }
14901
14902 return read_addr_index_1 (addr_index, addr_base, addr_size);
14903}
14904
14905/* Given a DW_AT_str_index, fetch the string. */
14906
d521ce57 14907static const char *
3019eac3
DE
14908read_str_index (const struct die_reader_specs *reader,
14909 struct dwarf2_cu *cu, ULONGEST str_index)
14910{
14911 struct objfile *objfile = dwarf2_per_objfile->objfile;
14912 const char *dwo_name = objfile->name;
14913 bfd *abfd = objfile->obfd;
14914 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 14915 const gdb_byte *info_ptr;
3019eac3
DE
14916 ULONGEST str_offset;
14917
14918 dwarf2_read_section (objfile, &sections->str);
14919 dwarf2_read_section (objfile, &sections->str_offsets);
14920 if (sections->str.buffer == NULL)
14921 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14922 " in CU at offset 0x%lx [in module %s]"),
14923 (long) cu->header.offset.sect_off, dwo_name);
14924 if (sections->str_offsets.buffer == NULL)
14925 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14926 " in CU at offset 0x%lx [in module %s]"),
14927 (long) cu->header.offset.sect_off, dwo_name);
14928 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14929 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14930 " section in CU at offset 0x%lx [in module %s]"),
14931 (long) cu->header.offset.sect_off, dwo_name);
14932 info_ptr = (sections->str_offsets.buffer
14933 + str_index * cu->header.offset_size);
14934 if (cu->header.offset_size == 4)
14935 str_offset = bfd_get_32 (abfd, info_ptr);
14936 else
14937 str_offset = bfd_get_64 (abfd, info_ptr);
14938 if (str_offset >= sections->str.size)
14939 error (_("Offset from DW_FORM_str_index pointing outside of"
14940 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14941 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 14942 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
14943}
14944
3019eac3
DE
14945/* Return the length of an LEB128 number in BUF. */
14946
14947static int
14948leb128_size (const gdb_byte *buf)
14949{
14950 const gdb_byte *begin = buf;
14951 gdb_byte byte;
14952
14953 while (1)
14954 {
14955 byte = *buf++;
14956 if ((byte & 128) == 0)
14957 return buf - begin;
14958 }
14959}
14960
c906108c 14961static void
e142c38c 14962set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14963{
14964 switch (lang)
14965 {
14966 case DW_LANG_C89:
76bee0cc 14967 case DW_LANG_C99:
c906108c 14968 case DW_LANG_C:
e142c38c 14969 cu->language = language_c;
c906108c
SS
14970 break;
14971 case DW_LANG_C_plus_plus:
e142c38c 14972 cu->language = language_cplus;
c906108c 14973 break;
6aecb9c2
JB
14974 case DW_LANG_D:
14975 cu->language = language_d;
14976 break;
c906108c
SS
14977 case DW_LANG_Fortran77:
14978 case DW_LANG_Fortran90:
b21b22e0 14979 case DW_LANG_Fortran95:
e142c38c 14980 cu->language = language_fortran;
c906108c 14981 break;
a766d390
DE
14982 case DW_LANG_Go:
14983 cu->language = language_go;
14984 break;
c906108c 14985 case DW_LANG_Mips_Assembler:
e142c38c 14986 cu->language = language_asm;
c906108c 14987 break;
bebd888e 14988 case DW_LANG_Java:
e142c38c 14989 cu->language = language_java;
bebd888e 14990 break;
c906108c 14991 case DW_LANG_Ada83:
8aaf0b47 14992 case DW_LANG_Ada95:
bc5f45f8
JB
14993 cu->language = language_ada;
14994 break;
72019c9c
GM
14995 case DW_LANG_Modula2:
14996 cu->language = language_m2;
14997 break;
fe8e67fd
PM
14998 case DW_LANG_Pascal83:
14999 cu->language = language_pascal;
15000 break;
22566fbd
DJ
15001 case DW_LANG_ObjC:
15002 cu->language = language_objc;
15003 break;
c906108c
SS
15004 case DW_LANG_Cobol74:
15005 case DW_LANG_Cobol85:
c906108c 15006 default:
e142c38c 15007 cu->language = language_minimal;
c906108c
SS
15008 break;
15009 }
e142c38c 15010 cu->language_defn = language_def (cu->language);
c906108c
SS
15011}
15012
15013/* Return the named attribute or NULL if not there. */
15014
15015static struct attribute *
e142c38c 15016dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15017{
a48e046c 15018 for (;;)
c906108c 15019 {
a48e046c
TT
15020 unsigned int i;
15021 struct attribute *spec = NULL;
15022
15023 for (i = 0; i < die->num_attrs; ++i)
15024 {
15025 if (die->attrs[i].name == name)
15026 return &die->attrs[i];
15027 if (die->attrs[i].name == DW_AT_specification
15028 || die->attrs[i].name == DW_AT_abstract_origin)
15029 spec = &die->attrs[i];
15030 }
15031
15032 if (!spec)
15033 break;
c906108c 15034
f2f0e013 15035 die = follow_die_ref (die, spec, &cu);
f2f0e013 15036 }
c5aa993b 15037
c906108c
SS
15038 return NULL;
15039}
15040
348e048f
DE
15041/* Return the named attribute or NULL if not there,
15042 but do not follow DW_AT_specification, etc.
15043 This is for use in contexts where we're reading .debug_types dies.
15044 Following DW_AT_specification, DW_AT_abstract_origin will take us
15045 back up the chain, and we want to go down. */
15046
15047static struct attribute *
45e58e77 15048dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15049{
15050 unsigned int i;
15051
15052 for (i = 0; i < die->num_attrs; ++i)
15053 if (die->attrs[i].name == name)
15054 return &die->attrs[i];
15055
15056 return NULL;
15057}
15058
05cf31d1
JB
15059/* Return non-zero iff the attribute NAME is defined for the given DIE,
15060 and holds a non-zero value. This function should only be used for
2dc7f7b3 15061 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15062
15063static int
15064dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15065{
15066 struct attribute *attr = dwarf2_attr (die, name, cu);
15067
15068 return (attr && DW_UNSND (attr));
15069}
15070
3ca72b44 15071static int
e142c38c 15072die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15073{
05cf31d1
JB
15074 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15075 which value is non-zero. However, we have to be careful with
15076 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15077 (via dwarf2_flag_true_p) follows this attribute. So we may
15078 end up accidently finding a declaration attribute that belongs
15079 to a different DIE referenced by the specification attribute,
15080 even though the given DIE does not have a declaration attribute. */
15081 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15082 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15083}
15084
63d06c5c 15085/* Return the die giving the specification for DIE, if there is
f2f0e013 15086 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15087 containing the return value on output. If there is no
15088 specification, but there is an abstract origin, that is
15089 returned. */
63d06c5c
DC
15090
15091static struct die_info *
f2f0e013 15092die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15093{
f2f0e013
DJ
15094 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15095 *spec_cu);
63d06c5c 15096
edb3359d
DJ
15097 if (spec_attr == NULL)
15098 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15099
63d06c5c
DC
15100 if (spec_attr == NULL)
15101 return NULL;
15102 else
f2f0e013 15103 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15104}
c906108c 15105
debd256d 15106/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15107 refers to.
15108 NOTE: This is also used as a "cleanup" function. */
15109
debd256d
JB
15110static void
15111free_line_header (struct line_header *lh)
15112{
15113 if (lh->standard_opcode_lengths)
a8bc7b56 15114 xfree (lh->standard_opcode_lengths);
debd256d
JB
15115
15116 /* Remember that all the lh->file_names[i].name pointers are
15117 pointers into debug_line_buffer, and don't need to be freed. */
15118 if (lh->file_names)
a8bc7b56 15119 xfree (lh->file_names);
debd256d
JB
15120
15121 /* Similarly for the include directory names. */
15122 if (lh->include_dirs)
a8bc7b56 15123 xfree (lh->include_dirs);
debd256d 15124
a8bc7b56 15125 xfree (lh);
debd256d
JB
15126}
15127
debd256d 15128/* Add an entry to LH's include directory table. */
ae2de4f8 15129
debd256d 15130static void
d521ce57 15131add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15132{
debd256d
JB
15133 /* Grow the array if necessary. */
15134 if (lh->include_dirs_size == 0)
c5aa993b 15135 {
debd256d
JB
15136 lh->include_dirs_size = 1; /* for testing */
15137 lh->include_dirs = xmalloc (lh->include_dirs_size
15138 * sizeof (*lh->include_dirs));
15139 }
15140 else if (lh->num_include_dirs >= lh->include_dirs_size)
15141 {
15142 lh->include_dirs_size *= 2;
15143 lh->include_dirs = xrealloc (lh->include_dirs,
15144 (lh->include_dirs_size
15145 * sizeof (*lh->include_dirs)));
c5aa993b 15146 }
c906108c 15147
debd256d
JB
15148 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15149}
6e70227d 15150
debd256d 15151/* Add an entry to LH's file name table. */
ae2de4f8 15152
debd256d
JB
15153static void
15154add_file_name (struct line_header *lh,
d521ce57 15155 const char *name,
debd256d
JB
15156 unsigned int dir_index,
15157 unsigned int mod_time,
15158 unsigned int length)
15159{
15160 struct file_entry *fe;
15161
15162 /* Grow the array if necessary. */
15163 if (lh->file_names_size == 0)
15164 {
15165 lh->file_names_size = 1; /* for testing */
15166 lh->file_names = xmalloc (lh->file_names_size
15167 * sizeof (*lh->file_names));
15168 }
15169 else if (lh->num_file_names >= lh->file_names_size)
15170 {
15171 lh->file_names_size *= 2;
15172 lh->file_names = xrealloc (lh->file_names,
15173 (lh->file_names_size
15174 * sizeof (*lh->file_names)));
15175 }
15176
15177 fe = &lh->file_names[lh->num_file_names++];
15178 fe->name = name;
15179 fe->dir_index = dir_index;
15180 fe->mod_time = mod_time;
15181 fe->length = length;
aaa75496 15182 fe->included_p = 0;
cb1df416 15183 fe->symtab = NULL;
debd256d 15184}
6e70227d 15185
36586728
TT
15186/* A convenience function to find the proper .debug_line section for a
15187 CU. */
15188
15189static struct dwarf2_section_info *
15190get_debug_line_section (struct dwarf2_cu *cu)
15191{
15192 struct dwarf2_section_info *section;
15193
15194 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15195 DWO file. */
15196 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15197 section = &cu->dwo_unit->dwo_file->sections.line;
15198 else if (cu->per_cu->is_dwz)
15199 {
15200 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15201
15202 section = &dwz->line;
15203 }
15204 else
15205 section = &dwarf2_per_objfile->line;
15206
15207 return section;
15208}
15209
debd256d 15210/* Read the statement program header starting at OFFSET in
3019eac3 15211 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15212 to a struct line_header, allocated using xmalloc.
debd256d
JB
15213
15214 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15215 the returned object point into the dwarf line section buffer,
15216 and must not be freed. */
ae2de4f8 15217
debd256d 15218static struct line_header *
3019eac3 15219dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15220{
15221 struct cleanup *back_to;
15222 struct line_header *lh;
d521ce57 15223 const gdb_byte *line_ptr;
c764a876 15224 unsigned int bytes_read, offset_size;
debd256d 15225 int i;
d521ce57 15226 const char *cur_dir, *cur_file;
3019eac3
DE
15227 struct dwarf2_section_info *section;
15228 bfd *abfd;
15229
36586728 15230 section = get_debug_line_section (cu);
3019eac3
DE
15231 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15232 if (section->buffer == NULL)
debd256d 15233 {
3019eac3
DE
15234 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15235 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15236 else
15237 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15238 return 0;
15239 }
15240
fceca515
DE
15241 /* We can't do this until we know the section is non-empty.
15242 Only then do we know we have such a section. */
15243 abfd = section->asection->owner;
15244
a738430d
MK
15245 /* Make sure that at least there's room for the total_length field.
15246 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15247 if (offset + 4 >= section->size)
debd256d 15248 {
4d3c2250 15249 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15250 return 0;
15251 }
15252
15253 lh = xmalloc (sizeof (*lh));
15254 memset (lh, 0, sizeof (*lh));
15255 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15256 (void *) lh);
15257
3019eac3 15258 line_ptr = section->buffer + offset;
debd256d 15259
a738430d 15260 /* Read in the header. */
6e70227d 15261 lh->total_length =
c764a876
DE
15262 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15263 &bytes_read, &offset_size);
debd256d 15264 line_ptr += bytes_read;
3019eac3 15265 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15266 {
4d3c2250 15267 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15268 return 0;
15269 }
15270 lh->statement_program_end = line_ptr + lh->total_length;
15271 lh->version = read_2_bytes (abfd, line_ptr);
15272 line_ptr += 2;
c764a876
DE
15273 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15274 line_ptr += offset_size;
debd256d
JB
15275 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15276 line_ptr += 1;
2dc7f7b3
TT
15277 if (lh->version >= 4)
15278 {
15279 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15280 line_ptr += 1;
15281 }
15282 else
15283 lh->maximum_ops_per_instruction = 1;
15284
15285 if (lh->maximum_ops_per_instruction == 0)
15286 {
15287 lh->maximum_ops_per_instruction = 1;
15288 complaint (&symfile_complaints,
3e43a32a
MS
15289 _("invalid maximum_ops_per_instruction "
15290 "in `.debug_line' section"));
2dc7f7b3
TT
15291 }
15292
debd256d
JB
15293 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15294 line_ptr += 1;
15295 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15296 line_ptr += 1;
15297 lh->line_range = read_1_byte (abfd, line_ptr);
15298 line_ptr += 1;
15299 lh->opcode_base = read_1_byte (abfd, line_ptr);
15300 line_ptr += 1;
15301 lh->standard_opcode_lengths
fe1b8b76 15302 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15303
15304 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15305 for (i = 1; i < lh->opcode_base; ++i)
15306 {
15307 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15308 line_ptr += 1;
15309 }
15310
a738430d 15311 /* Read directory table. */
9b1c24c8 15312 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15313 {
15314 line_ptr += bytes_read;
15315 add_include_dir (lh, cur_dir);
15316 }
15317 line_ptr += bytes_read;
15318
a738430d 15319 /* Read file name table. */
9b1c24c8 15320 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15321 {
15322 unsigned int dir_index, mod_time, length;
15323
15324 line_ptr += bytes_read;
15325 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15326 line_ptr += bytes_read;
15327 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15328 line_ptr += bytes_read;
15329 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15330 line_ptr += bytes_read;
15331
15332 add_file_name (lh, cur_file, dir_index, mod_time, length);
15333 }
15334 line_ptr += bytes_read;
6e70227d 15335 lh->statement_program_start = line_ptr;
debd256d 15336
3019eac3 15337 if (line_ptr > (section->buffer + section->size))
4d3c2250 15338 complaint (&symfile_complaints,
3e43a32a
MS
15339 _("line number info header doesn't "
15340 "fit in `.debug_line' section"));
debd256d
JB
15341
15342 discard_cleanups (back_to);
15343 return lh;
15344}
c906108c 15345
c6da4cef
DE
15346/* Subroutine of dwarf_decode_lines to simplify it.
15347 Return the file name of the psymtab for included file FILE_INDEX
15348 in line header LH of PST.
15349 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15350 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15351 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15352
15353 The function creates dangling cleanup registration. */
c6da4cef 15354
d521ce57 15355static const char *
c6da4cef
DE
15356psymtab_include_file_name (const struct line_header *lh, int file_index,
15357 const struct partial_symtab *pst,
15358 const char *comp_dir)
15359{
15360 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15361 const char *include_name = fe.name;
15362 const char *include_name_to_compare = include_name;
15363 const char *dir_name = NULL;
72b9f47f
TT
15364 const char *pst_filename;
15365 char *copied_name = NULL;
c6da4cef
DE
15366 int file_is_pst;
15367
15368 if (fe.dir_index)
15369 dir_name = lh->include_dirs[fe.dir_index - 1];
15370
15371 if (!IS_ABSOLUTE_PATH (include_name)
15372 && (dir_name != NULL || comp_dir != NULL))
15373 {
15374 /* Avoid creating a duplicate psymtab for PST.
15375 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15376 Before we do the comparison, however, we need to account
15377 for DIR_NAME and COMP_DIR.
15378 First prepend dir_name (if non-NULL). If we still don't
15379 have an absolute path prepend comp_dir (if non-NULL).
15380 However, the directory we record in the include-file's
15381 psymtab does not contain COMP_DIR (to match the
15382 corresponding symtab(s)).
15383
15384 Example:
15385
15386 bash$ cd /tmp
15387 bash$ gcc -g ./hello.c
15388 include_name = "hello.c"
15389 dir_name = "."
15390 DW_AT_comp_dir = comp_dir = "/tmp"
15391 DW_AT_name = "./hello.c" */
15392
15393 if (dir_name != NULL)
15394 {
d521ce57
TT
15395 char *tem = concat (dir_name, SLASH_STRING,
15396 include_name, (char *)NULL);
15397
15398 make_cleanup (xfree, tem);
15399 include_name = tem;
c6da4cef 15400 include_name_to_compare = include_name;
c6da4cef
DE
15401 }
15402 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15403 {
d521ce57
TT
15404 char *tem = concat (comp_dir, SLASH_STRING,
15405 include_name, (char *)NULL);
15406
15407 make_cleanup (xfree, tem);
15408 include_name_to_compare = tem;
c6da4cef
DE
15409 }
15410 }
15411
15412 pst_filename = pst->filename;
15413 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15414 {
72b9f47f
TT
15415 copied_name = concat (pst->dirname, SLASH_STRING,
15416 pst_filename, (char *)NULL);
15417 pst_filename = copied_name;
c6da4cef
DE
15418 }
15419
1e3fad37 15420 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15421
72b9f47f
TT
15422 if (copied_name != NULL)
15423 xfree (copied_name);
c6da4cef
DE
15424
15425 if (file_is_pst)
15426 return NULL;
15427 return include_name;
15428}
15429
c91513d8
PP
15430/* Ignore this record_line request. */
15431
15432static void
15433noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15434{
15435 return;
15436}
15437
f3f5162e
DE
15438/* Subroutine of dwarf_decode_lines to simplify it.
15439 Process the line number information in LH. */
debd256d 15440
c906108c 15441static void
f3f5162e
DE
15442dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15443 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15444{
d521ce57
TT
15445 const gdb_byte *line_ptr, *extended_end;
15446 const gdb_byte *line_end;
a8c50c1f 15447 unsigned int bytes_read, extended_len;
c906108c 15448 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15449 CORE_ADDR baseaddr;
15450 struct objfile *objfile = cu->objfile;
f3f5162e 15451 bfd *abfd = objfile->obfd;
fbf65064 15452 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15453 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15454 struct subfile *last_subfile = NULL;
c91513d8
PP
15455 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15456 = record_line;
e142c38c
DJ
15457
15458 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15459
debd256d
JB
15460 line_ptr = lh->statement_program_start;
15461 line_end = lh->statement_program_end;
c906108c
SS
15462
15463 /* Read the statement sequences until there's nothing left. */
15464 while (line_ptr < line_end)
15465 {
15466 /* state machine registers */
15467 CORE_ADDR address = 0;
15468 unsigned int file = 1;
15469 unsigned int line = 1;
15470 unsigned int column = 0;
debd256d 15471 int is_stmt = lh->default_is_stmt;
c906108c
SS
15472 int basic_block = 0;
15473 int end_sequence = 0;
fbf65064 15474 CORE_ADDR addr;
2dc7f7b3 15475 unsigned char op_index = 0;
c906108c 15476
aaa75496 15477 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15478 {
aaa75496 15479 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15480 /* lh->include_dirs and lh->file_names are 0-based, but the
15481 directory and file name numbers in the statement program
15482 are 1-based. */
15483 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15484 const char *dir = NULL;
a738430d 15485
debd256d
JB
15486 if (fe->dir_index)
15487 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15488
15489 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15490 }
15491
a738430d 15492 /* Decode the table. */
c5aa993b 15493 while (!end_sequence)
c906108c
SS
15494 {
15495 op_code = read_1_byte (abfd, line_ptr);
15496 line_ptr += 1;
59205f5a
JB
15497 if (line_ptr > line_end)
15498 {
15499 dwarf2_debug_line_missing_end_sequence_complaint ();
15500 break;
15501 }
9aa1fe7e 15502
debd256d 15503 if (op_code >= lh->opcode_base)
6e70227d 15504 {
a738430d 15505 /* Special operand. */
debd256d 15506 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15507 address += (((op_index + (adj_opcode / lh->line_range))
15508 / lh->maximum_ops_per_instruction)
15509 * lh->minimum_instruction_length);
15510 op_index = ((op_index + (adj_opcode / lh->line_range))
15511 % lh->maximum_ops_per_instruction);
debd256d 15512 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15513 if (lh->num_file_names < file || file == 0)
25e43795 15514 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15515 /* For now we ignore lines not starting on an
15516 instruction boundary. */
15517 else if (op_index == 0)
25e43795
DJ
15518 {
15519 lh->file_names[file - 1].included_p = 1;
ca5f395d 15520 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15521 {
15522 if (last_subfile != current_subfile)
15523 {
15524 addr = gdbarch_addr_bits_remove (gdbarch, address);
15525 if (last_subfile)
c91513d8 15526 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15527 last_subfile = current_subfile;
15528 }
25e43795 15529 /* Append row to matrix using current values. */
7019d805 15530 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15531 (*p_record_line) (current_subfile, line, addr);
366da635 15532 }
25e43795 15533 }
ca5f395d 15534 basic_block = 0;
9aa1fe7e
GK
15535 }
15536 else switch (op_code)
c906108c
SS
15537 {
15538 case DW_LNS_extended_op:
3e43a32a
MS
15539 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15540 &bytes_read);
473b7be6 15541 line_ptr += bytes_read;
a8c50c1f 15542 extended_end = line_ptr + extended_len;
c906108c
SS
15543 extended_op = read_1_byte (abfd, line_ptr);
15544 line_ptr += 1;
15545 switch (extended_op)
15546 {
15547 case DW_LNE_end_sequence:
c91513d8 15548 p_record_line = record_line;
c906108c 15549 end_sequence = 1;
c906108c
SS
15550 break;
15551 case DW_LNE_set_address:
e7c27a73 15552 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15553
15554 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15555 {
15556 /* This line table is for a function which has been
15557 GCd by the linker. Ignore it. PR gdb/12528 */
15558
15559 long line_offset
36586728 15560 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15561
15562 complaint (&symfile_complaints,
15563 _(".debug_line address at offset 0x%lx is 0 "
15564 "[in module %s]"),
bb5ed363 15565 line_offset, objfile->name);
c91513d8
PP
15566 p_record_line = noop_record_line;
15567 }
15568
2dc7f7b3 15569 op_index = 0;
107d2387
AC
15570 line_ptr += bytes_read;
15571 address += baseaddr;
c906108c
SS
15572 break;
15573 case DW_LNE_define_file:
debd256d 15574 {
d521ce57 15575 const char *cur_file;
debd256d 15576 unsigned int dir_index, mod_time, length;
6e70227d 15577
3e43a32a
MS
15578 cur_file = read_direct_string (abfd, line_ptr,
15579 &bytes_read);
debd256d
JB
15580 line_ptr += bytes_read;
15581 dir_index =
15582 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15583 line_ptr += bytes_read;
15584 mod_time =
15585 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15586 line_ptr += bytes_read;
15587 length =
15588 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15589 line_ptr += bytes_read;
15590 add_file_name (lh, cur_file, dir_index, mod_time, length);
15591 }
c906108c 15592 break;
d0c6ba3d
CC
15593 case DW_LNE_set_discriminator:
15594 /* The discriminator is not interesting to the debugger;
15595 just ignore it. */
15596 line_ptr = extended_end;
15597 break;
c906108c 15598 default:
4d3c2250 15599 complaint (&symfile_complaints,
e2e0b3e5 15600 _("mangled .debug_line section"));
debd256d 15601 return;
c906108c 15602 }
a8c50c1f
DJ
15603 /* Make sure that we parsed the extended op correctly. If e.g.
15604 we expected a different address size than the producer used,
15605 we may have read the wrong number of bytes. */
15606 if (line_ptr != extended_end)
15607 {
15608 complaint (&symfile_complaints,
15609 _("mangled .debug_line section"));
15610 return;
15611 }
c906108c
SS
15612 break;
15613 case DW_LNS_copy:
59205f5a 15614 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15615 dwarf2_debug_line_missing_file_complaint ();
15616 else
366da635 15617 {
25e43795 15618 lh->file_names[file - 1].included_p = 1;
ca5f395d 15619 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15620 {
15621 if (last_subfile != current_subfile)
15622 {
15623 addr = gdbarch_addr_bits_remove (gdbarch, address);
15624 if (last_subfile)
c91513d8 15625 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15626 last_subfile = current_subfile;
15627 }
7019d805 15628 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15629 (*p_record_line) (current_subfile, line, addr);
fbf65064 15630 }
366da635 15631 }
c906108c
SS
15632 basic_block = 0;
15633 break;
15634 case DW_LNS_advance_pc:
2dc7f7b3
TT
15635 {
15636 CORE_ADDR adjust
15637 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15638
15639 address += (((op_index + adjust)
15640 / lh->maximum_ops_per_instruction)
15641 * lh->minimum_instruction_length);
15642 op_index = ((op_index + adjust)
15643 % lh->maximum_ops_per_instruction);
15644 line_ptr += bytes_read;
15645 }
c906108c
SS
15646 break;
15647 case DW_LNS_advance_line:
15648 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15649 line_ptr += bytes_read;
15650 break;
15651 case DW_LNS_set_file:
debd256d 15652 {
a738430d
MK
15653 /* The arrays lh->include_dirs and lh->file_names are
15654 0-based, but the directory and file name numbers in
15655 the statement program are 1-based. */
debd256d 15656 struct file_entry *fe;
d521ce57 15657 const char *dir = NULL;
a738430d 15658
debd256d
JB
15659 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15660 line_ptr += bytes_read;
59205f5a 15661 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15662 dwarf2_debug_line_missing_file_complaint ();
15663 else
15664 {
15665 fe = &lh->file_names[file - 1];
15666 if (fe->dir_index)
15667 dir = lh->include_dirs[fe->dir_index - 1];
15668 if (!decode_for_pst_p)
15669 {
15670 last_subfile = current_subfile;
15671 dwarf2_start_subfile (fe->name, dir, comp_dir);
15672 }
15673 }
debd256d 15674 }
c906108c
SS
15675 break;
15676 case DW_LNS_set_column:
15677 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15678 line_ptr += bytes_read;
15679 break;
15680 case DW_LNS_negate_stmt:
15681 is_stmt = (!is_stmt);
15682 break;
15683 case DW_LNS_set_basic_block:
15684 basic_block = 1;
15685 break;
c2c6d25f
JM
15686 /* Add to the address register of the state machine the
15687 address increment value corresponding to special opcode
a738430d
MK
15688 255. I.e., this value is scaled by the minimum
15689 instruction length since special opcode 255 would have
b021a221 15690 scaled the increment. */
c906108c 15691 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15692 {
15693 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15694
15695 address += (((op_index + adjust)
15696 / lh->maximum_ops_per_instruction)
15697 * lh->minimum_instruction_length);
15698 op_index = ((op_index + adjust)
15699 % lh->maximum_ops_per_instruction);
15700 }
c906108c
SS
15701 break;
15702 case DW_LNS_fixed_advance_pc:
15703 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15704 op_index = 0;
c906108c
SS
15705 line_ptr += 2;
15706 break;
9aa1fe7e 15707 default:
a738430d
MK
15708 {
15709 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15710 int i;
a738430d 15711
debd256d 15712 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15713 {
15714 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15715 line_ptr += bytes_read;
15716 }
15717 }
c906108c
SS
15718 }
15719 }
59205f5a
JB
15720 if (lh->num_file_names < file || file == 0)
15721 dwarf2_debug_line_missing_file_complaint ();
15722 else
15723 {
15724 lh->file_names[file - 1].included_p = 1;
15725 if (!decode_for_pst_p)
fbf65064
UW
15726 {
15727 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15728 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15729 }
59205f5a 15730 }
c906108c 15731 }
f3f5162e
DE
15732}
15733
15734/* Decode the Line Number Program (LNP) for the given line_header
15735 structure and CU. The actual information extracted and the type
15736 of structures created from the LNP depends on the value of PST.
15737
15738 1. If PST is NULL, then this procedure uses the data from the program
15739 to create all necessary symbol tables, and their linetables.
15740
15741 2. If PST is not NULL, this procedure reads the program to determine
15742 the list of files included by the unit represented by PST, and
15743 builds all the associated partial symbol tables.
15744
15745 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15746 It is used for relative paths in the line table.
15747 NOTE: When processing partial symtabs (pst != NULL),
15748 comp_dir == pst->dirname.
15749
15750 NOTE: It is important that psymtabs have the same file name (via strcmp)
15751 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15752 symtab we don't use it in the name of the psymtabs we create.
15753 E.g. expand_line_sal requires this when finding psymtabs to expand.
15754 A good testcase for this is mb-inline.exp. */
15755
15756static void
15757dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15758 struct dwarf2_cu *cu, struct partial_symtab *pst,
15759 int want_line_info)
15760{
15761 struct objfile *objfile = cu->objfile;
15762 const int decode_for_pst_p = (pst != NULL);
15763 struct subfile *first_subfile = current_subfile;
15764
15765 if (want_line_info)
15766 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15767
15768 if (decode_for_pst_p)
15769 {
15770 int file_index;
15771
15772 /* Now that we're done scanning the Line Header Program, we can
15773 create the psymtab of each included file. */
15774 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15775 if (lh->file_names[file_index].included_p == 1)
15776 {
d521ce57 15777 const char *include_name =
c6da4cef
DE
15778 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15779 if (include_name != NULL)
aaa75496
JB
15780 dwarf2_create_include_psymtab (include_name, pst, objfile);
15781 }
15782 }
cb1df416
DJ
15783 else
15784 {
15785 /* Make sure a symtab is created for every file, even files
15786 which contain only variables (i.e. no code with associated
15787 line numbers). */
cb1df416 15788 int i;
cb1df416
DJ
15789
15790 for (i = 0; i < lh->num_file_names; i++)
15791 {
d521ce57 15792 const char *dir = NULL;
f3f5162e 15793 struct file_entry *fe;
9a619af0 15794
cb1df416
DJ
15795 fe = &lh->file_names[i];
15796 if (fe->dir_index)
15797 dir = lh->include_dirs[fe->dir_index - 1];
15798 dwarf2_start_subfile (fe->name, dir, comp_dir);
15799
15800 /* Skip the main file; we don't need it, and it must be
15801 allocated last, so that it will show up before the
15802 non-primary symtabs in the objfile's symtab list. */
15803 if (current_subfile == first_subfile)
15804 continue;
15805
15806 if (current_subfile->symtab == NULL)
15807 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15808 objfile);
cb1df416
DJ
15809 fe->symtab = current_subfile->symtab;
15810 }
15811 }
c906108c
SS
15812}
15813
15814/* Start a subfile for DWARF. FILENAME is the name of the file and
15815 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15816 or NULL if not known. COMP_DIR is the compilation directory for the
15817 linetable's compilation unit or NULL if not known.
c906108c
SS
15818 This routine tries to keep line numbers from identical absolute and
15819 relative file names in a common subfile.
15820
15821 Using the `list' example from the GDB testsuite, which resides in
15822 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15823 of /srcdir/list0.c yields the following debugging information for list0.c:
15824
c5aa993b
JM
15825 DW_AT_name: /srcdir/list0.c
15826 DW_AT_comp_dir: /compdir
357e46e7 15827 files.files[0].name: list0.h
c5aa993b 15828 files.files[0].dir: /srcdir
357e46e7 15829 files.files[1].name: list0.c
c5aa993b 15830 files.files[1].dir: /srcdir
c906108c
SS
15831
15832 The line number information for list0.c has to end up in a single
4f1520fb
FR
15833 subfile, so that `break /srcdir/list0.c:1' works as expected.
15834 start_subfile will ensure that this happens provided that we pass the
15835 concatenation of files.files[1].dir and files.files[1].name as the
15836 subfile's name. */
c906108c
SS
15837
15838static void
d521ce57 15839dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 15840 const char *comp_dir)
c906108c 15841{
d521ce57 15842 char *copy = NULL;
4f1520fb
FR
15843
15844 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15845 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15846 second argument to start_subfile. To be consistent, we do the
15847 same here. In order not to lose the line information directory,
15848 we concatenate it to the filename when it makes sense.
15849 Note that the Dwarf3 standard says (speaking of filenames in line
15850 information): ``The directory index is ignored for file names
15851 that represent full path names''. Thus ignoring dirname in the
15852 `else' branch below isn't an issue. */
c906108c 15853
d5166ae1 15854 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
15855 {
15856 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15857 filename = copy;
15858 }
c906108c 15859
d521ce57 15860 start_subfile (filename, comp_dir);
4f1520fb 15861
d521ce57
TT
15862 if (copy != NULL)
15863 xfree (copy);
c906108c
SS
15864}
15865
f4dc4d17
DE
15866/* Start a symtab for DWARF.
15867 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15868
15869static void
15870dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15871 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15872{
15873 start_symtab (name, comp_dir, low_pc);
15874 record_debugformat ("DWARF 2");
15875 record_producer (cu->producer);
15876
15877 /* We assume that we're processing GCC output. */
15878 processing_gcc_compilation = 2;
15879
4d4ec4e5 15880 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15881}
15882
4c2df51b
DJ
15883static void
15884var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15885 struct dwarf2_cu *cu)
4c2df51b 15886{
e7c27a73
DJ
15887 struct objfile *objfile = cu->objfile;
15888 struct comp_unit_head *cu_header = &cu->header;
15889
4c2df51b
DJ
15890 /* NOTE drow/2003-01-30: There used to be a comment and some special
15891 code here to turn a symbol with DW_AT_external and a
15892 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15893 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15894 with some versions of binutils) where shared libraries could have
15895 relocations against symbols in their debug information - the
15896 minimal symbol would have the right address, but the debug info
15897 would not. It's no longer necessary, because we will explicitly
15898 apply relocations when we read in the debug information now. */
15899
15900 /* A DW_AT_location attribute with no contents indicates that a
15901 variable has been optimized away. */
15902 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15903 {
f1e6e072 15904 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15905 return;
15906 }
15907
15908 /* Handle one degenerate form of location expression specially, to
15909 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15910 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15911 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15912
15913 if (attr_form_is_block (attr)
3019eac3
DE
15914 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15915 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15916 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15917 && (DW_BLOCK (attr)->size
15918 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15919 {
891d2f0b 15920 unsigned int dummy;
4c2df51b 15921
3019eac3
DE
15922 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15923 SYMBOL_VALUE_ADDRESS (sym) =
15924 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15925 else
15926 SYMBOL_VALUE_ADDRESS (sym) =
15927 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15928 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15929 fixup_symbol_section (sym, objfile);
15930 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15931 SYMBOL_SECTION (sym));
4c2df51b
DJ
15932 return;
15933 }
15934
15935 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15936 expression evaluator, and use LOC_COMPUTED only when necessary
15937 (i.e. when the value of a register or memory location is
15938 referenced, or a thread-local block, etc.). Then again, it might
15939 not be worthwhile. I'm assuming that it isn't unless performance
15940 or memory numbers show me otherwise. */
15941
f1e6e072 15942 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15943
f1e6e072 15944 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15945 cu->has_loclist = 1;
4c2df51b
DJ
15946}
15947
c906108c
SS
15948/* Given a pointer to a DWARF information entry, figure out if we need
15949 to make a symbol table entry for it, and if so, create a new entry
15950 and return a pointer to it.
15951 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15952 used the passed type.
15953 If SPACE is not NULL, use it to hold the new symbol. If it is
15954 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15955
15956static struct symbol *
34eaf542
TT
15957new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15958 struct symbol *space)
c906108c 15959{
e7c27a73 15960 struct objfile *objfile = cu->objfile;
c906108c 15961 struct symbol *sym = NULL;
15d034d0 15962 const char *name;
c906108c
SS
15963 struct attribute *attr = NULL;
15964 struct attribute *attr2 = NULL;
e142c38c 15965 CORE_ADDR baseaddr;
e37fd15a
SW
15966 struct pending **list_to_add = NULL;
15967
edb3359d 15968 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15969
15970 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15971
94af9270 15972 name = dwarf2_name (die, cu);
c906108c
SS
15973 if (name)
15974 {
94af9270 15975 const char *linkagename;
34eaf542 15976 int suppress_add = 0;
94af9270 15977
34eaf542
TT
15978 if (space)
15979 sym = space;
15980 else
e623cf5d 15981 sym = allocate_symbol (objfile);
c906108c 15982 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15983
15984 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 15985 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
15986 linkagename = dwarf2_physname (name, die, cu);
15987 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15988
f55ee35c
JK
15989 /* Fortran does not have mangling standard and the mangling does differ
15990 between gfortran, iFort etc. */
15991 if (cu->language == language_fortran
b250c185 15992 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15993 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15994 dwarf2_full_name (name, die, cu),
29df156d 15995 NULL);
f55ee35c 15996
c906108c 15997 /* Default assumptions.
c5aa993b 15998 Use the passed type or decode it from the die. */
176620f1 15999 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16000 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16001 if (type != NULL)
16002 SYMBOL_TYPE (sym) = type;
16003 else
e7c27a73 16004 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16005 attr = dwarf2_attr (die,
16006 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16007 cu);
c906108c
SS
16008 if (attr)
16009 {
16010 SYMBOL_LINE (sym) = DW_UNSND (attr);
16011 }
cb1df416 16012
edb3359d
DJ
16013 attr = dwarf2_attr (die,
16014 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16015 cu);
cb1df416
DJ
16016 if (attr)
16017 {
16018 int file_index = DW_UNSND (attr);
9a619af0 16019
cb1df416
DJ
16020 if (cu->line_header == NULL
16021 || file_index > cu->line_header->num_file_names)
16022 complaint (&symfile_complaints,
16023 _("file index out of range"));
1c3d648d 16024 else if (file_index > 0)
cb1df416
DJ
16025 {
16026 struct file_entry *fe;
9a619af0 16027
cb1df416
DJ
16028 fe = &cu->line_header->file_names[file_index - 1];
16029 SYMBOL_SYMTAB (sym) = fe->symtab;
16030 }
16031 }
16032
c906108c
SS
16033 switch (die->tag)
16034 {
16035 case DW_TAG_label:
e142c38c 16036 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16037 if (attr)
16038 {
16039 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16040 }
0f5238ed
TT
16041 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16042 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16043 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16044 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16045 break;
16046 case DW_TAG_subprogram:
16047 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16048 finish_block. */
f1e6e072 16049 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16050 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16051 if ((attr2 && (DW_UNSND (attr2) != 0))
16052 || cu->language == language_ada)
c906108c 16053 {
2cfa0c8d
JB
16054 /* Subprograms marked external are stored as a global symbol.
16055 Ada subprograms, whether marked external or not, are always
16056 stored as a global symbol, because we want to be able to
16057 access them globally. For instance, we want to be able
16058 to break on a nested subprogram without having to
16059 specify the context. */
e37fd15a 16060 list_to_add = &global_symbols;
c906108c
SS
16061 }
16062 else
16063 {
e37fd15a 16064 list_to_add = cu->list_in_scope;
c906108c
SS
16065 }
16066 break;
edb3359d
DJ
16067 case DW_TAG_inlined_subroutine:
16068 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16069 finish_block. */
f1e6e072 16070 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16071 SYMBOL_INLINED (sym) = 1;
481860b3 16072 list_to_add = cu->list_in_scope;
edb3359d 16073 break;
34eaf542
TT
16074 case DW_TAG_template_value_param:
16075 suppress_add = 1;
16076 /* Fall through. */
72929c62 16077 case DW_TAG_constant:
c906108c 16078 case DW_TAG_variable:
254e6b9e 16079 case DW_TAG_member:
0963b4bd
MS
16080 /* Compilation with minimal debug info may result in
16081 variables with missing type entries. Change the
16082 misleading `void' type to something sensible. */
c906108c 16083 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16084 SYMBOL_TYPE (sym)
46bf5051 16085 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16086
e142c38c 16087 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16088 /* In the case of DW_TAG_member, we should only be called for
16089 static const members. */
16090 if (die->tag == DW_TAG_member)
16091 {
3863f96c
DE
16092 /* dwarf2_add_field uses die_is_declaration,
16093 so we do the same. */
254e6b9e
DE
16094 gdb_assert (die_is_declaration (die, cu));
16095 gdb_assert (attr);
16096 }
c906108c
SS
16097 if (attr)
16098 {
e7c27a73 16099 dwarf2_const_value (attr, sym, cu);
e142c38c 16100 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16101 if (!suppress_add)
34eaf542
TT
16102 {
16103 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16104 list_to_add = &global_symbols;
34eaf542 16105 else
e37fd15a 16106 list_to_add = cu->list_in_scope;
34eaf542 16107 }
c906108c
SS
16108 break;
16109 }
e142c38c 16110 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16111 if (attr)
16112 {
e7c27a73 16113 var_decode_location (attr, sym, cu);
e142c38c 16114 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16115
16116 /* Fortran explicitly imports any global symbols to the local
16117 scope by DW_TAG_common_block. */
16118 if (cu->language == language_fortran && die->parent
16119 && die->parent->tag == DW_TAG_common_block)
16120 attr2 = NULL;
16121
caac4577
JG
16122 if (SYMBOL_CLASS (sym) == LOC_STATIC
16123 && SYMBOL_VALUE_ADDRESS (sym) == 0
16124 && !dwarf2_per_objfile->has_section_at_zero)
16125 {
16126 /* When a static variable is eliminated by the linker,
16127 the corresponding debug information is not stripped
16128 out, but the variable address is set to null;
16129 do not add such variables into symbol table. */
16130 }
16131 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16132 {
f55ee35c
JK
16133 /* Workaround gfortran PR debug/40040 - it uses
16134 DW_AT_location for variables in -fPIC libraries which may
16135 get overriden by other libraries/executable and get
16136 a different address. Resolve it by the minimal symbol
16137 which may come from inferior's executable using copy
16138 relocation. Make this workaround only for gfortran as for
16139 other compilers GDB cannot guess the minimal symbol
16140 Fortran mangling kind. */
16141 if (cu->language == language_fortran && die->parent
16142 && die->parent->tag == DW_TAG_module
16143 && cu->producer
16144 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16145 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16146
1c809c68
TT
16147 /* A variable with DW_AT_external is never static,
16148 but it may be block-scoped. */
16149 list_to_add = (cu->list_in_scope == &file_symbols
16150 ? &global_symbols : cu->list_in_scope);
1c809c68 16151 }
c906108c 16152 else
e37fd15a 16153 list_to_add = cu->list_in_scope;
c906108c
SS
16154 }
16155 else
16156 {
16157 /* We do not know the address of this symbol.
c5aa993b
JM
16158 If it is an external symbol and we have type information
16159 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16160 The address of the variable will then be determined from
16161 the minimal symbol table whenever the variable is
16162 referenced. */
e142c38c 16163 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16164
16165 /* Fortran explicitly imports any global symbols to the local
16166 scope by DW_TAG_common_block. */
16167 if (cu->language == language_fortran && die->parent
16168 && die->parent->tag == DW_TAG_common_block)
16169 {
16170 /* SYMBOL_CLASS doesn't matter here because
16171 read_common_block is going to reset it. */
16172 if (!suppress_add)
16173 list_to_add = cu->list_in_scope;
16174 }
16175 else if (attr2 && (DW_UNSND (attr2) != 0)
16176 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16177 {
0fe7935b
DJ
16178 /* A variable with DW_AT_external is never static, but it
16179 may be block-scoped. */
16180 list_to_add = (cu->list_in_scope == &file_symbols
16181 ? &global_symbols : cu->list_in_scope);
16182
f1e6e072 16183 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16184 }
442ddf59
JK
16185 else if (!die_is_declaration (die, cu))
16186 {
16187 /* Use the default LOC_OPTIMIZED_OUT class. */
16188 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16189 if (!suppress_add)
16190 list_to_add = cu->list_in_scope;
442ddf59 16191 }
c906108c
SS
16192 }
16193 break;
16194 case DW_TAG_formal_parameter:
edb3359d
DJ
16195 /* If we are inside a function, mark this as an argument. If
16196 not, we might be looking at an argument to an inlined function
16197 when we do not have enough information to show inlined frames;
16198 pretend it's a local variable in that case so that the user can
16199 still see it. */
16200 if (context_stack_depth > 0
16201 && context_stack[context_stack_depth - 1].name != NULL)
16202 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16203 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16204 if (attr)
16205 {
e7c27a73 16206 var_decode_location (attr, sym, cu);
c906108c 16207 }
e142c38c 16208 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16209 if (attr)
16210 {
e7c27a73 16211 dwarf2_const_value (attr, sym, cu);
c906108c 16212 }
f346a30d 16213
e37fd15a 16214 list_to_add = cu->list_in_scope;
c906108c
SS
16215 break;
16216 case DW_TAG_unspecified_parameters:
16217 /* From varargs functions; gdb doesn't seem to have any
16218 interest in this information, so just ignore it for now.
16219 (FIXME?) */
16220 break;
34eaf542
TT
16221 case DW_TAG_template_type_param:
16222 suppress_add = 1;
16223 /* Fall through. */
c906108c 16224 case DW_TAG_class_type:
680b30c7 16225 case DW_TAG_interface_type:
c906108c
SS
16226 case DW_TAG_structure_type:
16227 case DW_TAG_union_type:
72019c9c 16228 case DW_TAG_set_type:
c906108c 16229 case DW_TAG_enumeration_type:
f1e6e072 16230 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16231 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16232
63d06c5c 16233 {
987504bb 16234 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16235 really ever be static objects: otherwise, if you try
16236 to, say, break of a class's method and you're in a file
16237 which doesn't mention that class, it won't work unless
16238 the check for all static symbols in lookup_symbol_aux
16239 saves you. See the OtherFileClass tests in
16240 gdb.c++/namespace.exp. */
16241
e37fd15a 16242 if (!suppress_add)
34eaf542 16243 {
34eaf542
TT
16244 list_to_add = (cu->list_in_scope == &file_symbols
16245 && (cu->language == language_cplus
16246 || cu->language == language_java)
16247 ? &global_symbols : cu->list_in_scope);
63d06c5c 16248
64382290
TT
16249 /* The semantics of C++ state that "struct foo {
16250 ... }" also defines a typedef for "foo". A Java
16251 class declaration also defines a typedef for the
16252 class. */
16253 if (cu->language == language_cplus
16254 || cu->language == language_java
16255 || cu->language == language_ada)
16256 {
16257 /* The symbol's name is already allocated along
16258 with this objfile, so we don't need to
16259 duplicate it for the type. */
16260 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16261 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16262 }
63d06c5c
DC
16263 }
16264 }
c906108c
SS
16265 break;
16266 case DW_TAG_typedef:
f1e6e072 16267 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16268 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16269 list_to_add = cu->list_in_scope;
63d06c5c 16270 break;
c906108c 16271 case DW_TAG_base_type:
a02abb62 16272 case DW_TAG_subrange_type:
f1e6e072 16273 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16274 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16275 list_to_add = cu->list_in_scope;
c906108c
SS
16276 break;
16277 case DW_TAG_enumerator:
e142c38c 16278 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16279 if (attr)
16280 {
e7c27a73 16281 dwarf2_const_value (attr, sym, cu);
c906108c 16282 }
63d06c5c
DC
16283 {
16284 /* NOTE: carlton/2003-11-10: See comment above in the
16285 DW_TAG_class_type, etc. block. */
16286
e142c38c 16287 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16288 && (cu->language == language_cplus
16289 || cu->language == language_java)
e142c38c 16290 ? &global_symbols : cu->list_in_scope);
63d06c5c 16291 }
c906108c 16292 break;
5c4e30ca 16293 case DW_TAG_namespace:
f1e6e072 16294 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16295 list_to_add = &global_symbols;
5c4e30ca 16296 break;
4357ac6c 16297 case DW_TAG_common_block:
f1e6e072 16298 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16299 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16300 add_symbol_to_list (sym, cu->list_in_scope);
16301 break;
c906108c
SS
16302 default:
16303 /* Not a tag we recognize. Hopefully we aren't processing
16304 trash data, but since we must specifically ignore things
16305 we don't recognize, there is nothing else we should do at
0963b4bd 16306 this point. */
e2e0b3e5 16307 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16308 dwarf_tag_name (die->tag));
c906108c
SS
16309 break;
16310 }
df8a16a1 16311
e37fd15a
SW
16312 if (suppress_add)
16313 {
16314 sym->hash_next = objfile->template_symbols;
16315 objfile->template_symbols = sym;
16316 list_to_add = NULL;
16317 }
16318
16319 if (list_to_add != NULL)
16320 add_symbol_to_list (sym, list_to_add);
16321
df8a16a1
DJ
16322 /* For the benefit of old versions of GCC, check for anonymous
16323 namespaces based on the demangled name. */
4d4ec4e5 16324 if (!cu->processing_has_namespace_info
94af9270 16325 && cu->language == language_cplus)
a10964d1 16326 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16327 }
16328 return (sym);
16329}
16330
34eaf542
TT
16331/* A wrapper for new_symbol_full that always allocates a new symbol. */
16332
16333static struct symbol *
16334new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16335{
16336 return new_symbol_full (die, type, cu, NULL);
16337}
16338
98bfdba5
PA
16339/* Given an attr with a DW_FORM_dataN value in host byte order,
16340 zero-extend it as appropriate for the symbol's type. The DWARF
16341 standard (v4) is not entirely clear about the meaning of using
16342 DW_FORM_dataN for a constant with a signed type, where the type is
16343 wider than the data. The conclusion of a discussion on the DWARF
16344 list was that this is unspecified. We choose to always zero-extend
16345 because that is the interpretation long in use by GCC. */
c906108c 16346
98bfdba5
PA
16347static gdb_byte *
16348dwarf2_const_value_data (struct attribute *attr, struct type *type,
16349 const char *name, struct obstack *obstack,
12df843f 16350 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16351{
e7c27a73 16352 struct objfile *objfile = cu->objfile;
e17a4113
UW
16353 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16354 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16355 LONGEST l = DW_UNSND (attr);
16356
16357 if (bits < sizeof (*value) * 8)
16358 {
16359 l &= ((LONGEST) 1 << bits) - 1;
16360 *value = l;
16361 }
16362 else if (bits == sizeof (*value) * 8)
16363 *value = l;
16364 else
16365 {
16366 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16367 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16368 return bytes;
16369 }
16370
16371 return NULL;
16372}
16373
16374/* Read a constant value from an attribute. Either set *VALUE, or if
16375 the value does not fit in *VALUE, set *BYTES - either already
16376 allocated on the objfile obstack, or newly allocated on OBSTACK,
16377 or, set *BATON, if we translated the constant to a location
16378 expression. */
16379
16380static void
16381dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16382 const char *name, struct obstack *obstack,
16383 struct dwarf2_cu *cu,
d521ce57 16384 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16385 struct dwarf2_locexpr_baton **baton)
16386{
16387 struct objfile *objfile = cu->objfile;
16388 struct comp_unit_head *cu_header = &cu->header;
c906108c 16389 struct dwarf_block *blk;
98bfdba5
PA
16390 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16391 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16392
16393 *value = 0;
16394 *bytes = NULL;
16395 *baton = NULL;
c906108c
SS
16396
16397 switch (attr->form)
16398 {
16399 case DW_FORM_addr:
3019eac3 16400 case DW_FORM_GNU_addr_index:
ac56253d 16401 {
ac56253d
TT
16402 gdb_byte *data;
16403
98bfdba5
PA
16404 if (TYPE_LENGTH (type) != cu_header->addr_size)
16405 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16406 cu_header->addr_size,
98bfdba5 16407 TYPE_LENGTH (type));
ac56253d
TT
16408 /* Symbols of this form are reasonably rare, so we just
16409 piggyback on the existing location code rather than writing
16410 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16411 *baton = obstack_alloc (&objfile->objfile_obstack,
16412 sizeof (struct dwarf2_locexpr_baton));
16413 (*baton)->per_cu = cu->per_cu;
16414 gdb_assert ((*baton)->per_cu);
ac56253d 16415
98bfdba5
PA
16416 (*baton)->size = 2 + cu_header->addr_size;
16417 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16418 (*baton)->data = data;
ac56253d
TT
16419
16420 data[0] = DW_OP_addr;
16421 store_unsigned_integer (&data[1], cu_header->addr_size,
16422 byte_order, DW_ADDR (attr));
16423 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16424 }
c906108c 16425 break;
4ac36638 16426 case DW_FORM_string:
93b5768b 16427 case DW_FORM_strp:
3019eac3 16428 case DW_FORM_GNU_str_index:
36586728 16429 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16430 /* DW_STRING is already allocated on the objfile obstack, point
16431 directly to it. */
d521ce57 16432 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16433 break;
c906108c
SS
16434 case DW_FORM_block1:
16435 case DW_FORM_block2:
16436 case DW_FORM_block4:
16437 case DW_FORM_block:
2dc7f7b3 16438 case DW_FORM_exprloc:
c906108c 16439 blk = DW_BLOCK (attr);
98bfdba5
PA
16440 if (TYPE_LENGTH (type) != blk->size)
16441 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16442 TYPE_LENGTH (type));
16443 *bytes = blk->data;
c906108c 16444 break;
2df3850c
JM
16445
16446 /* The DW_AT_const_value attributes are supposed to carry the
16447 symbol's value "represented as it would be on the target
16448 architecture." By the time we get here, it's already been
16449 converted to host endianness, so we just need to sign- or
16450 zero-extend it as appropriate. */
16451 case DW_FORM_data1:
3e43a32a
MS
16452 *bytes = dwarf2_const_value_data (attr, type, name,
16453 obstack, cu, value, 8);
2df3850c 16454 break;
c906108c 16455 case DW_FORM_data2:
3e43a32a
MS
16456 *bytes = dwarf2_const_value_data (attr, type, name,
16457 obstack, cu, value, 16);
2df3850c 16458 break;
c906108c 16459 case DW_FORM_data4:
3e43a32a
MS
16460 *bytes = dwarf2_const_value_data (attr, type, name,
16461 obstack, cu, value, 32);
2df3850c 16462 break;
c906108c 16463 case DW_FORM_data8:
3e43a32a
MS
16464 *bytes = dwarf2_const_value_data (attr, type, name,
16465 obstack, cu, value, 64);
2df3850c
JM
16466 break;
16467
c906108c 16468 case DW_FORM_sdata:
98bfdba5 16469 *value = DW_SND (attr);
2df3850c
JM
16470 break;
16471
c906108c 16472 case DW_FORM_udata:
98bfdba5 16473 *value = DW_UNSND (attr);
c906108c 16474 break;
2df3850c 16475
c906108c 16476 default:
4d3c2250 16477 complaint (&symfile_complaints,
e2e0b3e5 16478 _("unsupported const value attribute form: '%s'"),
4d3c2250 16479 dwarf_form_name (attr->form));
98bfdba5 16480 *value = 0;
c906108c
SS
16481 break;
16482 }
16483}
16484
2df3850c 16485
98bfdba5
PA
16486/* Copy constant value from an attribute to a symbol. */
16487
2df3850c 16488static void
98bfdba5
PA
16489dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16490 struct dwarf2_cu *cu)
2df3850c 16491{
98bfdba5
PA
16492 struct objfile *objfile = cu->objfile;
16493 struct comp_unit_head *cu_header = &cu->header;
12df843f 16494 LONGEST value;
d521ce57 16495 const gdb_byte *bytes;
98bfdba5 16496 struct dwarf2_locexpr_baton *baton;
2df3850c 16497
98bfdba5
PA
16498 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16499 SYMBOL_PRINT_NAME (sym),
16500 &objfile->objfile_obstack, cu,
16501 &value, &bytes, &baton);
2df3850c 16502
98bfdba5
PA
16503 if (baton != NULL)
16504 {
98bfdba5 16505 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16506 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16507 }
16508 else if (bytes != NULL)
16509 {
16510 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16511 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16512 }
16513 else
16514 {
16515 SYMBOL_VALUE (sym) = value;
f1e6e072 16516 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16517 }
2df3850c
JM
16518}
16519
c906108c
SS
16520/* Return the type of the die in question using its DW_AT_type attribute. */
16521
16522static struct type *
e7c27a73 16523die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16524{
c906108c 16525 struct attribute *type_attr;
c906108c 16526
e142c38c 16527 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16528 if (!type_attr)
16529 {
16530 /* A missing DW_AT_type represents a void type. */
46bf5051 16531 return objfile_type (cu->objfile)->builtin_void;
c906108c 16532 }
348e048f 16533
673bfd45 16534 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16535}
16536
b4ba55a1
JB
16537/* True iff CU's producer generates GNAT Ada auxiliary information
16538 that allows to find parallel types through that information instead
16539 of having to do expensive parallel lookups by type name. */
16540
16541static int
16542need_gnat_info (struct dwarf2_cu *cu)
16543{
16544 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16545 of GNAT produces this auxiliary information, without any indication
16546 that it is produced. Part of enhancing the FSF version of GNAT
16547 to produce that information will be to put in place an indicator
16548 that we can use in order to determine whether the descriptive type
16549 info is available or not. One suggestion that has been made is
16550 to use a new attribute, attached to the CU die. For now, assume
16551 that the descriptive type info is not available. */
16552 return 0;
16553}
16554
b4ba55a1
JB
16555/* Return the auxiliary type of the die in question using its
16556 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16557 attribute is not present. */
16558
16559static struct type *
16560die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16561{
b4ba55a1 16562 struct attribute *type_attr;
b4ba55a1
JB
16563
16564 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16565 if (!type_attr)
16566 return NULL;
16567
673bfd45 16568 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16569}
16570
16571/* If DIE has a descriptive_type attribute, then set the TYPE's
16572 descriptive type accordingly. */
16573
16574static void
16575set_descriptive_type (struct type *type, struct die_info *die,
16576 struct dwarf2_cu *cu)
16577{
16578 struct type *descriptive_type = die_descriptive_type (die, cu);
16579
16580 if (descriptive_type)
16581 {
16582 ALLOCATE_GNAT_AUX_TYPE (type);
16583 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16584 }
16585}
16586
c906108c
SS
16587/* Return the containing type of the die in question using its
16588 DW_AT_containing_type attribute. */
16589
16590static struct type *
e7c27a73 16591die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16592{
c906108c 16593 struct attribute *type_attr;
c906108c 16594
e142c38c 16595 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16596 if (!type_attr)
16597 error (_("Dwarf Error: Problem turning containing type into gdb type "
16598 "[in module %s]"), cu->objfile->name);
16599
673bfd45 16600 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16601}
16602
ac9ec31b
DE
16603/* Return an error marker type to use for the ill formed type in DIE/CU. */
16604
16605static struct type *
16606build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
16607{
16608 struct objfile *objfile = dwarf2_per_objfile->objfile;
16609 char *message, *saved;
16610
16611 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16612 objfile->name,
16613 cu->header.offset.sect_off,
16614 die->offset.sect_off);
16615 saved = obstack_copy0 (&objfile->objfile_obstack,
16616 message, strlen (message));
16617 xfree (message);
16618
16619 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16620}
16621
673bfd45 16622/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
16623 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
16624 DW_AT_containing_type.
673bfd45
DE
16625 If there is no type substitute an error marker. */
16626
c906108c 16627static struct type *
673bfd45
DE
16628lookup_die_type (struct die_info *die, struct attribute *attr,
16629 struct dwarf2_cu *cu)
c906108c 16630{
bb5ed363 16631 struct objfile *objfile = cu->objfile;
f792889a
DJ
16632 struct type *this_type;
16633
ac9ec31b
DE
16634 gdb_assert (attr->name == DW_AT_type
16635 || attr->name == DW_AT_GNAT_descriptive_type
16636 || attr->name == DW_AT_containing_type);
16637
673bfd45
DE
16638 /* First see if we have it cached. */
16639
36586728
TT
16640 if (attr->form == DW_FORM_GNU_ref_alt)
16641 {
16642 struct dwarf2_per_cu_data *per_cu;
16643 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16644
16645 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16646 this_type = get_die_type_at_offset (offset, per_cu);
16647 }
16648 else if (is_ref_attr (attr))
673bfd45 16649 {
b64f50a1 16650 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16651
16652 this_type = get_die_type_at_offset (offset, cu->per_cu);
16653 }
55f1336d 16654 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 16655 {
ac9ec31b 16656 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 16657
ac9ec31b 16658 return get_signatured_type (die, signature, cu);
673bfd45
DE
16659 }
16660 else
16661 {
ac9ec31b
DE
16662 complaint (&symfile_complaints,
16663 _("Dwarf Error: Bad type attribute %s in DIE"
16664 " at 0x%x [in module %s]"),
16665 dwarf_attr_name (attr->name), die->offset.sect_off,
16666 objfile->name);
16667 return build_error_marker_type (cu, die);
673bfd45
DE
16668 }
16669
16670 /* If not cached we need to read it in. */
16671
16672 if (this_type == NULL)
16673 {
ac9ec31b 16674 struct die_info *type_die = NULL;
673bfd45
DE
16675 struct dwarf2_cu *type_cu = cu;
16676
ac9ec31b
DE
16677 if (is_ref_attr (attr))
16678 type_die = follow_die_ref (die, attr, &type_cu);
16679 if (type_die == NULL)
16680 return build_error_marker_type (cu, die);
16681 /* If we find the type now, it's probably because the type came
3019eac3
DE
16682 from an inter-CU reference and the type's CU got expanded before
16683 ours. */
ac9ec31b 16684 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
16685 }
16686
16687 /* If we still don't have a type use an error marker. */
16688
16689 if (this_type == NULL)
ac9ec31b 16690 return build_error_marker_type (cu, die);
673bfd45 16691
f792889a 16692 return this_type;
c906108c
SS
16693}
16694
673bfd45
DE
16695/* Return the type in DIE, CU.
16696 Returns NULL for invalid types.
16697
02142a6c 16698 This first does a lookup in die_type_hash,
673bfd45
DE
16699 and only reads the die in if necessary.
16700
16701 NOTE: This can be called when reading in partial or full symbols. */
16702
f792889a 16703static struct type *
e7c27a73 16704read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16705{
f792889a
DJ
16706 struct type *this_type;
16707
16708 this_type = get_die_type (die, cu);
16709 if (this_type)
16710 return this_type;
16711
673bfd45
DE
16712 return read_type_die_1 (die, cu);
16713}
16714
16715/* Read the type in DIE, CU.
16716 Returns NULL for invalid types. */
16717
16718static struct type *
16719read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16720{
16721 struct type *this_type = NULL;
16722
c906108c
SS
16723 switch (die->tag)
16724 {
16725 case DW_TAG_class_type:
680b30c7 16726 case DW_TAG_interface_type:
c906108c
SS
16727 case DW_TAG_structure_type:
16728 case DW_TAG_union_type:
f792889a 16729 this_type = read_structure_type (die, cu);
c906108c
SS
16730 break;
16731 case DW_TAG_enumeration_type:
f792889a 16732 this_type = read_enumeration_type (die, cu);
c906108c
SS
16733 break;
16734 case DW_TAG_subprogram:
16735 case DW_TAG_subroutine_type:
edb3359d 16736 case DW_TAG_inlined_subroutine:
f792889a 16737 this_type = read_subroutine_type (die, cu);
c906108c
SS
16738 break;
16739 case DW_TAG_array_type:
f792889a 16740 this_type = read_array_type (die, cu);
c906108c 16741 break;
72019c9c 16742 case DW_TAG_set_type:
f792889a 16743 this_type = read_set_type (die, cu);
72019c9c 16744 break;
c906108c 16745 case DW_TAG_pointer_type:
f792889a 16746 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16747 break;
16748 case DW_TAG_ptr_to_member_type:
f792889a 16749 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16750 break;
16751 case DW_TAG_reference_type:
f792889a 16752 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16753 break;
16754 case DW_TAG_const_type:
f792889a 16755 this_type = read_tag_const_type (die, cu);
c906108c
SS
16756 break;
16757 case DW_TAG_volatile_type:
f792889a 16758 this_type = read_tag_volatile_type (die, cu);
c906108c 16759 break;
06d66ee9
TT
16760 case DW_TAG_restrict_type:
16761 this_type = read_tag_restrict_type (die, cu);
16762 break;
c906108c 16763 case DW_TAG_string_type:
f792889a 16764 this_type = read_tag_string_type (die, cu);
c906108c
SS
16765 break;
16766 case DW_TAG_typedef:
f792889a 16767 this_type = read_typedef (die, cu);
c906108c 16768 break;
a02abb62 16769 case DW_TAG_subrange_type:
f792889a 16770 this_type = read_subrange_type (die, cu);
a02abb62 16771 break;
c906108c 16772 case DW_TAG_base_type:
f792889a 16773 this_type = read_base_type (die, cu);
c906108c 16774 break;
81a17f79 16775 case DW_TAG_unspecified_type:
f792889a 16776 this_type = read_unspecified_type (die, cu);
81a17f79 16777 break;
0114d602
DJ
16778 case DW_TAG_namespace:
16779 this_type = read_namespace_type (die, cu);
16780 break;
f55ee35c
JK
16781 case DW_TAG_module:
16782 this_type = read_module_type (die, cu);
16783 break;
c906108c 16784 default:
3e43a32a
MS
16785 complaint (&symfile_complaints,
16786 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16787 dwarf_tag_name (die->tag));
c906108c
SS
16788 break;
16789 }
63d06c5c 16790
f792889a 16791 return this_type;
63d06c5c
DC
16792}
16793
abc72ce4
DE
16794/* See if we can figure out if the class lives in a namespace. We do
16795 this by looking for a member function; its demangled name will
16796 contain namespace info, if there is any.
16797 Return the computed name or NULL.
16798 Space for the result is allocated on the objfile's obstack.
16799 This is the full-die version of guess_partial_die_structure_name.
16800 In this case we know DIE has no useful parent. */
16801
16802static char *
16803guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16804{
16805 struct die_info *spec_die;
16806 struct dwarf2_cu *spec_cu;
16807 struct die_info *child;
16808
16809 spec_cu = cu;
16810 spec_die = die_specification (die, &spec_cu);
16811 if (spec_die != NULL)
16812 {
16813 die = spec_die;
16814 cu = spec_cu;
16815 }
16816
16817 for (child = die->child;
16818 child != NULL;
16819 child = child->sibling)
16820 {
16821 if (child->tag == DW_TAG_subprogram)
16822 {
16823 struct attribute *attr;
16824
16825 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16826 if (attr == NULL)
16827 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16828 if (attr != NULL)
16829 {
16830 char *actual_name
16831 = language_class_name_from_physname (cu->language_defn,
16832 DW_STRING (attr));
16833 char *name = NULL;
16834
16835 if (actual_name != NULL)
16836 {
15d034d0 16837 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16838
16839 if (die_name != NULL
16840 && strcmp (die_name, actual_name) != 0)
16841 {
16842 /* Strip off the class name from the full name.
16843 We want the prefix. */
16844 int die_name_len = strlen (die_name);
16845 int actual_name_len = strlen (actual_name);
16846
16847 /* Test for '::' as a sanity check. */
16848 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16849 && actual_name[actual_name_len
16850 - die_name_len - 1] == ':')
abc72ce4 16851 name =
10f0c4bb
TT
16852 obstack_copy0 (&cu->objfile->objfile_obstack,
16853 actual_name,
16854 actual_name_len - die_name_len - 2);
abc72ce4
DE
16855 }
16856 }
16857 xfree (actual_name);
16858 return name;
16859 }
16860 }
16861 }
16862
16863 return NULL;
16864}
16865
96408a79
SA
16866/* GCC might emit a nameless typedef that has a linkage name. Determine the
16867 prefix part in such case. See
16868 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16869
16870static char *
16871anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16872{
16873 struct attribute *attr;
16874 char *base;
16875
16876 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16877 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16878 return NULL;
16879
16880 attr = dwarf2_attr (die, DW_AT_name, cu);
16881 if (attr != NULL && DW_STRING (attr) != NULL)
16882 return NULL;
16883
16884 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16885 if (attr == NULL)
16886 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16887 if (attr == NULL || DW_STRING (attr) == NULL)
16888 return NULL;
16889
16890 /* dwarf2_name had to be already called. */
16891 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16892
16893 /* Strip the base name, keep any leading namespaces/classes. */
16894 base = strrchr (DW_STRING (attr), ':');
16895 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16896 return "";
16897
10f0c4bb
TT
16898 return obstack_copy0 (&cu->objfile->objfile_obstack,
16899 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16900}
16901
fdde2d81 16902/* Return the name of the namespace/class that DIE is defined within,
0114d602 16903 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16904
0114d602
DJ
16905 For example, if we're within the method foo() in the following
16906 code:
16907
16908 namespace N {
16909 class C {
16910 void foo () {
16911 }
16912 };
16913 }
16914
16915 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16916
0d5cff50 16917static const char *
e142c38c 16918determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16919{
0114d602
DJ
16920 struct die_info *parent, *spec_die;
16921 struct dwarf2_cu *spec_cu;
16922 struct type *parent_type;
96408a79 16923 char *retval;
63d06c5c 16924
f55ee35c
JK
16925 if (cu->language != language_cplus && cu->language != language_java
16926 && cu->language != language_fortran)
0114d602
DJ
16927 return "";
16928
96408a79
SA
16929 retval = anonymous_struct_prefix (die, cu);
16930 if (retval)
16931 return retval;
16932
0114d602
DJ
16933 /* We have to be careful in the presence of DW_AT_specification.
16934 For example, with GCC 3.4, given the code
16935
16936 namespace N {
16937 void foo() {
16938 // Definition of N::foo.
16939 }
16940 }
16941
16942 then we'll have a tree of DIEs like this:
16943
16944 1: DW_TAG_compile_unit
16945 2: DW_TAG_namespace // N
16946 3: DW_TAG_subprogram // declaration of N::foo
16947 4: DW_TAG_subprogram // definition of N::foo
16948 DW_AT_specification // refers to die #3
16949
16950 Thus, when processing die #4, we have to pretend that we're in
16951 the context of its DW_AT_specification, namely the contex of die
16952 #3. */
16953 spec_cu = cu;
16954 spec_die = die_specification (die, &spec_cu);
16955 if (spec_die == NULL)
16956 parent = die->parent;
16957 else
63d06c5c 16958 {
0114d602
DJ
16959 parent = spec_die->parent;
16960 cu = spec_cu;
63d06c5c 16961 }
0114d602
DJ
16962
16963 if (parent == NULL)
16964 return "";
98bfdba5
PA
16965 else if (parent->building_fullname)
16966 {
16967 const char *name;
16968 const char *parent_name;
16969
16970 /* It has been seen on RealView 2.2 built binaries,
16971 DW_TAG_template_type_param types actually _defined_ as
16972 children of the parent class:
16973
16974 enum E {};
16975 template class <class Enum> Class{};
16976 Class<enum E> class_e;
16977
16978 1: DW_TAG_class_type (Class)
16979 2: DW_TAG_enumeration_type (E)
16980 3: DW_TAG_enumerator (enum1:0)
16981 3: DW_TAG_enumerator (enum2:1)
16982 ...
16983 2: DW_TAG_template_type_param
16984 DW_AT_type DW_FORM_ref_udata (E)
16985
16986 Besides being broken debug info, it can put GDB into an
16987 infinite loop. Consider:
16988
16989 When we're building the full name for Class<E>, we'll start
16990 at Class, and go look over its template type parameters,
16991 finding E. We'll then try to build the full name of E, and
16992 reach here. We're now trying to build the full name of E,
16993 and look over the parent DIE for containing scope. In the
16994 broken case, if we followed the parent DIE of E, we'd again
16995 find Class, and once again go look at its template type
16996 arguments, etc., etc. Simply don't consider such parent die
16997 as source-level parent of this die (it can't be, the language
16998 doesn't allow it), and break the loop here. */
16999 name = dwarf2_name (die, cu);
17000 parent_name = dwarf2_name (parent, cu);
17001 complaint (&symfile_complaints,
17002 _("template param type '%s' defined within parent '%s'"),
17003 name ? name : "<unknown>",
17004 parent_name ? parent_name : "<unknown>");
17005 return "";
17006 }
63d06c5c 17007 else
0114d602
DJ
17008 switch (parent->tag)
17009 {
63d06c5c 17010 case DW_TAG_namespace:
0114d602 17011 parent_type = read_type_die (parent, cu);
acebe513
UW
17012 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17013 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17014 Work around this problem here. */
17015 if (cu->language == language_cplus
17016 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17017 return "";
0114d602
DJ
17018 /* We give a name to even anonymous namespaces. */
17019 return TYPE_TAG_NAME (parent_type);
63d06c5c 17020 case DW_TAG_class_type:
680b30c7 17021 case DW_TAG_interface_type:
63d06c5c 17022 case DW_TAG_structure_type:
0114d602 17023 case DW_TAG_union_type:
f55ee35c 17024 case DW_TAG_module:
0114d602
DJ
17025 parent_type = read_type_die (parent, cu);
17026 if (TYPE_TAG_NAME (parent_type) != NULL)
17027 return TYPE_TAG_NAME (parent_type);
17028 else
17029 /* An anonymous structure is only allowed non-static data
17030 members; no typedefs, no member functions, et cetera.
17031 So it does not need a prefix. */
17032 return "";
abc72ce4 17033 case DW_TAG_compile_unit:
95554aad 17034 case DW_TAG_partial_unit:
abc72ce4
DE
17035 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17036 if (cu->language == language_cplus
8b70b953 17037 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17038 && die->child != NULL
17039 && (die->tag == DW_TAG_class_type
17040 || die->tag == DW_TAG_structure_type
17041 || die->tag == DW_TAG_union_type))
17042 {
17043 char *name = guess_full_die_structure_name (die, cu);
17044 if (name != NULL)
17045 return name;
17046 }
17047 return "";
63d06c5c 17048 default:
8176b9b8 17049 return determine_prefix (parent, cu);
63d06c5c 17050 }
63d06c5c
DC
17051}
17052
3e43a32a
MS
17053/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17054 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17055 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17056 an obconcat, otherwise allocate storage for the result. The CU argument is
17057 used to determine the language and hence, the appropriate separator. */
987504bb 17058
f55ee35c 17059#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17060
17061static char *
f55ee35c
JK
17062typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17063 int physname, struct dwarf2_cu *cu)
63d06c5c 17064{
f55ee35c 17065 const char *lead = "";
5c315b68 17066 const char *sep;
63d06c5c 17067
3e43a32a
MS
17068 if (suffix == NULL || suffix[0] == '\0'
17069 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17070 sep = "";
17071 else if (cu->language == language_java)
17072 sep = ".";
f55ee35c
JK
17073 else if (cu->language == language_fortran && physname)
17074 {
17075 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17076 DW_AT_MIPS_linkage_name is preferred and used instead. */
17077
17078 lead = "__";
17079 sep = "_MOD_";
17080 }
987504bb
JJ
17081 else
17082 sep = "::";
63d06c5c 17083
6dd47d34
DE
17084 if (prefix == NULL)
17085 prefix = "";
17086 if (suffix == NULL)
17087 suffix = "";
17088
987504bb
JJ
17089 if (obs == NULL)
17090 {
3e43a32a
MS
17091 char *retval
17092 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17093
f55ee35c
JK
17094 strcpy (retval, lead);
17095 strcat (retval, prefix);
6dd47d34
DE
17096 strcat (retval, sep);
17097 strcat (retval, suffix);
63d06c5c
DC
17098 return retval;
17099 }
987504bb
JJ
17100 else
17101 {
17102 /* We have an obstack. */
f55ee35c 17103 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17104 }
63d06c5c
DC
17105}
17106
c906108c
SS
17107/* Return sibling of die, NULL if no sibling. */
17108
f9aca02d 17109static struct die_info *
fba45db2 17110sibling_die (struct die_info *die)
c906108c 17111{
639d11d3 17112 return die->sibling;
c906108c
SS
17113}
17114
71c25dea
TT
17115/* Get name of a die, return NULL if not found. */
17116
15d034d0
TT
17117static const char *
17118dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17119 struct obstack *obstack)
17120{
17121 if (name && cu->language == language_cplus)
17122 {
17123 char *canon_name = cp_canonicalize_string (name);
17124
17125 if (canon_name != NULL)
17126 {
17127 if (strcmp (canon_name, name) != 0)
10f0c4bb 17128 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17129 xfree (canon_name);
17130 }
17131 }
17132
17133 return name;
c906108c
SS
17134}
17135
9219021c
DC
17136/* Get name of a die, return NULL if not found. */
17137
15d034d0 17138static const char *
e142c38c 17139dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17140{
17141 struct attribute *attr;
17142
e142c38c 17143 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17144 if ((!attr || !DW_STRING (attr))
17145 && die->tag != DW_TAG_class_type
17146 && die->tag != DW_TAG_interface_type
17147 && die->tag != DW_TAG_structure_type
17148 && die->tag != DW_TAG_union_type)
71c25dea
TT
17149 return NULL;
17150
17151 switch (die->tag)
17152 {
17153 case DW_TAG_compile_unit:
95554aad 17154 case DW_TAG_partial_unit:
71c25dea
TT
17155 /* Compilation units have a DW_AT_name that is a filename, not
17156 a source language identifier. */
17157 case DW_TAG_enumeration_type:
17158 case DW_TAG_enumerator:
17159 /* These tags always have simple identifiers already; no need
17160 to canonicalize them. */
17161 return DW_STRING (attr);
907af001 17162
418835cc
KS
17163 case DW_TAG_subprogram:
17164 /* Java constructors will all be named "<init>", so return
17165 the class name when we see this special case. */
17166 if (cu->language == language_java
17167 && DW_STRING (attr) != NULL
17168 && strcmp (DW_STRING (attr), "<init>") == 0)
17169 {
17170 struct dwarf2_cu *spec_cu = cu;
17171 struct die_info *spec_die;
17172
17173 /* GCJ will output '<init>' for Java constructor names.
17174 For this special case, return the name of the parent class. */
17175
17176 /* GCJ may output suprogram DIEs with AT_specification set.
17177 If so, use the name of the specified DIE. */
17178 spec_die = die_specification (die, &spec_cu);
17179 if (spec_die != NULL)
17180 return dwarf2_name (spec_die, spec_cu);
17181
17182 do
17183 {
17184 die = die->parent;
17185 if (die->tag == DW_TAG_class_type)
17186 return dwarf2_name (die, cu);
17187 }
95554aad
TT
17188 while (die->tag != DW_TAG_compile_unit
17189 && die->tag != DW_TAG_partial_unit);
418835cc 17190 }
907af001
UW
17191 break;
17192
17193 case DW_TAG_class_type:
17194 case DW_TAG_interface_type:
17195 case DW_TAG_structure_type:
17196 case DW_TAG_union_type:
17197 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17198 structures or unions. These were of the form "._%d" in GCC 4.1,
17199 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17200 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17201 if (attr && DW_STRING (attr)
17202 && (strncmp (DW_STRING (attr), "._", 2) == 0
17203 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17204 return NULL;
53832f31
TT
17205
17206 /* GCC might emit a nameless typedef that has a linkage name. See
17207 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17208 if (!attr || DW_STRING (attr) == NULL)
17209 {
df5c6c50 17210 char *demangled = NULL;
53832f31
TT
17211
17212 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17213 if (attr == NULL)
17214 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17215
17216 if (attr == NULL || DW_STRING (attr) == NULL)
17217 return NULL;
17218
df5c6c50
JK
17219 /* Avoid demangling DW_STRING (attr) the second time on a second
17220 call for the same DIE. */
17221 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17222 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17223
17224 if (demangled)
17225 {
96408a79
SA
17226 char *base;
17227
53832f31 17228 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17229 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17230 demangled, strlen (demangled));
53832f31
TT
17231 DW_STRING_IS_CANONICAL (attr) = 1;
17232 xfree (demangled);
96408a79
SA
17233
17234 /* Strip any leading namespaces/classes, keep only the base name.
17235 DW_AT_name for named DIEs does not contain the prefixes. */
17236 base = strrchr (DW_STRING (attr), ':');
17237 if (base && base > DW_STRING (attr) && base[-1] == ':')
17238 return &base[1];
17239 else
17240 return DW_STRING (attr);
53832f31
TT
17241 }
17242 }
907af001
UW
17243 break;
17244
71c25dea 17245 default:
907af001
UW
17246 break;
17247 }
17248
17249 if (!DW_STRING_IS_CANONICAL (attr))
17250 {
17251 DW_STRING (attr)
17252 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17253 &cu->objfile->objfile_obstack);
17254 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17255 }
907af001 17256 return DW_STRING (attr);
9219021c
DC
17257}
17258
17259/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17260 is none. *EXT_CU is the CU containing DIE on input, and the CU
17261 containing the return value on output. */
9219021c
DC
17262
17263static struct die_info *
f2f0e013 17264dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17265{
17266 struct attribute *attr;
9219021c 17267
f2f0e013 17268 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17269 if (attr == NULL)
17270 return NULL;
17271
f2f0e013 17272 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17273}
17274
c906108c
SS
17275/* Convert a DIE tag into its string name. */
17276
f39c6ffd 17277static const char *
aa1ee363 17278dwarf_tag_name (unsigned tag)
c906108c 17279{
f39c6ffd
TT
17280 const char *name = get_DW_TAG_name (tag);
17281
17282 if (name == NULL)
17283 return "DW_TAG_<unknown>";
17284
17285 return name;
c906108c
SS
17286}
17287
17288/* Convert a DWARF attribute code into its string name. */
17289
f39c6ffd 17290static const char *
aa1ee363 17291dwarf_attr_name (unsigned attr)
c906108c 17292{
f39c6ffd
TT
17293 const char *name;
17294
c764a876 17295#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17296 if (attr == DW_AT_MIPS_fde)
17297 return "DW_AT_MIPS_fde";
17298#else
17299 if (attr == DW_AT_HP_block_index)
17300 return "DW_AT_HP_block_index";
c764a876 17301#endif
f39c6ffd
TT
17302
17303 name = get_DW_AT_name (attr);
17304
17305 if (name == NULL)
17306 return "DW_AT_<unknown>";
17307
17308 return name;
c906108c
SS
17309}
17310
17311/* Convert a DWARF value form code into its string name. */
17312
f39c6ffd 17313static const char *
aa1ee363 17314dwarf_form_name (unsigned form)
c906108c 17315{
f39c6ffd
TT
17316 const char *name = get_DW_FORM_name (form);
17317
17318 if (name == NULL)
17319 return "DW_FORM_<unknown>";
17320
17321 return name;
c906108c
SS
17322}
17323
17324static char *
fba45db2 17325dwarf_bool_name (unsigned mybool)
c906108c
SS
17326{
17327 if (mybool)
17328 return "TRUE";
17329 else
17330 return "FALSE";
17331}
17332
17333/* Convert a DWARF type code into its string name. */
17334
f39c6ffd 17335static const char *
aa1ee363 17336dwarf_type_encoding_name (unsigned enc)
c906108c 17337{
f39c6ffd 17338 const char *name = get_DW_ATE_name (enc);
c906108c 17339
f39c6ffd
TT
17340 if (name == NULL)
17341 return "DW_ATE_<unknown>";
c906108c 17342
f39c6ffd 17343 return name;
c906108c 17344}
c906108c 17345
f9aca02d 17346static void
d97bc12b 17347dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17348{
17349 unsigned int i;
17350
d97bc12b
DE
17351 print_spaces (indent, f);
17352 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17353 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17354
17355 if (die->parent != NULL)
17356 {
17357 print_spaces (indent, f);
17358 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17359 die->parent->offset.sect_off);
d97bc12b
DE
17360 }
17361
17362 print_spaces (indent, f);
17363 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17364 dwarf_bool_name (die->child != NULL));
c906108c 17365
d97bc12b
DE
17366 print_spaces (indent, f);
17367 fprintf_unfiltered (f, " attributes:\n");
17368
c906108c
SS
17369 for (i = 0; i < die->num_attrs; ++i)
17370 {
d97bc12b
DE
17371 print_spaces (indent, f);
17372 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17373 dwarf_attr_name (die->attrs[i].name),
17374 dwarf_form_name (die->attrs[i].form));
d97bc12b 17375
c906108c
SS
17376 switch (die->attrs[i].form)
17377 {
c906108c 17378 case DW_FORM_addr:
3019eac3 17379 case DW_FORM_GNU_addr_index:
d97bc12b 17380 fprintf_unfiltered (f, "address: ");
5af949e3 17381 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17382 break;
17383 case DW_FORM_block2:
17384 case DW_FORM_block4:
17385 case DW_FORM_block:
17386 case DW_FORM_block1:
56eb65bd
SP
17387 fprintf_unfiltered (f, "block: size %s",
17388 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17389 break;
2dc7f7b3 17390 case DW_FORM_exprloc:
56eb65bd
SP
17391 fprintf_unfiltered (f, "expression: size %s",
17392 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17393 break;
4568ecf9
DE
17394 case DW_FORM_ref_addr:
17395 fprintf_unfiltered (f, "ref address: ");
17396 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17397 break;
36586728
TT
17398 case DW_FORM_GNU_ref_alt:
17399 fprintf_unfiltered (f, "alt ref address: ");
17400 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17401 break;
10b3939b
DJ
17402 case DW_FORM_ref1:
17403 case DW_FORM_ref2:
17404 case DW_FORM_ref4:
4568ecf9
DE
17405 case DW_FORM_ref8:
17406 case DW_FORM_ref_udata:
d97bc12b 17407 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17408 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17409 break;
c906108c
SS
17410 case DW_FORM_data1:
17411 case DW_FORM_data2:
17412 case DW_FORM_data4:
ce5d95e1 17413 case DW_FORM_data8:
c906108c
SS
17414 case DW_FORM_udata:
17415 case DW_FORM_sdata:
43bbcdc2
PH
17416 fprintf_unfiltered (f, "constant: %s",
17417 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17418 break;
2dc7f7b3
TT
17419 case DW_FORM_sec_offset:
17420 fprintf_unfiltered (f, "section offset: %s",
17421 pulongest (DW_UNSND (&die->attrs[i])));
17422 break;
55f1336d 17423 case DW_FORM_ref_sig8:
ac9ec31b
DE
17424 fprintf_unfiltered (f, "signature: %s",
17425 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17426 break;
c906108c 17427 case DW_FORM_string:
4bdf3d34 17428 case DW_FORM_strp:
3019eac3 17429 case DW_FORM_GNU_str_index:
36586728 17430 case DW_FORM_GNU_strp_alt:
8285870a 17431 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17432 DW_STRING (&die->attrs[i])
8285870a
JK
17433 ? DW_STRING (&die->attrs[i]) : "",
17434 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17435 break;
17436 case DW_FORM_flag:
17437 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17438 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17439 else
d97bc12b 17440 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17441 break;
2dc7f7b3
TT
17442 case DW_FORM_flag_present:
17443 fprintf_unfiltered (f, "flag: TRUE");
17444 break;
a8329558 17445 case DW_FORM_indirect:
0963b4bd
MS
17446 /* The reader will have reduced the indirect form to
17447 the "base form" so this form should not occur. */
3e43a32a
MS
17448 fprintf_unfiltered (f,
17449 "unexpected attribute form: DW_FORM_indirect");
a8329558 17450 break;
c906108c 17451 default:
d97bc12b 17452 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17453 die->attrs[i].form);
d97bc12b 17454 break;
c906108c 17455 }
d97bc12b 17456 fprintf_unfiltered (f, "\n");
c906108c
SS
17457 }
17458}
17459
f9aca02d 17460static void
d97bc12b 17461dump_die_for_error (struct die_info *die)
c906108c 17462{
d97bc12b
DE
17463 dump_die_shallow (gdb_stderr, 0, die);
17464}
17465
17466static void
17467dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17468{
17469 int indent = level * 4;
17470
17471 gdb_assert (die != NULL);
17472
17473 if (level >= max_level)
17474 return;
17475
17476 dump_die_shallow (f, indent, die);
17477
17478 if (die->child != NULL)
c906108c 17479 {
d97bc12b
DE
17480 print_spaces (indent, f);
17481 fprintf_unfiltered (f, " Children:");
17482 if (level + 1 < max_level)
17483 {
17484 fprintf_unfiltered (f, "\n");
17485 dump_die_1 (f, level + 1, max_level, die->child);
17486 }
17487 else
17488 {
3e43a32a
MS
17489 fprintf_unfiltered (f,
17490 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17491 }
17492 }
17493
17494 if (die->sibling != NULL && level > 0)
17495 {
17496 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17497 }
17498}
17499
d97bc12b
DE
17500/* This is called from the pdie macro in gdbinit.in.
17501 It's not static so gcc will keep a copy callable from gdb. */
17502
17503void
17504dump_die (struct die_info *die, int max_level)
17505{
17506 dump_die_1 (gdb_stdlog, 0, max_level, die);
17507}
17508
f9aca02d 17509static void
51545339 17510store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17511{
51545339 17512 void **slot;
c906108c 17513
b64f50a1
JK
17514 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17515 INSERT);
51545339
DJ
17516
17517 *slot = die;
c906108c
SS
17518}
17519
b64f50a1
JK
17520/* DW_ADDR is always stored already as sect_offset; despite for the forms
17521 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17522
93311388
DE
17523static int
17524is_ref_attr (struct attribute *attr)
c906108c 17525{
c906108c
SS
17526 switch (attr->form)
17527 {
17528 case DW_FORM_ref_addr:
c906108c
SS
17529 case DW_FORM_ref1:
17530 case DW_FORM_ref2:
17531 case DW_FORM_ref4:
613e1657 17532 case DW_FORM_ref8:
c906108c 17533 case DW_FORM_ref_udata:
36586728 17534 case DW_FORM_GNU_ref_alt:
93311388 17535 return 1;
c906108c 17536 default:
93311388 17537 return 0;
c906108c 17538 }
93311388
DE
17539}
17540
b64f50a1
JK
17541/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17542 required kind. */
17543
17544static sect_offset
93311388
DE
17545dwarf2_get_ref_die_offset (struct attribute *attr)
17546{
4568ecf9 17547 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17548
93311388 17549 if (is_ref_attr (attr))
b64f50a1 17550 return retval;
93311388 17551
b64f50a1 17552 retval.sect_off = 0;
93311388
DE
17553 complaint (&symfile_complaints,
17554 _("unsupported die ref attribute form: '%s'"),
17555 dwarf_form_name (attr->form));
b64f50a1 17556 return retval;
c906108c
SS
17557}
17558
43bbcdc2
PH
17559/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17560 * the value held by the attribute is not constant. */
a02abb62 17561
43bbcdc2 17562static LONGEST
a02abb62
JB
17563dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17564{
17565 if (attr->form == DW_FORM_sdata)
17566 return DW_SND (attr);
17567 else if (attr->form == DW_FORM_udata
17568 || attr->form == DW_FORM_data1
17569 || attr->form == DW_FORM_data2
17570 || attr->form == DW_FORM_data4
17571 || attr->form == DW_FORM_data8)
17572 return DW_UNSND (attr);
17573 else
17574 {
3e43a32a
MS
17575 complaint (&symfile_complaints,
17576 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17577 dwarf_form_name (attr->form));
17578 return default_value;
17579 }
17580}
17581
348e048f
DE
17582/* Follow reference or signature attribute ATTR of SRC_DIE.
17583 On entry *REF_CU is the CU of SRC_DIE.
17584 On exit *REF_CU is the CU of the result. */
17585
17586static struct die_info *
17587follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17588 struct dwarf2_cu **ref_cu)
17589{
17590 struct die_info *die;
17591
17592 if (is_ref_attr (attr))
17593 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17594 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17595 die = follow_die_sig (src_die, attr, ref_cu);
17596 else
17597 {
17598 dump_die_for_error (src_die);
17599 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17600 (*ref_cu)->objfile->name);
17601 }
17602
17603 return die;
03dd20cc
DJ
17604}
17605
5c631832 17606/* Follow reference OFFSET.
673bfd45
DE
17607 On entry *REF_CU is the CU of the source die referencing OFFSET.
17608 On exit *REF_CU is the CU of the result.
17609 Returns NULL if OFFSET is invalid. */
f504f079 17610
f9aca02d 17611static struct die_info *
36586728
TT
17612follow_die_offset (sect_offset offset, int offset_in_dwz,
17613 struct dwarf2_cu **ref_cu)
c906108c 17614{
10b3939b 17615 struct die_info temp_die;
f2f0e013 17616 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17617
348e048f
DE
17618 gdb_assert (cu->per_cu != NULL);
17619
98bfdba5
PA
17620 target_cu = cu;
17621
3019eac3 17622 if (cu->per_cu->is_debug_types)
348e048f
DE
17623 {
17624 /* .debug_types CUs cannot reference anything outside their CU.
17625 If they need to, they have to reference a signatured type via
55f1336d 17626 DW_FORM_ref_sig8. */
348e048f 17627 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17628 return NULL;
348e048f 17629 }
36586728
TT
17630 else if (offset_in_dwz != cu->per_cu->is_dwz
17631 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17632 {
17633 struct dwarf2_per_cu_data *per_cu;
9a619af0 17634
36586728
TT
17635 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17636 cu->objfile);
03dd20cc
DJ
17637
17638 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17639 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17640 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17641
10b3939b
DJ
17642 target_cu = per_cu->cu;
17643 }
98bfdba5
PA
17644 else if (cu->dies == NULL)
17645 {
17646 /* We're loading full DIEs during partial symbol reading. */
17647 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17648 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17649 }
c906108c 17650
f2f0e013 17651 *ref_cu = target_cu;
51545339 17652 temp_die.offset = offset;
b64f50a1 17653 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17654}
10b3939b 17655
5c631832
JK
17656/* Follow reference attribute ATTR of SRC_DIE.
17657 On entry *REF_CU is the CU of SRC_DIE.
17658 On exit *REF_CU is the CU of the result. */
17659
17660static struct die_info *
17661follow_die_ref (struct die_info *src_die, struct attribute *attr,
17662 struct dwarf2_cu **ref_cu)
17663{
b64f50a1 17664 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17665 struct dwarf2_cu *cu = *ref_cu;
17666 struct die_info *die;
17667
36586728
TT
17668 die = follow_die_offset (offset,
17669 (attr->form == DW_FORM_GNU_ref_alt
17670 || cu->per_cu->is_dwz),
17671 ref_cu);
5c631832
JK
17672 if (!die)
17673 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17674 "at 0x%x [in module %s]"),
b64f50a1 17675 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17676
5c631832
JK
17677 return die;
17678}
17679
d83e736b
JK
17680/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17681 Returned value is intended for DW_OP_call*. Returned
17682 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17683
17684struct dwarf2_locexpr_baton
8b9737bf
TT
17685dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17686 struct dwarf2_per_cu_data *per_cu,
17687 CORE_ADDR (*get_frame_pc) (void *baton),
17688 void *baton)
5c631832 17689{
918dd910 17690 struct dwarf2_cu *cu;
5c631832
JK
17691 struct die_info *die;
17692 struct attribute *attr;
17693 struct dwarf2_locexpr_baton retval;
17694
8cf6f0b1
TT
17695 dw2_setup (per_cu->objfile);
17696
918dd910
JK
17697 if (per_cu->cu == NULL)
17698 load_cu (per_cu);
17699 cu = per_cu->cu;
17700
36586728 17701 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17702 if (!die)
17703 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17704 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17705
17706 attr = dwarf2_attr (die, DW_AT_location, cu);
17707 if (!attr)
17708 {
e103e986
JK
17709 /* DWARF: "If there is no such attribute, then there is no effect.".
17710 DATA is ignored if SIZE is 0. */
5c631832 17711
e103e986 17712 retval.data = NULL;
5c631832
JK
17713 retval.size = 0;
17714 }
8cf6f0b1
TT
17715 else if (attr_form_is_section_offset (attr))
17716 {
17717 struct dwarf2_loclist_baton loclist_baton;
17718 CORE_ADDR pc = (*get_frame_pc) (baton);
17719 size_t size;
17720
17721 fill_in_loclist_baton (cu, &loclist_baton, attr);
17722
17723 retval.data = dwarf2_find_location_expression (&loclist_baton,
17724 &size, pc);
17725 retval.size = size;
17726 }
5c631832
JK
17727 else
17728 {
17729 if (!attr_form_is_block (attr))
17730 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17731 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17732 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17733
17734 retval.data = DW_BLOCK (attr)->data;
17735 retval.size = DW_BLOCK (attr)->size;
17736 }
17737 retval.per_cu = cu->per_cu;
918dd910 17738
918dd910
JK
17739 age_cached_comp_units ();
17740
5c631832 17741 return retval;
348e048f
DE
17742}
17743
8b9737bf
TT
17744/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17745 offset. */
17746
17747struct dwarf2_locexpr_baton
17748dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17749 struct dwarf2_per_cu_data *per_cu,
17750 CORE_ADDR (*get_frame_pc) (void *baton),
17751 void *baton)
17752{
17753 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17754
17755 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17756}
17757
8a9b8146
TT
17758/* Return the type of the DIE at DIE_OFFSET in the CU named by
17759 PER_CU. */
17760
17761struct type *
b64f50a1 17762dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17763 struct dwarf2_per_cu_data *per_cu)
17764{
b64f50a1
JK
17765 sect_offset die_offset_sect;
17766
8a9b8146 17767 dw2_setup (per_cu->objfile);
b64f50a1
JK
17768
17769 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17770 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17771}
17772
ac9ec31b 17773/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 17774 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
17775 On exit *REF_CU is the CU of the result.
17776 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
17777
17778static struct die_info *
ac9ec31b
DE
17779follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
17780 struct dwarf2_cu **ref_cu)
348e048f
DE
17781{
17782 struct objfile *objfile = (*ref_cu)->objfile;
17783 struct die_info temp_die;
348e048f
DE
17784 struct dwarf2_cu *sig_cu;
17785 struct die_info *die;
17786
ac9ec31b
DE
17787 /* While it might be nice to assert sig_type->type == NULL here,
17788 we can get here for DW_AT_imported_declaration where we need
17789 the DIE not the type. */
348e048f
DE
17790
17791 /* If necessary, add it to the queue and load its DIEs. */
17792
95554aad 17793 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17794 read_signatured_type (sig_type);
348e048f
DE
17795
17796 gdb_assert (sig_type->per_cu.cu != NULL);
17797
17798 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17799 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17800 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17801 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17802 temp_die.offset.sect_off);
348e048f
DE
17803 if (die)
17804 {
796a7ff8
DE
17805 /* For .gdb_index version 7 keep track of included TUs.
17806 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17807 if (dwarf2_per_objfile->index_table != NULL
17808 && dwarf2_per_objfile->index_table->version <= 7)
17809 {
17810 VEC_safe_push (dwarf2_per_cu_ptr,
17811 (*ref_cu)->per_cu->imported_symtabs,
17812 sig_cu->per_cu);
17813 }
17814
348e048f
DE
17815 *ref_cu = sig_cu;
17816 return die;
17817 }
17818
ac9ec31b
DE
17819 return NULL;
17820}
17821
17822/* Follow signatured type referenced by ATTR in SRC_DIE.
17823 On entry *REF_CU is the CU of SRC_DIE.
17824 On exit *REF_CU is the CU of the result.
17825 The result is the DIE of the type.
17826 If the referenced type cannot be found an error is thrown. */
17827
17828static struct die_info *
17829follow_die_sig (struct die_info *src_die, struct attribute *attr,
17830 struct dwarf2_cu **ref_cu)
17831{
17832 ULONGEST signature = DW_SIGNATURE (attr);
17833 struct signatured_type *sig_type;
17834 struct die_info *die;
17835
17836 gdb_assert (attr->form == DW_FORM_ref_sig8);
17837
17838 sig_type = lookup_signatured_type (signature);
17839 /* sig_type will be NULL if the signatured type is missing from
17840 the debug info. */
17841 if (sig_type == NULL)
17842 {
17843 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
17844 " from DIE at 0x%x [in module %s]"),
17845 hex_string (signature), src_die->offset.sect_off,
17846 (*ref_cu)->objfile->name);
17847 }
17848
17849 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
17850 if (die == NULL)
17851 {
17852 dump_die_for_error (src_die);
17853 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
17854 " from DIE at 0x%x [in module %s]"),
17855 hex_string (signature), src_die->offset.sect_off,
17856 (*ref_cu)->objfile->name);
17857 }
17858
17859 return die;
17860}
17861
17862/* Get the type specified by SIGNATURE referenced in DIE/CU,
17863 reading in and processing the type unit if necessary. */
17864
17865static struct type *
17866get_signatured_type (struct die_info *die, ULONGEST signature,
17867 struct dwarf2_cu *cu)
17868{
17869 struct signatured_type *sig_type;
17870 struct dwarf2_cu *type_cu;
17871 struct die_info *type_die;
17872 struct type *type;
17873
17874 sig_type = lookup_signatured_type (signature);
17875 /* sig_type will be NULL if the signatured type is missing from
17876 the debug info. */
17877 if (sig_type == NULL)
17878 {
17879 complaint (&symfile_complaints,
17880 _("Dwarf Error: Cannot find signatured DIE %s referenced"
17881 " from DIE at 0x%x [in module %s]"),
17882 hex_string (signature), die->offset.sect_off,
17883 dwarf2_per_objfile->objfile->name);
17884 return build_error_marker_type (cu, die);
17885 }
17886
17887 /* If we already know the type we're done. */
17888 if (sig_type->type != NULL)
17889 return sig_type->type;
17890
17891 type_cu = cu;
17892 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
17893 if (type_die != NULL)
17894 {
17895 /* N.B. We need to call get_die_type to ensure only one type for this DIE
17896 is created. This is important, for example, because for c++ classes
17897 we need TYPE_NAME set which is only done by new_symbol. Blech. */
17898 type = read_type_die (type_die, type_cu);
17899 if (type == NULL)
17900 {
17901 complaint (&symfile_complaints,
17902 _("Dwarf Error: Cannot build signatured type %s"
17903 " referenced from DIE at 0x%x [in module %s]"),
17904 hex_string (signature), die->offset.sect_off,
17905 dwarf2_per_objfile->objfile->name);
17906 type = build_error_marker_type (cu, die);
17907 }
17908 }
17909 else
17910 {
17911 complaint (&symfile_complaints,
17912 _("Dwarf Error: Problem reading signatured DIE %s referenced"
17913 " from DIE at 0x%x [in module %s]"),
17914 hex_string (signature), die->offset.sect_off,
17915 dwarf2_per_objfile->objfile->name);
17916 type = build_error_marker_type (cu, die);
17917 }
17918 sig_type->type = type;
17919
17920 return type;
17921}
17922
17923/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
17924 reading in and processing the type unit if necessary. */
17925
17926static struct type *
17927get_DW_AT_signature_type (struct die_info *die, struct attribute *attr,
17928 struct dwarf2_cu *cu)
17929{
17930 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
17931 if (is_ref_attr (attr))
17932 {
17933 struct dwarf2_cu *type_cu = cu;
17934 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
17935
17936 return read_type_die (type_die, type_cu);
17937 }
17938 else if (attr->form == DW_FORM_ref_sig8)
17939 {
17940 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
17941 }
17942 else
17943 {
17944 complaint (&symfile_complaints,
17945 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
17946 " at 0x%x [in module %s]"),
17947 dwarf_form_name (attr->form), die->offset.sect_off,
17948 dwarf2_per_objfile->objfile->name);
17949 return build_error_marker_type (cu, die);
17950 }
348e048f
DE
17951}
17952
e5fe5e75 17953/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17954
17955static void
e5fe5e75 17956load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17957{
52dc124a 17958 struct signatured_type *sig_type;
348e048f 17959
f4dc4d17
DE
17960 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17961 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17962
6721b2ec
DE
17963 /* We have the per_cu, but we need the signatured_type.
17964 Fortunately this is an easy translation. */
17965 gdb_assert (per_cu->is_debug_types);
17966 sig_type = (struct signatured_type *) per_cu;
348e048f 17967
6721b2ec 17968 gdb_assert (per_cu->cu == NULL);
348e048f 17969
52dc124a 17970 read_signatured_type (sig_type);
348e048f 17971
6721b2ec 17972 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17973}
17974
dee91e82
DE
17975/* die_reader_func for read_signatured_type.
17976 This is identical to load_full_comp_unit_reader,
17977 but is kept separate for now. */
348e048f
DE
17978
17979static void
dee91e82 17980read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 17981 const gdb_byte *info_ptr,
dee91e82
DE
17982 struct die_info *comp_unit_die,
17983 int has_children,
17984 void *data)
348e048f 17985{
dee91e82 17986 struct dwarf2_cu *cu = reader->cu;
348e048f 17987
dee91e82
DE
17988 gdb_assert (cu->die_hash == NULL);
17989 cu->die_hash =
17990 htab_create_alloc_ex (cu->header.length / 12,
17991 die_hash,
17992 die_eq,
17993 NULL,
17994 &cu->comp_unit_obstack,
17995 hashtab_obstack_allocate,
17996 dummy_obstack_deallocate);
348e048f 17997
dee91e82
DE
17998 if (has_children)
17999 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18000 &info_ptr, comp_unit_die);
18001 cu->dies = comp_unit_die;
18002 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18003
18004 /* We try not to read any attributes in this function, because not
9cdd5dbd 18005 all CUs needed for references have been loaded yet, and symbol
348e048f 18006 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18007 or we won't be able to build types correctly.
18008 Similarly, if we do not read the producer, we can not apply
18009 producer-specific interpretation. */
95554aad 18010 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18011}
348e048f 18012
3019eac3
DE
18013/* Read in a signatured type and build its CU and DIEs.
18014 If the type is a stub for the real type in a DWO file,
18015 read in the real type from the DWO file as well. */
dee91e82
DE
18016
18017static void
18018read_signatured_type (struct signatured_type *sig_type)
18019{
18020 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18021
3019eac3 18022 gdb_assert (per_cu->is_debug_types);
dee91e82 18023 gdb_assert (per_cu->cu == NULL);
348e048f 18024
f4dc4d17
DE
18025 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18026 read_signatured_type_reader, NULL);
c906108c
SS
18027}
18028
c906108c
SS
18029/* Decode simple location descriptions.
18030 Given a pointer to a dwarf block that defines a location, compute
18031 the location and return the value.
18032
4cecd739
DJ
18033 NOTE drow/2003-11-18: This function is called in two situations
18034 now: for the address of static or global variables (partial symbols
18035 only) and for offsets into structures which are expected to be
18036 (more or less) constant. The partial symbol case should go away,
18037 and only the constant case should remain. That will let this
18038 function complain more accurately. A few special modes are allowed
18039 without complaint for global variables (for instance, global
18040 register values and thread-local values).
c906108c
SS
18041
18042 A location description containing no operations indicates that the
4cecd739 18043 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18044 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18045 callers will only want a very basic result and this can become a
21ae7a4d
JK
18046 complaint.
18047
18048 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18049
18050static CORE_ADDR
e7c27a73 18051decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18052{
e7c27a73 18053 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18054 size_t i;
18055 size_t size = blk->size;
d521ce57 18056 const gdb_byte *data = blk->data;
21ae7a4d
JK
18057 CORE_ADDR stack[64];
18058 int stacki;
18059 unsigned int bytes_read, unsnd;
18060 gdb_byte op;
c906108c 18061
21ae7a4d
JK
18062 i = 0;
18063 stacki = 0;
18064 stack[stacki] = 0;
18065 stack[++stacki] = 0;
18066
18067 while (i < size)
18068 {
18069 op = data[i++];
18070 switch (op)
18071 {
18072 case DW_OP_lit0:
18073 case DW_OP_lit1:
18074 case DW_OP_lit2:
18075 case DW_OP_lit3:
18076 case DW_OP_lit4:
18077 case DW_OP_lit5:
18078 case DW_OP_lit6:
18079 case DW_OP_lit7:
18080 case DW_OP_lit8:
18081 case DW_OP_lit9:
18082 case DW_OP_lit10:
18083 case DW_OP_lit11:
18084 case DW_OP_lit12:
18085 case DW_OP_lit13:
18086 case DW_OP_lit14:
18087 case DW_OP_lit15:
18088 case DW_OP_lit16:
18089 case DW_OP_lit17:
18090 case DW_OP_lit18:
18091 case DW_OP_lit19:
18092 case DW_OP_lit20:
18093 case DW_OP_lit21:
18094 case DW_OP_lit22:
18095 case DW_OP_lit23:
18096 case DW_OP_lit24:
18097 case DW_OP_lit25:
18098 case DW_OP_lit26:
18099 case DW_OP_lit27:
18100 case DW_OP_lit28:
18101 case DW_OP_lit29:
18102 case DW_OP_lit30:
18103 case DW_OP_lit31:
18104 stack[++stacki] = op - DW_OP_lit0;
18105 break;
f1bea926 18106
21ae7a4d
JK
18107 case DW_OP_reg0:
18108 case DW_OP_reg1:
18109 case DW_OP_reg2:
18110 case DW_OP_reg3:
18111 case DW_OP_reg4:
18112 case DW_OP_reg5:
18113 case DW_OP_reg6:
18114 case DW_OP_reg7:
18115 case DW_OP_reg8:
18116 case DW_OP_reg9:
18117 case DW_OP_reg10:
18118 case DW_OP_reg11:
18119 case DW_OP_reg12:
18120 case DW_OP_reg13:
18121 case DW_OP_reg14:
18122 case DW_OP_reg15:
18123 case DW_OP_reg16:
18124 case DW_OP_reg17:
18125 case DW_OP_reg18:
18126 case DW_OP_reg19:
18127 case DW_OP_reg20:
18128 case DW_OP_reg21:
18129 case DW_OP_reg22:
18130 case DW_OP_reg23:
18131 case DW_OP_reg24:
18132 case DW_OP_reg25:
18133 case DW_OP_reg26:
18134 case DW_OP_reg27:
18135 case DW_OP_reg28:
18136 case DW_OP_reg29:
18137 case DW_OP_reg30:
18138 case DW_OP_reg31:
18139 stack[++stacki] = op - DW_OP_reg0;
18140 if (i < size)
18141 dwarf2_complex_location_expr_complaint ();
18142 break;
c906108c 18143
21ae7a4d
JK
18144 case DW_OP_regx:
18145 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18146 i += bytes_read;
18147 stack[++stacki] = unsnd;
18148 if (i < size)
18149 dwarf2_complex_location_expr_complaint ();
18150 break;
c906108c 18151
21ae7a4d
JK
18152 case DW_OP_addr:
18153 stack[++stacki] = read_address (objfile->obfd, &data[i],
18154 cu, &bytes_read);
18155 i += bytes_read;
18156 break;
d53d4ac5 18157
21ae7a4d
JK
18158 case DW_OP_const1u:
18159 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18160 i += 1;
18161 break;
18162
18163 case DW_OP_const1s:
18164 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18165 i += 1;
18166 break;
18167
18168 case DW_OP_const2u:
18169 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18170 i += 2;
18171 break;
18172
18173 case DW_OP_const2s:
18174 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18175 i += 2;
18176 break;
d53d4ac5 18177
21ae7a4d
JK
18178 case DW_OP_const4u:
18179 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18180 i += 4;
18181 break;
18182
18183 case DW_OP_const4s:
18184 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18185 i += 4;
18186 break;
18187
585861ea
JK
18188 case DW_OP_const8u:
18189 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18190 i += 8;
18191 break;
18192
21ae7a4d
JK
18193 case DW_OP_constu:
18194 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18195 &bytes_read);
18196 i += bytes_read;
18197 break;
18198
18199 case DW_OP_consts:
18200 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18201 i += bytes_read;
18202 break;
18203
18204 case DW_OP_dup:
18205 stack[stacki + 1] = stack[stacki];
18206 stacki++;
18207 break;
18208
18209 case DW_OP_plus:
18210 stack[stacki - 1] += stack[stacki];
18211 stacki--;
18212 break;
18213
18214 case DW_OP_plus_uconst:
18215 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18216 &bytes_read);
18217 i += bytes_read;
18218 break;
18219
18220 case DW_OP_minus:
18221 stack[stacki - 1] -= stack[stacki];
18222 stacki--;
18223 break;
18224
18225 case DW_OP_deref:
18226 /* If we're not the last op, then we definitely can't encode
18227 this using GDB's address_class enum. This is valid for partial
18228 global symbols, although the variable's address will be bogus
18229 in the psymtab. */
18230 if (i < size)
18231 dwarf2_complex_location_expr_complaint ();
18232 break;
18233
18234 case DW_OP_GNU_push_tls_address:
18235 /* The top of the stack has the offset from the beginning
18236 of the thread control block at which the variable is located. */
18237 /* Nothing should follow this operator, so the top of stack would
18238 be returned. */
18239 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18240 address will be bogus in the psymtab. Make it always at least
18241 non-zero to not look as a variable garbage collected by linker
18242 which have DW_OP_addr 0. */
21ae7a4d
JK
18243 if (i < size)
18244 dwarf2_complex_location_expr_complaint ();
585861ea 18245 stack[stacki]++;
21ae7a4d
JK
18246 break;
18247
18248 case DW_OP_GNU_uninit:
18249 break;
18250
3019eac3 18251 case DW_OP_GNU_addr_index:
49f6c839 18252 case DW_OP_GNU_const_index:
3019eac3
DE
18253 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18254 &bytes_read);
18255 i += bytes_read;
18256 break;
18257
21ae7a4d
JK
18258 default:
18259 {
f39c6ffd 18260 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18261
18262 if (name)
18263 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18264 name);
18265 else
18266 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18267 op);
18268 }
18269
18270 return (stack[stacki]);
d53d4ac5 18271 }
3c6e0cb3 18272
21ae7a4d
JK
18273 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18274 outside of the allocated space. Also enforce minimum>0. */
18275 if (stacki >= ARRAY_SIZE (stack) - 1)
18276 {
18277 complaint (&symfile_complaints,
18278 _("location description stack overflow"));
18279 return 0;
18280 }
18281
18282 if (stacki <= 0)
18283 {
18284 complaint (&symfile_complaints,
18285 _("location description stack underflow"));
18286 return 0;
18287 }
18288 }
18289 return (stack[stacki]);
c906108c
SS
18290}
18291
18292/* memory allocation interface */
18293
c906108c 18294static struct dwarf_block *
7b5a2f43 18295dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18296{
18297 struct dwarf_block *blk;
18298
18299 blk = (struct dwarf_block *)
7b5a2f43 18300 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18301 return (blk);
18302}
18303
c906108c 18304static struct die_info *
b60c80d6 18305dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18306{
18307 struct die_info *die;
b60c80d6
DJ
18308 size_t size = sizeof (struct die_info);
18309
18310 if (num_attrs > 1)
18311 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18312
b60c80d6 18313 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18314 memset (die, 0, sizeof (struct die_info));
18315 return (die);
18316}
2e276125
JB
18317
18318\f
18319/* Macro support. */
18320
233d95b5
JK
18321/* Return file name relative to the compilation directory of file number I in
18322 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18323 responsible for freeing it. */
233d95b5 18324
2e276125 18325static char *
233d95b5 18326file_file_name (int file, struct line_header *lh)
2e276125 18327{
6a83a1e6
EZ
18328 /* Is the file number a valid index into the line header's file name
18329 table? Remember that file numbers start with one, not zero. */
18330 if (1 <= file && file <= lh->num_file_names)
18331 {
18332 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18333
233d95b5 18334 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18335 return xstrdup (fe->name);
233d95b5
JK
18336 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18337 fe->name, NULL);
6a83a1e6 18338 }
2e276125
JB
18339 else
18340 {
6a83a1e6
EZ
18341 /* The compiler produced a bogus file number. We can at least
18342 record the macro definitions made in the file, even if we
18343 won't be able to find the file by name. */
18344 char fake_name[80];
9a619af0 18345
8c042590
PM
18346 xsnprintf (fake_name, sizeof (fake_name),
18347 "<bad macro file number %d>", file);
2e276125 18348
6e70227d 18349 complaint (&symfile_complaints,
6a83a1e6
EZ
18350 _("bad file number in macro information (%d)"),
18351 file);
2e276125 18352
6a83a1e6 18353 return xstrdup (fake_name);
2e276125
JB
18354 }
18355}
18356
233d95b5
JK
18357/* Return the full name of file number I in *LH's file name table.
18358 Use COMP_DIR as the name of the current directory of the
18359 compilation. The result is allocated using xmalloc; the caller is
18360 responsible for freeing it. */
18361static char *
18362file_full_name (int file, struct line_header *lh, const char *comp_dir)
18363{
18364 /* Is the file number a valid index into the line header's file name
18365 table? Remember that file numbers start with one, not zero. */
18366 if (1 <= file && file <= lh->num_file_names)
18367 {
18368 char *relative = file_file_name (file, lh);
18369
18370 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18371 return relative;
18372 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18373 }
18374 else
18375 return file_file_name (file, lh);
18376}
18377
2e276125
JB
18378
18379static struct macro_source_file *
18380macro_start_file (int file, int line,
18381 struct macro_source_file *current_file,
18382 const char *comp_dir,
18383 struct line_header *lh, struct objfile *objfile)
18384{
233d95b5
JK
18385 /* File name relative to the compilation directory of this source file. */
18386 char *file_name = file_file_name (file, lh);
2e276125
JB
18387
18388 /* We don't create a macro table for this compilation unit
18389 at all until we actually get a filename. */
18390 if (! pending_macros)
6532ff36 18391 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18392 objfile->per_bfd->macro_cache,
18393 comp_dir);
2e276125
JB
18394
18395 if (! current_file)
abc9d0dc
TT
18396 {
18397 /* If we have no current file, then this must be the start_file
18398 directive for the compilation unit's main source file. */
233d95b5 18399 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18400 macro_define_special (pending_macros);
18401 }
2e276125 18402 else
233d95b5 18403 current_file = macro_include (current_file, line, file_name);
2e276125 18404
233d95b5 18405 xfree (file_name);
6e70227d 18406
2e276125
JB
18407 return current_file;
18408}
18409
18410
18411/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18412 followed by a null byte. */
18413static char *
18414copy_string (const char *buf, int len)
18415{
18416 char *s = xmalloc (len + 1);
9a619af0 18417
2e276125
JB
18418 memcpy (s, buf, len);
18419 s[len] = '\0';
2e276125
JB
18420 return s;
18421}
18422
18423
18424static const char *
18425consume_improper_spaces (const char *p, const char *body)
18426{
18427 if (*p == ' ')
18428 {
4d3c2250 18429 complaint (&symfile_complaints,
3e43a32a
MS
18430 _("macro definition contains spaces "
18431 "in formal argument list:\n`%s'"),
4d3c2250 18432 body);
2e276125
JB
18433
18434 while (*p == ' ')
18435 p++;
18436 }
18437
18438 return p;
18439}
18440
18441
18442static void
18443parse_macro_definition (struct macro_source_file *file, int line,
18444 const char *body)
18445{
18446 const char *p;
18447
18448 /* The body string takes one of two forms. For object-like macro
18449 definitions, it should be:
18450
18451 <macro name> " " <definition>
18452
18453 For function-like macro definitions, it should be:
18454
18455 <macro name> "() " <definition>
18456 or
18457 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18458
18459 Spaces may appear only where explicitly indicated, and in the
18460 <definition>.
18461
18462 The Dwarf 2 spec says that an object-like macro's name is always
18463 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18464 the space when the macro's definition is the empty string.
2e276125
JB
18465
18466 The Dwarf 2 spec says that there should be no spaces between the
18467 formal arguments in a function-like macro's formal argument list,
18468 but versions of GCC around March 2002 include spaces after the
18469 commas. */
18470
18471
18472 /* Find the extent of the macro name. The macro name is terminated
18473 by either a space or null character (for an object-like macro) or
18474 an opening paren (for a function-like macro). */
18475 for (p = body; *p; p++)
18476 if (*p == ' ' || *p == '(')
18477 break;
18478
18479 if (*p == ' ' || *p == '\0')
18480 {
18481 /* It's an object-like macro. */
18482 int name_len = p - body;
18483 char *name = copy_string (body, name_len);
18484 const char *replacement;
18485
18486 if (*p == ' ')
18487 replacement = body + name_len + 1;
18488 else
18489 {
4d3c2250 18490 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18491 replacement = body + name_len;
18492 }
6e70227d 18493
2e276125
JB
18494 macro_define_object (file, line, name, replacement);
18495
18496 xfree (name);
18497 }
18498 else if (*p == '(')
18499 {
18500 /* It's a function-like macro. */
18501 char *name = copy_string (body, p - body);
18502 int argc = 0;
18503 int argv_size = 1;
18504 char **argv = xmalloc (argv_size * sizeof (*argv));
18505
18506 p++;
18507
18508 p = consume_improper_spaces (p, body);
18509
18510 /* Parse the formal argument list. */
18511 while (*p && *p != ')')
18512 {
18513 /* Find the extent of the current argument name. */
18514 const char *arg_start = p;
18515
18516 while (*p && *p != ',' && *p != ')' && *p != ' ')
18517 p++;
18518
18519 if (! *p || p == arg_start)
4d3c2250 18520 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18521 else
18522 {
18523 /* Make sure argv has room for the new argument. */
18524 if (argc >= argv_size)
18525 {
18526 argv_size *= 2;
18527 argv = xrealloc (argv, argv_size * sizeof (*argv));
18528 }
18529
18530 argv[argc++] = copy_string (arg_start, p - arg_start);
18531 }
18532
18533 p = consume_improper_spaces (p, body);
18534
18535 /* Consume the comma, if present. */
18536 if (*p == ',')
18537 {
18538 p++;
18539
18540 p = consume_improper_spaces (p, body);
18541 }
18542 }
18543
18544 if (*p == ')')
18545 {
18546 p++;
18547
18548 if (*p == ' ')
18549 /* Perfectly formed definition, no complaints. */
18550 macro_define_function (file, line, name,
6e70227d 18551 argc, (const char **) argv,
2e276125
JB
18552 p + 1);
18553 else if (*p == '\0')
18554 {
18555 /* Complain, but do define it. */
4d3c2250 18556 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18557 macro_define_function (file, line, name,
6e70227d 18558 argc, (const char **) argv,
2e276125
JB
18559 p);
18560 }
18561 else
18562 /* Just complain. */
4d3c2250 18563 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18564 }
18565 else
18566 /* Just complain. */
4d3c2250 18567 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18568
18569 xfree (name);
18570 {
18571 int i;
18572
18573 for (i = 0; i < argc; i++)
18574 xfree (argv[i]);
18575 }
18576 xfree (argv);
18577 }
18578 else
4d3c2250 18579 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18580}
18581
cf2c3c16
TT
18582/* Skip some bytes from BYTES according to the form given in FORM.
18583 Returns the new pointer. */
2e276125 18584
d521ce57
TT
18585static const gdb_byte *
18586skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
18587 enum dwarf_form form,
18588 unsigned int offset_size,
18589 struct dwarf2_section_info *section)
2e276125 18590{
cf2c3c16 18591 unsigned int bytes_read;
2e276125 18592
cf2c3c16 18593 switch (form)
2e276125 18594 {
cf2c3c16
TT
18595 case DW_FORM_data1:
18596 case DW_FORM_flag:
18597 ++bytes;
18598 break;
18599
18600 case DW_FORM_data2:
18601 bytes += 2;
18602 break;
18603
18604 case DW_FORM_data4:
18605 bytes += 4;
18606 break;
18607
18608 case DW_FORM_data8:
18609 bytes += 8;
18610 break;
18611
18612 case DW_FORM_string:
18613 read_direct_string (abfd, bytes, &bytes_read);
18614 bytes += bytes_read;
18615 break;
18616
18617 case DW_FORM_sec_offset:
18618 case DW_FORM_strp:
36586728 18619 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18620 bytes += offset_size;
18621 break;
18622
18623 case DW_FORM_block:
18624 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18625 bytes += bytes_read;
18626 break;
18627
18628 case DW_FORM_block1:
18629 bytes += 1 + read_1_byte (abfd, bytes);
18630 break;
18631 case DW_FORM_block2:
18632 bytes += 2 + read_2_bytes (abfd, bytes);
18633 break;
18634 case DW_FORM_block4:
18635 bytes += 4 + read_4_bytes (abfd, bytes);
18636 break;
18637
18638 case DW_FORM_sdata:
18639 case DW_FORM_udata:
3019eac3
DE
18640 case DW_FORM_GNU_addr_index:
18641 case DW_FORM_GNU_str_index:
d521ce57 18642 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
18643 if (bytes == NULL)
18644 {
18645 dwarf2_section_buffer_overflow_complaint (section);
18646 return NULL;
18647 }
cf2c3c16
TT
18648 break;
18649
18650 default:
18651 {
18652 complain:
18653 complaint (&symfile_complaints,
18654 _("invalid form 0x%x in `%s'"),
18655 form,
18656 section->asection->name);
18657 return NULL;
18658 }
2e276125
JB
18659 }
18660
cf2c3c16
TT
18661 return bytes;
18662}
757a13d0 18663
cf2c3c16
TT
18664/* A helper for dwarf_decode_macros that handles skipping an unknown
18665 opcode. Returns an updated pointer to the macro data buffer; or,
18666 on error, issues a complaint and returns NULL. */
757a13d0 18667
d521ce57 18668static const gdb_byte *
cf2c3c16 18669skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
18670 const gdb_byte **opcode_definitions,
18671 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
18672 bfd *abfd,
18673 unsigned int offset_size,
18674 struct dwarf2_section_info *section)
18675{
18676 unsigned int bytes_read, i;
18677 unsigned long arg;
d521ce57 18678 const gdb_byte *defn;
2e276125 18679
cf2c3c16 18680 if (opcode_definitions[opcode] == NULL)
2e276125 18681 {
cf2c3c16
TT
18682 complaint (&symfile_complaints,
18683 _("unrecognized DW_MACFINO opcode 0x%x"),
18684 opcode);
18685 return NULL;
18686 }
2e276125 18687
cf2c3c16
TT
18688 defn = opcode_definitions[opcode];
18689 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18690 defn += bytes_read;
2e276125 18691
cf2c3c16
TT
18692 for (i = 0; i < arg; ++i)
18693 {
f664829e
DE
18694 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18695 section);
cf2c3c16
TT
18696 if (mac_ptr == NULL)
18697 {
18698 /* skip_form_bytes already issued the complaint. */
18699 return NULL;
18700 }
18701 }
757a13d0 18702
cf2c3c16
TT
18703 return mac_ptr;
18704}
757a13d0 18705
cf2c3c16
TT
18706/* A helper function which parses the header of a macro section.
18707 If the macro section is the extended (for now called "GNU") type,
18708 then this updates *OFFSET_SIZE. Returns a pointer to just after
18709 the header, or issues a complaint and returns NULL on error. */
757a13d0 18710
d521ce57
TT
18711static const gdb_byte *
18712dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 18713 bfd *abfd,
d521ce57 18714 const gdb_byte *mac_ptr,
cf2c3c16
TT
18715 unsigned int *offset_size,
18716 int section_is_gnu)
18717{
18718 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18719
cf2c3c16
TT
18720 if (section_is_gnu)
18721 {
18722 unsigned int version, flags;
757a13d0 18723
cf2c3c16
TT
18724 version = read_2_bytes (abfd, mac_ptr);
18725 if (version != 4)
18726 {
18727 complaint (&symfile_complaints,
18728 _("unrecognized version `%d' in .debug_macro section"),
18729 version);
18730 return NULL;
18731 }
18732 mac_ptr += 2;
757a13d0 18733
cf2c3c16
TT
18734 flags = read_1_byte (abfd, mac_ptr);
18735 ++mac_ptr;
18736 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18737
cf2c3c16
TT
18738 if ((flags & 2) != 0)
18739 /* We don't need the line table offset. */
18740 mac_ptr += *offset_size;
757a13d0 18741
cf2c3c16
TT
18742 /* Vendor opcode descriptions. */
18743 if ((flags & 4) != 0)
18744 {
18745 unsigned int i, count;
757a13d0 18746
cf2c3c16
TT
18747 count = read_1_byte (abfd, mac_ptr);
18748 ++mac_ptr;
18749 for (i = 0; i < count; ++i)
18750 {
18751 unsigned int opcode, bytes_read;
18752 unsigned long arg;
18753
18754 opcode = read_1_byte (abfd, mac_ptr);
18755 ++mac_ptr;
18756 opcode_definitions[opcode] = mac_ptr;
18757 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18758 mac_ptr += bytes_read;
18759 mac_ptr += arg;
18760 }
757a13d0 18761 }
cf2c3c16 18762 }
757a13d0 18763
cf2c3c16
TT
18764 return mac_ptr;
18765}
757a13d0 18766
cf2c3c16 18767/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18768 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18769
18770static void
d521ce57
TT
18771dwarf_decode_macro_bytes (bfd *abfd,
18772 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 18773 struct macro_source_file *current_file,
15d034d0 18774 struct line_header *lh, const char *comp_dir,
cf2c3c16 18775 struct dwarf2_section_info *section,
36586728 18776 int section_is_gnu, int section_is_dwz,
cf2c3c16 18777 unsigned int offset_size,
8fc3fc34
TT
18778 struct objfile *objfile,
18779 htab_t include_hash)
cf2c3c16
TT
18780{
18781 enum dwarf_macro_record_type macinfo_type;
18782 int at_commandline;
d521ce57 18783 const gdb_byte *opcode_definitions[256];
757a13d0 18784
cf2c3c16
TT
18785 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18786 &offset_size, section_is_gnu);
18787 if (mac_ptr == NULL)
18788 {
18789 /* We already issued a complaint. */
18790 return;
18791 }
757a13d0
JK
18792
18793 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18794 GDB is still reading the definitions from command line. First
18795 DW_MACINFO_start_file will need to be ignored as it was already executed
18796 to create CURRENT_FILE for the main source holding also the command line
18797 definitions. On first met DW_MACINFO_start_file this flag is reset to
18798 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18799
18800 at_commandline = 1;
18801
18802 do
18803 {
18804 /* Do we at least have room for a macinfo type byte? */
18805 if (mac_ptr >= mac_end)
18806 {
f664829e 18807 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18808 break;
18809 }
18810
18811 macinfo_type = read_1_byte (abfd, mac_ptr);
18812 mac_ptr++;
18813
cf2c3c16
TT
18814 /* Note that we rely on the fact that the corresponding GNU and
18815 DWARF constants are the same. */
757a13d0
JK
18816 switch (macinfo_type)
18817 {
18818 /* A zero macinfo type indicates the end of the macro
18819 information. */
18820 case 0:
18821 break;
2e276125 18822
cf2c3c16
TT
18823 case DW_MACRO_GNU_define:
18824 case DW_MACRO_GNU_undef:
18825 case DW_MACRO_GNU_define_indirect:
18826 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18827 case DW_MACRO_GNU_define_indirect_alt:
18828 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18829 {
891d2f0b 18830 unsigned int bytes_read;
2e276125 18831 int line;
d521ce57 18832 const char *body;
cf2c3c16 18833 int is_define;
2e276125 18834
cf2c3c16
TT
18835 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18836 mac_ptr += bytes_read;
18837
18838 if (macinfo_type == DW_MACRO_GNU_define
18839 || macinfo_type == DW_MACRO_GNU_undef)
18840 {
18841 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18842 mac_ptr += bytes_read;
18843 }
18844 else
18845 {
18846 LONGEST str_offset;
18847
18848 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18849 mac_ptr += offset_size;
2e276125 18850
36586728 18851 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18852 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18853 || section_is_dwz)
36586728
TT
18854 {
18855 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18856
18857 body = read_indirect_string_from_dwz (dwz, str_offset);
18858 }
18859 else
18860 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18861 }
18862
18863 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18864 || macinfo_type == DW_MACRO_GNU_define_indirect
18865 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18866 if (! current_file)
757a13d0
JK
18867 {
18868 /* DWARF violation as no main source is present. */
18869 complaint (&symfile_complaints,
18870 _("debug info with no main source gives macro %s "
18871 "on line %d: %s"),
cf2c3c16
TT
18872 is_define ? _("definition") : _("undefinition"),
18873 line, body);
757a13d0
JK
18874 break;
18875 }
3e43a32a
MS
18876 if ((line == 0 && !at_commandline)
18877 || (line != 0 && at_commandline))
4d3c2250 18878 complaint (&symfile_complaints,
757a13d0
JK
18879 _("debug info gives %s macro %s with %s line %d: %s"),
18880 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18881 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18882 line == 0 ? _("zero") : _("non-zero"), line, body);
18883
cf2c3c16 18884 if (is_define)
757a13d0 18885 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18886 else
18887 {
18888 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18889 || macinfo_type == DW_MACRO_GNU_undef_indirect
18890 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18891 macro_undef (current_file, line, body);
18892 }
2e276125
JB
18893 }
18894 break;
18895
cf2c3c16 18896 case DW_MACRO_GNU_start_file:
2e276125 18897 {
891d2f0b 18898 unsigned int bytes_read;
2e276125
JB
18899 int line, file;
18900
18901 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18902 mac_ptr += bytes_read;
18903 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18904 mac_ptr += bytes_read;
18905
3e43a32a
MS
18906 if ((line == 0 && !at_commandline)
18907 || (line != 0 && at_commandline))
757a13d0
JK
18908 complaint (&symfile_complaints,
18909 _("debug info gives source %d included "
18910 "from %s at %s line %d"),
18911 file, at_commandline ? _("command-line") : _("file"),
18912 line == 0 ? _("zero") : _("non-zero"), line);
18913
18914 if (at_commandline)
18915 {
cf2c3c16
TT
18916 /* This DW_MACRO_GNU_start_file was executed in the
18917 pass one. */
757a13d0
JK
18918 at_commandline = 0;
18919 }
18920 else
18921 current_file = macro_start_file (file, line,
18922 current_file, comp_dir,
cf2c3c16 18923 lh, objfile);
2e276125
JB
18924 }
18925 break;
18926
cf2c3c16 18927 case DW_MACRO_GNU_end_file:
2e276125 18928 if (! current_file)
4d3c2250 18929 complaint (&symfile_complaints,
3e43a32a
MS
18930 _("macro debug info has an unmatched "
18931 "`close_file' directive"));
2e276125
JB
18932 else
18933 {
18934 current_file = current_file->included_by;
18935 if (! current_file)
18936 {
cf2c3c16 18937 enum dwarf_macro_record_type next_type;
2e276125
JB
18938
18939 /* GCC circa March 2002 doesn't produce the zero
18940 type byte marking the end of the compilation
18941 unit. Complain if it's not there, but exit no
18942 matter what. */
18943
18944 /* Do we at least have room for a macinfo type byte? */
18945 if (mac_ptr >= mac_end)
18946 {
f664829e 18947 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18948 return;
18949 }
18950
18951 /* We don't increment mac_ptr here, so this is just
18952 a look-ahead. */
18953 next_type = read_1_byte (abfd, mac_ptr);
18954 if (next_type != 0)
4d3c2250 18955 complaint (&symfile_complaints,
3e43a32a
MS
18956 _("no terminating 0-type entry for "
18957 "macros in `.debug_macinfo' section"));
2e276125
JB
18958
18959 return;
18960 }
18961 }
18962 break;
18963
cf2c3c16 18964 case DW_MACRO_GNU_transparent_include:
36586728 18965 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18966 {
18967 LONGEST offset;
8fc3fc34 18968 void **slot;
a036ba48
TT
18969 bfd *include_bfd = abfd;
18970 struct dwarf2_section_info *include_section = section;
18971 struct dwarf2_section_info alt_section;
d521ce57 18972 const gdb_byte *include_mac_end = mac_end;
a036ba48 18973 int is_dwz = section_is_dwz;
d521ce57 18974 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
18975
18976 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18977 mac_ptr += offset_size;
18978
a036ba48
TT
18979 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18980 {
18981 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18982
18983 dwarf2_read_section (dwarf2_per_objfile->objfile,
18984 &dwz->macro);
18985
18986 include_bfd = dwz->macro.asection->owner;
18987 include_section = &dwz->macro;
18988 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18989 is_dwz = 1;
18990 }
18991
18992 new_mac_ptr = include_section->buffer + offset;
18993 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18994
8fc3fc34
TT
18995 if (*slot != NULL)
18996 {
18997 /* This has actually happened; see
18998 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18999 complaint (&symfile_complaints,
19000 _("recursive DW_MACRO_GNU_transparent_include in "
19001 ".debug_macro section"));
19002 }
19003 else
19004 {
d521ce57 19005 *slot = (void *) new_mac_ptr;
36586728 19006
a036ba48 19007 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19008 include_mac_end, current_file,
8fc3fc34 19009 lh, comp_dir,
36586728 19010 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19011 offset_size, objfile, include_hash);
19012
d521ce57 19013 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19014 }
cf2c3c16
TT
19015 }
19016 break;
19017
2e276125 19018 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19019 if (!section_is_gnu)
19020 {
19021 unsigned int bytes_read;
19022 int constant;
2e276125 19023
cf2c3c16
TT
19024 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19025 mac_ptr += bytes_read;
19026 read_direct_string (abfd, mac_ptr, &bytes_read);
19027 mac_ptr += bytes_read;
2e276125 19028
cf2c3c16
TT
19029 /* We don't recognize any vendor extensions. */
19030 break;
19031 }
19032 /* FALLTHROUGH */
19033
19034 default:
19035 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19036 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19037 section);
19038 if (mac_ptr == NULL)
19039 return;
19040 break;
2e276125 19041 }
757a13d0 19042 } while (macinfo_type != 0);
2e276125 19043}
8e19ed76 19044
cf2c3c16 19045static void
09262596 19046dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19047 const char *comp_dir, int section_is_gnu)
cf2c3c16 19048{
bb5ed363 19049 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19050 struct line_header *lh = cu->line_header;
19051 bfd *abfd;
d521ce57 19052 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19053 struct macro_source_file *current_file = 0;
19054 enum dwarf_macro_record_type macinfo_type;
19055 unsigned int offset_size = cu->header.offset_size;
d521ce57 19056 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19057 struct cleanup *cleanup;
19058 htab_t include_hash;
19059 void **slot;
09262596
DE
19060 struct dwarf2_section_info *section;
19061 const char *section_name;
19062
19063 if (cu->dwo_unit != NULL)
19064 {
19065 if (section_is_gnu)
19066 {
19067 section = &cu->dwo_unit->dwo_file->sections.macro;
19068 section_name = ".debug_macro.dwo";
19069 }
19070 else
19071 {
19072 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19073 section_name = ".debug_macinfo.dwo";
19074 }
19075 }
19076 else
19077 {
19078 if (section_is_gnu)
19079 {
19080 section = &dwarf2_per_objfile->macro;
19081 section_name = ".debug_macro";
19082 }
19083 else
19084 {
19085 section = &dwarf2_per_objfile->macinfo;
19086 section_name = ".debug_macinfo";
19087 }
19088 }
cf2c3c16 19089
bb5ed363 19090 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19091 if (section->buffer == NULL)
19092 {
fceca515 19093 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19094 return;
19095 }
09262596 19096 abfd = section->asection->owner;
cf2c3c16
TT
19097
19098 /* First pass: Find the name of the base filename.
19099 This filename is needed in order to process all macros whose definition
19100 (or undefinition) comes from the command line. These macros are defined
19101 before the first DW_MACINFO_start_file entry, and yet still need to be
19102 associated to the base file.
19103
19104 To determine the base file name, we scan the macro definitions until we
19105 reach the first DW_MACINFO_start_file entry. We then initialize
19106 CURRENT_FILE accordingly so that any macro definition found before the
19107 first DW_MACINFO_start_file can still be associated to the base file. */
19108
19109 mac_ptr = section->buffer + offset;
19110 mac_end = section->buffer + section->size;
19111
19112 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19113 &offset_size, section_is_gnu);
19114 if (mac_ptr == NULL)
19115 {
19116 /* We already issued a complaint. */
19117 return;
19118 }
19119
19120 do
19121 {
19122 /* Do we at least have room for a macinfo type byte? */
19123 if (mac_ptr >= mac_end)
19124 {
19125 /* Complaint is printed during the second pass as GDB will probably
19126 stop the first pass earlier upon finding
19127 DW_MACINFO_start_file. */
19128 break;
19129 }
19130
19131 macinfo_type = read_1_byte (abfd, mac_ptr);
19132 mac_ptr++;
19133
19134 /* Note that we rely on the fact that the corresponding GNU and
19135 DWARF constants are the same. */
19136 switch (macinfo_type)
19137 {
19138 /* A zero macinfo type indicates the end of the macro
19139 information. */
19140 case 0:
19141 break;
19142
19143 case DW_MACRO_GNU_define:
19144 case DW_MACRO_GNU_undef:
19145 /* Only skip the data by MAC_PTR. */
19146 {
19147 unsigned int bytes_read;
19148
19149 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19150 mac_ptr += bytes_read;
19151 read_direct_string (abfd, mac_ptr, &bytes_read);
19152 mac_ptr += bytes_read;
19153 }
19154 break;
19155
19156 case DW_MACRO_GNU_start_file:
19157 {
19158 unsigned int bytes_read;
19159 int line, file;
19160
19161 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19162 mac_ptr += bytes_read;
19163 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19164 mac_ptr += bytes_read;
19165
19166 current_file = macro_start_file (file, line, current_file,
bb5ed363 19167 comp_dir, lh, objfile);
cf2c3c16
TT
19168 }
19169 break;
19170
19171 case DW_MACRO_GNU_end_file:
19172 /* No data to skip by MAC_PTR. */
19173 break;
19174
19175 case DW_MACRO_GNU_define_indirect:
19176 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19177 case DW_MACRO_GNU_define_indirect_alt:
19178 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19179 {
19180 unsigned int bytes_read;
19181
19182 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19183 mac_ptr += bytes_read;
19184 mac_ptr += offset_size;
19185 }
19186 break;
19187
19188 case DW_MACRO_GNU_transparent_include:
f7a35f02 19189 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19190 /* Note that, according to the spec, a transparent include
19191 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19192 skip this opcode. */
19193 mac_ptr += offset_size;
19194 break;
19195
19196 case DW_MACINFO_vendor_ext:
19197 /* Only skip the data by MAC_PTR. */
19198 if (!section_is_gnu)
19199 {
19200 unsigned int bytes_read;
19201
19202 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19203 mac_ptr += bytes_read;
19204 read_direct_string (abfd, mac_ptr, &bytes_read);
19205 mac_ptr += bytes_read;
19206 }
19207 /* FALLTHROUGH */
19208
19209 default:
19210 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19211 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19212 section);
19213 if (mac_ptr == NULL)
19214 return;
19215 break;
19216 }
19217 } while (macinfo_type != 0 && current_file == NULL);
19218
19219 /* Second pass: Process all entries.
19220
19221 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19222 command-line macro definitions/undefinitions. This flag is unset when we
19223 reach the first DW_MACINFO_start_file entry. */
19224
8fc3fc34
TT
19225 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19226 NULL, xcalloc, xfree);
19227 cleanup = make_cleanup_htab_delete (include_hash);
19228 mac_ptr = section->buffer + offset;
19229 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19230 *slot = (void *) mac_ptr;
8fc3fc34 19231 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19232 current_file, lh, comp_dir, section,
19233 section_is_gnu, 0,
8fc3fc34
TT
19234 offset_size, objfile, include_hash);
19235 do_cleanups (cleanup);
cf2c3c16
TT
19236}
19237
8e19ed76 19238/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19239 if so return true else false. */
380bca97 19240
8e19ed76
PS
19241static int
19242attr_form_is_block (struct attribute *attr)
19243{
19244 return (attr == NULL ? 0 :
19245 attr->form == DW_FORM_block1
19246 || attr->form == DW_FORM_block2
19247 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19248 || attr->form == DW_FORM_block
19249 || attr->form == DW_FORM_exprloc);
8e19ed76 19250}
4c2df51b 19251
c6a0999f
JB
19252/* Return non-zero if ATTR's value is a section offset --- classes
19253 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19254 You may use DW_UNSND (attr) to retrieve such offsets.
19255
19256 Section 7.5.4, "Attribute Encodings", explains that no attribute
19257 may have a value that belongs to more than one of these classes; it
19258 would be ambiguous if we did, because we use the same forms for all
19259 of them. */
380bca97 19260
3690dd37
JB
19261static int
19262attr_form_is_section_offset (struct attribute *attr)
19263{
19264 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19265 || attr->form == DW_FORM_data8
19266 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19267}
19268
3690dd37
JB
19269/* Return non-zero if ATTR's value falls in the 'constant' class, or
19270 zero otherwise. When this function returns true, you can apply
19271 dwarf2_get_attr_constant_value to it.
19272
19273 However, note that for some attributes you must check
19274 attr_form_is_section_offset before using this test. DW_FORM_data4
19275 and DW_FORM_data8 are members of both the constant class, and of
19276 the classes that contain offsets into other debug sections
19277 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19278 that, if an attribute's can be either a constant or one of the
19279 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19280 taken as section offsets, not constants. */
380bca97 19281
3690dd37
JB
19282static int
19283attr_form_is_constant (struct attribute *attr)
19284{
19285 switch (attr->form)
19286 {
19287 case DW_FORM_sdata:
19288 case DW_FORM_udata:
19289 case DW_FORM_data1:
19290 case DW_FORM_data2:
19291 case DW_FORM_data4:
19292 case DW_FORM_data8:
19293 return 1;
19294 default:
19295 return 0;
19296 }
19297}
19298
3019eac3
DE
19299/* Return the .debug_loc section to use for CU.
19300 For DWO files use .debug_loc.dwo. */
19301
19302static struct dwarf2_section_info *
19303cu_debug_loc_section (struct dwarf2_cu *cu)
19304{
19305 if (cu->dwo_unit)
19306 return &cu->dwo_unit->dwo_file->sections.loc;
19307 return &dwarf2_per_objfile->loc;
19308}
19309
8cf6f0b1
TT
19310/* A helper function that fills in a dwarf2_loclist_baton. */
19311
19312static void
19313fill_in_loclist_baton (struct dwarf2_cu *cu,
19314 struct dwarf2_loclist_baton *baton,
19315 struct attribute *attr)
19316{
3019eac3
DE
19317 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19318
19319 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19320
19321 baton->per_cu = cu->per_cu;
19322 gdb_assert (baton->per_cu);
19323 /* We don't know how long the location list is, but make sure we
19324 don't run off the edge of the section. */
3019eac3
DE
19325 baton->size = section->size - DW_UNSND (attr);
19326 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19327 baton->base_address = cu->base_address;
f664829e 19328 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19329}
19330
4c2df51b
DJ
19331static void
19332dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19333 struct dwarf2_cu *cu, int is_block)
4c2df51b 19334{
bb5ed363 19335 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19336 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19337
3690dd37 19338 if (attr_form_is_section_offset (attr)
3019eac3 19339 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19340 the section. If so, fall through to the complaint in the
19341 other branch. */
3019eac3 19342 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19343 {
0d53c4c4 19344 struct dwarf2_loclist_baton *baton;
4c2df51b 19345
bb5ed363 19346 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19347 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19348
8cf6f0b1 19349 fill_in_loclist_baton (cu, baton, attr);
be391dca 19350
d00adf39 19351 if (cu->base_known == 0)
0d53c4c4 19352 complaint (&symfile_complaints,
3e43a32a
MS
19353 _("Location list used without "
19354 "specifying the CU base address."));
4c2df51b 19355
f1e6e072
TT
19356 SYMBOL_ACLASS_INDEX (sym) = (is_block
19357 ? dwarf2_loclist_block_index
19358 : dwarf2_loclist_index);
0d53c4c4
DJ
19359 SYMBOL_LOCATION_BATON (sym) = baton;
19360 }
19361 else
19362 {
19363 struct dwarf2_locexpr_baton *baton;
19364
bb5ed363 19365 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19366 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19367 baton->per_cu = cu->per_cu;
19368 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19369
19370 if (attr_form_is_block (attr))
19371 {
19372 /* Note that we're just copying the block's data pointer
19373 here, not the actual data. We're still pointing into the
6502dd73
DJ
19374 info_buffer for SYM's objfile; right now we never release
19375 that buffer, but when we do clean up properly this may
19376 need to change. */
0d53c4c4
DJ
19377 baton->size = DW_BLOCK (attr)->size;
19378 baton->data = DW_BLOCK (attr)->data;
19379 }
19380 else
19381 {
19382 dwarf2_invalid_attrib_class_complaint ("location description",
19383 SYMBOL_NATURAL_NAME (sym));
19384 baton->size = 0;
0d53c4c4 19385 }
6e70227d 19386
f1e6e072
TT
19387 SYMBOL_ACLASS_INDEX (sym) = (is_block
19388 ? dwarf2_locexpr_block_index
19389 : dwarf2_locexpr_index);
0d53c4c4
DJ
19390 SYMBOL_LOCATION_BATON (sym) = baton;
19391 }
4c2df51b 19392}
6502dd73 19393
9aa1f1e3
TT
19394/* Return the OBJFILE associated with the compilation unit CU. If CU
19395 came from a separate debuginfo file, then the master objfile is
19396 returned. */
ae0d2f24
UW
19397
19398struct objfile *
19399dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19400{
9291a0cd 19401 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19402
19403 /* Return the master objfile, so that we can report and look up the
19404 correct file containing this variable. */
19405 if (objfile->separate_debug_objfile_backlink)
19406 objfile = objfile->separate_debug_objfile_backlink;
19407
19408 return objfile;
19409}
19410
96408a79
SA
19411/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19412 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19413 CU_HEADERP first. */
19414
19415static const struct comp_unit_head *
19416per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19417 struct dwarf2_per_cu_data *per_cu)
19418{
d521ce57 19419 const gdb_byte *info_ptr;
96408a79
SA
19420
19421 if (per_cu->cu)
19422 return &per_cu->cu->header;
19423
8a0459fd 19424 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19425
19426 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19427 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19428
19429 return cu_headerp;
19430}
19431
ae0d2f24
UW
19432/* Return the address size given in the compilation unit header for CU. */
19433
98714339 19434int
ae0d2f24
UW
19435dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19436{
96408a79
SA
19437 struct comp_unit_head cu_header_local;
19438 const struct comp_unit_head *cu_headerp;
c471e790 19439
96408a79
SA
19440 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19441
19442 return cu_headerp->addr_size;
ae0d2f24
UW
19443}
19444
9eae7c52
TT
19445/* Return the offset size given in the compilation unit header for CU. */
19446
19447int
19448dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19449{
96408a79
SA
19450 struct comp_unit_head cu_header_local;
19451 const struct comp_unit_head *cu_headerp;
9c6c53f7 19452
96408a79
SA
19453 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19454
19455 return cu_headerp->offset_size;
19456}
19457
19458/* See its dwarf2loc.h declaration. */
19459
19460int
19461dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19462{
19463 struct comp_unit_head cu_header_local;
19464 const struct comp_unit_head *cu_headerp;
19465
19466 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19467
19468 if (cu_headerp->version == 2)
19469 return cu_headerp->addr_size;
19470 else
19471 return cu_headerp->offset_size;
181cebd4
JK
19472}
19473
9aa1f1e3
TT
19474/* Return the text offset of the CU. The returned offset comes from
19475 this CU's objfile. If this objfile came from a separate debuginfo
19476 file, then the offset may be different from the corresponding
19477 offset in the parent objfile. */
19478
19479CORE_ADDR
19480dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19481{
bb3fa9d0 19482 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19483
19484 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19485}
19486
348e048f
DE
19487/* Locate the .debug_info compilation unit from CU's objfile which contains
19488 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19489
19490static struct dwarf2_per_cu_data *
b64f50a1 19491dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19492 unsigned int offset_in_dwz,
ae038cb0
DJ
19493 struct objfile *objfile)
19494{
19495 struct dwarf2_per_cu_data *this_cu;
19496 int low, high;
36586728 19497 const sect_offset *cu_off;
ae038cb0 19498
ae038cb0
DJ
19499 low = 0;
19500 high = dwarf2_per_objfile->n_comp_units - 1;
19501 while (high > low)
19502 {
36586728 19503 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19504 int mid = low + (high - low) / 2;
9a619af0 19505
36586728
TT
19506 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19507 cu_off = &mid_cu->offset;
19508 if (mid_cu->is_dwz > offset_in_dwz
19509 || (mid_cu->is_dwz == offset_in_dwz
19510 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19511 high = mid;
19512 else
19513 low = mid + 1;
19514 }
19515 gdb_assert (low == high);
36586728
TT
19516 this_cu = dwarf2_per_objfile->all_comp_units[low];
19517 cu_off = &this_cu->offset;
19518 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19519 {
36586728 19520 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19521 error (_("Dwarf Error: could not find partial DIE containing "
19522 "offset 0x%lx [in module %s]"),
b64f50a1 19523 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19524
b64f50a1
JK
19525 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19526 <= offset.sect_off);
ae038cb0
DJ
19527 return dwarf2_per_objfile->all_comp_units[low-1];
19528 }
19529 else
19530 {
19531 this_cu = dwarf2_per_objfile->all_comp_units[low];
19532 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19533 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19534 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19535 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19536 return this_cu;
19537 }
19538}
19539
23745b47 19540/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19541
9816fde3 19542static void
23745b47 19543init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19544{
9816fde3 19545 memset (cu, 0, sizeof (*cu));
23745b47
DE
19546 per_cu->cu = cu;
19547 cu->per_cu = per_cu;
19548 cu->objfile = per_cu->objfile;
93311388 19549 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19550}
19551
19552/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19553
19554static void
95554aad
TT
19555prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19556 enum language pretend_language)
9816fde3
JK
19557{
19558 struct attribute *attr;
19559
19560 /* Set the language we're debugging. */
19561 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19562 if (attr)
19563 set_cu_language (DW_UNSND (attr), cu);
19564 else
9cded63f 19565 {
95554aad 19566 cu->language = pretend_language;
9cded63f
TT
19567 cu->language_defn = language_def (cu->language);
19568 }
dee91e82
DE
19569
19570 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19571 if (attr)
19572 cu->producer = DW_STRING (attr);
93311388
DE
19573}
19574
ae038cb0
DJ
19575/* Release one cached compilation unit, CU. We unlink it from the tree
19576 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19577 the caller is responsible for that.
19578 NOTE: DATA is a void * because this function is also used as a
19579 cleanup routine. */
ae038cb0
DJ
19580
19581static void
68dc6402 19582free_heap_comp_unit (void *data)
ae038cb0
DJ
19583{
19584 struct dwarf2_cu *cu = data;
19585
23745b47
DE
19586 gdb_assert (cu->per_cu != NULL);
19587 cu->per_cu->cu = NULL;
ae038cb0
DJ
19588 cu->per_cu = NULL;
19589
19590 obstack_free (&cu->comp_unit_obstack, NULL);
19591
19592 xfree (cu);
19593}
19594
72bf9492 19595/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19596 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19597 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19598
19599static void
19600free_stack_comp_unit (void *data)
19601{
19602 struct dwarf2_cu *cu = data;
19603
23745b47
DE
19604 gdb_assert (cu->per_cu != NULL);
19605 cu->per_cu->cu = NULL;
19606 cu->per_cu = NULL;
19607
72bf9492
DJ
19608 obstack_free (&cu->comp_unit_obstack, NULL);
19609 cu->partial_dies = NULL;
ae038cb0
DJ
19610}
19611
19612/* Free all cached compilation units. */
19613
19614static void
19615free_cached_comp_units (void *data)
19616{
19617 struct dwarf2_per_cu_data *per_cu, **last_chain;
19618
19619 per_cu = dwarf2_per_objfile->read_in_chain;
19620 last_chain = &dwarf2_per_objfile->read_in_chain;
19621 while (per_cu != NULL)
19622 {
19623 struct dwarf2_per_cu_data *next_cu;
19624
19625 next_cu = per_cu->cu->read_in_chain;
19626
68dc6402 19627 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19628 *last_chain = next_cu;
19629
19630 per_cu = next_cu;
19631 }
19632}
19633
19634/* Increase the age counter on each cached compilation unit, and free
19635 any that are too old. */
19636
19637static void
19638age_cached_comp_units (void)
19639{
19640 struct dwarf2_per_cu_data *per_cu, **last_chain;
19641
19642 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19643 per_cu = dwarf2_per_objfile->read_in_chain;
19644 while (per_cu != NULL)
19645 {
19646 per_cu->cu->last_used ++;
19647 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19648 dwarf2_mark (per_cu->cu);
19649 per_cu = per_cu->cu->read_in_chain;
19650 }
19651
19652 per_cu = dwarf2_per_objfile->read_in_chain;
19653 last_chain = &dwarf2_per_objfile->read_in_chain;
19654 while (per_cu != NULL)
19655 {
19656 struct dwarf2_per_cu_data *next_cu;
19657
19658 next_cu = per_cu->cu->read_in_chain;
19659
19660 if (!per_cu->cu->mark)
19661 {
68dc6402 19662 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19663 *last_chain = next_cu;
19664 }
19665 else
19666 last_chain = &per_cu->cu->read_in_chain;
19667
19668 per_cu = next_cu;
19669 }
19670}
19671
19672/* Remove a single compilation unit from the cache. */
19673
19674static void
dee91e82 19675free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19676{
19677 struct dwarf2_per_cu_data *per_cu, **last_chain;
19678
19679 per_cu = dwarf2_per_objfile->read_in_chain;
19680 last_chain = &dwarf2_per_objfile->read_in_chain;
19681 while (per_cu != NULL)
19682 {
19683 struct dwarf2_per_cu_data *next_cu;
19684
19685 next_cu = per_cu->cu->read_in_chain;
19686
dee91e82 19687 if (per_cu == target_per_cu)
ae038cb0 19688 {
68dc6402 19689 free_heap_comp_unit (per_cu->cu);
dee91e82 19690 per_cu->cu = NULL;
ae038cb0
DJ
19691 *last_chain = next_cu;
19692 break;
19693 }
19694 else
19695 last_chain = &per_cu->cu->read_in_chain;
19696
19697 per_cu = next_cu;
19698 }
19699}
19700
fe3e1990
DJ
19701/* Release all extra memory associated with OBJFILE. */
19702
19703void
19704dwarf2_free_objfile (struct objfile *objfile)
19705{
19706 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19707
19708 if (dwarf2_per_objfile == NULL)
19709 return;
19710
19711 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19712 free_cached_comp_units (NULL);
19713
7b9f3c50
DE
19714 if (dwarf2_per_objfile->quick_file_names_table)
19715 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19716
fe3e1990
DJ
19717 /* Everything else should be on the objfile obstack. */
19718}
19719
dee91e82
DE
19720/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19721 We store these in a hash table separate from the DIEs, and preserve them
19722 when the DIEs are flushed out of cache.
19723
19724 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 19725 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
19726 or the type may come from a DWO file. Furthermore, while it's more logical
19727 to use per_cu->section+offset, with Fission the section with the data is in
19728 the DWO file but we don't know that section at the point we need it.
19729 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19730 because we can enter the lookup routine, get_die_type_at_offset, from
19731 outside this file, and thus won't necessarily have PER_CU->cu.
19732 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 19733
dee91e82 19734struct dwarf2_per_cu_offset_and_type
1c379e20 19735{
dee91e82 19736 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19737 sect_offset offset;
1c379e20
DJ
19738 struct type *type;
19739};
19740
dee91e82 19741/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19742
19743static hashval_t
dee91e82 19744per_cu_offset_and_type_hash (const void *item)
1c379e20 19745{
dee91e82 19746 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19747
dee91e82 19748 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19749}
19750
dee91e82 19751/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19752
19753static int
dee91e82 19754per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19755{
dee91e82
DE
19756 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19757 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19758
dee91e82
DE
19759 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19760 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19761}
19762
19763/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19764 table if necessary. For convenience, return TYPE.
19765
19766 The DIEs reading must have careful ordering to:
19767 * Not cause infite loops trying to read in DIEs as a prerequisite for
19768 reading current DIE.
19769 * Not trying to dereference contents of still incompletely read in types
19770 while reading in other DIEs.
19771 * Enable referencing still incompletely read in types just by a pointer to
19772 the type without accessing its fields.
19773
19774 Therefore caller should follow these rules:
19775 * Try to fetch any prerequisite types we may need to build this DIE type
19776 before building the type and calling set_die_type.
e71ec853 19777 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19778 possible before fetching more types to complete the current type.
19779 * Make the type as complete as possible before fetching more types. */
1c379e20 19780
f792889a 19781static struct type *
1c379e20
DJ
19782set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19783{
dee91e82 19784 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19785 struct objfile *objfile = cu->objfile;
1c379e20 19786
b4ba55a1
JB
19787 /* For Ada types, make sure that the gnat-specific data is always
19788 initialized (if not already set). There are a few types where
19789 we should not be doing so, because the type-specific area is
19790 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19791 where the type-specific area is used to store the floatformat).
19792 But this is not a problem, because the gnat-specific information
19793 is actually not needed for these types. */
19794 if (need_gnat_info (cu)
19795 && TYPE_CODE (type) != TYPE_CODE_FUNC
19796 && TYPE_CODE (type) != TYPE_CODE_FLT
19797 && !HAVE_GNAT_AUX_INFO (type))
19798 INIT_GNAT_SPECIFIC (type);
19799
dee91e82 19800 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19801 {
dee91e82
DE
19802 dwarf2_per_objfile->die_type_hash =
19803 htab_create_alloc_ex (127,
19804 per_cu_offset_and_type_hash,
19805 per_cu_offset_and_type_eq,
19806 NULL,
19807 &objfile->objfile_obstack,
19808 hashtab_obstack_allocate,
19809 dummy_obstack_deallocate);
f792889a 19810 }
1c379e20 19811
dee91e82 19812 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19813 ofs.offset = die->offset;
19814 ofs.type = type;
dee91e82
DE
19815 slot = (struct dwarf2_per_cu_offset_and_type **)
19816 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19817 if (*slot)
19818 complaint (&symfile_complaints,
19819 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19820 die->offset.sect_off);
673bfd45 19821 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19822 **slot = ofs;
f792889a 19823 return type;
1c379e20
DJ
19824}
19825
02142a6c
DE
19826/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
19827 or return NULL if the die does not have a saved type. */
1c379e20
DJ
19828
19829static struct type *
b64f50a1 19830get_die_type_at_offset (sect_offset offset,
673bfd45 19831 struct dwarf2_per_cu_data *per_cu)
1c379e20 19832{
dee91e82 19833 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19834
dee91e82 19835 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19836 return NULL;
1c379e20 19837
dee91e82 19838 ofs.per_cu = per_cu;
673bfd45 19839 ofs.offset = offset;
dee91e82 19840 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19841 if (slot)
19842 return slot->type;
19843 else
19844 return NULL;
19845}
19846
02142a6c 19847/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
19848 or return NULL if DIE does not have a saved type. */
19849
19850static struct type *
19851get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19852{
19853 return get_die_type_at_offset (die->offset, cu->per_cu);
19854}
19855
10b3939b
DJ
19856/* Add a dependence relationship from CU to REF_PER_CU. */
19857
19858static void
19859dwarf2_add_dependence (struct dwarf2_cu *cu,
19860 struct dwarf2_per_cu_data *ref_per_cu)
19861{
19862 void **slot;
19863
19864 if (cu->dependencies == NULL)
19865 cu->dependencies
19866 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19867 NULL, &cu->comp_unit_obstack,
19868 hashtab_obstack_allocate,
19869 dummy_obstack_deallocate);
19870
19871 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19872 if (*slot == NULL)
19873 *slot = ref_per_cu;
19874}
1c379e20 19875
f504f079
DE
19876/* Subroutine of dwarf2_mark to pass to htab_traverse.
19877 Set the mark field in every compilation unit in the
ae038cb0
DJ
19878 cache that we must keep because we are keeping CU. */
19879
10b3939b
DJ
19880static int
19881dwarf2_mark_helper (void **slot, void *data)
19882{
19883 struct dwarf2_per_cu_data *per_cu;
19884
19885 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19886
19887 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19888 reading of the chain. As such dependencies remain valid it is not much
19889 useful to track and undo them during QUIT cleanups. */
19890 if (per_cu->cu == NULL)
19891 return 1;
19892
10b3939b
DJ
19893 if (per_cu->cu->mark)
19894 return 1;
19895 per_cu->cu->mark = 1;
19896
19897 if (per_cu->cu->dependencies != NULL)
19898 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19899
19900 return 1;
19901}
19902
f504f079
DE
19903/* Set the mark field in CU and in every other compilation unit in the
19904 cache that we must keep because we are keeping CU. */
19905
ae038cb0
DJ
19906static void
19907dwarf2_mark (struct dwarf2_cu *cu)
19908{
19909 if (cu->mark)
19910 return;
19911 cu->mark = 1;
10b3939b
DJ
19912 if (cu->dependencies != NULL)
19913 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19914}
19915
19916static void
19917dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19918{
19919 while (per_cu)
19920 {
19921 per_cu->cu->mark = 0;
19922 per_cu = per_cu->cu->read_in_chain;
19923 }
72bf9492
DJ
19924}
19925
72bf9492
DJ
19926/* Trivial hash function for partial_die_info: the hash value of a DIE
19927 is its offset in .debug_info for this objfile. */
19928
19929static hashval_t
19930partial_die_hash (const void *item)
19931{
19932 const struct partial_die_info *part_die = item;
9a619af0 19933
b64f50a1 19934 return part_die->offset.sect_off;
72bf9492
DJ
19935}
19936
19937/* Trivial comparison function for partial_die_info structures: two DIEs
19938 are equal if they have the same offset. */
19939
19940static int
19941partial_die_eq (const void *item_lhs, const void *item_rhs)
19942{
19943 const struct partial_die_info *part_die_lhs = item_lhs;
19944 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19945
b64f50a1 19946 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19947}
19948
ae038cb0
DJ
19949static struct cmd_list_element *set_dwarf2_cmdlist;
19950static struct cmd_list_element *show_dwarf2_cmdlist;
19951
19952static void
19953set_dwarf2_cmd (char *args, int from_tty)
19954{
19955 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19956}
19957
19958static void
19959show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19960{
ae038cb0
DJ
19961 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19962}
19963
4bf44c1c 19964/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19965
19966static void
c1bd65d0 19967dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19968{
19969 struct dwarf2_per_objfile *data = d;
8b70b953 19970 int ix;
8b70b953 19971
95554aad
TT
19972 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19973 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19974 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19975
19976 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19977 VEC_free (dwarf2_per_cu_ptr,
19978 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19979
8b70b953 19980 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19981
19982 if (data->dwo_files)
19983 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
19984 if (data->dwp_file)
19985 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
19986
19987 if (data->dwz_file && data->dwz_file->dwz_bfd)
19988 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19989}
19990
19991\f
ae2de4f8 19992/* The "save gdb-index" command. */
9291a0cd
TT
19993
19994/* The contents of the hash table we create when building the string
19995 table. */
19996struct strtab_entry
19997{
19998 offset_type offset;
19999 const char *str;
20000};
20001
559a7a62
JK
20002/* Hash function for a strtab_entry.
20003
20004 Function is used only during write_hash_table so no index format backward
20005 compatibility is needed. */
b89be57b 20006
9291a0cd
TT
20007static hashval_t
20008hash_strtab_entry (const void *e)
20009{
20010 const struct strtab_entry *entry = e;
559a7a62 20011 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20012}
20013
20014/* Equality function for a strtab_entry. */
b89be57b 20015
9291a0cd
TT
20016static int
20017eq_strtab_entry (const void *a, const void *b)
20018{
20019 const struct strtab_entry *ea = a;
20020 const struct strtab_entry *eb = b;
20021 return !strcmp (ea->str, eb->str);
20022}
20023
20024/* Create a strtab_entry hash table. */
b89be57b 20025
9291a0cd
TT
20026static htab_t
20027create_strtab (void)
20028{
20029 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20030 xfree, xcalloc, xfree);
20031}
20032
20033/* Add a string to the constant pool. Return the string's offset in
20034 host order. */
b89be57b 20035
9291a0cd
TT
20036static offset_type
20037add_string (htab_t table, struct obstack *cpool, const char *str)
20038{
20039 void **slot;
20040 struct strtab_entry entry;
20041 struct strtab_entry *result;
20042
20043 entry.str = str;
20044 slot = htab_find_slot (table, &entry, INSERT);
20045 if (*slot)
20046 result = *slot;
20047 else
20048 {
20049 result = XNEW (struct strtab_entry);
20050 result->offset = obstack_object_size (cpool);
20051 result->str = str;
20052 obstack_grow_str0 (cpool, str);
20053 *slot = result;
20054 }
20055 return result->offset;
20056}
20057
20058/* An entry in the symbol table. */
20059struct symtab_index_entry
20060{
20061 /* The name of the symbol. */
20062 const char *name;
20063 /* The offset of the name in the constant pool. */
20064 offset_type index_offset;
20065 /* A sorted vector of the indices of all the CUs that hold an object
20066 of this name. */
20067 VEC (offset_type) *cu_indices;
20068};
20069
20070/* The symbol table. This is a power-of-2-sized hash table. */
20071struct mapped_symtab
20072{
20073 offset_type n_elements;
20074 offset_type size;
20075 struct symtab_index_entry **data;
20076};
20077
20078/* Hash function for a symtab_index_entry. */
b89be57b 20079
9291a0cd
TT
20080static hashval_t
20081hash_symtab_entry (const void *e)
20082{
20083 const struct symtab_index_entry *entry = e;
20084 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20085 sizeof (offset_type) * VEC_length (offset_type,
20086 entry->cu_indices),
20087 0);
20088}
20089
20090/* Equality function for a symtab_index_entry. */
b89be57b 20091
9291a0cd
TT
20092static int
20093eq_symtab_entry (const void *a, const void *b)
20094{
20095 const struct symtab_index_entry *ea = a;
20096 const struct symtab_index_entry *eb = b;
20097 int len = VEC_length (offset_type, ea->cu_indices);
20098 if (len != VEC_length (offset_type, eb->cu_indices))
20099 return 0;
20100 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20101 VEC_address (offset_type, eb->cu_indices),
20102 sizeof (offset_type) * len);
20103}
20104
20105/* Destroy a symtab_index_entry. */
b89be57b 20106
9291a0cd
TT
20107static void
20108delete_symtab_entry (void *p)
20109{
20110 struct symtab_index_entry *entry = p;
20111 VEC_free (offset_type, entry->cu_indices);
20112 xfree (entry);
20113}
20114
20115/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20116
9291a0cd 20117static htab_t
3876f04e 20118create_symbol_hash_table (void)
9291a0cd
TT
20119{
20120 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20121 delete_symtab_entry, xcalloc, xfree);
20122}
20123
20124/* Create a new mapped symtab object. */
b89be57b 20125
9291a0cd
TT
20126static struct mapped_symtab *
20127create_mapped_symtab (void)
20128{
20129 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20130 symtab->n_elements = 0;
20131 symtab->size = 1024;
20132 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20133 return symtab;
20134}
20135
20136/* Destroy a mapped_symtab. */
b89be57b 20137
9291a0cd
TT
20138static void
20139cleanup_mapped_symtab (void *p)
20140{
20141 struct mapped_symtab *symtab = p;
20142 /* The contents of the array are freed when the other hash table is
20143 destroyed. */
20144 xfree (symtab->data);
20145 xfree (symtab);
20146}
20147
20148/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20149 the slot.
20150
20151 Function is used only during write_hash_table so no index format backward
20152 compatibility is needed. */
b89be57b 20153
9291a0cd
TT
20154static struct symtab_index_entry **
20155find_slot (struct mapped_symtab *symtab, const char *name)
20156{
559a7a62 20157 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20158
20159 index = hash & (symtab->size - 1);
20160 step = ((hash * 17) & (symtab->size - 1)) | 1;
20161
20162 for (;;)
20163 {
20164 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20165 return &symtab->data[index];
20166 index = (index + step) & (symtab->size - 1);
20167 }
20168}
20169
20170/* Expand SYMTAB's hash table. */
b89be57b 20171
9291a0cd
TT
20172static void
20173hash_expand (struct mapped_symtab *symtab)
20174{
20175 offset_type old_size = symtab->size;
20176 offset_type i;
20177 struct symtab_index_entry **old_entries = symtab->data;
20178
20179 symtab->size *= 2;
20180 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20181
20182 for (i = 0; i < old_size; ++i)
20183 {
20184 if (old_entries[i])
20185 {
20186 struct symtab_index_entry **slot = find_slot (symtab,
20187 old_entries[i]->name);
20188 *slot = old_entries[i];
20189 }
20190 }
20191
20192 xfree (old_entries);
20193}
20194
156942c7
DE
20195/* Add an entry to SYMTAB. NAME is the name of the symbol.
20196 CU_INDEX is the index of the CU in which the symbol appears.
20197 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20198
9291a0cd
TT
20199static void
20200add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20201 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20202 offset_type cu_index)
20203{
20204 struct symtab_index_entry **slot;
156942c7 20205 offset_type cu_index_and_attrs;
9291a0cd
TT
20206
20207 ++symtab->n_elements;
20208 if (4 * symtab->n_elements / 3 >= symtab->size)
20209 hash_expand (symtab);
20210
20211 slot = find_slot (symtab, name);
20212 if (!*slot)
20213 {
20214 *slot = XNEW (struct symtab_index_entry);
20215 (*slot)->name = name;
156942c7 20216 /* index_offset is set later. */
9291a0cd
TT
20217 (*slot)->cu_indices = NULL;
20218 }
156942c7
DE
20219
20220 cu_index_and_attrs = 0;
20221 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20222 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20223 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20224
20225 /* We don't want to record an index value twice as we want to avoid the
20226 duplication.
20227 We process all global symbols and then all static symbols
20228 (which would allow us to avoid the duplication by only having to check
20229 the last entry pushed), but a symbol could have multiple kinds in one CU.
20230 To keep things simple we don't worry about the duplication here and
20231 sort and uniqufy the list after we've processed all symbols. */
20232 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20233}
20234
20235/* qsort helper routine for uniquify_cu_indices. */
20236
20237static int
20238offset_type_compare (const void *ap, const void *bp)
20239{
20240 offset_type a = *(offset_type *) ap;
20241 offset_type b = *(offset_type *) bp;
20242
20243 return (a > b) - (b > a);
20244}
20245
20246/* Sort and remove duplicates of all symbols' cu_indices lists. */
20247
20248static void
20249uniquify_cu_indices (struct mapped_symtab *symtab)
20250{
20251 int i;
20252
20253 for (i = 0; i < symtab->size; ++i)
20254 {
20255 struct symtab_index_entry *entry = symtab->data[i];
20256
20257 if (entry
20258 && entry->cu_indices != NULL)
20259 {
20260 unsigned int next_to_insert, next_to_check;
20261 offset_type last_value;
20262
20263 qsort (VEC_address (offset_type, entry->cu_indices),
20264 VEC_length (offset_type, entry->cu_indices),
20265 sizeof (offset_type), offset_type_compare);
20266
20267 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20268 next_to_insert = 1;
20269 for (next_to_check = 1;
20270 next_to_check < VEC_length (offset_type, entry->cu_indices);
20271 ++next_to_check)
20272 {
20273 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20274 != last_value)
20275 {
20276 last_value = VEC_index (offset_type, entry->cu_indices,
20277 next_to_check);
20278 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20279 last_value);
20280 ++next_to_insert;
20281 }
20282 }
20283 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20284 }
20285 }
9291a0cd
TT
20286}
20287
20288/* Add a vector of indices to the constant pool. */
b89be57b 20289
9291a0cd 20290static offset_type
3876f04e 20291add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20292 struct symtab_index_entry *entry)
20293{
20294 void **slot;
20295
3876f04e 20296 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20297 if (!*slot)
20298 {
20299 offset_type len = VEC_length (offset_type, entry->cu_indices);
20300 offset_type val = MAYBE_SWAP (len);
20301 offset_type iter;
20302 int i;
20303
20304 *slot = entry;
20305 entry->index_offset = obstack_object_size (cpool);
20306
20307 obstack_grow (cpool, &val, sizeof (val));
20308 for (i = 0;
20309 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20310 ++i)
20311 {
20312 val = MAYBE_SWAP (iter);
20313 obstack_grow (cpool, &val, sizeof (val));
20314 }
20315 }
20316 else
20317 {
20318 struct symtab_index_entry *old_entry = *slot;
20319 entry->index_offset = old_entry->index_offset;
20320 entry = old_entry;
20321 }
20322 return entry->index_offset;
20323}
20324
20325/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20326 constant pool entries going into the obstack CPOOL. */
b89be57b 20327
9291a0cd
TT
20328static void
20329write_hash_table (struct mapped_symtab *symtab,
20330 struct obstack *output, struct obstack *cpool)
20331{
20332 offset_type i;
3876f04e 20333 htab_t symbol_hash_table;
9291a0cd
TT
20334 htab_t str_table;
20335
3876f04e 20336 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20337 str_table = create_strtab ();
3876f04e 20338
9291a0cd
TT
20339 /* We add all the index vectors to the constant pool first, to
20340 ensure alignment is ok. */
20341 for (i = 0; i < symtab->size; ++i)
20342 {
20343 if (symtab->data[i])
3876f04e 20344 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20345 }
20346
20347 /* Now write out the hash table. */
20348 for (i = 0; i < symtab->size; ++i)
20349 {
20350 offset_type str_off, vec_off;
20351
20352 if (symtab->data[i])
20353 {
20354 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20355 vec_off = symtab->data[i]->index_offset;
20356 }
20357 else
20358 {
20359 /* While 0 is a valid constant pool index, it is not valid
20360 to have 0 for both offsets. */
20361 str_off = 0;
20362 vec_off = 0;
20363 }
20364
20365 str_off = MAYBE_SWAP (str_off);
20366 vec_off = MAYBE_SWAP (vec_off);
20367
20368 obstack_grow (output, &str_off, sizeof (str_off));
20369 obstack_grow (output, &vec_off, sizeof (vec_off));
20370 }
20371
20372 htab_delete (str_table);
3876f04e 20373 htab_delete (symbol_hash_table);
9291a0cd
TT
20374}
20375
0a5429f6
DE
20376/* Struct to map psymtab to CU index in the index file. */
20377struct psymtab_cu_index_map
20378{
20379 struct partial_symtab *psymtab;
20380 unsigned int cu_index;
20381};
20382
20383static hashval_t
20384hash_psymtab_cu_index (const void *item)
20385{
20386 const struct psymtab_cu_index_map *map = item;
20387
20388 return htab_hash_pointer (map->psymtab);
20389}
20390
20391static int
20392eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20393{
20394 const struct psymtab_cu_index_map *lhs = item_lhs;
20395 const struct psymtab_cu_index_map *rhs = item_rhs;
20396
20397 return lhs->psymtab == rhs->psymtab;
20398}
20399
20400/* Helper struct for building the address table. */
20401struct addrmap_index_data
20402{
20403 struct objfile *objfile;
20404 struct obstack *addr_obstack;
20405 htab_t cu_index_htab;
20406
20407 /* Non-zero if the previous_* fields are valid.
20408 We can't write an entry until we see the next entry (since it is only then
20409 that we know the end of the entry). */
20410 int previous_valid;
20411 /* Index of the CU in the table of all CUs in the index file. */
20412 unsigned int previous_cu_index;
0963b4bd 20413 /* Start address of the CU. */
0a5429f6
DE
20414 CORE_ADDR previous_cu_start;
20415};
20416
20417/* Write an address entry to OBSTACK. */
b89be57b 20418
9291a0cd 20419static void
0a5429f6
DE
20420add_address_entry (struct objfile *objfile, struct obstack *obstack,
20421 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20422{
0a5429f6 20423 offset_type cu_index_to_write;
9291a0cd
TT
20424 char addr[8];
20425 CORE_ADDR baseaddr;
20426
20427 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20428
0a5429f6
DE
20429 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20430 obstack_grow (obstack, addr, 8);
20431 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20432 obstack_grow (obstack, addr, 8);
20433 cu_index_to_write = MAYBE_SWAP (cu_index);
20434 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20435}
20436
20437/* Worker function for traversing an addrmap to build the address table. */
20438
20439static int
20440add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20441{
20442 struct addrmap_index_data *data = datap;
20443 struct partial_symtab *pst = obj;
0a5429f6
DE
20444
20445 if (data->previous_valid)
20446 add_address_entry (data->objfile, data->addr_obstack,
20447 data->previous_cu_start, start_addr,
20448 data->previous_cu_index);
20449
20450 data->previous_cu_start = start_addr;
20451 if (pst != NULL)
20452 {
20453 struct psymtab_cu_index_map find_map, *map;
20454 find_map.psymtab = pst;
20455 map = htab_find (data->cu_index_htab, &find_map);
20456 gdb_assert (map != NULL);
20457 data->previous_cu_index = map->cu_index;
20458 data->previous_valid = 1;
20459 }
20460 else
20461 data->previous_valid = 0;
20462
20463 return 0;
20464}
20465
20466/* Write OBJFILE's address map to OBSTACK.
20467 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20468 in the index file. */
20469
20470static void
20471write_address_map (struct objfile *objfile, struct obstack *obstack,
20472 htab_t cu_index_htab)
20473{
20474 struct addrmap_index_data addrmap_index_data;
20475
20476 /* When writing the address table, we have to cope with the fact that
20477 the addrmap iterator only provides the start of a region; we have to
20478 wait until the next invocation to get the start of the next region. */
20479
20480 addrmap_index_data.objfile = objfile;
20481 addrmap_index_data.addr_obstack = obstack;
20482 addrmap_index_data.cu_index_htab = cu_index_htab;
20483 addrmap_index_data.previous_valid = 0;
20484
20485 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20486 &addrmap_index_data);
20487
20488 /* It's highly unlikely the last entry (end address = 0xff...ff)
20489 is valid, but we should still handle it.
20490 The end address is recorded as the start of the next region, but that
20491 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20492 anyway. */
20493 if (addrmap_index_data.previous_valid)
20494 add_address_entry (objfile, obstack,
20495 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20496 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20497}
20498
156942c7
DE
20499/* Return the symbol kind of PSYM. */
20500
20501static gdb_index_symbol_kind
20502symbol_kind (struct partial_symbol *psym)
20503{
20504 domain_enum domain = PSYMBOL_DOMAIN (psym);
20505 enum address_class aclass = PSYMBOL_CLASS (psym);
20506
20507 switch (domain)
20508 {
20509 case VAR_DOMAIN:
20510 switch (aclass)
20511 {
20512 case LOC_BLOCK:
20513 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20514 case LOC_TYPEDEF:
20515 return GDB_INDEX_SYMBOL_KIND_TYPE;
20516 case LOC_COMPUTED:
20517 case LOC_CONST_BYTES:
20518 case LOC_OPTIMIZED_OUT:
20519 case LOC_STATIC:
20520 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20521 case LOC_CONST:
20522 /* Note: It's currently impossible to recognize psyms as enum values
20523 short of reading the type info. For now punt. */
20524 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20525 default:
20526 /* There are other LOC_FOO values that one might want to classify
20527 as variables, but dwarf2read.c doesn't currently use them. */
20528 return GDB_INDEX_SYMBOL_KIND_OTHER;
20529 }
20530 case STRUCT_DOMAIN:
20531 return GDB_INDEX_SYMBOL_KIND_TYPE;
20532 default:
20533 return GDB_INDEX_SYMBOL_KIND_OTHER;
20534 }
20535}
20536
9291a0cd 20537/* Add a list of partial symbols to SYMTAB. */
b89be57b 20538
9291a0cd
TT
20539static void
20540write_psymbols (struct mapped_symtab *symtab,
987d643c 20541 htab_t psyms_seen,
9291a0cd
TT
20542 struct partial_symbol **psymp,
20543 int count,
987d643c
TT
20544 offset_type cu_index,
20545 int is_static)
9291a0cd
TT
20546{
20547 for (; count-- > 0; ++psymp)
20548 {
156942c7
DE
20549 struct partial_symbol *psym = *psymp;
20550 void **slot;
987d643c 20551
156942c7 20552 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20553 error (_("Ada is not currently supported by the index"));
987d643c 20554
987d643c 20555 /* Only add a given psymbol once. */
156942c7 20556 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20557 if (!*slot)
20558 {
156942c7
DE
20559 gdb_index_symbol_kind kind = symbol_kind (psym);
20560
20561 *slot = psym;
20562 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20563 is_static, kind, cu_index);
987d643c 20564 }
9291a0cd
TT
20565 }
20566}
20567
20568/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20569 exception if there is an error. */
b89be57b 20570
9291a0cd
TT
20571static void
20572write_obstack (FILE *file, struct obstack *obstack)
20573{
20574 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20575 file)
20576 != obstack_object_size (obstack))
20577 error (_("couldn't data write to file"));
20578}
20579
20580/* Unlink a file if the argument is not NULL. */
b89be57b 20581
9291a0cd
TT
20582static void
20583unlink_if_set (void *p)
20584{
20585 char **filename = p;
20586 if (*filename)
20587 unlink (*filename);
20588}
20589
1fd400ff
TT
20590/* A helper struct used when iterating over debug_types. */
20591struct signatured_type_index_data
20592{
20593 struct objfile *objfile;
20594 struct mapped_symtab *symtab;
20595 struct obstack *types_list;
987d643c 20596 htab_t psyms_seen;
1fd400ff
TT
20597 int cu_index;
20598};
20599
20600/* A helper function that writes a single signatured_type to an
20601 obstack. */
b89be57b 20602
1fd400ff
TT
20603static int
20604write_one_signatured_type (void **slot, void *d)
20605{
20606 struct signatured_type_index_data *info = d;
20607 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 20608 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
20609 gdb_byte val[8];
20610
20611 write_psymbols (info->symtab,
987d643c 20612 info->psyms_seen,
3e43a32a
MS
20613 info->objfile->global_psymbols.list
20614 + psymtab->globals_offset,
987d643c
TT
20615 psymtab->n_global_syms, info->cu_index,
20616 0);
1fd400ff 20617 write_psymbols (info->symtab,
987d643c 20618 info->psyms_seen,
3e43a32a
MS
20619 info->objfile->static_psymbols.list
20620 + psymtab->statics_offset,
987d643c
TT
20621 psymtab->n_static_syms, info->cu_index,
20622 1);
1fd400ff 20623
b64f50a1
JK
20624 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20625 entry->per_cu.offset.sect_off);
1fd400ff 20626 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20627 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20628 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20629 obstack_grow (info->types_list, val, 8);
20630 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20631 obstack_grow (info->types_list, val, 8);
20632
20633 ++info->cu_index;
20634
20635 return 1;
20636}
20637
95554aad
TT
20638/* Recurse into all "included" dependencies and write their symbols as
20639 if they appeared in this psymtab. */
20640
20641static void
20642recursively_write_psymbols (struct objfile *objfile,
20643 struct partial_symtab *psymtab,
20644 struct mapped_symtab *symtab,
20645 htab_t psyms_seen,
20646 offset_type cu_index)
20647{
20648 int i;
20649
20650 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20651 if (psymtab->dependencies[i]->user != NULL)
20652 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20653 symtab, psyms_seen, cu_index);
20654
20655 write_psymbols (symtab,
20656 psyms_seen,
20657 objfile->global_psymbols.list + psymtab->globals_offset,
20658 psymtab->n_global_syms, cu_index,
20659 0);
20660 write_psymbols (symtab,
20661 psyms_seen,
20662 objfile->static_psymbols.list + psymtab->statics_offset,
20663 psymtab->n_static_syms, cu_index,
20664 1);
20665}
20666
9291a0cd 20667/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20668
9291a0cd
TT
20669static void
20670write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20671{
20672 struct cleanup *cleanup;
20673 char *filename, *cleanup_filename;
1fd400ff
TT
20674 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20675 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20676 int i;
20677 FILE *out_file;
20678 struct mapped_symtab *symtab;
20679 offset_type val, size_of_contents, total_len;
20680 struct stat st;
987d643c 20681 htab_t psyms_seen;
0a5429f6
DE
20682 htab_t cu_index_htab;
20683 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20684
b4f2f049 20685 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20686 return;
b4f2f049 20687
9291a0cd
TT
20688 if (dwarf2_per_objfile->using_index)
20689 error (_("Cannot use an index to create the index"));
20690
8b70b953
TT
20691 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20692 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20693
9291a0cd 20694 if (stat (objfile->name, &st) < 0)
7e17e088 20695 perror_with_name (objfile->name);
9291a0cd
TT
20696
20697 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20698 INDEX_SUFFIX, (char *) NULL);
20699 cleanup = make_cleanup (xfree, filename);
20700
20701 out_file = fopen (filename, "wb");
20702 if (!out_file)
20703 error (_("Can't open `%s' for writing"), filename);
20704
20705 cleanup_filename = filename;
20706 make_cleanup (unlink_if_set, &cleanup_filename);
20707
20708 symtab = create_mapped_symtab ();
20709 make_cleanup (cleanup_mapped_symtab, symtab);
20710
20711 obstack_init (&addr_obstack);
20712 make_cleanup_obstack_free (&addr_obstack);
20713
20714 obstack_init (&cu_list);
20715 make_cleanup_obstack_free (&cu_list);
20716
1fd400ff
TT
20717 obstack_init (&types_cu_list);
20718 make_cleanup_obstack_free (&types_cu_list);
20719
987d643c
TT
20720 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20721 NULL, xcalloc, xfree);
96408a79 20722 make_cleanup_htab_delete (psyms_seen);
987d643c 20723
0a5429f6
DE
20724 /* While we're scanning CU's create a table that maps a psymtab pointer
20725 (which is what addrmap records) to its index (which is what is recorded
20726 in the index file). This will later be needed to write the address
20727 table. */
20728 cu_index_htab = htab_create_alloc (100,
20729 hash_psymtab_cu_index,
20730 eq_psymtab_cu_index,
20731 NULL, xcalloc, xfree);
96408a79 20732 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20733 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20734 xmalloc (sizeof (struct psymtab_cu_index_map)
20735 * dwarf2_per_objfile->n_comp_units);
20736 make_cleanup (xfree, psymtab_cu_index_map);
20737
20738 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20739 work here. Also, the debug_types entries do not appear in
20740 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20741 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20742 {
3e43a32a
MS
20743 struct dwarf2_per_cu_data *per_cu
20744 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20745 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20746 gdb_byte val[8];
0a5429f6
DE
20747 struct psymtab_cu_index_map *map;
20748 void **slot;
9291a0cd 20749
95554aad
TT
20750 if (psymtab->user == NULL)
20751 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20752
0a5429f6
DE
20753 map = &psymtab_cu_index_map[i];
20754 map->psymtab = psymtab;
20755 map->cu_index = i;
20756 slot = htab_find_slot (cu_index_htab, map, INSERT);
20757 gdb_assert (slot != NULL);
20758 gdb_assert (*slot == NULL);
20759 *slot = map;
9291a0cd 20760
b64f50a1
JK
20761 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20762 per_cu->offset.sect_off);
9291a0cd 20763 obstack_grow (&cu_list, val, 8);
e254ef6a 20764 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20765 obstack_grow (&cu_list, val, 8);
20766 }
20767
0a5429f6
DE
20768 /* Dump the address map. */
20769 write_address_map (objfile, &addr_obstack, cu_index_htab);
20770
1fd400ff
TT
20771 /* Write out the .debug_type entries, if any. */
20772 if (dwarf2_per_objfile->signatured_types)
20773 {
20774 struct signatured_type_index_data sig_data;
20775
20776 sig_data.objfile = objfile;
20777 sig_data.symtab = symtab;
20778 sig_data.types_list = &types_cu_list;
987d643c 20779 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20780 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20781 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20782 write_one_signatured_type, &sig_data);
20783 }
20784
156942c7
DE
20785 /* Now that we've processed all symbols we can shrink their cu_indices
20786 lists. */
20787 uniquify_cu_indices (symtab);
20788
9291a0cd
TT
20789 obstack_init (&constant_pool);
20790 make_cleanup_obstack_free (&constant_pool);
20791 obstack_init (&symtab_obstack);
20792 make_cleanup_obstack_free (&symtab_obstack);
20793 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20794
20795 obstack_init (&contents);
20796 make_cleanup_obstack_free (&contents);
1fd400ff 20797 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20798 total_len = size_of_contents;
20799
20800 /* The version number. */
796a7ff8 20801 val = MAYBE_SWAP (8);
9291a0cd
TT
20802 obstack_grow (&contents, &val, sizeof (val));
20803
20804 /* The offset of the CU list from the start of the file. */
20805 val = MAYBE_SWAP (total_len);
20806 obstack_grow (&contents, &val, sizeof (val));
20807 total_len += obstack_object_size (&cu_list);
20808
1fd400ff
TT
20809 /* The offset of the types CU list from the start of the file. */
20810 val = MAYBE_SWAP (total_len);
20811 obstack_grow (&contents, &val, sizeof (val));
20812 total_len += obstack_object_size (&types_cu_list);
20813
9291a0cd
TT
20814 /* The offset of the address table from the start of the file. */
20815 val = MAYBE_SWAP (total_len);
20816 obstack_grow (&contents, &val, sizeof (val));
20817 total_len += obstack_object_size (&addr_obstack);
20818
20819 /* The offset of the symbol table from the start of the file. */
20820 val = MAYBE_SWAP (total_len);
20821 obstack_grow (&contents, &val, sizeof (val));
20822 total_len += obstack_object_size (&symtab_obstack);
20823
20824 /* The offset of the constant pool from the start of the file. */
20825 val = MAYBE_SWAP (total_len);
20826 obstack_grow (&contents, &val, sizeof (val));
20827 total_len += obstack_object_size (&constant_pool);
20828
20829 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20830
20831 write_obstack (out_file, &contents);
20832 write_obstack (out_file, &cu_list);
1fd400ff 20833 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20834 write_obstack (out_file, &addr_obstack);
20835 write_obstack (out_file, &symtab_obstack);
20836 write_obstack (out_file, &constant_pool);
20837
20838 fclose (out_file);
20839
20840 /* We want to keep the file, so we set cleanup_filename to NULL
20841 here. See unlink_if_set. */
20842 cleanup_filename = NULL;
20843
20844 do_cleanups (cleanup);
20845}
20846
90476074
TT
20847/* Implementation of the `save gdb-index' command.
20848
20849 Note that the file format used by this command is documented in the
20850 GDB manual. Any changes here must be documented there. */
11570e71 20851
9291a0cd
TT
20852static void
20853save_gdb_index_command (char *arg, int from_tty)
20854{
20855 struct objfile *objfile;
20856
20857 if (!arg || !*arg)
96d19272 20858 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20859
20860 ALL_OBJFILES (objfile)
20861 {
20862 struct stat st;
20863
20864 /* If the objfile does not correspond to an actual file, skip it. */
20865 if (stat (objfile->name, &st) < 0)
20866 continue;
20867
20868 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20869 if (dwarf2_per_objfile)
20870 {
20871 volatile struct gdb_exception except;
20872
20873 TRY_CATCH (except, RETURN_MASK_ERROR)
20874 {
20875 write_psymtabs_to_index (objfile, arg);
20876 }
20877 if (except.reason < 0)
20878 exception_fprintf (gdb_stderr, except,
20879 _("Error while writing index for `%s': "),
20880 objfile->name);
20881 }
20882 }
dce234bc
PP
20883}
20884
9291a0cd
TT
20885\f
20886
9eae7c52
TT
20887int dwarf2_always_disassemble;
20888
20889static void
20890show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20891 struct cmd_list_element *c, const char *value)
20892{
3e43a32a
MS
20893 fprintf_filtered (file,
20894 _("Whether to always disassemble "
20895 "DWARF expressions is %s.\n"),
9eae7c52
TT
20896 value);
20897}
20898
900e11f9
JK
20899static void
20900show_check_physname (struct ui_file *file, int from_tty,
20901 struct cmd_list_element *c, const char *value)
20902{
20903 fprintf_filtered (file,
20904 _("Whether to check \"physname\" is %s.\n"),
20905 value);
20906}
20907
6502dd73
DJ
20908void _initialize_dwarf2_read (void);
20909
20910void
20911_initialize_dwarf2_read (void)
20912{
96d19272
JK
20913 struct cmd_list_element *c;
20914
dce234bc 20915 dwarf2_objfile_data_key
c1bd65d0 20916 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20917
1bedd215
AC
20918 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20919Set DWARF 2 specific variables.\n\
20920Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20921 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20922 0/*allow-unknown*/, &maintenance_set_cmdlist);
20923
1bedd215
AC
20924 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20925Show DWARF 2 specific variables\n\
20926Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20927 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20928 0/*allow-unknown*/, &maintenance_show_cmdlist);
20929
20930 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20931 &dwarf2_max_cache_age, _("\
20932Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20933Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20934A higher limit means that cached compilation units will be stored\n\
20935in memory longer, and more total memory will be used. Zero disables\n\
20936caching, which can slow down startup."),
2c5b56ce 20937 NULL,
920d2a44 20938 show_dwarf2_max_cache_age,
2c5b56ce 20939 &set_dwarf2_cmdlist,
ae038cb0 20940 &show_dwarf2_cmdlist);
d97bc12b 20941
9eae7c52
TT
20942 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20943 &dwarf2_always_disassemble, _("\
20944Set whether `info address' always disassembles DWARF expressions."), _("\
20945Show whether `info address' always disassembles DWARF expressions."), _("\
20946When enabled, DWARF expressions are always printed in an assembly-like\n\
20947syntax. When disabled, expressions will be printed in a more\n\
20948conversational style, when possible."),
20949 NULL,
20950 show_dwarf2_always_disassemble,
20951 &set_dwarf2_cmdlist,
20952 &show_dwarf2_cmdlist);
20953
45cfd468
DE
20954 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20955Set debugging of the dwarf2 reader."), _("\
20956Show debugging of the dwarf2 reader."), _("\
20957When enabled, debugging messages are printed during dwarf2 reading\n\
20958and symtab expansion."),
20959 NULL,
20960 NULL,
20961 &setdebuglist, &showdebuglist);
20962
ccce17b0 20963 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20964Set debugging of the dwarf2 DIE reader."), _("\
20965Show debugging of the dwarf2 DIE reader."), _("\
20966When enabled (non-zero), DIEs are dumped after they are read in.\n\
20967The value is the maximum depth to print."),
ccce17b0
YQ
20968 NULL,
20969 NULL,
20970 &setdebuglist, &showdebuglist);
9291a0cd 20971
900e11f9
JK
20972 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20973Set cross-checking of \"physname\" code against demangler."), _("\
20974Show cross-checking of \"physname\" code against demangler."), _("\
20975When enabled, GDB's internal \"physname\" code is checked against\n\
20976the demangler."),
20977 NULL, show_check_physname,
20978 &setdebuglist, &showdebuglist);
20979
e615022a
DE
20980 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20981 no_class, &use_deprecated_index_sections, _("\
20982Set whether to use deprecated gdb_index sections."), _("\
20983Show whether to use deprecated gdb_index sections."), _("\
20984When enabled, deprecated .gdb_index sections are used anyway.\n\
20985Normally they are ignored either because of a missing feature or\n\
20986performance issue.\n\
20987Warning: This option must be enabled before gdb reads the file."),
20988 NULL,
20989 NULL,
20990 &setlist, &showlist);
20991
96d19272 20992 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20993 _("\
fc1a9d6e 20994Save a gdb-index file.\n\
11570e71 20995Usage: save gdb-index DIRECTORY"),
96d19272
JK
20996 &save_cmdlist);
20997 set_cmd_completer (c, filename_completer);
f1e6e072
TT
20998
20999 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21000 &dwarf2_locexpr_funcs);
21001 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21002 &dwarf2_loclist_funcs);
21003
21004 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21005 &dwarf2_block_frame_base_locexpr_funcs);
21006 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21007 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21008}
This page took 2.738653 seconds and 4 git commands to generate.