Update copyright year on elf-ifunc.c
[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
DE
603 /* The "per_cu" object of this type.
604 N.B.: This is the first member so that it's easy to convert pointers
605 between them. */
606 struct dwarf2_per_cu_data per_cu;
607
3019eac3 608 /* The type's signature. */
348e048f
DE
609 ULONGEST signature;
610
3019eac3 611 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
612 If this TU is a DWO stub and the definition lives in a DWO file
613 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
614 cu_offset type_offset_in_tu;
615
616 /* Offset in the section of the type's DIE.
617 If the definition lives in a DWO file, this is the offset in the
618 .debug_types.dwo section.
619 The value is zero until the actual value is known.
620 Zero is otherwise not a valid section offset. */
621 sect_offset type_offset_in_section;
0186c6a7
DE
622
623 /* Type units are grouped by their DW_AT_stmt_list entry so that they
624 can share them. This points to the containing symtab. */
625 struct type_unit_group *type_unit_group;
348e048f
DE
626};
627
0186c6a7
DE
628typedef struct signatured_type *sig_type_ptr;
629DEF_VEC_P (sig_type_ptr);
630
094b34ac
DE
631/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
632 This includes type_unit_group and quick_file_names. */
633
634struct stmt_list_hash
635{
636 /* The DWO unit this table is from or NULL if there is none. */
637 struct dwo_unit *dwo_unit;
638
639 /* Offset in .debug_line or .debug_line.dwo. */
640 sect_offset line_offset;
641};
642
f4dc4d17
DE
643/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
644 an object of this type. */
645
646struct type_unit_group
647{
0186c6a7 648 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
649 To simplify things we create an artificial CU that "includes" all the
650 type units using this stmt_list so that the rest of the code still has
651 a "per_cu" handle on the symtab.
652 This PER_CU is recognized by having no section. */
8a0459fd 653#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
654 struct dwarf2_per_cu_data per_cu;
655
0186c6a7
DE
656 /* The TUs that share this DW_AT_stmt_list entry.
657 This is added to while parsing type units to build partial symtabs,
658 and is deleted afterwards and not used again. */
659 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
660
661 /* The primary symtab.
094b34ac
DE
662 Type units in a group needn't all be defined in the same source file,
663 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
664 struct symtab *primary_symtab;
665
094b34ac
DE
666 /* The data used to construct the hash key. */
667 struct stmt_list_hash hash;
f4dc4d17
DE
668
669 /* The number of symtabs from the line header.
670 The value here must match line_header.num_file_names. */
671 unsigned int num_symtabs;
672
673 /* The symbol tables for this TU (obtained from the files listed in
674 DW_AT_stmt_list).
675 WARNING: The order of entries here must match the order of entries
676 in the line header. After the first TU using this type_unit_group, the
677 line header for the subsequent TUs is recreated from this. This is done
678 because we need to use the same symtabs for each TU using the same
679 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
680 there's no guarantee the line header doesn't have duplicate entries. */
681 struct symtab **symtabs;
682};
683
80626a55 684/* These sections are what may appear in a DWO file. */
3019eac3
DE
685
686struct dwo_sections
687{
688 struct dwarf2_section_info abbrev;
3019eac3
DE
689 struct dwarf2_section_info line;
690 struct dwarf2_section_info loc;
09262596
DE
691 struct dwarf2_section_info macinfo;
692 struct dwarf2_section_info macro;
3019eac3
DE
693 struct dwarf2_section_info str;
694 struct dwarf2_section_info str_offsets;
80626a55
DE
695 /* In the case of a virtual DWO file, these two are unused. */
696 struct dwarf2_section_info info;
3019eac3
DE
697 VEC (dwarf2_section_info_def) *types;
698};
699
c88ee1f0 700/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
701
702struct dwo_unit
703{
704 /* Backlink to the containing struct dwo_file. */
705 struct dwo_file *dwo_file;
706
707 /* The "id" that distinguishes this CU/TU.
708 .debug_info calls this "dwo_id", .debug_types calls this "signature".
709 Since signatures came first, we stick with it for consistency. */
710 ULONGEST signature;
711
712 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 713 struct dwarf2_section_info *section;
3019eac3
DE
714
715 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
716 sect_offset offset;
717 unsigned int length;
718
719 /* For types, offset in the type's DIE of the type defined by this TU. */
720 cu_offset type_offset_in_tu;
721};
722
80626a55
DE
723/* Data for one DWO file.
724 This includes virtual DWO files that have been packaged into a
725 DWP file. */
3019eac3
DE
726
727struct dwo_file
728{
0ac5b59e 729 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
730 For virtual DWO files the name is constructed from the section offsets
731 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
732 from related CU+TUs. */
0ac5b59e
DE
733 const char *dwo_name;
734
735 /* The DW_AT_comp_dir attribute. */
736 const char *comp_dir;
3019eac3 737
80626a55
DE
738 /* The bfd, when the file is open. Otherwise this is NULL.
739 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
740 bfd *dbfd;
3019eac3
DE
741
742 /* Section info for this file. */
743 struct dwo_sections sections;
744
19c3d4c9
DE
745 /* The CU in the file.
746 We only support one because having more than one requires hacking the
747 dwo_name of each to match, which is highly unlikely to happen.
748 Doing this means all TUs can share comp_dir: We also assume that
749 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
750 struct dwo_unit *cu;
3019eac3
DE
751
752 /* Table of TUs in the file.
753 Each element is a struct dwo_unit. */
754 htab_t tus;
755};
756
80626a55
DE
757/* These sections are what may appear in a DWP file. */
758
759struct dwp_sections
760{
761 struct dwarf2_section_info str;
762 struct dwarf2_section_info cu_index;
763 struct dwarf2_section_info tu_index;
764 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
765 by section number. We don't need to record them here. */
766};
767
768/* These sections are what may appear in a virtual DWO file. */
769
770struct virtual_dwo_sections
771{
772 struct dwarf2_section_info abbrev;
773 struct dwarf2_section_info line;
774 struct dwarf2_section_info loc;
775 struct dwarf2_section_info macinfo;
776 struct dwarf2_section_info macro;
777 struct dwarf2_section_info str_offsets;
778 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 779 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
780 struct dwarf2_section_info info_or_types;
781};
782
783/* Contents of DWP hash tables. */
784
785struct dwp_hash_table
786{
787 uint32_t nr_units, nr_slots;
788 const gdb_byte *hash_table, *unit_table, *section_pool;
789};
790
791/* Data for one DWP file. */
792
793struct dwp_file
794{
795 /* Name of the file. */
796 const char *name;
797
798 /* The bfd, when the file is open. Otherwise this is NULL. */
799 bfd *dbfd;
800
801 /* Section info for this file. */
802 struct dwp_sections sections;
803
804 /* Table of CUs in the file. */
805 const struct dwp_hash_table *cus;
806
807 /* Table of TUs in the file. */
808 const struct dwp_hash_table *tus;
809
810 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
811 htab_t loaded_cutus;
812
813 /* Table to map ELF section numbers to their sections. */
814 unsigned int num_sections;
815 asection **elf_sections;
816};
817
36586728
TT
818/* This represents a '.dwz' file. */
819
820struct dwz_file
821{
822 /* A dwz file can only contain a few sections. */
823 struct dwarf2_section_info abbrev;
824 struct dwarf2_section_info info;
825 struct dwarf2_section_info str;
826 struct dwarf2_section_info line;
827 struct dwarf2_section_info macro;
2ec9a5e0 828 struct dwarf2_section_info gdb_index;
36586728
TT
829
830 /* The dwz's BFD. */
831 bfd *dwz_bfd;
832};
833
0963b4bd
MS
834/* Struct used to pass misc. parameters to read_die_and_children, et
835 al. which are used for both .debug_info and .debug_types dies.
836 All parameters here are unchanging for the life of the call. This
dee91e82 837 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
838
839struct die_reader_specs
840{
dee91e82 841 /* die_section->asection->owner. */
93311388
DE
842 bfd* abfd;
843
844 /* The CU of the DIE we are parsing. */
845 struct dwarf2_cu *cu;
846
80626a55 847 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
848 struct dwo_file *dwo_file;
849
dee91e82 850 /* The section the die comes from.
3019eac3 851 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
852 struct dwarf2_section_info *die_section;
853
854 /* die_section->buffer. */
d521ce57 855 const gdb_byte *buffer;
f664829e
DE
856
857 /* The end of the buffer. */
858 const gdb_byte *buffer_end;
93311388
DE
859};
860
fd820528 861/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 862typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 863 const gdb_byte *info_ptr,
dee91e82
DE
864 struct die_info *comp_unit_die,
865 int has_children,
866 void *data);
867
debd256d
JB
868/* The line number information for a compilation unit (found in the
869 .debug_line section) begins with a "statement program header",
870 which contains the following information. */
871struct line_header
872{
873 unsigned int total_length;
874 unsigned short version;
875 unsigned int header_length;
876 unsigned char minimum_instruction_length;
2dc7f7b3 877 unsigned char maximum_ops_per_instruction;
debd256d
JB
878 unsigned char default_is_stmt;
879 int line_base;
880 unsigned char line_range;
881 unsigned char opcode_base;
882
883 /* standard_opcode_lengths[i] is the number of operands for the
884 standard opcode whose value is i. This means that
885 standard_opcode_lengths[0] is unused, and the last meaningful
886 element is standard_opcode_lengths[opcode_base - 1]. */
887 unsigned char *standard_opcode_lengths;
888
889 /* The include_directories table. NOTE! These strings are not
890 allocated with xmalloc; instead, they are pointers into
891 debug_line_buffer. If you try to free them, `free' will get
892 indigestion. */
893 unsigned int num_include_dirs, include_dirs_size;
d521ce57 894 const char **include_dirs;
debd256d
JB
895
896 /* The file_names table. NOTE! These strings are not allocated
897 with xmalloc; instead, they are pointers into debug_line_buffer.
898 Don't try to free them directly. */
899 unsigned int num_file_names, file_names_size;
900 struct file_entry
c906108c 901 {
d521ce57 902 const char *name;
debd256d
JB
903 unsigned int dir_index;
904 unsigned int mod_time;
905 unsigned int length;
aaa75496 906 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 907 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
908 } *file_names;
909
910 /* The start and end of the statement program following this
6502dd73 911 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 912 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 913};
c906108c
SS
914
915/* When we construct a partial symbol table entry we only
0963b4bd 916 need this much information. */
c906108c
SS
917struct partial_die_info
918 {
72bf9492 919 /* Offset of this DIE. */
b64f50a1 920 sect_offset offset;
72bf9492
DJ
921
922 /* DWARF-2 tag for this DIE. */
923 ENUM_BITFIELD(dwarf_tag) tag : 16;
924
72bf9492
DJ
925 /* Assorted flags describing the data found in this DIE. */
926 unsigned int has_children : 1;
927 unsigned int is_external : 1;
928 unsigned int is_declaration : 1;
929 unsigned int has_type : 1;
930 unsigned int has_specification : 1;
931 unsigned int has_pc_info : 1;
481860b3 932 unsigned int may_be_inlined : 1;
72bf9492
DJ
933
934 /* Flag set if the SCOPE field of this structure has been
935 computed. */
936 unsigned int scope_set : 1;
937
fa4028e9
JB
938 /* Flag set if the DIE has a byte_size attribute. */
939 unsigned int has_byte_size : 1;
940
98bfdba5
PA
941 /* Flag set if any of the DIE's children are template arguments. */
942 unsigned int has_template_arguments : 1;
943
abc72ce4
DE
944 /* Flag set if fixup_partial_die has been called on this die. */
945 unsigned int fixup_called : 1;
946
36586728
TT
947 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
948 unsigned int is_dwz : 1;
949
950 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
951 unsigned int spec_is_dwz : 1;
952
72bf9492 953 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 954 sometimes a default name for unnamed DIEs. */
15d034d0 955 const char *name;
72bf9492 956
abc72ce4
DE
957 /* The linkage name, if present. */
958 const char *linkage_name;
959
72bf9492
DJ
960 /* The scope to prepend to our children. This is generally
961 allocated on the comp_unit_obstack, so will disappear
962 when this compilation unit leaves the cache. */
15d034d0 963 const char *scope;
72bf9492 964
95554aad
TT
965 /* Some data associated with the partial DIE. The tag determines
966 which field is live. */
967 union
968 {
969 /* The location description associated with this DIE, if any. */
970 struct dwarf_block *locdesc;
971 /* The offset of an import, for DW_TAG_imported_unit. */
972 sect_offset offset;
973 } d;
72bf9492
DJ
974
975 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
976 CORE_ADDR lowpc;
977 CORE_ADDR highpc;
72bf9492 978
93311388 979 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 980 DW_AT_sibling, if any. */
abc72ce4
DE
981 /* NOTE: This member isn't strictly necessary, read_partial_die could
982 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 983 const gdb_byte *sibling;
72bf9492
DJ
984
985 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
986 DW_AT_specification (or DW_AT_abstract_origin or
987 DW_AT_extension). */
b64f50a1 988 sect_offset spec_offset;
72bf9492
DJ
989
990 /* Pointers to this DIE's parent, first child, and next sibling,
991 if any. */
992 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
993 };
994
0963b4bd 995/* This data structure holds the information of an abbrev. */
c906108c
SS
996struct abbrev_info
997 {
998 unsigned int number; /* number identifying abbrev */
999 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1000 unsigned short has_children; /* boolean */
1001 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1002 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1003 struct abbrev_info *next; /* next in chain */
1004 };
1005
1006struct attr_abbrev
1007 {
9d25dd43
DE
1008 ENUM_BITFIELD(dwarf_attribute) name : 16;
1009 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1010 };
1011
433df2d4
DE
1012/* Size of abbrev_table.abbrev_hash_table. */
1013#define ABBREV_HASH_SIZE 121
1014
1015/* Top level data structure to contain an abbreviation table. */
1016
1017struct abbrev_table
1018{
f4dc4d17
DE
1019 /* Where the abbrev table came from.
1020 This is used as a sanity check when the table is used. */
433df2d4
DE
1021 sect_offset offset;
1022
1023 /* Storage for the abbrev table. */
1024 struct obstack abbrev_obstack;
1025
1026 /* Hash table of abbrevs.
1027 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1028 It could be statically allocated, but the previous code didn't so we
1029 don't either. */
1030 struct abbrev_info **abbrevs;
1031};
1032
0963b4bd 1033/* Attributes have a name and a value. */
b60c80d6
DJ
1034struct attribute
1035 {
9d25dd43 1036 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1037 ENUM_BITFIELD(dwarf_form) form : 15;
1038
1039 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1040 field should be in u.str (existing only for DW_STRING) but it is kept
1041 here for better struct attribute alignment. */
1042 unsigned int string_is_canonical : 1;
1043
b60c80d6
DJ
1044 union
1045 {
15d034d0 1046 const char *str;
b60c80d6 1047 struct dwarf_block *blk;
43bbcdc2
PH
1048 ULONGEST unsnd;
1049 LONGEST snd;
b60c80d6 1050 CORE_ADDR addr;
348e048f 1051 struct signatured_type *signatured_type;
b60c80d6
DJ
1052 }
1053 u;
1054 };
1055
0963b4bd 1056/* This data structure holds a complete die structure. */
c906108c
SS
1057struct die_info
1058 {
76815b17
DE
1059 /* DWARF-2 tag for this DIE. */
1060 ENUM_BITFIELD(dwarf_tag) tag : 16;
1061
1062 /* Number of attributes */
98bfdba5
PA
1063 unsigned char num_attrs;
1064
1065 /* True if we're presently building the full type name for the
1066 type derived from this DIE. */
1067 unsigned char building_fullname : 1;
76815b17
DE
1068
1069 /* Abbrev number */
1070 unsigned int abbrev;
1071
93311388 1072 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1073 sect_offset offset;
78ba4af6
JB
1074
1075 /* The dies in a compilation unit form an n-ary tree. PARENT
1076 points to this die's parent; CHILD points to the first child of
1077 this node; and all the children of a given node are chained
4950bc1c 1078 together via their SIBLING fields. */
639d11d3
DC
1079 struct die_info *child; /* Its first child, if any. */
1080 struct die_info *sibling; /* Its next sibling, if any. */
1081 struct die_info *parent; /* Its parent, if any. */
c906108c 1082
b60c80d6
DJ
1083 /* An array of attributes, with NUM_ATTRS elements. There may be
1084 zero, but it's not common and zero-sized arrays are not
1085 sufficiently portable C. */
1086 struct attribute attrs[1];
c906108c
SS
1087 };
1088
0963b4bd 1089/* Get at parts of an attribute structure. */
c906108c
SS
1090
1091#define DW_STRING(attr) ((attr)->u.str)
8285870a 1092#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1093#define DW_UNSND(attr) ((attr)->u.unsnd)
1094#define DW_BLOCK(attr) ((attr)->u.blk)
1095#define DW_SND(attr) ((attr)->u.snd)
1096#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1097#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1098
0963b4bd 1099/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1100struct dwarf_block
1101 {
56eb65bd 1102 size_t size;
1d6edc3c
JK
1103
1104 /* Valid only if SIZE is not zero. */
d521ce57 1105 const gdb_byte *data;
c906108c
SS
1106 };
1107
c906108c
SS
1108#ifndef ATTR_ALLOC_CHUNK
1109#define ATTR_ALLOC_CHUNK 4
1110#endif
1111
c906108c
SS
1112/* Allocate fields for structs, unions and enums in this size. */
1113#ifndef DW_FIELD_ALLOC_CHUNK
1114#define DW_FIELD_ALLOC_CHUNK 4
1115#endif
1116
c906108c
SS
1117/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1118 but this would require a corresponding change in unpack_field_as_long
1119 and friends. */
1120static int bits_per_byte = 8;
1121
1122/* The routines that read and process dies for a C struct or C++ class
1123 pass lists of data member fields and lists of member function fields
1124 in an instance of a field_info structure, as defined below. */
1125struct field_info
c5aa993b 1126 {
0963b4bd 1127 /* List of data member and baseclasses fields. */
c5aa993b
JM
1128 struct nextfield
1129 {
1130 struct nextfield *next;
1131 int accessibility;
1132 int virtuality;
1133 struct field field;
1134 }
7d0ccb61 1135 *fields, *baseclasses;
c906108c 1136
7d0ccb61 1137 /* Number of fields (including baseclasses). */
c5aa993b 1138 int nfields;
c906108c 1139
c5aa993b
JM
1140 /* Number of baseclasses. */
1141 int nbaseclasses;
c906108c 1142
c5aa993b
JM
1143 /* Set if the accesibility of one of the fields is not public. */
1144 int non_public_fields;
c906108c 1145
c5aa993b
JM
1146 /* Member function fields array, entries are allocated in the order they
1147 are encountered in the object file. */
1148 struct nextfnfield
1149 {
1150 struct nextfnfield *next;
1151 struct fn_field fnfield;
1152 }
1153 *fnfields;
c906108c 1154
c5aa993b
JM
1155 /* Member function fieldlist array, contains name of possibly overloaded
1156 member function, number of overloaded member functions and a pointer
1157 to the head of the member function field chain. */
1158 struct fnfieldlist
1159 {
15d034d0 1160 const char *name;
c5aa993b
JM
1161 int length;
1162 struct nextfnfield *head;
1163 }
1164 *fnfieldlists;
c906108c 1165
c5aa993b
JM
1166 /* Number of entries in the fnfieldlists array. */
1167 int nfnfields;
98751a41
JK
1168
1169 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1170 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1171 struct typedef_field_list
1172 {
1173 struct typedef_field field;
1174 struct typedef_field_list *next;
1175 }
1176 *typedef_field_list;
1177 unsigned typedef_field_list_count;
c5aa993b 1178 };
c906108c 1179
10b3939b
DJ
1180/* One item on the queue of compilation units to read in full symbols
1181 for. */
1182struct dwarf2_queue_item
1183{
1184 struct dwarf2_per_cu_data *per_cu;
95554aad 1185 enum language pretend_language;
10b3939b
DJ
1186 struct dwarf2_queue_item *next;
1187};
1188
1189/* The current queue. */
1190static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1191
ae038cb0
DJ
1192/* Loaded secondary compilation units are kept in memory until they
1193 have not been referenced for the processing of this many
1194 compilation units. Set this to zero to disable caching. Cache
1195 sizes of up to at least twenty will improve startup time for
1196 typical inter-CU-reference binaries, at an obvious memory cost. */
1197static int dwarf2_max_cache_age = 5;
920d2a44
AC
1198static void
1199show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1200 struct cmd_list_element *c, const char *value)
1201{
3e43a32a
MS
1202 fprintf_filtered (file, _("The upper bound on the age of cached "
1203 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1204 value);
1205}
1206
ae038cb0 1207
0963b4bd 1208/* Various complaints about symbol reading that don't abort the process. */
c906108c 1209
4d3c2250
KB
1210static void
1211dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1212{
4d3c2250 1213 complaint (&symfile_complaints,
e2e0b3e5 1214 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1215}
1216
25e43795
DJ
1217static void
1218dwarf2_debug_line_missing_file_complaint (void)
1219{
1220 complaint (&symfile_complaints,
1221 _(".debug_line section has line data without a file"));
1222}
1223
59205f5a
JB
1224static void
1225dwarf2_debug_line_missing_end_sequence_complaint (void)
1226{
1227 complaint (&symfile_complaints,
3e43a32a
MS
1228 _(".debug_line section has line "
1229 "program sequence without an end"));
59205f5a
JB
1230}
1231
4d3c2250
KB
1232static void
1233dwarf2_complex_location_expr_complaint (void)
2e276125 1234{
e2e0b3e5 1235 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1236}
1237
4d3c2250
KB
1238static void
1239dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1240 int arg3)
2e276125 1241{
4d3c2250 1242 complaint (&symfile_complaints,
3e43a32a
MS
1243 _("const value length mismatch for '%s', got %d, expected %d"),
1244 arg1, arg2, arg3);
4d3c2250
KB
1245}
1246
1247static void
f664829e 1248dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1249{
4d3c2250 1250 complaint (&symfile_complaints,
f664829e
DE
1251 _("debug info runs off end of %s section"
1252 " [in module %s]"),
1253 section->asection->name,
1254 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1255}
1256
1257static void
1258dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1259{
4d3c2250 1260 complaint (&symfile_complaints,
3e43a32a
MS
1261 _("macro debug info contains a "
1262 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1263 arg1);
1264}
1265
1266static void
1267dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1268{
4d3c2250 1269 complaint (&symfile_complaints,
3e43a32a
MS
1270 _("invalid attribute class or form for '%s' in '%s'"),
1271 arg1, arg2);
4d3c2250 1272}
c906108c 1273
c906108c
SS
1274/* local function prototypes */
1275
4efb68b1 1276static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1277
918dd910
JK
1278static void dwarf2_find_base_address (struct die_info *die,
1279 struct dwarf2_cu *cu);
1280
0018ea6f
DE
1281static struct partial_symtab *create_partial_symtab
1282 (struct dwarf2_per_cu_data *per_cu, const char *name);
1283
c67a9c90 1284static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1285
72bf9492
DJ
1286static void scan_partial_symbols (struct partial_die_info *,
1287 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1288 int, struct dwarf2_cu *);
c906108c 1289
72bf9492
DJ
1290static void add_partial_symbol (struct partial_die_info *,
1291 struct dwarf2_cu *);
63d06c5c 1292
72bf9492
DJ
1293static void add_partial_namespace (struct partial_die_info *pdi,
1294 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1295 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1296
5d7cb8df
JK
1297static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1298 CORE_ADDR *highpc, int need_pc,
1299 struct dwarf2_cu *cu);
1300
72bf9492
DJ
1301static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1302 struct dwarf2_cu *cu);
91c24f0a 1303
bc30ff58
JB
1304static void add_partial_subprogram (struct partial_die_info *pdi,
1305 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1306 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1307
257e7a09
YQ
1308static void dwarf2_read_symtab (struct partial_symtab *,
1309 struct objfile *);
c906108c 1310
a14ed312 1311static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1312
433df2d4
DE
1313static struct abbrev_info *abbrev_table_lookup_abbrev
1314 (const struct abbrev_table *, unsigned int);
1315
1316static struct abbrev_table *abbrev_table_read_table
1317 (struct dwarf2_section_info *, sect_offset);
1318
1319static void abbrev_table_free (struct abbrev_table *);
1320
f4dc4d17
DE
1321static void abbrev_table_free_cleanup (void *);
1322
dee91e82
DE
1323static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1324 struct dwarf2_section_info *);
c906108c 1325
f3dd6933 1326static void dwarf2_free_abbrev_table (void *);
c906108c 1327
d521ce57 1328static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1329
dee91e82 1330static struct partial_die_info *load_partial_dies
d521ce57 1331 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1332
d521ce57
TT
1333static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1334 struct partial_die_info *,
1335 struct abbrev_info *,
1336 unsigned int,
1337 const gdb_byte *);
c906108c 1338
36586728 1339static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1340 struct dwarf2_cu *);
72bf9492
DJ
1341
1342static void fixup_partial_die (struct partial_die_info *,
1343 struct dwarf2_cu *);
1344
d521ce57
TT
1345static const gdb_byte *read_attribute (const struct die_reader_specs *,
1346 struct attribute *, struct attr_abbrev *,
1347 const gdb_byte *);
a8329558 1348
a1855c1d 1349static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1350
a1855c1d 1351static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1352
a1855c1d 1353static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1354
a1855c1d 1355static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1356
a1855c1d 1357static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1358
d521ce57 1359static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1360 unsigned int *);
c906108c 1361
d521ce57 1362static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1363
1364static LONGEST read_checked_initial_length_and_offset
d521ce57 1365 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1366 unsigned int *, unsigned int *);
613e1657 1367
d521ce57
TT
1368static LONGEST read_offset (bfd *, const gdb_byte *,
1369 const struct comp_unit_head *,
c764a876
DE
1370 unsigned int *);
1371
d521ce57 1372static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1373
f4dc4d17
DE
1374static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1375 sect_offset);
1376
d521ce57 1377static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1378
d521ce57 1379static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1380
d521ce57
TT
1381static const char *read_indirect_string (bfd *, const gdb_byte *,
1382 const struct comp_unit_head *,
1383 unsigned int *);
4bdf3d34 1384
d521ce57 1385static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1386
d521ce57 1387static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1388
d521ce57 1389static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1390
d521ce57
TT
1391static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1392 const gdb_byte *,
3019eac3
DE
1393 unsigned int *);
1394
d521ce57
TT
1395static const char *read_str_index (const struct die_reader_specs *reader,
1396 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1397
e142c38c 1398static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1399
e142c38c
DJ
1400static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1401 struct dwarf2_cu *);
c906108c 1402
348e048f 1403static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1404 unsigned int);
348e048f 1405
05cf31d1
JB
1406static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1407 struct dwarf2_cu *cu);
1408
e142c38c 1409static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1410
e142c38c 1411static struct die_info *die_specification (struct die_info *die,
f2f0e013 1412 struct dwarf2_cu **);
63d06c5c 1413
debd256d
JB
1414static void free_line_header (struct line_header *lh);
1415
3019eac3
DE
1416static struct line_header *dwarf_decode_line_header (unsigned int offset,
1417 struct dwarf2_cu *cu);
debd256d 1418
f3f5162e
DE
1419static void dwarf_decode_lines (struct line_header *, const char *,
1420 struct dwarf2_cu *, struct partial_symtab *,
1421 int);
c906108c 1422
d521ce57 1423static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1424
f4dc4d17 1425static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1426 const char *, const char *, CORE_ADDR);
f4dc4d17 1427
a14ed312 1428static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1429 struct dwarf2_cu *);
c906108c 1430
34eaf542
TT
1431static struct symbol *new_symbol_full (struct die_info *, struct type *,
1432 struct dwarf2_cu *, struct symbol *);
1433
a14ed312 1434static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1435 struct dwarf2_cu *);
c906108c 1436
98bfdba5
PA
1437static void dwarf2_const_value_attr (struct attribute *attr,
1438 struct type *type,
1439 const char *name,
1440 struct obstack *obstack,
12df843f 1441 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1442 const gdb_byte **bytes,
98bfdba5 1443 struct dwarf2_locexpr_baton **baton);
2df3850c 1444
e7c27a73 1445static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1446
b4ba55a1
JB
1447static int need_gnat_info (struct dwarf2_cu *);
1448
3e43a32a
MS
1449static struct type *die_descriptive_type (struct die_info *,
1450 struct dwarf2_cu *);
b4ba55a1
JB
1451
1452static void set_descriptive_type (struct type *, struct die_info *,
1453 struct dwarf2_cu *);
1454
e7c27a73
DJ
1455static struct type *die_containing_type (struct die_info *,
1456 struct dwarf2_cu *);
c906108c 1457
673bfd45
DE
1458static struct type *lookup_die_type (struct die_info *, struct attribute *,
1459 struct dwarf2_cu *);
c906108c 1460
f792889a 1461static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1462
673bfd45
DE
1463static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1464
0d5cff50 1465static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1466
6e70227d 1467static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1468 const char *suffix, int physname,
1469 struct dwarf2_cu *cu);
63d06c5c 1470
e7c27a73 1471static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1472
348e048f
DE
1473static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1474
e7c27a73 1475static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1476
e7c27a73 1477static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1478
96408a79
SA
1479static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1480
ff013f42
JK
1481static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1482 struct dwarf2_cu *, struct partial_symtab *);
1483
a14ed312 1484static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1485 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1486 struct partial_symtab *);
c906108c 1487
fae299cd
DC
1488static void get_scope_pc_bounds (struct die_info *,
1489 CORE_ADDR *, CORE_ADDR *,
1490 struct dwarf2_cu *);
1491
801e3a5b
JB
1492static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1493 CORE_ADDR, struct dwarf2_cu *);
1494
a14ed312 1495static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1496 struct dwarf2_cu *);
c906108c 1497
a14ed312 1498static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1499 struct type *, struct dwarf2_cu *);
c906108c 1500
a14ed312 1501static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1502 struct die_info *, struct type *,
e7c27a73 1503 struct dwarf2_cu *);
c906108c 1504
a14ed312 1505static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1506 struct type *,
1507 struct dwarf2_cu *);
c906108c 1508
134d01f1 1509static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1510
e7c27a73 1511static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1512
e7c27a73 1513static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1514
5d7cb8df
JK
1515static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1516
27aa8d6a
SW
1517static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
f55ee35c
JK
1519static struct type *read_module_type (struct die_info *die,
1520 struct dwarf2_cu *cu);
1521
38d518c9 1522static const char *namespace_name (struct die_info *die,
e142c38c 1523 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1524
134d01f1 1525static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1526
e7c27a73 1527static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1528
6e70227d 1529static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1530 struct dwarf2_cu *);
1531
bf6af496 1532static struct die_info *read_die_and_siblings_1
d521ce57 1533 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1534 struct die_info *);
639d11d3 1535
dee91e82 1536static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1537 const gdb_byte *info_ptr,
1538 const gdb_byte **new_info_ptr,
639d11d3
DC
1539 struct die_info *parent);
1540
d521ce57
TT
1541static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1542 struct die_info **, const gdb_byte *,
1543 int *, int);
3019eac3 1544
d521ce57
TT
1545static const gdb_byte *read_full_die (const struct die_reader_specs *,
1546 struct die_info **, const gdb_byte *,
1547 int *);
93311388 1548
e7c27a73 1549static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1550
15d034d0
TT
1551static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1552 struct obstack *);
71c25dea 1553
15d034d0 1554static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1555
15d034d0 1556static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1557 struct die_info *die,
1558 struct dwarf2_cu *cu);
1559
ca69b9e6
DE
1560static const char *dwarf2_physname (const char *name, struct die_info *die,
1561 struct dwarf2_cu *cu);
1562
e142c38c 1563static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1564 struct dwarf2_cu **);
9219021c 1565
f39c6ffd 1566static const char *dwarf_tag_name (unsigned int);
c906108c 1567
f39c6ffd 1568static const char *dwarf_attr_name (unsigned int);
c906108c 1569
f39c6ffd 1570static const char *dwarf_form_name (unsigned int);
c906108c 1571
a14ed312 1572static char *dwarf_bool_name (unsigned int);
c906108c 1573
f39c6ffd 1574static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1575
f9aca02d 1576static struct die_info *sibling_die (struct die_info *);
c906108c 1577
d97bc12b
DE
1578static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1579
1580static void dump_die_for_error (struct die_info *);
1581
1582static void dump_die_1 (struct ui_file *, int level, int max_level,
1583 struct die_info *);
c906108c 1584
d97bc12b 1585/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1586
51545339 1587static void store_in_ref_table (struct die_info *,
10b3939b 1588 struct dwarf2_cu *);
c906108c 1589
93311388
DE
1590static int is_ref_attr (struct attribute *);
1591
b64f50a1 1592static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1593
43bbcdc2 1594static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1595
348e048f
DE
1596static struct die_info *follow_die_ref_or_sig (struct die_info *,
1597 struct attribute *,
1598 struct dwarf2_cu **);
1599
10b3939b
DJ
1600static struct die_info *follow_die_ref (struct die_info *,
1601 struct attribute *,
f2f0e013 1602 struct dwarf2_cu **);
c906108c 1603
348e048f
DE
1604static struct die_info *follow_die_sig (struct die_info *,
1605 struct attribute *,
1606 struct dwarf2_cu **);
1607
e5fe5e75 1608static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1609
52dc124a 1610static void read_signatured_type (struct signatured_type *);
348e048f 1611
f4dc4d17 1612static struct type_unit_group *get_type_unit_group
094b34ac 1613 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1614
1615static void build_type_unit_groups (die_reader_func_ftype *, void *);
1616
c906108c
SS
1617/* memory allocation interface */
1618
7b5a2f43 1619static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1620
b60c80d6 1621static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1622
09262596 1623static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1624 const char *, int);
2e276125 1625
8e19ed76
PS
1626static int attr_form_is_block (struct attribute *);
1627
3690dd37
JB
1628static int attr_form_is_section_offset (struct attribute *);
1629
1630static int attr_form_is_constant (struct attribute *);
1631
8cf6f0b1
TT
1632static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1633 struct dwarf2_loclist_baton *baton,
1634 struct attribute *attr);
1635
93e7bd98
DJ
1636static void dwarf2_symbol_mark_computed (struct attribute *attr,
1637 struct symbol *sym,
f1e6e072
TT
1638 struct dwarf2_cu *cu,
1639 int is_block);
4c2df51b 1640
d521ce57
TT
1641static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1642 const gdb_byte *info_ptr,
1643 struct abbrev_info *abbrev);
4bb7a0a7 1644
72bf9492
DJ
1645static void free_stack_comp_unit (void *);
1646
72bf9492
DJ
1647static hashval_t partial_die_hash (const void *item);
1648
1649static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1650
ae038cb0 1651static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1652 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1653
9816fde3 1654static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1655 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1656
1657static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1658 struct die_info *comp_unit_die,
1659 enum language pretend_language);
93311388 1660
68dc6402 1661static void free_heap_comp_unit (void *);
ae038cb0
DJ
1662
1663static void free_cached_comp_units (void *);
1664
1665static void age_cached_comp_units (void);
1666
dee91e82 1667static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1668
f792889a
DJ
1669static struct type *set_die_type (struct die_info *, struct type *,
1670 struct dwarf2_cu *);
1c379e20 1671
ae038cb0
DJ
1672static void create_all_comp_units (struct objfile *);
1673
0e50663e 1674static int create_all_type_units (struct objfile *);
1fd400ff 1675
95554aad
TT
1676static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1677 enum language);
10b3939b 1678
95554aad
TT
1679static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1680 enum language);
10b3939b 1681
f4dc4d17
DE
1682static void process_full_type_unit (struct dwarf2_per_cu_data *,
1683 enum language);
1684
10b3939b
DJ
1685static void dwarf2_add_dependence (struct dwarf2_cu *,
1686 struct dwarf2_per_cu_data *);
1687
ae038cb0
DJ
1688static void dwarf2_mark (struct dwarf2_cu *);
1689
1690static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1691
b64f50a1 1692static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1693 struct dwarf2_per_cu_data *per_cu);
1694
f792889a 1695static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1696
9291a0cd
TT
1697static void dwarf2_release_queue (void *dummy);
1698
95554aad
TT
1699static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1700 enum language pretend_language);
1701
1702static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1703 struct dwarf2_per_cu_data *per_cu,
1704 enum language pretend_language);
9291a0cd 1705
a0f42c21 1706static void process_queue (void);
9291a0cd
TT
1707
1708static void find_file_and_directory (struct die_info *die,
1709 struct dwarf2_cu *cu,
15d034d0 1710 const char **name, const char **comp_dir);
9291a0cd
TT
1711
1712static char *file_full_name (int file, struct line_header *lh,
1713 const char *comp_dir);
1714
d521ce57 1715static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1716 (struct comp_unit_head *header,
1717 struct dwarf2_section_info *section,
d521ce57 1718 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1719 int is_debug_types_section);
1720
fd820528 1721static void init_cutu_and_read_dies
f4dc4d17
DE
1722 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1723 int use_existing_cu, int keep,
3019eac3
DE
1724 die_reader_func_ftype *die_reader_func, void *data);
1725
dee91e82
DE
1726static void init_cutu_and_read_dies_simple
1727 (struct dwarf2_per_cu_data *this_cu,
1728 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1729
673bfd45 1730static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1731
3019eac3
DE
1732static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1733
1734static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1735 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1736
1737static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1738 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1739
1740static void free_dwo_file_cleanup (void *);
1741
95554aad
TT
1742static void process_cu_includes (void);
1743
1b80a9fa
JK
1744static void check_producer (struct dwarf2_cu *cu);
1745
9291a0cd
TT
1746#if WORDS_BIGENDIAN
1747
1748/* Convert VALUE between big- and little-endian. */
1749static offset_type
1750byte_swap (offset_type value)
1751{
1752 offset_type result;
1753
1754 result = (value & 0xff) << 24;
1755 result |= (value & 0xff00) << 8;
1756 result |= (value & 0xff0000) >> 8;
1757 result |= (value & 0xff000000) >> 24;
1758 return result;
1759}
1760
1761#define MAYBE_SWAP(V) byte_swap (V)
1762
1763#else
1764#define MAYBE_SWAP(V) (V)
1765#endif /* WORDS_BIGENDIAN */
1766
1767/* The suffix for an index file. */
1768#define INDEX_SUFFIX ".gdb-index"
1769
c906108c 1770/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1771 information and return true if we have enough to do something.
1772 NAMES points to the dwarf2 section names, or is NULL if the standard
1773 ELF names are used. */
c906108c
SS
1774
1775int
251d32d9
TG
1776dwarf2_has_info (struct objfile *objfile,
1777 const struct dwarf2_debug_sections *names)
c906108c 1778{
be391dca
TT
1779 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1780 if (!dwarf2_per_objfile)
1781 {
1782 /* Initialize per-objfile state. */
1783 struct dwarf2_per_objfile *data
1784 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1785
be391dca
TT
1786 memset (data, 0, sizeof (*data));
1787 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1788 dwarf2_per_objfile = data;
6502dd73 1789
251d32d9
TG
1790 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1791 (void *) names);
be391dca
TT
1792 dwarf2_per_objfile->objfile = objfile;
1793 }
1794 return (dwarf2_per_objfile->info.asection != NULL
1795 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1796}
1797
251d32d9
TG
1798/* When loading sections, we look either for uncompressed section or for
1799 compressed section names. */
233a11ab
CS
1800
1801static int
251d32d9
TG
1802section_is_p (const char *section_name,
1803 const struct dwarf2_section_names *names)
233a11ab 1804{
251d32d9
TG
1805 if (names->normal != NULL
1806 && strcmp (section_name, names->normal) == 0)
1807 return 1;
1808 if (names->compressed != NULL
1809 && strcmp (section_name, names->compressed) == 0)
1810 return 1;
1811 return 0;
233a11ab
CS
1812}
1813
c906108c
SS
1814/* This function is mapped across the sections and remembers the
1815 offset and size of each of the debugging sections we are interested
1816 in. */
1817
1818static void
251d32d9 1819dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1820{
251d32d9 1821 const struct dwarf2_debug_sections *names;
dc7650b8 1822 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1823
1824 if (vnames == NULL)
1825 names = &dwarf2_elf_names;
1826 else
1827 names = (const struct dwarf2_debug_sections *) vnames;
1828
dc7650b8
JK
1829 if ((aflag & SEC_HAS_CONTENTS) == 0)
1830 {
1831 }
1832 else if (section_is_p (sectp->name, &names->info))
c906108c 1833 {
dce234bc
PP
1834 dwarf2_per_objfile->info.asection = sectp;
1835 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1836 }
251d32d9 1837 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1838 {
dce234bc
PP
1839 dwarf2_per_objfile->abbrev.asection = sectp;
1840 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1841 }
251d32d9 1842 else if (section_is_p (sectp->name, &names->line))
c906108c 1843 {
dce234bc
PP
1844 dwarf2_per_objfile->line.asection = sectp;
1845 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1846 }
251d32d9 1847 else if (section_is_p (sectp->name, &names->loc))
c906108c 1848 {
dce234bc
PP
1849 dwarf2_per_objfile->loc.asection = sectp;
1850 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1851 }
251d32d9 1852 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1853 {
dce234bc
PP
1854 dwarf2_per_objfile->macinfo.asection = sectp;
1855 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1856 }
cf2c3c16
TT
1857 else if (section_is_p (sectp->name, &names->macro))
1858 {
1859 dwarf2_per_objfile->macro.asection = sectp;
1860 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1861 }
251d32d9 1862 else if (section_is_p (sectp->name, &names->str))
c906108c 1863 {
dce234bc
PP
1864 dwarf2_per_objfile->str.asection = sectp;
1865 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1866 }
3019eac3
DE
1867 else if (section_is_p (sectp->name, &names->addr))
1868 {
1869 dwarf2_per_objfile->addr.asection = sectp;
1870 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1871 }
251d32d9 1872 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1873 {
dce234bc
PP
1874 dwarf2_per_objfile->frame.asection = sectp;
1875 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1876 }
251d32d9 1877 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1878 {
dc7650b8
JK
1879 dwarf2_per_objfile->eh_frame.asection = sectp;
1880 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1881 }
251d32d9 1882 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1883 {
dce234bc
PP
1884 dwarf2_per_objfile->ranges.asection = sectp;
1885 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1886 }
251d32d9 1887 else if (section_is_p (sectp->name, &names->types))
348e048f 1888 {
8b70b953
TT
1889 struct dwarf2_section_info type_section;
1890
1891 memset (&type_section, 0, sizeof (type_section));
1892 type_section.asection = sectp;
1893 type_section.size = bfd_get_section_size (sectp);
1894
1895 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1896 &type_section);
348e048f 1897 }
251d32d9 1898 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1899 {
1900 dwarf2_per_objfile->gdb_index.asection = sectp;
1901 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1902 }
dce234bc 1903
72dca2f5
FR
1904 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1905 && bfd_section_vma (abfd, sectp) == 0)
1906 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1907}
1908
fceca515
DE
1909/* A helper function that decides whether a section is empty,
1910 or not present. */
9e0ac564
TT
1911
1912static int
1913dwarf2_section_empty_p (struct dwarf2_section_info *info)
1914{
1915 return info->asection == NULL || info->size == 0;
1916}
1917
3019eac3
DE
1918/* Read the contents of the section INFO.
1919 OBJFILE is the main object file, but not necessarily the file where
1920 the section comes from. E.g., for DWO files INFO->asection->owner
1921 is the bfd of the DWO file.
dce234bc 1922 If the section is compressed, uncompress it before returning. */
c906108c 1923
dce234bc
PP
1924static void
1925dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1926{
dce234bc 1927 asection *sectp = info->asection;
3019eac3 1928 bfd *abfd;
dce234bc
PP
1929 gdb_byte *buf, *retbuf;
1930 unsigned char header[4];
c906108c 1931
be391dca
TT
1932 if (info->readin)
1933 return;
dce234bc 1934 info->buffer = NULL;
be391dca 1935 info->readin = 1;
188dd5d6 1936
9e0ac564 1937 if (dwarf2_section_empty_p (info))
dce234bc 1938 return;
c906108c 1939
3019eac3
DE
1940 abfd = sectp->owner;
1941
4bf44c1c
TT
1942 /* If the section has relocations, we must read it ourselves.
1943 Otherwise we attach it to the BFD. */
1944 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1945 {
d521ce57 1946 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1947 return;
dce234bc 1948 }
dce234bc 1949
4bf44c1c
TT
1950 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1951 info->buffer = buf;
dce234bc
PP
1952
1953 /* When debugging .o files, we may need to apply relocations; see
1954 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1955 We never compress sections in .o files, so we only need to
1956 try this when the section is not compressed. */
ac8035ab 1957 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1958 if (retbuf != NULL)
1959 {
1960 info->buffer = retbuf;
1961 return;
1962 }
1963
1964 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1965 || bfd_bread (buf, info->size, abfd) != info->size)
1966 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1967 bfd_get_filename (abfd));
1968}
1969
9e0ac564
TT
1970/* A helper function that returns the size of a section in a safe way.
1971 If you are positive that the section has been read before using the
1972 size, then it is safe to refer to the dwarf2_section_info object's
1973 "size" field directly. In other cases, you must call this
1974 function, because for compressed sections the size field is not set
1975 correctly until the section has been read. */
1976
1977static bfd_size_type
1978dwarf2_section_size (struct objfile *objfile,
1979 struct dwarf2_section_info *info)
1980{
1981 if (!info->readin)
1982 dwarf2_read_section (objfile, info);
1983 return info->size;
1984}
1985
dce234bc 1986/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1987 SECTION_NAME. */
af34e669 1988
dce234bc 1989void
3017a003
TG
1990dwarf2_get_section_info (struct objfile *objfile,
1991 enum dwarf2_section_enum sect,
d521ce57 1992 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1993 bfd_size_type *sizep)
1994{
1995 struct dwarf2_per_objfile *data
1996 = objfile_data (objfile, dwarf2_objfile_data_key);
1997 struct dwarf2_section_info *info;
a3b2a86b
TT
1998
1999 /* We may see an objfile without any DWARF, in which case we just
2000 return nothing. */
2001 if (data == NULL)
2002 {
2003 *sectp = NULL;
2004 *bufp = NULL;
2005 *sizep = 0;
2006 return;
2007 }
3017a003
TG
2008 switch (sect)
2009 {
2010 case DWARF2_DEBUG_FRAME:
2011 info = &data->frame;
2012 break;
2013 case DWARF2_EH_FRAME:
2014 info = &data->eh_frame;
2015 break;
2016 default:
2017 gdb_assert_not_reached ("unexpected section");
2018 }
dce234bc 2019
9e0ac564 2020 dwarf2_read_section (objfile, info);
dce234bc
PP
2021
2022 *sectp = info->asection;
2023 *bufp = info->buffer;
2024 *sizep = info->size;
2025}
2026
36586728
TT
2027/* A helper function to find the sections for a .dwz file. */
2028
2029static void
2030locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2031{
2032 struct dwz_file *dwz_file = arg;
2033
2034 /* Note that we only support the standard ELF names, because .dwz
2035 is ELF-only (at the time of writing). */
2036 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2037 {
2038 dwz_file->abbrev.asection = sectp;
2039 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2040 }
2041 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2042 {
2043 dwz_file->info.asection = sectp;
2044 dwz_file->info.size = bfd_get_section_size (sectp);
2045 }
2046 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2047 {
2048 dwz_file->str.asection = sectp;
2049 dwz_file->str.size = bfd_get_section_size (sectp);
2050 }
2051 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2052 {
2053 dwz_file->line.asection = sectp;
2054 dwz_file->line.size = bfd_get_section_size (sectp);
2055 }
2056 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2057 {
2058 dwz_file->macro.asection = sectp;
2059 dwz_file->macro.size = bfd_get_section_size (sectp);
2060 }
2ec9a5e0
TT
2061 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2062 {
2063 dwz_file->gdb_index.asection = sectp;
2064 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2065 }
36586728
TT
2066}
2067
2068/* Open the separate '.dwz' debug file, if needed. Error if the file
2069 cannot be found. */
2070
2071static struct dwz_file *
2072dwarf2_get_dwz_file (void)
2073{
2074 bfd *abfd, *dwz_bfd;
2075 asection *section;
2076 gdb_byte *data;
2077 struct cleanup *cleanup;
2078 const char *filename;
2079 struct dwz_file *result;
2080
2081 if (dwarf2_per_objfile->dwz_file != NULL)
2082 return dwarf2_per_objfile->dwz_file;
2083
2084 abfd = dwarf2_per_objfile->objfile->obfd;
2085 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2086 if (section == NULL)
2087 error (_("could not find '.gnu_debugaltlink' section"));
2088 if (!bfd_malloc_and_get_section (abfd, section, &data))
2089 error (_("could not read '.gnu_debugaltlink' section: %s"),
2090 bfd_errmsg (bfd_get_error ()));
2091 cleanup = make_cleanup (xfree, data);
2092
2093 filename = data;
2094 if (!IS_ABSOLUTE_PATH (filename))
2095 {
2096 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2097 char *rel;
2098
2099 make_cleanup (xfree, abs);
2100 abs = ldirname (abs);
2101 make_cleanup (xfree, abs);
2102
2103 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2104 make_cleanup (xfree, rel);
2105 filename = rel;
2106 }
2107
2108 /* The format is just a NUL-terminated file name, followed by the
2109 build-id. For now, though, we ignore the build-id. */
2110 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2111 if (dwz_bfd == NULL)
2112 error (_("could not read '%s': %s"), filename,
2113 bfd_errmsg (bfd_get_error ()));
2114
2115 if (!bfd_check_format (dwz_bfd, bfd_object))
2116 {
2117 gdb_bfd_unref (dwz_bfd);
2118 error (_("file '%s' was not usable: %s"), filename,
2119 bfd_errmsg (bfd_get_error ()));
2120 }
2121
2122 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2123 struct dwz_file);
2124 result->dwz_bfd = dwz_bfd;
2125
2126 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2127
2128 do_cleanups (cleanup);
2129
8d2cc612 2130 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2131 return result;
2132}
9291a0cd 2133\f
7b9f3c50
DE
2134/* DWARF quick_symbols_functions support. */
2135
2136/* TUs can share .debug_line entries, and there can be a lot more TUs than
2137 unique line tables, so we maintain a separate table of all .debug_line
2138 derived entries to support the sharing.
2139 All the quick functions need is the list of file names. We discard the
2140 line_header when we're done and don't need to record it here. */
2141struct quick_file_names
2142{
094b34ac
DE
2143 /* The data used to construct the hash key. */
2144 struct stmt_list_hash hash;
7b9f3c50
DE
2145
2146 /* The number of entries in file_names, real_names. */
2147 unsigned int num_file_names;
2148
2149 /* The file names from the line table, after being run through
2150 file_full_name. */
2151 const char **file_names;
2152
2153 /* The file names from the line table after being run through
2154 gdb_realpath. These are computed lazily. */
2155 const char **real_names;
2156};
2157
2158/* When using the index (and thus not using psymtabs), each CU has an
2159 object of this type. This is used to hold information needed by
2160 the various "quick" methods. */
2161struct dwarf2_per_cu_quick_data
2162{
2163 /* The file table. This can be NULL if there was no file table
2164 or it's currently not read in.
2165 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2166 struct quick_file_names *file_names;
2167
2168 /* The corresponding symbol table. This is NULL if symbols for this
2169 CU have not yet been read. */
2170 struct symtab *symtab;
2171
2172 /* A temporary mark bit used when iterating over all CUs in
2173 expand_symtabs_matching. */
2174 unsigned int mark : 1;
2175
2176 /* True if we've tried to read the file table and found there isn't one.
2177 There will be no point in trying to read it again next time. */
2178 unsigned int no_file_data : 1;
2179};
2180
094b34ac
DE
2181/* Utility hash function for a stmt_list_hash. */
2182
2183static hashval_t
2184hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2185{
2186 hashval_t v = 0;
2187
2188 if (stmt_list_hash->dwo_unit != NULL)
2189 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2190 v += stmt_list_hash->line_offset.sect_off;
2191 return v;
2192}
2193
2194/* Utility equality function for a stmt_list_hash. */
2195
2196static int
2197eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2198 const struct stmt_list_hash *rhs)
2199{
2200 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2201 return 0;
2202 if (lhs->dwo_unit != NULL
2203 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2204 return 0;
2205
2206 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2207}
2208
7b9f3c50
DE
2209/* Hash function for a quick_file_names. */
2210
2211static hashval_t
2212hash_file_name_entry (const void *e)
2213{
2214 const struct quick_file_names *file_data = e;
2215
094b34ac 2216 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2217}
2218
2219/* Equality function for a quick_file_names. */
2220
2221static int
2222eq_file_name_entry (const void *a, const void *b)
2223{
2224 const struct quick_file_names *ea = a;
2225 const struct quick_file_names *eb = b;
2226
094b34ac 2227 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2228}
2229
2230/* Delete function for a quick_file_names. */
2231
2232static void
2233delete_file_name_entry (void *e)
2234{
2235 struct quick_file_names *file_data = e;
2236 int i;
2237
2238 for (i = 0; i < file_data->num_file_names; ++i)
2239 {
2240 xfree ((void*) file_data->file_names[i]);
2241 if (file_data->real_names)
2242 xfree ((void*) file_data->real_names[i]);
2243 }
2244
2245 /* The space for the struct itself lives on objfile_obstack,
2246 so we don't free it here. */
2247}
2248
2249/* Create a quick_file_names hash table. */
2250
2251static htab_t
2252create_quick_file_names_table (unsigned int nr_initial_entries)
2253{
2254 return htab_create_alloc (nr_initial_entries,
2255 hash_file_name_entry, eq_file_name_entry,
2256 delete_file_name_entry, xcalloc, xfree);
2257}
9291a0cd 2258
918dd910
JK
2259/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2260 have to be created afterwards. You should call age_cached_comp_units after
2261 processing PER_CU->CU. dw2_setup must have been already called. */
2262
2263static void
2264load_cu (struct dwarf2_per_cu_data *per_cu)
2265{
3019eac3 2266 if (per_cu->is_debug_types)
e5fe5e75 2267 load_full_type_unit (per_cu);
918dd910 2268 else
95554aad 2269 load_full_comp_unit (per_cu, language_minimal);
918dd910 2270
918dd910 2271 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2272
2273 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2274}
2275
a0f42c21 2276/* Read in the symbols for PER_CU. */
2fdf6df6 2277
9291a0cd 2278static void
a0f42c21 2279dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2280{
2281 struct cleanup *back_to;
2282
f4dc4d17
DE
2283 /* Skip type_unit_groups, reading the type units they contain
2284 is handled elsewhere. */
2285 if (IS_TYPE_UNIT_GROUP (per_cu))
2286 return;
2287
9291a0cd
TT
2288 back_to = make_cleanup (dwarf2_release_queue, NULL);
2289
95554aad
TT
2290 if (dwarf2_per_objfile->using_index
2291 ? per_cu->v.quick->symtab == NULL
2292 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2293 {
2294 queue_comp_unit (per_cu, language_minimal);
2295 load_cu (per_cu);
2296 }
9291a0cd 2297
a0f42c21 2298 process_queue ();
9291a0cd
TT
2299
2300 /* Age the cache, releasing compilation units that have not
2301 been used recently. */
2302 age_cached_comp_units ();
2303
2304 do_cleanups (back_to);
2305}
2306
2307/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2308 the objfile from which this CU came. Returns the resulting symbol
2309 table. */
2fdf6df6 2310
9291a0cd 2311static struct symtab *
a0f42c21 2312dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2313{
95554aad 2314 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2315 if (!per_cu->v.quick->symtab)
2316 {
2317 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2318 increment_reading_symtab ();
a0f42c21 2319 dw2_do_instantiate_symtab (per_cu);
95554aad 2320 process_cu_includes ();
9291a0cd
TT
2321 do_cleanups (back_to);
2322 }
2323 return per_cu->v.quick->symtab;
2324}
2325
f4dc4d17
DE
2326/* Return the CU given its index.
2327
2328 This is intended for loops like:
2329
2330 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2331 + dwarf2_per_objfile->n_type_units); ++i)
2332 {
2333 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2334
2335 ...;
2336 }
2337*/
2fdf6df6 2338
1fd400ff
TT
2339static struct dwarf2_per_cu_data *
2340dw2_get_cu (int index)
2341{
2342 if (index >= dwarf2_per_objfile->n_comp_units)
2343 {
f4dc4d17 2344 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2345 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2346 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2347 }
2348
2349 return dwarf2_per_objfile->all_comp_units[index];
2350}
2351
2352/* Return the primary CU given its index.
2353 The difference between this function and dw2_get_cu is in the handling
2354 of type units (TUs). Here we return the type_unit_group object.
2355
2356 This is intended for loops like:
2357
2358 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2359 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2360 {
2361 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2362
2363 ...;
2364 }
2365*/
2366
2367static struct dwarf2_per_cu_data *
2368dw2_get_primary_cu (int index)
2369{
2370 if (index >= dwarf2_per_objfile->n_comp_units)
2371 {
1fd400ff 2372 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2373 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2374 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2375 }
f4dc4d17 2376
1fd400ff
TT
2377 return dwarf2_per_objfile->all_comp_units[index];
2378}
2379
2ec9a5e0
TT
2380/* A helper for create_cus_from_index that handles a given list of
2381 CUs. */
2fdf6df6 2382
74a0d9f6 2383static void
2ec9a5e0
TT
2384create_cus_from_index_list (struct objfile *objfile,
2385 const gdb_byte *cu_list, offset_type n_elements,
2386 struct dwarf2_section_info *section,
2387 int is_dwz,
2388 int base_offset)
9291a0cd
TT
2389{
2390 offset_type i;
9291a0cd 2391
2ec9a5e0 2392 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2393 {
2394 struct dwarf2_per_cu_data *the_cu;
2395 ULONGEST offset, length;
2396
74a0d9f6
JK
2397 gdb_static_assert (sizeof (ULONGEST) >= 8);
2398 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2399 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2400 cu_list += 2 * 8;
2401
2402 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2403 struct dwarf2_per_cu_data);
b64f50a1 2404 the_cu->offset.sect_off = offset;
9291a0cd
TT
2405 the_cu->length = length;
2406 the_cu->objfile = objfile;
8a0459fd 2407 the_cu->section = section;
9291a0cd
TT
2408 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2409 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2410 the_cu->is_dwz = is_dwz;
2411 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2412 }
9291a0cd
TT
2413}
2414
2ec9a5e0 2415/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2416 the CU objects for this objfile. */
2ec9a5e0 2417
74a0d9f6 2418static void
2ec9a5e0
TT
2419create_cus_from_index (struct objfile *objfile,
2420 const gdb_byte *cu_list, offset_type cu_list_elements,
2421 const gdb_byte *dwz_list, offset_type dwz_elements)
2422{
2423 struct dwz_file *dwz;
2424
2425 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2426 dwarf2_per_objfile->all_comp_units
2427 = obstack_alloc (&objfile->objfile_obstack,
2428 dwarf2_per_objfile->n_comp_units
2429 * sizeof (struct dwarf2_per_cu_data *));
2430
74a0d9f6
JK
2431 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2432 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2433
2434 if (dwz_elements == 0)
74a0d9f6 2435 return;
2ec9a5e0
TT
2436
2437 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2438 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2439 cu_list_elements / 2);
2ec9a5e0
TT
2440}
2441
1fd400ff 2442/* Create the signatured type hash table from the index. */
673bfd45 2443
74a0d9f6 2444static void
673bfd45 2445create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2446 struct dwarf2_section_info *section,
673bfd45
DE
2447 const gdb_byte *bytes,
2448 offset_type elements)
1fd400ff
TT
2449{
2450 offset_type i;
673bfd45 2451 htab_t sig_types_hash;
1fd400ff 2452
d467dd73
DE
2453 dwarf2_per_objfile->n_type_units = elements / 3;
2454 dwarf2_per_objfile->all_type_units
1fd400ff 2455 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2456 dwarf2_per_objfile->n_type_units
b4dd5633 2457 * sizeof (struct signatured_type *));
1fd400ff 2458
673bfd45 2459 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2460
2461 for (i = 0; i < elements; i += 3)
2462 {
52dc124a
DE
2463 struct signatured_type *sig_type;
2464 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2465 void **slot;
2466
74a0d9f6
JK
2467 gdb_static_assert (sizeof (ULONGEST) >= 8);
2468 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2469 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2470 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2471 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2472 bytes += 3 * 8;
2473
52dc124a 2474 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2475 struct signatured_type);
52dc124a 2476 sig_type->signature = signature;
3019eac3
DE
2477 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2478 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2479 sig_type->per_cu.section = section;
52dc124a
DE
2480 sig_type->per_cu.offset.sect_off = offset;
2481 sig_type->per_cu.objfile = objfile;
2482 sig_type->per_cu.v.quick
1fd400ff
TT
2483 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2484 struct dwarf2_per_cu_quick_data);
2485
52dc124a
DE
2486 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2487 *slot = sig_type;
1fd400ff 2488
b4dd5633 2489 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2490 }
2491
673bfd45 2492 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2493}
2494
9291a0cd
TT
2495/* Read the address map data from the mapped index, and use it to
2496 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2497
9291a0cd
TT
2498static void
2499create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2500{
2501 const gdb_byte *iter, *end;
2502 struct obstack temp_obstack;
2503 struct addrmap *mutable_map;
2504 struct cleanup *cleanup;
2505 CORE_ADDR baseaddr;
2506
2507 obstack_init (&temp_obstack);
2508 cleanup = make_cleanup_obstack_free (&temp_obstack);
2509 mutable_map = addrmap_create_mutable (&temp_obstack);
2510
2511 iter = index->address_table;
2512 end = iter + index->address_table_size;
2513
2514 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2515
2516 while (iter < end)
2517 {
2518 ULONGEST hi, lo, cu_index;
2519 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2520 iter += 8;
2521 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2522 iter += 8;
2523 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2524 iter += 4;
f652bce2
DE
2525
2526 if (cu_index < dwarf2_per_objfile->n_comp_units)
2527 {
2528 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2529 dw2_get_cu (cu_index));
2530 }
2531 else
2532 {
2533 complaint (&symfile_complaints,
2534 _(".gdb_index address table has invalid CU number %u"),
2535 (unsigned) cu_index);
2536 }
9291a0cd
TT
2537 }
2538
2539 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2540 &objfile->objfile_obstack);
2541 do_cleanups (cleanup);
2542}
2543
59d7bcaf
JK
2544/* The hash function for strings in the mapped index. This is the same as
2545 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2546 implementation. This is necessary because the hash function is tied to the
2547 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2548 SYMBOL_HASH_NEXT.
2549
2550 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2551
9291a0cd 2552static hashval_t
559a7a62 2553mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2554{
2555 const unsigned char *str = (const unsigned char *) p;
2556 hashval_t r = 0;
2557 unsigned char c;
2558
2559 while ((c = *str++) != 0)
559a7a62
JK
2560 {
2561 if (index_version >= 5)
2562 c = tolower (c);
2563 r = r * 67 + c - 113;
2564 }
9291a0cd
TT
2565
2566 return r;
2567}
2568
2569/* Find a slot in the mapped index INDEX for the object named NAME.
2570 If NAME is found, set *VEC_OUT to point to the CU vector in the
2571 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2572
9291a0cd
TT
2573static int
2574find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2575 offset_type **vec_out)
2576{
0cf03b49
JK
2577 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2578 offset_type hash;
9291a0cd 2579 offset_type slot, step;
559a7a62 2580 int (*cmp) (const char *, const char *);
9291a0cd 2581
0cf03b49
JK
2582 if (current_language->la_language == language_cplus
2583 || current_language->la_language == language_java
2584 || current_language->la_language == language_fortran)
2585 {
2586 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2587 not contain any. */
2588 const char *paren = strchr (name, '(');
2589
2590 if (paren)
2591 {
2592 char *dup;
2593
2594 dup = xmalloc (paren - name + 1);
2595 memcpy (dup, name, paren - name);
2596 dup[paren - name] = 0;
2597
2598 make_cleanup (xfree, dup);
2599 name = dup;
2600 }
2601 }
2602
559a7a62 2603 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2604 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2605 simulate our NAME being searched is also lowercased. */
2606 hash = mapped_index_string_hash ((index->version == 4
2607 && case_sensitivity == case_sensitive_off
2608 ? 5 : index->version),
2609 name);
2610
3876f04e
DE
2611 slot = hash & (index->symbol_table_slots - 1);
2612 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2613 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2614
2615 for (;;)
2616 {
2617 /* Convert a slot number to an offset into the table. */
2618 offset_type i = 2 * slot;
2619 const char *str;
3876f04e 2620 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2621 {
2622 do_cleanups (back_to);
2623 return 0;
2624 }
9291a0cd 2625
3876f04e 2626 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2627 if (!cmp (name, str))
9291a0cd
TT
2628 {
2629 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2630 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2631 do_cleanups (back_to);
9291a0cd
TT
2632 return 1;
2633 }
2634
3876f04e 2635 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2636 }
2637}
2638
2ec9a5e0
TT
2639/* A helper function that reads the .gdb_index from SECTION and fills
2640 in MAP. FILENAME is the name of the file containing the section;
2641 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2642 ok to use deprecated sections.
2643
2644 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2645 out parameters that are filled in with information about the CU and
2646 TU lists in the section.
2647
2648 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2649
9291a0cd 2650static int
2ec9a5e0
TT
2651read_index_from_section (struct objfile *objfile,
2652 const char *filename,
2653 int deprecated_ok,
2654 struct dwarf2_section_info *section,
2655 struct mapped_index *map,
2656 const gdb_byte **cu_list,
2657 offset_type *cu_list_elements,
2658 const gdb_byte **types_list,
2659 offset_type *types_list_elements)
9291a0cd 2660{
d521ce57 2661 const char *addr;
2ec9a5e0 2662 offset_type version;
b3b272e1 2663 offset_type *metadata;
1fd400ff 2664 int i;
9291a0cd 2665
2ec9a5e0 2666 if (dwarf2_section_empty_p (section))
9291a0cd 2667 return 0;
82430852
JK
2668
2669 /* Older elfutils strip versions could keep the section in the main
2670 executable while splitting it for the separate debug info file. */
2ec9a5e0 2671 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2672 return 0;
2673
2ec9a5e0 2674 dwarf2_read_section (objfile, section);
9291a0cd 2675
2ec9a5e0 2676 addr = section->buffer;
9291a0cd 2677 /* Version check. */
1fd400ff 2678 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2679 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2680 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2681 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2682 indices. */
831adc1f 2683 if (version < 4)
481860b3
GB
2684 {
2685 static int warning_printed = 0;
2686 if (!warning_printed)
2687 {
2688 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2689 filename);
481860b3
GB
2690 warning_printed = 1;
2691 }
2692 return 0;
2693 }
2694 /* Index version 4 uses a different hash function than index version
2695 5 and later.
2696
2697 Versions earlier than 6 did not emit psymbols for inlined
2698 functions. Using these files will cause GDB not to be able to
2699 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2700 indices unless the user has done
2701 "set use-deprecated-index-sections on". */
2ec9a5e0 2702 if (version < 6 && !deprecated_ok)
481860b3
GB
2703 {
2704 static int warning_printed = 0;
2705 if (!warning_printed)
2706 {
e615022a
DE
2707 warning (_("\
2708Skipping deprecated .gdb_index section in %s.\n\
2709Do \"set use-deprecated-index-sections on\" before the file is read\n\
2710to use the section anyway."),
2ec9a5e0 2711 filename);
481860b3
GB
2712 warning_printed = 1;
2713 }
2714 return 0;
2715 }
796a7ff8
DE
2716 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2717 of the TU (for symbols coming from TUs). It's just a performance bug, and
2718 we can't distinguish gdb-generated indices from gold-generated ones, so
2719 nothing to do here. */
2720
481860b3 2721 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2722 longer backward compatible. */
796a7ff8 2723 if (version > 8)
594e8718 2724 return 0;
9291a0cd 2725
559a7a62 2726 map->version = version;
2ec9a5e0 2727 map->total_size = section->size;
9291a0cd
TT
2728
2729 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2730
2731 i = 0;
2ec9a5e0
TT
2732 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2733 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2734 / 8);
1fd400ff
TT
2735 ++i;
2736
2ec9a5e0
TT
2737 *types_list = addr + MAYBE_SWAP (metadata[i]);
2738 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2739 - MAYBE_SWAP (metadata[i]))
2740 / 8);
987d643c 2741 ++i;
1fd400ff
TT
2742
2743 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2744 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2745 - MAYBE_SWAP (metadata[i]));
2746 ++i;
2747
3876f04e
DE
2748 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2749 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2750 - MAYBE_SWAP (metadata[i]))
2751 / (2 * sizeof (offset_type)));
1fd400ff 2752 ++i;
9291a0cd 2753
1fd400ff
TT
2754 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2755
2ec9a5e0
TT
2756 return 1;
2757}
2758
2759
2760/* Read the index file. If everything went ok, initialize the "quick"
2761 elements of all the CUs and return 1. Otherwise, return 0. */
2762
2763static int
2764dwarf2_read_index (struct objfile *objfile)
2765{
2766 struct mapped_index local_map, *map;
2767 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2768 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2769
2770 if (!read_index_from_section (objfile, objfile->name,
2771 use_deprecated_index_sections,
2772 &dwarf2_per_objfile->gdb_index, &local_map,
2773 &cu_list, &cu_list_elements,
2774 &types_list, &types_list_elements))
2775 return 0;
2776
0fefef59 2777 /* Don't use the index if it's empty. */
2ec9a5e0 2778 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2779 return 0;
2780
2ec9a5e0
TT
2781 /* If there is a .dwz file, read it so we can get its CU list as
2782 well. */
2783 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2784 {
2785 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2786 struct mapped_index dwz_map;
2787 const gdb_byte *dwz_types_ignore;
2788 offset_type dwz_types_elements_ignore;
2789
2790 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2791 1,
2792 &dwz->gdb_index, &dwz_map,
2793 &dwz_list, &dwz_list_elements,
2794 &dwz_types_ignore,
2795 &dwz_types_elements_ignore))
2796 {
2797 warning (_("could not read '.gdb_index' section from %s; skipping"),
2798 bfd_get_filename (dwz->dwz_bfd));
2799 return 0;
2800 }
2801 }
2802
74a0d9f6
JK
2803 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2804 dwz_list_elements);
1fd400ff 2805
8b70b953
TT
2806 if (types_list_elements)
2807 {
2808 struct dwarf2_section_info *section;
2809
2810 /* We can only handle a single .debug_types when we have an
2811 index. */
2812 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2813 return 0;
2814
2815 section = VEC_index (dwarf2_section_info_def,
2816 dwarf2_per_objfile->types, 0);
2817
74a0d9f6
JK
2818 create_signatured_type_table_from_index (objfile, section, types_list,
2819 types_list_elements);
8b70b953 2820 }
9291a0cd 2821
2ec9a5e0
TT
2822 create_addrmap_from_index (objfile, &local_map);
2823
2824 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2825 *map = local_map;
9291a0cd
TT
2826
2827 dwarf2_per_objfile->index_table = map;
2828 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2829 dwarf2_per_objfile->quick_file_names_table =
2830 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2831
2832 return 1;
2833}
2834
2835/* A helper for the "quick" functions which sets the global
2836 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2837
9291a0cd
TT
2838static void
2839dw2_setup (struct objfile *objfile)
2840{
2841 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2842 gdb_assert (dwarf2_per_objfile);
2843}
2844
dee91e82 2845/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2846
dee91e82
DE
2847static void
2848dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2849 const gdb_byte *info_ptr,
dee91e82
DE
2850 struct die_info *comp_unit_die,
2851 int has_children,
2852 void *data)
9291a0cd 2853{
dee91e82
DE
2854 struct dwarf2_cu *cu = reader->cu;
2855 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2856 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2857 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2858 struct line_header *lh;
9291a0cd 2859 struct attribute *attr;
dee91e82 2860 int i;
15d034d0 2861 const char *name, *comp_dir;
7b9f3c50
DE
2862 void **slot;
2863 struct quick_file_names *qfn;
2864 unsigned int line_offset;
9291a0cd 2865
0186c6a7
DE
2866 gdb_assert (! this_cu->is_debug_types);
2867
07261596
TT
2868 /* Our callers never want to match partial units -- instead they
2869 will match the enclosing full CU. */
2870 if (comp_unit_die->tag == DW_TAG_partial_unit)
2871 {
2872 this_cu->v.quick->no_file_data = 1;
2873 return;
2874 }
2875
0186c6a7 2876 lh_cu = this_cu;
7b9f3c50
DE
2877 lh = NULL;
2878 slot = NULL;
2879 line_offset = 0;
dee91e82
DE
2880
2881 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2882 if (attr)
2883 {
7b9f3c50
DE
2884 struct quick_file_names find_entry;
2885
2886 line_offset = DW_UNSND (attr);
2887
2888 /* We may have already read in this line header (TU line header sharing).
2889 If we have we're done. */
094b34ac
DE
2890 find_entry.hash.dwo_unit = cu->dwo_unit;
2891 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2892 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2893 &find_entry, INSERT);
2894 if (*slot != NULL)
2895 {
094b34ac 2896 lh_cu->v.quick->file_names = *slot;
dee91e82 2897 return;
7b9f3c50
DE
2898 }
2899
3019eac3 2900 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2901 }
2902 if (lh == NULL)
2903 {
094b34ac 2904 lh_cu->v.quick->no_file_data = 1;
dee91e82 2905 return;
9291a0cd
TT
2906 }
2907
7b9f3c50 2908 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2909 qfn->hash.dwo_unit = cu->dwo_unit;
2910 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2911 gdb_assert (slot != NULL);
2912 *slot = qfn;
9291a0cd 2913
dee91e82 2914 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2915
7b9f3c50
DE
2916 qfn->num_file_names = lh->num_file_names;
2917 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2918 lh->num_file_names * sizeof (char *));
9291a0cd 2919 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2920 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2921 qfn->real_names = NULL;
9291a0cd 2922
7b9f3c50 2923 free_line_header (lh);
7b9f3c50 2924
094b34ac 2925 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2926}
2927
2928/* A helper for the "quick" functions which attempts to read the line
2929 table for THIS_CU. */
2930
2931static struct quick_file_names *
e4a48d9d 2932dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2933{
0186c6a7
DE
2934 /* This should never be called for TUs. */
2935 gdb_assert (! this_cu->is_debug_types);
2936 /* Nor type unit groups. */
2937 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2938
dee91e82
DE
2939 if (this_cu->v.quick->file_names != NULL)
2940 return this_cu->v.quick->file_names;
2941 /* If we know there is no line data, no point in looking again. */
2942 if (this_cu->v.quick->no_file_data)
2943 return NULL;
2944
0186c6a7 2945 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2946
2947 if (this_cu->v.quick->no_file_data)
2948 return NULL;
2949 return this_cu->v.quick->file_names;
9291a0cd
TT
2950}
2951
2952/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2953 real path for a given file name from the line table. */
2fdf6df6 2954
9291a0cd 2955static const char *
7b9f3c50
DE
2956dw2_get_real_path (struct objfile *objfile,
2957 struct quick_file_names *qfn, int index)
9291a0cd 2958{
7b9f3c50
DE
2959 if (qfn->real_names == NULL)
2960 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2961 qfn->num_file_names, sizeof (char *));
9291a0cd 2962
7b9f3c50
DE
2963 if (qfn->real_names[index] == NULL)
2964 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2965
7b9f3c50 2966 return qfn->real_names[index];
9291a0cd
TT
2967}
2968
2969static struct symtab *
2970dw2_find_last_source_symtab (struct objfile *objfile)
2971{
2972 int index;
ae2de4f8 2973
9291a0cd
TT
2974 dw2_setup (objfile);
2975 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2976 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2977}
2978
7b9f3c50
DE
2979/* Traversal function for dw2_forget_cached_source_info. */
2980
2981static int
2982dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2983{
7b9f3c50 2984 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2985
7b9f3c50 2986 if (file_data->real_names)
9291a0cd 2987 {
7b9f3c50 2988 int i;
9291a0cd 2989
7b9f3c50 2990 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2991 {
7b9f3c50
DE
2992 xfree ((void*) file_data->real_names[i]);
2993 file_data->real_names[i] = NULL;
9291a0cd
TT
2994 }
2995 }
7b9f3c50
DE
2996
2997 return 1;
2998}
2999
3000static void
3001dw2_forget_cached_source_info (struct objfile *objfile)
3002{
3003 dw2_setup (objfile);
3004
3005 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3006 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3007}
3008
f8eba3c6
TT
3009/* Helper function for dw2_map_symtabs_matching_filename that expands
3010 the symtabs and calls the iterator. */
3011
3012static int
3013dw2_map_expand_apply (struct objfile *objfile,
3014 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3015 const char *name, const char *real_path,
f8eba3c6
TT
3016 int (*callback) (struct symtab *, void *),
3017 void *data)
3018{
3019 struct symtab *last_made = objfile->symtabs;
3020
3021 /* Don't visit already-expanded CUs. */
3022 if (per_cu->v.quick->symtab)
3023 return 0;
3024
3025 /* This may expand more than one symtab, and we want to iterate over
3026 all of them. */
a0f42c21 3027 dw2_instantiate_symtab (per_cu);
f8eba3c6 3028
f5b95b50 3029 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3030 objfile->symtabs, last_made);
3031}
3032
3033/* Implementation of the map_symtabs_matching_filename method. */
3034
9291a0cd 3035static int
f8eba3c6 3036dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3037 const char *real_path,
f8eba3c6
TT
3038 int (*callback) (struct symtab *, void *),
3039 void *data)
9291a0cd
TT
3040{
3041 int i;
c011a4f4 3042 const char *name_basename = lbasename (name);
9291a0cd
TT
3043
3044 dw2_setup (objfile);
ae2de4f8 3045
848e3e78
DE
3046 /* The rule is CUs specify all the files, including those used by
3047 any TU, so there's no need to scan TUs here. */
f4dc4d17 3048
848e3e78 3049 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3050 {
3051 int j;
f4dc4d17 3052 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3053 struct quick_file_names *file_data;
9291a0cd 3054
3d7bb9d9 3055 /* We only need to look at symtabs not already expanded. */
e254ef6a 3056 if (per_cu->v.quick->symtab)
9291a0cd
TT
3057 continue;
3058
e4a48d9d 3059 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3060 if (file_data == NULL)
9291a0cd
TT
3061 continue;
3062
7b9f3c50 3063 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3064 {
7b9f3c50 3065 const char *this_name = file_data->file_names[j];
da235a7c 3066 const char *this_real_name;
9291a0cd 3067
af529f8f 3068 if (compare_filenames_for_search (this_name, name))
9291a0cd 3069 {
f5b95b50 3070 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3071 callback, data))
3072 return 1;
288e77a7 3073 continue;
4aac40c8 3074 }
9291a0cd 3075
c011a4f4
DE
3076 /* Before we invoke realpath, which can get expensive when many
3077 files are involved, do a quick comparison of the basenames. */
3078 if (! basenames_may_differ
3079 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3080 continue;
3081
da235a7c
JK
3082 this_real_name = dw2_get_real_path (objfile, file_data, j);
3083 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3084 {
da235a7c
JK
3085 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3086 callback, data))
3087 return 1;
288e77a7 3088 continue;
da235a7c 3089 }
9291a0cd 3090
da235a7c
JK
3091 if (real_path != NULL)
3092 {
af529f8f
JK
3093 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3094 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3095 if (this_real_name != NULL
af529f8f 3096 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3097 {
f5b95b50 3098 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3099 callback, data))
3100 return 1;
288e77a7 3101 continue;
9291a0cd
TT
3102 }
3103 }
3104 }
3105 }
3106
9291a0cd
TT
3107 return 0;
3108}
3109
da51c347
DE
3110/* Struct used to manage iterating over all CUs looking for a symbol. */
3111
3112struct dw2_symtab_iterator
9291a0cd 3113{
da51c347
DE
3114 /* The internalized form of .gdb_index. */
3115 struct mapped_index *index;
3116 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3117 int want_specific_block;
3118 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3119 Unused if !WANT_SPECIFIC_BLOCK. */
3120 int block_index;
3121 /* The kind of symbol we're looking for. */
3122 domain_enum domain;
3123 /* The list of CUs from the index entry of the symbol,
3124 or NULL if not found. */
3125 offset_type *vec;
3126 /* The next element in VEC to look at. */
3127 int next;
3128 /* The number of elements in VEC, or zero if there is no match. */
3129 int length;
3130};
9291a0cd 3131
da51c347
DE
3132/* Initialize the index symtab iterator ITER.
3133 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3134 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3135
9291a0cd 3136static void
da51c347
DE
3137dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3138 struct mapped_index *index,
3139 int want_specific_block,
3140 int block_index,
3141 domain_enum domain,
3142 const char *name)
3143{
3144 iter->index = index;
3145 iter->want_specific_block = want_specific_block;
3146 iter->block_index = block_index;
3147 iter->domain = domain;
3148 iter->next = 0;
3149
3150 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3151 iter->length = MAYBE_SWAP (*iter->vec);
3152 else
3153 {
3154 iter->vec = NULL;
3155 iter->length = 0;
3156 }
3157}
3158
3159/* Return the next matching CU or NULL if there are no more. */
3160
3161static struct dwarf2_per_cu_data *
3162dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3163{
3164 for ( ; iter->next < iter->length; ++iter->next)
3165 {
3166 offset_type cu_index_and_attrs =
3167 MAYBE_SWAP (iter->vec[iter->next + 1]);
3168 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3169 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3170 int want_static = iter->block_index != GLOBAL_BLOCK;
3171 /* This value is only valid for index versions >= 7. */
3172 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3173 gdb_index_symbol_kind symbol_kind =
3174 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3175 /* Only check the symbol attributes if they're present.
3176 Indices prior to version 7 don't record them,
3177 and indices >= 7 may elide them for certain symbols
3178 (gold does this). */
3179 int attrs_valid =
3180 (iter->index->version >= 7
3181 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3182
3183 /* Skip if already read in. */
3184 if (per_cu->v.quick->symtab)
3185 continue;
3186
3187 if (attrs_valid
3188 && iter->want_specific_block
3189 && want_static != is_static)
3190 continue;
3191
3192 /* Only check the symbol's kind if it has one. */
3193 if (attrs_valid)
3194 {
3195 switch (iter->domain)
3196 {
3197 case VAR_DOMAIN:
3198 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3199 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3200 /* Some types are also in VAR_DOMAIN. */
3201 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3202 continue;
3203 break;
3204 case STRUCT_DOMAIN:
3205 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3206 continue;
3207 break;
3208 case LABEL_DOMAIN:
3209 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3210 continue;
3211 break;
3212 default:
3213 break;
3214 }
3215 }
3216
3217 ++iter->next;
3218 return per_cu;
3219 }
3220
3221 return NULL;
3222}
3223
3224static struct symtab *
3225dw2_lookup_symbol (struct objfile *objfile, int block_index,
3226 const char *name, domain_enum domain)
9291a0cd 3227{
da51c347 3228 struct symtab *stab_best = NULL;
156942c7
DE
3229 struct mapped_index *index;
3230
9291a0cd
TT
3231 dw2_setup (objfile);
3232
156942c7
DE
3233 index = dwarf2_per_objfile->index_table;
3234
da51c347 3235 /* index is NULL if OBJF_READNOW. */
156942c7 3236 if (index)
9291a0cd 3237 {
da51c347
DE
3238 struct dw2_symtab_iterator iter;
3239 struct dwarf2_per_cu_data *per_cu;
3240
3241 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3242
da51c347 3243 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3244 {
da51c347
DE
3245 struct symbol *sym = NULL;
3246 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3247
3248 /* Some caution must be observed with overloaded functions
3249 and methods, since the index will not contain any overload
3250 information (but NAME might contain it). */
3251 if (stab->primary)
9291a0cd 3252 {
da51c347
DE
3253 struct blockvector *bv = BLOCKVECTOR (stab);
3254 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3255
da51c347
DE
3256 sym = lookup_block_symbol (block, name, domain);
3257 }
1fd400ff 3258
da51c347
DE
3259 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3260 {
3261 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3262 return stab;
3263
3264 stab_best = stab;
9291a0cd 3265 }
da51c347
DE
3266
3267 /* Keep looking through other CUs. */
9291a0cd
TT
3268 }
3269 }
9291a0cd 3270
da51c347 3271 return stab_best;
9291a0cd
TT
3272}
3273
3274static void
3275dw2_print_stats (struct objfile *objfile)
3276{
e4a48d9d 3277 int i, total, count;
9291a0cd
TT
3278
3279 dw2_setup (objfile);
e4a48d9d 3280 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3281 count = 0;
e4a48d9d 3282 for (i = 0; i < total; ++i)
9291a0cd 3283 {
e254ef6a 3284 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3285
e254ef6a 3286 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3287 ++count;
3288 }
e4a48d9d 3289 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3290 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3291}
3292
3293static void
3294dw2_dump (struct objfile *objfile)
3295{
3296 /* Nothing worth printing. */
3297}
3298
3299static void
3300dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3301 struct section_offsets *delta)
3302{
3303 /* There's nothing to relocate here. */
3304}
3305
3306static void
3307dw2_expand_symtabs_for_function (struct objfile *objfile,
3308 const char *func_name)
3309{
da51c347
DE
3310 struct mapped_index *index;
3311
3312 dw2_setup (objfile);
3313
3314 index = dwarf2_per_objfile->index_table;
3315
3316 /* index is NULL if OBJF_READNOW. */
3317 if (index)
3318 {
3319 struct dw2_symtab_iterator iter;
3320 struct dwarf2_per_cu_data *per_cu;
3321
3322 /* Note: It doesn't matter what we pass for block_index here. */
3323 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3324 func_name);
3325
3326 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3327 dw2_instantiate_symtab (per_cu);
3328 }
9291a0cd
TT
3329}
3330
3331static void
3332dw2_expand_all_symtabs (struct objfile *objfile)
3333{
3334 int i;
3335
3336 dw2_setup (objfile);
1fd400ff
TT
3337
3338 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3339 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3340 {
e254ef6a 3341 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3342
a0f42c21 3343 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3344 }
3345}
3346
3347static void
652a8996
JK
3348dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3349 const char *fullname)
9291a0cd
TT
3350{
3351 int i;
3352
3353 dw2_setup (objfile);
d4637a04
DE
3354
3355 /* We don't need to consider type units here.
3356 This is only called for examining code, e.g. expand_line_sal.
3357 There can be an order of magnitude (or more) more type units
3358 than comp units, and we avoid them if we can. */
3359
3360 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3361 {
3362 int j;
e254ef6a 3363 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3364 struct quick_file_names *file_data;
9291a0cd 3365
3d7bb9d9 3366 /* We only need to look at symtabs not already expanded. */
e254ef6a 3367 if (per_cu->v.quick->symtab)
9291a0cd
TT
3368 continue;
3369
e4a48d9d 3370 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3371 if (file_data == NULL)
9291a0cd
TT
3372 continue;
3373
7b9f3c50 3374 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3375 {
652a8996
JK
3376 const char *this_fullname = file_data->file_names[j];
3377
3378 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3379 {
a0f42c21 3380 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3381 break;
3382 }
3383 }
3384 }
3385}
3386
356d9f9d
TT
3387/* A helper function for dw2_find_symbol_file that finds the primary
3388 file name for a given CU. This is a die_reader_func. */
3389
3390static void
3391dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
d521ce57 3392 const gdb_byte *info_ptr,
356d9f9d
TT
3393 struct die_info *comp_unit_die,
3394 int has_children,
3395 void *data)
3396{
3397 const char **result_ptr = data;
3398 struct dwarf2_cu *cu = reader->cu;
3399 struct attribute *attr;
3400
3401 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3402 if (attr == NULL)
3403 *result_ptr = NULL;
3404 else
3405 *result_ptr = DW_STRING (attr);
3406}
3407
dd786858 3408static const char *
9291a0cd
TT
3409dw2_find_symbol_file (struct objfile *objfile, const char *name)
3410{
e254ef6a 3411 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3412 offset_type *vec;
356d9f9d 3413 const char *filename;
9291a0cd
TT
3414
3415 dw2_setup (objfile);
3416
ae2de4f8 3417 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3418 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3419 {
3420 struct symtab *s;
3421
d790cf0a
DE
3422 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3423 {
3424 struct blockvector *bv = BLOCKVECTOR (s);
3425 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3426 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3427
3428 if (sym)
652a8996
JK
3429 {
3430 /* Only file extension of returned filename is recognized. */
3431 return SYMBOL_SYMTAB (sym)->filename;
3432 }
d790cf0a 3433 }
96408a79
SA
3434 return NULL;
3435 }
9291a0cd
TT
3436
3437 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3438 name, &vec))
3439 return NULL;
3440
3441 /* Note that this just looks at the very first one named NAME -- but
3442 actually we are looking for a function. find_main_filename
3443 should be rewritten so that it doesn't require a custom hook. It
3444 could just use the ordinary symbol tables. */
3445 /* vec[0] is the length, which must always be >0. */
156942c7 3446 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3447
356d9f9d 3448 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3449 {
3450 /* Only file extension of returned filename is recognized. */
3451 return per_cu->v.quick->symtab->filename;
3452 }
356d9f9d 3453
f4dc4d17
DE
3454 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3455 dw2_get_primary_filename_reader, &filename);
9291a0cd 3456
652a8996 3457 /* Only file extension of returned filename is recognized. */
356d9f9d 3458 return filename;
9291a0cd
TT
3459}
3460
3461static void
40658b94
PH
3462dw2_map_matching_symbols (const char * name, domain_enum namespace,
3463 struct objfile *objfile, int global,
3464 int (*callback) (struct block *,
3465 struct symbol *, void *),
2edb89d3
JK
3466 void *data, symbol_compare_ftype *match,
3467 symbol_compare_ftype *ordered_compare)
9291a0cd 3468{
40658b94 3469 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3470 current language is Ada for a non-Ada objfile using GNU index. As Ada
3471 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3472}
3473
3474static void
f8eba3c6
TT
3475dw2_expand_symtabs_matching
3476 (struct objfile *objfile,
fbd9ab74 3477 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3478 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3479 enum search_domain kind,
3480 void *data)
9291a0cd
TT
3481{
3482 int i;
3483 offset_type iter;
4b5246aa 3484 struct mapped_index *index;
9291a0cd
TT
3485
3486 dw2_setup (objfile);
ae2de4f8
DE
3487
3488 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3489 if (!dwarf2_per_objfile->index_table)
3490 return;
4b5246aa 3491 index = dwarf2_per_objfile->index_table;
9291a0cd 3492
7b08b9eb 3493 if (file_matcher != NULL)
24c79950
TT
3494 {
3495 struct cleanup *cleanup;
3496 htab_t visited_found, visited_not_found;
3497
3498 visited_found = htab_create_alloc (10,
3499 htab_hash_pointer, htab_eq_pointer,
3500 NULL, xcalloc, xfree);
3501 cleanup = make_cleanup_htab_delete (visited_found);
3502 visited_not_found = htab_create_alloc (10,
3503 htab_hash_pointer, htab_eq_pointer,
3504 NULL, xcalloc, xfree);
3505 make_cleanup_htab_delete (visited_not_found);
3506
848e3e78
DE
3507 /* The rule is CUs specify all the files, including those used by
3508 any TU, so there's no need to scan TUs here. */
3509
3510 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3511 {
3512 int j;
f4dc4d17 3513 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3514 struct quick_file_names *file_data;
3515 void **slot;
7b08b9eb 3516
24c79950 3517 per_cu->v.quick->mark = 0;
3d7bb9d9 3518
24c79950
TT
3519 /* We only need to look at symtabs not already expanded. */
3520 if (per_cu->v.quick->symtab)
3521 continue;
7b08b9eb 3522
e4a48d9d 3523 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3524 if (file_data == NULL)
3525 continue;
7b08b9eb 3526
24c79950
TT
3527 if (htab_find (visited_not_found, file_data) != NULL)
3528 continue;
3529 else if (htab_find (visited_found, file_data) != NULL)
3530 {
3531 per_cu->v.quick->mark = 1;
3532 continue;
3533 }
3534
3535 for (j = 0; j < file_data->num_file_names; ++j)
3536 {
da235a7c
JK
3537 const char *this_real_name;
3538
fbd9ab74 3539 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3540 {
3541 per_cu->v.quick->mark = 1;
3542 break;
3543 }
da235a7c
JK
3544
3545 /* Before we invoke realpath, which can get expensive when many
3546 files are involved, do a quick comparison of the basenames. */
3547 if (!basenames_may_differ
3548 && !file_matcher (lbasename (file_data->file_names[j]),
3549 data, 1))
3550 continue;
3551
3552 this_real_name = dw2_get_real_path (objfile, file_data, j);
3553 if (file_matcher (this_real_name, data, 0))
3554 {
3555 per_cu->v.quick->mark = 1;
3556 break;
3557 }
24c79950
TT
3558 }
3559
3560 slot = htab_find_slot (per_cu->v.quick->mark
3561 ? visited_found
3562 : visited_not_found,
3563 file_data, INSERT);
3564 *slot = file_data;
3565 }
3566
3567 do_cleanups (cleanup);
3568 }
9291a0cd 3569
3876f04e 3570 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3571 {
3572 offset_type idx = 2 * iter;
3573 const char *name;
3574 offset_type *vec, vec_len, vec_idx;
3575
3876f04e 3576 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3577 continue;
3578
3876f04e 3579 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3580
e078317b 3581 if (! (*name_matcher) (name, data))
9291a0cd
TT
3582 continue;
3583
3584 /* The name was matched, now expand corresponding CUs that were
3585 marked. */
4b5246aa 3586 vec = (offset_type *) (index->constant_pool
3876f04e 3587 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3588 vec_len = MAYBE_SWAP (vec[0]);
3589 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3590 {
e254ef6a 3591 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3592 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3593 gdb_index_symbol_kind symbol_kind =
3594 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3595 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3596
3597 /* Don't crash on bad data. */
3598 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3599 + dwarf2_per_objfile->n_type_units))
156942c7 3600 continue;
1fd400ff 3601
156942c7
DE
3602 /* Only check the symbol's kind if it has one.
3603 Indices prior to version 7 don't record it. */
3604 if (index->version >= 7)
3605 {
3606 switch (kind)
3607 {
3608 case VARIABLES_DOMAIN:
3609 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3610 continue;
3611 break;
3612 case FUNCTIONS_DOMAIN:
3613 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3614 continue;
3615 break;
3616 case TYPES_DOMAIN:
3617 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3618 continue;
3619 break;
3620 default:
3621 break;
3622 }
3623 }
3624
3625 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3626 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3627 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3628 }
3629 }
3630}
3631
9703b513
TT
3632/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3633 symtab. */
3634
3635static struct symtab *
3636recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3637{
3638 int i;
3639
3640 if (BLOCKVECTOR (symtab) != NULL
3641 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3642 return symtab;
3643
a3ec0bb1
DE
3644 if (symtab->includes == NULL)
3645 return NULL;
3646
9703b513
TT
3647 for (i = 0; symtab->includes[i]; ++i)
3648 {
a3ec0bb1 3649 struct symtab *s = symtab->includes[i];
9703b513
TT
3650
3651 s = recursively_find_pc_sect_symtab (s, pc);
3652 if (s != NULL)
3653 return s;
3654 }
3655
3656 return NULL;
3657}
3658
9291a0cd
TT
3659static struct symtab *
3660dw2_find_pc_sect_symtab (struct objfile *objfile,
3661 struct minimal_symbol *msymbol,
3662 CORE_ADDR pc,
3663 struct obj_section *section,
3664 int warn_if_readin)
3665{
3666 struct dwarf2_per_cu_data *data;
9703b513 3667 struct symtab *result;
9291a0cd
TT
3668
3669 dw2_setup (objfile);
3670
3671 if (!objfile->psymtabs_addrmap)
3672 return NULL;
3673
3674 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3675 if (!data)
3676 return NULL;
3677
3678 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3679 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3680 paddress (get_objfile_arch (objfile), pc));
3681
9703b513
TT
3682 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3683 gdb_assert (result != NULL);
3684 return result;
9291a0cd
TT
3685}
3686
9291a0cd 3687static void
44b13c5a 3688dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3689 void *data, int need_fullname)
9291a0cd
TT
3690{
3691 int i;
24c79950
TT
3692 struct cleanup *cleanup;
3693 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3694 NULL, xcalloc, xfree);
9291a0cd 3695
24c79950 3696 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3697 dw2_setup (objfile);
ae2de4f8 3698
848e3e78
DE
3699 /* The rule is CUs specify all the files, including those used by
3700 any TU, so there's no need to scan TUs here.
3701 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3702
848e3e78 3703 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3704 {
3705 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3706
3707 if (per_cu->v.quick->symtab)
3708 {
3709 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3710 INSERT);
3711
3712 *slot = per_cu->v.quick->file_names;
3713 }
3714 }
3715
848e3e78 3716 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3717 {
3718 int j;
f4dc4d17 3719 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3720 struct quick_file_names *file_data;
24c79950 3721 void **slot;
9291a0cd 3722
3d7bb9d9 3723 /* We only need to look at symtabs not already expanded. */
e254ef6a 3724 if (per_cu->v.quick->symtab)
9291a0cd
TT
3725 continue;
3726
e4a48d9d 3727 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3728 if (file_data == NULL)
9291a0cd
TT
3729 continue;
3730
24c79950
TT
3731 slot = htab_find_slot (visited, file_data, INSERT);
3732 if (*slot)
3733 {
3734 /* Already visited. */
3735 continue;
3736 }
3737 *slot = file_data;
3738
7b9f3c50 3739 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3740 {
74e2f255
DE
3741 const char *this_real_name;
3742
3743 if (need_fullname)
3744 this_real_name = dw2_get_real_path (objfile, file_data, j);
3745 else
3746 this_real_name = NULL;
7b9f3c50 3747 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3748 }
3749 }
24c79950
TT
3750
3751 do_cleanups (cleanup);
9291a0cd
TT
3752}
3753
3754static int
3755dw2_has_symbols (struct objfile *objfile)
3756{
3757 return 1;
3758}
3759
3760const struct quick_symbol_functions dwarf2_gdb_index_functions =
3761{
3762 dw2_has_symbols,
3763 dw2_find_last_source_symtab,
3764 dw2_forget_cached_source_info,
f8eba3c6 3765 dw2_map_symtabs_matching_filename,
9291a0cd 3766 dw2_lookup_symbol,
9291a0cd
TT
3767 dw2_print_stats,
3768 dw2_dump,
3769 dw2_relocate,
3770 dw2_expand_symtabs_for_function,
3771 dw2_expand_all_symtabs,
652a8996 3772 dw2_expand_symtabs_with_fullname,
9291a0cd 3773 dw2_find_symbol_file,
40658b94 3774 dw2_map_matching_symbols,
9291a0cd
TT
3775 dw2_expand_symtabs_matching,
3776 dw2_find_pc_sect_symtab,
9291a0cd
TT
3777 dw2_map_symbol_filenames
3778};
3779
3780/* Initialize for reading DWARF for this objfile. Return 0 if this
3781 file will use psymtabs, or 1 if using the GNU index. */
3782
3783int
3784dwarf2_initialize_objfile (struct objfile *objfile)
3785{
3786 /* If we're about to read full symbols, don't bother with the
3787 indices. In this case we also don't care if some other debug
3788 format is making psymtabs, because they are all about to be
3789 expanded anyway. */
3790 if ((objfile->flags & OBJF_READNOW))
3791 {
3792 int i;
3793
3794 dwarf2_per_objfile->using_index = 1;
3795 create_all_comp_units (objfile);
0e50663e 3796 create_all_type_units (objfile);
7b9f3c50
DE
3797 dwarf2_per_objfile->quick_file_names_table =
3798 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3799
1fd400ff 3800 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3801 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3802 {
e254ef6a 3803 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3804
e254ef6a
DE
3805 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3806 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3807 }
3808
3809 /* Return 1 so that gdb sees the "quick" functions. However,
3810 these functions will be no-ops because we will have expanded
3811 all symtabs. */
3812 return 1;
3813 }
3814
3815 if (dwarf2_read_index (objfile))
3816 return 1;
3817
9291a0cd
TT
3818 return 0;
3819}
3820
3821\f
3822
dce234bc
PP
3823/* Build a partial symbol table. */
3824
3825void
f29dff0a 3826dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3827{
c9bf0622
TT
3828 volatile struct gdb_exception except;
3829
f29dff0a 3830 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3831 {
3832 init_psymbol_list (objfile, 1024);
3833 }
3834
c9bf0622
TT
3835 TRY_CATCH (except, RETURN_MASK_ERROR)
3836 {
3837 /* This isn't really ideal: all the data we allocate on the
3838 objfile's obstack is still uselessly kept around. However,
3839 freeing it seems unsafe. */
3840 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3841
3842 dwarf2_build_psymtabs_hard (objfile);
3843 discard_cleanups (cleanups);
3844 }
3845 if (except.reason < 0)
3846 exception_print (gdb_stderr, except);
c906108c 3847}
c906108c 3848
1ce1cefd
DE
3849/* Return the total length of the CU described by HEADER. */
3850
3851static unsigned int
3852get_cu_length (const struct comp_unit_head *header)
3853{
3854 return header->initial_length_size + header->length;
3855}
3856
45452591
DE
3857/* Return TRUE if OFFSET is within CU_HEADER. */
3858
3859static inline int
b64f50a1 3860offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3861{
b64f50a1 3862 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3863 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3864
b64f50a1 3865 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3866}
3867
3b80fe9b
DE
3868/* Find the base address of the compilation unit for range lists and
3869 location lists. It will normally be specified by DW_AT_low_pc.
3870 In DWARF-3 draft 4, the base address could be overridden by
3871 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3872 compilation units with discontinuous ranges. */
3873
3874static void
3875dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3876{
3877 struct attribute *attr;
3878
3879 cu->base_known = 0;
3880 cu->base_address = 0;
3881
3882 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3883 if (attr)
3884 {
3885 cu->base_address = DW_ADDR (attr);
3886 cu->base_known = 1;
3887 }
3888 else
3889 {
3890 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3891 if (attr)
3892 {
3893 cu->base_address = DW_ADDR (attr);
3894 cu->base_known = 1;
3895 }
3896 }
3897}
3898
93311388
DE
3899/* Read in the comp unit header information from the debug_info at info_ptr.
3900 NOTE: This leaves members offset, first_die_offset to be filled in
3901 by the caller. */
107d2387 3902
d521ce57 3903static const gdb_byte *
107d2387 3904read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3905 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3906{
3907 int signed_addr;
891d2f0b 3908 unsigned int bytes_read;
c764a876
DE
3909
3910 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3911 cu_header->initial_length_size = bytes_read;
3912 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3913 info_ptr += bytes_read;
107d2387
AC
3914 cu_header->version = read_2_bytes (abfd, info_ptr);
3915 info_ptr += 2;
b64f50a1
JK
3916 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3917 &bytes_read);
613e1657 3918 info_ptr += bytes_read;
107d2387
AC
3919 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3920 info_ptr += 1;
3921 signed_addr = bfd_get_sign_extend_vma (abfd);
3922 if (signed_addr < 0)
8e65ff28 3923 internal_error (__FILE__, __LINE__,
e2e0b3e5 3924 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3925 cu_header->signed_addr_p = signed_addr;
c764a876 3926
107d2387
AC
3927 return info_ptr;
3928}
3929
36586728
TT
3930/* Helper function that returns the proper abbrev section for
3931 THIS_CU. */
3932
3933static struct dwarf2_section_info *
3934get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3935{
3936 struct dwarf2_section_info *abbrev;
3937
3938 if (this_cu->is_dwz)
3939 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3940 else
3941 abbrev = &dwarf2_per_objfile->abbrev;
3942
3943 return abbrev;
3944}
3945
9ff913ba
DE
3946/* Subroutine of read_and_check_comp_unit_head and
3947 read_and_check_type_unit_head to simplify them.
3948 Perform various error checking on the header. */
3949
3950static void
3951error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3952 struct dwarf2_section_info *section,
3953 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3954{
3955 bfd *abfd = section->asection->owner;
3956 const char *filename = bfd_get_filename (abfd);
3957
3958 if (header->version != 2 && header->version != 3 && header->version != 4)
3959 error (_("Dwarf Error: wrong version in compilation unit header "
3960 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3961 filename);
3962
b64f50a1 3963 if (header->abbrev_offset.sect_off
36586728 3964 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3965 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3966 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3967 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3968 filename);
3969
3970 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3971 avoid potential 32-bit overflow. */
1ce1cefd 3972 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3973 > section->size)
3974 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3975 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3976 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3977 filename);
3978}
3979
3980/* Read in a CU/TU header and perform some basic error checking.
3981 The contents of the header are stored in HEADER.
3982 The result is a pointer to the start of the first DIE. */
adabb602 3983
d521ce57 3984static const gdb_byte *
9ff913ba
DE
3985read_and_check_comp_unit_head (struct comp_unit_head *header,
3986 struct dwarf2_section_info *section,
4bdcc0c1 3987 struct dwarf2_section_info *abbrev_section,
d521ce57 3988 const gdb_byte *info_ptr,
9ff913ba 3989 int is_debug_types_section)
72bf9492 3990{
d521ce57 3991 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3992 bfd *abfd = section->asection->owner;
72bf9492 3993
b64f50a1 3994 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3995
72bf9492
DJ
3996 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3997
460c1c54
CC
3998 /* If we're reading a type unit, skip over the signature and
3999 type_offset fields. */
b0df02fd 4000 if (is_debug_types_section)
460c1c54
CC
4001 info_ptr += 8 /*signature*/ + header->offset_size;
4002
b64f50a1 4003 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4004
4bdcc0c1 4005 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4006
4007 return info_ptr;
4008}
4009
348e048f
DE
4010/* Read in the types comp unit header information from .debug_types entry at
4011 types_ptr. The result is a pointer to one past the end of the header. */
4012
d521ce57 4013static const gdb_byte *
9ff913ba
DE
4014read_and_check_type_unit_head (struct comp_unit_head *header,
4015 struct dwarf2_section_info *section,
4bdcc0c1 4016 struct dwarf2_section_info *abbrev_section,
d521ce57 4017 const gdb_byte *info_ptr,
dee91e82
DE
4018 ULONGEST *signature,
4019 cu_offset *type_offset_in_tu)
348e048f 4020{
d521ce57 4021 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4022 bfd *abfd = section->asection->owner;
348e048f 4023
b64f50a1 4024 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4025
9ff913ba 4026 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4027
9ff913ba
DE
4028 /* If we're reading a type unit, skip over the signature and
4029 type_offset fields. */
4030 if (signature != NULL)
4031 *signature = read_8_bytes (abfd, info_ptr);
4032 info_ptr += 8;
dee91e82
DE
4033 if (type_offset_in_tu != NULL)
4034 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4035 header->offset_size);
9ff913ba
DE
4036 info_ptr += header->offset_size;
4037
b64f50a1 4038 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4039
4bdcc0c1 4040 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4041
4042 return info_ptr;
348e048f
DE
4043}
4044
f4dc4d17
DE
4045/* Fetch the abbreviation table offset from a comp or type unit header. */
4046
4047static sect_offset
4048read_abbrev_offset (struct dwarf2_section_info *section,
4049 sect_offset offset)
4050{
4051 bfd *abfd = section->asection->owner;
d521ce57 4052 const gdb_byte *info_ptr;
f4dc4d17
DE
4053 unsigned int length, initial_length_size, offset_size;
4054 sect_offset abbrev_offset;
4055
4056 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4057 info_ptr = section->buffer + offset.sect_off;
4058 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4059 offset_size = initial_length_size == 4 ? 4 : 8;
4060 info_ptr += initial_length_size + 2 /*version*/;
4061 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4062 return abbrev_offset;
4063}
4064
aaa75496
JB
4065/* Allocate a new partial symtab for file named NAME and mark this new
4066 partial symtab as being an include of PST. */
4067
4068static void
d521ce57 4069dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4070 struct objfile *objfile)
4071{
4072 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4073
fbd9ab74
JK
4074 if (!IS_ABSOLUTE_PATH (subpst->filename))
4075 {
4076 /* It shares objfile->objfile_obstack. */
4077 subpst->dirname = pst->dirname;
4078 }
4079
aaa75496
JB
4080 subpst->section_offsets = pst->section_offsets;
4081 subpst->textlow = 0;
4082 subpst->texthigh = 0;
4083
4084 subpst->dependencies = (struct partial_symtab **)
4085 obstack_alloc (&objfile->objfile_obstack,
4086 sizeof (struct partial_symtab *));
4087 subpst->dependencies[0] = pst;
4088 subpst->number_of_dependencies = 1;
4089
4090 subpst->globals_offset = 0;
4091 subpst->n_global_syms = 0;
4092 subpst->statics_offset = 0;
4093 subpst->n_static_syms = 0;
4094 subpst->symtab = NULL;
4095 subpst->read_symtab = pst->read_symtab;
4096 subpst->readin = 0;
4097
4098 /* No private part is necessary for include psymtabs. This property
4099 can be used to differentiate between such include psymtabs and
10b3939b 4100 the regular ones. */
58a9656e 4101 subpst->read_symtab_private = NULL;
aaa75496
JB
4102}
4103
4104/* Read the Line Number Program data and extract the list of files
4105 included by the source file represented by PST. Build an include
d85a05f0 4106 partial symtab for each of these included files. */
aaa75496
JB
4107
4108static void
4109dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4110 struct die_info *die,
4111 struct partial_symtab *pst)
aaa75496 4112{
d85a05f0
DJ
4113 struct line_header *lh = NULL;
4114 struct attribute *attr;
aaa75496 4115
d85a05f0
DJ
4116 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4117 if (attr)
3019eac3 4118 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4119 if (lh == NULL)
4120 return; /* No linetable, so no includes. */
4121
c6da4cef 4122 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4123 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4124
4125 free_line_header (lh);
4126}
4127
348e048f 4128static hashval_t
52dc124a 4129hash_signatured_type (const void *item)
348e048f 4130{
52dc124a 4131 const struct signatured_type *sig_type = item;
9a619af0 4132
348e048f 4133 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4134 return sig_type->signature;
348e048f
DE
4135}
4136
4137static int
52dc124a 4138eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4139{
4140 const struct signatured_type *lhs = item_lhs;
4141 const struct signatured_type *rhs = item_rhs;
9a619af0 4142
348e048f
DE
4143 return lhs->signature == rhs->signature;
4144}
4145
1fd400ff
TT
4146/* Allocate a hash table for signatured types. */
4147
4148static htab_t
673bfd45 4149allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4150{
4151 return htab_create_alloc_ex (41,
52dc124a
DE
4152 hash_signatured_type,
4153 eq_signatured_type,
1fd400ff
TT
4154 NULL,
4155 &objfile->objfile_obstack,
4156 hashtab_obstack_allocate,
4157 dummy_obstack_deallocate);
4158}
4159
d467dd73 4160/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4161
4162static int
d467dd73 4163add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4164{
4165 struct signatured_type *sigt = *slot;
b4dd5633 4166 struct signatured_type ***datap = datum;
1fd400ff 4167
b4dd5633 4168 **datap = sigt;
1fd400ff
TT
4169 ++*datap;
4170
4171 return 1;
4172}
4173
c88ee1f0
DE
4174/* Create the hash table of all entries in the .debug_types
4175 (or .debug_types.dwo) section(s).
4176 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4177 otherwise it is NULL.
4178
4179 The result is a pointer to the hash table or NULL if there are no types.
4180
4181 Note: This function processes DWO files only, not DWP files. */
348e048f 4182
3019eac3
DE
4183static htab_t
4184create_debug_types_hash_table (struct dwo_file *dwo_file,
4185 VEC (dwarf2_section_info_def) *types)
348e048f 4186{
3019eac3 4187 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4188 htab_t types_htab = NULL;
8b70b953
TT
4189 int ix;
4190 struct dwarf2_section_info *section;
4bdcc0c1 4191 struct dwarf2_section_info *abbrev_section;
348e048f 4192
3019eac3
DE
4193 if (VEC_empty (dwarf2_section_info_def, types))
4194 return NULL;
348e048f 4195
4bdcc0c1
DE
4196 abbrev_section = (dwo_file != NULL
4197 ? &dwo_file->sections.abbrev
4198 : &dwarf2_per_objfile->abbrev);
4199
09406207
DE
4200 if (dwarf2_read_debug)
4201 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4202 dwo_file ? ".dwo" : "",
4203 bfd_get_filename (abbrev_section->asection->owner));
4204
8b70b953 4205 for (ix = 0;
3019eac3 4206 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4207 ++ix)
4208 {
3019eac3 4209 bfd *abfd;
d521ce57 4210 const gdb_byte *info_ptr, *end_ptr;
36586728 4211 struct dwarf2_section_info *abbrev_section;
348e048f 4212
8b70b953
TT
4213 dwarf2_read_section (objfile, section);
4214 info_ptr = section->buffer;
348e048f 4215
8b70b953
TT
4216 if (info_ptr == NULL)
4217 continue;
348e048f 4218
3019eac3
DE
4219 /* We can't set abfd until now because the section may be empty or
4220 not present, in which case section->asection will be NULL. */
4221 abfd = section->asection->owner;
4222
36586728
TT
4223 if (dwo_file)
4224 abbrev_section = &dwo_file->sections.abbrev;
4225 else
4226 abbrev_section = &dwarf2_per_objfile->abbrev;
4227
dee91e82
DE
4228 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4229 because we don't need to read any dies: the signature is in the
4230 header. */
8b70b953
TT
4231
4232 end_ptr = info_ptr + section->size;
4233 while (info_ptr < end_ptr)
4234 {
b64f50a1 4235 sect_offset offset;
3019eac3 4236 cu_offset type_offset_in_tu;
8b70b953 4237 ULONGEST signature;
52dc124a 4238 struct signatured_type *sig_type;
3019eac3 4239 struct dwo_unit *dwo_tu;
8b70b953 4240 void **slot;
d521ce57 4241 const gdb_byte *ptr = info_ptr;
9ff913ba 4242 struct comp_unit_head header;
dee91e82 4243 unsigned int length;
348e048f 4244
b64f50a1 4245 offset.sect_off = ptr - section->buffer;
348e048f 4246
8b70b953 4247 /* We need to read the type's signature in order to build the hash
9ff913ba 4248 table, but we don't need anything else just yet. */
348e048f 4249
4bdcc0c1
DE
4250 ptr = read_and_check_type_unit_head (&header, section,
4251 abbrev_section, ptr,
3019eac3 4252 &signature, &type_offset_in_tu);
6caca83c 4253
1ce1cefd 4254 length = get_cu_length (&header);
dee91e82 4255
6caca83c 4256 /* Skip dummy type units. */
dee91e82
DE
4257 if (ptr >= info_ptr + length
4258 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4259 {
1ce1cefd 4260 info_ptr += length;
6caca83c
CC
4261 continue;
4262 }
8b70b953 4263
0349ea22
DE
4264 if (types_htab == NULL)
4265 {
4266 if (dwo_file)
4267 types_htab = allocate_dwo_unit_table (objfile);
4268 else
4269 types_htab = allocate_signatured_type_table (objfile);
4270 }
4271
3019eac3
DE
4272 if (dwo_file)
4273 {
4274 sig_type = NULL;
4275 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4276 struct dwo_unit);
4277 dwo_tu->dwo_file = dwo_file;
4278 dwo_tu->signature = signature;
4279 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4280 dwo_tu->section = section;
3019eac3
DE
4281 dwo_tu->offset = offset;
4282 dwo_tu->length = length;
4283 }
4284 else
4285 {
4286 /* N.B.: type_offset is not usable if this type uses a DWO file.
4287 The real type_offset is in the DWO file. */
4288 dwo_tu = NULL;
4289 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4290 struct signatured_type);
4291 sig_type->signature = signature;
4292 sig_type->type_offset_in_tu = type_offset_in_tu;
4293 sig_type->per_cu.objfile = objfile;
4294 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4295 sig_type->per_cu.section = section;
3019eac3
DE
4296 sig_type->per_cu.offset = offset;
4297 sig_type->per_cu.length = length;
4298 }
8b70b953 4299
3019eac3
DE
4300 slot = htab_find_slot (types_htab,
4301 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4302 INSERT);
8b70b953
TT
4303 gdb_assert (slot != NULL);
4304 if (*slot != NULL)
4305 {
3019eac3
DE
4306 sect_offset dup_offset;
4307
4308 if (dwo_file)
4309 {
4310 const struct dwo_unit *dup_tu = *slot;
4311
4312 dup_offset = dup_tu->offset;
4313 }
4314 else
4315 {
4316 const struct signatured_type *dup_tu = *slot;
4317
4318 dup_offset = dup_tu->per_cu.offset;
4319 }
b3c8eb43 4320
8b70b953 4321 complaint (&symfile_complaints,
c88ee1f0 4322 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4323 " the entry at offset 0x%x, signature %s"),
3019eac3 4324 offset.sect_off, dup_offset.sect_off,
4031ecc5 4325 hex_string (signature));
8b70b953 4326 }
3019eac3 4327 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4328
09406207 4329 if (dwarf2_read_debug)
4031ecc5 4330 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4331 offset.sect_off,
4031ecc5 4332 hex_string (signature));
348e048f 4333
dee91e82 4334 info_ptr += length;
8b70b953 4335 }
348e048f
DE
4336 }
4337
3019eac3
DE
4338 return types_htab;
4339}
4340
4341/* Create the hash table of all entries in the .debug_types section,
4342 and initialize all_type_units.
4343 The result is zero if there is an error (e.g. missing .debug_types section),
4344 otherwise non-zero. */
4345
4346static int
4347create_all_type_units (struct objfile *objfile)
4348{
4349 htab_t types_htab;
b4dd5633 4350 struct signatured_type **iter;
3019eac3
DE
4351
4352 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4353 if (types_htab == NULL)
4354 {
4355 dwarf2_per_objfile->signatured_types = NULL;
4356 return 0;
4357 }
4358
348e048f
DE
4359 dwarf2_per_objfile->signatured_types = types_htab;
4360
d467dd73
DE
4361 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4362 dwarf2_per_objfile->all_type_units
1fd400ff 4363 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4364 dwarf2_per_objfile->n_type_units
b4dd5633 4365 * sizeof (struct signatured_type *));
d467dd73
DE
4366 iter = &dwarf2_per_objfile->all_type_units[0];
4367 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4368 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4369 == dwarf2_per_objfile->n_type_units);
1fd400ff 4370
348e048f
DE
4371 return 1;
4372}
4373
380bca97 4374/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4375 Returns NULL if signature SIG is not present in the table.
4376 It is up to the caller to complain about this. */
348e048f
DE
4377
4378static struct signatured_type *
e319fa28 4379lookup_signatured_type (ULONGEST sig)
348e048f
DE
4380{
4381 struct signatured_type find_entry, *entry;
4382
4383 if (dwarf2_per_objfile->signatured_types == NULL)
5a8b3f62 4384 return NULL;
348e048f
DE
4385 find_entry.signature = sig;
4386 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4387 return entry;
4388}
42e7ad6c
DE
4389\f
4390/* Low level DIE reading support. */
348e048f 4391
d85a05f0
DJ
4392/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4393
4394static void
4395init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4396 struct dwarf2_cu *cu,
3019eac3
DE
4397 struct dwarf2_section_info *section,
4398 struct dwo_file *dwo_file)
d85a05f0 4399{
fceca515 4400 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4401 reader->abfd = section->asection->owner;
d85a05f0 4402 reader->cu = cu;
3019eac3 4403 reader->dwo_file = dwo_file;
dee91e82
DE
4404 reader->die_section = section;
4405 reader->buffer = section->buffer;
f664829e 4406 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4407}
4408
b0c7bfa9
DE
4409/* Subroutine of init_cutu_and_read_dies to simplify it.
4410 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4411 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4412 already.
4413
4414 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4415 from it to the DIE in the DWO. If NULL we are skipping the stub.
4416 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4417 are filled in with the info of the DIE from the DWO file.
4418 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4419 provided an abbrev table to use.
4420 The result is non-zero if a valid (non-dummy) DIE was found. */
4421
4422static int
4423read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4424 struct dwo_unit *dwo_unit,
4425 int abbrev_table_provided,
4426 struct die_info *stub_comp_unit_die,
4427 struct die_reader_specs *result_reader,
d521ce57 4428 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4429 struct die_info **result_comp_unit_die,
4430 int *result_has_children)
4431{
4432 struct objfile *objfile = dwarf2_per_objfile->objfile;
4433 struct dwarf2_cu *cu = this_cu->cu;
4434 struct dwarf2_section_info *section;
4435 bfd *abfd;
d521ce57 4436 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4437 const char *comp_dir_string;
4438 ULONGEST signature; /* Or dwo_id. */
4439 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4440 int i,num_extra_attrs;
4441 struct dwarf2_section_info *dwo_abbrev_section;
4442 struct attribute *attr;
4443 struct die_info *comp_unit_die;
4444
4445 /* These attributes aren't processed until later:
4446 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4447 However, the attribute is found in the stub which we won't have later.
4448 In order to not impose this complication on the rest of the code,
4449 we read them here and copy them to the DWO CU/TU die. */
4450
4451 stmt_list = NULL;
4452 low_pc = NULL;
4453 high_pc = NULL;
4454 ranges = NULL;
4455 comp_dir = NULL;
4456
4457 if (stub_comp_unit_die != NULL)
4458 {
4459 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4460 DWO file. */
4461 if (! this_cu->is_debug_types)
4462 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4463 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4464 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4465 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4466 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4467
4468 /* There should be a DW_AT_addr_base attribute here (if needed).
4469 We need the value before we can process DW_FORM_GNU_addr_index. */
4470 cu->addr_base = 0;
4471 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4472 if (attr)
4473 cu->addr_base = DW_UNSND (attr);
4474
4475 /* There should be a DW_AT_ranges_base attribute here (if needed).
4476 We need the value before we can process DW_AT_ranges. */
4477 cu->ranges_base = 0;
4478 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4479 if (attr)
4480 cu->ranges_base = DW_UNSND (attr);
4481 }
4482
4483 /* Set up for reading the DWO CU/TU. */
4484 cu->dwo_unit = dwo_unit;
4485 section = dwo_unit->section;
4486 dwarf2_read_section (objfile, section);
4487 abfd = section->asection->owner;
4488 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4489 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4490 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4491
4492 if (this_cu->is_debug_types)
4493 {
4494 ULONGEST header_signature;
4495 cu_offset type_offset_in_tu;
4496 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4497
4498 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4499 dwo_abbrev_section,
4500 info_ptr,
4501 &header_signature,
4502 &type_offset_in_tu);
4503 gdb_assert (sig_type->signature == header_signature);
4504 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4505 /* For DWOs coming from DWP files, we don't know the CU length
4506 nor the type's offset in the TU until now. */
4507 dwo_unit->length = get_cu_length (&cu->header);
4508 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4509
4510 /* Establish the type offset that can be used to lookup the type.
4511 For DWO files, we don't know it until now. */
4512 sig_type->type_offset_in_section.sect_off =
4513 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4514 }
4515 else
4516 {
4517 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4518 dwo_abbrev_section,
4519 info_ptr, 0);
4520 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4521 /* For DWOs coming from DWP files, we don't know the CU length
4522 until now. */
4523 dwo_unit->length = get_cu_length (&cu->header);
4524 }
4525
02142a6c
DE
4526 /* Replace the CU's original abbrev table with the DWO's.
4527 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4528 if (abbrev_table_provided)
4529 {
4530 /* Don't free the provided abbrev table, the caller of
4531 init_cutu_and_read_dies owns it. */
4532 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4533 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4534 make_cleanup (dwarf2_free_abbrev_table, cu);
4535 }
4536 else
4537 {
4538 dwarf2_free_abbrev_table (cu);
4539 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4540 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4541 }
4542
4543 /* Read in the die, but leave space to copy over the attributes
4544 from the stub. This has the benefit of simplifying the rest of
4545 the code - all the work to maintain the illusion of a single
4546 DW_TAG_{compile,type}_unit DIE is done here. */
4547 num_extra_attrs = ((stmt_list != NULL)
4548 + (low_pc != NULL)
4549 + (high_pc != NULL)
4550 + (ranges != NULL)
4551 + (comp_dir != NULL));
4552 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4553 result_has_children, num_extra_attrs);
4554
4555 /* Copy over the attributes from the stub to the DIE we just read in. */
4556 comp_unit_die = *result_comp_unit_die;
4557 i = comp_unit_die->num_attrs;
4558 if (stmt_list != NULL)
4559 comp_unit_die->attrs[i++] = *stmt_list;
4560 if (low_pc != NULL)
4561 comp_unit_die->attrs[i++] = *low_pc;
4562 if (high_pc != NULL)
4563 comp_unit_die->attrs[i++] = *high_pc;
4564 if (ranges != NULL)
4565 comp_unit_die->attrs[i++] = *ranges;
4566 if (comp_dir != NULL)
4567 comp_unit_die->attrs[i++] = *comp_dir;
4568 comp_unit_die->num_attrs += num_extra_attrs;
4569
bf6af496
DE
4570 if (dwarf2_die_debug)
4571 {
4572 fprintf_unfiltered (gdb_stdlog,
4573 "Read die from %s@0x%x of %s:\n",
4574 bfd_section_name (abfd, section->asection),
4575 (unsigned) (begin_info_ptr - section->buffer),
4576 bfd_get_filename (abfd));
4577 dump_die (comp_unit_die, dwarf2_die_debug);
4578 }
4579
b0c7bfa9
DE
4580 /* Skip dummy compilation units. */
4581 if (info_ptr >= begin_info_ptr + dwo_unit->length
4582 || peek_abbrev_code (abfd, info_ptr) == 0)
4583 return 0;
4584
4585 *result_info_ptr = info_ptr;
4586 return 1;
4587}
4588
4589/* Subroutine of init_cutu_and_read_dies to simplify it.
4590 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4591 If the specified DWO unit cannot be found an error is thrown. */
4592
4593static struct dwo_unit *
4594lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4595 struct die_info *comp_unit_die)
4596{
4597 struct dwarf2_cu *cu = this_cu->cu;
4598 struct attribute *attr;
4599 ULONGEST signature;
4600 struct dwo_unit *dwo_unit;
4601 const char *comp_dir, *dwo_name;
4602
4603 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4604 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4605 gdb_assert (attr != NULL);
4606 dwo_name = DW_STRING (attr);
4607 comp_dir = NULL;
4608 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4609 if (attr)
4610 comp_dir = DW_STRING (attr);
4611
4612 if (this_cu->is_debug_types)
4613 {
4614 struct signatured_type *sig_type;
4615
4616 /* Since this_cu is the first member of struct signatured_type,
4617 we can go from a pointer to one to a pointer to the other. */
4618 sig_type = (struct signatured_type *) this_cu;
4619 signature = sig_type->signature;
4620 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4621 }
4622 else
4623 {
4624 struct attribute *attr;
4625
4626 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4627 if (! attr)
4628 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4629 " [in module %s]"),
4630 dwo_name, this_cu->objfile->name);
4631 signature = DW_UNSND (attr);
4632 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4633 signature);
4634 }
4635
4636 if (dwo_unit == NULL)
4637 {
4638 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4639 " with ID %s [in module %s]"),
4031ecc5 4640 this_cu->offset.sect_off, hex_string (signature),
b0c7bfa9
DE
4641 this_cu->objfile->name);
4642 }
4643
4644 return dwo_unit;
4645}
4646
fd820528 4647/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4648 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4649
f4dc4d17
DE
4650 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4651 Otherwise the table specified in the comp unit header is read in and used.
4652 This is an optimization for when we already have the abbrev table.
4653
dee91e82
DE
4654 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4655 Otherwise, a new CU is allocated with xmalloc.
4656
4657 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4658 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4659
4660 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4661 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4662
70221824 4663static void
fd820528 4664init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4665 struct abbrev_table *abbrev_table,
fd820528
DE
4666 int use_existing_cu, int keep,
4667 die_reader_func_ftype *die_reader_func,
4668 void *data)
c906108c 4669{
dee91e82 4670 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4671 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4672 bfd *abfd = section->asection->owner;
dee91e82 4673 struct dwarf2_cu *cu;
d521ce57 4674 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4675 struct die_reader_specs reader;
d85a05f0 4676 struct die_info *comp_unit_die;
dee91e82 4677 int has_children;
d85a05f0 4678 struct attribute *attr;
dee91e82
DE
4679 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4680 struct signatured_type *sig_type = NULL;
4bdcc0c1 4681 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4682 /* Non-zero if CU currently points to a DWO file and we need to
4683 reread it. When this happens we need to reread the skeleton die
4684 before we can reread the DWO file. */
4685 int rereading_dwo_cu = 0;
c906108c 4686
09406207
DE
4687 if (dwarf2_die_debug)
4688 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4689 this_cu->is_debug_types ? "type" : "comp",
4690 this_cu->offset.sect_off);
4691
dee91e82
DE
4692 if (use_existing_cu)
4693 gdb_assert (keep);
23745b47 4694
dee91e82
DE
4695 cleanups = make_cleanup (null_cleanup, NULL);
4696
4697 /* This is cheap if the section is already read in. */
4698 dwarf2_read_section (objfile, section);
4699
4700 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4701
4702 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4703
4704 if (use_existing_cu && this_cu->cu != NULL)
4705 {
4706 cu = this_cu->cu;
42e7ad6c
DE
4707
4708 /* If this CU is from a DWO file we need to start over, we need to
4709 refetch the attributes from the skeleton CU.
4710 This could be optimized by retrieving those attributes from when we
4711 were here the first time: the previous comp_unit_die was stored in
4712 comp_unit_obstack. But there's no data yet that we need this
4713 optimization. */
4714 if (cu->dwo_unit != NULL)
4715 rereading_dwo_cu = 1;
dee91e82
DE
4716 }
4717 else
4718 {
4719 /* If !use_existing_cu, this_cu->cu must be NULL. */
4720 gdb_assert (this_cu->cu == NULL);
4721
4722 cu = xmalloc (sizeof (*cu));
4723 init_one_comp_unit (cu, this_cu);
4724
4725 /* If an error occurs while loading, release our storage. */
4726 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4727 }
dee91e82 4728
b0c7bfa9 4729 /* Get the header. */
42e7ad6c
DE
4730 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4731 {
4732 /* We already have the header, there's no need to read it in again. */
4733 info_ptr += cu->header.first_die_offset.cu_off;
4734 }
4735 else
4736 {
3019eac3 4737 if (this_cu->is_debug_types)
dee91e82
DE
4738 {
4739 ULONGEST signature;
42e7ad6c 4740 cu_offset type_offset_in_tu;
dee91e82 4741
4bdcc0c1
DE
4742 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4743 abbrev_section, info_ptr,
42e7ad6c
DE
4744 &signature,
4745 &type_offset_in_tu);
dee91e82 4746
42e7ad6c
DE
4747 /* Since per_cu is the first member of struct signatured_type,
4748 we can go from a pointer to one to a pointer to the other. */
4749 sig_type = (struct signatured_type *) this_cu;
4750 gdb_assert (sig_type->signature == signature);
4751 gdb_assert (sig_type->type_offset_in_tu.cu_off
4752 == type_offset_in_tu.cu_off);
dee91e82
DE
4753 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4754
42e7ad6c
DE
4755 /* LENGTH has not been set yet for type units if we're
4756 using .gdb_index. */
1ce1cefd 4757 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4758
4759 /* Establish the type offset that can be used to lookup the type. */
4760 sig_type->type_offset_in_section.sect_off =
4761 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4762 }
4763 else
4764 {
4bdcc0c1
DE
4765 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4766 abbrev_section,
4767 info_ptr, 0);
dee91e82
DE
4768
4769 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4770 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4771 }
4772 }
10b3939b 4773
6caca83c 4774 /* Skip dummy compilation units. */
dee91e82 4775 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4776 || peek_abbrev_code (abfd, info_ptr) == 0)
4777 {
dee91e82 4778 do_cleanups (cleanups);
21b2bd31 4779 return;
6caca83c
CC
4780 }
4781
433df2d4
DE
4782 /* If we don't have them yet, read the abbrevs for this compilation unit.
4783 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4784 done. Note that it's important that if the CU had an abbrev table
4785 on entry we don't free it when we're done: Somewhere up the call stack
4786 it may be in use. */
f4dc4d17
DE
4787 if (abbrev_table != NULL)
4788 {
4789 gdb_assert (cu->abbrev_table == NULL);
4790 gdb_assert (cu->header.abbrev_offset.sect_off
4791 == abbrev_table->offset.sect_off);
4792 cu->abbrev_table = abbrev_table;
4793 }
4794 else if (cu->abbrev_table == NULL)
dee91e82 4795 {
4bdcc0c1 4796 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4797 make_cleanup (dwarf2_free_abbrev_table, cu);
4798 }
42e7ad6c
DE
4799 else if (rereading_dwo_cu)
4800 {
4801 dwarf2_free_abbrev_table (cu);
4802 dwarf2_read_abbrevs (cu, abbrev_section);
4803 }
af703f96 4804
dee91e82 4805 /* Read the top level CU/TU die. */
3019eac3 4806 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4807 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4808
b0c7bfa9
DE
4809 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4810 from the DWO file.
4811 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4812 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
4813 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4814 if (attr)
4815 {
3019eac3 4816 struct dwo_unit *dwo_unit;
b0c7bfa9 4817 struct die_info *dwo_comp_unit_die;
3019eac3
DE
4818
4819 if (has_children)
4820 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4821 " has children (offset 0x%x) [in module %s]"),
4822 this_cu->offset.sect_off, bfd_get_filename (abfd));
b0c7bfa9
DE
4823 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
4824 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
4825 abbrev_table != NULL,
4826 comp_unit_die,
4827 &reader, &info_ptr,
4828 &dwo_comp_unit_die, &has_children) == 0)
3019eac3 4829 {
b0c7bfa9 4830 /* Dummy die. */
3019eac3
DE
4831 do_cleanups (cleanups);
4832 return;
4833 }
b0c7bfa9 4834 comp_unit_die = dwo_comp_unit_die;
3019eac3
DE
4835 }
4836
b0c7bfa9 4837 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
4838 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4839
b0c7bfa9 4840 /* Done, clean up. */
dee91e82 4841 if (free_cu_cleanup != NULL)
348e048f 4842 {
dee91e82
DE
4843 if (keep)
4844 {
4845 /* We've successfully allocated this compilation unit. Let our
4846 caller clean it up when finished with it. */
4847 discard_cleanups (free_cu_cleanup);
4848
4849 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4850 So we have to manually free the abbrev table. */
4851 dwarf2_free_abbrev_table (cu);
4852
4853 /* Link this CU into read_in_chain. */
4854 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4855 dwarf2_per_objfile->read_in_chain = this_cu;
4856 }
4857 else
4858 do_cleanups (free_cu_cleanup);
348e048f 4859 }
dee91e82
DE
4860
4861 do_cleanups (cleanups);
4862}
4863
3019eac3
DE
4864/* Read CU/TU THIS_CU in section SECTION,
4865 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4866 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4867 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4868
4869 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4870 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4871
4872 We fill in THIS_CU->length.
4873
4874 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4875 linker) then DIE_READER_FUNC will not get called.
4876
4877 THIS_CU->cu is always freed when done.
3019eac3
DE
4878 This is done in order to not leave THIS_CU->cu in a state where we have
4879 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4880
4881static void
4882init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4883 struct dwarf2_section_info *abbrev_section,
3019eac3 4884 struct dwo_file *dwo_file,
dee91e82
DE
4885 die_reader_func_ftype *die_reader_func,
4886 void *data)
4887{
4888 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4889 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4890 bfd *abfd = section->asection->owner;
dee91e82 4891 struct dwarf2_cu cu;
d521ce57 4892 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
4893 struct die_reader_specs reader;
4894 struct cleanup *cleanups;
4895 struct die_info *comp_unit_die;
4896 int has_children;
4897
09406207
DE
4898 if (dwarf2_die_debug)
4899 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4900 this_cu->is_debug_types ? "type" : "comp",
4901 this_cu->offset.sect_off);
4902
dee91e82
DE
4903 gdb_assert (this_cu->cu == NULL);
4904
dee91e82
DE
4905 /* This is cheap if the section is already read in. */
4906 dwarf2_read_section (objfile, section);
4907
4908 init_one_comp_unit (&cu, this_cu);
4909
4910 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4911
4912 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4913 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4914 abbrev_section, info_ptr,
3019eac3 4915 this_cu->is_debug_types);
dee91e82 4916
1ce1cefd 4917 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4918
4919 /* Skip dummy compilation units. */
4920 if (info_ptr >= begin_info_ptr + this_cu->length
4921 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4922 {
dee91e82 4923 do_cleanups (cleanups);
21b2bd31 4924 return;
93311388 4925 }
72bf9492 4926
dee91e82
DE
4927 dwarf2_read_abbrevs (&cu, abbrev_section);
4928 make_cleanup (dwarf2_free_abbrev_table, &cu);
4929
3019eac3 4930 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4931 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4932
4933 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4934
4935 do_cleanups (cleanups);
4936}
4937
3019eac3
DE
4938/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4939 does not lookup the specified DWO file.
4940 This cannot be used to read DWO files.
dee91e82
DE
4941
4942 THIS_CU->cu is always freed when done.
3019eac3
DE
4943 This is done in order to not leave THIS_CU->cu in a state where we have
4944 to care whether it refers to the "main" CU or the DWO CU.
4945 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4946
4947static void
4948init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4949 die_reader_func_ftype *die_reader_func,
4950 void *data)
4951{
4952 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4953 get_abbrev_section_for_cu (this_cu),
3019eac3 4954 NULL,
dee91e82
DE
4955 die_reader_func, data);
4956}
0018ea6f
DE
4957\f
4958/* Type Unit Groups.
dee91e82 4959
0018ea6f
DE
4960 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4961 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4962 so that all types coming from the same compilation (.o file) are grouped
4963 together. A future step could be to put the types in the same symtab as
4964 the CU the types ultimately came from. */
ff013f42 4965
f4dc4d17
DE
4966static hashval_t
4967hash_type_unit_group (const void *item)
4968{
094b34ac 4969 const struct type_unit_group *tu_group = item;
f4dc4d17 4970
094b34ac 4971 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4972}
348e048f
DE
4973
4974static int
f4dc4d17 4975eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4976{
f4dc4d17
DE
4977 const struct type_unit_group *lhs = item_lhs;
4978 const struct type_unit_group *rhs = item_rhs;
348e048f 4979
094b34ac 4980 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4981}
348e048f 4982
f4dc4d17
DE
4983/* Allocate a hash table for type unit groups. */
4984
4985static htab_t
4986allocate_type_unit_groups_table (void)
4987{
4988 return htab_create_alloc_ex (3,
4989 hash_type_unit_group,
4990 eq_type_unit_group,
4991 NULL,
4992 &dwarf2_per_objfile->objfile->objfile_obstack,
4993 hashtab_obstack_allocate,
4994 dummy_obstack_deallocate);
4995}
dee91e82 4996
f4dc4d17
DE
4997/* Type units that don't have DW_AT_stmt_list are grouped into their own
4998 partial symtabs. We combine several TUs per psymtab to not let the size
4999 of any one psymtab grow too big. */
5000#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5001#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5002
094b34ac 5003/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5004 Create the type_unit_group object used to hold one or more TUs. */
5005
5006static struct type_unit_group *
094b34ac 5007create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5008{
5009 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5010 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5011 struct type_unit_group *tu_group;
f4dc4d17
DE
5012
5013 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5014 struct type_unit_group);
094b34ac 5015 per_cu = &tu_group->per_cu;
f4dc4d17 5016 per_cu->objfile = objfile;
f4dc4d17 5017
094b34ac
DE
5018 if (dwarf2_per_objfile->using_index)
5019 {
5020 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5021 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5022 }
5023 else
5024 {
5025 unsigned int line_offset = line_offset_struct.sect_off;
5026 struct partial_symtab *pst;
5027 char *name;
5028
5029 /* Give the symtab a useful name for debug purposes. */
5030 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5031 name = xstrprintf ("<type_units_%d>",
5032 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5033 else
5034 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5035
5036 pst = create_partial_symtab (per_cu, name);
5037 pst->anonymous = 1;
f4dc4d17 5038
094b34ac
DE
5039 xfree (name);
5040 }
f4dc4d17 5041
094b34ac
DE
5042 tu_group->hash.dwo_unit = cu->dwo_unit;
5043 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5044
5045 return tu_group;
5046}
5047
094b34ac
DE
5048/* Look up the type_unit_group for type unit CU, and create it if necessary.
5049 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5050
5051static struct type_unit_group *
094b34ac 5052get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5053{
5054 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5055 struct type_unit_group *tu_group;
5056 void **slot;
5057 unsigned int line_offset;
5058 struct type_unit_group type_unit_group_for_lookup;
5059
5060 if (dwarf2_per_objfile->type_unit_groups == NULL)
5061 {
5062 dwarf2_per_objfile->type_unit_groups =
5063 allocate_type_unit_groups_table ();
5064 }
5065
5066 /* Do we need to create a new group, or can we use an existing one? */
5067
5068 if (stmt_list)
5069 {
5070 line_offset = DW_UNSND (stmt_list);
5071 ++tu_stats->nr_symtab_sharers;
5072 }
5073 else
5074 {
5075 /* Ugh, no stmt_list. Rare, but we have to handle it.
5076 We can do various things here like create one group per TU or
5077 spread them over multiple groups to split up the expansion work.
5078 To avoid worst case scenarios (too many groups or too large groups)
5079 we, umm, group them in bunches. */
5080 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5081 | (tu_stats->nr_stmt_less_type_units
5082 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5083 ++tu_stats->nr_stmt_less_type_units;
5084 }
5085
094b34ac
DE
5086 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5087 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5088 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5089 &type_unit_group_for_lookup, INSERT);
5090 if (*slot != NULL)
5091 {
5092 tu_group = *slot;
5093 gdb_assert (tu_group != NULL);
5094 }
5095 else
5096 {
5097 sect_offset line_offset_struct;
5098
5099 line_offset_struct.sect_off = line_offset;
094b34ac 5100 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5101 *slot = tu_group;
5102 ++tu_stats->nr_symtabs;
5103 }
5104
5105 return tu_group;
5106}
5107
5108/* Struct used to sort TUs by their abbreviation table offset. */
5109
5110struct tu_abbrev_offset
5111{
5112 struct signatured_type *sig_type;
5113 sect_offset abbrev_offset;
5114};
5115
5116/* Helper routine for build_type_unit_groups, passed to qsort. */
5117
5118static int
5119sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5120{
5121 const struct tu_abbrev_offset * const *a = ap;
5122 const struct tu_abbrev_offset * const *b = bp;
5123 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5124 unsigned int boff = (*b)->abbrev_offset.sect_off;
5125
5126 return (aoff > boff) - (aoff < boff);
5127}
5128
5129/* A helper function to add a type_unit_group to a table. */
5130
5131static int
5132add_type_unit_group_to_table (void **slot, void *datum)
5133{
5134 struct type_unit_group *tu_group = *slot;
5135 struct type_unit_group ***datap = datum;
5136
5137 **datap = tu_group;
5138 ++*datap;
5139
5140 return 1;
5141}
5142
5143/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5144 each one passing FUNC,DATA.
5145
5146 The efficiency is because we sort TUs by the abbrev table they use and
5147 only read each abbrev table once. In one program there are 200K TUs
5148 sharing 8K abbrev tables.
5149
5150 The main purpose of this function is to support building the
5151 dwarf2_per_objfile->type_unit_groups table.
5152 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5153 can collapse the search space by grouping them by stmt_list.
5154 The savings can be significant, in the same program from above the 200K TUs
5155 share 8K stmt_list tables.
5156
5157 FUNC is expected to call get_type_unit_group, which will create the
5158 struct type_unit_group if necessary and add it to
5159 dwarf2_per_objfile->type_unit_groups. */
5160
5161static void
5162build_type_unit_groups (die_reader_func_ftype *func, void *data)
5163{
5164 struct objfile *objfile = dwarf2_per_objfile->objfile;
5165 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5166 struct cleanup *cleanups;
5167 struct abbrev_table *abbrev_table;
5168 sect_offset abbrev_offset;
5169 struct tu_abbrev_offset *sorted_by_abbrev;
5170 struct type_unit_group **iter;
5171 int i;
5172
5173 /* It's up to the caller to not call us multiple times. */
5174 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5175
5176 if (dwarf2_per_objfile->n_type_units == 0)
5177 return;
5178
5179 /* TUs typically share abbrev tables, and there can be way more TUs than
5180 abbrev tables. Sort by abbrev table to reduce the number of times we
5181 read each abbrev table in.
5182 Alternatives are to punt or to maintain a cache of abbrev tables.
5183 This is simpler and efficient enough for now.
5184
5185 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5186 symtab to use). Typically TUs with the same abbrev offset have the same
5187 stmt_list value too so in practice this should work well.
5188
5189 The basic algorithm here is:
5190
5191 sort TUs by abbrev table
5192 for each TU with same abbrev table:
5193 read abbrev table if first user
5194 read TU top level DIE
5195 [IWBN if DWO skeletons had DW_AT_stmt_list]
5196 call FUNC */
5197
5198 if (dwarf2_read_debug)
5199 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5200
5201 /* Sort in a separate table to maintain the order of all_type_units
5202 for .gdb_index: TU indices directly index all_type_units. */
5203 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5204 dwarf2_per_objfile->n_type_units);
5205 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5206 {
5207 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5208
5209 sorted_by_abbrev[i].sig_type = sig_type;
5210 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5211 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5212 sig_type->per_cu.offset);
5213 }
5214 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5215 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5216 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5217
094b34ac
DE
5218 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5219 called any number of times, so we don't reset tu_stats here. */
5220
f4dc4d17
DE
5221 abbrev_offset.sect_off = ~(unsigned) 0;
5222 abbrev_table = NULL;
5223 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5224
5225 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5226 {
5227 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5228
5229 /* Switch to the next abbrev table if necessary. */
5230 if (abbrev_table == NULL
5231 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5232 {
5233 if (abbrev_table != NULL)
5234 {
5235 abbrev_table_free (abbrev_table);
5236 /* Reset to NULL in case abbrev_table_read_table throws
5237 an error: abbrev_table_free_cleanup will get called. */
5238 abbrev_table = NULL;
5239 }
5240 abbrev_offset = tu->abbrev_offset;
5241 abbrev_table =
5242 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5243 abbrev_offset);
5244 ++tu_stats->nr_uniq_abbrev_tables;
5245 }
5246
5247 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5248 func, data);
5249 }
5250
5251 /* Create a vector of pointers to primary type units to make it easy to
5252 iterate over them and CUs. See dw2_get_primary_cu. */
5253 dwarf2_per_objfile->n_type_unit_groups =
5254 htab_elements (dwarf2_per_objfile->type_unit_groups);
5255 dwarf2_per_objfile->all_type_unit_groups =
5256 obstack_alloc (&objfile->objfile_obstack,
5257 dwarf2_per_objfile->n_type_unit_groups
5258 * sizeof (struct type_unit_group *));
5259 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5260 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5261 add_type_unit_group_to_table, &iter);
5262 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5263 == dwarf2_per_objfile->n_type_unit_groups);
5264
5265 do_cleanups (cleanups);
5266
5267 if (dwarf2_read_debug)
5268 {
5269 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5270 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5271 dwarf2_per_objfile->n_type_units);
5272 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5273 tu_stats->nr_uniq_abbrev_tables);
5274 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5275 tu_stats->nr_symtabs);
5276 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5277 tu_stats->nr_symtab_sharers);
5278 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5279 tu_stats->nr_stmt_less_type_units);
5280 }
5281}
0018ea6f
DE
5282\f
5283/* Partial symbol tables. */
5284
5285/* Create a psymtab named NAME and assign it to PER_CU.
5286
5287 The caller must fill in the following details:
5288 dirname, textlow, texthigh. */
5289
5290static struct partial_symtab *
5291create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5292{
5293 struct objfile *objfile = per_cu->objfile;
5294 struct partial_symtab *pst;
5295
5296 pst = start_psymtab_common (objfile, objfile->section_offsets,
5297 name, 0,
5298 objfile->global_psymbols.next,
5299 objfile->static_psymbols.next);
5300
5301 pst->psymtabs_addrmap_supported = 1;
5302
5303 /* This is the glue that links PST into GDB's symbol API. */
5304 pst->read_symtab_private = per_cu;
5305 pst->read_symtab = dwarf2_read_symtab;
5306 per_cu->v.psymtab = pst;
5307
5308 return pst;
5309}
5310
5311/* die_reader_func for process_psymtab_comp_unit. */
5312
5313static void
5314process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5315 const gdb_byte *info_ptr,
0018ea6f
DE
5316 struct die_info *comp_unit_die,
5317 int has_children,
5318 void *data)
5319{
5320 struct dwarf2_cu *cu = reader->cu;
5321 struct objfile *objfile = cu->objfile;
5322 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5323 struct attribute *attr;
5324 CORE_ADDR baseaddr;
5325 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5326 struct partial_symtab *pst;
5327 int has_pc_info;
5328 const char *filename;
5329 int *want_partial_unit_ptr = data;
5330
5331 if (comp_unit_die->tag == DW_TAG_partial_unit
5332 && (want_partial_unit_ptr == NULL
5333 || !*want_partial_unit_ptr))
5334 return;
5335
5336 gdb_assert (! per_cu->is_debug_types);
5337
5338 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5339
5340 cu->list_in_scope = &file_symbols;
5341
5342 /* Allocate a new partial symbol table structure. */
5343 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5344 if (attr == NULL || !DW_STRING (attr))
5345 filename = "";
5346 else
5347 filename = DW_STRING (attr);
5348
5349 pst = create_partial_symtab (per_cu, filename);
5350
5351 /* This must be done before calling dwarf2_build_include_psymtabs. */
5352 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5353 if (attr != NULL)
5354 pst->dirname = DW_STRING (attr);
5355
5356 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5357
5358 dwarf2_find_base_address (comp_unit_die, cu);
5359
5360 /* Possibly set the default values of LOWPC and HIGHPC from
5361 `DW_AT_ranges'. */
5362 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5363 &best_highpc, cu, pst);
5364 if (has_pc_info == 1 && best_lowpc < best_highpc)
5365 /* Store the contiguous range if it is not empty; it can be empty for
5366 CUs with no code. */
5367 addrmap_set_empty (objfile->psymtabs_addrmap,
5368 best_lowpc + baseaddr,
5369 best_highpc + baseaddr - 1, pst);
5370
5371 /* Check if comp unit has_children.
5372 If so, read the rest of the partial symbols from this comp unit.
5373 If not, there's no more debug_info for this comp unit. */
5374 if (has_children)
5375 {
5376 struct partial_die_info *first_die;
5377 CORE_ADDR lowpc, highpc;
5378
5379 lowpc = ((CORE_ADDR) -1);
5380 highpc = ((CORE_ADDR) 0);
5381
5382 first_die = load_partial_dies (reader, info_ptr, 1);
5383
5384 scan_partial_symbols (first_die, &lowpc, &highpc,
5385 ! has_pc_info, cu);
5386
5387 /* If we didn't find a lowpc, set it to highpc to avoid
5388 complaints from `maint check'. */
5389 if (lowpc == ((CORE_ADDR) -1))
5390 lowpc = highpc;
5391
5392 /* If the compilation unit didn't have an explicit address range,
5393 then use the information extracted from its child dies. */
5394 if (! has_pc_info)
5395 {
5396 best_lowpc = lowpc;
5397 best_highpc = highpc;
5398 }
5399 }
5400 pst->textlow = best_lowpc + baseaddr;
5401 pst->texthigh = best_highpc + baseaddr;
5402
5403 pst->n_global_syms = objfile->global_psymbols.next -
5404 (objfile->global_psymbols.list + pst->globals_offset);
5405 pst->n_static_syms = objfile->static_psymbols.next -
5406 (objfile->static_psymbols.list + pst->statics_offset);
5407 sort_pst_symbols (objfile, pst);
5408
5409 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5410 {
5411 int i;
5412 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5413 struct dwarf2_per_cu_data *iter;
5414
5415 /* Fill in 'dependencies' here; we fill in 'users' in a
5416 post-pass. */
5417 pst->number_of_dependencies = len;
5418 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5419 len * sizeof (struct symtab *));
5420 for (i = 0;
5421 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5422 i, iter);
5423 ++i)
5424 pst->dependencies[i] = iter->v.psymtab;
5425
5426 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5427 }
5428
5429 /* Get the list of files included in the current compilation unit,
5430 and build a psymtab for each of them. */
5431 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5432
5433 if (dwarf2_read_debug)
5434 {
5435 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5436
5437 fprintf_unfiltered (gdb_stdlog,
5438 "Psymtab for %s unit @0x%x: %s - %s"
5439 ", %d global, %d static syms\n",
5440 per_cu->is_debug_types ? "type" : "comp",
5441 per_cu->offset.sect_off,
5442 paddress (gdbarch, pst->textlow),
5443 paddress (gdbarch, pst->texthigh),
5444 pst->n_global_syms, pst->n_static_syms);
5445 }
5446}
5447
5448/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5449 Process compilation unit THIS_CU for a psymtab. */
5450
5451static void
5452process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5453 int want_partial_unit)
5454{
5455 /* If this compilation unit was already read in, free the
5456 cached copy in order to read it in again. This is
5457 necessary because we skipped some symbols when we first
5458 read in the compilation unit (see load_partial_dies).
5459 This problem could be avoided, but the benefit is unclear. */
5460 if (this_cu->cu != NULL)
5461 free_one_cached_comp_unit (this_cu);
5462
5463 gdb_assert (! this_cu->is_debug_types);
5464 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5465 process_psymtab_comp_unit_reader,
5466 &want_partial_unit);
5467
5468 /* Age out any secondary CUs. */
5469 age_cached_comp_units ();
5470}
f4dc4d17
DE
5471
5472/* Reader function for build_type_psymtabs. */
5473
5474static void
5475build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5476 const gdb_byte *info_ptr,
f4dc4d17
DE
5477 struct die_info *type_unit_die,
5478 int has_children,
5479 void *data)
5480{
5481 struct objfile *objfile = dwarf2_per_objfile->objfile;
5482 struct dwarf2_cu *cu = reader->cu;
5483 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5484 struct signatured_type *sig_type;
f4dc4d17
DE
5485 struct type_unit_group *tu_group;
5486 struct attribute *attr;
5487 struct partial_die_info *first_die;
5488 CORE_ADDR lowpc, highpc;
5489 struct partial_symtab *pst;
5490
5491 gdb_assert (data == NULL);
0186c6a7
DE
5492 gdb_assert (per_cu->is_debug_types);
5493 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5494
5495 if (! has_children)
5496 return;
5497
5498 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5499 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5500
0186c6a7 5501 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5502
5503 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5504 cu->list_in_scope = &file_symbols;
5505 pst = create_partial_symtab (per_cu, "");
5506 pst->anonymous = 1;
5507
5508 first_die = load_partial_dies (reader, info_ptr, 1);
5509
5510 lowpc = (CORE_ADDR) -1;
5511 highpc = (CORE_ADDR) 0;
5512 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5513
5514 pst->n_global_syms = objfile->global_psymbols.next -
5515 (objfile->global_psymbols.list + pst->globals_offset);
5516 pst->n_static_syms = objfile->static_psymbols.next -
5517 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5518 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5519}
5520
5521/* Traversal function for build_type_psymtabs. */
5522
5523static int
5524build_type_psymtab_dependencies (void **slot, void *info)
5525{
5526 struct objfile *objfile = dwarf2_per_objfile->objfile;
5527 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5528 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5529 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5530 int len = VEC_length (sig_type_ptr, tu_group->tus);
5531 struct signatured_type *iter;
f4dc4d17
DE
5532 int i;
5533
5534 gdb_assert (len > 0);
0186c6a7 5535 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5536
5537 pst->number_of_dependencies = len;
5538 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5539 len * sizeof (struct psymtab *));
5540 for (i = 0;
0186c6a7 5541 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5542 ++i)
5543 {
0186c6a7
DE
5544 gdb_assert (iter->per_cu.is_debug_types);
5545 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5546 iter->type_unit_group = tu_group;
f4dc4d17
DE
5547 }
5548
0186c6a7 5549 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5550
5551 return 1;
5552}
5553
5554/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5555 Build partial symbol tables for the .debug_types comp-units. */
5556
5557static void
5558build_type_psymtabs (struct objfile *objfile)
5559{
0e50663e 5560 if (! create_all_type_units (objfile))
348e048f
DE
5561 return;
5562
f4dc4d17
DE
5563 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5564
5565 /* Now that all TUs have been processed we can fill in the dependencies. */
5566 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5567 build_type_psymtab_dependencies, NULL);
348e048f
DE
5568}
5569
60606b2c
TT
5570/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5571
5572static void
5573psymtabs_addrmap_cleanup (void *o)
5574{
5575 struct objfile *objfile = o;
ec61707d 5576
60606b2c
TT
5577 objfile->psymtabs_addrmap = NULL;
5578}
5579
95554aad
TT
5580/* Compute the 'user' field for each psymtab in OBJFILE. */
5581
5582static void
5583set_partial_user (struct objfile *objfile)
5584{
5585 int i;
5586
5587 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5588 {
5589 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5590 struct partial_symtab *pst = per_cu->v.psymtab;
5591 int j;
5592
36586728
TT
5593 if (pst == NULL)
5594 continue;
5595
95554aad
TT
5596 for (j = 0; j < pst->number_of_dependencies; ++j)
5597 {
5598 /* Set the 'user' field only if it is not already set. */
5599 if (pst->dependencies[j]->user == NULL)
5600 pst->dependencies[j]->user = pst;
5601 }
5602 }
5603}
5604
93311388
DE
5605/* Build the partial symbol table by doing a quick pass through the
5606 .debug_info and .debug_abbrev sections. */
72bf9492 5607
93311388 5608static void
c67a9c90 5609dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5610{
60606b2c
TT
5611 struct cleanup *back_to, *addrmap_cleanup;
5612 struct obstack temp_obstack;
21b2bd31 5613 int i;
93311388 5614
45cfd468
DE
5615 if (dwarf2_read_debug)
5616 {
5617 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5618 objfile->name);
5619 }
5620
98bfdba5
PA
5621 dwarf2_per_objfile->reading_partial_symbols = 1;
5622
be391dca 5623 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5624
93311388
DE
5625 /* Any cached compilation units will be linked by the per-objfile
5626 read_in_chain. Make sure to free them when we're done. */
5627 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5628
348e048f
DE
5629 build_type_psymtabs (objfile);
5630
93311388 5631 create_all_comp_units (objfile);
c906108c 5632
60606b2c
TT
5633 /* Create a temporary address map on a temporary obstack. We later
5634 copy this to the final obstack. */
5635 obstack_init (&temp_obstack);
5636 make_cleanup_obstack_free (&temp_obstack);
5637 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5638 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5639
21b2bd31 5640 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5641 {
21b2bd31 5642 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5643
95554aad 5644 process_psymtab_comp_unit (per_cu, 0);
c906108c 5645 }
ff013f42 5646
95554aad
TT
5647 set_partial_user (objfile);
5648
ff013f42
JK
5649 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5650 &objfile->objfile_obstack);
60606b2c 5651 discard_cleanups (addrmap_cleanup);
ff013f42 5652
ae038cb0 5653 do_cleanups (back_to);
45cfd468
DE
5654
5655 if (dwarf2_read_debug)
5656 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5657 objfile->name);
ae038cb0
DJ
5658}
5659
3019eac3 5660/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5661
5662static void
dee91e82 5663load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5664 const gdb_byte *info_ptr,
dee91e82
DE
5665 struct die_info *comp_unit_die,
5666 int has_children,
5667 void *data)
ae038cb0 5668{
dee91e82 5669 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5670
95554aad 5671 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5672
ae038cb0
DJ
5673 /* Check if comp unit has_children.
5674 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5675 If not, there's no more debug_info for this comp unit. */
d85a05f0 5676 if (has_children)
dee91e82
DE
5677 load_partial_dies (reader, info_ptr, 0);
5678}
98bfdba5 5679
dee91e82
DE
5680/* Load the partial DIEs for a secondary CU into memory.
5681 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5682
dee91e82
DE
5683static void
5684load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5685{
f4dc4d17
DE
5686 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5687 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5688}
5689
ae038cb0 5690static void
36586728
TT
5691read_comp_units_from_section (struct objfile *objfile,
5692 struct dwarf2_section_info *section,
5693 unsigned int is_dwz,
5694 int *n_allocated,
5695 int *n_comp_units,
5696 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5697{
d521ce57 5698 const gdb_byte *info_ptr;
36586728 5699 bfd *abfd = section->asection->owner;
be391dca 5700
bf6af496
DE
5701 if (dwarf2_read_debug)
5702 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
5703 section->asection->name, bfd_get_filename (abfd));
5704
36586728 5705 dwarf2_read_section (objfile, section);
ae038cb0 5706
36586728 5707 info_ptr = section->buffer;
6e70227d 5708
36586728 5709 while (info_ptr < section->buffer + section->size)
ae038cb0 5710 {
c764a876 5711 unsigned int length, initial_length_size;
ae038cb0 5712 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5713 sect_offset offset;
ae038cb0 5714
36586728 5715 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5716
5717 /* Read just enough information to find out where the next
5718 compilation unit is. */
36586728 5719 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5720
5721 /* Save the compilation unit for later lookup. */
5722 this_cu = obstack_alloc (&objfile->objfile_obstack,
5723 sizeof (struct dwarf2_per_cu_data));
5724 memset (this_cu, 0, sizeof (*this_cu));
5725 this_cu->offset = offset;
c764a876 5726 this_cu->length = length + initial_length_size;
36586728 5727 this_cu->is_dwz = is_dwz;
9291a0cd 5728 this_cu->objfile = objfile;
8a0459fd 5729 this_cu->section = section;
ae038cb0 5730
36586728 5731 if (*n_comp_units == *n_allocated)
ae038cb0 5732 {
36586728
TT
5733 *n_allocated *= 2;
5734 *all_comp_units = xrealloc (*all_comp_units,
5735 *n_allocated
5736 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5737 }
36586728
TT
5738 (*all_comp_units)[*n_comp_units] = this_cu;
5739 ++*n_comp_units;
ae038cb0
DJ
5740
5741 info_ptr = info_ptr + this_cu->length;
5742 }
36586728
TT
5743}
5744
5745/* Create a list of all compilation units in OBJFILE.
5746 This is only done for -readnow and building partial symtabs. */
5747
5748static void
5749create_all_comp_units (struct objfile *objfile)
5750{
5751 int n_allocated;
5752 int n_comp_units;
5753 struct dwarf2_per_cu_data **all_comp_units;
5754
5755 n_comp_units = 0;
5756 n_allocated = 10;
5757 all_comp_units = xmalloc (n_allocated
5758 * sizeof (struct dwarf2_per_cu_data *));
5759
5760 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5761 &n_allocated, &n_comp_units, &all_comp_units);
5762
5763 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5764 {
5765 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5766
5767 read_comp_units_from_section (objfile, &dwz->info, 1,
5768 &n_allocated, &n_comp_units,
5769 &all_comp_units);
5770 }
ae038cb0
DJ
5771
5772 dwarf2_per_objfile->all_comp_units
5773 = obstack_alloc (&objfile->objfile_obstack,
5774 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5775 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5776 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5777 xfree (all_comp_units);
5778 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5779}
5780
5734ee8b
DJ
5781/* Process all loaded DIEs for compilation unit CU, starting at
5782 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5783 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5784 DW_AT_ranges). If NEED_PC is set, then this function will set
5785 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5786 and record the covered ranges in the addrmap. */
c906108c 5787
72bf9492
DJ
5788static void
5789scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5790 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5791{
72bf9492 5792 struct partial_die_info *pdi;
c906108c 5793
91c24f0a
DC
5794 /* Now, march along the PDI's, descending into ones which have
5795 interesting children but skipping the children of the other ones,
5796 until we reach the end of the compilation unit. */
c906108c 5797
72bf9492 5798 pdi = first_die;
91c24f0a 5799
72bf9492
DJ
5800 while (pdi != NULL)
5801 {
5802 fixup_partial_die (pdi, cu);
c906108c 5803
f55ee35c 5804 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5805 children, so we need to look at them. Ditto for anonymous
5806 enums. */
933c6fe4 5807
72bf9492 5808 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5809 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5810 || pdi->tag == DW_TAG_imported_unit)
c906108c 5811 {
72bf9492 5812 switch (pdi->tag)
c906108c
SS
5813 {
5814 case DW_TAG_subprogram:
5734ee8b 5815 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5816 break;
72929c62 5817 case DW_TAG_constant:
c906108c
SS
5818 case DW_TAG_variable:
5819 case DW_TAG_typedef:
91c24f0a 5820 case DW_TAG_union_type:
72bf9492 5821 if (!pdi->is_declaration)
63d06c5c 5822 {
72bf9492 5823 add_partial_symbol (pdi, cu);
63d06c5c
DC
5824 }
5825 break;
c906108c 5826 case DW_TAG_class_type:
680b30c7 5827 case DW_TAG_interface_type:
c906108c 5828 case DW_TAG_structure_type:
72bf9492 5829 if (!pdi->is_declaration)
c906108c 5830 {
72bf9492 5831 add_partial_symbol (pdi, cu);
c906108c
SS
5832 }
5833 break;
91c24f0a 5834 case DW_TAG_enumeration_type:
72bf9492
DJ
5835 if (!pdi->is_declaration)
5836 add_partial_enumeration (pdi, cu);
c906108c
SS
5837 break;
5838 case DW_TAG_base_type:
a02abb62 5839 case DW_TAG_subrange_type:
c906108c 5840 /* File scope base type definitions are added to the partial
c5aa993b 5841 symbol table. */
72bf9492 5842 add_partial_symbol (pdi, cu);
c906108c 5843 break;
d9fa45fe 5844 case DW_TAG_namespace:
5734ee8b 5845 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5846 break;
5d7cb8df
JK
5847 case DW_TAG_module:
5848 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5849 break;
95554aad
TT
5850 case DW_TAG_imported_unit:
5851 {
5852 struct dwarf2_per_cu_data *per_cu;
5853
f4dc4d17
DE
5854 /* For now we don't handle imported units in type units. */
5855 if (cu->per_cu->is_debug_types)
5856 {
5857 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5858 " supported in type units [in module %s]"),
5859 cu->objfile->name);
5860 }
5861
95554aad 5862 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5863 pdi->is_dwz,
95554aad
TT
5864 cu->objfile);
5865
5866 /* Go read the partial unit, if needed. */
5867 if (per_cu->v.psymtab == NULL)
5868 process_psymtab_comp_unit (per_cu, 1);
5869
f4dc4d17 5870 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5871 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5872 }
5873 break;
c906108c
SS
5874 default:
5875 break;
5876 }
5877 }
5878
72bf9492
DJ
5879 /* If the die has a sibling, skip to the sibling. */
5880
5881 pdi = pdi->die_sibling;
5882 }
5883}
5884
5885/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5886
72bf9492 5887 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5888 name is concatenated with "::" and the partial DIE's name. For
5889 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5890 Enumerators are an exception; they use the scope of their parent
5891 enumeration type, i.e. the name of the enumeration type is not
5892 prepended to the enumerator.
91c24f0a 5893
72bf9492
DJ
5894 There are two complexities. One is DW_AT_specification; in this
5895 case "parent" means the parent of the target of the specification,
5896 instead of the direct parent of the DIE. The other is compilers
5897 which do not emit DW_TAG_namespace; in this case we try to guess
5898 the fully qualified name of structure types from their members'
5899 linkage names. This must be done using the DIE's children rather
5900 than the children of any DW_AT_specification target. We only need
5901 to do this for structures at the top level, i.e. if the target of
5902 any DW_AT_specification (if any; otherwise the DIE itself) does not
5903 have a parent. */
5904
5905/* Compute the scope prefix associated with PDI's parent, in
5906 compilation unit CU. The result will be allocated on CU's
5907 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5908 field. NULL is returned if no prefix is necessary. */
15d034d0 5909static const char *
72bf9492
DJ
5910partial_die_parent_scope (struct partial_die_info *pdi,
5911 struct dwarf2_cu *cu)
5912{
15d034d0 5913 const char *grandparent_scope;
72bf9492 5914 struct partial_die_info *parent, *real_pdi;
91c24f0a 5915
72bf9492
DJ
5916 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5917 then this means the parent of the specification DIE. */
5918
5919 real_pdi = pdi;
72bf9492 5920 while (real_pdi->has_specification)
36586728
TT
5921 real_pdi = find_partial_die (real_pdi->spec_offset,
5922 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5923
5924 parent = real_pdi->die_parent;
5925 if (parent == NULL)
5926 return NULL;
5927
5928 if (parent->scope_set)
5929 return parent->scope;
5930
5931 fixup_partial_die (parent, cu);
5932
10b3939b 5933 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5934
acebe513
UW
5935 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5936 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5937 Work around this problem here. */
5938 if (cu->language == language_cplus
6e70227d 5939 && parent->tag == DW_TAG_namespace
acebe513
UW
5940 && strcmp (parent->name, "::") == 0
5941 && grandparent_scope == NULL)
5942 {
5943 parent->scope = NULL;
5944 parent->scope_set = 1;
5945 return NULL;
5946 }
5947
9c6c53f7
SA
5948 if (pdi->tag == DW_TAG_enumerator)
5949 /* Enumerators should not get the name of the enumeration as a prefix. */
5950 parent->scope = grandparent_scope;
5951 else if (parent->tag == DW_TAG_namespace
f55ee35c 5952 || parent->tag == DW_TAG_module
72bf9492
DJ
5953 || parent->tag == DW_TAG_structure_type
5954 || parent->tag == DW_TAG_class_type
680b30c7 5955 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5956 || parent->tag == DW_TAG_union_type
5957 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5958 {
5959 if (grandparent_scope == NULL)
5960 parent->scope = parent->name;
5961 else
3e43a32a
MS
5962 parent->scope = typename_concat (&cu->comp_unit_obstack,
5963 grandparent_scope,
f55ee35c 5964 parent->name, 0, cu);
72bf9492 5965 }
72bf9492
DJ
5966 else
5967 {
5968 /* FIXME drow/2004-04-01: What should we be doing with
5969 function-local names? For partial symbols, we should probably be
5970 ignoring them. */
5971 complaint (&symfile_complaints,
e2e0b3e5 5972 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5973 parent->tag, pdi->offset.sect_off);
72bf9492 5974 parent->scope = grandparent_scope;
c906108c
SS
5975 }
5976
72bf9492
DJ
5977 parent->scope_set = 1;
5978 return parent->scope;
5979}
5980
5981/* Return the fully scoped name associated with PDI, from compilation unit
5982 CU. The result will be allocated with malloc. */
4568ecf9 5983
72bf9492
DJ
5984static char *
5985partial_die_full_name (struct partial_die_info *pdi,
5986 struct dwarf2_cu *cu)
5987{
15d034d0 5988 const char *parent_scope;
72bf9492 5989
98bfdba5
PA
5990 /* If this is a template instantiation, we can not work out the
5991 template arguments from partial DIEs. So, unfortunately, we have
5992 to go through the full DIEs. At least any work we do building
5993 types here will be reused if full symbols are loaded later. */
5994 if (pdi->has_template_arguments)
5995 {
5996 fixup_partial_die (pdi, cu);
5997
5998 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5999 {
6000 struct die_info *die;
6001 struct attribute attr;
6002 struct dwarf2_cu *ref_cu = cu;
6003
b64f50a1 6004 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6005 attr.name = 0;
6006 attr.form = DW_FORM_ref_addr;
4568ecf9 6007 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6008 die = follow_die_ref (NULL, &attr, &ref_cu);
6009
6010 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6011 }
6012 }
6013
72bf9492
DJ
6014 parent_scope = partial_die_parent_scope (pdi, cu);
6015 if (parent_scope == NULL)
6016 return NULL;
6017 else
f55ee35c 6018 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6019}
6020
6021static void
72bf9492 6022add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6023{
e7c27a73 6024 struct objfile *objfile = cu->objfile;
c906108c 6025 CORE_ADDR addr = 0;
15d034d0 6026 const char *actual_name = NULL;
e142c38c 6027 CORE_ADDR baseaddr;
15d034d0 6028 char *built_actual_name;
e142c38c
DJ
6029
6030 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6031
15d034d0
TT
6032 built_actual_name = partial_die_full_name (pdi, cu);
6033 if (built_actual_name != NULL)
6034 actual_name = built_actual_name;
63d06c5c 6035
72bf9492
DJ
6036 if (actual_name == NULL)
6037 actual_name = pdi->name;
6038
c906108c
SS
6039 switch (pdi->tag)
6040 {
6041 case DW_TAG_subprogram:
2cfa0c8d 6042 if (pdi->is_external || cu->language == language_ada)
c906108c 6043 {
2cfa0c8d
JB
6044 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6045 of the global scope. But in Ada, we want to be able to access
6046 nested procedures globally. So all Ada subprograms are stored
6047 in the global scope. */
f47fb265 6048 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6049 mst_text, objfile); */
f47fb265 6050 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6051 built_actual_name != NULL,
f47fb265
MS
6052 VAR_DOMAIN, LOC_BLOCK,
6053 &objfile->global_psymbols,
6054 0, pdi->lowpc + baseaddr,
6055 cu->language, objfile);
c906108c
SS
6056 }
6057 else
6058 {
f47fb265 6059 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6060 mst_file_text, objfile); */
f47fb265 6061 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6062 built_actual_name != NULL,
f47fb265
MS
6063 VAR_DOMAIN, LOC_BLOCK,
6064 &objfile->static_psymbols,
6065 0, pdi->lowpc + baseaddr,
6066 cu->language, objfile);
c906108c
SS
6067 }
6068 break;
72929c62
JB
6069 case DW_TAG_constant:
6070 {
6071 struct psymbol_allocation_list *list;
6072
6073 if (pdi->is_external)
6074 list = &objfile->global_psymbols;
6075 else
6076 list = &objfile->static_psymbols;
f47fb265 6077 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6078 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6079 list, 0, 0, cu->language, objfile);
72929c62
JB
6080 }
6081 break;
c906108c 6082 case DW_TAG_variable:
95554aad
TT
6083 if (pdi->d.locdesc)
6084 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6085
95554aad 6086 if (pdi->d.locdesc
caac4577
JG
6087 && addr == 0
6088 && !dwarf2_per_objfile->has_section_at_zero)
6089 {
6090 /* A global or static variable may also have been stripped
6091 out by the linker if unused, in which case its address
6092 will be nullified; do not add such variables into partial
6093 symbol table then. */
6094 }
6095 else if (pdi->is_external)
c906108c
SS
6096 {
6097 /* Global Variable.
6098 Don't enter into the minimal symbol tables as there is
6099 a minimal symbol table entry from the ELF symbols already.
6100 Enter into partial symbol table if it has a location
6101 descriptor or a type.
6102 If the location descriptor is missing, new_symbol will create
6103 a LOC_UNRESOLVED symbol, the address of the variable will then
6104 be determined from the minimal symbol table whenever the variable
6105 is referenced.
6106 The address for the partial symbol table entry is not
6107 used by GDB, but it comes in handy for debugging partial symbol
6108 table building. */
6109
95554aad 6110 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6111 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6112 built_actual_name != NULL,
f47fb265
MS
6113 VAR_DOMAIN, LOC_STATIC,
6114 &objfile->global_psymbols,
6115 0, addr + baseaddr,
6116 cu->language, objfile);
c906108c
SS
6117 }
6118 else
6119 {
0963b4bd 6120 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6121 if (pdi->d.locdesc == NULL)
decbce07 6122 {
15d034d0 6123 xfree (built_actual_name);
decbce07
MS
6124 return;
6125 }
f47fb265 6126 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6127 mst_file_data, objfile); */
f47fb265 6128 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6129 built_actual_name != NULL,
f47fb265
MS
6130 VAR_DOMAIN, LOC_STATIC,
6131 &objfile->static_psymbols,
6132 0, addr + baseaddr,
6133 cu->language, objfile);
c906108c
SS
6134 }
6135 break;
6136 case DW_TAG_typedef:
6137 case DW_TAG_base_type:
a02abb62 6138 case DW_TAG_subrange_type:
38d518c9 6139 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6140 built_actual_name != NULL,
176620f1 6141 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6142 &objfile->static_psymbols,
e142c38c 6143 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6144 break;
72bf9492
DJ
6145 case DW_TAG_namespace:
6146 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6147 built_actual_name != NULL,
72bf9492
DJ
6148 VAR_DOMAIN, LOC_TYPEDEF,
6149 &objfile->global_psymbols,
6150 0, (CORE_ADDR) 0, cu->language, objfile);
6151 break;
c906108c 6152 case DW_TAG_class_type:
680b30c7 6153 case DW_TAG_interface_type:
c906108c
SS
6154 case DW_TAG_structure_type:
6155 case DW_TAG_union_type:
6156 case DW_TAG_enumeration_type:
fa4028e9
JB
6157 /* Skip external references. The DWARF standard says in the section
6158 about "Structure, Union, and Class Type Entries": "An incomplete
6159 structure, union or class type is represented by a structure,
6160 union or class entry that does not have a byte size attribute
6161 and that has a DW_AT_declaration attribute." */
6162 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6163 {
15d034d0 6164 xfree (built_actual_name);
decbce07
MS
6165 return;
6166 }
fa4028e9 6167
63d06c5c
DC
6168 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6169 static vs. global. */
38d518c9 6170 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6171 built_actual_name != NULL,
176620f1 6172 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6173 (cu->language == language_cplus
6174 || cu->language == language_java)
63d06c5c
DC
6175 ? &objfile->global_psymbols
6176 : &objfile->static_psymbols,
e142c38c 6177 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6178
c906108c
SS
6179 break;
6180 case DW_TAG_enumerator:
38d518c9 6181 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6182 built_actual_name != NULL,
176620f1 6183 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6184 (cu->language == language_cplus
6185 || cu->language == language_java)
f6fe98ef
DJ
6186 ? &objfile->global_psymbols
6187 : &objfile->static_psymbols,
e142c38c 6188 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6189 break;
6190 default:
6191 break;
6192 }
5c4e30ca 6193
15d034d0 6194 xfree (built_actual_name);
c906108c
SS
6195}
6196
5c4e30ca
DC
6197/* Read a partial die corresponding to a namespace; also, add a symbol
6198 corresponding to that namespace to the symbol table. NAMESPACE is
6199 the name of the enclosing namespace. */
91c24f0a 6200
72bf9492
DJ
6201static void
6202add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6203 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6204 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6205{
72bf9492 6206 /* Add a symbol for the namespace. */
e7c27a73 6207
72bf9492 6208 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6209
6210 /* Now scan partial symbols in that namespace. */
6211
91c24f0a 6212 if (pdi->has_children)
5734ee8b 6213 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6214}
6215
5d7cb8df
JK
6216/* Read a partial die corresponding to a Fortran module. */
6217
6218static void
6219add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6220 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6221{
f55ee35c 6222 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6223
6224 if (pdi->has_children)
6225 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6226}
6227
bc30ff58
JB
6228/* Read a partial die corresponding to a subprogram and create a partial
6229 symbol for that subprogram. When the CU language allows it, this
6230 routine also defines a partial symbol for each nested subprogram
6231 that this subprogram contains.
6e70227d 6232
bc30ff58
JB
6233 DIE my also be a lexical block, in which case we simply search
6234 recursively for suprograms defined inside that lexical block.
6235 Again, this is only performed when the CU language allows this
6236 type of definitions. */
6237
6238static void
6239add_partial_subprogram (struct partial_die_info *pdi,
6240 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6241 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6242{
6243 if (pdi->tag == DW_TAG_subprogram)
6244 {
6245 if (pdi->has_pc_info)
6246 {
6247 if (pdi->lowpc < *lowpc)
6248 *lowpc = pdi->lowpc;
6249 if (pdi->highpc > *highpc)
6250 *highpc = pdi->highpc;
5734ee8b
DJ
6251 if (need_pc)
6252 {
6253 CORE_ADDR baseaddr;
6254 struct objfile *objfile = cu->objfile;
6255
6256 baseaddr = ANOFFSET (objfile->section_offsets,
6257 SECT_OFF_TEXT (objfile));
6258 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6259 pdi->lowpc + baseaddr,
6260 pdi->highpc - 1 + baseaddr,
9291a0cd 6261 cu->per_cu->v.psymtab);
5734ee8b 6262 }
481860b3
GB
6263 }
6264
6265 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6266 {
bc30ff58 6267 if (!pdi->is_declaration)
e8d05480
JB
6268 /* Ignore subprogram DIEs that do not have a name, they are
6269 illegal. Do not emit a complaint at this point, we will
6270 do so when we convert this psymtab into a symtab. */
6271 if (pdi->name)
6272 add_partial_symbol (pdi, cu);
bc30ff58
JB
6273 }
6274 }
6e70227d 6275
bc30ff58
JB
6276 if (! pdi->has_children)
6277 return;
6278
6279 if (cu->language == language_ada)
6280 {
6281 pdi = pdi->die_child;
6282 while (pdi != NULL)
6283 {
6284 fixup_partial_die (pdi, cu);
6285 if (pdi->tag == DW_TAG_subprogram
6286 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6287 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6288 pdi = pdi->die_sibling;
6289 }
6290 }
6291}
6292
91c24f0a
DC
6293/* Read a partial die corresponding to an enumeration type. */
6294
72bf9492
DJ
6295static void
6296add_partial_enumeration (struct partial_die_info *enum_pdi,
6297 struct dwarf2_cu *cu)
91c24f0a 6298{
72bf9492 6299 struct partial_die_info *pdi;
91c24f0a
DC
6300
6301 if (enum_pdi->name != NULL)
72bf9492
DJ
6302 add_partial_symbol (enum_pdi, cu);
6303
6304 pdi = enum_pdi->die_child;
6305 while (pdi)
91c24f0a 6306 {
72bf9492 6307 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6308 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6309 else
72bf9492
DJ
6310 add_partial_symbol (pdi, cu);
6311 pdi = pdi->die_sibling;
91c24f0a 6312 }
91c24f0a
DC
6313}
6314
6caca83c
CC
6315/* Return the initial uleb128 in the die at INFO_PTR. */
6316
6317static unsigned int
d521ce57 6318peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6319{
6320 unsigned int bytes_read;
6321
6322 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6323}
6324
4bb7a0a7
DJ
6325/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6326 Return the corresponding abbrev, or NULL if the number is zero (indicating
6327 an empty DIE). In either case *BYTES_READ will be set to the length of
6328 the initial number. */
6329
6330static struct abbrev_info *
d521ce57 6331peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6332 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6333{
6334 bfd *abfd = cu->objfile->obfd;
6335 unsigned int abbrev_number;
6336 struct abbrev_info *abbrev;
6337
6338 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6339
6340 if (abbrev_number == 0)
6341 return NULL;
6342
433df2d4 6343 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6344 if (!abbrev)
6345 {
3e43a32a
MS
6346 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6347 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6348 }
6349
6350 return abbrev;
6351}
6352
93311388
DE
6353/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6354 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6355 DIE. Any children of the skipped DIEs will also be skipped. */
6356
d521ce57
TT
6357static const gdb_byte *
6358skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6359{
dee91e82 6360 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6361 struct abbrev_info *abbrev;
6362 unsigned int bytes_read;
6363
6364 while (1)
6365 {
6366 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6367 if (abbrev == NULL)
6368 return info_ptr + bytes_read;
6369 else
dee91e82 6370 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6371 }
6372}
6373
93311388
DE
6374/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6375 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6376 abbrev corresponding to that skipped uleb128 should be passed in
6377 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6378 children. */
6379
d521ce57
TT
6380static const gdb_byte *
6381skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6382 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6383{
6384 unsigned int bytes_read;
6385 struct attribute attr;
dee91e82
DE
6386 bfd *abfd = reader->abfd;
6387 struct dwarf2_cu *cu = reader->cu;
d521ce57 6388 const gdb_byte *buffer = reader->buffer;
f664829e 6389 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6390 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6391 unsigned int form, i;
6392
6393 for (i = 0; i < abbrev->num_attrs; i++)
6394 {
6395 /* The only abbrev we care about is DW_AT_sibling. */
6396 if (abbrev->attrs[i].name == DW_AT_sibling)
6397 {
dee91e82 6398 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6399 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6400 complaint (&symfile_complaints,
6401 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6402 else
b64f50a1 6403 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6404 }
6405
6406 /* If it isn't DW_AT_sibling, skip this attribute. */
6407 form = abbrev->attrs[i].form;
6408 skip_attribute:
6409 switch (form)
6410 {
4bb7a0a7 6411 case DW_FORM_ref_addr:
ae411497
TT
6412 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6413 and later it is offset sized. */
6414 if (cu->header.version == 2)
6415 info_ptr += cu->header.addr_size;
6416 else
6417 info_ptr += cu->header.offset_size;
6418 break;
36586728
TT
6419 case DW_FORM_GNU_ref_alt:
6420 info_ptr += cu->header.offset_size;
6421 break;
ae411497 6422 case DW_FORM_addr:
4bb7a0a7
DJ
6423 info_ptr += cu->header.addr_size;
6424 break;
6425 case DW_FORM_data1:
6426 case DW_FORM_ref1:
6427 case DW_FORM_flag:
6428 info_ptr += 1;
6429 break;
2dc7f7b3
TT
6430 case DW_FORM_flag_present:
6431 break;
4bb7a0a7
DJ
6432 case DW_FORM_data2:
6433 case DW_FORM_ref2:
6434 info_ptr += 2;
6435 break;
6436 case DW_FORM_data4:
6437 case DW_FORM_ref4:
6438 info_ptr += 4;
6439 break;
6440 case DW_FORM_data8:
6441 case DW_FORM_ref8:
55f1336d 6442 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6443 info_ptr += 8;
6444 break;
6445 case DW_FORM_string:
9b1c24c8 6446 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6447 info_ptr += bytes_read;
6448 break;
2dc7f7b3 6449 case DW_FORM_sec_offset:
4bb7a0a7 6450 case DW_FORM_strp:
36586728 6451 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6452 info_ptr += cu->header.offset_size;
6453 break;
2dc7f7b3 6454 case DW_FORM_exprloc:
4bb7a0a7
DJ
6455 case DW_FORM_block:
6456 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6457 info_ptr += bytes_read;
6458 break;
6459 case DW_FORM_block1:
6460 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6461 break;
6462 case DW_FORM_block2:
6463 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6464 break;
6465 case DW_FORM_block4:
6466 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6467 break;
6468 case DW_FORM_sdata:
6469 case DW_FORM_udata:
6470 case DW_FORM_ref_udata:
3019eac3
DE
6471 case DW_FORM_GNU_addr_index:
6472 case DW_FORM_GNU_str_index:
d521ce57 6473 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6474 break;
6475 case DW_FORM_indirect:
6476 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6477 info_ptr += bytes_read;
6478 /* We need to continue parsing from here, so just go back to
6479 the top. */
6480 goto skip_attribute;
6481
6482 default:
3e43a32a
MS
6483 error (_("Dwarf Error: Cannot handle %s "
6484 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6485 dwarf_form_name (form),
6486 bfd_get_filename (abfd));
6487 }
6488 }
6489
6490 if (abbrev->has_children)
dee91e82 6491 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6492 else
6493 return info_ptr;
6494}
6495
93311388 6496/* Locate ORIG_PDI's sibling.
dee91e82 6497 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6498
d521ce57 6499static const gdb_byte *
dee91e82
DE
6500locate_pdi_sibling (const struct die_reader_specs *reader,
6501 struct partial_die_info *orig_pdi,
d521ce57 6502 const gdb_byte *info_ptr)
91c24f0a
DC
6503{
6504 /* Do we know the sibling already? */
72bf9492 6505
91c24f0a
DC
6506 if (orig_pdi->sibling)
6507 return orig_pdi->sibling;
6508
6509 /* Are there any children to deal with? */
6510
6511 if (!orig_pdi->has_children)
6512 return info_ptr;
6513
4bb7a0a7 6514 /* Skip the children the long way. */
91c24f0a 6515
dee91e82 6516 return skip_children (reader, info_ptr);
91c24f0a
DC
6517}
6518
257e7a09 6519/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6520 not NULL. */
c906108c
SS
6521
6522static void
257e7a09
YQ
6523dwarf2_read_symtab (struct partial_symtab *self,
6524 struct objfile *objfile)
c906108c 6525{
257e7a09 6526 if (self->readin)
c906108c 6527 {
442e4d9c 6528 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6529 self->filename);
442e4d9c
YQ
6530 }
6531 else
6532 {
6533 if (info_verbose)
c906108c 6534 {
442e4d9c 6535 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6536 self->filename);
442e4d9c 6537 gdb_flush (gdb_stdout);
c906108c 6538 }
c906108c 6539
442e4d9c
YQ
6540 /* Restore our global data. */
6541 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6542
442e4d9c
YQ
6543 /* If this psymtab is constructed from a debug-only objfile, the
6544 has_section_at_zero flag will not necessarily be correct. We
6545 can get the correct value for this flag by looking at the data
6546 associated with the (presumably stripped) associated objfile. */
6547 if (objfile->separate_debug_objfile_backlink)
6548 {
6549 struct dwarf2_per_objfile *dpo_backlink
6550 = objfile_data (objfile->separate_debug_objfile_backlink,
6551 dwarf2_objfile_data_key);
9a619af0 6552
442e4d9c
YQ
6553 dwarf2_per_objfile->has_section_at_zero
6554 = dpo_backlink->has_section_at_zero;
6555 }
b2ab525c 6556
442e4d9c 6557 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6558
257e7a09 6559 psymtab_to_symtab_1 (self);
c906108c 6560
442e4d9c
YQ
6561 /* Finish up the debug error message. */
6562 if (info_verbose)
6563 printf_filtered (_("done.\n"));
c906108c 6564 }
95554aad
TT
6565
6566 process_cu_includes ();
c906108c 6567}
9cdd5dbd
DE
6568\f
6569/* Reading in full CUs. */
c906108c 6570
10b3939b
DJ
6571/* Add PER_CU to the queue. */
6572
6573static void
95554aad
TT
6574queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6575 enum language pretend_language)
10b3939b
DJ
6576{
6577 struct dwarf2_queue_item *item;
6578
6579 per_cu->queued = 1;
6580 item = xmalloc (sizeof (*item));
6581 item->per_cu = per_cu;
95554aad 6582 item->pretend_language = pretend_language;
10b3939b
DJ
6583 item->next = NULL;
6584
6585 if (dwarf2_queue == NULL)
6586 dwarf2_queue = item;
6587 else
6588 dwarf2_queue_tail->next = item;
6589
6590 dwarf2_queue_tail = item;
6591}
6592
0907af0c
DE
6593/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6594 unit and add it to our queue.
6595 The result is non-zero if PER_CU was queued, otherwise the result is zero
6596 meaning either PER_CU is already queued or it is already loaded. */
6597
6598static int
6599maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6600 struct dwarf2_per_cu_data *per_cu,
6601 enum language pretend_language)
6602{
6603 /* We may arrive here during partial symbol reading, if we need full
6604 DIEs to process an unusual case (e.g. template arguments). Do
6605 not queue PER_CU, just tell our caller to load its DIEs. */
6606 if (dwarf2_per_objfile->reading_partial_symbols)
6607 {
6608 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6609 return 1;
6610 return 0;
6611 }
6612
6613 /* Mark the dependence relation so that we don't flush PER_CU
6614 too early. */
6615 dwarf2_add_dependence (this_cu, per_cu);
6616
6617 /* If it's already on the queue, we have nothing to do. */
6618 if (per_cu->queued)
6619 return 0;
6620
6621 /* If the compilation unit is already loaded, just mark it as
6622 used. */
6623 if (per_cu->cu != NULL)
6624 {
6625 per_cu->cu->last_used = 0;
6626 return 0;
6627 }
6628
6629 /* Add it to the queue. */
6630 queue_comp_unit (per_cu, pretend_language);
6631
6632 return 1;
6633}
6634
10b3939b
DJ
6635/* Process the queue. */
6636
6637static void
a0f42c21 6638process_queue (void)
10b3939b
DJ
6639{
6640 struct dwarf2_queue_item *item, *next_item;
6641
45cfd468
DE
6642 if (dwarf2_read_debug)
6643 {
6644 fprintf_unfiltered (gdb_stdlog,
6645 "Expanding one or more symtabs of objfile %s ...\n",
6646 dwarf2_per_objfile->objfile->name);
6647 }
6648
03dd20cc
DJ
6649 /* The queue starts out with one item, but following a DIE reference
6650 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6651 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6652 {
9291a0cd
TT
6653 if (dwarf2_per_objfile->using_index
6654 ? !item->per_cu->v.quick->symtab
6655 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6656 {
6657 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6658
6659 if (dwarf2_read_debug)
6660 {
6661 fprintf_unfiltered (gdb_stdlog,
6662 "Expanding symtab of %s at offset 0x%x\n",
6663 per_cu->is_debug_types ? "TU" : "CU",
6664 per_cu->offset.sect_off);
6665 }
6666
6667 if (per_cu->is_debug_types)
6668 process_full_type_unit (per_cu, item->pretend_language);
6669 else
6670 process_full_comp_unit (per_cu, item->pretend_language);
6671
6672 if (dwarf2_read_debug)
6673 {
6674 fprintf_unfiltered (gdb_stdlog,
6675 "Done expanding %s at offset 0x%x\n",
6676 per_cu->is_debug_types ? "TU" : "CU",
6677 per_cu->offset.sect_off);
6678 }
6679 }
10b3939b
DJ
6680
6681 item->per_cu->queued = 0;
6682 next_item = item->next;
6683 xfree (item);
6684 }
6685
6686 dwarf2_queue_tail = NULL;
45cfd468
DE
6687
6688 if (dwarf2_read_debug)
6689 {
6690 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6691 dwarf2_per_objfile->objfile->name);
6692 }
10b3939b
DJ
6693}
6694
6695/* Free all allocated queue entries. This function only releases anything if
6696 an error was thrown; if the queue was processed then it would have been
6697 freed as we went along. */
6698
6699static void
6700dwarf2_release_queue (void *dummy)
6701{
6702 struct dwarf2_queue_item *item, *last;
6703
6704 item = dwarf2_queue;
6705 while (item)
6706 {
6707 /* Anything still marked queued is likely to be in an
6708 inconsistent state, so discard it. */
6709 if (item->per_cu->queued)
6710 {
6711 if (item->per_cu->cu != NULL)
dee91e82 6712 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6713 item->per_cu->queued = 0;
6714 }
6715
6716 last = item;
6717 item = item->next;
6718 xfree (last);
6719 }
6720
6721 dwarf2_queue = dwarf2_queue_tail = NULL;
6722}
6723
6724/* Read in full symbols for PST, and anything it depends on. */
6725
c906108c 6726static void
fba45db2 6727psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6728{
10b3939b 6729 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6730 int i;
6731
95554aad
TT
6732 if (pst->readin)
6733 return;
6734
aaa75496 6735 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6736 if (!pst->dependencies[i]->readin
6737 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6738 {
6739 /* Inform about additional files that need to be read in. */
6740 if (info_verbose)
6741 {
a3f17187 6742 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6743 fputs_filtered (" ", gdb_stdout);
6744 wrap_here ("");
6745 fputs_filtered ("and ", gdb_stdout);
6746 wrap_here ("");
6747 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6748 wrap_here (""); /* Flush output. */
aaa75496
JB
6749 gdb_flush (gdb_stdout);
6750 }
6751 psymtab_to_symtab_1 (pst->dependencies[i]);
6752 }
6753
e38df1d0 6754 per_cu = pst->read_symtab_private;
10b3939b
DJ
6755
6756 if (per_cu == NULL)
aaa75496
JB
6757 {
6758 /* It's an include file, no symbols to read for it.
6759 Everything is in the parent symtab. */
6760 pst->readin = 1;
6761 return;
6762 }
c906108c 6763
a0f42c21 6764 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6765}
6766
dee91e82
DE
6767/* Trivial hash function for die_info: the hash value of a DIE
6768 is its offset in .debug_info for this objfile. */
10b3939b 6769
dee91e82
DE
6770static hashval_t
6771die_hash (const void *item)
10b3939b 6772{
dee91e82 6773 const struct die_info *die = item;
6502dd73 6774
dee91e82
DE
6775 return die->offset.sect_off;
6776}
63d06c5c 6777
dee91e82
DE
6778/* Trivial comparison function for die_info structures: two DIEs
6779 are equal if they have the same offset. */
98bfdba5 6780
dee91e82
DE
6781static int
6782die_eq (const void *item_lhs, const void *item_rhs)
6783{
6784 const struct die_info *die_lhs = item_lhs;
6785 const struct die_info *die_rhs = item_rhs;
c906108c 6786
dee91e82
DE
6787 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6788}
c906108c 6789
dee91e82
DE
6790/* die_reader_func for load_full_comp_unit.
6791 This is identical to read_signatured_type_reader,
6792 but is kept separate for now. */
c906108c 6793
dee91e82
DE
6794static void
6795load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6796 const gdb_byte *info_ptr,
dee91e82
DE
6797 struct die_info *comp_unit_die,
6798 int has_children,
6799 void *data)
6800{
6801 struct dwarf2_cu *cu = reader->cu;
95554aad 6802 enum language *language_ptr = data;
6caca83c 6803
dee91e82
DE
6804 gdb_assert (cu->die_hash == NULL);
6805 cu->die_hash =
6806 htab_create_alloc_ex (cu->header.length / 12,
6807 die_hash,
6808 die_eq,
6809 NULL,
6810 &cu->comp_unit_obstack,
6811 hashtab_obstack_allocate,
6812 dummy_obstack_deallocate);
e142c38c 6813
dee91e82
DE
6814 if (has_children)
6815 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6816 &info_ptr, comp_unit_die);
6817 cu->dies = comp_unit_die;
6818 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6819
6820 /* We try not to read any attributes in this function, because not
9cdd5dbd 6821 all CUs needed for references have been loaded yet, and symbol
10b3939b 6822 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6823 or we won't be able to build types correctly.
6824 Similarly, if we do not read the producer, we can not apply
6825 producer-specific interpretation. */
95554aad 6826 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6827}
10b3939b 6828
dee91e82 6829/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6830
dee91e82 6831static void
95554aad
TT
6832load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6833 enum language pretend_language)
dee91e82 6834{
3019eac3 6835 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6836
f4dc4d17
DE
6837 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6838 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6839}
6840
3da10d80
KS
6841/* Add a DIE to the delayed physname list. */
6842
6843static void
6844add_to_method_list (struct type *type, int fnfield_index, int index,
6845 const char *name, struct die_info *die,
6846 struct dwarf2_cu *cu)
6847{
6848 struct delayed_method_info mi;
6849 mi.type = type;
6850 mi.fnfield_index = fnfield_index;
6851 mi.index = index;
6852 mi.name = name;
6853 mi.die = die;
6854 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6855}
6856
6857/* A cleanup for freeing the delayed method list. */
6858
6859static void
6860free_delayed_list (void *ptr)
6861{
6862 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6863 if (cu->method_list != NULL)
6864 {
6865 VEC_free (delayed_method_info, cu->method_list);
6866 cu->method_list = NULL;
6867 }
6868}
6869
6870/* Compute the physnames of any methods on the CU's method list.
6871
6872 The computation of method physnames is delayed in order to avoid the
6873 (bad) condition that one of the method's formal parameters is of an as yet
6874 incomplete type. */
6875
6876static void
6877compute_delayed_physnames (struct dwarf2_cu *cu)
6878{
6879 int i;
6880 struct delayed_method_info *mi;
6881 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6882 {
1d06ead6 6883 const char *physname;
3da10d80
KS
6884 struct fn_fieldlist *fn_flp
6885 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6886 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6887 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6888 }
6889}
6890
a766d390
DE
6891/* Go objects should be embedded in a DW_TAG_module DIE,
6892 and it's not clear if/how imported objects will appear.
6893 To keep Go support simple until that's worked out,
6894 go back through what we've read and create something usable.
6895 We could do this while processing each DIE, and feels kinda cleaner,
6896 but that way is more invasive.
6897 This is to, for example, allow the user to type "p var" or "b main"
6898 without having to specify the package name, and allow lookups
6899 of module.object to work in contexts that use the expression
6900 parser. */
6901
6902static void
6903fixup_go_packaging (struct dwarf2_cu *cu)
6904{
6905 char *package_name = NULL;
6906 struct pending *list;
6907 int i;
6908
6909 for (list = global_symbols; list != NULL; list = list->next)
6910 {
6911 for (i = 0; i < list->nsyms; ++i)
6912 {
6913 struct symbol *sym = list->symbol[i];
6914
6915 if (SYMBOL_LANGUAGE (sym) == language_go
6916 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6917 {
6918 char *this_package_name = go_symbol_package_name (sym);
6919
6920 if (this_package_name == NULL)
6921 continue;
6922 if (package_name == NULL)
6923 package_name = this_package_name;
6924 else
6925 {
6926 if (strcmp (package_name, this_package_name) != 0)
6927 complaint (&symfile_complaints,
6928 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6929 (SYMBOL_SYMTAB (sym)
05cba821 6930 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
6931 : cu->objfile->name),
6932 this_package_name, package_name);
6933 xfree (this_package_name);
6934 }
6935 }
6936 }
6937 }
6938
6939 if (package_name != NULL)
6940 {
6941 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6942 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6943 package_name,
6944 strlen (package_name));
a766d390 6945 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6946 saved_package_name, objfile);
a766d390
DE
6947 struct symbol *sym;
6948
6949 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6950
e623cf5d 6951 sym = allocate_symbol (objfile);
f85f34ed 6952 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
6953 SYMBOL_SET_NAMES (sym, saved_package_name,
6954 strlen (saved_package_name), 0, objfile);
a766d390
DE
6955 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6956 e.g., "main" finds the "main" module and not C's main(). */
6957 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 6958 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
6959 SYMBOL_TYPE (sym) = type;
6960
6961 add_symbol_to_list (sym, &global_symbols);
6962
6963 xfree (package_name);
6964 }
6965}
6966
95554aad
TT
6967/* Return the symtab for PER_CU. This works properly regardless of
6968 whether we're using the index or psymtabs. */
6969
6970static struct symtab *
6971get_symtab (struct dwarf2_per_cu_data *per_cu)
6972{
6973 return (dwarf2_per_objfile->using_index
6974 ? per_cu->v.quick->symtab
6975 : per_cu->v.psymtab->symtab);
6976}
6977
6978/* A helper function for computing the list of all symbol tables
6979 included by PER_CU. */
6980
6981static void
6982recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6983 htab_t all_children,
6984 struct dwarf2_per_cu_data *per_cu)
6985{
6986 void **slot;
6987 int ix;
6988 struct dwarf2_per_cu_data *iter;
6989
6990 slot = htab_find_slot (all_children, per_cu, INSERT);
6991 if (*slot != NULL)
6992 {
6993 /* This inclusion and its children have been processed. */
6994 return;
6995 }
6996
6997 *slot = per_cu;
6998 /* Only add a CU if it has a symbol table. */
6999 if (get_symtab (per_cu) != NULL)
7000 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7001
7002 for (ix = 0;
796a7ff8 7003 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
7004 ++ix)
7005 recursively_compute_inclusions (result, all_children, iter);
7006}
7007
7008/* Compute the symtab 'includes' fields for the symtab related to
7009 PER_CU. */
7010
7011static void
7012compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7013{
f4dc4d17
DE
7014 gdb_assert (! per_cu->is_debug_types);
7015
796a7ff8 7016 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7017 {
7018 int ix, len;
7019 struct dwarf2_per_cu_data *iter;
7020 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7021 htab_t all_children;
7022 struct symtab *symtab = get_symtab (per_cu);
7023
7024 /* If we don't have a symtab, we can just skip this case. */
7025 if (symtab == NULL)
7026 return;
7027
7028 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7029 NULL, xcalloc, xfree);
7030
7031 for (ix = 0;
796a7ff8 7032 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
7033 ix, iter);
7034 ++ix)
7035 recursively_compute_inclusions (&result_children, all_children, iter);
7036
796a7ff8
DE
7037 /* Now we have a transitive closure of all the included CUs, and
7038 for .gdb_index version 7 the included TUs, so we can convert it
7039 to a list of symtabs. */
95554aad
TT
7040 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7041 symtab->includes
7042 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7043 (len + 1) * sizeof (struct symtab *));
7044 for (ix = 0;
7045 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7046 ++ix)
7047 symtab->includes[ix] = get_symtab (iter);
7048 symtab->includes[len] = NULL;
7049
7050 VEC_free (dwarf2_per_cu_ptr, result_children);
7051 htab_delete (all_children);
7052 }
7053}
7054
7055/* Compute the 'includes' field for the symtabs of all the CUs we just
7056 read. */
7057
7058static void
7059process_cu_includes (void)
7060{
7061 int ix;
7062 struct dwarf2_per_cu_data *iter;
7063
7064 for (ix = 0;
7065 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7066 ix, iter);
7067 ++ix)
f4dc4d17
DE
7068 {
7069 if (! iter->is_debug_types)
7070 compute_symtab_includes (iter);
7071 }
95554aad
TT
7072
7073 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7074}
7075
9cdd5dbd 7076/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7077 already been loaded into memory. */
7078
7079static void
95554aad
TT
7080process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7081 enum language pretend_language)
10b3939b 7082{
10b3939b 7083 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7084 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7085 CORE_ADDR lowpc, highpc;
7086 struct symtab *symtab;
3da10d80 7087 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7088 CORE_ADDR baseaddr;
4359dff1 7089 struct block *static_block;
10b3939b
DJ
7090
7091 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7092
10b3939b
DJ
7093 buildsym_init ();
7094 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7095 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7096
7097 cu->list_in_scope = &file_symbols;
c906108c 7098
95554aad
TT
7099 cu->language = pretend_language;
7100 cu->language_defn = language_def (cu->language);
7101
c906108c 7102 /* Do line number decoding in read_file_scope () */
10b3939b 7103 process_die (cu->dies, cu);
c906108c 7104
a766d390
DE
7105 /* For now fudge the Go package. */
7106 if (cu->language == language_go)
7107 fixup_go_packaging (cu);
7108
3da10d80
KS
7109 /* Now that we have processed all the DIEs in the CU, all the types
7110 should be complete, and it should now be safe to compute all of the
7111 physnames. */
7112 compute_delayed_physnames (cu);
7113 do_cleanups (delayed_list_cleanup);
7114
fae299cd
DC
7115 /* Some compilers don't define a DW_AT_high_pc attribute for the
7116 compilation unit. If the DW_AT_high_pc is missing, synthesize
7117 it, by scanning the DIE's below the compilation unit. */
10b3939b 7118 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7119
36586728
TT
7120 static_block
7121 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7122 per_cu->imported_symtabs != NULL);
4359dff1
JK
7123
7124 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7125 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7126 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7127 addrmap to help ensure it has an accurate map of pc values belonging to
7128 this comp unit. */
7129 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7130
7131 symtab = end_symtab_from_static_block (static_block, objfile,
7132 SECT_OFF_TEXT (objfile), 0);
c906108c 7133
8be455d7 7134 if (symtab != NULL)
c906108c 7135 {
df15bd07 7136 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7137
8be455d7
JK
7138 /* Set symtab language to language from DW_AT_language. If the
7139 compilation is from a C file generated by language preprocessors, do
7140 not set the language if it was already deduced by start_subfile. */
7141 if (!(cu->language == language_c && symtab->language != language_c))
7142 symtab->language = cu->language;
7143
7144 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7145 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7146 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7147 there were bugs in prologue debug info, fixed later in GCC-4.5
7148 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7149
7150 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7151 needed, it would be wrong due to missing DW_AT_producer there.
7152
7153 Still one can confuse GDB by using non-standard GCC compilation
7154 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7155 */
ab260dad 7156 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7157 symtab->locations_valid = 1;
e0d00bc7
JK
7158
7159 if (gcc_4_minor >= 5)
7160 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7161
7162 symtab->call_site_htab = cu->call_site_htab;
c906108c 7163 }
9291a0cd
TT
7164
7165 if (dwarf2_per_objfile->using_index)
7166 per_cu->v.quick->symtab = symtab;
7167 else
7168 {
7169 struct partial_symtab *pst = per_cu->v.psymtab;
7170 pst->symtab = symtab;
7171 pst->readin = 1;
7172 }
c906108c 7173
95554aad
TT
7174 /* Push it for inclusion processing later. */
7175 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7176
c906108c 7177 do_cleanups (back_to);
f4dc4d17 7178}
45cfd468 7179
f4dc4d17
DE
7180/* Generate full symbol information for type unit PER_CU, whose DIEs have
7181 already been loaded into memory. */
7182
7183static void
7184process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7185 enum language pretend_language)
7186{
7187 struct dwarf2_cu *cu = per_cu->cu;
7188 struct objfile *objfile = per_cu->objfile;
7189 struct symtab *symtab;
7190 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7191 struct signatured_type *sig_type;
7192
7193 gdb_assert (per_cu->is_debug_types);
7194 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7195
7196 buildsym_init ();
7197 back_to = make_cleanup (really_free_pendings, NULL);
7198 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7199
7200 cu->list_in_scope = &file_symbols;
7201
7202 cu->language = pretend_language;
7203 cu->language_defn = language_def (cu->language);
7204
7205 /* The symbol tables are set up in read_type_unit_scope. */
7206 process_die (cu->dies, cu);
7207
7208 /* For now fudge the Go package. */
7209 if (cu->language == language_go)
7210 fixup_go_packaging (cu);
7211
7212 /* Now that we have processed all the DIEs in the CU, all the types
7213 should be complete, and it should now be safe to compute all of the
7214 physnames. */
7215 compute_delayed_physnames (cu);
7216 do_cleanups (delayed_list_cleanup);
7217
7218 /* TUs share symbol tables.
7219 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7220 of it with end_expandable_symtab. Otherwise, complete the addition of
7221 this TU's symbols to the existing symtab. */
0186c6a7 7222 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7223 {
f4dc4d17 7224 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7225 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7226
7227 if (symtab != NULL)
7228 {
7229 /* Set symtab language to language from DW_AT_language. If the
7230 compilation is from a C file generated by language preprocessors,
7231 do not set the language if it was already deduced by
7232 start_subfile. */
7233 if (!(cu->language == language_c && symtab->language != language_c))
7234 symtab->language = cu->language;
7235 }
7236 }
7237 else
7238 {
7239 augment_type_symtab (objfile,
0186c6a7
DE
7240 sig_type->type_unit_group->primary_symtab);
7241 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7242 }
7243
7244 if (dwarf2_per_objfile->using_index)
7245 per_cu->v.quick->symtab = symtab;
7246 else
7247 {
7248 struct partial_symtab *pst = per_cu->v.psymtab;
7249 pst->symtab = symtab;
7250 pst->readin = 1;
45cfd468 7251 }
f4dc4d17
DE
7252
7253 do_cleanups (back_to);
c906108c
SS
7254}
7255
95554aad
TT
7256/* Process an imported unit DIE. */
7257
7258static void
7259process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7260{
7261 struct attribute *attr;
7262
f4dc4d17
DE
7263 /* For now we don't handle imported units in type units. */
7264 if (cu->per_cu->is_debug_types)
7265 {
7266 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7267 " supported in type units [in module %s]"),
7268 cu->objfile->name);
7269 }
7270
95554aad
TT
7271 attr = dwarf2_attr (die, DW_AT_import, cu);
7272 if (attr != NULL)
7273 {
7274 struct dwarf2_per_cu_data *per_cu;
7275 struct symtab *imported_symtab;
7276 sect_offset offset;
36586728 7277 int is_dwz;
95554aad
TT
7278
7279 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7280 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7281 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7282
7283 /* Queue the unit, if needed. */
7284 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7285 load_full_comp_unit (per_cu, cu->language);
7286
796a7ff8 7287 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7288 per_cu);
7289 }
7290}
7291
c906108c
SS
7292/* Process a die and its children. */
7293
7294static void
e7c27a73 7295process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7296{
7297 switch (die->tag)
7298 {
7299 case DW_TAG_padding:
7300 break;
7301 case DW_TAG_compile_unit:
95554aad 7302 case DW_TAG_partial_unit:
e7c27a73 7303 read_file_scope (die, cu);
c906108c 7304 break;
348e048f
DE
7305 case DW_TAG_type_unit:
7306 read_type_unit_scope (die, cu);
7307 break;
c906108c 7308 case DW_TAG_subprogram:
c906108c 7309 case DW_TAG_inlined_subroutine:
edb3359d 7310 read_func_scope (die, cu);
c906108c
SS
7311 break;
7312 case DW_TAG_lexical_block:
14898363
L
7313 case DW_TAG_try_block:
7314 case DW_TAG_catch_block:
e7c27a73 7315 read_lexical_block_scope (die, cu);
c906108c 7316 break;
96408a79
SA
7317 case DW_TAG_GNU_call_site:
7318 read_call_site_scope (die, cu);
7319 break;
c906108c 7320 case DW_TAG_class_type:
680b30c7 7321 case DW_TAG_interface_type:
c906108c
SS
7322 case DW_TAG_structure_type:
7323 case DW_TAG_union_type:
134d01f1 7324 process_structure_scope (die, cu);
c906108c
SS
7325 break;
7326 case DW_TAG_enumeration_type:
134d01f1 7327 process_enumeration_scope (die, cu);
c906108c 7328 break;
134d01f1 7329
f792889a
DJ
7330 /* These dies have a type, but processing them does not create
7331 a symbol or recurse to process the children. Therefore we can
7332 read them on-demand through read_type_die. */
c906108c 7333 case DW_TAG_subroutine_type:
72019c9c 7334 case DW_TAG_set_type:
c906108c 7335 case DW_TAG_array_type:
c906108c 7336 case DW_TAG_pointer_type:
c906108c 7337 case DW_TAG_ptr_to_member_type:
c906108c 7338 case DW_TAG_reference_type:
c906108c 7339 case DW_TAG_string_type:
c906108c 7340 break;
134d01f1 7341
c906108c 7342 case DW_TAG_base_type:
a02abb62 7343 case DW_TAG_subrange_type:
cb249c71 7344 case DW_TAG_typedef:
134d01f1
DJ
7345 /* Add a typedef symbol for the type definition, if it has a
7346 DW_AT_name. */
f792889a 7347 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7348 break;
c906108c 7349 case DW_TAG_common_block:
e7c27a73 7350 read_common_block (die, cu);
c906108c
SS
7351 break;
7352 case DW_TAG_common_inclusion:
7353 break;
d9fa45fe 7354 case DW_TAG_namespace:
4d4ec4e5 7355 cu->processing_has_namespace_info = 1;
e7c27a73 7356 read_namespace (die, cu);
d9fa45fe 7357 break;
5d7cb8df 7358 case DW_TAG_module:
4d4ec4e5 7359 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7360 read_module (die, cu);
7361 break;
d9fa45fe
DC
7362 case DW_TAG_imported_declaration:
7363 case DW_TAG_imported_module:
4d4ec4e5 7364 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7365 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7366 || cu->language != language_fortran))
7367 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7368 dwarf_tag_name (die->tag));
7369 read_import_statement (die, cu);
d9fa45fe 7370 break;
95554aad
TT
7371
7372 case DW_TAG_imported_unit:
7373 process_imported_unit_die (die, cu);
7374 break;
7375
c906108c 7376 default:
e7c27a73 7377 new_symbol (die, NULL, cu);
c906108c
SS
7378 break;
7379 }
7380}
ca69b9e6
DE
7381\f
7382/* DWARF name computation. */
c906108c 7383
94af9270
KS
7384/* A helper function for dwarf2_compute_name which determines whether DIE
7385 needs to have the name of the scope prepended to the name listed in the
7386 die. */
7387
7388static int
7389die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7390{
1c809c68
TT
7391 struct attribute *attr;
7392
94af9270
KS
7393 switch (die->tag)
7394 {
7395 case DW_TAG_namespace:
7396 case DW_TAG_typedef:
7397 case DW_TAG_class_type:
7398 case DW_TAG_interface_type:
7399 case DW_TAG_structure_type:
7400 case DW_TAG_union_type:
7401 case DW_TAG_enumeration_type:
7402 case DW_TAG_enumerator:
7403 case DW_TAG_subprogram:
7404 case DW_TAG_member:
7405 return 1;
7406
7407 case DW_TAG_variable:
c2b0a229 7408 case DW_TAG_constant:
94af9270
KS
7409 /* We only need to prefix "globally" visible variables. These include
7410 any variable marked with DW_AT_external or any variable that
7411 lives in a namespace. [Variables in anonymous namespaces
7412 require prefixing, but they are not DW_AT_external.] */
7413
7414 if (dwarf2_attr (die, DW_AT_specification, cu))
7415 {
7416 struct dwarf2_cu *spec_cu = cu;
9a619af0 7417
94af9270
KS
7418 return die_needs_namespace (die_specification (die, &spec_cu),
7419 spec_cu);
7420 }
7421
1c809c68 7422 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7423 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7424 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7425 return 0;
7426 /* A variable in a lexical block of some kind does not need a
7427 namespace, even though in C++ such variables may be external
7428 and have a mangled name. */
7429 if (die->parent->tag == DW_TAG_lexical_block
7430 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7431 || die->parent->tag == DW_TAG_catch_block
7432 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7433 return 0;
7434 return 1;
94af9270
KS
7435
7436 default:
7437 return 0;
7438 }
7439}
7440
98bfdba5
PA
7441/* Retrieve the last character from a mem_file. */
7442
7443static void
7444do_ui_file_peek_last (void *object, const char *buffer, long length)
7445{
7446 char *last_char_p = (char *) object;
7447
7448 if (length > 0)
7449 *last_char_p = buffer[length - 1];
7450}
7451
94af9270 7452/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7453 compute the physname for the object, which include a method's:
7454 - formal parameters (C++/Java),
7455 - receiver type (Go),
7456 - return type (Java).
7457
7458 The term "physname" is a bit confusing.
7459 For C++, for example, it is the demangled name.
7460 For Go, for example, it's the mangled name.
94af9270 7461
af6b7be1
JB
7462 For Ada, return the DIE's linkage name rather than the fully qualified
7463 name. PHYSNAME is ignored..
7464
94af9270
KS
7465 The result is allocated on the objfile_obstack and canonicalized. */
7466
7467static const char *
15d034d0
TT
7468dwarf2_compute_name (const char *name,
7469 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7470 int physname)
7471{
bb5ed363
DE
7472 struct objfile *objfile = cu->objfile;
7473
94af9270
KS
7474 if (name == NULL)
7475 name = dwarf2_name (die, cu);
7476
f55ee35c
JK
7477 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7478 compute it by typename_concat inside GDB. */
7479 if (cu->language == language_ada
7480 || (cu->language == language_fortran && physname))
7481 {
7482 /* For Ada unit, we prefer the linkage name over the name, as
7483 the former contains the exported name, which the user expects
7484 to be able to reference. Ideally, we want the user to be able
7485 to reference this entity using either natural or linkage name,
7486 but we haven't started looking at this enhancement yet. */
7487 struct attribute *attr;
7488
7489 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7490 if (attr == NULL)
7491 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7492 if (attr && DW_STRING (attr))
7493 return DW_STRING (attr);
7494 }
7495
94af9270
KS
7496 /* These are the only languages we know how to qualify names in. */
7497 if (name != NULL
f55ee35c
JK
7498 && (cu->language == language_cplus || cu->language == language_java
7499 || cu->language == language_fortran))
94af9270
KS
7500 {
7501 if (die_needs_namespace (die, cu))
7502 {
7503 long length;
0d5cff50 7504 const char *prefix;
94af9270
KS
7505 struct ui_file *buf;
7506
7507 prefix = determine_prefix (die, cu);
7508 buf = mem_fileopen ();
7509 if (*prefix != '\0')
7510 {
f55ee35c
JK
7511 char *prefixed_name = typename_concat (NULL, prefix, name,
7512 physname, cu);
9a619af0 7513
94af9270
KS
7514 fputs_unfiltered (prefixed_name, buf);
7515 xfree (prefixed_name);
7516 }
7517 else
62d5b8da 7518 fputs_unfiltered (name, buf);
94af9270 7519
98bfdba5
PA
7520 /* Template parameters may be specified in the DIE's DW_AT_name, or
7521 as children with DW_TAG_template_type_param or
7522 DW_TAG_value_type_param. If the latter, add them to the name
7523 here. If the name already has template parameters, then
7524 skip this step; some versions of GCC emit both, and
7525 it is more efficient to use the pre-computed name.
7526
7527 Something to keep in mind about this process: it is very
7528 unlikely, or in some cases downright impossible, to produce
7529 something that will match the mangled name of a function.
7530 If the definition of the function has the same debug info,
7531 we should be able to match up with it anyway. But fallbacks
7532 using the minimal symbol, for instance to find a method
7533 implemented in a stripped copy of libstdc++, will not work.
7534 If we do not have debug info for the definition, we will have to
7535 match them up some other way.
7536
7537 When we do name matching there is a related problem with function
7538 templates; two instantiated function templates are allowed to
7539 differ only by their return types, which we do not add here. */
7540
7541 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7542 {
7543 struct attribute *attr;
7544 struct die_info *child;
7545 int first = 1;
7546
7547 die->building_fullname = 1;
7548
7549 for (child = die->child; child != NULL; child = child->sibling)
7550 {
7551 struct type *type;
12df843f 7552 LONGEST value;
d521ce57 7553 const gdb_byte *bytes;
98bfdba5
PA
7554 struct dwarf2_locexpr_baton *baton;
7555 struct value *v;
7556
7557 if (child->tag != DW_TAG_template_type_param
7558 && child->tag != DW_TAG_template_value_param)
7559 continue;
7560
7561 if (first)
7562 {
7563 fputs_unfiltered ("<", buf);
7564 first = 0;
7565 }
7566 else
7567 fputs_unfiltered (", ", buf);
7568
7569 attr = dwarf2_attr (child, DW_AT_type, cu);
7570 if (attr == NULL)
7571 {
7572 complaint (&symfile_complaints,
7573 _("template parameter missing DW_AT_type"));
7574 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7575 continue;
7576 }
7577 type = die_type (child, cu);
7578
7579 if (child->tag == DW_TAG_template_type_param)
7580 {
79d43c61 7581 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7582 continue;
7583 }
7584
7585 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7586 if (attr == NULL)
7587 {
7588 complaint (&symfile_complaints,
3e43a32a
MS
7589 _("template parameter missing "
7590 "DW_AT_const_value"));
98bfdba5
PA
7591 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7592 continue;
7593 }
7594
7595 dwarf2_const_value_attr (attr, type, name,
7596 &cu->comp_unit_obstack, cu,
7597 &value, &bytes, &baton);
7598
7599 if (TYPE_NOSIGN (type))
7600 /* GDB prints characters as NUMBER 'CHAR'. If that's
7601 changed, this can use value_print instead. */
7602 c_printchar (value, type, buf);
7603 else
7604 {
7605 struct value_print_options opts;
7606
7607 if (baton != NULL)
7608 v = dwarf2_evaluate_loc_desc (type, NULL,
7609 baton->data,
7610 baton->size,
7611 baton->per_cu);
7612 else if (bytes != NULL)
7613 {
7614 v = allocate_value (type);
7615 memcpy (value_contents_writeable (v), bytes,
7616 TYPE_LENGTH (type));
7617 }
7618 else
7619 v = value_from_longest (type, value);
7620
3e43a32a
MS
7621 /* Specify decimal so that we do not depend on
7622 the radix. */
98bfdba5
PA
7623 get_formatted_print_options (&opts, 'd');
7624 opts.raw = 1;
7625 value_print (v, buf, &opts);
7626 release_value (v);
7627 value_free (v);
7628 }
7629 }
7630
7631 die->building_fullname = 0;
7632
7633 if (!first)
7634 {
7635 /* Close the argument list, with a space if necessary
7636 (nested templates). */
7637 char last_char = '\0';
7638 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7639 if (last_char == '>')
7640 fputs_unfiltered (" >", buf);
7641 else
7642 fputs_unfiltered (">", buf);
7643 }
7644 }
7645
94af9270
KS
7646 /* For Java and C++ methods, append formal parameter type
7647 information, if PHYSNAME. */
6e70227d 7648
94af9270
KS
7649 if (physname && die->tag == DW_TAG_subprogram
7650 && (cu->language == language_cplus
7651 || cu->language == language_java))
7652 {
7653 struct type *type = read_type_die (die, cu);
7654
79d43c61
TT
7655 c_type_print_args (type, buf, 1, cu->language,
7656 &type_print_raw_options);
94af9270
KS
7657
7658 if (cu->language == language_java)
7659 {
7660 /* For java, we must append the return type to method
0963b4bd 7661 names. */
94af9270
KS
7662 if (die->tag == DW_TAG_subprogram)
7663 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7664 0, 0, &type_print_raw_options);
94af9270
KS
7665 }
7666 else if (cu->language == language_cplus)
7667 {
60430eff
DJ
7668 /* Assume that an artificial first parameter is
7669 "this", but do not crash if it is not. RealView
7670 marks unnamed (and thus unused) parameters as
7671 artificial; there is no way to differentiate
7672 the two cases. */
94af9270
KS
7673 if (TYPE_NFIELDS (type) > 0
7674 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7675 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7676 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7677 0))))
94af9270
KS
7678 fputs_unfiltered (" const", buf);
7679 }
7680 }
7681
bb5ed363 7682 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7683 &length);
7684 ui_file_delete (buf);
7685
7686 if (cu->language == language_cplus)
7687 {
15d034d0 7688 const char *cname
94af9270 7689 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7690 &objfile->objfile_obstack);
9a619af0 7691
94af9270
KS
7692 if (cname != NULL)
7693 name = cname;
7694 }
7695 }
7696 }
7697
7698 return name;
7699}
7700
0114d602
DJ
7701/* Return the fully qualified name of DIE, based on its DW_AT_name.
7702 If scope qualifiers are appropriate they will be added. The result
7703 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7704 not have a name. NAME may either be from a previous call to
7705 dwarf2_name or NULL.
7706
0963b4bd 7707 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7708
7709static const char *
15d034d0 7710dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7711{
94af9270
KS
7712 return dwarf2_compute_name (name, die, cu, 0);
7713}
0114d602 7714
94af9270
KS
7715/* Construct a physname for the given DIE in CU. NAME may either be
7716 from a previous call to dwarf2_name or NULL. The result will be
7717 allocated on the objfile_objstack or NULL if the DIE does not have a
7718 name.
0114d602 7719
94af9270 7720 The output string will be canonicalized (if C++/Java). */
0114d602 7721
94af9270 7722static const char *
15d034d0 7723dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7724{
bb5ed363 7725 struct objfile *objfile = cu->objfile;
900e11f9
JK
7726 struct attribute *attr;
7727 const char *retval, *mangled = NULL, *canon = NULL;
7728 struct cleanup *back_to;
7729 int need_copy = 1;
7730
7731 /* In this case dwarf2_compute_name is just a shortcut not building anything
7732 on its own. */
7733 if (!die_needs_namespace (die, cu))
7734 return dwarf2_compute_name (name, die, cu, 1);
7735
7736 back_to = make_cleanup (null_cleanup, NULL);
7737
7738 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7739 if (!attr)
7740 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7741
7742 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7743 has computed. */
7744 if (attr && DW_STRING (attr))
7745 {
7746 char *demangled;
7747
7748 mangled = DW_STRING (attr);
7749
7750 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7751 type. It is easier for GDB users to search for such functions as
7752 `name(params)' than `long name(params)'. In such case the minimal
7753 symbol names do not match the full symbol names but for template
7754 functions there is never a need to look up their definition from their
7755 declaration so the only disadvantage remains the minimal symbol
7756 variant `long name(params)' does not have the proper inferior type.
7757 */
7758
a766d390
DE
7759 if (cu->language == language_go)
7760 {
7761 /* This is a lie, but we already lie to the caller new_symbol_full.
7762 new_symbol_full assumes we return the mangled name.
7763 This just undoes that lie until things are cleaned up. */
7764 demangled = NULL;
7765 }
7766 else
7767 {
8de20a37
TT
7768 demangled = gdb_demangle (mangled,
7769 (DMGL_PARAMS | DMGL_ANSI
7770 | (cu->language == language_java
7771 ? DMGL_JAVA | DMGL_RET_POSTFIX
7772 : DMGL_RET_DROP)));
a766d390 7773 }
900e11f9
JK
7774 if (demangled)
7775 {
7776 make_cleanup (xfree, demangled);
7777 canon = demangled;
7778 }
7779 else
7780 {
7781 canon = mangled;
7782 need_copy = 0;
7783 }
7784 }
7785
7786 if (canon == NULL || check_physname)
7787 {
7788 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7789
7790 if (canon != NULL && strcmp (physname, canon) != 0)
7791 {
7792 /* It may not mean a bug in GDB. The compiler could also
7793 compute DW_AT_linkage_name incorrectly. But in such case
7794 GDB would need to be bug-to-bug compatible. */
7795
7796 complaint (&symfile_complaints,
7797 _("Computed physname <%s> does not match demangled <%s> "
7798 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7799 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7800
7801 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7802 is available here - over computed PHYSNAME. It is safer
7803 against both buggy GDB and buggy compilers. */
7804
7805 retval = canon;
7806 }
7807 else
7808 {
7809 retval = physname;
7810 need_copy = 0;
7811 }
7812 }
7813 else
7814 retval = canon;
7815
7816 if (need_copy)
10f0c4bb 7817 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7818
7819 do_cleanups (back_to);
7820 return retval;
0114d602
DJ
7821}
7822
27aa8d6a
SW
7823/* Read the import statement specified by the given die and record it. */
7824
7825static void
7826read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7827{
bb5ed363 7828 struct objfile *objfile = cu->objfile;
27aa8d6a 7829 struct attribute *import_attr;
32019081 7830 struct die_info *imported_die, *child_die;
de4affc9 7831 struct dwarf2_cu *imported_cu;
27aa8d6a 7832 const char *imported_name;
794684b6 7833 const char *imported_name_prefix;
13387711
SW
7834 const char *canonical_name;
7835 const char *import_alias;
7836 const char *imported_declaration = NULL;
794684b6 7837 const char *import_prefix;
32019081
JK
7838 VEC (const_char_ptr) *excludes = NULL;
7839 struct cleanup *cleanups;
13387711 7840
27aa8d6a
SW
7841 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7842 if (import_attr == NULL)
7843 {
7844 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7845 dwarf_tag_name (die->tag));
7846 return;
7847 }
7848
de4affc9
CC
7849 imported_cu = cu;
7850 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7851 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7852 if (imported_name == NULL)
7853 {
7854 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7855
7856 The import in the following code:
7857 namespace A
7858 {
7859 typedef int B;
7860 }
7861
7862 int main ()
7863 {
7864 using A::B;
7865 B b;
7866 return b;
7867 }
7868
7869 ...
7870 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7871 <52> DW_AT_decl_file : 1
7872 <53> DW_AT_decl_line : 6
7873 <54> DW_AT_import : <0x75>
7874 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7875 <59> DW_AT_name : B
7876 <5b> DW_AT_decl_file : 1
7877 <5c> DW_AT_decl_line : 2
7878 <5d> DW_AT_type : <0x6e>
7879 ...
7880 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7881 <76> DW_AT_byte_size : 4
7882 <77> DW_AT_encoding : 5 (signed)
7883
7884 imports the wrong die ( 0x75 instead of 0x58 ).
7885 This case will be ignored until the gcc bug is fixed. */
7886 return;
7887 }
7888
82856980
SW
7889 /* Figure out the local name after import. */
7890 import_alias = dwarf2_name (die, cu);
27aa8d6a 7891
794684b6
SW
7892 /* Figure out where the statement is being imported to. */
7893 import_prefix = determine_prefix (die, cu);
7894
7895 /* Figure out what the scope of the imported die is and prepend it
7896 to the name of the imported die. */
de4affc9 7897 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7898
f55ee35c
JK
7899 if (imported_die->tag != DW_TAG_namespace
7900 && imported_die->tag != DW_TAG_module)
794684b6 7901 {
13387711
SW
7902 imported_declaration = imported_name;
7903 canonical_name = imported_name_prefix;
794684b6 7904 }
13387711 7905 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7906 canonical_name = obconcat (&objfile->objfile_obstack,
7907 imported_name_prefix, "::", imported_name,
7908 (char *) NULL);
13387711
SW
7909 else
7910 canonical_name = imported_name;
794684b6 7911
32019081
JK
7912 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7913
7914 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7915 for (child_die = die->child; child_die && child_die->tag;
7916 child_die = sibling_die (child_die))
7917 {
7918 /* DWARF-4: A Fortran use statement with a “rename list” may be
7919 represented by an imported module entry with an import attribute
7920 referring to the module and owned entries corresponding to those
7921 entities that are renamed as part of being imported. */
7922
7923 if (child_die->tag != DW_TAG_imported_declaration)
7924 {
7925 complaint (&symfile_complaints,
7926 _("child DW_TAG_imported_declaration expected "
7927 "- DIE at 0x%x [in module %s]"),
b64f50a1 7928 child_die->offset.sect_off, objfile->name);
32019081
JK
7929 continue;
7930 }
7931
7932 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7933 if (import_attr == NULL)
7934 {
7935 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7936 dwarf_tag_name (child_die->tag));
7937 continue;
7938 }
7939
7940 imported_cu = cu;
7941 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7942 &imported_cu);
7943 imported_name = dwarf2_name (imported_die, imported_cu);
7944 if (imported_name == NULL)
7945 {
7946 complaint (&symfile_complaints,
7947 _("child DW_TAG_imported_declaration has unknown "
7948 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7949 child_die->offset.sect_off, objfile->name);
32019081
JK
7950 continue;
7951 }
7952
7953 VEC_safe_push (const_char_ptr, excludes, imported_name);
7954
7955 process_die (child_die, cu);
7956 }
7957
c0cc3a76
SW
7958 cp_add_using_directive (import_prefix,
7959 canonical_name,
7960 import_alias,
13387711 7961 imported_declaration,
32019081 7962 excludes,
12aaed36 7963 0,
bb5ed363 7964 &objfile->objfile_obstack);
32019081
JK
7965
7966 do_cleanups (cleanups);
27aa8d6a
SW
7967}
7968
f4dc4d17 7969/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7970
cb1df416
DJ
7971static void
7972free_cu_line_header (void *arg)
7973{
7974 struct dwarf2_cu *cu = arg;
7975
7976 free_line_header (cu->line_header);
7977 cu->line_header = NULL;
7978}
7979
1b80a9fa
JK
7980/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7981 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7982 this, it was first present in GCC release 4.3.0. */
7983
7984static int
7985producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7986{
7987 if (!cu->checked_producer)
7988 check_producer (cu);
7989
7990 return cu->producer_is_gcc_lt_4_3;
7991}
7992
9291a0cd
TT
7993static void
7994find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7995 const char **name, const char **comp_dir)
9291a0cd
TT
7996{
7997 struct attribute *attr;
7998
7999 *name = NULL;
8000 *comp_dir = NULL;
8001
8002 /* Find the filename. Do not use dwarf2_name here, since the filename
8003 is not a source language identifier. */
8004 attr = dwarf2_attr (die, DW_AT_name, cu);
8005 if (attr)
8006 {
8007 *name = DW_STRING (attr);
8008 }
8009
8010 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8011 if (attr)
8012 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8013 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8014 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8015 {
15d034d0
TT
8016 char *d = ldirname (*name);
8017
8018 *comp_dir = d;
8019 if (d != NULL)
8020 make_cleanup (xfree, d);
9291a0cd
TT
8021 }
8022 if (*comp_dir != NULL)
8023 {
8024 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8025 directory, get rid of it. */
8026 char *cp = strchr (*comp_dir, ':');
8027
8028 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8029 *comp_dir = cp + 1;
8030 }
8031
8032 if (*name == NULL)
8033 *name = "<unknown>";
8034}
8035
f4dc4d17
DE
8036/* Handle DW_AT_stmt_list for a compilation unit.
8037 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8038 COMP_DIR is the compilation directory.
8039 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8040
8041static void
8042handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 8043 const char *comp_dir)
2ab95328
TT
8044{
8045 struct attribute *attr;
2ab95328 8046
f4dc4d17
DE
8047 gdb_assert (! cu->per_cu->is_debug_types);
8048
2ab95328
TT
8049 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8050 if (attr)
8051 {
8052 unsigned int line_offset = DW_UNSND (attr);
8053 struct line_header *line_header
3019eac3 8054 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8055
8056 if (line_header)
dee91e82
DE
8057 {
8058 cu->line_header = line_header;
8059 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8060 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8061 }
2ab95328
TT
8062 }
8063}
8064
95554aad 8065/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8066
c906108c 8067static void
e7c27a73 8068read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8069{
dee91e82 8070 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8071 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8072 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8073 CORE_ADDR highpc = ((CORE_ADDR) 0);
8074 struct attribute *attr;
15d034d0
TT
8075 const char *name = NULL;
8076 const char *comp_dir = NULL;
c906108c
SS
8077 struct die_info *child_die;
8078 bfd *abfd = objfile->obfd;
e142c38c 8079 CORE_ADDR baseaddr;
6e70227d 8080
e142c38c 8081 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8082
fae299cd 8083 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8084
8085 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8086 from finish_block. */
2acceee2 8087 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8088 lowpc = highpc;
8089 lowpc += baseaddr;
8090 highpc += baseaddr;
8091
9291a0cd 8092 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8093
95554aad 8094 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8095
f4b8a18d
KW
8096 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8097 standardised yet. As a workaround for the language detection we fall
8098 back to the DW_AT_producer string. */
8099 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8100 cu->language = language_opencl;
8101
3019eac3
DE
8102 /* Similar hack for Go. */
8103 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8104 set_cu_language (DW_LANG_Go, cu);
8105
f4dc4d17 8106 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8107
8108 /* Decode line number information if present. We do this before
8109 processing child DIEs, so that the line header table is available
8110 for DW_AT_decl_file. */
f4dc4d17 8111 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8112
8113 /* Process all dies in compilation unit. */
8114 if (die->child != NULL)
8115 {
8116 child_die = die->child;
8117 while (child_die && child_die->tag)
8118 {
8119 process_die (child_die, cu);
8120 child_die = sibling_die (child_die);
8121 }
8122 }
8123
8124 /* Decode macro information, if present. Dwarf 2 macro information
8125 refers to information in the line number info statement program
8126 header, so we can only read it if we've read the header
8127 successfully. */
8128 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8129 if (attr && cu->line_header)
8130 {
8131 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8132 complaint (&symfile_complaints,
8133 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8134
09262596 8135 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8136 }
8137 else
8138 {
8139 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8140 if (attr && cu->line_header)
8141 {
8142 unsigned int macro_offset = DW_UNSND (attr);
8143
09262596 8144 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8145 }
8146 }
8147
8148 do_cleanups (back_to);
8149}
8150
f4dc4d17
DE
8151/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8152 Create the set of symtabs used by this TU, or if this TU is sharing
8153 symtabs with another TU and the symtabs have already been created
8154 then restore those symtabs in the line header.
8155 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8156
8157static void
f4dc4d17 8158setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8159{
f4dc4d17
DE
8160 struct objfile *objfile = dwarf2_per_objfile->objfile;
8161 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8162 struct type_unit_group *tu_group;
8163 int first_time;
8164 struct line_header *lh;
3019eac3 8165 struct attribute *attr;
f4dc4d17 8166 unsigned int i, line_offset;
0186c6a7 8167 struct signatured_type *sig_type;
3019eac3 8168
f4dc4d17 8169 gdb_assert (per_cu->is_debug_types);
0186c6a7 8170 sig_type = (struct signatured_type *) per_cu;
3019eac3 8171
f4dc4d17 8172 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8173
f4dc4d17
DE
8174 /* If we're using .gdb_index (includes -readnow) then
8175 per_cu->s.type_unit_group may not have been set up yet. */
0186c6a7
DE
8176 if (sig_type->type_unit_group == NULL)
8177 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8178 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8179
8180 /* If we've already processed this stmt_list there's no real need to
8181 do it again, we could fake it and just recreate the part we need
8182 (file name,index -> symtab mapping). If data shows this optimization
8183 is useful we can do it then. */
8184 first_time = tu_group->primary_symtab == NULL;
8185
8186 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8187 debug info. */
8188 lh = NULL;
8189 if (attr != NULL)
3019eac3 8190 {
f4dc4d17
DE
8191 line_offset = DW_UNSND (attr);
8192 lh = dwarf_decode_line_header (line_offset, cu);
8193 }
8194 if (lh == NULL)
8195 {
8196 if (first_time)
8197 dwarf2_start_symtab (cu, "", NULL, 0);
8198 else
8199 {
8200 gdb_assert (tu_group->symtabs == NULL);
8201 restart_symtab (0);
8202 }
8203 /* Note: The primary symtab will get allocated at the end. */
8204 return;
3019eac3
DE
8205 }
8206
f4dc4d17
DE
8207 cu->line_header = lh;
8208 make_cleanup (free_cu_line_header, cu);
3019eac3 8209
f4dc4d17
DE
8210 if (first_time)
8211 {
8212 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8213
f4dc4d17
DE
8214 tu_group->num_symtabs = lh->num_file_names;
8215 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8216
f4dc4d17
DE
8217 for (i = 0; i < lh->num_file_names; ++i)
8218 {
d521ce57 8219 const char *dir = NULL;
f4dc4d17 8220 struct file_entry *fe = &lh->file_names[i];
3019eac3 8221
f4dc4d17
DE
8222 if (fe->dir_index)
8223 dir = lh->include_dirs[fe->dir_index - 1];
8224 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8225
f4dc4d17
DE
8226 /* Note: We don't have to watch for the main subfile here, type units
8227 don't have DW_AT_name. */
3019eac3 8228
f4dc4d17
DE
8229 if (current_subfile->symtab == NULL)
8230 {
8231 /* NOTE: start_subfile will recognize when it's been passed
8232 a file it has already seen. So we can't assume there's a
8233 simple mapping from lh->file_names to subfiles,
8234 lh->file_names may contain dups. */
8235 current_subfile->symtab = allocate_symtab (current_subfile->name,
8236 objfile);
8237 }
8238
8239 fe->symtab = current_subfile->symtab;
8240 tu_group->symtabs[i] = fe->symtab;
8241 }
8242 }
8243 else
3019eac3 8244 {
f4dc4d17
DE
8245 restart_symtab (0);
8246
8247 for (i = 0; i < lh->num_file_names; ++i)
8248 {
8249 struct file_entry *fe = &lh->file_names[i];
8250
8251 fe->symtab = tu_group->symtabs[i];
8252 }
3019eac3
DE
8253 }
8254
f4dc4d17
DE
8255 /* The main symtab is allocated last. Type units don't have DW_AT_name
8256 so they don't have a "real" (so to speak) symtab anyway.
8257 There is later code that will assign the main symtab to all symbols
8258 that don't have one. We need to handle the case of a symbol with a
8259 missing symtab (DW_AT_decl_file) anyway. */
8260}
3019eac3 8261
f4dc4d17
DE
8262/* Process DW_TAG_type_unit.
8263 For TUs we want to skip the first top level sibling if it's not the
8264 actual type being defined by this TU. In this case the first top
8265 level sibling is there to provide context only. */
3019eac3 8266
f4dc4d17
DE
8267static void
8268read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8269{
8270 struct die_info *child_die;
3019eac3 8271
f4dc4d17
DE
8272 prepare_one_comp_unit (cu, die, language_minimal);
8273
8274 /* Initialize (or reinitialize) the machinery for building symtabs.
8275 We do this before processing child DIEs, so that the line header table
8276 is available for DW_AT_decl_file. */
8277 setup_type_unit_groups (die, cu);
8278
8279 if (die->child != NULL)
8280 {
8281 child_die = die->child;
8282 while (child_die && child_die->tag)
8283 {
8284 process_die (child_die, cu);
8285 child_die = sibling_die (child_die);
8286 }
8287 }
3019eac3
DE
8288}
8289\f
80626a55
DE
8290/* DWO/DWP files.
8291
8292 http://gcc.gnu.org/wiki/DebugFission
8293 http://gcc.gnu.org/wiki/DebugFissionDWP
8294
8295 To simplify handling of both DWO files ("object" files with the DWARF info)
8296 and DWP files (a file with the DWOs packaged up into one file), we treat
8297 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8298
8299static hashval_t
8300hash_dwo_file (const void *item)
8301{
8302 const struct dwo_file *dwo_file = item;
8303
0ac5b59e
DE
8304 return (htab_hash_string (dwo_file->dwo_name)
8305 + htab_hash_string (dwo_file->comp_dir));
3019eac3
DE
8306}
8307
8308static int
8309eq_dwo_file (const void *item_lhs, const void *item_rhs)
8310{
8311 const struct dwo_file *lhs = item_lhs;
8312 const struct dwo_file *rhs = item_rhs;
8313
0ac5b59e
DE
8314 return (strcmp (lhs->dwo_name, rhs->dwo_name) == 0
8315 && strcmp (lhs->comp_dir, rhs->comp_dir) == 0);
3019eac3
DE
8316}
8317
8318/* Allocate a hash table for DWO files. */
8319
8320static htab_t
8321allocate_dwo_file_hash_table (void)
8322{
8323 struct objfile *objfile = dwarf2_per_objfile->objfile;
8324
8325 return htab_create_alloc_ex (41,
8326 hash_dwo_file,
8327 eq_dwo_file,
8328 NULL,
8329 &objfile->objfile_obstack,
8330 hashtab_obstack_allocate,
8331 dummy_obstack_deallocate);
8332}
8333
80626a55
DE
8334/* Lookup DWO file DWO_NAME. */
8335
8336static void **
0ac5b59e 8337lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8338{
8339 struct dwo_file find_entry;
8340 void **slot;
8341
8342 if (dwarf2_per_objfile->dwo_files == NULL)
8343 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8344
8345 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8346 find_entry.dwo_name = dwo_name;
8347 find_entry.comp_dir = comp_dir;
80626a55
DE
8348 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8349
8350 return slot;
8351}
8352
3019eac3
DE
8353static hashval_t
8354hash_dwo_unit (const void *item)
8355{
8356 const struct dwo_unit *dwo_unit = item;
8357
8358 /* This drops the top 32 bits of the id, but is ok for a hash. */
8359 return dwo_unit->signature;
8360}
8361
8362static int
8363eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8364{
8365 const struct dwo_unit *lhs = item_lhs;
8366 const struct dwo_unit *rhs = item_rhs;
8367
8368 /* The signature is assumed to be unique within the DWO file.
8369 So while object file CU dwo_id's always have the value zero,
8370 that's OK, assuming each object file DWO file has only one CU,
8371 and that's the rule for now. */
8372 return lhs->signature == rhs->signature;
8373}
8374
8375/* Allocate a hash table for DWO CUs,TUs.
8376 There is one of these tables for each of CUs,TUs for each DWO file. */
8377
8378static htab_t
8379allocate_dwo_unit_table (struct objfile *objfile)
8380{
8381 /* Start out with a pretty small number.
8382 Generally DWO files contain only one CU and maybe some TUs. */
8383 return htab_create_alloc_ex (3,
8384 hash_dwo_unit,
8385 eq_dwo_unit,
8386 NULL,
8387 &objfile->objfile_obstack,
8388 hashtab_obstack_allocate,
8389 dummy_obstack_deallocate);
8390}
8391
80626a55 8392/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8393
19c3d4c9 8394struct create_dwo_cu_data
3019eac3
DE
8395{
8396 struct dwo_file *dwo_file;
19c3d4c9 8397 struct dwo_unit dwo_unit;
3019eac3
DE
8398};
8399
19c3d4c9 8400/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8401
8402static void
19c3d4c9
DE
8403create_dwo_cu_reader (const struct die_reader_specs *reader,
8404 const gdb_byte *info_ptr,
8405 struct die_info *comp_unit_die,
8406 int has_children,
8407 void *datap)
3019eac3
DE
8408{
8409 struct dwarf2_cu *cu = reader->cu;
8410 struct objfile *objfile = dwarf2_per_objfile->objfile;
8411 sect_offset offset = cu->per_cu->offset;
8a0459fd 8412 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8413 struct create_dwo_cu_data *data = datap;
3019eac3 8414 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8415 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8416 struct attribute *attr;
3019eac3
DE
8417
8418 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8419 if (attr == NULL)
8420 {
19c3d4c9
DE
8421 complaint (&symfile_complaints,
8422 _("Dwarf Error: debug entry at offset 0x%x is missing"
8423 " its dwo_id [in module %s]"),
8424 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8425 return;
8426 }
8427
3019eac3
DE
8428 dwo_unit->dwo_file = dwo_file;
8429 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8430 dwo_unit->section = section;
3019eac3
DE
8431 dwo_unit->offset = offset;
8432 dwo_unit->length = cu->per_cu->length;
8433
09406207 8434 if (dwarf2_read_debug)
4031ecc5
DE
8435 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8436 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8437}
8438
19c3d4c9
DE
8439/* Create the dwo_unit for the lone CU in DWO_FILE.
8440 Note: This function processes DWO files only, not DWP files. */
3019eac3 8441
19c3d4c9
DE
8442static struct dwo_unit *
8443create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8444{
8445 struct objfile *objfile = dwarf2_per_objfile->objfile;
8446 struct dwarf2_section_info *section = &dwo_file->sections.info;
8447 bfd *abfd;
8448 htab_t cu_htab;
d521ce57 8449 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8450 struct create_dwo_cu_data create_dwo_cu_data;
8451 struct dwo_unit *dwo_unit;
3019eac3
DE
8452
8453 dwarf2_read_section (objfile, section);
8454 info_ptr = section->buffer;
8455
8456 if (info_ptr == NULL)
8457 return NULL;
8458
8459 /* We can't set abfd until now because the section may be empty or
8460 not present, in which case section->asection will be NULL. */
8461 abfd = section->asection->owner;
8462
09406207 8463 if (dwarf2_read_debug)
19c3d4c9
DE
8464 {
8465 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8466 bfd_section_name (abfd, section->asection),
8467 bfd_get_filename (abfd));
8468 }
3019eac3 8469
19c3d4c9
DE
8470 create_dwo_cu_data.dwo_file = dwo_file;
8471 dwo_unit = NULL;
3019eac3
DE
8472
8473 end_ptr = info_ptr + section->size;
8474 while (info_ptr < end_ptr)
8475 {
8476 struct dwarf2_per_cu_data per_cu;
8477
19c3d4c9
DE
8478 memset (&create_dwo_cu_data.dwo_unit, 0,
8479 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8480 memset (&per_cu, 0, sizeof (per_cu));
8481 per_cu.objfile = objfile;
8482 per_cu.is_debug_types = 0;
8483 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8484 per_cu.section = section;
3019eac3
DE
8485
8486 init_cutu_and_read_dies_no_follow (&per_cu,
8487 &dwo_file->sections.abbrev,
8488 dwo_file,
19c3d4c9
DE
8489 create_dwo_cu_reader,
8490 &create_dwo_cu_data);
8491
8492 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8493 {
8494 /* If we've already found one, complain. We only support one
8495 because having more than one requires hacking the dwo_name of
8496 each to match, which is highly unlikely to happen. */
8497 if (dwo_unit != NULL)
8498 {
8499 complaint (&symfile_complaints,
8500 _("Multiple CUs in DWO file %s [in module %s]"),
8501 dwo_file->dwo_name, objfile->name);
8502 break;
8503 }
8504
8505 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8506 *dwo_unit = create_dwo_cu_data.dwo_unit;
8507 }
3019eac3
DE
8508
8509 info_ptr += per_cu.length;
8510 }
8511
19c3d4c9 8512 return dwo_unit;
3019eac3
DE
8513}
8514
80626a55
DE
8515/* DWP file .debug_{cu,tu}_index section format:
8516 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8517
8518 Both index sections have the same format, and serve to map a 64-bit
8519 signature to a set of section numbers. Each section begins with a header,
8520 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8521 indexes, and a pool of 32-bit section numbers. The index sections will be
8522 aligned at 8-byte boundaries in the file.
8523
8524 The index section header contains two unsigned 32-bit values (using the
8525 byte order of the application binary):
8526
8527 N, the number of compilation units or type units in the index
8528 M, the number of slots in the hash table
8529
8530 (We assume that N and M will not exceed 2^32 - 1.)
8531
8532 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8533
8534 The hash table begins at offset 8 in the section, and consists of an array
8535 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8536 order of the application binary). Unused slots in the hash table are 0.
8537 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8538
8539 The parallel table begins immediately after the hash table
8540 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8541 array of 32-bit indexes (using the byte order of the application binary),
8542 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8543 table contains a 32-bit index into the pool of section numbers. For unused
8544 hash table slots, the corresponding entry in the parallel table will be 0.
8545
8546 Given a 64-bit compilation unit signature or a type signature S, an entry
8547 in the hash table is located as follows:
8548
8549 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8550 the low-order k bits all set to 1.
8551
8552 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8553
8554 3) If the hash table entry at index H matches the signature, use that
8555 entry. If the hash table entry at index H is unused (all zeroes),
8556 terminate the search: the signature is not present in the table.
8557
8558 4) Let H = (H + H') modulo M. Repeat at Step 3.
8559
8560 Because M > N and H' and M are relatively prime, the search is guaranteed
8561 to stop at an unused slot or find the match.
8562
8563 The pool of section numbers begins immediately following the hash table
8564 (at offset 8 + 12 * M from the beginning of the section). The pool of
8565 section numbers consists of an array of 32-bit words (using the byte order
8566 of the application binary). Each item in the array is indexed starting
8567 from 0. The hash table entry provides the index of the first section
8568 number in the set. Additional section numbers in the set follow, and the
8569 set is terminated by a 0 entry (section number 0 is not used in ELF).
8570
8571 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8572 section must be the first entry in the set, and the .debug_abbrev.dwo must
8573 be the second entry. Other members of the set may follow in any order. */
8574
8575/* Create a hash table to map DWO IDs to their CU/TU entry in
8576 .debug_{info,types}.dwo in DWP_FILE.
8577 Returns NULL if there isn't one.
8578 Note: This function processes DWP files only, not DWO files. */
8579
8580static struct dwp_hash_table *
8581create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8582{
8583 struct objfile *objfile = dwarf2_per_objfile->objfile;
8584 bfd *dbfd = dwp_file->dbfd;
d521ce57 8585 const char *index_ptr, *index_end;
80626a55
DE
8586 struct dwarf2_section_info *index;
8587 uint32_t version, nr_units, nr_slots;
8588 struct dwp_hash_table *htab;
8589
8590 if (is_debug_types)
8591 index = &dwp_file->sections.tu_index;
8592 else
8593 index = &dwp_file->sections.cu_index;
8594
8595 if (dwarf2_section_empty_p (index))
8596 return NULL;
8597 dwarf2_read_section (objfile, index);
8598
8599 index_ptr = index->buffer;
8600 index_end = index_ptr + index->size;
8601
8602 version = read_4_bytes (dbfd, index_ptr);
8603 index_ptr += 8; /* Skip the unused word. */
8604 nr_units = read_4_bytes (dbfd, index_ptr);
8605 index_ptr += 4;
8606 nr_slots = read_4_bytes (dbfd, index_ptr);
8607 index_ptr += 4;
8608
8609 if (version != 1)
8610 {
8611 error (_("Dwarf Error: unsupported DWP file version (%u)"
8612 " [in module %s]"),
8613 version, dwp_file->name);
8614 }
8615 if (nr_slots != (nr_slots & -nr_slots))
8616 {
8617 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8618 " is not power of 2 [in module %s]"),
8619 nr_slots, dwp_file->name);
8620 }
8621
8622 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8623 htab->nr_units = nr_units;
8624 htab->nr_slots = nr_slots;
8625 htab->hash_table = index_ptr;
8626 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8627 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8628
8629 return htab;
8630}
8631
8632/* Update SECTIONS with the data from SECTP.
8633
8634 This function is like the other "locate" section routines that are
8635 passed to bfd_map_over_sections, but in this context the sections to
8636 read comes from the DWP hash table, not the full ELF section table.
8637
8638 The result is non-zero for success, or zero if an error was found. */
8639
8640static int
8641locate_virtual_dwo_sections (asection *sectp,
8642 struct virtual_dwo_sections *sections)
8643{
8644 const struct dwop_section_names *names = &dwop_section_names;
8645
8646 if (section_is_p (sectp->name, &names->abbrev_dwo))
8647 {
8648 /* There can be only one. */
8649 if (sections->abbrev.asection != NULL)
8650 return 0;
8651 sections->abbrev.asection = sectp;
8652 sections->abbrev.size = bfd_get_section_size (sectp);
8653 }
8654 else if (section_is_p (sectp->name, &names->info_dwo)
8655 || section_is_p (sectp->name, &names->types_dwo))
8656 {
8657 /* There can be only one. */
8658 if (sections->info_or_types.asection != NULL)
8659 return 0;
8660 sections->info_or_types.asection = sectp;
8661 sections->info_or_types.size = bfd_get_section_size (sectp);
8662 }
8663 else if (section_is_p (sectp->name, &names->line_dwo))
8664 {
8665 /* There can be only one. */
8666 if (sections->line.asection != NULL)
8667 return 0;
8668 sections->line.asection = sectp;
8669 sections->line.size = bfd_get_section_size (sectp);
8670 }
8671 else if (section_is_p (sectp->name, &names->loc_dwo))
8672 {
8673 /* There can be only one. */
8674 if (sections->loc.asection != NULL)
8675 return 0;
8676 sections->loc.asection = sectp;
8677 sections->loc.size = bfd_get_section_size (sectp);
8678 }
8679 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8680 {
8681 /* There can be only one. */
8682 if (sections->macinfo.asection != NULL)
8683 return 0;
8684 sections->macinfo.asection = sectp;
8685 sections->macinfo.size = bfd_get_section_size (sectp);
8686 }
8687 else if (section_is_p (sectp->name, &names->macro_dwo))
8688 {
8689 /* There can be only one. */
8690 if (sections->macro.asection != NULL)
8691 return 0;
8692 sections->macro.asection = sectp;
8693 sections->macro.size = bfd_get_section_size (sectp);
8694 }
8695 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8696 {
8697 /* There can be only one. */
8698 if (sections->str_offsets.asection != NULL)
8699 return 0;
8700 sections->str_offsets.asection = sectp;
8701 sections->str_offsets.size = bfd_get_section_size (sectp);
8702 }
8703 else
8704 {
8705 /* No other kind of section is valid. */
8706 return 0;
8707 }
8708
8709 return 1;
8710}
8711
8712/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8713 HTAB is the hash table from the DWP file.
0ac5b59e
DE
8714 SECTION_INDEX is the index of the DWO in HTAB.
8715 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
8716
8717static struct dwo_unit *
8718create_dwo_in_dwp (struct dwp_file *dwp_file,
8719 const struct dwp_hash_table *htab,
8720 uint32_t section_index,
0ac5b59e 8721 const char *comp_dir,
80626a55
DE
8722 ULONGEST signature, int is_debug_types)
8723{
8724 struct objfile *objfile = dwarf2_per_objfile->objfile;
8725 bfd *dbfd = dwp_file->dbfd;
8726 const char *kind = is_debug_types ? "TU" : "CU";
8727 struct dwo_file *dwo_file;
8728 struct dwo_unit *dwo_unit;
8729 struct virtual_dwo_sections sections;
8730 void **dwo_file_slot;
8731 char *virtual_dwo_name;
8732 struct dwarf2_section_info *cutu;
8733 struct cleanup *cleanups;
8734 int i;
8735
8736 if (dwarf2_read_debug)
8737 {
4031ecc5 8738 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/%s in DWP file: %s\n",
80626a55 8739 kind,
4031ecc5 8740 section_index, hex_string (signature),
80626a55
DE
8741 dwp_file->name);
8742 }
8743
8744 /* Fetch the sections of this DWO.
8745 Put a limit on the number of sections we look for so that bad data
8746 doesn't cause us to loop forever. */
8747
8748#define MAX_NR_DWO_SECTIONS \
8749 (1 /* .debug_info or .debug_types */ \
8750 + 1 /* .debug_abbrev */ \
8751 + 1 /* .debug_line */ \
8752 + 1 /* .debug_loc */ \
8753 + 1 /* .debug_str_offsets */ \
8754 + 1 /* .debug_macro */ \
8755 + 1 /* .debug_macinfo */ \
8756 + 1 /* trailing zero */)
8757
8758 memset (&sections, 0, sizeof (sections));
8759 cleanups = make_cleanup (null_cleanup, 0);
8760
8761 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8762 {
8763 asection *sectp;
8764 uint32_t section_nr =
8765 read_4_bytes (dbfd,
8766 htab->section_pool
8767 + (section_index + i) * sizeof (uint32_t));
8768
8769 if (section_nr == 0)
8770 break;
8771 if (section_nr >= dwp_file->num_sections)
8772 {
8773 error (_("Dwarf Error: bad DWP hash table, section number too large"
8774 " [in module %s]"),
8775 dwp_file->name);
8776 }
8777
8778 sectp = dwp_file->elf_sections[section_nr];
8779 if (! locate_virtual_dwo_sections (sectp, &sections))
8780 {
8781 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8782 " [in module %s]"),
8783 dwp_file->name);
8784 }
8785 }
8786
8787 if (i < 2
8788 || sections.info_or_types.asection == NULL
8789 || sections.abbrev.asection == NULL)
8790 {
8791 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8792 " [in module %s]"),
8793 dwp_file->name);
8794 }
8795 if (i == MAX_NR_DWO_SECTIONS)
8796 {
8797 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8798 " [in module %s]"),
8799 dwp_file->name);
8800 }
8801
8802 /* It's easier for the rest of the code if we fake a struct dwo_file and
8803 have dwo_unit "live" in that. At least for now.
8804
8805 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8806 However, for each CU + set of TUs that came from the same original DWO
8807 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8808 (fewer struct dwo_file objects to allocated). Remember that for really
8809 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8810
2792b94d
PM
8811 virtual_dwo_name =
8812 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8813 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8814 sections.line.asection ? sections.line.asection->id : 0,
8815 sections.loc.asection ? sections.loc.asection->id : 0,
8816 (sections.str_offsets.asection
8817 ? sections.str_offsets.asection->id
8818 : 0));
80626a55
DE
8819 make_cleanup (xfree, virtual_dwo_name);
8820 /* Can we use an existing virtual DWO file? */
0ac5b59e 8821 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
8822 /* Create one if necessary. */
8823 if (*dwo_file_slot == NULL)
8824 {
8825 if (dwarf2_read_debug)
8826 {
8827 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8828 virtual_dwo_name);
8829 }
8830 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
8831 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
8832 virtual_dwo_name,
8833 strlen (virtual_dwo_name));
8834 dwo_file->comp_dir = comp_dir;
80626a55
DE
8835 dwo_file->sections.abbrev = sections.abbrev;
8836 dwo_file->sections.line = sections.line;
8837 dwo_file->sections.loc = sections.loc;
8838 dwo_file->sections.macinfo = sections.macinfo;
8839 dwo_file->sections.macro = sections.macro;
8840 dwo_file->sections.str_offsets = sections.str_offsets;
8841 /* The "str" section is global to the entire DWP file. */
8842 dwo_file->sections.str = dwp_file->sections.str;
8843 /* The info or types section is assigned later to dwo_unit,
8844 there's no need to record it in dwo_file.
8845 Also, we can't simply record type sections in dwo_file because
8846 we record a pointer into the vector in dwo_unit. As we collect more
8847 types we'll grow the vector and eventually have to reallocate space
8848 for it, invalidating all the pointers into the current copy. */
8849 *dwo_file_slot = dwo_file;
8850 }
8851 else
8852 {
8853 if (dwarf2_read_debug)
8854 {
8855 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8856 virtual_dwo_name);
8857 }
8858 dwo_file = *dwo_file_slot;
8859 }
8860 do_cleanups (cleanups);
8861
8862 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8863 dwo_unit->dwo_file = dwo_file;
8864 dwo_unit->signature = signature;
8a0459fd
DE
8865 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8866 sizeof (struct dwarf2_section_info));
8867 *dwo_unit->section = sections.info_or_types;
80626a55
DE
8868 /* offset, length, type_offset_in_tu are set later. */
8869
8870 return dwo_unit;
8871}
8872
8873/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8874
8875static struct dwo_unit *
8876lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8877 const struct dwp_hash_table *htab,
0ac5b59e 8878 const char *comp_dir,
80626a55
DE
8879 ULONGEST signature, int is_debug_types)
8880{
8881 bfd *dbfd = dwp_file->dbfd;
8882 uint32_t mask = htab->nr_slots - 1;
8883 uint32_t hash = signature & mask;
8884 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8885 unsigned int i;
8886 void **slot;
8887 struct dwo_unit find_dwo_cu, *dwo_cu;
8888
8889 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8890 find_dwo_cu.signature = signature;
8891 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8892
8893 if (*slot != NULL)
8894 return *slot;
8895
8896 /* Use a for loop so that we don't loop forever on bad debug info. */
8897 for (i = 0; i < htab->nr_slots; ++i)
8898 {
8899 ULONGEST signature_in_table;
8900
8901 signature_in_table =
8902 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8903 if (signature_in_table == signature)
8904 {
8905 uint32_t section_index =
8906 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8907
8908 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 8909 comp_dir, signature, is_debug_types);
80626a55
DE
8910 return *slot;
8911 }
8912 if (signature_in_table == 0)
8913 return NULL;
8914 hash = (hash + hash2) & mask;
8915 }
8916
8917 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8918 " [in module %s]"),
8919 dwp_file->name);
8920}
8921
ab5088bf 8922/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
8923 Open the file specified by FILE_NAME and hand it off to BFD for
8924 preliminary analysis. Return a newly initialized bfd *, which
8925 includes a canonicalized copy of FILE_NAME.
80626a55 8926 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8927 In case of trouble, return NULL.
8928 NOTE: This function is derived from symfile_bfd_open. */
8929
8930static bfd *
80626a55 8931try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8932{
8933 bfd *sym_bfd;
80626a55 8934 int desc, flags;
3019eac3 8935 char *absolute_name;
3019eac3 8936
80626a55
DE
8937 flags = OPF_TRY_CWD_FIRST;
8938 if (is_dwp)
8939 flags |= OPF_SEARCH_IN_PATH;
8940 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8941 O_RDONLY | O_BINARY, &absolute_name);
8942 if (desc < 0)
8943 return NULL;
8944
bb397797 8945 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8946 if (!sym_bfd)
8947 {
3019eac3
DE
8948 xfree (absolute_name);
8949 return NULL;
8950 }
a4453b7e 8951 xfree (absolute_name);
3019eac3
DE
8952 bfd_set_cacheable (sym_bfd, 1);
8953
8954 if (!bfd_check_format (sym_bfd, bfd_object))
8955 {
cbb099e8 8956 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8957 return NULL;
8958 }
8959
3019eac3
DE
8960 return sym_bfd;
8961}
8962
ab5088bf 8963/* Try to open DWO file FILE_NAME.
3019eac3
DE
8964 COMP_DIR is the DW_AT_comp_dir attribute.
8965 The result is the bfd handle of the file.
8966 If there is a problem finding or opening the file, return NULL.
8967 Upon success, the canonicalized path of the file is stored in the bfd,
8968 same as symfile_bfd_open. */
8969
8970static bfd *
ab5088bf 8971open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
8972{
8973 bfd *abfd;
3019eac3 8974
80626a55 8975 if (IS_ABSOLUTE_PATH (file_name))
ab5088bf 8976 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
8977
8978 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8979
8980 if (comp_dir != NULL)
8981 {
80626a55 8982 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8983
8984 /* NOTE: If comp_dir is a relative path, this will also try the
8985 search path, which seems useful. */
ab5088bf 8986 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
3019eac3
DE
8987 xfree (path_to_try);
8988 if (abfd != NULL)
8989 return abfd;
8990 }
8991
8992 /* That didn't work, try debug-file-directory, which, despite its name,
8993 is a list of paths. */
8994
8995 if (*debug_file_directory == '\0')
8996 return NULL;
8997
ab5088bf 8998 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
3019eac3
DE
8999}
9000
80626a55
DE
9001/* This function is mapped across the sections and remembers the offset and
9002 size of each of the DWO debugging sections we are interested in. */
9003
9004static void
9005dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9006{
9007 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9008 const struct dwop_section_names *names = &dwop_section_names;
9009
9010 if (section_is_p (sectp->name, &names->abbrev_dwo))
9011 {
9012 dwo_sections->abbrev.asection = sectp;
9013 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9014 }
9015 else if (section_is_p (sectp->name, &names->info_dwo))
9016 {
9017 dwo_sections->info.asection = sectp;
9018 dwo_sections->info.size = bfd_get_section_size (sectp);
9019 }
9020 else if (section_is_p (sectp->name, &names->line_dwo))
9021 {
9022 dwo_sections->line.asection = sectp;
9023 dwo_sections->line.size = bfd_get_section_size (sectp);
9024 }
9025 else if (section_is_p (sectp->name, &names->loc_dwo))
9026 {
9027 dwo_sections->loc.asection = sectp;
9028 dwo_sections->loc.size = bfd_get_section_size (sectp);
9029 }
9030 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9031 {
9032 dwo_sections->macinfo.asection = sectp;
9033 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9034 }
9035 else if (section_is_p (sectp->name, &names->macro_dwo))
9036 {
9037 dwo_sections->macro.asection = sectp;
9038 dwo_sections->macro.size = bfd_get_section_size (sectp);
9039 }
9040 else if (section_is_p (sectp->name, &names->str_dwo))
9041 {
9042 dwo_sections->str.asection = sectp;
9043 dwo_sections->str.size = bfd_get_section_size (sectp);
9044 }
9045 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9046 {
9047 dwo_sections->str_offsets.asection = sectp;
9048 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9049 }
9050 else if (section_is_p (sectp->name, &names->types_dwo))
9051 {
9052 struct dwarf2_section_info type_section;
9053
9054 memset (&type_section, 0, sizeof (type_section));
9055 type_section.asection = sectp;
9056 type_section.size = bfd_get_section_size (sectp);
9057 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9058 &type_section);
9059 }
9060}
9061
ab5088bf 9062/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9063 by PER_CU. This is for the non-DWP case.
80626a55 9064 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9065
9066static struct dwo_file *
0ac5b59e
DE
9067open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9068 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9069{
9070 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9071 struct dwo_file *dwo_file;
9072 bfd *dbfd;
3019eac3
DE
9073 struct cleanup *cleanups;
9074
ab5088bf 9075 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9076 if (dbfd == NULL)
9077 {
9078 if (dwarf2_read_debug)
9079 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9080 return NULL;
9081 }
9082 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9083 dwo_file->dwo_name = dwo_name;
9084 dwo_file->comp_dir = comp_dir;
80626a55 9085 dwo_file->dbfd = dbfd;
3019eac3
DE
9086
9087 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9088
80626a55 9089 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9090
19c3d4c9 9091 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9092
9093 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9094 dwo_file->sections.types);
9095
9096 discard_cleanups (cleanups);
9097
80626a55
DE
9098 if (dwarf2_read_debug)
9099 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9100
3019eac3
DE
9101 return dwo_file;
9102}
9103
80626a55
DE
9104/* This function is mapped across the sections and remembers the offset and
9105 size of each of the DWP debugging sections we are interested in. */
3019eac3 9106
80626a55
DE
9107static void
9108dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9109{
80626a55
DE
9110 struct dwp_file *dwp_file = dwp_file_ptr;
9111 const struct dwop_section_names *names = &dwop_section_names;
9112 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9113
80626a55
DE
9114 /* Record the ELF section number for later lookup: this is what the
9115 .debug_cu_index,.debug_tu_index tables use. */
9116 gdb_assert (elf_section_nr < dwp_file->num_sections);
9117 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9118
80626a55
DE
9119 /* Look for specific sections that we need. */
9120 if (section_is_p (sectp->name, &names->str_dwo))
9121 {
9122 dwp_file->sections.str.asection = sectp;
9123 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9124 }
9125 else if (section_is_p (sectp->name, &names->cu_index))
9126 {
9127 dwp_file->sections.cu_index.asection = sectp;
9128 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9129 }
9130 else if (section_is_p (sectp->name, &names->tu_index))
9131 {
9132 dwp_file->sections.tu_index.asection = sectp;
9133 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9134 }
9135}
3019eac3 9136
80626a55 9137/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9138
80626a55
DE
9139static hashval_t
9140hash_dwp_loaded_cutus (const void *item)
9141{
9142 const struct dwo_unit *dwo_unit = item;
3019eac3 9143
80626a55
DE
9144 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9145 return dwo_unit->signature;
3019eac3
DE
9146}
9147
80626a55 9148/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9149
80626a55
DE
9150static int
9151eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9152{
80626a55
DE
9153 const struct dwo_unit *dua = a;
9154 const struct dwo_unit *dub = b;
3019eac3 9155
80626a55
DE
9156 return dua->signature == dub->signature;
9157}
3019eac3 9158
80626a55 9159/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9160
80626a55
DE
9161static htab_t
9162allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9163{
9164 return htab_create_alloc_ex (3,
9165 hash_dwp_loaded_cutus,
9166 eq_dwp_loaded_cutus,
9167 NULL,
9168 &objfile->objfile_obstack,
9169 hashtab_obstack_allocate,
9170 dummy_obstack_deallocate);
9171}
3019eac3 9172
ab5088bf
DE
9173/* Try to open DWP file FILE_NAME.
9174 The result is the bfd handle of the file.
9175 If there is a problem finding or opening the file, return NULL.
9176 Upon success, the canonicalized path of the file is stored in the bfd,
9177 same as symfile_bfd_open. */
9178
9179static bfd *
9180open_dwp_file (const char *file_name)
9181{
9182 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9183}
9184
80626a55
DE
9185/* Initialize the use of the DWP file for the current objfile.
9186 By convention the name of the DWP file is ${objfile}.dwp.
9187 The result is NULL if it can't be found. */
a766d390 9188
80626a55 9189static struct dwp_file *
ab5088bf 9190open_and_init_dwp_file (void)
80626a55
DE
9191{
9192 struct objfile *objfile = dwarf2_per_objfile->objfile;
9193 struct dwp_file *dwp_file;
9194 char *dwp_name;
9195 bfd *dbfd;
9196 struct cleanup *cleanups;
9197
2792b94d 9198 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9199 cleanups = make_cleanup (xfree, dwp_name);
9200
ab5088bf 9201 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9202 if (dbfd == NULL)
9203 {
9204 if (dwarf2_read_debug)
9205 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9206 do_cleanups (cleanups);
9207 return NULL;
3019eac3 9208 }
80626a55
DE
9209 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9210 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9211 dwp_name, strlen (dwp_name));
9212 dwp_file->dbfd = dbfd;
9213 do_cleanups (cleanups);
c906108c 9214
80626a55
DE
9215 /* +1: section 0 is unused */
9216 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9217 dwp_file->elf_sections =
9218 OBSTACK_CALLOC (&objfile->objfile_obstack,
9219 dwp_file->num_sections, asection *);
9220
9221 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9222
9223 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9224
9225 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9226
9227 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9228
80626a55
DE
9229 if (dwarf2_read_debug)
9230 {
9231 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9232 fprintf_unfiltered (gdb_stdlog,
9233 " %u CUs, %u TUs\n",
9234 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9235 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9236 }
9237
9238 return dwp_file;
3019eac3 9239}
c906108c 9240
ab5088bf
DE
9241/* Wrapper around open_and_init_dwp_file, only open it once. */
9242
9243static struct dwp_file *
9244get_dwp_file (void)
9245{
9246 if (! dwarf2_per_objfile->dwp_checked)
9247 {
9248 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9249 dwarf2_per_objfile->dwp_checked = 1;
9250 }
9251 return dwarf2_per_objfile->dwp_file;
9252}
9253
80626a55
DE
9254/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9255 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9256 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9257 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9258 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9259
9260 This is called, for example, when wanting to read a variable with a
9261 complex location. Therefore we don't want to do file i/o for every call.
9262 Therefore we don't want to look for a DWO file on every call.
9263 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9264 then we check if we've already seen DWO_NAME, and only THEN do we check
9265 for a DWO file.
9266
1c658ad5 9267 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9268 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9269
3019eac3 9270static struct dwo_unit *
80626a55
DE
9271lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9272 const char *dwo_name, const char *comp_dir,
9273 ULONGEST signature, int is_debug_types)
3019eac3
DE
9274{
9275 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9276 const char *kind = is_debug_types ? "TU" : "CU";
9277 void **dwo_file_slot;
3019eac3 9278 struct dwo_file *dwo_file;
80626a55 9279 struct dwp_file *dwp_file;
cb1df416 9280
80626a55 9281 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9282
ab5088bf 9283 dwp_file = get_dwp_file ();
80626a55 9284 if (dwp_file != NULL)
cf2c3c16 9285 {
80626a55
DE
9286 const struct dwp_hash_table *dwp_htab =
9287 is_debug_types ? dwp_file->tus : dwp_file->cus;
9288
9289 if (dwp_htab != NULL)
9290 {
9291 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9292 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9293 signature, is_debug_types);
80626a55
DE
9294
9295 if (dwo_cutu != NULL)
9296 {
9297 if (dwarf2_read_debug)
9298 {
9299 fprintf_unfiltered (gdb_stdlog,
9300 "Virtual DWO %s %s found: @%s\n",
9301 kind, hex_string (signature),
9302 host_address_to_string (dwo_cutu));
9303 }
9304 return dwo_cutu;
9305 }
9306 }
9307 }
9308
9309 /* Have we already seen DWO_NAME? */
9310
0ac5b59e 9311 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
80626a55
DE
9312 if (*dwo_file_slot == NULL)
9313 {
9314 /* Read in the file and build a table of the DWOs it contains. */
0ac5b59e 9315 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
80626a55
DE
9316 }
9317 /* NOTE: This will be NULL if unable to open the file. */
9318 dwo_file = *dwo_file_slot;
9319
9320 if (dwo_file != NULL)
9321 {
19c3d4c9 9322 struct dwo_unit *dwo_cutu = NULL;
80626a55 9323
19c3d4c9 9324 if (is_debug_types && dwo_file->tus)
80626a55 9325 {
19c3d4c9 9326 struct dwo_unit find_dwo_cutu;
9a619af0 9327
80626a55
DE
9328 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9329 find_dwo_cutu.signature = signature;
19c3d4c9
DE
9330 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9331 }
9332 else if (!is_debug_types && dwo_file->cu)
9333 {
9334 if (signature == dwo_file->cu->signature)
9335 dwo_cutu = dwo_file->cu;
9336 }
3019eac3 9337
19c3d4c9
DE
9338 if (dwo_cutu != NULL)
9339 {
9340 if (dwarf2_read_debug)
80626a55 9341 {
19c3d4c9
DE
9342 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9343 kind, dwo_name, hex_string (signature),
9344 host_address_to_string (dwo_cutu));
80626a55 9345 }
19c3d4c9 9346 return dwo_cutu;
80626a55 9347 }
2e276125 9348 }
9cdd5dbd 9349
80626a55
DE
9350 /* We didn't find it. This could mean a dwo_id mismatch, or
9351 someone deleted the DWO/DWP file, or the search path isn't set up
9352 correctly to find the file. */
9353
9354 if (dwarf2_read_debug)
9355 {
9356 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9357 kind, dwo_name, hex_string (signature));
9358 }
3019eac3
DE
9359
9360 complaint (&symfile_complaints,
6296d8c1 9361 _("Could not find DWO %s referenced by CU at offset 0x%x"
3019eac3 9362 " [in module %s]"),
6296d8c1 9363 kind, this_unit->offset.sect_off, objfile->name);
3019eac3 9364 return NULL;
5fb290d7
DJ
9365}
9366
80626a55
DE
9367/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9368 See lookup_dwo_cutu_unit for details. */
9369
9370static struct dwo_unit *
9371lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9372 const char *dwo_name, const char *comp_dir,
9373 ULONGEST signature)
9374{
9375 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9376}
9377
9378/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9379 See lookup_dwo_cutu_unit for details. */
9380
9381static struct dwo_unit *
9382lookup_dwo_type_unit (struct signatured_type *this_tu,
9383 const char *dwo_name, const char *comp_dir)
9384{
9385 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9386}
9387
3019eac3
DE
9388/* Free all resources associated with DWO_FILE.
9389 Close the DWO file and munmap the sections.
9390 All memory should be on the objfile obstack. */
348e048f
DE
9391
9392static void
3019eac3 9393free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9394{
3019eac3
DE
9395 int ix;
9396 struct dwarf2_section_info *section;
348e048f 9397
5c6fa7ab 9398 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9399 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9400
3019eac3
DE
9401 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9402}
348e048f 9403
3019eac3 9404/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9405
3019eac3
DE
9406static void
9407free_dwo_file_cleanup (void *arg)
9408{
9409 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9410 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9411
3019eac3
DE
9412 free_dwo_file (dwo_file, objfile);
9413}
348e048f 9414
3019eac3 9415/* Traversal function for free_dwo_files. */
2ab95328 9416
3019eac3
DE
9417static int
9418free_dwo_file_from_slot (void **slot, void *info)
9419{
9420 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9421 struct objfile *objfile = (struct objfile *) info;
348e048f 9422
3019eac3 9423 free_dwo_file (dwo_file, objfile);
348e048f 9424
3019eac3
DE
9425 return 1;
9426}
348e048f 9427
3019eac3 9428/* Free all resources associated with DWO_FILES. */
348e048f 9429
3019eac3
DE
9430static void
9431free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9432{
9433 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9434}
3019eac3
DE
9435\f
9436/* Read in various DIEs. */
348e048f 9437
d389af10
JK
9438/* qsort helper for inherit_abstract_dies. */
9439
9440static int
9441unsigned_int_compar (const void *ap, const void *bp)
9442{
9443 unsigned int a = *(unsigned int *) ap;
9444 unsigned int b = *(unsigned int *) bp;
9445
9446 return (a > b) - (b > a);
9447}
9448
9449/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9450 Inherit only the children of the DW_AT_abstract_origin DIE not being
9451 already referenced by DW_AT_abstract_origin from the children of the
9452 current DIE. */
d389af10
JK
9453
9454static void
9455inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9456{
9457 struct die_info *child_die;
9458 unsigned die_children_count;
9459 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9460 sect_offset *offsets;
9461 sect_offset *offsets_end, *offsetp;
d389af10
JK
9462 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9463 struct die_info *origin_die;
9464 /* Iterator of the ORIGIN_DIE children. */
9465 struct die_info *origin_child_die;
9466 struct cleanup *cleanups;
9467 struct attribute *attr;
cd02d79d
PA
9468 struct dwarf2_cu *origin_cu;
9469 struct pending **origin_previous_list_in_scope;
d389af10
JK
9470
9471 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9472 if (!attr)
9473 return;
9474
cd02d79d
PA
9475 /* Note that following die references may follow to a die in a
9476 different cu. */
9477
9478 origin_cu = cu;
9479 origin_die = follow_die_ref (die, attr, &origin_cu);
9480
9481 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9482 symbols in. */
9483 origin_previous_list_in_scope = origin_cu->list_in_scope;
9484 origin_cu->list_in_scope = cu->list_in_scope;
9485
edb3359d
DJ
9486 if (die->tag != origin_die->tag
9487 && !(die->tag == DW_TAG_inlined_subroutine
9488 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9489 complaint (&symfile_complaints,
9490 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9491 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9492
9493 child_die = die->child;
9494 die_children_count = 0;
9495 while (child_die && child_die->tag)
9496 {
9497 child_die = sibling_die (child_die);
9498 die_children_count++;
9499 }
9500 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9501 cleanups = make_cleanup (xfree, offsets);
9502
9503 offsets_end = offsets;
9504 child_die = die->child;
9505 while (child_die && child_die->tag)
9506 {
c38f313d
DJ
9507 /* For each CHILD_DIE, find the corresponding child of
9508 ORIGIN_DIE. If there is more than one layer of
9509 DW_AT_abstract_origin, follow them all; there shouldn't be,
9510 but GCC versions at least through 4.4 generate this (GCC PR
9511 40573). */
9512 struct die_info *child_origin_die = child_die;
cd02d79d 9513 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9514
c38f313d
DJ
9515 while (1)
9516 {
cd02d79d
PA
9517 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9518 child_origin_cu);
c38f313d
DJ
9519 if (attr == NULL)
9520 break;
cd02d79d
PA
9521 child_origin_die = follow_die_ref (child_origin_die, attr,
9522 &child_origin_cu);
c38f313d
DJ
9523 }
9524
d389af10
JK
9525 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9526 counterpart may exist. */
c38f313d 9527 if (child_origin_die != child_die)
d389af10 9528 {
edb3359d
DJ
9529 if (child_die->tag != child_origin_die->tag
9530 && !(child_die->tag == DW_TAG_inlined_subroutine
9531 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9532 complaint (&symfile_complaints,
9533 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9534 "different tags"), child_die->offset.sect_off,
9535 child_origin_die->offset.sect_off);
c38f313d
DJ
9536 if (child_origin_die->parent != origin_die)
9537 complaint (&symfile_complaints,
9538 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9539 "different parents"), child_die->offset.sect_off,
9540 child_origin_die->offset.sect_off);
c38f313d
DJ
9541 else
9542 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9543 }
9544 child_die = sibling_die (child_die);
9545 }
9546 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9547 unsigned_int_compar);
9548 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9549 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9550 complaint (&symfile_complaints,
9551 _("Multiple children of DIE 0x%x refer "
9552 "to DIE 0x%x as their abstract origin"),
b64f50a1 9553 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9554
9555 offsetp = offsets;
9556 origin_child_die = origin_die->child;
9557 while (origin_child_die && origin_child_die->tag)
9558 {
9559 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9560 while (offsetp < offsets_end
9561 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9562 offsetp++;
b64f50a1
JK
9563 if (offsetp >= offsets_end
9564 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9565 {
9566 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9567 process_die (origin_child_die, origin_cu);
d389af10
JK
9568 }
9569 origin_child_die = sibling_die (origin_child_die);
9570 }
cd02d79d 9571 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9572
9573 do_cleanups (cleanups);
9574}
9575
c906108c 9576static void
e7c27a73 9577read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9578{
e7c27a73 9579 struct objfile *objfile = cu->objfile;
52f0bd74 9580 struct context_stack *new;
c906108c
SS
9581 CORE_ADDR lowpc;
9582 CORE_ADDR highpc;
9583 struct die_info *child_die;
edb3359d 9584 struct attribute *attr, *call_line, *call_file;
15d034d0 9585 const char *name;
e142c38c 9586 CORE_ADDR baseaddr;
801e3a5b 9587 struct block *block;
edb3359d 9588 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9589 VEC (symbolp) *template_args = NULL;
9590 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9591
9592 if (inlined_func)
9593 {
9594 /* If we do not have call site information, we can't show the
9595 caller of this inlined function. That's too confusing, so
9596 only use the scope for local variables. */
9597 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9598 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9599 if (call_line == NULL || call_file == NULL)
9600 {
9601 read_lexical_block_scope (die, cu);
9602 return;
9603 }
9604 }
c906108c 9605
e142c38c
DJ
9606 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9607
94af9270 9608 name = dwarf2_name (die, cu);
c906108c 9609
e8d05480
JB
9610 /* Ignore functions with missing or empty names. These are actually
9611 illegal according to the DWARF standard. */
9612 if (name == NULL)
9613 {
9614 complaint (&symfile_complaints,
b64f50a1
JK
9615 _("missing name for subprogram DIE at %d"),
9616 die->offset.sect_off);
e8d05480
JB
9617 return;
9618 }
9619
9620 /* Ignore functions with missing or invalid low and high pc attributes. */
9621 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9622 {
ae4d0c03
PM
9623 attr = dwarf2_attr (die, DW_AT_external, cu);
9624 if (!attr || !DW_UNSND (attr))
9625 complaint (&symfile_complaints,
3e43a32a
MS
9626 _("cannot get low and high bounds "
9627 "for subprogram DIE at %d"),
b64f50a1 9628 die->offset.sect_off);
e8d05480
JB
9629 return;
9630 }
c906108c
SS
9631
9632 lowpc += baseaddr;
9633 highpc += baseaddr;
9634
34eaf542
TT
9635 /* If we have any template arguments, then we must allocate a
9636 different sort of symbol. */
9637 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9638 {
9639 if (child_die->tag == DW_TAG_template_type_param
9640 || child_die->tag == DW_TAG_template_value_param)
9641 {
e623cf5d 9642 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
9643 templ_func->base.is_cplus_template_function = 1;
9644 break;
9645 }
9646 }
9647
c906108c 9648 new = push_context (0, lowpc);
34eaf542
TT
9649 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9650 (struct symbol *) templ_func);
4c2df51b 9651
4cecd739
DJ
9652 /* If there is a location expression for DW_AT_frame_base, record
9653 it. */
e142c38c 9654 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9655 if (attr)
f1e6e072 9656 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 9657
e142c38c 9658 cu->list_in_scope = &local_symbols;
c906108c 9659
639d11d3 9660 if (die->child != NULL)
c906108c 9661 {
639d11d3 9662 child_die = die->child;
c906108c
SS
9663 while (child_die && child_die->tag)
9664 {
34eaf542
TT
9665 if (child_die->tag == DW_TAG_template_type_param
9666 || child_die->tag == DW_TAG_template_value_param)
9667 {
9668 struct symbol *arg = new_symbol (child_die, NULL, cu);
9669
f1078f66
DJ
9670 if (arg != NULL)
9671 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9672 }
9673 else
9674 process_die (child_die, cu);
c906108c
SS
9675 child_die = sibling_die (child_die);
9676 }
9677 }
9678
d389af10
JK
9679 inherit_abstract_dies (die, cu);
9680
4a811a97
UW
9681 /* If we have a DW_AT_specification, we might need to import using
9682 directives from the context of the specification DIE. See the
9683 comment in determine_prefix. */
9684 if (cu->language == language_cplus
9685 && dwarf2_attr (die, DW_AT_specification, cu))
9686 {
9687 struct dwarf2_cu *spec_cu = cu;
9688 struct die_info *spec_die = die_specification (die, &spec_cu);
9689
9690 while (spec_die)
9691 {
9692 child_die = spec_die->child;
9693 while (child_die && child_die->tag)
9694 {
9695 if (child_die->tag == DW_TAG_imported_module)
9696 process_die (child_die, spec_cu);
9697 child_die = sibling_die (child_die);
9698 }
9699
9700 /* In some cases, GCC generates specification DIEs that
9701 themselves contain DW_AT_specification attributes. */
9702 spec_die = die_specification (spec_die, &spec_cu);
9703 }
9704 }
9705
c906108c
SS
9706 new = pop_context ();
9707 /* Make a block for the local symbols within. */
801e3a5b
JB
9708 block = finish_block (new->name, &local_symbols, new->old_blocks,
9709 lowpc, highpc, objfile);
9710
df8a16a1 9711 /* For C++, set the block's scope. */
195a3f6c 9712 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9713 && cu->processing_has_namespace_info)
195a3f6c
TT
9714 block_set_scope (block, determine_prefix (die, cu),
9715 &objfile->objfile_obstack);
df8a16a1 9716
801e3a5b
JB
9717 /* If we have address ranges, record them. */
9718 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9719
34eaf542
TT
9720 /* Attach template arguments to function. */
9721 if (! VEC_empty (symbolp, template_args))
9722 {
9723 gdb_assert (templ_func != NULL);
9724
9725 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9726 templ_func->template_arguments
9727 = obstack_alloc (&objfile->objfile_obstack,
9728 (templ_func->n_template_arguments
9729 * sizeof (struct symbol *)));
9730 memcpy (templ_func->template_arguments,
9731 VEC_address (symbolp, template_args),
9732 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9733 VEC_free (symbolp, template_args);
9734 }
9735
208d8187
JB
9736 /* In C++, we can have functions nested inside functions (e.g., when
9737 a function declares a class that has methods). This means that
9738 when we finish processing a function scope, we may need to go
9739 back to building a containing block's symbol lists. */
9740 local_symbols = new->locals;
27aa8d6a 9741 using_directives = new->using_directives;
208d8187 9742
921e78cf
JB
9743 /* If we've finished processing a top-level function, subsequent
9744 symbols go in the file symbol list. */
9745 if (outermost_context_p ())
e142c38c 9746 cu->list_in_scope = &file_symbols;
c906108c
SS
9747}
9748
9749/* Process all the DIES contained within a lexical block scope. Start
9750 a new scope, process the dies, and then close the scope. */
9751
9752static void
e7c27a73 9753read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9754{
e7c27a73 9755 struct objfile *objfile = cu->objfile;
52f0bd74 9756 struct context_stack *new;
c906108c
SS
9757 CORE_ADDR lowpc, highpc;
9758 struct die_info *child_die;
e142c38c
DJ
9759 CORE_ADDR baseaddr;
9760
9761 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9762
9763 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9764 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9765 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9766 be nasty. Might be easier to properly extend generic blocks to
af34e669 9767 describe ranges. */
d85a05f0 9768 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9769 return;
9770 lowpc += baseaddr;
9771 highpc += baseaddr;
9772
9773 push_context (0, lowpc);
639d11d3 9774 if (die->child != NULL)
c906108c 9775 {
639d11d3 9776 child_die = die->child;
c906108c
SS
9777 while (child_die && child_die->tag)
9778 {
e7c27a73 9779 process_die (child_die, cu);
c906108c
SS
9780 child_die = sibling_die (child_die);
9781 }
9782 }
9783 new = pop_context ();
9784
8540c487 9785 if (local_symbols != NULL || using_directives != NULL)
c906108c 9786 {
801e3a5b
JB
9787 struct block *block
9788 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9789 highpc, objfile);
9790
9791 /* Note that recording ranges after traversing children, as we
9792 do here, means that recording a parent's ranges entails
9793 walking across all its children's ranges as they appear in
9794 the address map, which is quadratic behavior.
9795
9796 It would be nicer to record the parent's ranges before
9797 traversing its children, simply overriding whatever you find
9798 there. But since we don't even decide whether to create a
9799 block until after we've traversed its children, that's hard
9800 to do. */
9801 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9802 }
9803 local_symbols = new->locals;
27aa8d6a 9804 using_directives = new->using_directives;
c906108c
SS
9805}
9806
96408a79
SA
9807/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9808
9809static void
9810read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9811{
9812 struct objfile *objfile = cu->objfile;
9813 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9814 CORE_ADDR pc, baseaddr;
9815 struct attribute *attr;
9816 struct call_site *call_site, call_site_local;
9817 void **slot;
9818 int nparams;
9819 struct die_info *child_die;
9820
9821 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9822
9823 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9824 if (!attr)
9825 {
9826 complaint (&symfile_complaints,
9827 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9828 "DIE 0x%x [in module %s]"),
b64f50a1 9829 die->offset.sect_off, objfile->name);
96408a79
SA
9830 return;
9831 }
9832 pc = DW_ADDR (attr) + baseaddr;
9833
9834 if (cu->call_site_htab == NULL)
9835 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9836 NULL, &objfile->objfile_obstack,
9837 hashtab_obstack_allocate, NULL);
9838 call_site_local.pc = pc;
9839 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9840 if (*slot != NULL)
9841 {
9842 complaint (&symfile_complaints,
9843 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9844 "DIE 0x%x [in module %s]"),
b64f50a1 9845 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9846 return;
9847 }
9848
9849 /* Count parameters at the caller. */
9850
9851 nparams = 0;
9852 for (child_die = die->child; child_die && child_die->tag;
9853 child_die = sibling_die (child_die))
9854 {
9855 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9856 {
9857 complaint (&symfile_complaints,
9858 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9859 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9860 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9861 continue;
9862 }
9863
9864 nparams++;
9865 }
9866
9867 call_site = obstack_alloc (&objfile->objfile_obstack,
9868 (sizeof (*call_site)
9869 + (sizeof (*call_site->parameter)
9870 * (nparams - 1))));
9871 *slot = call_site;
9872 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9873 call_site->pc = pc;
9874
9875 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9876 {
9877 struct die_info *func_die;
9878
9879 /* Skip also over DW_TAG_inlined_subroutine. */
9880 for (func_die = die->parent;
9881 func_die && func_die->tag != DW_TAG_subprogram
9882 && func_die->tag != DW_TAG_subroutine_type;
9883 func_die = func_die->parent);
9884
9885 /* DW_AT_GNU_all_call_sites is a superset
9886 of DW_AT_GNU_all_tail_call_sites. */
9887 if (func_die
9888 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9889 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9890 {
9891 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9892 not complete. But keep CALL_SITE for look ups via call_site_htab,
9893 both the initial caller containing the real return address PC and
9894 the final callee containing the current PC of a chain of tail
9895 calls do not need to have the tail call list complete. But any
9896 function candidate for a virtual tail call frame searched via
9897 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9898 determined unambiguously. */
9899 }
9900 else
9901 {
9902 struct type *func_type = NULL;
9903
9904 if (func_die)
9905 func_type = get_die_type (func_die, cu);
9906 if (func_type != NULL)
9907 {
9908 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9909
9910 /* Enlist this call site to the function. */
9911 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9912 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9913 }
9914 else
9915 complaint (&symfile_complaints,
9916 _("Cannot find function owning DW_TAG_GNU_call_site "
9917 "DIE 0x%x [in module %s]"),
b64f50a1 9918 die->offset.sect_off, objfile->name);
96408a79
SA
9919 }
9920 }
9921
9922 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9923 if (attr == NULL)
9924 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9925 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9926 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9927 /* Keep NULL DWARF_BLOCK. */;
9928 else if (attr_form_is_block (attr))
9929 {
9930 struct dwarf2_locexpr_baton *dlbaton;
9931
9932 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9933 dlbaton->data = DW_BLOCK (attr)->data;
9934 dlbaton->size = DW_BLOCK (attr)->size;
9935 dlbaton->per_cu = cu->per_cu;
9936
9937 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9938 }
9939 else if (is_ref_attr (attr))
9940 {
96408a79
SA
9941 struct dwarf2_cu *target_cu = cu;
9942 struct die_info *target_die;
9943
9944 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9945 gdb_assert (target_cu->objfile == objfile);
9946 if (die_is_declaration (target_die, target_cu))
9947 {
9112db09
JK
9948 const char *target_physname = NULL;
9949 struct attribute *target_attr;
9950
9951 /* Prefer the mangled name; otherwise compute the demangled one. */
9952 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9953 if (target_attr == NULL)
9954 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9955 target_cu);
9956 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9957 target_physname = DW_STRING (target_attr);
9958 else
9959 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
9960 if (target_physname == NULL)
9961 complaint (&symfile_complaints,
9962 _("DW_AT_GNU_call_site_target target DIE has invalid "
9963 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9964 die->offset.sect_off, objfile->name);
96408a79 9965 else
7d455152 9966 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9967 }
9968 else
9969 {
9970 CORE_ADDR lowpc;
9971
9972 /* DW_AT_entry_pc should be preferred. */
9973 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9974 complaint (&symfile_complaints,
9975 _("DW_AT_GNU_call_site_target target DIE has invalid "
9976 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9977 die->offset.sect_off, objfile->name);
96408a79
SA
9978 else
9979 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9980 }
9981 }
9982 else
9983 complaint (&symfile_complaints,
9984 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9985 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9986 die->offset.sect_off, objfile->name);
96408a79
SA
9987
9988 call_site->per_cu = cu->per_cu;
9989
9990 for (child_die = die->child;
9991 child_die && child_die->tag;
9992 child_die = sibling_die (child_die))
9993 {
96408a79 9994 struct call_site_parameter *parameter;
1788b2d3 9995 struct attribute *loc, *origin;
96408a79
SA
9996
9997 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9998 {
9999 /* Already printed the complaint above. */
10000 continue;
10001 }
10002
10003 gdb_assert (call_site->parameter_count < nparams);
10004 parameter = &call_site->parameter[call_site->parameter_count];
10005
1788b2d3
JK
10006 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10007 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10008 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10009
24c5c679 10010 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
10011 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10012 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10013 {
10014 sect_offset offset;
10015
10016 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10017 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10018 if (!offset_in_cu_p (&cu->header, offset))
10019 {
10020 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10021 binding can be done only inside one CU. Such referenced DIE
10022 therefore cannot be even moved to DW_TAG_partial_unit. */
10023 complaint (&symfile_complaints,
10024 _("DW_AT_abstract_origin offset is not in CU for "
10025 "DW_TAG_GNU_call_site child DIE 0x%x "
10026 "[in module %s]"),
10027 child_die->offset.sect_off, objfile->name);
10028 continue;
10029 }
1788b2d3
JK
10030 parameter->u.param_offset.cu_off = (offset.sect_off
10031 - cu->header.offset.sect_off);
10032 }
10033 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10034 {
10035 complaint (&symfile_complaints,
10036 _("No DW_FORM_block* DW_AT_location for "
10037 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10038 child_die->offset.sect_off, objfile->name);
96408a79
SA
10039 continue;
10040 }
24c5c679 10041 else
96408a79 10042 {
24c5c679
JK
10043 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10044 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10045 if (parameter->u.dwarf_reg != -1)
10046 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10047 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10048 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10049 &parameter->u.fb_offset))
10050 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10051 else
10052 {
10053 complaint (&symfile_complaints,
10054 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10055 "for DW_FORM_block* DW_AT_location is supported for "
10056 "DW_TAG_GNU_call_site child DIE 0x%x "
10057 "[in module %s]"),
10058 child_die->offset.sect_off, objfile->name);
10059 continue;
10060 }
96408a79
SA
10061 }
10062
10063 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10064 if (!attr_form_is_block (attr))
10065 {
10066 complaint (&symfile_complaints,
10067 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10068 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10069 child_die->offset.sect_off, objfile->name);
96408a79
SA
10070 continue;
10071 }
10072 parameter->value = DW_BLOCK (attr)->data;
10073 parameter->value_size = DW_BLOCK (attr)->size;
10074
10075 /* Parameters are not pre-cleared by memset above. */
10076 parameter->data_value = NULL;
10077 parameter->data_value_size = 0;
10078 call_site->parameter_count++;
10079
10080 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10081 if (attr)
10082 {
10083 if (!attr_form_is_block (attr))
10084 complaint (&symfile_complaints,
10085 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10086 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10087 child_die->offset.sect_off, objfile->name);
96408a79
SA
10088 else
10089 {
10090 parameter->data_value = DW_BLOCK (attr)->data;
10091 parameter->data_value_size = DW_BLOCK (attr)->size;
10092 }
10093 }
10094 }
10095}
10096
43039443 10097/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10098 Return 1 if the attributes are present and valid, otherwise, return 0.
10099 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10100
10101static int
10102dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10103 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10104 struct partial_symtab *ranges_pst)
43039443
JK
10105{
10106 struct objfile *objfile = cu->objfile;
10107 struct comp_unit_head *cu_header = &cu->header;
10108 bfd *obfd = objfile->obfd;
10109 unsigned int addr_size = cu_header->addr_size;
10110 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10111 /* Base address selection entry. */
10112 CORE_ADDR base;
10113 int found_base;
10114 unsigned int dummy;
d521ce57 10115 const gdb_byte *buffer;
43039443
JK
10116 CORE_ADDR marker;
10117 int low_set;
10118 CORE_ADDR low = 0;
10119 CORE_ADDR high = 0;
ff013f42 10120 CORE_ADDR baseaddr;
43039443 10121
d00adf39
DE
10122 found_base = cu->base_known;
10123 base = cu->base_address;
43039443 10124
be391dca 10125 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10126 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10127 {
10128 complaint (&symfile_complaints,
10129 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10130 offset);
10131 return 0;
10132 }
dce234bc 10133 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10134
10135 /* Read in the largest possible address. */
10136 marker = read_address (obfd, buffer, cu, &dummy);
10137 if ((marker & mask) == mask)
10138 {
10139 /* If we found the largest possible address, then
10140 read the base address. */
10141 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10142 buffer += 2 * addr_size;
10143 offset += 2 * addr_size;
10144 found_base = 1;
10145 }
10146
10147 low_set = 0;
10148
e7030f15 10149 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10150
43039443
JK
10151 while (1)
10152 {
10153 CORE_ADDR range_beginning, range_end;
10154
10155 range_beginning = read_address (obfd, buffer, cu, &dummy);
10156 buffer += addr_size;
10157 range_end = read_address (obfd, buffer, cu, &dummy);
10158 buffer += addr_size;
10159 offset += 2 * addr_size;
10160
10161 /* An end of list marker is a pair of zero addresses. */
10162 if (range_beginning == 0 && range_end == 0)
10163 /* Found the end of list entry. */
10164 break;
10165
10166 /* Each base address selection entry is a pair of 2 values.
10167 The first is the largest possible address, the second is
10168 the base address. Check for a base address here. */
10169 if ((range_beginning & mask) == mask)
10170 {
10171 /* If we found the largest possible address, then
10172 read the base address. */
10173 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10174 found_base = 1;
10175 continue;
10176 }
10177
10178 if (!found_base)
10179 {
10180 /* We have no valid base address for the ranges
10181 data. */
10182 complaint (&symfile_complaints,
10183 _("Invalid .debug_ranges data (no base address)"));
10184 return 0;
10185 }
10186
9277c30c
UW
10187 if (range_beginning > range_end)
10188 {
10189 /* Inverted range entries are invalid. */
10190 complaint (&symfile_complaints,
10191 _("Invalid .debug_ranges data (inverted range)"));
10192 return 0;
10193 }
10194
10195 /* Empty range entries have no effect. */
10196 if (range_beginning == range_end)
10197 continue;
10198
43039443
JK
10199 range_beginning += base;
10200 range_end += base;
10201
01093045
DE
10202 /* A not-uncommon case of bad debug info.
10203 Don't pollute the addrmap with bad data. */
10204 if (range_beginning + baseaddr == 0
10205 && !dwarf2_per_objfile->has_section_at_zero)
10206 {
10207 complaint (&symfile_complaints,
10208 _(".debug_ranges entry has start address of zero"
10209 " [in module %s]"), objfile->name);
10210 continue;
10211 }
10212
9277c30c 10213 if (ranges_pst != NULL)
ff013f42 10214 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10215 range_beginning + baseaddr,
10216 range_end - 1 + baseaddr,
ff013f42
JK
10217 ranges_pst);
10218
43039443
JK
10219 /* FIXME: This is recording everything as a low-high
10220 segment of consecutive addresses. We should have a
10221 data structure for discontiguous block ranges
10222 instead. */
10223 if (! low_set)
10224 {
10225 low = range_beginning;
10226 high = range_end;
10227 low_set = 1;
10228 }
10229 else
10230 {
10231 if (range_beginning < low)
10232 low = range_beginning;
10233 if (range_end > high)
10234 high = range_end;
10235 }
10236 }
10237
10238 if (! low_set)
10239 /* If the first entry is an end-of-list marker, the range
10240 describes an empty scope, i.e. no instructions. */
10241 return 0;
10242
10243 if (low_return)
10244 *low_return = low;
10245 if (high_return)
10246 *high_return = high;
10247 return 1;
10248}
10249
af34e669
DJ
10250/* Get low and high pc attributes from a die. Return 1 if the attributes
10251 are present and valid, otherwise, return 0. Return -1 if the range is
10252 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10253
c906108c 10254static int
af34e669 10255dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10256 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10257 struct partial_symtab *pst)
c906108c
SS
10258{
10259 struct attribute *attr;
91da1414 10260 struct attribute *attr_high;
af34e669
DJ
10261 CORE_ADDR low = 0;
10262 CORE_ADDR high = 0;
10263 int ret = 0;
c906108c 10264
91da1414
MW
10265 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10266 if (attr_high)
af34e669 10267 {
e142c38c 10268 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10269 if (attr)
91da1414
MW
10270 {
10271 low = DW_ADDR (attr);
3019eac3
DE
10272 if (attr_high->form == DW_FORM_addr
10273 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10274 high = DW_ADDR (attr_high);
10275 else
10276 high = low + DW_UNSND (attr_high);
10277 }
af34e669
DJ
10278 else
10279 /* Found high w/o low attribute. */
10280 return 0;
10281
10282 /* Found consecutive range of addresses. */
10283 ret = 1;
10284 }
c906108c 10285 else
af34e669 10286 {
e142c38c 10287 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10288 if (attr != NULL)
10289 {
ab435259
DE
10290 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10291 We take advantage of the fact that DW_AT_ranges does not appear
10292 in DW_TAG_compile_unit of DWO files. */
10293 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10294 unsigned int ranges_offset = (DW_UNSND (attr)
10295 + (need_ranges_base
10296 ? cu->ranges_base
10297 : 0));
2e3cf129 10298
af34e669 10299 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10300 .debug_ranges section. */
2e3cf129 10301 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10302 return 0;
43039443 10303 /* Found discontinuous range of addresses. */
af34e669
DJ
10304 ret = -1;
10305 }
10306 }
c906108c 10307
9373cf26
JK
10308 /* read_partial_die has also the strict LOW < HIGH requirement. */
10309 if (high <= low)
c906108c
SS
10310 return 0;
10311
10312 /* When using the GNU linker, .gnu.linkonce. sections are used to
10313 eliminate duplicate copies of functions and vtables and such.
10314 The linker will arbitrarily choose one and discard the others.
10315 The AT_*_pc values for such functions refer to local labels in
10316 these sections. If the section from that file was discarded, the
10317 labels are not in the output, so the relocs get a value of 0.
10318 If this is a discarded function, mark the pc bounds as invalid,
10319 so that GDB will ignore it. */
72dca2f5 10320 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10321 return 0;
10322
10323 *lowpc = low;
96408a79
SA
10324 if (highpc)
10325 *highpc = high;
af34e669 10326 return ret;
c906108c
SS
10327}
10328
b084d499
JB
10329/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10330 its low and high PC addresses. Do nothing if these addresses could not
10331 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10332 and HIGHPC to the high address if greater than HIGHPC. */
10333
10334static void
10335dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10336 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10337 struct dwarf2_cu *cu)
10338{
10339 CORE_ADDR low, high;
10340 struct die_info *child = die->child;
10341
d85a05f0 10342 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10343 {
10344 *lowpc = min (*lowpc, low);
10345 *highpc = max (*highpc, high);
10346 }
10347
10348 /* If the language does not allow nested subprograms (either inside
10349 subprograms or lexical blocks), we're done. */
10350 if (cu->language != language_ada)
10351 return;
6e70227d 10352
b084d499
JB
10353 /* Check all the children of the given DIE. If it contains nested
10354 subprograms, then check their pc bounds. Likewise, we need to
10355 check lexical blocks as well, as they may also contain subprogram
10356 definitions. */
10357 while (child && child->tag)
10358 {
10359 if (child->tag == DW_TAG_subprogram
10360 || child->tag == DW_TAG_lexical_block)
10361 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10362 child = sibling_die (child);
10363 }
10364}
10365
fae299cd
DC
10366/* Get the low and high pc's represented by the scope DIE, and store
10367 them in *LOWPC and *HIGHPC. If the correct values can't be
10368 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10369
10370static void
10371get_scope_pc_bounds (struct die_info *die,
10372 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10373 struct dwarf2_cu *cu)
10374{
10375 CORE_ADDR best_low = (CORE_ADDR) -1;
10376 CORE_ADDR best_high = (CORE_ADDR) 0;
10377 CORE_ADDR current_low, current_high;
10378
d85a05f0 10379 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10380 {
10381 best_low = current_low;
10382 best_high = current_high;
10383 }
10384 else
10385 {
10386 struct die_info *child = die->child;
10387
10388 while (child && child->tag)
10389 {
10390 switch (child->tag) {
10391 case DW_TAG_subprogram:
b084d499 10392 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10393 break;
10394 case DW_TAG_namespace:
f55ee35c 10395 case DW_TAG_module:
fae299cd
DC
10396 /* FIXME: carlton/2004-01-16: Should we do this for
10397 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10398 that current GCC's always emit the DIEs corresponding
10399 to definitions of methods of classes as children of a
10400 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10401 the DIEs giving the declarations, which could be
10402 anywhere). But I don't see any reason why the
10403 standards says that they have to be there. */
10404 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10405
10406 if (current_low != ((CORE_ADDR) -1))
10407 {
10408 best_low = min (best_low, current_low);
10409 best_high = max (best_high, current_high);
10410 }
10411 break;
10412 default:
0963b4bd 10413 /* Ignore. */
fae299cd
DC
10414 break;
10415 }
10416
10417 child = sibling_die (child);
10418 }
10419 }
10420
10421 *lowpc = best_low;
10422 *highpc = best_high;
10423}
10424
801e3a5b
JB
10425/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10426 in DIE. */
380bca97 10427
801e3a5b
JB
10428static void
10429dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10430 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10431{
bb5ed363 10432 struct objfile *objfile = cu->objfile;
801e3a5b 10433 struct attribute *attr;
91da1414 10434 struct attribute *attr_high;
801e3a5b 10435
91da1414
MW
10436 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10437 if (attr_high)
801e3a5b 10438 {
801e3a5b
JB
10439 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10440 if (attr)
10441 {
10442 CORE_ADDR low = DW_ADDR (attr);
91da1414 10443 CORE_ADDR high;
3019eac3
DE
10444 if (attr_high->form == DW_FORM_addr
10445 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10446 high = DW_ADDR (attr_high);
10447 else
10448 high = low + DW_UNSND (attr_high);
9a619af0 10449
801e3a5b
JB
10450 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10451 }
10452 }
10453
10454 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10455 if (attr)
10456 {
bb5ed363 10457 bfd *obfd = objfile->obfd;
ab435259
DE
10458 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10459 We take advantage of the fact that DW_AT_ranges does not appear
10460 in DW_TAG_compile_unit of DWO files. */
10461 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10462
10463 /* The value of the DW_AT_ranges attribute is the offset of the
10464 address range list in the .debug_ranges section. */
ab435259
DE
10465 unsigned long offset = (DW_UNSND (attr)
10466 + (need_ranges_base ? cu->ranges_base : 0));
d521ce57 10467 const gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10468
10469 /* For some target architectures, but not others, the
10470 read_address function sign-extends the addresses it returns.
10471 To recognize base address selection entries, we need a
10472 mask. */
10473 unsigned int addr_size = cu->header.addr_size;
10474 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10475
10476 /* The base address, to which the next pair is relative. Note
10477 that this 'base' is a DWARF concept: most entries in a range
10478 list are relative, to reduce the number of relocs against the
10479 debugging information. This is separate from this function's
10480 'baseaddr' argument, which GDB uses to relocate debugging
10481 information from a shared library based on the address at
10482 which the library was loaded. */
d00adf39
DE
10483 CORE_ADDR base = cu->base_address;
10484 int base_known = cu->base_known;
801e3a5b 10485
be391dca 10486 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10487 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10488 {
10489 complaint (&symfile_complaints,
10490 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10491 offset);
10492 return;
10493 }
10494
10495 for (;;)
10496 {
10497 unsigned int bytes_read;
10498 CORE_ADDR start, end;
10499
10500 start = read_address (obfd, buffer, cu, &bytes_read);
10501 buffer += bytes_read;
10502 end = read_address (obfd, buffer, cu, &bytes_read);
10503 buffer += bytes_read;
10504
10505 /* Did we find the end of the range list? */
10506 if (start == 0 && end == 0)
10507 break;
10508
10509 /* Did we find a base address selection entry? */
10510 else if ((start & base_select_mask) == base_select_mask)
10511 {
10512 base = end;
10513 base_known = 1;
10514 }
10515
10516 /* We found an ordinary address range. */
10517 else
10518 {
10519 if (!base_known)
10520 {
10521 complaint (&symfile_complaints,
3e43a32a
MS
10522 _("Invalid .debug_ranges data "
10523 "(no base address)"));
801e3a5b
JB
10524 return;
10525 }
10526
9277c30c
UW
10527 if (start > end)
10528 {
10529 /* Inverted range entries are invalid. */
10530 complaint (&symfile_complaints,
10531 _("Invalid .debug_ranges data "
10532 "(inverted range)"));
10533 return;
10534 }
10535
10536 /* Empty range entries have no effect. */
10537 if (start == end)
10538 continue;
10539
01093045
DE
10540 start += base + baseaddr;
10541 end += base + baseaddr;
10542
10543 /* A not-uncommon case of bad debug info.
10544 Don't pollute the addrmap with bad data. */
10545 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10546 {
10547 complaint (&symfile_complaints,
10548 _(".debug_ranges entry has start address of zero"
10549 " [in module %s]"), objfile->name);
10550 continue;
10551 }
10552
10553 record_block_range (block, start, end - 1);
801e3a5b
JB
10554 }
10555 }
10556 }
10557}
10558
685b1105
JK
10559/* Check whether the producer field indicates either of GCC < 4.6, or the
10560 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10561
685b1105
JK
10562static void
10563check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10564{
10565 const char *cs;
10566 int major, minor, release;
10567
10568 if (cu->producer == NULL)
10569 {
10570 /* For unknown compilers expect their behavior is DWARF version
10571 compliant.
10572
10573 GCC started to support .debug_types sections by -gdwarf-4 since
10574 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10575 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10576 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10577 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10578 }
685b1105 10579 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10580 {
685b1105
JK
10581 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10582
ba919b58
TT
10583 cs = &cu->producer[strlen ("GNU ")];
10584 while (*cs && !isdigit (*cs))
10585 cs++;
10586 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10587 {
10588 /* Not recognized as GCC. */
10589 }
10590 else
1b80a9fa
JK
10591 {
10592 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10593 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10594 }
685b1105
JK
10595 }
10596 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10597 cu->producer_is_icc = 1;
10598 else
10599 {
10600 /* For other non-GCC compilers, expect their behavior is DWARF version
10601 compliant. */
60d5a603
JK
10602 }
10603
ba919b58 10604 cu->checked_producer = 1;
685b1105 10605}
ba919b58 10606
685b1105
JK
10607/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10608 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10609 during 4.6.0 experimental. */
10610
10611static int
10612producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10613{
10614 if (!cu->checked_producer)
10615 check_producer (cu);
10616
10617 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10618}
10619
10620/* Return the default accessibility type if it is not overriden by
10621 DW_AT_accessibility. */
10622
10623static enum dwarf_access_attribute
10624dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10625{
10626 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10627 {
10628 /* The default DWARF 2 accessibility for members is public, the default
10629 accessibility for inheritance is private. */
10630
10631 if (die->tag != DW_TAG_inheritance)
10632 return DW_ACCESS_public;
10633 else
10634 return DW_ACCESS_private;
10635 }
10636 else
10637 {
10638 /* DWARF 3+ defines the default accessibility a different way. The same
10639 rules apply now for DW_TAG_inheritance as for the members and it only
10640 depends on the container kind. */
10641
10642 if (die->parent->tag == DW_TAG_class_type)
10643 return DW_ACCESS_private;
10644 else
10645 return DW_ACCESS_public;
10646 }
10647}
10648
74ac6d43
TT
10649/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10650 offset. If the attribute was not found return 0, otherwise return
10651 1. If it was found but could not properly be handled, set *OFFSET
10652 to 0. */
10653
10654static int
10655handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10656 LONGEST *offset)
10657{
10658 struct attribute *attr;
10659
10660 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10661 if (attr != NULL)
10662 {
10663 *offset = 0;
10664
10665 /* Note that we do not check for a section offset first here.
10666 This is because DW_AT_data_member_location is new in DWARF 4,
10667 so if we see it, we can assume that a constant form is really
10668 a constant and not a section offset. */
10669 if (attr_form_is_constant (attr))
10670 *offset = dwarf2_get_attr_constant_value (attr, 0);
10671 else if (attr_form_is_section_offset (attr))
10672 dwarf2_complex_location_expr_complaint ();
10673 else if (attr_form_is_block (attr))
10674 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10675 else
10676 dwarf2_complex_location_expr_complaint ();
10677
10678 return 1;
10679 }
10680
10681 return 0;
10682}
10683
c906108c
SS
10684/* Add an aggregate field to the field list. */
10685
10686static void
107d2387 10687dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10688 struct dwarf2_cu *cu)
6e70227d 10689{
e7c27a73 10690 struct objfile *objfile = cu->objfile;
5e2b427d 10691 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10692 struct nextfield *new_field;
10693 struct attribute *attr;
10694 struct field *fp;
15d034d0 10695 const char *fieldname = "";
c906108c
SS
10696
10697 /* Allocate a new field list entry and link it in. */
10698 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10699 make_cleanup (xfree, new_field);
c906108c 10700 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10701
10702 if (die->tag == DW_TAG_inheritance)
10703 {
10704 new_field->next = fip->baseclasses;
10705 fip->baseclasses = new_field;
10706 }
10707 else
10708 {
10709 new_field->next = fip->fields;
10710 fip->fields = new_field;
10711 }
c906108c
SS
10712 fip->nfields++;
10713
e142c38c 10714 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10715 if (attr)
10716 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10717 else
10718 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10719 if (new_field->accessibility != DW_ACCESS_public)
10720 fip->non_public_fields = 1;
60d5a603 10721
e142c38c 10722 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10723 if (attr)
10724 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10725 else
10726 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10727
10728 fp = &new_field->field;
a9a9bd0f 10729
e142c38c 10730 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10731 {
74ac6d43
TT
10732 LONGEST offset;
10733
a9a9bd0f 10734 /* Data member other than a C++ static data member. */
6e70227d 10735
c906108c 10736 /* Get type of field. */
e7c27a73 10737 fp->type = die_type (die, cu);
c906108c 10738
d6a843b5 10739 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10740
c906108c 10741 /* Get bit size of field (zero if none). */
e142c38c 10742 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10743 if (attr)
10744 {
10745 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10746 }
10747 else
10748 {
10749 FIELD_BITSIZE (*fp) = 0;
10750 }
10751
10752 /* Get bit offset of field. */
74ac6d43
TT
10753 if (handle_data_member_location (die, cu, &offset))
10754 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10755 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10756 if (attr)
10757 {
5e2b427d 10758 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10759 {
10760 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10761 additional bit offset from the MSB of the containing
10762 anonymous object to the MSB of the field. We don't
10763 have to do anything special since we don't need to
10764 know the size of the anonymous object. */
f41f5e61 10765 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10766 }
10767 else
10768 {
10769 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10770 MSB of the anonymous object, subtract off the number of
10771 bits from the MSB of the field to the MSB of the
10772 object, and then subtract off the number of bits of
10773 the field itself. The result is the bit offset of
10774 the LSB of the field. */
c906108c
SS
10775 int anonymous_size;
10776 int bit_offset = DW_UNSND (attr);
10777
e142c38c 10778 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10779 if (attr)
10780 {
10781 /* The size of the anonymous object containing
10782 the bit field is explicit, so use the
10783 indicated size (in bytes). */
10784 anonymous_size = DW_UNSND (attr);
10785 }
10786 else
10787 {
10788 /* The size of the anonymous object containing
10789 the bit field must be inferred from the type
10790 attribute of the data member containing the
10791 bit field. */
10792 anonymous_size = TYPE_LENGTH (fp->type);
10793 }
f41f5e61
PA
10794 SET_FIELD_BITPOS (*fp,
10795 (FIELD_BITPOS (*fp)
10796 + anonymous_size * bits_per_byte
10797 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10798 }
10799 }
10800
10801 /* Get name of field. */
39cbfefa
DJ
10802 fieldname = dwarf2_name (die, cu);
10803 if (fieldname == NULL)
10804 fieldname = "";
d8151005
DJ
10805
10806 /* The name is already allocated along with this objfile, so we don't
10807 need to duplicate it for the type. */
10808 fp->name = fieldname;
c906108c
SS
10809
10810 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10811 pointer or virtual base class pointer) to private. */
e142c38c 10812 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10813 {
d48cc9dd 10814 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10815 new_field->accessibility = DW_ACCESS_private;
10816 fip->non_public_fields = 1;
10817 }
10818 }
a9a9bd0f 10819 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10820 {
a9a9bd0f
DC
10821 /* C++ static member. */
10822
10823 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10824 is a declaration, but all versions of G++ as of this writing
10825 (so through at least 3.2.1) incorrectly generate
10826 DW_TAG_variable tags. */
6e70227d 10827
ff355380 10828 const char *physname;
c906108c 10829
a9a9bd0f 10830 /* Get name of field. */
39cbfefa
DJ
10831 fieldname = dwarf2_name (die, cu);
10832 if (fieldname == NULL)
c906108c
SS
10833 return;
10834
254e6b9e 10835 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10836 if (attr
10837 /* Only create a symbol if this is an external value.
10838 new_symbol checks this and puts the value in the global symbol
10839 table, which we want. If it is not external, new_symbol
10840 will try to put the value in cu->list_in_scope which is wrong. */
10841 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10842 {
10843 /* A static const member, not much different than an enum as far as
10844 we're concerned, except that we can support more types. */
10845 new_symbol (die, NULL, cu);
10846 }
10847
2df3850c 10848 /* Get physical name. */
ff355380 10849 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10850
d8151005
DJ
10851 /* The name is already allocated along with this objfile, so we don't
10852 need to duplicate it for the type. */
10853 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10854 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10855 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10856 }
10857 else if (die->tag == DW_TAG_inheritance)
10858 {
74ac6d43 10859 LONGEST offset;
d4b96c9a 10860
74ac6d43
TT
10861 /* C++ base class field. */
10862 if (handle_data_member_location (die, cu, &offset))
10863 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10864 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10865 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10866 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10867 fip->nbaseclasses++;
10868 }
10869}
10870
98751a41
JK
10871/* Add a typedef defined in the scope of the FIP's class. */
10872
10873static void
10874dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10875 struct dwarf2_cu *cu)
6e70227d 10876{
98751a41 10877 struct objfile *objfile = cu->objfile;
98751a41
JK
10878 struct typedef_field_list *new_field;
10879 struct attribute *attr;
10880 struct typedef_field *fp;
10881 char *fieldname = "";
10882
10883 /* Allocate a new field list entry and link it in. */
10884 new_field = xzalloc (sizeof (*new_field));
10885 make_cleanup (xfree, new_field);
10886
10887 gdb_assert (die->tag == DW_TAG_typedef);
10888
10889 fp = &new_field->field;
10890
10891 /* Get name of field. */
10892 fp->name = dwarf2_name (die, cu);
10893 if (fp->name == NULL)
10894 return;
10895
10896 fp->type = read_type_die (die, cu);
10897
10898 new_field->next = fip->typedef_field_list;
10899 fip->typedef_field_list = new_field;
10900 fip->typedef_field_list_count++;
10901}
10902
c906108c
SS
10903/* Create the vector of fields, and attach it to the type. */
10904
10905static void
fba45db2 10906dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10907 struct dwarf2_cu *cu)
c906108c
SS
10908{
10909 int nfields = fip->nfields;
10910
10911 /* Record the field count, allocate space for the array of fields,
10912 and create blank accessibility bitfields if necessary. */
10913 TYPE_NFIELDS (type) = nfields;
10914 TYPE_FIELDS (type) = (struct field *)
10915 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10916 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10917
b4ba55a1 10918 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10919 {
10920 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10921
10922 TYPE_FIELD_PRIVATE_BITS (type) =
10923 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10924 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10925
10926 TYPE_FIELD_PROTECTED_BITS (type) =
10927 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10928 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10929
774b6a14
TT
10930 TYPE_FIELD_IGNORE_BITS (type) =
10931 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10932 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10933 }
10934
10935 /* If the type has baseclasses, allocate and clear a bit vector for
10936 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10937 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10938 {
10939 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10940 unsigned char *pointer;
c906108c
SS
10941
10942 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10943 pointer = TYPE_ALLOC (type, num_bytes);
10944 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10945 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10946 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10947 }
10948
3e43a32a
MS
10949 /* Copy the saved-up fields into the field vector. Start from the head of
10950 the list, adding to the tail of the field array, so that they end up in
10951 the same order in the array in which they were added to the list. */
c906108c
SS
10952 while (nfields-- > 0)
10953 {
7d0ccb61
DJ
10954 struct nextfield *fieldp;
10955
10956 if (fip->fields)
10957 {
10958 fieldp = fip->fields;
10959 fip->fields = fieldp->next;
10960 }
10961 else
10962 {
10963 fieldp = fip->baseclasses;
10964 fip->baseclasses = fieldp->next;
10965 }
10966
10967 TYPE_FIELD (type, nfields) = fieldp->field;
10968 switch (fieldp->accessibility)
c906108c 10969 {
c5aa993b 10970 case DW_ACCESS_private:
b4ba55a1
JB
10971 if (cu->language != language_ada)
10972 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10973 break;
c906108c 10974
c5aa993b 10975 case DW_ACCESS_protected:
b4ba55a1
JB
10976 if (cu->language != language_ada)
10977 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10978 break;
c906108c 10979
c5aa993b
JM
10980 case DW_ACCESS_public:
10981 break;
c906108c 10982
c5aa993b
JM
10983 default:
10984 /* Unknown accessibility. Complain and treat it as public. */
10985 {
e2e0b3e5 10986 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10987 fieldp->accessibility);
c5aa993b
JM
10988 }
10989 break;
c906108c
SS
10990 }
10991 if (nfields < fip->nbaseclasses)
10992 {
7d0ccb61 10993 switch (fieldp->virtuality)
c906108c 10994 {
c5aa993b
JM
10995 case DW_VIRTUALITY_virtual:
10996 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10997 if (cu->language == language_ada)
a73c6dcd 10998 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10999 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11000 break;
c906108c
SS
11001 }
11002 }
c906108c
SS
11003 }
11004}
11005
7d27a96d
TT
11006/* Return true if this member function is a constructor, false
11007 otherwise. */
11008
11009static int
11010dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11011{
11012 const char *fieldname;
11013 const char *typename;
11014 int len;
11015
11016 if (die->parent == NULL)
11017 return 0;
11018
11019 if (die->parent->tag != DW_TAG_structure_type
11020 && die->parent->tag != DW_TAG_union_type
11021 && die->parent->tag != DW_TAG_class_type)
11022 return 0;
11023
11024 fieldname = dwarf2_name (die, cu);
11025 typename = dwarf2_name (die->parent, cu);
11026 if (fieldname == NULL || typename == NULL)
11027 return 0;
11028
11029 len = strlen (fieldname);
11030 return (strncmp (fieldname, typename, len) == 0
11031 && (typename[len] == '\0' || typename[len] == '<'));
11032}
11033
c906108c
SS
11034/* Add a member function to the proper fieldlist. */
11035
11036static void
107d2387 11037dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11038 struct type *type, struct dwarf2_cu *cu)
c906108c 11039{
e7c27a73 11040 struct objfile *objfile = cu->objfile;
c906108c
SS
11041 struct attribute *attr;
11042 struct fnfieldlist *flp;
11043 int i;
11044 struct fn_field *fnp;
15d034d0 11045 const char *fieldname;
c906108c 11046 struct nextfnfield *new_fnfield;
f792889a 11047 struct type *this_type;
60d5a603 11048 enum dwarf_access_attribute accessibility;
c906108c 11049
b4ba55a1 11050 if (cu->language == language_ada)
a73c6dcd 11051 error (_("unexpected member function in Ada type"));
b4ba55a1 11052
2df3850c 11053 /* Get name of member function. */
39cbfefa
DJ
11054 fieldname = dwarf2_name (die, cu);
11055 if (fieldname == NULL)
2df3850c 11056 return;
c906108c 11057
c906108c
SS
11058 /* Look up member function name in fieldlist. */
11059 for (i = 0; i < fip->nfnfields; i++)
11060 {
27bfe10e 11061 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11062 break;
11063 }
11064
11065 /* Create new list element if necessary. */
11066 if (i < fip->nfnfields)
11067 flp = &fip->fnfieldlists[i];
11068 else
11069 {
11070 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11071 {
11072 fip->fnfieldlists = (struct fnfieldlist *)
11073 xrealloc (fip->fnfieldlists,
11074 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11075 * sizeof (struct fnfieldlist));
c906108c 11076 if (fip->nfnfields == 0)
c13c43fd 11077 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11078 }
11079 flp = &fip->fnfieldlists[fip->nfnfields];
11080 flp->name = fieldname;
11081 flp->length = 0;
11082 flp->head = NULL;
3da10d80 11083 i = fip->nfnfields++;
c906108c
SS
11084 }
11085
11086 /* Create a new member function field and chain it to the field list
0963b4bd 11087 entry. */
c906108c 11088 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11089 make_cleanup (xfree, new_fnfield);
c906108c
SS
11090 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11091 new_fnfield->next = flp->head;
11092 flp->head = new_fnfield;
11093 flp->length++;
11094
11095 /* Fill in the member function field info. */
11096 fnp = &new_fnfield->fnfield;
3da10d80
KS
11097
11098 /* Delay processing of the physname until later. */
11099 if (cu->language == language_cplus || cu->language == language_java)
11100 {
11101 add_to_method_list (type, i, flp->length - 1, fieldname,
11102 die, cu);
11103 }
11104 else
11105 {
1d06ead6 11106 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11107 fnp->physname = physname ? physname : "";
11108 }
11109
c906108c 11110 fnp->type = alloc_type (objfile);
f792889a
DJ
11111 this_type = read_type_die (die, cu);
11112 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11113 {
f792889a 11114 int nparams = TYPE_NFIELDS (this_type);
c906108c 11115
f792889a 11116 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11117 of the method itself (TYPE_CODE_METHOD). */
11118 smash_to_method_type (fnp->type, type,
f792889a
DJ
11119 TYPE_TARGET_TYPE (this_type),
11120 TYPE_FIELDS (this_type),
11121 TYPE_NFIELDS (this_type),
11122 TYPE_VARARGS (this_type));
c906108c
SS
11123
11124 /* Handle static member functions.
c5aa993b 11125 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11126 member functions. G++ helps GDB by marking the first
11127 parameter for non-static member functions (which is the this
11128 pointer) as artificial. We obtain this information from
11129 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11130 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11131 fnp->voffset = VOFFSET_STATIC;
11132 }
11133 else
e2e0b3e5 11134 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11135 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11136
11137 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11138 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11139 fnp->fcontext = die_containing_type (die, cu);
c906108c 11140
3e43a32a
MS
11141 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11142 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11143
11144 /* Get accessibility. */
e142c38c 11145 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11146 if (attr)
60d5a603
JK
11147 accessibility = DW_UNSND (attr);
11148 else
11149 accessibility = dwarf2_default_access_attribute (die, cu);
11150 switch (accessibility)
c906108c 11151 {
60d5a603
JK
11152 case DW_ACCESS_private:
11153 fnp->is_private = 1;
11154 break;
11155 case DW_ACCESS_protected:
11156 fnp->is_protected = 1;
11157 break;
c906108c
SS
11158 }
11159
b02dede2 11160 /* Check for artificial methods. */
e142c38c 11161 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11162 if (attr && DW_UNSND (attr) != 0)
11163 fnp->is_artificial = 1;
11164
7d27a96d
TT
11165 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11166
0d564a31 11167 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11168 function. For older versions of GCC, this is an offset in the
11169 appropriate virtual table, as specified by DW_AT_containing_type.
11170 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11171 to the object address. */
11172
e142c38c 11173 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11174 if (attr)
8e19ed76 11175 {
aec5aa8b 11176 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11177 {
aec5aa8b
TT
11178 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11179 {
11180 /* Old-style GCC. */
11181 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11182 }
11183 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11184 || (DW_BLOCK (attr)->size > 1
11185 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11186 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11187 {
11188 struct dwarf_block blk;
11189 int offset;
11190
11191 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11192 ? 1 : 2);
11193 blk.size = DW_BLOCK (attr)->size - offset;
11194 blk.data = DW_BLOCK (attr)->data + offset;
11195 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11196 if ((fnp->voffset % cu->header.addr_size) != 0)
11197 dwarf2_complex_location_expr_complaint ();
11198 else
11199 fnp->voffset /= cu->header.addr_size;
11200 fnp->voffset += 2;
11201 }
11202 else
11203 dwarf2_complex_location_expr_complaint ();
11204
11205 if (!fnp->fcontext)
11206 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11207 }
3690dd37 11208 else if (attr_form_is_section_offset (attr))
8e19ed76 11209 {
4d3c2250 11210 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11211 }
11212 else
11213 {
4d3c2250
KB
11214 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11215 fieldname);
8e19ed76 11216 }
0d564a31 11217 }
d48cc9dd
DJ
11218 else
11219 {
11220 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11221 if (attr && DW_UNSND (attr))
11222 {
11223 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11224 complaint (&symfile_complaints,
3e43a32a
MS
11225 _("Member function \"%s\" (offset %d) is virtual "
11226 "but the vtable offset is not specified"),
b64f50a1 11227 fieldname, die->offset.sect_off);
9655fd1a 11228 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11229 TYPE_CPLUS_DYNAMIC (type) = 1;
11230 }
11231 }
c906108c
SS
11232}
11233
11234/* Create the vector of member function fields, and attach it to the type. */
11235
11236static void
fba45db2 11237dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11238 struct dwarf2_cu *cu)
c906108c
SS
11239{
11240 struct fnfieldlist *flp;
c906108c
SS
11241 int i;
11242
b4ba55a1 11243 if (cu->language == language_ada)
a73c6dcd 11244 error (_("unexpected member functions in Ada type"));
b4ba55a1 11245
c906108c
SS
11246 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11247 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11248 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11249
11250 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11251 {
11252 struct nextfnfield *nfp = flp->head;
11253 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11254 int k;
11255
11256 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11257 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11258 fn_flp->fn_fields = (struct fn_field *)
11259 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11260 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11261 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11262 }
11263
11264 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11265}
11266
1168df01
JB
11267/* Returns non-zero if NAME is the name of a vtable member in CU's
11268 language, zero otherwise. */
11269static int
11270is_vtable_name (const char *name, struct dwarf2_cu *cu)
11271{
11272 static const char vptr[] = "_vptr";
987504bb 11273 static const char vtable[] = "vtable";
1168df01 11274
987504bb
JJ
11275 /* Look for the C++ and Java forms of the vtable. */
11276 if ((cu->language == language_java
11277 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11278 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11279 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11280 return 1;
11281
11282 return 0;
11283}
11284
c0dd20ea 11285/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11286 functions, with the ABI-specified layout. If TYPE describes
11287 such a structure, smash it into a member function type.
61049d3b
DJ
11288
11289 GCC shouldn't do this; it should just output pointer to member DIEs.
11290 This is GCC PR debug/28767. */
c0dd20ea 11291
0b92b5bb
TT
11292static void
11293quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11294{
0b92b5bb 11295 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11296
11297 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11298 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11299 return;
c0dd20ea
DJ
11300
11301 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11302 if (TYPE_FIELD_NAME (type, 0) == NULL
11303 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11304 || TYPE_FIELD_NAME (type, 1) == NULL
11305 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11306 return;
c0dd20ea
DJ
11307
11308 /* Find the type of the method. */
0b92b5bb 11309 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11310 if (pfn_type == NULL
11311 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11312 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11313 return;
c0dd20ea
DJ
11314
11315 /* Look for the "this" argument. */
11316 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11317 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11318 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11319 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11320 return;
c0dd20ea
DJ
11321
11322 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11323 new_type = alloc_type (objfile);
11324 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11325 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11326 TYPE_VARARGS (pfn_type));
0b92b5bb 11327 smash_to_methodptr_type (type, new_type);
c0dd20ea 11328}
1168df01 11329
685b1105
JK
11330/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11331 (icc). */
11332
11333static int
11334producer_is_icc (struct dwarf2_cu *cu)
11335{
11336 if (!cu->checked_producer)
11337 check_producer (cu);
11338
11339 return cu->producer_is_icc;
11340}
11341
c906108c 11342/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11343 (definition) to create a type for the structure or union. Fill in
11344 the type's name and general properties; the members will not be
3d1d5ea3 11345 processed until process_structure_scope.
c906108c 11346
c767944b
DJ
11347 NOTE: we need to call these functions regardless of whether or not the
11348 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11349 structure or union. This gets the type entered into our set of
11350 user defined types.
11351
11352 However, if the structure is incomplete (an opaque struct/union)
11353 then suppress creating a symbol table entry for it since gdb only
11354 wants to find the one with the complete definition. Note that if
11355 it is complete, we just call new_symbol, which does it's own
11356 checking about whether the struct/union is anonymous or not (and
11357 suppresses creating a symbol table entry itself). */
11358
f792889a 11359static struct type *
134d01f1 11360read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11361{
e7c27a73 11362 struct objfile *objfile = cu->objfile;
c906108c
SS
11363 struct type *type;
11364 struct attribute *attr;
15d034d0 11365 const char *name;
c906108c 11366
348e048f
DE
11367 /* If the definition of this type lives in .debug_types, read that type.
11368 Don't follow DW_AT_specification though, that will take us back up
11369 the chain and we want to go down. */
45e58e77 11370 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11371 if (attr)
11372 {
11373 struct dwarf2_cu *type_cu = cu;
11374 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11375
348e048f
DE
11376 /* We could just recurse on read_structure_type, but we need to call
11377 get_die_type to ensure only one type for this DIE is created.
11378 This is important, for example, because for c++ classes we need
11379 TYPE_NAME set which is only done by new_symbol. Blech. */
11380 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11381
11382 /* TYPE_CU may not be the same as CU.
02142a6c 11383 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11384 return set_die_type (die, type, cu);
11385 }
11386
c0dd20ea 11387 type = alloc_type (objfile);
c906108c 11388 INIT_CPLUS_SPECIFIC (type);
93311388 11389
39cbfefa
DJ
11390 name = dwarf2_name (die, cu);
11391 if (name != NULL)
c906108c 11392 {
987504bb
JJ
11393 if (cu->language == language_cplus
11394 || cu->language == language_java)
63d06c5c 11395 {
15d034d0 11396 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11397
11398 /* dwarf2_full_name might have already finished building the DIE's
11399 type. If so, there is no need to continue. */
11400 if (get_die_type (die, cu) != NULL)
11401 return get_die_type (die, cu);
11402
11403 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11404 if (die->tag == DW_TAG_structure_type
11405 || die->tag == DW_TAG_class_type)
11406 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11407 }
11408 else
11409 {
d8151005
DJ
11410 /* The name is already allocated along with this objfile, so
11411 we don't need to duplicate it for the type. */
7d455152 11412 TYPE_TAG_NAME (type) = name;
94af9270
KS
11413 if (die->tag == DW_TAG_class_type)
11414 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11415 }
c906108c
SS
11416 }
11417
11418 if (die->tag == DW_TAG_structure_type)
11419 {
11420 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11421 }
11422 else if (die->tag == DW_TAG_union_type)
11423 {
11424 TYPE_CODE (type) = TYPE_CODE_UNION;
11425 }
11426 else
11427 {
c906108c
SS
11428 TYPE_CODE (type) = TYPE_CODE_CLASS;
11429 }
11430
0cc2414c
TT
11431 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11432 TYPE_DECLARED_CLASS (type) = 1;
11433
e142c38c 11434 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11435 if (attr)
11436 {
11437 TYPE_LENGTH (type) = DW_UNSND (attr);
11438 }
11439 else
11440 {
11441 TYPE_LENGTH (type) = 0;
11442 }
11443
685b1105
JK
11444 if (producer_is_icc (cu))
11445 {
11446 /* ICC does not output the required DW_AT_declaration
11447 on incomplete types, but gives them a size of zero. */
11448 }
11449 else
11450 TYPE_STUB_SUPPORTED (type) = 1;
11451
dc718098 11452 if (die_is_declaration (die, cu))
876cecd0 11453 TYPE_STUB (type) = 1;
a6c727b2
DJ
11454 else if (attr == NULL && die->child == NULL
11455 && producer_is_realview (cu->producer))
11456 /* RealView does not output the required DW_AT_declaration
11457 on incomplete types. */
11458 TYPE_STUB (type) = 1;
dc718098 11459
c906108c
SS
11460 /* We need to add the type field to the die immediately so we don't
11461 infinitely recurse when dealing with pointers to the structure
0963b4bd 11462 type within the structure itself. */
1c379e20 11463 set_die_type (die, type, cu);
c906108c 11464
7e314c57
JK
11465 /* set_die_type should be already done. */
11466 set_descriptive_type (type, die, cu);
11467
c767944b
DJ
11468 return type;
11469}
11470
11471/* Finish creating a structure or union type, including filling in
11472 its members and creating a symbol for it. */
11473
11474static void
11475process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11476{
11477 struct objfile *objfile = cu->objfile;
11478 struct die_info *child_die = die->child;
11479 struct type *type;
11480
11481 type = get_die_type (die, cu);
11482 if (type == NULL)
11483 type = read_structure_type (die, cu);
11484
e142c38c 11485 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11486 {
11487 struct field_info fi;
11488 struct die_info *child_die;
34eaf542 11489 VEC (symbolp) *template_args = NULL;
c767944b 11490 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11491
11492 memset (&fi, 0, sizeof (struct field_info));
11493
639d11d3 11494 child_die = die->child;
c906108c
SS
11495
11496 while (child_die && child_die->tag)
11497 {
a9a9bd0f
DC
11498 if (child_die->tag == DW_TAG_member
11499 || child_die->tag == DW_TAG_variable)
c906108c 11500 {
a9a9bd0f
DC
11501 /* NOTE: carlton/2002-11-05: A C++ static data member
11502 should be a DW_TAG_member that is a declaration, but
11503 all versions of G++ as of this writing (so through at
11504 least 3.2.1) incorrectly generate DW_TAG_variable
11505 tags for them instead. */
e7c27a73 11506 dwarf2_add_field (&fi, child_die, cu);
c906108c 11507 }
8713b1b1 11508 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11509 {
0963b4bd 11510 /* C++ member function. */
e7c27a73 11511 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11512 }
11513 else if (child_die->tag == DW_TAG_inheritance)
11514 {
11515 /* C++ base class field. */
e7c27a73 11516 dwarf2_add_field (&fi, child_die, cu);
c906108c 11517 }
98751a41
JK
11518 else if (child_die->tag == DW_TAG_typedef)
11519 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11520 else if (child_die->tag == DW_TAG_template_type_param
11521 || child_die->tag == DW_TAG_template_value_param)
11522 {
11523 struct symbol *arg = new_symbol (child_die, NULL, cu);
11524
f1078f66
DJ
11525 if (arg != NULL)
11526 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11527 }
11528
c906108c
SS
11529 child_die = sibling_die (child_die);
11530 }
11531
34eaf542
TT
11532 /* Attach template arguments to type. */
11533 if (! VEC_empty (symbolp, template_args))
11534 {
11535 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11536 TYPE_N_TEMPLATE_ARGUMENTS (type)
11537 = VEC_length (symbolp, template_args);
11538 TYPE_TEMPLATE_ARGUMENTS (type)
11539 = obstack_alloc (&objfile->objfile_obstack,
11540 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11541 * sizeof (struct symbol *)));
11542 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11543 VEC_address (symbolp, template_args),
11544 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11545 * sizeof (struct symbol *)));
11546 VEC_free (symbolp, template_args);
11547 }
11548
c906108c
SS
11549 /* Attach fields and member functions to the type. */
11550 if (fi.nfields)
e7c27a73 11551 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11552 if (fi.nfnfields)
11553 {
e7c27a73 11554 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11555
c5aa993b 11556 /* Get the type which refers to the base class (possibly this
c906108c 11557 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11558 class from the DW_AT_containing_type attribute. This use of
11559 DW_AT_containing_type is a GNU extension. */
c906108c 11560
e142c38c 11561 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11562 {
e7c27a73 11563 struct type *t = die_containing_type (die, cu);
c906108c
SS
11564
11565 TYPE_VPTR_BASETYPE (type) = t;
11566 if (type == t)
11567 {
c906108c
SS
11568 int i;
11569
11570 /* Our own class provides vtbl ptr. */
11571 for (i = TYPE_NFIELDS (t) - 1;
11572 i >= TYPE_N_BASECLASSES (t);
11573 --i)
11574 {
0d5cff50 11575 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11576
1168df01 11577 if (is_vtable_name (fieldname, cu))
c906108c
SS
11578 {
11579 TYPE_VPTR_FIELDNO (type) = i;
11580 break;
11581 }
11582 }
11583
11584 /* Complain if virtual function table field not found. */
11585 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11586 complaint (&symfile_complaints,
3e43a32a
MS
11587 _("virtual function table pointer "
11588 "not found when defining class '%s'"),
4d3c2250
KB
11589 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11590 "");
c906108c
SS
11591 }
11592 else
11593 {
11594 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11595 }
11596 }
f6235d4c
EZ
11597 else if (cu->producer
11598 && strncmp (cu->producer,
11599 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11600 {
11601 /* The IBM XLC compiler does not provide direct indication
11602 of the containing type, but the vtable pointer is
11603 always named __vfp. */
11604
11605 int i;
11606
11607 for (i = TYPE_NFIELDS (type) - 1;
11608 i >= TYPE_N_BASECLASSES (type);
11609 --i)
11610 {
11611 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11612 {
11613 TYPE_VPTR_FIELDNO (type) = i;
11614 TYPE_VPTR_BASETYPE (type) = type;
11615 break;
11616 }
11617 }
11618 }
c906108c 11619 }
98751a41
JK
11620
11621 /* Copy fi.typedef_field_list linked list elements content into the
11622 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11623 if (fi.typedef_field_list)
11624 {
11625 int i = fi.typedef_field_list_count;
11626
a0d7a4ff 11627 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11628 TYPE_TYPEDEF_FIELD_ARRAY (type)
11629 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11630 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11631
11632 /* Reverse the list order to keep the debug info elements order. */
11633 while (--i >= 0)
11634 {
11635 struct typedef_field *dest, *src;
6e70227d 11636
98751a41
JK
11637 dest = &TYPE_TYPEDEF_FIELD (type, i);
11638 src = &fi.typedef_field_list->field;
11639 fi.typedef_field_list = fi.typedef_field_list->next;
11640 *dest = *src;
11641 }
11642 }
c767944b
DJ
11643
11644 do_cleanups (back_to);
eb2a6f42
TT
11645
11646 if (HAVE_CPLUS_STRUCT (type))
11647 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11648 }
63d06c5c 11649
bb5ed363 11650 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11651
90aeadfc
DC
11652 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11653 snapshots) has been known to create a die giving a declaration
11654 for a class that has, as a child, a die giving a definition for a
11655 nested class. So we have to process our children even if the
11656 current die is a declaration. Normally, of course, a declaration
11657 won't have any children at all. */
134d01f1 11658
90aeadfc
DC
11659 while (child_die != NULL && child_die->tag)
11660 {
11661 if (child_die->tag == DW_TAG_member
11662 || child_die->tag == DW_TAG_variable
34eaf542
TT
11663 || child_die->tag == DW_TAG_inheritance
11664 || child_die->tag == DW_TAG_template_value_param
11665 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11666 {
90aeadfc 11667 /* Do nothing. */
134d01f1 11668 }
90aeadfc
DC
11669 else
11670 process_die (child_die, cu);
134d01f1 11671
90aeadfc 11672 child_die = sibling_die (child_die);
134d01f1
DJ
11673 }
11674
fa4028e9
JB
11675 /* Do not consider external references. According to the DWARF standard,
11676 these DIEs are identified by the fact that they have no byte_size
11677 attribute, and a declaration attribute. */
11678 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11679 || !die_is_declaration (die, cu))
c767944b 11680 new_symbol (die, type, cu);
134d01f1
DJ
11681}
11682
11683/* Given a DW_AT_enumeration_type die, set its type. We do not
11684 complete the type's fields yet, or create any symbols. */
c906108c 11685
f792889a 11686static struct type *
134d01f1 11687read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11688{
e7c27a73 11689 struct objfile *objfile = cu->objfile;
c906108c 11690 struct type *type;
c906108c 11691 struct attribute *attr;
0114d602 11692 const char *name;
134d01f1 11693
348e048f
DE
11694 /* If the definition of this type lives in .debug_types, read that type.
11695 Don't follow DW_AT_specification though, that will take us back up
11696 the chain and we want to go down. */
45e58e77 11697 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11698 if (attr)
11699 {
11700 struct dwarf2_cu *type_cu = cu;
11701 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11702
348e048f 11703 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11704
11705 /* TYPE_CU may not be the same as CU.
02142a6c 11706 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11707 return set_die_type (die, type, cu);
11708 }
11709
c906108c
SS
11710 type = alloc_type (objfile);
11711
11712 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11713 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11714 if (name != NULL)
7d455152 11715 TYPE_TAG_NAME (type) = name;
c906108c 11716
e142c38c 11717 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11718 if (attr)
11719 {
11720 TYPE_LENGTH (type) = DW_UNSND (attr);
11721 }
11722 else
11723 {
11724 TYPE_LENGTH (type) = 0;
11725 }
11726
137033e9
JB
11727 /* The enumeration DIE can be incomplete. In Ada, any type can be
11728 declared as private in the package spec, and then defined only
11729 inside the package body. Such types are known as Taft Amendment
11730 Types. When another package uses such a type, an incomplete DIE
11731 may be generated by the compiler. */
02eb380e 11732 if (die_is_declaration (die, cu))
876cecd0 11733 TYPE_STUB (type) = 1;
02eb380e 11734
f792889a 11735 return set_die_type (die, type, cu);
134d01f1
DJ
11736}
11737
11738/* Given a pointer to a die which begins an enumeration, process all
11739 the dies that define the members of the enumeration, and create the
11740 symbol for the enumeration type.
11741
11742 NOTE: We reverse the order of the element list. */
11743
11744static void
11745process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11746{
f792889a 11747 struct type *this_type;
134d01f1 11748
f792889a
DJ
11749 this_type = get_die_type (die, cu);
11750 if (this_type == NULL)
11751 this_type = read_enumeration_type (die, cu);
9dc481d3 11752
639d11d3 11753 if (die->child != NULL)
c906108c 11754 {
9dc481d3
DE
11755 struct die_info *child_die;
11756 struct symbol *sym;
11757 struct field *fields = NULL;
11758 int num_fields = 0;
11759 int unsigned_enum = 1;
15d034d0 11760 const char *name;
cafec441
TT
11761 int flag_enum = 1;
11762 ULONGEST mask = 0;
9dc481d3 11763
639d11d3 11764 child_die = die->child;
c906108c
SS
11765 while (child_die && child_die->tag)
11766 {
11767 if (child_die->tag != DW_TAG_enumerator)
11768 {
e7c27a73 11769 process_die (child_die, cu);
c906108c
SS
11770 }
11771 else
11772 {
39cbfefa
DJ
11773 name = dwarf2_name (child_die, cu);
11774 if (name)
c906108c 11775 {
f792889a 11776 sym = new_symbol (child_die, this_type, cu);
c906108c 11777 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11778 {
11779 unsigned_enum = 0;
11780 flag_enum = 0;
11781 }
11782 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11783 flag_enum = 0;
11784 else
11785 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11786
11787 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11788 {
11789 fields = (struct field *)
11790 xrealloc (fields,
11791 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11792 * sizeof (struct field));
c906108c
SS
11793 }
11794
3567439c 11795 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11796 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11797 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11798 FIELD_BITSIZE (fields[num_fields]) = 0;
11799
11800 num_fields++;
11801 }
11802 }
11803
11804 child_die = sibling_die (child_die);
11805 }
11806
11807 if (num_fields)
11808 {
f792889a
DJ
11809 TYPE_NFIELDS (this_type) = num_fields;
11810 TYPE_FIELDS (this_type) = (struct field *)
11811 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11812 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11813 sizeof (struct field) * num_fields);
b8c9b27d 11814 xfree (fields);
c906108c
SS
11815 }
11816 if (unsigned_enum)
876cecd0 11817 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11818 if (flag_enum)
11819 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11820 }
134d01f1 11821
6c83ed52
TT
11822 /* If we are reading an enum from a .debug_types unit, and the enum
11823 is a declaration, and the enum is not the signatured type in the
11824 unit, then we do not want to add a symbol for it. Adding a
11825 symbol would in some cases obscure the true definition of the
11826 enum, giving users an incomplete type when the definition is
11827 actually available. Note that we do not want to do this for all
11828 enums which are just declarations, because C++0x allows forward
11829 enum declarations. */
3019eac3 11830 if (cu->per_cu->is_debug_types
6c83ed52
TT
11831 && die_is_declaration (die, cu))
11832 {
52dc124a 11833 struct signatured_type *sig_type;
6c83ed52 11834
c0f78cd4 11835 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
11836 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11837 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11838 return;
11839 }
11840
f792889a 11841 new_symbol (die, this_type, cu);
c906108c
SS
11842}
11843
11844/* Extract all information from a DW_TAG_array_type DIE and put it in
11845 the DIE's type field. For now, this only handles one dimensional
11846 arrays. */
11847
f792889a 11848static struct type *
e7c27a73 11849read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11850{
e7c27a73 11851 struct objfile *objfile = cu->objfile;
c906108c 11852 struct die_info *child_die;
7e314c57 11853 struct type *type;
c906108c
SS
11854 struct type *element_type, *range_type, *index_type;
11855 struct type **range_types = NULL;
11856 struct attribute *attr;
11857 int ndim = 0;
11858 struct cleanup *back_to;
15d034d0 11859 const char *name;
c906108c 11860
e7c27a73 11861 element_type = die_type (die, cu);
c906108c 11862
7e314c57
JK
11863 /* The die_type call above may have already set the type for this DIE. */
11864 type = get_die_type (die, cu);
11865 if (type)
11866 return type;
11867
c906108c
SS
11868 /* Irix 6.2 native cc creates array types without children for
11869 arrays with unspecified length. */
639d11d3 11870 if (die->child == NULL)
c906108c 11871 {
46bf5051 11872 index_type = objfile_type (objfile)->builtin_int;
c906108c 11873 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11874 type = create_array_type (NULL, element_type, range_type);
11875 return set_die_type (die, type, cu);
c906108c
SS
11876 }
11877
11878 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11879 child_die = die->child;
c906108c
SS
11880 while (child_die && child_die->tag)
11881 {
11882 if (child_die->tag == DW_TAG_subrange_type)
11883 {
f792889a 11884 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11885
f792889a 11886 if (child_type != NULL)
a02abb62 11887 {
0963b4bd
MS
11888 /* The range type was succesfully read. Save it for the
11889 array type creation. */
a02abb62
JB
11890 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11891 {
11892 range_types = (struct type **)
11893 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11894 * sizeof (struct type *));
11895 if (ndim == 0)
11896 make_cleanup (free_current_contents, &range_types);
11897 }
f792889a 11898 range_types[ndim++] = child_type;
a02abb62 11899 }
c906108c
SS
11900 }
11901 child_die = sibling_die (child_die);
11902 }
11903
11904 /* Dwarf2 dimensions are output from left to right, create the
11905 necessary array types in backwards order. */
7ca2d3a3 11906
c906108c 11907 type = element_type;
7ca2d3a3
DL
11908
11909 if (read_array_order (die, cu) == DW_ORD_col_major)
11910 {
11911 int i = 0;
9a619af0 11912
7ca2d3a3
DL
11913 while (i < ndim)
11914 type = create_array_type (NULL, type, range_types[i++]);
11915 }
11916 else
11917 {
11918 while (ndim-- > 0)
11919 type = create_array_type (NULL, type, range_types[ndim]);
11920 }
c906108c 11921
f5f8a009
EZ
11922 /* Understand Dwarf2 support for vector types (like they occur on
11923 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11924 array type. This is not part of the Dwarf2/3 standard yet, but a
11925 custom vendor extension. The main difference between a regular
11926 array and the vector variant is that vectors are passed by value
11927 to functions. */
e142c38c 11928 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11929 if (attr)
ea37ba09 11930 make_vector_type (type);
f5f8a009 11931
dbc98a8b
KW
11932 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11933 implementation may choose to implement triple vectors using this
11934 attribute. */
11935 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11936 if (attr)
11937 {
11938 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11939 TYPE_LENGTH (type) = DW_UNSND (attr);
11940 else
3e43a32a
MS
11941 complaint (&symfile_complaints,
11942 _("DW_AT_byte_size for array type smaller "
11943 "than the total size of elements"));
dbc98a8b
KW
11944 }
11945
39cbfefa
DJ
11946 name = dwarf2_name (die, cu);
11947 if (name)
11948 TYPE_NAME (type) = name;
6e70227d 11949
0963b4bd 11950 /* Install the type in the die. */
7e314c57
JK
11951 set_die_type (die, type, cu);
11952
11953 /* set_die_type should be already done. */
b4ba55a1
JB
11954 set_descriptive_type (type, die, cu);
11955
c906108c
SS
11956 do_cleanups (back_to);
11957
7e314c57 11958 return type;
c906108c
SS
11959}
11960
7ca2d3a3 11961static enum dwarf_array_dim_ordering
6e70227d 11962read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11963{
11964 struct attribute *attr;
11965
11966 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11967
11968 if (attr) return DW_SND (attr);
11969
0963b4bd
MS
11970 /* GNU F77 is a special case, as at 08/2004 array type info is the
11971 opposite order to the dwarf2 specification, but data is still
11972 laid out as per normal fortran.
7ca2d3a3 11973
0963b4bd
MS
11974 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11975 version checking. */
7ca2d3a3 11976
905e0470
PM
11977 if (cu->language == language_fortran
11978 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11979 {
11980 return DW_ORD_row_major;
11981 }
11982
6e70227d 11983 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11984 {
11985 case array_column_major:
11986 return DW_ORD_col_major;
11987 case array_row_major:
11988 default:
11989 return DW_ORD_row_major;
11990 };
11991}
11992
72019c9c 11993/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11994 the DIE's type field. */
72019c9c 11995
f792889a 11996static struct type *
72019c9c
GM
11997read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11998{
7e314c57
JK
11999 struct type *domain_type, *set_type;
12000 struct attribute *attr;
f792889a 12001
7e314c57
JK
12002 domain_type = die_type (die, cu);
12003
12004 /* The die_type call above may have already set the type for this DIE. */
12005 set_type = get_die_type (die, cu);
12006 if (set_type)
12007 return set_type;
12008
12009 set_type = create_set_type (NULL, domain_type);
12010
12011 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12012 if (attr)
12013 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12014
f792889a 12015 return set_die_type (die, set_type, cu);
72019c9c 12016}
7ca2d3a3 12017
0971de02
TT
12018/* A helper for read_common_block that creates a locexpr baton.
12019 SYM is the symbol which we are marking as computed.
12020 COMMON_DIE is the DIE for the common block.
12021 COMMON_LOC is the location expression attribute for the common
12022 block itself.
12023 MEMBER_LOC is the location expression attribute for the particular
12024 member of the common block that we are processing.
12025 CU is the CU from which the above come. */
12026
12027static void
12028mark_common_block_symbol_computed (struct symbol *sym,
12029 struct die_info *common_die,
12030 struct attribute *common_loc,
12031 struct attribute *member_loc,
12032 struct dwarf2_cu *cu)
12033{
12034 struct objfile *objfile = dwarf2_per_objfile->objfile;
12035 struct dwarf2_locexpr_baton *baton;
12036 gdb_byte *ptr;
12037 unsigned int cu_off;
12038 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12039 LONGEST offset = 0;
12040
12041 gdb_assert (common_loc && member_loc);
12042 gdb_assert (attr_form_is_block (common_loc));
12043 gdb_assert (attr_form_is_block (member_loc)
12044 || attr_form_is_constant (member_loc));
12045
12046 baton = obstack_alloc (&objfile->objfile_obstack,
12047 sizeof (struct dwarf2_locexpr_baton));
12048 baton->per_cu = cu->per_cu;
12049 gdb_assert (baton->per_cu);
12050
12051 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12052
12053 if (attr_form_is_constant (member_loc))
12054 {
12055 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12056 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12057 }
12058 else
12059 baton->size += DW_BLOCK (member_loc)->size;
12060
12061 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12062 baton->data = ptr;
12063
12064 *ptr++ = DW_OP_call4;
12065 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12066 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12067 ptr += 4;
12068
12069 if (attr_form_is_constant (member_loc))
12070 {
12071 *ptr++ = DW_OP_addr;
12072 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12073 ptr += cu->header.addr_size;
12074 }
12075 else
12076 {
12077 /* We have to copy the data here, because DW_OP_call4 will only
12078 use a DW_AT_location attribute. */
12079 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12080 ptr += DW_BLOCK (member_loc)->size;
12081 }
12082
12083 *ptr++ = DW_OP_plus;
12084 gdb_assert (ptr - baton->data == baton->size);
12085
0971de02 12086 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12087 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12088}
12089
4357ac6c
TT
12090/* Create appropriate locally-scoped variables for all the
12091 DW_TAG_common_block entries. Also create a struct common_block
12092 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12093 is used to sepate the common blocks name namespace from regular
12094 variable names. */
c906108c
SS
12095
12096static void
e7c27a73 12097read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12098{
0971de02
TT
12099 struct attribute *attr;
12100
12101 attr = dwarf2_attr (die, DW_AT_location, cu);
12102 if (attr)
12103 {
12104 /* Support the .debug_loc offsets. */
12105 if (attr_form_is_block (attr))
12106 {
12107 /* Ok. */
12108 }
12109 else if (attr_form_is_section_offset (attr))
12110 {
12111 dwarf2_complex_location_expr_complaint ();
12112 attr = NULL;
12113 }
12114 else
12115 {
12116 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12117 "common block member");
12118 attr = NULL;
12119 }
12120 }
12121
639d11d3 12122 if (die->child != NULL)
c906108c 12123 {
4357ac6c
TT
12124 struct objfile *objfile = cu->objfile;
12125 struct die_info *child_die;
12126 size_t n_entries = 0, size;
12127 struct common_block *common_block;
12128 struct symbol *sym;
74ac6d43 12129
4357ac6c
TT
12130 for (child_die = die->child;
12131 child_die && child_die->tag;
12132 child_die = sibling_die (child_die))
12133 ++n_entries;
12134
12135 size = (sizeof (struct common_block)
12136 + (n_entries - 1) * sizeof (struct symbol *));
12137 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12138 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12139 common_block->n_entries = 0;
12140
12141 for (child_die = die->child;
12142 child_die && child_die->tag;
12143 child_die = sibling_die (child_die))
12144 {
12145 /* Create the symbol in the DW_TAG_common_block block in the current
12146 symbol scope. */
e7c27a73 12147 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12148 if (sym != NULL)
12149 {
12150 struct attribute *member_loc;
12151
12152 common_block->contents[common_block->n_entries++] = sym;
12153
12154 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12155 cu);
12156 if (member_loc)
12157 {
12158 /* GDB has handled this for a long time, but it is
12159 not specified by DWARF. It seems to have been
12160 emitted by gfortran at least as recently as:
12161 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12162 complaint (&symfile_complaints,
12163 _("Variable in common block has "
12164 "DW_AT_data_member_location "
12165 "- DIE at 0x%x [in module %s]"),
12166 child_die->offset.sect_off, cu->objfile->name);
12167
12168 if (attr_form_is_section_offset (member_loc))
12169 dwarf2_complex_location_expr_complaint ();
12170 else if (attr_form_is_constant (member_loc)
12171 || attr_form_is_block (member_loc))
12172 {
12173 if (attr)
12174 mark_common_block_symbol_computed (sym, die, attr,
12175 member_loc, cu);
12176 }
12177 else
12178 dwarf2_complex_location_expr_complaint ();
12179 }
12180 }
c906108c 12181 }
4357ac6c
TT
12182
12183 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12184 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12185 }
12186}
12187
0114d602 12188/* Create a type for a C++ namespace. */
d9fa45fe 12189
0114d602
DJ
12190static struct type *
12191read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12192{
e7c27a73 12193 struct objfile *objfile = cu->objfile;
0114d602 12194 const char *previous_prefix, *name;
9219021c 12195 int is_anonymous;
0114d602
DJ
12196 struct type *type;
12197
12198 /* For extensions, reuse the type of the original namespace. */
12199 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12200 {
12201 struct die_info *ext_die;
12202 struct dwarf2_cu *ext_cu = cu;
9a619af0 12203
0114d602
DJ
12204 ext_die = dwarf2_extension (die, &ext_cu);
12205 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12206
12207 /* EXT_CU may not be the same as CU.
02142a6c 12208 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12209 return set_die_type (die, type, cu);
12210 }
9219021c 12211
e142c38c 12212 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12213
12214 /* Now build the name of the current namespace. */
12215
0114d602
DJ
12216 previous_prefix = determine_prefix (die, cu);
12217 if (previous_prefix[0] != '\0')
12218 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12219 previous_prefix, name, 0, cu);
0114d602
DJ
12220
12221 /* Create the type. */
12222 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12223 objfile);
abee88f2 12224 TYPE_NAME (type) = name;
0114d602
DJ
12225 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12226
60531b24 12227 return set_die_type (die, type, cu);
0114d602
DJ
12228}
12229
12230/* Read a C++ namespace. */
12231
12232static void
12233read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12234{
12235 struct objfile *objfile = cu->objfile;
0114d602 12236 int is_anonymous;
9219021c 12237
5c4e30ca
DC
12238 /* Add a symbol associated to this if we haven't seen the namespace
12239 before. Also, add a using directive if it's an anonymous
12240 namespace. */
9219021c 12241
f2f0e013 12242 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12243 {
12244 struct type *type;
12245
0114d602 12246 type = read_type_die (die, cu);
e7c27a73 12247 new_symbol (die, type, cu);
5c4e30ca 12248
e8e80198 12249 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12250 if (is_anonymous)
0114d602
DJ
12251 {
12252 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12253
c0cc3a76 12254 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12255 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12256 }
5c4e30ca 12257 }
9219021c 12258
639d11d3 12259 if (die->child != NULL)
d9fa45fe 12260 {
639d11d3 12261 struct die_info *child_die = die->child;
6e70227d 12262
d9fa45fe
DC
12263 while (child_die && child_die->tag)
12264 {
e7c27a73 12265 process_die (child_die, cu);
d9fa45fe
DC
12266 child_die = sibling_die (child_die);
12267 }
12268 }
38d518c9
EZ
12269}
12270
f55ee35c
JK
12271/* Read a Fortran module as type. This DIE can be only a declaration used for
12272 imported module. Still we need that type as local Fortran "use ... only"
12273 declaration imports depend on the created type in determine_prefix. */
12274
12275static struct type *
12276read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12277{
12278 struct objfile *objfile = cu->objfile;
15d034d0 12279 const char *module_name;
f55ee35c
JK
12280 struct type *type;
12281
12282 module_name = dwarf2_name (die, cu);
12283 if (!module_name)
3e43a32a
MS
12284 complaint (&symfile_complaints,
12285 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12286 die->offset.sect_off);
f55ee35c
JK
12287 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12288
12289 /* determine_prefix uses TYPE_TAG_NAME. */
12290 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12291
12292 return set_die_type (die, type, cu);
12293}
12294
5d7cb8df
JK
12295/* Read a Fortran module. */
12296
12297static void
12298read_module (struct die_info *die, struct dwarf2_cu *cu)
12299{
12300 struct die_info *child_die = die->child;
12301
5d7cb8df
JK
12302 while (child_die && child_die->tag)
12303 {
12304 process_die (child_die, cu);
12305 child_die = sibling_die (child_die);
12306 }
12307}
12308
38d518c9
EZ
12309/* Return the name of the namespace represented by DIE. Set
12310 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12311 namespace. */
12312
12313static const char *
e142c38c 12314namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12315{
12316 struct die_info *current_die;
12317 const char *name = NULL;
12318
12319 /* Loop through the extensions until we find a name. */
12320
12321 for (current_die = die;
12322 current_die != NULL;
f2f0e013 12323 current_die = dwarf2_extension (die, &cu))
38d518c9 12324 {
e142c38c 12325 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12326 if (name != NULL)
12327 break;
12328 }
12329
12330 /* Is it an anonymous namespace? */
12331
12332 *is_anonymous = (name == NULL);
12333 if (*is_anonymous)
2b1dbab0 12334 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12335
12336 return name;
d9fa45fe
DC
12337}
12338
c906108c
SS
12339/* Extract all information from a DW_TAG_pointer_type DIE and add to
12340 the user defined type vector. */
12341
f792889a 12342static struct type *
e7c27a73 12343read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12344{
5e2b427d 12345 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12346 struct comp_unit_head *cu_header = &cu->header;
c906108c 12347 struct type *type;
8b2dbe47
KB
12348 struct attribute *attr_byte_size;
12349 struct attribute *attr_address_class;
12350 int byte_size, addr_class;
7e314c57
JK
12351 struct type *target_type;
12352
12353 target_type = die_type (die, cu);
c906108c 12354
7e314c57
JK
12355 /* The die_type call above may have already set the type for this DIE. */
12356 type = get_die_type (die, cu);
12357 if (type)
12358 return type;
12359
12360 type = lookup_pointer_type (target_type);
8b2dbe47 12361
e142c38c 12362 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12363 if (attr_byte_size)
12364 byte_size = DW_UNSND (attr_byte_size);
c906108c 12365 else
8b2dbe47
KB
12366 byte_size = cu_header->addr_size;
12367
e142c38c 12368 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12369 if (attr_address_class)
12370 addr_class = DW_UNSND (attr_address_class);
12371 else
12372 addr_class = DW_ADDR_none;
12373
12374 /* If the pointer size or address class is different than the
12375 default, create a type variant marked as such and set the
12376 length accordingly. */
12377 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12378 {
5e2b427d 12379 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12380 {
12381 int type_flags;
12382
849957d9 12383 type_flags = gdbarch_address_class_type_flags
5e2b427d 12384 (gdbarch, byte_size, addr_class);
876cecd0
TT
12385 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12386 == 0);
8b2dbe47
KB
12387 type = make_type_with_address_space (type, type_flags);
12388 }
12389 else if (TYPE_LENGTH (type) != byte_size)
12390 {
3e43a32a
MS
12391 complaint (&symfile_complaints,
12392 _("invalid pointer size %d"), byte_size);
8b2dbe47 12393 }
6e70227d 12394 else
9a619af0
MS
12395 {
12396 /* Should we also complain about unhandled address classes? */
12397 }
c906108c 12398 }
8b2dbe47
KB
12399
12400 TYPE_LENGTH (type) = byte_size;
f792889a 12401 return set_die_type (die, type, cu);
c906108c
SS
12402}
12403
12404/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12405 the user defined type vector. */
12406
f792889a 12407static struct type *
e7c27a73 12408read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12409{
12410 struct type *type;
12411 struct type *to_type;
12412 struct type *domain;
12413
e7c27a73
DJ
12414 to_type = die_type (die, cu);
12415 domain = die_containing_type (die, cu);
0d5de010 12416
7e314c57
JK
12417 /* The calls above may have already set the type for this DIE. */
12418 type = get_die_type (die, cu);
12419 if (type)
12420 return type;
12421
0d5de010
DJ
12422 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12423 type = lookup_methodptr_type (to_type);
7078baeb
TT
12424 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12425 {
12426 struct type *new_type = alloc_type (cu->objfile);
12427
12428 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12429 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12430 TYPE_VARARGS (to_type));
12431 type = lookup_methodptr_type (new_type);
12432 }
0d5de010
DJ
12433 else
12434 type = lookup_memberptr_type (to_type, domain);
c906108c 12435
f792889a 12436 return set_die_type (die, type, cu);
c906108c
SS
12437}
12438
12439/* Extract all information from a DW_TAG_reference_type DIE and add to
12440 the user defined type vector. */
12441
f792889a 12442static struct type *
e7c27a73 12443read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12444{
e7c27a73 12445 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12446 struct type *type, *target_type;
c906108c
SS
12447 struct attribute *attr;
12448
7e314c57
JK
12449 target_type = die_type (die, cu);
12450
12451 /* The die_type call above may have already set the type for this DIE. */
12452 type = get_die_type (die, cu);
12453 if (type)
12454 return type;
12455
12456 type = lookup_reference_type (target_type);
e142c38c 12457 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12458 if (attr)
12459 {
12460 TYPE_LENGTH (type) = DW_UNSND (attr);
12461 }
12462 else
12463 {
107d2387 12464 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12465 }
f792889a 12466 return set_die_type (die, type, cu);
c906108c
SS
12467}
12468
f792889a 12469static struct type *
e7c27a73 12470read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12471{
f792889a 12472 struct type *base_type, *cv_type;
c906108c 12473
e7c27a73 12474 base_type = die_type (die, cu);
7e314c57
JK
12475
12476 /* The die_type call above may have already set the type for this DIE. */
12477 cv_type = get_die_type (die, cu);
12478 if (cv_type)
12479 return cv_type;
12480
2f608a3a
KW
12481 /* In case the const qualifier is applied to an array type, the element type
12482 is so qualified, not the array type (section 6.7.3 of C99). */
12483 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12484 {
12485 struct type *el_type, *inner_array;
12486
12487 base_type = copy_type (base_type);
12488 inner_array = base_type;
12489
12490 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12491 {
12492 TYPE_TARGET_TYPE (inner_array) =
12493 copy_type (TYPE_TARGET_TYPE (inner_array));
12494 inner_array = TYPE_TARGET_TYPE (inner_array);
12495 }
12496
12497 el_type = TYPE_TARGET_TYPE (inner_array);
12498 TYPE_TARGET_TYPE (inner_array) =
12499 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12500
12501 return set_die_type (die, base_type, cu);
12502 }
12503
f792889a
DJ
12504 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12505 return set_die_type (die, cv_type, cu);
c906108c
SS
12506}
12507
f792889a 12508static struct type *
e7c27a73 12509read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12510{
f792889a 12511 struct type *base_type, *cv_type;
c906108c 12512
e7c27a73 12513 base_type = die_type (die, cu);
7e314c57
JK
12514
12515 /* The die_type call above may have already set the type for this DIE. */
12516 cv_type = get_die_type (die, cu);
12517 if (cv_type)
12518 return cv_type;
12519
f792889a
DJ
12520 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12521 return set_die_type (die, cv_type, cu);
c906108c
SS
12522}
12523
06d66ee9
TT
12524/* Handle DW_TAG_restrict_type. */
12525
12526static struct type *
12527read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12528{
12529 struct type *base_type, *cv_type;
12530
12531 base_type = die_type (die, cu);
12532
12533 /* The die_type call above may have already set the type for this DIE. */
12534 cv_type = get_die_type (die, cu);
12535 if (cv_type)
12536 return cv_type;
12537
12538 cv_type = make_restrict_type (base_type);
12539 return set_die_type (die, cv_type, cu);
12540}
12541
c906108c
SS
12542/* Extract all information from a DW_TAG_string_type DIE and add to
12543 the user defined type vector. It isn't really a user defined type,
12544 but it behaves like one, with other DIE's using an AT_user_def_type
12545 attribute to reference it. */
12546
f792889a 12547static struct type *
e7c27a73 12548read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12549{
e7c27a73 12550 struct objfile *objfile = cu->objfile;
3b7538c0 12551 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12552 struct type *type, *range_type, *index_type, *char_type;
12553 struct attribute *attr;
12554 unsigned int length;
12555
e142c38c 12556 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12557 if (attr)
12558 {
12559 length = DW_UNSND (attr);
12560 }
12561 else
12562 {
0963b4bd 12563 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12564 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12565 if (attr)
12566 {
12567 length = DW_UNSND (attr);
12568 }
12569 else
12570 {
12571 length = 1;
12572 }
c906108c 12573 }
6ccb9162 12574
46bf5051 12575 index_type = objfile_type (objfile)->builtin_int;
c906108c 12576 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12577 char_type = language_string_char_type (cu->language_defn, gdbarch);
12578 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12579
f792889a 12580 return set_die_type (die, type, cu);
c906108c
SS
12581}
12582
12583/* Handle DIES due to C code like:
12584
12585 struct foo
c5aa993b
JM
12586 {
12587 int (*funcp)(int a, long l);
12588 int b;
12589 };
c906108c 12590
0963b4bd 12591 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12592
f792889a 12593static struct type *
e7c27a73 12594read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12595{
bb5ed363 12596 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12597 struct type *type; /* Type that this function returns. */
12598 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12599 struct attribute *attr;
12600
e7c27a73 12601 type = die_type (die, cu);
7e314c57
JK
12602
12603 /* The die_type call above may have already set the type for this DIE. */
12604 ftype = get_die_type (die, cu);
12605 if (ftype)
12606 return ftype;
12607
0c8b41f1 12608 ftype = lookup_function_type (type);
c906108c 12609
5b8101ae 12610 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12611 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12612 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12613 || cu->language == language_cplus
5b8101ae
PM
12614 || cu->language == language_java
12615 || cu->language == language_pascal)
876cecd0 12616 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12617 else if (producer_is_realview (cu->producer))
12618 /* RealView does not emit DW_AT_prototyped. We can not
12619 distinguish prototyped and unprototyped functions; default to
12620 prototyped, since that is more common in modern code (and
12621 RealView warns about unprototyped functions). */
12622 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12623
c055b101
CV
12624 /* Store the calling convention in the type if it's available in
12625 the subroutine die. Otherwise set the calling convention to
12626 the default value DW_CC_normal. */
12627 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12628 if (attr)
12629 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12630 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12631 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12632 else
12633 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12634
12635 /* We need to add the subroutine type to the die immediately so
12636 we don't infinitely recurse when dealing with parameters
0963b4bd 12637 declared as the same subroutine type. */
76c10ea2 12638 set_die_type (die, ftype, cu);
6e70227d 12639
639d11d3 12640 if (die->child != NULL)
c906108c 12641 {
bb5ed363 12642 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12643 struct die_info *child_die;
8072405b 12644 int nparams, iparams;
c906108c
SS
12645
12646 /* Count the number of parameters.
12647 FIXME: GDB currently ignores vararg functions, but knows about
12648 vararg member functions. */
8072405b 12649 nparams = 0;
639d11d3 12650 child_die = die->child;
c906108c
SS
12651 while (child_die && child_die->tag)
12652 {
12653 if (child_die->tag == DW_TAG_formal_parameter)
12654 nparams++;
12655 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12656 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12657 child_die = sibling_die (child_die);
12658 }
12659
12660 /* Allocate storage for parameters and fill them in. */
12661 TYPE_NFIELDS (ftype) = nparams;
12662 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12663 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12664
8072405b
JK
12665 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12666 even if we error out during the parameters reading below. */
12667 for (iparams = 0; iparams < nparams; iparams++)
12668 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12669
12670 iparams = 0;
639d11d3 12671 child_die = die->child;
c906108c
SS
12672 while (child_die && child_die->tag)
12673 {
12674 if (child_die->tag == DW_TAG_formal_parameter)
12675 {
3ce3b1ba
PA
12676 struct type *arg_type;
12677
12678 /* DWARF version 2 has no clean way to discern C++
12679 static and non-static member functions. G++ helps
12680 GDB by marking the first parameter for non-static
12681 member functions (which is the this pointer) as
12682 artificial. We pass this information to
12683 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12684
12685 DWARF version 3 added DW_AT_object_pointer, which GCC
12686 4.5 does not yet generate. */
e142c38c 12687 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12688 if (attr)
12689 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12690 else
418835cc
KS
12691 {
12692 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12693
12694 /* GCC/43521: In java, the formal parameter
12695 "this" is sometimes not marked with DW_AT_artificial. */
12696 if (cu->language == language_java)
12697 {
12698 const char *name = dwarf2_name (child_die, cu);
9a619af0 12699
418835cc
KS
12700 if (name && !strcmp (name, "this"))
12701 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12702 }
12703 }
3ce3b1ba
PA
12704 arg_type = die_type (child_die, cu);
12705
12706 /* RealView does not mark THIS as const, which the testsuite
12707 expects. GCC marks THIS as const in method definitions,
12708 but not in the class specifications (GCC PR 43053). */
12709 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12710 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12711 {
12712 int is_this = 0;
12713 struct dwarf2_cu *arg_cu = cu;
12714 const char *name = dwarf2_name (child_die, cu);
12715
12716 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12717 if (attr)
12718 {
12719 /* If the compiler emits this, use it. */
12720 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12721 is_this = 1;
12722 }
12723 else if (name && strcmp (name, "this") == 0)
12724 /* Function definitions will have the argument names. */
12725 is_this = 1;
12726 else if (name == NULL && iparams == 0)
12727 /* Declarations may not have the names, so like
12728 elsewhere in GDB, assume an artificial first
12729 argument is "this". */
12730 is_this = 1;
12731
12732 if (is_this)
12733 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12734 arg_type, 0);
12735 }
12736
12737 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12738 iparams++;
12739 }
12740 child_die = sibling_die (child_die);
12741 }
12742 }
12743
76c10ea2 12744 return ftype;
c906108c
SS
12745}
12746
f792889a 12747static struct type *
e7c27a73 12748read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12749{
e7c27a73 12750 struct objfile *objfile = cu->objfile;
0114d602 12751 const char *name = NULL;
3c8e0968 12752 struct type *this_type, *target_type;
c906108c 12753
94af9270 12754 name = dwarf2_full_name (NULL, die, cu);
f792889a 12755 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12756 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12757 TYPE_NAME (this_type) = name;
f792889a 12758 set_die_type (die, this_type, cu);
3c8e0968
DE
12759 target_type = die_type (die, cu);
12760 if (target_type != this_type)
12761 TYPE_TARGET_TYPE (this_type) = target_type;
12762 else
12763 {
12764 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12765 spec and cause infinite loops in GDB. */
12766 complaint (&symfile_complaints,
12767 _("Self-referential DW_TAG_typedef "
12768 "- DIE at 0x%x [in module %s]"),
b64f50a1 12769 die->offset.sect_off, objfile->name);
3c8e0968
DE
12770 TYPE_TARGET_TYPE (this_type) = NULL;
12771 }
f792889a 12772 return this_type;
c906108c
SS
12773}
12774
12775/* Find a representation of a given base type and install
12776 it in the TYPE field of the die. */
12777
f792889a 12778static struct type *
e7c27a73 12779read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12780{
e7c27a73 12781 struct objfile *objfile = cu->objfile;
c906108c
SS
12782 struct type *type;
12783 struct attribute *attr;
12784 int encoding = 0, size = 0;
15d034d0 12785 const char *name;
6ccb9162
UW
12786 enum type_code code = TYPE_CODE_INT;
12787 int type_flags = 0;
12788 struct type *target_type = NULL;
c906108c 12789
e142c38c 12790 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12791 if (attr)
12792 {
12793 encoding = DW_UNSND (attr);
12794 }
e142c38c 12795 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12796 if (attr)
12797 {
12798 size = DW_UNSND (attr);
12799 }
39cbfefa 12800 name = dwarf2_name (die, cu);
6ccb9162 12801 if (!name)
c906108c 12802 {
6ccb9162
UW
12803 complaint (&symfile_complaints,
12804 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12805 }
6ccb9162
UW
12806
12807 switch (encoding)
c906108c 12808 {
6ccb9162
UW
12809 case DW_ATE_address:
12810 /* Turn DW_ATE_address into a void * pointer. */
12811 code = TYPE_CODE_PTR;
12812 type_flags |= TYPE_FLAG_UNSIGNED;
12813 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12814 break;
12815 case DW_ATE_boolean:
12816 code = TYPE_CODE_BOOL;
12817 type_flags |= TYPE_FLAG_UNSIGNED;
12818 break;
12819 case DW_ATE_complex_float:
12820 code = TYPE_CODE_COMPLEX;
12821 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12822 break;
12823 case DW_ATE_decimal_float:
12824 code = TYPE_CODE_DECFLOAT;
12825 break;
12826 case DW_ATE_float:
12827 code = TYPE_CODE_FLT;
12828 break;
12829 case DW_ATE_signed:
12830 break;
12831 case DW_ATE_unsigned:
12832 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12833 if (cu->language == language_fortran
12834 && name
12835 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12836 code = TYPE_CODE_CHAR;
6ccb9162
UW
12837 break;
12838 case DW_ATE_signed_char:
6e70227d 12839 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12840 || cu->language == language_pascal
12841 || cu->language == language_fortran)
6ccb9162
UW
12842 code = TYPE_CODE_CHAR;
12843 break;
12844 case DW_ATE_unsigned_char:
868a0084 12845 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12846 || cu->language == language_pascal
12847 || cu->language == language_fortran)
6ccb9162
UW
12848 code = TYPE_CODE_CHAR;
12849 type_flags |= TYPE_FLAG_UNSIGNED;
12850 break;
75079b2b
TT
12851 case DW_ATE_UTF:
12852 /* We just treat this as an integer and then recognize the
12853 type by name elsewhere. */
12854 break;
12855
6ccb9162
UW
12856 default:
12857 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12858 dwarf_type_encoding_name (encoding));
12859 break;
c906108c 12860 }
6ccb9162 12861
0114d602
DJ
12862 type = init_type (code, size, type_flags, NULL, objfile);
12863 TYPE_NAME (type) = name;
6ccb9162
UW
12864 TYPE_TARGET_TYPE (type) = target_type;
12865
0114d602 12866 if (name && strcmp (name, "char") == 0)
876cecd0 12867 TYPE_NOSIGN (type) = 1;
0114d602 12868
f792889a 12869 return set_die_type (die, type, cu);
c906108c
SS
12870}
12871
a02abb62
JB
12872/* Read the given DW_AT_subrange DIE. */
12873
f792889a 12874static struct type *
a02abb62
JB
12875read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12876{
4c9ad8c2 12877 struct type *base_type, *orig_base_type;
a02abb62
JB
12878 struct type *range_type;
12879 struct attribute *attr;
4fae6e18
JK
12880 LONGEST low, high;
12881 int low_default_is_valid;
15d034d0 12882 const char *name;
43bbcdc2 12883 LONGEST negative_mask;
e77813c8 12884
4c9ad8c2
TT
12885 orig_base_type = die_type (die, cu);
12886 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12887 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12888 creating the range type, but we use the result of check_typedef
12889 when examining properties of the type. */
12890 base_type = check_typedef (orig_base_type);
a02abb62 12891
7e314c57
JK
12892 /* The die_type call above may have already set the type for this DIE. */
12893 range_type = get_die_type (die, cu);
12894 if (range_type)
12895 return range_type;
12896
4fae6e18
JK
12897 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12898 omitting DW_AT_lower_bound. */
12899 switch (cu->language)
6e70227d 12900 {
4fae6e18
JK
12901 case language_c:
12902 case language_cplus:
12903 low = 0;
12904 low_default_is_valid = 1;
12905 break;
12906 case language_fortran:
12907 low = 1;
12908 low_default_is_valid = 1;
12909 break;
12910 case language_d:
12911 case language_java:
12912 case language_objc:
12913 low = 0;
12914 low_default_is_valid = (cu->header.version >= 4);
12915 break;
12916 case language_ada:
12917 case language_m2:
12918 case language_pascal:
a02abb62 12919 low = 1;
4fae6e18
JK
12920 low_default_is_valid = (cu->header.version >= 4);
12921 break;
12922 default:
12923 low = 0;
12924 low_default_is_valid = 0;
12925 break;
a02abb62
JB
12926 }
12927
dd5e6932
DJ
12928 /* FIXME: For variable sized arrays either of these could be
12929 a variable rather than a constant value. We'll allow it,
12930 but we don't know how to handle it. */
e142c38c 12931 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12932 if (attr)
4fae6e18
JK
12933 low = dwarf2_get_attr_constant_value (attr, low);
12934 else if (!low_default_is_valid)
12935 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12936 "- DIE at 0x%x [in module %s]"),
12937 die->offset.sect_off, cu->objfile->name);
a02abb62 12938
e142c38c 12939 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12940 if (attr)
6e70227d 12941 {
d48323d8 12942 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12943 {
12944 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12945 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12946 FIXME: GDB does not yet know how to handle dynamic
12947 arrays properly, treat them as arrays with unspecified
12948 length for now.
12949
12950 FIXME: jimb/2003-09-22: GDB does not really know
12951 how to handle arrays of unspecified length
12952 either; we just represent them as zero-length
12953 arrays. Choose an appropriate upper bound given
12954 the lower bound we've computed above. */
12955 high = low - 1;
12956 }
12957 else
12958 high = dwarf2_get_attr_constant_value (attr, 1);
12959 }
e77813c8
PM
12960 else
12961 {
12962 attr = dwarf2_attr (die, DW_AT_count, cu);
12963 if (attr)
12964 {
12965 int count = dwarf2_get_attr_constant_value (attr, 1);
12966 high = low + count - 1;
12967 }
c2ff108b
JK
12968 else
12969 {
12970 /* Unspecified array length. */
12971 high = low - 1;
12972 }
e77813c8
PM
12973 }
12974
12975 /* Dwarf-2 specifications explicitly allows to create subrange types
12976 without specifying a base type.
12977 In that case, the base type must be set to the type of
12978 the lower bound, upper bound or count, in that order, if any of these
12979 three attributes references an object that has a type.
12980 If no base type is found, the Dwarf-2 specifications say that
12981 a signed integer type of size equal to the size of an address should
12982 be used.
12983 For the following C code: `extern char gdb_int [];'
12984 GCC produces an empty range DIE.
12985 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12986 high bound or count are not yet handled by this code. */
e77813c8
PM
12987 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12988 {
12989 struct objfile *objfile = cu->objfile;
12990 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12991 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12992 struct type *int_type = objfile_type (objfile)->builtin_int;
12993
12994 /* Test "int", "long int", and "long long int" objfile types,
12995 and select the first one having a size above or equal to the
12996 architecture address size. */
12997 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12998 base_type = int_type;
12999 else
13000 {
13001 int_type = objfile_type (objfile)->builtin_long;
13002 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13003 base_type = int_type;
13004 else
13005 {
13006 int_type = objfile_type (objfile)->builtin_long_long;
13007 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13008 base_type = int_type;
13009 }
13010 }
13011 }
a02abb62 13012
6e70227d 13013 negative_mask =
43bbcdc2
PH
13014 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13015 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13016 low |= negative_mask;
13017 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13018 high |= negative_mask;
13019
4c9ad8c2 13020 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13021
bbb0eef6
JK
13022 /* Mark arrays with dynamic length at least as an array of unspecified
13023 length. GDB could check the boundary but before it gets implemented at
13024 least allow accessing the array elements. */
d48323d8 13025 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13026 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13027
c2ff108b
JK
13028 /* Ada expects an empty array on no boundary attributes. */
13029 if (attr == NULL && cu->language != language_ada)
13030 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13031
39cbfefa
DJ
13032 name = dwarf2_name (die, cu);
13033 if (name)
13034 TYPE_NAME (range_type) = name;
6e70227d 13035
e142c38c 13036 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13037 if (attr)
13038 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13039
7e314c57
JK
13040 set_die_type (die, range_type, cu);
13041
13042 /* set_die_type should be already done. */
b4ba55a1
JB
13043 set_descriptive_type (range_type, die, cu);
13044
7e314c57 13045 return range_type;
a02abb62 13046}
6e70227d 13047
f792889a 13048static struct type *
81a17f79
JB
13049read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13050{
13051 struct type *type;
81a17f79 13052
81a17f79
JB
13053 /* For now, we only support the C meaning of an unspecified type: void. */
13054
0114d602
DJ
13055 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13056 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13057
f792889a 13058 return set_die_type (die, type, cu);
81a17f79 13059}
a02abb62 13060
639d11d3
DC
13061/* Read a single die and all its descendents. Set the die's sibling
13062 field to NULL; set other fields in the die correctly, and set all
13063 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13064 location of the info_ptr after reading all of those dies. PARENT
13065 is the parent of the die in question. */
13066
13067static struct die_info *
dee91e82 13068read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13069 const gdb_byte *info_ptr,
13070 const gdb_byte **new_info_ptr,
dee91e82 13071 struct die_info *parent)
639d11d3
DC
13072{
13073 struct die_info *die;
d521ce57 13074 const gdb_byte *cur_ptr;
639d11d3
DC
13075 int has_children;
13076
bf6af496 13077 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13078 if (die == NULL)
13079 {
13080 *new_info_ptr = cur_ptr;
13081 return NULL;
13082 }
93311388 13083 store_in_ref_table (die, reader->cu);
639d11d3
DC
13084
13085 if (has_children)
bf6af496 13086 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13087 else
13088 {
13089 die->child = NULL;
13090 *new_info_ptr = cur_ptr;
13091 }
13092
13093 die->sibling = NULL;
13094 die->parent = parent;
13095 return die;
13096}
13097
13098/* Read a die, all of its descendents, and all of its siblings; set
13099 all of the fields of all of the dies correctly. Arguments are as
13100 in read_die_and_children. */
13101
13102static struct die_info *
bf6af496 13103read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13104 const gdb_byte *info_ptr,
13105 const gdb_byte **new_info_ptr,
bf6af496 13106 struct die_info *parent)
639d11d3
DC
13107{
13108 struct die_info *first_die, *last_sibling;
d521ce57 13109 const gdb_byte *cur_ptr;
639d11d3 13110
c906108c 13111 cur_ptr = info_ptr;
639d11d3
DC
13112 first_die = last_sibling = NULL;
13113
13114 while (1)
c906108c 13115 {
639d11d3 13116 struct die_info *die
dee91e82 13117 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13118
1d325ec1 13119 if (die == NULL)
c906108c 13120 {
639d11d3
DC
13121 *new_info_ptr = cur_ptr;
13122 return first_die;
c906108c 13123 }
1d325ec1
DJ
13124
13125 if (!first_die)
13126 first_die = die;
c906108c 13127 else
1d325ec1
DJ
13128 last_sibling->sibling = die;
13129
13130 last_sibling = die;
c906108c 13131 }
c906108c
SS
13132}
13133
bf6af496
DE
13134/* Read a die, all of its descendents, and all of its siblings; set
13135 all of the fields of all of the dies correctly. Arguments are as
13136 in read_die_and_children.
13137 This the main entry point for reading a DIE and all its children. */
13138
13139static struct die_info *
13140read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13141 const gdb_byte *info_ptr,
13142 const gdb_byte **new_info_ptr,
bf6af496
DE
13143 struct die_info *parent)
13144{
13145 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13146 new_info_ptr, parent);
13147
13148 if (dwarf2_die_debug)
13149 {
13150 fprintf_unfiltered (gdb_stdlog,
13151 "Read die from %s@0x%x of %s:\n",
13152 bfd_section_name (reader->abfd,
13153 reader->die_section->asection),
13154 (unsigned) (info_ptr - reader->die_section->buffer),
13155 bfd_get_filename (reader->abfd));
13156 dump_die (die, dwarf2_die_debug);
13157 }
13158
13159 return die;
13160}
13161
3019eac3
DE
13162/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13163 attributes.
13164 The caller is responsible for filling in the extra attributes
13165 and updating (*DIEP)->num_attrs.
13166 Set DIEP to point to a newly allocated die with its information,
13167 except for its child, sibling, and parent fields.
13168 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13169
d521ce57 13170static const gdb_byte *
3019eac3 13171read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13172 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13173 int *has_children, int num_extra_attrs)
93311388 13174{
b64f50a1
JK
13175 unsigned int abbrev_number, bytes_read, i;
13176 sect_offset offset;
93311388
DE
13177 struct abbrev_info *abbrev;
13178 struct die_info *die;
13179 struct dwarf2_cu *cu = reader->cu;
13180 bfd *abfd = reader->abfd;
13181
b64f50a1 13182 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13183 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13184 info_ptr += bytes_read;
13185 if (!abbrev_number)
13186 {
13187 *diep = NULL;
13188 *has_children = 0;
13189 return info_ptr;
13190 }
13191
433df2d4 13192 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13193 if (!abbrev)
348e048f
DE
13194 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13195 abbrev_number,
13196 bfd_get_filename (abfd));
13197
3019eac3 13198 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13199 die->offset = offset;
13200 die->tag = abbrev->tag;
13201 die->abbrev = abbrev_number;
13202
3019eac3
DE
13203 /* Make the result usable.
13204 The caller needs to update num_attrs after adding the extra
13205 attributes. */
93311388
DE
13206 die->num_attrs = abbrev->num_attrs;
13207
13208 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13209 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13210 info_ptr);
93311388
DE
13211
13212 *diep = die;
13213 *has_children = abbrev->has_children;
13214 return info_ptr;
13215}
13216
3019eac3
DE
13217/* Read a die and all its attributes.
13218 Set DIEP to point to a newly allocated die with its information,
13219 except for its child, sibling, and parent fields.
13220 Set HAS_CHILDREN to tell whether the die has children or not. */
13221
d521ce57 13222static const gdb_byte *
3019eac3 13223read_full_die (const struct die_reader_specs *reader,
d521ce57 13224 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13225 int *has_children)
13226{
d521ce57 13227 const gdb_byte *result;
bf6af496
DE
13228
13229 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13230
13231 if (dwarf2_die_debug)
13232 {
13233 fprintf_unfiltered (gdb_stdlog,
13234 "Read die from %s@0x%x of %s:\n",
13235 bfd_section_name (reader->abfd,
13236 reader->die_section->asection),
13237 (unsigned) (info_ptr - reader->die_section->buffer),
13238 bfd_get_filename (reader->abfd));
13239 dump_die (*diep, dwarf2_die_debug);
13240 }
13241
13242 return result;
3019eac3 13243}
433df2d4
DE
13244\f
13245/* Abbreviation tables.
3019eac3 13246
433df2d4 13247 In DWARF version 2, the description of the debugging information is
c906108c
SS
13248 stored in a separate .debug_abbrev section. Before we read any
13249 dies from a section we read in all abbreviations and install them
433df2d4
DE
13250 in a hash table. */
13251
13252/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13253
13254static struct abbrev_info *
13255abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13256{
13257 struct abbrev_info *abbrev;
13258
13259 abbrev = (struct abbrev_info *)
13260 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13261 memset (abbrev, 0, sizeof (struct abbrev_info));
13262 return abbrev;
13263}
13264
13265/* Add an abbreviation to the table. */
c906108c
SS
13266
13267static void
433df2d4
DE
13268abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13269 unsigned int abbrev_number,
13270 struct abbrev_info *abbrev)
13271{
13272 unsigned int hash_number;
13273
13274 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13275 abbrev->next = abbrev_table->abbrevs[hash_number];
13276 abbrev_table->abbrevs[hash_number] = abbrev;
13277}
dee91e82 13278
433df2d4
DE
13279/* Look up an abbrev in the table.
13280 Returns NULL if the abbrev is not found. */
13281
13282static struct abbrev_info *
13283abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13284 unsigned int abbrev_number)
c906108c 13285{
433df2d4
DE
13286 unsigned int hash_number;
13287 struct abbrev_info *abbrev;
13288
13289 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13290 abbrev = abbrev_table->abbrevs[hash_number];
13291
13292 while (abbrev)
13293 {
13294 if (abbrev->number == abbrev_number)
13295 return abbrev;
13296 abbrev = abbrev->next;
13297 }
13298 return NULL;
13299}
13300
13301/* Read in an abbrev table. */
13302
13303static struct abbrev_table *
13304abbrev_table_read_table (struct dwarf2_section_info *section,
13305 sect_offset offset)
13306{
13307 struct objfile *objfile = dwarf2_per_objfile->objfile;
13308 bfd *abfd = section->asection->owner;
13309 struct abbrev_table *abbrev_table;
d521ce57 13310 const gdb_byte *abbrev_ptr;
c906108c
SS
13311 struct abbrev_info *cur_abbrev;
13312 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13313 unsigned int abbrev_form;
f3dd6933
DJ
13314 struct attr_abbrev *cur_attrs;
13315 unsigned int allocated_attrs;
c906108c 13316
433df2d4 13317 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13318 abbrev_table->offset = offset;
433df2d4
DE
13319 obstack_init (&abbrev_table->abbrev_obstack);
13320 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13321 (ABBREV_HASH_SIZE
13322 * sizeof (struct abbrev_info *)));
13323 memset (abbrev_table->abbrevs, 0,
13324 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13325
433df2d4
DE
13326 dwarf2_read_section (objfile, section);
13327 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13328 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13329 abbrev_ptr += bytes_read;
13330
f3dd6933
DJ
13331 allocated_attrs = ATTR_ALLOC_CHUNK;
13332 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13333
0963b4bd 13334 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13335 while (abbrev_number)
13336 {
433df2d4 13337 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13338
13339 /* read in abbrev header */
13340 cur_abbrev->number = abbrev_number;
13341 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13342 abbrev_ptr += bytes_read;
13343 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13344 abbrev_ptr += 1;
13345
13346 /* now read in declarations */
13347 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13348 abbrev_ptr += bytes_read;
13349 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13350 abbrev_ptr += bytes_read;
13351 while (abbrev_name)
13352 {
f3dd6933 13353 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13354 {
f3dd6933
DJ
13355 allocated_attrs += ATTR_ALLOC_CHUNK;
13356 cur_attrs
13357 = xrealloc (cur_attrs, (allocated_attrs
13358 * sizeof (struct attr_abbrev)));
c906108c 13359 }
ae038cb0 13360
f3dd6933
DJ
13361 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13362 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13363 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13364 abbrev_ptr += bytes_read;
13365 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13366 abbrev_ptr += bytes_read;
13367 }
13368
433df2d4 13369 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13370 (cur_abbrev->num_attrs
13371 * sizeof (struct attr_abbrev)));
13372 memcpy (cur_abbrev->attrs, cur_attrs,
13373 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13374
433df2d4 13375 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13376
13377 /* Get next abbreviation.
13378 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13379 always properly terminated with an abbrev number of 0.
13380 Exit loop if we encounter an abbreviation which we have
13381 already read (which means we are about to read the abbreviations
13382 for the next compile unit) or if the end of the abbreviation
13383 table is reached. */
433df2d4 13384 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13385 break;
13386 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13387 abbrev_ptr += bytes_read;
433df2d4 13388 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13389 break;
13390 }
f3dd6933
DJ
13391
13392 xfree (cur_attrs);
433df2d4 13393 return abbrev_table;
c906108c
SS
13394}
13395
433df2d4 13396/* Free the resources held by ABBREV_TABLE. */
c906108c 13397
c906108c 13398static void
433df2d4 13399abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13400{
433df2d4
DE
13401 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13402 xfree (abbrev_table);
c906108c
SS
13403}
13404
f4dc4d17
DE
13405/* Same as abbrev_table_free but as a cleanup.
13406 We pass in a pointer to the pointer to the table so that we can
13407 set the pointer to NULL when we're done. It also simplifies
13408 build_type_unit_groups. */
13409
13410static void
13411abbrev_table_free_cleanup (void *table_ptr)
13412{
13413 struct abbrev_table **abbrev_table_ptr = table_ptr;
13414
13415 if (*abbrev_table_ptr != NULL)
13416 abbrev_table_free (*abbrev_table_ptr);
13417 *abbrev_table_ptr = NULL;
13418}
13419
433df2d4
DE
13420/* Read the abbrev table for CU from ABBREV_SECTION. */
13421
13422static void
13423dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13424 struct dwarf2_section_info *abbrev_section)
c906108c 13425{
433df2d4
DE
13426 cu->abbrev_table =
13427 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13428}
c906108c 13429
433df2d4 13430/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13431
433df2d4
DE
13432static void
13433dwarf2_free_abbrev_table (void *ptr_to_cu)
13434{
13435 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13436
433df2d4
DE
13437 abbrev_table_free (cu->abbrev_table);
13438 /* Set this to NULL so that we SEGV if we try to read it later,
13439 and also because free_comp_unit verifies this is NULL. */
13440 cu->abbrev_table = NULL;
13441}
13442\f
72bf9492
DJ
13443/* Returns nonzero if TAG represents a type that we might generate a partial
13444 symbol for. */
13445
13446static int
13447is_type_tag_for_partial (int tag)
13448{
13449 switch (tag)
13450 {
13451#if 0
13452 /* Some types that would be reasonable to generate partial symbols for,
13453 that we don't at present. */
13454 case DW_TAG_array_type:
13455 case DW_TAG_file_type:
13456 case DW_TAG_ptr_to_member_type:
13457 case DW_TAG_set_type:
13458 case DW_TAG_string_type:
13459 case DW_TAG_subroutine_type:
13460#endif
13461 case DW_TAG_base_type:
13462 case DW_TAG_class_type:
680b30c7 13463 case DW_TAG_interface_type:
72bf9492
DJ
13464 case DW_TAG_enumeration_type:
13465 case DW_TAG_structure_type:
13466 case DW_TAG_subrange_type:
13467 case DW_TAG_typedef:
13468 case DW_TAG_union_type:
13469 return 1;
13470 default:
13471 return 0;
13472 }
13473}
13474
13475/* Load all DIEs that are interesting for partial symbols into memory. */
13476
13477static struct partial_die_info *
dee91e82 13478load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13479 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13480{
dee91e82 13481 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13482 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13483 struct partial_die_info *part_die;
13484 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13485 struct abbrev_info *abbrev;
13486 unsigned int bytes_read;
5afb4e99 13487 unsigned int load_all = 0;
72bf9492
DJ
13488 int nesting_level = 1;
13489
13490 parent_die = NULL;
13491 last_die = NULL;
13492
7adf1e79
DE
13493 gdb_assert (cu->per_cu != NULL);
13494 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13495 load_all = 1;
13496
72bf9492
DJ
13497 cu->partial_dies
13498 = htab_create_alloc_ex (cu->header.length / 12,
13499 partial_die_hash,
13500 partial_die_eq,
13501 NULL,
13502 &cu->comp_unit_obstack,
13503 hashtab_obstack_allocate,
13504 dummy_obstack_deallocate);
13505
13506 part_die = obstack_alloc (&cu->comp_unit_obstack,
13507 sizeof (struct partial_die_info));
13508
13509 while (1)
13510 {
13511 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13512
13513 /* A NULL abbrev means the end of a series of children. */
13514 if (abbrev == NULL)
13515 {
13516 if (--nesting_level == 0)
13517 {
13518 /* PART_DIE was probably the last thing allocated on the
13519 comp_unit_obstack, so we could call obstack_free
13520 here. We don't do that because the waste is small,
13521 and will be cleaned up when we're done with this
13522 compilation unit. This way, we're also more robust
13523 against other users of the comp_unit_obstack. */
13524 return first_die;
13525 }
13526 info_ptr += bytes_read;
13527 last_die = parent_die;
13528 parent_die = parent_die->die_parent;
13529 continue;
13530 }
13531
98bfdba5
PA
13532 /* Check for template arguments. We never save these; if
13533 they're seen, we just mark the parent, and go on our way. */
13534 if (parent_die != NULL
13535 && cu->language == language_cplus
13536 && (abbrev->tag == DW_TAG_template_type_param
13537 || abbrev->tag == DW_TAG_template_value_param))
13538 {
13539 parent_die->has_template_arguments = 1;
13540
13541 if (!load_all)
13542 {
13543 /* We don't need a partial DIE for the template argument. */
dee91e82 13544 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13545 continue;
13546 }
13547 }
13548
0d99eb77 13549 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13550 Skip their other children. */
13551 if (!load_all
13552 && cu->language == language_cplus
13553 && parent_die != NULL
13554 && parent_die->tag == DW_TAG_subprogram)
13555 {
dee91e82 13556 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13557 continue;
13558 }
13559
5afb4e99
DJ
13560 /* Check whether this DIE is interesting enough to save. Normally
13561 we would not be interested in members here, but there may be
13562 later variables referencing them via DW_AT_specification (for
13563 static members). */
13564 if (!load_all
13565 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13566 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13567 && abbrev->tag != DW_TAG_enumerator
13568 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13569 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13570 && abbrev->tag != DW_TAG_variable
5afb4e99 13571 && abbrev->tag != DW_TAG_namespace
f55ee35c 13572 && abbrev->tag != DW_TAG_module
95554aad
TT
13573 && abbrev->tag != DW_TAG_member
13574 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13575 {
13576 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13577 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13578 continue;
13579 }
13580
dee91e82
DE
13581 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13582 info_ptr);
72bf9492
DJ
13583
13584 /* This two-pass algorithm for processing partial symbols has a
13585 high cost in cache pressure. Thus, handle some simple cases
13586 here which cover the majority of C partial symbols. DIEs
13587 which neither have specification tags in them, nor could have
13588 specification tags elsewhere pointing at them, can simply be
13589 processed and discarded.
13590
13591 This segment is also optional; scan_partial_symbols and
13592 add_partial_symbol will handle these DIEs if we chain
13593 them in normally. When compilers which do not emit large
13594 quantities of duplicate debug information are more common,
13595 this code can probably be removed. */
13596
13597 /* Any complete simple types at the top level (pretty much all
13598 of them, for a language without namespaces), can be processed
13599 directly. */
13600 if (parent_die == NULL
13601 && part_die->has_specification == 0
13602 && part_die->is_declaration == 0
d8228535 13603 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13604 || part_die->tag == DW_TAG_base_type
13605 || part_die->tag == DW_TAG_subrange_type))
13606 {
13607 if (building_psymtab && part_die->name != NULL)
04a679b8 13608 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13609 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13610 &objfile->static_psymbols,
13611 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13612 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13613 continue;
13614 }
13615
d8228535
JK
13616 /* The exception for DW_TAG_typedef with has_children above is
13617 a workaround of GCC PR debug/47510. In the case of this complaint
13618 type_name_no_tag_or_error will error on such types later.
13619
13620 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13621 it could not find the child DIEs referenced later, this is checked
13622 above. In correct DWARF DW_TAG_typedef should have no children. */
13623
13624 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13625 complaint (&symfile_complaints,
13626 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13627 "- DIE at 0x%x [in module %s]"),
b64f50a1 13628 part_die->offset.sect_off, objfile->name);
d8228535 13629
72bf9492
DJ
13630 /* If we're at the second level, and we're an enumerator, and
13631 our parent has no specification (meaning possibly lives in a
13632 namespace elsewhere), then we can add the partial symbol now
13633 instead of queueing it. */
13634 if (part_die->tag == DW_TAG_enumerator
13635 && parent_die != NULL
13636 && parent_die->die_parent == NULL
13637 && parent_die->tag == DW_TAG_enumeration_type
13638 && parent_die->has_specification == 0)
13639 {
13640 if (part_die->name == NULL)
3e43a32a
MS
13641 complaint (&symfile_complaints,
13642 _("malformed enumerator DIE ignored"));
72bf9492 13643 else if (building_psymtab)
04a679b8 13644 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13645 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13646 (cu->language == language_cplus
13647 || cu->language == language_java)
bb5ed363
DE
13648 ? &objfile->global_psymbols
13649 : &objfile->static_psymbols,
13650 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13651
dee91e82 13652 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13653 continue;
13654 }
13655
13656 /* We'll save this DIE so link it in. */
13657 part_die->die_parent = parent_die;
13658 part_die->die_sibling = NULL;
13659 part_die->die_child = NULL;
13660
13661 if (last_die && last_die == parent_die)
13662 last_die->die_child = part_die;
13663 else if (last_die)
13664 last_die->die_sibling = part_die;
13665
13666 last_die = part_die;
13667
13668 if (first_die == NULL)
13669 first_die = part_die;
13670
13671 /* Maybe add the DIE to the hash table. Not all DIEs that we
13672 find interesting need to be in the hash table, because we
13673 also have the parent/sibling/child chains; only those that we
13674 might refer to by offset later during partial symbol reading.
13675
13676 For now this means things that might have be the target of a
13677 DW_AT_specification, DW_AT_abstract_origin, or
13678 DW_AT_extension. DW_AT_extension will refer only to
13679 namespaces; DW_AT_abstract_origin refers to functions (and
13680 many things under the function DIE, but we do not recurse
13681 into function DIEs during partial symbol reading) and
13682 possibly variables as well; DW_AT_specification refers to
13683 declarations. Declarations ought to have the DW_AT_declaration
13684 flag. It happens that GCC forgets to put it in sometimes, but
13685 only for functions, not for types.
13686
13687 Adding more things than necessary to the hash table is harmless
13688 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13689 wasted time in find_partial_die, when we reread the compilation
13690 unit with load_all_dies set. */
72bf9492 13691
5afb4e99 13692 if (load_all
72929c62 13693 || abbrev->tag == DW_TAG_constant
5afb4e99 13694 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13695 || abbrev->tag == DW_TAG_variable
13696 || abbrev->tag == DW_TAG_namespace
13697 || part_die->is_declaration)
13698 {
13699 void **slot;
13700
13701 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13702 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13703 *slot = part_die;
13704 }
13705
13706 part_die = obstack_alloc (&cu->comp_unit_obstack,
13707 sizeof (struct partial_die_info));
13708
13709 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13710 we have no reason to follow the children of structures; for other
98bfdba5
PA
13711 languages we have to, so that we can get at method physnames
13712 to infer fully qualified class names, for DW_AT_specification,
13713 and for C++ template arguments. For C++, we also look one level
13714 inside functions to find template arguments (if the name of the
13715 function does not already contain the template arguments).
bc30ff58
JB
13716
13717 For Ada, we need to scan the children of subprograms and lexical
13718 blocks as well because Ada allows the definition of nested
13719 entities that could be interesting for the debugger, such as
13720 nested subprograms for instance. */
72bf9492 13721 if (last_die->has_children
5afb4e99
DJ
13722 && (load_all
13723 || last_die->tag == DW_TAG_namespace
f55ee35c 13724 || last_die->tag == DW_TAG_module
72bf9492 13725 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13726 || (cu->language == language_cplus
13727 && last_die->tag == DW_TAG_subprogram
13728 && (last_die->name == NULL
13729 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13730 || (cu->language != language_c
13731 && (last_die->tag == DW_TAG_class_type
680b30c7 13732 || last_die->tag == DW_TAG_interface_type
72bf9492 13733 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13734 || last_die->tag == DW_TAG_union_type))
13735 || (cu->language == language_ada
13736 && (last_die->tag == DW_TAG_subprogram
13737 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13738 {
13739 nesting_level++;
13740 parent_die = last_die;
13741 continue;
13742 }
13743
13744 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13745 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13746
13747 /* Back to the top, do it again. */
13748 }
13749}
13750
c906108c
SS
13751/* Read a minimal amount of information into the minimal die structure. */
13752
d521ce57 13753static const gdb_byte *
dee91e82
DE
13754read_partial_die (const struct die_reader_specs *reader,
13755 struct partial_die_info *part_die,
13756 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 13757 const gdb_byte *info_ptr)
c906108c 13758{
dee91e82 13759 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13760 struct objfile *objfile = cu->objfile;
d521ce57 13761 const gdb_byte *buffer = reader->buffer;
fa238c03 13762 unsigned int i;
c906108c 13763 struct attribute attr;
c5aa993b 13764 int has_low_pc_attr = 0;
c906108c 13765 int has_high_pc_attr = 0;
91da1414 13766 int high_pc_relative = 0;
c906108c 13767
72bf9492 13768 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13769
b64f50a1 13770 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13771
13772 info_ptr += abbrev_len;
13773
13774 if (abbrev == NULL)
13775 return info_ptr;
13776
c906108c
SS
13777 part_die->tag = abbrev->tag;
13778 part_die->has_children = abbrev->has_children;
c906108c
SS
13779
13780 for (i = 0; i < abbrev->num_attrs; ++i)
13781 {
dee91e82 13782 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13783
13784 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13785 partial symbol table. */
c906108c
SS
13786 switch (attr.name)
13787 {
13788 case DW_AT_name:
71c25dea
TT
13789 switch (part_die->tag)
13790 {
13791 case DW_TAG_compile_unit:
95554aad 13792 case DW_TAG_partial_unit:
348e048f 13793 case DW_TAG_type_unit:
71c25dea
TT
13794 /* Compilation units have a DW_AT_name that is a filename, not
13795 a source language identifier. */
13796 case DW_TAG_enumeration_type:
13797 case DW_TAG_enumerator:
13798 /* These tags always have simple identifiers already; no need
13799 to canonicalize them. */
13800 part_die->name = DW_STRING (&attr);
13801 break;
13802 default:
13803 part_die->name
13804 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13805 &objfile->objfile_obstack);
71c25dea
TT
13806 break;
13807 }
c906108c 13808 break;
31ef98ae 13809 case DW_AT_linkage_name:
c906108c 13810 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13811 /* Note that both forms of linkage name might appear. We
13812 assume they will be the same, and we only store the last
13813 one we see. */
94af9270
KS
13814 if (cu->language == language_ada)
13815 part_die->name = DW_STRING (&attr);
abc72ce4 13816 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13817 break;
13818 case DW_AT_low_pc:
13819 has_low_pc_attr = 1;
13820 part_die->lowpc = DW_ADDR (&attr);
13821 break;
13822 case DW_AT_high_pc:
13823 has_high_pc_attr = 1;
3019eac3
DE
13824 if (attr.form == DW_FORM_addr
13825 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13826 part_die->highpc = DW_ADDR (&attr);
13827 else
13828 {
13829 high_pc_relative = 1;
13830 part_die->highpc = DW_UNSND (&attr);
13831 }
c906108c
SS
13832 break;
13833 case DW_AT_location:
0963b4bd 13834 /* Support the .debug_loc offsets. */
8e19ed76
PS
13835 if (attr_form_is_block (&attr))
13836 {
95554aad 13837 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13838 }
3690dd37 13839 else if (attr_form_is_section_offset (&attr))
8e19ed76 13840 {
4d3c2250 13841 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13842 }
13843 else
13844 {
4d3c2250
KB
13845 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13846 "partial symbol information");
8e19ed76 13847 }
c906108c 13848 break;
c906108c
SS
13849 case DW_AT_external:
13850 part_die->is_external = DW_UNSND (&attr);
13851 break;
13852 case DW_AT_declaration:
13853 part_die->is_declaration = DW_UNSND (&attr);
13854 break;
13855 case DW_AT_type:
13856 part_die->has_type = 1;
13857 break;
13858 case DW_AT_abstract_origin:
13859 case DW_AT_specification:
72bf9492
DJ
13860 case DW_AT_extension:
13861 part_die->has_specification = 1;
c764a876 13862 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13863 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13864 || cu->per_cu->is_dwz);
c906108c
SS
13865 break;
13866 case DW_AT_sibling:
13867 /* Ignore absolute siblings, they might point outside of
13868 the current compile unit. */
13869 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13870 complaint (&symfile_complaints,
13871 _("ignoring absolute DW_AT_sibling"));
c906108c 13872 else
b64f50a1 13873 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13874 break;
fa4028e9
JB
13875 case DW_AT_byte_size:
13876 part_die->has_byte_size = 1;
13877 break;
68511cec
CES
13878 case DW_AT_calling_convention:
13879 /* DWARF doesn't provide a way to identify a program's source-level
13880 entry point. DW_AT_calling_convention attributes are only meant
13881 to describe functions' calling conventions.
13882
13883 However, because it's a necessary piece of information in
13884 Fortran, and because DW_CC_program is the only piece of debugging
13885 information whose definition refers to a 'main program' at all,
13886 several compilers have begun marking Fortran main programs with
13887 DW_CC_program --- even when those functions use the standard
13888 calling conventions.
13889
13890 So until DWARF specifies a way to provide this information and
13891 compilers pick up the new representation, we'll support this
13892 practice. */
13893 if (DW_UNSND (&attr) == DW_CC_program
13894 && cu->language == language_fortran)
01f8c46d
JK
13895 {
13896 set_main_name (part_die->name);
13897
13898 /* As this DIE has a static linkage the name would be difficult
13899 to look up later. */
13900 language_of_main = language_fortran;
13901 }
68511cec 13902 break;
481860b3
GB
13903 case DW_AT_inline:
13904 if (DW_UNSND (&attr) == DW_INL_inlined
13905 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13906 part_die->may_be_inlined = 1;
13907 break;
95554aad
TT
13908
13909 case DW_AT_import:
13910 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13911 {
13912 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13913 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13914 || cu->per_cu->is_dwz);
13915 }
95554aad
TT
13916 break;
13917
c906108c
SS
13918 default:
13919 break;
13920 }
13921 }
13922
91da1414
MW
13923 if (high_pc_relative)
13924 part_die->highpc += part_die->lowpc;
13925
9373cf26
JK
13926 if (has_low_pc_attr && has_high_pc_attr)
13927 {
13928 /* When using the GNU linker, .gnu.linkonce. sections are used to
13929 eliminate duplicate copies of functions and vtables and such.
13930 The linker will arbitrarily choose one and discard the others.
13931 The AT_*_pc values for such functions refer to local labels in
13932 these sections. If the section from that file was discarded, the
13933 labels are not in the output, so the relocs get a value of 0.
13934 If this is a discarded function, mark the pc bounds as invalid,
13935 so that GDB will ignore it. */
13936 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13937 {
bb5ed363 13938 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13939
13940 complaint (&symfile_complaints,
13941 _("DW_AT_low_pc %s is zero "
13942 "for DIE at 0x%x [in module %s]"),
13943 paddress (gdbarch, part_die->lowpc),
b64f50a1 13944 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13945 }
13946 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13947 else if (part_die->lowpc >= part_die->highpc)
13948 {
bb5ed363 13949 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13950
13951 complaint (&symfile_complaints,
13952 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13953 "for DIE at 0x%x [in module %s]"),
13954 paddress (gdbarch, part_die->lowpc),
13955 paddress (gdbarch, part_die->highpc),
b64f50a1 13956 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13957 }
13958 else
13959 part_die->has_pc_info = 1;
13960 }
85cbf3d3 13961
c906108c
SS
13962 return info_ptr;
13963}
13964
72bf9492
DJ
13965/* Find a cached partial DIE at OFFSET in CU. */
13966
13967static struct partial_die_info *
b64f50a1 13968find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13969{
13970 struct partial_die_info *lookup_die = NULL;
13971 struct partial_die_info part_die;
13972
13973 part_die.offset = offset;
b64f50a1
JK
13974 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13975 offset.sect_off);
72bf9492 13976
72bf9492
DJ
13977 return lookup_die;
13978}
13979
348e048f
DE
13980/* Find a partial DIE at OFFSET, which may or may not be in CU,
13981 except in the case of .debug_types DIEs which do not reference
13982 outside their CU (they do however referencing other types via
55f1336d 13983 DW_FORM_ref_sig8). */
72bf9492
DJ
13984
13985static struct partial_die_info *
36586728 13986find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13987{
bb5ed363 13988 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13989 struct dwarf2_per_cu_data *per_cu = NULL;
13990 struct partial_die_info *pd = NULL;
72bf9492 13991
36586728
TT
13992 if (offset_in_dwz == cu->per_cu->is_dwz
13993 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13994 {
13995 pd = find_partial_die_in_comp_unit (offset, cu);
13996 if (pd != NULL)
13997 return pd;
0d99eb77
DE
13998 /* We missed recording what we needed.
13999 Load all dies and try again. */
14000 per_cu = cu->per_cu;
5afb4e99 14001 }
0d99eb77
DE
14002 else
14003 {
14004 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14005 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14006 {
14007 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14008 " external reference to offset 0x%lx [in module %s].\n"),
14009 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14010 bfd_get_filename (objfile->obfd));
14011 }
36586728
TT
14012 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14013 objfile);
72bf9492 14014
0d99eb77
DE
14015 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14016 load_partial_comp_unit (per_cu);
ae038cb0 14017
0d99eb77
DE
14018 per_cu->cu->last_used = 0;
14019 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14020 }
5afb4e99 14021
dee91e82
DE
14022 /* If we didn't find it, and not all dies have been loaded,
14023 load them all and try again. */
14024
5afb4e99
DJ
14025 if (pd == NULL && per_cu->load_all_dies == 0)
14026 {
5afb4e99 14027 per_cu->load_all_dies = 1;
fd820528
DE
14028
14029 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14030 THIS_CU->cu may already be in use. So we can't just free it and
14031 replace its DIEs with the ones we read in. Instead, we leave those
14032 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14033 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14034 set. */
dee91e82 14035 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14036
14037 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14038 }
14039
14040 if (pd == NULL)
14041 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14042 _("could not find partial DIE 0x%x "
14043 "in cache [from module %s]\n"),
b64f50a1 14044 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14045 return pd;
72bf9492
DJ
14046}
14047
abc72ce4
DE
14048/* See if we can figure out if the class lives in a namespace. We do
14049 this by looking for a member function; its demangled name will
14050 contain namespace info, if there is any. */
14051
14052static void
14053guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14054 struct dwarf2_cu *cu)
14055{
14056 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14057 what template types look like, because the demangler
14058 frequently doesn't give the same name as the debug info. We
14059 could fix this by only using the demangled name to get the
14060 prefix (but see comment in read_structure_type). */
14061
14062 struct partial_die_info *real_pdi;
14063 struct partial_die_info *child_pdi;
14064
14065 /* If this DIE (this DIE's specification, if any) has a parent, then
14066 we should not do this. We'll prepend the parent's fully qualified
14067 name when we create the partial symbol. */
14068
14069 real_pdi = struct_pdi;
14070 while (real_pdi->has_specification)
36586728
TT
14071 real_pdi = find_partial_die (real_pdi->spec_offset,
14072 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14073
14074 if (real_pdi->die_parent != NULL)
14075 return;
14076
14077 for (child_pdi = struct_pdi->die_child;
14078 child_pdi != NULL;
14079 child_pdi = child_pdi->die_sibling)
14080 {
14081 if (child_pdi->tag == DW_TAG_subprogram
14082 && child_pdi->linkage_name != NULL)
14083 {
14084 char *actual_class_name
14085 = language_class_name_from_physname (cu->language_defn,
14086 child_pdi->linkage_name);
14087 if (actual_class_name != NULL)
14088 {
14089 struct_pdi->name
10f0c4bb
TT
14090 = obstack_copy0 (&cu->objfile->objfile_obstack,
14091 actual_class_name,
14092 strlen (actual_class_name));
abc72ce4
DE
14093 xfree (actual_class_name);
14094 }
14095 break;
14096 }
14097 }
14098}
14099
72bf9492
DJ
14100/* Adjust PART_DIE before generating a symbol for it. This function
14101 may set the is_external flag or change the DIE's name. */
14102
14103static void
14104fixup_partial_die (struct partial_die_info *part_die,
14105 struct dwarf2_cu *cu)
14106{
abc72ce4
DE
14107 /* Once we've fixed up a die, there's no point in doing so again.
14108 This also avoids a memory leak if we were to call
14109 guess_partial_die_structure_name multiple times. */
14110 if (part_die->fixup_called)
14111 return;
14112
72bf9492
DJ
14113 /* If we found a reference attribute and the DIE has no name, try
14114 to find a name in the referred to DIE. */
14115
14116 if (part_die->name == NULL && part_die->has_specification)
14117 {
14118 struct partial_die_info *spec_die;
72bf9492 14119
36586728
TT
14120 spec_die = find_partial_die (part_die->spec_offset,
14121 part_die->spec_is_dwz, cu);
72bf9492 14122
10b3939b 14123 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14124
14125 if (spec_die->name)
14126 {
14127 part_die->name = spec_die->name;
14128
14129 /* Copy DW_AT_external attribute if it is set. */
14130 if (spec_die->is_external)
14131 part_die->is_external = spec_die->is_external;
14132 }
14133 }
14134
14135 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14136
14137 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14138 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14139
abc72ce4
DE
14140 /* If there is no parent die to provide a namespace, and there are
14141 children, see if we can determine the namespace from their linkage
122d1940 14142 name. */
abc72ce4 14143 if (cu->language == language_cplus
8b70b953 14144 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14145 && part_die->die_parent == NULL
14146 && part_die->has_children
14147 && (part_die->tag == DW_TAG_class_type
14148 || part_die->tag == DW_TAG_structure_type
14149 || part_die->tag == DW_TAG_union_type))
14150 guess_partial_die_structure_name (part_die, cu);
14151
53832f31
TT
14152 /* GCC might emit a nameless struct or union that has a linkage
14153 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14154 if (part_die->name == NULL
96408a79
SA
14155 && (part_die->tag == DW_TAG_class_type
14156 || part_die->tag == DW_TAG_interface_type
14157 || part_die->tag == DW_TAG_structure_type
14158 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14159 && part_die->linkage_name != NULL)
14160 {
14161 char *demangled;
14162
8de20a37 14163 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14164 if (demangled)
14165 {
96408a79
SA
14166 const char *base;
14167
14168 /* Strip any leading namespaces/classes, keep only the base name.
14169 DW_AT_name for named DIEs does not contain the prefixes. */
14170 base = strrchr (demangled, ':');
14171 if (base && base > demangled && base[-1] == ':')
14172 base++;
14173 else
14174 base = demangled;
14175
10f0c4bb
TT
14176 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14177 base, strlen (base));
53832f31
TT
14178 xfree (demangled);
14179 }
14180 }
14181
abc72ce4 14182 part_die->fixup_called = 1;
72bf9492
DJ
14183}
14184
a8329558 14185/* Read an attribute value described by an attribute form. */
c906108c 14186
d521ce57 14187static const gdb_byte *
dee91e82
DE
14188read_attribute_value (const struct die_reader_specs *reader,
14189 struct attribute *attr, unsigned form,
d521ce57 14190 const gdb_byte *info_ptr)
c906108c 14191{
dee91e82
DE
14192 struct dwarf2_cu *cu = reader->cu;
14193 bfd *abfd = reader->abfd;
e7c27a73 14194 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14195 unsigned int bytes_read;
14196 struct dwarf_block *blk;
14197
a8329558
KW
14198 attr->form = form;
14199 switch (form)
c906108c 14200 {
c906108c 14201 case DW_FORM_ref_addr:
ae411497 14202 if (cu->header.version == 2)
4568ecf9 14203 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14204 else
4568ecf9
DE
14205 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14206 &cu->header, &bytes_read);
ae411497
TT
14207 info_ptr += bytes_read;
14208 break;
36586728
TT
14209 case DW_FORM_GNU_ref_alt:
14210 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14211 info_ptr += bytes_read;
14212 break;
ae411497 14213 case DW_FORM_addr:
e7c27a73 14214 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14215 info_ptr += bytes_read;
c906108c
SS
14216 break;
14217 case DW_FORM_block2:
7b5a2f43 14218 blk = dwarf_alloc_block (cu);
c906108c
SS
14219 blk->size = read_2_bytes (abfd, info_ptr);
14220 info_ptr += 2;
14221 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14222 info_ptr += blk->size;
14223 DW_BLOCK (attr) = blk;
14224 break;
14225 case DW_FORM_block4:
7b5a2f43 14226 blk = dwarf_alloc_block (cu);
c906108c
SS
14227 blk->size = read_4_bytes (abfd, info_ptr);
14228 info_ptr += 4;
14229 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14230 info_ptr += blk->size;
14231 DW_BLOCK (attr) = blk;
14232 break;
14233 case DW_FORM_data2:
14234 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14235 info_ptr += 2;
14236 break;
14237 case DW_FORM_data4:
14238 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14239 info_ptr += 4;
14240 break;
14241 case DW_FORM_data8:
14242 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14243 info_ptr += 8;
14244 break;
2dc7f7b3
TT
14245 case DW_FORM_sec_offset:
14246 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14247 info_ptr += bytes_read;
14248 break;
c906108c 14249 case DW_FORM_string:
9b1c24c8 14250 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14251 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14252 info_ptr += bytes_read;
14253 break;
4bdf3d34 14254 case DW_FORM_strp:
36586728
TT
14255 if (!cu->per_cu->is_dwz)
14256 {
14257 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14258 &bytes_read);
14259 DW_STRING_IS_CANONICAL (attr) = 0;
14260 info_ptr += bytes_read;
14261 break;
14262 }
14263 /* FALLTHROUGH */
14264 case DW_FORM_GNU_strp_alt:
14265 {
14266 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14267 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14268 &bytes_read);
14269
14270 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14271 DW_STRING_IS_CANONICAL (attr) = 0;
14272 info_ptr += bytes_read;
14273 }
4bdf3d34 14274 break;
2dc7f7b3 14275 case DW_FORM_exprloc:
c906108c 14276 case DW_FORM_block:
7b5a2f43 14277 blk = dwarf_alloc_block (cu);
c906108c
SS
14278 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14279 info_ptr += bytes_read;
14280 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14281 info_ptr += blk->size;
14282 DW_BLOCK (attr) = blk;
14283 break;
14284 case DW_FORM_block1:
7b5a2f43 14285 blk = dwarf_alloc_block (cu);
c906108c
SS
14286 blk->size = read_1_byte (abfd, info_ptr);
14287 info_ptr += 1;
14288 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14289 info_ptr += blk->size;
14290 DW_BLOCK (attr) = blk;
14291 break;
14292 case DW_FORM_data1:
14293 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14294 info_ptr += 1;
14295 break;
14296 case DW_FORM_flag:
14297 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14298 info_ptr += 1;
14299 break;
2dc7f7b3
TT
14300 case DW_FORM_flag_present:
14301 DW_UNSND (attr) = 1;
14302 break;
c906108c
SS
14303 case DW_FORM_sdata:
14304 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14305 info_ptr += bytes_read;
14306 break;
14307 case DW_FORM_udata:
14308 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14309 info_ptr += bytes_read;
14310 break;
14311 case DW_FORM_ref1:
4568ecf9
DE
14312 DW_UNSND (attr) = (cu->header.offset.sect_off
14313 + read_1_byte (abfd, info_ptr));
c906108c
SS
14314 info_ptr += 1;
14315 break;
14316 case DW_FORM_ref2:
4568ecf9
DE
14317 DW_UNSND (attr) = (cu->header.offset.sect_off
14318 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14319 info_ptr += 2;
14320 break;
14321 case DW_FORM_ref4:
4568ecf9
DE
14322 DW_UNSND (attr) = (cu->header.offset.sect_off
14323 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14324 info_ptr += 4;
14325 break;
613e1657 14326 case DW_FORM_ref8:
4568ecf9
DE
14327 DW_UNSND (attr) = (cu->header.offset.sect_off
14328 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14329 info_ptr += 8;
14330 break;
55f1336d 14331 case DW_FORM_ref_sig8:
348e048f
DE
14332 /* Convert the signature to something we can record in DW_UNSND
14333 for later lookup.
14334 NOTE: This is NULL if the type wasn't found. */
14335 DW_SIGNATURED_TYPE (attr) =
e319fa28 14336 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14337 info_ptr += 8;
14338 break;
c906108c 14339 case DW_FORM_ref_udata:
4568ecf9
DE
14340 DW_UNSND (attr) = (cu->header.offset.sect_off
14341 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14342 info_ptr += bytes_read;
14343 break;
c906108c 14344 case DW_FORM_indirect:
a8329558
KW
14345 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14346 info_ptr += bytes_read;
dee91e82 14347 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14348 break;
3019eac3
DE
14349 case DW_FORM_GNU_addr_index:
14350 if (reader->dwo_file == NULL)
14351 {
14352 /* For now flag a hard error.
14353 Later we can turn this into a complaint. */
14354 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14355 dwarf_form_name (form),
14356 bfd_get_filename (abfd));
14357 }
14358 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14359 info_ptr += bytes_read;
14360 break;
14361 case DW_FORM_GNU_str_index:
14362 if (reader->dwo_file == NULL)
14363 {
14364 /* For now flag a hard error.
14365 Later we can turn this into a complaint if warranted. */
14366 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14367 dwarf_form_name (form),
14368 bfd_get_filename (abfd));
14369 }
14370 {
14371 ULONGEST str_index =
14372 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14373
14374 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14375 DW_STRING_IS_CANONICAL (attr) = 0;
14376 info_ptr += bytes_read;
14377 }
14378 break;
c906108c 14379 default:
8a3fe4f8 14380 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14381 dwarf_form_name (form),
14382 bfd_get_filename (abfd));
c906108c 14383 }
28e94949 14384
36586728
TT
14385 /* Super hack. */
14386 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14387 attr->form = DW_FORM_GNU_ref_alt;
14388
28e94949
JB
14389 /* We have seen instances where the compiler tried to emit a byte
14390 size attribute of -1 which ended up being encoded as an unsigned
14391 0xffffffff. Although 0xffffffff is technically a valid size value,
14392 an object of this size seems pretty unlikely so we can relatively
14393 safely treat these cases as if the size attribute was invalid and
14394 treat them as zero by default. */
14395 if (attr->name == DW_AT_byte_size
14396 && form == DW_FORM_data4
14397 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14398 {
14399 complaint
14400 (&symfile_complaints,
43bbcdc2
PH
14401 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14402 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14403 DW_UNSND (attr) = 0;
14404 }
28e94949 14405
c906108c
SS
14406 return info_ptr;
14407}
14408
a8329558
KW
14409/* Read an attribute described by an abbreviated attribute. */
14410
d521ce57 14411static const gdb_byte *
dee91e82
DE
14412read_attribute (const struct die_reader_specs *reader,
14413 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14414 const gdb_byte *info_ptr)
a8329558
KW
14415{
14416 attr->name = abbrev->name;
dee91e82 14417 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14418}
14419
0963b4bd 14420/* Read dwarf information from a buffer. */
c906108c
SS
14421
14422static unsigned int
a1855c1d 14423read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14424{
fe1b8b76 14425 return bfd_get_8 (abfd, buf);
c906108c
SS
14426}
14427
14428static int
a1855c1d 14429read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14430{
fe1b8b76 14431 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14432}
14433
14434static unsigned int
a1855c1d 14435read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14436{
fe1b8b76 14437 return bfd_get_16 (abfd, buf);
c906108c
SS
14438}
14439
21ae7a4d 14440static int
a1855c1d 14441read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14442{
14443 return bfd_get_signed_16 (abfd, buf);
14444}
14445
c906108c 14446static unsigned int
a1855c1d 14447read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14448{
fe1b8b76 14449 return bfd_get_32 (abfd, buf);
c906108c
SS
14450}
14451
21ae7a4d 14452static int
a1855c1d 14453read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14454{
14455 return bfd_get_signed_32 (abfd, buf);
14456}
14457
93311388 14458static ULONGEST
a1855c1d 14459read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14460{
fe1b8b76 14461 return bfd_get_64 (abfd, buf);
c906108c
SS
14462}
14463
14464static CORE_ADDR
d521ce57 14465read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14466 unsigned int *bytes_read)
c906108c 14467{
e7c27a73 14468 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14469 CORE_ADDR retval = 0;
14470
107d2387 14471 if (cu_header->signed_addr_p)
c906108c 14472 {
107d2387
AC
14473 switch (cu_header->addr_size)
14474 {
14475 case 2:
fe1b8b76 14476 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14477 break;
14478 case 4:
fe1b8b76 14479 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14480 break;
14481 case 8:
fe1b8b76 14482 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14483 break;
14484 default:
8e65ff28 14485 internal_error (__FILE__, __LINE__,
e2e0b3e5 14486 _("read_address: bad switch, signed [in module %s]"),
659b0389 14487 bfd_get_filename (abfd));
107d2387
AC
14488 }
14489 }
14490 else
14491 {
14492 switch (cu_header->addr_size)
14493 {
14494 case 2:
fe1b8b76 14495 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14496 break;
14497 case 4:
fe1b8b76 14498 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14499 break;
14500 case 8:
fe1b8b76 14501 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14502 break;
14503 default:
8e65ff28 14504 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14505 _("read_address: bad switch, "
14506 "unsigned [in module %s]"),
659b0389 14507 bfd_get_filename (abfd));
107d2387 14508 }
c906108c 14509 }
64367e0a 14510
107d2387
AC
14511 *bytes_read = cu_header->addr_size;
14512 return retval;
c906108c
SS
14513}
14514
f7ef9339 14515/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14516 specification allows the initial length to take up either 4 bytes
14517 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14518 bytes describe the length and all offsets will be 8 bytes in length
14519 instead of 4.
14520
f7ef9339
KB
14521 An older, non-standard 64-bit format is also handled by this
14522 function. The older format in question stores the initial length
14523 as an 8-byte quantity without an escape value. Lengths greater
14524 than 2^32 aren't very common which means that the initial 4 bytes
14525 is almost always zero. Since a length value of zero doesn't make
14526 sense for the 32-bit format, this initial zero can be considered to
14527 be an escape value which indicates the presence of the older 64-bit
14528 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14529 greater than 4GB. If it becomes necessary to handle lengths
14530 somewhat larger than 4GB, we could allow other small values (such
14531 as the non-sensical values of 1, 2, and 3) to also be used as
14532 escape values indicating the presence of the old format.
f7ef9339 14533
917c78fc
MK
14534 The value returned via bytes_read should be used to increment the
14535 relevant pointer after calling read_initial_length().
c764a876 14536
613e1657
KB
14537 [ Note: read_initial_length() and read_offset() are based on the
14538 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14539 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14540 from:
14541
f7ef9339 14542 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14543
613e1657
KB
14544 This document is only a draft and is subject to change. (So beware.)
14545
f7ef9339 14546 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14547 determined empirically by examining 64-bit ELF files produced by
14548 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14549
14550 - Kevin, July 16, 2002
613e1657
KB
14551 ] */
14552
14553static LONGEST
d521ce57 14554read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 14555{
fe1b8b76 14556 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14557
dd373385 14558 if (length == 0xffffffff)
613e1657 14559 {
fe1b8b76 14560 length = bfd_get_64 (abfd, buf + 4);
613e1657 14561 *bytes_read = 12;
613e1657 14562 }
dd373385 14563 else if (length == 0)
f7ef9339 14564 {
dd373385 14565 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14566 length = bfd_get_64 (abfd, buf);
f7ef9339 14567 *bytes_read = 8;
f7ef9339 14568 }
613e1657
KB
14569 else
14570 {
14571 *bytes_read = 4;
613e1657
KB
14572 }
14573
c764a876
DE
14574 return length;
14575}
dd373385 14576
c764a876
DE
14577/* Cover function for read_initial_length.
14578 Returns the length of the object at BUF, and stores the size of the
14579 initial length in *BYTES_READ and stores the size that offsets will be in
14580 *OFFSET_SIZE.
14581 If the initial length size is not equivalent to that specified in
14582 CU_HEADER then issue a complaint.
14583 This is useful when reading non-comp-unit headers. */
dd373385 14584
c764a876 14585static LONGEST
d521ce57 14586read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
14587 const struct comp_unit_head *cu_header,
14588 unsigned int *bytes_read,
14589 unsigned int *offset_size)
14590{
14591 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14592
14593 gdb_assert (cu_header->initial_length_size == 4
14594 || cu_header->initial_length_size == 8
14595 || cu_header->initial_length_size == 12);
14596
14597 if (cu_header->initial_length_size != *bytes_read)
14598 complaint (&symfile_complaints,
14599 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14600
c764a876 14601 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14602 return length;
613e1657
KB
14603}
14604
14605/* Read an offset from the data stream. The size of the offset is
917c78fc 14606 given by cu_header->offset_size. */
613e1657
KB
14607
14608static LONGEST
d521ce57
TT
14609read_offset (bfd *abfd, const gdb_byte *buf,
14610 const struct comp_unit_head *cu_header,
891d2f0b 14611 unsigned int *bytes_read)
c764a876
DE
14612{
14613 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14614
c764a876
DE
14615 *bytes_read = cu_header->offset_size;
14616 return offset;
14617}
14618
14619/* Read an offset from the data stream. */
14620
14621static LONGEST
d521ce57 14622read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14623{
14624 LONGEST retval = 0;
14625
c764a876 14626 switch (offset_size)
613e1657
KB
14627 {
14628 case 4:
fe1b8b76 14629 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14630 break;
14631 case 8:
fe1b8b76 14632 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14633 break;
14634 default:
8e65ff28 14635 internal_error (__FILE__, __LINE__,
c764a876 14636 _("read_offset_1: bad switch [in module %s]"),
659b0389 14637 bfd_get_filename (abfd));
613e1657
KB
14638 }
14639
917c78fc 14640 return retval;
613e1657
KB
14641}
14642
d521ce57
TT
14643static const gdb_byte *
14644read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
14645{
14646 /* If the size of a host char is 8 bits, we can return a pointer
14647 to the buffer, otherwise we have to copy the data to a buffer
14648 allocated on the temporary obstack. */
4bdf3d34 14649 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14650 return buf;
c906108c
SS
14651}
14652
d521ce57
TT
14653static const char *
14654read_direct_string (bfd *abfd, const gdb_byte *buf,
14655 unsigned int *bytes_read_ptr)
c906108c
SS
14656{
14657 /* If the size of a host char is 8 bits, we can return a pointer
14658 to the string, otherwise we have to copy the string to a buffer
14659 allocated on the temporary obstack. */
4bdf3d34 14660 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14661 if (*buf == '\0')
14662 {
14663 *bytes_read_ptr = 1;
14664 return NULL;
14665 }
d521ce57
TT
14666 *bytes_read_ptr = strlen ((const char *) buf) + 1;
14667 return (const char *) buf;
4bdf3d34
JJ
14668}
14669
d521ce57 14670static const char *
cf2c3c16 14671read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14672{
be391dca 14673 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14674 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14675 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14676 bfd_get_filename (abfd));
dce234bc 14677 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14678 error (_("DW_FORM_strp pointing outside of "
14679 ".debug_str section [in module %s]"),
14680 bfd_get_filename (abfd));
4bdf3d34 14681 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14682 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14683 return NULL;
d521ce57 14684 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14685}
14686
36586728
TT
14687/* Read a string at offset STR_OFFSET in the .debug_str section from
14688 the .dwz file DWZ. Throw an error if the offset is too large. If
14689 the string consists of a single NUL byte, return NULL; otherwise
14690 return a pointer to the string. */
14691
d521ce57 14692static const char *
36586728
TT
14693read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14694{
14695 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14696
14697 if (dwz->str.buffer == NULL)
14698 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14699 "section [in module %s]"),
14700 bfd_get_filename (dwz->dwz_bfd));
14701 if (str_offset >= dwz->str.size)
14702 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14703 ".debug_str section [in module %s]"),
14704 bfd_get_filename (dwz->dwz_bfd));
14705 gdb_assert (HOST_CHAR_BIT == 8);
14706 if (dwz->str.buffer[str_offset] == '\0')
14707 return NULL;
d521ce57 14708 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
14709}
14710
d521ce57
TT
14711static const char *
14712read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
14713 const struct comp_unit_head *cu_header,
14714 unsigned int *bytes_read_ptr)
14715{
14716 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14717
14718 return read_indirect_string_at_offset (abfd, str_offset);
14719}
14720
12df843f 14721static ULONGEST
d521ce57
TT
14722read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
14723 unsigned int *bytes_read_ptr)
c906108c 14724{
12df843f 14725 ULONGEST result;
ce5d95e1 14726 unsigned int num_read;
c906108c
SS
14727 int i, shift;
14728 unsigned char byte;
14729
14730 result = 0;
14731 shift = 0;
14732 num_read = 0;
14733 i = 0;
14734 while (1)
14735 {
fe1b8b76 14736 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14737 buf++;
14738 num_read++;
12df843f 14739 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14740 if ((byte & 128) == 0)
14741 {
14742 break;
14743 }
14744 shift += 7;
14745 }
14746 *bytes_read_ptr = num_read;
14747 return result;
14748}
14749
12df843f 14750static LONGEST
d521ce57
TT
14751read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
14752 unsigned int *bytes_read_ptr)
c906108c 14753{
12df843f 14754 LONGEST result;
77e0b926 14755 int i, shift, num_read;
c906108c
SS
14756 unsigned char byte;
14757
14758 result = 0;
14759 shift = 0;
c906108c
SS
14760 num_read = 0;
14761 i = 0;
14762 while (1)
14763 {
fe1b8b76 14764 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14765 buf++;
14766 num_read++;
12df843f 14767 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14768 shift += 7;
14769 if ((byte & 128) == 0)
14770 {
14771 break;
14772 }
14773 }
77e0b926 14774 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14775 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14776 *bytes_read_ptr = num_read;
14777 return result;
14778}
14779
3019eac3
DE
14780/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14781 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14782 ADDR_SIZE is the size of addresses from the CU header. */
14783
14784static CORE_ADDR
14785read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14786{
14787 struct objfile *objfile = dwarf2_per_objfile->objfile;
14788 bfd *abfd = objfile->obfd;
14789 const gdb_byte *info_ptr;
14790
14791 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14792 if (dwarf2_per_objfile->addr.buffer == NULL)
14793 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14794 objfile->name);
14795 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14796 error (_("DW_FORM_addr_index pointing outside of "
14797 ".debug_addr section [in module %s]"),
14798 objfile->name);
14799 info_ptr = (dwarf2_per_objfile->addr.buffer
14800 + addr_base + addr_index * addr_size);
14801 if (addr_size == 4)
14802 return bfd_get_32 (abfd, info_ptr);
14803 else
14804 return bfd_get_64 (abfd, info_ptr);
14805}
14806
14807/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14808
14809static CORE_ADDR
14810read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14811{
14812 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14813}
14814
14815/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14816
14817static CORE_ADDR
d521ce57 14818read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
14819 unsigned int *bytes_read)
14820{
14821 bfd *abfd = cu->objfile->obfd;
14822 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14823
14824 return read_addr_index (cu, addr_index);
14825}
14826
14827/* Data structure to pass results from dwarf2_read_addr_index_reader
14828 back to dwarf2_read_addr_index. */
14829
14830struct dwarf2_read_addr_index_data
14831{
14832 ULONGEST addr_base;
14833 int addr_size;
14834};
14835
14836/* die_reader_func for dwarf2_read_addr_index. */
14837
14838static void
14839dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 14840 const gdb_byte *info_ptr,
3019eac3
DE
14841 struct die_info *comp_unit_die,
14842 int has_children,
14843 void *data)
14844{
14845 struct dwarf2_cu *cu = reader->cu;
14846 struct dwarf2_read_addr_index_data *aidata =
14847 (struct dwarf2_read_addr_index_data *) data;
14848
14849 aidata->addr_base = cu->addr_base;
14850 aidata->addr_size = cu->header.addr_size;
14851}
14852
14853/* Given an index in .debug_addr, fetch the value.
14854 NOTE: This can be called during dwarf expression evaluation,
14855 long after the debug information has been read, and thus per_cu->cu
14856 may no longer exist. */
14857
14858CORE_ADDR
14859dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14860 unsigned int addr_index)
14861{
14862 struct objfile *objfile = per_cu->objfile;
14863 struct dwarf2_cu *cu = per_cu->cu;
14864 ULONGEST addr_base;
14865 int addr_size;
14866
14867 /* This is intended to be called from outside this file. */
14868 dw2_setup (objfile);
14869
14870 /* We need addr_base and addr_size.
14871 If we don't have PER_CU->cu, we have to get it.
14872 Nasty, but the alternative is storing the needed info in PER_CU,
14873 which at this point doesn't seem justified: it's not clear how frequently
14874 it would get used and it would increase the size of every PER_CU.
14875 Entry points like dwarf2_per_cu_addr_size do a similar thing
14876 so we're not in uncharted territory here.
14877 Alas we need to be a bit more complicated as addr_base is contained
14878 in the DIE.
14879
14880 We don't need to read the entire CU(/TU).
14881 We just need the header and top level die.
a1b64ce1 14882
3019eac3 14883 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14884 For now we skip this optimization. */
3019eac3
DE
14885
14886 if (cu != NULL)
14887 {
14888 addr_base = cu->addr_base;
14889 addr_size = cu->header.addr_size;
14890 }
14891 else
14892 {
14893 struct dwarf2_read_addr_index_data aidata;
14894
a1b64ce1
DE
14895 /* Note: We can't use init_cutu_and_read_dies_simple here,
14896 we need addr_base. */
14897 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14898 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14899 addr_base = aidata.addr_base;
14900 addr_size = aidata.addr_size;
14901 }
14902
14903 return read_addr_index_1 (addr_index, addr_base, addr_size);
14904}
14905
14906/* Given a DW_AT_str_index, fetch the string. */
14907
d521ce57 14908static const char *
3019eac3
DE
14909read_str_index (const struct die_reader_specs *reader,
14910 struct dwarf2_cu *cu, ULONGEST str_index)
14911{
14912 struct objfile *objfile = dwarf2_per_objfile->objfile;
14913 const char *dwo_name = objfile->name;
14914 bfd *abfd = objfile->obfd;
14915 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 14916 const gdb_byte *info_ptr;
3019eac3
DE
14917 ULONGEST str_offset;
14918
14919 dwarf2_read_section (objfile, &sections->str);
14920 dwarf2_read_section (objfile, &sections->str_offsets);
14921 if (sections->str.buffer == NULL)
14922 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14923 " in CU at offset 0x%lx [in module %s]"),
14924 (long) cu->header.offset.sect_off, dwo_name);
14925 if (sections->str_offsets.buffer == NULL)
14926 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14927 " in CU at offset 0x%lx [in module %s]"),
14928 (long) cu->header.offset.sect_off, dwo_name);
14929 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14930 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14931 " section in CU at offset 0x%lx [in module %s]"),
14932 (long) cu->header.offset.sect_off, dwo_name);
14933 info_ptr = (sections->str_offsets.buffer
14934 + str_index * cu->header.offset_size);
14935 if (cu->header.offset_size == 4)
14936 str_offset = bfd_get_32 (abfd, info_ptr);
14937 else
14938 str_offset = bfd_get_64 (abfd, info_ptr);
14939 if (str_offset >= sections->str.size)
14940 error (_("Offset from DW_FORM_str_index pointing outside of"
14941 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14942 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 14943 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
14944}
14945
3019eac3
DE
14946/* Return the length of an LEB128 number in BUF. */
14947
14948static int
14949leb128_size (const gdb_byte *buf)
14950{
14951 const gdb_byte *begin = buf;
14952 gdb_byte byte;
14953
14954 while (1)
14955 {
14956 byte = *buf++;
14957 if ((byte & 128) == 0)
14958 return buf - begin;
14959 }
14960}
14961
c906108c 14962static void
e142c38c 14963set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14964{
14965 switch (lang)
14966 {
14967 case DW_LANG_C89:
76bee0cc 14968 case DW_LANG_C99:
c906108c 14969 case DW_LANG_C:
e142c38c 14970 cu->language = language_c;
c906108c
SS
14971 break;
14972 case DW_LANG_C_plus_plus:
e142c38c 14973 cu->language = language_cplus;
c906108c 14974 break;
6aecb9c2
JB
14975 case DW_LANG_D:
14976 cu->language = language_d;
14977 break;
c906108c
SS
14978 case DW_LANG_Fortran77:
14979 case DW_LANG_Fortran90:
b21b22e0 14980 case DW_LANG_Fortran95:
e142c38c 14981 cu->language = language_fortran;
c906108c 14982 break;
a766d390
DE
14983 case DW_LANG_Go:
14984 cu->language = language_go;
14985 break;
c906108c 14986 case DW_LANG_Mips_Assembler:
e142c38c 14987 cu->language = language_asm;
c906108c 14988 break;
bebd888e 14989 case DW_LANG_Java:
e142c38c 14990 cu->language = language_java;
bebd888e 14991 break;
c906108c 14992 case DW_LANG_Ada83:
8aaf0b47 14993 case DW_LANG_Ada95:
bc5f45f8
JB
14994 cu->language = language_ada;
14995 break;
72019c9c
GM
14996 case DW_LANG_Modula2:
14997 cu->language = language_m2;
14998 break;
fe8e67fd
PM
14999 case DW_LANG_Pascal83:
15000 cu->language = language_pascal;
15001 break;
22566fbd
DJ
15002 case DW_LANG_ObjC:
15003 cu->language = language_objc;
15004 break;
c906108c
SS
15005 case DW_LANG_Cobol74:
15006 case DW_LANG_Cobol85:
c906108c 15007 default:
e142c38c 15008 cu->language = language_minimal;
c906108c
SS
15009 break;
15010 }
e142c38c 15011 cu->language_defn = language_def (cu->language);
c906108c
SS
15012}
15013
15014/* Return the named attribute or NULL if not there. */
15015
15016static struct attribute *
e142c38c 15017dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15018{
a48e046c 15019 for (;;)
c906108c 15020 {
a48e046c
TT
15021 unsigned int i;
15022 struct attribute *spec = NULL;
15023
15024 for (i = 0; i < die->num_attrs; ++i)
15025 {
15026 if (die->attrs[i].name == name)
15027 return &die->attrs[i];
15028 if (die->attrs[i].name == DW_AT_specification
15029 || die->attrs[i].name == DW_AT_abstract_origin)
15030 spec = &die->attrs[i];
15031 }
15032
15033 if (!spec)
15034 break;
c906108c 15035
f2f0e013 15036 die = follow_die_ref (die, spec, &cu);
f2f0e013 15037 }
c5aa993b 15038
c906108c
SS
15039 return NULL;
15040}
15041
348e048f
DE
15042/* Return the named attribute or NULL if not there,
15043 but do not follow DW_AT_specification, etc.
15044 This is for use in contexts where we're reading .debug_types dies.
15045 Following DW_AT_specification, DW_AT_abstract_origin will take us
15046 back up the chain, and we want to go down. */
15047
15048static struct attribute *
45e58e77 15049dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15050{
15051 unsigned int i;
15052
15053 for (i = 0; i < die->num_attrs; ++i)
15054 if (die->attrs[i].name == name)
15055 return &die->attrs[i];
15056
15057 return NULL;
15058}
15059
05cf31d1
JB
15060/* Return non-zero iff the attribute NAME is defined for the given DIE,
15061 and holds a non-zero value. This function should only be used for
2dc7f7b3 15062 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15063
15064static int
15065dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15066{
15067 struct attribute *attr = dwarf2_attr (die, name, cu);
15068
15069 return (attr && DW_UNSND (attr));
15070}
15071
3ca72b44 15072static int
e142c38c 15073die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15074{
05cf31d1
JB
15075 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15076 which value is non-zero. However, we have to be careful with
15077 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15078 (via dwarf2_flag_true_p) follows this attribute. So we may
15079 end up accidently finding a declaration attribute that belongs
15080 to a different DIE referenced by the specification attribute,
15081 even though the given DIE does not have a declaration attribute. */
15082 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15083 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15084}
15085
63d06c5c 15086/* Return the die giving the specification for DIE, if there is
f2f0e013 15087 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15088 containing the return value on output. If there is no
15089 specification, but there is an abstract origin, that is
15090 returned. */
63d06c5c
DC
15091
15092static struct die_info *
f2f0e013 15093die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15094{
f2f0e013
DJ
15095 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15096 *spec_cu);
63d06c5c 15097
edb3359d
DJ
15098 if (spec_attr == NULL)
15099 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15100
63d06c5c
DC
15101 if (spec_attr == NULL)
15102 return NULL;
15103 else
f2f0e013 15104 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15105}
c906108c 15106
debd256d 15107/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15108 refers to.
15109 NOTE: This is also used as a "cleanup" function. */
15110
debd256d
JB
15111static void
15112free_line_header (struct line_header *lh)
15113{
15114 if (lh->standard_opcode_lengths)
a8bc7b56 15115 xfree (lh->standard_opcode_lengths);
debd256d
JB
15116
15117 /* Remember that all the lh->file_names[i].name pointers are
15118 pointers into debug_line_buffer, and don't need to be freed. */
15119 if (lh->file_names)
a8bc7b56 15120 xfree (lh->file_names);
debd256d
JB
15121
15122 /* Similarly for the include directory names. */
15123 if (lh->include_dirs)
a8bc7b56 15124 xfree (lh->include_dirs);
debd256d 15125
a8bc7b56 15126 xfree (lh);
debd256d
JB
15127}
15128
debd256d 15129/* Add an entry to LH's include directory table. */
ae2de4f8 15130
debd256d 15131static void
d521ce57 15132add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15133{
debd256d
JB
15134 /* Grow the array if necessary. */
15135 if (lh->include_dirs_size == 0)
c5aa993b 15136 {
debd256d
JB
15137 lh->include_dirs_size = 1; /* for testing */
15138 lh->include_dirs = xmalloc (lh->include_dirs_size
15139 * sizeof (*lh->include_dirs));
15140 }
15141 else if (lh->num_include_dirs >= lh->include_dirs_size)
15142 {
15143 lh->include_dirs_size *= 2;
15144 lh->include_dirs = xrealloc (lh->include_dirs,
15145 (lh->include_dirs_size
15146 * sizeof (*lh->include_dirs)));
c5aa993b 15147 }
c906108c 15148
debd256d
JB
15149 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15150}
6e70227d 15151
debd256d 15152/* Add an entry to LH's file name table. */
ae2de4f8 15153
debd256d
JB
15154static void
15155add_file_name (struct line_header *lh,
d521ce57 15156 const char *name,
debd256d
JB
15157 unsigned int dir_index,
15158 unsigned int mod_time,
15159 unsigned int length)
15160{
15161 struct file_entry *fe;
15162
15163 /* Grow the array if necessary. */
15164 if (lh->file_names_size == 0)
15165 {
15166 lh->file_names_size = 1; /* for testing */
15167 lh->file_names = xmalloc (lh->file_names_size
15168 * sizeof (*lh->file_names));
15169 }
15170 else if (lh->num_file_names >= lh->file_names_size)
15171 {
15172 lh->file_names_size *= 2;
15173 lh->file_names = xrealloc (lh->file_names,
15174 (lh->file_names_size
15175 * sizeof (*lh->file_names)));
15176 }
15177
15178 fe = &lh->file_names[lh->num_file_names++];
15179 fe->name = name;
15180 fe->dir_index = dir_index;
15181 fe->mod_time = mod_time;
15182 fe->length = length;
aaa75496 15183 fe->included_p = 0;
cb1df416 15184 fe->symtab = NULL;
debd256d 15185}
6e70227d 15186
36586728
TT
15187/* A convenience function to find the proper .debug_line section for a
15188 CU. */
15189
15190static struct dwarf2_section_info *
15191get_debug_line_section (struct dwarf2_cu *cu)
15192{
15193 struct dwarf2_section_info *section;
15194
15195 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15196 DWO file. */
15197 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15198 section = &cu->dwo_unit->dwo_file->sections.line;
15199 else if (cu->per_cu->is_dwz)
15200 {
15201 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15202
15203 section = &dwz->line;
15204 }
15205 else
15206 section = &dwarf2_per_objfile->line;
15207
15208 return section;
15209}
15210
debd256d 15211/* Read the statement program header starting at OFFSET in
3019eac3 15212 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15213 to a struct line_header, allocated using xmalloc.
debd256d
JB
15214
15215 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15216 the returned object point into the dwarf line section buffer,
15217 and must not be freed. */
ae2de4f8 15218
debd256d 15219static struct line_header *
3019eac3 15220dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15221{
15222 struct cleanup *back_to;
15223 struct line_header *lh;
d521ce57 15224 const gdb_byte *line_ptr;
c764a876 15225 unsigned int bytes_read, offset_size;
debd256d 15226 int i;
d521ce57 15227 const char *cur_dir, *cur_file;
3019eac3
DE
15228 struct dwarf2_section_info *section;
15229 bfd *abfd;
15230
36586728 15231 section = get_debug_line_section (cu);
3019eac3
DE
15232 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15233 if (section->buffer == NULL)
debd256d 15234 {
3019eac3
DE
15235 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15236 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15237 else
15238 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15239 return 0;
15240 }
15241
fceca515
DE
15242 /* We can't do this until we know the section is non-empty.
15243 Only then do we know we have such a section. */
15244 abfd = section->asection->owner;
15245
a738430d
MK
15246 /* Make sure that at least there's room for the total_length field.
15247 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15248 if (offset + 4 >= section->size)
debd256d 15249 {
4d3c2250 15250 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15251 return 0;
15252 }
15253
15254 lh = xmalloc (sizeof (*lh));
15255 memset (lh, 0, sizeof (*lh));
15256 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15257 (void *) lh);
15258
3019eac3 15259 line_ptr = section->buffer + offset;
debd256d 15260
a738430d 15261 /* Read in the header. */
6e70227d 15262 lh->total_length =
c764a876
DE
15263 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15264 &bytes_read, &offset_size);
debd256d 15265 line_ptr += bytes_read;
3019eac3 15266 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15267 {
4d3c2250 15268 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15269 return 0;
15270 }
15271 lh->statement_program_end = line_ptr + lh->total_length;
15272 lh->version = read_2_bytes (abfd, line_ptr);
15273 line_ptr += 2;
c764a876
DE
15274 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15275 line_ptr += offset_size;
debd256d
JB
15276 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15277 line_ptr += 1;
2dc7f7b3
TT
15278 if (lh->version >= 4)
15279 {
15280 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15281 line_ptr += 1;
15282 }
15283 else
15284 lh->maximum_ops_per_instruction = 1;
15285
15286 if (lh->maximum_ops_per_instruction == 0)
15287 {
15288 lh->maximum_ops_per_instruction = 1;
15289 complaint (&symfile_complaints,
3e43a32a
MS
15290 _("invalid maximum_ops_per_instruction "
15291 "in `.debug_line' section"));
2dc7f7b3
TT
15292 }
15293
debd256d
JB
15294 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15295 line_ptr += 1;
15296 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15297 line_ptr += 1;
15298 lh->line_range = read_1_byte (abfd, line_ptr);
15299 line_ptr += 1;
15300 lh->opcode_base = read_1_byte (abfd, line_ptr);
15301 line_ptr += 1;
15302 lh->standard_opcode_lengths
fe1b8b76 15303 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15304
15305 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15306 for (i = 1; i < lh->opcode_base; ++i)
15307 {
15308 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15309 line_ptr += 1;
15310 }
15311
a738430d 15312 /* Read directory table. */
9b1c24c8 15313 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15314 {
15315 line_ptr += bytes_read;
15316 add_include_dir (lh, cur_dir);
15317 }
15318 line_ptr += bytes_read;
15319
a738430d 15320 /* Read file name table. */
9b1c24c8 15321 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15322 {
15323 unsigned int dir_index, mod_time, length;
15324
15325 line_ptr += bytes_read;
15326 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15327 line_ptr += bytes_read;
15328 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15329 line_ptr += bytes_read;
15330 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15331 line_ptr += bytes_read;
15332
15333 add_file_name (lh, cur_file, dir_index, mod_time, length);
15334 }
15335 line_ptr += bytes_read;
6e70227d 15336 lh->statement_program_start = line_ptr;
debd256d 15337
3019eac3 15338 if (line_ptr > (section->buffer + section->size))
4d3c2250 15339 complaint (&symfile_complaints,
3e43a32a
MS
15340 _("line number info header doesn't "
15341 "fit in `.debug_line' section"));
debd256d
JB
15342
15343 discard_cleanups (back_to);
15344 return lh;
15345}
c906108c 15346
c6da4cef
DE
15347/* Subroutine of dwarf_decode_lines to simplify it.
15348 Return the file name of the psymtab for included file FILE_INDEX
15349 in line header LH of PST.
15350 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15351 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15352 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15353
15354 The function creates dangling cleanup registration. */
c6da4cef 15355
d521ce57 15356static const char *
c6da4cef
DE
15357psymtab_include_file_name (const struct line_header *lh, int file_index,
15358 const struct partial_symtab *pst,
15359 const char *comp_dir)
15360{
15361 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15362 const char *include_name = fe.name;
15363 const char *include_name_to_compare = include_name;
15364 const char *dir_name = NULL;
72b9f47f
TT
15365 const char *pst_filename;
15366 char *copied_name = NULL;
c6da4cef
DE
15367 int file_is_pst;
15368
15369 if (fe.dir_index)
15370 dir_name = lh->include_dirs[fe.dir_index - 1];
15371
15372 if (!IS_ABSOLUTE_PATH (include_name)
15373 && (dir_name != NULL || comp_dir != NULL))
15374 {
15375 /* Avoid creating a duplicate psymtab for PST.
15376 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15377 Before we do the comparison, however, we need to account
15378 for DIR_NAME and COMP_DIR.
15379 First prepend dir_name (if non-NULL). If we still don't
15380 have an absolute path prepend comp_dir (if non-NULL).
15381 However, the directory we record in the include-file's
15382 psymtab does not contain COMP_DIR (to match the
15383 corresponding symtab(s)).
15384
15385 Example:
15386
15387 bash$ cd /tmp
15388 bash$ gcc -g ./hello.c
15389 include_name = "hello.c"
15390 dir_name = "."
15391 DW_AT_comp_dir = comp_dir = "/tmp"
15392 DW_AT_name = "./hello.c" */
15393
15394 if (dir_name != NULL)
15395 {
d521ce57
TT
15396 char *tem = concat (dir_name, SLASH_STRING,
15397 include_name, (char *)NULL);
15398
15399 make_cleanup (xfree, tem);
15400 include_name = tem;
c6da4cef 15401 include_name_to_compare = include_name;
c6da4cef
DE
15402 }
15403 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15404 {
d521ce57
TT
15405 char *tem = concat (comp_dir, SLASH_STRING,
15406 include_name, (char *)NULL);
15407
15408 make_cleanup (xfree, tem);
15409 include_name_to_compare = tem;
c6da4cef
DE
15410 }
15411 }
15412
15413 pst_filename = pst->filename;
15414 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15415 {
72b9f47f
TT
15416 copied_name = concat (pst->dirname, SLASH_STRING,
15417 pst_filename, (char *)NULL);
15418 pst_filename = copied_name;
c6da4cef
DE
15419 }
15420
1e3fad37 15421 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15422
72b9f47f
TT
15423 if (copied_name != NULL)
15424 xfree (copied_name);
c6da4cef
DE
15425
15426 if (file_is_pst)
15427 return NULL;
15428 return include_name;
15429}
15430
c91513d8
PP
15431/* Ignore this record_line request. */
15432
15433static void
15434noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15435{
15436 return;
15437}
15438
f3f5162e
DE
15439/* Subroutine of dwarf_decode_lines to simplify it.
15440 Process the line number information in LH. */
debd256d 15441
c906108c 15442static void
f3f5162e
DE
15443dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15444 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15445{
d521ce57
TT
15446 const gdb_byte *line_ptr, *extended_end;
15447 const gdb_byte *line_end;
a8c50c1f 15448 unsigned int bytes_read, extended_len;
c906108c 15449 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15450 CORE_ADDR baseaddr;
15451 struct objfile *objfile = cu->objfile;
f3f5162e 15452 bfd *abfd = objfile->obfd;
fbf65064 15453 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15454 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15455 struct subfile *last_subfile = NULL;
c91513d8
PP
15456 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15457 = record_line;
e142c38c
DJ
15458
15459 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15460
debd256d
JB
15461 line_ptr = lh->statement_program_start;
15462 line_end = lh->statement_program_end;
c906108c
SS
15463
15464 /* Read the statement sequences until there's nothing left. */
15465 while (line_ptr < line_end)
15466 {
15467 /* state machine registers */
15468 CORE_ADDR address = 0;
15469 unsigned int file = 1;
15470 unsigned int line = 1;
15471 unsigned int column = 0;
debd256d 15472 int is_stmt = lh->default_is_stmt;
c906108c
SS
15473 int basic_block = 0;
15474 int end_sequence = 0;
fbf65064 15475 CORE_ADDR addr;
2dc7f7b3 15476 unsigned char op_index = 0;
c906108c 15477
aaa75496 15478 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15479 {
aaa75496 15480 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15481 /* lh->include_dirs and lh->file_names are 0-based, but the
15482 directory and file name numbers in the statement program
15483 are 1-based. */
15484 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15485 const char *dir = NULL;
a738430d 15486
debd256d
JB
15487 if (fe->dir_index)
15488 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15489
15490 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15491 }
15492
a738430d 15493 /* Decode the table. */
c5aa993b 15494 while (!end_sequence)
c906108c
SS
15495 {
15496 op_code = read_1_byte (abfd, line_ptr);
15497 line_ptr += 1;
59205f5a
JB
15498 if (line_ptr > line_end)
15499 {
15500 dwarf2_debug_line_missing_end_sequence_complaint ();
15501 break;
15502 }
9aa1fe7e 15503
debd256d 15504 if (op_code >= lh->opcode_base)
6e70227d 15505 {
a738430d 15506 /* Special operand. */
debd256d 15507 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15508 address += (((op_index + (adj_opcode / lh->line_range))
15509 / lh->maximum_ops_per_instruction)
15510 * lh->minimum_instruction_length);
15511 op_index = ((op_index + (adj_opcode / lh->line_range))
15512 % lh->maximum_ops_per_instruction);
debd256d 15513 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15514 if (lh->num_file_names < file || file == 0)
25e43795 15515 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15516 /* For now we ignore lines not starting on an
15517 instruction boundary. */
15518 else if (op_index == 0)
25e43795
DJ
15519 {
15520 lh->file_names[file - 1].included_p = 1;
ca5f395d 15521 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15522 {
15523 if (last_subfile != current_subfile)
15524 {
15525 addr = gdbarch_addr_bits_remove (gdbarch, address);
15526 if (last_subfile)
c91513d8 15527 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15528 last_subfile = current_subfile;
15529 }
25e43795 15530 /* Append row to matrix using current values. */
7019d805 15531 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15532 (*p_record_line) (current_subfile, line, addr);
366da635 15533 }
25e43795 15534 }
ca5f395d 15535 basic_block = 0;
9aa1fe7e
GK
15536 }
15537 else switch (op_code)
c906108c
SS
15538 {
15539 case DW_LNS_extended_op:
3e43a32a
MS
15540 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15541 &bytes_read);
473b7be6 15542 line_ptr += bytes_read;
a8c50c1f 15543 extended_end = line_ptr + extended_len;
c906108c
SS
15544 extended_op = read_1_byte (abfd, line_ptr);
15545 line_ptr += 1;
15546 switch (extended_op)
15547 {
15548 case DW_LNE_end_sequence:
c91513d8 15549 p_record_line = record_line;
c906108c 15550 end_sequence = 1;
c906108c
SS
15551 break;
15552 case DW_LNE_set_address:
e7c27a73 15553 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15554
15555 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15556 {
15557 /* This line table is for a function which has been
15558 GCd by the linker. Ignore it. PR gdb/12528 */
15559
15560 long line_offset
36586728 15561 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15562
15563 complaint (&symfile_complaints,
15564 _(".debug_line address at offset 0x%lx is 0 "
15565 "[in module %s]"),
bb5ed363 15566 line_offset, objfile->name);
c91513d8
PP
15567 p_record_line = noop_record_line;
15568 }
15569
2dc7f7b3 15570 op_index = 0;
107d2387
AC
15571 line_ptr += bytes_read;
15572 address += baseaddr;
c906108c
SS
15573 break;
15574 case DW_LNE_define_file:
debd256d 15575 {
d521ce57 15576 const char *cur_file;
debd256d 15577 unsigned int dir_index, mod_time, length;
6e70227d 15578
3e43a32a
MS
15579 cur_file = read_direct_string (abfd, line_ptr,
15580 &bytes_read);
debd256d
JB
15581 line_ptr += bytes_read;
15582 dir_index =
15583 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15584 line_ptr += bytes_read;
15585 mod_time =
15586 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15587 line_ptr += bytes_read;
15588 length =
15589 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15590 line_ptr += bytes_read;
15591 add_file_name (lh, cur_file, dir_index, mod_time, length);
15592 }
c906108c 15593 break;
d0c6ba3d
CC
15594 case DW_LNE_set_discriminator:
15595 /* The discriminator is not interesting to the debugger;
15596 just ignore it. */
15597 line_ptr = extended_end;
15598 break;
c906108c 15599 default:
4d3c2250 15600 complaint (&symfile_complaints,
e2e0b3e5 15601 _("mangled .debug_line section"));
debd256d 15602 return;
c906108c 15603 }
a8c50c1f
DJ
15604 /* Make sure that we parsed the extended op correctly. If e.g.
15605 we expected a different address size than the producer used,
15606 we may have read the wrong number of bytes. */
15607 if (line_ptr != extended_end)
15608 {
15609 complaint (&symfile_complaints,
15610 _("mangled .debug_line section"));
15611 return;
15612 }
c906108c
SS
15613 break;
15614 case DW_LNS_copy:
59205f5a 15615 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15616 dwarf2_debug_line_missing_file_complaint ();
15617 else
366da635 15618 {
25e43795 15619 lh->file_names[file - 1].included_p = 1;
ca5f395d 15620 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15621 {
15622 if (last_subfile != current_subfile)
15623 {
15624 addr = gdbarch_addr_bits_remove (gdbarch, address);
15625 if (last_subfile)
c91513d8 15626 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15627 last_subfile = current_subfile;
15628 }
7019d805 15629 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15630 (*p_record_line) (current_subfile, line, addr);
fbf65064 15631 }
366da635 15632 }
c906108c
SS
15633 basic_block = 0;
15634 break;
15635 case DW_LNS_advance_pc:
2dc7f7b3
TT
15636 {
15637 CORE_ADDR adjust
15638 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15639
15640 address += (((op_index + adjust)
15641 / lh->maximum_ops_per_instruction)
15642 * lh->minimum_instruction_length);
15643 op_index = ((op_index + adjust)
15644 % lh->maximum_ops_per_instruction);
15645 line_ptr += bytes_read;
15646 }
c906108c
SS
15647 break;
15648 case DW_LNS_advance_line:
15649 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15650 line_ptr += bytes_read;
15651 break;
15652 case DW_LNS_set_file:
debd256d 15653 {
a738430d
MK
15654 /* The arrays lh->include_dirs and lh->file_names are
15655 0-based, but the directory and file name numbers in
15656 the statement program are 1-based. */
debd256d 15657 struct file_entry *fe;
d521ce57 15658 const char *dir = NULL;
a738430d 15659
debd256d
JB
15660 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15661 line_ptr += bytes_read;
59205f5a 15662 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15663 dwarf2_debug_line_missing_file_complaint ();
15664 else
15665 {
15666 fe = &lh->file_names[file - 1];
15667 if (fe->dir_index)
15668 dir = lh->include_dirs[fe->dir_index - 1];
15669 if (!decode_for_pst_p)
15670 {
15671 last_subfile = current_subfile;
15672 dwarf2_start_subfile (fe->name, dir, comp_dir);
15673 }
15674 }
debd256d 15675 }
c906108c
SS
15676 break;
15677 case DW_LNS_set_column:
15678 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15679 line_ptr += bytes_read;
15680 break;
15681 case DW_LNS_negate_stmt:
15682 is_stmt = (!is_stmt);
15683 break;
15684 case DW_LNS_set_basic_block:
15685 basic_block = 1;
15686 break;
c2c6d25f
JM
15687 /* Add to the address register of the state machine the
15688 address increment value corresponding to special opcode
a738430d
MK
15689 255. I.e., this value is scaled by the minimum
15690 instruction length since special opcode 255 would have
b021a221 15691 scaled the increment. */
c906108c 15692 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15693 {
15694 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15695
15696 address += (((op_index + adjust)
15697 / lh->maximum_ops_per_instruction)
15698 * lh->minimum_instruction_length);
15699 op_index = ((op_index + adjust)
15700 % lh->maximum_ops_per_instruction);
15701 }
c906108c
SS
15702 break;
15703 case DW_LNS_fixed_advance_pc:
15704 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15705 op_index = 0;
c906108c
SS
15706 line_ptr += 2;
15707 break;
9aa1fe7e 15708 default:
a738430d
MK
15709 {
15710 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15711 int i;
a738430d 15712
debd256d 15713 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15714 {
15715 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15716 line_ptr += bytes_read;
15717 }
15718 }
c906108c
SS
15719 }
15720 }
59205f5a
JB
15721 if (lh->num_file_names < file || file == 0)
15722 dwarf2_debug_line_missing_file_complaint ();
15723 else
15724 {
15725 lh->file_names[file - 1].included_p = 1;
15726 if (!decode_for_pst_p)
fbf65064
UW
15727 {
15728 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15729 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15730 }
59205f5a 15731 }
c906108c 15732 }
f3f5162e
DE
15733}
15734
15735/* Decode the Line Number Program (LNP) for the given line_header
15736 structure and CU. The actual information extracted and the type
15737 of structures created from the LNP depends on the value of PST.
15738
15739 1. If PST is NULL, then this procedure uses the data from the program
15740 to create all necessary symbol tables, and their linetables.
15741
15742 2. If PST is not NULL, this procedure reads the program to determine
15743 the list of files included by the unit represented by PST, and
15744 builds all the associated partial symbol tables.
15745
15746 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15747 It is used for relative paths in the line table.
15748 NOTE: When processing partial symtabs (pst != NULL),
15749 comp_dir == pst->dirname.
15750
15751 NOTE: It is important that psymtabs have the same file name (via strcmp)
15752 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15753 symtab we don't use it in the name of the psymtabs we create.
15754 E.g. expand_line_sal requires this when finding psymtabs to expand.
15755 A good testcase for this is mb-inline.exp. */
15756
15757static void
15758dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15759 struct dwarf2_cu *cu, struct partial_symtab *pst,
15760 int want_line_info)
15761{
15762 struct objfile *objfile = cu->objfile;
15763 const int decode_for_pst_p = (pst != NULL);
15764 struct subfile *first_subfile = current_subfile;
15765
15766 if (want_line_info)
15767 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15768
15769 if (decode_for_pst_p)
15770 {
15771 int file_index;
15772
15773 /* Now that we're done scanning the Line Header Program, we can
15774 create the psymtab of each included file. */
15775 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15776 if (lh->file_names[file_index].included_p == 1)
15777 {
d521ce57 15778 const char *include_name =
c6da4cef
DE
15779 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15780 if (include_name != NULL)
aaa75496
JB
15781 dwarf2_create_include_psymtab (include_name, pst, objfile);
15782 }
15783 }
cb1df416
DJ
15784 else
15785 {
15786 /* Make sure a symtab is created for every file, even files
15787 which contain only variables (i.e. no code with associated
15788 line numbers). */
cb1df416 15789 int i;
cb1df416
DJ
15790
15791 for (i = 0; i < lh->num_file_names; i++)
15792 {
d521ce57 15793 const char *dir = NULL;
f3f5162e 15794 struct file_entry *fe;
9a619af0 15795
cb1df416
DJ
15796 fe = &lh->file_names[i];
15797 if (fe->dir_index)
15798 dir = lh->include_dirs[fe->dir_index - 1];
15799 dwarf2_start_subfile (fe->name, dir, comp_dir);
15800
15801 /* Skip the main file; we don't need it, and it must be
15802 allocated last, so that it will show up before the
15803 non-primary symtabs in the objfile's symtab list. */
15804 if (current_subfile == first_subfile)
15805 continue;
15806
15807 if (current_subfile->symtab == NULL)
15808 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15809 objfile);
cb1df416
DJ
15810 fe->symtab = current_subfile->symtab;
15811 }
15812 }
c906108c
SS
15813}
15814
15815/* Start a subfile for DWARF. FILENAME is the name of the file and
15816 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15817 or NULL if not known. COMP_DIR is the compilation directory for the
15818 linetable's compilation unit or NULL if not known.
c906108c
SS
15819 This routine tries to keep line numbers from identical absolute and
15820 relative file names in a common subfile.
15821
15822 Using the `list' example from the GDB testsuite, which resides in
15823 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15824 of /srcdir/list0.c yields the following debugging information for list0.c:
15825
c5aa993b
JM
15826 DW_AT_name: /srcdir/list0.c
15827 DW_AT_comp_dir: /compdir
357e46e7 15828 files.files[0].name: list0.h
c5aa993b 15829 files.files[0].dir: /srcdir
357e46e7 15830 files.files[1].name: list0.c
c5aa993b 15831 files.files[1].dir: /srcdir
c906108c
SS
15832
15833 The line number information for list0.c has to end up in a single
4f1520fb
FR
15834 subfile, so that `break /srcdir/list0.c:1' works as expected.
15835 start_subfile will ensure that this happens provided that we pass the
15836 concatenation of files.files[1].dir and files.files[1].name as the
15837 subfile's name. */
c906108c
SS
15838
15839static void
d521ce57 15840dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 15841 const char *comp_dir)
c906108c 15842{
d521ce57 15843 char *copy = NULL;
4f1520fb
FR
15844
15845 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15846 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15847 second argument to start_subfile. To be consistent, we do the
15848 same here. In order not to lose the line information directory,
15849 we concatenate it to the filename when it makes sense.
15850 Note that the Dwarf3 standard says (speaking of filenames in line
15851 information): ``The directory index is ignored for file names
15852 that represent full path names''. Thus ignoring dirname in the
15853 `else' branch below isn't an issue. */
c906108c 15854
d5166ae1 15855 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
15856 {
15857 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15858 filename = copy;
15859 }
c906108c 15860
d521ce57 15861 start_subfile (filename, comp_dir);
4f1520fb 15862
d521ce57
TT
15863 if (copy != NULL)
15864 xfree (copy);
c906108c
SS
15865}
15866
f4dc4d17
DE
15867/* Start a symtab for DWARF.
15868 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15869
15870static void
15871dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15872 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15873{
15874 start_symtab (name, comp_dir, low_pc);
15875 record_debugformat ("DWARF 2");
15876 record_producer (cu->producer);
15877
15878 /* We assume that we're processing GCC output. */
15879 processing_gcc_compilation = 2;
15880
4d4ec4e5 15881 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15882}
15883
4c2df51b
DJ
15884static void
15885var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15886 struct dwarf2_cu *cu)
4c2df51b 15887{
e7c27a73
DJ
15888 struct objfile *objfile = cu->objfile;
15889 struct comp_unit_head *cu_header = &cu->header;
15890
4c2df51b
DJ
15891 /* NOTE drow/2003-01-30: There used to be a comment and some special
15892 code here to turn a symbol with DW_AT_external and a
15893 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15894 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15895 with some versions of binutils) where shared libraries could have
15896 relocations against symbols in their debug information - the
15897 minimal symbol would have the right address, but the debug info
15898 would not. It's no longer necessary, because we will explicitly
15899 apply relocations when we read in the debug information now. */
15900
15901 /* A DW_AT_location attribute with no contents indicates that a
15902 variable has been optimized away. */
15903 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15904 {
f1e6e072 15905 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
15906 return;
15907 }
15908
15909 /* Handle one degenerate form of location expression specially, to
15910 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15911 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15912 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15913
15914 if (attr_form_is_block (attr)
3019eac3
DE
15915 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15916 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15917 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15918 && (DW_BLOCK (attr)->size
15919 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15920 {
891d2f0b 15921 unsigned int dummy;
4c2df51b 15922
3019eac3
DE
15923 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15924 SYMBOL_VALUE_ADDRESS (sym) =
15925 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15926 else
15927 SYMBOL_VALUE_ADDRESS (sym) =
15928 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 15929 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
15930 fixup_symbol_section (sym, objfile);
15931 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15932 SYMBOL_SECTION (sym));
4c2df51b
DJ
15933 return;
15934 }
15935
15936 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15937 expression evaluator, and use LOC_COMPUTED only when necessary
15938 (i.e. when the value of a register or memory location is
15939 referenced, or a thread-local block, etc.). Then again, it might
15940 not be worthwhile. I'm assuming that it isn't unless performance
15941 or memory numbers show me otherwise. */
15942
f1e6e072 15943 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 15944
f1e6e072 15945 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 15946 cu->has_loclist = 1;
4c2df51b
DJ
15947}
15948
c906108c
SS
15949/* Given a pointer to a DWARF information entry, figure out if we need
15950 to make a symbol table entry for it, and if so, create a new entry
15951 and return a pointer to it.
15952 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15953 used the passed type.
15954 If SPACE is not NULL, use it to hold the new symbol. If it is
15955 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15956
15957static struct symbol *
34eaf542
TT
15958new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15959 struct symbol *space)
c906108c 15960{
e7c27a73 15961 struct objfile *objfile = cu->objfile;
c906108c 15962 struct symbol *sym = NULL;
15d034d0 15963 const char *name;
c906108c
SS
15964 struct attribute *attr = NULL;
15965 struct attribute *attr2 = NULL;
e142c38c 15966 CORE_ADDR baseaddr;
e37fd15a
SW
15967 struct pending **list_to_add = NULL;
15968
edb3359d 15969 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15970
15971 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15972
94af9270 15973 name = dwarf2_name (die, cu);
c906108c
SS
15974 if (name)
15975 {
94af9270 15976 const char *linkagename;
34eaf542 15977 int suppress_add = 0;
94af9270 15978
34eaf542
TT
15979 if (space)
15980 sym = space;
15981 else
e623cf5d 15982 sym = allocate_symbol (objfile);
c906108c 15983 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15984
15985 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 15986 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
15987 linkagename = dwarf2_physname (name, die, cu);
15988 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15989
f55ee35c
JK
15990 /* Fortran does not have mangling standard and the mangling does differ
15991 between gfortran, iFort etc. */
15992 if (cu->language == language_fortran
b250c185 15993 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15994 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15995 dwarf2_full_name (name, die, cu),
29df156d 15996 NULL);
f55ee35c 15997
c906108c 15998 /* Default assumptions.
c5aa993b 15999 Use the passed type or decode it from the die. */
176620f1 16000 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16001 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16002 if (type != NULL)
16003 SYMBOL_TYPE (sym) = type;
16004 else
e7c27a73 16005 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16006 attr = dwarf2_attr (die,
16007 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16008 cu);
c906108c
SS
16009 if (attr)
16010 {
16011 SYMBOL_LINE (sym) = DW_UNSND (attr);
16012 }
cb1df416 16013
edb3359d
DJ
16014 attr = dwarf2_attr (die,
16015 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16016 cu);
cb1df416
DJ
16017 if (attr)
16018 {
16019 int file_index = DW_UNSND (attr);
9a619af0 16020
cb1df416
DJ
16021 if (cu->line_header == NULL
16022 || file_index > cu->line_header->num_file_names)
16023 complaint (&symfile_complaints,
16024 _("file index out of range"));
1c3d648d 16025 else if (file_index > 0)
cb1df416
DJ
16026 {
16027 struct file_entry *fe;
9a619af0 16028
cb1df416
DJ
16029 fe = &cu->line_header->file_names[file_index - 1];
16030 SYMBOL_SYMTAB (sym) = fe->symtab;
16031 }
16032 }
16033
c906108c
SS
16034 switch (die->tag)
16035 {
16036 case DW_TAG_label:
e142c38c 16037 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16038 if (attr)
16039 {
16040 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16041 }
0f5238ed
TT
16042 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16043 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16044 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16045 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16046 break;
16047 case DW_TAG_subprogram:
16048 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16049 finish_block. */
f1e6e072 16050 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16051 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16052 if ((attr2 && (DW_UNSND (attr2) != 0))
16053 || cu->language == language_ada)
c906108c 16054 {
2cfa0c8d
JB
16055 /* Subprograms marked external are stored as a global symbol.
16056 Ada subprograms, whether marked external or not, are always
16057 stored as a global symbol, because we want to be able to
16058 access them globally. For instance, we want to be able
16059 to break on a nested subprogram without having to
16060 specify the context. */
e37fd15a 16061 list_to_add = &global_symbols;
c906108c
SS
16062 }
16063 else
16064 {
e37fd15a 16065 list_to_add = cu->list_in_scope;
c906108c
SS
16066 }
16067 break;
edb3359d
DJ
16068 case DW_TAG_inlined_subroutine:
16069 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16070 finish_block. */
f1e6e072 16071 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16072 SYMBOL_INLINED (sym) = 1;
481860b3 16073 list_to_add = cu->list_in_scope;
edb3359d 16074 break;
34eaf542
TT
16075 case DW_TAG_template_value_param:
16076 suppress_add = 1;
16077 /* Fall through. */
72929c62 16078 case DW_TAG_constant:
c906108c 16079 case DW_TAG_variable:
254e6b9e 16080 case DW_TAG_member:
0963b4bd
MS
16081 /* Compilation with minimal debug info may result in
16082 variables with missing type entries. Change the
16083 misleading `void' type to something sensible. */
c906108c 16084 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16085 SYMBOL_TYPE (sym)
46bf5051 16086 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16087
e142c38c 16088 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16089 /* In the case of DW_TAG_member, we should only be called for
16090 static const members. */
16091 if (die->tag == DW_TAG_member)
16092 {
3863f96c
DE
16093 /* dwarf2_add_field uses die_is_declaration,
16094 so we do the same. */
254e6b9e
DE
16095 gdb_assert (die_is_declaration (die, cu));
16096 gdb_assert (attr);
16097 }
c906108c
SS
16098 if (attr)
16099 {
e7c27a73 16100 dwarf2_const_value (attr, sym, cu);
e142c38c 16101 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16102 if (!suppress_add)
34eaf542
TT
16103 {
16104 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16105 list_to_add = &global_symbols;
34eaf542 16106 else
e37fd15a 16107 list_to_add = cu->list_in_scope;
34eaf542 16108 }
c906108c
SS
16109 break;
16110 }
e142c38c 16111 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16112 if (attr)
16113 {
e7c27a73 16114 var_decode_location (attr, sym, cu);
e142c38c 16115 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16116
16117 /* Fortran explicitly imports any global symbols to the local
16118 scope by DW_TAG_common_block. */
16119 if (cu->language == language_fortran && die->parent
16120 && die->parent->tag == DW_TAG_common_block)
16121 attr2 = NULL;
16122
caac4577
JG
16123 if (SYMBOL_CLASS (sym) == LOC_STATIC
16124 && SYMBOL_VALUE_ADDRESS (sym) == 0
16125 && !dwarf2_per_objfile->has_section_at_zero)
16126 {
16127 /* When a static variable is eliminated by the linker,
16128 the corresponding debug information is not stripped
16129 out, but the variable address is set to null;
16130 do not add such variables into symbol table. */
16131 }
16132 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16133 {
f55ee35c
JK
16134 /* Workaround gfortran PR debug/40040 - it uses
16135 DW_AT_location for variables in -fPIC libraries which may
16136 get overriden by other libraries/executable and get
16137 a different address. Resolve it by the minimal symbol
16138 which may come from inferior's executable using copy
16139 relocation. Make this workaround only for gfortran as for
16140 other compilers GDB cannot guess the minimal symbol
16141 Fortran mangling kind. */
16142 if (cu->language == language_fortran && die->parent
16143 && die->parent->tag == DW_TAG_module
16144 && cu->producer
16145 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16146 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16147
1c809c68
TT
16148 /* A variable with DW_AT_external is never static,
16149 but it may be block-scoped. */
16150 list_to_add = (cu->list_in_scope == &file_symbols
16151 ? &global_symbols : cu->list_in_scope);
1c809c68 16152 }
c906108c 16153 else
e37fd15a 16154 list_to_add = cu->list_in_scope;
c906108c
SS
16155 }
16156 else
16157 {
16158 /* We do not know the address of this symbol.
c5aa993b
JM
16159 If it is an external symbol and we have type information
16160 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16161 The address of the variable will then be determined from
16162 the minimal symbol table whenever the variable is
16163 referenced. */
e142c38c 16164 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16165
16166 /* Fortran explicitly imports any global symbols to the local
16167 scope by DW_TAG_common_block. */
16168 if (cu->language == language_fortran && die->parent
16169 && die->parent->tag == DW_TAG_common_block)
16170 {
16171 /* SYMBOL_CLASS doesn't matter here because
16172 read_common_block is going to reset it. */
16173 if (!suppress_add)
16174 list_to_add = cu->list_in_scope;
16175 }
16176 else if (attr2 && (DW_UNSND (attr2) != 0)
16177 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16178 {
0fe7935b
DJ
16179 /* A variable with DW_AT_external is never static, but it
16180 may be block-scoped. */
16181 list_to_add = (cu->list_in_scope == &file_symbols
16182 ? &global_symbols : cu->list_in_scope);
16183
f1e6e072 16184 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16185 }
442ddf59
JK
16186 else if (!die_is_declaration (die, cu))
16187 {
16188 /* Use the default LOC_OPTIMIZED_OUT class. */
16189 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16190 if (!suppress_add)
16191 list_to_add = cu->list_in_scope;
442ddf59 16192 }
c906108c
SS
16193 }
16194 break;
16195 case DW_TAG_formal_parameter:
edb3359d
DJ
16196 /* If we are inside a function, mark this as an argument. If
16197 not, we might be looking at an argument to an inlined function
16198 when we do not have enough information to show inlined frames;
16199 pretend it's a local variable in that case so that the user can
16200 still see it. */
16201 if (context_stack_depth > 0
16202 && context_stack[context_stack_depth - 1].name != NULL)
16203 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16204 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16205 if (attr)
16206 {
e7c27a73 16207 var_decode_location (attr, sym, cu);
c906108c 16208 }
e142c38c 16209 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16210 if (attr)
16211 {
e7c27a73 16212 dwarf2_const_value (attr, sym, cu);
c906108c 16213 }
f346a30d 16214
e37fd15a 16215 list_to_add = cu->list_in_scope;
c906108c
SS
16216 break;
16217 case DW_TAG_unspecified_parameters:
16218 /* From varargs functions; gdb doesn't seem to have any
16219 interest in this information, so just ignore it for now.
16220 (FIXME?) */
16221 break;
34eaf542
TT
16222 case DW_TAG_template_type_param:
16223 suppress_add = 1;
16224 /* Fall through. */
c906108c 16225 case DW_TAG_class_type:
680b30c7 16226 case DW_TAG_interface_type:
c906108c
SS
16227 case DW_TAG_structure_type:
16228 case DW_TAG_union_type:
72019c9c 16229 case DW_TAG_set_type:
c906108c 16230 case DW_TAG_enumeration_type:
f1e6e072 16231 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16232 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16233
63d06c5c 16234 {
987504bb 16235 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16236 really ever be static objects: otherwise, if you try
16237 to, say, break of a class's method and you're in a file
16238 which doesn't mention that class, it won't work unless
16239 the check for all static symbols in lookup_symbol_aux
16240 saves you. See the OtherFileClass tests in
16241 gdb.c++/namespace.exp. */
16242
e37fd15a 16243 if (!suppress_add)
34eaf542 16244 {
34eaf542
TT
16245 list_to_add = (cu->list_in_scope == &file_symbols
16246 && (cu->language == language_cplus
16247 || cu->language == language_java)
16248 ? &global_symbols : cu->list_in_scope);
63d06c5c 16249
64382290
TT
16250 /* The semantics of C++ state that "struct foo {
16251 ... }" also defines a typedef for "foo". A Java
16252 class declaration also defines a typedef for the
16253 class. */
16254 if (cu->language == language_cplus
16255 || cu->language == language_java
16256 || cu->language == language_ada)
16257 {
16258 /* The symbol's name is already allocated along
16259 with this objfile, so we don't need to
16260 duplicate it for the type. */
16261 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16262 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16263 }
63d06c5c
DC
16264 }
16265 }
c906108c
SS
16266 break;
16267 case DW_TAG_typedef:
f1e6e072 16268 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16269 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16270 list_to_add = cu->list_in_scope;
63d06c5c 16271 break;
c906108c 16272 case DW_TAG_base_type:
a02abb62 16273 case DW_TAG_subrange_type:
f1e6e072 16274 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16275 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16276 list_to_add = cu->list_in_scope;
c906108c
SS
16277 break;
16278 case DW_TAG_enumerator:
e142c38c 16279 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16280 if (attr)
16281 {
e7c27a73 16282 dwarf2_const_value (attr, sym, cu);
c906108c 16283 }
63d06c5c
DC
16284 {
16285 /* NOTE: carlton/2003-11-10: See comment above in the
16286 DW_TAG_class_type, etc. block. */
16287
e142c38c 16288 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16289 && (cu->language == language_cplus
16290 || cu->language == language_java)
e142c38c 16291 ? &global_symbols : cu->list_in_scope);
63d06c5c 16292 }
c906108c 16293 break;
5c4e30ca 16294 case DW_TAG_namespace:
f1e6e072 16295 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16296 list_to_add = &global_symbols;
5c4e30ca 16297 break;
4357ac6c 16298 case DW_TAG_common_block:
f1e6e072 16299 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16300 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16301 add_symbol_to_list (sym, cu->list_in_scope);
16302 break;
c906108c
SS
16303 default:
16304 /* Not a tag we recognize. Hopefully we aren't processing
16305 trash data, but since we must specifically ignore things
16306 we don't recognize, there is nothing else we should do at
0963b4bd 16307 this point. */
e2e0b3e5 16308 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16309 dwarf_tag_name (die->tag));
c906108c
SS
16310 break;
16311 }
df8a16a1 16312
e37fd15a
SW
16313 if (suppress_add)
16314 {
16315 sym->hash_next = objfile->template_symbols;
16316 objfile->template_symbols = sym;
16317 list_to_add = NULL;
16318 }
16319
16320 if (list_to_add != NULL)
16321 add_symbol_to_list (sym, list_to_add);
16322
df8a16a1
DJ
16323 /* For the benefit of old versions of GCC, check for anonymous
16324 namespaces based on the demangled name. */
4d4ec4e5 16325 if (!cu->processing_has_namespace_info
94af9270 16326 && cu->language == language_cplus)
a10964d1 16327 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16328 }
16329 return (sym);
16330}
16331
34eaf542
TT
16332/* A wrapper for new_symbol_full that always allocates a new symbol. */
16333
16334static struct symbol *
16335new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16336{
16337 return new_symbol_full (die, type, cu, NULL);
16338}
16339
98bfdba5
PA
16340/* Given an attr with a DW_FORM_dataN value in host byte order,
16341 zero-extend it as appropriate for the symbol's type. The DWARF
16342 standard (v4) is not entirely clear about the meaning of using
16343 DW_FORM_dataN for a constant with a signed type, where the type is
16344 wider than the data. The conclusion of a discussion on the DWARF
16345 list was that this is unspecified. We choose to always zero-extend
16346 because that is the interpretation long in use by GCC. */
c906108c 16347
98bfdba5
PA
16348static gdb_byte *
16349dwarf2_const_value_data (struct attribute *attr, struct type *type,
16350 const char *name, struct obstack *obstack,
12df843f 16351 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16352{
e7c27a73 16353 struct objfile *objfile = cu->objfile;
e17a4113
UW
16354 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16355 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16356 LONGEST l = DW_UNSND (attr);
16357
16358 if (bits < sizeof (*value) * 8)
16359 {
16360 l &= ((LONGEST) 1 << bits) - 1;
16361 *value = l;
16362 }
16363 else if (bits == sizeof (*value) * 8)
16364 *value = l;
16365 else
16366 {
16367 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16368 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16369 return bytes;
16370 }
16371
16372 return NULL;
16373}
16374
16375/* Read a constant value from an attribute. Either set *VALUE, or if
16376 the value does not fit in *VALUE, set *BYTES - either already
16377 allocated on the objfile obstack, or newly allocated on OBSTACK,
16378 or, set *BATON, if we translated the constant to a location
16379 expression. */
16380
16381static void
16382dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16383 const char *name, struct obstack *obstack,
16384 struct dwarf2_cu *cu,
d521ce57 16385 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16386 struct dwarf2_locexpr_baton **baton)
16387{
16388 struct objfile *objfile = cu->objfile;
16389 struct comp_unit_head *cu_header = &cu->header;
c906108c 16390 struct dwarf_block *blk;
98bfdba5
PA
16391 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16392 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16393
16394 *value = 0;
16395 *bytes = NULL;
16396 *baton = NULL;
c906108c
SS
16397
16398 switch (attr->form)
16399 {
16400 case DW_FORM_addr:
3019eac3 16401 case DW_FORM_GNU_addr_index:
ac56253d 16402 {
ac56253d
TT
16403 gdb_byte *data;
16404
98bfdba5
PA
16405 if (TYPE_LENGTH (type) != cu_header->addr_size)
16406 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16407 cu_header->addr_size,
98bfdba5 16408 TYPE_LENGTH (type));
ac56253d
TT
16409 /* Symbols of this form are reasonably rare, so we just
16410 piggyback on the existing location code rather than writing
16411 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16412 *baton = obstack_alloc (&objfile->objfile_obstack,
16413 sizeof (struct dwarf2_locexpr_baton));
16414 (*baton)->per_cu = cu->per_cu;
16415 gdb_assert ((*baton)->per_cu);
ac56253d 16416
98bfdba5
PA
16417 (*baton)->size = 2 + cu_header->addr_size;
16418 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16419 (*baton)->data = data;
ac56253d
TT
16420
16421 data[0] = DW_OP_addr;
16422 store_unsigned_integer (&data[1], cu_header->addr_size,
16423 byte_order, DW_ADDR (attr));
16424 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16425 }
c906108c 16426 break;
4ac36638 16427 case DW_FORM_string:
93b5768b 16428 case DW_FORM_strp:
3019eac3 16429 case DW_FORM_GNU_str_index:
36586728 16430 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16431 /* DW_STRING is already allocated on the objfile obstack, point
16432 directly to it. */
d521ce57 16433 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16434 break;
c906108c
SS
16435 case DW_FORM_block1:
16436 case DW_FORM_block2:
16437 case DW_FORM_block4:
16438 case DW_FORM_block:
2dc7f7b3 16439 case DW_FORM_exprloc:
c906108c 16440 blk = DW_BLOCK (attr);
98bfdba5
PA
16441 if (TYPE_LENGTH (type) != blk->size)
16442 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16443 TYPE_LENGTH (type));
16444 *bytes = blk->data;
c906108c 16445 break;
2df3850c
JM
16446
16447 /* The DW_AT_const_value attributes are supposed to carry the
16448 symbol's value "represented as it would be on the target
16449 architecture." By the time we get here, it's already been
16450 converted to host endianness, so we just need to sign- or
16451 zero-extend it as appropriate. */
16452 case DW_FORM_data1:
3e43a32a
MS
16453 *bytes = dwarf2_const_value_data (attr, type, name,
16454 obstack, cu, value, 8);
2df3850c 16455 break;
c906108c 16456 case DW_FORM_data2:
3e43a32a
MS
16457 *bytes = dwarf2_const_value_data (attr, type, name,
16458 obstack, cu, value, 16);
2df3850c 16459 break;
c906108c 16460 case DW_FORM_data4:
3e43a32a
MS
16461 *bytes = dwarf2_const_value_data (attr, type, name,
16462 obstack, cu, value, 32);
2df3850c 16463 break;
c906108c 16464 case DW_FORM_data8:
3e43a32a
MS
16465 *bytes = dwarf2_const_value_data (attr, type, name,
16466 obstack, cu, value, 64);
2df3850c
JM
16467 break;
16468
c906108c 16469 case DW_FORM_sdata:
98bfdba5 16470 *value = DW_SND (attr);
2df3850c
JM
16471 break;
16472
c906108c 16473 case DW_FORM_udata:
98bfdba5 16474 *value = DW_UNSND (attr);
c906108c 16475 break;
2df3850c 16476
c906108c 16477 default:
4d3c2250 16478 complaint (&symfile_complaints,
e2e0b3e5 16479 _("unsupported const value attribute form: '%s'"),
4d3c2250 16480 dwarf_form_name (attr->form));
98bfdba5 16481 *value = 0;
c906108c
SS
16482 break;
16483 }
16484}
16485
2df3850c 16486
98bfdba5
PA
16487/* Copy constant value from an attribute to a symbol. */
16488
2df3850c 16489static void
98bfdba5
PA
16490dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16491 struct dwarf2_cu *cu)
2df3850c 16492{
98bfdba5
PA
16493 struct objfile *objfile = cu->objfile;
16494 struct comp_unit_head *cu_header = &cu->header;
12df843f 16495 LONGEST value;
d521ce57 16496 const gdb_byte *bytes;
98bfdba5 16497 struct dwarf2_locexpr_baton *baton;
2df3850c 16498
98bfdba5
PA
16499 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16500 SYMBOL_PRINT_NAME (sym),
16501 &objfile->objfile_obstack, cu,
16502 &value, &bytes, &baton);
2df3850c 16503
98bfdba5
PA
16504 if (baton != NULL)
16505 {
98bfdba5 16506 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16507 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16508 }
16509 else if (bytes != NULL)
16510 {
16511 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16512 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16513 }
16514 else
16515 {
16516 SYMBOL_VALUE (sym) = value;
f1e6e072 16517 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16518 }
2df3850c
JM
16519}
16520
c906108c
SS
16521/* Return the type of the die in question using its DW_AT_type attribute. */
16522
16523static struct type *
e7c27a73 16524die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16525{
c906108c 16526 struct attribute *type_attr;
c906108c 16527
e142c38c 16528 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16529 if (!type_attr)
16530 {
16531 /* A missing DW_AT_type represents a void type. */
46bf5051 16532 return objfile_type (cu->objfile)->builtin_void;
c906108c 16533 }
348e048f 16534
673bfd45 16535 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16536}
16537
b4ba55a1
JB
16538/* True iff CU's producer generates GNAT Ada auxiliary information
16539 that allows to find parallel types through that information instead
16540 of having to do expensive parallel lookups by type name. */
16541
16542static int
16543need_gnat_info (struct dwarf2_cu *cu)
16544{
16545 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16546 of GNAT produces this auxiliary information, without any indication
16547 that it is produced. Part of enhancing the FSF version of GNAT
16548 to produce that information will be to put in place an indicator
16549 that we can use in order to determine whether the descriptive type
16550 info is available or not. One suggestion that has been made is
16551 to use a new attribute, attached to the CU die. For now, assume
16552 that the descriptive type info is not available. */
16553 return 0;
16554}
16555
b4ba55a1
JB
16556/* Return the auxiliary type of the die in question using its
16557 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16558 attribute is not present. */
16559
16560static struct type *
16561die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16562{
b4ba55a1 16563 struct attribute *type_attr;
b4ba55a1
JB
16564
16565 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16566 if (!type_attr)
16567 return NULL;
16568
673bfd45 16569 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16570}
16571
16572/* If DIE has a descriptive_type attribute, then set the TYPE's
16573 descriptive type accordingly. */
16574
16575static void
16576set_descriptive_type (struct type *type, struct die_info *die,
16577 struct dwarf2_cu *cu)
16578{
16579 struct type *descriptive_type = die_descriptive_type (die, cu);
16580
16581 if (descriptive_type)
16582 {
16583 ALLOCATE_GNAT_AUX_TYPE (type);
16584 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16585 }
16586}
16587
c906108c
SS
16588/* Return the containing type of the die in question using its
16589 DW_AT_containing_type attribute. */
16590
16591static struct type *
e7c27a73 16592die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16593{
c906108c 16594 struct attribute *type_attr;
c906108c 16595
e142c38c 16596 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16597 if (!type_attr)
16598 error (_("Dwarf Error: Problem turning containing type into gdb type "
16599 "[in module %s]"), cu->objfile->name);
16600
673bfd45 16601 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16602}
16603
673bfd45
DE
16604/* Look up the type of DIE in CU using its type attribute ATTR.
16605 If there is no type substitute an error marker. */
16606
c906108c 16607static struct type *
673bfd45
DE
16608lookup_die_type (struct die_info *die, struct attribute *attr,
16609 struct dwarf2_cu *cu)
c906108c 16610{
bb5ed363 16611 struct objfile *objfile = cu->objfile;
f792889a
DJ
16612 struct type *this_type;
16613
673bfd45
DE
16614 /* First see if we have it cached. */
16615
36586728
TT
16616 if (attr->form == DW_FORM_GNU_ref_alt)
16617 {
16618 struct dwarf2_per_cu_data *per_cu;
16619 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16620
16621 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16622 this_type = get_die_type_at_offset (offset, per_cu);
16623 }
16624 else if (is_ref_attr (attr))
673bfd45 16625 {
b64f50a1 16626 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16627
16628 this_type = get_die_type_at_offset (offset, cu->per_cu);
16629 }
55f1336d 16630 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16631 {
16632 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16633
16634 /* sig_type will be NULL if the signatured type is missing from
16635 the debug info. */
16636 if (sig_type == NULL)
16637 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16638 "at 0x%x [in module %s]"),
b64f50a1 16639 die->offset.sect_off, objfile->name);
673bfd45 16640
3019eac3
DE
16641 gdb_assert (sig_type->per_cu.is_debug_types);
16642 /* If we haven't filled in type_offset_in_section yet, then we
16643 haven't read the type in yet. */
16644 this_type = NULL;
16645 if (sig_type->type_offset_in_section.sect_off != 0)
16646 {
16647 this_type =
16648 get_die_type_at_offset (sig_type->type_offset_in_section,
16649 &sig_type->per_cu);
16650 }
673bfd45
DE
16651 }
16652 else
16653 {
16654 dump_die_for_error (die);
16655 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16656 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16657 }
16658
16659 /* If not cached we need to read it in. */
16660
16661 if (this_type == NULL)
16662 {
16663 struct die_info *type_die;
16664 struct dwarf2_cu *type_cu = cu;
16665
16666 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16667 /* If we found the type now, it's probably because the type came
16668 from an inter-CU reference and the type's CU got expanded before
16669 ours. */
16670 this_type = get_die_type (type_die, type_cu);
16671 if (this_type == NULL)
16672 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16673 }
16674
16675 /* If we still don't have a type use an error marker. */
16676
16677 if (this_type == NULL)
c906108c 16678 {
b00fdb78
TT
16679 char *message, *saved;
16680
16681 /* read_type_die already issued a complaint. */
16682 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16683 objfile->name,
b64f50a1
JK
16684 cu->header.offset.sect_off,
16685 die->offset.sect_off);
bb5ed363 16686 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16687 message, strlen (message));
16688 xfree (message);
16689
bb5ed363 16690 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16691 }
673bfd45 16692
f792889a 16693 return this_type;
c906108c
SS
16694}
16695
673bfd45
DE
16696/* Return the type in DIE, CU.
16697 Returns NULL for invalid types.
16698
02142a6c 16699 This first does a lookup in die_type_hash,
673bfd45
DE
16700 and only reads the die in if necessary.
16701
16702 NOTE: This can be called when reading in partial or full symbols. */
16703
f792889a 16704static struct type *
e7c27a73 16705read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16706{
f792889a
DJ
16707 struct type *this_type;
16708
16709 this_type = get_die_type (die, cu);
16710 if (this_type)
16711 return this_type;
16712
673bfd45
DE
16713 return read_type_die_1 (die, cu);
16714}
16715
16716/* Read the type in DIE, CU.
16717 Returns NULL for invalid types. */
16718
16719static struct type *
16720read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16721{
16722 struct type *this_type = NULL;
16723
c906108c
SS
16724 switch (die->tag)
16725 {
16726 case DW_TAG_class_type:
680b30c7 16727 case DW_TAG_interface_type:
c906108c
SS
16728 case DW_TAG_structure_type:
16729 case DW_TAG_union_type:
f792889a 16730 this_type = read_structure_type (die, cu);
c906108c
SS
16731 break;
16732 case DW_TAG_enumeration_type:
f792889a 16733 this_type = read_enumeration_type (die, cu);
c906108c
SS
16734 break;
16735 case DW_TAG_subprogram:
16736 case DW_TAG_subroutine_type:
edb3359d 16737 case DW_TAG_inlined_subroutine:
f792889a 16738 this_type = read_subroutine_type (die, cu);
c906108c
SS
16739 break;
16740 case DW_TAG_array_type:
f792889a 16741 this_type = read_array_type (die, cu);
c906108c 16742 break;
72019c9c 16743 case DW_TAG_set_type:
f792889a 16744 this_type = read_set_type (die, cu);
72019c9c 16745 break;
c906108c 16746 case DW_TAG_pointer_type:
f792889a 16747 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16748 break;
16749 case DW_TAG_ptr_to_member_type:
f792889a 16750 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16751 break;
16752 case DW_TAG_reference_type:
f792889a 16753 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16754 break;
16755 case DW_TAG_const_type:
f792889a 16756 this_type = read_tag_const_type (die, cu);
c906108c
SS
16757 break;
16758 case DW_TAG_volatile_type:
f792889a 16759 this_type = read_tag_volatile_type (die, cu);
c906108c 16760 break;
06d66ee9
TT
16761 case DW_TAG_restrict_type:
16762 this_type = read_tag_restrict_type (die, cu);
16763 break;
c906108c 16764 case DW_TAG_string_type:
f792889a 16765 this_type = read_tag_string_type (die, cu);
c906108c
SS
16766 break;
16767 case DW_TAG_typedef:
f792889a 16768 this_type = read_typedef (die, cu);
c906108c 16769 break;
a02abb62 16770 case DW_TAG_subrange_type:
f792889a 16771 this_type = read_subrange_type (die, cu);
a02abb62 16772 break;
c906108c 16773 case DW_TAG_base_type:
f792889a 16774 this_type = read_base_type (die, cu);
c906108c 16775 break;
81a17f79 16776 case DW_TAG_unspecified_type:
f792889a 16777 this_type = read_unspecified_type (die, cu);
81a17f79 16778 break;
0114d602
DJ
16779 case DW_TAG_namespace:
16780 this_type = read_namespace_type (die, cu);
16781 break;
f55ee35c
JK
16782 case DW_TAG_module:
16783 this_type = read_module_type (die, cu);
16784 break;
c906108c 16785 default:
3e43a32a
MS
16786 complaint (&symfile_complaints,
16787 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16788 dwarf_tag_name (die->tag));
c906108c
SS
16789 break;
16790 }
63d06c5c 16791
f792889a 16792 return this_type;
63d06c5c
DC
16793}
16794
abc72ce4
DE
16795/* See if we can figure out if the class lives in a namespace. We do
16796 this by looking for a member function; its demangled name will
16797 contain namespace info, if there is any.
16798 Return the computed name or NULL.
16799 Space for the result is allocated on the objfile's obstack.
16800 This is the full-die version of guess_partial_die_structure_name.
16801 In this case we know DIE has no useful parent. */
16802
16803static char *
16804guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16805{
16806 struct die_info *spec_die;
16807 struct dwarf2_cu *spec_cu;
16808 struct die_info *child;
16809
16810 spec_cu = cu;
16811 spec_die = die_specification (die, &spec_cu);
16812 if (spec_die != NULL)
16813 {
16814 die = spec_die;
16815 cu = spec_cu;
16816 }
16817
16818 for (child = die->child;
16819 child != NULL;
16820 child = child->sibling)
16821 {
16822 if (child->tag == DW_TAG_subprogram)
16823 {
16824 struct attribute *attr;
16825
16826 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16827 if (attr == NULL)
16828 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16829 if (attr != NULL)
16830 {
16831 char *actual_name
16832 = language_class_name_from_physname (cu->language_defn,
16833 DW_STRING (attr));
16834 char *name = NULL;
16835
16836 if (actual_name != NULL)
16837 {
15d034d0 16838 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16839
16840 if (die_name != NULL
16841 && strcmp (die_name, actual_name) != 0)
16842 {
16843 /* Strip off the class name from the full name.
16844 We want the prefix. */
16845 int die_name_len = strlen (die_name);
16846 int actual_name_len = strlen (actual_name);
16847
16848 /* Test for '::' as a sanity check. */
16849 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16850 && actual_name[actual_name_len
16851 - die_name_len - 1] == ':')
abc72ce4 16852 name =
10f0c4bb
TT
16853 obstack_copy0 (&cu->objfile->objfile_obstack,
16854 actual_name,
16855 actual_name_len - die_name_len - 2);
abc72ce4
DE
16856 }
16857 }
16858 xfree (actual_name);
16859 return name;
16860 }
16861 }
16862 }
16863
16864 return NULL;
16865}
16866
96408a79
SA
16867/* GCC might emit a nameless typedef that has a linkage name. Determine the
16868 prefix part in such case. See
16869 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16870
16871static char *
16872anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16873{
16874 struct attribute *attr;
16875 char *base;
16876
16877 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16878 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16879 return NULL;
16880
16881 attr = dwarf2_attr (die, DW_AT_name, cu);
16882 if (attr != NULL && DW_STRING (attr) != NULL)
16883 return NULL;
16884
16885 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16886 if (attr == NULL)
16887 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16888 if (attr == NULL || DW_STRING (attr) == NULL)
16889 return NULL;
16890
16891 /* dwarf2_name had to be already called. */
16892 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16893
16894 /* Strip the base name, keep any leading namespaces/classes. */
16895 base = strrchr (DW_STRING (attr), ':');
16896 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16897 return "";
16898
10f0c4bb
TT
16899 return obstack_copy0 (&cu->objfile->objfile_obstack,
16900 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16901}
16902
fdde2d81 16903/* Return the name of the namespace/class that DIE is defined within,
0114d602 16904 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16905
0114d602
DJ
16906 For example, if we're within the method foo() in the following
16907 code:
16908
16909 namespace N {
16910 class C {
16911 void foo () {
16912 }
16913 };
16914 }
16915
16916 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16917
0d5cff50 16918static const char *
e142c38c 16919determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16920{
0114d602
DJ
16921 struct die_info *parent, *spec_die;
16922 struct dwarf2_cu *spec_cu;
16923 struct type *parent_type;
96408a79 16924 char *retval;
63d06c5c 16925
f55ee35c
JK
16926 if (cu->language != language_cplus && cu->language != language_java
16927 && cu->language != language_fortran)
0114d602
DJ
16928 return "";
16929
96408a79
SA
16930 retval = anonymous_struct_prefix (die, cu);
16931 if (retval)
16932 return retval;
16933
0114d602
DJ
16934 /* We have to be careful in the presence of DW_AT_specification.
16935 For example, with GCC 3.4, given the code
16936
16937 namespace N {
16938 void foo() {
16939 // Definition of N::foo.
16940 }
16941 }
16942
16943 then we'll have a tree of DIEs like this:
16944
16945 1: DW_TAG_compile_unit
16946 2: DW_TAG_namespace // N
16947 3: DW_TAG_subprogram // declaration of N::foo
16948 4: DW_TAG_subprogram // definition of N::foo
16949 DW_AT_specification // refers to die #3
16950
16951 Thus, when processing die #4, we have to pretend that we're in
16952 the context of its DW_AT_specification, namely the contex of die
16953 #3. */
16954 spec_cu = cu;
16955 spec_die = die_specification (die, &spec_cu);
16956 if (spec_die == NULL)
16957 parent = die->parent;
16958 else
63d06c5c 16959 {
0114d602
DJ
16960 parent = spec_die->parent;
16961 cu = spec_cu;
63d06c5c 16962 }
0114d602
DJ
16963
16964 if (parent == NULL)
16965 return "";
98bfdba5
PA
16966 else if (parent->building_fullname)
16967 {
16968 const char *name;
16969 const char *parent_name;
16970
16971 /* It has been seen on RealView 2.2 built binaries,
16972 DW_TAG_template_type_param types actually _defined_ as
16973 children of the parent class:
16974
16975 enum E {};
16976 template class <class Enum> Class{};
16977 Class<enum E> class_e;
16978
16979 1: DW_TAG_class_type (Class)
16980 2: DW_TAG_enumeration_type (E)
16981 3: DW_TAG_enumerator (enum1:0)
16982 3: DW_TAG_enumerator (enum2:1)
16983 ...
16984 2: DW_TAG_template_type_param
16985 DW_AT_type DW_FORM_ref_udata (E)
16986
16987 Besides being broken debug info, it can put GDB into an
16988 infinite loop. Consider:
16989
16990 When we're building the full name for Class<E>, we'll start
16991 at Class, and go look over its template type parameters,
16992 finding E. We'll then try to build the full name of E, and
16993 reach here. We're now trying to build the full name of E,
16994 and look over the parent DIE for containing scope. In the
16995 broken case, if we followed the parent DIE of E, we'd again
16996 find Class, and once again go look at its template type
16997 arguments, etc., etc. Simply don't consider such parent die
16998 as source-level parent of this die (it can't be, the language
16999 doesn't allow it), and break the loop here. */
17000 name = dwarf2_name (die, cu);
17001 parent_name = dwarf2_name (parent, cu);
17002 complaint (&symfile_complaints,
17003 _("template param type '%s' defined within parent '%s'"),
17004 name ? name : "<unknown>",
17005 parent_name ? parent_name : "<unknown>");
17006 return "";
17007 }
63d06c5c 17008 else
0114d602
DJ
17009 switch (parent->tag)
17010 {
63d06c5c 17011 case DW_TAG_namespace:
0114d602 17012 parent_type = read_type_die (parent, cu);
acebe513
UW
17013 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17014 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17015 Work around this problem here. */
17016 if (cu->language == language_cplus
17017 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17018 return "";
0114d602
DJ
17019 /* We give a name to even anonymous namespaces. */
17020 return TYPE_TAG_NAME (parent_type);
63d06c5c 17021 case DW_TAG_class_type:
680b30c7 17022 case DW_TAG_interface_type:
63d06c5c 17023 case DW_TAG_structure_type:
0114d602 17024 case DW_TAG_union_type:
f55ee35c 17025 case DW_TAG_module:
0114d602
DJ
17026 parent_type = read_type_die (parent, cu);
17027 if (TYPE_TAG_NAME (parent_type) != NULL)
17028 return TYPE_TAG_NAME (parent_type);
17029 else
17030 /* An anonymous structure is only allowed non-static data
17031 members; no typedefs, no member functions, et cetera.
17032 So it does not need a prefix. */
17033 return "";
abc72ce4 17034 case DW_TAG_compile_unit:
95554aad 17035 case DW_TAG_partial_unit:
abc72ce4
DE
17036 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17037 if (cu->language == language_cplus
8b70b953 17038 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17039 && die->child != NULL
17040 && (die->tag == DW_TAG_class_type
17041 || die->tag == DW_TAG_structure_type
17042 || die->tag == DW_TAG_union_type))
17043 {
17044 char *name = guess_full_die_structure_name (die, cu);
17045 if (name != NULL)
17046 return name;
17047 }
17048 return "";
63d06c5c 17049 default:
8176b9b8 17050 return determine_prefix (parent, cu);
63d06c5c 17051 }
63d06c5c
DC
17052}
17053
3e43a32a
MS
17054/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17055 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17056 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17057 an obconcat, otherwise allocate storage for the result. The CU argument is
17058 used to determine the language and hence, the appropriate separator. */
987504bb 17059
f55ee35c 17060#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17061
17062static char *
f55ee35c
JK
17063typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17064 int physname, struct dwarf2_cu *cu)
63d06c5c 17065{
f55ee35c 17066 const char *lead = "";
5c315b68 17067 const char *sep;
63d06c5c 17068
3e43a32a
MS
17069 if (suffix == NULL || suffix[0] == '\0'
17070 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17071 sep = "";
17072 else if (cu->language == language_java)
17073 sep = ".";
f55ee35c
JK
17074 else if (cu->language == language_fortran && physname)
17075 {
17076 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17077 DW_AT_MIPS_linkage_name is preferred and used instead. */
17078
17079 lead = "__";
17080 sep = "_MOD_";
17081 }
987504bb
JJ
17082 else
17083 sep = "::";
63d06c5c 17084
6dd47d34
DE
17085 if (prefix == NULL)
17086 prefix = "";
17087 if (suffix == NULL)
17088 suffix = "";
17089
987504bb
JJ
17090 if (obs == NULL)
17091 {
3e43a32a
MS
17092 char *retval
17093 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17094
f55ee35c
JK
17095 strcpy (retval, lead);
17096 strcat (retval, prefix);
6dd47d34
DE
17097 strcat (retval, sep);
17098 strcat (retval, suffix);
63d06c5c
DC
17099 return retval;
17100 }
987504bb
JJ
17101 else
17102 {
17103 /* We have an obstack. */
f55ee35c 17104 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17105 }
63d06c5c
DC
17106}
17107
c906108c
SS
17108/* Return sibling of die, NULL if no sibling. */
17109
f9aca02d 17110static struct die_info *
fba45db2 17111sibling_die (struct die_info *die)
c906108c 17112{
639d11d3 17113 return die->sibling;
c906108c
SS
17114}
17115
71c25dea
TT
17116/* Get name of a die, return NULL if not found. */
17117
15d034d0
TT
17118static const char *
17119dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17120 struct obstack *obstack)
17121{
17122 if (name && cu->language == language_cplus)
17123 {
17124 char *canon_name = cp_canonicalize_string (name);
17125
17126 if (canon_name != NULL)
17127 {
17128 if (strcmp (canon_name, name) != 0)
10f0c4bb 17129 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17130 xfree (canon_name);
17131 }
17132 }
17133
17134 return name;
c906108c
SS
17135}
17136
9219021c
DC
17137/* Get name of a die, return NULL if not found. */
17138
15d034d0 17139static const char *
e142c38c 17140dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17141{
17142 struct attribute *attr;
17143
e142c38c 17144 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17145 if ((!attr || !DW_STRING (attr))
17146 && die->tag != DW_TAG_class_type
17147 && die->tag != DW_TAG_interface_type
17148 && die->tag != DW_TAG_structure_type
17149 && die->tag != DW_TAG_union_type)
71c25dea
TT
17150 return NULL;
17151
17152 switch (die->tag)
17153 {
17154 case DW_TAG_compile_unit:
95554aad 17155 case DW_TAG_partial_unit:
71c25dea
TT
17156 /* Compilation units have a DW_AT_name that is a filename, not
17157 a source language identifier. */
17158 case DW_TAG_enumeration_type:
17159 case DW_TAG_enumerator:
17160 /* These tags always have simple identifiers already; no need
17161 to canonicalize them. */
17162 return DW_STRING (attr);
907af001 17163
418835cc
KS
17164 case DW_TAG_subprogram:
17165 /* Java constructors will all be named "<init>", so return
17166 the class name when we see this special case. */
17167 if (cu->language == language_java
17168 && DW_STRING (attr) != NULL
17169 && strcmp (DW_STRING (attr), "<init>") == 0)
17170 {
17171 struct dwarf2_cu *spec_cu = cu;
17172 struct die_info *spec_die;
17173
17174 /* GCJ will output '<init>' for Java constructor names.
17175 For this special case, return the name of the parent class. */
17176
17177 /* GCJ may output suprogram DIEs with AT_specification set.
17178 If so, use the name of the specified DIE. */
17179 spec_die = die_specification (die, &spec_cu);
17180 if (spec_die != NULL)
17181 return dwarf2_name (spec_die, spec_cu);
17182
17183 do
17184 {
17185 die = die->parent;
17186 if (die->tag == DW_TAG_class_type)
17187 return dwarf2_name (die, cu);
17188 }
95554aad
TT
17189 while (die->tag != DW_TAG_compile_unit
17190 && die->tag != DW_TAG_partial_unit);
418835cc 17191 }
907af001
UW
17192 break;
17193
17194 case DW_TAG_class_type:
17195 case DW_TAG_interface_type:
17196 case DW_TAG_structure_type:
17197 case DW_TAG_union_type:
17198 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17199 structures or unions. These were of the form "._%d" in GCC 4.1,
17200 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17201 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17202 if (attr && DW_STRING (attr)
17203 && (strncmp (DW_STRING (attr), "._", 2) == 0
17204 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17205 return NULL;
53832f31
TT
17206
17207 /* GCC might emit a nameless typedef that has a linkage name. See
17208 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17209 if (!attr || DW_STRING (attr) == NULL)
17210 {
df5c6c50 17211 char *demangled = NULL;
53832f31
TT
17212
17213 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17214 if (attr == NULL)
17215 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17216
17217 if (attr == NULL || DW_STRING (attr) == NULL)
17218 return NULL;
17219
df5c6c50
JK
17220 /* Avoid demangling DW_STRING (attr) the second time on a second
17221 call for the same DIE. */
17222 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17223 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17224
17225 if (demangled)
17226 {
96408a79
SA
17227 char *base;
17228
53832f31 17229 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17230 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17231 demangled, strlen (demangled));
53832f31
TT
17232 DW_STRING_IS_CANONICAL (attr) = 1;
17233 xfree (demangled);
96408a79
SA
17234
17235 /* Strip any leading namespaces/classes, keep only the base name.
17236 DW_AT_name for named DIEs does not contain the prefixes. */
17237 base = strrchr (DW_STRING (attr), ':');
17238 if (base && base > DW_STRING (attr) && base[-1] == ':')
17239 return &base[1];
17240 else
17241 return DW_STRING (attr);
53832f31
TT
17242 }
17243 }
907af001
UW
17244 break;
17245
71c25dea 17246 default:
907af001
UW
17247 break;
17248 }
17249
17250 if (!DW_STRING_IS_CANONICAL (attr))
17251 {
17252 DW_STRING (attr)
17253 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17254 &cu->objfile->objfile_obstack);
17255 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17256 }
907af001 17257 return DW_STRING (attr);
9219021c
DC
17258}
17259
17260/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17261 is none. *EXT_CU is the CU containing DIE on input, and the CU
17262 containing the return value on output. */
9219021c
DC
17263
17264static struct die_info *
f2f0e013 17265dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17266{
17267 struct attribute *attr;
9219021c 17268
f2f0e013 17269 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17270 if (attr == NULL)
17271 return NULL;
17272
f2f0e013 17273 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17274}
17275
c906108c
SS
17276/* Convert a DIE tag into its string name. */
17277
f39c6ffd 17278static const char *
aa1ee363 17279dwarf_tag_name (unsigned tag)
c906108c 17280{
f39c6ffd
TT
17281 const char *name = get_DW_TAG_name (tag);
17282
17283 if (name == NULL)
17284 return "DW_TAG_<unknown>";
17285
17286 return name;
c906108c
SS
17287}
17288
17289/* Convert a DWARF attribute code into its string name. */
17290
f39c6ffd 17291static const char *
aa1ee363 17292dwarf_attr_name (unsigned attr)
c906108c 17293{
f39c6ffd
TT
17294 const char *name;
17295
c764a876 17296#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17297 if (attr == DW_AT_MIPS_fde)
17298 return "DW_AT_MIPS_fde";
17299#else
17300 if (attr == DW_AT_HP_block_index)
17301 return "DW_AT_HP_block_index";
c764a876 17302#endif
f39c6ffd
TT
17303
17304 name = get_DW_AT_name (attr);
17305
17306 if (name == NULL)
17307 return "DW_AT_<unknown>";
17308
17309 return name;
c906108c
SS
17310}
17311
17312/* Convert a DWARF value form code into its string name. */
17313
f39c6ffd 17314static const char *
aa1ee363 17315dwarf_form_name (unsigned form)
c906108c 17316{
f39c6ffd
TT
17317 const char *name = get_DW_FORM_name (form);
17318
17319 if (name == NULL)
17320 return "DW_FORM_<unknown>";
17321
17322 return name;
c906108c
SS
17323}
17324
17325static char *
fba45db2 17326dwarf_bool_name (unsigned mybool)
c906108c
SS
17327{
17328 if (mybool)
17329 return "TRUE";
17330 else
17331 return "FALSE";
17332}
17333
17334/* Convert a DWARF type code into its string name. */
17335
f39c6ffd 17336static const char *
aa1ee363 17337dwarf_type_encoding_name (unsigned enc)
c906108c 17338{
f39c6ffd 17339 const char *name = get_DW_ATE_name (enc);
c906108c 17340
f39c6ffd
TT
17341 if (name == NULL)
17342 return "DW_ATE_<unknown>";
c906108c 17343
f39c6ffd 17344 return name;
c906108c 17345}
c906108c 17346
f9aca02d 17347static void
d97bc12b 17348dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17349{
17350 unsigned int i;
17351
d97bc12b
DE
17352 print_spaces (indent, f);
17353 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17354 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17355
17356 if (die->parent != NULL)
17357 {
17358 print_spaces (indent, f);
17359 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17360 die->parent->offset.sect_off);
d97bc12b
DE
17361 }
17362
17363 print_spaces (indent, f);
17364 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17365 dwarf_bool_name (die->child != NULL));
c906108c 17366
d97bc12b
DE
17367 print_spaces (indent, f);
17368 fprintf_unfiltered (f, " attributes:\n");
17369
c906108c
SS
17370 for (i = 0; i < die->num_attrs; ++i)
17371 {
d97bc12b
DE
17372 print_spaces (indent, f);
17373 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17374 dwarf_attr_name (die->attrs[i].name),
17375 dwarf_form_name (die->attrs[i].form));
d97bc12b 17376
c906108c
SS
17377 switch (die->attrs[i].form)
17378 {
c906108c 17379 case DW_FORM_addr:
3019eac3 17380 case DW_FORM_GNU_addr_index:
d97bc12b 17381 fprintf_unfiltered (f, "address: ");
5af949e3 17382 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17383 break;
17384 case DW_FORM_block2:
17385 case DW_FORM_block4:
17386 case DW_FORM_block:
17387 case DW_FORM_block1:
56eb65bd
SP
17388 fprintf_unfiltered (f, "block: size %s",
17389 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17390 break;
2dc7f7b3 17391 case DW_FORM_exprloc:
56eb65bd
SP
17392 fprintf_unfiltered (f, "expression: size %s",
17393 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17394 break;
4568ecf9
DE
17395 case DW_FORM_ref_addr:
17396 fprintf_unfiltered (f, "ref address: ");
17397 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17398 break;
36586728
TT
17399 case DW_FORM_GNU_ref_alt:
17400 fprintf_unfiltered (f, "alt ref address: ");
17401 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17402 break;
10b3939b
DJ
17403 case DW_FORM_ref1:
17404 case DW_FORM_ref2:
17405 case DW_FORM_ref4:
4568ecf9
DE
17406 case DW_FORM_ref8:
17407 case DW_FORM_ref_udata:
d97bc12b 17408 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17409 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17410 break;
c906108c
SS
17411 case DW_FORM_data1:
17412 case DW_FORM_data2:
17413 case DW_FORM_data4:
ce5d95e1 17414 case DW_FORM_data8:
c906108c
SS
17415 case DW_FORM_udata:
17416 case DW_FORM_sdata:
43bbcdc2
PH
17417 fprintf_unfiltered (f, "constant: %s",
17418 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17419 break;
2dc7f7b3
TT
17420 case DW_FORM_sec_offset:
17421 fprintf_unfiltered (f, "section offset: %s",
17422 pulongest (DW_UNSND (&die->attrs[i])));
17423 break;
55f1336d 17424 case DW_FORM_ref_sig8:
348e048f 17425 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
e4a48d9d
DE
17426 {
17427 struct signatured_type *sig_type =
17428 DW_SIGNATURED_TYPE (&die->attrs[i]);
17429
17430 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17431 hex_string (sig_type->signature),
17432 sig_type->per_cu.offset.sect_off);
17433 }
348e048f 17434 else
e4a48d9d 17435 fprintf_unfiltered (f, "signatured type, unknown");
348e048f 17436 break;
c906108c 17437 case DW_FORM_string:
4bdf3d34 17438 case DW_FORM_strp:
3019eac3 17439 case DW_FORM_GNU_str_index:
36586728 17440 case DW_FORM_GNU_strp_alt:
8285870a 17441 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17442 DW_STRING (&die->attrs[i])
8285870a
JK
17443 ? DW_STRING (&die->attrs[i]) : "",
17444 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17445 break;
17446 case DW_FORM_flag:
17447 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17448 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17449 else
d97bc12b 17450 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17451 break;
2dc7f7b3
TT
17452 case DW_FORM_flag_present:
17453 fprintf_unfiltered (f, "flag: TRUE");
17454 break;
a8329558 17455 case DW_FORM_indirect:
0963b4bd
MS
17456 /* The reader will have reduced the indirect form to
17457 the "base form" so this form should not occur. */
3e43a32a
MS
17458 fprintf_unfiltered (f,
17459 "unexpected attribute form: DW_FORM_indirect");
a8329558 17460 break;
c906108c 17461 default:
d97bc12b 17462 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17463 die->attrs[i].form);
d97bc12b 17464 break;
c906108c 17465 }
d97bc12b 17466 fprintf_unfiltered (f, "\n");
c906108c
SS
17467 }
17468}
17469
f9aca02d 17470static void
d97bc12b 17471dump_die_for_error (struct die_info *die)
c906108c 17472{
d97bc12b
DE
17473 dump_die_shallow (gdb_stderr, 0, die);
17474}
17475
17476static void
17477dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17478{
17479 int indent = level * 4;
17480
17481 gdb_assert (die != NULL);
17482
17483 if (level >= max_level)
17484 return;
17485
17486 dump_die_shallow (f, indent, die);
17487
17488 if (die->child != NULL)
c906108c 17489 {
d97bc12b
DE
17490 print_spaces (indent, f);
17491 fprintf_unfiltered (f, " Children:");
17492 if (level + 1 < max_level)
17493 {
17494 fprintf_unfiltered (f, "\n");
17495 dump_die_1 (f, level + 1, max_level, die->child);
17496 }
17497 else
17498 {
3e43a32a
MS
17499 fprintf_unfiltered (f,
17500 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17501 }
17502 }
17503
17504 if (die->sibling != NULL && level > 0)
17505 {
17506 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17507 }
17508}
17509
d97bc12b
DE
17510/* This is called from the pdie macro in gdbinit.in.
17511 It's not static so gcc will keep a copy callable from gdb. */
17512
17513void
17514dump_die (struct die_info *die, int max_level)
17515{
17516 dump_die_1 (gdb_stdlog, 0, max_level, die);
17517}
17518
f9aca02d 17519static void
51545339 17520store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17521{
51545339 17522 void **slot;
c906108c 17523
b64f50a1
JK
17524 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17525 INSERT);
51545339
DJ
17526
17527 *slot = die;
c906108c
SS
17528}
17529
b64f50a1
JK
17530/* DW_ADDR is always stored already as sect_offset; despite for the forms
17531 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17532
93311388
DE
17533static int
17534is_ref_attr (struct attribute *attr)
c906108c 17535{
c906108c
SS
17536 switch (attr->form)
17537 {
17538 case DW_FORM_ref_addr:
c906108c
SS
17539 case DW_FORM_ref1:
17540 case DW_FORM_ref2:
17541 case DW_FORM_ref4:
613e1657 17542 case DW_FORM_ref8:
c906108c 17543 case DW_FORM_ref_udata:
36586728 17544 case DW_FORM_GNU_ref_alt:
93311388 17545 return 1;
c906108c 17546 default:
93311388 17547 return 0;
c906108c 17548 }
93311388
DE
17549}
17550
b64f50a1
JK
17551/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17552 required kind. */
17553
17554static sect_offset
93311388
DE
17555dwarf2_get_ref_die_offset (struct attribute *attr)
17556{
4568ecf9 17557 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17558
93311388 17559 if (is_ref_attr (attr))
b64f50a1 17560 return retval;
93311388 17561
b64f50a1 17562 retval.sect_off = 0;
93311388
DE
17563 complaint (&symfile_complaints,
17564 _("unsupported die ref attribute form: '%s'"),
17565 dwarf_form_name (attr->form));
b64f50a1 17566 return retval;
c906108c
SS
17567}
17568
43bbcdc2
PH
17569/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17570 * the value held by the attribute is not constant. */
a02abb62 17571
43bbcdc2 17572static LONGEST
a02abb62
JB
17573dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17574{
17575 if (attr->form == DW_FORM_sdata)
17576 return DW_SND (attr);
17577 else if (attr->form == DW_FORM_udata
17578 || attr->form == DW_FORM_data1
17579 || attr->form == DW_FORM_data2
17580 || attr->form == DW_FORM_data4
17581 || attr->form == DW_FORM_data8)
17582 return DW_UNSND (attr);
17583 else
17584 {
3e43a32a
MS
17585 complaint (&symfile_complaints,
17586 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17587 dwarf_form_name (attr->form));
17588 return default_value;
17589 }
17590}
17591
348e048f
DE
17592/* Follow reference or signature attribute ATTR of SRC_DIE.
17593 On entry *REF_CU is the CU of SRC_DIE.
17594 On exit *REF_CU is the CU of the result. */
17595
17596static struct die_info *
17597follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17598 struct dwarf2_cu **ref_cu)
17599{
17600 struct die_info *die;
17601
17602 if (is_ref_attr (attr))
17603 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17604 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17605 die = follow_die_sig (src_die, attr, ref_cu);
17606 else
17607 {
17608 dump_die_for_error (src_die);
17609 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17610 (*ref_cu)->objfile->name);
17611 }
17612
17613 return die;
03dd20cc
DJ
17614}
17615
5c631832 17616/* Follow reference OFFSET.
673bfd45
DE
17617 On entry *REF_CU is the CU of the source die referencing OFFSET.
17618 On exit *REF_CU is the CU of the result.
17619 Returns NULL if OFFSET is invalid. */
f504f079 17620
f9aca02d 17621static struct die_info *
36586728
TT
17622follow_die_offset (sect_offset offset, int offset_in_dwz,
17623 struct dwarf2_cu **ref_cu)
c906108c 17624{
10b3939b 17625 struct die_info temp_die;
f2f0e013 17626 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17627
348e048f
DE
17628 gdb_assert (cu->per_cu != NULL);
17629
98bfdba5
PA
17630 target_cu = cu;
17631
3019eac3 17632 if (cu->per_cu->is_debug_types)
348e048f
DE
17633 {
17634 /* .debug_types CUs cannot reference anything outside their CU.
17635 If they need to, they have to reference a signatured type via
55f1336d 17636 DW_FORM_ref_sig8. */
348e048f 17637 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17638 return NULL;
348e048f 17639 }
36586728
TT
17640 else if (offset_in_dwz != cu->per_cu->is_dwz
17641 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17642 {
17643 struct dwarf2_per_cu_data *per_cu;
9a619af0 17644
36586728
TT
17645 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17646 cu->objfile);
03dd20cc
DJ
17647
17648 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17649 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17650 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17651
10b3939b
DJ
17652 target_cu = per_cu->cu;
17653 }
98bfdba5
PA
17654 else if (cu->dies == NULL)
17655 {
17656 /* We're loading full DIEs during partial symbol reading. */
17657 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17658 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17659 }
c906108c 17660
f2f0e013 17661 *ref_cu = target_cu;
51545339 17662 temp_die.offset = offset;
b64f50a1 17663 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17664}
10b3939b 17665
5c631832
JK
17666/* Follow reference attribute ATTR of SRC_DIE.
17667 On entry *REF_CU is the CU of SRC_DIE.
17668 On exit *REF_CU is the CU of the result. */
17669
17670static struct die_info *
17671follow_die_ref (struct die_info *src_die, struct attribute *attr,
17672 struct dwarf2_cu **ref_cu)
17673{
b64f50a1 17674 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17675 struct dwarf2_cu *cu = *ref_cu;
17676 struct die_info *die;
17677
36586728
TT
17678 die = follow_die_offset (offset,
17679 (attr->form == DW_FORM_GNU_ref_alt
17680 || cu->per_cu->is_dwz),
17681 ref_cu);
5c631832
JK
17682 if (!die)
17683 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17684 "at 0x%x [in module %s]"),
b64f50a1 17685 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17686
5c631832
JK
17687 return die;
17688}
17689
d83e736b
JK
17690/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17691 Returned value is intended for DW_OP_call*. Returned
17692 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17693
17694struct dwarf2_locexpr_baton
8b9737bf
TT
17695dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17696 struct dwarf2_per_cu_data *per_cu,
17697 CORE_ADDR (*get_frame_pc) (void *baton),
17698 void *baton)
5c631832 17699{
918dd910 17700 struct dwarf2_cu *cu;
5c631832
JK
17701 struct die_info *die;
17702 struct attribute *attr;
17703 struct dwarf2_locexpr_baton retval;
17704
8cf6f0b1
TT
17705 dw2_setup (per_cu->objfile);
17706
918dd910
JK
17707 if (per_cu->cu == NULL)
17708 load_cu (per_cu);
17709 cu = per_cu->cu;
17710
36586728 17711 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17712 if (!die)
17713 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17714 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17715
17716 attr = dwarf2_attr (die, DW_AT_location, cu);
17717 if (!attr)
17718 {
e103e986
JK
17719 /* DWARF: "If there is no such attribute, then there is no effect.".
17720 DATA is ignored if SIZE is 0. */
5c631832 17721
e103e986 17722 retval.data = NULL;
5c631832
JK
17723 retval.size = 0;
17724 }
8cf6f0b1
TT
17725 else if (attr_form_is_section_offset (attr))
17726 {
17727 struct dwarf2_loclist_baton loclist_baton;
17728 CORE_ADDR pc = (*get_frame_pc) (baton);
17729 size_t size;
17730
17731 fill_in_loclist_baton (cu, &loclist_baton, attr);
17732
17733 retval.data = dwarf2_find_location_expression (&loclist_baton,
17734 &size, pc);
17735 retval.size = size;
17736 }
5c631832
JK
17737 else
17738 {
17739 if (!attr_form_is_block (attr))
17740 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17741 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17742 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17743
17744 retval.data = DW_BLOCK (attr)->data;
17745 retval.size = DW_BLOCK (attr)->size;
17746 }
17747 retval.per_cu = cu->per_cu;
918dd910 17748
918dd910
JK
17749 age_cached_comp_units ();
17750
5c631832 17751 return retval;
348e048f
DE
17752}
17753
8b9737bf
TT
17754/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17755 offset. */
17756
17757struct dwarf2_locexpr_baton
17758dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17759 struct dwarf2_per_cu_data *per_cu,
17760 CORE_ADDR (*get_frame_pc) (void *baton),
17761 void *baton)
17762{
17763 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17764
17765 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17766}
17767
8a9b8146
TT
17768/* Return the type of the DIE at DIE_OFFSET in the CU named by
17769 PER_CU. */
17770
17771struct type *
b64f50a1 17772dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17773 struct dwarf2_per_cu_data *per_cu)
17774{
b64f50a1
JK
17775 sect_offset die_offset_sect;
17776
8a9b8146 17777 dw2_setup (per_cu->objfile);
b64f50a1
JK
17778
17779 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17780 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17781}
17782
348e048f
DE
17783/* Follow the signature attribute ATTR in SRC_DIE.
17784 On entry *REF_CU is the CU of SRC_DIE.
17785 On exit *REF_CU is the CU of the result. */
17786
17787static struct die_info *
17788follow_die_sig (struct die_info *src_die, struct attribute *attr,
17789 struct dwarf2_cu **ref_cu)
17790{
17791 struct objfile *objfile = (*ref_cu)->objfile;
17792 struct die_info temp_die;
17793 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17794 struct dwarf2_cu *sig_cu;
17795 struct die_info *die;
17796
17797 /* sig_type will be NULL if the signatured type is missing from
17798 the debug info. */
17799 if (sig_type == NULL)
17800 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17801 "at 0x%x [in module %s]"),
b64f50a1 17802 src_die->offset.sect_off, objfile->name);
348e048f
DE
17803
17804 /* If necessary, add it to the queue and load its DIEs. */
17805
95554aad 17806 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17807 read_signatured_type (sig_type);
348e048f
DE
17808
17809 gdb_assert (sig_type->per_cu.cu != NULL);
17810
17811 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17812 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17813 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17814 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17815 temp_die.offset.sect_off);
348e048f
DE
17816 if (die)
17817 {
796a7ff8
DE
17818 /* For .gdb_index version 7 keep track of included TUs.
17819 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17820 if (dwarf2_per_objfile->index_table != NULL
17821 && dwarf2_per_objfile->index_table->version <= 7)
17822 {
17823 VEC_safe_push (dwarf2_per_cu_ptr,
17824 (*ref_cu)->per_cu->imported_symtabs,
17825 sig_cu->per_cu);
17826 }
17827
348e048f
DE
17828 *ref_cu = sig_cu;
17829 return die;
17830 }
17831
3e43a32a
MS
17832 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17833 "from DIE at 0x%x [in module %s]"),
b64f50a1 17834 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17835}
17836
e5fe5e75 17837/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17838
17839static void
e5fe5e75 17840load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17841{
52dc124a 17842 struct signatured_type *sig_type;
348e048f 17843
f4dc4d17
DE
17844 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17845 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17846
6721b2ec
DE
17847 /* We have the per_cu, but we need the signatured_type.
17848 Fortunately this is an easy translation. */
17849 gdb_assert (per_cu->is_debug_types);
17850 sig_type = (struct signatured_type *) per_cu;
348e048f 17851
6721b2ec 17852 gdb_assert (per_cu->cu == NULL);
348e048f 17853
52dc124a 17854 read_signatured_type (sig_type);
348e048f 17855
6721b2ec 17856 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17857}
17858
dee91e82
DE
17859/* die_reader_func for read_signatured_type.
17860 This is identical to load_full_comp_unit_reader,
17861 but is kept separate for now. */
348e048f
DE
17862
17863static void
dee91e82 17864read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 17865 const gdb_byte *info_ptr,
dee91e82
DE
17866 struct die_info *comp_unit_die,
17867 int has_children,
17868 void *data)
348e048f 17869{
dee91e82 17870 struct dwarf2_cu *cu = reader->cu;
348e048f 17871
dee91e82
DE
17872 gdb_assert (cu->die_hash == NULL);
17873 cu->die_hash =
17874 htab_create_alloc_ex (cu->header.length / 12,
17875 die_hash,
17876 die_eq,
17877 NULL,
17878 &cu->comp_unit_obstack,
17879 hashtab_obstack_allocate,
17880 dummy_obstack_deallocate);
348e048f 17881
dee91e82
DE
17882 if (has_children)
17883 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17884 &info_ptr, comp_unit_die);
17885 cu->dies = comp_unit_die;
17886 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17887
17888 /* We try not to read any attributes in this function, because not
9cdd5dbd 17889 all CUs needed for references have been loaded yet, and symbol
348e048f 17890 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17891 or we won't be able to build types correctly.
17892 Similarly, if we do not read the producer, we can not apply
17893 producer-specific interpretation. */
95554aad 17894 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17895}
348e048f 17896
3019eac3
DE
17897/* Read in a signatured type and build its CU and DIEs.
17898 If the type is a stub for the real type in a DWO file,
17899 read in the real type from the DWO file as well. */
dee91e82
DE
17900
17901static void
17902read_signatured_type (struct signatured_type *sig_type)
17903{
17904 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17905
3019eac3 17906 gdb_assert (per_cu->is_debug_types);
dee91e82 17907 gdb_assert (per_cu->cu == NULL);
348e048f 17908
f4dc4d17
DE
17909 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17910 read_signatured_type_reader, NULL);
c906108c
SS
17911}
17912
c906108c
SS
17913/* Decode simple location descriptions.
17914 Given a pointer to a dwarf block that defines a location, compute
17915 the location and return the value.
17916
4cecd739
DJ
17917 NOTE drow/2003-11-18: This function is called in two situations
17918 now: for the address of static or global variables (partial symbols
17919 only) and for offsets into structures which are expected to be
17920 (more or less) constant. The partial symbol case should go away,
17921 and only the constant case should remain. That will let this
17922 function complain more accurately. A few special modes are allowed
17923 without complaint for global variables (for instance, global
17924 register values and thread-local values).
c906108c
SS
17925
17926 A location description containing no operations indicates that the
4cecd739 17927 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17928 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17929 callers will only want a very basic result and this can become a
21ae7a4d
JK
17930 complaint.
17931
17932 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17933
17934static CORE_ADDR
e7c27a73 17935decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17936{
e7c27a73 17937 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17938 size_t i;
17939 size_t size = blk->size;
d521ce57 17940 const gdb_byte *data = blk->data;
21ae7a4d
JK
17941 CORE_ADDR stack[64];
17942 int stacki;
17943 unsigned int bytes_read, unsnd;
17944 gdb_byte op;
c906108c 17945
21ae7a4d
JK
17946 i = 0;
17947 stacki = 0;
17948 stack[stacki] = 0;
17949 stack[++stacki] = 0;
17950
17951 while (i < size)
17952 {
17953 op = data[i++];
17954 switch (op)
17955 {
17956 case DW_OP_lit0:
17957 case DW_OP_lit1:
17958 case DW_OP_lit2:
17959 case DW_OP_lit3:
17960 case DW_OP_lit4:
17961 case DW_OP_lit5:
17962 case DW_OP_lit6:
17963 case DW_OP_lit7:
17964 case DW_OP_lit8:
17965 case DW_OP_lit9:
17966 case DW_OP_lit10:
17967 case DW_OP_lit11:
17968 case DW_OP_lit12:
17969 case DW_OP_lit13:
17970 case DW_OP_lit14:
17971 case DW_OP_lit15:
17972 case DW_OP_lit16:
17973 case DW_OP_lit17:
17974 case DW_OP_lit18:
17975 case DW_OP_lit19:
17976 case DW_OP_lit20:
17977 case DW_OP_lit21:
17978 case DW_OP_lit22:
17979 case DW_OP_lit23:
17980 case DW_OP_lit24:
17981 case DW_OP_lit25:
17982 case DW_OP_lit26:
17983 case DW_OP_lit27:
17984 case DW_OP_lit28:
17985 case DW_OP_lit29:
17986 case DW_OP_lit30:
17987 case DW_OP_lit31:
17988 stack[++stacki] = op - DW_OP_lit0;
17989 break;
f1bea926 17990
21ae7a4d
JK
17991 case DW_OP_reg0:
17992 case DW_OP_reg1:
17993 case DW_OP_reg2:
17994 case DW_OP_reg3:
17995 case DW_OP_reg4:
17996 case DW_OP_reg5:
17997 case DW_OP_reg6:
17998 case DW_OP_reg7:
17999 case DW_OP_reg8:
18000 case DW_OP_reg9:
18001 case DW_OP_reg10:
18002 case DW_OP_reg11:
18003 case DW_OP_reg12:
18004 case DW_OP_reg13:
18005 case DW_OP_reg14:
18006 case DW_OP_reg15:
18007 case DW_OP_reg16:
18008 case DW_OP_reg17:
18009 case DW_OP_reg18:
18010 case DW_OP_reg19:
18011 case DW_OP_reg20:
18012 case DW_OP_reg21:
18013 case DW_OP_reg22:
18014 case DW_OP_reg23:
18015 case DW_OP_reg24:
18016 case DW_OP_reg25:
18017 case DW_OP_reg26:
18018 case DW_OP_reg27:
18019 case DW_OP_reg28:
18020 case DW_OP_reg29:
18021 case DW_OP_reg30:
18022 case DW_OP_reg31:
18023 stack[++stacki] = op - DW_OP_reg0;
18024 if (i < size)
18025 dwarf2_complex_location_expr_complaint ();
18026 break;
c906108c 18027
21ae7a4d
JK
18028 case DW_OP_regx:
18029 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18030 i += bytes_read;
18031 stack[++stacki] = unsnd;
18032 if (i < size)
18033 dwarf2_complex_location_expr_complaint ();
18034 break;
c906108c 18035
21ae7a4d
JK
18036 case DW_OP_addr:
18037 stack[++stacki] = read_address (objfile->obfd, &data[i],
18038 cu, &bytes_read);
18039 i += bytes_read;
18040 break;
d53d4ac5 18041
21ae7a4d
JK
18042 case DW_OP_const1u:
18043 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18044 i += 1;
18045 break;
18046
18047 case DW_OP_const1s:
18048 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18049 i += 1;
18050 break;
18051
18052 case DW_OP_const2u:
18053 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18054 i += 2;
18055 break;
18056
18057 case DW_OP_const2s:
18058 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18059 i += 2;
18060 break;
d53d4ac5 18061
21ae7a4d
JK
18062 case DW_OP_const4u:
18063 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18064 i += 4;
18065 break;
18066
18067 case DW_OP_const4s:
18068 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18069 i += 4;
18070 break;
18071
585861ea
JK
18072 case DW_OP_const8u:
18073 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18074 i += 8;
18075 break;
18076
21ae7a4d
JK
18077 case DW_OP_constu:
18078 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18079 &bytes_read);
18080 i += bytes_read;
18081 break;
18082
18083 case DW_OP_consts:
18084 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18085 i += bytes_read;
18086 break;
18087
18088 case DW_OP_dup:
18089 stack[stacki + 1] = stack[stacki];
18090 stacki++;
18091 break;
18092
18093 case DW_OP_plus:
18094 stack[stacki - 1] += stack[stacki];
18095 stacki--;
18096 break;
18097
18098 case DW_OP_plus_uconst:
18099 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18100 &bytes_read);
18101 i += bytes_read;
18102 break;
18103
18104 case DW_OP_minus:
18105 stack[stacki - 1] -= stack[stacki];
18106 stacki--;
18107 break;
18108
18109 case DW_OP_deref:
18110 /* If we're not the last op, then we definitely can't encode
18111 this using GDB's address_class enum. This is valid for partial
18112 global symbols, although the variable's address will be bogus
18113 in the psymtab. */
18114 if (i < size)
18115 dwarf2_complex_location_expr_complaint ();
18116 break;
18117
18118 case DW_OP_GNU_push_tls_address:
18119 /* The top of the stack has the offset from the beginning
18120 of the thread control block at which the variable is located. */
18121 /* Nothing should follow this operator, so the top of stack would
18122 be returned. */
18123 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18124 address will be bogus in the psymtab. Make it always at least
18125 non-zero to not look as a variable garbage collected by linker
18126 which have DW_OP_addr 0. */
21ae7a4d
JK
18127 if (i < size)
18128 dwarf2_complex_location_expr_complaint ();
585861ea 18129 stack[stacki]++;
21ae7a4d
JK
18130 break;
18131
18132 case DW_OP_GNU_uninit:
18133 break;
18134
3019eac3 18135 case DW_OP_GNU_addr_index:
49f6c839 18136 case DW_OP_GNU_const_index:
3019eac3
DE
18137 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18138 &bytes_read);
18139 i += bytes_read;
18140 break;
18141
21ae7a4d
JK
18142 default:
18143 {
f39c6ffd 18144 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18145
18146 if (name)
18147 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18148 name);
18149 else
18150 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18151 op);
18152 }
18153
18154 return (stack[stacki]);
d53d4ac5 18155 }
3c6e0cb3 18156
21ae7a4d
JK
18157 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18158 outside of the allocated space. Also enforce minimum>0. */
18159 if (stacki >= ARRAY_SIZE (stack) - 1)
18160 {
18161 complaint (&symfile_complaints,
18162 _("location description stack overflow"));
18163 return 0;
18164 }
18165
18166 if (stacki <= 0)
18167 {
18168 complaint (&symfile_complaints,
18169 _("location description stack underflow"));
18170 return 0;
18171 }
18172 }
18173 return (stack[stacki]);
c906108c
SS
18174}
18175
18176/* memory allocation interface */
18177
c906108c 18178static struct dwarf_block *
7b5a2f43 18179dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18180{
18181 struct dwarf_block *blk;
18182
18183 blk = (struct dwarf_block *)
7b5a2f43 18184 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18185 return (blk);
18186}
18187
c906108c 18188static struct die_info *
b60c80d6 18189dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18190{
18191 struct die_info *die;
b60c80d6
DJ
18192 size_t size = sizeof (struct die_info);
18193
18194 if (num_attrs > 1)
18195 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18196
b60c80d6 18197 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18198 memset (die, 0, sizeof (struct die_info));
18199 return (die);
18200}
2e276125
JB
18201
18202\f
18203/* Macro support. */
18204
233d95b5
JK
18205/* Return file name relative to the compilation directory of file number I in
18206 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18207 responsible for freeing it. */
233d95b5 18208
2e276125 18209static char *
233d95b5 18210file_file_name (int file, struct line_header *lh)
2e276125 18211{
6a83a1e6
EZ
18212 /* Is the file number a valid index into the line header's file name
18213 table? Remember that file numbers start with one, not zero. */
18214 if (1 <= file && file <= lh->num_file_names)
18215 {
18216 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18217
233d95b5 18218 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18219 return xstrdup (fe->name);
233d95b5
JK
18220 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18221 fe->name, NULL);
6a83a1e6 18222 }
2e276125
JB
18223 else
18224 {
6a83a1e6
EZ
18225 /* The compiler produced a bogus file number. We can at least
18226 record the macro definitions made in the file, even if we
18227 won't be able to find the file by name. */
18228 char fake_name[80];
9a619af0 18229
8c042590
PM
18230 xsnprintf (fake_name, sizeof (fake_name),
18231 "<bad macro file number %d>", file);
2e276125 18232
6e70227d 18233 complaint (&symfile_complaints,
6a83a1e6
EZ
18234 _("bad file number in macro information (%d)"),
18235 file);
2e276125 18236
6a83a1e6 18237 return xstrdup (fake_name);
2e276125
JB
18238 }
18239}
18240
233d95b5
JK
18241/* Return the full name of file number I in *LH's file name table.
18242 Use COMP_DIR as the name of the current directory of the
18243 compilation. The result is allocated using xmalloc; the caller is
18244 responsible for freeing it. */
18245static char *
18246file_full_name (int file, struct line_header *lh, const char *comp_dir)
18247{
18248 /* Is the file number a valid index into the line header's file name
18249 table? Remember that file numbers start with one, not zero. */
18250 if (1 <= file && file <= lh->num_file_names)
18251 {
18252 char *relative = file_file_name (file, lh);
18253
18254 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18255 return relative;
18256 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18257 }
18258 else
18259 return file_file_name (file, lh);
18260}
18261
2e276125
JB
18262
18263static struct macro_source_file *
18264macro_start_file (int file, int line,
18265 struct macro_source_file *current_file,
18266 const char *comp_dir,
18267 struct line_header *lh, struct objfile *objfile)
18268{
233d95b5
JK
18269 /* File name relative to the compilation directory of this source file. */
18270 char *file_name = file_file_name (file, lh);
2e276125
JB
18271
18272 /* We don't create a macro table for this compilation unit
18273 at all until we actually get a filename. */
18274 if (! pending_macros)
6532ff36 18275 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18276 objfile->per_bfd->macro_cache,
18277 comp_dir);
2e276125
JB
18278
18279 if (! current_file)
abc9d0dc
TT
18280 {
18281 /* If we have no current file, then this must be the start_file
18282 directive for the compilation unit's main source file. */
233d95b5 18283 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18284 macro_define_special (pending_macros);
18285 }
2e276125 18286 else
233d95b5 18287 current_file = macro_include (current_file, line, file_name);
2e276125 18288
233d95b5 18289 xfree (file_name);
6e70227d 18290
2e276125
JB
18291 return current_file;
18292}
18293
18294
18295/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18296 followed by a null byte. */
18297static char *
18298copy_string (const char *buf, int len)
18299{
18300 char *s = xmalloc (len + 1);
9a619af0 18301
2e276125
JB
18302 memcpy (s, buf, len);
18303 s[len] = '\0';
2e276125
JB
18304 return s;
18305}
18306
18307
18308static const char *
18309consume_improper_spaces (const char *p, const char *body)
18310{
18311 if (*p == ' ')
18312 {
4d3c2250 18313 complaint (&symfile_complaints,
3e43a32a
MS
18314 _("macro definition contains spaces "
18315 "in formal argument list:\n`%s'"),
4d3c2250 18316 body);
2e276125
JB
18317
18318 while (*p == ' ')
18319 p++;
18320 }
18321
18322 return p;
18323}
18324
18325
18326static void
18327parse_macro_definition (struct macro_source_file *file, int line,
18328 const char *body)
18329{
18330 const char *p;
18331
18332 /* The body string takes one of two forms. For object-like macro
18333 definitions, it should be:
18334
18335 <macro name> " " <definition>
18336
18337 For function-like macro definitions, it should be:
18338
18339 <macro name> "() " <definition>
18340 or
18341 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18342
18343 Spaces may appear only where explicitly indicated, and in the
18344 <definition>.
18345
18346 The Dwarf 2 spec says that an object-like macro's name is always
18347 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18348 the space when the macro's definition is the empty string.
2e276125
JB
18349
18350 The Dwarf 2 spec says that there should be no spaces between the
18351 formal arguments in a function-like macro's formal argument list,
18352 but versions of GCC around March 2002 include spaces after the
18353 commas. */
18354
18355
18356 /* Find the extent of the macro name. The macro name is terminated
18357 by either a space or null character (for an object-like macro) or
18358 an opening paren (for a function-like macro). */
18359 for (p = body; *p; p++)
18360 if (*p == ' ' || *p == '(')
18361 break;
18362
18363 if (*p == ' ' || *p == '\0')
18364 {
18365 /* It's an object-like macro. */
18366 int name_len = p - body;
18367 char *name = copy_string (body, name_len);
18368 const char *replacement;
18369
18370 if (*p == ' ')
18371 replacement = body + name_len + 1;
18372 else
18373 {
4d3c2250 18374 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18375 replacement = body + name_len;
18376 }
6e70227d 18377
2e276125
JB
18378 macro_define_object (file, line, name, replacement);
18379
18380 xfree (name);
18381 }
18382 else if (*p == '(')
18383 {
18384 /* It's a function-like macro. */
18385 char *name = copy_string (body, p - body);
18386 int argc = 0;
18387 int argv_size = 1;
18388 char **argv = xmalloc (argv_size * sizeof (*argv));
18389
18390 p++;
18391
18392 p = consume_improper_spaces (p, body);
18393
18394 /* Parse the formal argument list. */
18395 while (*p && *p != ')')
18396 {
18397 /* Find the extent of the current argument name. */
18398 const char *arg_start = p;
18399
18400 while (*p && *p != ',' && *p != ')' && *p != ' ')
18401 p++;
18402
18403 if (! *p || p == arg_start)
4d3c2250 18404 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18405 else
18406 {
18407 /* Make sure argv has room for the new argument. */
18408 if (argc >= argv_size)
18409 {
18410 argv_size *= 2;
18411 argv = xrealloc (argv, argv_size * sizeof (*argv));
18412 }
18413
18414 argv[argc++] = copy_string (arg_start, p - arg_start);
18415 }
18416
18417 p = consume_improper_spaces (p, body);
18418
18419 /* Consume the comma, if present. */
18420 if (*p == ',')
18421 {
18422 p++;
18423
18424 p = consume_improper_spaces (p, body);
18425 }
18426 }
18427
18428 if (*p == ')')
18429 {
18430 p++;
18431
18432 if (*p == ' ')
18433 /* Perfectly formed definition, no complaints. */
18434 macro_define_function (file, line, name,
6e70227d 18435 argc, (const char **) argv,
2e276125
JB
18436 p + 1);
18437 else if (*p == '\0')
18438 {
18439 /* Complain, but do define it. */
4d3c2250 18440 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18441 macro_define_function (file, line, name,
6e70227d 18442 argc, (const char **) argv,
2e276125
JB
18443 p);
18444 }
18445 else
18446 /* Just complain. */
4d3c2250 18447 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18448 }
18449 else
18450 /* Just complain. */
4d3c2250 18451 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18452
18453 xfree (name);
18454 {
18455 int i;
18456
18457 for (i = 0; i < argc; i++)
18458 xfree (argv[i]);
18459 }
18460 xfree (argv);
18461 }
18462 else
4d3c2250 18463 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18464}
18465
cf2c3c16
TT
18466/* Skip some bytes from BYTES according to the form given in FORM.
18467 Returns the new pointer. */
2e276125 18468
d521ce57
TT
18469static const gdb_byte *
18470skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
18471 enum dwarf_form form,
18472 unsigned int offset_size,
18473 struct dwarf2_section_info *section)
2e276125 18474{
cf2c3c16 18475 unsigned int bytes_read;
2e276125 18476
cf2c3c16 18477 switch (form)
2e276125 18478 {
cf2c3c16
TT
18479 case DW_FORM_data1:
18480 case DW_FORM_flag:
18481 ++bytes;
18482 break;
18483
18484 case DW_FORM_data2:
18485 bytes += 2;
18486 break;
18487
18488 case DW_FORM_data4:
18489 bytes += 4;
18490 break;
18491
18492 case DW_FORM_data8:
18493 bytes += 8;
18494 break;
18495
18496 case DW_FORM_string:
18497 read_direct_string (abfd, bytes, &bytes_read);
18498 bytes += bytes_read;
18499 break;
18500
18501 case DW_FORM_sec_offset:
18502 case DW_FORM_strp:
36586728 18503 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18504 bytes += offset_size;
18505 break;
18506
18507 case DW_FORM_block:
18508 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18509 bytes += bytes_read;
18510 break;
18511
18512 case DW_FORM_block1:
18513 bytes += 1 + read_1_byte (abfd, bytes);
18514 break;
18515 case DW_FORM_block2:
18516 bytes += 2 + read_2_bytes (abfd, bytes);
18517 break;
18518 case DW_FORM_block4:
18519 bytes += 4 + read_4_bytes (abfd, bytes);
18520 break;
18521
18522 case DW_FORM_sdata:
18523 case DW_FORM_udata:
3019eac3
DE
18524 case DW_FORM_GNU_addr_index:
18525 case DW_FORM_GNU_str_index:
d521ce57 18526 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
18527 if (bytes == NULL)
18528 {
18529 dwarf2_section_buffer_overflow_complaint (section);
18530 return NULL;
18531 }
cf2c3c16
TT
18532 break;
18533
18534 default:
18535 {
18536 complain:
18537 complaint (&symfile_complaints,
18538 _("invalid form 0x%x in `%s'"),
18539 form,
18540 section->asection->name);
18541 return NULL;
18542 }
2e276125
JB
18543 }
18544
cf2c3c16
TT
18545 return bytes;
18546}
757a13d0 18547
cf2c3c16
TT
18548/* A helper for dwarf_decode_macros that handles skipping an unknown
18549 opcode. Returns an updated pointer to the macro data buffer; or,
18550 on error, issues a complaint and returns NULL. */
757a13d0 18551
d521ce57 18552static const gdb_byte *
cf2c3c16 18553skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
18554 const gdb_byte **opcode_definitions,
18555 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
18556 bfd *abfd,
18557 unsigned int offset_size,
18558 struct dwarf2_section_info *section)
18559{
18560 unsigned int bytes_read, i;
18561 unsigned long arg;
d521ce57 18562 const gdb_byte *defn;
2e276125 18563
cf2c3c16 18564 if (opcode_definitions[opcode] == NULL)
2e276125 18565 {
cf2c3c16
TT
18566 complaint (&symfile_complaints,
18567 _("unrecognized DW_MACFINO opcode 0x%x"),
18568 opcode);
18569 return NULL;
18570 }
2e276125 18571
cf2c3c16
TT
18572 defn = opcode_definitions[opcode];
18573 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18574 defn += bytes_read;
2e276125 18575
cf2c3c16
TT
18576 for (i = 0; i < arg; ++i)
18577 {
f664829e
DE
18578 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18579 section);
cf2c3c16
TT
18580 if (mac_ptr == NULL)
18581 {
18582 /* skip_form_bytes already issued the complaint. */
18583 return NULL;
18584 }
18585 }
757a13d0 18586
cf2c3c16
TT
18587 return mac_ptr;
18588}
757a13d0 18589
cf2c3c16
TT
18590/* A helper function which parses the header of a macro section.
18591 If the macro section is the extended (for now called "GNU") type,
18592 then this updates *OFFSET_SIZE. Returns a pointer to just after
18593 the header, or issues a complaint and returns NULL on error. */
757a13d0 18594
d521ce57
TT
18595static const gdb_byte *
18596dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 18597 bfd *abfd,
d521ce57 18598 const gdb_byte *mac_ptr,
cf2c3c16
TT
18599 unsigned int *offset_size,
18600 int section_is_gnu)
18601{
18602 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18603
cf2c3c16
TT
18604 if (section_is_gnu)
18605 {
18606 unsigned int version, flags;
757a13d0 18607
cf2c3c16
TT
18608 version = read_2_bytes (abfd, mac_ptr);
18609 if (version != 4)
18610 {
18611 complaint (&symfile_complaints,
18612 _("unrecognized version `%d' in .debug_macro section"),
18613 version);
18614 return NULL;
18615 }
18616 mac_ptr += 2;
757a13d0 18617
cf2c3c16
TT
18618 flags = read_1_byte (abfd, mac_ptr);
18619 ++mac_ptr;
18620 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18621
cf2c3c16
TT
18622 if ((flags & 2) != 0)
18623 /* We don't need the line table offset. */
18624 mac_ptr += *offset_size;
757a13d0 18625
cf2c3c16
TT
18626 /* Vendor opcode descriptions. */
18627 if ((flags & 4) != 0)
18628 {
18629 unsigned int i, count;
757a13d0 18630
cf2c3c16
TT
18631 count = read_1_byte (abfd, mac_ptr);
18632 ++mac_ptr;
18633 for (i = 0; i < count; ++i)
18634 {
18635 unsigned int opcode, bytes_read;
18636 unsigned long arg;
18637
18638 opcode = read_1_byte (abfd, mac_ptr);
18639 ++mac_ptr;
18640 opcode_definitions[opcode] = mac_ptr;
18641 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18642 mac_ptr += bytes_read;
18643 mac_ptr += arg;
18644 }
757a13d0 18645 }
cf2c3c16 18646 }
757a13d0 18647
cf2c3c16
TT
18648 return mac_ptr;
18649}
757a13d0 18650
cf2c3c16 18651/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18652 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18653
18654static void
d521ce57
TT
18655dwarf_decode_macro_bytes (bfd *abfd,
18656 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 18657 struct macro_source_file *current_file,
15d034d0 18658 struct line_header *lh, const char *comp_dir,
cf2c3c16 18659 struct dwarf2_section_info *section,
36586728 18660 int section_is_gnu, int section_is_dwz,
cf2c3c16 18661 unsigned int offset_size,
8fc3fc34
TT
18662 struct objfile *objfile,
18663 htab_t include_hash)
cf2c3c16
TT
18664{
18665 enum dwarf_macro_record_type macinfo_type;
18666 int at_commandline;
d521ce57 18667 const gdb_byte *opcode_definitions[256];
757a13d0 18668
cf2c3c16
TT
18669 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18670 &offset_size, section_is_gnu);
18671 if (mac_ptr == NULL)
18672 {
18673 /* We already issued a complaint. */
18674 return;
18675 }
757a13d0
JK
18676
18677 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18678 GDB is still reading the definitions from command line. First
18679 DW_MACINFO_start_file will need to be ignored as it was already executed
18680 to create CURRENT_FILE for the main source holding also the command line
18681 definitions. On first met DW_MACINFO_start_file this flag is reset to
18682 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18683
18684 at_commandline = 1;
18685
18686 do
18687 {
18688 /* Do we at least have room for a macinfo type byte? */
18689 if (mac_ptr >= mac_end)
18690 {
f664829e 18691 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18692 break;
18693 }
18694
18695 macinfo_type = read_1_byte (abfd, mac_ptr);
18696 mac_ptr++;
18697
cf2c3c16
TT
18698 /* Note that we rely on the fact that the corresponding GNU and
18699 DWARF constants are the same. */
757a13d0
JK
18700 switch (macinfo_type)
18701 {
18702 /* A zero macinfo type indicates the end of the macro
18703 information. */
18704 case 0:
18705 break;
2e276125 18706
cf2c3c16
TT
18707 case DW_MACRO_GNU_define:
18708 case DW_MACRO_GNU_undef:
18709 case DW_MACRO_GNU_define_indirect:
18710 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18711 case DW_MACRO_GNU_define_indirect_alt:
18712 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18713 {
891d2f0b 18714 unsigned int bytes_read;
2e276125 18715 int line;
d521ce57 18716 const char *body;
cf2c3c16 18717 int is_define;
2e276125 18718
cf2c3c16
TT
18719 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18720 mac_ptr += bytes_read;
18721
18722 if (macinfo_type == DW_MACRO_GNU_define
18723 || macinfo_type == DW_MACRO_GNU_undef)
18724 {
18725 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18726 mac_ptr += bytes_read;
18727 }
18728 else
18729 {
18730 LONGEST str_offset;
18731
18732 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18733 mac_ptr += offset_size;
2e276125 18734
36586728 18735 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18736 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18737 || section_is_dwz)
36586728
TT
18738 {
18739 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18740
18741 body = read_indirect_string_from_dwz (dwz, str_offset);
18742 }
18743 else
18744 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18745 }
18746
18747 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18748 || macinfo_type == DW_MACRO_GNU_define_indirect
18749 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18750 if (! current_file)
757a13d0
JK
18751 {
18752 /* DWARF violation as no main source is present. */
18753 complaint (&symfile_complaints,
18754 _("debug info with no main source gives macro %s "
18755 "on line %d: %s"),
cf2c3c16
TT
18756 is_define ? _("definition") : _("undefinition"),
18757 line, body);
757a13d0
JK
18758 break;
18759 }
3e43a32a
MS
18760 if ((line == 0 && !at_commandline)
18761 || (line != 0 && at_commandline))
4d3c2250 18762 complaint (&symfile_complaints,
757a13d0
JK
18763 _("debug info gives %s macro %s with %s line %d: %s"),
18764 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18765 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18766 line == 0 ? _("zero") : _("non-zero"), line, body);
18767
cf2c3c16 18768 if (is_define)
757a13d0 18769 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18770 else
18771 {
18772 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18773 || macinfo_type == DW_MACRO_GNU_undef_indirect
18774 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18775 macro_undef (current_file, line, body);
18776 }
2e276125
JB
18777 }
18778 break;
18779
cf2c3c16 18780 case DW_MACRO_GNU_start_file:
2e276125 18781 {
891d2f0b 18782 unsigned int bytes_read;
2e276125
JB
18783 int line, file;
18784
18785 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18786 mac_ptr += bytes_read;
18787 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18788 mac_ptr += bytes_read;
18789
3e43a32a
MS
18790 if ((line == 0 && !at_commandline)
18791 || (line != 0 && at_commandline))
757a13d0
JK
18792 complaint (&symfile_complaints,
18793 _("debug info gives source %d included "
18794 "from %s at %s line %d"),
18795 file, at_commandline ? _("command-line") : _("file"),
18796 line == 0 ? _("zero") : _("non-zero"), line);
18797
18798 if (at_commandline)
18799 {
cf2c3c16
TT
18800 /* This DW_MACRO_GNU_start_file was executed in the
18801 pass one. */
757a13d0
JK
18802 at_commandline = 0;
18803 }
18804 else
18805 current_file = macro_start_file (file, line,
18806 current_file, comp_dir,
cf2c3c16 18807 lh, objfile);
2e276125
JB
18808 }
18809 break;
18810
cf2c3c16 18811 case DW_MACRO_GNU_end_file:
2e276125 18812 if (! current_file)
4d3c2250 18813 complaint (&symfile_complaints,
3e43a32a
MS
18814 _("macro debug info has an unmatched "
18815 "`close_file' directive"));
2e276125
JB
18816 else
18817 {
18818 current_file = current_file->included_by;
18819 if (! current_file)
18820 {
cf2c3c16 18821 enum dwarf_macro_record_type next_type;
2e276125
JB
18822
18823 /* GCC circa March 2002 doesn't produce the zero
18824 type byte marking the end of the compilation
18825 unit. Complain if it's not there, but exit no
18826 matter what. */
18827
18828 /* Do we at least have room for a macinfo type byte? */
18829 if (mac_ptr >= mac_end)
18830 {
f664829e 18831 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18832 return;
18833 }
18834
18835 /* We don't increment mac_ptr here, so this is just
18836 a look-ahead. */
18837 next_type = read_1_byte (abfd, mac_ptr);
18838 if (next_type != 0)
4d3c2250 18839 complaint (&symfile_complaints,
3e43a32a
MS
18840 _("no terminating 0-type entry for "
18841 "macros in `.debug_macinfo' section"));
2e276125
JB
18842
18843 return;
18844 }
18845 }
18846 break;
18847
cf2c3c16 18848 case DW_MACRO_GNU_transparent_include:
36586728 18849 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18850 {
18851 LONGEST offset;
8fc3fc34 18852 void **slot;
a036ba48
TT
18853 bfd *include_bfd = abfd;
18854 struct dwarf2_section_info *include_section = section;
18855 struct dwarf2_section_info alt_section;
d521ce57 18856 const gdb_byte *include_mac_end = mac_end;
a036ba48 18857 int is_dwz = section_is_dwz;
d521ce57 18858 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
18859
18860 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18861 mac_ptr += offset_size;
18862
a036ba48
TT
18863 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18864 {
18865 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18866
18867 dwarf2_read_section (dwarf2_per_objfile->objfile,
18868 &dwz->macro);
18869
18870 include_bfd = dwz->macro.asection->owner;
18871 include_section = &dwz->macro;
18872 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18873 is_dwz = 1;
18874 }
18875
18876 new_mac_ptr = include_section->buffer + offset;
18877 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18878
8fc3fc34
TT
18879 if (*slot != NULL)
18880 {
18881 /* This has actually happened; see
18882 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18883 complaint (&symfile_complaints,
18884 _("recursive DW_MACRO_GNU_transparent_include in "
18885 ".debug_macro section"));
18886 }
18887 else
18888 {
d521ce57 18889 *slot = (void *) new_mac_ptr;
36586728 18890
a036ba48 18891 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18892 include_mac_end, current_file,
8fc3fc34 18893 lh, comp_dir,
36586728 18894 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18895 offset_size, objfile, include_hash);
18896
d521ce57 18897 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 18898 }
cf2c3c16
TT
18899 }
18900 break;
18901
2e276125 18902 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18903 if (!section_is_gnu)
18904 {
18905 unsigned int bytes_read;
18906 int constant;
2e276125 18907
cf2c3c16
TT
18908 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18909 mac_ptr += bytes_read;
18910 read_direct_string (abfd, mac_ptr, &bytes_read);
18911 mac_ptr += bytes_read;
2e276125 18912
cf2c3c16
TT
18913 /* We don't recognize any vendor extensions. */
18914 break;
18915 }
18916 /* FALLTHROUGH */
18917
18918 default:
18919 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18920 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18921 section);
18922 if (mac_ptr == NULL)
18923 return;
18924 break;
2e276125 18925 }
757a13d0 18926 } while (macinfo_type != 0);
2e276125 18927}
8e19ed76 18928
cf2c3c16 18929static void
09262596 18930dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18931 const char *comp_dir, int section_is_gnu)
cf2c3c16 18932{
bb5ed363 18933 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18934 struct line_header *lh = cu->line_header;
18935 bfd *abfd;
d521ce57 18936 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
18937 struct macro_source_file *current_file = 0;
18938 enum dwarf_macro_record_type macinfo_type;
18939 unsigned int offset_size = cu->header.offset_size;
d521ce57 18940 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18941 struct cleanup *cleanup;
18942 htab_t include_hash;
18943 void **slot;
09262596
DE
18944 struct dwarf2_section_info *section;
18945 const char *section_name;
18946
18947 if (cu->dwo_unit != NULL)
18948 {
18949 if (section_is_gnu)
18950 {
18951 section = &cu->dwo_unit->dwo_file->sections.macro;
18952 section_name = ".debug_macro.dwo";
18953 }
18954 else
18955 {
18956 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18957 section_name = ".debug_macinfo.dwo";
18958 }
18959 }
18960 else
18961 {
18962 if (section_is_gnu)
18963 {
18964 section = &dwarf2_per_objfile->macro;
18965 section_name = ".debug_macro";
18966 }
18967 else
18968 {
18969 section = &dwarf2_per_objfile->macinfo;
18970 section_name = ".debug_macinfo";
18971 }
18972 }
cf2c3c16 18973
bb5ed363 18974 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18975 if (section->buffer == NULL)
18976 {
fceca515 18977 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18978 return;
18979 }
09262596 18980 abfd = section->asection->owner;
cf2c3c16
TT
18981
18982 /* First pass: Find the name of the base filename.
18983 This filename is needed in order to process all macros whose definition
18984 (or undefinition) comes from the command line. These macros are defined
18985 before the first DW_MACINFO_start_file entry, and yet still need to be
18986 associated to the base file.
18987
18988 To determine the base file name, we scan the macro definitions until we
18989 reach the first DW_MACINFO_start_file entry. We then initialize
18990 CURRENT_FILE accordingly so that any macro definition found before the
18991 first DW_MACINFO_start_file can still be associated to the base file. */
18992
18993 mac_ptr = section->buffer + offset;
18994 mac_end = section->buffer + section->size;
18995
18996 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18997 &offset_size, section_is_gnu);
18998 if (mac_ptr == NULL)
18999 {
19000 /* We already issued a complaint. */
19001 return;
19002 }
19003
19004 do
19005 {
19006 /* Do we at least have room for a macinfo type byte? */
19007 if (mac_ptr >= mac_end)
19008 {
19009 /* Complaint is printed during the second pass as GDB will probably
19010 stop the first pass earlier upon finding
19011 DW_MACINFO_start_file. */
19012 break;
19013 }
19014
19015 macinfo_type = read_1_byte (abfd, mac_ptr);
19016 mac_ptr++;
19017
19018 /* Note that we rely on the fact that the corresponding GNU and
19019 DWARF constants are the same. */
19020 switch (macinfo_type)
19021 {
19022 /* A zero macinfo type indicates the end of the macro
19023 information. */
19024 case 0:
19025 break;
19026
19027 case DW_MACRO_GNU_define:
19028 case DW_MACRO_GNU_undef:
19029 /* Only skip the data by MAC_PTR. */
19030 {
19031 unsigned int bytes_read;
19032
19033 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19034 mac_ptr += bytes_read;
19035 read_direct_string (abfd, mac_ptr, &bytes_read);
19036 mac_ptr += bytes_read;
19037 }
19038 break;
19039
19040 case DW_MACRO_GNU_start_file:
19041 {
19042 unsigned int bytes_read;
19043 int line, file;
19044
19045 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19046 mac_ptr += bytes_read;
19047 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19048 mac_ptr += bytes_read;
19049
19050 current_file = macro_start_file (file, line, current_file,
bb5ed363 19051 comp_dir, lh, objfile);
cf2c3c16
TT
19052 }
19053 break;
19054
19055 case DW_MACRO_GNU_end_file:
19056 /* No data to skip by MAC_PTR. */
19057 break;
19058
19059 case DW_MACRO_GNU_define_indirect:
19060 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19061 case DW_MACRO_GNU_define_indirect_alt:
19062 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19063 {
19064 unsigned int bytes_read;
19065
19066 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19067 mac_ptr += bytes_read;
19068 mac_ptr += offset_size;
19069 }
19070 break;
19071
19072 case DW_MACRO_GNU_transparent_include:
f7a35f02 19073 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19074 /* Note that, according to the spec, a transparent include
19075 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19076 skip this opcode. */
19077 mac_ptr += offset_size;
19078 break;
19079
19080 case DW_MACINFO_vendor_ext:
19081 /* Only skip the data by MAC_PTR. */
19082 if (!section_is_gnu)
19083 {
19084 unsigned int bytes_read;
19085
19086 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19087 mac_ptr += bytes_read;
19088 read_direct_string (abfd, mac_ptr, &bytes_read);
19089 mac_ptr += bytes_read;
19090 }
19091 /* FALLTHROUGH */
19092
19093 default:
19094 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19095 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19096 section);
19097 if (mac_ptr == NULL)
19098 return;
19099 break;
19100 }
19101 } while (macinfo_type != 0 && current_file == NULL);
19102
19103 /* Second pass: Process all entries.
19104
19105 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19106 command-line macro definitions/undefinitions. This flag is unset when we
19107 reach the first DW_MACINFO_start_file entry. */
19108
8fc3fc34
TT
19109 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19110 NULL, xcalloc, xfree);
19111 cleanup = make_cleanup_htab_delete (include_hash);
19112 mac_ptr = section->buffer + offset;
19113 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19114 *slot = (void *) mac_ptr;
8fc3fc34 19115 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19116 current_file, lh, comp_dir, section,
19117 section_is_gnu, 0,
8fc3fc34
TT
19118 offset_size, objfile, include_hash);
19119 do_cleanups (cleanup);
cf2c3c16
TT
19120}
19121
8e19ed76 19122/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19123 if so return true else false. */
380bca97 19124
8e19ed76
PS
19125static int
19126attr_form_is_block (struct attribute *attr)
19127{
19128 return (attr == NULL ? 0 :
19129 attr->form == DW_FORM_block1
19130 || attr->form == DW_FORM_block2
19131 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19132 || attr->form == DW_FORM_block
19133 || attr->form == DW_FORM_exprloc);
8e19ed76 19134}
4c2df51b 19135
c6a0999f
JB
19136/* Return non-zero if ATTR's value is a section offset --- classes
19137 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19138 You may use DW_UNSND (attr) to retrieve such offsets.
19139
19140 Section 7.5.4, "Attribute Encodings", explains that no attribute
19141 may have a value that belongs to more than one of these classes; it
19142 would be ambiguous if we did, because we use the same forms for all
19143 of them. */
380bca97 19144
3690dd37
JB
19145static int
19146attr_form_is_section_offset (struct attribute *attr)
19147{
19148 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19149 || attr->form == DW_FORM_data8
19150 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19151}
19152
3690dd37
JB
19153/* Return non-zero if ATTR's value falls in the 'constant' class, or
19154 zero otherwise. When this function returns true, you can apply
19155 dwarf2_get_attr_constant_value to it.
19156
19157 However, note that for some attributes you must check
19158 attr_form_is_section_offset before using this test. DW_FORM_data4
19159 and DW_FORM_data8 are members of both the constant class, and of
19160 the classes that contain offsets into other debug sections
19161 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19162 that, if an attribute's can be either a constant or one of the
19163 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19164 taken as section offsets, not constants. */
380bca97 19165
3690dd37
JB
19166static int
19167attr_form_is_constant (struct attribute *attr)
19168{
19169 switch (attr->form)
19170 {
19171 case DW_FORM_sdata:
19172 case DW_FORM_udata:
19173 case DW_FORM_data1:
19174 case DW_FORM_data2:
19175 case DW_FORM_data4:
19176 case DW_FORM_data8:
19177 return 1;
19178 default:
19179 return 0;
19180 }
19181}
19182
3019eac3
DE
19183/* Return the .debug_loc section to use for CU.
19184 For DWO files use .debug_loc.dwo. */
19185
19186static struct dwarf2_section_info *
19187cu_debug_loc_section (struct dwarf2_cu *cu)
19188{
19189 if (cu->dwo_unit)
19190 return &cu->dwo_unit->dwo_file->sections.loc;
19191 return &dwarf2_per_objfile->loc;
19192}
19193
8cf6f0b1
TT
19194/* A helper function that fills in a dwarf2_loclist_baton. */
19195
19196static void
19197fill_in_loclist_baton (struct dwarf2_cu *cu,
19198 struct dwarf2_loclist_baton *baton,
19199 struct attribute *attr)
19200{
3019eac3
DE
19201 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19202
19203 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19204
19205 baton->per_cu = cu->per_cu;
19206 gdb_assert (baton->per_cu);
19207 /* We don't know how long the location list is, but make sure we
19208 don't run off the edge of the section. */
3019eac3
DE
19209 baton->size = section->size - DW_UNSND (attr);
19210 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19211 baton->base_address = cu->base_address;
f664829e 19212 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19213}
19214
4c2df51b
DJ
19215static void
19216dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
f1e6e072 19217 struct dwarf2_cu *cu, int is_block)
4c2df51b 19218{
bb5ed363 19219 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19220 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19221
3690dd37 19222 if (attr_form_is_section_offset (attr)
3019eac3 19223 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19224 the section. If so, fall through to the complaint in the
19225 other branch. */
3019eac3 19226 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19227 {
0d53c4c4 19228 struct dwarf2_loclist_baton *baton;
4c2df51b 19229
bb5ed363 19230 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19231 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19232
8cf6f0b1 19233 fill_in_loclist_baton (cu, baton, attr);
be391dca 19234
d00adf39 19235 if (cu->base_known == 0)
0d53c4c4 19236 complaint (&symfile_complaints,
3e43a32a
MS
19237 _("Location list used without "
19238 "specifying the CU base address."));
4c2df51b 19239
f1e6e072
TT
19240 SYMBOL_ACLASS_INDEX (sym) = (is_block
19241 ? dwarf2_loclist_block_index
19242 : dwarf2_loclist_index);
0d53c4c4
DJ
19243 SYMBOL_LOCATION_BATON (sym) = baton;
19244 }
19245 else
19246 {
19247 struct dwarf2_locexpr_baton *baton;
19248
bb5ed363 19249 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19250 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19251 baton->per_cu = cu->per_cu;
19252 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19253
19254 if (attr_form_is_block (attr))
19255 {
19256 /* Note that we're just copying the block's data pointer
19257 here, not the actual data. We're still pointing into the
6502dd73
DJ
19258 info_buffer for SYM's objfile; right now we never release
19259 that buffer, but when we do clean up properly this may
19260 need to change. */
0d53c4c4
DJ
19261 baton->size = DW_BLOCK (attr)->size;
19262 baton->data = DW_BLOCK (attr)->data;
19263 }
19264 else
19265 {
19266 dwarf2_invalid_attrib_class_complaint ("location description",
19267 SYMBOL_NATURAL_NAME (sym));
19268 baton->size = 0;
0d53c4c4 19269 }
6e70227d 19270
f1e6e072
TT
19271 SYMBOL_ACLASS_INDEX (sym) = (is_block
19272 ? dwarf2_locexpr_block_index
19273 : dwarf2_locexpr_index);
0d53c4c4
DJ
19274 SYMBOL_LOCATION_BATON (sym) = baton;
19275 }
4c2df51b 19276}
6502dd73 19277
9aa1f1e3
TT
19278/* Return the OBJFILE associated with the compilation unit CU. If CU
19279 came from a separate debuginfo file, then the master objfile is
19280 returned. */
ae0d2f24
UW
19281
19282struct objfile *
19283dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19284{
9291a0cd 19285 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19286
19287 /* Return the master objfile, so that we can report and look up the
19288 correct file containing this variable. */
19289 if (objfile->separate_debug_objfile_backlink)
19290 objfile = objfile->separate_debug_objfile_backlink;
19291
19292 return objfile;
19293}
19294
96408a79
SA
19295/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19296 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19297 CU_HEADERP first. */
19298
19299static const struct comp_unit_head *
19300per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19301 struct dwarf2_per_cu_data *per_cu)
19302{
d521ce57 19303 const gdb_byte *info_ptr;
96408a79
SA
19304
19305 if (per_cu->cu)
19306 return &per_cu->cu->header;
19307
8a0459fd 19308 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
19309
19310 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19311 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19312
19313 return cu_headerp;
19314}
19315
ae0d2f24
UW
19316/* Return the address size given in the compilation unit header for CU. */
19317
98714339 19318int
ae0d2f24
UW
19319dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19320{
96408a79
SA
19321 struct comp_unit_head cu_header_local;
19322 const struct comp_unit_head *cu_headerp;
c471e790 19323
96408a79
SA
19324 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19325
19326 return cu_headerp->addr_size;
ae0d2f24
UW
19327}
19328
9eae7c52
TT
19329/* Return the offset size given in the compilation unit header for CU. */
19330
19331int
19332dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19333{
96408a79
SA
19334 struct comp_unit_head cu_header_local;
19335 const struct comp_unit_head *cu_headerp;
9c6c53f7 19336
96408a79
SA
19337 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19338
19339 return cu_headerp->offset_size;
19340}
19341
19342/* See its dwarf2loc.h declaration. */
19343
19344int
19345dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19346{
19347 struct comp_unit_head cu_header_local;
19348 const struct comp_unit_head *cu_headerp;
19349
19350 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19351
19352 if (cu_headerp->version == 2)
19353 return cu_headerp->addr_size;
19354 else
19355 return cu_headerp->offset_size;
181cebd4
JK
19356}
19357
9aa1f1e3
TT
19358/* Return the text offset of the CU. The returned offset comes from
19359 this CU's objfile. If this objfile came from a separate debuginfo
19360 file, then the offset may be different from the corresponding
19361 offset in the parent objfile. */
19362
19363CORE_ADDR
19364dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19365{
bb3fa9d0 19366 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19367
19368 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19369}
19370
348e048f
DE
19371/* Locate the .debug_info compilation unit from CU's objfile which contains
19372 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19373
19374static struct dwarf2_per_cu_data *
b64f50a1 19375dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19376 unsigned int offset_in_dwz,
ae038cb0
DJ
19377 struct objfile *objfile)
19378{
19379 struct dwarf2_per_cu_data *this_cu;
19380 int low, high;
36586728 19381 const sect_offset *cu_off;
ae038cb0 19382
ae038cb0
DJ
19383 low = 0;
19384 high = dwarf2_per_objfile->n_comp_units - 1;
19385 while (high > low)
19386 {
36586728 19387 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19388 int mid = low + (high - low) / 2;
9a619af0 19389
36586728
TT
19390 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19391 cu_off = &mid_cu->offset;
19392 if (mid_cu->is_dwz > offset_in_dwz
19393 || (mid_cu->is_dwz == offset_in_dwz
19394 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19395 high = mid;
19396 else
19397 low = mid + 1;
19398 }
19399 gdb_assert (low == high);
36586728
TT
19400 this_cu = dwarf2_per_objfile->all_comp_units[low];
19401 cu_off = &this_cu->offset;
19402 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19403 {
36586728 19404 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19405 error (_("Dwarf Error: could not find partial DIE containing "
19406 "offset 0x%lx [in module %s]"),
b64f50a1 19407 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19408
b64f50a1
JK
19409 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19410 <= offset.sect_off);
ae038cb0
DJ
19411 return dwarf2_per_objfile->all_comp_units[low-1];
19412 }
19413 else
19414 {
19415 this_cu = dwarf2_per_objfile->all_comp_units[low];
19416 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19417 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19418 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19419 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19420 return this_cu;
19421 }
19422}
19423
23745b47 19424/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19425
9816fde3 19426static void
23745b47 19427init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19428{
9816fde3 19429 memset (cu, 0, sizeof (*cu));
23745b47
DE
19430 per_cu->cu = cu;
19431 cu->per_cu = per_cu;
19432 cu->objfile = per_cu->objfile;
93311388 19433 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19434}
19435
19436/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19437
19438static void
95554aad
TT
19439prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19440 enum language pretend_language)
9816fde3
JK
19441{
19442 struct attribute *attr;
19443
19444 /* Set the language we're debugging. */
19445 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19446 if (attr)
19447 set_cu_language (DW_UNSND (attr), cu);
19448 else
9cded63f 19449 {
95554aad 19450 cu->language = pretend_language;
9cded63f
TT
19451 cu->language_defn = language_def (cu->language);
19452 }
dee91e82
DE
19453
19454 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19455 if (attr)
19456 cu->producer = DW_STRING (attr);
93311388
DE
19457}
19458
ae038cb0
DJ
19459/* Release one cached compilation unit, CU. We unlink it from the tree
19460 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19461 the caller is responsible for that.
19462 NOTE: DATA is a void * because this function is also used as a
19463 cleanup routine. */
ae038cb0
DJ
19464
19465static void
68dc6402 19466free_heap_comp_unit (void *data)
ae038cb0
DJ
19467{
19468 struct dwarf2_cu *cu = data;
19469
23745b47
DE
19470 gdb_assert (cu->per_cu != NULL);
19471 cu->per_cu->cu = NULL;
ae038cb0
DJ
19472 cu->per_cu = NULL;
19473
19474 obstack_free (&cu->comp_unit_obstack, NULL);
19475
19476 xfree (cu);
19477}
19478
72bf9492 19479/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19480 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19481 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19482
19483static void
19484free_stack_comp_unit (void *data)
19485{
19486 struct dwarf2_cu *cu = data;
19487
23745b47
DE
19488 gdb_assert (cu->per_cu != NULL);
19489 cu->per_cu->cu = NULL;
19490 cu->per_cu = NULL;
19491
72bf9492
DJ
19492 obstack_free (&cu->comp_unit_obstack, NULL);
19493 cu->partial_dies = NULL;
ae038cb0
DJ
19494}
19495
19496/* Free all cached compilation units. */
19497
19498static void
19499free_cached_comp_units (void *data)
19500{
19501 struct dwarf2_per_cu_data *per_cu, **last_chain;
19502
19503 per_cu = dwarf2_per_objfile->read_in_chain;
19504 last_chain = &dwarf2_per_objfile->read_in_chain;
19505 while (per_cu != NULL)
19506 {
19507 struct dwarf2_per_cu_data *next_cu;
19508
19509 next_cu = per_cu->cu->read_in_chain;
19510
68dc6402 19511 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19512 *last_chain = next_cu;
19513
19514 per_cu = next_cu;
19515 }
19516}
19517
19518/* Increase the age counter on each cached compilation unit, and free
19519 any that are too old. */
19520
19521static void
19522age_cached_comp_units (void)
19523{
19524 struct dwarf2_per_cu_data *per_cu, **last_chain;
19525
19526 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19527 per_cu = dwarf2_per_objfile->read_in_chain;
19528 while (per_cu != NULL)
19529 {
19530 per_cu->cu->last_used ++;
19531 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19532 dwarf2_mark (per_cu->cu);
19533 per_cu = per_cu->cu->read_in_chain;
19534 }
19535
19536 per_cu = dwarf2_per_objfile->read_in_chain;
19537 last_chain = &dwarf2_per_objfile->read_in_chain;
19538 while (per_cu != NULL)
19539 {
19540 struct dwarf2_per_cu_data *next_cu;
19541
19542 next_cu = per_cu->cu->read_in_chain;
19543
19544 if (!per_cu->cu->mark)
19545 {
68dc6402 19546 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19547 *last_chain = next_cu;
19548 }
19549 else
19550 last_chain = &per_cu->cu->read_in_chain;
19551
19552 per_cu = next_cu;
19553 }
19554}
19555
19556/* Remove a single compilation unit from the cache. */
19557
19558static void
dee91e82 19559free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19560{
19561 struct dwarf2_per_cu_data *per_cu, **last_chain;
19562
19563 per_cu = dwarf2_per_objfile->read_in_chain;
19564 last_chain = &dwarf2_per_objfile->read_in_chain;
19565 while (per_cu != NULL)
19566 {
19567 struct dwarf2_per_cu_data *next_cu;
19568
19569 next_cu = per_cu->cu->read_in_chain;
19570
dee91e82 19571 if (per_cu == target_per_cu)
ae038cb0 19572 {
68dc6402 19573 free_heap_comp_unit (per_cu->cu);
dee91e82 19574 per_cu->cu = NULL;
ae038cb0
DJ
19575 *last_chain = next_cu;
19576 break;
19577 }
19578 else
19579 last_chain = &per_cu->cu->read_in_chain;
19580
19581 per_cu = next_cu;
19582 }
19583}
19584
fe3e1990
DJ
19585/* Release all extra memory associated with OBJFILE. */
19586
19587void
19588dwarf2_free_objfile (struct objfile *objfile)
19589{
19590 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19591
19592 if (dwarf2_per_objfile == NULL)
19593 return;
19594
19595 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19596 free_cached_comp_units (NULL);
19597
7b9f3c50
DE
19598 if (dwarf2_per_objfile->quick_file_names_table)
19599 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19600
fe3e1990
DJ
19601 /* Everything else should be on the objfile obstack. */
19602}
19603
dee91e82
DE
19604/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19605 We store these in a hash table separate from the DIEs, and preserve them
19606 when the DIEs are flushed out of cache.
19607
19608 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 19609 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
19610 or the type may come from a DWO file. Furthermore, while it's more logical
19611 to use per_cu->section+offset, with Fission the section with the data is in
19612 the DWO file but we don't know that section at the point we need it.
19613 We have to use something in dwarf2_per_cu_data (or the pointer to it)
19614 because we can enter the lookup routine, get_die_type_at_offset, from
19615 outside this file, and thus won't necessarily have PER_CU->cu.
19616 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 19617
dee91e82 19618struct dwarf2_per_cu_offset_and_type
1c379e20 19619{
dee91e82 19620 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19621 sect_offset offset;
1c379e20
DJ
19622 struct type *type;
19623};
19624
dee91e82 19625/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19626
19627static hashval_t
dee91e82 19628per_cu_offset_and_type_hash (const void *item)
1c379e20 19629{
dee91e82 19630 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19631
dee91e82 19632 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19633}
19634
dee91e82 19635/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19636
19637static int
dee91e82 19638per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19639{
dee91e82
DE
19640 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19641 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19642
dee91e82
DE
19643 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19644 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19645}
19646
19647/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19648 table if necessary. For convenience, return TYPE.
19649
19650 The DIEs reading must have careful ordering to:
19651 * Not cause infite loops trying to read in DIEs as a prerequisite for
19652 reading current DIE.
19653 * Not trying to dereference contents of still incompletely read in types
19654 while reading in other DIEs.
19655 * Enable referencing still incompletely read in types just by a pointer to
19656 the type without accessing its fields.
19657
19658 Therefore caller should follow these rules:
19659 * Try to fetch any prerequisite types we may need to build this DIE type
19660 before building the type and calling set_die_type.
e71ec853 19661 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19662 possible before fetching more types to complete the current type.
19663 * Make the type as complete as possible before fetching more types. */
1c379e20 19664
f792889a 19665static struct type *
1c379e20
DJ
19666set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19667{
dee91e82 19668 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19669 struct objfile *objfile = cu->objfile;
1c379e20 19670
b4ba55a1
JB
19671 /* For Ada types, make sure that the gnat-specific data is always
19672 initialized (if not already set). There are a few types where
19673 we should not be doing so, because the type-specific area is
19674 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19675 where the type-specific area is used to store the floatformat).
19676 But this is not a problem, because the gnat-specific information
19677 is actually not needed for these types. */
19678 if (need_gnat_info (cu)
19679 && TYPE_CODE (type) != TYPE_CODE_FUNC
19680 && TYPE_CODE (type) != TYPE_CODE_FLT
19681 && !HAVE_GNAT_AUX_INFO (type))
19682 INIT_GNAT_SPECIFIC (type);
19683
dee91e82 19684 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19685 {
dee91e82
DE
19686 dwarf2_per_objfile->die_type_hash =
19687 htab_create_alloc_ex (127,
19688 per_cu_offset_and_type_hash,
19689 per_cu_offset_and_type_eq,
19690 NULL,
19691 &objfile->objfile_obstack,
19692 hashtab_obstack_allocate,
19693 dummy_obstack_deallocate);
f792889a 19694 }
1c379e20 19695
dee91e82 19696 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19697 ofs.offset = die->offset;
19698 ofs.type = type;
dee91e82
DE
19699 slot = (struct dwarf2_per_cu_offset_and_type **)
19700 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19701 if (*slot)
19702 complaint (&symfile_complaints,
19703 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19704 die->offset.sect_off);
673bfd45 19705 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19706 **slot = ofs;
f792889a 19707 return type;
1c379e20
DJ
19708}
19709
02142a6c
DE
19710/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
19711 or return NULL if the die does not have a saved type. */
1c379e20
DJ
19712
19713static struct type *
b64f50a1 19714get_die_type_at_offset (sect_offset offset,
673bfd45 19715 struct dwarf2_per_cu_data *per_cu)
1c379e20 19716{
dee91e82 19717 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19718
dee91e82 19719 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19720 return NULL;
1c379e20 19721
dee91e82 19722 ofs.per_cu = per_cu;
673bfd45 19723 ofs.offset = offset;
dee91e82 19724 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19725 if (slot)
19726 return slot->type;
19727 else
19728 return NULL;
19729}
19730
02142a6c 19731/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
19732 or return NULL if DIE does not have a saved type. */
19733
19734static struct type *
19735get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19736{
19737 return get_die_type_at_offset (die->offset, cu->per_cu);
19738}
19739
10b3939b
DJ
19740/* Add a dependence relationship from CU to REF_PER_CU. */
19741
19742static void
19743dwarf2_add_dependence (struct dwarf2_cu *cu,
19744 struct dwarf2_per_cu_data *ref_per_cu)
19745{
19746 void **slot;
19747
19748 if (cu->dependencies == NULL)
19749 cu->dependencies
19750 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19751 NULL, &cu->comp_unit_obstack,
19752 hashtab_obstack_allocate,
19753 dummy_obstack_deallocate);
19754
19755 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19756 if (*slot == NULL)
19757 *slot = ref_per_cu;
19758}
1c379e20 19759
f504f079
DE
19760/* Subroutine of dwarf2_mark to pass to htab_traverse.
19761 Set the mark field in every compilation unit in the
ae038cb0
DJ
19762 cache that we must keep because we are keeping CU. */
19763
10b3939b
DJ
19764static int
19765dwarf2_mark_helper (void **slot, void *data)
19766{
19767 struct dwarf2_per_cu_data *per_cu;
19768
19769 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19770
19771 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19772 reading of the chain. As such dependencies remain valid it is not much
19773 useful to track and undo them during QUIT cleanups. */
19774 if (per_cu->cu == NULL)
19775 return 1;
19776
10b3939b
DJ
19777 if (per_cu->cu->mark)
19778 return 1;
19779 per_cu->cu->mark = 1;
19780
19781 if (per_cu->cu->dependencies != NULL)
19782 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19783
19784 return 1;
19785}
19786
f504f079
DE
19787/* Set the mark field in CU and in every other compilation unit in the
19788 cache that we must keep because we are keeping CU. */
19789
ae038cb0
DJ
19790static void
19791dwarf2_mark (struct dwarf2_cu *cu)
19792{
19793 if (cu->mark)
19794 return;
19795 cu->mark = 1;
10b3939b
DJ
19796 if (cu->dependencies != NULL)
19797 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19798}
19799
19800static void
19801dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19802{
19803 while (per_cu)
19804 {
19805 per_cu->cu->mark = 0;
19806 per_cu = per_cu->cu->read_in_chain;
19807 }
72bf9492
DJ
19808}
19809
72bf9492
DJ
19810/* Trivial hash function for partial_die_info: the hash value of a DIE
19811 is its offset in .debug_info for this objfile. */
19812
19813static hashval_t
19814partial_die_hash (const void *item)
19815{
19816 const struct partial_die_info *part_die = item;
9a619af0 19817
b64f50a1 19818 return part_die->offset.sect_off;
72bf9492
DJ
19819}
19820
19821/* Trivial comparison function for partial_die_info structures: two DIEs
19822 are equal if they have the same offset. */
19823
19824static int
19825partial_die_eq (const void *item_lhs, const void *item_rhs)
19826{
19827 const struct partial_die_info *part_die_lhs = item_lhs;
19828 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19829
b64f50a1 19830 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19831}
19832
ae038cb0
DJ
19833static struct cmd_list_element *set_dwarf2_cmdlist;
19834static struct cmd_list_element *show_dwarf2_cmdlist;
19835
19836static void
19837set_dwarf2_cmd (char *args, int from_tty)
19838{
19839 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19840}
19841
19842static void
19843show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19844{
ae038cb0
DJ
19845 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19846}
19847
4bf44c1c 19848/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19849
19850static void
c1bd65d0 19851dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19852{
19853 struct dwarf2_per_objfile *data = d;
8b70b953 19854 int ix;
8b70b953 19855
95554aad
TT
19856 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19857 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19858 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19859
19860 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19861 VEC_free (dwarf2_per_cu_ptr,
19862 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19863
8b70b953 19864 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19865
19866 if (data->dwo_files)
19867 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
19868 if (data->dwp_file)
19869 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
19870
19871 if (data->dwz_file && data->dwz_file->dwz_bfd)
19872 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19873}
19874
19875\f
ae2de4f8 19876/* The "save gdb-index" command. */
9291a0cd
TT
19877
19878/* The contents of the hash table we create when building the string
19879 table. */
19880struct strtab_entry
19881{
19882 offset_type offset;
19883 const char *str;
19884};
19885
559a7a62
JK
19886/* Hash function for a strtab_entry.
19887
19888 Function is used only during write_hash_table so no index format backward
19889 compatibility is needed. */
b89be57b 19890
9291a0cd
TT
19891static hashval_t
19892hash_strtab_entry (const void *e)
19893{
19894 const struct strtab_entry *entry = e;
559a7a62 19895 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19896}
19897
19898/* Equality function for a strtab_entry. */
b89be57b 19899
9291a0cd
TT
19900static int
19901eq_strtab_entry (const void *a, const void *b)
19902{
19903 const struct strtab_entry *ea = a;
19904 const struct strtab_entry *eb = b;
19905 return !strcmp (ea->str, eb->str);
19906}
19907
19908/* Create a strtab_entry hash table. */
b89be57b 19909
9291a0cd
TT
19910static htab_t
19911create_strtab (void)
19912{
19913 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19914 xfree, xcalloc, xfree);
19915}
19916
19917/* Add a string to the constant pool. Return the string's offset in
19918 host order. */
b89be57b 19919
9291a0cd
TT
19920static offset_type
19921add_string (htab_t table, struct obstack *cpool, const char *str)
19922{
19923 void **slot;
19924 struct strtab_entry entry;
19925 struct strtab_entry *result;
19926
19927 entry.str = str;
19928 slot = htab_find_slot (table, &entry, INSERT);
19929 if (*slot)
19930 result = *slot;
19931 else
19932 {
19933 result = XNEW (struct strtab_entry);
19934 result->offset = obstack_object_size (cpool);
19935 result->str = str;
19936 obstack_grow_str0 (cpool, str);
19937 *slot = result;
19938 }
19939 return result->offset;
19940}
19941
19942/* An entry in the symbol table. */
19943struct symtab_index_entry
19944{
19945 /* The name of the symbol. */
19946 const char *name;
19947 /* The offset of the name in the constant pool. */
19948 offset_type index_offset;
19949 /* A sorted vector of the indices of all the CUs that hold an object
19950 of this name. */
19951 VEC (offset_type) *cu_indices;
19952};
19953
19954/* The symbol table. This is a power-of-2-sized hash table. */
19955struct mapped_symtab
19956{
19957 offset_type n_elements;
19958 offset_type size;
19959 struct symtab_index_entry **data;
19960};
19961
19962/* Hash function for a symtab_index_entry. */
b89be57b 19963
9291a0cd
TT
19964static hashval_t
19965hash_symtab_entry (const void *e)
19966{
19967 const struct symtab_index_entry *entry = e;
19968 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19969 sizeof (offset_type) * VEC_length (offset_type,
19970 entry->cu_indices),
19971 0);
19972}
19973
19974/* Equality function for a symtab_index_entry. */
b89be57b 19975
9291a0cd
TT
19976static int
19977eq_symtab_entry (const void *a, const void *b)
19978{
19979 const struct symtab_index_entry *ea = a;
19980 const struct symtab_index_entry *eb = b;
19981 int len = VEC_length (offset_type, ea->cu_indices);
19982 if (len != VEC_length (offset_type, eb->cu_indices))
19983 return 0;
19984 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19985 VEC_address (offset_type, eb->cu_indices),
19986 sizeof (offset_type) * len);
19987}
19988
19989/* Destroy a symtab_index_entry. */
b89be57b 19990
9291a0cd
TT
19991static void
19992delete_symtab_entry (void *p)
19993{
19994 struct symtab_index_entry *entry = p;
19995 VEC_free (offset_type, entry->cu_indices);
19996 xfree (entry);
19997}
19998
19999/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20000
9291a0cd 20001static htab_t
3876f04e 20002create_symbol_hash_table (void)
9291a0cd
TT
20003{
20004 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20005 delete_symtab_entry, xcalloc, xfree);
20006}
20007
20008/* Create a new mapped symtab object. */
b89be57b 20009
9291a0cd
TT
20010static struct mapped_symtab *
20011create_mapped_symtab (void)
20012{
20013 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20014 symtab->n_elements = 0;
20015 symtab->size = 1024;
20016 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20017 return symtab;
20018}
20019
20020/* Destroy a mapped_symtab. */
b89be57b 20021
9291a0cd
TT
20022static void
20023cleanup_mapped_symtab (void *p)
20024{
20025 struct mapped_symtab *symtab = p;
20026 /* The contents of the array are freed when the other hash table is
20027 destroyed. */
20028 xfree (symtab->data);
20029 xfree (symtab);
20030}
20031
20032/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20033 the slot.
20034
20035 Function is used only during write_hash_table so no index format backward
20036 compatibility is needed. */
b89be57b 20037
9291a0cd
TT
20038static struct symtab_index_entry **
20039find_slot (struct mapped_symtab *symtab, const char *name)
20040{
559a7a62 20041 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20042
20043 index = hash & (symtab->size - 1);
20044 step = ((hash * 17) & (symtab->size - 1)) | 1;
20045
20046 for (;;)
20047 {
20048 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20049 return &symtab->data[index];
20050 index = (index + step) & (symtab->size - 1);
20051 }
20052}
20053
20054/* Expand SYMTAB's hash table. */
b89be57b 20055
9291a0cd
TT
20056static void
20057hash_expand (struct mapped_symtab *symtab)
20058{
20059 offset_type old_size = symtab->size;
20060 offset_type i;
20061 struct symtab_index_entry **old_entries = symtab->data;
20062
20063 symtab->size *= 2;
20064 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20065
20066 for (i = 0; i < old_size; ++i)
20067 {
20068 if (old_entries[i])
20069 {
20070 struct symtab_index_entry **slot = find_slot (symtab,
20071 old_entries[i]->name);
20072 *slot = old_entries[i];
20073 }
20074 }
20075
20076 xfree (old_entries);
20077}
20078
156942c7
DE
20079/* Add an entry to SYMTAB. NAME is the name of the symbol.
20080 CU_INDEX is the index of the CU in which the symbol appears.
20081 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20082
9291a0cd
TT
20083static void
20084add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20085 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20086 offset_type cu_index)
20087{
20088 struct symtab_index_entry **slot;
156942c7 20089 offset_type cu_index_and_attrs;
9291a0cd
TT
20090
20091 ++symtab->n_elements;
20092 if (4 * symtab->n_elements / 3 >= symtab->size)
20093 hash_expand (symtab);
20094
20095 slot = find_slot (symtab, name);
20096 if (!*slot)
20097 {
20098 *slot = XNEW (struct symtab_index_entry);
20099 (*slot)->name = name;
156942c7 20100 /* index_offset is set later. */
9291a0cd
TT
20101 (*slot)->cu_indices = NULL;
20102 }
156942c7
DE
20103
20104 cu_index_and_attrs = 0;
20105 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20106 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20107 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20108
20109 /* We don't want to record an index value twice as we want to avoid the
20110 duplication.
20111 We process all global symbols and then all static symbols
20112 (which would allow us to avoid the duplication by only having to check
20113 the last entry pushed), but a symbol could have multiple kinds in one CU.
20114 To keep things simple we don't worry about the duplication here and
20115 sort and uniqufy the list after we've processed all symbols. */
20116 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20117}
20118
20119/* qsort helper routine for uniquify_cu_indices. */
20120
20121static int
20122offset_type_compare (const void *ap, const void *bp)
20123{
20124 offset_type a = *(offset_type *) ap;
20125 offset_type b = *(offset_type *) bp;
20126
20127 return (a > b) - (b > a);
20128}
20129
20130/* Sort and remove duplicates of all symbols' cu_indices lists. */
20131
20132static void
20133uniquify_cu_indices (struct mapped_symtab *symtab)
20134{
20135 int i;
20136
20137 for (i = 0; i < symtab->size; ++i)
20138 {
20139 struct symtab_index_entry *entry = symtab->data[i];
20140
20141 if (entry
20142 && entry->cu_indices != NULL)
20143 {
20144 unsigned int next_to_insert, next_to_check;
20145 offset_type last_value;
20146
20147 qsort (VEC_address (offset_type, entry->cu_indices),
20148 VEC_length (offset_type, entry->cu_indices),
20149 sizeof (offset_type), offset_type_compare);
20150
20151 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20152 next_to_insert = 1;
20153 for (next_to_check = 1;
20154 next_to_check < VEC_length (offset_type, entry->cu_indices);
20155 ++next_to_check)
20156 {
20157 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20158 != last_value)
20159 {
20160 last_value = VEC_index (offset_type, entry->cu_indices,
20161 next_to_check);
20162 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20163 last_value);
20164 ++next_to_insert;
20165 }
20166 }
20167 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20168 }
20169 }
9291a0cd
TT
20170}
20171
20172/* Add a vector of indices to the constant pool. */
b89be57b 20173
9291a0cd 20174static offset_type
3876f04e 20175add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20176 struct symtab_index_entry *entry)
20177{
20178 void **slot;
20179
3876f04e 20180 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20181 if (!*slot)
20182 {
20183 offset_type len = VEC_length (offset_type, entry->cu_indices);
20184 offset_type val = MAYBE_SWAP (len);
20185 offset_type iter;
20186 int i;
20187
20188 *slot = entry;
20189 entry->index_offset = obstack_object_size (cpool);
20190
20191 obstack_grow (cpool, &val, sizeof (val));
20192 for (i = 0;
20193 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20194 ++i)
20195 {
20196 val = MAYBE_SWAP (iter);
20197 obstack_grow (cpool, &val, sizeof (val));
20198 }
20199 }
20200 else
20201 {
20202 struct symtab_index_entry *old_entry = *slot;
20203 entry->index_offset = old_entry->index_offset;
20204 entry = old_entry;
20205 }
20206 return entry->index_offset;
20207}
20208
20209/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20210 constant pool entries going into the obstack CPOOL. */
b89be57b 20211
9291a0cd
TT
20212static void
20213write_hash_table (struct mapped_symtab *symtab,
20214 struct obstack *output, struct obstack *cpool)
20215{
20216 offset_type i;
3876f04e 20217 htab_t symbol_hash_table;
9291a0cd
TT
20218 htab_t str_table;
20219
3876f04e 20220 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20221 str_table = create_strtab ();
3876f04e 20222
9291a0cd
TT
20223 /* We add all the index vectors to the constant pool first, to
20224 ensure alignment is ok. */
20225 for (i = 0; i < symtab->size; ++i)
20226 {
20227 if (symtab->data[i])
3876f04e 20228 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20229 }
20230
20231 /* Now write out the hash table. */
20232 for (i = 0; i < symtab->size; ++i)
20233 {
20234 offset_type str_off, vec_off;
20235
20236 if (symtab->data[i])
20237 {
20238 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20239 vec_off = symtab->data[i]->index_offset;
20240 }
20241 else
20242 {
20243 /* While 0 is a valid constant pool index, it is not valid
20244 to have 0 for both offsets. */
20245 str_off = 0;
20246 vec_off = 0;
20247 }
20248
20249 str_off = MAYBE_SWAP (str_off);
20250 vec_off = MAYBE_SWAP (vec_off);
20251
20252 obstack_grow (output, &str_off, sizeof (str_off));
20253 obstack_grow (output, &vec_off, sizeof (vec_off));
20254 }
20255
20256 htab_delete (str_table);
3876f04e 20257 htab_delete (symbol_hash_table);
9291a0cd
TT
20258}
20259
0a5429f6
DE
20260/* Struct to map psymtab to CU index in the index file. */
20261struct psymtab_cu_index_map
20262{
20263 struct partial_symtab *psymtab;
20264 unsigned int cu_index;
20265};
20266
20267static hashval_t
20268hash_psymtab_cu_index (const void *item)
20269{
20270 const struct psymtab_cu_index_map *map = item;
20271
20272 return htab_hash_pointer (map->psymtab);
20273}
20274
20275static int
20276eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20277{
20278 const struct psymtab_cu_index_map *lhs = item_lhs;
20279 const struct psymtab_cu_index_map *rhs = item_rhs;
20280
20281 return lhs->psymtab == rhs->psymtab;
20282}
20283
20284/* Helper struct for building the address table. */
20285struct addrmap_index_data
20286{
20287 struct objfile *objfile;
20288 struct obstack *addr_obstack;
20289 htab_t cu_index_htab;
20290
20291 /* Non-zero if the previous_* fields are valid.
20292 We can't write an entry until we see the next entry (since it is only then
20293 that we know the end of the entry). */
20294 int previous_valid;
20295 /* Index of the CU in the table of all CUs in the index file. */
20296 unsigned int previous_cu_index;
0963b4bd 20297 /* Start address of the CU. */
0a5429f6
DE
20298 CORE_ADDR previous_cu_start;
20299};
20300
20301/* Write an address entry to OBSTACK. */
b89be57b 20302
9291a0cd 20303static void
0a5429f6
DE
20304add_address_entry (struct objfile *objfile, struct obstack *obstack,
20305 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20306{
0a5429f6 20307 offset_type cu_index_to_write;
9291a0cd
TT
20308 char addr[8];
20309 CORE_ADDR baseaddr;
20310
20311 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20312
0a5429f6
DE
20313 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20314 obstack_grow (obstack, addr, 8);
20315 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20316 obstack_grow (obstack, addr, 8);
20317 cu_index_to_write = MAYBE_SWAP (cu_index);
20318 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20319}
20320
20321/* Worker function for traversing an addrmap to build the address table. */
20322
20323static int
20324add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20325{
20326 struct addrmap_index_data *data = datap;
20327 struct partial_symtab *pst = obj;
0a5429f6
DE
20328
20329 if (data->previous_valid)
20330 add_address_entry (data->objfile, data->addr_obstack,
20331 data->previous_cu_start, start_addr,
20332 data->previous_cu_index);
20333
20334 data->previous_cu_start = start_addr;
20335 if (pst != NULL)
20336 {
20337 struct psymtab_cu_index_map find_map, *map;
20338 find_map.psymtab = pst;
20339 map = htab_find (data->cu_index_htab, &find_map);
20340 gdb_assert (map != NULL);
20341 data->previous_cu_index = map->cu_index;
20342 data->previous_valid = 1;
20343 }
20344 else
20345 data->previous_valid = 0;
20346
20347 return 0;
20348}
20349
20350/* Write OBJFILE's address map to OBSTACK.
20351 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20352 in the index file. */
20353
20354static void
20355write_address_map (struct objfile *objfile, struct obstack *obstack,
20356 htab_t cu_index_htab)
20357{
20358 struct addrmap_index_data addrmap_index_data;
20359
20360 /* When writing the address table, we have to cope with the fact that
20361 the addrmap iterator only provides the start of a region; we have to
20362 wait until the next invocation to get the start of the next region. */
20363
20364 addrmap_index_data.objfile = objfile;
20365 addrmap_index_data.addr_obstack = obstack;
20366 addrmap_index_data.cu_index_htab = cu_index_htab;
20367 addrmap_index_data.previous_valid = 0;
20368
20369 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20370 &addrmap_index_data);
20371
20372 /* It's highly unlikely the last entry (end address = 0xff...ff)
20373 is valid, but we should still handle it.
20374 The end address is recorded as the start of the next region, but that
20375 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20376 anyway. */
20377 if (addrmap_index_data.previous_valid)
20378 add_address_entry (objfile, obstack,
20379 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20380 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20381}
20382
156942c7
DE
20383/* Return the symbol kind of PSYM. */
20384
20385static gdb_index_symbol_kind
20386symbol_kind (struct partial_symbol *psym)
20387{
20388 domain_enum domain = PSYMBOL_DOMAIN (psym);
20389 enum address_class aclass = PSYMBOL_CLASS (psym);
20390
20391 switch (domain)
20392 {
20393 case VAR_DOMAIN:
20394 switch (aclass)
20395 {
20396 case LOC_BLOCK:
20397 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20398 case LOC_TYPEDEF:
20399 return GDB_INDEX_SYMBOL_KIND_TYPE;
20400 case LOC_COMPUTED:
20401 case LOC_CONST_BYTES:
20402 case LOC_OPTIMIZED_OUT:
20403 case LOC_STATIC:
20404 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20405 case LOC_CONST:
20406 /* Note: It's currently impossible to recognize psyms as enum values
20407 short of reading the type info. For now punt. */
20408 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20409 default:
20410 /* There are other LOC_FOO values that one might want to classify
20411 as variables, but dwarf2read.c doesn't currently use them. */
20412 return GDB_INDEX_SYMBOL_KIND_OTHER;
20413 }
20414 case STRUCT_DOMAIN:
20415 return GDB_INDEX_SYMBOL_KIND_TYPE;
20416 default:
20417 return GDB_INDEX_SYMBOL_KIND_OTHER;
20418 }
20419}
20420
9291a0cd 20421/* Add a list of partial symbols to SYMTAB. */
b89be57b 20422
9291a0cd
TT
20423static void
20424write_psymbols (struct mapped_symtab *symtab,
987d643c 20425 htab_t psyms_seen,
9291a0cd
TT
20426 struct partial_symbol **psymp,
20427 int count,
987d643c
TT
20428 offset_type cu_index,
20429 int is_static)
9291a0cd
TT
20430{
20431 for (; count-- > 0; ++psymp)
20432 {
156942c7
DE
20433 struct partial_symbol *psym = *psymp;
20434 void **slot;
987d643c 20435
156942c7 20436 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20437 error (_("Ada is not currently supported by the index"));
987d643c 20438
987d643c 20439 /* Only add a given psymbol once. */
156942c7 20440 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20441 if (!*slot)
20442 {
156942c7
DE
20443 gdb_index_symbol_kind kind = symbol_kind (psym);
20444
20445 *slot = psym;
20446 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20447 is_static, kind, cu_index);
987d643c 20448 }
9291a0cd
TT
20449 }
20450}
20451
20452/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20453 exception if there is an error. */
b89be57b 20454
9291a0cd
TT
20455static void
20456write_obstack (FILE *file, struct obstack *obstack)
20457{
20458 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20459 file)
20460 != obstack_object_size (obstack))
20461 error (_("couldn't data write to file"));
20462}
20463
20464/* Unlink a file if the argument is not NULL. */
b89be57b 20465
9291a0cd
TT
20466static void
20467unlink_if_set (void *p)
20468{
20469 char **filename = p;
20470 if (*filename)
20471 unlink (*filename);
20472}
20473
1fd400ff
TT
20474/* A helper struct used when iterating over debug_types. */
20475struct signatured_type_index_data
20476{
20477 struct objfile *objfile;
20478 struct mapped_symtab *symtab;
20479 struct obstack *types_list;
987d643c 20480 htab_t psyms_seen;
1fd400ff
TT
20481 int cu_index;
20482};
20483
20484/* A helper function that writes a single signatured_type to an
20485 obstack. */
b89be57b 20486
1fd400ff
TT
20487static int
20488write_one_signatured_type (void **slot, void *d)
20489{
20490 struct signatured_type_index_data *info = d;
20491 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 20492 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
20493 gdb_byte val[8];
20494
20495 write_psymbols (info->symtab,
987d643c 20496 info->psyms_seen,
3e43a32a
MS
20497 info->objfile->global_psymbols.list
20498 + psymtab->globals_offset,
987d643c
TT
20499 psymtab->n_global_syms, info->cu_index,
20500 0);
1fd400ff 20501 write_psymbols (info->symtab,
987d643c 20502 info->psyms_seen,
3e43a32a
MS
20503 info->objfile->static_psymbols.list
20504 + psymtab->statics_offset,
987d643c
TT
20505 psymtab->n_static_syms, info->cu_index,
20506 1);
1fd400ff 20507
b64f50a1
JK
20508 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20509 entry->per_cu.offset.sect_off);
1fd400ff 20510 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20511 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20512 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20513 obstack_grow (info->types_list, val, 8);
20514 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20515 obstack_grow (info->types_list, val, 8);
20516
20517 ++info->cu_index;
20518
20519 return 1;
20520}
20521
95554aad
TT
20522/* Recurse into all "included" dependencies and write their symbols as
20523 if they appeared in this psymtab. */
20524
20525static void
20526recursively_write_psymbols (struct objfile *objfile,
20527 struct partial_symtab *psymtab,
20528 struct mapped_symtab *symtab,
20529 htab_t psyms_seen,
20530 offset_type cu_index)
20531{
20532 int i;
20533
20534 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20535 if (psymtab->dependencies[i]->user != NULL)
20536 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20537 symtab, psyms_seen, cu_index);
20538
20539 write_psymbols (symtab,
20540 psyms_seen,
20541 objfile->global_psymbols.list + psymtab->globals_offset,
20542 psymtab->n_global_syms, cu_index,
20543 0);
20544 write_psymbols (symtab,
20545 psyms_seen,
20546 objfile->static_psymbols.list + psymtab->statics_offset,
20547 psymtab->n_static_syms, cu_index,
20548 1);
20549}
20550
9291a0cd 20551/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20552
9291a0cd
TT
20553static void
20554write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20555{
20556 struct cleanup *cleanup;
20557 char *filename, *cleanup_filename;
1fd400ff
TT
20558 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20559 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20560 int i;
20561 FILE *out_file;
20562 struct mapped_symtab *symtab;
20563 offset_type val, size_of_contents, total_len;
20564 struct stat st;
987d643c 20565 htab_t psyms_seen;
0a5429f6
DE
20566 htab_t cu_index_htab;
20567 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20568
b4f2f049 20569 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20570 return;
b4f2f049 20571
9291a0cd
TT
20572 if (dwarf2_per_objfile->using_index)
20573 error (_("Cannot use an index to create the index"));
20574
8b70b953
TT
20575 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20576 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20577
9291a0cd 20578 if (stat (objfile->name, &st) < 0)
7e17e088 20579 perror_with_name (objfile->name);
9291a0cd
TT
20580
20581 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20582 INDEX_SUFFIX, (char *) NULL);
20583 cleanup = make_cleanup (xfree, filename);
20584
20585 out_file = fopen (filename, "wb");
20586 if (!out_file)
20587 error (_("Can't open `%s' for writing"), filename);
20588
20589 cleanup_filename = filename;
20590 make_cleanup (unlink_if_set, &cleanup_filename);
20591
20592 symtab = create_mapped_symtab ();
20593 make_cleanup (cleanup_mapped_symtab, symtab);
20594
20595 obstack_init (&addr_obstack);
20596 make_cleanup_obstack_free (&addr_obstack);
20597
20598 obstack_init (&cu_list);
20599 make_cleanup_obstack_free (&cu_list);
20600
1fd400ff
TT
20601 obstack_init (&types_cu_list);
20602 make_cleanup_obstack_free (&types_cu_list);
20603
987d643c
TT
20604 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20605 NULL, xcalloc, xfree);
96408a79 20606 make_cleanup_htab_delete (psyms_seen);
987d643c 20607
0a5429f6
DE
20608 /* While we're scanning CU's create a table that maps a psymtab pointer
20609 (which is what addrmap records) to its index (which is what is recorded
20610 in the index file). This will later be needed to write the address
20611 table. */
20612 cu_index_htab = htab_create_alloc (100,
20613 hash_psymtab_cu_index,
20614 eq_psymtab_cu_index,
20615 NULL, xcalloc, xfree);
96408a79 20616 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20617 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20618 xmalloc (sizeof (struct psymtab_cu_index_map)
20619 * dwarf2_per_objfile->n_comp_units);
20620 make_cleanup (xfree, psymtab_cu_index_map);
20621
20622 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20623 work here. Also, the debug_types entries do not appear in
20624 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20625 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20626 {
3e43a32a
MS
20627 struct dwarf2_per_cu_data *per_cu
20628 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20629 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20630 gdb_byte val[8];
0a5429f6
DE
20631 struct psymtab_cu_index_map *map;
20632 void **slot;
9291a0cd 20633
95554aad
TT
20634 if (psymtab->user == NULL)
20635 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20636
0a5429f6
DE
20637 map = &psymtab_cu_index_map[i];
20638 map->psymtab = psymtab;
20639 map->cu_index = i;
20640 slot = htab_find_slot (cu_index_htab, map, INSERT);
20641 gdb_assert (slot != NULL);
20642 gdb_assert (*slot == NULL);
20643 *slot = map;
9291a0cd 20644
b64f50a1
JK
20645 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20646 per_cu->offset.sect_off);
9291a0cd 20647 obstack_grow (&cu_list, val, 8);
e254ef6a 20648 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20649 obstack_grow (&cu_list, val, 8);
20650 }
20651
0a5429f6
DE
20652 /* Dump the address map. */
20653 write_address_map (objfile, &addr_obstack, cu_index_htab);
20654
1fd400ff
TT
20655 /* Write out the .debug_type entries, if any. */
20656 if (dwarf2_per_objfile->signatured_types)
20657 {
20658 struct signatured_type_index_data sig_data;
20659
20660 sig_data.objfile = objfile;
20661 sig_data.symtab = symtab;
20662 sig_data.types_list = &types_cu_list;
987d643c 20663 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20664 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20665 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20666 write_one_signatured_type, &sig_data);
20667 }
20668
156942c7
DE
20669 /* Now that we've processed all symbols we can shrink their cu_indices
20670 lists. */
20671 uniquify_cu_indices (symtab);
20672
9291a0cd
TT
20673 obstack_init (&constant_pool);
20674 make_cleanup_obstack_free (&constant_pool);
20675 obstack_init (&symtab_obstack);
20676 make_cleanup_obstack_free (&symtab_obstack);
20677 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20678
20679 obstack_init (&contents);
20680 make_cleanup_obstack_free (&contents);
1fd400ff 20681 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20682 total_len = size_of_contents;
20683
20684 /* The version number. */
796a7ff8 20685 val = MAYBE_SWAP (8);
9291a0cd
TT
20686 obstack_grow (&contents, &val, sizeof (val));
20687
20688 /* The offset of the CU list from the start of the file. */
20689 val = MAYBE_SWAP (total_len);
20690 obstack_grow (&contents, &val, sizeof (val));
20691 total_len += obstack_object_size (&cu_list);
20692
1fd400ff
TT
20693 /* The offset of the types CU list from the start of the file. */
20694 val = MAYBE_SWAP (total_len);
20695 obstack_grow (&contents, &val, sizeof (val));
20696 total_len += obstack_object_size (&types_cu_list);
20697
9291a0cd
TT
20698 /* The offset of the address table from the start of the file. */
20699 val = MAYBE_SWAP (total_len);
20700 obstack_grow (&contents, &val, sizeof (val));
20701 total_len += obstack_object_size (&addr_obstack);
20702
20703 /* The offset of the symbol table from the start of the file. */
20704 val = MAYBE_SWAP (total_len);
20705 obstack_grow (&contents, &val, sizeof (val));
20706 total_len += obstack_object_size (&symtab_obstack);
20707
20708 /* The offset of the constant pool from the start of the file. */
20709 val = MAYBE_SWAP (total_len);
20710 obstack_grow (&contents, &val, sizeof (val));
20711 total_len += obstack_object_size (&constant_pool);
20712
20713 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20714
20715 write_obstack (out_file, &contents);
20716 write_obstack (out_file, &cu_list);
1fd400ff 20717 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20718 write_obstack (out_file, &addr_obstack);
20719 write_obstack (out_file, &symtab_obstack);
20720 write_obstack (out_file, &constant_pool);
20721
20722 fclose (out_file);
20723
20724 /* We want to keep the file, so we set cleanup_filename to NULL
20725 here. See unlink_if_set. */
20726 cleanup_filename = NULL;
20727
20728 do_cleanups (cleanup);
20729}
20730
90476074
TT
20731/* Implementation of the `save gdb-index' command.
20732
20733 Note that the file format used by this command is documented in the
20734 GDB manual. Any changes here must be documented there. */
11570e71 20735
9291a0cd
TT
20736static void
20737save_gdb_index_command (char *arg, int from_tty)
20738{
20739 struct objfile *objfile;
20740
20741 if (!arg || !*arg)
96d19272 20742 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20743
20744 ALL_OBJFILES (objfile)
20745 {
20746 struct stat st;
20747
20748 /* If the objfile does not correspond to an actual file, skip it. */
20749 if (stat (objfile->name, &st) < 0)
20750 continue;
20751
20752 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20753 if (dwarf2_per_objfile)
20754 {
20755 volatile struct gdb_exception except;
20756
20757 TRY_CATCH (except, RETURN_MASK_ERROR)
20758 {
20759 write_psymtabs_to_index (objfile, arg);
20760 }
20761 if (except.reason < 0)
20762 exception_fprintf (gdb_stderr, except,
20763 _("Error while writing index for `%s': "),
20764 objfile->name);
20765 }
20766 }
dce234bc
PP
20767}
20768
9291a0cd
TT
20769\f
20770
9eae7c52
TT
20771int dwarf2_always_disassemble;
20772
20773static void
20774show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20775 struct cmd_list_element *c, const char *value)
20776{
3e43a32a
MS
20777 fprintf_filtered (file,
20778 _("Whether to always disassemble "
20779 "DWARF expressions is %s.\n"),
9eae7c52
TT
20780 value);
20781}
20782
900e11f9
JK
20783static void
20784show_check_physname (struct ui_file *file, int from_tty,
20785 struct cmd_list_element *c, const char *value)
20786{
20787 fprintf_filtered (file,
20788 _("Whether to check \"physname\" is %s.\n"),
20789 value);
20790}
20791
6502dd73
DJ
20792void _initialize_dwarf2_read (void);
20793
20794void
20795_initialize_dwarf2_read (void)
20796{
96d19272
JK
20797 struct cmd_list_element *c;
20798
dce234bc 20799 dwarf2_objfile_data_key
c1bd65d0 20800 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20801
1bedd215
AC
20802 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20803Set DWARF 2 specific variables.\n\
20804Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20805 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20806 0/*allow-unknown*/, &maintenance_set_cmdlist);
20807
1bedd215
AC
20808 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20809Show DWARF 2 specific variables\n\
20810Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20811 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20812 0/*allow-unknown*/, &maintenance_show_cmdlist);
20813
20814 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20815 &dwarf2_max_cache_age, _("\
20816Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20817Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20818A higher limit means that cached compilation units will be stored\n\
20819in memory longer, and more total memory will be used. Zero disables\n\
20820caching, which can slow down startup."),
2c5b56ce 20821 NULL,
920d2a44 20822 show_dwarf2_max_cache_age,
2c5b56ce 20823 &set_dwarf2_cmdlist,
ae038cb0 20824 &show_dwarf2_cmdlist);
d97bc12b 20825
9eae7c52
TT
20826 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20827 &dwarf2_always_disassemble, _("\
20828Set whether `info address' always disassembles DWARF expressions."), _("\
20829Show whether `info address' always disassembles DWARF expressions."), _("\
20830When enabled, DWARF expressions are always printed in an assembly-like\n\
20831syntax. When disabled, expressions will be printed in a more\n\
20832conversational style, when possible."),
20833 NULL,
20834 show_dwarf2_always_disassemble,
20835 &set_dwarf2_cmdlist,
20836 &show_dwarf2_cmdlist);
20837
45cfd468
DE
20838 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20839Set debugging of the dwarf2 reader."), _("\
20840Show debugging of the dwarf2 reader."), _("\
20841When enabled, debugging messages are printed during dwarf2 reading\n\
20842and symtab expansion."),
20843 NULL,
20844 NULL,
20845 &setdebuglist, &showdebuglist);
20846
ccce17b0 20847 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20848Set debugging of the dwarf2 DIE reader."), _("\
20849Show debugging of the dwarf2 DIE reader."), _("\
20850When enabled (non-zero), DIEs are dumped after they are read in.\n\
20851The value is the maximum depth to print."),
ccce17b0
YQ
20852 NULL,
20853 NULL,
20854 &setdebuglist, &showdebuglist);
9291a0cd 20855
900e11f9
JK
20856 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20857Set cross-checking of \"physname\" code against demangler."), _("\
20858Show cross-checking of \"physname\" code against demangler."), _("\
20859When enabled, GDB's internal \"physname\" code is checked against\n\
20860the demangler."),
20861 NULL, show_check_physname,
20862 &setdebuglist, &showdebuglist);
20863
e615022a
DE
20864 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20865 no_class, &use_deprecated_index_sections, _("\
20866Set whether to use deprecated gdb_index sections."), _("\
20867Show whether to use deprecated gdb_index sections."), _("\
20868When enabled, deprecated .gdb_index sections are used anyway.\n\
20869Normally they are ignored either because of a missing feature or\n\
20870performance issue.\n\
20871Warning: This option must be enabled before gdb reads the file."),
20872 NULL,
20873 NULL,
20874 &setlist, &showlist);
20875
96d19272 20876 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20877 _("\
fc1a9d6e 20878Save a gdb-index file.\n\
11570e71 20879Usage: save gdb-index DIRECTORY"),
96d19272
JK
20880 &save_cmdlist);
20881 set_cmd_completer (c, filename_completer);
f1e6e072
TT
20882
20883 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20884 &dwarf2_locexpr_funcs);
20885 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20886 &dwarf2_loclist_funcs);
20887
20888 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20889 &dwarf2_block_frame_base_locexpr_funcs);
20890 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20891 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 20892}
This page took 3.742048 seconds and 4 git commands to generate.